Skip to content

system/nxinit: register g_builtins with the kernel under PROTECTED build - #3821

Closed
JianyuWang0623 wants to merge 1 commit into
apache:masterfrom
JianyuWang0623:mars-116-apps-builtins
Closed

JianyuWang0623 wants to merge 1 commit into
apache:masterfrom
JianyuWang0623:mars-116-apps-builtins

Conversation

@JianyuWang0623

Copy link
Copy Markdown
Contributor

Summary

Under CONFIG_BUILD_PROTECTED, nxinit's init_main() runs in user space while the binfmt builtin loader and BINFS run in kernel space. The builtin application table (g_builtins/g_builtin_count) is linked only into the user blob (nuttx_user) and is never present in the kernel image. As a result, any kernel-space lookup against that table — such as the posix_spawnp() call that starts the console sh service from init.rc — fails with ENOENT, because the kernel-side g_builtins[] was never populated.

NuttX already ships the fix for this gap: include/nuttx/lib/builtin.h documents that boardctl(BOARDIOC_BUILTINS) must be called by the application layer to register the table with the kernel. This change wires that call up in init_main(), before any service is started. The block is guarded by #if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_BUILTIN) and compiled out entirely for FLAT and KERNEL builds, which are unaffected (in FLAT, there is no separate address space; in KERNEL, user-space addresses cannot meaningfully be passed across the boundary in this way).

nsh is not affected by this problem: exec_builtin() has a task_spawn() fallback path that does not require a populated kernel-side table.

Impact

Only affects CONFIG_BUILD_PROTECTED && CONFIG_BUILTIN builds. FLAT and KERNEL builds are not changed. The apache/nuttx rv-virt:pnsh configuration (already switched to nxinit on master) currently fails to start its console service with Error Starting service 'console': 2; this PR fixes that. A pending nuttx PR that switches qemu-armv7r to nxinit also depends on this change.

Testing

Host: Linux x86_64, arm-none-eabi-gcc 10.3.1

Target: qemu-armv7r:pnsh (PROTECTED build)

QEMU: 10.2.1, -M virt -cpu cortex-r5f

Before this change: boot log prints Error Starting service 'console': 2 and the console service never starts.

After this change, three consecutive runs:

nsh> ps
  TID   PID  PPID PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK            STACK    USED FILLED COMMAND
    0     0     0   0 FIFO     Kthread   - Ready    ...                                                Idle_Task
    1     0     0 192 RR       Kthread   - Waiting  Semaphore ...                                      hpwork
    3     3     0 100 RR       Task      - Waiting  Semaphore ...                                      init_main
    4     4     3 100 RR       Task      - Running  ...                                                sh

init_main (PID 3, PPID 0) and sh (PID 4, PPID 3) confirm the expected parent-child relationship.

qemu-armv7r:nsh (flat build): compiles cleanly with this change; the #if guard excludes the new code entirely.

rv-virt:pnsh: not built or run on this branch; it is known to exhibit the same failure on apache/nuttx master without this fix.

In PROTECTED builds g_builtins is linked only into the user blob, so the
kernel-side builtin table stays empty and init.rc services fail to spawn
with ENOENT. Register it via boardctl(BOARDIOC_BUILTINS) before starting
any service.

Assisted-by: OpenCode:claude-sonnet-5
Signed-off-by: wangjianyu3 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant