trap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "ureg.h"
  7. #include "io.h"
  8. #include "tos.h"
  9. #include "../port/error.h"
  10. static Lock vctllock;
  11. static Vctl *vctl[256];
  12. void
  13. hwintrinit(void)
  14. {
  15. i8259init();
  16. mpicenable(0, nil); /* 8259 interrupts are routed through MPIC intr 0 */
  17. }
  18. static int
  19. hwintrenable(Vctl *v)
  20. {
  21. int vec, irq;
  22. irq = v->irq;
  23. if(BUSTYPE(v->tbdf) == BusPCI) { /* MPIC? */
  24. if(irq > 15) {
  25. print("intrenable: pci irq %d out of range\n", v->irq);
  26. return -1;
  27. }
  28. vec = irq;
  29. mpicenable(vec, v);
  30. }
  31. else {
  32. if(irq > MaxIrqPIC) {
  33. print("intrenable: irq %d out of range\n", v->irq);
  34. return -1;
  35. }
  36. vec = irq+VectorPIC;
  37. if(i8259enable(v) == -1)
  38. return -1;
  39. }
  40. return vec;
  41. }
  42. static int
  43. hwintrdisable(Vctl *v)
  44. {
  45. int vec, irq;
  46. irq = v->irq;
  47. if(BUSTYPE(v->tbdf) == BusPCI) { /* MPIC? */
  48. if(irq > 15) {
  49. print("intrdisable: pci irq %d out of range\n", v->irq);
  50. return -1;
  51. }
  52. vec = irq;
  53. mpicdisable(vec);
  54. }
  55. else {
  56. if(irq > MaxIrqPIC) {
  57. print("intrdisable: irq %d out of range\n", v->irq);
  58. return -1;
  59. }
  60. vec = irq+VectorPIC;
  61. if(i8259disable(irq) == -1)
  62. return -1;
  63. }
  64. return vec;
  65. }
  66. static int
  67. hwvecno(int irq, int tbdf)
  68. {
  69. if(BUSTYPE(tbdf) == BusPCI) /* MPIC? */
  70. return irq;
  71. else
  72. return irq+VectorPIC;
  73. }
  74. void
  75. intrenable(int irq, void (*f)(Ureg*, void*), void* a, int tbdf, char *name)
  76. {
  77. int vno;
  78. Vctl *v;
  79. if(f == nil){
  80. print("intrenable: nil handler for %d, tbdf 0x%uX for %s\n",
  81. irq, tbdf, name);
  82. return;
  83. }
  84. v = xalloc(sizeof(Vctl));
  85. v->isintr = 1;
  86. v->irq = irq;
  87. v->tbdf = tbdf;
  88. v->f = f;
  89. v->a = a;
  90. strncpy(v->name, name, KNAMELEN-1);
  91. v->name[KNAMELEN-1] = 0;
  92. ilock(&vctllock);
  93. vno = hwintrenable(v);
  94. if(vno == -1){
  95. iunlock(&vctllock);
  96. print("intrenable: couldn't enable irq %d, tbdf 0x%uX for %s\n",
  97. irq, tbdf, v->name);
  98. xfree(v);
  99. return;
  100. }
  101. if(vctl[vno]){
  102. if(vctl[vno]->isr != v->isr || vctl[vno]->eoi != v->eoi)
  103. panic("intrenable: handler: %s %s %luX %luX %luX %luX\n",
  104. vctl[vno]->name, v->name,
  105. vctl[vno]->isr, v->isr, vctl[vno]->eoi, v->eoi);
  106. v->next = vctl[vno];
  107. }
  108. vctl[vno] = v;
  109. iunlock(&vctllock);
  110. }
  111. void
  112. intrdisable(int irq, void (*f)(Ureg *, void *), void *a, int tbdf, char *name)
  113. {
  114. Vctl **pv, *v;
  115. int vno;
  116. vno = hwvecno(irq, tbdf);
  117. ilock(&vctllock);
  118. pv = &vctl[vno];
  119. while (*pv &&
  120. ((*pv)->irq != irq || (*pv)->tbdf != tbdf || (*pv)->f != f || (*pv)->a != a ||
  121. strcmp((*pv)->name, name)))
  122. pv = &((*pv)->next);
  123. assert(*pv);
  124. v = *pv;
  125. *pv = (*pv)->next; /* Link out the entry */
  126. if(vctl[vno] == nil)
  127. hwintrdisable(v);
  128. iunlock(&vctllock);
  129. xfree(v);
  130. }
  131. void syscall(Ureg*);
  132. void noted(Ureg*, ulong);
  133. static void _dumpstack(Ureg*);
  134. char *excname[] =
  135. {
  136. "reserved 0",
  137. "system reset",
  138. "machine check",
  139. "data access",
  140. "instruction access",
  141. "external interrupt",
  142. "alignment",
  143. "program exception",
  144. "floating-point unavailable",
  145. "decrementer",
  146. "reserved A",
  147. "reserved B",
  148. "system call",
  149. "trace trap",
  150. "floating point assist",
  151. "reserved F",
  152. "reserved 10",
  153. "reserved 11",
  154. "reserved 12",
  155. "instruction address breakpoint",
  156. "system management interrupt",
  157. };
  158. char *fpcause[] =
  159. {
  160. "inexact operation",
  161. "division by zero",
  162. "underflow",
  163. "overflow",
  164. "invalid operation",
  165. };
  166. char *fpexcname(Ureg*, ulong, char*);
  167. #define FPEXPMASK 0xfff80300 /* Floating exception bits in fpscr */
  168. char *regname[]={
  169. "CAUSE", "SRR1",
  170. "PC", "GOK",
  171. "LR", "CR",
  172. "XER", "CTR",
  173. "R0", "R1",
  174. "R2", "R3",
  175. "R4", "R5",
  176. "R6", "R7",
  177. "R8", "R9",
  178. "R10", "R11",
  179. "R12", "R13",
  180. "R14", "R15",
  181. "R16", "R17",
  182. "R18", "R19",
  183. "R20", "R21",
  184. "R22", "R23",
  185. "R24", "R25",
  186. "R26", "R27",
  187. "R28", "R29",
  188. "R30", "R31",
  189. };
  190. void
  191. trap(Ureg *ureg)
  192. {
  193. ulong dsisr;
  194. int ecode, user;
  195. char buf[ERRMAX], *s;
  196. ecode = (ureg->cause >> 8) & 0xff;
  197. user = (ureg->srr1 & MSR_PR) != 0;
  198. if(user)
  199. up->dbgreg = ureg;
  200. if(ureg->status & MSR_RI == 0)
  201. print("double fault?: ecode = %d\n", ecode);
  202. switch(ecode) {
  203. case CEI:
  204. intr(ureg);
  205. break;
  206. case CDEC:
  207. clockintr(ureg);
  208. break;
  209. case CSYSCALL:
  210. if(!user)
  211. panic("syscall in kernel: srr1 0x%4.4luX\n", ureg->srr1);
  212. syscall(ureg);
  213. return; /* syscall() calls notify itself, don't do it again */
  214. case CFPU:
  215. if(!user || up == nil) {
  216. dumpregs(ureg);
  217. panic("floating point in kernel");
  218. }
  219. switch(up->fpstate){
  220. case FPinit:
  221. fprestore(&initfp);
  222. up->fpstate = FPactive;
  223. break;
  224. case FPinactive:
  225. fprestore(&up->fpsave);
  226. up->fpstate = FPactive;
  227. break;
  228. default:
  229. panic("fpstate");
  230. }
  231. ureg->srr1 |= MSR_FP;
  232. break;
  233. case CISI:
  234. faultpower(ureg, ureg->pc, 1);
  235. break;
  236. case CDSI:
  237. dsisr = getdsisr();
  238. if(dsisr & BIT(6))
  239. faultpower(ureg, getdar(), 0);
  240. else
  241. faultpower(ureg, getdar(), 1);
  242. break;
  243. case CPROG:
  244. if(ureg->status & (1<<19))
  245. s = "floating point exception";
  246. else if(ureg->status & (1<<18))
  247. s = "illegal instruction";
  248. else if(ureg->status & (1<<17))
  249. s = "privileged instruction";
  250. else
  251. s = "undefined program exception";
  252. if(user){
  253. spllo();
  254. sprint(buf, "sys: trap: %s", s);
  255. postnote(up, 1, buf, NDebug);
  256. break;
  257. }
  258. dumpregs(ureg);
  259. panic(s);
  260. break;
  261. default:
  262. if(ecode <= nelem(excname) && user){
  263. spllo();
  264. sprint(buf, "sys: trap: %s", excname[ecode]);
  265. postnote(up, 1, buf, NDebug);
  266. break;
  267. }
  268. dumpregs(ureg);
  269. if(ecode < nelem(excname))
  270. panic("%s", excname[ecode]);
  271. panic("unknown trap/intr: %d\n", ecode);
  272. }
  273. /* restoreureg must execute at high IPL */
  274. splhi();
  275. /* delaysched set because we held a lock or because our quantum ended */
  276. if(up && up->delaysched && ecode == CDEC){
  277. sched();
  278. splhi();
  279. }
  280. if(user) {
  281. notify(ureg);
  282. if(up->fpstate == FPinactive)
  283. ureg->srr1 &= ~MSR_FP;
  284. kexit(ureg);
  285. }
  286. }
  287. void
  288. faultpower(Ureg *ureg, ulong addr, int read)
  289. {
  290. int user, insyscall, n;
  291. char buf[ERRMAX];
  292. user = (ureg->srr1 & MSR_PR) != 0;
  293. insyscall = up->insyscall;
  294. up->insyscall = 1;
  295. n = fault(addr, read);
  296. if(n < 0){
  297. if(!user){
  298. dumpregs(ureg);
  299. panic("fault: 0x%lux", addr);
  300. }
  301. sprint(buf, "sys: trap: fault %s addr=0x%lux", read? "read" : "write", addr);
  302. postnote(up, 1, buf, NDebug);
  303. }
  304. up->insyscall = insyscall;
  305. }
  306. void
  307. sethvec(int v, void (*r)(void))
  308. {
  309. ulong *vp, pa, o;
  310. vp = KADDR(v);
  311. vp[0] = 0x7c1043a6; /* MOVW R0, SPR(SPRG0) */
  312. vp[1] = 0x7c0802a6; /* MOVW LR, R0 */
  313. vp[2] = 0x7c1243a6; /* MOVW R0, SPR(SPRG2) */
  314. pa = PADDR(r);
  315. o = pa >> 25;
  316. if(o != 0 && o != 0x7F){
  317. /* a branch too far */
  318. vp[3] = (15<<26)|(pa>>16); /* MOVW $r&~0xFFFF, R0 */
  319. vp[4] = (24<<26)|(pa&0xFFFF); /* OR $r&0xFFFF, R0 */
  320. vp[5] = 0x7c0803a6; /* MOVW R0, LR */
  321. vp[6] = 0x4e800021; /* BL (LR) */
  322. }else
  323. vp[3] = (18<<26)|(pa&0x3FFFFFC)|3; /* bla */
  324. dcflush(vp, 8*sizeof(ulong));
  325. }
  326. void
  327. trapinit(void)
  328. {
  329. int i;
  330. /*
  331. * set all exceptions to trap
  332. */
  333. for(i = 0; i < 0x2000; i += 0x100)
  334. sethvec(i, trapvec);
  335. putmsr(getmsr() & ~MSR_IP);
  336. }
  337. void
  338. intr(Ureg *ureg)
  339. {
  340. int vno;
  341. Vctl *ctl, *v;
  342. vno = mpicintack();
  343. if(vno == 0) { /* 8259, wired through MPIC vec 0 */
  344. vno = i8259intack();
  345. mpiceoi(0);
  346. }
  347. if(vno > nelem(vctl) || (ctl = vctl[vno]) == 0) {
  348. panic("spurious intr %d", vno);
  349. return;
  350. }
  351. if(ctl->isr)
  352. ctl->isr(vno);
  353. for(v = ctl; v != nil; v = v->next){
  354. if(v->f)
  355. v->f(ureg, v->a);
  356. }
  357. if(ctl->eoi)
  358. ctl->eoi(vno);
  359. if(up)
  360. preempted();
  361. }
  362. char*
  363. fpexcname(Ureg *ur, ulong fpscr, char *buf)
  364. {
  365. int i;
  366. char *s;
  367. ulong fppc;
  368. fppc = ur->pc;
  369. s = 0;
  370. fpscr >>= 3; /* trap enable bits */
  371. fpscr &= (fpscr>>22); /* anded with exceptions */
  372. for(i=0; i<5; i++)
  373. if(fpscr & (1<<i))
  374. s = fpcause[i];
  375. if(s == 0)
  376. return "no floating point exception";
  377. sprint(buf, "%s fppc=0x%lux", s, fppc);
  378. return buf;
  379. }
  380. /*
  381. * Fill in enough of Ureg to get a stack trace, and call a function.
  382. * Used by debugging interface rdb.
  383. */
  384. static void
  385. getpcsp(ulong *pc, ulong *sp)
  386. {
  387. *pc = getcallerpc(&pc);
  388. *sp = (ulong)&pc-4;
  389. }
  390. void
  391. callwithureg(void (*fn)(Ureg*))
  392. {
  393. Ureg ureg;
  394. getpcsp((ulong*)&ureg.pc, (ulong*)&ureg.sp);
  395. ureg.lr = getcallerpc(&fn);
  396. fn(&ureg);
  397. }
  398. static void
  399. _dumpstack(Ureg *ureg)
  400. {
  401. ulong l, sl, el, v;
  402. int i;
  403. l = (ulong)&l;
  404. if(up == 0){
  405. el = (ulong)m+BY2PG;
  406. sl = el-KSTACK;
  407. }
  408. else{
  409. sl = (ulong)up->kstack;
  410. el = sl + KSTACK;
  411. }
  412. if(l > el || l < sl){
  413. el = (ulong)m+BY2PG;
  414. sl = el-KSTACK;
  415. }
  416. if(l > el || l < sl)
  417. return;
  418. print("ktrace /kernel/path %.8lux %.8lux %.8lux\n", ureg->pc, ureg->sp, ureg->lr);
  419. i = 0;
  420. for(; l < el; l += 4){
  421. v = *(ulong*)l;
  422. if(KTZERO < v && v < (ulong)etext){
  423. print("%.8lux=%.8lux ", l, v);
  424. if(i++ == 4){
  425. print("\n");
  426. i = 0;
  427. }
  428. }
  429. }
  430. }
  431. void
  432. dumpstack(void)
  433. {
  434. callwithureg(_dumpstack);
  435. }
  436. void
  437. dumpregs(Ureg *ur)
  438. {
  439. int i;
  440. ulong *l;
  441. if(up) {
  442. print("registers for %s %ld\n", up->text, up->pid);
  443. if(ur->srr1 & MSR_PR == 0)
  444. if(ur->usp < (ulong)up->kstack || ur->usp > (ulong)up->kstack+KSTACK)
  445. print("invalid stack ptr\n");
  446. }
  447. else
  448. print("registers for kernel\n");
  449. print("dsisr\t%.8lux\tdar\t%.8lux\n", getdsisr(), getdar());
  450. l = &ur->cause;
  451. for(i=0; i<sizeof regname/sizeof(char*); i+=2, l+=2)
  452. print("%s\t%.8lux\t%s\t%.8lux\n", regname[i], l[0], regname[i+1], l[1]);
  453. }
  454. static void
  455. linkproc(void)
  456. {
  457. spllo();
  458. (*up->kpfun)(up->kparg);
  459. pexit("", 0);
  460. }
  461. void
  462. kprocchild(Proc *p, void (*func)(void*), void *arg)
  463. {
  464. p->sched.pc = (ulong)linkproc;
  465. p->sched.sp = (ulong)p->kstack+KSTACK;
  466. p->kpfun = func;
  467. p->kparg = arg;
  468. }
  469. /*
  470. * called in sysfile.c
  471. */
  472. void
  473. evenaddr(ulong addr)
  474. {
  475. if(addr & 3){
  476. postnote(up, 1, "sys: odd address", NDebug);
  477. error(Ebadarg);
  478. }
  479. }
  480. long
  481. execregs(ulong entry, ulong ssize, ulong nargs)
  482. {
  483. ulong *sp;
  484. Ureg *ureg;
  485. sp = (ulong*)(USTKTOP - ssize);
  486. *--sp = nargs;
  487. ureg = up->dbgreg;
  488. ureg->usp = (ulong)sp;
  489. ureg->pc = entry;
  490. ureg->srr1 &= ~MSR_FP;
  491. return USTKTOP-sizeof(Tos); /* address of kernel/user shared data */
  492. }
  493. void
  494. forkchild(Proc *p, Ureg *ur)
  495. {
  496. Ureg *cur;
  497. p->sched.sp = (ulong)p->kstack+KSTACK-UREGSIZE;
  498. p->sched.pc = (ulong)forkret;
  499. cur = (Ureg*)(p->sched.sp+2*BY2WD);
  500. memmove(cur, ur, sizeof(Ureg));
  501. cur->r3 = 0;
  502. /* Things from bottom of syscall we never got to execute */
  503. p->psstate = 0;
  504. p->insyscall = 0;
  505. }
  506. ulong
  507. userpc(void)
  508. {
  509. Ureg *ureg;
  510. ureg = (Ureg*)up->dbgreg;
  511. return ureg->pc;
  512. }
  513. /* This routine must save the values of registers the user is not
  514. * permitted to write from devproc and then restore the saved values
  515. * before returning
  516. */
  517. void
  518. setregisters(Ureg *xp, char *pureg, char *uva, int n)
  519. {
  520. ulong status;
  521. status = xp->status;
  522. memmove(pureg, uva, n);
  523. xp->status = status;
  524. }
  525. /* Give enough context in the ureg to produce a kernel stack for
  526. * a sleeping process
  527. */
  528. void
  529. setkernur(Ureg* ureg, Proc* p)
  530. {
  531. ureg->pc = p->sched.pc;
  532. ureg->sp = p->sched.sp+4;
  533. }
  534. ulong
  535. dbgpc(Proc *p)
  536. {
  537. Ureg *ureg;
  538. ureg = p->dbgreg;
  539. if(ureg == 0)
  540. return 0;
  541. return ureg->pc;
  542. }
  543. /*
  544. * system calls
  545. */
  546. #include "../port/systab.h"
  547. /* TODO: make this trap a separate asm entry point, like on other RISC architectures */
  548. void
  549. syscall(Ureg* ureg)
  550. {
  551. int i;
  552. char *e;
  553. long ret;
  554. ulong sp, scallnr;
  555. m->syscall++;
  556. up->insyscall = 1;
  557. up->pc = ureg->pc;
  558. up->dbgreg = ureg;
  559. scallnr = ureg->r3;
  560. up->scallnr = ureg->r3;
  561. spllo();
  562. sp = ureg->usp;
  563. up->nerrlab = 0;
  564. ret = -1;
  565. if(!waserror()){
  566. if(scallnr >= nsyscall || systab[scallnr] == nil){
  567. pprint("bad sys call number %d pc %lux\n", scallnr, ureg->pc);
  568. postnote(up, 1, "sys: bad sys call", NDebug);
  569. error(Ebadarg);
  570. }
  571. if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)-BY2WD))
  572. validaddr(sp, sizeof(Sargs)+BY2WD, 0);
  573. up->s = *((Sargs*)(sp+BY2WD));
  574. up->psstate = sysctab[scallnr];
  575. ret = systab[scallnr](up->s.args);
  576. poperror();
  577. }else{
  578. /* failure: save the error buffer for errstr */
  579. e = up->syserrstr;
  580. up->syserrstr = up->errstr;
  581. up->errstr = e;
  582. }
  583. if(up->nerrlab){
  584. print("bad errstack [%uld]: %d extra\n", scallnr, up->nerrlab);
  585. print("scall %s lr =%lux\n", sysctab[scallnr], ureg->lr);
  586. for(i = 0; i < NERR; i++)
  587. print("sp=%lux pc=%lux\n", up->errlab[i].sp, up->errlab[i].pc);
  588. panic("error stack");
  589. }
  590. up->insyscall = 0;
  591. up->psstate = 0;
  592. /*
  593. * Put return value in frame. On the x86 the syscall is
  594. * just another trap and the return value from syscall is
  595. * ignored. On other machines the return value is put into
  596. * the results register by caller of syscall.
  597. */
  598. ureg->r3 = ret;
  599. if(scallnr == NOTED)
  600. noted(ureg, *(ulong*)(sp+BY2WD));
  601. /* restoreureg must execute at high IPL */
  602. splhi();
  603. if(scallnr!=RFORK)
  604. notify(ureg);
  605. if(up->fpstate == FPinactive)
  606. ureg->srr1 &= ~MSR_FP;
  607. }
  608. /*
  609. * Call user, if necessary, with note.
  610. * Pass user the Ureg struct and the note on his stack.
  611. */
  612. int
  613. notify(Ureg* ur)
  614. {
  615. int l;
  616. ulong s, sp;
  617. Note *n;
  618. if(up->procctl)
  619. procctl(up);
  620. if(up->nnote == 0)
  621. return 0;
  622. s = spllo();
  623. qlock(&up->debug);
  624. up->notepending = 0;
  625. n = &up->note[0];
  626. if(strncmp(n->msg, "sys:", 4) == 0){
  627. l = strlen(n->msg);
  628. if(l > ERRMAX-15) /* " pc=0x12345678\0" */
  629. l = ERRMAX-15;
  630. sprint(n->msg+l, " pc=0x%.8lux", ur->pc);
  631. }
  632. if(n->flag!=NUser && (up->notified || up->notify==0)){
  633. if(n->flag == NDebug)
  634. pprint("suicide: %s\n", n->msg);
  635. qunlock(&up->debug);
  636. pexit(n->msg, n->flag!=NDebug);
  637. }
  638. if(up->notified) {
  639. qunlock(&up->debug);
  640. splhi();
  641. return 0;
  642. }
  643. if(!up->notify) {
  644. qunlock(&up->debug);
  645. pexit(n->msg, n->flag!=NDebug);
  646. }
  647. sp = ur->usp & ~(BY2V-1);
  648. sp -= sizeof(Ureg);
  649. if(!okaddr((ulong)up->notify, BY2WD, 0) ||
  650. !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)) {
  651. pprint("suicide: bad address or sp in notify\n");
  652. qunlock(&up->debug);
  653. pexit("Suicide", 0);
  654. }
  655. memmove((Ureg*)sp, ur, sizeof(Ureg));
  656. *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */
  657. up->ureg = (void*)sp;
  658. sp -= BY2WD+ERRMAX;
  659. memmove((char*)sp, up->note[0].msg, ERRMAX);
  660. sp -= 3*BY2WD;
  661. *(ulong*)(sp+2*BY2WD) = sp+3*BY2WD; /* arg 2 is string */
  662. ur->r1 = (long)up->ureg; /* arg 1 is ureg* */
  663. ((ulong*)sp)[1] = (ulong)up->ureg; /* arg 1 0(FP) is ureg* */
  664. ((ulong*)sp)[0] = 0; /* arg 0 is pc */
  665. ur->usp = sp;
  666. ur->pc = (ulong)up->notify;
  667. up->notified = 1;
  668. up->nnote--;
  669. memmove(&up->lastnote, &up->note[0], sizeof(Note));
  670. memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
  671. qunlock(&up->debug);
  672. splx(s);
  673. return 1;
  674. }
  675. /*
  676. * Return user to state before notify()
  677. */
  678. void
  679. noted(Ureg* ureg, ulong arg0)
  680. {
  681. Ureg *nureg;
  682. ulong oureg, sp;
  683. qlock(&up->debug);
  684. if(arg0!=NRSTR && !up->notified) {
  685. qunlock(&up->debug);
  686. pprint("call to noted() when not notified\n");
  687. pexit("Suicide", 0);
  688. }
  689. up->notified = 0;
  690. nureg = up->ureg; /* pointer to user returned Ureg struct */
  691. /* sanity clause */
  692. oureg = (ulong)nureg;
  693. if(!okaddr((ulong)oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){
  694. pprint("bad ureg in noted or call to noted when not notified\n");
  695. qunlock(&up->debug);
  696. pexit("Suicide", 0);
  697. }
  698. memmove(ureg, nureg, sizeof(Ureg));
  699. switch(arg0){
  700. case NCONT:
  701. case NRSTR:
  702. if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->usp, BY2WD, 0)){
  703. pprint("suicide: trap in noted\n");
  704. qunlock(&up->debug);
  705. pexit("Suicide", 0);
  706. }
  707. up->ureg = (Ureg*)(*(ulong*)(oureg-BY2WD));
  708. qunlock(&up->debug);
  709. break;
  710. case NSAVE:
  711. if(!okaddr(nureg->pc, BY2WD, 0)
  712. || !okaddr(nureg->usp, BY2WD, 0)){
  713. pprint("suicide: trap in noted\n");
  714. qunlock(&up->debug);
  715. pexit("Suicide", 0);
  716. }
  717. qunlock(&up->debug);
  718. sp = oureg-4*BY2WD-ERRMAX;
  719. splhi();
  720. ureg->sp = sp;
  721. ((ulong*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */
  722. ((ulong*)sp)[0] = 0; /* arg 0 is pc */
  723. break;
  724. default:
  725. pprint("unknown noted arg 0x%lux\n", arg0);
  726. up->lastnote.flag = NDebug;
  727. /* fall through */
  728. case NDFLT:
  729. if(up->lastnote.flag == NDebug)
  730. pprint("suicide: %s\n", up->lastnote.msg);
  731. qunlock(&up->debug);
  732. pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
  733. }
  734. }