kernel.c 6.8 KB

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