2 #include "../port/lib.h"
8 #include "../port/error.h"
14 Vctl* next; /* handlers on this vector */
15 char *name; /* of driver, xallocated */
16 void (*f)(Ureg*, void*); /* handler to call */
17 void* a; /* argument to call it with */
21 static Vctl *vctl[32];
22 static Vctl *gpiovctl[27];
23 static int gpioirqref[12];
26 * Layout at virtual address 0.
28 typedef struct Vpage0 {
29 void (*vectors[8])(void);
34 static int irq(Ureg*);
35 static void gpiointr(Ureg*, void*);
37 /* recover state after power suspend
38 * NB: to help debugging bad suspend code,
39 * I changed some prints below to iprints,
40 * to avoid deadlocks when a panic is being
41 * issued during the suspend/resume handler.
46 vpage0 = (Vpage0*)EVECTORS;
47 memmove(vpage0->vectors, vectors, sizeof(vpage0->vectors));
48 memmove(vpage0->vtable, vtable, sizeof(vpage0->vtable));
54 * set up for exceptions
59 /* set up the exception vectors */
60 vpage0 = (Vpage0*)EVECTORS;
61 memmove(vpage0->vectors, vectors, sizeof(vpage0->vectors));
62 memmove(vpage0->vtable, vtable, sizeof(vpage0->vtable));
66 /* use exception vectors at 0xFFFF0000 */
69 /* set up the stacks for the interrupt modes */
70 setr13(PsrMfiq, m->sfiq);
71 setr13(PsrMirq, m->sirq);
72 setr13(PsrMabt, m->sabt);
73 setr13(PsrMund, m->sund);
75 /* map in interrupt registers */
76 intrregs = mapspecial(INTRREGS, sizeof(*intrregs));
78 /* make all interrupts IRQ (i.e. not FIQ) and disable all interrupts */
82 /* turn off all gpio interrupts */
84 gpioregs->falling = 0;
85 gpioregs->edgestatus = gpioregs->edgestatus;
87 /* allow all enabled interrupts to take processor out of sleep mode */
94 iprint("%s: icip %lux icmr %lux iclr %lux iccr %lux icfp %lux\n",
95 tag, intrregs->icip, intrregs->icmr, intrregs->iclr,
96 intrregs->iccr, intrregs->icfp);
100 warnregs(Ureg *ur, char *tag)
103 char *e = buf+sizeof(buf);
106 p = seprint(buf, e, "%s:\n", tag);
107 p = seprint(p, e, "type 0x%.8lux psr 0x%.8lux pc 0x%.8lux\n",
108 ur->type, ur->psr, ur->pc);
109 p = seprint(p, e, "r0 0x%.8lux r1 0x%.8lux r2 0x%.8lux r3 0x%.8lux\n",
110 ur->r0, ur->r1, ur->r2, ur->r3);
111 p = seprint(p, e, "r4 0x%.8lux r5 0x%.8lux r6 0x%.8lux r7 0x%.8lux\n",
112 ur->r4, ur->r5, ur->r6, ur->r7);
113 p = seprint(p, e, "r8 0x%.8lux r9 0x%.8lux r10 0x%.8lux r11 0x%.8lux\n",
114 ur->r8, ur->r9, ur->r10, ur->r11);
115 seprint(p, e, "r12 0x%.8lux r13 0x%.8lux r14 0x%.8lux\n",
116 ur->r12, ur->r13, ur->r14);
121 * enable an irq interrupt
124 irqenable(int irq, IntrHandler *f, void* a, char *name)
128 if(irq >= nelem(vctl) || irq < 0)
131 v = malloc(sizeof(Vctl));
134 v->name = xalloc(strlen(name)+1);
135 strcpy(v->name, name);
140 intrregs->icmr |= 1<<irq;
145 * disable an irq interrupt
148 irqdisable(int irq, IntrHandler *f, void* a, char *name)
152 if(irq >= nelem(vctl) || irq < 0)
153 panic("intrdisable");
156 for(vp = &vctl[irq]; v = *vp; vp = &v->next)
157 if (v->f == f && v->a == a && strcmp(v->name, name) == 0){
158 print("irqdisable: remove %s\n", name);
164 print("irqdisable: irq %d, name %s not enabled\n", irq, name);
165 if (vctl[irq] == nil){
166 print("irqdisable: clear icmr bit %d\n", irq);
167 intrregs->icmr &= ~(1<<irq);
173 * enable an interrupt
176 intrenable(int type, int which, IntrHandler *f, void* a, char *name)
182 irqenable(which, f, a, name);
186 /* from here down, it must be a GPIO edge interrupt */
188 if(which >= nelem(gpiovctl) || which < 0)
193 /* the pin had better be configured as input */
194 if((1<<which) & gpioregs->direction)
195 panic("intrenable of output pin %d", which);
197 /* create a second level vctl for the gpio edge interrupt */
198 v = malloc(sizeof(Vctl));
201 v->name = xalloc(strlen(name)+1);
202 strcpy(v->name, name);
205 v->next = gpiovctl[which];
208 /* set edge register to enable interrupt */
211 gpioregs->rising |= 1<<which;
212 gpioregs->falling |= 1<<which;
215 gpioregs->falling |= 1<<which;
218 gpioregs->rising |= 1<<which;
222 /* point the irq to the gpio interrupt handler */
223 if(gpioirqref[irq]++ == 0)
224 irqenable(irq, gpiointr, nil, "gpio edge");
228 * disable an interrupt
231 intrdisable(int type, int which, IntrHandler *f, void* a, char *name)
238 irqdisable(which, f, a, name);
242 /* from here down, it must be a GPIO edge interrupt */
244 if(which >= nelem(gpiovctl) || which < 0)
245 panic("intrdisable");
250 for(vp = &gpiovctl[which]; v = *vp; vp = &v->next)
251 if (v->f == f && v->a == a && strcmp(v->name, name) == 0){
254 if (gpiovctl[which] == nil){
255 /* set edge register to enable interrupt */
258 print("intrdisable: gpio-rising+falling clear bit %d\n", which);
259 gpioregs->rising &= ~(1<<which);
260 gpioregs->falling &= ~(1<<which);
263 print("intrdisable: gpio-falling clear bit %d\n", which);
264 gpioregs->falling &= ~(1<<which);
267 print("intrdisable: gpio-rising clear bit %d\n", which);
268 gpioregs->rising &= ~(1<<which);
274 print("intrdisable: removing %s\n", name);
277 print("intrdisable: which %d, name %s not enabled\n", which, name);
279 /* disable the gpio interrupt handler if necessary */
280 if(--gpioirqref[irq] == 0){
281 print("intrdisable: inrqdisable gpiointr\n");
282 irqdisable(irq, gpiointr, nil, "gpio edge");
288 * called by trap to handle access faults
291 faultarm(Ureg *ureg, ulong va, int user, int read)
297 warnregs(ureg, "kernel fault");
298 panic("fault: nil up in faultarm, accessing 0x%lux", va);
300 insyscall = up->insyscall;
305 warnregs(ureg, "kernel fault");
306 panic("fault: kernel accessing 0x%lux", va);
308 // warnregs(ureg, "user fault");
309 sprint(buf, "sys: trap: fault %s va=0x%lux", read ? "read" : "write", va);
310 postnote(up, 1, buf, NDebug);
312 up->insyscall = insyscall;
316 * returns 1 if the instruction writes memory, 0 otherwise
319 writetomem(ulong inst)
321 /* swap always write memory */
322 if((inst & 0x0FC00000) == 0x01000000)
325 /* loads and stores are distinguished by bit 20 */
334 * here on all exceptions other than syscall (SWI)
340 int clockintr, user, x, rv;
346 rem = ((char*)ureg)-up->kstack;
348 rem = ((char*)ureg)-((char*)(MACHADDR+sizeof(Mach)));
351 panic("trap %d bytes remaining, up = %#p, ureg = %#p, at pc 0x%lux",
352 rem, up, ureg, ureg->pc);
355 user = (ureg->psr & PsrMask) == PsrMusr;
358 * All interrupts/exceptions should be resumed at ureg->pc-4,
359 * except for Data Abort which resumes at ureg->pc-8.
361 if(ureg->type == (PsrMabt+1))
369 panic("unknown trap");
372 clockintr = irq(ureg);
374 case PsrMabt: /* prefetch fault */
375 faultarm(ureg, ureg->pc, user, 1);
377 case PsrMabt+1: /* data fault */
379 inst = *(ulong*)(ureg->pc);
380 fsr = getfsr() & 0xf;
383 panic("vector exception at %lux", ureg->pc);
388 snprint(buf, sizeof(buf), "sys: alignment: pc 0x%lux va 0x%lux\n",
390 postnote(up, 1, buf, NDebug);
392 panic("kernel alignment: pc 0x%lux va 0x%lux", ureg->pc, va);
395 panic("terminal exception at %lux", ureg->pc);
403 panic("external abort 0x%lux pc 0x%lux addr 0x%lux", fsr, ureg->pc, va);
407 /* translation fault, i.e., no pte entry */
408 faultarm(ureg, va, user, !writetomem(inst));
412 /* domain fault, accessing something we shouldn't */
414 sprint(buf, "sys: access violation: pc 0x%lux va 0x%lux\n",
416 postnote(up, 1, buf, NDebug);
418 panic("kernel access violation: pc 0x%lux va 0x%lux",
423 /* permission error, copy on write or real permission error */
424 faultarm(ureg, va, user, !writetomem(inst));
428 case PsrMund: /* undefined instruction */
430 /* look for floating point instructions to interpret */
435 sprint(buf, "undefined instruction: pc 0x%lux\n", ureg->pc);
436 postnote(up, 1, buf, NDebug);
439 iprint("undefined instruction: pc=0x%lux, inst=0x%lux, 0x%lux, 0x%lux, 0x%lux, 0x%lux\n", ureg->pc, ((ulong*)ureg->pc)[-2], ((ulong*)ureg->pc)[-1], ((ulong*)ureg->pc)[0], ((ulong*)ureg->pc)[1], ((ulong*)ureg->pc)[2]);
440 panic("undefined instruction");
446 /* delaysched set because we held a lock or because our quantum ended */
447 if(up && up->delaysched && clockintr){
453 if(up->procctl || up->nnote)
471 if(va & (1<<IRQtimer0))
475 for(i = 0; i < 32; i++){
476 if(((1<<i) & va) == 0)
478 for(v = vctl[i]; v != nil; v = v->next){
484 print("unknown interrupt: %lux\n", va);
490 * here on gpio interrupts
493 gpiointr(Ureg *ur, void*)
499 va = gpioregs->edgestatus;
500 gpioregs->edgestatus = va;
502 for(i = 0; i < 27; i++){
503 if(((1<<i) & va) == 0)
505 for(v = gpiovctl[i]; v != nil; v = v->next){
511 print("unknown gpio interrupt: %lux\n", va);
517 #include "../port/systab.h"
520 * Syscall is called directly from assembler without going through trap().
530 if((ureg->psr & PsrMask) != PsrMusr) {
531 panic("syscall: pc 0x%lux r14 0x%lux cs 0x%lux", ureg->pc, ureg->r14, ureg->psr);
540 up->scallnr = scallnr;
547 if(scallnr >= nsyscall){
548 pprint("bad sys call number %d pc %lux\n",
550 postnote(up, 1, "sys: bad sys call", NDebug);
554 if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)-BY2WD))
555 validaddr(sp, sizeof(Sargs)+BY2WD, 0);
557 up->s = *((Sargs*)(sp+BY2WD));
558 up->psstate = sysctab[scallnr];
560 ret = systab[scallnr](up->s.args);
563 /* failure: save the error buffer for errstr */
565 up->syserrstr = up->errstr;
569 print("bad errstack [%d]: %d extra\n", scallnr, up->nerrlab);
570 for(i = 0; i < NERR; i++)
571 print("sp=%lux pc=%lux\n",
572 up->errlab[i].sp, up->errlab[i].pc);
573 panic("error stack");
580 * Put return value in frame. On the x86 the syscall is
581 * just another trap and the return value from syscall is
582 * ignored. On other machines the return value is put into
583 * the results register by caller of syscall.
588 noted(ureg, *(ulong*)(sp+BY2WD));
594 if(scallnr != RFORK && (up->procctl || up->nnote))
599 * Return user to state before notify()
602 noted(Ureg* ureg, ulong arg0)
608 if(arg0!=NRSTR && !up->notified) {
610 pprint("call to noted() when not notified\n");
615 nureg = up->ureg; /* pointer to user returned Ureg struct */
618 oureg = (ulong)nureg;
619 if(!okaddr((ulong)oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){
620 pprint("bad ureg in noted or call to noted when not notified\n");
625 /* don't let user change system flags */
626 nureg->psr = (ureg->psr & ~(PsrMask|PsrDfiq|PsrDirq)) |
627 (nureg->psr & (PsrMask|PsrDfiq|PsrDirq));
629 memmove(ureg, nureg, sizeof(Ureg));
634 if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->sp, BY2WD, 0)){
636 pprint("suicide: trap in noted\n");
639 up->ureg = (Ureg*)(*(ulong*)(oureg-BY2WD));
644 if(!okaddr(nureg->pc, BY2WD, 0)
645 || !okaddr(nureg->sp, BY2WD, 0)){
647 pprint("suicide: trap in noted\n");
651 sp = oureg-4*BY2WD-ERRMAX;
654 ((ulong*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */
655 ((ulong*)sp)[0] = 0; /* arg 0 is pc */
659 pprint("unknown noted arg 0x%lux\n", arg0);
660 up->lastnote.flag = NDebug;
664 if(up->lastnote.flag == NDebug){
666 pprint("suicide: %s\n", up->lastnote.msg);
669 pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
674 * Call user, if necessary, with note.
675 * Pass user the Ureg struct and the note on his stack.
693 if(strncmp(n->msg, "sys:", 4) == 0){
695 if(l > ERRMAX-15) /* " pc=0x12345678\0" */
697 sprint(n->msg+l, " pc=0x%.8lux", ureg->pc);
700 if(n->flag!=NUser && (up->notified || up->notify==0)){
701 if(n->flag == NDebug)
702 pprint("suicide: %s\n", n->msg);
704 pexit(n->msg, n->flag!=NDebug);
715 pexit(n->msg, n->flag!=NDebug);
720 if(!okaddr((ulong)up->notify, 1, 0)
721 || !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)){
722 pprint("suicide: bad address in notify\n");
727 up->ureg = (void*)sp;
728 memmove((Ureg*)sp, ureg, sizeof(Ureg));
729 *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */
730 up->ureg = (void*)sp;
732 memmove((char*)sp, up->note[0].msg, ERRMAX);
734 *(ulong*)(sp+2*BY2WD) = sp+3*BY2WD; /* arg 2 is string */
735 *(ulong*)(sp+1*BY2WD) = (ulong)up->ureg; /* arg 1 is ureg* */
736 *(ulong*)(sp+0*BY2WD) = 0; /* arg 0 is pc */
738 ureg->pc = (ulong)up->notify;
741 memmove(&up->lastnote, &up->note[0], sizeof(Note));
742 memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
749 /* Give enough context in the ureg to produce a kernel stack for
753 setkernur(Ureg *ureg, Proc *p)
755 ureg->pc = p->sched.pc;
756 ureg->sp = p->sched.sp+4;
757 ureg->r14 = (ulong)sched;
761 * return the userpc the last exception happened at
768 ureg = (Ureg*)up->dbgreg;
772 /* This routine must save the values of registers the user is not permitted
773 * to write from devproc and then restore the saved values before returning.
776 setregisters(Ureg* ureg, char* pureg, char* uva, int n)
778 USED(ureg, pureg, uva, n);
782 * this is the body for all kproc's
788 up->kpfun(up->kparg);
789 pexit("kproc exiting", 0);
793 * setup stack and initial PC for a new kernel proc. This is architecture
794 * dependent because of the starting stack location
797 kprocchild(Proc *p, void (*func)(void*), void *arg)
799 p->sched.pc = (ulong)linkproc;
800 p->sched.sp = (ulong)p->kstack+KSTACK;
808 * Craft a return frame which will cause the child to pop out of
809 * the scheduler in user mode with the return register zero. Set
810 * pc to point to a l.s return function.
813 forkchild(Proc *p, Ureg *ureg)
817 //print("%lud setting up for forking child %lud\n", up->pid, p->pid);
818 p->sched.sp = (ulong)p->kstack+KSTACK-sizeof(Ureg);
819 p->sched.pc = (ulong)forkret;
821 cureg = (Ureg*)(p->sched.sp);
822 memmove(cureg, ureg, sizeof(Ureg));
824 /* syscall returns 0 for child */
827 /* Things from bottom of syscall which were never executed */
833 * setup stack, initial PC, and any arch dependent regs for an execing user proc.
836 execregs(ulong entry, ulong ssize, ulong nargs)
841 sp = (ulong*)(USTKTOP - ssize);
845 memset(ureg, 0, 15*sizeof(ulong));
846 ureg->r13 = (ulong)sp;
848 //print("%lud: EXECREGS pc 0x%lux sp 0x%lux\n", up->pid, ureg->pc, ureg->r13);
849 return USTKTOP-sizeof(Tos); /* address of kernel/user shared data */
853 * Fill in enough of Ureg to get a stack trace, and call a function.
854 * Used by debugging interface rdb.
857 callwithureg(void (*fn)(Ureg*))
860 ureg.pc = getcallerpc(&fn);
861 ureg.sp = (ulong)&fn;
866 _dumpstack(Ureg *ureg)
873 iprint("no current proc\n");
877 iprint("ktrace /kernel/path %.8lux %.8lux %.8lux\n", ureg->pc, ureg->sp, ureg->r14);
879 for(l=(ulong)&l; l<(ulong)(up->kstack+KSTACK); l+=4){
881 if(KTZERO < v && v < (ulong)&etext && (v&3)==0){
884 if((*p & 0x0f000000) == 0x0b000000){
885 iprint("%.8lux=%.8lux ", l, v);
901 callwithureg(_dumpstack);
920 * called in sysfile.c
926 postnote(up, 1, "sys: odd address", NDebug);