still serv_close bug...
[mit-jos.git] / lib / pgfault.c
blob1d9e06daa40c55ce8e78d901fede5a3e675d3bee
1 // User-level page fault handler support.
2 // We use an assembly language wrapper around a C function.
3 // The assembly wrapper is in pfentry.S.
5 #include <inc/lib.h>
8 // Assembly language pgfault entrypoint defined in lib/pgfaultentry.S.
9 extern void _pgfault_upcall(void);
11 // Pointer to currently installed C-language pgfault handler.
12 void (*_pgfault_handler)(struct UTrapframe *utf);
15 // Set the page fault handler function.
16 // If there isn't one yet, _pgfault_handler will be 0.
17 // The first time we register a handler, we need to
18 // allocate an exception stack (one page of memory with its top
19 // at UXSTACKTOP), and tell the kernel to call the assembly-language
20 // _pgfault_upcall routine when a page fault occurs.
22 void
23 set_pgfault_handler(void (*handler)(struct UTrapframe *utf))
25 int r;
27 if (_pgfault_handler == 0) {
28 // First time through!
29 // LAB 4: Your code here.
30 if ((r = sys_page_alloc(0, (void *)(UXSTACKTOP-PGSIZE),
31 PTE_U | PTE_W | PTE_P)) < 0)
32 panic("set_pgfault_handler: page alloc error: %e", r);
35 // Save handler pointer for assembly to call.
36 _pgfault_handler = handler;
38 // register the env pgfault upcall
39 if ((r = sys_env_set_pgfault_upcall(0, _pgfault_upcall)) < 0)
40 panic("set_pgfault_handler: set pgfault upcall error: %e", r);