Commit Graph

179 Commits

Author SHA1 Message Date
xuxingliang 7044b10c88 task: use get_task_name where possible
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-10-01 20:38:06 +08:00
anjiahao 29e4e71167 modlib/dlfcn:unify same code
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-09-29 15:06:54 +08:00
dongjiuzhu1 1f1d90de1c binfmt/modlib: support loading each sections to different memory for Relocate object
The feature depends on ARCH_USE_SEPARATED_SECTION
the different memory area has different access speed and cache
capability, so the arch can custom allocate them based on
section names to achieve performance optimization

test:
sim:elf
sim:sotest

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2024-09-29 15:06:54 +08:00
hujun5 908df725ad arch: use up_current_regs/up_set_current_regs replace CURRENT_REGS
reason:
1 On different architectures, we can utilize more optimized strategies
  to implement up_current_regs/up_set_current_regs.
eg. use interrupt registersor percpu registers.

code size
before
    text    data     bss     dec     hex filename
 262848   49985   63893  376726   5bf96 nuttx

after
       text    data     bss     dec     hex filename
 262844   49985   63893  376722   5bf92 nuttx

size change -4

Configuring NuttX and compile:
$ ./tools/configure.sh -l qemu-armv8a:nsh_smp
$ make
Running with qemu
$ qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic \
   -machine virt,virtualization=on,gic-version=3 \
   -net none -chardev stdio,id=con,mux=on -serial chardev:con \
   -mon chardev=con,mode=readline -kernel ./nuttx

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2024-09-13 23:18:58 +08:00
Alin Jerpelea 61a4ab8966 binfmt: migrate to SPDX identifier
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2024-09-10 11:34:18 +08:00
Petro Karashchenko 1528b8dcca nuttx: resolve various 'FAR' and 'CODE' issues
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2024-08-26 10:21:03 +08:00
Petro Karashchenko d499ac9d58 nuttx: fix multiple 'FAR', 'CODE' and style issues
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2024-08-25 19:22:15 +08:00
cuiziwei e21885b84a nuttx/boards:Uniform initialization format for init_array.
(1) Keep the `.init_array` and `.ctors` symbols and sort them according to their initialization priority.
(2) Exclude symbols ending with crtend.* and crtbegin.* to support c++
application.if we not exclude crtend.* crtbegin.* frame_dummy will be
added when enable any c++ application with global variables, this symbol
execution is problematic, removing it does not affect the application.

Signed-off-by: cuiziwei <cuiziwei@xiaomi.com>
2024-08-19 19:48:32 +08:00
anjiahao 5f4c1721ff coredump:support coredump aligned access registers
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-08-12 02:02:25 +08:00
Ville Juven 9da9d3ea8c binfmt/elf_loadfile: Set sh_addr even if SHF_ALLOC == 0
Set sh_addr for regions that are not allocated. Some relocations might
depend on this.

The fault in my case occurs when setting CONFIG_HAVE_CXX=y. In this case,
the .ctor and .dtor sections do not get allocated, but the crt code
depends on linker defined symbols _sctors/_ectors etc. These generate PC
relative relocations and thus, the .ctor and .dtor output sections need
an output VMA even though nothing is there. Otherwise the relocations will
point to god knows where (in my case to address 0).

The problem results in full system crash later:
elf_symvalue: Other: 00000000+00000001=00000001
up_relocateadd: PCREL_HI20 at c00002dc [00000417] to sym=0x80409e80 st_value=1
_calc_imm: offset=-3221226203: hi=-786432 lo=-731
up_relocateadd: ERROR: PCREL_HI20 at c00002dc bad:ffffffff40000000
elf_relocateadd: ERROR: Section 2 reloc 52: Relocation failed: -22

The RISC-V elf64 linker does not like the uninitialized PC relative
relocation entries, as the relocation offset cannot be reached with
with the RV64 instruction set.

More about this issue can be found here:
https://github.com/apache/nuttx/pull/11322
2024-08-07 08:45:50 +08:00
Tiago Medicci Serrano 84d39a8d9a binfmt/libelf: Enable ELF loader if text heap read is word-aligned
The ELF loader needs to load the app into the memory before
executing it from the same location. As expected, this memory space
should be able to execute code. For architectures containing data
and instruction buses, the instruction bus may not be able to be
accessed in a non-aligned way, which is usually required when
copying data to that location. Eventually, this same memory space
can be accessed through the data bus, using different address
ranges. This commit enables accessing the memory through the data
bus to copy the app's data before executing it when
`CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ` is enabled.
2024-07-31 02:33:12 +08:00
chao an e456c88c09 Revert "sched: replace some global variables to macro"
sched implementation not depends on macro abstraction, so revert below commit:

