kernel.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* Kernel: entry point at 0x7E00, timer (PIT 1 kHz), process init, terminal. */
  2. #include "acpi.h"
  3. #include "cpu.h"
  4. #include "idle.h"
  5. #include "io.h"
  6. #include "lapic.h"
  7. #include "panic.h"
  8. #include "kmalloc.h"
  9. #include "mm.h"
  10. #include "mouse.h"
  11. #include "netdev.h"
  12. #include "dhcp.h"
  13. #include "netlog.h"
  14. #include "paging.h"
  15. #include "process.h"
  16. #include "ramfs.h"
  17. #include "sys_gfx.h"
  18. #include "timer.h"
  19. #include "tss.h"
  20. #include "usbhid.h"
  21. #include "wm.h"
  22. #include "serial.h"
  23. #include "smp.h"
  24. #include "version.h"
  25. #include "vga.h"
  26. #include "vfs.h"
  27. static inline void outb(unsigned short port, unsigned char v)
  28. {
  29. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  30. }
  31. volatile unsigned int ticks;
  32. struct idt_entry {
  33. unsigned short offset_lo;
  34. unsigned short selector;
  35. unsigned char zero;
  36. unsigned char type;
  37. unsigned short offset_hi;
  38. } __attribute__((packed));
  39. struct idtr_desc {
  40. unsigned short limit;
  41. unsigned int base;
  42. } __attribute__((packed));
  43. static struct idt_entry idt[256];
  44. static struct idtr_desc idtr;
  45. extern void pit_irq_stub(void);
  46. extern void kbd_irq_stub(void);
  47. extern void fdc_irq_stub(void);
  48. extern void default_irq_stub(void);
  49. extern void resched_stub(void);
  50. extern void lapic_timer_stub(void);
  51. extern void lapic_resched_stub(void);
  52. extern void lapic_spurious_stub(void);
  53. extern void syscall_stub(void);
  54. extern unsigned int exc_stub_table[];
  55. unsigned int get_ticks_ms(void)
  56. {
  57. return ticks;
  58. }
  59. void exc_handler(struct exc_state *st);
  60. void exc_handler(struct exc_state *st)
  61. {
  62. unsigned int vector = st->vector;
  63. unsigned int cr2 = 0;
  64. static const char * const names[] = {
  65. "#DE Divide Error", "#DB Debug", "NMI", "#BP Breakpoint",
  66. "#OF Overflow", "#BR Bound Range", "#UD Invalid Opcode",
  67. "#NM No FPU", "#DF Double Fault", "Coprocessor Overrun",
  68. "#TS Invalid TSS", "#NP Seg Not Present", "#SS Stack Fault",
  69. "#GP General Protection", "#PF Page Fault", "Reserved",
  70. "#MF FPU Error", "#AC Alignment Check", "#MC Machine Check",
  71. "#XM SIMD Error",
  72. };
  73. __asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
  74. /* A fault from ring 3 is a bug in a user command (or its ELF): kill that
  75. * process and keep the kernel running. Kernel faults still halt. */
  76. if ((st->cs & 3) == 3) {
  77. serial_puts(SERIAL_COM1, "\n*** user fault vec ");
  78. serial_puthex(SERIAL_COM1, vector);
  79. serial_puts(SERIAL_COM1, " eip=");
  80. serial_puthex(SERIAL_COM1, st->eip);
  81. serial_puts(SERIAL_COM1, " cr2=");
  82. serial_puthex(SERIAL_COM1, cr2);
  83. serial_puts(SERIAL_COM1, " -- process killed\n");
  84. io_putstr("\n*** fault: ");
  85. io_putstr(vector < 20 ? names[vector] : "unknown vector");
  86. io_putstr(" -- command killed\n");
  87. process_exit(); /* marks us dead and waits to be switched out */
  88. }
  89. /* Fatal kernel fault: the banner goes straight into the LFB (no serial
  90. * console needed) and the full state is mirrored to COM1. */
  91. panic_report(st, vector < 20 ? names[vector] : "unknown vector");
  92. for (;;)
  93. __asm__ volatile ("cli; hlt");
  94. }
  95. static void set_idt_gate(int vec, unsigned int handler)
  96. {
  97. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  98. idt[vec].selector = 0x08;
  99. idt[vec].zero = 0;
  100. idt[vec].type = 0x8E;
  101. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  102. }
  103. /* Trap gate (DPL=3) so ring-3 can `int` into it; a trap gate does NOT clear
  104. * IF, so a blocking syscall (e.g. sleep) can still be preempted by the timer. */
  105. static void set_idt_gate_user(int vec, unsigned int handler)
  106. {
  107. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  108. idt[vec].selector = 0x08;
  109. idt[vec].zero = 0;
  110. idt[vec].type = 0xEF;
  111. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  112. }
  113. static void timer_init(void)
  114. {
  115. int i;
  116. /* Remap PICs: master at 0x20, slave at 0x28. */
  117. outb(0x20, 0x11);
  118. outb(0xA0, 0x11);
  119. outb(0x21, 0x20);
  120. outb(0xA1, 0x28);
  121. outb(0x21, 0x04);
  122. outb(0xA1, 0x02);
  123. outb(0x21, 0x01);
  124. outb(0xA1, 0x01);
  125. outb(0x21, 0xFC);
  126. outb(0xA1, 0xFF);
  127. /* Default handler for all 256 vectors: absorbs any unexpected
  128. interrupt (including spurious IRQ 7) with an EOI + iret. */
  129. for (i = 0; i < 256; i++)
  130. set_idt_gate(i, (unsigned int)default_irq_stub);
  131. /* Exception handlers 0-31: ring-3 faults kill the process, kernel
  132. faults print the panic banner and halt (see exc_handler). */
  133. for (i = 0; i < 32; i++)
  134. set_idt_gate(i, exc_stub_table[i]);
  135. /* Hardware IRQ handlers. */
  136. set_idt_gate(0x20, (unsigned int)pit_irq_stub);
  137. set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
  138. set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
  139. /* Local-APIC vectors: the AP timer and reschedule IPI, plus the
  140. * spurious vector the LAPIC can deliver at any time. */
  141. set_idt_gate(LAPIC_TIMER_VECTOR, (unsigned int)lapic_timer_stub);
  142. set_idt_gate(LAPIC_RESCHED_VECTOR, (unsigned int)lapic_resched_stub);
  143. set_idt_gate(LAPIC_SPURIOUS_VECTOR, (unsigned int)lapic_spurious_stub);
  144. /* Syscall gate (ring-3 can int 0x80). */
  145. set_idt_gate_user(0x80, (unsigned int)syscall_stub);
  146. /* Kernel-only voluntary reschedule gate (int 0x81). */
  147. set_idt_gate(0x81, (unsigned int)resched_stub);
  148. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  149. idtr.base = (unsigned int)idt;
  150. __asm__ volatile ("lidt %0" : : "m"(idtr));
  151. outb(0x43, 0x36);
  152. outb(0x40, (unsigned char)(1193 & 0xFF));
  153. outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
  154. __asm__ volatile ("sti");
  155. }
  156. /* The BSP built the IDT once; APs only need to load the same pointer. */
  157. void idt_load_ap(void)
  158. {
  159. __asm__ volatile ("lidt %0" : : "m"(idtr));
  160. }
  161. extern unsigned char __bss_start[], __bss_end[];
  162. static int kworker_pid = -1;
  163. /* Parts of the system that must keep running independently of the WM:
  164. * input routing, cursor blink, kexec recovery, netlog flushing, gfx-session
  165. * cleanup and the shadow-buffer flush. */
  166. static void system_housekeeping(void)
  167. {
  168. process_reap_dead();
  169. mouse_poll_update();
  170. wm_pump_keyboard();
  171. wm_blink_cursor();
  172. netlog_flush();
  173. wm_handle_kexec();
  174. wm_server_supervise();
  175. if (wm_server_can_reap())
  176. sys_gfx_collect();
  177. if (!wm_server_frame_active())
  178. vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
  179. }
  180. static void kworker_main(void)
  181. {
  182. for (;;) {
  183. system_housekeeping();
  184. process_sleep_ms(5);
  185. }
  186. }
  187. /* Idle is not a normal runnable task: schedule() selects it only when no
  188. * READY process exists. The kworker does the housekeeping; if it dies (or
  189. * could not be created), idle takes over as a degraded fallback. */
  190. static void kernel_idle_loop(void)
  191. {
  192. for (;;) {
  193. if (kworker_pid <= 0 ||
  194. !process_is_alive((unsigned int)kworker_pid)) {
  195. /* Idle must never sleep on the BKL: if a user process on
  196. * another CPU owns it, just keep idling and retry. */
  197. if (bkl_trylock()) {
  198. system_housekeeping();
  199. bkl_unlock();
  200. }
  201. }
  202. cpu_idle();
  203. }
  204. }
  205. void kernel_main(void);
  206. void kernel_main(void)
  207. {
  208. unsigned char *p = __bss_start;
  209. while (p < __bss_end)
  210. *p++ = 0;
  211. serial_init(SERIAL_COM1);
  212. serial_ring_init();
  213. /* Network console mirror from the very first line on; the ring holds the
  214. * early boot output until the WM loop starts flushing it. */
  215. netlog_init();
  216. serial_puts(SERIAL_COM1, "\n" OS_NAME " v" OS_VERSION " booted\n");
  217. /* GDT/TSS and the per-CPU GS base must exist before anything that can
  218. * use current_proc()/this_cpu(). The BSS is already cleared and the
  219. * GDT/TSS live at identity-mapped addresses. */
  220. gdt_tss_init(0);
  221. mm_init();
  222. mm_dump();
  223. kmalloc_init();
  224. paging_init();
  225. /* paging_init only ORs in PSE; make sure the BSP's SSE state is on
  226. * before any drawing path can select an SSE2 helper. */
  227. cpu_enable_sse();
  228. /* ACPI needs paging for table access; a missing or unusable MADT simply
  229. * leaves the machine at one CPU. */
  230. acpi_init();
  231. acpi_dump();
  232. io_init();
  233. idle_init();
  234. /* process_init must run before timer_init: once the PIT is started (sti
  235. * in timer_init) the context-switch stub runs on every tick and reads
  236. * the current process, its CR3 and the TSS, so those must be valid. */
  237. process_init();
  238. timer_init();
  239. /* The PIT (and its sti) is live, so the AP bring-up can use it as a
  240. * timeout clock. APs idle until smp_enable_sched() after boot. */
  241. smp_init();
  242. mouse_init();
  243. /* USB HID backend (mouse + keyboard): controller bring-up + first
  244. * scan; devices plugged in later are picked up by the hotplug
  245. * watcher in the WM loop. */
  246. usbhid_init();
  247. vga_shadow_init();
  248. vga_fb_flush();
  249. vfs_mount_all();
  250. /* The command pack: read-only, mounted at /ramfs (see ramfs.c). */
  251. {
  252. extern unsigned char _binary_cmds_pack_gz_bin_start[];
  253. extern unsigned char _binary_cmds_pack_gz_bin_end[];
  254. unsigned int pack_len =
  255. (unsigned int)(_binary_cmds_pack_gz_bin_end -
  256. _binary_cmds_pack_gz_bin_start);
  257. if (ramfs_init(_binary_cmds_pack_gz_bin_start, pack_len) != 0)
  258. serial_puts(SERIAL_COM1, "ramfs: bad pack\n");
  259. else {
  260. serial_puts(SERIAL_COM1, "ramfs: pack ok, ");
  261. serial_putdec(SERIAL_COM1, ramfs_count());
  262. serial_puts(SERIAL_COM1, " files, ");
  263. serial_putdec(SERIAL_COM1, pack_len);
  264. serial_puts(SERIAL_COM1, " -> ");
  265. serial_putdec(SERIAL_COM1, ramfs_pack_size());
  266. serial_puts(SERIAL_COM1, " bytes\n");
  267. vfs_mount_ramfs();
  268. }
  269. }
  270. /* Bring the NIC up before the WM loop starts, so an idle board can pick
  271. * up a network kexec (`make net-dev`) without any typing. */
  272. netdev_active();
  273. /* With a NIC present, try DHCP; on failure the static default stays. */
  274. if (netdev_active())
  275. dhcp_run(3000);
  276. /* The idle process is the kernel_main context; the ring-3 WM is spawned
  277. * from /ramfs/bin/wm. Housekeeping runs in its own process so it cannot
  278. * be starved by a busy system. */
  279. wm_server_init();
  280. wm_server_start();
  281. kworker_pid = process_create("kworker", kworker_main);
  282. if (kworker_pid < 0) {
  283. serial_puts(SERIAL_COM1, "kworker: cannot create, using idle fallback\n");
  284. kworker_pid = -1;
  285. } else {
  286. process_set_weight((unsigned int)kworker_pid, 1);
  287. process_set_affinity((unsigned int)kworker_pid, 1);
  288. }
  289. /* Boot-time device/FS setup is complete; APs may now take user work.
  290. * Kernel threads stay pinned to the BSP and serialize on the BKL. */
  291. smp_enable_sched();
  292. kernel_idle_loop();
  293. }