kernel.c 5.7 KB

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