trap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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->Vkey.irq = irq;
  55. v->Vkey.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->Vkey, 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->Vkey, 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->Vkey.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->Vkey.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 - machp()->perf.intrts;
  241. machp()->perf.intrts = x;
  242. machp()->perf.inintr += diff;
  243. if(up == nil && machp()->perf.inidle > diff)
  244. machp()->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 = machp();
  274. tos->core = mp->machno;
  275. tos->nixtype = mp->NIX.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. /* thread local storage */
  283. wrmsr(FSbase, up->tls);
  284. }
  285. void
  286. kstackok(void)
  287. {
  288. Proc *up = externup();
  289. if(up == nil){
  290. uintptr_t *stk = (uintptr_t*)machp()->stack;
  291. if(*stk != STACKGUARD)
  292. panic("trap: mach %d machstk went through bottom %p\n", machp()->machno, machp()->stack);
  293. } else {
  294. uintptr_t *stk = (uintptr_t*)up->kstack;
  295. if(*stk != STACKGUARD)
  296. panic("trap: proc %d kstack went through bottom %p\n", up->pid, up->kstack);
  297. }
  298. }
  299. void
  300. _trap(Ureg *ureg)
  301. {
  302. /*
  303. * If it's a real trap in this core, then we want to
  304. * use the hardware cr2 register.
  305. * We cannot do this in trap() because application cores
  306. * would update m->cr2 with their cr2 values upon page faults,
  307. * and then call trap().
  308. * If we do this in trap(), we would overwrite that with our own cr2.
  309. */
  310. if(ureg->type == VectorPF)
  311. machp()->MMU.cr2 = cr2get();
  312. trap(ureg);
  313. }
  314. /*
  315. * All traps come here. It is slower to have all traps call trap()
  316. * rather than directly vectoring the handler. However, this avoids a
  317. * lot of code duplication and possible bugs. The only exception is
  318. * VectorSYSCALL.
  319. * Trap is called with interrupts disabled via interrupt-gates.
  320. */
  321. void
  322. trap(Ureg* ureg)
  323. {
  324. int clockintr, vno, user;
  325. // cache the previous vno to see what might be causing
  326. // trouble
  327. static int lastvno;
  328. vno = ureg->type;
  329. uint64_t gsbase = rdmsr(GSbase);
  330. //if (sce > scx) iprint("====================");
  331. if (vno == 8) {
  332. iprint("Lstar is %p\n", (void *)rdmsr(Lstar));
  333. iprint("GSbase is %p\n", (void *)gsbase);
  334. iprint("ire %d irx %d sce %d scx %d lastvno %d\n",
  335. ire, irx, sce, scx, lastvno);
  336. iprint("irxe %d \n",
  337. irxe);
  338. die("8");
  339. }
  340. lastvno = vno;
  341. if (gsbase < 1ULL<<63)
  342. die("bogus gsbase");
  343. Proc *up = externup();
  344. char buf[ERRMAX];
  345. Vctl *ctl, *v;
  346. if (0 && machp() && up && up->pid == 6) {
  347. //iprint("type %x\n", ureg->type);
  348. if (ureg->type != 0x49)
  349. die("6\n");
  350. }
  351. machp()->perf.intrts = perfticks();
  352. user = userureg(ureg);
  353. if(user && (machp()->NIX.nixtype == NIXTC)){
  354. up->dbgreg = ureg;
  355. cycles(&up->kentry);
  356. }
  357. clockintr = 0;
  358. //_pmcupdate(machp());
  359. if(ctl = vctl[vno]){
  360. if(ctl->isintr){
  361. machp()->intr++;
  362. if(vno >= VectorPIC && vno != VectorSYSCALL)
  363. machp()->lastintr = ctl->Vkey.irq;
  364. }else
  365. if(up)
  366. up->nqtrap++;
  367. if(ctl->isr){
  368. ctl->isr(vno);
  369. if(islo())print("trap %d: isr %p enabled interrupts\n", vno, ctl->isr);
  370. }
  371. for(v = ctl; v != nil; v = v->next){
  372. if(v->f){
  373. v->f(ureg, v->a);
  374. if(islo())print("trap %d: ctlf %p enabled interrupts\n", vno, v->f);
  375. }
  376. }
  377. if(ctl->eoi){
  378. ctl->eoi(vno);
  379. if(islo())print("trap %d: eoi %p enabled interrupts\n", vno, ctl->eoi);
  380. }
  381. intrtime(vno);
  382. if(ctl->isintr){
  383. if(ctl->Vkey.irq == IrqCLOCK || ctl->Vkey.irq == IrqTIMER)
  384. clockintr = 1;
  385. if (ctl->Vkey.irq == IrqTIMER)
  386. oprof_alarm_handler(ureg);
  387. if(up && !clockintr)
  388. preempted();
  389. }
  390. }
  391. else if(vno < nelem(excname) && user){
  392. spllo();
  393. snprint(buf, sizeof buf, "sys: trap: %s", excname[vno]);
  394. postnote(up, 1, buf, NDebug);
  395. }
  396. else if(vno >= VectorPIC && vno != VectorSYSCALL){
  397. /*
  398. * An unknown interrupt.
  399. * Check for a default IRQ7. This can happen when
  400. * the IRQ input goes away before the acknowledge.
  401. * In this case, a 'default IRQ7' is generated, but
  402. * the corresponding bit in the ISR isn't set.
  403. * In fact, just ignore all such interrupts.
  404. */
  405. /* clear the interrupt */
  406. i8259isr(vno);
  407. iprint("cpu%d: spurious interrupt %d, last %d\n",
  408. machp()->machno, vno, machp()->lastintr);
  409. intrtime(vno);
  410. if(user)
  411. kexit(ureg);
  412. return;
  413. }
  414. else{
  415. if(vno == VectorNMI){
  416. nmienable();
  417. if(machp()->machno != 0){
  418. iprint("cpu%d: PC %#llux\n",
  419. machp()->machno, ureg->ip);
  420. for(;;);
  421. }
  422. }
  423. dumpregs(ureg);
  424. if(!user){
  425. ureg->sp = PTR2UINT(&ureg->sp);
  426. dumpstackwithureg(ureg);
  427. }
  428. if(vno < nelem(excname))
  429. panic("%s", excname[vno]);
  430. panic("unknown trap/intr: %d\n", vno);
  431. }
  432. splhi();
  433. /* delaysched set because we held a lock or because our quantum ended */
  434. if(up && up->delaysched && clockintr){
  435. if(0)
  436. if(user && up->ac == nil && up->nqtrap == 0 && up->nqsyscall == 0){
  437. if(!waserror()){
  438. up->ac = getac(up, -1);
  439. poperror();
  440. runacore();
  441. return;
  442. }
  443. }
  444. sched();
  445. splhi();
  446. }
  447. if(user){
  448. if(up && up->procctl || up->nnote)
  449. notify(ureg);
  450. kexit(ureg);
  451. }
  452. }
  453. /*
  454. * Dump general registers.
  455. */
  456. void
  457. dumpgpr(Ureg* ureg)
  458. {
  459. Proc *up = externup();
  460. if(up != nil)
  461. print("cpu%d: registers for %s %d\n",
  462. machp()->machno, up->text, up->pid);
  463. else
  464. print("cpu%d: registers for kernel\n", machp()->machno);
  465. print("ax\t%#16.16llux\n", ureg->ax);
  466. print("bx\t%#16.16llux\n", ureg->bx);
  467. print("cx\t%#16.16llux\n", ureg->cx);
  468. print("dx\t%#16.16llux\n", ureg->dx);
  469. print("di\t%#16.16llux\n", ureg->di);
  470. print("si\t%#16.16llux\n", ureg->si);
  471. print("bp\t%#16.16llux\n", ureg->bp);
  472. print("r8\t%#16.16llux\n", ureg->r8);
  473. print("r9\t%#16.16llux\n", ureg->r9);
  474. print("r10\t%#16.16llux\n", ureg->r10);
  475. print("r11\t%#16.16llux\n", ureg->r11);
  476. print("r12\t%#16.16llux\n", ureg->r12);
  477. print("r13\t%#16.16llux\n", ureg->r13);
  478. print("r14\t%#16.16llux\n", ureg->r14);
  479. print("r15\t%#16.16llux\n", ureg->r15);
  480. print("type\t%#llux\n", ureg->type);
  481. print("error\t%#llux\n", ureg->error);
  482. print("pc\t%#llux\n", ureg->ip);
  483. print("cs\t%#llux\n", ureg->cs);
  484. print("flags\t%#llux\n", ureg->flags);
  485. print("sp\t%#llux\n", ureg->sp);
  486. print("ss\t%#llux\n", ureg->ss);
  487. print("type\t%#llux\n", ureg->type);
  488. print("FS\t%#llux\n", rdmsr(FSbase));
  489. print("GS\t%#llux\n", rdmsr(GSbase));
  490. print("m\t%#16.16p\nup\t%#16.16p\n", machp(), up);
  491. }
  492. void
  493. dumpregs(Ureg* ureg)
  494. {
  495. dumpgpr(ureg);
  496. /*
  497. * Processor control registers.
  498. * If machine check exception, time stamp counter, page size extensions
  499. * or enhanced virtual 8086 mode extensions are supported, there is a
  500. * CR4. If there is a CR4 and machine check extensions, read the machine
  501. * check address and machine check type registers if RDMSR supported.
  502. */
  503. print("cr0\t%#16.16llux\n", cr0get());
  504. print("cr2\t%#16.16llux\n", machp()->MMU.cr2);
  505. print("cr3\t%#16.16llux\n", cr3get());
  506. die("dumpregs");
  507. // archdumpregs();
  508. }
  509. /*
  510. * Fill in enough of Ureg to get a stack trace, and call a function.
  511. * Used by debugging interface rdb.
  512. */
  513. void
  514. callwithureg(void (*fn)(Ureg*))
  515. {
  516. Ureg ureg;
  517. ureg.ip = getcallerpc(&fn);
  518. ureg.sp = PTR2UINT(&fn);
  519. fn(&ureg);
  520. }
  521. static void
  522. dumpstackwithureg(Ureg* ureg)
  523. {
  524. Proc *up = externup();
  525. uintptr_t l, v, i, estack;
  526. // extern char etext;
  527. int x;
  528. if (0) { //if((s = getconf("*nodumpstack")) != nil && atoi(s) != 0){
  529. iprint("dumpstack disabled\n");
  530. return;
  531. }
  532. iprint("dumpstack\n");
  533. x = 0;
  534. x += iprint("ktrace 9%s %#p %#p\n", strrchr(conffile, '/')+1, ureg->ip, ureg->sp);
  535. i = 0;
  536. if(up != nil
  537. // && (uintptr)&l >= (uintptr)up->kstack
  538. && (uintptr_t)&l <= (uintptr_t)up->kstack+KSTACK)
  539. estack = (uintptr_t)up->kstack+KSTACK;
  540. else if((uintptr_t)&l >= machp()->stack && (uintptr_t)&l <= machp()->stack+MACHSTKSZ)
  541. estack = machp()->stack+MACHSTKSZ;
  542. else{
  543. if(up != nil)
  544. iprint("&up->kstack %#p &l %#p\n", up->kstack, &l);
  545. else
  546. iprint("&m %#p &l %#p\n", machp(), &l);
  547. return;
  548. }
  549. x += iprint("estackx %#p\n", estack);
  550. for(l = (uintptr_t)&l; l < estack; l += sizeof(uintptr_t)){
  551. v = *(uintptr_t*)l;
  552. if((KTZERO < v && v < (uintptr_t)&etext)
  553. || ((uintptr_t)&l < v && v < estack) || estack-l < 256){
  554. x += iprint("%#16.16p=%#16.16p ", l, v);
  555. i++;
  556. }
  557. if(i == 2){
  558. i = 0;
  559. x += iprint("\n");
  560. }
  561. }
  562. if(i)
  563. iprint("\n");
  564. }
  565. void
  566. dumpstack(void)
  567. {
  568. callwithureg(dumpstackwithureg);
  569. }
  570. static void
  571. debugbpt(Ureg* ureg, void* v)
  572. {
  573. Proc *up = externup();
  574. char buf[ERRMAX];
  575. if(up == 0)
  576. panic("kernel bpt");
  577. /* restore pc to instruction that caused the trap */
  578. ureg->ip--;
  579. sprint(buf, "sys: breakpoint");
  580. postnote(up, 1, buf, NDebug);
  581. }
  582. static void
  583. doublefault(Ureg* ureg, void* v)
  584. {
  585. iprint("cr2 %p\n", (void *)cr2get());
  586. panic("double fault");
  587. }
  588. static void
  589. unexpected(Ureg* ureg, void* v)
  590. {
  591. iprint("unexpected trap %llud; ignoring\n", ureg->type);
  592. }
  593. static void
  594. expected(Ureg* ureg, void* v)
  595. {
  596. }
  597. static void
  598. faultamd64(Ureg* ureg, void* v)
  599. {
  600. Proc *up = externup();
  601. uint64_t addr;
  602. int ftype, user, insyscall;
  603. char buf[ERRMAX];
  604. addr = machp()->MMU.cr2;
  605. user = userureg(ureg);
  606. if(!user && mmukmapsync(addr))
  607. return;
  608. /*
  609. * There must be a user context.
  610. * If not, the usual problem is causing a fault during
  611. * initialisation before the system is fully up.
  612. */
  613. if(up == nil){
  614. panic("fault with up == nil; pc %#llux addr %#llux\n",
  615. ureg->ip, addr);
  616. }
  617. ftype = (ureg->error&2) ? FT_WRITE : (ureg->error&16) ? FT_EXEC : FT_READ;
  618. /*
  619. if (read) hi("read fault\n"); else hi("write fault\n");
  620. hi("addr "); put64(addr); hi("\n");
  621. */
  622. insyscall = up->insyscall;
  623. up->insyscall = 1;
  624. if (0)hi("call fault\n");
  625. if(fault(addr, ureg->ip, ftype) < 0){
  626. iprint("could not %s fault %p\n", faulttypes[ftype], addr);
  627. if (! user)
  628. panic("fault went bad in kernel\n");
  629. else
  630. /*
  631. * It is possible to get here with !user if, for example,
  632. * a process was in a system call accessing a shared
  633. * segment but was preempted by another process which shrunk
  634. * or deallocated the shared segment; when the original
  635. * process resumes it may fault while in kernel mode.
  636. * No need to panic this case, post a note to the process
  637. * and unwind the error stack. There must be an error stack
  638. * (up->nerrlab != 0) if this is a system call, if not then
  639. * the game's a bogey.
  640. */
  641. if(!user && (!insyscall || up->nerrlab == 0))
  642. panic("fault: %#llux\n", addr);
  643. sprint(buf, "sys: trap: fault %s addr=%#llux",
  644. faulttypes[ftype], addr);
  645. postnote(up, 1, buf, NDebug);
  646. if(insyscall)
  647. error(buf);
  648. }
  649. up->insyscall = insyscall;
  650. }
  651. /*
  652. * return the userpc the last exception happened at
  653. */
  654. uintptr_t
  655. userpc(Ureg* ureg)
  656. {
  657. Proc *up = externup();
  658. if(ureg == nil)
  659. ureg = up->dbgreg;
  660. return ureg->ip;
  661. }
  662. /* This routine must save the values of registers the user is not permitted
  663. * to write from devproc and then restore the saved values before returning.
  664. * TODO: fix this because the segment registers are wrong for 64-bit mode.
  665. */
  666. void
  667. setregisters(Ureg* ureg, char* pureg, char* uva, int n)
  668. {
  669. uint64_t cs, flags, ss;
  670. ss = ureg->ss;
  671. flags = ureg->flags;
  672. cs = ureg->cs;
  673. memmove(pureg, uva, n);
  674. ureg->cs = cs;
  675. ureg->flags = (ureg->flags & 0x00ff) | (flags & 0xff00);
  676. ureg->ss = ss;
  677. }
  678. /* Give enough context in the ureg to produce a kernel stack for
  679. * a sleeping process
  680. */
  681. void
  682. setkernur(Ureg* ureg, Proc* p)
  683. {
  684. ureg->ip = p->sched.pc;
  685. ureg->sp = p->sched.sp+BY2SE;
  686. }
  687. uintptr_t
  688. dbgpc(Proc *p)
  689. {
  690. Ureg *ureg;
  691. ureg = p->dbgreg;
  692. if(ureg == 0)
  693. return 0;
  694. return ureg->ip;
  695. }