Userspace can trigger syscalls

This commit is contained in:
Adrien Bourmault 2021-03-04 11:49:50 +01:00
parent b3d83fac11
commit 59318483f9
Signed by: neox
GPG Key ID: 6EB408FE0ACEC664
6 changed files with 63 additions and 16 deletions

View File

@ -344,7 +344,7 @@ run: test
testnokvm: all installonimage
@qemu-system-x86_64 -vga std -machine type=q35 \
-cpu host -rtc base=localtime -m $(ram) \
-cpu qemu64 -rtc base=localtime -m $(ram) \
-drive file=$(installdisk),index=0,media=disk,format=raw \
-net nic,model=rtl8139 -audiodev id=pa,driver=pa \
-machine pcspk-audiodev=pa \
@ -359,7 +359,7 @@ testnosnd: all installonimage
test32: all installonimage
@qemu-system-i386 -vga std -enable-kvm -machine type=q35 \
-cpu host -rtc base=localtime -m $(ram) \
-cpu qemu32 -rtc base=localtime -m $(ram) \
-drive file=$(installdisk),index=0,media=disk,format=raw \
-net nic,model=rtl8139 -audiodev id=pa,driver=pa \
-machine pcspk-audiodev=pa \
@ -390,7 +390,7 @@ ddd: all installonimage
gdbnokvm: all installonimage
@setsid qemu-system-x86_64 -vga std -machine type=q35 \
-cpu host -rtc base=localtime -m $(ram) \
-cpu qemu64 -rtc base=localtime -m $(ram) \
-drive file=$(installdisk),index=0,media=disk,format=raw \
-net nic,model=rtl8139 -audiodev id=pa,driver=pa \
-machine pcspk-audiodev=pa -no-reboot -no-shutdown\
@ -403,7 +403,7 @@ gdbnokvm: all installonimage
dddnokvm: all installonimage
@setsid qemu-system-x86_64 -vga std -machine type=q35 \
-cpu host -rtc base=localtime -m $(ram) \
-cpu qemu64 -rtc base=localtime -m $(ram) \
-drive file=$(installdisk),index=0,media=disk,format=raw \
-net nic,model=rtl8139 -audiodev id=pa,driver=pa \
-machine pcspk-audiodev=pa -no-reboot -no-shutdown\

View File

@ -35,7 +35,7 @@ extern error_t KeSyscall(ulong code);
extern void KeJumpToUserspace(ulong args, void *entryPoint, void *stackAddr);
error_t _KeSyscallHandler(ulong code);
error_t _KeSyscallHandler(ulong code, ISRFrame_t *regs);
//----------------------------------------------------------------------------//

View File

@ -122,12 +122,24 @@ void KeSetupIDT(void)
// Set IDT ptr
_KeIdtPtr.limit = (sizeof(IdtEntry_t) * 256) - 1;
_KeIdtPtr.base = &idt;
/* ISR flags :
0x8E = 1 0 0 0 1 1 1 0
| | | | '-'-'-'------ Gate type : 0xE is interrupt gate
| | | '-------------- S (must be 0)
| '-'---------------- DPL : 0 is Supervisor
'-------------------- P : 1 is present
0xEE = 1 1 1 0 1 1 1 0
| | | | '-'-'-'------ Gate type : 0xE is interrupt gate
| | | '-------------- S (must be 0)
| '-'---------------- DPL : 3 is user
'-------------------- P : 1 is present */
// Set IDT Exception Gates
KeSetIDTGate(0x00, (ulong)isr0, codeSeg, 0x8E, 2);
KeSetIDTGate(0x01, (ulong)isr1, codeSeg, 0x8E, 2);
KeSetIDTGate(0x02, (ulong)isr2, codeSeg, 0x8E, 2);
KeSetIDTGate(0x03, (ulong)isr3, codeSeg, 0x8E, 2);
KeSetIDTGate(0x03, (ulong)isr3, codeSeg, 0xEE, 2);
KeSetIDTGate(0x04, (ulong)isr4, codeSeg, 0x8E, 2);
KeSetIDTGate(0x05, (ulong)isr5, codeSeg, 0x8E, 2);
KeSetIDTGate(0x06, (ulong)isr6, codeSeg, 0x8E, 2);
@ -176,7 +188,7 @@ void KeSetupIDT(void)
KeSetIDTGate(0x2F, (ulong)isr47, codeSeg, 0x8E, 3);
// SYSCALL
KeSetIDTGate(0x80, (ulong)isr128, codeSeg, 0x8E, 3); // Directly handled by _KeSyscallHandler, without registration
KeSetIDTGate(0x80, (ulong)isr128, codeSeg, 0xEE, 3); // Directly handled by _KeSyscallHandler, without registration
KeIdtIsInitialized++;
@ -328,7 +340,7 @@ void _KeHandleISR(ISRFrame_t *regs)
if (regs->intNo >= 0x20 && regs->intNo <= 0x2F && !(KeGetIrqRegister(0x0b) & (1<<(regs->intNo - 0x20)))) {
KeSpuriousCount++;
return;
}
}
for (int i = 0; i < isrList.n; i++) {
if (regs->intNo == isrList.entry[i].isrNo) {

View File

@ -82,7 +82,21 @@ Die:
;; System call pre-handler
;;
syscallPreHandler:
pushAll
push r15
push r14
push r13
push r12
push r11
push r10
push r9
push r8
push rbp
push 0
push rsi
push rdx
push rcx
push rbx
push rax
mov rax, cr8
push rax
mov rax, cr4
@ -99,17 +113,33 @@ syscallPreHandler:
; Call the C routine to dispatch interrupts
cld ; DF must be cleared by the caller
mov rdi, rsp ; First argument points to the processor state
mov rsi, rsp ; First argument points to the processor state
mov rbp, 0 ; Terminate stack traces here
call _KeSyscallHandler
; pop the control registers
add rsp, 48
; pop registers
popAll
; pop registers except return value
pop rbx
pop rbx ; 2x rbx to discard rax
pop rcx
pop rdx
pop rsi
pop rdi
pop rbp
pop r8
pop r9
pop r10
pop r11
pop r12
pop r13
pop r14
pop r15
; pop the error code and interrupt id
add rsp, 16
iretq
Die2:
hlt

View File

@ -27,9 +27,14 @@
#include <ke/idt.h>
#include <io/vga.h>
error_t _KeSyscallHandler(ulong code)
error_t _KeSyscallHandler(ulong code, ISRFrame_t *regs)
{
DebugLog("Got a system call code %ld !\n", code);
DebugLog("Got a system call code %ld from cs %#x\n", code, regs->cs);
if ((ulong)regs->cs != (ulong)BtLoaderInfo.codeSegment) {
bprintf(BStdOut, "Got a system call from userspace code %d\n", code);
BStdOut->flusher(BStdOut);
}
return EOK;
}

View File

@ -28,8 +28,8 @@ global UserTest
global EndOfUser
UserTest:
mov rdi, 13
;int 0x80
mov rdi, 45
int 0x80
.clone:
nop