This reverts commit 4e62d0005a
This reverts commit 0f0c370520
This reverts commit ad0efd04ee

Signed-off-by: chao an <anchao@lixiang.com>
2024-06-06 22:00:25 +08:00
Mingjie Shen 99109b8d79 all: Fix accessing uninitialized local variables
Prior to this commit, in elf_emit() and elf_emit_align(),
ret was uninitialized if total was 0.

Signed-off-by: Mingjie Shen <shen497@purdue.edu>
2024-04-04 11:51:54 +08:00
chao an 4e62d0005a sched: replace some global variables to macro
replace to macro will help to extend the scheduling implementation

Signed-off-by: chao an <anchao@lixiang.com>
2024-03-21 18:51:25 +08:00
Yanfeng Liu 675f775494 log messages of binfmt are very much.
this is to adjust levels of a few of them based on debugging
experiences. These non-critical logs were marked as errors. So we
likely can adjust them as warnings so that real errors can
stand out easily.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2023-12-29 17:36:47 +08:00
Ville Juven 996625ec58 riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly
There is a problem with the current elf loader for risc-v: when a pair of
PCREL_HI20 / LO12 relocations are encountered, it is assumed that these
will follow each other immediately, as follows:

label:
	auipc      a0, %pcrel_hi(symbol)    // R_RISCV_PCREL_HI20
	load/store a0, %pcrel_lo(label)(a0) // R_RISCV_PCREL_LO12_I/S

With this assumption, the hi/lo relocations are both done when a hi20
relocation entry is encountered, first to the current instruction (addr)
and to the next instruction (addr + 4).

However, this assumption is wrong. There is nothing in the elf relocation
specification[1] that mandates this. Thus, the hi/lo relocation always
needs to first fixup the hi-part, and when the lo-part is encountered, it
needs to find the corresponding hi relocation entry, via the given "label".
This necessitates (re-)visiting the relocation entries for the current
section as well as looking for "label" in the symbol table.

The NuttX elf loader does not allow such operations to be done in the
machine specific part, so this patch fixes the relocation issue by
introducing an architecture specific cache for the hi20 relocation and
symbol table entries. When a lo12 relocation is encountered, the cache
can be consulted to find the hi20 part.

