diff --git a/.build.yml b/.build.yml index db70ad3..fdf6d47 100644 --- a/.build.yml +++ b/.build.yml @@ -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 diff --git a/LICENSE b/LICENSE index d785a5c..33ca992 100644 --- a/LICENSE +++ b/LICENSE @@ -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: diff --git a/Makefile b/Makefile index 404a655..e8d198c 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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) @@ -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) diff --git a/doc/kexc b/doc/kexc index 6a50216..3655382 100644 --- a/doc/kexc +++ b/doc/kexc @@ -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. 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. @@ -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. diff --git a/include/defines.inc b/include/defines.inc index f6f0b9d..5991a9a 100644 --- a/include/defines.inc +++ b/include/defines.inc @@ -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 diff --git a/src/00/header.asm b/src/00/header.asm index 677f6d7..8efb5b1 100644 --- a/src/00/header.asm +++ b/src/00/header.asm @@ -44,5 +44,4 @@ rlcall: ; 0x0056 .db 0xFF, 0xA5, 0xFF .fill 0x64-$ -.exec git describe --dirty=+ ; Version string -.db 0 +.asciiz "KERNEL_VERSION" diff --git a/src/00/locks.asm b/src/00/locks.asm index 830e726..dd4a704 100644 --- a/src/00/locks.asm +++ b/src/00/locks.asm @@ -90,7 +90,7 @@ hasUSBLock: push hl push af call getCurrentThreadId - ld hl, hwLockUsb + ld hl, hwLockUSB cp (hl) pop hl ld a, h diff --git a/src/00/random.asm b/src/00/random.asm index 16f6088..75a53cd 100644 --- a/src/00/random.asm +++ b/src/00/random.asm @@ -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 diff --git a/src/00/thread.asm b/src/00/thread.asm index 5b01997..060a32e 100644 --- a/src/00/thread.asm +++ b/src/00/thread.asm @@ -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 @@ -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, _ @@ -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 @@ -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. diff --git a/src/02/crypto.asm b/src/02/crypto.asm index eb4fd06..1b4f6b9 100644 --- a/src/02/crypto.asm +++ b/src/02/crypto.asm @@ -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 @@ -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 @@ -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 diff --git a/src/boot/base.asm b/src/boot/base.asm index 7741975..854f2d4 100644 --- a/src/boot/base.asm +++ b/src/boot/base.asm @@ -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)