trap.c 19 KB

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