3 #include "../port/lib.h"
9 #include "../port/error.h"
12 static int trapinited;
14 void noted(Ureg*, ulong);
16 static void debugexc(Ureg*, void*);
17 static void debugbpt(Ureg*, void*);
18 static void faultamd64(Ureg*, void*);
19 static void doublefault(Ureg*, void*);
20 static void unexpected(Ureg*, void*);
21 static void _dumpstack(Ureg*);
24 static Vctl *vctl[256];
28 Ntimevec = 20 /* number of time buckets for each intr */
30 ulong intrtimes[256][Ntimevec];
33 intrenable(int irq, void (*f)(Ureg*, void*), void* a, int tbdf, char *name)
39 print("intrenable: nil handler for %d, tbdf 0x%uX for %s\n",
44 if(tbdf != BUSUNKNOWN && (irq == 0xff || irq == 0)){
45 print("intrenable: got unassigned irq %d, tbdf 0x%uX for %s\n",
52 * IRQ2 doesn't really exist, it's used to gang the interrupt
53 * controllers together. A device set to IRQ2 will appear on
54 * the second interrupt controller as IRQ9.
59 if((v = xalloc(sizeof(Vctl))) == nil)
60 panic("intrenable: out of memory");
66 strncpy(v->name, name, KNAMELEN-1);
67 v->name[KNAMELEN-1] = 0;
70 vno = arch->intrenable(v);
73 print("intrenable: couldn't enable irq %d, tbdf 0x%uX for %s\n",
79 if(vctl[vno]->isr != v->isr || vctl[vno]->eoi != v->eoi)
80 panic("intrenable: handler: %s %s %#p %#p %#p %#p",
81 vctl[vno]->name, v->name,
82 vctl[vno]->isr, v->isr, vctl[vno]->eoi, v->eoi);
90 intrdisable(int irq, void (*f)(Ureg *, void *), void *a, int tbdf, char *name)
97 if(arch->intrvecno == nil || (tbdf != BUSUNKNOWN && (irq == 0xff || irq == 0))){
99 * on APIC machine, irq is pretty meaningless
100 * and disabling a the vector is not implemented.
101 * however, we still want to remove the matching
102 * Vctl entry to prevent calling Vctl.f() with a
103 * stale Vctl.a pointer.
108 vno = arch->intrvecno(irq);
112 for(pv = &vctl[vno]; (v = *pv) != nil; pv = &v->next){
113 if(v->isintr && (v->irq == irq || irq == -1)
114 && v->tbdf == tbdf && v->f == f && v->a == a
115 && strcmp(v->name, name) == 0)
122 if(irq != -1 && vctl[vno] == nil && arch->intrdisable != nil)
123 arch->intrdisable(irq);
126 } while(irq == -1 && ++vno <= MaxVectorAPIC);
131 irqallocread(Chan*, void *a, long n, vlong offset)
133 char buf[2*(11+1)+KNAMELEN+1+1];
137 if(n < 0 || offset < 0)
140 for(vno=0; vno<nelem(vctl); vno++){
141 for(v=vctl[vno]; v; v=v->next){
142 m = snprint(buf, sizeof(buf), "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
149 memmove(a, buf+offset, n);
157 trapenable(int vno, void (*f)(Ureg*, void*), void* a, char *name)
161 if(vno < 0 || vno >= VectorPIC)
162 panic("trapenable: vno %d", vno);
163 if((v = xalloc(sizeof(Vctl))) == nil)
164 panic("trapenable: out of memory");
165 v->tbdf = BUSUNKNOWN;
168 strncpy(v->name, name, KNAMELEN-1);
169 v->name[KNAMELEN-1] = 0;
173 v->next = vctl[vno]->next;
184 * Hack: should be locked with NVRAM access.
186 outb(0x70, 0x80); /* NMI latch clear */
189 x = inb(0x61) & 0x07; /* Enable NMI */
201 idt = (Segdesc*)IDTADDR;
202 vaddr = (uintptr)vectortable;
203 for(v = 0; v < 256; v++){
204 d1 = (vaddr & 0xFFFF0000)|SEGP;
208 d1 |= SEGPL(3)|SEGIG;
212 d1 |= SEGPL(3)|SEGIG;
216 d1 |= SEGPL(0)|SEGIG;
220 idt->d0 = (vaddr & 0xFFFF)|(KESEL<<16);
224 idt->d0 = (vaddr >> 32);
237 * Syscall() is called directly without going through trap().
239 trapenable(VectorDE, debugexc, 0, "debugexc");
240 trapenable(VectorBPT, debugbpt, 0, "debugpt");
241 trapenable(VectorPF, faultamd64, 0, "faultamd64");
242 trapenable(Vector2F, doublefault, 0, "doublefault");
243 trapenable(Vector15, unexpected, 0, "unexpected");
245 addarchfile("irqalloc", 0444, irqallocread, nil);
249 static char* excname[32] = {
252 "nonmaskable interrupt",
257 "coprocessor not available",
259 "coprocessor segment overrun",
261 "segment not present",
263 "general protection violation",
285 * keep histogram of interrupt service times
288 intrtime(Mach*, int vno)
294 diff = x - m->perf.intrts;
297 m->perf.inintr += diff;
298 if(up == nil && m->perf.inidle > diff)
299 m->perf.inidle -= diff;
301 diff /= m->cpumhz*100; /* quantum = 100µsec */
304 intrtimes[vno][diff]++;
307 /* go to user space */
314 /* precise time accounting, kernel exit */
315 tos = (Tos*)((uintptr)USTKTOP-sizeof(Tos));
317 tos->kcycles += t - up->kentry;
318 tos->pcycles = t + up->pcycles;
325 int clockintr, i, vno, user;
331 /* faultamd64 can give a better error message */
332 if(ureg->type == VectorPF)
333 faultamd64(ureg, nil);
334 panic("trap %llud: not ready", ureg->type);
337 m->perf.intrts = perfticks();
338 user = userureg(ureg);
352 m->lastintr = ctl->irq;
356 for(v = ctl; v != nil; v = v->next){
366 if(ctl->irq == IrqCLOCK || ctl->irq == IrqTIMER)
373 else if(vno < nelem(excname) && user){
375 sprint(buf, "sys: trap: %s", excname[vno]);
376 postnote(up, 1, buf, NDebug);
378 else if(vno >= VectorPIC){
380 * An unknown interrupt.
381 * Check for a default IRQ7. This can happen when
382 * the IRQ input goes away before the acknowledge.
383 * In this case, a 'default IRQ7' is generated, but
384 * the corresponding bit in the ISR isn't set.
385 * In fact, just ignore all such interrupts.
388 /* call all interrupt routines, just in case */
389 for(i = VectorPIC; i <= MaxIrqLAPIC; i++){
395 for(v = ctl; v != nil; v = v->next){
399 /* should we do this? */
404 /* clear the interrupt */
407 if(0)print("cpu%d: spurious interrupt %d, last %d\n",
408 m->machno, vno, m->lastintr);
409 if(0)if(conf.nmach > 1){
410 for(i = 0; i < MAXMACH; i++){
411 if(active.machs[i] == 0)
414 if(m->machno == mach->machno)
416 print(" cpu%d: last %d",
417 mach->machno, mach->lastintr);
427 if(vno == VectorNMI){
429 * Don't re-enable, it confuses the crash dumps.
432 iprint("cpu%d: nmi PC %#p, status %ux\n",
433 m->machno, ureg->pc, inb(0x61));
434 while(m->machno != 0)
441 extern void _rdmsrinst(void);
442 extern void _wrmsrinst(void);
444 pc = (void*)ureg->pc;
445 if(pc == _rdmsrinst || pc == _wrmsrinst){
446 if(vno == VectorGPF){
456 ureg->sp = (uintptr)&ureg->sp;
459 if(vno < nelem(excname))
460 panic("%s", excname[vno]);
461 panic("unknown trap/intr: %d", vno);
465 /* delaysched set because we held a lock or because our quantum ended */
466 if(up && up->delaysched && clockintr){
472 if(up->procctl || up->nnote)
482 iprint("cpu%d: registers for %s %lud\n",
483 m->machno, up->text, up->pid);
485 iprint("cpu%d: registers for kernel\n", m->machno);
487 iprint(" AX %.16lluX BX %.16lluX CX %.16lluX\n",
488 ureg->ax, ureg->bx, ureg->cx);
489 iprint(" DX %.16lluX SI %.16lluX DI %.16lluX\n",
490 ureg->dx, ureg->si, ureg->di);
491 iprint(" BP %.16lluX R8 %.16lluX R9 %.16lluX\n",
492 ureg->bp, ureg->r8, ureg->r9);
493 iprint(" R10 %.16lluX R11 %.16lluX R12 %.16lluX\n",
494 ureg->r10, ureg->r11, ureg->r12);
495 iprint(" R13 %.16lluX R14 %.16lluX R15 %.16lluX\n",
496 ureg->r13, ureg->r14, ureg->r15);
497 iprint(" CS %.4lluX SS %.4lluX PC %.16lluX SP %.16lluX\n",
498 ureg->cs & 0xffff, ureg->ss & 0xffff, ureg->pc, ureg->sp);
499 iprint("TYPE %.2lluX ERROR %.4lluX FLAGS %.8lluX\n",
500 ureg->type & 0xff, ureg->error & 0xffff, ureg->flags & 0xffffffff);
503 * Processor control registers.
504 * If machine check exception, time stamp counter, page size extensions
505 * or enhanced virtual 8086 mode extensions are supported, there is a
506 * CR4. If there is a CR4 and machine check extensions, read the machine
507 * check address and machine check type registers if RDMSR supported.
509 iprint(" CR0 %8.8llux CR2 %16.16llux CR3 %16.16llux",
510 getcr0(), getcr2(), getcr3());
511 if(m->cpuiddx & (Mce|Tsc|Pse|Vmex)){
512 iprint(" CR4 %16.16llux\n", getcr4());
516 iprint(" ur %#p up %#p\n", ureg, up);
521 * Fill in enough of Ureg to get a stack trace, and call a function.
522 * Used by debugging interface rdb.
525 callwithureg(void (*fn)(Ureg*))
528 ureg.pc = getcallerpc(&fn);
529 ureg.sp = (uintptr)&fn;
534 _dumpstack(Ureg *ureg)
536 uintptr l, v, i, estack;
541 if((s = getconf("*nodumpstack")) != nil && strcmp(s, "0") != 0){
542 iprint("dumpstack disabled\n");
545 iprint("dumpstack\n");
548 x += iprint("ktrace /kernel/path %#p %#p <<EOF\n", ureg->pc, ureg->sp);
551 && (uintptr)&l >= (uintptr)up->kstack
552 && (uintptr)&l <= (uintptr)up->kstack+KSTACK)
553 estack = (uintptr)up->kstack+KSTACK;
554 else if((uintptr)&l >= (uintptr)m->stack
555 && (uintptr)&l <= (uintptr)m+MACHSIZE)
556 estack = (uintptr)m+MACHSIZE;
559 x += iprint("estackx %p\n", estack);
561 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
563 if((KTZERO < v && v < (uintptr)&etext) || estack-l < 32){
565 * Could Pick off general CALL (((uchar*)v)[-5] == 0xE8)
566 * and CALL indirect through AX
567 * (((uchar*)v)[-2] == 0xFF && ((uchar*)v)[-2] == 0xD0),
568 * but this is too clever and misses faulting address.
570 x += iprint("%.8lux=%.8lux ", (ulong)l, (ulong)v);
582 if(ureg->type != VectorNMI)
586 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
587 iprint("%.8p ", *(uintptr*)l);
600 callwithureg(_dumpstack);
604 debugexc(Ureg *ureg, void *)
613 panic("kernel debug exception dr6=%#.8ullx", dr6);
617 else if(!canqlock(&up->debug))
620 m = (m >> 4 | m >> 3) & 8 | (m >> 3 | m >> 2) & 4 | (m >> 2 | m >> 1) & 2 | (m >> 1 | m) & 1;
623 sprint(buf, "sys: debug exception dr6=%#.8ullx", dr6);
624 postnote(up, 0, buf, NDebug);
627 e = buf + sizeof(buf);
628 p = seprint(p, e, "sys: watchpoint ");
629 for(i = 0; i < 4; i++)
631 p = seprint(p, e, "%d%s", i, (m >> i + 1 != 0) ? "," : "");
632 postnote(up, 0, buf, NDebug);
638 debugbpt(Ureg* ureg, void*)
644 /* restore pc to instruction that caused the trap */
646 sprint(buf, "sys: breakpoint");
647 postnote(up, 1, buf, NDebug);
651 doublefault(Ureg*, void*)
653 panic("double fault");
657 unexpected(Ureg* ureg, void*)
659 print("unexpected trap %llud; ignoring\n", ureg->type);
662 extern void checkpages(void);
665 faultamd64(Ureg* ureg, void*)
668 int read, user, n, insyscall, f;
672 read = !(ureg->error & 2);
673 user = userureg(ureg);
676 panic("kernel fault: bad address pc=%#p addr=%#p", ureg->pc, addr);
678 panic("kernel fault: no user process pc=%#p addr=%#p", ureg->pc, addr);
681 panic("user fault: up=0 pc=%#p addr=%#p", ureg->pc, addr);
683 insyscall = up->insyscall;
686 if(!user && waserror()){
689 up->insyscall = insyscall;
693 n = fault(addr, read);
697 panic("fault: %#p", addr);
700 sprint(buf, "sys: trap: fault %s addr=%#p",
701 read ? "read" : "write", addr);
702 postnote(up, 1, buf, NDebug);
704 if(!user) poperror();
707 up->insyscall = insyscall;
713 #include "../port/systab.h"
716 * Syscall is called directly from assembler without going through trap().
726 vlong startns, stopns;
729 panic("syscall: cs 0x%4.4lluX", ureg->cs);
739 scallnr = ureg->bp; /* RARG */
740 up->scallnr = scallnr;
748 if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)-BY2WD))
749 validaddr(sp, sizeof(Sargs)+BY2WD, 0);
751 up->s = *((Sargs*)(sp+BY2WD));
753 syscallfmt(scallnr, ureg->pc, (va_list)up->s.args);
754 print("syscall: %s\n", up->syscalltrace);
757 if(up->procctl == Proc_tracesyscall){
758 syscallfmt(scallnr, ureg->pc, (va_list)up->s.args);
760 up->procctl = Proc_stopme;
763 startns = todget(nil);
765 if(scallnr >= nsyscall || systab[scallnr] == 0){
766 pprint("bad sys call number %lud pc %#p\n",
768 postnote(up, 1, "sys: bad sys call", NDebug);
771 up->psstate = sysctab[scallnr];
772 ret = systab[scallnr]((va_list)up->s.args);
775 /* failure: save the error buffer for errstr */
777 up->syserrstr = up->errstr;
779 if(0 && up->pid == 1)
780 print("syscall %lud error %s\n", scallnr, up->syserrstr);
783 print("bad errstack [%lud]: %d extra\n", scallnr, up->nerrlab);
784 for(i = 0; i < NERR; i++)
785 print("sp=%#p pc=%#p\n",
786 up->errlab[i].sp, up->errlab[i].pc);
787 panic("error stack");
792 print("syscallret: %lud %s %s ret=%lld\n",
793 up->pid, up->text, sysctab[scallnr], ret);
796 if(up->procctl == Proc_tracesyscall){
797 stopns = todget(nil);
798 sysretfmt(scallnr, (va_list)up->s.args, ret, startns, stopns);
800 up->procctl = Proc_stopme;
810 if(scallnr == NOTED){
811 noted(ureg, *((ulong*)up->s.args));
813 * normally, syscall() returns to forkret()
814 * not restoring general registers when going
815 * to userspace. to completely restore the
816 * interrupted context, we have to return thru
817 * noteret(). we override return pc to jump to
818 * to it when returning form syscall()
820 ((void**)&ureg)[-1] = (void*)noteret;
823 if(scallnr!=RFORK && (up->procctl || up->nnote)){
825 ((void**)&ureg)[-1] = (void*)noteret; /* loads RARG */
828 /* if we delayed sched because we held a lock, sched now */
835 * Call user, if necessary, with note.
836 * Pass user the Ureg struct and the note on his stack.
853 if(strncmp(n->msg, "sys:", 4) == 0){
855 if(l > ERRMAX-15) /* " pc=0x12345678\0" */
857 sprint(n->msg+l, " pc=%#p", ureg->pc);
860 if(n->flag!=NUser && (up->notified || up->notify==0)){
862 if(n->flag == NDebug)
863 pprint("suicide: %s\n", n->msg);
864 pexit(n->msg, n->flag!=NDebug);
875 pexit(n->msg, n->flag!=NDebug);
878 sp -= 256; /* debugging: preserve context causing problem */
880 if(0) print("%s %lud: notify %#p %#p %#p %s\n",
881 up->text, up->pid, ureg->pc, ureg->sp, sp, n->msg);
883 if(!okaddr((uintptr)up->notify, 1, 0)
884 || !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)){
886 pprint("suicide: bad address in notify\n");
890 memmove((Ureg*)sp, ureg, sizeof(Ureg));
891 *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */
892 up->ureg = (void*)sp;
894 memmove((char*)sp, up->note[0].msg, ERRMAX);
896 ((uintptr*)sp)[2] = sp + 3*BY2WD; /* arg2 string */
897 ((uintptr*)sp)[1] = (uintptr)up->ureg; /* arg1 is ureg* */
898 ((uintptr*)sp)[0] = 0; /* arg0 is pc */
900 ureg->pc = (uintptr)up->notify;
901 ureg->bp = (uintptr)up->ureg; /* arg1 passed in RARG */
906 memmove(&up->lastnote, &up->note[0], sizeof(Note));
907 memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
910 if(up->fpstate == FPactive){
912 up->fpstate = FPinactive;
914 up->fpstate |= FPillegal;
919 * Return user to state before notify()
922 noted(Ureg* ureg, ulong arg0)
927 up->fpstate &= ~FPillegal;
930 if(arg0!=NRSTR && !up->notified) {
932 pprint("call to noted() when not notified\n");
937 nureg = up->ureg; /* pointer to user returned Ureg struct */
940 oureg = (uintptr)nureg;
941 if(!okaddr(oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){
943 pprint("bad ureg in noted or call to noted when not notified\n");
947 /* don't let user change system flags or segment registers */
948 setregisters(ureg, (char*)ureg, (char*)nureg, sizeof(Ureg));
953 if(0) print("%s %lud: noted %#p %#p\n",
954 up->text, up->pid, nureg->pc, nureg->sp);
955 if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->sp, BY2WD, 0)){
957 pprint("suicide: trap in noted\n");
960 up->ureg = (Ureg*)(*(uintptr*)(oureg-BY2WD));
965 if(!okaddr(nureg->pc, 1, 0)
966 || !okaddr(nureg->sp, BY2WD, 0)){
968 pprint("suicide: trap in noted\n");
972 sp = oureg-4*BY2WD-ERRMAX;
975 ureg->bp = oureg; /* arg 1 passed in RARG */
976 ((uintptr*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */
977 ((uintptr*)sp)[0] = 0; /* arg 0 is pc */
981 up->lastnote.flag = NDebug;
986 if(up->lastnote.flag == NDebug)
987 pprint("suicide: %s\n", up->lastnote.msg);
988 pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
993 execregs(uintptr entry, ulong ssize, ulong nargs)
998 sp = (uintptr*)(USTKTOP - ssize);
1001 ureg->sp = (uintptr)sp;
1005 ureg->r14 = ureg->r15 = 0; /* extern user registers */
1006 return (uintptr)USTKTOP-sizeof(Tos); /* address of kernel/user shared data */
1010 * return the userpc the last exception happened at
1017 ureg = (Ureg*)up->dbgreg;
1021 /* This routine must save the values of registers the user is not permitted
1022 * to write from devproc and noted() and then restore the saved values before returning.
1025 setregisters(Ureg* ureg, char* pureg, char* uva, int n)
1029 flags = ureg->flags;
1030 memmove(pureg, uva, n);
1033 ureg->flags = (ureg->flags & 0x00ff) | (flags & 0xff00);
1034 ureg->pc &= UADDRMASK;
1041 up->kpfun(up->kparg);
1042 pexit("kproc dying", 0);
1046 kprocchild(Proc* p, void (*func)(void*), void* arg)
1049 * gotolabel() needs a word on the stack in
1050 * which to place the return PC used to jump
1053 p->sched.pc = (uintptr)linkproc;
1054 p->sched.sp = (uintptr)p->kstack+KSTACK-BY2WD;
1061 forkchild(Proc *p, Ureg *ureg)
1066 * Add 2*BY2WD to the stack to account for
1068 * - trap's argument (ur)
1070 p->sched.sp = (uintptr)p->kstack+KSTACK-(sizeof(Ureg)+2*BY2WD);
1071 p->sched.pc = (uintptr)forkret;
1073 cureg = (Ureg*)(p->sched.sp+2*BY2WD);
1074 memmove(cureg, ureg, sizeof(Ureg));
1078 /* Things from bottom of syscall which were never executed */
1083 /* Give enough context in the ureg to produce a kernel stack for
1084 * a sleeping process
1087 setkernur(Ureg* ureg, Proc* p)
1089 ureg->pc = p->sched.pc;
1090 ureg->sp = p->sched.sp+8;
1091 ureg->r14 = (uintptr)p;