From c24a9376b4a048a3f9408df2d47eaad367459bd7 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 25 Jan 2023 20:56:36 +0100 Subject: kernel: consolidate some header files --- src/kernel/arch/amd64/32/gdt.c | 2 +- src/kernel/arch/amd64/32/gdt.h | 18 ---- src/kernel/arch/amd64/boot.c | 1 - src/kernel/arch/amd64/boot.h | 19 ++++ src/kernel/arch/amd64/driver/pata.c | 1 - src/kernel/arch/amd64/driver/ps2.c | 1 - src/kernel/arch/amd64/driver/rtl8139.c | 1 - src/kernel/arch/amd64/driver/serial.c | 2 - src/kernel/arch/amd64/driver/util.c | 8 +- src/kernel/arch/amd64/driver/util.h | 2 + src/kernel/arch/amd64/driver/video.c | 1 - src/kernel/arch/amd64/interrupts/idt.c | 3 +- src/kernel/arch/amd64/interrupts/idt.h | 3 - src/kernel/mem/virt.c | 160 --------------------------------- src/kernel/mem/virt.h | 12 --- src/kernel/pcpy.c | 159 ++++++++++++++++++++++++++++++++ src/kernel/pipe.c | 1 - src/kernel/proc.c | 1 - src/kernel/proc.h | 8 ++ src/kernel/ring.c | 9 -- src/kernel/ring.h | 6 -- src/kernel/syscalls.c | 1 - src/kernel/vfs/procfs.c | 1 - src/kernel/vfs/request.c | 1 - 24 files changed, 197 insertions(+), 224 deletions(-) delete mode 100644 src/kernel/arch/amd64/32/gdt.h delete mode 100644 src/kernel/arch/amd64/interrupts/idt.h delete mode 100644 src/kernel/mem/virt.c delete mode 100644 src/kernel/mem/virt.h create mode 100644 src/kernel/pcpy.c delete mode 100644 src/kernel/ring.h diff --git a/src/kernel/arch/amd64/32/gdt.c b/src/kernel/arch/amd64/32/gdt.c index 33fcf7b..bd5fa48 100644 --- a/src/kernel/arch/amd64/32/gdt.c +++ b/src/kernel/arch/amd64/32/gdt.c @@ -1,5 +1,5 @@ -#include #include +#include #include #include diff --git a/src/kernel/arch/amd64/32/gdt.h b/src/kernel/arch/amd64/32/gdt.h deleted file mode 100644 index 4e08c88..0000000 --- a/src/kernel/arch/amd64/32/gdt.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -enum { - SEG_null, - // order dictated by SYSENTER - SEG_r0code, - SEG_r0data, - SEG_r3code, - SEG_r3data, - SEG_TSS, - SEG_TSS2, - - SEG_end -}; - -void gdt_init(void); - -extern struct lgdt_arg lgdt_arg; // used by amd64/32/boot.s diff --git a/src/kernel/arch/amd64/boot.c b/src/kernel/arch/amd64/boot.c index d78d4f6..32d0452 100644 --- a/src/kernel/arch/amd64/boot.c +++ b/src/kernel/arch/amd64/boot.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include diff --git a/src/kernel/arch/amd64/boot.h b/src/kernel/arch/amd64/boot.h index 251086c..b68f52b 100644 --- a/src/kernel/arch/amd64/boot.h +++ b/src/kernel/arch/amd64/boot.h @@ -1,2 +1,21 @@ #pragma once + +enum gdt_segs { + SEG_null, + /* order dictated by SYSENTER */ + SEG_r0code, + SEG_r0data, + SEG_r3code, + SEG_r3data, + SEG_TSS, + SEG_TSS2, + + SEG_end +}; + void kmain_early(void *mbi); +void gdt_init(void); +void idt_init(void); + +/* used from asm */ +extern struct lgdt_arg lgdt_arg; diff --git a/src/kernel/arch/amd64/driver/pata.c b/src/kernel/arch/amd64/driver/pata.c index b0ed592..3e9fa7a 100644 --- a/src/kernel/arch/amd64/driver/pata.c +++ b/src/kernel/arch/amd64/driver/pata.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include diff --git a/src/kernel/arch/amd64/driver/ps2.c b/src/kernel/arch/amd64/driver/ps2.c index cdcbf19..971d7d1 100644 --- a/src/kernel/arch/amd64/driver/ps2.c +++ b/src/kernel/arch/amd64/driver/ps2.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include diff --git a/src/kernel/arch/amd64/driver/rtl8139.c b/src/kernel/arch/amd64/driver/rtl8139.c index dd11102..d0e4d90 100644 --- a/src/kernel/arch/amd64/driver/rtl8139.c +++ b/src/kernel/arch/amd64/driver/rtl8139.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include diff --git a/src/kernel/arch/amd64/driver/serial.c b/src/kernel/arch/amd64/driver/serial.c index 6fe4500..5004c31 100644 --- a/src/kernel/arch/amd64/driver/serial.c +++ b/src/kernel/arch/amd64/driver/serial.c @@ -2,10 +2,8 @@ #include #include #include -#include #include #include -#include #include #include diff --git a/src/kernel/arch/amd64/driver/util.c b/src/kernel/arch/amd64/driver/util.c index 957005c..1462258 100644 --- a/src/kernel/arch/amd64/driver/util.c +++ b/src/kernel/arch/amd64/driver/util.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -53,3 +52,10 @@ void postqueue_ringreadall(VfsReq **queue, ring_t *r) { } *queue = NULL; } + +size_t ring_to_virt(ring_t *r, Proc *proc, void __user *ubuf, size_t max) { + char tmp[32]; + if (max > sizeof tmp) max = sizeof tmp; + max = ring_get(r, tmp, max); + return pcpy_to(proc, ubuf, tmp, max); +} diff --git a/src/kernel/arch/amd64/driver/util.h b/src/kernel/arch/amd64/driver/util.h index 5827fa9..0f7db7a 100644 --- a/src/kernel/arch/amd64/driver/util.h +++ b/src/kernel/arch/amd64/driver/util.h @@ -19,3 +19,5 @@ bool postqueue_pop(VfsReq **queue, void (*accept)(VfsReq *)); /** If there are any pending read requests, and the ring buffer isn't empty, fulfill them * all with a single read. */ void postqueue_ringreadall(VfsReq **queue, ring_t *r); + +size_t ring_to_virt(ring_t *r, Proc *proc, void __user *ubuf, size_t max); diff --git a/src/kernel/arch/amd64/driver/video.c b/src/kernel/arch/amd64/driver/video.c index d7046b4..ecf713f 100644 --- a/src/kernel/arch/amd64/driver/video.c +++ b/src/kernel/arch/amd64/driver/video.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include diff --git a/src/kernel/arch/amd64/interrupts/idt.c b/src/kernel/arch/amd64/interrupts/idt.c index f9f3073..622ac1a 100644 --- a/src/kernel/arch/amd64/interrupts/idt.c +++ b/src/kernel/arch/amd64/interrupts/idt.c @@ -1,5 +1,4 @@ -#include -#include +#include #include #include #include diff --git a/src/kernel/arch/amd64/interrupts/idt.h b/src/kernel/arch/amd64/interrupts/idt.h deleted file mode 100644 index 6576cf9..0000000 --- a/src/kernel/arch/amd64/interrupts/idt.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void idt_init(void); diff --git a/src/kernel/mem/virt.c b/src/kernel/mem/virt.c deleted file mode 100644 index 1ac6fbc..0000000 --- a/src/kernel/mem/virt.c +++ /dev/null @@ -1,160 +0,0 @@ -/* Copies stuff between different pagedirs. Mostly a wrapper behind an old, - * bad interface. */ - -// TODO ensure the behaviour of kernel vs user fs on faults is the same - -#include -#include -#include -#include -#include -#include - -struct virt_iter { - void *frag; - size_t frag_len; - size_t prior; // sum of all prior frag_lens - bool error; - - void __user *_virt; - size_t _remaining; - Pagedir *_pages; - bool _user; - bool _writeable; -}; - -struct virt_cpy_error { // unused - bool read_fail, write_fail; -}; - -/* if pages == NULL, creates an iterator over physical memory. */ -static void virt_iter_new( - struct virt_iter *iter, void __user *virt, size_t length, - Pagedir *pages, bool user, bool writeable -); -static bool virt_iter_next(struct virt_iter *); -static size_t virt_cpy( - Pagedir *dest_pages, void __user *dest, - Pagedir *src_pages, const void __user *src, - size_t length, struct virt_cpy_error *err -); - - -static void -virt_iter_new( - struct virt_iter *iter, void __user *virt, size_t length, - Pagedir *pages, bool user, bool writeable -) { - iter->frag = NULL; - iter->frag_len = 0; - iter->prior = 0; - iter->error = false; - iter->_virt = virt; - iter->_remaining = length; - iter->_pages = pages; - iter->_user = user; - iter->_writeable = writeable; -} - -static bool -virt_iter_next(struct virt_iter *iter) -{ - /* note: While i'm pretty sure that this should always work, this - * was only tested in cases where the pages were consecutive both in - * virtual and physical memory, which might not always be the case. - * TODO test this */ - - size_t partial = iter->_remaining; - iter->prior += iter->frag_len; - if (partial <= 0) return false; - - if (iter->_pages) { // if iterating over virtual memory - // don't cross page boundaries - size_t to_page = PAGE_SIZE - ((uintptr_t)iter->_virt & PAGE_MASK); - partial = min(partial, to_page); - - iter->frag = pagedir_virt2phys(iter->_pages, - iter->_virt, iter->_user, iter->_writeable); - - if (!iter->frag) { - iter->error = true; - return false; - } - } else { - // "iterate" over physical memory - // the double cast supresses the warning about changing address spaces - iter->frag = (void* __force)iter->_virt; - } - - iter->frag_len = partial; - iter->_remaining -= partial; - iter->_virt += partial; - return true; -} - -static size_t -virt_cpy( - Pagedir *dest_pages, void __user *dest, - Pagedir *src_pages, const void __user *src, - size_t length, struct virt_cpy_error *err -) { - struct virt_iter dest_iter, src_iter; - size_t total = 0, partial; - - virt_iter_new(&dest_iter, dest, length, dest_pages, true, true); - virt_iter_new( &src_iter, (userptr_t)src, length, src_pages, true, false); - dest_iter.frag_len = 0; - src_iter.frag_len = 0; - - for (;;) { - if (dest_iter.frag_len <= 0 && !virt_iter_next(&dest_iter)) break; - if ( src_iter.frag_len <= 0 && !virt_iter_next( &src_iter)) break; - - partial = min(src_iter.frag_len, dest_iter.frag_len); - total += partial; - memcpy(dest_iter.frag, src_iter.frag, partial); - - dest_iter.frag_len -= partial; - dest_iter.frag += partial; - src_iter.frag_len -= partial; - src_iter.frag += partial; - } - - if (err) { - err->read_fail = src_iter.error; - err->write_fail = dest_iter.error; - } - if (src_iter.error || dest_iter.error) - assert(total != length); - else - assert(total == length); - return total; -} - -size_t -pcpy_to(Proc *p, __user void *dst, const void *src, size_t len) -{ - assert(p); - if (!p->pages) return 0; - return virt_cpy(p->pages, dst, NULL, (__user void*)src, len, NULL); -} - -size_t -pcpy_from(Proc *p, void *dst, const __user void *src, size_t len) -{ - assert(p); - if (!p->pages) return 0; - return virt_cpy(NULL, (__user void*)dst, p->pages, src, len, NULL); -} - -size_t -pcpy_bi( - Proc *dstp, __user void *dst, - Proc *srcp, const __user void *src, - size_t len -) { - assert(dstp && srcp); - if (!dstp->pages) return 0; - if (!srcp->pages) return 0; - return virt_cpy(dstp->pages, dst, srcp->pages, src, len, NULL); -} diff --git a/src/kernel/mem/virt.h b/src/kernel/mem/virt.h deleted file mode 100644 index 7c9380e..0000000 --- a/src/kernel/mem/virt.h +++ /dev/null @@ -1,12 +0,0 @@ -// move this to proc.h, maybe? -#pragma once -#include -#include - -size_t pcpy_to(Proc *p, __user void *dst, const void *src, size_t len); -size_t pcpy_from(Proc *p, void *dst, const __user void *src, size_t len); -size_t pcpy_bi( - Proc *dstp, __user void *dst, - Proc *srcp, const __user void *src, - size_t len -); diff --git a/src/kernel/pcpy.c b/src/kernel/pcpy.c new file mode 100644 index 0000000..9cf7c37 --- /dev/null +++ b/src/kernel/pcpy.c @@ -0,0 +1,159 @@ +/* Copies stuff between different pagedirs. Mostly a wrapper behind an old, + * bad interface. */ + +// TODO ensure the behaviour of kernel vs user fs on faults is the same + +#include +#include +#include +#include +#include + +struct virt_iter { + void *frag; + size_t frag_len; + size_t prior; // sum of all prior frag_lens + bool error; + + void __user *_virt; + size_t _remaining; + Pagedir *_pages; + bool _user; + bool _writeable; +}; + +struct virt_cpy_error { // unused + bool read_fail, write_fail; +}; + +/* if pages == NULL, creates an iterator over physical memory. */ +static void virt_iter_new( + struct virt_iter *iter, void __user *virt, size_t length, + Pagedir *pages, bool user, bool writeable +); +static bool virt_iter_next(struct virt_iter *); +static size_t virt_cpy( + Pagedir *dest_pages, void __user *dest, + Pagedir *src_pages, const void __user *src, + size_t length, struct virt_cpy_error *err +); + + +static void +virt_iter_new( + struct virt_iter *iter, void __user *virt, size_t length, + Pagedir *pages, bool user, bool writeable +) { + iter->frag = NULL; + iter->frag_len = 0; + iter->prior = 0; + iter->error = false; + iter->_virt = virt; + iter->_remaining = length; + iter->_pages = pages; + iter->_user = user; + iter->_writeable = writeable; +} + +static bool +virt_iter_next(struct virt_iter *iter) +{ + /* note: While i'm pretty sure that this should always work, this + * was only tested in cases where the pages were consecutive both in + * virtual and physical memory, which might not always be the case. + * TODO test this */ + + size_t partial = iter->_remaining; + iter->prior += iter->frag_len; + if (partial <= 0) return false; + + if (iter->_pages) { // if iterating over virtual memory + // don't cross page boundaries + size_t to_page = PAGE_SIZE - ((uintptr_t)iter->_virt & PAGE_MASK); + partial = min(partial, to_page); + + iter->frag = pagedir_virt2phys(iter->_pages, + iter->_virt, iter->_user, iter->_writeable); + + if (!iter->frag) { + iter->error = true; + return false; + } + } else { + // "iterate" over physical memory + // the double cast supresses the warning about changing address spaces + iter->frag = (void* __force)iter->_virt; + } + + iter->frag_len = partial; + iter->_remaining -= partial; + iter->_virt += partial; + return true; +} + +static size_t +virt_cpy( + Pagedir *dest_pages, void __user *dest, + Pagedir *src_pages, const void __user *src, + size_t length, struct virt_cpy_error *err +) { + struct virt_iter dest_iter, src_iter; + size_t total = 0, partial; + + virt_iter_new(&dest_iter, dest, length, dest_pages, true, true); + virt_iter_new( &src_iter, (userptr_t)src, length, src_pages, true, false); + dest_iter.frag_len = 0; + src_iter.frag_len = 0; + + for (;;) { + if (dest_iter.frag_len <= 0 && !virt_iter_next(&dest_iter)) break; + if ( src_iter.frag_len <= 0 && !virt_iter_next( &src_iter)) break; + + partial = min(src_iter.frag_len, dest_iter.frag_len); + total += partial; + memcpy(dest_iter.frag, src_iter.frag, partial); + + dest_iter.frag_len -= partial; + dest_iter.frag += partial; + src_iter.frag_len -= partial; + src_iter.frag += partial; + } + + if (err) { + err->read_fail = src_iter.error; + err->write_fail = dest_iter.error; + } + if (src_iter.error || dest_iter.error) + assert(total != length); + else + assert(total == length); + return total; +} + +size_t +pcpy_to(Proc *p, __user void *dst, const void *src, size_t len) +{ + assert(p); + if (!p->pages) return 0; + return virt_cpy(p->pages, dst, NULL, (__user void*)src, len, NULL); +} + +size_t +pcpy_from(Proc *p, void *dst, const __user void *src, size_t len) +{ + assert(p); + if (!p->pages) return 0; + return virt_cpy(NULL, (__user void*)dst, p->pages, src, len, NULL); +} + +size_t +pcpy_bi( + Proc *dstp, __user void *dst, + Proc *srcp, const __user void *src, + size_t len +) { + assert(dstp && srcp); + if (!dstp->pages) return 0; + if (!srcp->pages) return 0; + return virt_cpy(dstp->pages, dst, srcp->pages, src, len, NULL); +} diff --git a/src/kernel/pipe.c b/src/kernel/pipe.c index 3ac52d3..6325369 100644 --- a/src/kernel/pipe.c +++ b/src/kernel/pipe.c @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/src/kernel/proc.c b/src/kernel/proc.c index 93fef1a..836490b 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include diff --git a/src/kernel/proc.h b/src/kernel/proc.h index 5120778..8a19d8f 100644 --- a/src/kernel/proc.h +++ b/src/kernel/proc.h @@ -128,3 +128,11 @@ Handle *proc_hid_take(Proc *, hid_t); hid_t proc_handle_put(Proc *, Handle *); void proc_setstate(Proc *, enum proc_state); + +size_t pcpy_to(Proc *p, __user void *dst, const void *src, size_t len); +size_t pcpy_from(Proc *p, void *dst, const __user void *src, size_t len); +size_t pcpy_bi( + Proc *dstp, __user void *dst, + Proc *srcp, const __user void *src, + size_t len +); diff --git a/src/kernel/ring.c b/src/kernel/ring.c index 2cb6961..e69de29 100644 --- a/src/kernel/ring.c +++ b/src/kernel/ring.c @@ -1,9 +0,0 @@ -#include -#include - -size_t ring_to_virt(ring_t *r, Proc *proc, void __user *ubuf, size_t max) { - char tmp[32]; - if (max > sizeof tmp) max = sizeof tmp; - max = ring_get(r, tmp, max); - return pcpy_to(proc, ubuf, tmp, max); -} diff --git a/src/kernel/ring.h b/src/kernel/ring.h deleted file mode 100644 index 89770d0..0000000 --- a/src/kernel/ring.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once -// TODO merge into driver/util.h -#include -#include - -size_t ring_to_virt(ring_t *r, Proc *proc, void __user *ubuf, size_t max); diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index 1ab2efd..72d19f0 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include diff --git a/src/kernel/vfs/procfs.c b/src/kernel/vfs/procfs.c index 2a8dd93..ecca839 100644 --- a/src/kernel/vfs/procfs.c +++ b/src/kernel/vfs/procfs.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/src/kernel/vfs/request.c b/src/kernel/vfs/request.c index 7e5877d..35df29c 100644 --- a/src/kernel/vfs/request.c +++ b/src/kernel/vfs/request.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include -- cgit v1.2.3