Skip to content

Commit

Permalink
Implement relocation tables in KEXC binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Noam Preil committed Jan 29, 2020
1 parent 2f65d75 commit 269e216
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
19 changes: 10 additions & 9 deletions doc/kexc
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ good idea.

The following headers are defined in kernel.inc:

Name Description

KEXC_HEADER_END The end of the header list. Value may be omitted.
KEXC_ENTRY_POINT Pointer to executable entry point.
KEXC_STACK_SIZE Bytes of stack required, divided by two.
KEXC_KERNEL_VER Minimum kernel version supported. Major, minor.
KEXC_THREAD_FLAGS Thread flags. Only the upper 8 bits are considered.
KEXC_NAME Pointer to program name.
KEXC_DESCRIPTION Pointer to program description.
Name Description

KEXC_HEADER_END The end of the header list. Value may be omitted.
KEXC_ENTRY_POINT Pointer to executable entry point.
KEXC_STACK_SIZE Bytes of stack required, divided by two.
KEXC_KERNEL_VER Minimum kernel version supported. Major, minor.
KEXC_THREAD_FLAGS Thread flags. Only the upper 8 bits are considered.
KEXC_NAME Pointer to program name.
KEXC_DESCRIPTION Pointer to program description.
KEXC_RELOCATION_TABLE Pointer to the relocation table.

Header keys are numbered from 0x00-0xFF, inclusive. The 0x00-0x7F range is
reserved for kernel use, and 0x80-0xFF is available for arbitrary use.
Expand Down
3 changes: 2 additions & 1 deletion include/defines.inc
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ panic_failed_init .equ 4

; KEXC headers
; 0x00-0x7F reserved for kernel use
; 0x80-0xFF available for arbituary use
; 0x80-0xFF available for arbitrary use
KEXC_HEADER_END .equ 0x00
KEXC_ENTRY_POINT .equ 0x01
KEXC_STACK_SIZE .equ 0x02
KEXC_KERNEL_VER .equ 0x03
KEXC_THREAD_FLAGS .equ 0x04
KEXC_NAME .equ 0x05
KEXC_DESCRIPTION .equ 0x06
KEXC_RELOCATION_TABLE .equ 0x07

; Thread flags
THREAD_NON_SUSPENDABLE .equ 2
Expand Down
53 changes: 48 additions & 5 deletions src/00/thread.asm
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ _: pop af
cp a
ret

.echo "lp: 0x{0:X4}" launchProgram
;; launchProgram [Threading]
;; Loads the specified file into memory as a program and starts a
;; new thread for it. The file must be a valid KEXC executable.
Expand Down Expand Up @@ -426,12 +427,18 @@ launchProgram:
.unknown_ver:
; no minimum version is specified by the executable
.no_minimum_ver:
; Check for a relocation table
ld b, KEXC_RELOCATION_TABLE
push ix \ call _getThreadHeader \ pop ix
call z, .relocate

; Grab header info
ld b, KEXC_ENTRY_POINT
push ix \ call _getThreadHeader \ pop ix
jr nz, .no_entry_point
push hl
ld b, KEXC_STACK_SIZE
; b still has KEXC_ENTRY_POINT, and KEXC_STACK_SIZE is 1 higher
inc b
push ix \ call _getThreadHeader \ pop ix
ld c, l ; TODO: Error out if H is nonzero?
jr z, _
Expand Down Expand Up @@ -459,14 +466,14 @@ _: ld a, b
pop bc
cp a
ret
.kernel_too_low:
ld a, errKernelMismatch
.magic_error:
ld a, errNoMagic
jr .error
.no_entry_point:
ld a, errNoEntryPoint
jr .error
.magic_error:
ld a, errNoMagic
.kernel_too_low:
ld a, errKernelMismatch
jr .error
.error_pop2:
inc sp \ inc sp
Expand All @@ -484,6 +491,42 @@ _: or 1
ld a, b
pop bc
ret
; thrashes de, bc, and hl
.relocate:
; ix = executable address
; hl = program-relative relocation table address
push ix \ pop de
add hl, de
; hl = absolute address of relocation table
.relocation_loop:
ld e, (hl)
inc hl
ld d, (hl)
; de = first entry in relocation table
dec hl
; hl: preserved
ld bc, 0
call cpBCDE
ret z
; de contains the program-relative address of a program-relative pointer to relocate
; need to execute, in effect, `add (ix + de), ix`
push ix
add ix, de
push ix \ pop de
pop ix
; de = absolute address of pointer to relocate
; add (de), ix
push ix \ pop bc
ld a, (de)
add a, c
ld (de), a
inc de
ld a, (de)
add a, b
ld (de), a
inc hl \ inc hl
jr .relocation_loop

;; exitThread [Threading]
;; Immediately terminates the running thread.
Expand Down

0 comments on commit 269e216

Please sign in to comment.