kernel.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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);
  50. void exc_handler(unsigned int eip, unsigned int vector)
  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. /* Draw the fault info directly into the framebuffer FIRST: with no
  64. * serial console attached, a fault was previously invisible (the text
  65. * went to the current process's screen buffer, which the WM never got
  66. * to render because the CPU halts here). */
  67. kdbg_panic(vector < 20 ? names[vector] : "unknown vector", eip, cr2);
  68. /* Print the fault to serial, using only minimal/no-stack code, so it
  69. * survives even if the screen buffer or kernel stack is the problem. */
  70. serial_puts(SERIAL_COM1, "\n*** EXC vec ");
  71. serial_puthex(SERIAL_COM1, vector);
  72. serial_puts(SERIAL_COM1, " eip=");
  73. serial_puthex(SERIAL_COM1, eip);
  74. serial_puts(SERIAL_COM1, " cr2=");
  75. serial_puthex(SERIAL_COM1, cr2);
  76. serial_puts(SERIAL_COM1, " ");
  77. if (vector < 20)
  78. serial_puts(SERIAL_COM1, names[vector]);
  79. serial_puts(SERIAL_COM1, " ***\n");
  80. /* Then attempt the screen output (may fault if the screen is broken). */
  81. io_putstr("\n*** EXCEPTION: ");
  82. if (vector < 20)
  83. io_putstr(names[vector]);
  84. else {
  85. io_putstr("vector ");
  86. if (vector >= 10)
  87. io_putchar((char)('0' + vector / 10));
  88. io_putchar((char)('0' + vector % 10));
  89. }
  90. io_putstr(" ***\nSystem halted.\n");
  91. for (;;)
  92. __asm__ volatile ("cli; hlt");
  93. }
  94. static void set_idt_gate(int vec, unsigned int handler)
  95. {
  96. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  97. idt[vec].selector = 0x08;
  98. idt[vec].zero = 0;
  99. idt[vec].type = 0x8E;
  100. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  101. }
  102. /* Trap gate (DPL=3) so ring-3 can `int` into it; a trap gate does NOT clear
  103. * IF, so a blocking syscall (e.g. sleep) can still be preempted by the timer. */
  104. static void set_idt_gate_user(int vec, unsigned int handler)
  105. {
  106. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  107. idt[vec].selector = 0x08;
  108. idt[vec].zero = 0;
  109. idt[vec].type = 0xEF;
  110. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  111. }
  112. static void timer_init(void)
  113. {
  114. int i;
  115. /* Remap PICs: master at 0x20, slave at 0x28. */
  116. outb(0x20, 0x11);
  117. outb(0xA0, 0x11);
  118. outb(0x21, 0x20);
  119. outb(0xA1, 0x28);
  120. outb(0x21, 0x04);
  121. outb(0xA1, 0x02);
  122. outb(0x21, 0x01);
  123. outb(0xA1, 0x01);
  124. outb(0x21, 0xFC);
  125. outb(0xA1, 0xFF);
  126. /* Default handler for all 256 vectors: absorbs any unexpected
  127. interrupt (including spurious IRQ 7) with an EOI + iret. */
  128. for (i = 0; i < 256; i++)
  129. set_idt_gate(i, (unsigned int)default_irq_stub);
  130. /* Exception handlers 0-31: print the fault name and halt. */
  131. for (i = 0; i < 32; i++)
  132. set_idt_gate(i, exc_stub_table[i]);
  133. /* Hardware IRQ handlers. */
  134. set_idt_gate(0x20, (unsigned int)pit_irq_stub);
  135. set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
  136. set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
  137. /* Syscall gate (ring-3 can int 0x80). */
  138. set_idt_gate_user(0x80, (unsigned int)syscall_stub);
  139. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  140. idtr.base = (unsigned int)idt;
  141. __asm__ volatile ("lidt %0" : : "m"(idtr));
  142. outb(0x43, 0x36);
  143. outb(0x40, (unsigned char)(1193 & 0xFF));
  144. outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
  145. __asm__ volatile ("sti");
  146. }
  147. extern unsigned char __bss_start[], __bss_end[];
  148. void kernel_main(void);
  149. void kernel_main(void)
  150. {
  151. unsigned char *p = __bss_start;
  152. while (p < __bss_end)
  153. *p++ = 0;
  154. serial_init(SERIAL_COM1);
  155. serial_ring_init();
  156. /* Network console mirror from the very first line on; the ring holds the
  157. * early boot output until the WM loop starts flushing it. */
  158. netlog_init();
  159. serial_puts(SERIAL_COM1, "\n" OS_NAME " v" OS_VERSION " booted\n");
  160. mm_init();
  161. mm_dump();
  162. kmalloc_init();
  163. paging_init();
  164. io_init();
  165. idle_init();
  166. /* process_init + gdt_tss_init must run BEFORE timer_init: once the PIT is
  167. * started (sti in timer_init) the context-switch stub runs on every tick
  168. * and reads procs[].cr3 / procs[].kstack_top / the TSS, so those have to be
  169. * valid first (otherwise the first switch loads a garbage CR3 and faults). */
  170. process_init();
  171. gdt_tss_init();
  172. timer_init();
  173. mouse_init();
  174. /* USB HID backend (mouse + keyboard): controller bring-up + first
  175. * scan; devices plugged in later are picked up by the hotplug
  176. * watcher in the WM loop. */
  177. usbhid_init();
  178. vga_shadow_init();
  179. vga_fb_flush();
  180. kdbg_init();
  181. kdbg_status();
  182. vfs_mount_all();
  183. /* Bring the NIC up before the WM loop starts, so an idle board can pick
  184. * up a network kexec (`make net-dev`) without any typing. */
  185. netdev_active();
  186. /* With a NIC present, try DHCP; on failure the static default stays. */
  187. if (netdev_active())
  188. dhcp_run(3000);
  189. wm_run();
  190. }