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 / console_x86.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* console_x86.c -- ck_puts/ck_printf for AIENOS-KERNEL-0. |
| 18 | * |
| 19 | * Three sinks. COM1 (I/O port 0x3f8) only when a 16550 answers there (the |
| 20 | * scratch register keeps a written value): QEMU has one, the 2017 iMac has |
| 21 | * none, so nothing is sent there on the iMac. The GOP framebuffer |
| 22 | * (core/fbcon.c) once the kernel mapped it and called ck_x86_console_fb_on. |
| 23 | * And a RAM copy of the first CK_X86_LOG_BYTES (64 KiB, console_log.h) written, replayed onto the |
| 24 | * screen when it comes on, so the screen also shows the lines from before |
| 25 | * ExitBootServices (UEFI ConOut is not used: on a Mac it is invisible unless |
| 26 | * the firmware is switched to text mode); report_x86.c also sends that copy |
| 27 | * over the network. '\n' goes to COM1 as CR LF. A fault |
| 28 | * while drawing turns the screen off for good. While the startup picture |
| 29 | * plays (ck_x86_console_fb_hold) the screen is known but the text stays off |
| 30 | * it; ck_x86_console_fb_show brings the text back (a panic always does). */ |
| 31 | #include "../core/ck_internal.h" |
| 32 | #include "../core/fbcon.h" |
| 33 | #include "x86.h" |
| 34 | #include "console_log.h" |
| 35 | |
| 36 | #define COM1 0x3f8 |
| 37 | |
| 38 | static int com1_on; |
| 39 | static struct ck_fbcon fb; |
| 40 | static int fb_on, fb_busy; |
| 41 | static int fb_held; /* the screen is known but the text stays off it (the startup picture) */ |
| 42 | static void (*held_tick)(void); |
| 43 | static int tick_busy; |
| 44 | static struct ck_x86_log klog; |
| 45 | |
| 46 | int ck_x86_com1_probe(void) |
| 47 | { |
| 48 | ck_outb(COM1 + 7, 0x5a); |
| 49 | if (ck_inb(COM1 + 7) != 0x5a) |
| 50 | return com1_on = 0; |
| 51 | ck_outb(COM1 + 7, 0xa5); |
| 52 | com1_on = ck_inb(COM1 + 7) == 0xa5; |
| 53 | return com1_on; |
| 54 | } |
| 55 | |
| 56 | static void com1_putc(char c) |
| 57 | { |
| 58 | /* LSR bit 5: transmit holding register empty. Bounded: never hangs. */ |
| 59 | for (unsigned i = 0; i < 100000 && !(ck_inb(COM1 + 5) & 0x20); i++) |
| 60 | ck_pause(); |
| 61 | ck_outb(COM1, (uint8_t)c); |
| 62 | } |
| 63 | |
| 64 | static void fb_write(const char *s, size_t n) |
| 65 | { |
| 66 | if (!fb_on) |
| 67 | return; |
| 68 | if (fb_busy) { |
| 69 | fb_on = 0; /* re-entered from a fault inside the drawing: screen off */ |
| 70 | return; |
| 71 | } |
| 72 | fb_busy = 1; |
| 73 | ck_fbcon_write(&fb, s, n); |
| 74 | fb_busy = 0; |
| 75 | } |
| 76 | |
| 77 | static void write_n(const char *s, size_t n) |
| 78 | { |
| 79 | ck_x86_log_append(&klog, s, n); |
| 80 | fb_write(s, n); |
| 81 | if (fb_held && held_tick && !tick_busy) { |
| 82 | tick_busy = 1; /* a fault while drawing prints, and must not draw again */ |
| 83 | held_tick(); |
| 84 | tick_busy = 0; |
| 85 | } |
| 86 | if (!com1_on) |
| 87 | return; |
| 88 | for (size_t i = 0; i < n; i++) { |
| 89 | if (s[i] == '\n') |
| 90 | com1_putc('\r'); |
| 91 | com1_putc(s[i]); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* The boot sequence owns the screen while it plays (x86/boot_x86.c): the |
| 96 | * console stops drawing on it, then starts again on a cleared screen. */ |
| 97 | static int fb_paused; |
| 98 | void ck_x86_console_fb_pause(void) |
| 99 | { |
| 100 | if (fb_on) |
| 101 | fb_paused = 1, fb_on = 0; |
| 102 | } |
| 103 | void ck_x86_console_fb_resume(void) |
| 104 | { |
| 105 | if (!fb_paused) |
| 106 | return; |
| 107 | fb_paused = 0; |
| 108 | ck_fbcon_clear(&fb); |
| 109 | fb_on = 1; |
| 110 | } |
| 111 | |
| 112 | int ck_x86_console_fb_on(volatile void *base, uint32_t width, uint32_t height, uint32_t pitch, |
| 113 | uint32_t format, uint32_t *scale, uint32_t *cols, uint32_t *rows) |
| 114 | { |
| 115 | if (held_tick) { |
| 116 | if (ck_fbcon_attach(&fb, base, width, height, pitch, format) != 0) |
| 117 | return -1; |
| 118 | fb_held = 1; |
| 119 | } else { |
| 120 | if (ck_fbcon_setup(&fb, base, width, height, pitch, format) != 0) |
| 121 | return -1; |
| 122 | fb_on = 1; |
| 123 | fb_write(klog.buf, klog.len); |
| 124 | } |
| 125 | *scale = fb.scale; |
| 126 | *cols = fb.cols; |
| 127 | *rows = fb.rows; |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | int ck_x86_console_fb_get(volatile uint32_t **px, uint32_t *width, uint32_t *height, uint32_t *pitch, |
| 132 | uint32_t *format) |
| 133 | { |
| 134 | if (!(fb_on || fb_held) || fb_busy) |
| 135 | return -1; |
| 136 | *px = fb.px; |
| 137 | *width = fb.width; |
| 138 | *height = fb.height; |
| 139 | *pitch = fb.pitch; |
| 140 | *format = fb.format; |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | void ck_x86_console_fb_off(void) |
| 145 | { |
| 146 | fb_on = fb_held = 0; /* text keeps going to the RAM copy and COM1 */ |
| 147 | held_tick = 0; |
| 148 | } |
| 149 | |
| 150 | void ck_x86_console_fb_hold(void (*tick)(void)) { held_tick = tick; } |
| 151 | |
| 152 | void ck_x86_console_fb_show(void) |
| 153 | { |
| 154 | if (!fb_held || fb_busy) |
| 155 | return; |
| 156 | fb_held = 0; |
| 157 | held_tick = 0; |
| 158 | fb_on = 1; |
| 159 | fb_busy = 1; |
| 160 | ck_fbcon_clear(&fb); |
| 161 | fb_busy = 0; |
| 162 | fb_write(klog.buf, klog.len); |
| 163 | } |
| 164 | |
| 165 | const char *ck_x86_console_log(size_t *len) |
| 166 | { |
| 167 | *len = klog.len; |
| 168 | return klog.buf; |
| 169 | } |
| 170 | |
| 171 | size_t ck_x86_console_log_dropped(void) |
| 172 | { |
| 173 | return klog.dropped; |
| 174 | } |
| 175 | |
| 176 | void ck_puts(const char *s) |
| 177 | { |
| 178 | write_n(s, strlen(s)); |
| 179 | } |
| 180 | |
| 181 | struct pbuf { |
| 182 | char b[128]; |
| 183 | size_t n; |
| 184 | }; |
| 185 | |
| 186 | static void pbuf_emit(void *ctx, char c) |
| 187 | { |
| 188 | struct pbuf *p = ctx; |
| 189 | p->b[p->n++] = c; |
| 190 | if (p->n == sizeof p->b) { |
| 191 | write_n(p->b, p->n); |
| 192 | p->n = 0; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void ck_vprintf(const char *fmt, va_list ap) |
| 197 | { |
| 198 | struct pbuf p; |
| 199 | p.n = 0; |
| 200 | ck_vformat(pbuf_emit, &p, fmt, ap); |
| 201 | if (p.n) |
| 202 | write_n(p.b, p.n); |
| 203 | } |
| 204 | |
| 205 | void ck_printf(const char *fmt, ...) |
| 206 | { |
| 207 | va_list ap; |
| 208 | va_start(ap, fmt); |
| 209 | ck_vprintf(fmt, ap); |
| 210 | va_end(ap); |
| 211 | } |