kernel.c 5.9 KB

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