trap.c 20 KB

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