Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement relocation tables in KEXC binaries #194

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
10 changes: 8 additions & 2 deletions .build.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
image: archlinux
packages:
- mono
- make
- sass
- mktiupgrade
- genkfs
- mkrom
- patchrom
sources:
- https://github.com/KnightOS/kernel
- https://github.com/KnightOS/scas
environment:
project: kernel
tasks:
- scas: |
cd scas
mkdir build
cd build
cmake ..
make
sudo make install
- ti73: |
cd ${project}
make TI73
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014 The KnightOS Group
Copyright (c) 2014-2020 The KnightOS Group

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
31 changes: 12 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Makefile for KnightOS kernel
AS=sass
ASFLAGS=--encoding "Windows-1252"
AS=scas
ASFLAGS=-v
#ASFLAGS=--encoding "Windows-1252"
.DEFAULT_GOAL=TI84pSE
PLATFORM:=TI84pSE
TAG:=$(shell git describe --abbrev=0)
TAG:=$(shell git describe --abbrev=0 --dirty=+)
OUTDIR=bin/

KERNEL_VERSION = -DKERNEL_VERSION=$(TAG)

# Platforms:
# Variables (all in hex):
# PRIVILEGED: The address of the privileged page
Expand Down Expand Up @@ -65,7 +68,7 @@ TI84pCSE: BOOT := 3FC000
TI84pCSE: LENGTH := 0x400000
TI84pCSE: kernel $(OUTDIR)

DEFINES=--define $(PLATFORM)
DEFINES=--define $(PLATFORM) $(KERNEL_VERSION)
BINDIR=$(OUTDIR)$(PLATFORM)/
INCLUDE=include/;$(BINDIR)

Expand Down Expand Up @@ -109,28 +112,18 @@ baserom:
mkdir -p $(BINDIR)
mkrom $(BINDIR)kernel.rom $(LENGTH) /dev/null:0x00

$(OUTDIR)$(PLATFORM)/00.bin: src/00/*.asm include/constants.asm src/00/jumptable.config
@mkdir -p $(BINDIR)
$(AS) $(ASFLAGS) $(DEFINES) --include "$(INCLUDE);src/00/" --symbols $(BINDIR)00.sym --listing $(BINDIR)00.list src/00/base.asm $(BINDIR)00.bin
patchrom src/00/jumptable.config $(BINDIR)kernel.rom 00 < $(BINDIR)00.sym > $(BINDIR)00.inc

$(OUTDIR)$(PLATFORM)/01.bin: $(OUTDIR)$(PLATFORM)/00.bin src/01/*.asm include/constants.asm src/01/jumptable.config
$(OUTDIR)$(PLATFORM)/%.bin: src/%/*.asm include/constants.asm src/%/jumptable.config
@mkdir -p $(BINDIR)
$(AS) $(ASFLAGS) $(DEFINES) --include "$(INCLUDE);src/01/" --symbols $(BINDIR)01.sym --listing $(BINDIR)01.list src/01/base.asm $(BINDIR)01.bin
patchrom src/01/jumptable.config $(BINDIR)kernel.rom 01 < $(BINDIR)01.sym > $(BINDIR)01.inc

$(OUTDIR)$(PLATFORM)/02.bin: $(OUTDIR)$(PLATFORM)/00.bin src/02/*.asm include/constants.asm src/02/jumptable.config
@mkdir -p $(BINDIR)
$(AS) $(ASFLAGS) $(DEFINES) --include "$(INCLUDE);src/02/" --symbols $(BINDIR)02.sym --listing $(BINDIR)02.list src/02/base.asm $(BINDIR)02.bin
patchrom src/02/jumptable.config $(BINDIR)kernel.rom 02 < $(BINDIR)02.sym > $(BINDIR)02.inc
$(AS) $(ASFLAGS) $(DEFINES) --include "$(INCLUDE);src/$*/" --symbols $(BINDIR)$*.sym --listing $(BINDIR)$*.list src/$*/base.asm -o $(BINDIR)$*.bin
patchrom src/$*/jumptable.config $(BINDIR)kernel.rom $* < $(BINDIR)$*.sym > $(BINDIR)$*.inc

