// Allocate an open file. int open_alloc(struct Open **o) { int i, r; // Find an available open-file table entry for (i = 0; i < MAXOPEN; i++) { switch (pageref(opentab[i].o_ff)) { case 0: //writef("^^^^^^^^^^^^^^^^ (u_int)opentab[i].o_ff: %x\n",(u_int)opentab[i].o_ff); if ((r = syscall_mem_alloc(0, (u_int)opentab[i].o_ff, PTE_V | PTE_R | PTE_LIBRARY)) < 0) { return r; } case 1: opentab[i].o_fileid += MAXOPEN; *o = &opentab[i]; user_bzero((void *)opentab[i].o_ff, BY2PG); return (*o)->o_fileid; } } return -E_MAX_OPEN; }
int opencons(void) { int r; struct Fd *fd; if ((r = fd_alloc(&fd)) < 0) return r; if ((r = syscall_mem_alloc(0, (u_int)fd, PTE_V|PTE_R|PTE_LIBRARY)) < 0) return r; fd->fd_dev_id = devcons.dev_id; fd->fd_omode = O_RDWR; return fd2num(fd); }
// // Set the page fault handler function. // If there isn't one yet, _pgfault_handler will be 0. // The first time we register a handler, we need to // allocate an exception stack and tell the kernel to // call _asm_pgfault_handler on it. // void set_pgfault_handler(void (*fn)(u_int va, u_int err)) { int r; if (__pgfault_handler == 0) { // Your code here: // map one page of exception stack with top at UXSTACKTOP // register assembly handler and stack with operating system if(syscall_mem_alloc(0, UXSTACKTOP - BY2PG, PTE_V|PTE_R)<0 || syscall_set_pgfault_handler(0, __asm_pgfault_handler, UXSTACKTOP)<0) { writef("cannot set pgfault handler\n"); return; } // panic("set_pgfault_handler not implemented"); } // Save handler pointer for assembly to call. __pgfault_handler = fn; }