trap.c 21 KB

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