trap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include <tos.h>
  16. #include "ureg.h"
  17. #include "../port/pmc.h"
  18. #include "io.h"
  19. #include "amd64.h"
  20. // counters. Set by assembly code.
  21. // interrupt enter and exit, systecm call enter and exit.
  22. unsigned long ire, irx, sce, scx;
  23. // Did we start doing an exit for the interrupts?
  24. // ir exit entry :-)
  25. unsigned long irxe;
  26. extern int notify(Ureg*);
  27. static void debugbpt(Ureg*, void*);
  28. static void faultamd64(Ureg*, void*);
  29. static void doublefault(Ureg*, void*);
  30. static void unexpected(Ureg*, void*);
  31. static void expected(Ureg*, void*);
  32. static void dumpstackwithureg(Ureg*);
  33. static Lock vctllock;
  34. static Vctl *vctl[256];
  35. typedef struct Intrtime Intrtime;
  36. struct Intrtime {
  37. uint64_t count;
  38. uint64_t cycles;
  39. };
  40. static Intrtime intrtimes[256];
  41. void*
  42. intrenable(int irq, void (*f)(Ureg*, void*), void* a, int tbdf, char *name)
  43. {
  44. int vno;
  45. Vctl *v;
  46. extern int ioapicintrenable(Vctl*);
  47. if(f == nil){
  48. print("intrenable: nil handler for %d, tbdf %#ux for %s\n",
  49. irq, tbdf, name);
  50. return nil;
  51. }
  52. v = malloc(sizeof(Vctl));
  53. v->isintr = 1;
  54. v->irq = irq;
  55. v->tbdf = tbdf;
  56. v->f = f;
  57. v->a = a;
  58. strncpy(v->name, name, KNAMELEN-1);
  59. v->name[KNAMELEN-1] = 0;
  60. ilock(&vctllock);
  61. vno = ioapicintrenable(v);
  62. if(vno == -1){
  63. iunlock(&vctllock);
  64. print("intrenable: couldn't enable irq %d, tbdf %#ux for %s\n",
  65. irq, tbdf, v->name);
  66. free(v);
  67. return nil;
  68. }
  69. if(vctl[vno]){
  70. if(vctl[v->vno]->isr != v->isr || vctl[v->vno]->eoi != v->eoi)
  71. panic("intrenable: handler: %s %s %#p %#p %#p %#p",
  72. vctl[v->vno]->name, v->name,
  73. vctl[v->vno]->isr, v->isr, vctl[v->vno]->eoi, v->eoi);
  74. }
  75. v->vno = vno;
  76. v->next = vctl[vno];
  77. vctl[vno] = v;
  78. iunlock(&vctllock);
  79. if(v->mask)
  80. v->mask(v, 0);
  81. /*
  82. * Return the assigned vector so intrdisable can find
  83. * the handler; the IRQ is useless in the wonderful world
  84. * of the IOAPIC.
  85. */
  86. return v;
  87. }
  88. int
  89. intrdisable(void* vector)
  90. {
  91. Vctl *v, *x, **ll;
  92. extern int ioapicintrdisable(int);
  93. ilock(&vctllock);
  94. v = vector;
  95. if(v == nil || vctl[v->vno] != v)
  96. panic("intrdisable: v %#p", v);
  97. for(ll = vctl+v->vno; x = *ll; ll = &x->next)
  98. if(v == x)
  99. break;
  100. if(x != v)
  101. panic("intrdisable: v %#p", v);
  102. if(v->mask)
  103. v->mask(v, 1);
  104. v->f(nil, v->a);
  105. *ll = v->next;
  106. ioapicintrdisable(v->vno);
  107. iunlock(&vctllock);
  108. free(v);
  109. return 0;
  110. }
  111. static int32_t
  112. irqallocread(Chan* c, void *vbuf, int32_t n, int64_t offset)
  113. {
  114. char *buf, *p, str[2*(11+1)+2*(20+1)+(KNAMELEN+1)+(8+1)+1];
  115. int m, vno;
  116. int32_t oldn;
  117. Intrtime *t;
  118. Vctl *v;
  119. if(n < 0 || offset < 0)
  120. error(Ebadarg);
  121. oldn = n;
  122. buf = vbuf;
  123. for(vno=0; vno<nelem(vctl); vno++){
  124. for(v=vctl[vno]; v; v=v->next){
  125. t = intrtimes + vno;
  126. m = snprint(str, sizeof str, "%11d %11d %20llud %20llud %-*.*s %.*s\n",
  127. vno, v->irq, t->count, t->cycles, 8, 8, v->type, KNAMELEN, v->name);
  128. if(m <= offset) /* if do not want this, skip entry */
  129. offset -= m;
  130. else{
  131. /* skip offset bytes */
  132. m -= offset;
  133. p = str+offset;
  134. offset = 0;
  135. /* write at most max(n,m) bytes */
  136. if(m > n)
  137. m = n;
  138. memmove(buf, p, m);
  139. n -= m;
  140. buf += m;
  141. if(n == 0)
  142. return oldn;
  143. }
  144. }
  145. }
  146. return oldn - n;
  147. }
  148. void
  149. trapenable(int vno, void (*f)(Ureg*, void*), void* a, char *name)
  150. {
  151. Vctl *v;
  152. if(vno < 0 || vno >= 256)
  153. panic("trapenable: vno %d\n", vno);
  154. v = malloc(sizeof(Vctl));
  155. v->type = "trap";
  156. v->tbdf = BUSUNKNOWN;
  157. v->f = f;
  158. v->a = a;
  159. strncpy(v->name, name, KNAMELEN);
  160. v->name[KNAMELEN-1] = 0;
  161. ilock(&vctllock);
  162. v->next = vctl[vno];
  163. vctl[vno] = v;
  164. iunlock(&vctllock);
  165. }
  166. static void
  167. nmienable(void)
  168. {
  169. int x;
  170. /*
  171. * Hack: should be locked with NVRAM access.
  172. */
  173. outb(0x70, 0x80); /* NMI latch clear */
  174. outb(0x70, 0);
  175. x = inb(0x61) & 0x07; /* Enable NMI */
  176. outb(0x61, 0x08|x);
  177. outb(0x61, x);
  178. }
  179. void
  180. trapinit(void)
  181. {
  182. /*
  183. * Need to set BPT interrupt gate - here or in vsvminit?
  184. */
  185. /*
  186. * Special traps.
  187. * Syscall() is called directly without going through trap().
  188. */
  189. trapenable(VectorBPT, debugbpt, 0, "#BP");
  190. trapenable(VectorPF, faultamd64, 0, "#PF");
  191. trapenable(Vector2F, doublefault, 0, "#DF");
  192. intrenable(IdtIPI, expected, 0, BUSUNKNOWN, "#IPI");
  193. trapenable(Vector15, unexpected, 0, "#15");
  194. nmienable();
  195. addarchfile("irqalloc", 0444, irqallocread, nil);
  196. }
  197. static char* excname[32] = {
  198. "#DE", /* Divide-by-Zero Error */
  199. "#DB", /* Debug */
  200. "#NMI", /* Non-Maskable-Interrupt */
  201. "#BP", /* Breakpoint */
  202. "#OF", /* Overflow */
  203. "#BR", /* Bound-Range */
  204. "#UD", /* Invalid-Opcode */
  205. "#NM", /* Device-Not-Available */
  206. "#DF", /* Double-Fault */
  207. "#9 (reserved)",
  208. "#TS", /* Invalid-TSS */
  209. "#NP", /* Segment-Not-Present */
  210. "#SS", /* Stack */
  211. "#GP", /* General-Protection */
  212. "#PF", /* Page-Fault */
  213. "#15 (reserved)",
  214. "#MF", /* x87 FPE-Pending */
  215. "#AC", /* Alignment-Check */
  216. "#MC", /* Machine-Check */
  217. "#XF", /* SIMD Floating-Point */
  218. "#20 (reserved)",
  219. "#21 (reserved)",
  220. "#22 (reserved)",
  221. "#23 (reserved)",
  222. "#24 (reserved)",
  223. "#25 (reserved)",
  224. "#26 (reserved)",
  225. "#27 (reserved)",
  226. "#28 (reserved)",
  227. "#29 (reserved)",
  228. "#30 (reserved)",
  229. "#31 (reserved)",
  230. };
  231. /*
  232. * keep interrupt service times and counts
  233. */
  234. void
  235. intrtime(int vno)
  236. {
  237. Proc *up = externup();
  238. uint32_t diff, x; /* should be uint64_t */
  239. x = perfticks();
  240. diff = x - m->perf.intrts;
  241. m->perf.intrts = x;
  242. m->perf.inintr += diff;
  243. if(up == nil && m->perf.inidle > diff)
  244. m->perf.inidle -= diff;
  245. intrtimes[vno].cycles += diff;
  246. intrtimes[vno].count++;
  247. }
  248. static void
  249. pmcnop(Mach *m)
  250. {
  251. }
  252. void (*_pmcupdate)(Mach *m) = pmcnop;
  253. /* go to user space */
  254. void
  255. kexit(Ureg* u)
  256. {
  257. Proc *up = externup();
  258. uint64_t t;
  259. Tos *tos;
  260. Mach *mp;
  261. /*
  262. * precise time accounting, kernel exit
  263. * initialized in exec, sysproc.c
  264. */
  265. tos = (Tos*)(USTKTOP-sizeof(Tos));
  266. cycles(&t);
  267. tos->kcycles += t - up->kentry;
  268. tos->pcycles = up->pcycles;
  269. tos->pid = up->pid;
  270. if (up->ac != nil)
  271. mp = up->ac;
  272. else
  273. mp = m;
  274. tos->core = mp->machno;
  275. tos->nixtype = mp->nixtype;
  276. //_pmcupdate(m);
  277. /*
  278. * The process may change its core.
  279. * Be sure it has the right cyclefreq.
  280. */
  281. tos->cyclefreq = mp->cyclefreq;
  282. }
  283. void
  284. _trap(Ureg *ureg)
  285. {
  286. Proc *up = externup();
  287. /*
  288. * If it's a real trap in this core, then we want to
  289. * use the hardware cr2 register.
  290. * We cannot do this in trap() because application cores
  291. * would update m->cr2 with their cr2 values upon page faults,
  292. * and then call trap().
  293. * If we do this in trap(), we would overwrite that with our own cr2.
  294. */
  295. if(ureg->type == VectorPF)
  296. m->cr2 = cr2get();
  297. trap(ureg);
  298. }
  299. /*
  300. * All traps come here. It is slower to have all traps call trap()
  301. * rather than directly vectoring the handler. However, this avoids a
  302. * lot of code duplication and possible bugs. The only exception is
  303. * VectorSYSCALL.
  304. * Trap is called with interrupts disabled via interrupt-gates.
  305. */
  306. void
  307. trap(Ureg* ureg)
  308. {
  309. int clockintr, vno, user;
  310. // cache the previous vno to see what might be causing
  311. // trouble
  312. static int lastvno;
  313. vno = ureg->type;
  314. uint64_t gsbase = rdmsr(GSbase);
  315. //if (sce > scx) iprint("====================");
  316. if (vno == 8) {
  317. iprint("Lstar is %p\n", (void *)rdmsr(Lstar));
  318. iprint("GSbase is %p\n", (void *)gsbase);
  319. iprint("ire %d irx %d sce %d scx %d lastvno %d\n",
  320. ire, irx, sce, scx, lastvno);
  321. iprint("irxe %d \n",
  322. irxe);
  323. die("8");
  324. }
  325. lastvno = vno;
  326. if (gsbase < 1ULL<<63)
  327. die("bogus gsbase");
  328. Proc *up = externup();
  329. char buf[ERRMAX];
  330. Vctl *ctl, *v;
  331. if (0 && m && up && up->pid == 6) {
  332. //iprint("type %x\n", ureg->type);
  333. if (ureg->type != 0x49)
  334. die("6\n");
  335. }
  336. m->perf.intrts = perfticks();
  337. user = userureg(ureg);
  338. if(user && (m->nixtype == NIXTC)){
  339. up->dbgreg = ureg;
  340. cycles(&up->kentry);
  341. }
  342. clockintr = 0;
  343. //_pmcupdate(m);
  344. if(ctl = vctl[vno]){
  345. if(ctl->isintr){
  346. m->intr++;
  347. if(vno >= VectorPIC && vno != VectorSYSCALL)
  348. m->lastintr = ctl->irq;
  349. }else
  350. if(up)
  351. up->nqtrap++;
  352. if(ctl->isr)
  353. ctl->isr(vno);
  354. for(v = ctl; v != nil; v = v->next){
  355. if(v->f)
  356. v->f(ureg, v->a);
  357. }
  358. if(ctl->eoi)
  359. ctl->eoi(vno);
  360. intrtime(vno);
  361. if(ctl->isintr){
  362. if(ctl->irq == IrqCLOCK || ctl->irq == IrqTIMER)
  363. clockintr = 1;
  364. if(up && !clockintr)
  365. preempted();
  366. }
  367. }
  368. else if(vno < nelem(excname) && user){
  369. spllo();
  370. snprint(buf, sizeof buf, "sys: trap: %s", excname[vno]);
  371. postnote(up, 1, buf, NDebug);
  372. }
  373. else if(vno >= VectorPIC && vno != VectorSYSCALL){
  374. /*
  375. * An unknown interrupt.
  376. * Check for a default IRQ7. This can happen when
  377. * the IRQ input goes away before the acknowledge.
  378. * In this case, a 'default IRQ7' is generated, but
  379. * the corresponding bit in the ISR isn't set.
  380. * In fact, just ignore all such interrupts.
  381. */
  382. /* clear the interrupt */
  383. i8259isr(vno);
  384. iprint("cpu%d: spurious interrupt %d, last %d\n",
  385. machp()->machno, vno, m->lastintr);
  386. intrtime(vno);
  387. if(user)
  388. kexit(ureg);
  389. return;
  390. }
  391. else{
  392. if(vno == VectorNMI){
  393. nmienable();
  394. if(machp()->machno != 0){
  395. iprint("cpu%d: PC %#llux\n",
  396. machp()->machno, ureg->ip);
  397. for(;;);
  398. }
  399. }
  400. dumpregs(ureg);
  401. if(!user){
  402. ureg->sp = PTR2UINT(&ureg->sp);
  403. dumpstackwithureg(ureg);
  404. }
  405. if(vno < nelem(excname))
  406. panic("%s", excname[vno]);
  407. panic("unknown trap/intr: %d\n", vno);
  408. }
  409. splhi();
  410. /* delaysched set because we held a lock or because our quantum ended */
  411. if(up && up->delaysched && clockintr){
  412. if(0)
  413. if(user && up->ac == nil && up->nqtrap == 0 && up->nqsyscall == 0){
  414. if(!waserror()){
  415. up->ac = getac(up, -1);
  416. poperror();
  417. runacore();
  418. return;
  419. }
  420. }
  421. sched();
  422. splhi();
  423. }
  424. if(user){
  425. if(up && up->procctl || up->nnote)
  426. notify(ureg);
  427. kexit(ureg);
  428. }
  429. }
  430. /*
  431. * Dump general registers.
  432. */
  433. void
  434. dumpgpr(Ureg* ureg)
  435. {
  436. Proc *up = externup();
  437. if(up != nil)
  438. iprint("cpu%d: registers for %s %d\n",
  439. machp()->machno, up->text, up->pid);
  440. else
  441. iprint("cpu%d: registers for kernel\n", machp()->machno);
  442. iprint("ax\t%#16.16llux\n", ureg->ax);
  443. iprint("bx\t%#16.16llux\n", ureg->bx);
  444. iprint("cx\t%#16.16llux\n", ureg->cx);
  445. iprint("dx\t%#16.16llux\n", ureg->dx);
  446. iprint("di\t%#16.16llux\n", ureg->di);
  447. iprint("si\t%#16.16llux\n", ureg->si);
  448. iprint("bp\t%#16.16llux\n", ureg->bp);
  449. iprint("r8\t%#16.16llux\n", ureg->r8);
  450. iprint("r9\t%#16.16llux\n", ureg->r9);
  451. iprint("r10\t%#16.16llux\n", ureg->r10);
  452. iprint("r11\t%#16.16llux\n", ureg->r11);
  453. iprint("r12\t%#16.16llux\n", ureg->r12);
  454. iprint("r13\t%#16.16llux\n", ureg->r13);
  455. iprint("r14\t%#16.16llux\n", ureg->r14);
  456. iprint("r15\t%#16.16llux\n", ureg->r15);
  457. iprint("type\t%#llux\n", ureg->type);
  458. iprint("error\t%#llux\n", ureg->error);
  459. iprint("pc\t%#llux\n", ureg->ip);
  460. iprint("cs\t%#llux\n", ureg->cs);
  461. iprint("flags\t%#llux\n", ureg->flags);
  462. iprint("sp\t%#llux\n", ureg->sp);
  463. iprint("ss\t%#llux\n", ureg->ss);
  464. iprint("type\t%#llux\n", ureg->type);
  465. iprint("FS\t%#llux\n", rdmsr(FSbase));
  466. iprint("GS\t%#llux\n", rdmsr(GSbase));
  467. iprint("m\t%#16.16p\nup\t%#16.16p\n", m, up);
  468. }
  469. void
  470. dumpregs(Ureg* ureg)
  471. {
  472. Proc *up = externup();
  473. dumpgpr(ureg);
  474. /*
  475. * Processor control registers.
  476. * If machine check exception, time stamp counter, page size extensions
  477. * or enhanced virtual 8086 mode extensions are supported, there is a
  478. * CR4. If there is a CR4 and machine check extensions, read the machine
  479. * check address and machine check type registers if RDMSR supported.
  480. */
  481. iprint("cr0\t%#16.16llux\n", cr0get());
  482. iprint("cr2\t%#16.16llux\n", m->cr2);
  483. iprint("cr3\t%#16.16llux\n", cr3get());
  484. die("dumpregs");
  485. // archdumpregs();
  486. }
  487. /*
  488. * Fill in enough of Ureg to get a stack trace, and call a function.
  489. * Used by debugging interface rdb.
  490. */
  491. void
  492. callwithureg(void (*fn)(Ureg*))
  493. {
  494. Ureg ureg;
  495. ureg.ip = getcallerpc(&fn);
  496. ureg.sp = PTR2UINT(&fn);
  497. fn(&ureg);
  498. }
  499. static void
  500. dumpstackwithureg(Ureg* ureg)
  501. {
  502. Proc *up = externup();
  503. uintptr_t l, v, i, estack;
  504. // extern char etext;
  505. int x;
  506. if (0) { //if((s = getconf("*nodumpstack")) != nil && atoi(s) != 0){
  507. iprint("dumpstack disabled\n");
  508. return;
  509. }
  510. iprint("dumpstack\n");
  511. x = 0;
  512. x += iprint("ktrace 9%s %#p %#p\n", strrchr(conffile, '/')+1, ureg->ip, ureg->sp);
  513. i = 0;
  514. if(up != nil
  515. // && (uintptr)&l >= (uintptr)up->kstack
  516. && (uintptr_t)&l <= (uintptr_t)up->kstack+KSTACK)
  517. estack = (uintptr_t)up->kstack+KSTACK;
  518. else if((uintptr_t)&l >= m->stack && (uintptr_t)&l <= m->stack+MACHSTKSZ)
  519. estack = m->stack+MACHSTKSZ;
  520. else{
  521. if(up != nil)
  522. iprint("&up->kstack %#p &l %#p\n", up->kstack, &l);
  523. else
  524. iprint("&m %#p &l %#p\n", m, &l);
  525. return;
  526. }
  527. x += iprint("estackx %#p\n", estack);
  528. for(l = (uintptr_t)&l; l < estack; l += sizeof(uintptr_t)){
  529. v = *(uintptr_t*)l;
  530. if((KTZERO < v && v < (uintptr_t)&etext)
  531. || ((uintptr_t)&l < v && v < estack) || estack-l < 256){
  532. x += iprint("%#16.16p=%#16.16p ", l, v);
  533. i++;
  534. }
  535. if(i == 2){
  536. i = 0;
  537. x += iprint("\n");
  538. }
  539. }
  540. if(i)
  541. iprint("\n");
  542. }
  543. void
  544. dumpstack(void)
  545. {
  546. callwithureg(dumpstackwithureg);
  547. }
  548. static void
  549. debugbpt(Ureg* ureg, void* v)
  550. {
  551. Proc *up = externup();
  552. char buf[ERRMAX];
  553. if(up == 0)
  554. panic("kernel bpt");
  555. /* restore pc to instruction that caused the trap */
  556. ureg->ip--;
  557. sprint(buf, "sys: breakpoint");
  558. postnote(up, 1, buf, NDebug);
  559. }
  560. static void
  561. doublefault(Ureg* ureg, void* v)
  562. {
  563. iprint("cr2 %p\n", (void *)cr2get());
  564. panic("double fault");
  565. }
  566. static void
  567. unexpected(Ureg* ureg, void* v)
  568. {
  569. iprint("unexpected trap %llud; ignoring\n", ureg->type);
  570. }
  571. static void
  572. expected(Ureg* ureg, void* v)
  573. {
  574. }
  575. static void
  576. faultamd64(Ureg* ureg, void* v)
  577. {
  578. Proc *up = externup();
  579. uint64_t addr;
  580. int read, user, insyscall;
  581. char buf[ERRMAX];
  582. addr = m->cr2;
  583. user = userureg(ureg);
  584. if(!user && mmukmapsync(addr))
  585. return;
  586. /*
  587. * There must be a user context.
  588. * If not, the usual problem is causing a fault during
  589. * initialisation before the system is fully up.
  590. */
  591. if(up == nil){
  592. panic("fault with up == nil; pc %#llux addr %#llux\n",
  593. ureg->ip, addr);
  594. }
  595. read = !(ureg->error & 2);
  596. /*
  597. if (read) hi("read fault\n"); else hi("write fault\n");
  598. hi("addr "); put64(addr); hi("\n");
  599. */
  600. insyscall = up->insyscall;
  601. up->insyscall = 1;
  602. if (0)hi("call fault\n");
  603. if(fault(addr, read) < 0){
  604. iprint("could not fault %p\n", addr);
  605. if (! user)
  606. panic("fault went bad in kernel\n");
  607. else
  608. /*
  609. * It is possible to get here with !user if, for example,
  610. * a process was in a system call accessing a shared
  611. * segment but was preempted by another process which shrunk
  612. * or deallocated the shared segment; when the original
  613. * process resumes it may fault while in kernel mode.
  614. * No need to panic this case, post a note to the process
  615. * and unwind the error stack. There must be an error stack
  616. * (up->nerrlab != 0) if this is a system call, if not then
  617. * the game's a bogey.
  618. */
  619. if(!user && (!insyscall || up->nerrlab == 0))
  620. panic("fault: %#llux\n", addr);
  621. sprint(buf, "sys: trap: fault %s addr=%#llux",
  622. read? "read": "write", addr);
  623. postnote(up, 1, buf, NDebug);
  624. if(insyscall)
  625. error(buf);
  626. }
  627. up->insyscall = insyscall;
  628. }
  629. /*
  630. * return the userpc the last exception happened at
  631. */
  632. uintptr_t
  633. userpc(Ureg* ureg)
  634. {
  635. Proc *up = externup();
  636. if(ureg == nil)
  637. ureg = up->dbgreg;
  638. return ureg->ip;
  639. }
  640. /* This routine must save the values of registers the user is not permitted
  641. * to write from devproc and then restore the saved values before returning.
  642. * TODO: fix this because the segment registers are wrong for 64-bit mode.
  643. */
  644. void
  645. setregisters(Ureg* ureg, char* pureg, char* uva, int n)
  646. {
  647. uint64_t cs, flags, ss;
  648. ss = ureg->ss;
  649. flags = ureg->flags;
  650. cs = ureg->cs;
  651. memmove(pureg, uva, n);
  652. ureg->cs = cs;
  653. ureg->flags = (ureg->flags & 0x00ff) | (flags & 0xff00);
  654. ureg->ss = ss;
  655. }
  656. /* Give enough context in the ureg to produce a kernel stack for
  657. * a sleeping process
  658. */
  659. void
  660. setkernur(Ureg* ureg, Proc* p)
  661. {
  662. ureg->ip = p->sched.pc;
  663. ureg->sp = p->sched.sp+BY2SE;
  664. }
  665. uintptr_t
  666. dbgpc(Proc *p)
  667. {
  668. Ureg *ureg;
  669. ureg = p->dbgreg;
  670. if(ureg == 0)
  671. return 0;
  672. return ureg->ip;
  673. }