feat: shell (sh) with keyboard driver, SYS_READ, SYS_EXEC

- PS/2 keyboard driver: IRQ1 handler, scancode set 1 translation,
  ring buffer, shift key support
- SYS_READ (fd=0): non-blocking keyboard read for stdin
- SYS_YIELD: calls schedule_tick directly (no nested INT 0x80)
- SYS_EXEC: loads binary from CPIO initrd, replaces process image
- User-space libc: crt0.S (C runtime startup), syscalls.h (inline
  syscall wrappers, basic string functions)
- Shell app (sh): readline with echo/backspace, builtins (cd, env,
  help, exit), fork+exec for external commands
- Updated build_apps.sh: C app support with crt0 linking, libc
  include path, .bss section in objcopy

Tested: shell boots, keyboard input works, hello-world runs via
fork+exec, env shows CWD, exit cleanly terminates.
This commit is contained in:
AI
2026-02-23 13:08:06 +00:00
parent e9b66cd60e
commit c25ba1fccd
10 changed files with 745 additions and 16 deletions

View File

@@ -11,14 +11,24 @@ LINKER_SCRIPT="$APPS_DIR/user.ld"
CC="${CC:-clang}"
OBJCOPY="${OBJCOPY:-objcopy}"
CFLAGS="-ffreestanding -m32 -fno-pie -fno-pic -fno-builtin -fno-stack-protector -mno-sse -mno-mmx -O2 -Wall"
CFLAGS="-ffreestanding -m32 -fno-pie -fno-pic -fno-builtin -fno-stack-protector -mno-sse -mno-mmx -O2 -Wall -I$APPS_DIR/libc"
LDFLAGS="-m32 -nostdlib -no-pie -Wl,--no-dynamic-linker"
mkdir -p "$OUTPUT_DIR"
# Build crt0 if it exists
CRT0_OBJ=""
if [ -f "$APPS_DIR/libc/crt0.S" ]; then
CRT0_OBJ="$OUTPUT_DIR/_crt0.o"
$CC $CFLAGS -c "$APPS_DIR/libc/crt0.S" -o "$CRT0_OBJ"
fi
for app_dir in "$APPS_DIR"/*/; do
[ -d "$app_dir" ] || continue
app_name=$(basename "$app_dir")
# Skip the libc directory (shared library, not an app)
[ "$app_name" = "libc" ] && continue
echo "Building app: $app_name"
@@ -36,13 +46,22 @@ for app_dir in "$APPS_DIR"/*/; do
continue
fi
# Link into ELF
# Link into ELF (include crt0 if app has .c files and doesn't have its own _start)
elf="$OUTPUT_DIR/$app_name.elf"
$CC $LDFLAGS -T "$LINKER_SCRIPT" $OBJ_FILES -o "$elf"
HAS_C_FILES=""
for src in "$app_dir"*.c; do
[ -f "$src" ] && HAS_C_FILES="yes"
done
# Convert to flat binary (strip non-code sections)
if [ -n "$HAS_C_FILES" ] && [ -n "$CRT0_OBJ" ]; then
$CC $LDFLAGS -T "$LINKER_SCRIPT" "$CRT0_OBJ" $OBJ_FILES -o "$elf"
else
$CC $LDFLAGS -T "$LINKER_SCRIPT" $OBJ_FILES -o "$elf"
fi
# Convert to flat binary (include .bss for zero-initialized data)
bin="$OUTPUT_DIR/$app_name"
$OBJCOPY -O binary --only-section=.text --only-section=.rodata --only-section=.data "$elf" "$bin"
$OBJCOPY -O binary --only-section=.text --only-section=.rodata --only-section=.data --only-section=.bss "$elf" "$bin"
size=$(wc -c < "$bin")
echo " Built: $bin ($size bytes)"