Improve syscall gateway: now position-independent

Also it now keeps all register values except SS:SP on entry.
This commit is contained in:
Nero 2021-01-04 23:00:53 +00:00
parent fb4bbfc8cf
commit 0256d6ab36
2 changed files with 63 additions and 32 deletions

View File

@ -44,7 +44,7 @@ restart:
hlt: hlt hlt: hlt
jmp hlt jmp hlt
%include "kernel/bdos.asm" %include "kernel/syscall.asm"
%include "kernel/far.asm" %include "kernel/far.asm"
%include "kernel/fcb.asm" %include "kernel/fcb.asm"
%include "kernel/find.asm" %include "kernel/find.asm"

View File

@ -1,7 +1,41 @@
; This is the syscall gateway:
; - backs up the data from user space (kernel funcs trash many regs)
; - sets up the kernel stack
; - calls kernel funcs
; - exports a specified set of registers back to userspace
;
; The syscall table acts as a "export"-list for kernel near funcs.
; Code invoked via it can reply on SS being zero.
section .bss section .bss
curpsp: resw 1 curpsp: resw 1
section .data
stab: ; syscall table
; cells: ptr to handler, ptr to sysret
; 0-7
dw restart, sret
dw getc, sretb
dw putc, sret
dw err, sret ; reader input
dw err, sret ; punch outout
dw err, sret ; list output
dw conio, sret ; direct console i/o
dw err, sret
; 8-f
dw err, sret
dw err, sret ; print string
dw err, sret ; read into buffer
dw err, sret ; console status
dw err, sret ; return version num
dw err, sret ; reset disks
dw err, sret ; select disk
dw err, sret ; open file
.end:
section .text
absolute 0 absolute 0
@ -22,15 +56,23 @@ PSPDS: resw 1
PSPBX: resw 1 PSPBX: resw 1
PSPES: resw 1 PSPES: resw 1
section .text section .text
; Load current PSP segment into DS
; OUT ds PSP segment ; OUT ds PSP segment
; ax 0
pspds: xor ax, ax pspds: xor ax, ax
mov ds, ax mov ds, ax
mov ds, [curpsp] mov ds, [curpsp]
ret ret
; Fix CS:AX pointer for reloc
fixax: call .l01
.l01: pop bx
sub bx, .l01
add ax, bx
ret
int21: push ds int21: push ds
; load program PSP and save userdata ; load program PSP and save userdata
push ax push ax
@ -45,39 +87,28 @@ int21: push ds
mov ss, ax mov ss, ax
mov sp, ( stack+stacksize ) mov sp, ( stack+stacksize )
; get ptr to syscall table
mov al, [PSPAX+1] mov al, [PSPAX+1]
shl ax, 1 shl ax, 1
shl ax, 1 shl ax, 1
call .etbl add ax, stab
; syscall table call fixax
; cells: ptr to handler, ptr to sysret mov bx, ax
; 0-7 ; load sysret ptr
dw restart, sret push bx
dw getc, sretb mov ax, [cs:bx+2]
dw putc, sret call fixax
dw err, sret ; reader input pop bx
dw err, sret ; punch outout push ax
dw err, sret ; list output ; load handler ptr
dw conio, sret ; direct console i/o mov ax, [cs:bx]
dw err, sret call fixax
; 8-f push ax
dw err, sret ; restore user data
dw err, sret ; print string mov bx, [PSPBX]
dw err, sret ; read into buffer mov ax, [PSPAX]
dw err, sret ; console status mov ds, [PSPDS]
dw err, sret ; return version num ; launch ROP chain
dw err, sret ; reset disks
dw err, sret ; select disk
dw err, sret ; open file
; set up a return chain and execute it
; first return into handler function
; second return into appropiate sysret
.etbl: pop bx
add bx, ax
push word [cs:bx+2]
push word [cs:bx]
ret ret
err: mov ah, 0xFF err: mov ah, 0xFF