Skip to content

Commit

Permalink
syscalls and page fault take a register_state_t* instead of registers…
Browse files Browse the repository at this point in the history
…_state_t
  • Loading branch information
codyd51 committed Jun 27, 2018
1 parent 31555ef commit 681eb75
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -13,6 +13,7 @@ Features
* Compositing window manager with clipping and alpha blending
* Text renderer with SSAA antialiasing
* Auto-boots in 1024x768 full RGB
* FAT filesystem with IDE hard-drive driver, and an initrd.
* ELF loader
* Newlib port
* MLFQ scheduler
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/interrupts/int_handler_stubs.s
Expand Up @@ -158,7 +158,7 @@ irq_common_stub:

call irq_receive

; push registers_t* arg back into esp
; pop registers_t* arg back into esp
pop esp

; restore data segment selector
Expand Down
1 change: 1 addition & 0 deletions src/kernel/interrupts/interrupts.c
Expand Up @@ -81,6 +81,7 @@ void irq_receive(register_state_t* regs) {
}

pic_signal_end_of_interrupt(int_no);

return ret;
}

Expand Down
14 changes: 7 additions & 7 deletions src/kernel/syscall/syscall.c
Expand Up @@ -8,7 +8,7 @@

#define MAX_SYSCALLS 128

static int syscall_handler(register_state_t regs);
static int syscall_handler(register_state_t* regs);

array_m* syscalls = {0};

Expand All @@ -33,16 +33,16 @@ void syscall_add(void* syscall) {
array_m_insert(syscalls, syscall);
}

static int syscall_handler(register_state_t regs) {
static int syscall_handler(register_state_t* regs) {
//check requested syscall number
//stored in eax
if (!syscalls || regs.eax >= MAX_SYSCALLS) {
printf_err("Syscall %d called but not defined", regs.eax);
if (!syscalls || regs->eax >= MAX_SYSCALLS) {
printf_err("Syscall %d called but not defined", regs->eax);
return -1;
}

//location of syscall funcptr
int (*location)() = (int(*)())array_m_lookup(syscalls, regs.eax);
int (*location)() = (int(*)())array_m_lookup(syscalls, regs->eax);

//we don't know how many arguments the function wants.
//so just push them all on the stack in correct order
Expand All @@ -61,7 +61,7 @@ static int syscall_handler(register_state_t regs) {
pop %%ebx; \
pop %%ebx; \
pop %%ebx; \
" : "=a" (ret) : "r" (regs.edi), "r" (regs.esi), "r" (regs.edx), "r" (regs.ecx), "r" (regs.ebx), "r" (location));
regs.eax = ret;
" : "=a" (ret) : "r" (regs->edi), "r" (regs->esi), "r" (regs->edx), "r" (regs->ecx), "r" (regs->ebx), "r" (location));
regs->eax = ret;
return ret;
}
18 changes: 9 additions & 9 deletions src/kernel/vmm/vmm.c
Expand Up @@ -12,7 +12,7 @@
#define PAGES_IN_PAGE_TABLE 1024
#define PAGE_TABLES_IN_PAGE_DIR 1024

static void page_fault(register_state_t regs);
static void page_fault(const register_state_t* regs);
static uint32_t vmm_page_table_idx_for_virt_addr(uint32_t addr);
static uint32_t vmm_page_idx_within_table_for_virt_addr(uint32_t addr);
static void vmm_page_table_alloc_for_virt_addr(vmm_pdir_t* dir, uint32_t addr);
Expand Down Expand Up @@ -117,18 +117,18 @@ uint32_t vmm_get_phys_for_virt(uint32_t virtualaddr) {
return (uint32_t)((pt[ptindex] & ~0xFFF) + ((unsigned long)virtualaddr & 0xFFF));
}

static void page_fault(register_state_t regs) {
static void page_fault(const register_state_t* regs) {
//page fault has occured
//faulting address is stored in CR2 register
uint32_t faulting_address;
asm volatile("mov %%cr2, %0" : "=r" (faulting_address));

//error code tells us what happened
int present = !(regs.err_code & 0x1); //page not present
int rw = regs.err_code & 0x2; //write operation?
int us = regs.err_code & 0x4; //were we in user mode?
int reserved = regs.err_code & 0x8; //overwritten CPU-reserved bits of page entry?
int id = regs.err_code & 0x10; //caused by instruction fetch?
int present = !(regs->err_code & 0x1); //page not present
int rw = regs->err_code & 0x2; //write operation?
int us = regs->err_code & 0x4; //were we in user mode?
int reserved = regs->err_code & 0x8; //overwritten CPU-reserved bits of page entry?
int id = regs->err_code & 0x10; //caused by instruction fetch?

//if this page was present, attempt to recover by allocating the page
if (present) {
Expand All @@ -152,10 +152,10 @@ static void page_fault(register_state_t regs) {
if (reserved) printf_err("Overwrote CPU-resereved bits of page entry");
if (id) printf_err("Faulted during instruction fetch");

bool caused_by_execution = (regs.eip == faulting_address);
bool caused_by_execution = (regs->eip == faulting_address);
printf("Caused by %s unpaged memory\n", caused_by_execution ? "executing" : "reading");
printf("Kernel spinlooping due to unhandled page fault\n");
asm("sti");
//asm("sti");
while (1) {}
}

Expand Down

0 comments on commit 681eb75

Please sign in to comment.