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 faultamd64(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, faultamd64, 0, "faultamd64");
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 /* faultamd64 can give a better error message */
319 if(ureg->type == VectorPF)
320 faultamd64(ureg, nil);
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 < MAXMACH; i++){
398 if(active.machs[i] == 0)
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: nmi PC %#p, status %ux\n",
420 m->machno, ureg->pc, inb(0x61));
421 while(m->machno != 0)
428 extern void _rdmsrinst(void);
429 extern void _wrmsrinst(void);
431 pc = (void*)ureg->pc;
432 if(pc == _rdmsrinst || pc == _wrmsrinst){
433 if(vno == VectorGPF){
443 ureg->sp = (uintptr)&ureg->sp;
446 if(vno < nelem(excname))
447 panic("%s", excname[vno]);
448 panic("unknown trap/intr: %d", vno);
452 /* delaysched set because we held a lock or because our quantum ended */
453 if(up && up->delaysched && clockintr){
459 if(up->procctl || up->nnote)
469 iprint("cpu%d: registers for %s %lud\n",
470 m->machno, up->text, up->pid);
472 iprint("cpu%d: registers for kernel\n", m->machno);
474 iprint(" AX %.16lluX BX %.16lluX CX %.16lluX\n",
475 ureg->ax, ureg->bx, ureg->cx);
476 iprint(" DX %.16lluX SI %.16lluX DI %.16lluX\n",
477 ureg->dx, ureg->si, ureg->di);
478 iprint(" BP %.16lluX R8 %.16lluX R9 %.16lluX\n",
479 ureg->bp, ureg->r8, ureg->r9);
480 iprint(" R10 %.16lluX R11 %.16lluX R12 %.16lluX\n",
481 ureg->r10, ureg->r11, ureg->r12);
482 iprint(" R13 %.16lluX R14 %.16lluX R15 %.16lluX\n",
483 ureg->r13, ureg->r14, ureg->r15);
484 iprint(" CS %.4lluX SS %.4lluX PC %.16lluX SP %.16lluX\n",
485 ureg->cs & 0xffff, ureg->ss & 0xffff, ureg->pc, ureg->sp);
486 iprint("TYPE %.2lluX ERROR %.4lluX FLAGS %.8lluX\n",
487 ureg->type & 0xff, ureg->error & 0xffff, ureg->flags & 0xffffffff);
490 * Processor control registers.
491 * If machine check exception, time stamp counter, page size extensions
492 * or enhanced virtual 8086 mode extensions are supported, there is a
493 * CR4. If there is a CR4 and machine check extensions, read the machine
494 * check address and machine check type registers if RDMSR supported.
496 iprint(" CR0 %8.8llux CR2 %16.16llux CR3 %16.16llux",
497 getcr0(), getcr2(), getcr3());
498 if(m->cpuiddx & (Mce|Tsc|Pse|Vmex)){
499 iprint(" CR4 %16.16llux\n", getcr4());
503 iprint(" ur %#p up %#p\n", ureg, up);
508 * Fill in enough of Ureg to get a stack trace, and call a function.
509 * Used by debugging interface rdb.
512 callwithureg(void (*fn)(Ureg*))
515 ureg.pc = getcallerpc(&fn);
516 ureg.sp = (uintptr)&fn;
521 _dumpstack(Ureg *ureg)
523 uintptr l, v, i, estack;
528 if((s = getconf("*nodumpstack")) != nil && strcmp(s, "0") != 0){
529 iprint("dumpstack disabled\n");
532 iprint("dumpstack\n");
535 x += iprint("ktrace /kernel/path %#p %#p <<EOF\n", ureg->pc, ureg->sp);
538 && (uintptr)&l >= (uintptr)up->kstack
539 && (uintptr)&l <= (uintptr)up->kstack+KSTACK)
540 estack = (uintptr)up->kstack+KSTACK;
541 else if((uintptr)&l >= (uintptr)m->stack
542 && (uintptr)&l <= (uintptr)m+MACHSIZE)
543 estack = (uintptr)m+MACHSIZE;
546 x += iprint("estackx %p\n", estack);
548 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
550 if((KTZERO < v && v < (uintptr)&etext) || estack-l < 32){
552 * Could Pick off general CALL (((uchar*)v)[-5] == 0xE8)
553 * and CALL indirect through AX
554 * (((uchar*)v)[-2] == 0xFF && ((uchar*)v)[-2] == 0xD0),
555 * but this is too clever and misses faulting address.
557 x += iprint("%.8lux=%.8lux ", (ulong)l, (ulong)v);
569 if(ureg->type != VectorNMI)
573 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
574 iprint("%.8p ", *(uintptr*)l);
587 callwithureg(_dumpstack);
591 debugbpt(Ureg* ureg, void*)
597 /* restore pc to instruction that caused the trap */
599 sprint(buf, "sys: breakpoint");
600 postnote(up, 1, buf, NDebug);
604 doublefault(Ureg*, void*)
606 panic("double fault");
610 unexpected(Ureg* ureg, void*)
612 print("unexpected trap %llud; ignoring\n", ureg->type);
615 extern void checkpages(void);
617 faultamd64(Ureg* ureg, void*)
620 int read, user, n, insyscall;
624 read = !(ureg->error & 2);
625 user = userureg(ureg);
628 panic("kernel fault: bad address pc=%#p addr=%#p", ureg->pc, addr);
630 panic("kernel fault: no user process pc=%#p addr=%#p", ureg->pc, addr);
633 panic("user fault: up=0 pc=%#p addr=%#p", ureg->pc, addr);
635 insyscall = up->insyscall;
637 n = fault(addr, read);
641 panic("fault: %#p", addr);
644 sprint(buf, "sys: trap: fault %s addr=%#p",
645 read ? "read" : "write", addr);
646 postnote(up, 1, buf, NDebug);
648 up->insyscall = insyscall;
654 #include "../port/systab.h"
657 * Syscall is called directly from assembler without going through trap().
667 vlong startns, stopns;
670 panic("syscall: cs 0x%4.4lluX", ureg->cs);
680 scallnr = ureg->bp; /* RARG */
681 up->scallnr = scallnr;
688 if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)-BY2WD))
689 validaddr(sp, sizeof(Sargs)+BY2WD, 0);
691 up->s = *((Sargs*)(sp+BY2WD));
693 syscallfmt(scallnr, ureg->pc, (va_list)up->s.args);
694 print("syscall: %s\n", up->syscalltrace);
697 if(up->procctl == Proc_tracesyscall){
698 syscallfmt(scallnr, ureg->pc, (va_list)up->s.args);
700 up->procctl = Proc_stopme;
703 startns = todget(nil);
705 if(scallnr >= nsyscall || systab[scallnr] == 0){
706 pprint("bad sys call number %lud pc %#p\n",
708 postnote(up, 1, "sys: bad sys call", NDebug);
711 up->psstate = sysctab[scallnr];
712 ret = systab[scallnr]((va_list)up->s.args);
715 /* failure: save the error buffer for errstr */
717 up->syserrstr = up->errstr;
719 if(0 && up->pid == 1)
720 print("syscall %lud error %s\n", scallnr, up->syserrstr);
723 print("bad errstack [%lud]: %d extra\n", scallnr, up->nerrlab);
724 for(i = 0; i < NERR; i++)
725 print("sp=%#p pc=%#p\n",
726 up->errlab[i].sp, up->errlab[i].pc);
727 panic("error stack");
732 print("syscallret: %lud %s %s ret=%lld\n",
733 up->pid, up->text, sysctab[scallnr], ret);
736 if(up->procctl == Proc_tracesyscall){
737 stopns = todget(nil);
738 sysretfmt(scallnr, (va_list)up->s.args, ret, startns, stopns);
740 up->procctl = Proc_stopme;
748 if(scallnr == NOTED){
749 noted(ureg, *((ulong*)up->s.args));
752 * normally, syscall() returns to forkret()
753 * not restoring general registers when going
754 * to userspace. to completely restore the
755 * interrupted context, we have to return thru
756 * noteret(). we override return pc to jump to
757 * to it when returning form syscall()
759 ((void**)&ureg)[-1] = (void*)noteret;
762 if(scallnr!=RFORK && (up->procctl || up->nnote)){
765 ((void**)&ureg)[-1] = (void*)noteret; /* loads RARG */
767 /* if we delayed sched because we held a lock, sched now */
774 * Call user, if necessary, with note.
775 * Pass user the Ureg struct and the note on his stack.
789 if(up->fpstate == FPactive){
791 up->fpstate = FPinactive;
793 up->fpstate |= FPillegal;
799 if(strncmp(n->msg, "sys:", 4) == 0){
801 if(l > ERRMAX-15) /* " pc=0x12345678\0" */
803 sprint(n->msg+l, " pc=%#p", ureg->pc);
806 if(n->flag!=NUser && (up->notified || up->notify==0)){
808 if(n->flag == NDebug)
809 pprint("suicide: %s\n", n->msg);
810 pexit(n->msg, n->flag!=NDebug);
821 pexit(n->msg, n->flag!=NDebug);
824 sp -= 256; /* debugging: preserve context causing problem */
826 if(0) print("%s %lud: notify %#p %#p %#p %s\n",
827 up->text, up->pid, ureg->pc, ureg->sp, sp, n->msg);
829 if(!okaddr((uintptr)up->notify, 1, 0)
830 || !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)){
832 pprint("suicide: bad address in notify\n");
836 memmove((Ureg*)sp, ureg, sizeof(Ureg));
837 *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */
838 up->ureg = (void*)sp;
840 memmove((char*)sp, up->note[0].msg, ERRMAX);
842 ((uintptr*)sp)[2] = sp + 3*BY2WD; /* arg2 string */
843 ((uintptr*)sp)[1] = (uintptr)up->ureg; /* arg1 is ureg* */
844 ((uintptr*)sp)[0] = 0; /* arg0 is pc */
846 ureg->pc = (uintptr)up->notify;
847 ureg->bp = (uintptr)up->ureg; /* arg1 passed in RARG */
852 memmove(&up->lastnote, &up->note[0], sizeof(Note));
853 memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
862 * Return user to state before notify()
865 noted(Ureg* ureg, ulong arg0)
871 if(arg0!=NRSTR && !up->notified) {
873 pprint("call to noted() when not notified\n");
878 nureg = up->ureg; /* pointer to user returned Ureg struct */
880 up->fpstate &= ~FPillegal;
883 oureg = (uintptr)nureg;
884 if(!okaddr(oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){
886 pprint("bad ureg in noted or call to noted when not notified\n");
890 /* don't let user change system flags or segment registers */
891 setregisters(ureg, (char*)ureg, (char*)nureg, sizeof(Ureg));
896 if(0) print("%s %lud: noted %#p %#p\n",
897 up->text, up->pid, nureg->pc, nureg->sp);
898 if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->sp, BY2WD, 0)){
900 pprint("suicide: trap in noted\n");
903 up->ureg = (Ureg*)(*(uintptr*)(oureg-BY2WD));
908 if(!okaddr(nureg->pc, 1, 0)
909 || !okaddr(nureg->sp, BY2WD, 0)){
911 pprint("suicide: trap in noted\n");
915 sp = oureg-4*BY2WD-ERRMAX;
918 ureg->bp = oureg; /* arg 1 passed in RARG */
919 ((uintptr*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */
920 ((uintptr*)sp)[0] = 0; /* arg 0 is pc */
924 up->lastnote.flag = NDebug;
929 if(up->lastnote.flag == NDebug)
930 pprint("suicide: %s\n", up->lastnote.msg);
931 pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
936 execregs(uintptr entry, ulong ssize, ulong nargs)
941 sp = (uintptr*)(USTKTOP - ssize);
944 ureg->sp = (uintptr)sp;
948 ureg->r14 = ureg->r15 = 0; /* extern user registers */
949 return (uintptr)USTKTOP-sizeof(Tos); /* address of kernel/user shared data */
953 * return the userpc the last exception happened at
960 ureg = (Ureg*)up->dbgreg;
964 /* This routine must save the values of registers the user is not permitted
965 * to write from devproc and noted() and then restore the saved values before returning.
968 setregisters(Ureg* ureg, char* pureg, char* uva, int n)
973 memmove(pureg, uva, n);
976 ureg->flags = (ureg->flags & 0x00ff) | (flags & 0xff00);
977 ureg->pc &= UADDRMASK;
984 up->kpfun(up->kparg);
985 pexit("kproc dying", 0);
989 kprocchild(Proc* p, void (*func)(void*), void* arg)
992 * gotolabel() needs a word on the stack in
993 * which to place the return PC used to jump
996 p->sched.pc = (uintptr)linkproc;
997 p->sched.sp = (uintptr)p->kstack+KSTACK-BY2WD;
1004 forkchild(Proc *p, Ureg *ureg)
1009 * Add 2*BY2WD to the stack to account for
1011 * - trap's argument (ur)
1013 p->sched.sp = (uintptr)p->kstack+KSTACK-(sizeof(Ureg)+2*BY2WD);
1014 p->sched.pc = (uintptr)forkret;
1016 cureg = (Ureg*)(p->sched.sp+2*BY2WD);
1017 memmove(cureg, ureg, sizeof(Ureg));
1021 /* Things from bottom of syscall which were never executed */
1026 /* Give enough context in the ureg to produce a kernel stack for
1027 * a sleeping process
1030 setkernur(Ureg* ureg, Proc* p)
1032 ureg->pc = p->sched.pc;
1033 ureg->sp = p->sched.sp+8;
1034 ureg->r14 = (uintptr)p;