trap.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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 %#p %#p %#p %#p",
  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", 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. v->next = vctl[vno];
  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. /*
  155. * Minimal trap setup. Just enough so that we can panic
  156. * on traps (bugs) during kernel initialization.
  157. * Called very early - malloc is not yet available.
  158. */
  159. void
  160. trapinit0(void)
  161. {
  162. int d1, v;
  163. ulong vaddr;
  164. Segdesc *idt;
  165. idt = (Segdesc*)IDTADDR;
  166. vaddr = (ulong)vectortable;
  167. for(v = 0; v < 256; v++){
  168. d1 = (vaddr & 0xFFFF0000)|SEGP;
  169. switch(v){
  170. case VectorBPT:
  171. d1 |= SEGPL(3)|SEGIG;
  172. break;
  173. case VectorSYSCALL:
  174. d1 |= SEGPL(3)|SEGIG;
  175. break;
  176. default:
  177. d1 |= SEGPL(0)|SEGIG;
  178. break;
  179. }
  180. idt[v].d0 = (vaddr & 0xFFFF)|(KESEL<<16);
  181. idt[v].d1 = d1;
  182. vaddr += 6;
  183. }
  184. }
  185. void
  186. trapinit(void)
  187. {
  188. /*
  189. * Special traps.
  190. * Syscall() is called directly without going through trap().
  191. */
  192. trapenable(VectorBPT, debugbpt, 0, "debugpt");
  193. trapenable(VectorPF, fault386, 0, "fault386");
  194. trapenable(Vector2F, doublefault, 0, "doublefault");
  195. trapenable(Vector15, unexpected, 0, "unexpected");
  196. nmienable();
  197. addarchfile("irqalloc", 0444, irqallocread, nil);
  198. trapinited = 1;
  199. }
  200. static char* excname[32] = {
  201. "divide error",
  202. "debug exception",
  203. "nonmaskable interrupt",
  204. "breakpoint",
  205. "overflow",
  206. "bounds check",
  207. "invalid opcode",
  208. "coprocessor not available",
  209. "double fault",
  210. "coprocessor segment overrun",
  211. "invalid TSS",
  212. "segment not present",
  213. "stack exception",
  214. "general protection violation",
  215. "page fault",
  216. "15 (reserved)",
  217. "coprocessor error",
  218. "alignment check",
  219. "machine check",
  220. "19 (reserved)",
  221. "20 (reserved)",
  222. "21 (reserved)",
  223. "22 (reserved)",
  224. "23 (reserved)",
  225. "24 (reserved)",
  226. "25 (reserved)",
  227. "26 (reserved)",
  228. "27 (reserved)",
  229. "28 (reserved)",
  230. "29 (reserved)",
  231. "30 (reserved)",
  232. "31 (reserved)",
  233. };
  234. /*
  235. * keep histogram of interrupt service times
  236. */
  237. void
  238. intrtime(Mach*, int vno)
  239. {
  240. ulong diff;
  241. ulong x;
  242. x = perfticks();
  243. diff = x - m->perf.intrts;
  244. m->perf.intrts = x;
  245. m->perf.inintr += diff;
  246. if(up == nil && m->perf.inidle > diff)
  247. m->perf.inidle -= diff;
  248. diff /= m->cpumhz*100; /* quantum = 100µsec */
  249. if(diff >= Ntimevec)
  250. diff = Ntimevec-1;
  251. intrtimes[vno][diff]++;
  252. }
  253. /* go to user space */
  254. void
  255. kexit(Ureg*)
  256. {
  257. uvlong t;
  258. Tos *tos;
  259. /* precise time accounting, kernel exit */
  260. tos = (Tos*)(USTKTOP-sizeof(Tos));
  261. cycles(&t);
  262. tos->kcycles += t - up->kentry;
  263. tos->pcycles = up->pcycles;
  264. tos->pid = up->pid;
  265. }
  266. /*
  267. * All traps come here. It is slower to have all traps call trap()
  268. * rather than directly vectoring the handler. However, this avoids a
  269. * lot of code duplication and possible bugs. The only exception is
  270. * VectorSYSCALL.
  271. * Trap is called with interrupts disabled via interrupt-gates.
  272. */
  273. void
  274. trap(Ureg* ureg)
  275. {
  276. int clockintr, i, vno, user;
  277. char buf[ERRMAX];
  278. Vctl *ctl, *v;
  279. Mach *mach;
  280. if(!trapinited){
  281. /* fault386 can give a better error message */
  282. if(ureg->trap == VectorPF)
  283. fault386(ureg, nil);
  284. panic("trap %lud: not ready", ureg->trap);
  285. }
  286. m->perf.intrts = perfticks();
  287. user = (ureg->cs & 0xFFFF) == UESEL;
  288. if(user){
  289. up->dbgreg = ureg;
  290. cycles(&up->kentry);
  291. }
  292. clockintr = 0;
  293. vno = ureg->trap;
  294. if(ctl = vctl[vno]){
  295. if(ctl->isintr){
  296. m->intr++;
  297. if(vno >= VectorPIC && vno != VectorSYSCALL)
  298. m->lastintr = ctl->irq;
  299. }
  300. if(ctl->isr)
  301. ctl->isr(vno);
  302. for(v = ctl; v != nil; v = v->next){
  303. if(v->f)
  304. v->f(ureg, v->a);
  305. }
  306. if(ctl->eoi)
  307. ctl->eoi(vno);
  308. if(ctl->isintr){
  309. intrtime(m, vno);
  310. if(ctl->irq == IrqCLOCK || ctl->irq == IrqTIMER)
  311. clockintr = 1;
  312. if(up && !clockintr)
  313. preempted();
  314. }
  315. }
  316. else if(vno < nelem(excname) && user){
  317. spllo();
  318. snprint(buf, sizeof buf, "sys: trap: %s", excname[vno]);
  319. postnote(up, 1, buf, NDebug);
  320. }
  321. else if(vno >= VectorPIC && vno != VectorSYSCALL){
  322. /*
  323. * An unknown interrupt.
  324. * Check for a default IRQ7. This can happen when
  325. * the IRQ input goes away before the acknowledge.
  326. * In this case, a 'default IRQ7' is generated, but
  327. * the corresponding bit in the ISR isn't set.
  328. * In fact, just ignore all such interrupts.
  329. */
  330. /* call all interrupt routines, just in case */
  331. for(i = VectorPIC; i <= MaxIrqLAPIC; i++){
  332. ctl = vctl[i];
  333. if(ctl == nil)
  334. continue;
  335. if(!ctl->isintr)
  336. continue;
  337. for(v = ctl; v != nil; v = v->next){
  338. if(v->f)
  339. v->f(ureg, v->a);
  340. }
  341. /* should we do this? */
  342. if(ctl->eoi)
  343. ctl->eoi(i);
  344. }
  345. /* clear the interrupt */
  346. i8259isr(vno);
  347. if(0)print("cpu%d: spurious interrupt %d, last %d\n",
  348. m->machno, vno, m->lastintr);
  349. if(0)if(conf.nmach > 1){
  350. for(i = 0; i < 32; i++){
  351. if(!(active.machs & (1<<i)))
  352. continue;
  353. mach = MACHP(i);
  354. if(m->machno == mach->machno)
  355. continue;
  356. print(" cpu%d: last %d",
  357. mach->machno, mach->lastintr);
  358. }
  359. print("\n");
  360. }
  361. m->spuriousintr++;
  362. if(user)
  363. kexit(ureg);
  364. return;
  365. }
  366. else{
  367. if(vno == VectorNMI){
  368. /*
  369. * Don't re-enable, it confuses the crash dumps.
  370. nmienable();
  371. */
  372. iprint("cpu%d: NMI PC %#8.8lux\n", m->machno, ureg->pc);
  373. while(m->machno != 0)
  374. ;
  375. }
  376. dumpregs(ureg);
  377. if(!user){
  378. ureg->sp = (ulong)&ureg->sp;
  379. _dumpstack(ureg);
  380. }
  381. if(vno < nelem(excname))
  382. panic("%s", excname[vno]);
  383. panic("unknown trap/intr: %d", vno);
  384. }
  385. splhi();
  386. /* delaysched set because we held a lock or because our quantum ended */
  387. if(up && up->delaysched && clockintr){
  388. sched();
  389. splhi();
  390. }
  391. if(user){
  392. if(up->procctl || up->nnote)
  393. notify(ureg);
  394. kexit(ureg);
  395. }
  396. }
  397. /*
  398. * dump registers
  399. */
  400. void
  401. dumpregs2(Ureg* ureg)
  402. {
  403. if(up)
  404. iprint("cpu%d: registers for %s %lud\n",
  405. m->machno, up->text, up->pid);
  406. else
  407. iprint("cpu%d: registers for kernel\n", m->machno);
  408. iprint("FLAGS=%luX TRAP=%luX ECODE=%luX PC=%luX",
  409. ureg->flags, ureg->trap, ureg->ecode, ureg->pc);
  410. iprint(" SS=%4.4luX USP=%luX\n", ureg->ss & 0xFFFF, ureg->usp);
  411. iprint(" AX %8.8luX BX %8.8luX CX %8.8luX DX %8.8luX\n",
  412. ureg->ax, ureg->bx, ureg->cx, ureg->dx);
  413. iprint(" SI %8.8luX DI %8.8luX BP %8.8luX\n",
  414. ureg->si, ureg->di, ureg->bp);
  415. iprint(" CS %4.4luX DS %4.4luX ES %4.4luX FS %4.4luX GS %4.4luX\n",
  416. ureg->cs & 0xFFFF, ureg->ds & 0xFFFF, ureg->es & 0xFFFF,
  417. ureg->fs & 0xFFFF, ureg->gs & 0xFFFF);
  418. }
  419. void
  420. dumpregs(Ureg* ureg)
  421. {
  422. vlong mca, mct;
  423. dumpregs2(ureg);
  424. /*
  425. * Processor control registers.
  426. * If machine check exception, time stamp counter, page size extensions
  427. * or enhanced virtual 8086 mode extensions are supported, there is a
  428. * CR4. If there is a CR4 and machine check extensions, read the machine
  429. * check address and machine check type registers if RDMSR supported.
  430. */
  431. iprint(" CR0 %8.8lux CR2 %8.8lux CR3 %8.8lux",
  432. getcr0(), getcr2(), getcr3());
  433. if(m->cpuiddx & (Mce|Tsc|Pse|Vmex)){
  434. iprint(" CR4 %8.8lux", getcr4());
  435. if((m->cpuiddx & (Mce|Cpumsr)) == (Mce|Cpumsr)){
  436. rdmsr(0x00, &mca);
  437. rdmsr(0x01, &mct);
  438. iprint("\n MCA %8.8llux MCT %8.8llux", mca, mct);
  439. }
  440. }
  441. iprint("\n ur %#p up %#p\n", ureg, up);
  442. }
  443. /*
  444. * Fill in enough of Ureg to get a stack trace, and call a function.
  445. * Used by debugging interface rdb.
  446. */
  447. void
  448. callwithureg(void (*fn)(Ureg*))
  449. {
  450. Ureg ureg;
  451. ureg.pc = getcallerpc(&fn);
  452. ureg.sp = (ulong)&fn;
  453. fn(&ureg);
  454. }
  455. static void
  456. _dumpstack(Ureg *ureg)
  457. {
  458. uintptr l, v, i, estack;
  459. extern ulong etext;
  460. int x;
  461. char *s;
  462. if((s = getconf("*nodumpstack")) != nil && strcmp(s, "0") != 0){
  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. snprint(buf, sizeof 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", addr);
  567. }
  568. checkpages();
  569. checkfault(addr, ureg->pc);
  570. snprint(buf, sizeof 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. vlong startns, stopns;
  592. if((ureg->cs & 0xFFFF) != UESEL)
  593. panic("syscall: cs 0x%4.4luX", ureg->cs);
  594. cycles(&up->kentry);
  595. m->syscall++;
  596. up->insyscall = 1;
  597. up->pc = ureg->pc;
  598. up->dbgreg = ureg;
  599. sp = ureg->usp;
  600. scallnr = ureg->ax;
  601. up->scallnr = scallnr;
  602. if(up->procctl == Proc_tracesyscall){
  603. /*
  604. * Redundant validaddr. Do we care?
  605. * Tracing syscalls is not exactly a fast path...
  606. * Beware, validaddr currently does a pexit rather
  607. * than an error if there's a problem; that might
  608. * change in the future.
  609. */
  610. if(sp < (USTKTOP-BY2PG) || sp > (USTKTOP-sizeof(Sargs)-BY2WD))
  611. validaddr(sp, sizeof(Sargs)+BY2WD, 0);
  612. syscallfmt(scallnr, ureg->pc, (va_list)(sp+BY2WD));
  613. up->procctl = Proc_stopme;
  614. procctl(up);
  615. if(up->syscalltrace)
  616. free(up->syscalltrace);
  617. up->syscalltrace = nil;
  618. startns = todget(nil);
  619. }
  620. if(scallnr == RFORK && up->fpstate == FPactive){
  621. fpsave(&up->fpsave);
  622. up->fpstate = FPinactive;
  623. }
  624. spllo();
  625. up->nerrlab = 0;
  626. ret = -1;
  627. if(!waserror()){
  628. if(scallnr >= nsyscall || systab[scallnr] == 0){
  629. pprint("bad sys call number %lud pc %lux\n",
  630. scallnr, ureg->pc);
  631. postnote(up, 1, "sys: bad sys call", NDebug);
  632. error(Ebadarg);
  633. }
  634. if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)-BY2WD))
  635. validaddr(sp, sizeof(Sargs)+BY2WD, 0);
  636. up->s = *((Sargs*)(sp+BY2WD));
  637. up->psstate = sysctab[scallnr];
  638. ret = systab[scallnr](up->s.args);
  639. poperror();
  640. }else{
  641. /* failure: save the error buffer for errstr */
  642. e = up->syserrstr;
  643. up->syserrstr = up->errstr;
  644. up->errstr = e;
  645. if(0 && up->pid == 1)
  646. print("syscall %lud error %s\n", scallnr, up->syserrstr);
  647. }
  648. if(up->nerrlab){
  649. print("bad errstack [%lud]: %d extra\n", scallnr, up->nerrlab);
  650. for(i = 0; i < NERR; i++)
  651. print("sp=%lux pc=%lux\n",
  652. up->errlab[i].sp, up->errlab[i].pc);
  653. panic("error stack");
  654. }
  655. /*
  656. * Put return value in frame. On the x86 the syscall is
  657. * just another trap and the return value from syscall is
  658. * ignored. On other machines the return value is put into
  659. * the results register by caller of syscall.
  660. */
  661. ureg->ax = ret;
  662. if(up->procctl == Proc_tracesyscall){
  663. stopns = todget(nil);
  664. up->procctl = Proc_stopme;
  665. sysretfmt(scallnr, (va_list)(sp+BY2WD), ret, startns, stopns);
  666. s = splhi();
  667. procctl(up);
  668. splx(s);
  669. if(up->syscalltrace)
  670. free(up->syscalltrace);
  671. up->syscalltrace = nil;
  672. }
  673. up->insyscall = 0;
  674. up->psstate = 0;
  675. if(scallnr == NOTED)
  676. noted(ureg, *(ulong*)(sp+BY2WD));
  677. if(scallnr!=RFORK && (up->procctl || up->nnote)){
  678. splhi();
  679. notify(ureg);
  680. }
  681. /* if we delayed sched because we held a lock, sched now */
  682. if(up->delaysched)
  683. sched();
  684. kexit(ureg);
  685. }
  686. /*
  687. * Call user, if necessary, with note.
  688. * Pass user the Ureg struct and the note on his stack.
  689. */
  690. int
  691. notify(Ureg* ureg)
  692. {
  693. int l;
  694. ulong s, sp;
  695. Note *n;
  696. if(up->procctl)
  697. procctl(up);
  698. if(up->nnote == 0)
  699. return 0;
  700. if(up->fpstate == FPactive){
  701. fpsave(&up->fpsave);
  702. up->fpstate = FPinactive;
  703. }
  704. up->fpstate |= FPillegal;
  705. s = spllo();
  706. qlock(&up->debug);
  707. up->notepending = 0;
  708. n = &up->note[0];
  709. if(strncmp(n->msg, "sys:", 4) == 0){
  710. l = strlen(n->msg);
  711. if(l > ERRMAX-15) /* " pc=0x12345678\0" */
  712. l = ERRMAX-15;
  713. seprint(n->msg+l, &n->msg[sizeof n->msg], " pc=0x%.8lux",
  714. ureg->pc);
  715. }
  716. if(n->flag!=NUser && (up->notified || up->notify==0)){
  717. if(n->flag == NDebug)
  718. pprint("suicide: %s\n", n->msg);
  719. qunlock(&up->debug);
  720. pexit(n->msg, n->flag!=NDebug);
  721. }
  722. if(up->notified){
  723. qunlock(&up->debug);
  724. splhi();
  725. return 0;
  726. }
  727. if(!up->notify){
  728. qunlock(&up->debug);
  729. pexit(n->msg, n->flag!=NDebug);
  730. }
  731. sp = ureg->usp;
  732. sp -= 256; /* debugging: preserve context causing problem */
  733. sp -= sizeof(Ureg);
  734. if(0) print("%s %lud: notify %.8lux %.8lux %.8lux %s\n",
  735. up->text, up->pid, ureg->pc, ureg->usp, sp, n->msg);
  736. if(!okaddr((ulong)up->notify, 1, 0)
  737. || !okaddr(sp-ERRMAX-4*BY2WD, sizeof(Ureg)+ERRMAX+4*BY2WD, 1)){
  738. qunlock(&up->debug);
  739. pprint("suicide: bad address in notify\n");
  740. pexit("Suicide", 0);
  741. }
  742. memmove((Ureg*)sp, ureg, sizeof(Ureg));
  743. *(Ureg**)(sp-BY2WD) = up->ureg; /* word under Ureg is old up->ureg */
  744. up->ureg = (void*)sp;
  745. sp -= BY2WD+ERRMAX;
  746. memmove((char*)sp, up->note[0].msg, ERRMAX);
  747. sp -= 3*BY2WD;
  748. *(ulong*)(sp+2*BY2WD) = sp+3*BY2WD; /* arg 2 is string */
  749. *(ulong*)(sp+1*BY2WD) = (ulong)up->ureg; /* arg 1 is ureg* */
  750. *(ulong*)(sp+0*BY2WD) = 0; /* arg 0 is pc */
  751. ureg->usp = sp;
  752. ureg->pc = (ulong)up->notify;
  753. up->notified = 1;
  754. up->nnote--;
  755. memmove(&up->lastnote, &up->note[0], sizeof(Note));
  756. memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
  757. qunlock(&up->debug);
  758. splx(s);
  759. return 1;
  760. }
  761. /*
  762. * Return user to state before notify()
  763. */
  764. void
  765. noted(Ureg* ureg, ulong arg0)
  766. {
  767. Ureg *nureg;
  768. ulong oureg, sp;
  769. qlock(&up->debug);
  770. if(arg0!=NRSTR && !up->notified) {
  771. qunlock(&up->debug);
  772. pprint("call to noted() when not notified\n");
  773. pexit("Suicide", 0);
  774. }
  775. up->notified = 0;
  776. nureg = up->ureg; /* pointer to user returned Ureg struct */
  777. up->fpstate &= ~FPillegal;
  778. /* sanity clause */
  779. oureg = (ulong)nureg;
  780. if(!okaddr((ulong)oureg-BY2WD, BY2WD+sizeof(Ureg), 0)){
  781. qunlock(&up->debug);
  782. pprint("bad ureg in noted or call to noted when not notified\n");
  783. pexit("Suicide", 0);
  784. }
  785. /*
  786. * Check the segment selectors are all valid, otherwise
  787. * a fault will be taken on attempting to return to the
  788. * user process.
  789. * Take care with the comparisons as different processor
  790. * generations push segment descriptors in different ways.
  791. */
  792. if((nureg->cs & 0xFFFF) != UESEL || (nureg->ss & 0xFFFF) != UDSEL
  793. || (nureg->ds & 0xFFFF) != UDSEL || (nureg->es & 0xFFFF) != UDSEL
  794. || (nureg->fs & 0xFFFF) != UDSEL || (nureg->gs & 0xFFFF) != UDSEL){
  795. qunlock(&up->debug);
  796. pprint("bad segment selector in noted\n");
  797. pexit("Suicide", 0);
  798. }
  799. /* don't let user change system flags */
  800. nureg->flags = (ureg->flags & ~0xCD5) | (nureg->flags & 0xCD5);
  801. memmove(ureg, nureg, sizeof(Ureg));
  802. switch(arg0){
  803. case NCONT:
  804. case NRSTR:
  805. if(0) print("%s %lud: noted %.8lux %.8lux\n",
  806. up->text, up->pid, nureg->pc, nureg->usp);
  807. if(!okaddr(nureg->pc, 1, 0) || !okaddr(nureg->usp, BY2WD, 0)){
  808. qunlock(&up->debug);
  809. pprint("suicide: trap in noted\n");
  810. pexit("Suicide", 0);
  811. }
  812. up->ureg = (Ureg*)(*(ulong*)(oureg-BY2WD));
  813. qunlock(&up->debug);
  814. break;
  815. case NSAVE:
  816. if(!okaddr(nureg->pc, BY2WD, 0)
  817. || !okaddr(nureg->usp, BY2WD, 0)){
  818. qunlock(&up->debug);
  819. pprint("suicide: trap in noted\n");
  820. pexit("Suicide", 0);
  821. }
  822. qunlock(&up->debug);
  823. sp = oureg-4*BY2WD-ERRMAX;
  824. splhi();
  825. ureg->sp = sp;
  826. ((ulong*)sp)[1] = oureg; /* arg 1 0(FP) is ureg* */
  827. ((ulong*)sp)[0] = 0; /* arg 0 is pc */
  828. break;
  829. default:
  830. pprint("unknown noted arg 0x%lux\n", arg0);
  831. up->lastnote.flag = NDebug;
  832. /* fall through */
  833. case NDFLT:
  834. if(up->lastnote.flag == NDebug){
  835. qunlock(&up->debug);
  836. pprint("suicide: %s\n", up->lastnote.msg);
  837. } else
  838. qunlock(&up->debug);
  839. pexit(up->lastnote.msg, up->lastnote.flag!=NDebug);
  840. }
  841. }
  842. void
  843. validalign(uintptr addr, unsigned align)
  844. {
  845. /*
  846. * Plan 9 is a 32-bit O/S, and the hardware it runs on
  847. * does not usually have instructions which move 64-bit
  848. * quantities directly, synthesizing the operations
  849. * with 32-bit move instructions. Therefore, the compiler
  850. * (and hardware) usually only enforce 32-bit alignment,
  851. * if at all.
  852. *
  853. * Take this out if the architecture warrants it.
  854. */
  855. if(align == sizeof(vlong))
  856. align = sizeof(long);
  857. /*
  858. * Check align is a power of 2, then addr alignment.
  859. */
  860. if((align != 0 && !(align & (align-1))) && !(addr & (align-1)))
  861. return;
  862. postnote(up, 1, "sys: odd address", NDebug);
  863. error(Ebadarg);
  864. /*NOTREACHED*/
  865. }
  866. long
  867. execregs(ulong entry, ulong ssize, ulong nargs)
  868. {
  869. ulong *sp;
  870. Ureg *ureg;
  871. up->fpstate = FPinit;
  872. fpoff();
  873. sp = (ulong*)(USTKTOP - ssize);
  874. *--sp = nargs;
  875. ureg = up->dbgreg;
  876. ureg->usp = (ulong)sp;
  877. ureg->pc = entry;
  878. return USTKTOP-sizeof(Tos); /* address of kernel/user shared data */
  879. }
  880. /*
  881. * return the userpc the last exception happened at
  882. */
  883. ulong
  884. userpc(void)
  885. {
  886. Ureg *ureg;
  887. ureg = (Ureg*)up->dbgreg;
  888. return ureg->pc;
  889. }
  890. /* This routine must save the values of registers the user is not permitted
  891. * to write from devproc and then restore the saved values before returning.
  892. */
  893. void
  894. setregisters(Ureg* ureg, char* pureg, char* uva, int n)
  895. {
  896. ulong cs, ds, es, flags, fs, gs, ss;
  897. ss = ureg->ss;
  898. flags = ureg->flags;
  899. cs = ureg->cs;
  900. ds = ureg->ds;
  901. es = ureg->es;
  902. fs = ureg->fs;
  903. gs = ureg->gs;
  904. memmove(pureg, uva, n);
  905. ureg->gs = gs;
  906. ureg->fs = fs;
  907. ureg->es = es;
  908. ureg->ds = ds;
  909. ureg->cs = cs;
  910. ureg->flags = (ureg->flags & 0x00FF) | (flags & 0xFF00);
  911. ureg->ss = ss;
  912. }
  913. static void
  914. linkproc(void)
  915. {
  916. spllo();
  917. up->kpfun(up->kparg);
  918. pexit("kproc dying", 0);
  919. }
  920. void
  921. kprocchild(Proc* p, void (*func)(void*), void* arg)
  922. {
  923. /*
  924. * gotolabel() needs a word on the stack in
  925. * which to place the return PC used to jump
  926. * to linkproc().
  927. */
  928. p->sched.pc = (ulong)linkproc;
  929. p->sched.sp = (ulong)p->kstack+KSTACK-BY2WD;
  930. p->kpfun = func;
  931. p->kparg = arg;
  932. }
  933. void
  934. forkchild(Proc *p, Ureg *ureg)
  935. {
  936. Ureg *cureg;
  937. /*
  938. * Add 2*BY2WD to the stack to account for
  939. * - the return PC
  940. * - trap's argument (ur)
  941. */
  942. p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Ureg)+2*BY2WD);
  943. p->sched.pc = (ulong)forkret;
  944. cureg = (Ureg*)(p->sched.sp+2*BY2WD);
  945. memmove(cureg, ureg, sizeof(Ureg));
  946. /* return value of syscall in child */
  947. cureg->ax = 0;
  948. /* Things from bottom of syscall which were never executed */
  949. p->psstate = 0;
  950. p->insyscall = 0;
  951. }
  952. /* Give enough context in the ureg to produce a kernel stack for
  953. * a sleeping process
  954. */
  955. void
  956. setkernur(Ureg* ureg, Proc* p)
  957. {
  958. ureg->pc = p->sched.pc;
  959. ureg->sp = p->sched.sp+4;
  960. }
  961. ulong
  962. dbgpc(Proc *p)
  963. {
  964. Ureg *ureg;
  965. ureg = p->dbgreg;
  966. if(ureg == 0)
  967. return 0;
  968. return ureg->pc;
  969. }