panic.c 10 KB

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