kernel.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "sys_gfx.h"
  15. #include "timer.h"
  16. #include "tss.h"
  17. #include "usbhid.h"
  18. #include "wm.h"
  19. #include "serial.h"
  20. #include "version.h"
  21. #include "vga.h"
  22. #include "vfs.h"
  23. static inline void outb(unsigned short port, unsigned char v)
  24. {
  25. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  26. }
  27. volatile unsigned int ticks;
  28. struct idt_entry {
  29. unsigned short offset_lo;
  30. unsigned short selector;
  31. unsigned char zero;
  32. unsigned char type;
  33. unsigned short offset_hi;
  34. } __attribute__((packed));
  35. struct idtr_desc {
  36. unsigned short limit;
  37. unsigned int base;
  38. } __attribute__((packed));
  39. static struct idt_entry idt[256];
  40. static struct idtr_desc idtr;
  41. extern void pit_irq_stub(void);
  42. extern void kbd_irq_stub(void);
  43. extern void fdc_irq_stub(void);
  44. extern void default_irq_stub(void);
  45. extern void syscall_stub(void);
  46. extern unsigned int exc_stub_table[];
  47. unsigned int get_ticks_ms(void)
  48. {
  49. return ticks;
  50. }
  51. void exc_handler(struct exc_state *st);
  52. void exc_handler(struct exc_state *st)
  53. {
  54. unsigned int vector = st->vector;
  55. unsigned int cr2 = 0;
  56. static const char * const names[] = {
  57. "#DE Divide Error", "#DB Debug", "NMI", "#BP Breakpoint",
  58. "#OF Overflow", "#BR Bound Range", "#UD Invalid Opcode",
  59. "#NM No FPU", "#DF Double Fault", "Coprocessor Overrun",
  60. "#TS Invalid TSS", "#NP Seg Not Present", "#SS Stack Fault",
  61. "#GP General Protection", "#PF Page Fault", "Reserved",
  62. "#MF FPU Error", "#AC Alignment Check", "#MC Machine Check",
  63. "#XM SIMD Error",
  64. };
  65. __asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
  66. /* A fault from ring 3 is a bug in a user command (or its ELF): kill that
  67. * process and keep the kernel running. Kernel faults still halt. */
  68. if ((st->cs & 3) == 3) {
  69. serial_puts(SERIAL_COM1, "\n*** user fault vec ");
  70. serial_puthex(SERIAL_COM1, vector);
  71. serial_puts(SERIAL_COM1, " eip=");
  72. serial_puthex(SERIAL_COM1, st->eip);
  73. serial_puts(SERIAL_COM1, " cr2=");
  74. serial_puthex(SERIAL_COM1, cr2);
  75. serial_puts(SERIAL_COM1, " -- process killed\n");
  76. io_putstr("\n*** fault: ");
  77. io_putstr(vector < 20 ? names[vector] : "unknown vector");
  78. io_putstr(" -- command killed\n");
  79. process_exit(); /* marks us dead and waits to be switched out */
  80. }
  81. /* Fatal kernel fault: the banner goes straight into the LFB (no serial
  82. * console needed) and the full state is mirrored to COM1. */
  83. panic_report(st, vector < 20 ? names[vector] : "unknown vector");
  84. for (;;)
  85. __asm__ volatile ("cli; hlt");
  86. }
  87. static void set_idt_gate(int vec, unsigned int handler)
  88. {
  89. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  90. idt[vec].selector = 0x08;
  91. idt[vec].zero = 0;
  92. idt[vec].type = 0x8E;
  93. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  94. }
  95. /* Trap gate (DPL=3) so ring-3 can `int` into it; a trap gate does NOT clear
  96. * IF, so a blocking syscall (e.g. sleep) can still be preempted by the timer. */
  97. static void set_idt_gate_user(int vec, unsigned int handler)
  98. {
  99. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  100. idt[vec].selector = 0x08;
  101. idt[vec].zero = 0;
  102. idt[vec].type = 0xEF;
  103. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  104. }
  105. static void timer_init(void)
  106. {
  107. int i;
  108. /* Remap PICs: master at 0x20, slave at 0x28. */
  109. outb(0x20, 0x11);
  110. outb(0xA0, 0x11);
  111. outb(0x21, 0x20);
  112. outb(0xA1, 0x28);
  113. outb(0x21, 0x04);
  114. outb(0xA1, 0x02);
  115. outb(0x21, 0x01);
  116. outb(0xA1, 0x01);
  117. outb(0x21, 0xFC);
  118. outb(0xA1, 0xFF);
  119. /* Default handler for all 256 vectors: absorbs any unexpected
  120. interrupt (including spurious IRQ 7) with an EOI + iret. */
  121. for (i = 0; i < 256; i++)
  122. set_idt_gate(i, (unsigned int)default_irq_stub);
  123. /* Exception handlers 0-31: ring-3 faults kill the process, kernel
  124. faults print the panic banner and halt (see exc_handler). */
  125. for (i = 0; i < 32; i++)
  126. set_idt_gate(i, exc_stub_table[i]);
  127. /* Hardware IRQ handlers. */
  128. set_idt_gate(0x20, (unsigned int)pit_irq_stub);
  129. set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
  130. set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
  131. /* Syscall gate (ring-3 can int 0x80). */
  132. set_idt_gate_user(0x80, (unsigned int)syscall_stub);
  133. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  134. idtr.base = (unsigned int)idt;
  135. __asm__ volatile ("lidt %0" : : "m"(idtr));
  136. outb(0x43, 0x36);
  137. outb(0x40, (unsigned char)(1193 & 0xFF));
  138. outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
  139. __asm__ volatile ("sti");
  140. }
  141. extern unsigned char __bss_start[], __bss_end[];
  142. /* The idle process: keeps the parts of the system that must not depend on
  143. * the window manager running. Input routing, cursor blink, kexec recovery,
  144. * netlog flushing and gfx-session cleanup live here; the WM process only
  145. * handles window state and drawing. */
  146. static void kernel_idle_loop(void)
  147. {
  148. for (;;) {
  149. mouse_poll_update();
  150. wm_pump_keyboard();
  151. wm_blink_cursor();
  152. netlog_flush();
  153. wm_handle_kexec();
  154. wm_server_supervise();
  155. if (wm_server_can_reap())
  156. sys_gfx_collect();
  157. if (!wm_server_frame_active())
  158. vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
  159. cpu_idle();
  160. }
  161. }
  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. vfs_mount_all();
  195. /* The command pack: read-only, mounted at /ramfs (see ramfs.c). */
  196. {
  197. extern unsigned char _binary_cmds_pack_start[];
  198. extern unsigned char _binary_cmds_pack_end[];
  199. unsigned int pack_len = (unsigned int)(_binary_cmds_pack_end -
  200. _binary_cmds_pack_start);
  201. if (ramfs_init(_binary_cmds_pack_start, pack_len) != 0)
  202. serial_puts(SERIAL_COM1, "ramfs: bad pack\n");
  203. else {
  204. serial_puts(SERIAL_COM1, "ramfs: pack ok, ");
  205. serial_putdec(SERIAL_COM1, ramfs_count());
  206. serial_puts(SERIAL_COM1, " files\n");
  207. vfs_mount_ramfs();
  208. }
  209. }
  210. /* Bring the NIC up before the WM loop starts, so an idle board can pick
  211. * up a network kexec (`make net-dev`) without any typing. */
  212. netdev_active();
  213. /* With a NIC present, try DHCP; on failure the static default stays. */
  214. if (netdev_active())
  215. dhcp_run(3000);
  216. /* Slot 0 is the idle loop; the window server is initialized before the
  217. * ring-3 WM is spawned from /ramfs/bin/wm. */
  218. wm_server_init();
  219. wm_server_start();
  220. kernel_idle_loop();
  221. }