kdbg.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* On-screen kernel debug output: see kdbg.h. */
  2. #include "kdbg.h"
  3. #include "font_8x8.h"
  4. #include "timer.h"
  5. #include "vga.h"
  6. volatile unsigned int kdbg_irq_count;
  7. volatile unsigned int kdbg_irq_bytes;
  8. volatile unsigned int kdbg_irq_other;
  9. volatile unsigned int kdbg_last_sc;
  10. volatile unsigned int kdbg_irq_masked;
  11. volatile unsigned int kdbg_wm_count;
  12. volatile unsigned int kdbg_wm_last;
  13. volatile unsigned int kdbg_term_count;
  14. volatile unsigned int kdbg_term_last;
  15. static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;
  16. static volatile unsigned char *fb_lfb;
  17. static int fb_ok;
  18. #define KDBG_INFO_MAX 14
  19. static char kdbg_info_buf[KDBG_INFO_MAX][40];
  20. static unsigned char kdbg_info_used[KDBG_INFO_MAX];
  21. static unsigned int kdbg_info_next;
  22. void kdbg_init(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 > 60 && 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 if (fb_bpp == 24) {
  44. p[0] = (unsigned char)b;
  45. p[1] = (unsigned char)g;
  46. p[2] = (unsigned char)r;
  47. } else {
  48. p[0] = (unsigned char)b;
  49. p[1] = (unsigned char)g;
  50. p[2] = (unsigned char)r;
  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. static unsigned int app_str(char *buf, unsigned int i, const char *s)
  89. {
  90. while (*s)
  91. buf[i++] = *s++;
  92. return i;
  93. }
  94. static unsigned int app_dec(char *buf, unsigned int i, unsigned int v)
  95. {
  96. char tmp[11];
  97. unsigned int n = 0, j;
  98. if (v == 0) {
  99. buf[i++] = '0';
  100. return i;
  101. }
  102. while (v) {
  103. tmp[n++] = (char)('0' + v % 10);
  104. v /= 10;
  105. }
  106. for (j = n; j > 0; j--)
  107. buf[i++] = tmp[j - 1];
  108. return i;
  109. }
  110. static unsigned int app_hex(char *buf, unsigned int i, unsigned int v,
  111. unsigned int digits)
  112. {
  113. static const char hx[] = "0123456789ABCDEF";
  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 void kdbg_draw_info_row(unsigned int row)
  120. {
  121. unsigned int y;
  122. if (!fb_ok || row >= KDBG_INFO_MAX || !kdbg_info_used[row])
  123. return;
  124. y = fb_h - 31 - row * 10;
  125. rect(0, y, 400, 9, 0, 0, 0);
  126. draw_str(1, y, 1, kdbg_info_buf[row], 255, 220, 80);
  127. }
  128. void kdbg_info(unsigned int row, const char *s)
  129. {
  130. unsigned int i = 0;
  131. if (row >= KDBG_INFO_MAX)
  132. return;
  133. while (s[i] && i < sizeof(kdbg_info_buf[0]) - 1) {
  134. kdbg_info_buf[row][i] = s[i];
  135. i++;
  136. }
  137. kdbg_info_buf[row][i] = '\0';
  138. kdbg_info_used[row] = 1;
  139. kdbg_draw_info_row(row);
  140. }
  141. static void kdbg_redraw_info(void)
  142. {
  143. unsigned int r;
  144. for (r = 0; r < KDBG_INFO_MAX; r++)
  145. kdbg_draw_info_row(r);
  146. }
  147. int kdbg_info_alloc(void)
  148. {
  149. if (kdbg_info_next >= KDBG_INFO_MAX)
  150. return -1;
  151. return (int)kdbg_info_next++;
  152. }
  153. void kdbg_status(void)
  154. {
  155. char buf[96];
  156. unsigned int i;
  157. if (!fb_ok)
  158. return;
  159. i = 0;
  160. i = app_str(buf, i, "IRQ ");
  161. i = app_dec(buf, i, kdbg_irq_count);
  162. i = app_str(buf, i, " SC ");
  163. i = app_hex(buf, i, kdbg_last_sc, 2);
  164. i = app_str(buf, i, " BY ");
  165. i = app_dec(buf, i, kdbg_irq_bytes);
  166. i = app_str(buf, i, " OT ");
  167. i = app_dec(buf, i, kdbg_irq_other);
  168. i = app_str(buf, i, " MK ");
  169. i = app_dec(buf, i, kdbg_irq_masked);
  170. buf[i] = '\0';
  171. rect(0, fb_h - 21, 400, 9, 0, 0, 0);
  172. draw_str(1, fb_h - 21, 1, buf, 120, 255, 120);
  173. i = 0;
  174. i = app_str(buf, i, "WM ");
  175. i = app_dec(buf, i, kdbg_wm_count);
  176. i = app_str(buf, i, " K ");
  177. i = app_hex(buf, i, kdbg_wm_last, 4);
  178. i = app_str(buf, i, " TRM ");
  179. i = app_dec(buf, i, kdbg_term_count);
  180. i = app_str(buf, i, " K ");
  181. i = app_hex(buf, i, kdbg_term_last, 4);
  182. buf[i] = '\0';
  183. rect(0, fb_h - 11, 400, 9, 0, 0, 0);
  184. draw_str(1, fb_h - 11, 1, buf, 120, 180, 255);
  185. kdbg_redraw_info();
  186. }
  187. /* Called from the PIT IRQ (~1 kHz). */
  188. void kdbg_pit_tick(void)
  189. {
  190. static unsigned int n;
  191. n++;
  192. if (!fb_ok)
  193. return;
  194. if ((n % 500) == 0) {
  195. int on = (int)((n / 500) & 1);
  196. /* 1 Hz heartbeat square, bottom-right corner. */
  197. rect(fb_w - 12, fb_h - 12, 10, 10,
  198. on ? 0 : 60, on ? 255 : 60, on ? 0 : 60);
  199. }
  200. if ((n % 1000) == 0)
  201. kdbg_status();
  202. }
  203. void kdbg_panic(const char *name, unsigned int eip, unsigned int cr2)
  204. {
  205. char buf[64];
  206. unsigned int i = 0;
  207. if (!fb_ok)
  208. return;
  209. rect(0, 0, fb_w, 52, 140, 0, 0);
  210. draw_str(4, 4, 2, "*** EXCEPTION ***", 255, 255, 255);
  211. draw_str(4, 26, 1, name, 255, 255, 0);
  212. i = app_str(buf, i, "EIP ");
  213. i = app_hex(buf, i, eip, 8);
  214. i = app_str(buf, i, " CR2 ");
  215. i = app_hex(buf, i, cr2, 8);
  216. buf[i] = '\0';
  217. draw_str(4, 38, 1, buf, 255, 200, 200);
  218. kdbg_status();
  219. }