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)
86 * For now, none of this will work with the APIC code,
87 * there is no mapping between irq and vector as the IRQ
88 * is pretty meaningless.
90 if(arch->intrvecno == nil)
92 vno = arch->intrvecno(irq);
96 ((*pv)->irq != irq || (*pv)->tbdf != tbdf || (*pv)->f != f || (*pv)->a != a ||
97 strcmp((*pv)->name, name)))
102 *pv = (*pv)->next; /* Link out the entry */
104 if(vctl[vno] == nil && arch->intrdisable != nil)
105 arch->intrdisable(irq);
112 irqallocread(Chan*, void *vbuf, long n, vlong offset)
114 char *buf, *p, str[2*(11+1)+KNAMELEN+1+1];
119 if(n < 0 || offset < 0)
124 for(vno=0; vno<nelem(vctl); vno++){
125 for(v=vctl[vno]; v; v=v->next){
126 m = snprint(str, sizeof str, "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
127 if(m <= offset) /* if do not want this, skip entry */
130 /* skip offset bytes */
135 /* write at most max(n,m) bytes */
151 trapenable(int vno, void (*f)(Ureg*, void*), void* a, char *name)
155 if(vno < 0 || vno >= VectorPIC)
156 panic("trapenable: vno %d", vno);
157 if((v = xalloc(sizeof(Vctl))) == nil)
158 panic("trapenable: out of memory");
159 v->tbdf = BUSUNKNOWN;
162 strncpy(v->name, name, KNAMELEN-1);
163 v->name[KNAMELEN-1] = 0;
167 v->next = vctl[vno]->next;
178 * Hack: should be locked with NVRAM access.
180 outb(0x70, 0x80); /* NMI latch clear */
183 x = inb(0x61) & 0x07; /* Enable NMI */
189 * Minimal trap setup. Just enough so that we can panic
190 * on traps (bugs) during kernel initialization.
191 * Called very early - malloc is not yet available.
200 idt = (Segdesc*)IDTADDR;
201 vaddr = (ulong)vectortable;
202 for(v = 0; v < 256; v++){
203 d1 = (vaddr & 0xFFFF0000)|SEGP;
207 d1 |= SEGPL(3)|SEGIG;
211 d1 |= SEGPL(3)|SEGIG;
215 d1 |= SEGPL(0)|SEGIG;
218 idt[v].d0 = (vaddr & 0xFFFF)|(KESEL<<16);
229 * Syscall() is called directly without going through trap().
231 trapenable(VectorBPT, debugbpt, 0, "debugpt");
232 trapenable(VectorPF, fault386, 0, "fault386");
233 trapenable(Vector2F, doublefault, 0, "doublefault");
234 trapenable(Vector15, unexpected, 0, "unexpected");
237 addarchfile("irqalloc", 0444, irqallocread, nil);
241 static char* excname[32] = {
244 "nonmaskable interrupt",
249 "coprocessor not available",
251 "coprocessor segment overrun",
253 "segment not present",
255 "general protection violation",
277 * keep histogram of interrupt service times
280 intrtime(Mach*, int vno)
286 diff = x - m->perf.intrts;
289 m->perf.inintr += diff;
290 if(up == nil && m->perf.inidle > diff)
291 m->perf.inidle -= diff;
293 diff /= m->cpumhz*100; /* quantum = 100µsec */
296 intrtimes[vno][diff]++;
299 /* go to user space */
306 /* precise time accounting, kernel exit */
307 tos = (Tos*)(USTKTOP-sizeof(Tos));
309 tos->kcycles += t - up->kentry;
310 tos->pcycles = t + up->pcycles;
315 * All traps come here. It is slower to have all traps call trap()
316 * rather than directly vectoring the handler. However, this avoids a
317 * lot of code duplication and possible bugs. The only exception is
319 * Trap is called with interrupts disabled via interrupt-gates.
324 int clockintr, i, vno, user;
330 /* fault386 can give a better error message */
331 if(ureg->trap == VectorPF)
333 panic("trap %lud: not ready", ureg->trap);
336 m->perf.intrts = perfticks();
337 user = userureg(ureg);
349 if(vno >= VectorPIC && vno != VectorSYSCALL)
350 m->lastintr = ctl->irq;
355 for(v = ctl; v != nil; v = v->next){
365 if(ctl->irq == IrqCLOCK || ctl->irq == IrqTIMER)
372 else if(vno < nelem(excname) && user){
374 sprint(buf, "sys: trap: %s", excname[vno]);
375 postnote(up, 1, buf, NDebug);
377 else if(vno >= VectorPIC && vno != VectorSYSCALL){
379 * An unknown interrupt.
380 * Check for a default IRQ7. This can happen when
381 * the IRQ input goes away before the acknowledge.
382 * In this case, a 'default IRQ7' is generated, but
383 * the corresponding bit in the ISR isn't set.
384 * In fact, just ignore all such interrupts.
387 /* call all interrupt routines, just in case */
388 for(i = VectorPIC; i <= MaxIrqLAPIC; i++){
394 for(v = ctl; v != nil; v = v->next){
398 /* should we do this? */
403 /* clear the interrupt */
406 if(0)print("cpu%d: spurious interrupt %d, last %d\n",
407 m->machno, vno, m->lastintr);
408 if(0)if(conf.nmach > 1){
409 for(i = 0; i < 32; i++){
410 if(!(active.machs & (1<<i)))
413 if(m->machno == mach->machno)
415 print(" cpu%d: last %d",
416 mach->machno, mach->lastintr);
426 if(vno == VectorNMI){
428 * Don't re-enable, it confuses the crash dumps.
431 iprint("cpu%d: PC %#8.8lux\n", m->machno, ureg->pc);
432 while(m->machno != 0)
440 extern void _forkretpopgs(void);
441 extern void _forkretpopfs(void);
442 extern void _forkretpopes(void);
443 extern void _forkretpopds(void);
444 extern void _forkretiret(void);
445 extern void _rdmsrinst(void);
446 extern void _wrmsrinst(void);
448 extern void load_fs(ulong);
449 extern void load_gs(ulong);
454 sp = (ulong*)&ureg->sp; /* kernel stack */
455 pc = (void*)ureg->pc;
457 if(pc == _forkretpopgs || pc == _forkretpopfs ||
458 pc == _forkretpopes || pc == _forkretpopds){
459 if(vno == VectorGPF || vno == VectorSNP){
463 } else if(pc == _forkretiret){
464 if(vno == VectorGPF || vno == VectorSNP){
465 sp[1] = UESEL; /* CS */
466 sp[4] = UDSEL; /* SS */
469 } else if(pc == _rdmsrinst || pc == _wrmsrinst){
470 if(vno == VectorGPF){
480 ureg->sp = (ulong)&ureg->sp;
483 if(vno < nelem(excname))
484 panic("%s", excname[vno]);
485 panic("unknown trap/intr: %d", vno);
489 /* delaysched set because we held a lock or because our quantum ended */
490 if(up && up->delaysched && clockintr){
496 if(up->procctl || up->nnote)
506 dumpregs2(Ureg* ureg)
509 iprint("cpu%d: registers for %s %lud\n",
510 m->machno, up->text, up->pid);
512 iprint("cpu%d: registers for kernel\n", m->machno);
513 iprint("FLAGS=%luX TRAP=%luX ECODE=%luX PC=%luX",
514 ureg->flags, ureg->trap, ureg->ecode, ureg->pc);
516 iprint(" SS=%4.4luX USP=%luX\n", ureg->ss & 0xFFFF, ureg->usp);
518 iprint(" SP=%luX\n", (ulong)&ureg->sp);
519 iprint(" AX %8.8luX BX %8.8luX CX %8.8luX DX %8.8luX\n",
520 ureg->ax, ureg->bx, ureg->cx, ureg->dx);
521 iprint(" SI %8.8luX DI %8.8luX BP %8.8luX\n",
522 ureg->si, ureg->di, ureg->bp);
523 iprint(" CS %4.4luX DS %4.4luX ES %4.4luX FS %4.4luX GS %4.4luX\n",
524 ureg->cs & 0xFFFF, ureg->ds & 0xFFFF, ureg->es & 0xFFFF,
525 ureg->fs & 0xFFFF, ureg->gs & 0xFFFF);
536 * Processor control registers.
537 * If machine check exception, time stamp counter, page size extensions
538 * or enhanced virtual 8086 mode extensions are supported, there is a
539 * CR4. If there is a CR4 and machine check extensions, read the machine
540 * check address and machine check type registers if RDMSR supported.
542 iprint(" CR0 %8.8lux CR2 %8.8lux CR3 %8.8lux",
543 getcr0(), getcr2(), getcr3());
544 if(m->cpuiddx & (Mce|Tsc|Pse|Vmex)){
545 iprint(" CR4 %8.8lux", getcr4());
546 if((m->cpuiddx & (Mce|Cpumsr)) == (Mce|Cpumsr)){
549 iprint("\n MCA %8.8llux MCT %8.8llux", mca, mct);
552 iprint("\n ur %#p up %#p\n", ureg, up);
557 * Fill in enough of Ureg to get a stack trace, and call a function.
558 * Used by debugging interface rdb.
561 callwithureg(void (*fn)(Ureg*))
564 ureg.pc = getcallerpc(&fn);
565 ureg.sp = (ulong)&fn;
570 _dumpstack(Ureg *ureg)
572 uintptr l, v, i, estack;
577 if((s = getconf("*nodumpstack")) != nil && strcmp(s, "0") != 0){
578 iprint("dumpstack disabled\n");
581 iprint("dumpstack\n");
584 x += iprint("ktrace /kernel/path %.8lux %.8lux <<EOF\n", ureg->pc, ureg->sp);
587 && (uintptr)&l >= (uintptr)up->kstack
588 && (uintptr)&l <= (uintptr)up->kstack+KSTACK)
589 estack = (uintptr)up->kstack+KSTACK;
590 else if((uintptr)&l >= (uintptr)m->stack
591 && (uintptr)&l <= (uintptr)m+MACHSIZE)
592 estack = (uintptr)m+MACHSIZE;
595 x += iprint("estackx %p\n", estack);
597 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
599 if((KTZERO < v && v < (uintptr)&etext) || estack-l < 32){
601 * Could Pick off general CALL (((uchar*)v)[-5] == 0xE8)
602 * and CALL indirect through AX
603 * (((uchar*)v)[-2] == 0xFF && ((uchar*)v)[-2] == 0xD0),
604 * but this is too clever and misses faulting address.
606 x += iprint("%.8p=%.8p ", l, v);
618 if(ureg->trap != VectorNMI)
622 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
623 iprint("%.8p ", *(uintptr*)l);
636 callwithureg(_dumpstack);
640 debugbpt(Ureg* ureg, void*)
646 /* restore pc to instruction that caused the trap */
648 sprint(buf, "sys: breakpoint");
649 postnote(up, 1, buf, NDebug);
653 doublefault(Ureg*, void*)
655 panic("double fault");
659 unexpected(Ureg* ureg, void*)
661 print("unexpected trap %lud; ignoring\n", ureg->trap);
664 extern void checkpages(void);
665 extern void checkfault(ulong, ulong);
667 fault386(Ureg* ureg, void*)
670 int read, user, n, insyscall;
674 read = !(ureg->ecode & 2);
676 user = userureg(ureg);
681 panic("kernel fault: bad address pc=0x%.8lux addr=0x%.8lux", ureg->pc, addr);
683 panic("kernel fault: no user process pc=0x%.8lux addr=0x%.8lux", ureg->pc, addr);
686 panic("user fault: up=0 pc=0x%.8lux addr=0x%.8lux", ureg->pc, addr);
688 insyscall = up->insyscall;
690 n = fault(addr, read);
694 panic("fault: 0x%lux", addr);
697 checkfault(addr, ureg->pc);
698 sprint(buf, "sys: trap: fault %s addr=0x%lux",
699 read ? "read" : "write", addr);
700 postnote(up, 1, buf, NDebug);
702 up->insyscall = insyscall;
708 #include "../port/systab.h"
711 * Syscall is called directly from assembler without going through trap().
721 vlong startns, stopns;
724 panic("syscall: cs 0x%4.4luX", ureg->cs);
735 up->scallnr = scallnr;
742 if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)-BY2WD))
743 validaddr(sp, sizeof(Sargs)+BY2WD, 0);
745 up->s = *((Sargs*)(sp+BY2WD));
747 if(up->procctl == Proc_tracesyscall){
748 syscallfmt(scallnr, ureg->pc, (va_list)up->s.args);
750 up->procctl = Proc_stopme;
753 startns = todget(nil);
756 if(scallnr >= nsyscall || systab[scallnr] == 0){
757 pprint("bad sys call number %lud pc %lux\n",
759 postnote(up, 1, "sys: bad sys call", NDebug);
762 up->psstate = sysctab[scallnr];
763 ret = systab[scallnr]((va_list)up->s.args);
766 /* failure: save the error buffer for errstr */
768 up->syserrstr = up->errstr;
770 if(0 && up->pid == 1)
771 print("syscall %lud error %s\n", scallnr, up->syserrstr);
774 print("bad errstack [%lud]: %d extra\n", scallnr, up->nerrlab);
775 for(i = 0; i < NERR; i++)
776 print("sp=%lux pc=%lux\n",
777 up->errlab[i].sp, up->errlab[i].pc);
778 panic("error stack");
782 * Put return value in frame. On the x86 the syscall is
783 * just another trap and the return value from syscall is
784 * ignored. On other machines the return value is put into
785 * the results register by caller of syscall.
789 if(up->procctl == Proc_tracesyscall){
790 stopns = todget(nil);
791 sysretfmt(scallnr, (va_list)up->s.args, ret, startns, stopns);
793 up->procctl = Proc_stopme;
802 noted(ureg, *((ulong*)up->s.args));
804 if(scallnr!=RFORK && (up->procctl || up->nnote)){
808 /* if we delayed sched because we held a lock, sched now */
815 * Call user, if necessary, with note.
816 * Pass user the Ureg struct and the note on his stack.
830 if(up->fpstate == FPactive){
832 up->fpstate = FPinactive;
834 up->fpstate |= FPillegal;
840 if(strncmp(n->msg, "sys:", 4) == 0){
842 if(l > ERRMAX-15) /* " pc=0x12345678\0" */
844 sprint(n->msg+l, " pc=0x%.8lux", ureg->pc);
847 if(n->flag!=NUser && (up->notified || up->notify==0)){
849 if(n->flag == NDebug)
850 pprint("suicide: %s\n", n->msg);
851 pexit(n->msg, n->flag!=NDebug);
862 pexit(n->msg, n->flag!=NDebug);
865 sp -= 256; /* debugging: preserve context causing problem */
867 if(0) print("%s %lud: notify %.8lux %.8lux %.8lux %s\n",
868 up->text, up->pid, ureg->pc, ureg->usp, sp, n->msg);
870 if(!okaddr((uintptr)up->notify, 1, 0)
871 || !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)){
873 pprint("suicide: bad address in notify\n");
877 memmove((Ureg*)sp, ureg, sizeof(Ureg));
878 *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */
879 up->ureg = (void*)sp;
881 memmove((char*)sp, up->note[0].msg, ERRMAX);
883 *(ulong*)(sp+2*BY2WD) = sp+3*BY2WD; /* arg 2 is string */
884 *(ulong*)(sp+1*BY2WD) = (ulong)up->ureg; /* arg 1 is ureg* */
885 *(ulong*)(sp+0*BY2WD) = 0; /* arg 0 is pc */
887 ureg->pc = (ulong)up->notify;
889 ureg->ss = ureg->ds = ureg->es = UDSEL;
892 memmove(&up->lastnote, &up->note[0], sizeof(Note));
893 memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
901 * Return user to state before notify()
904 noted(Ureg* ureg, ulong arg0)
910 if(arg0!=NRSTR && !up->notified) {
912 pprint("call to noted() when not notified\n");
917 nureg = up->ureg; /* pointer to user returned Ureg struct */
919 up->fpstate &= ~FPillegal;
922 oureg = (ulong)nureg;
923 if(!okaddr(oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){
925 pprint("bad ureg in noted or call to noted when not notified\n");
929 /* don't let user change system flags */
930 nureg->flags = (ureg->flags & ~0xCD5) | (nureg->flags & 0xCD5);
934 memmove(ureg, nureg, sizeof(Ureg));
939 if(0) print("%s %lud: noted %.8lux %.8lux\n",
940 up->text, up->pid, nureg->pc, nureg->usp);
941 if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->usp, BY2WD, 0)){
943 pprint("suicide: trap in noted\n");
946 up->ureg = (Ureg*)(*(ulong*)(oureg-BY2WD));
951 if(!okaddr(nureg->pc, BY2WD, 0)
952 || !okaddr(nureg->usp, BY2WD, 0)){
954 pprint("suicide: trap in noted\n");
958 sp = oureg-4*BY2WD-ERRMAX;
961 ((ulong*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */
962 ((ulong*)sp)[0] = 0; /* arg 0 is pc */
966 up->lastnote.flag = NDebug;
971 if(up->lastnote.flag == NDebug)
972 pprint("suicide: %s\n", up->lastnote.msg);
973 pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
978 execregs(uintptr entry, ulong ssize, ulong nargs)
983 sp = (ulong*)(USTKTOP - ssize);
987 ureg->usp = (ulong)sp;
990 ureg->ss = ureg->ds = ureg->es = UDSEL;
991 ureg->fs = ureg->gs = NULLSEL;
992 return USTKTOP-sizeof(Tos); /* address of kernel/user shared data */
996 * return the userpc the last exception happened at
1003 ureg = (Ureg*)up->dbgreg;
1007 /* This routine must save the values of registers the user is not permitted
1008 * to write from devproc and then restore the saved values before returning.
1011 setregisters(Ureg* ureg, char* pureg, char* uva, int n)
1015 flags = ureg->flags;
1016 memmove(pureg, uva, n);
1017 ureg->flags = (ureg->flags & 0xCD5) | (flags & ~0xCD5);
1026 up->kpfun(up->kparg);
1027 pexit("kproc dying", 0);
1031 kprocchild(Proc* p, void (*func)(void*), void* arg)
1034 * gotolabel() needs a word on the stack in
1035 * which to place the return PC used to jump
1038 p->sched.pc = (ulong)linkproc;
1039 p->sched.sp = (ulong)p->kstack+KSTACK-BY2WD;
1046 forkchild(Proc *p, Ureg *ureg)
1051 * Add 2*BY2WD to the stack to account for
1053 * - trap's argument (ur)
1055 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Ureg)+2*BY2WD);
1056 p->sched.pc = (ulong)forkret;
1058 cureg = (Ureg*)(p->sched.sp+2*BY2WD);
1059 memmove(cureg, ureg, sizeof(Ureg));
1060 /* return value of syscall in child */
1063 /* Things from bottom of syscall which were never executed */
1068 /* Give enough context in the ureg to produce a kernel stack for
1069 * a sleeping process
1072 setkernur(Ureg* ureg, Proc* p)
1074 ureg->pc = p->sched.pc;
1075 ureg->sp = p->sched.sp+4;