From bb7f68d633e4a353174c8b96c6d860a9a96612a4 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Sun, 5 Nov 2017 09:50:04 +0000 Subject: [PATCH] Add module "syscall" --- arch/Makefile | 1 + arch/config.h | 2 ++ arch/protected.c | 7 ++++--- arch/syscall.asm | 32 ++++++++++++++++++++++++++++++++ arch/syscall.c | 14 ++++++++++++++ arch/syscall.h | 6 ++++++ 6 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 arch/syscall.asm create mode 100644 arch/syscall.c create mode 100644 arch/syscall.h diff --git a/arch/Makefile b/arch/Makefile index 196f1a8..0bda5c8 100644 --- a/arch/Makefile +++ b/arch/Makefile @@ -19,6 +19,7 @@ OBJS += kprintf.c.o OBJS += protected.c.o protected.asm.cpp.o OBJS += exception.c.o exception.asm.cpp.o OBJS += hwint.c.o hwint.asm.cpp.o +OBJS += syscall.c.o syscall.asm.cpp.o OBJS += timer.c.o diff --git a/arch/config.h b/arch/config.h index d423a67..a345759 100644 --- a/arch/config.h +++ b/arch/config.h @@ -38,4 +38,6 @@ #define INT_HWINT_FIRST (INT_EXCEPTION_LAST + 1) #define INT_HWINT_LAST (INT_HWINT_FIRST + INT_HWINT_COUNT - 1) +#define INT_SYSCALL 0x80 + #endif diff --git a/arch/protected.c b/arch/protected.c index 06f27be..e41046b 100644 --- a/arch/protected.c +++ b/arch/protected.c @@ -5,6 +5,7 @@ #include "asm.h" #include "exception.h" #include "hwint.h" +#include "syscall.h" #include @@ -129,6 +130,8 @@ void protected_initialize() idt_set_gate(46, (unsigned int)hwint_14, 0x08, 0x8E); idt_set_gate(47, (unsigned int)hwint_15, 0x08, 0x8E); + idt_set_gate(INT_SYSCALL, (unsigned int)syscall_gate, 0x08, 0x8E | 0x60); + logger_info("Load GDT."); gdt_pointer.limit = sizeof(struct GdtEntry) * GDT_SIZE - 1; @@ -168,7 +171,5 @@ void idt_set_gate(unsigned char num, unsigned int base, unsigned short sel, unsi idt_entries[num].base_hi = (base >> 16) & 0xFFFF; idt_entries[num].sel = sel; idt_entries[num].always0 = 0; - // We must uncomment the OR below when we get to using user-mode. - // It sets the interrupt gate's privilege level to 3. - idt_entries[num].flags = flags /* | 0x60 */; + idt_entries[num].flags = flags; } diff --git a/arch/syscall.asm b/arch/syscall.asm new file mode 100644 index 0000000..a39a8c0 --- /dev/null +++ b/arch/syscall.asm @@ -0,0 +1,32 @@ +#include "config.h" + +[EXTERN syscall_handler] + +[GLOBAL syscall_gate] + +syscall_gate: + cli + + pusha ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax + + mov ax, ds ; Lower 16-bits of eax = ds. + push eax ; save the data segment descriptor + + mov ax, GDT_KERNEL_DS_SELECTOR + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + call syscall_handler + + pop eax ; reload the original data segment descriptor + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + + popa ; Pops edi,esi,ebp... + + sti + iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP diff --git a/arch/syscall.c b/arch/syscall.c new file mode 100644 index 0000000..22feb53 --- /dev/null +++ b/arch/syscall.c @@ -0,0 +1,14 @@ +#include "kprintf.h" + +struct IsrRegisters { + unsigned int ds; // Data segment selector + unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax; // Pushed by pusha. + unsigned int eip, cs, eflags, useresp, ss; // Pushed by the processor automatically. +}; + +void syscall_handler(struct IsrRegisters regs) +{ + const unsigned int id = regs.eax; + + kprintf("syscall %u\n", id); +} diff --git a/arch/syscall.h b/arch/syscall.h new file mode 100644 index 0000000..1cea0b4 --- /dev/null +++ b/arch/syscall.h @@ -0,0 +1,6 @@ +#ifndef KERNELMQ_INCLUDED_SYSCALL +#define KERNELMQ_INCLUDED_SYSCALL 1 + +void syscall_gate(); + +#endif