Skip to content

ND-500 Kernel Bootstrap Implementation

Status: ✓ COMPLETED

The ND-500 demo kernel now uses the NDIX-C assembly-in-C bootstrap pattern to properly initialize the stack before executing any function calls.

Problem Solved

Previous Issue: The kernel's start() function began with an ENTS (Enter Subroutine) instruction, which violated the ND-500 architecture requirement:

"Execution of an entry point instruction (except ENTT) not resulting from a subroutine call will cause an instruction sequence error trap condition." — ND-500 Reference Manual, Page 230

Root Cause: C functions always get compiler-generated ENTS prologue before function body, even with inline asm("init ...") statements.

Solution Implemented

Adopted the NDIX-C locore.c pattern from the production ND-500 vmunix kernel:

1. Created examples/05-c-kernel/locore.c

Assembly-in-C file with proper bootstrap sequence:

.text
.org 4                  # Start at text address 4 (after magic number)
.globl start
start:
    init    _Kstack, $20, $UPAGES*NBPG    # FIRST instruction!
    dctsb                                  # Clear data cache
    pctsb                                  # Clear program cache
    call    _kernel_main, $0               # Call C kernel entry
halt_loop:
    go halt_loop                           # Halt if main returns

Key Points:

  • start: is a label, not a C function (no ENTS prologue)
  • INIT is first instruction - sets B, TOS, SP registers
  • Uses C preprocessor (-E -DLOCORE) to expand constants, then assembles

2. Modified examples/05-c-kernel/kernel.c

Renamed entry point to avoid conflict:

// Was: void start()
// Now: void kernel_main()
void kernel_main()
{
    /* Stack already initialized by locore.c INIT instruction */
    init_kernel();
    scheduler();
    while(1);
}

3. Updated examples/05-c-kernel/Makefile

New build sequence matching NDIX-C:

# Step 1a: Preprocess locore.c (assembly-in-C pattern)
$(LOCORE_ASM): $(LOCORE_SRC)
    $(CC) -E -DLOCORE $(CFLAGS) $(LOCORE_SRC) | \
        sed -e 's/\(_[0-9]\)[  ]*:/\1:/' > $(LOCORE_ASM)

# Step 2a: Assemble locore.s
$(LOCORE_OBJ): $(LOCORE_ASM)
    $(AS) $(LOCORE_ASM) -o $(LOCORE_OBJ)

# Step 3: Link locore.o FIRST, then kernel.o
$(KERNEL): $(LOCORE_OBJ) $(OBJ)
    $(LD) $(LDFLAGS) -o $(KERNEL) $(LOCORE_OBJ) $(OBJ)

Critical: locore.o linked FIRST to position bootstrap at text segment start.

Linker flags:

  • -e start - Entry point at start label (locore.c)
  • -i - IMAGIC format (0411)
  • -x - Preserve symbols for debugging
  • -m - Generate map file

Verification

Build Output

Text size:      762 bytes (0x2fa)
Data size:      72 bytes (0x48)
BSS size:       78608 bytes (0x13310)
Entry point:    0x4

Disassembly Verification

Entry Point:  0x4

00000004: init         $3892318208,$20,$16384
0000000D: dctsb
0000000F: pctsb
00000011: call         $436207616,$0
00000017: go           $0

✓ INIT is first instruction at entry point

Debugger Test

> load examples/05-c-kernel/kernel
> set PC 4
> step 3

[INIT] Stack initialized at B=0xE8001000, TOS=0xE8005000, SP=0xE8001000
[STUB] Dctsb instruction not implemented
[STUB] Pctsb instruction not implemented

> regs
PC=00000011 FLAGS=00000000
B=E8001000
TOS=E8005000

✓ Stack properly initialized before any function calls ✓ No ENTS trap ✓ B and TOS registers set correctly

Stack Layout

Initialized by INIT instruction:

  • B (Base): 0xE8001000 - Stack bottom (in U-area at 0xE8000000 + 0x1000)
  • TOS (Top of Stack): 0xE8005000 - Stack top
  • Stack size: 16384 bytes (16 KB)
  • Initial demand: 32 bytes ($20)

Deployment

The new kernel has been deployed to all locations:

examples/05-c-kernel/kernel.zip
src/frontend/nd500wasm/web/demo/kernel.zip
build_wasm/bin/kernel.zip

Checksum: fc9d81e044fb831a6d3c50901d55725e (all locations match)

