kernel.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Kernel: entry point at 0x7E00, timer (PIT 1 kHz), process init, terminal. */
  2. #include "idle.h"
  3. #include "io.h"
  4. #include "kdbg.h"
  5. #include "kmalloc.h"
  6. #include "mm.h"
  7. #include "mouse.h"
  8. #include "netdev.h"
  9. #include "dhcp.h"
  10. #include "netlog.h"
  11. #include "paging.h"
  12. #include "process.h"
  13. #include "ramfs.h"
  14. #include "timer.h"
  15. #include "tss.h"
  16. #include "usbhid.h"
  17. #include "wm.h"
  18. #include "serial.h"
  19. #include "version.h"
  20. #include "vga.h"
  21. #include "vfs.h"
  22. static inline void outb(unsigned short port, unsigned char v)
  23. {
  24. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  25. }
  26. volatile unsigned int ticks;
  27. struct idt_entry {
  28. unsigned short offset_lo;
  29. unsigned short selector;
  30. unsigned char zero;
  31. unsigned char type;
  32. unsigned short offset_hi;
  33. } __attribute__((packed));
  34. struct idtr_desc {
  35. unsigned short limit;
  36. unsigned int base;
  37. } __attribute__((packed));
  38. static struct idt_entry idt[256];
  39. static struct idtr_desc idtr;
  40. extern void pit_irq_stub(void);
  41. extern void kbd_irq_stub(void);
  42. extern void fdc_irq_stub(void);
  43. extern void default_irq_stub(void);
  44. extern void syscall_stub(void);
  45. extern unsigned int exc_stub_table[];
  46. unsigned int get_ticks_ms(void)
  47. {
  48. return ticks;
  49. }
  50. void exc_handler(unsigned int eip, unsigned int vector, unsigned int cs);
  51. void exc_handler(unsigned int eip, unsigned int vector, unsigned int cs)
  52. {
  53. unsigned int cr2 = 0;
  54. static const char * const names[] = {
  55. "#DE Divide Error", "#DB Debug", "NMI", "#BP Breakpoint",
  56. "#OF Overflow", "#BR Bound Range", "#UD Invalid Opcode",
  57. "#NM No FPU", "#DF Double Fault", "Coprocessor Overrun",
  58. "#TS Invalid TSS", "#NP Seg Not Present", "#SS Stack Fault",
  59. "#GP General Protection", "#PF Page Fault", "Reserved",
  60. "#MF FPU Error", "#AC Alignment Check", "#MC Machine Check",
  61. "#XM SIMD Error",
  62. };
  63. __asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
  64. /* A fault from ring 3 is a bug in a user command (or its ELF): kill that
  65. * process and keep the kernel running. Kernel faults still halt. */
  66. if ((cs & 3) == 3) {
  67. serial_puts(SERIAL_COM1, "\n*** user fault vec ");
  68. serial_puthex(SERIAL_COM1, vector);
  69. serial_puts(SERIAL_COM1, " eip=");
  70. serial_puthex(SERIAL_COM1, eip);
  71. serial_puts(SERIAL_COM1, " -- process killed\n");
  72. io_putstr("\n*** fault: ");
  73. io_putstr(vector < 20 ? names[vector] : "unknown vector");
  74. io_putstr(" -- command killed\n");
  75. process_exit(); /* marks us dead and waits to be switched out */
  76. }
  77. /* Draw the fault info directly into the framebuffer FIRST: with no
  78. * serial console attached, a fault was previously invisible (the text
  79. * went to the current process's screen buffer, which the WM never got
  80. * to render because the CPU halts here). */
  81. kdbg_panic(vector < 20 ? names[vector] : "unknown vector", eip, cr2);
  82. /* Print the fault to serial, using only minimal/no-stack code, so it
  83. * survives even if the screen buffer or kernel stack is the problem. */
  84. serial_puts(SERIAL_COM1, "\n*** EXC vec ");
  85. serial_puthex(SERIAL_COM1, vector);
  86. serial_puts(SERIAL_COM1, " eip=");
  87. serial_puthex(SERIAL_COM1, eip);
  88. serial_puts(SERIAL_COM1, " cr2=");
  89. serial_puthex(SERIAL_COM1, cr2);
  90. serial_puts(SERIAL_COM1, " ");
  91. if (vector < 20)
  92. serial_puts(SERIAL_COM1, names[vector]);
  93. serial_puts(SERIAL_COM1, " ***\n");
  94. /* Then attempt the screen output (may fault if the screen is broken). */
  95. io_putstr("\n*** EXCEPTION: ");
  96. if (vector < 20)
  97. io_putstr(names[vector]);
  98. else {
  99. io_putstr("vector ");
  100. if (vector >= 10)
  101. io_putchar((char)('0' + vector / 10));
  102. io_putchar((char)('0' + vector % 10));
  103. }
  104. io_putstr(" ***\nSystem halted.\n");
  105. for (;;)
  106. __asm__ volatile ("cli; hlt");
  107. }
  108. static void set_idt_gate(int vec, unsigned int handler)
  109. {
  110. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  111. idt[vec].selector = 0x08;
  112. idt[vec].zero = 0;
  113. idt[vec].type = 0x8E;
  114. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  115. }
  116. /* Trap gate (DPL=3) so ring-3 can `int` into it; a trap gate does NOT clear
  117. * IF, so a blocking syscall (e.g. sleep) can still be preempted by the timer. */
  118. static void set_idt_gate_user(int vec, unsigned int handler)
  119. {
  120. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  121. idt[vec].selector = 0x08;
  122. idt[vec].zero = 0;
  123. idt[vec].type = 0xEF;
  124. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  125. }
  126. static void timer_init(void)
  127. {
  128. int i;
  129. /* Remap PICs: master at 0x20, slave at 0x28. */
  130. outb(0x20, 0x11);
  131. outb(0xA0, 0x11);
  132. outb(0x21, 0x20);
  133. outb(0xA1, 0x28);
  134. outb(0x21, 0x04);
  135. outb(0xA1, 0x02);
  136. outb(0x21, 0x01);
  137. outb(0xA1, 0x01);
  138. outb(0x21, 0xFC);
  139. outb(0xA1, 0xFF);
  140. /* Default handler for all 256 vectors: absorbs any unexpected
  141. interrupt (including spurious IRQ 7) with an EOI + iret. */
  142. for (i = 0; i < 256; i++)
  143. set_idt_gate(i, (unsigned int)default_irq_stub);
  144. /* Exception handlers 0-31: ring-3 faults kill the process, kernel
  145. faults print the panic banner and halt (see exc_handler). */
  146. for (i = 0; i < 32; i++)
  147. set_idt_gate(i, exc_stub_table[i]);
  148. /* Hardware IRQ handlers. */
  149. set_idt_gate(0x20, (unsigned int)pit_irq_stub);
  150. set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
  151. set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
  152. /* Syscall gate (ring-3 can int 0x80). */
  153. set_idt_gate_user(0x80, (unsigned int)syscall_stub);
  154. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  155. idtr.base = (unsigned int)idt;
  156. __asm__ volatile ("lidt %0" : : "m"(idtr));
  157. outb(0x43, 0x36);
  158. outb(0x40, (unsigned char)(1193 & 0xFF));
  159. outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
  160. __asm__ volatile ("sti");
  161. }
  162. extern unsigned char __bss_start[], __bss_end[];
  163. void kernel_main(void);
  164. void kernel_main(void)
  165. {
  166. unsigned char *p = __bss_start;
  167. while (p < __bss_end)
  168. *p++ = 0;
  169. serial_init(SERIAL_COM1);
  170. serial_ring_init();
  171. /* Network console mirror from the very first line on; the ring holds the
  172. * early boot output until the WM loop starts flushing it. */
  173. netlog_init();
  174. serial_puts(SERIAL_COM1, "\n" OS_NAME " v" OS_VERSION " booted\n");
  175. mm_init();
  176. mm_dump();
  177. kmalloc_init();
  178. paging_init();
  179. io_init();
  180. idle_init();
  181. /* process_init + gdt_tss_init must run BEFORE timer_init: once the PIT is
  182. * started (sti in timer_init) the context-switch stub runs on every tick
  183. * and reads procs[].cr3 / procs[].kstack_top / the TSS, so those have to be
  184. * valid first (otherwise the first switch loads a garbage CR3 and faults). */
  185. process_init();
  186. gdt_tss_init();
  187. timer_init();
  188. mouse_init();
  189. /* USB HID backend (mouse + keyboard): controller bring-up + first
  190. * scan; devices plugged in later are picked up by the hotplug
  191. * watcher in the WM loop. */
  192. usbhid_init();
  193. vga_shadow_init();
  194. vga_fb_flush();
  195. kdbg_init();
  196. kdbg_status();
  197. vfs_mount_all();
  198. /* The command pack: read-only, mounted at /ramfs (see ramfs.c). */
  199. {
  200. extern unsigned char _binary_cmds_pack_start[];
  201. extern unsigned char _binary_cmds_pack_end[];
  202. unsigned int pack_len = (unsigned int)(_binary_cmds_pack_end -
  203. _binary_cmds_pack_start);
  204. if (ramfs_init(_binary_cmds_pack_start, pack_len) != 0)
  205. serial_puts(SERIAL_COM1, "ramfs: bad pack\n");
  206. else {
  207. serial_puts(SERIAL_COM1, "ramfs: pack ok, ");
  208. serial_putdec(SERIAL_COM1, ramfs_count());
  209. serial_puts(SERIAL_COM1, " files\n");
  210. vfs_mount_ramfs();
  211. }
  212. }
  213. /* Bring the NIC up before the WM loop starts, so an idle board can pick
  214. * up a network kexec (`make net-dev`) without any typing. */
  215. netdev_active();
  216. /* With a NIC present, try DHCP; on failure the static default stays. */
  217. if (netdev_active())
  218. dhcp_run(3000);
  219. wm_run();
  220. }