summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Lee <me@nwlee.tech>2026-06-17 13:05:15 -0500
committerNathan Lee <me@nwlee.tech>2026-06-17 13:05:15 -0500
commitb106df2447d23d6b3fcffa7cc87edde218d054f3 (patch)
tree85b01191beada41b85bc391482cee7294b73f89b
parent2fb81f37d20fd442d1954a7356e3ab7f88314a9c (diff)
create wdt device discovery
discuss: per wrv32em status, consider moving custom wdt format to dtb or pci device discovery (potentially both: we have the framework for pci.ko extension after using dtb to discover memory and pci devices)
-rw-r--r--.gitignore2
-rw-r--r--Makefile2
-rw-r--r--include/mem.h22
-rw-r--r--include/types.h17
-rw-r--r--include/wdt.h18
-rw-r--r--kernel/kernel.c21
-rw-r--r--kernel/mem.c32
7 files changed, 108 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore
index e2e7327..22e0bda 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
/out
+/.cache
+compile_commands.json
diff --git a/Makefile b/Makefile
index 9465dd1..2de743c 100644
--- a/Makefile
+++ b/Makefile
@@ -109,7 +109,7 @@ $(KERNEL_ELF): $(KERNEL_OBJS) linker.ld | $(BIN_DIR)
$(KERNEL_BIN): $(KERNEL_ELF) | $(BIN_DIR)
@echo " OBJCOPY $@"
- $(OBJCOPY) $(OBJCOPY_FLAG) $(KERNEL_ELF) $(KERNEL_BIN)
+ $(OBJCOPY) $(KERNEL_OBJCOPY_FLAG) $(KERNEL_ELF) $(KERNEL_BIN)
$(OBJ_DIR)/%.o: %.c# | $(OBJ_DIR)/%
@mkdir -p $(dir $@)
diff --git a/include/mem.h b/include/mem.h
new file mode 100644
index 0000000..12976df
--- /dev/null
+++ b/include/mem.h
@@ -0,0 +1,22 @@
+#pragma once
+#include <types.h>
+
+#define EHEAP_SIZE (512 * 1024)
+
+typedef enum {
+ PAGE_FREE,
+ PAGE_ALLOCATED
+} MemoryPageAvailability;
+
+struct page {
+ void* locator;
+ MemoryPageAvailability availability;
+ struct page* next;
+};
+
+void* kmalloc(size_t size);
+void* memcpy(void* to, const void* from,size_t size);
+
+void* kemalloc(size_t size);
+
+extern struct page root;
diff --git a/include/types.h b/include/types.h
new file mode 100644
index 0000000..4c7965c
--- /dev/null
+++ b/include/types.h
@@ -0,0 +1,17 @@
+#pragma once
+
+#define NULL (void*)0
+
+#define size_t __wk_size_t
+#define uint8_t __wk_uint8_t
+#define uint16_t __wk_uint16_t
+#define uint32_t __wk_uint32_t
+#define uint64_t __wk_uint64_t
+#define be32_to_cpu(x) __builtin_bswap32(x)
+
+typedef __UINTPTR_TYPE__ __wk_size_t;
+typedef __UINT8_TYPE__ __wk_uint8_t;
+typedef __UINT16_TYPE__ __wk_uint16_t;
+typedef __UINT32_TYPE__ __wk_uint32_t;
+typedef __UINT64_TYPE__ __wk_uint64_t;
+
diff --git a/include/wdt.h b/include/wdt.h
new file mode 100644
index 0000000..31739ab
--- /dev/null
+++ b/include/wdt.h
@@ -0,0 +1,18 @@
+#pragma once
+#include <types.h>
+
+#define WDT_MAGIC 0x67674141
+
+struct wdt_device {
+ uint32_t type;
+ uint32_t driver_id;
+ uint32_t start;
+ uint32_t size;
+};
+
+struct wdt_root {
+ uint32_t magic;
+ uint32_t count;
+ struct wdt_device devices[];
+};
+
diff --git a/kernel/kernel.c b/kernel/kernel.c
index 753f4a5..3dbf0d1 100644
--- a/kernel/kernel.c
+++ b/kernel/kernel.c
@@ -1,3 +1,5 @@
+#include <types.h>
+#include <wdt.h>
#define UART_BASE 0x10000000 // Example UART address
static inline void putchar(char c) {
@@ -11,10 +13,19 @@ static void print_string(const char *str) {
}
}
-void kernel_main(void) {
- print_string("Hello from RISC-V kernel!\n");
-
- while (1) {
- // Wait
+void kernel_main(void *wdt) {
+ struct wdt_root *root = wdt;
+ print_string("deserialized all!\n");
+ uint32_t count = root->count;
+ for (uint32_t i = 0; i < count; i++) {
+ print_string("deserializing wdt node!\n");
+ print_string("device type: ");
+ if (root->devices[i].type == 0) {
+ print_string("mem");
+ } else {
+ print_string("unknown");
+ }
+ print_string("\n");
}
+ while(1);
}
diff --git a/kernel/mem.c b/kernel/mem.c
new file mode 100644
index 0000000..531dd81
--- /dev/null
+++ b/kernel/mem.c
@@ -0,0 +1,32 @@
+#include <types.h>
+#include <mem.h>
+
+static char eheap[EHEAP_SIZE] = {0};
+static size_t eheap_off = 0;
+
+void* kmalloc(size_t size) {
+ return (void*)size;
+}
+
+void* kemalloc(size_t size) {
+ size = (size + 7) & ~7;
+
+ if (eheap_off + size > EHEAP_SIZE) {
+ return NULL;
+ }
+
+ void *ptr = &eheap[eheap_off];
+ eheap_off += size;
+ return ptr;
+}
+
+void* memcpy(void *to, const void *from, size_t size) {
+ char *dest = (char *)to;
+ const char *src = (const char *)from;
+
+ while (size--) {
+ *dest++ = *src++;
+ }
+
+ return dest;
+}