$(OUTDIR)$(PLATFORM)/privileged.bin: src/privileged/*.asm include/constants.asm $(OUTDIR)$(PLATFORM)/00.bin
@mkdir -p $(BINDIR)
$(AS) $(ASFLAGS) $(DEFINES) --include "$(INCLUDE);src/privileged/" --listing $(BINDIR)priviledged.list src/privileged/base.asm $(BINDIR)privileged.bin
$(AS) $(ASFLAGS) $(DEFINES) --include "$(INCLUDE);src/privileged/" --listing $(BINDIR)priviledged.list src/privileged/base.asm -o $(BINDIR)privileged.bin

$(OUTDIR)$(PLATFORM)/boot.bin: src/boot/*.asm include/constants.asm
@mkdir -p $(BINDIR)
$(AS) $(ASFLAGS) $(DEFINES) --include "$(INCLUDE);src/boot/" --listing $(BINDIR)boot.list src/boot/base.asm $(BINDIR)boot.bin
$(AS) $(ASFLAGS) $(DEFINES) --include "$(INCLUDE);src/boot/" --listing $(BINDIR)boot.list src/boot/base.asm -o $(BINDIR)boot.bin

clean:
rm -rf $(OUTDIR)
23 changes: 15 additions & 8 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
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_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.
pixelherodev marked this conversation as resolved.
Show resolved Hide resolved

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 All @@ -52,3 +53,9 @@ reserved for kernel use, and 0x80-0xFF is available for arbitrary use.

When used to describe an executable program, the only required header is
KEXC_ENTRY_POINT (and of course KEXC_HEADER_END).

Relocation Table

The relocation table consists of any number of program-relative addresses
terminated by a null word. Those addresses should themselves contain
program-relative pointers to relocate.
1 change: 1 addition & 0 deletions include/defines.inc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ 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
3 changes: 1 addition & 2 deletions src/00/header.asm
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ rlcall:
; 0x0056
.db 0xFF, 0xA5, 0xFF
.fill 0x64-$
.exec git describe --dirty=+ ; Version string
.db 0
.asciiz "KERNEL_VERSION"
2 changes: 1 addition & 1 deletion src/00/locks.asm
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ hasUSBLock:
push hl
push af
call getCurrentThreadId
ld hl, hwLockUsb
ld hl, hwLockUSB
cp (hl)
pop hl
ld a, h
Expand Down
2 changes: 1 addition & 1 deletion src/00/random.asm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ getRandom:
add hl, hl \ rl c \ rl b
ld (random_seed+6), bc
sbc a, a
and %11000101
and 0b11000101
xor l
ld l, a
ld (random_seed+4), hl
Expand Down
54 changes: 48 additions & 6 deletions src/00/thread.asm
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ launchProgram:
; Check magic number
ld a, 'K'
cp (ix)
jr nz, .magic_error
jp nz, .magic_error
ld a, 'E'
cp (ix + 1)
jr nz, .magic_error
Expand Down Expand Up @@ -426,12 +426,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 +465,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 +490,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
14 changes: 7 additions & 7 deletions src/02/crypto.asm
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ sha1Init:
.defaultMemblock:

; Holds the completed hash.
sha1_hash .equ $ - .defaultMemblock
.equ sha1_hash, $ - .defaultMemblock
.db 0x67,0x45,0x23,0x01
.db 0xEF,0xCD,0xAB,0x89
.db 0x98,0xBA,0xDC,0xFE
Expand Down Expand Up @@ -107,13 +107,13 @@ sha1Pad_noPush:
; append the bit '1' to the message
; append 0 <= k < 512 bits '0', so that the resulting message length (in bits)
; is congruent to 448 = -64 (mod 512)
ld a, $80
ld a, 0x80
.zero:
call sha1AddByte_noLength
ld a, (ix + sha1_block_front_ptr)
add a, 56
cp (ix + sha1_block_ptr)
ld a, $00
ld a, 0x00
jr nz, .zero
; append length of message (before padding), in bits, as 64-bit big-endian integer
push ix \ pop hl
Expand Down Expand Up @@ -223,10 +223,10 @@ sha1ProcessBlock:
dec hl
ld (ix + sha1_block_ptr), l
ld (ix + sha1_block_ptr + 1), h
ld hl, sha1Operation_mux \ call sha1Do20Rounds \ .db $5A,$82,$79,$99
ld hl, sha1Operation_xor \ call sha1Do20Rounds \ .db $6E,$D9,$EB,$A1
ld hl, sha1Operation_maj \ call sha1Do20Rounds \ .db $8F,$1B,$BC,$DC
ld hl, sha1Operation_xor \ call sha1Do20Rounds \ .db $CA,$62,$C1,$D6
ld hl, sha1Operation_mux \ call sha1Do20Rounds \ .db 0x5A,0x82,0x79,0x99
ld hl, sha1Operation_xor \ call sha1Do20Rounds \ .db 0x6E,0xD9,0xEB,0xA1
ld hl, sha1Operation_maj \ call sha1Do20Rounds \ .db 0x8F,0x1B,0xBC,0xDC
ld hl, sha1Operation_xor \ call sha1Do20Rounds \ .db 0xCA,0x62,0xC1,0xD6

; Add this chunk's hash to result so far
; h0 += a
Expand Down
2 changes: 1 addition & 1 deletion src/boot/base.asm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "constants.asm"
; Dummy boot page to get emulators to boot the OS
jr _
.fill 0x0F - $
.block 0x0F - $
.db "Emulated", 0
_:
in a, (PORT_FLASHRAMSIZE)
Expand Down