kernel.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* Kernel: entered from bootloader in 32-bit protected mode at 0x7E00.
  2. * Must be linked so this function is at 0x7E00 (see kernel.ld).
  3. * Simple terminal: keyboard echo. */
  4. #define VGA_BASE ((volatile unsigned short *)0xB8000)
  5. #define VGA_ATTR 0x0A /* green on black */
  6. #define VGA_COLS 80
  7. #define VGA_ROWS 25
  8. #define VGA_SIZE (VGA_COLS * VGA_ROWS)
  9. #define DEBUG_ROW 24 /* last row: debug line (input vs echo) */
  10. #define CONTENT_ROWS (DEBUG_ROW) /* rows 0..23 for terminal */
  11. #define CONTENT_SIZE (CONTENT_ROWS * VGA_COLS)
  12. /* Port I/O */
  13. static inline unsigned char inb(unsigned short port)
  14. {
  15. unsigned char v;
  16. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  17. return v;
  18. }
  19. static inline void outb(unsigned short port, unsigned char v)
  20. {
  21. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  22. }
  23. /* Disable the VGA hardware cursor (BIOS blinking underline at top-left). */
  24. static void disable_hw_cursor(void)
  25. {
  26. outb(0x3D4, 0x0A); /* Cursor Start register */
  27. outb(0x3D5, 0x20); /* Bit 5 = cursor disable */
  28. }
  29. /* PS/2 keyboard: data port 0x60, status 0x64 (bit 0 = output buffer full) */
  30. #define KB_DATA 0x60
  31. #define KB_STATUS 0x64
  32. /* Timer: PIT at 1 kHz (1 tick = 1 ms). IRQ0 -> INT 0x20 after PIC remap. */
  33. #define PIT_HZ 1000
  34. #define CURSOR_MS 500
  35. #define BUSY_MS 33
  36. volatile unsigned int ticks; /* milliseconds since timer_init */
  37. /* IDT entry (8 bytes) and table for vector 0x20 (IRQ0). */
  38. struct idt_entry {
  39. unsigned short offset_lo;
  40. unsigned short selector;
  41. unsigned char zero;
  42. unsigned char type;
  43. unsigned short offset_hi;
  44. } __attribute__((packed));
  45. struct idtr_desc {
  46. unsigned short limit;
  47. unsigned int base;
  48. } __attribute__((packed));
  49. static struct idt_entry idt[256];
  50. static struct idtr_desc idtr;
  51. extern void pit_irq_stub(void);
  52. void pit_irq(void); /* called from timer.S stub */
  53. void pit_irq(void)
  54. {
  55. ticks++;
  56. outb(0x20, 0x20); /* EOI to master PIC */
  57. }
  58. static void timer_init(void)
  59. {
  60. unsigned int stub = (unsigned int)pit_irq_stub;
  61. /* Remap PIC: IRQ0..7 -> INT 0x20..0x27 */
  62. outb(0x20, 0x11);
  63. outb(0xA0, 0x11);
  64. outb(0x21, 0x20);
  65. outb(0xA1, 0x28);
  66. outb(0x21, 0x04);
  67. outb(0xA1, 0x02);
  68. outb(0x21, 0x01);
  69. outb(0xA1, 0x01);
  70. outb(0x21, 0xFE); /* mask all except IRQ0 on master */
  71. outb(0xA1, 0xFF); /* mask all on slave */
  72. /* IDT entry for 0x20 (IRQ0) */
  73. idt[0x20].offset_lo = (unsigned short)(stub & 0xFFFF);
  74. idt[0x20].selector = 0x08;
  75. idt[0x20].zero = 0;
  76. idt[0x20].type = 0x8E; /* 32-bit interrupt gate */
  77. idt[0x20].offset_hi = (unsigned short)((stub >> 16) & 0xFFFF);
  78. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  79. idtr.base = (unsigned int)idt;
  80. __asm__ volatile ("lidt %0" : : "m"(idtr));
  81. /* PIT channel 0: 1 kHz (divisor 1193) */
  82. outb(0x43, 0x36);
  83. outb(0x40, (unsigned char)(1193 & 0xFF));
  84. outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
  85. __asm__ volatile ("sti");
  86. }
  87. /* Scancode set 1 (XT) -> ASCII. 0 = ignore. Set 1 break = make | 0x80, we ignore those. */
  88. static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
  89. [0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
  90. [0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
  91. [0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
  92. [0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
  93. [0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
  94. [0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
  95. [0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
  96. [0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
  97. [0x28] = '\'', [0x29] = '`',
  98. [0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
  99. [0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
  100. [0x35] = '/', [0x39] = ' ',
  101. };
  102. /* Set 1: left shift make 0x2A break 0xAA, right shift make 0x36 break 0xB6 */
  103. static int shift_pressed;
  104. static unsigned int cursor; /* linear position 0 .. CONTENT_SIZE-1 */
  105. static unsigned char last_scancode; /* set by getchar for debug line */
  106. #define BUSY_POS (DEBUG_ROW * VGA_COLS + 79) /* bottom-right: never overwritten by echo */
  107. static unsigned int busy_idx; /* 0..3 for busy_chars */
  108. static unsigned int last_busy_tick;
  109. /* Explicit ASCII so no encoding/escape issues: - \ | / */
  110. static const unsigned char busy_chars[4] = { 0x2D, 0x5C, 0x7C, 0x2F };
  111. /* Blinking cursor: cell content when cursor is hidden; 0xDB = full block (CP437) */
  112. static unsigned short cursor_cell;
  113. static int cursor_visible;
  114. static unsigned int last_cursor_tick;
  115. #define CURSOR_BLOCK ((unsigned short)((VGA_ATTR << 8) | 0xDB))
  116. /* Restore cell at current cursor, then update cursor position and cursor_cell. */
  117. static void hide_cursor_at(unsigned int pos)
  118. {
  119. VGA_BASE[pos] = cursor_cell;
  120. }
  121. static void putchar(char c)
  122. {
  123. if (c == '\n') {
  124. hide_cursor_at(cursor);
  125. cursor = (cursor / VGA_COLS + 1) * VGA_COLS;
  126. if (cursor >= CONTENT_SIZE) {
  127. unsigned int i;
  128. for (i = 0; i < (CONTENT_ROWS - 1) * VGA_COLS; i++)
  129. VGA_BASE[i] = VGA_BASE[i + VGA_COLS];
  130. for (i = (CONTENT_ROWS - 1) * VGA_COLS; i < CONTENT_SIZE; i++)
  131. VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
  132. cursor = (CONTENT_ROWS - 1) * VGA_COLS;
  133. }
  134. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  135. return;
  136. }
  137. if (c == '\b') {
  138. if (cursor > 0) {
  139. hide_cursor_at(cursor);
  140. cursor--;
  141. cursor_cell = VGA_BASE[cursor];
  142. }
  143. return;
  144. }
  145. if (cursor < CONTENT_SIZE) {
  146. VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
  147. cursor++;
  148. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  149. }
  150. }
  151. /* Draw blinking cursor at current cursor position (every CURSOR_MS ms). */
  152. static void update_cursor_blink(void)
  153. {
  154. unsigned int t = ticks;
  155. if (t - last_cursor_tick >= CURSOR_MS) {
  156. last_cursor_tick = t;
  157. cursor_visible = !cursor_visible;
  158. }
  159. if (cursor_visible)
  160. VGA_BASE[cursor] = CURSOR_BLOCK;
  161. else
  162. VGA_BASE[cursor] = cursor_cell;
  163. }
  164. /* Write one hex byte at VGA position; returns next position. */
  165. static unsigned int puthex(unsigned int pos, unsigned char byte)
  166. {
  167. static const char hex[] = "0123456789ABCDEF";
  168. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | hex[byte >> 4]);
  169. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | hex[byte & 0x0F]);
  170. return pos;
  171. }
  172. /* Update bottom line with last scancode and decoded char. Leaves col 79 for busy. */
  173. static void debug_line(unsigned char sc, char c)
  174. {
  175. unsigned int i, pos = DEBUG_ROW * VGA_COLS;
  176. for (i = 0; i < 79; i++)
  177. VGA_BASE[pos + i] = (unsigned short)(VGA_ATTR << 8);
  178. /* "SC:XX CH:YY 'c'" */
  179. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'S');
  180. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'C');
  181. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ':');
  182. pos = puthex(pos, sc);
  183. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ' ');
  184. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'C');
  185. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'H');
  186. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ':');
  187. pos = puthex(pos, (unsigned char)c);
  188. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ' ');
  189. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | '\'');
  190. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | (c >= 0x20 && c < 0x7F ? (unsigned char)c : '.'));
  191. VGA_BASE[pos] = (unsigned short)((VGA_ATTR << 8) | '\'');
  192. }
  193. static void putstr(const char *s)
  194. {
  195. while (*s) putchar(*s++);
  196. }
  197. /* Apply shift to get US layout shifted character. Returns c unchanged if no shift variant. */
  198. static char apply_shift(char c)
  199. {
  200. if (!shift_pressed) return c;
  201. if (c >= 'a' && c <= 'z') return (char)(c - 32);
  202. switch ((unsigned char)c) {
  203. case '1': return '!'; case '2': return '@'; case '3': return '#';
  204. case '4': return '$'; case '5': return '%'; case '6': return '^';
  205. case '7': return '&'; case '8': return '*'; case '9': return '(';
  206. case '0': return ')'; case '-': return '_'; case '=': return '+';
  207. case '[': return '{'; case ']': return '}'; case ';': return ':';
  208. case '\'': return '"'; case '`': return '~'; case '\\': return '|';
  209. case ',': return '<'; case '.': return '>'; case '/': return '?';
  210. default: return c;
  211. }
  212. }
  213. /* Draw busy indicator at bottom-right (every BUSY_MS ms). */
  214. static void update_busy(void)
  215. {
  216. unsigned int t = ticks;
  217. if (t - last_busy_tick >= BUSY_MS) {
  218. last_busy_tick = t;
  219. busy_idx = (busy_idx + 1) % 4;
  220. }
  221. VGA_BASE[BUSY_POS] = (unsigned short)((VGA_ATTR << 8) | busy_chars[busy_idx]);
  222. }
  223. /* String in .text so it loads with the kernel */
  224. static const char msg[] __attribute__((section(".text"))) = "Nebs OS |o|";
  225. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  226. /* Entry point: must be first in .text so bootloader jump to 0x7E00 hits it */
  227. void kernel_main(void) __attribute__((section(".text.entry")));
  228. void kernel_main(void)
  229. {
  230. unsigned i;
  231. disable_hw_cursor(); /* remove BIOS blinking underline at top-left */
  232. /* Clear screen */
  233. for (i = 0; i < VGA_SIZE; i++)
  234. VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
  235. cursor = 0;
  236. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  237. cursor_visible = 1;
  238. last_cursor_tick = 0;
  239. last_busy_tick = 0;
  240. busy_idx = 0;
  241. putstr(msg);
  242. putstr(prompt);
  243. timer_init(); /* PIT 1 kHz, IDT, sti; ticks counts ms */
  244. last_cursor_tick = ticks;
  245. last_busy_tick = ticks;
  246. /* Drain keyboard buffer so we don't echo leftover/garbage bytes */
  247. while (inb(KB_STATUS) & 1)
  248. (void)inb(KB_DATA);
  249. /* Terminal echo loop: busy indicator, blinking cursor at cursor, then poll keyboard */
  250. for (;;) {
  251. update_busy();
  252. update_cursor_blink();
  253. if (inb(KB_STATUS) & 1) {
  254. unsigned char sc = inb(KB_DATA);
  255. if (sc & 0x80) {
  256. /* Set 1 break: release shift */
  257. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  258. continue;
  259. }
  260. if (sc == 0x2A || sc == 0x36) {
  261. shift_pressed = 1;
  262. continue;
  263. }
  264. {
  265. char c = (char)scancode_to_ascii[sc];
  266. if (c) {
  267. c = apply_shift(c);
  268. last_scancode = sc;
  269. debug_line(last_scancode, c);
  270. putchar(c);
  271. if (c == '\n')
  272. putstr(prompt);
  273. }
  274. }
  275. }
  276. else
  277. __asm__ volatile ("hlt"); /* wait for next interrupt (PIT or other) */
  278. }
  279. }