Remove stack switching, implement some syscalls

This commit is contained in:
Nero 2020-04-19 00:35:31 +02:00
parent c1c012c5fc
commit 97f26c1f89

View File

@ -4,79 +4,42 @@
%include "inc/bpb.asm" %include "inc/bpb.asm"
banner: db "rdos", 0xA, 0xD, 0 banner: db "rdos", 0xA, 0xD, '$'
; syscalls push a register set to stack int20h: xor ah, ah
; this is for accessing it int21h: ; inside of kernel, direction always goes up
rsdssi: equ 0x08 ; the iret will restore it to the user value later
rsesdi: equ 0x0C cld
rsbp: equ 0x10 ; set sfptr from ah
rslpad: equ 0x12
rscsip: equ 0x14
rsflag: equ 0x18
lpads: times 0x30 call entry
entry: push bp
; BX+0C: ES:DI
push es
push di
; BX+08: DS:SI
push ds
push si
; BX+00: AX,CX,DX,BX
push bx push bx
push dx xor bx, bx
push cx mov bl, ah
push ax add bl, bl
add bx, sftab
mov ax, ss mov bx, [cs:bx]
mov es, ax mov [cs:sfptr], bx
mov bx, sp
; This needs to be done when coming from userspace
mov ax, cs
mov ss, ax
xor sp, sp
; Jump back to userspace
ujump: cli
mov ax, es
mov ss, ax
mov sp, bx
; Pop whole registerset
; base registers
pop ax
pop cx
pop dx
pop bx pop bx
; string ptrs ; do the actual subfunction call
pop si call [cs:sfptr]
pop ds ; inherit the lower 8 flag bits to userspace
pop di push ax
pop es push bp
; BP is special mov bp, sp
lahf
mov [bp+8], ah
pop bp pop bp
; skip landing pad number pop ax
add sp, 2 ; iret frame: IP CS FLAGS
; pop IP, CS and flags
iret iret
; Processes register set DS:BX as syscall ; Subfunction ptr
scall: ; Clear status flags ; this is used as extra register in int21h
and byte [bx+rsflag], 0 sfptr: dw 0
ret
; No such subfunction
sferr: ; set carry
or byte [bx+rsflag], 1
ret
; Subfunction table ; Subfunction table
sftab: dw sferr, sferr, sferr, sferr sftab: dw sferr, sferr, putc, sferr
dw sferr, sferr, sferr, sferr
dw sferr, sferr, sferr, sferr dw sferr, sferr, sferr, sferr
dw sferr, puts, sferr, sferr
dw sferr, sferr, sferr, sferr dw sferr, sferr, sferr, sferr
; 10 ; 10
dw sferr, sferr, sferr, sferr dw sferr, sferr, sferr, sferr
@ -94,19 +57,32 @@ sftab: dw sferr, sferr, sferr, sferr
dw sferr, sferr, sferr, sferr dw sferr, sferr, sferr, sferr
dw sferr, sferr, sferr, sferr dw sferr, sferr, sferr, sferr
; DOS 2+ - GET INTERRUPT VECTOR ; DOS 1+ 2h - WRITE CHARACTER TO STANDARD OUTPUT
; IN al interrupt number ; IN dl character to write
; OUT es:bx current interrupt handler putc: push ax
getint: xor bx, bx push bx
; BX=AL*4 mov ah, 0x0E
mov bl, al mov al, dl
add bl, bl xor bx, bx
add bl, bl int 0x10
; load vector into ES:BX pop bx
les bx, [cs:bx] pop ax
ret ret
; DOS 25h: Set interrupt vector ; DOS 1+ 9h - WRITE STRING TO STANDARD OUTPUT
; IN ds:dx '$'-terminated string
puts: push si
mov ah, 0x0E
xor bx, bx
.loop: lodsb
cmp al, '$'
je .end
int 0x10
jmp .loop
.end: pop si
ret
; DOS 1+ 25h - SET INTERRUPT VECTOR
; IN al interrupt number ; IN al interrupt number
; ds:dx entry point ; ds:dx entry point
setint: push bx setint: push bx
@ -121,6 +97,23 @@ setint: push bx
pop bx pop bx
ret ret
; DOS 2+ 35h - GET INTERRUPT VECTOR
; IN al interrupt number
; OUT es:bx current interrupt handler
getint: xor bx, bx
; BX=AL*4
mov bl, al
add bl, bl
add bl, bl
; load vector into ES:BX
les bx, [cs:bx]
ret
; Fallback for non-existant subfunctions
; The carry flag is inherited to user
sferr: stc
ret
bpb: times bpb_len db 0 bpb: times bpb_len db 0
drvnum: db 0 drvnum: db 0
align 4 align 4
@ -210,13 +203,8 @@ ldbpb: push ds
ret ret
main: mov si, banner main: mov si, banner
mov ah, 0x0e mov ah, 0x09
xor bx, bx int 0x21
loop: lodsb
test al, al
jz end
int 0x10
jmp loop
end: hlt end: hlt
jmp end jmp end
@ -235,4 +223,11 @@ init: cli
call dnconv call dnconv
call select call select
mov al, 0x20
mov dx, int20h
call setint
mov al, 0x21
mov dx, int21h
call setint
jmp 0:main jmp 0:main