From eb36d836458803b5dcfa668fef487c52e5c45cdc Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 7 Aug 2022 18:55:59 +0200 Subject: kernel: ring_to_virt --- src/kernel/arch/amd64/driver/ps2.c | 15 +++++---------- src/kernel/arch/amd64/driver/serial.c | 17 ++++++----------- src/kernel/ring.c | 11 +++++++++++ src/kernel/ring.h | 5 +++++ 4 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 src/kernel/ring.c create mode 100644 src/kernel/ring.h (limited to 'src') diff --git a/src/kernel/arch/amd64/driver/ps2.c b/src/kernel/arch/amd64/driver/ps2.c index be20c66..e3aaef8 100644 --- a/src/kernel/arch/amd64/driver/ps2.c +++ b/src/kernel/arch/amd64/driver/ps2.c @@ -1,15 +1,12 @@ #include #include #include -#include #include +#include #include -#include -#include -#define BACKLOG_CAPACITY 64 -static volatile uint8_t backlog_buf[BACKLOG_CAPACITY]; -static volatile ring_t backlog = {(void*)backlog_buf, BACKLOG_CAPACITY, 0, 0}; +static volatile uint8_t backlog_buf[64]; +static volatile ring_t backlog = {(void*)backlog_buf, sizeof backlog_buf, 0, 0}; static void accept(struct vfs_request *req); static bool is_ready(struct vfs_backend *self); @@ -30,7 +27,6 @@ void ps2_irq(void) { static void accept(struct vfs_request *req) { // when you fix something here go also fix it in the COM1 driver - static uint8_t buf[32]; // pretty damn stupid int ret; bool valid; switch (req->type) { @@ -43,9 +39,8 @@ static void accept(struct vfs_request *req) { // nothing to read blocked_on = req; } else if (req->caller) { - ret = clamp(0, req->output.len, sizeof buf); - ret = ring_get((void*)&backlog, buf, ret); - virt_cpy_to(req->caller->pages, req->output.buf, buf, ret); + if (ret < 0) ret = 0; + ret = ring_to_virt((void*)&backlog, req->caller->pages, req->output.buf, req->output.len); vfsreq_finish_short(req, ret); } else { vfsreq_finish_short(req, -1); diff --git a/src/kernel/arch/amd64/driver/serial.c b/src/kernel/arch/amd64/driver/serial.c index 93202c2..1401642 100644 --- a/src/kernel/arch/amd64/driver/serial.c +++ b/src/kernel/arch/amd64/driver/serial.c @@ -2,14 +2,12 @@ #include #include #include +#include #include -#include -#include #include -#define BACKLOG_CAPACITY 64 -static volatile uint8_t backlog_buf[BACKLOG_CAPACITY]; -static volatile ring_t backlog = {(void*)backlog_buf, BACKLOG_CAPACITY, 0, 0}; +static volatile uint8_t backlog_buf[64]; +static volatile ring_t backlog = {(void*)backlog_buf, sizeof backlog_buf, 0, 0}; static const int COM1 = 0x3f8; @@ -67,7 +65,6 @@ void serial_write(const char *buf, size_t len) { static void accept(struct vfs_request *req) { - static char buf[32]; int ret; bool valid; switch (req->type) { @@ -80,13 +77,11 @@ static void accept(struct vfs_request *req) { /* nothing to read, join queue */ assert(!req->postqueue_next); struct vfs_request **slot = &blocked_on; - while (*slot) - slot = &(*slot)->postqueue_next; + while (*slot) slot = &(*slot)->postqueue_next; *slot = req; } else if (req->caller) { - ret = clamp(0, req->output.len, sizeof buf); - ret = ring_get((void*)&backlog, buf, ret); - virt_cpy_to(req->caller->pages, req->output.buf, buf, ret); + if (ret < 0) ret = 0; + ret = ring_to_virt((void*)&backlog, req->caller->pages, req->output.buf, req->output.len); vfsreq_finish_short(req, ret); } else { vfsreq_finish_short(req, -1); diff --git a/src/kernel/ring.c b/src/kernel/ring.c new file mode 100644 index 0000000..e25024a --- /dev/null +++ b/src/kernel/ring.c @@ -0,0 +1,11 @@ +#include +#include + +size_t ring_to_virt(ring_t *r, struct pagedir *pages, void __user *ubuf, size_t max) { + char tmp[32]; + if (max > sizeof tmp) max = sizeof tmp; + max = ring_get(r, tmp, max); + // TODO decide how write errors should be handled + virt_cpy_to(pages, ubuf, tmp, max); + return max; +} diff --git a/src/kernel/ring.h b/src/kernel/ring.h new file mode 100644 index 0000000..f5f9679 --- /dev/null +++ b/src/kernel/ring.h @@ -0,0 +1,5 @@ +#pragma once +#include +struct pagedir; + +size_t ring_to_virt(ring_t *r, struct pagedir *pages, void __user *ubuf, size_t max); -- cgit v1.2.3