trap.c 20 KB

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