kernel.c 21 KB

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