kvisc/ka/sys/intr/trap0.k

216 lines
3.9 KiB
Plaintext
Raw Normal View History

2019-07-09 19:51:03 +02:00
; The OS/K Team licenses this file to you under the MIT license.
; See the LICENSE file in the project root for more information.
2019-08-08 18:39:12 +02:00
TrapHandlers.prolog:
2019-09-07 16:45:03 +02:00
nul ebp
2019-08-08 18:39:12 +02:00
2019-09-07 16:45:03 +02:00
; eax = caller's cr1
call RFS.LoadReg, nx2, $cr1
2019-08-08 18:39:12 +02:00
2019-09-07 16:45:03 +02:00
; we don't preserve the nx0 we got
mov nx0, eax
nul edx
2019-08-08 18:39:12 +02:00
2019-09-07 16:45:03 +02:00
jmp ecx
2019-08-08 18:39:12 +02:00
TrapHandlers.epilog:
2019-09-07 16:45:03 +02:00
; TRAP return values: eax-edx
2019-08-08 18:39:12 +02:00
2019-09-07 16:45:03 +02:00
mov ax2, eax
call RFS.StoreReg, nx2, $eax
2019-08-08 18:39:12 +02:00
2019-09-07 16:45:03 +02:00
mov ax2, edx
call RFS.StoreReg, nx2, $edx
2019-08-08 18:39:12 +02:00
2019-09-07 16:45:03 +02:00
call IDT.DoneHandling, nx1
2019-08-08 18:39:12 +02:00
iret
;
; TRAP #0 handler
;
2019-08-06 23:21:37 +02:00
DefaultTrapHandler:
2019-07-09 19:51:03 +02:00
.init:
2019-09-07 16:45:03 +02:00
mov ecx, .text
mov esp, TRAP0_STACK
2019-07-09 19:51:03 +02:00
jmp TrapHandlers.prolog
2019-07-10 12:28:20 +02:00
.text:
2019-09-07 16:45:03 +02:00
call RFS.LoadReg, nx2, $eax
call RFS.LoadArgs, nx2
2019-07-09 19:51:03 +02:00
2019-08-21 16:57:32 +02:00
; will be optimized with a table
; when we have a "finished" consistent API
2019-08-14 09:52:39 +02:00
jraxz .handle_Shutdown
2019-09-07 16:45:03 +02:00
beq eax, Sys.Exit, .handle_Exit
beq eax, Sys.ExecuteInFrame, .handle_EIF
beq eax, Sys.ReadFile, .handle_ReadFile
beq eax, Sys.OpenFile, .handle_OpenFile
beq eax, Sys.CloseFile, .handle_CloseFile
beq eax, Sys.CreateFile, .handle_CreateFile
beq eax, Sys.RemoveFile, .handle_RemoveFile
beq eax, Sys.FindNext, .handle_FindNext
beq eax, Sys.FindFirst, .handle_FindFirst
beq eax, Sys.EnterHaltMode, .handle_HaltMode
2019-08-05 15:55:46 +02:00
2019-08-21 16:57:32 +02:00
.sod:
2019-09-07 16:45:03 +02:00
call ScreenOfDeath, .badsyscall, nx1
2019-07-09 19:51:03 +02:00
2019-08-21 16:57:32 +02:00
.wrong:
2019-09-07 16:45:03 +02:00
call ScreenOfDeath, .badparams, nx1
2019-08-21 16:57:32 +02:00
2019-08-05 14:56:22 +02:00
.fini.savecx:
2019-09-07 16:45:03 +02:00
mov ax2, ecx
call RFS.StoreReg, nx2, $ecx
2019-08-05 14:56:22 +02:00
2019-07-09 19:51:03 +02:00
.fini:
jmp TrapHandlers.epilog
2019-09-07 16:45:03 +02:00
.badsyscall = "Invalid syscall number in eax"
2019-08-21 16:57:32 +02:00
.badparams = "Invalid syscall parameters"
2019-08-06 22:47:39 +02:00
2019-07-10 20:11:35 +02:00
;------------------------------------------------;
; Syscall implementations ;
;------------------------------------------------;
2019-07-10 17:17:45 +02:00
2019-07-09 19:51:03 +02:00
;
2019-07-10 17:17:45 +02:00
; Pass control to COMMAND.COM in frame 0
2019-07-09 19:51:03 +02:00
;
2019-07-10 17:17:45 +02:00
.handle_Exit:
2019-08-21 16:57:32 +02:00
; deactivate current rframe
2019-09-07 16:45:03 +02:00
bzr nx2, .hE_nz ; unless it's 0...
call RFS.DeactivateFrame, nx2
2019-07-10 17:17:45 +02:00
2019-08-21 16:57:32 +02:00
.hE_nz:
; open COMMAND.COM
2019-07-18 22:49:31 +02:00
call DISK.OpenFile, .cmdcom
2019-07-10 17:17:45 +02:00
2019-08-21 16:57:32 +02:00
; crash on failure
2019-09-07 16:45:03 +02:00
bltz eax, abort
2019-07-10 17:17:45 +02:00
2019-08-21 16:57:32 +02:00
; load at CMDCOM_LOADP
2019-07-10 17:17:45 +02:00
mov ax1, CMDCOM_LOADP
mov ax2, CMDCOM_MAXSZ
2019-09-07 16:45:03 +02:00
call DISK.ReadFile, eax
2019-07-10 17:17:45 +02:00
2019-08-21 16:57:32 +02:00
; assume that COMMAND.COM being
2019-07-10 17:17:45 +02:00
; less then 4KB means something
; went wrong
2019-09-07 16:45:03 +02:00
blt eax, 0x1000, abort
2019-07-10 17:17:45 +02:00
2019-08-21 16:57:32 +02:00
; close the handle
2019-09-07 16:45:03 +02:00
call DISK.CloseFile, eax
2019-07-10 17:17:45 +02:00
2019-08-21 16:57:32 +02:00
; code address
2019-07-10 17:17:45 +02:00
mov ax2, 0x100000
2019-09-07 16:45:03 +02:00
call RFS.StoreReg, zero, $eip
2019-07-10 17:17:45 +02:00
2019-08-21 16:57:32 +02:00
; usermode
2019-07-10 17:17:45 +02:00
mov ax2, 3
2019-07-18 22:49:31 +02:00
call RFS.StoreReg, zero, $cr0
2019-07-10 17:17:45 +02:00
2019-09-07 16:45:03 +02:00
mov ecx, CMDCOM_LOADP
sub ecx, 0x100000
2019-07-09 19:51:03 +02:00
2019-08-21 16:57:32 +02:00
; code offset
2019-09-07 16:45:03 +02:00
mov ax2, ecx
2019-07-18 22:49:31 +02:00
call RFS.StoreReg, zero, $cr1
2019-07-10 17:17:45 +02:00
2019-08-21 16:57:32 +02:00
; return frame
2019-09-07 16:45:03 +02:00
nul nx2
2019-07-10 17:17:45 +02:00
jmp .fini
.cmdcom = "command.com"
2019-08-21 16:57:32 +02:00
.handle_EIF:
blt ax1, 5, .wrong
2019-09-07 16:45:03 +02:00
; eip can't be <1MB
mov ecx, 0x100000
blt ax0, ecx, .wrong
2019-08-21 16:57:32 +02:00
; add old CR1
2019-09-07 16:45:03 +02:00
add ax0, nx0
2019-08-21 16:57:32 +02:00
2019-09-07 16:45:03 +02:00
; real eip can't be <1MB+32KB either
2019-08-21 16:57:32 +02:00
; (kernel + cmdcom personal space)
2019-09-07 16:45:03 +02:00
add edx, ecx, 0x8000
blt ax0, ecx, .wrong
2019-08-21 16:57:32 +02:00
; save rframe and compute new CR1
2019-09-07 16:45:03 +02:00
mov ebx, ax1
sub edi, ax0, ecx
2019-08-21 16:57:32 +02:00
; activate rframe
2019-09-07 16:45:03 +02:00
call RFS.ActivateFrame, ebx
2019-08-21 16:57:32 +02:00
; save new CR1
2019-09-07 16:45:03 +02:00
mov ax2, edi
call RFS.StoreReg, ebx, $cr1
2019-08-21 16:57:32 +02:00
; interruptible user mode
mov ax2, 3
2019-09-07 16:45:03 +02:00
call RFS.StoreReg, ebx, $cr0
2019-08-21 16:57:32 +02:00
2019-09-07 16:45:03 +02:00
; set eip = 1MB
mov ax2, ecx
call RFS.StoreReg, ebx, $eip
2019-08-21 16:57:32 +02:00
; change return frame
2019-09-07 16:45:03 +02:00
mov nx2, ebx
2019-08-21 16:57:32 +02:00
jmp .fini
2019-07-10 17:17:45 +02:00
;
; Disk API
;
2019-07-10 12:26:15 +02:00
.handle_FindFirst:
2019-09-07 16:45:03 +02:00
add ax0, nx0
2019-07-10 12:26:15 +02:00
call DISK.FindFirst
2019-08-05 14:56:22 +02:00
jmp .fini.savecx
2019-07-10 12:26:15 +02:00
.handle_FindNext:
2019-09-07 16:45:03 +02:00
add ax0, nx0
2019-07-10 12:26:15 +02:00
call DISK.FindNext
2019-08-05 14:56:22 +02:00
jmp .fini.savecx
2019-07-09 19:51:03 +02:00
2019-07-13 12:38:03 +02:00
.handle_OpenFile:
2019-09-07 16:45:03 +02:00
add ax0, nx0
2019-07-13 12:38:03 +02:00
call DISK.OpenFile
jmp .fini
2019-08-05 14:56:22 +02:00
.handle_CreateFile:
2019-09-07 16:45:03 +02:00
add ax0, nx0
2019-08-05 14:56:22 +02:00
call DISK.CreateFile
jmp .fini
2019-08-05 15:55:46 +02:00
.handle_RemoveFile:
2019-09-07 16:45:03 +02:00
add ax0, nx0
2019-08-05 15:55:46 +02:00
call DISK.RemoveFile
jmp .fini
2019-07-13 12:38:03 +02:00
.handle_CloseFile:
call DISK.CloseFile
jmp .fini
.handle_ReadFile:
2019-09-07 16:45:03 +02:00
add ax1, nx0
2019-07-13 12:38:03 +02:00
call DISK.ReadFile
jmp .fini
2019-07-10 17:17:45 +02:00
;
; Misc.
;
2019-07-10 22:37:59 +02:00
.handle_Shutdown:
2019-08-14 09:52:39 +02:00
call IDT.DelHandler, zero
2019-07-10 22:37:59 +02:00
stop
2019-07-13 12:38:03 +02:00
.handle_HaltMode:
2019-08-14 09:52:39 +02:00
pause
pause
hlt
jmp .handle_HaltMode
2019-07-09 19:51:03 +02:00