Commit Graph

454 Commits

Author SHA1 Message Date
dongjiuzhu1
18819b6b24 sched/task: close file descriptor with O_CLOEXEC before active task or exec
VELAPLATFO-18473

refs:
https://man7.org/linux/man-pages/man2/fcntl.2.html
If the FD_CLOEXEC bit is set, the file descriptor will automatically
be closed during a successful execve(2).
(If the execve(2) fails, the file descriptor is left open.)

modify:
1. Ensure that the child task copies all fds of the parent task,
   including those with O_CLOEXE.
2. Make sure spawn_file_action is executed under fd with O_CLOEXEC,
   otherwise it will fail.
3. When a new task is activated or exec is called, close all fds
   with O_CLOEXEC flags.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-10-27 21:15:07 +08:00
dongjiuzhu1
4068f11129 binfmt/elf: Select ARCH_USE_TEXT_HEAP if ARCH_HAVE_TEXT_HEAP
Using up_textheap_memalign to allocate memory if arch support textheap
for loading section.

The default system heap does not support execution permissions,
so up_textheap_memalign allocation is required.

this patch can fix issue about #11043

update esp32 elf config:
remove -CONFIG_ARCH_USE_TEXT_HEAP=y becuase ARCH_CHIP_ESP32 select
ARCH_HAVE_TEXT_HEAP

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-10-27 15:42:02 +09: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
Ville Juven
ab78e3817a sched/task_[posix]spawn: Simplify how spawn attributes are handled
Handle task spawn attributes as task spawn file actions are handled.

Why? This removes the need for sched_lock() when the task is being
spawned. When loading the new task from a file the scheduler can be
locked for a VERY LONG time, in the order of hundreds of milliseconds!

This is unacceptable for real time operation.

Also fixes a latent bug in exec_module, spawn_file_actions is executed
at a bad location; when CONFIG_ARCH_ADDRENV=y actions will point to the
new process's address environment (as it is temporarily instantiated at
that point). Fix this by moving it to after addrenv_restore.
2023-10-25 11:55:44 -03:00
wanggang26
e930476b4b enable O_CLOEXEC explicit
Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2023-09-22 13:51:00 +08:00
yangyalei
3017cc4402 Exec: Support run exec in current task
There is a problem when vfork() calls execv() (or execl()) to start a new application:
When the parent thread calls vfork() it receives and gets the pid of the vforked task,
and not the pid of the desired execv'ed application.
see issue #3334

Signed-off-by: yangyalei <yangyalei@xiaomi.com>
2023-09-20 16:53:30 +08:00
Ville Juven
0ef735f93a binfmt/binfmt_execmodule: Copy filename if CONFIG_BUILD_KERNEL and argv=NULL
The 'filename' parameter comes from user space and cannot be accessed
after calling ret = addrenv_select(binp->addrenv, &binp->oldenv); as
it changes the address environment and 'filename' points to who knows
where. In this case, calling nxtask_init(filename...) will cause a crash.

Solve this by making a local copy before changing address environment IF
argv = NULL. Why ? Because argv[0] contains the process name in this case
and the argument vector is already copied into kernel memory, thus
passing argv[0] to nxtask_init(argv[0]...) is safe.
2023-09-16 14:41:52 +08:00
Masayuki Ishikawa
1b97c05ab5 Revert "Exec: Support run exec in current task"
This reverts commit 670c245ff2.
2023-09-16 07:22:32 +03:00
yangyalei
670c245ff2 Exec: Support run exec in current task
Fix the problem when vfork() calls execv() (or execl()) to start a new application:
When the parent thread calls vfork() it receives and gets the pid of the vforked task,
and not the pid of the desired execv'ed application.

issue #3334

