panic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Fatal exception screen: see panic.h. */
  2. #include "panic.h"
  3. #include "font_8x8.h"
  4. #include "process.h"
  5. #include "serial.h"
  6. #include "timer.h"
  7. #include "vga.h"
  8. #define LINE_H 10
  9. #define PANIC_LINES 12
  10. #define RAM_LIMIT 0x08000000u /* MM_MAX_PHYS: identity-mapped RAM */
  11. #define KERNEL_BASE 0x7E00u /* kernel load address (kernel.ld) */
  12. static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;
  13. static volatile unsigned char *fb_lfb;
  14. static int fb_ok;
  15. static unsigned int panic_line;
  16. static const char hx[] = "0123456789ABCDEF";
  17. extern unsigned char __bss_end[];
  18. /* 8 GPRs from pusha + vector/err/eip/cs/eflags/esp/ss. */
  19. _Static_assert(sizeof(struct exc_state) == 15 * sizeof(unsigned int),
  20. "struct exc_state must match the timer.S stub");
  21. /* --- direct LFB drawing (works with no WM/shadow) ----------------------- */
  22. static void cache_fb(void)
  23. {
  24. fb_w = vga_fb_width();
  25. fb_h = vga_fb_height();
  26. fb_pitch = vga_fb_pitch();
  27. fb_bpp = vga_fb_bpp();
  28. fb_lfb = (volatile unsigned char *)(unsigned long)vga_fb_lfb_addr();
  29. fb_ok = (fb_w > 0 && fb_h > 0 && fb_pitch > 0 && fb_lfb &&
  30. (fb_bpp == 16 || fb_bpp == 24 || fb_bpp == 32));
  31. }
  32. static void px(unsigned int x, unsigned int y,
  33. unsigned int r, unsigned int g, unsigned int b)
  34. {
  35. volatile unsigned char *p;
  36. if (!fb_ok || x >= fb_w || y >= fb_h)
  37. return;
  38. p = fb_lfb + (unsigned long)y * fb_pitch + (unsigned long)x * (fb_bpp / 8);
  39. if (fb_bpp == 16) {
  40. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  41. p[0] = (unsigned char)(v & 0xFF);
  42. p[1] = (unsigned char)(v >> 8);
  43. } else {
  44. p[0] = (unsigned char)b;
  45. p[1] = (unsigned char)g;
  46. p[2] = (unsigned char)r;
  47. if (fb_bpp == 32)
  48. p[3] = 0;
  49. }
  50. }
  51. static void rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  52. unsigned int r, unsigned int g, unsigned int b)
  53. {
  54. unsigned int i, j;
  55. for (j = 0; j < h; j++)
  56. for (i = 0; i < w; i++)
  57. px(x + i, y + j, r, g, b);
  58. }
  59. static void draw_ch(unsigned int x, unsigned int y, char c, unsigned int sc,
  60. unsigned int r, unsigned int g, unsigned int b)
  61. {
  62. const unsigned char *gl = &font_8x8[(unsigned char)c * 8];
  63. unsigned int row, col, i, j;
  64. for (row = 0; row < 8; row++) {
  65. unsigned char bits = gl[row];
  66. for (col = 0; col < 8; col++) {
  67. if (bits & (0x80 >> col)) {
  68. for (i = 0; i < sc; i++)
  69. for (j = 0; j < sc; j++)
  70. px(x + col * sc + i,
  71. y + row * sc + j, r, g, b);
  72. }
  73. }
  74. }
  75. }
  76. static void draw_str(unsigned int x, unsigned int y, unsigned int sc,
  77. const char *s, unsigned int r, unsigned int g, unsigned int b)
  78. {
  79. while (*s) {
  80. draw_ch(x, y, *s, sc, r, g, b);
  81. x += 8 * sc;
  82. s++;
  83. }
  84. }
  85. /* --- small freestanding formatters ------------------------------------- */
  86. static unsigned int app_str(char *buf, unsigned int i, const char *s)
  87. {
  88. while (*s)
  89. buf[i++] = *s++;
  90. return i;
  91. }
  92. static unsigned int app_dec(char *buf, unsigned int i, unsigned int v)
  93. {
  94. char tmp[11];
  95. unsigned int n = 0, j;
  96. if (v == 0) {
  97. buf[i++] = '0';
  98. return i;
  99. }
  100. while (v) {
  101. tmp[n++] = (char)('0' + v % 10);
  102. v /= 10;
  103. }
  104. for (j = n; j > 0; j--)
  105. buf[i++] = tmp[j - 1];
  106. return i;
  107. }
  108. static unsigned int app_hex(char *buf, unsigned int i, unsigned int v,
  109. unsigned int digits)
  110. {
  111. unsigned int d;
  112. for (d = 0; d < digits; d++)
  113. buf[i++] = hx[(v >> (4 * (digits - 1 - d))) & 0xF];
  114. return i;
  115. }
  116. static unsigned int app_ptr(char *buf, unsigned int i, unsigned int v)
  117. {
  118. i = app_str(buf, i, "0x");
  119. return app_hex(buf, i, v, 8);
  120. }
  121. /* One report line: the screen (until the block is full) and COM1. */
  122. static void emit(const char *s)
  123. {
  124. if (fb_ok && panic_line < PANIC_LINES) {
  125. unsigned int y = 26 + panic_line * LINE_H;
  126. draw_str(4, y, 1, s, 255, 235, 190);
  127. }
  128. panic_line++;
  129. serial_puts(SERIAL_COM1, s);
  130. serial_putc(SERIAL_COM1, '\n');
  131. }
  132. /* --- the report --------------------------------------------------------- */
  133. void panic_report(const struct exc_state *st, const char *name)
  134. {
  135. char buf[128];
  136. unsigned int i, n, k;
  137. unsigned int cr0, cr2, cr3, cr4;
  138. unsigned int esp, page_top;
  139. unsigned int code_n = 0;
  140. unsigned char code[16];
  141. unsigned int cand_val[12], cand_off[12];
  142. unsigned int cand_n = 0;
  143. int stack_ok, user = (st->cs & 3) == 3;
  144. __asm__ volatile ("mov %%cr0, %0" : "=r"(cr0));
  145. __asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
  146. __asm__ volatile ("mov %%cr3, %0" : "=r"(cr3));
  147. __asm__ volatile ("mov %%cr4, %0" : "=r"(cr4));
  148. /* Fault-time stack pointer: the CPU also pushes SS:ESP on a privilege
  149. * change; for a ring-0 fault it is just above the EFLAGS slot. */
  150. esp = user ? st->esp : (unsigned int)&st->eflags + 4;
  151. stack_ok = (esp >= 0x1000u && esp < RAM_LIMIT);
  152. page_top = stack_ok ? (esp | 0xFFFu) + 1 : esp;
  153. /* The faulting instruction bytes (only from mapped RAM, and never
  154. * across a page boundary, so a bad EIP cannot fault the handler). */
  155. if (st->eip < RAM_LIMIT) {
  156. unsigned int eip_top = (st->eip | 0xFFFu) + 1;
  157. while (code_n < sizeof(code) && st->eip + code_n < eip_top) {
  158. code[code_n] = *(volatile unsigned char *)(st->eip + code_n);
  159. code_n++;
  160. }
  161. }
  162. /* Return-address candidates: scan the faulting stack page for dwords
  163. * that point into the kernel image. There is no EBP chain to walk
  164. * (-O2 omits frame pointers), so these are best-effort hints. */
  165. if (stack_ok) {
  166. for (n = esp; n + 4 <= page_top && cand_n < 12; n += 4) {
  167. unsigned int v = *(volatile unsigned int *)n;
  168. if (v >= KERNEL_BASE && v < (unsigned int)__bss_end) {
  169. cand_val[cand_n] = v;
  170. cand_off[cand_n] = n - esp;
  171. cand_n++;
  172. }
  173. }
  174. }
  175. cache_fb();
  176. if (fb_ok) {
  177. rect(0, 0, fb_w, 22, 160, 0, 0);
  178. draw_str(4, 4, 2, "*** KERNEL EXCEPTION ***", 255, 255, 255);
  179. rect(0, 22, fb_w, 6 + PANIC_LINES * LINE_H, 24, 0, 0);
  180. }
  181. panic_line = 0;
  182. serial_puts(SERIAL_COM1, "\n*** KERNEL EXCEPTION ***\n");
  183. /* Vector, error code, and the #PF error-code bits. */
  184. i = 0;
  185. i = app_str(buf, i, name);
  186. i = app_str(buf, i, " vec ");
  187. i = app_dec(buf, i, st->vector);
  188. i = app_str(buf, i, " err 0x");
  189. i = app_hex(buf, i, st->err, 8);
  190. if (st->vector == 14) {
  191. i = app_str(buf, i, " P=");
  192. buf[i++] = (char)((st->err & 0x01) ? '1' : '0');
  193. i = app_str(buf, i, " W=");
  194. buf[i++] = (char)((st->err & 0x02) ? '1' : '0');
  195. i = app_str(buf, i, " U=");
  196. buf[i++] = (char)((st->err & 0x04) ? '1' : '0');
  197. i = app_str(buf, i, " RSVD=");
  198. buf[i++] = (char)((st->err & 0x08) ? '1' : '0');
  199. i = app_str(buf, i, " ID=");
  200. buf[i++] = (char)((st->err & 0x10) ? '1' : '0');
  201. }
  202. buf[i] = '\0';
  203. emit(buf);
  204. /* Faulting instruction pointer and flags. */
  205. i = 0;
  206. i = app_str(buf, i, "EIP ");
  207. i = app_ptr(buf, i, st->eip);
  208. i = app_str(buf, i, " CS ");
  209. i = app_str(buf, i, "0x");
  210. i = app_hex(buf, i, st->cs, 4);
  211. i = app_str(buf, i, user ? " ring 3" : " ring 0");
  212. i = app_str(buf, i, " EFLAGS 0x");
  213. i = app_hex(buf, i, st->eflags, 8);
  214. if (st->eflags & 0x200u)
  215. i = app_str(buf, i, " IF");
  216. if (st->eflags & 0x20000u)
  217. i = app_str(buf, i, " VM");
  218. buf[i] = '\0';
  219. emit(buf);
  220. /* General registers (two lines of four). */
  221. i = 0;
  222. i = app_str(buf, i, "EAX "); i = app_ptr(buf, i, st->eax);
  223. i = app_str(buf, i, " EBX "); i = app_ptr(buf, i, st->ebx);
  224. i = app_str(buf, i, " ECX "); i = app_ptr(buf, i, st->ecx);
  225. i = app_str(buf, i, " EDX "); i = app_ptr(buf, i, st->edx);
  226. buf[i] = '\0';
  227. emit(buf);
  228. i = 0;
  229. i = app_str(buf, i, "ESI "); i = app_ptr(buf, i, st->esi);
  230. i = app_str(buf, i, " EDI "); i = app_ptr(buf, i, st->edi);
  231. i = app_str(buf, i, " EBP "); i = app_ptr(buf, i, st->ebp);
  232. i = app_str(buf, i, " ESP "); i = app_ptr(buf, i, esp);
  233. buf[i] = '\0';
  234. emit(buf);
  235. /* Control registers. */
  236. i = 0;
  237. i = app_str(buf, i, "CR0 "); i = app_ptr(buf, i, cr0);
  238. i = app_str(buf, i, " CR2 "); i = app_ptr(buf, i, cr2);
  239. i = app_str(buf, i, " CR3 "); i = app_ptr(buf, i, cr3);
  240. i = app_str(buf, i, " CR4 "); i = app_ptr(buf, i, cr4);
  241. buf[i] = '\0';
  242. emit(buf);
  243. /* Current process and uptime. */
  244. i = 0;
  245. i = app_str(buf, i, "pid ");
  246. i = app_dec(buf, i, current_pid);
  247. if (procs) {
  248. i = app_str(buf, i, " (");
  249. for (n = 0; n < PROC_NAME_LEN && procs[current_pid].name[n]; n++)
  250. buf[i++] = procs[current_pid].name[n];
  251. buf[i++] = ')';
  252. }
  253. i = app_str(buf, i, " ticks ");
  254. i = app_dec(buf, i, get_ticks_ms());
  255. buf[i] = '\0';
  256. emit(buf);
  257. /* Kernel stack bounds: an ESP below the base means stack overflow. */
  258. if (procs) {
  259. i = 0;
  260. i = app_str(buf, i, "kstack base ");
  261. i = app_ptr(buf, i, procs[current_pid].stack_base);
  262. i = app_str(buf, i, " top ");
  263. i = app_ptr(buf, i, procs[current_pid].kstack_top);
  264. buf[i] = '\0';
  265. emit(buf);
  266. }
  267. /* Bytes at EIP: the faulting instruction (raw, for a disassembler). */
  268. i = 0;
  269. i = app_str(buf, i, "code");
  270. for (n = 0; n < code_n; n++) {
  271. buf[i++] = ' ';
  272. buf[i++] = hx[code[n] >> 4];
  273. buf[i++] = hx[code[n] & 0xF];
  274. }
  275. if (code_n == 0)
  276. i = app_str(buf, i, " (unmapped)");
  277. buf[i] = '\0';
  278. emit(buf);
  279. /* A few dwords at the faulting stack pointer. */
  280. if (stack_ok) {
  281. for (k = 0; k < 2; k++) {
  282. unsigned int p = esp + k * 16;
  283. i = 0;
  284. i = app_str(buf, i, "stk ");
  285. i = app_ptr(buf, i, p);
  286. i = app_str(buf, i, ":");
  287. for (n = 0; n < 4; n++) {
  288. unsigned int a = p + n * 4;
  289. if (a + 4 > page_top)
  290. break;
  291. i = app_str(buf, i, " ");
  292. i = app_hex(buf, i, *(volatile unsigned int *)a, 8);
  293. }
  294. buf[i] = '\0';
  295. emit(buf);
  296. }
  297. } else {
  298. i = 0;
  299. i = app_str(buf, i, "stk ");
  300. i = app_ptr(buf, i, esp);
  301. i = app_str(buf, i, " (not mapped)");
  302. buf[i] = '\0';
  303. emit(buf);
  304. }
  305. /* Return-address candidates found on the stack. */
  306. i = 0;
  307. i = app_str(buf, i, "ret");
  308. for (n = 0; n < cand_n && n < 5; n++) {
  309. buf[i++] = ' ';
  310. i = app_ptr(buf, i, cand_val[n]);
  311. }
  312. if (cand_n == 0)
  313. i = app_str(buf, i, " none");
  314. buf[i] = '\0';
  315. emit(buf);
  316. /* Serial gets the full stack and every candidate, each with its offset
  317. * from the faulting ESP, so a crash log can be read without the screen. */
  318. if (stack_ok) {
  319. for (n = 0; n < 32; n++) {
  320. unsigned int p = esp + n * 4;
  321. if (p + 4 > page_top)
  322. break;
  323. i = 0;
  324. i = app_str(buf, i, "stk +");
  325. i = app_hex(buf, i, n * 4, 3);
  326. i = app_str(buf, i, " @");
  327. i = app_hex(buf, i, p, 8);
  328. i = app_str(buf, i, " = ");
  329. i = app_hex(buf, i, *(volatile unsigned int *)p, 8);
  330. buf[i] = '\0';
  331. serial_puts(SERIAL_COM1, buf);
  332. serial_putc(SERIAL_COM1, '\n');
  333. }
  334. }
  335. for (n = 0; n < cand_n; n++) {
  336. i = 0;
  337. i = app_str(buf, i, "ret +");
  338. i = app_hex(buf, i, cand_off[n], 3);
  339. i = app_str(buf, i, " ");
  340. i = app_ptr(buf, i, cand_val[n]);
  341. buf[i] = '\0';
  342. serial_puts(SERIAL_COM1, buf);
  343. serial_putc(SERIAL_COM1, '\n');
  344. }
  345. }