Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/x86/courier_x86.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / x86 / courier_x86.c` 132 lines ยท 4059 bytes Raw Blame History 1 /* courier_x86.c -- Allen's courier on the second serial port (COM2, I/O 2 * 0x2f8), a 16550 like the console's COM1. Polled from the hold loop, no 3 * interrupt: LSR bit 0 (Data Ready) says a byte waits in RBR, LSR bit 5 4 * (THRE) that the next may be written (PC16550D datasheet, Table II and 5 * section 8.5; recorded with docs-read). 115200 8N1, FIFOs on. 6 * 7 * The wire speaks lines. "HELLO " is a call: whoever hears one answers 8 * "HI " and is ready; "HI" is the answer and is never answered, so the 9 * exchange always ends. This side calls once when the port is set up; the far 10 * side calls until it hears either. It does not matter who was up first, nor 11 * that setting up the port clears the FIFOs and so eats whatever the far side 12 * sent before (in QEMU the far side is a relay on the host joined to -serial 13 * unix:...; on hardware with no second port, or nobody on it, Allen stays on 14 * his own). Lines longer than the buffer are dropped whole rather than cut. */ 15 #include "x86.h" 16 #include "../svc/courier.h" 17 18 #define COM2 0x2f8 19 #define LINE_MAX 1024u 20 21 static int on, hello; 22 static char in[LINE_MAX]; 23 static unsigned in_n; 24 static int in_drop; /* the current line overflowed: drop it to its newline */ 25 static char line[LINE_MAX]; /* a whole line waiting for take() */ 26 static unsigned line_n; 27 static int line_ready; 28 29 static int probe(void) 30 { 31 ck_outb(COM2 + 7, 0x5a); 32 if (ck_inb(COM2 + 7) != 0x5a) 33 return 0; 34 ck_outb(COM2 + 7, 0xa5); 35 return ck_inb(COM2 + 7) == 0xa5; 36 } 37 38 static void putc_(char c) 39 { 40 for (unsigned i = 0; i < 100000 && !(ck_inb(COM2 + 5) & 0x20); i++) 41 ck_pause(); 42 ck_outb(COM2, (uint8_t)c); 43 } 44 45 static int send(const char *s, unsigned n) 46 { 47 if (!on) 48 return -1; 49 for (unsigned i = 0; i < n; i++) 50 putc_(s[i] == '\n' ? ' ' : s[i]); 51 putc_('\n'); 52 return 0; 53 } 54 55 static int ready(void) { return on && hello; } 56 57 /* Reads what waits in the FIFO; a complete line is kept for take(). Reading 58 * stops while a line waits, so the next one stays in the port (the far side 59 * waits for room) rather than being dropped behind it. */ 60 static void pump(void) 61 { 62 while (!line_ready && (ck_inb(COM2 + 5) & 0x01)) { 63 char c = (char)ck_inb(COM2); 64 if (c == '\r') 65 continue; 66 if (c != '\n') { 67 if (in_n + 1 < LINE_MAX) 68 in[in_n++] = c; 69 else 70 in_drop = 1; 71 continue; 72 } 73 in[in_n] = 0; 74 if (!in_drop && in_n) { 75 if (in[0] == 'H' && in[1] == 'E' && in[2] == 'L' && in[3] == 'L' && in[4] == 'O' && 76 (in[5] == 0 || in[5] == ' ')) { 77 hello = 1; 78 send("HI allen", 8); 79 } else if (in[0] == 'H' && in[1] == 'I' && (in[2] == 0 || in[2] == ' ')) { 80 hello = 1; 81 } else if (!line_ready) { 82 for (unsigned i = 0; i <= in_n; i++) 83 line[i] = in[i]; 84 line_n = in_n; 85 line_ready = 1; 86 } 87 } 88 in_n = 0; 89 in_drop = 0; 90 } 91 } 92 93 static int take(char *out, unsigned cap) 94 { 95 if (!on) 96 return 0; 97 pump(); 98 if (!line_ready || !out || !cap) 99 return 0; 100 unsigned n = line_n < cap - 1 ? line_n : cap - 1; 101 for (unsigned i = 0; i < n; i++) 102 out[i] = line[i]; 103 out[n] = 0; 104 line_ready = 0; 105 return 1; 106 } 107 108 static const ck_courier courier = {ready, send, take}; 109 110 const ck_courier *ck_x86_courier_init(void) 111 { 112 if (!probe()) 113 return 0; 114 ck_outb(COM2 + 1, 0x00); /* no interrupts: polled */ 115 ck_outb(COM2 + 3, 0x80); /* DLAB on: divisor */ 116 ck_outb(COM2 + 0, 0x01); /* 115200 */ 117 ck_outb(COM2 + 1, 0x00); 118 ck_outb(COM2 + 3, 0x03); /* 8N1, DLAB off */ 119 ck_outb(COM2 + 2, 0xc7); /* FIFOs on and cleared, 14-byte trigger */ 120 ck_outb(COM2 + 4, 0x0b); /* DTR, RTS, OUT2 */ 121 on = 1; 122 send("HELLO allen", 11); 123 return &courier; 124 } 125 126 int ck_x86_courier_hello(void) 127 { 128 if (!on) 129 return 0; 130 pump(); 131 return hello; 132 }