Package contents:

  • kernel.c - C kernel source (8.5 KB)
  • kernel.s - Generated assembly (10.8 KB)
  • kernel - Linked executable (1.6 KB)
  • kernel.map - Symbol map (16.2 KB)

Architecture Compliance

The kernel now complies with ND-500 architecture specification:

  1. ✓ INIT instruction executed before any ENTS
  2. ✓ Stack registers (B, TOS, SP) properly initialized
  3. ✓ Entry point at text address 4 (after magic number)
  4. ✓ No instruction sequence error trap on startup

Comparison with NDIX-C

Our implementation matches the NDIX-C vmunix kernel pattern:

Aspect NDIX vmunix Demo Kernel Status
Bootstrap file locore.c locore.c ✓ Match
Entry label start: start: ✓ Match
First instruction INIT INIT ✓ Match
Cache operations dctsb, pctsb dctsb, pctsb ✓ Match
C entry point _main _kernel_main ~ Different name
Link order locore.o first locore.o first ✓ Match
Entry flag -e start -e start ✓ Match

Future Integration Path

This bootstrap implementation prepares the way for full NDIX vmunix integration:

Phase 1: ✓ Bootstrap (COMPLETED)

  • Adopted locore.c assembly-in-C pattern
  • Stack initialization working correctly
  • Build process matches NDIX

Phase 2: Memory Layout Alignment (Future)

  • Add full NDIX memory map constants
  • Implement U-area structure (trap vectors, process context)
  • Align data structures with NDIX layout

Phase 3: Full vmunix Source (Future)

  • Import complete NDIX-C kernel source tree
  • Use existing NDIX locore.c unchanged
  • Build with minimal modifications
  • Enhance ND500X emulator (full MMU, domains, ND-100 interface)

Technical Notes

Why Assembly-in-C Works

  1. File extension .c triggers C preprocessor
  2. Preprocessor expands #define macros and .set constants
  3. Assembly directives (.text, .globl, etc.) pass through
  4. Output goes to assembler (not compiler backend)
  5. Result: Pure assembly with constant substitution

Build Process

locore.c → [cpp -E -DLOCORE] → locore.s → [as] → locore.o
kernel.c → [cc -S] → kernel.s → [as] → kernel.o
locore.o + kernel.o → [ld -e start] → kernel
kernel → [splitseg] → kernel.pseg + kernel.dseg

Why This Failed Before

Attempt 1: Inline assembly

void start() {
    asm("init ...");  // Too late - ENTS already generated!
}
Problem: Compiler adds ENTS prologue before function body.

Attempt 2: Separate bootstrap.s

_start:
    init ...
    call _kernel_main
Problem: pcc-nd500 linker doesn't recognize hand-written labels as valid entry points.

Attempt 3: Bootstrap prefix concatenation

cat bootstrap.s kernel.s > combined.s
Problem: Symbol table corruption when mixing custom assembly with compiler output.

The Correct Solution

NDIX locore.c approach:

  • Assembly directives in .c file
  • C preprocessor for constant expansion
  • Label (not function) at entry point
  • Linked first in object order
  • Proven in production ND-500 kernels since 1980s

References

  • NDIX-C kernel source: $NDIX/kernel/MASTER/machine/locore.c
  • ND-500 Reference Manual: Page 230 (ENTS instruction specification)
  • Analysis document: NDIX_VMUNIX_BOOTSTRAP_ANALYSIS.md
  • Issue documentation: KERNEL_STACK_INIT.md, BOOTSTRAP_EXPLAINED.md

Build and Test

Rebuild Kernel

cd examples/05-c-kernel
make clean
make all

Test in Debugger

cd .
./build/bin/nd500x --debug
> load examples/05-c-kernel/kernel
> set PC 4
> step 3
> regs

Expected output:

[INIT] Stack initialized at B=0xE8001000, TOS=0xE8005000, SP=0xE8001000

Verify Deployment

md5sum examples/05-c-kernel/kernel.zip \
       src/frontend/nd500wasm/web/demo/kernel.zip \
       build_wasm/bin/kernel.zip

All checksums should match: fc9d81e044fb831a6d3c50901d55725e


Status: Bootstrap problem SOLVED using proven NDIX-C pattern. Date: 2025-11-16 Impact: Kernel now compliant with ND-500 architecture specification, ready for future vmunix integration.