From 642b5fb0007b64c77d186fcb018d571152ee1d47 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Mon, 14 Aug 2023 18:51:07 +0200 Subject: reorganization: first steps --- src/cmd/init/driver/driver.h | 8 +++ src/cmd/init/driver/initctl.c | 44 ++++++++++++++ src/cmd/init/driver/ps2.c | 129 +++++++++++++++++++++++++++++++++++++++++ src/cmd/init/driver/termcook.c | 103 ++++++++++++++++++++++++++++++++ 4 files changed, 284 insertions(+) create mode 100644 src/cmd/init/driver/driver.h create mode 100644 src/cmd/init/driver/initctl.c create mode 100644 src/cmd/init/driver/ps2.c create mode 100644 src/cmd/init/driver/termcook.c (limited to 'src/cmd/init/driver') diff --git a/src/cmd/init/driver/driver.h b/src/cmd/init/driver/driver.h new file mode 100644 index 0000000..98c18f1 --- /dev/null +++ b/src/cmd/init/driver/driver.h @@ -0,0 +1,8 @@ +#pragma once +#include + +void initctl_drv(hid_t killswitch); +void ps2_drv(void); +void tmpfs_drv(void); + +void termcook(void); diff --git a/src/cmd/init/driver/initctl.c b/src/cmd/init/driver/initctl.c new file mode 100644 index 0000000..fed71b7 --- /dev/null +++ b/src/cmd/init/driver/initctl.c @@ -0,0 +1,44 @@ +#include "driver.h" +#include +#include +#include +#include +#include +#include + +void initctl_drv(hid_t killswitch) { + struct ufs_request res; + char buf[64]; + const size_t buflen = sizeof buf; + while (!c0_fs_wait(buf, buflen, &res)) { + switch (res.op) { + case VFSOP_OPEN: + c0_fs_respond(NULL, res.len == 0 ? 0 : -1, 0); + break; + case VFSOP_WRITE: + /* null terminate */ + if (res.len > buflen - 1) + res.len = buflen - 1; + buf[res.len] = '\0'; + /* cut at first whitespace */ + for (size_t i = 0; buf[i]; i++) { + if (isspace(buf[i])) { + buf[i] = '\0'; + break; + } + } + if (!strcmp(buf, "halt")) { + _sys_write(killswitch, "halt", 4, 0, 0); + } + if (!strcmp(buf, "intr")) { + _sys_write(killswitch, "intr", 4, 0, 0); + } + c0_fs_respond(NULL, res.len, 0); + break; + default: + c0_fs_respond(NULL, -ENOSYS, 0); + break; + } + } + exit(1); +} diff --git a/src/cmd/init/driver/ps2.c b/src/cmd/init/driver/ps2.c new file mode 100644 index 0000000..5470249 --- /dev/null +++ b/src/cmd/init/driver/ps2.c @@ -0,0 +1,129 @@ +#include "driver.h" +#include +#include +#include +#include +#include +#include +#include +#include + + +static const char keymap_lower[] = { + '\0', '\0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t', + 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\r', '\0', 'a', 's', + 'd', 'f', 'g', 'h', 'j', 'k', 'l', '\0', '\'', '`', '\0', '\\', 'z', 'x', 'c', 'v', + 'b', 'n', 'm', ',', '.', '/', '\0', '*', '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '8', '9', '-', '4', '5', '6', '+', '1', + '2', '3', '0', '.', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', +}; + +static const char keymap_upper[] = { + '\0', '\0', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t', + 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\r', '\0', 'A', 'S', + 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', '\0', '|', 'Z', 'X', 'C', 'V', + 'B', 'N', 'M', '<', '>', '?', '\0', '*', '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '8', '9', '-', '4', '5', '6', '+', '1', + '2', '3', '0', '.', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', +}; + + +static volatile uint8_t backlog_buf[16]; +static volatile ring_t backlog = {(void*)backlog_buf, sizeof backlog_buf, 0, 0}; + +static hid_t fd; + +static bool keys[0x80] = {0}; + +static void parse_scancode(uint8_t s) { + bool ctrl = keys[0x1D]; + bool shift = keys[0x2A] || keys[0x36]; + bool down = !(s & 0x80); + char c; + s &= 0x7f; + keys[s] = down; + + c = shift ? keymap_upper[s] : keymap_lower[s]; + if (ctrl && keymap_upper[s] >= 'A' && keymap_upper[s] <= 'Z') + c = keymap_upper[s] - 'A' + 1; + if (down && c) ring_put1b((void*)&backlog, c); +} + + +/** Is a thread waiting for /kdev/ps2/kb? */ +static volatile bool blocked = false; +/* for use in read_thread */ +static hid_t rt_reqh; +static size_t rt_cap; + +static void read_thread(void *unused) { + char buf[512]; + (void)unused; + + assert(blocked); + while (ring_used((void*)&backlog) == 0) { + /* read raw input until we have something to output */ + int len = _sys_read(fd, buf, sizeof buf, 0); + if (len == 0) break; + for (int i = 0; i < len; i++) + parse_scancode(buf[i]); + } + if (ring_used((void*)&backlog) > 0) { + int ret = ring_get((void*)&backlog, buf, rt_cap); + _sys_fs_respond(rt_reqh, buf, ret, 0); + } else { + _sys_fs_respond(rt_reqh, 0, -EGENERIC, 0); + } + blocked = false; +} + +static void fs_loop(void) { + static char buf[512]; + int ret; + for (;;) { + struct ufs_request res; + hid_t reqh = _sys_fs_wait(buf, sizeof buf, &res); + if (reqh < 0) return; + + switch (res.op) { + case VFSOP_OPEN: + if (res.len == 0) { + _sys_fs_respond(reqh, NULL, 1, 0); + } else { + _sys_fs_respond(reqh, NULL, -ENOENT, 0); + } + break; + + case VFSOP_READ: + if (blocked) { + _sys_fs_respond(reqh, NULL, -EAGAIN, 0); + break; + } else if (ring_used((void*)&backlog) > 0) { + ret = ring_get((void*)&backlog, buf, res.capacity); + _sys_fs_respond(reqh, buf, ret, 0); + } else { + blocked = true; + rt_reqh = reqh; + rt_cap = res.capacity; + thread_create(0, read_thread, 0); + } + break; + + default: + _sys_fs_respond(reqh, NULL, -1, 0); + break; + } + } +} + +void ps2_drv(void) { + fd = _sys_open("/kdev/ps2/kb", 12, 0); + if (fd < 0) exit(1); + + fs_loop(); + exit(0); +} diff --git a/src/cmd/init/driver/termcook.c b/src/cmd/init/driver/termcook.c new file mode 100644 index 0000000..a76f3a8 --- /dev/null +++ b/src/cmd/init/driver/termcook.c @@ -0,0 +1,103 @@ +#include "driver.h" +#include +#include +#include +#include + +enum tstate { + Normal, + Esc, + CSI, +}; + +static void w_output(hid_t output, const char *buf, size_t len) { + size_t pos = 0; + while (pos < len) { + int ret = _sys_write(output, buf + pos, len - pos, pos, 0); + if (ret < 0) break; + pos += ret; + } +} + +static void line_editor(hid_t input, hid_t output) { + char readbuf[16], linebuf[256]; + size_t linepos = 0; + enum tstate state = Normal; + for (;;) { + int readlen = _sys_read(input, readbuf, sizeof readbuf, -1); + if (readlen < 0) return; + for (int i = 0; i < readlen; i++) { + char c = readbuf[i]; + switch (state) { + case Normal: + switch (c) { + case '\b': + case 0x7f: + if (linepos != 0) { + printf("\b \b"); + linepos--; + } + break; + case 3: /* C-c */ + _sys_exit(1); + case 4: /* EOT, C-d */ + if (linepos > 0) { + w_output(output, linebuf, linepos); + linepos = 0; + } else { + _sys_write(output, NULL, 0, 0, 0); /* EOF */ + } + break; + case '\n': + case '\r': + printf("\n"); + if (linepos < sizeof linebuf) + linebuf[linepos++] = '\n'; + w_output(output, linebuf, linepos); + linepos = 0; + break; + case '\e': + state = Esc; + break; + case '\t': + break; + default: + if (linepos < sizeof linebuf) { + linebuf[linepos++] = c; + printf("%c", c); + } + break; + } + break; + case Esc: + if (c == '[') state = CSI; + else state = Normal; + break; + case CSI: + if (0x40 <= c && c <= 0x7E) state = Normal; + break; + } + } + } +} + +void termcook(void) { + hid_t stdin_pipe[2] = {-1, -1}; + if (_sys_pipe(stdin_pipe, 0) < 0) + return; + + if (!fork()) { + /* the caller continues in a child process, + * so it can be killed when the line editor quits */ + _sys_dup(stdin_pipe[0], 0, 0); + close(stdin_pipe[0]); + close(stdin_pipe[1]); + return; + } + if (!fork()) { + close(stdin_pipe[0]); + line_editor(0, stdin_pipe[1]); + exit(0); + } + exit(_sys_await()); +} -- cgit v1.2.3