panic.c 10 KB

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