kdbg.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 10
  19. static char kdbg_info_buf[KDBG_INFO_MAX][40];
  20. static unsigned char kdbg_info_used[KDBG_INFO_MAX];
  21. void kdbg_init(void)
  22. {
  23. fb_w = vga_fb_width();
  24. fb_h = vga_fb_height();
  25. fb_pitch = vga_fb_pitch();
  26. fb_bpp = vga_fb_bpp();
  27. fb_lfb = (volatile unsigned char *)(unsigned long)vga_fb_lfb_addr();
  28. fb_ok = (fb_w > 0 && fb_h > 60 && fb_pitch > 0 && fb_lfb &&
  29. (fb_bpp == 16 || fb_bpp == 24 || fb_bpp == 32));
  30. }
  31. static void px(unsigned int x, unsigned int y,
  32. unsigned int r, unsigned int g, unsigned int b)
  33. {
  34. volatile unsigned char *p;
  35. if (!fb_ok || x >= fb_w || y >= fb_h)
  36. return;
  37. p = fb_lfb + (unsigned long)y * fb_pitch + (unsigned long)x * (fb_bpp / 8);
  38. if (fb_bpp == 16) {
  39. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  40. p[0] = (unsigned char)(v & 0xFF);
  41. p[1] = (unsigned char)(v >> 8);
  42. } else if (fb_bpp == 24) {
  43. p[0] = (unsigned char)b;
  44. p[1] = (unsigned char)g;
  45. p[2] = (unsigned char)r;
  46. } else {
  47. p[0] = (unsigned char)b;
  48. p[1] = (unsigned char)g;
  49. p[2] = (unsigned char)r;
  50. p[3] = 0;
  51. }
  52. }
  53. static void rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
  54. unsigned int r, unsigned int g, unsigned int b)
  55. {
  56. unsigned int i, j;
  57. for (j = 0; j < h; j++)
  58. for (i = 0; i < w; i++)
  59. px(x + i, y + j, r, g, b);
  60. }
  61. static void draw_ch(unsigned int x, unsigned int y, char c, unsigned int sc,
  62. unsigned int r, unsigned int g, unsigned int b)
  63. {
  64. const unsigned char *gl = &font_8x8[(unsigned char)c * 8];
  65. unsigned int row, col, i, j;
  66. for (row = 0; row < 8; row++) {
  67. unsigned char bits = gl[row];
  68. for (col = 0; col < 8; col++) {
  69. if (bits & (0x80 >> col)) {
  70. for (i = 0; i < sc; i++)
  71. for (j = 0; j < sc; j++)
  72. px(x + col * sc + i,
  73. y + row * sc + j, r, g, b);
  74. }
  75. }
  76. }
  77. }
  78. static void draw_str(unsigned int x, unsigned int y, unsigned int sc,
  79. const char *s, unsigned int r, unsigned int g, unsigned int b)
  80. {
  81. while (*s) {
  82. draw_ch(x, y, *s, sc, r, g, b);
  83. x += 8 * sc;
  84. s++;
  85. }
  86. }
  87. static unsigned int app_str(char *buf, unsigned int i, const char *s)
  88. {
  89. while (*s)
  90. buf[i++] = *s++;
  91. return i;
  92. }
  93. static unsigned int app_dec(char *buf, unsigned int i, unsigned int v)
  94. {
  95. char tmp[11];
  96. unsigned int n = 0, j;
  97. if (v == 0) {
  98. buf[i++] = '0';
  99. return i;
  100. }
  101. while (v) {
  102. tmp[n++] = (char)('0' + v % 10);
  103. v /= 10;
  104. }
  105. for (j = n; j > 0; j--)
  106. buf[i++] = tmp[j - 1];
  107. return i;
  108. }
  109. static unsigned int app_hex(char *buf, unsigned int i, unsigned int v,
  110. unsigned int digits)
  111. {
  112. static const char hx[] = "0123456789ABCDEF";
  113. unsigned int d;
  114. for (d = 0; d < digits; d++)
  115. buf[i++] = hx[(v >> (4 * (digits - 1 - d))) & 0xF];
  116. return i;
  117. }
  118. static void kdbg_draw_info_row(unsigned int row)
  119. {
  120. unsigned int y;
  121. if (!fb_ok || row >= KDBG_INFO_MAX || !kdbg_info_used[row])
  122. return;
  123. y = fb_h - 31 - row * 10;
  124. rect(0, y, 400, 9, 0, 0, 0);
  125. draw_str(1, y, 1, kdbg_info_buf[row], 255, 220, 80);
  126. }
  127. void kdbg_info(unsigned int row, const char *s)
  128. {
  129. unsigned int i = 0;
  130. if (row >= KDBG_INFO_MAX)
  131. return;
  132. while (s[i] && i < sizeof(kdbg_info_buf[0]) - 1) {
  133. kdbg_info_buf[row][i] = s[i];
  134. i++;
  135. }
  136. kdbg_info_buf[row][i] = '\0';
  137. kdbg_info_used[row] = 1;
  138. kdbg_draw_info_row(row);
  139. }
  140. static void kdbg_redraw_info(void)
  141. {
  142. unsigned int r;
  143. for (r = 0; r < KDBG_INFO_MAX; r++)
  144. kdbg_draw_info_row(r);
  145. }
  146. void kdbg_status(void)
  147. {
  148. char buf[96];
  149. unsigned int i;
  150. if (!fb_ok)
  151. return;
  152. i = 0;
  153. i = app_str(buf, i, "IRQ ");
  154. i = app_dec(buf, i, kdbg_irq_count);
  155. i = app_str(buf, i, " SC ");
  156. i = app_hex(buf, i, kdbg_last_sc, 2);
  157. i = app_str(buf, i, " BY ");
  158. i = app_dec(buf, i, kdbg_irq_bytes);
  159. i = app_str(buf, i, " OT ");
  160. i = app_dec(buf, i, kdbg_irq_other);
  161. i = app_str(buf, i, " MK ");
  162. i = app_dec(buf, i, kdbg_irq_masked);
  163. buf[i] = '\0';
  164. rect(0, fb_h - 21, 400, 9, 0, 0, 0);
  165. draw_str(1, fb_h - 21, 1, buf, 120, 255, 120);
  166. i = 0;
  167. i = app_str(buf, i, "WM ");
  168. i = app_dec(buf, i, kdbg_wm_count);
  169. i = app_str(buf, i, " K ");
  170. i = app_hex(buf, i, kdbg_wm_last, 4);
  171. i = app_str(buf, i, " TRM ");
  172. i = app_dec(buf, i, kdbg_term_count);
  173. i = app_str(buf, i, " K ");
  174. i = app_hex(buf, i, kdbg_term_last, 4);
  175. buf[i] = '\0';
  176. rect(0, fb_h - 11, 400, 9, 0, 0, 0);
  177. draw_str(1, fb_h - 11, 1, buf, 120, 180, 255);
  178. kdbg_redraw_info();
  179. }
  180. /* Called from the PIT IRQ (~1 kHz). */
  181. void kdbg_pit_tick(void)
  182. {
  183. static unsigned int n;
  184. n++;
  185. if (!fb_ok)
  186. return;
  187. if ((n % 500) == 0) {
  188. int on = (int)((n / 500) & 1);
  189. /* 1 Hz heartbeat square, bottom-right corner. */
  190. rect(fb_w - 12, fb_h - 12, 10, 10,
  191. on ? 0 : 60, on ? 255 : 60, on ? 0 : 60);
  192. }
  193. if ((n % 1000) == 0)
  194. kdbg_status();
  195. }
  196. void kdbg_panic(const char *name, unsigned int eip, unsigned int cr2)
  197. {
  198. char buf[64];
  199. unsigned int i = 0;
  200. if (!fb_ok)
  201. return;
  202. rect(0, 0, fb_w, 52, 140, 0, 0);
  203. draw_str(4, 4, 2, "*** EXCEPTION ***", 255, 255, 255);
  204. draw_str(4, 26, 1, name, 255, 255, 0);
  205. i = app_str(buf, i, "EIP ");
  206. i = app_hex(buf, i, eip, 8);
  207. i = app_str(buf, i, " CR2 ");
  208. i = app_hex(buf, i, cr2, 8);
  209. buf[i] = '\0';
  210. draw_str(4, 38, 1, buf, 255, 200, 200);
  211. kdbg_status();
  212. }