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",
43 if(tbdf != BUSUNKNOWN && (irq == 0xff || irq == 0)){
44 print("intrenable: got unassigned irq %d, tbdf 0x%uX for %s\n",
50 * IRQ2 doesn't really exist, it's used to gang the interrupt
51 * controllers together. A device set to IRQ2 will appear on
52 * the second interrupt controller as IRQ9.
57 if((v = xalloc(sizeof(Vctl))) == nil)
58 panic("intrenable: out of memory");
64 strncpy(v->name, name, KNAMELEN-1);
65 v->name[KNAMELEN-1] = 0;
68 vno = arch->intrenable(v);
71 print("intrenable: couldn't enable irq %d, tbdf 0x%uX for %s\n",
77 if(vctl[vno]->isr != v->isr || vctl[vno]->eoi != v->eoi)
78 panic("intrenable: handler: %s %s %#p %#p %#p %#p",
79 vctl[vno]->name, v->name,
80 vctl[vno]->isr, v->isr, vctl[vno]->eoi, v->eoi);
88 intrdisable(int irq, void (*f)(Ureg *, void *), void *a, int tbdf, char *name)
95 if(arch->intrvecno == nil || (tbdf != BUSUNKNOWN && (irq == 0xff || irq == 0))){
97 * on APIC machine, irq is pretty meaningless
98 * and disabling a the vector is not implemented.
99 * however, we still want to remove the matching
100 * Vctl entry to prevent calling Vctl.f() with a
101 * stale Vctl.a pointer.
106 vno = arch->intrvecno(irq);
110 for(pv = &vctl[vno]; (v = *pv) != nil; pv = &v->next){
111 if(v->isintr && (v->irq == irq || irq == -1)
112 && v->tbdf == tbdf && v->f == f && v->a == a
113 && strcmp(v->name, name) == 0)
120 if(irq != -1 && vctl[vno] == nil && arch->intrdisable != nil)
121 arch->intrdisable(irq);
124 } while(irq == -1 && ++vno <= MaxVectorAPIC);
129 irqallocread(Chan*, void *a, long n, vlong offset)
131 char buf[2*(11+1)+KNAMELEN+1+1];
135 if(n < 0 || offset < 0)
138 for(vno=0; vno<nelem(vctl); vno++){
139 for(v=vctl[vno]; v; v=v->next){
140 m = snprint(buf, sizeof(buf), "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
147 memmove(a, buf+offset, n);
155 trapenable(int vno, void (*f)(Ureg*, void*), void* a, char *name)
159 if(vno < 0 || vno >= VectorPIC)
160 panic("trapenable: vno %d", vno);
161 if((v = xalloc(sizeof(Vctl))) == nil)
162 panic("trapenable: out of memory");
163 v->tbdf = BUSUNKNOWN;
166 strncpy(v->name, name, KNAMELEN-1);
167 v->name[KNAMELEN-1] = 0;
171 v->next = vctl[vno]->next;
182 * Hack: should be locked with NVRAM access.
184 outb(0x70, 0x80); /* NMI latch clear */
187 x = inb(0x61) & 0x07; /* Enable NMI */
193 * Minimal trap setup. Just enough so that we can panic
194 * on traps (bugs) during kernel initialization.
195 * Called very early - malloc is not yet available.
204 idt = (Segdesc*)IDTADDR;
205 vaddr = (ulong)vectortable;
206 for(v = 0; v < 256; v++){
207 d1 = (vaddr & 0xFFFF0000)|SEGP;
211 d1 |= SEGPL(3)|SEGIG;
215 d1 |= SEGPL(3)|SEGIG;
219 d1 |= SEGPL(0)|SEGIG;
222 idt[v].d0 = (vaddr & 0xFFFF)|(KESEL<<16);
233 * Syscall() is called directly without going through trap().
235 trapenable(VectorDE, debugexc, 0, "debugexc");
236 trapenable(VectorBPT, debugbpt, 0, "debugpt");
237 trapenable(VectorPF, fault386, 0, "fault386");
238 trapenable(Vector2F, doublefault, 0, "doublefault");
239 trapenable(Vector15, unexpected, 0, "unexpected");
242 addarchfile("irqalloc", 0444, irqallocread, nil);
246 static char* excname[32] = {
249 "nonmaskable interrupt",
254 "coprocessor not available",
256 "coprocessor segment overrun",
258 "segment not present",
260 "general protection violation",
282 * keep histogram of interrupt service times
285 intrtime(Mach*, int vno)
291 diff = x - m->perf.intrts;
294 m->perf.inintr += diff;
295 if(up == nil && m->perf.inidle > diff)
296 m->perf.inidle -= diff;
298 diff /= m->cpumhz*100; /* quantum = 100µsec */
301 intrtimes[vno][diff]++;
304 /* go to user space */
311 /* precise time accounting, kernel exit */
312 tos = (Tos*)(USTKTOP-sizeof(Tos));
314 tos->kcycles += t - up->kentry;
315 tos->pcycles = t + up->pcycles;
320 * All traps come here. It is slower to have all traps call trap()
321 * rather than directly vectoring the handler. However, this avoids a
322 * lot of code duplication and possible bugs. The only exception is
324 * Trap is called with interrupts disabled via interrupt-gates.
329 int clockintr, i, vno, user;
335 /* fault386 can give a better error message */
336 if(ureg->trap == VectorPF)
338 panic("trap %lud: not ready", ureg->trap);
341 m->perf.intrts = perfticks();
342 user = userureg(ureg);
354 if(vno >= VectorPIC && vno != VectorSYSCALL)
355 m->lastintr = ctl->irq;
360 for(v = ctl; v != nil; v = v->next){
370 if(ctl->irq == IrqCLOCK || ctl->irq == IrqTIMER)
377 else if(vno < nelem(excname) && user){
379 sprint(buf, "sys: trap: %s", excname[vno]);
380 postnote(up, 1, buf, NDebug);
382 else if(vno >= VectorPIC && vno != VectorSYSCALL){
384 * An unknown interrupt.
385 * Check for a default IRQ7. This can happen when
386 * the IRQ input goes away before the acknowledge.
387 * In this case, a 'default IRQ7' is generated, but
388 * the corresponding bit in the ISR isn't set.
389 * In fact, just ignore all such interrupts.
392 /* call all interrupt routines, just in case */
393 for(i = VectorPIC; i <= MaxIrqLAPIC; i++){
399 for(v = ctl; v != nil; v = v->next){
403 /* should we do this? */
408 /* clear the interrupt */
411 if(0)print("cpu%d: spurious interrupt %d, last %d\n",
412 m->machno, vno, m->lastintr);
413 if(0)if(conf.nmach > 1){
414 for(i = 0; i < MAXMACH; i++){
415 if(active.machs[i] == 0)
418 if(m->machno == mach->machno)
420 print(" cpu%d: last %d",
421 mach->machno, mach->lastintr);
431 if(vno == VectorNMI){
433 * Don't re-enable, it confuses the crash dumps.
436 iprint("cpu%d: nmi PC %#8.8lux, status %ux\n",
437 m->machno, ureg->pc, inb(0x61));
438 while(m->machno != 0)
446 extern void _forkretpopgs(void);
447 extern void _forkretpopfs(void);
448 extern void _forkretpopes(void);
449 extern void _forkretpopds(void);
450 extern void _forkretiret(void);
451 extern void _rdmsrinst(void);
452 extern void _wrmsrinst(void);
454 extern void load_fs(ulong);
455 extern void load_gs(ulong);
460 sp = (ulong*)&ureg->sp; /* kernel stack */
461 pc = (void*)ureg->pc;
463 if(pc == _forkretpopgs || pc == _forkretpopfs ||
464 pc == _forkretpopes || pc == _forkretpopds){
465 if(vno == VectorGPF || vno == VectorSNP){
469 } else if(pc == _forkretiret){
470 if(vno == VectorGPF || vno == VectorSNP){
471 sp[1] = UESEL; /* CS */
472 sp[4] = UDSEL; /* SS */
475 } else if(pc == _rdmsrinst || pc == _wrmsrinst){
476 if(vno == VectorGPF){
486 ureg->sp = (ulong)&ureg->sp;
489 if(vno < nelem(excname))
490 panic("%s", excname[vno]);
491 panic("unknown trap/intr: %d", vno);
495 /* delaysched set because we held a lock or because our quantum ended */
496 if(up && up->delaysched && clockintr){
502 if(up->procctl || up->nnote)
512 dumpregs2(Ureg* ureg)
515 iprint("cpu%d: registers for %s %lud\n",
516 m->machno, up->text, up->pid);
518 iprint("cpu%d: registers for kernel\n", m->machno);
519 iprint("FLAGS=%luX TRAP=%luX ECODE=%luX PC=%luX",
520 ureg->flags, ureg->trap, ureg->ecode, ureg->pc);
522 iprint(" SS=%4.4luX USP=%luX\n", ureg->ss & 0xFFFF, ureg->usp);
524 iprint(" SP=%luX\n", (ulong)&ureg->sp);
525 iprint(" AX %8.8luX BX %8.8luX CX %8.8luX DX %8.8luX\n",
526 ureg->ax, ureg->bx, ureg->cx, ureg->dx);
527 iprint(" SI %8.8luX DI %8.8luX BP %8.8luX\n",
528 ureg->si, ureg->di, ureg->bp);
529 iprint(" CS %4.4luX DS %4.4luX ES %4.4luX FS %4.4luX GS %4.4luX\n",
530 ureg->cs & 0xFFFF, ureg->ds & 0xFFFF, ureg->es & 0xFFFF,
531 ureg->fs & 0xFFFF, ureg->gs & 0xFFFF);
540 * Processor control registers.
541 * If machine check exception, time stamp counter, page size extensions
542 * or enhanced virtual 8086 mode extensions are supported, there is a
543 * CR4. If there is a CR4 and machine check extensions, read the machine
544 * check address and machine check type registers if RDMSR supported.
546 iprint(" CR0 %8.8lux CR2 %8.8lux CR3 %8.8lux",
547 getcr0(), getcr2(), getcr3());
548 if(m->cpuiddx & (Mce|Tsc|Pse|Vmex)){
549 iprint(" CR4 %8.8lux\n", getcr4());
553 iprint("\n ur %#p up %#p\n", ureg, up);
558 * Fill in enough of Ureg to get a stack trace, and call a function.
559 * Used by debugging interface rdb.
562 callwithureg(void (*fn)(Ureg*))
565 ureg.pc = getcallerpc(&fn);
566 ureg.sp = (ulong)&fn;
571 _dumpstack(Ureg *ureg)
573 uintptr l, v, i, estack;
578 if((s = getconf("*nodumpstack")) != nil && strcmp(s, "0") != 0){
579 iprint("dumpstack disabled\n");
582 iprint("dumpstack\n");
585 x += iprint("ktrace /kernel/path %.8lux %.8lux <<EOF\n", ureg->pc, ureg->sp);
588 && (uintptr)&l >= (uintptr)up->kstack
589 && (uintptr)&l <= (uintptr)up->kstack+KSTACK)
590 estack = (uintptr)up->kstack+KSTACK;
591 else if((uintptr)&l >= (uintptr)m->stack
592 && (uintptr)&l <= (uintptr)m+MACHSIZE)
593 estack = (uintptr)m+MACHSIZE;
596 x += iprint("estackx %p\n", estack);
598 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
600 if((KTZERO < v && v < (uintptr)&etext) || estack-l < 32){
602 * Could Pick off general CALL (((uchar*)v)[-5] == 0xE8)
603 * and CALL indirect through AX
604 * (((uchar*)v)[-2] == 0xFF && ((uchar*)v)[-2] == 0xD0),
605 * but this is too clever and misses faulting address.
607 x += iprint("%.8p=%.8p ", l, v);
619 if(ureg->trap != VectorNMI)
623 for(l = (uintptr)&l; l < estack; l += sizeof(uintptr)){
624 iprint("%.8p ", *(uintptr*)l);
637 callwithureg(_dumpstack);
641 debugexc(Ureg *ureg, void *)
650 panic("kernel debug exception dr6=%#.8ux", dr6);
654 else if(!canqlock(&up->debug))
657 m = (m >> 4 | m >> 3) & 8 | (m >> 3 | m >> 2) & 4 | (m >> 2 | m >> 1) & 2 | (m >> 1 | m) & 1;
660 sprint(buf, "sys: debug exception dr6=%#.8ux", dr6);
661 postnote(up, 0, buf, NDebug);
664 e = buf + sizeof(buf);
665 p = seprint(p, e, "sys: watchpoint ");
666 for(i = 0; i < 4; i++)
668 p = seprint(p, e, "%d%s", i, (m >> i + 1 != 0) ? "," : "");
669 postnote(up, 0, buf, NDebug);
675 debugbpt(Ureg* ureg, void*)
681 /* restore pc to instruction that caused the trap */
683 sprint(buf, "sys: breakpoint");
684 postnote(up, 1, buf, NDebug);
688 doublefault(Ureg*, void*)
690 panic("double fault");
694 unexpected(Ureg* ureg, void*)
696 print("unexpected trap %lud; ignoring\n", ureg->trap);
699 extern void checkpages(void);
700 extern void checkfault(ulong, ulong);
702 fault386(Ureg* ureg, void*)
705 int read, user, n, insyscall;
709 read = !(ureg->ecode & 2);
711 user = userureg(ureg);
716 panic("kernel fault: bad address pc=0x%.8lux addr=0x%.8lux", ureg->pc, addr);
718 panic("kernel fault: no user process pc=0x%.8lux addr=0x%.8lux", ureg->pc, addr);
721 panic("user fault: up=0 pc=0x%.8lux addr=0x%.8lux", ureg->pc, addr);
723 insyscall = up->insyscall;
725 n = fault(addr, read);
729 panic("fault: 0x%lux", addr);
732 checkfault(addr, ureg->pc);
733 sprint(buf, "sys: trap: fault %s addr=0x%lux",
734 read ? "read" : "write", addr);
735 postnote(up, 1, buf, NDebug);
737 up->insyscall = insyscall;
743 #include "../port/systab.h"
746 * Syscall is called directly from assembler without going through trap().
756 vlong startns, stopns;
759 panic("syscall: cs 0x%4.4luX", ureg->cs);
770 up->scallnr = scallnr;
777 if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)-BY2WD))
778 validaddr(sp, sizeof(Sargs)+BY2WD, 0);
780 up->s = *((Sargs*)(sp+BY2WD));
782 if(up->procctl == Proc_tracesyscall){
783 syscallfmt(scallnr, ureg->pc, (va_list)up->s.args);
785 up->procctl = Proc_stopme;
788 startns = todget(nil);
791 if(scallnr >= nsyscall || systab[scallnr] == 0){
792 pprint("bad sys call number %lud pc %lux\n",
794 postnote(up, 1, "sys: bad sys call", NDebug);
797 up->psstate = sysctab[scallnr];
798 ret = systab[scallnr]((va_list)up->s.args);
801 /* failure: save the error buffer for errstr */
803 up->syserrstr = up->errstr;
805 if(0 && up->pid == 1)
806 print("syscall %lud error %s\n", scallnr, up->syserrstr);
809 print("bad errstack [%lud]: %d extra\n", scallnr, up->nerrlab);
810 for(i = 0; i < NERR; i++)
811 print("sp=%lux pc=%lux\n",
812 up->errlab[i].sp, up->errlab[i].pc);
813 panic("error stack");
817 * Put return value in frame. On the x86 the syscall is
818 * just another trap and the return value from syscall is
819 * ignored. On other machines the return value is put into
820 * the results register by caller of syscall.
824 if(up->procctl == Proc_tracesyscall){
825 stopns = todget(nil);
826 sysretfmt(scallnr, (va_list)up->s.args, ret, startns, stopns);
828 up->procctl = Proc_stopme;
837 noted(ureg, *((ulong*)up->s.args));
839 if(scallnr!=RFORK && (up->procctl || up->nnote)){
843 /* if we delayed sched because we held a lock, sched now */
850 * Call user, if necessary, with note.
851 * Pass user the Ureg struct and the note on his stack.
865 if(up->fpstate == FPactive){
867 up->fpstate = FPinactive;
869 up->fpstate |= FPillegal;
875 if(strncmp(n->msg, "sys:", 4) == 0){
877 if(l > ERRMAX-15) /* " pc=0x12345678\0" */
879 sprint(n->msg+l, " pc=0x%.8lux", ureg->pc);
882 if(n->flag!=NUser && (up->notified || up->notify==0)){
884 if(n->flag == NDebug)
885 pprint("suicide: %s\n", n->msg);
886 pexit(n->msg, n->flag!=NDebug);
897 pexit(n->msg, n->flag!=NDebug);
900 sp -= 256; /* debugging: preserve context causing problem */
902 if(0) print("%s %lud: notify %.8lux %.8lux %.8lux %s\n",
903 up->text, up->pid, ureg->pc, ureg->usp, sp, n->msg);
905 if(!okaddr((uintptr)up->notify, 1, 0)
906 || !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)){
908 pprint("suicide: bad address in notify\n");
912 memmove((Ureg*)sp, ureg, sizeof(Ureg));
913 *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */
914 up->ureg = (void*)sp;
916 memmove((char*)sp, up->note[0].msg, ERRMAX);
918 *(ulong*)(sp+2*BY2WD) = sp+3*BY2WD; /* arg 2 is string */
919 *(ulong*)(sp+1*BY2WD) = (ulong)up->ureg; /* arg 1 is ureg* */
920 *(ulong*)(sp+0*BY2WD) = 0; /* arg 0 is pc */
922 ureg->pc = (ulong)up->notify;
924 ureg->ss = ureg->ds = ureg->es = UDSEL;
927 memmove(&up->lastnote, &up->note[0], sizeof(Note));
928 memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
936 * Return user to state before notify()
939 noted(Ureg* ureg, ulong arg0)
945 if(arg0!=NRSTR && !up->notified) {
947 pprint("call to noted() when not notified\n");
952 nureg = up->ureg; /* pointer to user returned Ureg struct */
954 up->fpstate &= ~FPillegal;
957 oureg = (ulong)nureg;
958 if(!okaddr(oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){
960 pprint("bad ureg in noted or call to noted when not notified\n");
964 /* don't let user change system flags */
965 nureg->flags = (ureg->flags & ~0xCD5) | (nureg->flags & 0xCD5);
969 memmove(ureg, nureg, sizeof(Ureg));
974 if(0) print("%s %lud: noted %.8lux %.8lux\n",
975 up->text, up->pid, nureg->pc, nureg->usp);
976 if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->usp, BY2WD, 0)){
978 pprint("suicide: trap in noted\n");
981 up->ureg = (Ureg*)(*(ulong*)(oureg-BY2WD));
986 if(!okaddr(nureg->pc, BY2WD, 0)
987 || !okaddr(nureg->usp, BY2WD, 0)){
989 pprint("suicide: trap in noted\n");
993 sp = oureg-4*BY2WD-ERRMAX;
996 ((ulong*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */
997 ((ulong*)sp)[0] = 0; /* arg 0 is pc */
1001 up->lastnote.flag = NDebug;
1005 qunlock(&up->debug);
1006 if(up->lastnote.flag == NDebug)
1007 pprint("suicide: %s\n", up->lastnote.msg);
1008 pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
1013 execregs(uintptr entry, ulong ssize, ulong nargs)
1018 sp = (ulong*)(USTKTOP - ssize);
1022 ureg->usp = (ulong)sp;
1025 ureg->ss = ureg->ds = ureg->es = UDSEL;
1026 ureg->fs = ureg->gs = NULLSEL;
1027 return USTKTOP-sizeof(Tos); /* address of kernel/user shared data */
1031 * return the userpc the last exception happened at
1038 ureg = (Ureg*)up->dbgreg;
1042 /* This routine must save the values of registers the user is not permitted
1043 * to write from devproc and then restore the saved values before returning.
1046 setregisters(Ureg* ureg, char* pureg, char* uva, int n)
1050 flags = ureg->flags;
1051 memmove(pureg, uva, n);
1052 ureg->flags = (ureg->flags & 0xCD5) | (flags & ~0xCD5);
1061 up->kpfun(up->kparg);
1062 pexit("kproc dying", 0);
1066 kprocchild(Proc* p, void (*func)(void*), void* arg)
1069 * gotolabel() needs a word on the stack in
1070 * which to place the return PC used to jump
1073 p->sched.pc = (ulong)linkproc;
1074 p->sched.sp = (ulong)p->kstack+KSTACK-BY2WD;
1081 forkchild(Proc *p, Ureg *ureg)
1086 * Add 2*BY2WD to the stack to account for
1088 * - trap's argument (ur)
1090 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Ureg)+2*BY2WD);
1091 p->sched.pc = (ulong)forkret;
1093 cureg = (Ureg*)(p->sched.sp+2*BY2WD);
1094 memmove(cureg, ureg, sizeof(Ureg));
1095 /* return value of syscall in child */
1098 /* Things from bottom of syscall which were never executed */
1103 /* Give enough context in the ureg to produce a kernel stack for
1104 * a sleeping process
1107 setkernur(Ureg* ureg, Proc* p)
1109 ureg->pc = p->sched.pc;
1110 ureg->sp = p->sched.sp+4;