kernel.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Kernel: entry point at 0x7E00, timer (PIT 1 kHz), process init, terminal. */
  2. #include "idle.h"
  3. #include "io.h"
  4. #include "panic.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(struct exc_state *st);
  51. void exc_handler(struct exc_state *st)
  52. {
  53. unsigned int vector = st->vector;
  54. unsigned int cr2 = 0;
  55. static const char * const names[] = {
  56. "#DE Divide Error", "#DB Debug", "NMI", "#BP Breakpoint",
  57. "#OF Overflow", "#BR Bound Range", "#UD Invalid Opcode",
  58. "#NM No FPU", "#DF Double Fault", "Coprocessor Overrun",
  59. "#TS Invalid TSS", "#NP Seg Not Present", "#SS Stack Fault",
  60. "#GP General Protection", "#PF Page Fault", "Reserved",
  61. "#MF FPU Error", "#AC Alignment Check", "#MC Machine Check",
  62. "#XM SIMD Error",
  63. };
  64. __asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
  65. /* A fault from ring 3 is a bug in a user command (or its ELF): kill that
  66. * process and keep the kernel running. Kernel faults still halt. */
  67. if ((st->cs & 3) == 3) {
  68. serial_puts(SERIAL_COM1, "\n*** user fault vec ");
  69. serial_puthex(SERIAL_COM1, vector);
  70. serial_puts(SERIAL_COM1, " eip=");
  71. serial_puthex(SERIAL_COM1, st->eip);
  72. serial_puts(SERIAL_COM1, " cr2=");
  73. serial_puthex(SERIAL_COM1, cr2);
  74. serial_puts(SERIAL_COM1, " -- process killed\n");
  75. io_putstr("\n*** fault: ");
  76. io_putstr(vector < 20 ? names[vector] : "unknown vector");
  77. io_putstr(" -- command killed\n");
  78. process_exit(); /* marks us dead and waits to be switched out */
  79. }
  80. /* Fatal kernel fault: the banner goes straight into the LFB (no serial
  81. * console needed) and the full state is mirrored to COM1. */
  82. panic_report(st, vector < 20 ? names[vector] : "unknown vector");
  83. for (;;)
  84. __asm__ volatile ("cli; hlt");
  85. }
  86. static void set_idt_gate(int vec, unsigned int handler)
  87. {
  88. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  89. idt[vec].selector = 0x08;
  90. idt[vec].zero = 0;
  91. idt[vec].type = 0x8E;
  92. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  93. }
  94. /* Trap gate (DPL=3) so ring-3 can `int` into it; a trap gate does NOT clear
  95. * IF, so a blocking syscall (e.g. sleep) can still be preempted by the timer. */
  96. static void set_idt_gate_user(int vec, unsigned int handler)
  97. {
  98. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  99. idt[vec].selector = 0x08;
  100. idt[vec].zero = 0;
  101. idt[vec].type = 0xEF;
  102. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  103. }
  104. static void timer_init(void)
  105. {
  106. int i;
  107. /* Remap PICs: master at 0x20, slave at 0x28. */
  108. outb(0x20, 0x11);
  109. outb(0xA0, 0x11);
  110. outb(0x21, 0x20);
  111. outb(0xA1, 0x28);
  112. outb(0x21, 0x04);
  113. outb(0xA1, 0x02);
  114. outb(0x21, 0x01);
  115. outb(0xA1, 0x01);
  116. outb(0x21, 0xFC);
  117. outb(0xA1, 0xFF);
  118. /* Default handler for all 256 vectors: absorbs any unexpected
  119. interrupt (including spurious IRQ 7) with an EOI + iret. */
  120. for (i = 0; i < 256; i++)
  121. set_idt_gate(i, (unsigned int)default_irq_stub);
  122. /* Exception handlers 0-31: ring-3 faults kill the process, kernel
  123. faults print the panic banner and halt (see exc_handler). */
  124. for (i = 0; i < 32; i++)
  125. set_idt_gate(i, exc_stub_table[i]);
  126. /* Hardware IRQ handlers. */
  127. set_idt_gate(0x20, (unsigned int)pit_irq_stub);
  128. set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
  129. set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
  130. /* Syscall gate (ring-3 can int 0x80). */
  131. set_idt_gate_user(0x80, (unsigned int)syscall_stub);
  132. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  133. idtr.base = (unsigned int)idt;
  134. __asm__ volatile ("lidt %0" : : "m"(idtr));
  135. outb(0x43, 0x36);
  136. outb(0x40, (unsigned char)(1193 & 0xFF));
  137. outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
  138. __asm__ volatile ("sti");
  139. }
  140. extern unsigned char __bss_start[], __bss_end[];
  141. void kernel_main(void);
  142. void kernel_main(void)
  143. {
  144. unsigned char *p = __bss_start;
  145. while (p < __bss_end)
  146. *p++ = 0;
  147. serial_init(SERIAL_COM1);
  148. serial_ring_init();
  149. /* Network console mirror from the very first line on; the ring holds the
  150. * early boot output until the WM loop starts flushing it. */
  151. netlog_init();
  152. serial_puts(SERIAL_COM1, "\n" OS_NAME " v" OS_VERSION " booted\n");
  153. mm_init();
  154. mm_dump();
  155. kmalloc_init();
  156. paging_init();
  157. io_init();
  158. idle_init();
  159. /* process_init + gdt_tss_init must run BEFORE timer_init: once the PIT is
  160. * started (sti in timer_init) the context-switch stub runs on every tick
  161. * and reads procs[].cr3 / procs[].kstack_top / the TSS, so those have to be
  162. * valid first (otherwise the first switch loads a garbage CR3 and faults). */
  163. process_init();
  164. gdt_tss_init();
  165. timer_init();
  166. mouse_init();
  167. /* USB HID backend (mouse + keyboard): controller bring-up + first
  168. * scan; devices plugged in later are picked up by the hotplug
  169. * watcher in the WM loop. */
  170. usbhid_init();
  171. vga_shadow_init();
  172. vga_fb_flush();
  173. vfs_mount_all();
  174. /* The command pack: read-only, mounted at /ramfs (see ramfs.c). */
  175. {
  176. extern unsigned char _binary_cmds_pack_start[];
  177. extern unsigned char _binary_cmds_pack_end[];
  178. unsigned int pack_len = (unsigned int)(_binary_cmds_pack_end -
  179. _binary_cmds_pack_start);
  180. if (ramfs_init(_binary_cmds_pack_start, pack_len) != 0)
  181. serial_puts(SERIAL_COM1, "ramfs: bad pack\n");
  182. else {
  183. serial_puts(SERIAL_COM1, "ramfs: pack ok, ");
  184. serial_putdec(SERIAL_COM1, ramfs_count());
  185. serial_puts(SERIAL_COM1, " files\n");
  186. vfs_mount_ramfs();
  187. }
  188. }
  189. /* Bring the NIC up before the WM loop starts, so an idle board can pick
  190. * up a network kexec (`make net-dev`) without any typing. */
  191. netdev_active();
  192. /* With a NIC present, try DHCP; on failure the static default stays. */
  193. if (netdev_active())
  194. dhcp_run(3000);
  195. wm_run();
  196. }