summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordzwdz2021-07-10 17:41:32 +0200
committerdzwdz2021-07-10 17:41:32 +0200
commit1bf5e324005ce7122a195af106cec656960648dc (patch)
tree7a8394a5243ad50d1e8d5574d94908461fe96cc6 /src
parent6cbdc62b5cbe34d7355047722d6d483a4d25c7f3 (diff)
a sensible source structure
The idea is that src/kernel/ is only allowed to interface with the hardware using whatever's defined in src/arch/generic.h. I'll probably write a small script for checking this later on. This is a giant commit so I've probably fucked something up. It boots fine on Bochs and QEMU, so at least there's that.
Diffstat (limited to 'src')
-rw-r--r--src/arch/generic.h14
-rw-r--r--src/arch/i386/boot.c16
-rw-r--r--src/arch/i386/boot.s25
-rw-r--r--src/arch/i386/gdt.h (renamed from src/kernel/gdt.h)1
-rw-r--r--src/arch/i386/gdt/farjump.s9
-rw-r--r--src/arch/i386/gdt/gdt.c (renamed from src/kernel/gdt.c)8
-rw-r--r--src/arch/i386/interrupts/idt.c (renamed from src/kernel/idt.c)6
-rw-r--r--src/arch/i386/interrupts/idt.h (renamed from src/kernel/idt.h)0
-rw-r--r--src/arch/i386/interrupts/isr.c (renamed from src/kernel/isr.c)4
-rw-r--r--src/arch/i386/interrupts/isr.h (renamed from src/kernel/isr.h)0
-rw-r--r--src/arch/i386/multiboot.s11
-rw-r--r--src/arch/i386/sysenter.h2
-rw-r--r--src/arch/i386/sysenter.s (renamed from src/platform/sysenter.s)2
-rw-r--r--src/arch/i386/tty.c (renamed from src/kernel/tty.c)4
-rw-r--r--src/arch/i386/tty.h (renamed from src/kernel/tty.h)0
-rw-r--r--src/kernel/main.c15
-rw-r--r--src/kernel/main.h3
-rw-r--r--src/kernel/mem.c1
-rw-r--r--src/kernel/mem.h2
-rw-r--r--src/kernel/panic.h4
-rw-r--r--src/kernel/proc.c2
-rw-r--r--src/platform/asm.h9
-rw-r--r--src/platform/boot.s47
23 files changed, 97 insertions, 88 deletions
diff --git a/src/arch/generic.h b/src/arch/generic.h
new file mode 100644
index 0000000..7807316
--- /dev/null
+++ b/src/arch/generic.h
@@ -0,0 +1,14 @@
+#pragma once
+
+// i have no idea where else to put it
+#define PAGE_SIZE 4096
+
+// src/arch/i386/boot.s
+extern void stack_top;
+
+__attribute__((noreturn))
+void halt_cpu();
+
+// src/arch/i386/sysenter.s
+void sysexit(void (*fun)(), void *stack_top);
+void sysenter_setup();
diff --git a/src/arch/i386/boot.c b/src/arch/i386/boot.c
new file mode 100644
index 0000000..b2b84f0
--- /dev/null
+++ b/src/arch/i386/boot.c
@@ -0,0 +1,16 @@
+#include <arch/i386/gdt.h>
+#include <arch/i386/interrupts/idt.h>
+#include <arch/i386/sysenter.h>
+#include <arch/i386/tty.h>
+#include <kernel/main.h>
+
+void kmain_early() {
+ tty_clear();
+ tty_const("gdt...");
+ gdt_init();
+ tty_const("idt...");
+ idt_init();
+ tty_const("sysenter...");
+ sysenter_setup();
+ kmain();
+}
diff --git a/src/arch/i386/boot.s b/src/arch/i386/boot.s
new file mode 100644
index 0000000..74ec312
--- /dev/null
+++ b/src/arch/i386/boot.s
@@ -0,0 +1,25 @@
+/* a lil stack TODO move to linker.ld */
+.section .bss
+.global stack_top
+.type stack_top, @object
+.align 16
+stack_bottom:
+.skip 16384
+stack_top:
+
+
+.section .text
+.global _start
+.type _start, @function
+_start:
+ mov $stack_top, %esp
+ call kmain_early
+
+.global halt_cpu
+.type halt_cpu, @function
+halt_cpu:
+ cli
+1: hlt
+ jmp 1b
+
+.size _start, . - _start
diff --git a/src/kernel/gdt.h b/src/arch/i386/gdt.h
index 9e5a6a5..bcb9870 100644
--- a/src/kernel/gdt.h
+++ b/src/arch/i386/gdt.h
@@ -13,3 +13,4 @@ enum {
};
void gdt_init();
+void gdt_farjump(int segment);
diff --git a/src/arch/i386/gdt/farjump.s b/src/arch/i386/gdt/farjump.s
new file mode 100644
index 0000000..85d8ba5
--- /dev/null
+++ b/src/arch/i386/gdt/farjump.s
@@ -0,0 +1,9 @@
+.section .text
+.global gdt_farjump
+.type gdt_farjump, @function
+gdt_farjump:
+ /* retf pops off the return address and code segment off the stack.
+ * it turns out that in the i386 cdecl calling convention they're in
+ * the correct place already.
+ */
+ retf
diff --git a/src/kernel/gdt.c b/src/arch/i386/gdt/gdt.c
index b0d6320..3e0647b 100644
--- a/src/kernel/gdt.c
+++ b/src/arch/i386/gdt/gdt.c
@@ -1,8 +1,8 @@
-#include <kernel/gdt.h>
+#include <arch/generic.h>
+#include <arch/i386/gdt.h>
#include <kernel/util.h>
#include <stdint.h>
-extern void stack_top; // platform/boot.s
struct gdt_entry {
uint64_t limit_low : 16;
@@ -95,8 +95,6 @@ static void gdt_prepare() {
GDT[SEG_TSS].base_high = (((uint32_t) &TSS) >> 24) & 0xff;
}
-void change_cs(int seg); // temporary
-
static void gdt_load() {
lgdt_arg.limit = sizeof(GDT) - 1;
lgdt_arg.base = (uint32_t) &GDT;
@@ -106,7 +104,7 @@ static void gdt_load() {
: : "a" (SEG_TSS << 3 | 3) : "memory");
// update all segment registers
- change_cs(SEG_r0code << 3);
+ gdt_farjump(SEG_r0code << 3);
asm("mov %0, %%ds;"
"mov %0, %%ss;"
"mov %0, %%es;"
diff --git a/src/kernel/idt.c b/src/arch/i386/interrupts/idt.c
index 65f511f..4e23118 100644
--- a/src/kernel/idt.c
+++ b/src/arch/i386/interrupts/idt.c
@@ -1,6 +1,6 @@
-#include <kernel/gdt.h>
-#include <kernel/idt.h>
-#include <kernel/isr.h>
+#include <arch/i386/gdt.h>
+#include <arch/i386/interrupts/idt.h>
+#include <arch/i386/interrupts/isr.h>
#include <kernel/panic.h>
#include <stdbool.h>
#include <stdint.h>
diff --git a/src/kernel/idt.h b/src/arch/i386/interrupts/idt.h
index 5627657..5627657 100644
--- a/src/kernel/idt.h
+++ b/src/arch/i386/interrupts/idt.h
diff --git a/src/kernel/isr.c b/src/arch/i386/interrupts/isr.c
index b715de0..03399ea 100644
--- a/src/kernel/isr.c
+++ b/src/arch/i386/interrupts/isr.c
@@ -1,6 +1,6 @@
-#include <kernel/isr.h>
+#include <arch/i386/interrupts/isr.h>
+#include <arch/i386/tty.h>
#include <kernel/panic.h>
-#include <kernel/tty.h>
#include <stdbool.h>
#include <stdint.h>
diff --git a/src/kernel/isr.h b/src/arch/i386/interrupts/isr.h
index 150fc46..150fc46 100644
--- a/src/kernel/isr.h
+++ b/src/arch/i386/interrupts/isr.h
diff --git a/src/arch/i386/multiboot.s b/src/arch/i386/multiboot.s
new file mode 100644
index 0000000..2194982
--- /dev/null
+++ b/src/arch/i386/multiboot.s
@@ -0,0 +1,11 @@
+.set MAGIC, 0x1BADB002
+.set FLAG_ALIGN, 1<<0 /* align modules on page boundaries */
+.set FLAG_MEMINFO, 1<<1 /* memory map */
+.set FLAGS, FLAG_ALIGN | FLAG_MEMINFO
+.set CHECKSUM, -(MAGIC + FLAGS)
+
+.section .multiboot
+.align 4
+.long MAGIC
+.long FLAGS
+.long CHECKSUM
diff --git a/src/arch/i386/sysenter.h b/src/arch/i386/sysenter.h
new file mode 100644
index 0000000..b531fe8
--- /dev/null
+++ b/src/arch/i386/sysenter.h
@@ -0,0 +1,2 @@
+#pragma once
+void sysenter_setup();
diff --git a/src/platform/sysenter.s b/src/arch/i386/sysenter.s
index 270dc08..666c0f0 100644
--- a/src/platform/sysenter.s
+++ b/src/arch/i386/sysenter.s
@@ -1,4 +1,4 @@
-/* kernel/gdt.c */
+/* arch/i386/gdt.c */
.set SEG_r0code, 1
.set SEG_r3code, 3
.set SEG_r3data, 4
diff --git a/src/kernel/tty.c b/src/arch/i386/tty.c
index 5c4c47d..a7c74f5 100644
--- a/src/kernel/tty.c
+++ b/src/arch/i386/tty.c
@@ -1,6 +1,4 @@
-/* will be moved to userspace later on */
-
-#include <kernel/tty.h>
+#include <arch/i386/tty.h>
struct vga_cell {
unsigned char c;
diff --git a/src/kernel/tty.h b/src/arch/i386/tty.h
index 3dc1525..3dc1525 100644
--- a/src/kernel/tty.h
+++ b/src/arch/i386/tty.h
diff --git a/src/kernel/main.c b/src/kernel/main.c
index 0193514..aa67d7c 100644
--- a/src/kernel/main.c
+++ b/src/kernel/main.c
@@ -1,22 +1,11 @@
-#include <kernel/gdt.h>
-#include <kernel/idt.h>
+#include <arch/generic.h>
#include <kernel/mem.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
-#include <kernel/tty.h>
-#include <platform/asm.h>
void r3_test();
-void kmain()
-{
- tty_clear();
- tty_const("gdt...");
- gdt_init();
- tty_const("idt...");
- idt_init();
- tty_const("sysenter...");
- sysenter_setup();
+void kmain() {
tty_const("mem...");
mem_init();
diff --git a/src/kernel/main.h b/src/kernel/main.h
new file mode 100644
index 0000000..6f27386
--- /dev/null
+++ b/src/kernel/main.h
@@ -0,0 +1,3 @@
+#pragma once
+
+void kmain();
diff --git a/src/kernel/mem.c b/src/kernel/mem.c
index 08eb285..f383e8b 100644
--- a/src/kernel/mem.c
+++ b/src/kernel/mem.c
@@ -1,3 +1,4 @@
+#include <arch/generic.h>
#include <kernel/mem.h>
extern void *_kernel_end;
diff --git a/src/kernel/mem.h b/src/kernel/mem.h
index e8e6a4f..efa65d6 100644
--- a/src/kernel/mem.h
+++ b/src/kernel/mem.h
@@ -1,8 +1,6 @@
#pragma once
#include <stddef.h>
-#define PAGE_SIZE 4096
-
void mem_init();
// allocates `pages` consecutive pages
diff --git a/src/kernel/panic.h b/src/kernel/panic.h
index 7d2ea5f..54a0b8b 100644
--- a/src/kernel/panic.h
+++ b/src/kernel/panic.h
@@ -1,6 +1,6 @@
#pragma once
-#include <kernel/tty.h>
-#include <platform/asm.h>
+#include <arch/generic.h>
+#include <arch/i386/tty.h> // TODO abstract away
// dumb c shit
#define panic_tostr2(x) #x
diff --git a/src/kernel/proc.c b/src/kernel/proc.c
index 776238c..7d42bce 100644
--- a/src/kernel/proc.c
+++ b/src/kernel/proc.c
@@ -1,6 +1,6 @@
+#include <arch/generic.h>
#include <kernel/mem.h>
#include <kernel/proc.h>
-#include <platform/asm.h>
struct process *process_current;
diff --git a/src/platform/asm.h b/src/platform/asm.h
deleted file mode 100644
index 6f72617..0000000
--- a/src/platform/asm.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#pragma once
-
-// boot.s
-__attribute__((noreturn))
-void halt_cpu();
-
-// sysenter.c
-void sysexit(void (*fun)(), void *stack_top);
-void sysenter_setup();
diff --git a/src/platform/boot.s b/src/platform/boot.s
deleted file mode 100644
index d5bfda5..0000000
--- a/src/platform/boot.s
+++ /dev/null
@@ -1,47 +0,0 @@
-.set MAGIC, 0x1BADB002
-.set FLAG_ALIGN, 1<<0 /* align modules on page boundaries */
-.set FLAG_MEMINFO, 1<<1 /* memory map */
-.set FLAGS, FLAG_ALIGN | FLAG_MEMINFO
-.set CHECKSUM, -(MAGIC + FLAGS)
-
-.section .multiboot
-.align 4
-.long MAGIC
-.long FLAGS
-.long CHECKSUM
-
-/* a lil stack */
-.section .bss
-.global stack_top
-.type stack_top, @object
-.align 16
-stack_bottom:
-.skip 16384
-stack_top:
-
-
-.section .text
-.global _start
-.type _start, @function
-_start:
- mov $stack_top, %esp
- call kmain
-
-.global halt_cpu
-.type halt_cpu, @function
-halt_cpu:
- cli
-1: hlt
- jmp 1b
-
-// temporary, will be moved to another file soon
-.global change_cs
-.type change_cs, @function
-change_cs:
- /* retf pops off the return address and code segment off the stack.
- * it turns out that in the i386 cdecl calling convention they're in
- * the correct place already.
- */
- retf
-
-.size _start, . - _start