[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc
2023-12-12 17:32:36 -08:00
anjiahao 90517b9f11 coredump:support arm64 coredump
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2023-12-08 21:33:03 -03:00
dongjiuzhu1 e88a36fa92 libs/modlib: Adding architecture-specific memory allocator for dynamic data loading
Arch can specific the memory allocator for data to optimize access speed.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-10-26 01:53:38 +08:00
wanggang26 e930476b4b enable O_CLOEXEC explicit
Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2023-09-22 13:51:00 +08:00
wangjianyu3 0627b9970e binfmt: The program headers are optional.
Fix problems mentioned in:
https://github.com/apache/nuttx/pull/10462

Brief:
rv-virt:knsh64, qemu-7.2.4
qemu-system-riscv64 -semihosting -nographic -cpu rv64 -smp 8 -M virt,aclint=on -bios none -kernel nuttx
```
[    0.006000] _assert: Current Version: NuttX  12.0.0 8a13da322d Sep  4 2023 14:31:15 risc-v
[    0.006000] _assert: Assertion failed : at file: init/nx_bringup.c:302 task: Idle_Task 0x800017fc
```

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2023-09-05 12:31:31 +08:00
wangjianyu3 8a13da322d binfmt: Support arch copy section by self for dynamic code loading
This option enables architecture-specific memory copy for dynamic code loading.

For example, Ambiq has MRAM regions for instruction which can't load by
the memcpy directly.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2023-09-04 12:23:26 +08:00
wangjianyu3 c749e4bfbd binfmt: ELF support load to LMA
Load all sections to LMA not VMA, so the startup code(e.g. start.S) need
relocate .data section to the final address(VMA) and zero .bss section by self.

For example, SiFli and Actions: Background: Device with small sram,
Bootloader run in sram and psram, need boot to Application, with memory overlap
and without XIP. VMA of .data is in "psram" and LMA in "rom", if not enable
`ELF_LOADTO_LMA`, ELF loader will load the section to VMA (will fill bootloader
itself).

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2023-09-04 12:23:26 +08:00
chao an 664927c86e mm/alloc: remove all unnecessary cast for alloc
Fix the minor style issue and remove unnecessary cast

Signed-off-by: chao an <anchao@xiaomi.com>
2023-08-30 14:34:20 +08:00
Xiang Xiao cb8df39207 binfmt/elf: Fix the minor style issue
and remove the unused macros and unnecessary cast

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-08-27 18:46:40 -03:00
fangxinyong 2d73e86b47 binfmt: support euid of process set from the file system
From posix spec, if set-user-ID bit is set in the file permissions,
then the effective user ID of the new process shall be set to the
user ID of the new process image file.
Let's ignore whether ST_NOSUID is set on the mounted file system.

https://pubs.opengroup.org/onlinepubs/007904875/functions/exec.html

test step:
hello example build as a module and call geteuid and getegid API.
then set file binary set-user-ID bit on the host.

$ chmod +s apps/bin/hello

nsh> mount -t hostfs -o fs=. /data
nsh> ls -l /data/apps/bin/hello
 -rwsrwsr-x    1000    1000    9264 /data/apps/bin/hello
nsh> /data/apps/bin/hello
geteuid:1000
getegid:1000

Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
2023-08-12 02:18:25 +08:00
hujun5 9f1cb4135b binfmt/elf: bss section should init to zero
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-07-29 06:54:25 -07:00
wanggang26 abc1cade35 binfmt/elf: Support to load ET_EXEC in flat mode
Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2023-07-28 08:35:27 -07:00
wanggang26 9d67c7b6ac binfmt/elf: both regular file and non-regular file (such as /dev/node) should be accessible
Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2023-07-28 07:52:37 -07:00
Xiang Xiao f3269a6caa sched: Rename DEBUG_TCBINFO to ARCH_HAVE_TCBINFO
and select if the arch support to define g_tcbinfo variable

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-26 09:33:38 +02:00
Xiang Xiao 879dab08a6 binfmt: Move elf_allocbuffer to elf_sectname and elf_symname
it's better to allocate the buffer just before really use it.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-16 14:17:07 +03:00
Xiang Xiao 229293f9f8 binfmt: Move [elf|nxflat]_[un]initialize to private header file
like what builtin binary format do

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-10 23:03:17 +03:00
chao an 6ee9ec7656 build: add initial cmake build system
1. Update all CMakeLists.txt to adapt to new layout
2. Fix cmake build break
3. Update all new file license
4. Fully compatible with current compilation environment(use configure.sh or cmake as you choose)

------------------

How to test

From within nuttx/. Configure:

cmake -B build -DBOARD_CONFIG=sim/nsh -GNinja
cmake -B build -DBOARD_CONFIG=sim:nsh -GNinja
cmake -B build -DBOARD_CONFIG=sabre-6quad/smp -GNinja
cmake -B build -DBOARD_CONFIG=lm3s6965-ek/qemu-flat -GNinja

(or full path in custom board) :
cmake -B build -DBOARD_CONFIG=$PWD/boards/sim/sim/sim/configs/nsh -GNinja

This uses ninja generator (install with sudo apt install ninja-build). To build:

$ cmake --build build

menuconfig:

$ cmake --build build -t menuconfig

--------------------------

2. cmake/build: reformat the cmake style by cmake-format

https://github.com/cheshirekow/cmake_format

$ pip install cmakelang

$ for i in `find -name CMakeLists.txt`;do cmake-format $i -o $i;done
$ for i in `find -name *\.cmake`;do cmake-format $i -o $i;done

Co-authored-by: Matias N <matias@protobits.dev>
Signed-off-by: chao an <anchao@xiaomi.com>
2023-07-08 13:50:48 +08:00
Xiang Xiao 8f38fa6062 symtab: Remove the unnessary inclusion of nuttx/symtab.h
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-06 09:24:02 -03:00
Xiang Xiao 1b2f37259a binfmt/elf: Replace nx_stat with file_stat
since kernel code prefer to use file_ API

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-06 09:20:24 -03:00
Xiang Xiao 7bb97f7e22 elf: Replace {0x7f, 'E', 'L', 'F'} to EI_MAGIC
to avoid the duplication of the magic number.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-06 09:15:50 -03:00
Petro Karashchenko 1b801a5bbc style: remove extra spaces and align parameters
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-06-11 12:55:29 +08:00
chao an 090a52c5fb elf/coredump: alignment stack buffer to 64 to match gdb request
Signed-off-by: chao an <anchao@xiaomi.com>
2023-05-30 23:00:23 +08:00
Stuart Ianna a05e8fd9ff binfmt/elf: Support loading fully linked executables.
The following changes make it possible for Nuttx to load binaries in ELF format which are fully linked.

The change does not include the necessary modifications to produce such binaries. In order to build an applicable binary:
 - The userspace applications linker script (`gnu-elf.ld`) needs to modified so the data and text section origin's match those setup by the address environment.
 - The makefile used, in `apps/import/Make.defs` needs to remove the `-r` LDELFFLAG.
2023-05-26 10:37:45 -03:00
chao an 78006f9824 elf/coredump: add sanity checks for stack pointer
stack pointer may be invalid value if in SMP mode, add sanity checks
to avoid invalid access

Signed-off-by: chao an <anchao@xiaomi.com>
2023-05-25 11:37:30 +08:00
chao an 24f4216066 elf/coredump: add support of dump task stack without memory segments
Signed-off-by: chao an <anchao@xiaomi.com>
2023-05-24 22:34:47 +08:00
Xiang Xiao 7990f90915 Indent the define statement by two spaces
follow the code style convention

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-05-21 09:52:08 -03:00
chao an 064c75aa92 elf/coredump: correct register offset after xcp.regs update
The offset should be calculated from pointer xcp.regs

Signed-off-by: chao an <anchao@xiaomi.com>
2023-05-18 20:13:05 +08:00
Ville Juven d48114a4b7 sched/addrenv.c: Implement re-entrancy for addrenv_select()
Store the old environment in a local context so another temporary address
environment can be selected. This can happen especially when a process
is being loaded (the new process's mappings are temporarily instantiated)
and and interrupt occurs.
2023-04-25 14:33:19 +02:00
Stuart Ianna f4dcdbec0e libelf: Support prioritized init and fini arrays.
Add a wildcard operator to gnu-elf.ld to ensure all c++ constructors
and destruction are included in partially linked application binaries.
2023-04-22 01:28:01 +08:00
Ville Juven 64d8249895 sched/addrenv, binfmt: Always allocate address environment from heap
Instead of using a volatile storage for the address environment in the
binfmt / loadinfo structures, always allocate the address environment
from kheap.

This serves two purposes:
- If the task creation fails, any kernel thread that depends on the
  address environment created during task creation will not lose their
  mappings (because they hold a reference to it)
- The current address environment variable (g_addrenv) will NEVER contain
  a stale / incorrect value
- Releasing the address environment is simplified as any pointer given
  to addrenv_drop() can be assumed to be heap memory
- Makes the kludge function addrenv_clear_current irrelevant, as the
  system will NEVER have invalid mappings any more
2023-04-22 00:43:30 +08:00
chao an 3a0fdb019d nuttx: replace all ARRAY_SIZE()/ARRAYSIZE() to nitems()
Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-09 20:05:44 +08:00
Ville Juven f4b82b6405 sched/addrenv: Remove up_addrenv_restore
The function is not relevant any longer, remove it. Also remove
save_addrenv_t, the parameter taken by up_addrenv_restore.

Implement addrenv_select() / addrenv_restore() to handle the temporary
instantiation of address environments, e.g. when a process is being
created.
2023-02-08 02:51:23 +08:00
Gustavo Henrique Nihei e6b204f438 nuttx: Use MIN/MAX definitions from "sys/param.h"
Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
2023-02-01 23:47:44 +08:00
Ville Juven fb12b6e3a9 libelf/elf_addrenv_free(): Don't instantiate address environment prior to destroying it
Same as with group_free(), there is no need to instantiate the address
environment to destroy it.

The only problem was the ARM implementation modified the L1 mappings
in up_addrenv_destroy(), which it no longer does.
2023-01-18 11:02:19 +08:00
yinshengkai 377bf97613 stream: replace the stream function pointer with a macro
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2022-11-25 21:41:51 +08:00