trap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "ureg.h"
  8. #include "../port/error.h"
  9. void noted(Ureg*, ulong);
  10. static void debugbpt(Ureg*, void*);
  11. static void fault386(Ureg*, void*);
  12. static Lock vctllock;
  13. static Vctl *vctl[256];
  14. ulong *intrtimes[256];
  15. enum
  16. {
  17. Ntimevec = 1000 /* number of time buckets for each intr */
  18. };
  19. void
  20. intrenable(int irq, void (*f)(Ureg*, void*), void* a, int tbdf, char *name)
  21. {
  22. int vno;
  23. Vctl *v;
  24. if(f == nil){
  25. print("intrenable: nil handler for %d, tbdf 0x%uX for %s\n",
  26. irq, tbdf, name);
  27. return;
  28. }
  29. v = xalloc(sizeof(Vctl));
  30. v->isintr = 1;
  31. v->irq = irq;
  32. v->tbdf = tbdf;
  33. v->f = f;
  34. v->a = a;
  35. strncpy(v->name, name, KNAMELEN-1);
  36. v->name[KNAMELEN-1] = 0;
  37. ilock(&vctllock);
  38. vno = arch->intrenable(v);
  39. if(vno == -1){
  40. iunlock(&vctllock);
  41. print("intrenable: couldn't enable irq %d, tbdf 0x%uX for %s\n",
  42. irq, tbdf, v->name);
  43. xfree(v);
  44. return;
  45. }
  46. if(vctl[vno]){
  47. if(vctl[vno]->isr != v->isr || vctl[vno]->eoi != v->eoi)
  48. panic("intrenable: handler: %s %s %luX %luX %luX %luX\n",
  49. vctl[vno]->name, v->name,
  50. vctl[vno]->isr, v->isr, vctl[vno]->eoi, v->eoi);
  51. v->next = vctl[vno];
  52. }
  53. if(intrtimes[vno] == nil)
  54. intrtimes[vno] = xalloc(Ntimevec*sizeof(ulong));
  55. vctl[vno] = v;
  56. iunlock(&vctllock);
  57. }
  58. int
  59. intrdisable(int irq, void (*f)(Ureg *, void *), void *a, int tbdf, char *name)
  60. {
  61. Vctl **pv, *v;
  62. int vno;
  63. /*
  64. * For now, none of this will work with the APIC code,
  65. * there is no mapping between irq and vector as the IRQ
  66. * is pretty meaningless.
  67. */
  68. if(arch->intrvecno == nil)
  69. return -1;
  70. vno = arch->intrvecno(irq);
  71. ilock(&vctllock);
  72. pv = &vctl[vno];
  73. while (*pv &&
  74. ((*pv)->irq != irq || (*pv)->tbdf != tbdf || (*pv)->f != f || (*pv)->a != a ||
  75. strcmp((*pv)->name, name)))
  76. pv = &((*pv)->next);
  77. assert(*pv);
  78. v = *pv;
  79. *pv = (*pv)->next; /* Link out the entry */
  80. if (vctl[vno] == nil && arch->intrdisable != nil)
  81. arch->intrdisable(irq);
  82. iunlock(&vctllock);
  83. xfree(v);
  84. return 0;
  85. }
  86. static long
  87. irqallocread(Chan*, void *vbuf, long n, vlong offset)
  88. {
  89. char *buf, *p, str[2*(11+1)+KNAMELEN+1+1];
  90. int m, vno;
  91. long oldn;
  92. Vctl *v;
  93. if(n < 0 || offset < 0)
  94. error(Ebadarg);
  95. oldn = n;
  96. buf = vbuf;
  97. for(vno=0; vno<nelem(vctl); vno++){
  98. for(v=vctl[vno]; v; v=v->next){
  99. m = snprint(str, sizeof str, "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
  100. if(m <= offset) /* if do not want this, skip entry */
  101. offset -= m;
  102. else{
  103. /* skip offset bytes */
  104. m -= offset;
  105. p = str+offset;
  106. offset = 0;
  107. /* write at most max(n,m) bytes */
  108. if(m > n)
  109. m = n;
  110. memmove(buf, p, m);
  111. n -= m;
  112. buf += m;
  113. if(n == 0)
  114. return oldn;
  115. }
  116. }
  117. }
  118. return oldn - n;
  119. }
  120. void
  121. trapenable(int vno, void (*f)(Ureg*, void*), void* a, char *name)
  122. {
  123. Vctl *v;
  124. if(vno < 0 || vno >= VectorPIC)
  125. panic("trapenable: vno %d\n", vno);
  126. v = xalloc(sizeof(Vctl));
  127. v->tbdf = BUSUNKNOWN;
  128. v->f = f;
  129. v->a = a;
  130. strncpy(v->name, name, KNAMELEN);
  131. v->name[KNAMELEN-1] = 0;
  132. lock(&vctllock);
  133. if(vctl[vno])
  134. v->next = vctl[vno]->next;
  135. vctl[vno] = v;
  136. unlock(&vctllock);
  137. }
  138. static void
  139. nmienable(void)
  140. {
  141. int x;
  142. /*
  143. * Hack: should be locked with NVRAM access.
  144. */
  145. outb(0x70, 0x80); /* NMI latch clear */
  146. outb(0x70, 0);
  147. x = inb(0x61) & 0x07; /* Enable NMI */
  148. outb(0x61, 0x08|x);
  149. outb(0x61, x);
  150. }
  151. void
  152. trapinit(void)
  153. {
  154. int d1, v;
  155. ulong vaddr;
  156. Segdesc *idt;
  157. idt = (Segdesc*)IDTADDR;
  158. vaddr = (ulong)vectortable;
  159. for(v = 0; v < 256; v++){
  160. d1 = (vaddr & 0xFFFF0000)|SEGP;
  161. switch(v){
  162. case VectorBPT:
  163. d1 |= SEGPL(3)|SEGIG;
  164. break;
  165. case VectorSYSCALL:
  166. d1 |= SEGPL(3)|SEGIG;
  167. break;
  168. default:
  169. d1 |= SEGPL(0)|SEGIG;
  170. break;
  171. }
  172. idt[v].d0 = (vaddr & 0xFFFF)|(KESEL<<16);
  173. idt[v].d1 = d1;
  174. vaddr += 6;
  175. }
  176. /*
  177. * Special traps.
  178. * Syscall() is called directly without going through trap().
  179. */
  180. trapenable(VectorBPT, debugbpt, 0, "debugpt");
  181. trapenable(VectorPF, fault386, 0, "fault386");
  182. nmienable();
  183. addarchfile("irqalloc", 0444, irqallocread, nil);
  184. }
  185. static char* excname[32] = {
  186. "divide error",
  187. "debug exception",
  188. "nonmaskable interrupt",
  189. "breakpoint",
  190. "overflow",
  191. "bounds check",
  192. "invalid opcode",
  193. "coprocessor not available",
  194. "double fault",
  195. "coprocessor segment overrun",
  196. "invalid TSS",
  197. "segment not present",
  198. "stack exception",
  199. "general protection violation",
  200. "page fault",
  201. "15 (reserved)",
  202. "coprocessor error",
  203. "alignment check",
  204. "machine check",
  205. "19 (reserved)",
  206. "20 (reserved)",
  207. "21 (reserved)",
  208. "22 (reserved)",
  209. "23 (reserved)",
  210. "24 (reserved)",
  211. "25 (reserved)",
  212. "26 (reserved)",
  213. "27 (reserved)",
  214. "28 (reserved)",
  215. "29 (reserved)",
  216. "30 (reserved)",
  217. "31 (reserved)",
  218. };
  219. /*
  220. * keep histogram of interrupt service times
  221. */
  222. void
  223. intrtime(Mach*, int vno)
  224. {
  225. ulong diff;
  226. ulong x = perfticks();
  227. diff = x - m->perf.intrts;
  228. m->perf.intrts = x;
  229. m->perf.inintr += diff;
  230. if(up == nil && m->perf.inidle > diff)
  231. m->perf.inidle -= diff;
  232. diff /= m->cpumhz;
  233. if(diff >= Ntimevec){
  234. diff = Ntimevec-1;
  235. }
  236. intrtimes[vno][diff]++;
  237. }
  238. /*
  239. * All traps come here. It is slower to have all traps call trap()
  240. * rather than directly vectoring the handler. However, this avoids a
  241. * lot of code duplication and possible bugs. The only exception is
  242. * VectorSYSCALL.
  243. * Trap is called with interrupts disabled via interrupt-gates.
  244. */
  245. void
  246. trap(Ureg* ureg)
  247. {
  248. int i, vno, user;
  249. char buf[ERRMAX];
  250. Vctl *ctl, *v;
  251. Mach *mach;
  252. m->perf.intrts = perfticks();
  253. user = 0;
  254. if((ureg->cs & 0xFFFF) == UESEL){
  255. user = 1;
  256. up->dbgreg = ureg;
  257. }
  258. vno = ureg->trap;
  259. if(ctl = vctl[vno]){
  260. if(ctl->isintr){
  261. m->intr++;
  262. if(vno >= VectorPIC && vno != VectorSYSCALL)
  263. m->lastintr = ctl->irq;
  264. }
  265. if(ctl->isr)
  266. ctl->isr(vno);
  267. for(v = ctl; v != nil; v = v->next){
  268. if(v->f)
  269. v->f(ureg, v->a);
  270. }
  271. if(ctl->eoi)
  272. ctl->eoi(vno);
  273. if(ctl->isintr){
  274. intrtime(m, vno);
  275. if(up && ctl->irq != IrqTIMER && ctl->irq != IrqCLOCK)
  276. preempted();
  277. }
  278. }
  279. else if(vno <= nelem(excname) && user){
  280. spllo();
  281. sprint(buf, "sys: trap: %s", excname[vno]);
  282. postnote(up, 1, buf, NDebug);
  283. }
  284. else if(vno >= VectorPIC && vno != VectorSYSCALL){
  285. /*
  286. * An unknown interrupt.
  287. * Check for a default IRQ7. This can happen when
  288. * the IRQ input goes away before the acknowledge.
  289. * In this case, a 'default IRQ7' is generated, but
  290. * the corresponding bit in the ISR isn't set.
  291. * In fact, just ignore all such interrupts.
  292. */
  293. /* call all interrupt routines, just in case */
  294. for(i = VectorPIC; i <= MaxIrqLAPIC; i++){
  295. ctl = vctl[i];
  296. if(ctl == nil)
  297. continue;
  298. if(!ctl->isintr)
  299. continue;
  300. for(v = ctl; v != nil; v = v->next){
  301. if(v->f)
  302. v->f(ureg, v->a);
  303. }
  304. /* should we do this? */
  305. if(ctl->eoi)
  306. ctl->eoi(i);
  307. }
  308. /* clear the interrupt */
  309. i8259isr(vno);
  310. if(0)print("cpu%d: spurious interrupt %d, last %d",
  311. m->machno, vno, m->lastintr);
  312. for(i = 0; i < 32; i++){
  313. if(!(active.machs & (1<<i)))
  314. continue;
  315. mach = MACHP(i);
  316. if(m->machno == mach->machno)
  317. continue;
  318. print(": cpu%d: last %d", mach->machno, mach->lastintr);
  319. }
  320. print("\n");
  321. m->spuriousintr++;
  322. return;
  323. }
  324. else{
  325. if(vno == VectorNMI){
  326. nmienable();
  327. if(m->machno != 0){
  328. print("cpu%d: PC %8.8luX\n",
  329. m->machno, ureg->pc);
  330. for(;;);
  331. }
  332. }
  333. dumpregs(ureg);
  334. if(vno < nelem(excname))
  335. panic("%s", excname[vno]);
  336. panic("unknown trap/intr: %d\n", vno);
  337. }
  338. if(user && (up->procctl || up->nnote)){
  339. splhi();
  340. notify(ureg);
  341. }
  342. }
  343. /*
  344. * dump registers
  345. */
  346. void
  347. dumpregs2(Ureg* ureg)
  348. {
  349. if(up)
  350. print("cpu%d: registers for %s %lud\n",
  351. m->machno, up->text, up->pid);
  352. else
  353. print("cpu%d: registers for kernel\n", m->machno);
  354. print("FLAGS=%luX TRAP=%luX ECODE=%luX PC=%luX",
  355. ureg->flags, ureg->trap, ureg->ecode, ureg->pc);
  356. print(" SS=%4.4luX USP=%luX\n", ureg->ss & 0xFFFF, ureg->usp);
  357. print(" AX %8.8luX BX %8.8luX CX %8.8luX DX %8.8luX\n",
  358. ureg->ax, ureg->bx, ureg->cx, ureg->dx);
  359. print(" SI %8.8luX DI %8.8luX BP %8.8luX\n",
  360. ureg->si, ureg->di, ureg->bp);
  361. print(" CS %4.4luX DS %4.4luX ES %4.4luX FS %4.4luX GS %4.4luX\n",
  362. ureg->cs & 0xFFFF, ureg->ds & 0xFFFF, ureg->es & 0xFFFF,
  363. ureg->fs & 0xFFFF, ureg->gs & 0xFFFF);
  364. }
  365. void
  366. dumpregs(Ureg* ureg)
  367. {
  368. extern ulong etext;
  369. vlong mca, mct;
  370. dumpregs2(ureg);
  371. /*
  372. * Processor control registers.
  373. * If machine check exception, time stamp counter, page size extensions
  374. * or enhanced virtual 8086 mode extensions are supported, there is a
  375. * CR4. If there is a CR4 and machine check extensions, read the machine
  376. * check address and machine check type registers if RDMSR supported.
  377. */
  378. print(" CR0 %8.8lux CR2 %8.8lux CR3 %8.8lux",
  379. getcr0(), getcr2(), getcr3());
  380. if(m->cpuiddx & 0x9A){
  381. print(" CR4 %8.8lux", getcr4());
  382. if((m->cpuiddx & 0xA0) == 0xA0){
  383. rdmsr(0x00, &mca);
  384. rdmsr(0x01, &mct);
  385. print("\n MCA %8.8llux MCT %8.8llux", mca, mct);
  386. }
  387. }
  388. print("\n ur %lux up %lux\n", ureg, up);
  389. }
  390. /*
  391. * Fill in enough of Ureg to get a stack trace, and call a function.
  392. * Used by debugging interface rdb.
  393. */
  394. void
  395. callwithureg(void (*fn)(Ureg*))
  396. {
  397. Ureg ureg;
  398. ureg.pc = getcallerpc(&fn);
  399. ureg.sp = (ulong)&fn;
  400. fn(&ureg);
  401. }
  402. static void
  403. _dumpstack(Ureg *ureg)
  404. {
  405. ulong l, v, i, estack;
  406. extern ulong etext;
  407. return;
  408. print("ktrace /kernel/path %.8lux %.8lux\n", ureg->pc, ureg->sp);
  409. i = 0;
  410. if(up
  411. && (ulong)&l >= (ulong)up->kstack
  412. && (ulong)&l <= (ulong)up->kstack+KSTACK)
  413. estack = (ulong)up->kstack+KSTACK;
  414. else if((ulong)&l >= (ulong)m->stack
  415. && (ulong)&l <= (ulong)m+BY2PG)
  416. estack = (ulong)m+MACHSIZE;
  417. else
  418. return;
  419. for(l=(ulong)&l; l<estack; l+=4){
  420. v = *(ulong*)l;
  421. if(KTZERO < v && v < (ulong)&etext){
  422. /*
  423. * we could Pick off general CALL (((uchar*)v)[-5] == 0xE8)
  424. * and CALL indirect through AX (((uchar*)v)[-2] == 0xFF && ((uchar*)v)[-2] == 0xD0),
  425. * but this is too clever and misses faulting address.
  426. */
  427. print("%.8lux=%.8lux ", l, v);
  428. i++;
  429. }
  430. if(i == 4){
  431. i = 0;
  432. print("\n");
  433. }
  434. }
  435. if(i)
  436. print("\n");
  437. }
  438. void
  439. dumpstack(void)
  440. {
  441. callwithureg(_dumpstack);
  442. }
  443. static void
  444. debugbpt(Ureg* ureg, void*)
  445. {
  446. char buf[ERRMAX];
  447. if(up == 0)
  448. panic("kernel bpt");
  449. /* restore pc to instruction that caused the trap */
  450. ureg->pc--;
  451. sprint(buf, "sys: breakpoint");
  452. postnote(up, 1, buf, NDebug);
  453. }
  454. static void
  455. fault386(Ureg* ureg, void*)
  456. {
  457. ulong addr;
  458. int read, user, n, insyscall;
  459. char buf[ERRMAX];
  460. addr = getcr2();
  461. user = (ureg->cs & 0xFFFF) == UESEL;
  462. if(!user && mmukmapsync(addr))
  463. return;
  464. read = !(ureg->ecode & 2);
  465. if(up == nil)
  466. panic("fault but up is zero; pc 0x%8.8lux addr 0x%8.8lux\n", ureg->pc, addr);
  467. insyscall = up->insyscall;
  468. up->insyscall = 1;
  469. n = fault(addr, read);
  470. if(n < 0){
  471. if(!user){
  472. dumpregs(ureg);
  473. panic("fault: 0x%lux\n", addr);
  474. }
  475. sprint(buf, "sys: trap: fault %s addr=0x%lux",
  476. read? "read" : "write", addr);
  477. postnote(up, 1, buf, NDebug);
  478. }
  479. up->insyscall = insyscall;
  480. }
  481. /*
  482. * system calls
  483. */
  484. #include "../port/systab.h"
  485. /*
  486. * Syscall is called directly from assembler without going through trap().
  487. */
  488. void
  489. syscall(Ureg* ureg)
  490. {
  491. char *e;
  492. ulong sp;
  493. long ret;
  494. int i;
  495. ulong scallnr;
  496. if((ureg->cs & 0xFFFF) != UESEL)
  497. panic("syscall: cs 0x%4.4luX\n", ureg->cs);
  498. m->syscall++;
  499. up->insyscall = 1;
  500. up->pc = ureg->pc;
  501. up->dbgreg = ureg;
  502. scallnr = ureg->ax;
  503. up->scallnr = scallnr;
  504. if(scallnr == RFORK && up->fpstate == FPactive){
  505. fpsave(&up->fpsave);
  506. up->fpstate = FPinactive;
  507. }
  508. spllo();
  509. sp = ureg->usp;
  510. up->nerrlab = 0;
  511. ret = -1;
  512. if(!waserror()){
  513. if(scallnr >= nsyscall || systab[scallnr] == 0){
  514. pprint("bad sys call number %d pc %lux\n",
  515. scallnr, ureg->pc);
  516. postnote(up, 1, "sys: bad sys call", NDebug);
  517. error(Ebadarg);
  518. }
  519. if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)-BY2WD))
  520. validaddr(sp, sizeof(Sargs)+BY2WD, 0);
  521. up->s = *((Sargs*)(sp+BY2WD));
  522. up->psstate = sysctab[scallnr];
  523. ret = systab[scallnr](up->s.args);
  524. poperror();
  525. }else{
  526. /* failure: save the error buffer for errstr */
  527. e = up->syserrstr;
  528. up->syserrstr = up->errstr;
  529. up->errstr = e;
  530. if(0 && up->pid == 1)
  531. print("syscall %lud error %s\n", scallnr, up->syserrstr);
  532. }
  533. if(up->nerrlab){
  534. print("bad errstack [%uld]: %d extra\n", scallnr, up->nerrlab);
  535. for(i = 0; i < NERR; i++)
  536. print("sp=%lux pc=%lux\n",
  537. up->errlab[i].sp, up->errlab[i].pc);
  538. panic("error stack");
  539. }
  540. up->insyscall = 0;
  541. up->psstate = 0;
  542. /*
  543. * Put return value in frame. On the x86 the syscall is
  544. * just another trap and the return value from syscall is
  545. * ignored. On other machines the return value is put into
  546. * the results register by caller of syscall.
  547. */
  548. ureg->ax = ret;
  549. if(scallnr == NOTED)
  550. noted(ureg, *(ulong*)(sp+BY2WD));
  551. if(scallnr!=RFORK && (up->procctl || up->nnote)){
  552. splhi();
  553. notify(ureg);
  554. }
  555. }
  556. /*
  557. * Call user, if necessary, with note.
  558. * Pass user the Ureg struct and the note on his stack.
  559. */
  560. int
  561. notify(Ureg* ureg)
  562. {
  563. int l;
  564. ulong s, sp;
  565. Note *n;
  566. if(up->procctl)
  567. procctl(up);
  568. if(up->nnote == 0)
  569. return 0;
  570. if(up->fpstate == FPactive){
  571. fpsave(&up->fpsave);
  572. up->fpstate = FPinactive;
  573. }
  574. up->fpstate |= FPillegal;
  575. s = spllo();
  576. qlock(&up->debug);
  577. up->notepending = 0;
  578. n = &up->note[0];
  579. if(strncmp(n->msg, "sys:", 4) == 0){
  580. l = strlen(n->msg);
  581. if(l > ERRMAX-15) /* " pc=0x12345678\0" */
  582. l = ERRMAX-15;
  583. sprint(n->msg+l, " pc=0x%.8lux", ureg->pc);
  584. }
  585. if(n->flag!=NUser && (up->notified || up->notify==0)){
  586. if(n->flag == NDebug)
  587. pprint("suicide: %s\n", n->msg);
  588. qunlock(&up->debug);
  589. pexit(n->msg, n->flag!=NDebug);
  590. }
  591. if(up->notified) {
  592. qunlock(&up->debug);
  593. splhi();
  594. return 0;
  595. }
  596. if(!up->notify){
  597. qunlock(&up->debug);
  598. pexit(n->msg, n->flag!=NDebug);
  599. }
  600. sp = ureg->usp;
  601. sp -= sizeof(Ureg);
  602. if(!okaddr((ulong)up->notify, 1, 0)
  603. || !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)){
  604. pprint("suicide: bad address in notify\n");
  605. qunlock(&up->debug);
  606. pexit("Suicide", 0);
  607. }
  608. up->ureg = (void*)sp;
  609. memmove((Ureg*)sp, ureg, sizeof(Ureg));
  610. *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */
  611. up->ureg = (void*)sp;
  612. sp -= BY2WD+ERRMAX;
  613. memmove((char*)sp, up->note[0].msg, ERRMAX);
  614. sp -= 3*BY2WD;
  615. *(ulong*)(sp+2*BY2WD) = sp+3*BY2WD; /* arg 2 is string */
  616. *(ulong*)(sp+1*BY2WD) = (ulong)up->ureg; /* arg 1 is ureg* */
  617. *(ulong*)(sp+0*BY2WD) = 0; /* arg 0 is pc */
  618. ureg->usp = sp;
  619. ureg->pc = (ulong)up->notify;
  620. up->notified = 1;
  621. up->nnote--;
  622. memmove(&up->lastnote, &up->note[0], sizeof(Note));
  623. memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
  624. qunlock(&up->debug);
  625. splx(s);
  626. return 1;
  627. }
  628. /*
  629. * Return user to state before notify()
  630. */
  631. void
  632. noted(Ureg* ureg, ulong arg0)
  633. {
  634. Ureg *nureg;
  635. ulong oureg, sp;
  636. qlock(&up->debug);
  637. if(arg0!=NRSTR && !up->notified) {
  638. qunlock(&up->debug);
  639. pprint("call to noted() when not notified\n");
  640. pexit("Suicide", 0);
  641. }
  642. up->notified = 0;
  643. nureg = up->ureg; /* pointer to user returned Ureg struct */
  644. up->fpstate &= ~FPillegal;
  645. /* sanity clause */
  646. oureg = (ulong)nureg;
  647. if(!okaddr((ulong)oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){
  648. pprint("bad ureg in noted or call to noted when not notified\n");
  649. qunlock(&up->debug);
  650. pexit("Suicide", 0);
  651. }
  652. /*
  653. * Check the segment selectors are all valid, otherwise
  654. * a fault will be taken on attempting to return to the
  655. * user process.
  656. * Take care with the comparisons as different processor
  657. * generations push segment descriptors in different ways.
  658. */
  659. if((nureg->cs & 0xFFFF) != UESEL || (nureg->ss & 0xFFFF) != UDSEL
  660. || (nureg->ds & 0xFFFF) != UDSEL || (nureg->es & 0xFFFF) != UDSEL
  661. || (nureg->fs & 0xFFFF) != UDSEL || (nureg->gs & 0xFFFF) != UDSEL){
  662. pprint("bad segment selector in noted\n");
  663. qunlock(&up->debug);
  664. pexit("Suicide", 0);
  665. }
  666. /* don't let user change system flags */
  667. nureg->flags = (ureg->flags & ~0xCD5) | (nureg->flags & 0xCD5);
  668. memmove(ureg, nureg, sizeof(Ureg));
  669. switch(arg0){
  670. case NCONT:
  671. case NRSTR:
  672. if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->usp, BY2WD, 0)){
  673. qunlock(&up->debug);
  674. pprint("suicide: trap in noted\n");
  675. pexit("Suicide", 0);
  676. }
  677. up->ureg = (Ureg*)(*(ulong*)(oureg-BY2WD));
  678. qunlock(&up->debug);
  679. break;
  680. case NSAVE:
  681. if(!okaddr(nureg->pc, BY2WD, 0)
  682. || !okaddr(nureg->usp, BY2WD, 0)){
  683. qunlock(&up->debug);
  684. pprint("suicide: trap in noted\n");
  685. pexit("Suicide", 0);
  686. }
  687. qunlock(&up->debug);
  688. sp = oureg-4*BY2WD-ERRMAX;
  689. splhi();
  690. ureg->sp = sp;
  691. ((ulong*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */
  692. ((ulong*)sp)[0] = 0; /* arg 0 is pc */
  693. break;
  694. default:
  695. pprint("unknown noted arg 0x%lux\n", arg0);
  696. up->lastnote.flag = NDebug;
  697. /* fall through */
  698. case NDFLT:
  699. if(up->lastnote.flag == NDebug){
  700. qunlock(&up->debug);
  701. pprint("suicide: %s\n", up->lastnote.msg);
  702. } else
  703. qunlock(&up->debug);
  704. pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
  705. }
  706. }
  707. long
  708. execregs(ulong entry, ulong ssize, ulong nargs)
  709. {
  710. ulong *sp;
  711. Ureg *ureg;
  712. up->fpstate = FPinit;
  713. fpoff();
  714. sp = (ulong*)(USTKTOP - ssize);
  715. *--sp = nargs;
  716. ureg = up->dbgreg;
  717. ureg->usp = (ulong)sp;
  718. ureg->pc = entry;
  719. return USTKTOP-BY2WD; /* address of user-level clock */
  720. }
  721. /*
  722. * return the userpc the last exception happened at
  723. */
  724. ulong
  725. userpc(void)
  726. {
  727. Ureg *ureg;
  728. ureg = (Ureg*)up->dbgreg;
  729. return ureg->pc;
  730. }
  731. /* This routine must save the values of registers the user is not permitted
  732. * to write from devproc and then restore the saved values before returning.
  733. */
  734. void
  735. setregisters(Ureg* ureg, char* pureg, char* uva, int n)
  736. {
  737. ulong flags;
  738. ulong cs;
  739. ulong ss;
  740. flags = ureg->flags;
  741. cs = ureg->cs;
  742. ss = ureg->ss;
  743. memmove(pureg, uva, n);
  744. ureg->flags = (ureg->flags & 0x00FF) | (flags & 0xFF00);
  745. ureg->cs = cs;
  746. ureg->ss = ss;
  747. }
  748. static void
  749. linkproc(void)
  750. {
  751. spllo();
  752. up->kpfun(up->kparg);
  753. pexit("kproc dying", 0);
  754. }
  755. void
  756. kprocchild(Proc* p, void (*func)(void*), void* arg)
  757. {
  758. /*
  759. * gotolabel() needs a word on the stack in
  760. * which to place the return PC used to jump
  761. * to linkproc().
  762. */
  763. p->sched.pc = (ulong)linkproc;
  764. p->sched.sp = (ulong)p->kstack+KSTACK-BY2WD;
  765. p->kpfun = func;
  766. p->kparg = arg;
  767. }
  768. void
  769. forkchild(Proc *p, Ureg *ureg)
  770. {
  771. Ureg *cureg;
  772. /*
  773. * Add 2*BY2WD to the stack to account for
  774. * - the return PC
  775. * - trap's argument (ur)
  776. */
  777. p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Ureg)+2*BY2WD);
  778. p->sched.pc = (ulong)forkret;
  779. cureg = (Ureg*)(p->sched.sp+2*BY2WD);
  780. memmove(cureg, ureg, sizeof(Ureg));
  781. /* return value of syscall in child */
  782. cureg->ax = 0;
  783. /* Things from bottom of syscall which were never executed */
  784. p->psstate = 0;
  785. p->insyscall = 0;
  786. }
  787. /* Give enough context in the ureg to produce a kernel stack for
  788. * a sleeping process
  789. */
  790. void
  791. setkernur(Ureg* ureg, Proc* p)
  792. {
  793. ureg->pc = p->sched.pc;
  794. ureg->sp = p->sched.sp+4;
  795. }
  796. ulong
  797. dbgpc(Proc *p)
  798. {
  799. Ureg *ureg;
  800. ureg = p->dbgreg;
  801. if(ureg == 0)
  802. return 0;
  803. return ureg->pc;
  804. }