Syscall vector operational (for kernel mode)

This commit is contained in:
Adrien Bourmault 2021-02-26 19:10:25 +01:00
parent 4e5da302a0
commit d3db4efba4
Signed by: neox
GPG Key ID: 6EB408FE0ACEC664
9 changed files with 99 additions and 28 deletions

View File

@ -38,11 +38,6 @@
//
#define interrupt(n) asm volatile ("int %0" : : "N" (n) : "cc", "memory")
//
// Trigger a system call
//
#define syscall(n) asm volatile ("movq %0, %rdi\nint $0x80" : : "N" (n) : "cc", "memory")
//
// Returns whether IRQs are enabled
//
@ -75,6 +70,11 @@ extern void KeSendEOItoPIC(uchar isr);
extern void KeEnableNMI(void);
extern void KeDisableNMI(void);
//
// System Call
//
extern error_t KeSyscall(ulong code);
//
// Restore IRQ flag to its state before KePauseIRQs
//

View File

@ -33,7 +33,7 @@
extern void KeJumpToUserspace(ulong args, void *entryPoint, void *stackAddr);
void KeEnableSystemCalls();
error_t _KeSyscallHandler(ulong code);
//----------------------------------------------------------------------------//

View File

@ -82,7 +82,6 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
MmActivatePageHandler();
KeGetCpuInfos();
IoEnableKeyb();
KeEnableSystemCalls();
// ACPI
IoInitAcpi();

View File

@ -26,6 +26,7 @@
global divideByZero
global KeActivateSSE
global KeSyscall
%include "kaleid/kernel/ke/cpuf.inc"
@ -49,3 +50,10 @@ KeActivateSSE:
mov cr4, rax
pop rax
ret
;;
;;
;;
KeSyscall:
int 0x80
ret

View File

@ -69,6 +69,15 @@ isr%1:
jmp isrPreHandler
%endmacro
%macro SyscallHandler 1
global isr%1
isr%1:
cli
push 0
push %1
jmp syscallPreHandler
%endmacro
%macro IsrWithErrCode 1
global isr%1
isr%1:

View File

@ -74,7 +74,6 @@ static char *ExceptionsChar[32] = {
static void EnablePIC(void);
static void EarlyExceptionHandler(ISRFrame_t *regs);
static void DoubleFaultHandler(ISRFrame_t *regs);
static void EarlySyscallHandler(ISRFrame_t *regs);
//
// Registers an isr with his IRQ to handle driver interrupts
@ -177,7 +176,7 @@ void KeSetupIDT(void)
KeSetIDTGate(0x2F, (ulong)isr47, codeSeg, 0x8E, 3);
// SYSCALL
KeSetIDTGate(0x80, (ulong)isr128, codeSeg, 0x8E, 3);
KeSetIDTGate(0x80, (ulong)isr128, codeSeg, 0x8E, 3); // Directly handled by _KeSyscallHandler, without registration
KeIdtIsInitialized++;
@ -188,7 +187,6 @@ void KeSetupIDT(void)
KeRegisterISR(KeBrkDumpRegisters, 0x3);
KeRegisterISR(DoubleFaultHandler, 0x8);
KeRegisterISR(EarlySyscallHandler, 0x80);
// Load IDT
KeLoadIDT();
@ -391,11 +389,6 @@ static void DoubleFaultHandler(ISRFrame_t *regs)
KeHaltCPU();
}
static void EarlySyscallHandler(ISRFrame_t *regs)
{
DebugLog("Got an early system call !\n");
}
void KeBrkDumpRegisters(ISRFrame_t *regs)
{
IoDoBeepNoIdt();

View File

@ -30,6 +30,7 @@ global KeLoadIDT
extern _KeIdtPtr
extern _KeHandleISR
extern _KeSyscallHandler
;;
;; Loads the IDT
@ -99,6 +100,66 @@ Die:
hlt
jmp Die
;;
;; System call pre-handler
;;
syscallPreHandler:
pushAll
mov rax, cr8
push rax
mov rax, cr4
push rax
mov rax, cr3
push rax
mov rax, cr2
push rax
mov rax, cr0
push rax
mov rcx, 0xC0000080
rdmsr
push rax
.SEnter:
; Increment mask count as we configure all interrupts to mask IF
; automatically in the IDT
inc qword [gs:8]
; Call the C routine for dispatching an interrupt
cld ; DF must be cleared by the caller
mov rbp, 0 ; Terminate stack traces here
call _KeSyscallHandler
; decrement mask count
dec qword [gs:8]
.SExit:
; pop the control registers
add rsp, 48
pop rbx ; do not overwrite rax, so double pop on rbx
pop rbx
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
jmp Die
;; Divide Error Fault
IsrWithoutErrCode 0
@ -182,4 +243,4 @@ IsrWithoutErrCode 31
%endrep
;; Syscall Vector
IsrWithoutErrCode 128
SyscallHandler 128

View File

@ -27,17 +27,9 @@
#include <ke/idt.h>
#include <io/vga.h>
static uchar SyscallHandler(ISRFrame_t *regs)
error_t _KeSyscallHandler(ulong code)
{
DebugLog("Got a system call code %d !\n", regs->rdi);
DebugLog("Got a system call code %ld !\n", code);
return EOK;
}
void KeEnableSystemCalls()
{
ulong flags = KePauseIRQs();
KeRegisterISR(SyscallHandler, 0x80);
KeRestoreIRQs(flags);
KeEnableNMI();
}

View File

@ -314,9 +314,18 @@ error_t CmdTimerTest(int argc, char **argv, char *cmdline)
error_t CmdSyscallTest(int argc, char **argv, char *cmdline)
{
interrupt(0x80);
if (argc != 2) {
KernLog("Invalid arguments !\n");
return EINVAL;
}
return EOK;
ulong code = (ulong)atol(argv[1]);
error_t err = KeSyscall(code);
KernLog("Exit : %s\n", strerror(err));
return err;
}
error_t CmdRing3Test(int argc, char **argv, char *cmdline)