3 #include "../port/lib.h"
9 #include "../port/error.h"
12 static int trapinited;
14 void noted(Ureg*, ulong);
16 static void debugbpt(Ureg*, void*);
17 static void fault386(Ureg*, void*);
18 static void doublefault(Ureg*, void*);
19 static void unexpected(Ureg*, void*);
20 static void _dumpstack(Ureg*);
23 static Vctl *vctl[256];
27 Ntimevec = 20 /* number of time buckets for each intr */
29 ulong intrtimes[256][Ntimevec];
32 intrenable(int irq, void (*f)(Ureg*, void*), void* a, int tbdf, char *name)
38 print("intrenable: nil handler for %d, tbdf 0x%uX for %s\n",
43 if(tbdf != BUSUNKNOWN && (irq == 0xff || irq == 0)){
44 print("intrenable: got unassigned irq %d, tbdf 0x%uX for %s\n",
49 if((v = xalloc(sizeof(Vctl))) == nil)
50 panic("intrenable: out of memory");
56 strncpy(v->name, name, KNAMELEN-1);
57 v->name[KNAMELEN-1] = 0;
60 vno = arch->intrenable(v);
63 print("intrenable: couldn't enable irq %d, tbdf 0x%uX for %s\n",
69 if(vctl[vno]->isr != v->isr || vctl[vno]->eoi != v->eoi)
70 panic("intrenable: handler: %s %s %#p %#p %#p %#p",
71 vctl[vno]->name, v->name,
72 vctl[vno]->isr, v->isr, vctl[vno]->eoi, v->eoi);
80 intrdisable(int irq, void (*f)(Ureg *, void *), void *a, int tbdf, char *name)
85 if(arch->intrvecno == nil || (tbdf != BUSUNKNOWN && (irq == 0xff || irq == 0))){
87 * on APIC machine, irq is pretty meaningless
88 * and disabling a the vector is not implemented.
89 * however, we still want to remove the matching
90 * Vctl entry to prevent calling Vctl.f() with a
91 * stale Vctl.a pointer.
96 vno = arch->intrvecno(irq);
100 for(pv = &vctl[vno]; (v = *pv) != nil; pv = &v->next){
101 if(v->isintr && (v->irq == irq || irq == -1)
102 && v->tbdf == tbdf && v->f == f && v->a == a
103 && strcmp(v->name, name) == 0)
110 if(irq != -1 && vctl[vno] == nil && arch->intrdisable != nil)
111 arch->intrdisable(irq);
114 } while(irq == -1 && ++vno <= MaxVectorAPIC);
119 irqallocread(Chan*, void *a, long n, vlong offset)
121 char buf[2*(11+1)+KNAMELEN+1+1];
125 if(n < 0 || offset < 0)
128 for(vno=0; vno<nelem(vctl); vno++){
129 for(v=vctl[vno]; v; v=v->next){
130 m = snprint(buf, sizeof(buf), "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
137 memmove(a, buf+offset, n);
145 trapenable(int vno, void (*f)(Ureg*, void*), void* a, char *name)
149 if(vno < 0 || vno >= VectorPIC)
150 panic("trapenable: vno %d", vno);
151 if((v = xalloc(sizeof(Vctl))) == nil)
152 panic("trapenable: out of memory");
153 v->tbdf = BUSUNKNOWN;
156 strncpy(v->name, name, KNAMELEN-1);
157 v->name[KNAMELEN-1] = 0;
161 v->next = vctl[vno]->next;
172 * Hack: should be locked with NVRAM access.
174 outb(0x70, 0x80); /* NMI latch clear */
177 x = inb(0x61) & 0x07; /* Enable NMI */
189 idt = (Segdesc*)IDTADDR;
190 vaddr = (uintptr)vectortable;
191 for(v = 0; v < 256; v++){
192 d1 = (vaddr & 0xFFFF0000)|SEGP;
196 d1 |= SEGPL(3)|SEGIG;
200 d1 |= SEGPL(3)|SEGIG;
204 d1 |= SEGPL(0)|SEGIG;
208 idt->d0 = (vaddr & 0xFFFF)|(KESEL<<16);
212 idt->d0 = (vaddr >> 32);
225 * Syscall() is called directly without going through trap().
227 trapenable(VectorBPT, debugbpt, 0, "debugpt");
228 trapenable(VectorPF, fault386, 0, "fault386");
229 trapenable(Vector2F, doublefault, 0, "doublefault");
230 trapenable(Vector15, unexpected, 0, "unexpected");
232 addarchfile("irqalloc", 0444, irqallocread, nil);
236 static char* excname[32] = {
239 "nonmaskable interrupt",
244 "coprocessor not available",
246 "coprocessor segment overrun",
248 "segment not present",
250 "general protection violation",
272 * keep histogram of interrupt service times
275 intrtime(Mach*, int vno)
281 diff = x - m->perf.intrts;
284 m->perf.inintr += diff;
285 if(up == nil && m->perf.inidle > diff)
286 m->perf.inidle -= diff;
288 diff /= m->cpumhz*100; /* quantum = 100µsec */
291 intrtimes[vno][diff]++;
294 /* go to user space */
301 /* precise time accounting, kernel exit */
302 tos = (Tos*)((uintptr)USTKTOP-sizeof(Tos));
304 tos->kcycles += t - up->kentry;
305 tos->pcycles = t + up->pcycles;
312 int clockintr, i, vno, user;
318 /* fault386 can give a better error message */
319 if(ureg->type == VectorPF)
321 panic("trap %llud: not ready", ureg->type);
324 m->perf.intrts = perfticks();
325 user = userureg(ureg);
339 m->lastintr = ctl->irq;
343 for(v = ctl; v != nil; v = v->next){
353 if(ctl->irq == IrqCLOCK || ctl->irq == IrqTIMER)
360 else if(vno < nelem(excname) && user){
362 sprint(buf, "sys: trap: %s", excname[vno]);
363 postnote(up, 1, buf, NDebug);
365 else if(vno >= VectorPIC){
367 * An unknown interrupt.
368 * Check for a default IRQ7. This can happen when
369 * the IRQ input goes away before the acknowledge.
370 * In this case, a 'default IRQ7' is generated, but
371 * the corresponding bit in the ISR isn't set.
372 * In fact, just ignore all such interrupts.
375 /* call all interrupt routines, just in case */
376 for(i = VectorPIC; i <= MaxIrqLAPIC; i++){
382 for(v = ctl; v != nil; v = v->next){
386 /* should we do this? */
391 /* clear the interrupt */
394 if(0)print("cpu%d: spurious interrupt %d, last %d\n",
395 m->machno, vno, m->lastintr);
396 if(0)if(conf.nmach > 1){
397 for(i = 0; i < 32; i++){
398 if(!(active.machs & (1<<i)))
401 if(m->machno == mach->machno)
403 print(" cpu%d: last %d",
404 mach->machno, mach->lastintr);
414 if(vno == VectorNMI){
416 * Don't re-enable, it confuses the crash dumps.
419 iprint("cpu%d: PC %#p\n", m->machno, ureg->pc);
420 while(m->machno != 0)
427 extern void _rdmsrinst(void);
428 extern void _wrmsrinst(void);
430 pc = (void*)ureg->pc;
431 if(pc == _rdmsrinst || pc == _wrmsrinst){
432 if(vno == VectorGPF){
442 ureg->sp = (uintptr)&ureg->sp;
445 if(vno < nelem(excname))
446 panic("%s", excname[vno]);
447 panic("unknown trap/intr: %d", vno);
451 /* delaysched set because we held a lock or because our quantum ended */
452 if(up && up->delaysched && clockintr){
458 if(up->procctl || up->nnote)
468 iprint("cpu%d: registers for %s %lud\n",
469 m->machno, up->text, up->pid);
471 iprint("cpu%d: registers for kernel\n", m->machno);
473 iprint(" AX %.16lluX BX %.16lluX CX %.16lluX\n",
474 ureg->ax, ureg->bx, ureg->cx);
475 iprint(" DX %.16lluX SI %.16lluX DI %.16lluX\n",
476 ureg->dx, ureg->si, ureg->di);
477 iprint(" BP %.16lluX R8 %.16lluX R9 %.16lluX\n",
478 ureg->bp, ureg->r8, ureg->r9);
479 iprint(" R10 %.16lluX R11 %.16lluX R12 %.16lluX\n",
480 ureg->r10, ureg->r11, ureg->r12);
481 iprint(" R13 %.16lluX R14 %.16lluX R15 %.16lluX\n",
482 ureg->r13, ureg->r14, ureg->r15);
483 iprint(" CS %.4lluX SS %.4lluX PC %.16lluX SP %.16lluX\n",
484 ureg->cs & 0xffff, ureg->ss & 0xffff, ureg->pc, ureg->sp);
485 iprint("TYPE %.2lluX ERROR %.4lluX FLAGS %.8lluX\n",
486 ureg->type & 0xff, ureg->error & 0xffff, ureg->flags & 0xffffffff);
489 * Processor control registers.
490 * If machine check exception, time stamp counter, page size extensions
491 * or enhanced virtual 8086 mode extensions are supported, there is a
492 * CR4. If there is a CR4 and machine check extensions, read the machine
493 * check address and machine check type registers if RDMSR supported.
495 iprint(" CR0 %8.8llux CR2 %16.16llux CR3 %16.16llux",
496 getcr0(), getcr2(), getcr3());
497 if(m->cpuiddx & (Mce|Tsc|Pse|Vmex)){
498 iprint(" CR4 %16.16llux\n", getcr4());
502 iprint(" ur %#p up %#p\n", ureg, up);
507 * Fill in enough of Ureg to get a stack trace, and call a function.
508 * Used by debugging interface rdb.
511 callwithureg(void (*fn)(Ureg*))
514 ureg.pc = getcallerpc(&fn);
515 ureg.sp = (uintptr)&fn;
520 _dumpstack(Ureg *ureg)
522 uintptr l, v, i, estack;
527 if((s = getconf("*nodumpstack")) != nil && strcmp(s, "0") != 0){
528 iprint("dumpstack disabled\n");
531 iprint("dumpstack\n");
534 x += iprint("ktrace /kernel/path %#p %#p <<EOF\n", ureg->pc, ureg->sp);
537 && (uintptr)&l >= (uintptr)up->kstack
538 && (uintptr)&l <= (uintptr)up->kstack+KSTACK)
539 estack = (uintptr)up->kstack+KSTACK;
540 else if((uintptr)&l >= (uintptr)m->stack
541 && (uintptr)&l <= (uintptr)m+MACHSIZE)
542 estack = (uintptr)m+MACHSIZE;
545 x += iprint("estackx %p\n", estack);
547 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
549 if((KTZERO < v && v < (uintptr)&etext) || estack-l < 32){
551 * Could Pick off general CALL (((uchar*)v)[-5] == 0xE8)
552 * and CALL indirect through AX
553 * (((uchar*)v)[-2] == 0xFF && ((uchar*)v)[-2] == 0xD0),
554 * but this is too clever and misses faulting address.
556 x += iprint("%.8lux=%.8lux ", (ulong)l, (ulong)v);
568 if(ureg->type != VectorNMI)
572 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
573 iprint("%.8p ", *(uintptr*)l);
586 callwithureg(_dumpstack);
590 debugbpt(Ureg* ureg, void*)
596 /* restore pc to instruction that caused the trap */
598 sprint(buf, "sys: breakpoint");
599 postnote(up, 1, buf, NDebug);
603 doublefault(Ureg*, void*)
605 panic("double fault");
609 unexpected(Ureg* ureg, void*)
611 print("unexpected trap %llud; ignoring\n", ureg->type);
614 extern void checkpages(void);
616 fault386(Ureg* ureg, void*)
619 int read, user, n, insyscall;
623 read = !(ureg->error & 2);
624 user = userureg(ureg);
629 panic("kernel fault: bad address pc=%#p addr=%#p", ureg->pc, addr);
631 panic("kernel fault: no user process pc=%#p addr=%#p", ureg->pc, addr);
634 panic("user fault: up=0 pc=%#p addr=%#p", ureg->pc, addr);
636 insyscall = up->insyscall;
638 n = fault(addr, read);
642 panic("fault: %#p", addr);
645 sprint(buf, "sys: trap: fault %s addr=%#p",
646 read ? "read" : "write", addr);
647 postnote(up, 1, buf, NDebug);
649 up->insyscall = insyscall;
655 #include "../port/systab.h"
658 * Syscall is called directly from assembler without going through trap().
668 vlong startns, stopns;
671 panic("syscall: cs 0x%4.4lluX", ureg->cs);
681 scallnr = ureg->bp; /* RARG */
682 up->scallnr = scallnr;
689 if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)-BY2WD))
690 validaddr(sp, sizeof(Sargs)+BY2WD, 0);
692 up->s = *((Sargs*)(sp+BY2WD));
694 syscallfmt(scallnr, ureg->pc, (va_list)up->s.args);
695 print("syscall: %s\n", up->syscalltrace);
698 if(up->procctl == Proc_tracesyscall){
699 syscallfmt(scallnr, ureg->pc, (va_list)up->s.args);
701 up->procctl = Proc_stopme;
704 startns = todget(nil);
706 if(scallnr >= nsyscall || systab[scallnr] == 0){
707 pprint("bad sys call number %lud pc %#p\n",
709 postnote(up, 1, "sys: bad sys call", NDebug);
712 up->psstate = sysctab[scallnr];
713 ret = systab[scallnr]((va_list)up->s.args);
716 /* failure: save the error buffer for errstr */
718 up->syserrstr = up->errstr;
720 if(0 && up->pid == 1)
721 print("syscall %lud error %s\n", scallnr, up->syserrstr);
724 print("bad errstack [%lud]: %d extra\n", scallnr, up->nerrlab);
725 for(i = 0; i < NERR; i++)
726 print("sp=%#p pc=%#p\n",
727 up->errlab[i].sp, up->errlab[i].pc);
728 panic("error stack");
733 print("syscallret: %lud %s %s ret=%lld\n",
734 up->pid, up->text, sysctab[scallnr], ret);
737 if(up->procctl == Proc_tracesyscall){
738 stopns = todget(nil);
739 sysretfmt(scallnr, (va_list)up->s.args, ret, startns, stopns);
741 up->procctl = Proc_stopme;
749 if(scallnr == NOTED){
750 noted(ureg, *((ulong*)up->s.args));
753 * normally, syscall() returns to forkret()
754 * not restoring general registers when going
755 * to userspace. to completely restore the
756 * interrupted context, we have to return thru
757 * noteret(). we override return pc to jump to
758 * to it when returning form syscall()
760 ((void**)&ureg)[-1] = (void*)noteret;
763 if(scallnr!=RFORK && (up->procctl || up->nnote)){
766 ((void**)&ureg)[-1] = (void*)noteret; /* loads RARG */
768 /* if we delayed sched because we held a lock, sched now */
775 * Call user, if necessary, with note.
776 * Pass user the Ureg struct and the note on his stack.
790 if(up->fpstate == FPactive){
792 up->fpstate = FPinactive;
794 up->fpstate |= FPillegal;
800 if(strncmp(n->msg, "sys:", 4) == 0){
802 if(l > ERRMAX-15) /* " pc=0x12345678\0" */
804 sprint(n->msg+l, " pc=%#p", ureg->pc);
807 if(n->flag!=NUser && (up->notified || up->notify==0)){
809 if(n->flag == NDebug)
810 pprint("suicide: %s\n", n->msg);
811 pexit(n->msg, n->flag!=NDebug);
822 pexit(n->msg, n->flag!=NDebug);
825 sp -= 256; /* debugging: preserve context causing problem */
827 if(0) print("%s %lud: notify %#p %#p %#p %s\n",
828 up->text, up->pid, ureg->pc, ureg->sp, sp, n->msg);
830 if(!okaddr((uintptr)up->notify, 1, 0)
831 || !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)){
833 pprint("suicide: bad address in notify\n");
837 memmove((Ureg*)sp, ureg, sizeof(Ureg));
838 *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */
839 up->ureg = (void*)sp;
841 memmove((char*)sp, up->note[0].msg, ERRMAX);
843 ((uintptr*)sp)[2] = sp + 3*BY2WD; /* arg2 string */
844 ((uintptr*)sp)[1] = (uintptr)up->ureg; /* arg1 is ureg* */
845 ((uintptr*)sp)[0] = 0; /* arg0 is pc */
847 ureg->pc = (uintptr)up->notify;
848 ureg->bp = (uintptr)up->ureg; /* arg1 passed in RARG */
853 memmove(&up->lastnote, &up->note[0], sizeof(Note));
854 memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
863 * Return user to state before notify()
866 noted(Ureg* ureg, ulong arg0)
872 if(arg0!=NRSTR && !up->notified) {
874 pprint("call to noted() when not notified\n");
879 nureg = up->ureg; /* pointer to user returned Ureg struct */
881 up->fpstate &= ~FPillegal;
884 oureg = (uintptr)nureg;
885 if(!okaddr(oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){
887 pprint("bad ureg in noted or call to noted when not notified\n");
891 /* don't let user change system flags or segment registers */
892 setregisters(ureg, (char*)ureg, (char*)nureg, sizeof(Ureg));
897 if(0) print("%s %lud: noted %#p %#p\n",
898 up->text, up->pid, nureg->pc, nureg->sp);
899 if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->sp, BY2WD, 0)){
901 pprint("suicide: trap in noted\n");
904 up->ureg = (Ureg*)(*(uintptr*)(oureg-BY2WD));
909 if(!okaddr(nureg->pc, 1, 0)
910 || !okaddr(nureg->sp, BY2WD, 0)){
912 pprint("suicide: trap in noted\n");
916 sp = oureg-4*BY2WD-ERRMAX;
919 ureg->bp = oureg; /* arg 1 passed in RARG */
920 ((uintptr*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */
921 ((uintptr*)sp)[0] = 0; /* arg 0 is pc */
925 up->lastnote.flag = NDebug;
930 if(up->lastnote.flag == NDebug)
931 pprint("suicide: %s\n", up->lastnote.msg);
932 pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
937 execregs(uintptr entry, ulong ssize, ulong nargs)
942 sp = (uintptr*)(USTKTOP - ssize);
945 ureg->sp = (uintptr)sp;
949 ureg->r14 = ureg->r15 = 0; /* extern user registers */
950 return (uintptr)USTKTOP-sizeof(Tos); /* address of kernel/user shared data */
954 * return the userpc the last exception happened at
961 ureg = (Ureg*)up->dbgreg;
965 /* This routine must save the values of registers the user is not permitted
966 * to write from devproc and noted() and then restore the saved values before returning.
969 setregisters(Ureg* ureg, char* pureg, char* uva, int n)
974 memmove(pureg, uva, n);
977 ureg->flags = (ureg->flags & 0x00ff) | (flags & 0xff00);
978 ureg->pc &= UADDRMASK;
985 up->kpfun(up->kparg);
986 pexit("kproc dying", 0);
990 kprocchild(Proc* p, void (*func)(void*), void* arg)
993 * gotolabel() needs a word on the stack in
994 * which to place the return PC used to jump
997 p->sched.pc = (uintptr)linkproc;
998 p->sched.sp = (uintptr)p->kstack+KSTACK-BY2WD;
1005 forkchild(Proc *p, Ureg *ureg)
1010 * Add 2*BY2WD to the stack to account for
1012 * - trap's argument (ur)
1014 p->sched.sp = (uintptr)p->kstack+KSTACK-(sizeof(Ureg)+2*BY2WD);
1015 p->sched.pc = (uintptr)forkret;
1017 cureg = (Ureg*)(p->sched.sp+2*BY2WD);
1018 memmove(cureg, ureg, sizeof(Ureg));
1022 /* Things from bottom of syscall which were never executed */
1027 /* Give enough context in the ureg to produce a kernel stack for
1028 * a sleeping process
1031 setkernur(Ureg* ureg, Proc* p)
1033 ureg->pc = p->sched.pc;
1034 ureg->sp = p->sched.sp+8;
1035 ureg->r14 = (uintptr)p;