aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / x86 / boot_x86.c
| 1 | /* boot_x86.c -- the boot sequence on the x86 kernel (ui/boot_show.h): the |
| 2 | * night on the GOP framebuffer (in a VM, QEMU virtio-vga's linear |
| 3 | * framebuffer, which is the aienos-run window), its chirp on the VM's |
| 4 | * virtio-sound when there is one (dev/virtio_snd.c, the same driver as the |
| 5 | * aarch64 kernel), timed by the TSC. Built when CK_BOOT_SHOW=1. |
| 6 | * DMA: this kernel has no IOMMU support yet (VT-d is a later cut), so the |
| 7 | * sound device's DMA is not confined; it is the only device writing to |
| 8 | * memory here (four 100 ms sample buffers and two command pages, read by the |
| 9 | * device, plus 8-byte status words it writes), and it is reset after the |
| 10 | * chirp. */ |
| 11 | #include "x86.h" |
| 12 | #include "../core/ck_internal.h" |
| 13 | #include "../dev/virtio_snd.h" |
| 14 | #include "../mm/frames.h" |
| 15 | #include "../ui/allen_ui.h" |
| 16 | #include "../ui/boot_show.h" |
| 17 | #include "aienos_virtio_pci.h" |
| 18 | #include "ck.h" |
| 19 | |
| 20 | static uint64_t hz; |
| 21 | static uint64_t counter(void) { return ck_rdtsc(); } |
| 22 | |
| 23 | static struct { |
| 24 | volatile uint8_t *win[4]; |
| 25 | } vs; |
| 26 | |
| 27 | static uint32_t o_read(void *ctx, vnet_window w, uint32_t off, unsigned width) |
| 28 | { |
| 29 | (void)ctx; |
| 30 | volatile uint8_t *p = vs.win[w] + off; |
| 31 | if (width == 1) |
| 32 | return *p; |
| 33 | if (width == 2) |
| 34 | return *(volatile uint16_t *)p; |
| 35 | return *(volatile uint32_t *)p; |
| 36 | } |
| 37 | static void o_write(void *ctx, vnet_window w, uint32_t off, unsigned width, uint32_t v) |
| 38 | { |
| 39 | (void)ctx; |
| 40 | volatile uint8_t *p = vs.win[w] + off; |
| 41 | if (width == 1) |
| 42 | *p = (uint8_t)v; |
| 43 | else if (width == 2) |
| 44 | *(volatile uint16_t *)p = (uint16_t)v; |
| 45 | else |
| 46 | *(volatile uint32_t *)p = v; |
| 47 | __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 48 | } |
| 49 | static void o_notify(void *ctx, uint32_t off, uint16_t q) |
| 50 | { |
| 51 | (void)ctx; |
| 52 | __atomic_thread_fence(__ATOMIC_SEQ_CST); /* ring writes visible before the doorbell */ |
| 53 | *(volatile uint16_t *)(vs.win[VNET_WIN_NOTIFY] + off) = q; |
| 54 | } |
| 55 | |
| 56 | /* The BAR's address (64-bit aware); 0 for an I/O or unassigned BAR. */ |
| 57 | static uint64_t bar_addr(volatile uint8_t *cfg, unsigned bar) |
| 58 | { |
| 59 | if (bar > 5) |
| 60 | return 0; |
| 61 | uint32_t lo = *(volatile uint32_t *)(cfg + 0x10 + 4 * bar); |
| 62 | if (lo & 1) |
| 63 | return 0; |
| 64 | uint64_t a = lo & ~0xfull; |
| 65 | if ((lo & 6) == 4 && bar < 5) |
| 66 | a |= (uint64_t)*(volatile uint32_t *)(cfg + 0x14 + 4 * bar) << 32; |
| 67 | return a; |
| 68 | } |
| 69 | |
| 70 | static vq_dev snd_dev; |
| 71 | |
| 72 | /* virtio-sound (1af4:1059) behind virtio_q: 0 when the stream is ready. */ |
| 73 | static int sound_open(struct ck_frames *fr) |
| 74 | { |
| 75 | uint8_t bdf[3]; |
| 76 | volatile uint8_t *cfg = ck_x86_pci_find(0x1af4u, 0x1059u, bdf); |
| 77 | if (!cfg) { |
| 78 | ck_puts("virtio_snd: none (no 1af4:1059 function)\n"); |
| 79 | return -1; |
| 80 | } |
| 81 | static uint8_t img[256]; |
| 82 | for (unsigned off = 0; off < sizeof img; off += 4) { |
| 83 | uint32_t v = *(volatile uint32_t *)(cfg + off); |
| 84 | memcpy(img + off, &v, 4); |
| 85 | } |
| 86 | virtio_pci_caps caps; |
| 87 | virtio_pci_err pe = virtio_pci_parse_caps(img, sizeof img, &caps); |
| 88 | if (pe != VIRTIO_PCI_OK) { |
| 89 | ck_printf("virtio_snd: %02x:%02x.%u not used (capabilities: %s)\n", bdf[0], bdf[1], bdf[2], |
| 90 | virtio_pci_err_name(pe)); |
| 91 | return -2; |
| 92 | } |
| 93 | const virtio_pci_region *r[4] = {&caps.common_cfg, &caps.notify_cfg, &caps.isr_cfg, &caps.device_cfg}; |
| 94 | for (int i = 0; i < 4; i++) { |
| 95 | uint64_t a = r[i]->present ? bar_addr(cfg, r[i]->bar) : 0; |
| 96 | if (!a || ck_x86_map_mmio(a + r[i]->offset, r[i]->length)) { |
| 97 | ck_printf("virtio_snd: not used (capability window %d not mappable)\n", i); |
| 98 | return -3; |
| 99 | } |
| 100 | vs.win[i] = (volatile uint8_t *)(uintptr_t)(a + r[i]->offset); |
| 101 | } |
| 102 | size_t len = vq_mem_bytes(3) + VSND_EXTRA_BYTES; |
| 103 | uint64_t pa; |
| 104 | if (ck_frames_alloc(fr, (len + 4095u) / 4096u, 4096u, &pa)) { |
| 105 | ck_puts("virtio_snd: not used (no memory for its buffers)\n"); |
| 106 | return -4; |
| 107 | } |
| 108 | uint8_t *mem = (uint8_t *)(uintptr_t)pa; |
| 109 | memset(mem, 0, len); |
| 110 | *(volatile uint16_t *)(cfg + 4) |= 0x6; /* memory decode + bus master */ |
| 111 | vnet_ops ops = {0, o_read, o_write, o_notify}; |
| 112 | int e = vq_init(&snd_dev, &ops, &caps, mem, pa, vq_mem_bytes(3), 3, 0); |
| 113 | if (e) { |
| 114 | ck_printf("virtio_snd: init FAIL (%s)\n", vq_err_name(e)); |
| 115 | *(volatile uint16_t *)(cfg + 4) &= (uint16_t)~0x4; |
| 116 | return -5; |
| 117 | } |
| 118 | ck_printf("virtio_snd: %02x:%02x.%u bus master on, DMA not confined (no IOMMU in this kernel yet)\n", bdf[0], |
| 119 | bdf[1], bdf[2]); |
| 120 | return ck_vsnd_attach(&snd_dev, mem + vq_mem_bytes(3), pa + vq_mem_bytes(3)); |
| 121 | } |
| 122 | |
| 123 | #define BOOT_X86_MAX_W 1920u |
| 124 | #define BOOT_X86_MAX_H 1080u |
| 125 | |
| 126 | void ck_x86_boot_show(volatile uint32_t *fb, uint32_t w, uint32_t h, uint32_t pitch, uint32_t format, |
| 127 | struct ck_frames *fr, uint64_t tsc_hz) |
| 128 | { |
| 129 | if (!fb || !tsc_hz) { |
| 130 | ck_puts("boot-show: skipped (no screen or no clock)\n"); |
| 131 | return; |
| 132 | } |
| 133 | hz = tsc_hz; |
| 134 | w = w < BOOT_X86_MAX_W ? w : BOOT_X86_MAX_W; |
| 135 | h = h < BOOT_X86_MAX_H ? h : BOOT_X86_MAX_H; |
| 136 | size_t need = ck_allen_ui_mem_bytes(w, h); |
| 137 | uint64_t pa; |
| 138 | if (ck_frames_alloc(fr, (need + 4095u) / 4096u, 4096u, &pa)) { |
| 139 | ck_printf("boot-show: skipped (no %zu KiB of drawing memory for %ux%u)\n", need >> 10, w, h); |
| 140 | return; |
| 141 | } |
| 142 | int snd = sound_open(fr); |
| 143 | ck_x86_console_fb_pause(); |
| 144 | if (ck_boot_show_start(fb, w, h, pitch, format, (void *)(uintptr_t)pa, need, hz, counter(), counter, 0, 0)) { |
| 145 | ck_x86_console_fb_resume(); |
| 146 | ck_printf("boot-show: skipped (screen %ux%u pitch %u format %u refused)\n", w, h, pitch, format); |
| 147 | ck_frames_free(fr, pa, (need + 4095u) / 4096u); |
| 148 | return; |
| 149 | } |
| 150 | ck_printf("boot-show: the night starts on %ux%u (gop)\n", w, h); |
| 151 | int playing = 1, sounding = snd == 0; |
| 152 | while (playing || sounding) { |
| 153 | uint64_t now = counter(); |
| 154 | if (!playing && ck_boot_show_ms(now) > CK_BOOT_SHOW_MS + 2000u) { |
| 155 | ck_puts("sound: the speakers stopped answering; chirp abandoned 2 s after the night\n"); |
| 156 | break; |
| 157 | } |
| 158 | if (playing) |
| 159 | playing = ck_boot_show_tick(now); |
| 160 | if (sounding) |
| 161 | sounding = ck_vsnd_step(ck_boot_show_ms(now), ck_boot_show_audio_frame(now)); |
| 162 | } |
| 163 | if (snd == 0) |
| 164 | vq_reset(&snd_dev); /* the device stops touching memory before the buffers go back */ |
| 165 | ck_x86_console_fb_resume(); |
| 166 | struct ck_boot_show_stats st; |
| 167 | ck_boot_show_stats(&st); |
| 168 | ck_printf(CK_BOOT_SHOW_LINE, CK_BOOT_SHOW_ARGS(st, w, h, "gop")); |
| 169 | if (snd) |
| 170 | ck_puts("sound: none for the chirp (no virtio-sound); the night played silent\n"); |
| 171 | ck_frames_free(fr, pa, (need + 4095u) / 4096u); |
| 172 | } |