void trap(Ureg *ureg) { int user; ulong opc, cp; user = userureg(ureg); if(user){ if(up == nil) panic("user trap: up=nil"); up->dbgreg = ureg; cycles(&up->kentry); } switch(ureg->type){ case PsrMund: ureg->pc -= 4; if(user){ spllo(); if(okaddr(ureg->pc, 4, 0)){ opc = *(ulong*)ureg->pc; if((opc & 0x0f000000) == 0x0e000000 || (opc & 0x0e000000) == 0x0c000000){ cp = opc >> 8 & 15; if(cp == 10 || cp == 11){ mathtrap(ureg, opc); break; } } } postnote(up, 1, "sys: trap: invalid opcode", NDebug); break; }
/* * Return user to state before notify() */ void noted(Ureg* cur, uintptr arg0) { NFrame *nf; Note note; Ureg *nur; qlock(&up->debug); if(arg0 != NRSTR && !up->notified){ qunlock(&up->debug); pprint("suicide: call to noted when not notified\n"); pexit("Suicide", 0); } up->notified = 0; fpunoted(); nf = up->ureg; /* sanity clause */ if(!okaddr(PTR2UINT(nf), sizeof(NFrame), 0)){ qunlock(&up->debug); pprint("suicide: bad ureg %#p in noted\n", nf); pexit("Suicide", 0); } /* * Check the segment selectors are all valid. */ nur = &nf->ureg; if(nur->cs != SSEL(SiUCS, SsRPL3) || nur->ss != SSEL(SiUDS, SsRPL3) || nur->ds != SSEL(SiUDS, SsRPL3) || nur->es != SSEL(SiUDS, SsRPL3) || nur->fs != SSEL(SiUDS, SsRPL3) || nur->gs != SSEL(SiUDS, SsRPL3)){ qunlock(&up->debug); pprint("suicide: bad segment selector in noted\n"); pexit("Suicide", 0); } /* don't let user change system flags */ nur->flags &= (Of|Df|Sf|Zf|Af|Pf|Cf); nur->flags |= cur->flags & ~(Of|Df|Sf|Zf|Af|Pf|Cf); memmove(cur, nur, sizeof(Ureg)); switch((int)arg0){ case NCONT: case NRSTR: if(!okaddr(nur->ip, BY2SE, 0) || !okaddr(nur->sp, BY2SE, 0)){ qunlock(&up->debug); pprint("suicide: trap in noted pc=%#p sp=%#p\n", nur->ip, nur->sp); pexit("Suicide", 0); } up->ureg = nf->old; qunlock(&up->debug); break; case NSAVE: if(!okaddr(nur->ip, BY2SE, 0) || !okaddr(nur->sp, BY2SE, 0)){ qunlock(&up->debug); pprint("suicide: trap in noted pc=%#p sp=%#p\n", nur->ip, nur->sp); pexit("Suicide", 0); } qunlock(&up->debug); splhi(); nf->arg1 = nf->msg; nf->arg0 = &nf->ureg; cur->bp = PTR2UINT(nf->arg0); nf->ip = 0; cur->sp = PTR2UINT(nf); break; default: memmove(¬e, &up->lastnote, sizeof(Note)); qunlock(&up->debug); pprint("suicide: bad arg %#p in noted: %s\n", arg0, note.msg); pexit(note.msg, 0); break; case NDFLT: memmove(¬e, &up->lastnote, sizeof(Note)); qunlock(&up->debug); if(note.flag == NDebug) pprint("suicide: %s\n", note.msg); pexit(note.msg, note.flag != NDebug); break; } }
/* * Call user, if necessary, with note. * Pass user the Ureg struct and the note on his stack. */ int notify(Ureg* ureg) { int l; Mpl pl; Note note; uintptr sp; NFrame *nf; /* * Calls procctl splhi, see comment in procctl for the reasoning. */ if(up->procctl) procctl(up); if(up->nnote == 0) return 0; fpunotify(ureg); pl = spllo(); qlock(&up->debug); up->notepending = 0; memmove(¬e, &up->note[0], sizeof(Note)); if(strncmp(note.msg, "sys:", 4) == 0){ l = strlen(note.msg); if(l > ERRMAX-sizeof(" pc=0x0123456789abcdef")) l = ERRMAX-sizeof(" pc=0x0123456789abcdef"); sprint(note.msg+l, " pc=%#p", ureg->ip); } if(note.flag != NUser && (up->notified || up->notify == nil)){ qunlock(&up->debug); if(note.flag == NDebug) pprint("suicide: %s\n", note.msg); pexit(note.msg, note.flag != NDebug); } if(up->notified){ qunlock(&up->debug); splhi(); return 0; } if(up->notify == nil){ qunlock(&up->debug); pexit(note.msg, note.flag != NDebug); } if(!okaddr(PTR2UINT(up->notify), sizeof(ureg->ip), 0)){ qunlock(&up->debug); pprint("suicide: bad function address %#p in notify\n", up->notify); pexit("Suicide", 0); } sp = ureg->sp - sizeof(NFrame); if(!okaddr(sp, sizeof(NFrame), 1)){ qunlock(&up->debug); pprint("suicide: bad stack address %#p in notify\n", sp); pexit("Suicide", 0); } nf = UINT2PTR(sp); memmove(&nf->ureg, ureg, sizeof(Ureg)); nf->old = up->ureg; up->ureg = nf; /* actually the NFrame, for noted */ memmove(nf->msg, note.msg, ERRMAX); nf->arg1 = nf->msg; nf->arg0 = &nf->ureg; ureg->bp = PTR2UINT(nf->arg0); nf->ip = 0; ureg->sp = sp; ureg->ip = PTR2UINT(up->notify); up->notified = 1; up->nnote--; memmove(&up->lastnote, ¬e, sizeof(Note)); memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note)); qunlock(&up->debug); splx(pl); return 1; }
/* * Return user to state before notify() */ void noted(Ureg* ureg, ulong arg0) { Ureg *nureg; ulong oureg, sp; qlock(&up->debug); if(arg0!=NRSTR && !up->notified) { qunlock(&up->debug); pprint("call to noted() when not notified\n"); pexit("Suicide", 0); } up->notified = 0; nureg = up->ureg; /* pointer to user returned Ureg struct */ up->fpstate &= ~FPillegal; /* sanity clause */ oureg = (ulong)nureg; if(!okaddr((ulong)oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){ pprint("bad ureg in noted or call to noted when not notified\n"); qunlock(&up->debug); pexit("Suicide", 0); } /* * Check the segment selectors are all valid, otherwise * a fault will be taken on attempting to return to the * user process. * Take care with the comparisons as different processor * generations push segment descriptors in different ways. */ if((nureg->cs & 0xFFFF) != UESEL || (nureg->ss & 0xFFFF) != UDSEL || (nureg->ds & 0xFFFF) != UDSEL || (nureg->es & 0xFFFF) != UDSEL || (nureg->fs & 0xFFFF) != UDSEL || (nureg->gs & 0xFFFF) != UDSEL){ pprint("bad segment selector in noted\n"); qunlock(&up->debug); pexit("Suicide", 0); } /* don't let user change system flags */ nureg->flags = (ureg->flags & ~0xCD5) | (nureg->flags & 0xCD5); memmove(ureg, nureg, sizeof(Ureg)); switch(arg0){ case NCONT: case NRSTR: if(0) print("%s %lud: noted %.8lux %.8lux\n", up->text, up->pid, nureg->pc, nureg->usp); if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->usp, BY2WD, 0)){ qunlock(&up->debug); pprint("suicide: trap in noted\n"); pexit("Suicide", 0); } up->ureg = (Ureg*)(*(ulong*)(oureg-BY2WD)); qunlock(&up->debug); break; case NSAVE: if(!okaddr(nureg->pc, BY2WD, 0) || !okaddr(nureg->usp, BY2WD, 0)){ qunlock(&up->debug); pprint("suicide: trap in noted\n"); pexit("Suicide", 0); } qunlock(&up->debug); sp = oureg-4*BY2WD-ERRMAX; splhi(); ureg->sp = sp; ((ulong*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */ ((ulong*)sp)[0] = 0; /* arg 0 is pc */ break; default: pprint("unknown noted arg 0x%lux\n", arg0); up->lastnote.flag = NDebug; /* fall through */ case NDFLT: if(up->lastnote.flag == NDebug){ qunlock(&up->debug); pprint("suicide: %s\n", up->lastnote.msg); } else qunlock(&up->debug); pexit(up->lastnote.msg, up->lastnote.flag!=NDebug); } }
/* * Call user, if necessary, with note. * Pass user the Ureg struct and the note on his stack. */ int notify(Ureg* ureg) { int l; ulong s, sp; Note *n; if(up->procctl) procctl(up); if(up->nnote == 0) return 0; if(up->fpstate == FPactive){ fpsave(&up->fpsave); up->fpstate = FPinactive; } up->fpstate |= FPillegal; s = spllo(); qlock(&up->debug); up->notepending = 0; n = &up->note[0]; if(strncmp(n->msg, "sys:", 4) == 0){ l = strlen(n->msg); if(l > ERRMAX-15) /* " pc=0x12345678\0" */ l = ERRMAX-15; sprint(n->msg+l, " pc=0x%.8lux", ureg->pc); } if(n->flag!=NUser && (up->notified || up->notify==0)){ if(n->flag == NDebug) pprint("suicide: %s\n", n->msg); qunlock(&up->debug); pexit(n->msg, n->flag!=NDebug); } if(up->notified){ qunlock(&up->debug); splhi(); return 0; } if(!up->notify){ qunlock(&up->debug); pexit(n->msg, n->flag!=NDebug); } sp = ureg->usp; sp -= 256; /* debugging: preserve context causing problem */ sp -= sizeof(Ureg); if(0) print("%s %lud: notify %.8lux %.8lux %.8lux %s\n", up->text, up->pid, ureg->pc, ureg->usp, sp, n->msg); if(!okaddr((ulong)up->notify, 1, 0) || !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)){ pprint("suicide: bad address in notify\n"); qunlock(&up->debug); pexit("Suicide", 0); } memmove((Ureg*)sp, ureg, sizeof(Ureg)); *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */ up->ureg = (void*)sp; sp -= BY2WD+ERRMAX; memmove((char*)sp, up->note[0].msg, ERRMAX); sp -= 3*BY2WD; *(ulong*)(sp+2*BY2WD) = sp+3*BY2WD; /* arg 2 is string */ *(ulong*)(sp+1*BY2WD) = (ulong)up->ureg; /* arg 1 is ureg* */ *(ulong*)(sp+0*BY2WD) = 0; /* arg 0 is pc */ ureg->usp = sp; ureg->pc = (ulong)up->notify; up->notified = 1; up->nnote--; memmove(&up->lastnote, &up->note[0], sizeof(Note)); memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note)); qunlock(&up->debug); splx(s); return 1; }
/* * Return user to state before notify() */ static void noted(Ureg* cur, uintptr arg0) { NFrame *nf; Ureg *nur; qlock(&up->debug); if(arg0 != NRSTR && !up->notified){ qunlock(&up->debug); pprint("call to noted() when not notified\n"); pexit("Suicide", 0); } up->notified = 0; fpunoted(); nf = up->ureg; /* sanity clause */ if(!okaddr(PTR2UINT(nf), sizeof(NFrame), 0)){ qunlock(&up->debug); pprint("bad ureg in noted %#p\n", nf); pexit("Suicide", 0); } /* don't let user change system flags */ nur = &nf->ureg; nur->psr &= PsrMask|PsrDfiq|PsrDirq; nur->psr |= (cur->psr & ~(PsrMask|PsrDfiq|PsrDirq)); memmove(cur, nur, sizeof(Ureg)); switch((int)arg0){ case NCONT: case NRSTR: if(!okaddr(nur->pc, BY2WD, 0) || !okaddr(nur->sp, BY2WD, 0)){ qunlock(&up->debug); pprint("suicide: trap in noted\n"); pexit("Suicide", 0); } up->ureg = nf->old; qunlock(&up->debug); break; case NSAVE: if(!okaddr(nur->pc, BY2WD, 0) || !okaddr(nur->sp, BY2WD, 0)){ qunlock(&up->debug); pprint("suicide: trap in noted\n"); pexit("Suicide", 0); } qunlock(&up->debug); splhi(); nf->arg1 = nf->msg; nf->arg0 = &nf->ureg; nf->ip = 0; cur->sp = PTR2UINT(nf); cur->r0 = PTR2UINT(nf->arg0); break; default: pprint("unknown noted arg %#p\n", arg0); up->lastnote.flag = NDebug; /*FALLTHROUGH*/ case NDFLT: if(up->lastnote.flag == NDebug){ qunlock(&up->debug); pprint("suicide: %s\n", up->lastnote.msg); } else qunlock(&up->debug); pexit(up->lastnote.msg, up->lastnote.flag != NDebug); } }
/* * Call user, if necessary, with note. * Pass user the Ureg struct and the note on his stack. */ int notify(Ureg* ureg) { int l; Note *n; u32int s; uintptr sp; NFrame *nf; if(up->procctl) procctl(up); if(up->nnote == 0) return 0; fpunotify(ureg); s = spllo(); qlock(&up->debug); up->notepending = 0; n = &up->note[0]; if(strncmp(n->msg, "sys:", 4) == 0){ l = strlen(n->msg); if(l > ERRMAX-23) /* " pc=0x0123456789abcdef\0" */ l = ERRMAX-23; snprint(n->msg + l, sizeof n->msg - l, " pc=%#lux", ureg->pc); } if(n->flag != NUser && (up->notified || up->notify == 0)){ if(n->flag == NDebug) pprint("suicide: %s\n", n->msg); qunlock(&up->debug); pexit(n->msg, n->flag != NDebug); } if(up->notified){ qunlock(&up->debug); splhi(); return 0; } if(up->notify == nil){ qunlock(&up->debug); pexit(n->msg, n->flag != NDebug); } if(!okaddr(PTR2UINT(up->notify), 1, 0)){ pprint("suicide: notify function address %#p\n", up->notify); qunlock(&up->debug); pexit("Suicide", 0); } sp = ureg->sp - sizeof(NFrame); if(!okaddr(sp, sizeof(NFrame), 1)){ qunlock(&up->debug); pprint("suicide: notify stack address %#p\n", sp); pexit("Suicide", 0); } nf = UINT2PTR(sp); memmove(&nf->ureg, ureg, sizeof(Ureg)); nf->old = up->ureg; up->ureg = nf; memmove(nf->msg, up->note[0].msg, ERRMAX); nf->arg1 = nf->msg; nf->arg0 = &nf->ureg; ureg->r0 = PTR2UINT(nf->arg0); nf->ip = 0; ureg->sp = sp; ureg->pc = PTR2UINT(up->notify); up->notified = 1; up->nnote--; memmove(&up->lastnote, &up->note[0], sizeof(Note)); memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note)); qunlock(&up->debug); splx(s); return 1; }
/* * Return user to state before notify() */ void noted(Ureg* ureg, ulong arg0) { Ureg *nureg; ulong oureg, sp; qlock(&up->debug); if(arg0!=NRSTR && !up->notified) { qunlock(&up->debug); pprint("call to noted() when not notified\n"); pexit("Suicide", 0); } up->notified = 0; nureg = up->ureg; /* pointer to user returned Ureg struct */ up->fpstate &= ~FPillegal; /* sanity clause */ oureg = (ulong)nureg; if(!okaddr(oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){ qunlock(&up->debug); pprint("bad ureg in noted or call to noted when not notified\n"); pexit("Suicide", 0); } /* don't let user change system flags */ nureg->flags = (ureg->flags & ~0xCD5) | (nureg->flags & 0xCD5); nureg->cs |= 3; nureg->ss |= 3; memmove(ureg, nureg, sizeof(Ureg)); switch(arg0){ case NCONT: case NRSTR: if(0) print("%s %lud: noted %.8lux %.8lux\n", up->text, up->pid, nureg->pc, nureg->usp); if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->usp, BY2WD, 0)){ qunlock(&up->debug); pprint("suicide: trap in noted\n"); pexit("Suicide", 0); } up->ureg = (Ureg*)(*(ulong*)(oureg-BY2WD)); qunlock(&up->debug); break; case NSAVE: if(!okaddr(nureg->pc, BY2WD, 0) || !okaddr(nureg->usp, BY2WD, 0)){ qunlock(&up->debug); pprint("suicide: trap in noted\n"); pexit("Suicide", 0); } qunlock(&up->debug); sp = oureg-4*BY2WD-ERRMAX; splhi(); ureg->sp = sp; ((ulong*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */ ((ulong*)sp)[0] = 0; /* arg 0 is pc */ break; default: up->lastnote.flag = NDebug; /* fall through */ case NDFLT: qunlock(&up->debug); if(up->lastnote.flag == NDebug) pprint("suicide: %s\n", up->lastnote.msg); pexit(up->lastnote.msg, up->lastnote.flag!=NDebug); } }