From 70ee5cfdfd08acfca294e0e180d4f4d1767601cc Mon Sep 17 00:00:00 2001 From: Ry Date: Fri, 24 May 2024 16:51:21 -0700 Subject: [PATCH] kernel + sh: Theoretically support FXF binaries with a bss section --- applications/sh/launch.asm | 27 +++++++++++++++++++++++++++ kernel/fxf/FXF specification.md | 17 +++++++++-------- kernel/fxf/fxf.asm | 13 ++++++++----- kernel/fxf/launch.asm | 24 ++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 13 deletions(-) diff --git a/applications/sh/launch.asm b/applications/sh/launch.asm index 423012e..7c5b335 100644 --- a/applications/sh/launch.asm +++ b/applications/sh/launch.asm @@ -1,5 +1,8 @@ ; FXF launcher helper routines +; FIXME: this should really use the `launch_fxf_from_open_file` routine +; it will need some work to ensure things like the debug prefix works though + ; launch an FXF binary from a shell entry ; inputs: ; r0: pointer to FXF binary name @@ -65,9 +68,32 @@ launch_fxf_name_loop_done: cmp r0, 0 ifz ret + ; if this is not FXF version 0, then there is a bss section + mov r0, 3 + mov r1, launch_fxf_struct + call seek + mov r0, 1 + mov r1, launch_fxf_struct + mov r2, launch_fxf_temp + call read + cmp.8 [launch_fxf_temp], 0 + ifz mov [launch_fxf_temp], 0 + ifz jmp launch_fxf_continue + mov r0, 0x14 + mov r1, launch_fxf_struct + call seek + mov r0, 4 + mov r1, launch_fxf_struct + mov r2, launch_fxf_temp + call read +launch_fxf_continue: + mov r0, 0 + mov r1, launch_fxf_struct + call seek ; allocate memory for the binary mov r0, launch_fxf_struct call get_size + add r0, [launch_fxf_temp] ; add bss size found above call allocate_memory cmp r0, 0 ifz jmp allocate_error @@ -164,6 +190,7 @@ launch_fxf_struct: data.fill 0, 32 launch_fxf_task_id: data.8 0 launch_fxf_binary_ptr: data.32 0 launch_fxf_stack_ptr: data.32 0 +launch_fxf_temp: data.32 0 launch_fxf_yield_should_suspend: data.8 0 launch_fxf_debug_mode: data.8 0 diff --git a/kernel/fxf/FXF specification.md b/kernel/fxf/FXF specification.md index 46c4b8a..c92d38e 100644 --- a/kernel/fxf/FXF specification.md +++ b/kernel/fxf/FXF specification.md @@ -1,8 +1,9 @@ -| Byte Range | Description | -| :-------------------: | -------------------------- | -| 0x00000000-0x00000002 | "FXF" magic bytes | -| 0x00000003 | header version (must be 0) | -| 0x00000004-0x00000007 | code size | -| 0x00000008-0x0000000B | pointer to code | -| 0x0000000C-0x0000000F | reloc table size | -| 0x00000010-0x00000013 | pointer to reloc table | +| Byte Range | Description | +| :-------------------: | --------------------------------------- | +| 0x00000000-0x00000002 | "FXF" magic bytes | +| 0x00000003 | header version (0 for no bss, else bss) | +| 0x00000004-0x00000007 | code size | +| 0x00000008-0x0000000B | pointer to code | +| 0x0000000C-0x0000000F | reloc table size | +| 0x00000010-0x00000013 | pointer to reloc table | +| 0x00000014-0x00000017 | bss allocation size (if version != 0) | diff --git a/kernel/fxf/fxf.asm b/kernel/fxf/fxf.asm index f44e0f2..a1d198c 100644 --- a/kernel/fxf/fxf.asm +++ b/kernel/fxf/fxf.asm @@ -7,18 +7,20 @@ ; r0: relocation address or 0 on error parse_fxf_binary: push r1 - mov r1, [r0] - cmp r1, [fxf_magic] - ifnz pop r1 + push r2 + mov r1, fxf_magic + mov r2, 3 + call compare_memory_bytes + pop r2 + pop r1 ifnz mov r0, 0 ifnz ret - pop r1 call fxf_reloc ret -fxf_magic: data.strz "FXF" +fxf_magic: data.str "FXF" #include "fxf/launch.asm" #include "fxf/reloc.asm" @@ -27,3 +29,4 @@ const FXF_CODE_SIZE: 0x00000004 const FXF_CODE_PTR: 0x00000008 const FXF_RELOC_SIZE: 0x0000000C const FXF_RELOC_PTR: 0x00000010 +const FXF_BSS_SIZE: 0x00000014 diff --git a/kernel/fxf/launch.asm b/kernel/fxf/launch.asm index 5f2c252..5a42e43 100644 --- a/kernel/fxf/launch.asm +++ b/kernel/fxf/launch.asm @@ -60,9 +60,32 @@ launch_fxf_from_disk: cmp r0, 0 ifz jmp launch_fxf_from_disk_file_error launch_fxf_from_open_file_1: + ; if this is not FXF version 0, then there is a bss section + mov r0, 3 + mov r1, [launch_fxf_struct_ptr] + call seek + mov r0, 1 + mov r1, [launch_fxf_struct_ptr] + mov r2, launch_fxf_bss_size + call read + cmp.8 [launch_fxf_bss_size], 0 + ifz mov [launch_fxf_bss_size], 0 + ifz jmp launch_fxf_continue + mov r0, FXF_BSS_SIZE + mov r1, [launch_fxf_struct_ptr] + call seek + mov r0, 4 + mov r1, [launch_fxf_struct_ptr] + mov r2, launch_fxf_bss_size + call read +launch_fxf_continue: + mov r0, 0 + mov r1, [launch_fxf_struct_ptr] + call seek ; allocate memory for the binary mov r0, [launch_fxf_struct_ptr] call get_size + add r0, [launch_fxf_bss_size] call allocate_memory cmp r0, 0 ifz jmp launch_fxf_from_disk_allocate_error @@ -165,6 +188,7 @@ launch_fxf_struct: data.fill 0, 32 launch_fxf_task_id: data.8 0 launch_fxf_binary_ptr: data.32 0 launch_fxf_stack_ptr: data.32 0 +launch_fxf_bss_size: data.32 0 launch_fxf_allocate_error_string1: data.strz "Failed to allocate memory for a new task" launch_fxf_allocate_error_string2: data.strz "The memory allocator seems to be in an" launch_fxf_allocate_error_string3: data.strz "invalid state, a reboot is recommended"