Signed-off-by: yangyalei <yangyalei@xiaomi.com>
2023-09-14 22:37:44 +08:00
simbit18
b3973496cd Fix Kconfig style
Remove spaces from Kconfig
Add comments
2023-09-13 21:39:49 +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
xuxin19
f2f0d7fbad cmake:fix drivers build block during cmake reforming
Signed-off-by: xuxin19 <xuxin19@xiaomi.com>
2023-08-16 22:38:52 +08:00
fangxinyong
903e87a7bd builtin: support uid/gid config for binfs app
Implement I_SUID/I_SGID feature for binfs in the POSIX compliant way.
If set-user-ID bit is set in the file permissions, then the effective
user ID of process shall be set to UID of the new process image file.

test case:
hello example emulates to set uid and file set-user-ID bit, and call
geteuid and getegid API.
UID  = 2000
GID  = 3000
MODE = 06555

nsh> ls -l /bin/hello
 -r-sr-sr-x    2000    3000       0 hello
nsh> hello
geteuid:2000
getegid:3000

Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
2023-08-14 01:37:00 +08: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
b185f8d889 binfmt: add enter_critical_section
adding enter_critical_section to ensure non preemption in smp
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-08-10 20:46:59 +08:00
Xiang Xiao
6b4e5c0d15 binfmt: Change the default of BINFMT_DISABLE to DEFAULT_SMALL
to optimize the image size when and DEFAULT_SMALL is enabled
and refresh the defconfig in boards/

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-08-10 11:35:41 +03: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
hujun5
4109908013 binfmt: remove sched_[un]lock
In the current usage mode, multiple tasks will not modify g_binfmts simultaneously

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-07-26 09:43:51 -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
ebcb03dce9 libc/symtab: Don't include symtab.h in the header files
to unify the inclusion of symtab.h only from the source files

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-10 23:03:17 +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
wanggang26
57d521cc59 binfmt: Immediately exit from elf_loadbinary if elf format isn't supported 2023-07-03 00:52:16 +08:00
Stuart Ianna
6492f0172e binfmt/elf: Allow the userspace ELF type to be defined by board configuration.
This change allows boards to define an additional kconfig option, which specifies the final link format of application executables.

By selecting `CONFIG_BINFMT_ELF_RELOCATABLE`, and providing an appropriate linker script, applications can be fully linked, removing the need to process relocations.
2023-06-28 15:16:28 +08:00
Xiang Xiao
a8e0a5faa4 sched: Remove the unnecessary cast from pid_t to int
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-06-19 11:37:34 +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
Ville Juven
a636edcbe4 addrenv/kstack: Allocate the kernel stack before initializing tcb
This is preparation to use kernel stack for everything when the user
process enters the kernel. Now the user stack is in use when the user
process runs a system call, which might not be the safest option.
2023-06-09 13:53:27 +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
chao an
507c8145a9 sched/spawn: remove spawn proxy thread to simplify task/posix_spawn()
The spawn proxy thread is a special existence in NuttX, usually some developers
spend a lot of time on stack overflow of spawn proxy thread:

https://github.com/apache/nuttx/issues/9046
https://github.com/apache/nuttx/pull/9081

In order to avoid similar issues, this PR will remove spawn proxy thread to simplify
the process of task/posix_spawn().

1. Postpone the related processing of spawn file actions until after task_init()
2. Delete the temporary thread of spawn proxy and related global variables

Signed-off-by: chao an <anchao@xiaomi.com>
2023-04-27 17:35:58 +08:00
chao an
e3ca288087 binfmt/execmodule: correct destroy flow
1. stack_alloc_ptr should be freed in kernel mode
2. replace nxsched_release_tcb() to nxtask_uninit() to remove tcb from inactive list

Signed-off-by: chao an <anchao@xiaomi.com>
2023-04-27 00:39:38 +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
Ville Juven
b982c1747b sched/addrenv: Miscellaneous clean-up and fixes
- Remove the temporary "saved" variable when temporarily changing MMU
  mappings to access another process's memory. The fact that it has an
  address environment is enough to make the choice
- Restore nxflat_addrenv_restore-macro. It was accidentally lost when
  the address environment handling was re-factored.
2023-04-15 13:21:48 +09: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