kernel.c 20 KB

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