static void kernel_call_finish(struct proc * caller, message *msg, int result) { if(result == VMSUSPEND) { /* Special case: message has to be saved for handling * until VM tells us it's allowed. VM has been notified * and we must wait for its reply to restart the call. */ assert(RTS_ISSET(caller, RTS_VMREQUEST)); assert(caller->p_vmrequest.type == VMSTYPE_KERNELCALL); caller->p_vmrequest.saved.reqmsg = *msg; caller->p_misc_flags |= MF_KCALL_RESUME; } else { /* * call is finished, we could have been suspended because of VM, * remove the request message */ caller->p_vmrequest.saved.reqmsg.m_source = NONE; if (result != EDONTREPLY) { /* copy the result as a message to the original user buffer */ msg->m_source = SYSTEM; msg->m_type = result; /* report status of call */ #if DEBUG_IPC_HOOK hook_ipc_msgkresult(msg, caller); #endif if (copy_msg_to_user(msg, (message *)caller->p_delivermsg_vir)) { printf("WARNING wrong user pointer 0x%08x from " "process %s / %d\n", caller->p_delivermsg_vir, caller->p_name, caller->p_endpoint); cause_sig(proc_nr(caller), SIGSEGV); } } } }
/* * this function checks the basic syscall parameters and if accepted it * dispatches its handling to the right handler */ void kernel_call(message *m_user, struct proc * caller) { int result = OK; message msg; caller->p_delivermsg_vir = (vir_bytes) m_user; /* * the ldt and cr3 of the caller process is loaded because it just've trapped * into the kernel or was already set in switch_to_user() before we resume * execution of an interrupted kernel call */ if (copy_msg_from_user(m_user, &msg) == 0) { msg.m_source = caller->p_endpoint; result = kernel_call_dispatch(caller, &msg); } else { printf("WARNING wrong user pointer 0x%08x from process %s / %d\n", m_user, caller->p_name, caller->p_endpoint); cause_sig(proc_nr(caller), SIGSEGV); return; } /* remember who invoked the kcall so we can bill it its time */ kbill_kcall = caller; kernel_call_finish(caller, &msg, result); }
/*===========================================================================* * do_clocktick * *===========================================================================*/ PRIVATE void do_clocktick() { /* Despite its name, this routine is not called on every clock tick. It * is called on those clock ticks when a lot of work needs to be done. */ register struct proc *rp; register int proc_nr; if (next_alarm <= realtime) { /* An alarm may have gone off, but proc may have exited, so check. */ next_alarm = LONG_MAX; /* start computing next alarm */ for (rp = BEG_PROC_ADDR; rp < END_PROC_ADDR; rp++) { if (rp->p_alarm != 0) { /* See if this alarm time has been reached. */ if (rp->p_alarm <= realtime) { /* A timer has gone off. If it is a user proc, * send it a signal. If it is a task, call the * function previously specified by the task. */ proc_nr = proc_number(rp); if (watch_dog[proc_nr+NR_TASKS]) { watchdog_proc= proc_nr; (*watch_dog[proc_nr+NR_TASKS])(); } else cause_sig(proc_nr, SIGALRM); rp->p_alarm = 0; } /* Work on determining which alarm is next. */ if (rp->p_alarm != 0 && rp->p_alarm < next_alarm) next_alarm = rp->p_alarm; } } } /* If a user process has been running too long, pick another one. */ if (--sched_ticks == 0) { if (bill_ptr == prev_ptr) lock_sched(); /* process has run too long */ sched_ticks = SCHED_RATE; /* reset quantum */ prev_ptr = bill_ptr; /* new previous process */ } #if (SHADOWING == 1) if (rdy_head[SHADOW_Q]) unshadow(rdy_head[SHADOW_Q]); #endif }
/*===========================================================================* * exception * *===========================================================================*/ PUBLIC void exception_handler(int is_nested, struct exception_frame * frame) { /* An exception or unexpected interrupt has occurred. */ struct ex_s { char *msg; int signum; int minprocessor; }; static struct ex_s ex_data[] = { { "Divide error", SIGFPE, 86 }, { "Debug exception", SIGTRAP, 86 }, { "Nonmaskable interrupt", SIGBUS, 86 }, { "Breakpoint", SIGEMT, 86 }, { "Overflow", SIGFPE, 86 }, { "Bounds check", SIGFPE, 186 }, { "Invalid opcode", SIGILL, 186 }, { "Coprocessor not available", SIGFPE, 186 }, { "Double fault", SIGBUS, 286 }, { "Coprocessor segment overrun", SIGSEGV, 286 }, { "Invalid TSS", SIGSEGV, 286 }, { "Segment not present", SIGSEGV, 286 }, { "Stack exception", SIGSEGV, 286 }, /* STACK_FAULT already used */ { "General protection", SIGSEGV, 286 }, { "Page fault", SIGSEGV, 386 }, /* not close */ { NULL, SIGILL, 0 }, /* probably software trap */ { "Coprocessor error", SIGFPE, 386 }, { "Alignment check", SIGBUS, 386 }, { "Machine check", SIGBUS, 386 }, { "SIMD exception", SIGFPE, 386 }, }; register struct ex_s *ep; struct proc *saved_proc; /* Save proc_ptr, because it may be changed by debug statements. */ saved_proc = proc_ptr; ep = &ex_data[frame->vector]; if (frame->vector == 2) { /* spurious NMI on some machines */ printf("got spurious NMI\n"); return; } /* * handle special cases for nested problems as they might be tricky or filter * them out quickly if the traps are not nested */ if (is_nested) { /* * if a problem occured while copying a message from userspace because * of a wrong pointer supplied by userland, handle it the only way we * can handle it ... */ if (((void*)frame->eip >= (void*)copy_msg_to_user && (void*)frame->eip <= (void*)__copy_msg_to_user_end) || ((void*)frame->eip >= (void*)copy_msg_from_user && (void*)frame->eip <= (void*)__copy_msg_from_user_end)) { switch(frame->vector) { /* these error are expected */ case PAGE_FAULT_VECTOR: case PROTECTION_VECTOR: frame->eip = (reg_t) __user_copy_msg_pointer_failure; return; default: panic("Copy involving a user pointer failed unexpectedly!"); } } } if(frame->vector == PAGE_FAULT_VECTOR) { pagefault(saved_proc, frame, is_nested); return; } /* If an exception occurs while running a process, the is_nested variable * will be zero. Exceptions in interrupt handlers or system traps will make * is_nested non-zero. */ if (is_nested == 0 && ! iskernelp(saved_proc)) { #if 0 { printf( "vec_nr= %d, trap_errno= 0x%lx, eip= 0x%lx, cs= 0x%x, eflags= 0x%lx\n", frame->vector, (unsigned long)frame->errcode, (unsigned long)frame->eip, frame->cs, (unsigned long)frame->eflags); printseg("cs: ", 1, saved_proc, frame->cs); printseg("ds: ", 0, saved_proc, saved_proc->p_reg.ds); if(saved_proc->p_reg.ds != saved_proc->p_reg.ss) { printseg("ss: ", 0, saved_proc, saved_proc->p_reg.ss); } proc_stacktrace(saved_proc); } #endif cause_sig(proc_nr(saved_proc), ep->signum); return; } /* Exception in system code. This is not supposed to happen. */ if (ep->msg == NULL || machine.processor < ep->minprocessor) printf("\nIntel-reserved exception %d\n", frame->vector); else printf("\n%s\n", ep->msg); printf("is_nested = %d ", is_nested); printf("vec_nr= %d, trap_errno= 0x%x, eip= 0x%x, " "cs= 0x%x, eflags= 0x%x trap_esp 0x%08x\n", frame->vector, frame->errcode, frame->eip, frame->cs, frame->eflags, frame); printf("KERNEL registers :\n"); printf( "\t%%eax 0x%08x %%ebx 0x%08x %%ecx 0x%08x %%edx 0x%08x\n" "\t%%esp 0x%08x %%ebp 0x%08x %%esi 0x%08x %%edi 0x%08x\n", ((u32_t *)frame)[-1], ((u32_t *)frame)[-2], ((u32_t *)frame)[-3], ((u32_t *)frame)[-4], ((u32_t *)frame)[-5], ((u32_t *)frame)[-6], ((u32_t *)frame)[-7], ((u32_t *)frame)[-8] ); printseg("ker cs: ", 1, NULL, frame->cs); printseg("ker ds: ", 0, NULL, DS_SELECTOR); /* TODO should we enable this only when compiled for some debug mode? */ if (saved_proc) { printf("scheduled was: process %d (%s), ", proc_nr(saved_proc), saved_proc->p_name); printf("pc = %u:0x%x\n", (unsigned) saved_proc->p_reg.cs, (unsigned) saved_proc->p_reg.pc); proc_stacktrace(saved_proc); panic("Unhandled kernel exception"); } else { /* in an early stage of boot process we don't have processes yet */ panic("exception in kernel while booting"); } }
PRIVATE void pagefault( struct proc *pr, struct exception_frame * frame, int is_nested) { int in_physcopy = 0; reg_t pagefaultcr2; message m_pagefault; int err; pagefaultcr2 = read_cr2(); #if 0 printf("kernel: pagefault in pr %d, addr 0x%lx, his cr3 0x%lx, actual cr3 0x%lx\n", pr->p_endpoint, pagefaultcr2, pr->p_seg.p_cr3, read_cr3()); #endif in_physcopy = (frame->eip > (vir_bytes) phys_copy) && (frame->eip < (vir_bytes) phys_copy_fault); if((is_nested || iskernelp(pr)) && catch_pagefaults && in_physcopy) { #if 0 printf("pf caught! addr 0x%lx\n", pagefaultcr2); #endif if (is_nested) { frame->eip = (reg_t) phys_copy_fault_in_kernel; } else { pr->p_reg.pc = (reg_t) phys_copy_fault; pr->p_reg.retreg = pagefaultcr2; } return; } if(is_nested) { printf("pagefault in kernel at pc 0x%lx address 0x%lx\n", frame->eip, pagefaultcr2); inkernel_disaster(pr, frame, NULL, is_nested); } /* System processes that don't have their own page table can't * have page faults. VM does have its own page table but also * can't have page faults (because VM has to handle them). */ if((pr->p_endpoint <= INIT_PROC_NR && !(pr->p_misc_flags & MF_FULLVM)) || pr->p_endpoint == VM_PROC_NR) { /* Page fault we can't / don't want to * handle. */ printf("pagefault for process %d ('%s') on CPU %d, " "pc = 0x%x, addr = 0x%x, flags = 0x%x, is_nested %d\n", pr->p_endpoint, pr->p_name, cpuid, pr->p_reg.pc, pagefaultcr2, frame->errcode, is_nested); if(!is_nested) { printf("process vir addr of pagefault is 0x%lx\n", pagefaultcr2 - (pr->p_memmap[D].mem_phys << CLICK_SHIFT)); } proc_stacktrace(pr); printf("pc of pagefault: 0x%lx\n", frame->eip); cause_sig(proc_nr(pr), SIGSEGV); return; } /* Don't schedule this process until pagefault is handled. */ RTS_SET(pr, RTS_PAGEFAULT); /* tell Vm about the pagefault */ m_pagefault.m_source = pr->p_endpoint; m_pagefault.m_type = VM_PAGEFAULT; m_pagefault.VPF_ADDR = pagefaultcr2; m_pagefault.VPF_FLAGS = frame->errcode; if ((err = mini_send(pr, VM_PROC_NR, &m_pagefault, FROM_KERNEL))) { panic("WARNING: pagefault: mini_send returned %d\n", err); } return; }
/*===========================================================================* * exception * *===========================================================================*/ PUBLIC void exception_handler(int is_nested, struct exception_frame * frame) { /* An exception or unexpected interrupt has occurred. */ register struct ex_s *ep; struct proc *saved_proc; /* Save proc_ptr, because it may be changed by debug statements. */ saved_proc = get_cpulocal_var(proc_ptr); ep = &ex_data[frame->vector]; if (frame->vector == 2) { /* spurious NMI on some machines */ printf("got spurious NMI\n"); return; } /* * handle special cases for nested problems as they might be tricky or filter * them out quickly if the traps are not nested */ if (is_nested) { /* * if a problem occured while copying a message from userspace because * of a wrong pointer supplied by userland, handle it the only way we * can handle it ... */ if (((void*)frame->eip >= (void*)copy_msg_to_user && (void*)frame->eip <= (void*)__copy_msg_to_user_end) || ((void*)frame->eip >= (void*)copy_msg_from_user && (void*)frame->eip <= (void*)__copy_msg_from_user_end)) { switch(frame->vector) { /* these error are expected */ case PAGE_FAULT_VECTOR: case PROTECTION_VECTOR: frame->eip = (reg_t) __user_copy_msg_pointer_failure; return; default: panic("Copy involving a user pointer failed unexpectedly!"); } } /* Pass any error resulting from restoring FPU state, as a FPU * exception to the process. */ if (((void*)frame->eip >= (void*)fxrstor && (void *)frame->eip <= (void*)__fxrstor_end) || ((void*)frame->eip >= (void*)frstor && (void *)frame->eip <= (void*)__frstor_end)) { frame->eip = (reg_t) __frstor_failure; return; } } if(frame->vector == PAGE_FAULT_VECTOR) { pagefault(saved_proc, frame, is_nested); return; } /* If an exception occurs while running a process, the is_nested variable * will be zero. Exceptions in interrupt handlers or system traps will make * is_nested non-zero. */ if (is_nested == 0 && ! iskernelp(saved_proc)) { #if 0 { printf( "vec_nr= %d, trap_errno= 0x%lx, eip= 0x%lx, cs= 0x%x, eflags= 0x%lx\n", frame->vector, (unsigned long)frame->errcode, (unsigned long)frame->eip, frame->cs, (unsigned long)frame->eflags); printseg("cs: ", 1, saved_proc, frame->cs); printseg("ds: ", 0, saved_proc, saved_proc->p_reg.ds); if(saved_proc->p_reg.ds != saved_proc->p_reg.ss) { printseg("ss: ", 0, saved_proc, saved_proc->p_reg.ss); } proc_stacktrace(saved_proc); } #endif cause_sig(proc_nr(saved_proc), ep->signum); return; } /* Exception in system code. This is not supposed to happen. */ inkernel_disaster(saved_proc, frame, ep, is_nested); panic("return from inkernel_disaster"); }