kernel.c 23 KB

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