trap.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 "lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "io.h"
  15. #include "ureg.h"
  16. #define DATASEGM(p) { 0xFFFF, SEGG|SEGB|(0xF<<16)|SEGP|SEGPL(p)|SEGDATA|SEGW }
  17. #define EXECSEGM(p) { 0xFFFF, SEGG|SEGD|(0xF<<16)|SEGP|SEGPL(p)|SEGEXEC|SEGR }
  18. Segdesc gdt[NGDT] =
  19. {
  20. [NULLSEG] { 0, 0}, /* null descriptor */
  21. [KDSEG] DATASEGM(0), /* kernel data/stack */
  22. [KESEG] EXECSEGM(0), /* kernel code */
  23. };
  24. void intr0(void), intr1(void), intr2(void), intr3(void);
  25. void intr4(void), intr5(void), intr6(void), intr7(void);
  26. void intr8(void), intr9(void), intr10(void), intr11(void);
  27. void intr12(void), intr13(void), intr14(void), intr15(void);
  28. void intr16(void);
  29. void intr24(void), intr25(void), intr26(void), intr27(void);
  30. void intr28(void), intr29(void), intr30(void), intr31(void);
  31. void intr32(void), intr33(void), intr34(void), intr35(void);
  32. void intr36(void), intr37(void), intr38(void), intr39(void);
  33. void intr64(void);
  34. void intrbad(void);
  35. /*
  36. * 8259 interrupt controllers
  37. */
  38. enum
  39. {
  40. Int0ctl= 0x20, /* control port (ICW1, OCW2, OCW3) */
  41. Int0aux= 0x21, /* everything else (ICW2, ICW3, ICW4, OCW1) */
  42. Int1ctl= 0xA0, /* control port */
  43. Int1aux= 0xA1, /* everything else (ICW2, ICW3, ICW4, OCW1) */
  44. Icw1= 0x10, /* select bit in ctl register */
  45. Ocw2= 0x00,
  46. Ocw3= 0x08,
  47. EOI= 0x20, /* non-specific end of interrupt */
  48. Elcr1= 0x4D0, /* Edge/Level Triggered Register */
  49. Elcr2= 0x4D1,
  50. };
  51. int int0mask = 0xff; /* interrupts enabled for first 8259 */
  52. int int1mask = 0xff; /* interrupts enabled for second 8259 */
  53. int i8259elcr; /* mask of level-triggered interrupts */
  54. /*
  55. * trap/interrupt gates
  56. */
  57. Segdesc ilt[256];
  58. enum
  59. {
  60. Maxhandler= 32, /* max number of interrupt handlers */
  61. };
  62. typedef struct Handler Handler;
  63. struct Handler
  64. {
  65. void (*r)(Ureg*, void*);
  66. void *arg;
  67. Handler *next;
  68. };
  69. struct
  70. {
  71. Handler *ivec[256];
  72. Handler h[Maxhandler];
  73. int nextfree;
  74. } halloc;
  75. void
  76. sethvec(int v, void (*r)(void), int type, int pri)
  77. {
  78. ilt[v].d0 = ((uint32_t)r)&0xFFFF|(KESEL<<16);
  79. ilt[v].d1 = ((uint32_t)r)&0xFFFF0000|SEGP|SEGPL(pri)|type;
  80. }
  81. void
  82. setvec(int v, void (*r)(Ureg*, void*), void *arg)
  83. {
  84. Handler *h;
  85. if(halloc.nextfree >= Maxhandler)
  86. panic("out of interrupt handlers");
  87. h = &halloc.h[halloc.nextfree++];
  88. h->next = halloc.ivec[v];
  89. h->r = r;
  90. h->arg = arg;
  91. halloc.ivec[v] = h;
  92. /*
  93. * enable corresponding interrupt in 8259
  94. */
  95. if((v&~0x7) == VectorPIC){
  96. int0mask &= ~(1<<(v&7));
  97. outb(Int0aux, int0mask);
  98. } else if((v&~0x7) == VectorPIC+8){
  99. int1mask &= ~(1<<(v&7));
  100. outb(Int1aux, int1mask);
  101. }
  102. }
  103. void
  104. trapdisable(void)
  105. {
  106. outb(Int0aux, 0xFF);
  107. outb(Int1aux, 0xFF);
  108. outb(Int0ctl, EOI);
  109. outb(Int1ctl, EOI);
  110. }
  111. void
  112. trapenable(void)
  113. {
  114. outb(Int0aux, int0mask);
  115. outb(Int1aux, int1mask);
  116. }
  117. /*
  118. * set up the interrupt/trap gates
  119. */
  120. void
  121. trapinit(void)
  122. {
  123. int i, x;
  124. uint32_t a;
  125. uint16_t ptr[3];
  126. /*
  127. * set all interrupts to panics
  128. */
  129. for(i = 0; i < 256; i++)
  130. sethvec(i, intrbad, SEGTG, 0);
  131. /*
  132. * 80386 processor (and coprocessor) traps
  133. */
  134. sethvec(0, intr0, SEGTG, 0);
  135. sethvec(1, intr1, SEGTG, 0);
  136. sethvec(2, intr2, SEGTG, 0);
  137. sethvec(3, intr3, SEGTG, 0);
  138. sethvec(4, intr4, SEGTG, 0);
  139. sethvec(5, intr5, SEGTG, 0);
  140. sethvec(6, intr6, SEGTG, 0);
  141. sethvec(7, intr7, SEGTG, 0);
  142. sethvec(8, intr8, SEGTG, 0);
  143. sethvec(9, intr9, SEGTG, 0);
  144. sethvec(10, intr10, SEGTG, 0);
  145. sethvec(11, intr11, SEGTG, 0);
  146. sethvec(12, intr12, SEGTG, 0);
  147. sethvec(13, intr13, SEGTG, 0);
  148. sethvec(14, intr14, SEGTG, 0);
  149. sethvec(15, intr15, SEGTG, 0);
  150. sethvec(16, intr16, SEGTG, 0);
  151. /*
  152. * device interrupts
  153. */
  154. sethvec(24, intr24, SEGIG, 0);
  155. sethvec(25, intr25, SEGIG, 0);
  156. sethvec(26, intr26, SEGIG, 0);
  157. sethvec(27, intr27, SEGIG, 0);
  158. sethvec(28, intr28, SEGIG, 0);
  159. sethvec(29, intr29, SEGIG, 0);
  160. sethvec(30, intr30, SEGIG, 0);
  161. sethvec(31, intr31, SEGIG, 0);
  162. sethvec(32, intr32, SEGIG, 0);
  163. sethvec(33, intr33, SEGIG, 0);
  164. sethvec(34, intr34, SEGIG, 0);
  165. sethvec(35, intr35, SEGIG, 0);
  166. sethvec(36, intr36, SEGIG, 0);
  167. sethvec(37, intr37, SEGIG, 0);
  168. sethvec(38, intr38, SEGIG, 0);
  169. sethvec(39, intr39, SEGIG, 0);
  170. /*
  171. * tell the hardware where the table is (and how long)
  172. */
  173. memmove(m->gdt, gdt, sizeof gdt);
  174. ptr[0] = sizeof(gdt)-1;
  175. a = (uint32_t)m->gdt;
  176. ptr[1] = a & 0xFFFF;
  177. ptr[2] = (a>>16) & 0xFFFF;
  178. lgdt(ptr);
  179. ptr[0] = sizeof(Segdesc)*256-1;
  180. a = (uint32_t)ilt;
  181. ptr[1] = a & 0xFFFF;
  182. ptr[2] = (a>>16) & 0xFFFF;
  183. lidt(ptr);
  184. /*
  185. * Set up the first 8259 interrupt processor.
  186. * Make 8259 interrupts start at CPU vector VectorPIC.
  187. * Set the 8259 as master with edge triggered
  188. * input with fully nested interrupts.
  189. */
  190. outb(Int0ctl, Icw1|0x01); /* ICW1 - edge triggered, master,
  191. ICW4 will be sent */
  192. outb(Int0aux, VectorPIC); /* ICW2 - interrupt vector offset */
  193. outb(Int0aux, 0x04); /* ICW3 - have slave on level 2 */
  194. outb(Int0aux, 0x01); /* ICW4 - 8086 mode, not buffered */
  195. /*
  196. * Set up the second 8259 interrupt processor.
  197. * Make 8259 interrupts start at CPU vector VectorPIC+8.
  198. * Set the 8259 as master with edge triggered
  199. * input with fully nested interrupts.
  200. */
  201. outb(Int1ctl, Icw1|0x01); /* ICW1 - edge triggered, master,
  202. ICW4 will be sent */
  203. outb(Int1aux, VectorPIC+8); /* ICW2 - interrupt vector offset */
  204. outb(Int1aux, 0x02); /* ICW3 - I am a slave on level 2 */
  205. outb(Int1aux, 0x01); /* ICW4 - 8086 mode, not buffered */
  206. outb(Int1aux, int1mask);
  207. /*
  208. * pass #2 8259 interrupts to #1
  209. */
  210. int0mask &= ~0x04;
  211. outb(Int0aux, int0mask);
  212. /*
  213. * Set Ocw3 to return the ISR when ctl read.
  214. */
  215. outb(Int0ctl, Ocw3|0x03);
  216. outb(Int1ctl, Ocw3|0x03);
  217. /*
  218. * Check for Edge/Level register.
  219. * This check may not work for all chipsets.
  220. * First try a non-intrusive test - the bits for
  221. * IRQs 13, 8, 2, 1 and 0 must be edge (0). If
  222. * that's OK try a R/W test.
  223. */
  224. x = (inb(Elcr2)<<8)|inb(Elcr1);
  225. if(!(x & 0x2107)){
  226. outb(Elcr1, 0);
  227. if(inb(Elcr1) == 0){
  228. outb(Elcr1, 0x20);
  229. if(inb(Elcr1) == 0x20)
  230. i8259elcr = x;
  231. outb(Elcr1, x & 0xFF);
  232. print("ELCR: %4.4uX\n", i8259elcr);
  233. }
  234. }
  235. }
  236. /*
  237. * dump registers
  238. */
  239. static void
  240. dumpregs(Ureg *ur)
  241. {
  242. print("FLAGS=%lux TRAP=%lux ECODE=%lux PC=%lux\n",
  243. ur->flags, ur->trap, ur->ecode, ur->pc);
  244. print(" AX %8.8lux BX %8.8lux CX %8.8lux DX %8.8lux\n",
  245. ur->ax, ur->bx, ur->cx, ur->dx);
  246. print(" SI %8.8lux DI %8.8lux BP %8.8lux\n",
  247. ur->si, ur->di, ur->bp);
  248. print(" CS %4.4lux DS %4.4lux ES %4.4lux FS %4.4lux GS %4.4lux\n",
  249. ur->cs & 0xFF, ur->ds & 0xFFFF, ur->es & 0xFFFF, ur->fs & 0xFFFF, ur->gs & 0xFFFF);
  250. print(" CR0 %8.8lux CR2 %8.8lux CR3 %8.8lux\n",
  251. getcr0(), getcr2(), getcr3());
  252. }
  253. /*
  254. * All traps
  255. */
  256. void
  257. trap(Ureg *ur)
  258. {
  259. int v;
  260. int c;
  261. Handler *h;
  262. uint16_t isr;
  263. v = ur->trap;
  264. /*
  265. * tell the 8259 that we're done with the
  266. * highest level interrupt (interrupts are still
  267. * off at this point)
  268. */
  269. c = v&~0x7;
  270. isr = 0;
  271. if(c==VectorPIC || c==VectorPIC+8){
  272. isr = inb(Int0ctl);
  273. outb(Int0ctl, EOI);
  274. if(c == VectorPIC+8){
  275. isr |= inb(Int1ctl)<<8;
  276. outb(Int1ctl, EOI);
  277. }
  278. }
  279. if(v>=256 || (h = halloc.ivec[v]) == 0){
  280. if(v >= VectorPIC && v < VectorPIC+16){
  281. v -= VectorPIC;
  282. /*
  283. * Check for a default IRQ7. This can happen when
  284. * the IRQ input goes away before the acknowledge.
  285. * In this case, a 'default IRQ7' is generated, but
  286. * the corresponding bit in the ISR isn't set.
  287. * In fact, just ignore all such interrupts.
  288. */
  289. if(isr & (1<<v))
  290. print("unknown interrupt %d pc=0x%lux\n", v, ur->pc);
  291. return;
  292. }
  293. switch(v){
  294. case 0x02: /* NMI */
  295. print("NMI: nmisc=0x%2.2ux, nmiertc=0x%2.2ux, nmiesc=0x%2.2ux\n",
  296. inb(0x61), inb(0x70), inb(0x461));
  297. return;
  298. case 0x06:
  299. {
  300. uint8_t *p = (uint8_t*)(ur->pc-20);
  301. int i;
  302. for(i = 0; i < 40; i++){
  303. print(" %2.2ux", *p);
  304. p++;
  305. }
  306. }
  307. default:
  308. dumpregs(ur);
  309. panic("exception/interrupt %d", v);
  310. return;
  311. }
  312. }
  313. /*
  314. * call the trap routines
  315. */
  316. do {
  317. (*h->r)(ur, h->arg);
  318. h = h->next;
  319. } while(h);
  320. }