kernel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /* Kernel: entry point at 0x7E00, timer (PIT 1 kHz), process init, terminal. */
  2. #include "acpi.h"
  3. #include "asix.h"
  4. #include "bootcon.h"
  5. #include "bootinfo.h"
  6. #include "cpu.h"
  7. #include "cros_ec.h"
  8. #include "idle.h"
  9. #include "io.h"
  10. #include "ioapic.h"
  11. #include "lapic.h"
  12. #include "panic.h"
  13. #include "kmalloc.h"
  14. #include "mm.h"
  15. #include "mouse.h"
  16. #include "netdev.h"
  17. #include "net.h"
  18. #include "dhcp.h"
  19. #include "netlog.h"
  20. #include "paging.h"
  21. #include "power.h"
  22. #include "process.h"
  23. #include "ramfs.h"
  24. #include "cursor.h"
  25. #include "rndis.h"
  26. #include "rtl8821au.h"
  27. #include "sched.h"
  28. #include "sys_fs.h"
  29. #include "sys_wm.h"
  30. #include "thermal.h"
  31. #include "timer.h"
  32. #include "tpec.h"
  33. #include "tss.h"
  34. #include "usbhid.h"
  35. #include "usbmsc.h"
  36. #include "wm.h"
  37. #include "serial.h"
  38. #include "smp.h"
  39. #include "version.h"
  40. #include "vga.h"
  41. #include "vfs.h"
  42. #include "wifi_cfg.h"
  43. static inline void outb(unsigned short port, unsigned char v)
  44. {
  45. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  46. }
  47. volatile unsigned int ticks;
  48. static unsigned int timer_hz;
  49. static unsigned int tick_ms = 1;
  50. struct idt_entry {
  51. unsigned short offset_lo;
  52. unsigned short selector;
  53. unsigned char zero;
  54. unsigned char type;
  55. unsigned short offset_hi;
  56. } __attribute__((packed));
  57. struct idtr_desc {
  58. unsigned short limit;
  59. unsigned int base;
  60. } __attribute__((packed));
  61. static struct idt_entry idt[256];
  62. static struct idtr_desc idtr;
  63. extern void pit_irq_stub(void);
  64. extern void kbd_irq_stub(void);
  65. extern void mouse_irq_stub(void);
  66. extern void fdc_irq_stub(void);
  67. extern void default_irq_stub(void);
  68. extern void resched_stub(void);
  69. extern void lapic_timer_stub(void);
  70. extern void lapic_resched_stub(void);
  71. extern void lapic_spurious_stub(void);
  72. extern void syscall_stub(void);
  73. extern unsigned int exc_stub_table[];
  74. unsigned int get_ticks_ms(void)
  75. {
  76. return ticks;
  77. }
  78. unsigned int timer_tick_ms(void)
  79. {
  80. return tick_ms;
  81. }
  82. void timer_tick_advance(void)
  83. {
  84. ticks += tick_ms;
  85. }
  86. void timer_set_hz(unsigned int hz)
  87. {
  88. unsigned int flags, divisor;
  89. if (hz < 100 || hz > 1000)
  90. hz = 1000;
  91. if (hz == timer_hz)
  92. return;
  93. __asm__ volatile ("pushfl; popl %0" : "=r"(flags));
  94. __asm__ volatile ("cli" ::: "memory");
  95. timer_hz = hz;
  96. tick_ms = 1000 / hz;
  97. divisor = 1193182u / hz;
  98. outb(0x43, 0x36);
  99. outb(0x40, (unsigned char)(divisor & 0xFF));
  100. outb(0x40, (unsigned char)((divisor >> 8) & 0xFF));
  101. if (flags & (1u << 9))
  102. __asm__ volatile ("sti" ::: "memory");
  103. }
  104. void exc_handler(struct exc_state *st);
  105. void exc_handler(struct exc_state *st)
  106. {
  107. unsigned int vector = st->vector;
  108. unsigned int cr2 = 0;
  109. static const char * const names[] = {
  110. "#DE Divide Error", "#DB Debug", "NMI", "#BP Breakpoint",
  111. "#OF Overflow", "#BR Bound Range", "#UD Invalid Opcode",
  112. "#NM No FPU", "#DF Double Fault", "Coprocessor Overrun",
  113. "#TS Invalid TSS", "#NP Seg Not Present", "#SS Stack Fault",
  114. "#GP General Protection", "#PF Page Fault", "Reserved",
  115. "#MF FPU Error", "#AC Alignment Check", "#MC Machine Check",
  116. "#XM SIMD Error",
  117. };
  118. __asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
  119. /* A fault from ring 3 is a bug in a user command (or its ELF): kill that
  120. * process and keep the kernel running. Kernel faults still halt. */
  121. if ((st->cs & 3) == 3) {
  122. struct process *p = current_proc();
  123. serial_puts(SERIAL_COM1, "\n*** user fault vec ");
  124. serial_puthex(SERIAL_COM1, vector);
  125. serial_puts(SERIAL_COM1, " eip=");
  126. serial_puthex(SERIAL_COM1, st->eip);
  127. serial_puts(SERIAL_COM1, " cr2=");
  128. serial_puthex(SERIAL_COM1, cr2);
  129. if (p) {
  130. serial_puts(SERIAL_COM1, " pid=");
  131. serial_putdec(SERIAL_COM1, p->pid);
  132. serial_puts(SERIAL_COM1, " ");
  133. serial_puts(SERIAL_COM1, p->name);
  134. }
  135. serial_puts(SERIAL_COM1, " -- process killed\n");
  136. io_putstr("\n*** fault: ");
  137. io_putstr(vector < 20 ? names[vector] : "unknown vector");
  138. io_putstr(" -- command killed\n");
  139. process_exit(); /* marks us dead and waits to be switched out */
  140. }
  141. /* Fatal kernel fault: the banner goes straight into the LFB (no serial
  142. * console needed) and the full state is mirrored to COM1. */
  143. panic_report(st, vector < 20 ? names[vector] : "unknown vector");
  144. for (;;)
  145. __asm__ volatile ("cli; hlt");
  146. }
  147. static void set_idt_gate(int vec, unsigned int handler)
  148. {
  149. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  150. idt[vec].selector = 0x08;
  151. idt[vec].zero = 0;
  152. idt[vec].type = 0x8E;
  153. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  154. }
  155. /* Trap gate (DPL=3) so ring-3 can `int` into it; a trap gate does NOT clear
  156. * IF, so a blocking syscall (e.g. sleep) can still be preempted by the timer. */
  157. static void set_idt_gate_user(int vec, unsigned int handler)
  158. {
  159. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  160. idt[vec].selector = 0x08;
  161. idt[vec].zero = 0;
  162. idt[vec].type = 0xEF;
  163. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  164. }
  165. static void timer_init(void)
  166. {
  167. int i;
  168. /* Remap PICs: master at 0x20, slave at 0x28. */
  169. outb(0x20, 0x11);
  170. outb(0xA0, 0x11);
  171. outb(0x21, 0x20);
  172. outb(0xA1, 0x28);
  173. outb(0x21, 0x04);
  174. outb(0xA1, 0x02);
  175. outb(0x21, 0x01);
  176. outb(0xA1, 0x01);
  177. outb(0x21, 0xFC);
  178. outb(0xA1, 0xFF);
  179. /* Default handler for all 256 vectors: absorbs any unexpected
  180. interrupt (including spurious IRQ 7) with an EOI + iret. */
  181. for (i = 0; i < 256; i++)
  182. set_idt_gate(i, (unsigned int)default_irq_stub);
  183. /* Exception handlers 0-31: ring-3 faults kill the process, kernel
  184. faults print the panic banner and halt (see exc_handler). */
  185. for (i = 0; i < 32; i++)
  186. set_idt_gate(i, exc_stub_table[i]);
  187. /* Hardware IRQ handlers. */
  188. set_idt_gate(0x20, (unsigned int)pit_irq_stub);
  189. set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
  190. set_idt_gate(0x2c, (unsigned int)mouse_irq_stub);
  191. set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
  192. /* Local-APIC vectors: the AP timer and reschedule IPI, plus the
  193. * spurious vector the LAPIC can deliver at any time. */
  194. set_idt_gate(LAPIC_TIMER_VECTOR, (unsigned int)lapic_timer_stub);
  195. set_idt_gate(LAPIC_RESCHED_VECTOR, (unsigned int)lapic_resched_stub);
  196. set_idt_gate(LAPIC_SPURIOUS_VECTOR, (unsigned int)lapic_spurious_stub);
  197. /* Syscall gate (ring-3 can int 0x80). */
  198. set_idt_gate_user(0x80, (unsigned int)syscall_stub);
  199. /* Kernel-only voluntary reschedule gate (int 0x81). */
  200. set_idt_gate(0x81, (unsigned int)resched_stub);
  201. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  202. idtr.base = (unsigned int)idt;
  203. __asm__ volatile ("lidt %0" : : "m"(idtr));
  204. /* Start the PIT at 1 kHz; schedule() drops it to 100 Hz while the BSP
  205. * is idle (see timer_set_hz). */
  206. timer_set_hz(1000);
  207. __asm__ volatile ("sti");
  208. }
  209. /* The BSP built the IDT once; APs only need to load the same pointer. */
  210. void idt_load_ap(void)
  211. {
  212. __asm__ volatile ("lidt %0" : : "m"(idtr));
  213. }
  214. extern unsigned char __bss_start[], __bss_end[];
  215. static int kworker_pid = -1;
  216. /* Parts of the system that must keep running independently of the WM:
  217. * input routing, thermal polling, cursor blink, kexec recovery, netlog
  218. * flushing, gfx-session cleanup and the shadow-buffer flush. */
  219. static void system_housekeeping(void)
  220. {
  221. process_reap_dead();
  222. thermal_housekeeping();
  223. mouse_poll_update();
  224. #ifndef NOWM
  225. wm_pump_keyboard();
  226. #endif
  227. netlog_flush();
  228. #ifndef NOWM
  229. wm_handle_kexec();
  230. wm_server_supervise();
  231. if (!wm_server_frame_active())
  232. vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
  233. #else
  234. /* Diagnostic build: keep the direct-LFB boot console visible and do
  235. * not let the WM shadow flush overwrite it. */
  236. #endif
  237. }
  238. static void kworker_main(void)
  239. {
  240. for (;;) {
  241. system_housekeeping();
  242. process_sleep_ms(2);
  243. }
  244. }
  245. /* Idle is not a normal runnable task: schedule() selects it only when no
  246. * READY process exists. The kworker does the housekeeping; if it dies (or
  247. * could not be created), idle takes over as a degraded fallback. */
  248. static void kernel_idle_loop(void)
  249. {
  250. for (;;) {
  251. if (kworker_pid <= 0 ||
  252. !process_is_alive((unsigned int)kworker_pid)) {
  253. /* Idle must never sleep on the BKL: if a user process on
  254. * another CPU owns it, just keep idling and retry. */
  255. if (bkl_trylock()) {
  256. system_housekeeping();
  257. bkl_unlock();
  258. }
  259. }
  260. cpu_idle();
  261. }
  262. }
  263. /* Boot-stage tracing: COM1 (netlog mirrors it) plus the direct-LFB boot
  264. * console, so a machine without serial still shows where the boot stops. */
  265. static unsigned int boot_t0;
  266. static void boot_trace(const char *what)
  267. {
  268. unsigned int ms = get_ticks_ms() - boot_t0;
  269. bootcon_puts("[");
  270. bootcon_puts(what);
  271. bootcon_puts("] +");
  272. bootcon_dec(ms);
  273. bootcon_puts(" ms\n");
  274. serial_puts(SERIAL_COM1, "[boot] ");
  275. serial_puts(SERIAL_COM1, what);
  276. serial_puts(SERIAL_COM1, " +");
  277. serial_putdec(SERIAL_COM1, ms);
  278. serial_puts(SERIAL_COM1, " ms\n");
  279. }
  280. static void boot_val(const char *name, unsigned int v)
  281. {
  282. bootcon_puts(name);
  283. bootcon_puts(": ");
  284. bootcon_dec(v);
  285. bootcon_puts("\n");
  286. serial_puts(SERIAL_COM1, name);
  287. serial_puts(SERIAL_COM1, ": ");
  288. serial_putdec(SERIAL_COM1, v);
  289. serial_puts(SERIAL_COM1, "\n");
  290. }
  291. /* Bounded PIT self-test. If the 8259 does not deliver IRQ0, every later
  292. * delay/timeout would spin on a frozen tick counter; say so and stop. */
  293. static int pit_ticking(void)
  294. {
  295. unsigned int t0 = get_ticks_ms();
  296. unsigned int n;
  297. for (n = 0; n < 5000000u; n++) {
  298. if (get_ticks_ms() != t0)
  299. return 1;
  300. __asm__ volatile ("pause");
  301. }
  302. return 0;
  303. }
  304. /* A kexec/net-dev reload starts a fresh kernel with zeroed BSS, so the
  305. * leased address and resolver would otherwise fall back to the static
  306. * default until someone types dhcp. The previous kernel stashed them in
  307. * the boot-info reserved words; apply them before the boot DHCP attempt. */
  308. static int net_restore_boot_config(void)
  309. {
  310. const volatile unsigned int *p;
  311. unsigned char ip[4], mask[4], gw[4], dns[4];
  312. unsigned int i;
  313. if (!bootinfo_valid() || !(bootinfo_flags() & BOOTINFO_FLAG_NET))
  314. return 0;
  315. p = bootinfo_words();
  316. for (i = 0; i < 4; i++) {
  317. ip[i] = (unsigned char)(p[7] >> (8 * i));
  318. mask[i] = (unsigned char)(p[8] >> (8 * i));
  319. gw[i] = (unsigned char)(p[9] >> (8 * i));
  320. dns[i] = (unsigned char)(p[10] >> (8 * i));
  321. }
  322. if (!(ip[0] || ip[1] || ip[2] || ip[3]))
  323. return 0;
  324. net_configure(ip, mask, gw);
  325. if (dns[0] || dns[1] || dns[2] || dns[3])
  326. net_set_dns(dns);
  327. boot_trace("net retained");
  328. /* A resolver is what net-dev users notice missing; without one, let
  329. * the boot DHCP attempt learn it. */
  330. return dns[0] || dns[1] || dns[2] || dns[3];
  331. }
  332. /* Saved WiFi networks on the writable /data volume: let the normal
  333. * supplicant scan and join any known network in the background. It leaves a
  334. * wired link that is physically up alone, and a missing /data or wifi helper
  335. * is harmless. */
  336. static void boot_wifi_autoconnect(void)
  337. {
  338. unsigned int sz = 0;
  339. int is_dir = 0;
  340. if (vfs_stat(WIFI_CRED_PATH, &sz, &is_dir) == 0 &&
  341. !is_dir && sz > 0) {
  342. struct vfs_file f;
  343. struct vfs_mount *m = 0;
  344. if (vfs_load_file("/ramfs/bin/wifi", &f, &m) == 0) {
  345. if (process_create_user("wifi", f.data, f.size,
  346. "autoconnect", "/") > 0)
  347. serial_puts(SERIAL_COM1,
  348. "wifi: autoconnect started\n");
  349. vfs_release_file(m, &f);
  350. }
  351. }
  352. }
  353. /* Boot-time storage scan in its own kernel thread. A process (unlike the
  354. * idle context kernel_main runs in) may sleep, so the probes' long waits
  355. * (floppy motor, ATA/AHCI timeouts) go through vfs_scan_relax() and the WM
  356. * and the first terminal run meanwhile. Disk file operations wait for
  357. * vfs_scan_busy; /ramfs and /share do not. The WiFi autoconnect needs
  358. * /data, so it runs here after the mount. Every COM1 character this thread
  359. * writes is also captured into the bootcon scan ring, which the ring-3
  360. * `bootscan` window tails live (it is spawned before the first terminal). */
  361. static volatile int boot_dhcp_done;
  362. static void boot_storage_main(void)
  363. {
  364. bootcon_scan_begin();
  365. vfs_mount_all();
  366. sys_mount_load_config();
  367. boot_trace("storage");
  368. /* As before, WiFi only starts after the boot DHCP attempt on the
  369. * wired NIC, so two DHCP clients never overlap. */
  370. while (!boot_dhcp_done)
  371. process_sleep_ms(20);
  372. boot_wifi_autoconnect();
  373. boot_trace("wifi auto");
  374. bootcon_scan_end();
  375. }
  376. void kernel_main(void);
  377. void kernel_main(void)
  378. {
  379. unsigned char *p = __bss_start;
  380. int net_kept, storage_inline;
  381. while (p < __bss_end)
  382. *p++ = 0;
  383. /* Earliest possible output: straight into the boot framebuffer, before
  384. * paging, drivers, the heap or the serial port. Everything below logs
  385. * here as well as to COM1, so a machine without serial (the X201) can
  386. * show where the boot stops. */
  387. bootcon_init();
  388. bootcon_puts(OS_NAME " v" OS_VERSION "\n");
  389. serial_init(SERIAL_COM1);
  390. serial_ring_init();
  391. /* Network console mirror from the very first line on; the ring holds the
  392. * early boot output until the WM loop starts flushing it. */
  393. netlog_init();
  394. serial_puts(SERIAL_COM1, "\n" OS_NAME " v" OS_VERSION " booted\n");
  395. boot_trace("start");
  396. /* GDT/TSS and the per-CPU GS base must exist before anything that can
  397. * use current_proc()/this_cpu(). The BSS is already cleared and the
  398. * GDT/TSS live at identity-mapped addresses. */
  399. gdt_tss_init(0);
  400. boot_trace("gdt");
  401. mm_init();
  402. mm_dump();
  403. boot_trace("mm");
  404. boot_val("mm free KB", mm_free_pages() * 4);
  405. kmalloc_init();
  406. /* Report the VBE geometry before paging, so a boot that dies in the
  407. * translation switch still says which mode the BIOS picked. */
  408. bootcon_fb_info();
  409. paging_init();
  410. boot_trace("paging");
  411. /* Keep a RAM copy of the boot console text; the WM hides the
  412. * framebuffer console later and this is what `bootlog` prints. */
  413. bootcon_log_init();
  414. /* paging_init only ORs in PSE; make sure the BSP's SSE state is on
  415. * before any drawing path can select an SSE2 helper. */
  416. cpu_enable_sse();
  417. cpu_init_fpu();
  418. /* ACPI needs paging for table access; a missing or unusable MADT simply
  419. * leaves the machine at one CPU. */
  420. acpi_init();
  421. acpi_dump();
  422. /* The kernel keeps the 8259 on LAPIC LINT0; firmware-left IOAPIC
  423. * entries would otherwise deliver into the PIC/LAPIC vector space. */
  424. ioapic_mask_all();
  425. boot_trace("acpi");
  426. boot_val("acpi cpus", acpi_cpu_count());
  427. io_init();
  428. boot_trace("io");
  429. /* P-states/deep C-states first: idle_init() picks its default from
  430. * the power module (deep MWAIT on Nehalem, HLT elsewhere). */
  431. power_init();
  432. idle_init();
  433. boot_trace("power");
  434. thermal_init();
  435. boot_trace("thermal");
  436. /* Board sensors: the ThinkPad EC probe is gated on SMBIOS identifying a
  437. * Lenovo/IBM machine, so it is a no-op everywhere else. The Chrome EC
  438. * probe is skipped when the ThinkPad EC was found. */
  439. tpec_init();
  440. cros_ec_init();
  441. boot_trace("ec");
  442. /* process_init must run before timer_init: once the PIT is started (sti
  443. * in timer_init) the context-switch stub runs on every tick and reads
  444. * the current process, its CR3 and the TSS, so those must be valid. */
  445. process_init();
  446. boot_trace("process");
  447. timer_init();
  448. boot_t0 = get_ticks_ms();
  449. boot_trace("timer");
  450. if (!pit_ticking()) {
  451. bootcon_puts("*** PIT NOT TICKING ***\n");
  452. serial_puts(SERIAL_COM1, "*** PIT NOT TICKING ***\n");
  453. for (;;)
  454. __asm__ volatile ("cli; hlt");
  455. }
  456. boot_val("timer ticks", get_ticks_ms());
  457. /* The PIT (and its sti) is live, so the AP bring-up can use it as a
  458. * timeout clock. APs idle until smp_enable_sched() after boot. */
  459. smp_init();
  460. boot_trace("smp");
  461. boot_val("smp cpus", cpu_count);
  462. mouse_init();
  463. boot_trace("ps2");
  464. vga_shadow_init();
  465. vga_fb_flush();
  466. boot_trace("vga");
  467. /* The command pack: read-only, mounted at /ramfs (see ramfs.c). The
  468. * boot path copied it into RAM (the legacy stage's low gap, the UEFI
  469. * loader or kexec); ramfs_init takes its own copy so the boot region
  470. * can be released. The mount is created even without a pack, so a running
  471. * board can be recovered with `make cmds-dev`/`make net-cmds`. */
  472. {
  473. unsigned int pack_addr = bootinfo_pack_addr();
  474. unsigned int pack_len = bootinfo_pack_size();
  475. int ok = 0, gz = 0;
  476. if (!pack_addr || !pack_len)
  477. serial_puts(SERIAL_COM1, "ramfs: no boot pack\n");
  478. else {
  479. const unsigned char *p = (const unsigned char *)pack_addr;
  480. gz = pack_len >= 2 && p[0] == 0x1F && p[1] == 0x8B;
  481. serial_puts(SERIAL_COM1, "ramfs: boot pack at ");
  482. serial_puthex(SERIAL_COM1, pack_addr);
  483. serial_puts(SERIAL_COM1, ", ");
  484. serial_putdec(SERIAL_COM1, pack_len);
  485. serial_puts(SERIAL_COM1, gz ? " bytes (gzip)\n" :
  486. " bytes\n");
  487. if (gz)
  488. ramfs_set_boot_image((const void *)pack_addr,
  489. pack_len, 1);
  490. ok = ramfs_init((const void *)pack_addr, pack_len) == 0;
  491. if (!ok) {
  492. serial_puts(SERIAL_COM1, "ramfs: bad boot pack\n");
  493. ramfs_set_boot_image(0, 0, 0);
  494. mm_free_range(pack_addr, pack_len);
  495. } else {
  496. serial_puts(SERIAL_COM1, "ramfs: pack ok, ");
  497. serial_putdec(SERIAL_COM1, ramfs_count());
  498. serial_puts(SERIAL_COM1, " files, ");
  499. serial_putdec(SERIAL_COM1, ramfs_pack_size());
  500. serial_puts(SERIAL_COM1, " bytes\n");
  501. cursor_load();
  502. if (!gz) {
  503. /* ramfs has its own copy now; keep that
  504. * as the self-update image. */
  505. ramfs_set_boot_image(ramfs_pack_base(),
  506. ramfs_pack_size(), 0);
  507. mm_free_range(pack_addr, pack_len);
  508. }
  509. /* A gzip boot image stays reserved so
  510. * `install self` can write it to P2. */
  511. }
  512. }
  513. (void)ok;
  514. vfs_mount_ramfs();
  515. /* /share too: the WM and the first terminal may use it while the
  516. * bootscan thread is still probing the disks, and a path that
  517. * does not resolve yet would wait for the whole scan. */
  518. vfs_mount_share();
  519. }
  520. boot_trace("ramfs");
  521. boot_val("ramfs files", ramfs_count());
  522. #ifndef NOWM
  523. /* Start the WM before touching disks: the first frame can be painted
  524. * while storage and the network are still being probed. */
  525. wm_server_init();
  526. boot_trace("wm init");
  527. wm_server_start();
  528. boot_trace("wm");
  529. #else
  530. /* Diagnostic build: boot to the console, no WM, no terminal. */
  531. bootcon_puts("WM disabled (diagnostic build)\n");
  532. serial_puts(SERIAL_COM1, "nowm: window manager disabled\n");
  533. boot_trace("wm off");
  534. #endif
  535. /* Publish the storage scan before anything can run a command: disk
  536. * paths wait for vfs_scan_busy (owned by the idle context until the
  537. * bootscan thread's vfs_mount_all() takes it over), /ramfs and /share
  538. * do not. Then let the WM spawn the bootscan window and the first
  539. * terminal at once; neither it nor the WM needs the NIC probe or the
  540. * disks below. */
  541. vfs_scan_owner_pid = 0;
  542. vfs_scan_busy = 1;
  543. #ifndef NOWM
  544. wm_server_boot_ready();
  545. boot_trace("terminal ready");
  546. #else
  547. boot_trace("terminal off");
  548. #endif
  549. #ifndef NOWM
  550. /* Let the WM put the bootscan log window and the first terminal on
  551. * screen before the USB bring-up below: kernel_main is the idle
  552. * context, so halting here hands the CPU to the WM and those windows.
  553. * Bounded, in case no terminal ever presents. */
  554. {
  555. unsigned int t0 = get_ticks_ms();
  556. while (!wm_first_window_presented &&
  557. get_ticks_ms() - t0 < 1000)
  558. __asm__ volatile ("sti; hlt");
  559. }
  560. boot_trace("terminal shown");
  561. #endif
  562. /* USB HID backend (mouse + keyboard) and the USB device drivers:
  563. * controller bring-up + first scan; devices plugged in later are
  564. * picked up by the hotplug watcher. Non-preemptible, exactly like
  565. * the bring-up before the WM existed: port resets and the SOF a
  566. * freshly enabled low-speed device needs within 3 ms must not wait
  567. * for a WM frame. Before the NIC probe: ASIX/RNDIS/RTL8821AU bind
  568. * during this scan, and USB disks before the bootscan thread. */
  569. asix_register();
  570. usbmsc_register();
  571. rtl8821au_register();
  572. rndis_register();
  573. preempt_disable();
  574. usbhid_init();
  575. preempt_enable();
  576. boot_trace("usb");
  577. /* Probe the NIC (link negotiation is asynchronous now) before the
  578. * kworker starts, so its netlog flushes have a device to use. */
  579. netdev_active();
  580. net_kept = net_restore_boot_config();
  581. boot_trace("net");
  582. /* The idle process is the kernel_main context; housekeeping runs in
  583. * its own process so it cannot be starved by a busy system. */
  584. kworker_pid = process_create("kworker", kworker_main);
  585. if (kworker_pid < 0) {
  586. serial_puts(SERIAL_COM1, "kworker: cannot create, using idle fallback\n");
  587. kworker_pid = -1;
  588. } else {
  589. process_set_weight((unsigned int)kworker_pid, 4);
  590. process_set_affinity((unsigned int)kworker_pid, 1);
  591. }
  592. boot_trace("kworker");
  593. /* Slow storage bring-up happens behind the usable desktop, in a
  594. * thread that may sleep through its probe timeouts. The scan was
  595. * published above; its vfs_mount_all() takes over the ownership.
  596. * Without a thread slot, scan inline as before. */
  597. boot_trace("storage begin");
  598. storage_inline = 0;
  599. if (process_create("bootscan", boot_storage_main) <= 0) {
  600. vfs_mount_all();
  601. boot_trace("storage");
  602. storage_inline = 1;
  603. }
  604. /* With a linked NIC present, try DHCP; on failure the static default
  605. * stays. A wired NIC without a cable is skipped so the WiFi fallback
  606. * below does not have to wait out the 4 s timeout first. Deliberately
  607. * after the WM: a missing cable or DHCP server must not delay the first
  608. * frame. */
  609. {
  610. const struct netdev *nd = netdev_active();
  611. int linked = nd && netdev_link(nd);
  612. if (linked && !net_kept)
  613. dhcp_run(4000);
  614. boot_trace(net_kept ? "dhcp skipped" :
  615. (linked ? "dhcp" : "dhcp no link"));
  616. }
  617. boot_dhcp_done = 1;
  618. if (storage_inline) {
  619. boot_wifi_autoconnect();
  620. boot_trace("wifi auto");
  621. }
  622. /* Dump the active NIC's counters after the boot DHCP attempt. With
  623. * the ASIX as the management link this appears in netmon even when
  624. * the active e1000e cannot pass traffic. */
  625. {
  626. const struct netdev *nd = netdev_active();
  627. if (nd && (nd->dump_brief || nd->dump_regs)) {
  628. io_putstr("\n[boot netregs: ");
  629. io_putstr(nd->name);
  630. io_putstr("]\n");
  631. if (nd->dump_brief)
  632. nd->dump_brief();
  633. else
  634. nd->dump_regs();
  635. }
  636. }
  637. /* Boot-time device/FS setup is complete; APs may now take user work.
  638. * Kernel threads stay pinned to the BSP and serialize on the BKL. */
  639. smp_enable_sched();
  640. boot_trace("sched");
  641. kernel_idle_loop();
  642. }