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 fault386(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",
50 if((v = xalloc(sizeof(Vctl))) == nil)
51 panic("intrenable: out of memory");
57 strncpy(v->name, name, KNAMELEN-1);
58 v->name[KNAMELEN-1] = 0;
61 vno = arch->intrenable(v);
64 print("intrenable: couldn't enable irq %d, tbdf 0x%uX for %s\n",
70 if(vctl[vno]->isr != v->isr || vctl[vno]->eoi != v->eoi)
71 panic("intrenable: handler: %s %s %#p %#p %#p %#p",
72 vctl[vno]->name, v->name,
73 vctl[vno]->isr, v->isr, vctl[vno]->eoi, v->eoi);
81 intrdisable(int irq, void (*f)(Ureg *, void *), void *a, int tbdf, char *name)
86 if(arch->intrvecno == nil || (tbdf != BUSUNKNOWN && (irq == 0xff || irq == 0))){
88 * on APIC machine, irq is pretty meaningless
89 * and disabling a the vector is not implemented.
90 * however, we still want to remove the matching
91 * Vctl entry to prevent calling Vctl.f() with a
92 * stale Vctl.a pointer.
97 vno = arch->intrvecno(irq);
101 for(pv = &vctl[vno]; (v = *pv) != nil; pv = &v->next){
102 if(v->isintr && (v->irq == irq || irq == -1)
103 && v->tbdf == tbdf && v->f == f && v->a == a
104 && strcmp(v->name, name) == 0)
111 if(irq != -1 && vctl[vno] == nil && arch->intrdisable != nil)
112 arch->intrdisable(irq);
115 } while(irq == -1 && ++vno <= MaxVectorAPIC);
120 irqallocread(Chan*, void *a, long n, vlong offset)
122 char buf[2*(11+1)+KNAMELEN+1+1];
126 if(n < 0 || offset < 0)
129 for(vno=0; vno<nelem(vctl); vno++){
130 for(v=vctl[vno]; v; v=v->next){
131 m = snprint(buf, sizeof(buf), "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
138 memmove(a, buf+offset, n);
146 trapenable(int vno, void (*f)(Ureg*, void*), void* a, char *name)
150 if(vno < 0 || vno >= VectorPIC)
151 panic("trapenable: vno %d", vno);
152 if((v = xalloc(sizeof(Vctl))) == nil)
153 panic("trapenable: out of memory");
154 v->tbdf = BUSUNKNOWN;
157 strncpy(v->name, name, KNAMELEN-1);
158 v->name[KNAMELEN-1] = 0;
162 v->next = vctl[vno]->next;
173 * Hack: should be locked with NVRAM access.
175 outb(0x70, 0x80); /* NMI latch clear */
178 x = inb(0x61) & 0x07; /* Enable NMI */
184 * Minimal trap setup. Just enough so that we can panic
185 * on traps (bugs) during kernel initialization.
186 * Called very early - malloc is not yet available.
195 idt = (Segdesc*)IDTADDR;
196 vaddr = (ulong)vectortable;
197 for(v = 0; v < 256; v++){
198 d1 = (vaddr & 0xFFFF0000)|SEGP;
202 d1 |= SEGPL(3)|SEGIG;
206 d1 |= SEGPL(3)|SEGIG;
210 d1 |= SEGPL(0)|SEGIG;
213 idt[v].d0 = (vaddr & 0xFFFF)|(KESEL<<16);
224 * Syscall() is called directly without going through trap().
226 trapenable(VectorDE, debugexc, 0, "debugexc");
227 trapenable(VectorBPT, debugbpt, 0, "debugpt");
228 trapenable(VectorPF, fault386, 0, "fault386");
229 trapenable(Vector2F, doublefault, 0, "doublefault");
230 trapenable(Vector15, unexpected, 0, "unexpected");
233 addarchfile("irqalloc", 0444, irqallocread, nil);
237 static char* excname[32] = {
240 "nonmaskable interrupt",
245 "coprocessor not available",
247 "coprocessor segment overrun",
249 "segment not present",
251 "general protection violation",
273 * keep histogram of interrupt service times
276 intrtime(Mach*, int vno)
282 diff = x - m->perf.intrts;
285 m->perf.inintr += diff;
286 if(up == nil && m->perf.inidle > diff)
287 m->perf.inidle -= diff;
289 diff /= m->cpumhz*100; /* quantum = 100µsec */
292 intrtimes[vno][diff]++;
295 /* go to user space */
302 /* precise time accounting, kernel exit */
303 tos = (Tos*)(USTKTOP-sizeof(Tos));
305 tos->kcycles += t - up->kentry;
306 tos->pcycles = t + up->pcycles;
311 * All traps come here. It is slower to have all traps call trap()
312 * rather than directly vectoring the handler. However, this avoids a
313 * lot of code duplication and possible bugs. The only exception is
315 * Trap is called with interrupts disabled via interrupt-gates.
320 int clockintr, i, vno, user;
326 /* fault386 can give a better error message */
327 if(ureg->trap == VectorPF)
329 panic("trap %lud: not ready", ureg->trap);
332 m->perf.intrts = perfticks();
333 user = userureg(ureg);
345 if(vno >= VectorPIC && vno != VectorSYSCALL)
346 m->lastintr = ctl->irq;
351 for(v = ctl; v != nil; v = v->next){
361 if(ctl->irq == IrqCLOCK || ctl->irq == IrqTIMER)
368 else if(vno < nelem(excname) && user){
370 sprint(buf, "sys: trap: %s", excname[vno]);
371 postnote(up, 1, buf, NDebug);
373 else if(vno >= VectorPIC && vno != VectorSYSCALL){
375 * An unknown interrupt.
376 * Check for a default IRQ7. This can happen when
377 * the IRQ input goes away before the acknowledge.
378 * In this case, a 'default IRQ7' is generated, but
379 * the corresponding bit in the ISR isn't set.
380 * In fact, just ignore all such interrupts.
383 /* call all interrupt routines, just in case */
384 for(i = VectorPIC; i <= MaxIrqLAPIC; i++){
390 for(v = ctl; v != nil; v = v->next){
394 /* should we do this? */
399 /* clear the interrupt */
402 if(0)print("cpu%d: spurious interrupt %d, last %d\n",
403 m->machno, vno, m->lastintr);
404 if(0)if(conf.nmach > 1){
405 for(i = 0; i < MAXMACH; i++){
406 if(active.machs[i] == 0)
409 if(m->machno == mach->machno)
411 print(" cpu%d: last %d",
412 mach->machno, mach->lastintr);
422 if(vno == VectorNMI){
424 * Don't re-enable, it confuses the crash dumps.
427 iprint("cpu%d: nmi PC %#8.8lux, status %ux\n",
428 m->machno, ureg->pc, inb(0x61));
429 while(m->machno != 0)
437 extern void _forkretpopgs(void);
438 extern void _forkretpopfs(void);
439 extern void _forkretpopes(void);
440 extern void _forkretpopds(void);
441 extern void _forkretiret(void);
442 extern void _rdmsrinst(void);
443 extern void _wrmsrinst(void);
445 extern void load_fs(ulong);
446 extern void load_gs(ulong);
451 sp = (ulong*)&ureg->sp; /* kernel stack */
452 pc = (void*)ureg->pc;
454 if(pc == _forkretpopgs || pc == _forkretpopfs ||
455 pc == _forkretpopes || pc == _forkretpopds){
456 if(vno == VectorGPF || vno == VectorSNP){
460 } else if(pc == _forkretiret){
461 if(vno == VectorGPF || vno == VectorSNP){
462 sp[1] = UESEL; /* CS */
463 sp[4] = UDSEL; /* SS */
466 } else if(pc == _rdmsrinst || pc == _wrmsrinst){
467 if(vno == VectorGPF){
477 ureg->sp = (ulong)&ureg->sp;
480 if(vno < nelem(excname))
481 panic("%s", excname[vno]);
482 panic("unknown trap/intr: %d", vno);
486 /* delaysched set because we held a lock or because our quantum ended */
487 if(up && up->delaysched && clockintr){
493 if(up->procctl || up->nnote)
503 dumpregs2(Ureg* ureg)
506 iprint("cpu%d: registers for %s %lud\n",
507 m->machno, up->text, up->pid);
509 iprint("cpu%d: registers for kernel\n", m->machno);
510 iprint("FLAGS=%luX TRAP=%luX ECODE=%luX PC=%luX",
511 ureg->flags, ureg->trap, ureg->ecode, ureg->pc);
513 iprint(" SS=%4.4luX USP=%luX\n", ureg->ss & 0xFFFF, ureg->usp);
515 iprint(" SP=%luX\n", (ulong)&ureg->sp);
516 iprint(" AX %8.8luX BX %8.8luX CX %8.8luX DX %8.8luX\n",
517 ureg->ax, ureg->bx, ureg->cx, ureg->dx);
518 iprint(" SI %8.8luX DI %8.8luX BP %8.8luX\n",
519 ureg->si, ureg->di, ureg->bp);
520 iprint(" CS %4.4luX DS %4.4luX ES %4.4luX FS %4.4luX GS %4.4luX\n",
521 ureg->cs & 0xFFFF, ureg->ds & 0xFFFF, ureg->es & 0xFFFF,
522 ureg->fs & 0xFFFF, ureg->gs & 0xFFFF);
531 * Processor control registers.
532 * If machine check exception, time stamp counter, page size extensions
533 * or enhanced virtual 8086 mode extensions are supported, there is a
534 * CR4. If there is a CR4 and machine check extensions, read the machine
535 * check address and machine check type registers if RDMSR supported.
537 iprint(" CR0 %8.8lux CR2 %8.8lux CR3 %8.8lux",
538 getcr0(), getcr2(), getcr3());
539 if(m->cpuiddx & (Mce|Tsc|Pse|Vmex)){
540 iprint(" CR4 %8.8lux\n", getcr4());
544 iprint("\n ur %#p up %#p\n", ureg, up);
549 * Fill in enough of Ureg to get a stack trace, and call a function.
550 * Used by debugging interface rdb.
553 callwithureg(void (*fn)(Ureg*))
556 ureg.pc = getcallerpc(&fn);
557 ureg.sp = (ulong)&fn;
562 _dumpstack(Ureg *ureg)
564 uintptr l, v, i, estack;
569 if((s = getconf("*nodumpstack")) != nil && strcmp(s, "0") != 0){
570 iprint("dumpstack disabled\n");
573 iprint("dumpstack\n");
576 x += iprint("ktrace /kernel/path %.8lux %.8lux <<EOF\n", ureg->pc, ureg->sp);
579 && (uintptr)&l >= (uintptr)up->kstack
580 && (uintptr)&l <= (uintptr)up->kstack+KSTACK)
581 estack = (uintptr)up->kstack+KSTACK;
582 else if((uintptr)&l >= (uintptr)m->stack
583 && (uintptr)&l <= (uintptr)m+MACHSIZE)
584 estack = (uintptr)m+MACHSIZE;
587 x += iprint("estackx %p\n", estack);
589 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
591 if((KTZERO < v && v < (uintptr)&etext) || estack-l < 32){
593 * Could Pick off general CALL (((uchar*)v)[-5] == 0xE8)
594 * and CALL indirect through AX
595 * (((uchar*)v)[-2] == 0xFF && ((uchar*)v)[-2] == 0xD0),
596 * but this is too clever and misses faulting address.
598 x += iprint("%.8p=%.8p ", l, v);
610 if(ureg->trap != VectorNMI)
614 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
615 iprint("%.8p ", *(uintptr*)l);
628 callwithureg(_dumpstack);
632 debugexc(Ureg *, void *)
641 panic("kernel debug exception dr6=%#.8ux", dr6);
644 m = (m >> 4 | m >> 3) & 8 | (m >> 3 | m >> 2) & 4 | (m >> 2 | m >> 1) & 2 | (m >> 1 | m) & 1;
647 sprint(buf, "sys: debug exception dr6=%#.8ux", dr6);
648 postnote(up, 1, buf, NDebug);
651 e = buf + sizeof(buf);
652 p = seprint(p, e, "sys: watchpoint ");
653 for(i = 0; i < 4; i++)
655 p = seprint(p, e, "%d%s", i, (m >> i + 1 != 0) ? "," : "");
656 postnote(up, 1, buf, NDebug);
661 debugbpt(Ureg* ureg, void*)
667 /* restore pc to instruction that caused the trap */
669 sprint(buf, "sys: breakpoint");
670 postnote(up, 1, buf, NDebug);
674 doublefault(Ureg*, void*)
676 panic("double fault");
680 unexpected(Ureg* ureg, void*)
682 print("unexpected trap %lud; ignoring\n", ureg->trap);
685 extern void checkpages(void);
686 extern void checkfault(ulong, ulong);
688 fault386(Ureg* ureg, void*)
691 int read, user, n, insyscall;
695 read = !(ureg->ecode & 2);
697 user = userureg(ureg);
702 panic("kernel fault: bad address pc=0x%.8lux addr=0x%.8lux", ureg->pc, addr);
704 panic("kernel fault: no user process pc=0x%.8lux addr=0x%.8lux", ureg->pc, addr);
707 panic("user fault: up=0 pc=0x%.8lux addr=0x%.8lux", ureg->pc, addr);
709 insyscall = up->insyscall;
711 n = fault(addr, read);
715 panic("fault: 0x%lux", addr);
718 checkfault(addr, ureg->pc);
719 sprint(buf, "sys: trap: fault %s addr=0x%lux",
720 read ? "read" : "write", addr);
721 postnote(up, 1, buf, NDebug);
723 up->insyscall = insyscall;
729 #include "../port/systab.h"
732 * Syscall is called directly from assembler without going through trap().
742 vlong startns, stopns;
745 panic("syscall: cs 0x%4.4luX", ureg->cs);
756 up->scallnr = scallnr;
763 if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)-BY2WD))
764 validaddr(sp, sizeof(Sargs)+BY2WD, 0);
766 up->s = *((Sargs*)(sp+BY2WD));
768 if(up->procctl == Proc_tracesyscall){
769 syscallfmt(scallnr, ureg->pc, (va_list)up->s.args);
771 up->procctl = Proc_stopme;
774 startns = todget(nil);
777 if(scallnr >= nsyscall || systab[scallnr] == 0){
778 pprint("bad sys call number %lud pc %lux\n",
780 postnote(up, 1, "sys: bad sys call", NDebug);
783 up->psstate = sysctab[scallnr];
784 ret = systab[scallnr]((va_list)up->s.args);
787 /* failure: save the error buffer for errstr */
789 up->syserrstr = up->errstr;
791 if(0 && up->pid == 1)
792 print("syscall %lud error %s\n", scallnr, up->syserrstr);
795 print("bad errstack [%lud]: %d extra\n", scallnr, up->nerrlab);
796 for(i = 0; i < NERR; i++)
797 print("sp=%lux pc=%lux\n",
798 up->errlab[i].sp, up->errlab[i].pc);
799 panic("error stack");
803 * Put return value in frame. On the x86 the syscall is
804 * just another trap and the return value from syscall is
805 * ignored. On other machines the return value is put into
806 * the results register by caller of syscall.
810 if(up->procctl == Proc_tracesyscall){
811 stopns = todget(nil);
812 sysretfmt(scallnr, (va_list)up->s.args, ret, startns, stopns);
814 up->procctl = Proc_stopme;
823 noted(ureg, *((ulong*)up->s.args));
825 if(scallnr!=RFORK && (up->procctl || up->nnote)){
829 /* if we delayed sched because we held a lock, sched now */
836 * Call user, if necessary, with note.
837 * Pass user the Ureg struct and the note on his stack.
851 if(up->fpstate == FPactive){
853 up->fpstate = FPinactive;
855 up->fpstate |= FPillegal;
861 if(strncmp(n->msg, "sys:", 4) == 0){
863 if(l > ERRMAX-15) /* " pc=0x12345678\0" */
865 sprint(n->msg+l, " pc=0x%.8lux", ureg->pc);
868 if(n->flag!=NUser && (up->notified || up->notify==0)){
870 if(n->flag == NDebug)
871 pprint("suicide: %s\n", n->msg);
872 pexit(n->msg, n->flag!=NDebug);
883 pexit(n->msg, n->flag!=NDebug);
886 sp -= 256; /* debugging: preserve context causing problem */
888 if(0) print("%s %lud: notify %.8lux %.8lux %.8lux %s\n",
889 up->text, up->pid, ureg->pc, ureg->usp, sp, n->msg);
891 if(!okaddr((uintptr)up->notify, 1, 0)
892 || !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)){
894 pprint("suicide: bad address in notify\n");
898 memmove((Ureg*)sp, ureg, sizeof(Ureg));
899 *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */
900 up->ureg = (void*)sp;
902 memmove((char*)sp, up->note[0].msg, ERRMAX);
904 *(ulong*)(sp+2*BY2WD) = sp+3*BY2WD; /* arg 2 is string */
905 *(ulong*)(sp+1*BY2WD) = (ulong)up->ureg; /* arg 1 is ureg* */
906 *(ulong*)(sp+0*BY2WD) = 0; /* arg 0 is pc */
908 ureg->pc = (ulong)up->notify;
910 ureg->ss = ureg->ds = ureg->es = UDSEL;
913 memmove(&up->lastnote, &up->note[0], sizeof(Note));
914 memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
922 * Return user to state before notify()
925 noted(Ureg* ureg, ulong arg0)
931 if(arg0!=NRSTR && !up->notified) {
933 pprint("call to noted() when not notified\n");
938 nureg = up->ureg; /* pointer to user returned Ureg struct */
940 up->fpstate &= ~FPillegal;
943 oureg = (ulong)nureg;
944 if(!okaddr(oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){
946 pprint("bad ureg in noted or call to noted when not notified\n");
950 /* don't let user change system flags */
951 nureg->flags = (ureg->flags & ~0xCD5) | (nureg->flags & 0xCD5);
955 memmove(ureg, nureg, sizeof(Ureg));
960 if(0) print("%s %lud: noted %.8lux %.8lux\n",
961 up->text, up->pid, nureg->pc, nureg->usp);
962 if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->usp, BY2WD, 0)){
964 pprint("suicide: trap in noted\n");
967 up->ureg = (Ureg*)(*(ulong*)(oureg-BY2WD));
972 if(!okaddr(nureg->pc, BY2WD, 0)
973 || !okaddr(nureg->usp, BY2WD, 0)){
975 pprint("suicide: trap in noted\n");
979 sp = oureg-4*BY2WD-ERRMAX;
982 ((ulong*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */
983 ((ulong*)sp)[0] = 0; /* arg 0 is pc */
987 up->lastnote.flag = NDebug;
992 if(up->lastnote.flag == NDebug)
993 pprint("suicide: %s\n", up->lastnote.msg);
994 pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
999 execregs(uintptr entry, ulong ssize, ulong nargs)
1004 sp = (ulong*)(USTKTOP - ssize);
1008 ureg->usp = (ulong)sp;
1011 ureg->ss = ureg->ds = ureg->es = UDSEL;
1012 ureg->fs = ureg->gs = NULLSEL;
1013 return USTKTOP-sizeof(Tos); /* address of kernel/user shared data */
1017 * return the userpc the last exception happened at
1024 ureg = (Ureg*)up->dbgreg;
1028 /* This routine must save the values of registers the user is not permitted
1029 * to write from devproc and then restore the saved values before returning.
1032 setregisters(Ureg* ureg, char* pureg, char* uva, int n)
1036 flags = ureg->flags;
1037 memmove(pureg, uva, n);
1038 ureg->flags = (ureg->flags & 0xCD5) | (flags & ~0xCD5);
1047 up->kpfun(up->kparg);
1048 pexit("kproc dying", 0);
1052 kprocchild(Proc* p, void (*func)(void*), void* arg)
1055 * gotolabel() needs a word on the stack in
1056 * which to place the return PC used to jump
1059 p->sched.pc = (ulong)linkproc;
1060 p->sched.sp = (ulong)p->kstack+KSTACK-BY2WD;
1067 forkchild(Proc *p, Ureg *ureg)
1072 * Add 2*BY2WD to the stack to account for
1074 * - trap's argument (ur)
1076 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Ureg)+2*BY2WD);
1077 p->sched.pc = (ulong)forkret;
1079 cureg = (Ureg*)(p->sched.sp+2*BY2WD);
1080 memmove(cureg, ureg, sizeof(Ureg));
1081 /* return value of syscall in child */
1084 /* Things from bottom of syscall which were never executed */
1089 /* Give enough context in the ureg to produce a kernel stack for
1090 * a sleeping process
1093 setkernur(Ureg* ureg, Proc* p)
1095 ureg->pc = p->sched.pc;
1096 ureg->sp = p->sched.sp+4;