summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile17
-rw-r--r--include/libk/container_of.h (renamed from include/kernel/container_of.h)4
-rw-r--r--include/libk/data/list.h (renamed from include/kernel/data/list.h)4
-rw-r--r--include/libk/data/ringbuf.h (renamed from include/kernel/data/ringbuf.h)4
-rw-r--r--include/libk/io.h (renamed from include/kernel/io.h)5
-rw-r--r--include/libk/kmalloc.h (renamed from include/kernel/kmalloc.h)7
-rw-r--r--include/libk/string.h (renamed from include/kernel/string.h)5
-rw-r--r--kernel/init.c30
-rw-r--r--libk/io.c (renamed from kernel/io.c)20
-rw-r--r--libk/stack_protector.c11
-rw-r--r--libk/string.c (renamed from kernel/string.c)11
11 files changed, 70 insertions, 48 deletions
diff --git a/Makefile b/Makefile
index 043e883..9a50fb6 100644
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,7 @@ ARCH?=i386
CC:=i686-elf-gcc
VERSION:="$(shell git describe --abbrev=4 --dirty --always --tags)"
INCLUDE:=$(INCLUDE)
-CFLAGS:=$(CFLAGS) -Wall -Wextra -DVERSION=\"$(VERSION)\" -ggdb
+CFLAGS:=$(CFLAGS) -Wall -Wextra -DVERSION=\"$(VERSION)\" -ggdb -fstack-protector-all -O0
LDFLAGS:=$(LDFLAGS)
LIBS:=$(LIBS)
ARCH:=$(ARCH)
@@ -24,21 +24,28 @@ LIBS:=$(LIBS) $(KERNEL_ARCH_LIBS)
KERNEL=vmbox
+LIBK_OBJS=libk/string.o \
+ libk/io.o \
+ libk/stack_protector.o \
+ libk/ubsan.o \
+
KERNEL_OBJS=$(KERNEL_ARCH_OBJS) \
kernel/init.o \
- kernel/string.o \
- kernel/io.o \
kernel/panic.o \
+ kernel/vfs.o \
kernel/sched.o \
+ kernel/kthread.o \
OBJS=$(ARCHDIR)/boot/crti.o \
$(ARCHDIR)/crtbegin.o \
$(KERNEL_OBJS) \
+ $(LIBK_OBJS) \
$(ARCHDIR)/crtend.o \
$(ARCHDIR)/boot/crtn.o \
LINK_LIST=$(LDFLAGS) \
$(KERNEL_OBJS) \
+ $(LIBK_OBJS) \
$(LIBS) \
.PHONY: all clean install install-headers install-kernel
@@ -87,9 +94,9 @@ install-disk: $(KERNEL)
mcopy -i a.img vmbox ::vmbox
run: $(KERNEL)
- qemu-system-i386 -kernel $(KERNEL) -serial stdio
+ qemu-system-i386 -kernel $(KERNEL) -serial stdio -m 3G -drive file=a.img,format=raw -append "root=/dev/sda init=/bin/sh"
debug: $(KERNEL)
- qemu-system-i386 -kernel $(KERNEL) -s -S
+ qemu-system-i386 -kernel $(KERNEL) -s -S -m 3G -drive file=a.img,format=raw -append "root=/dev/sda init=/bin/sh" &
-include $(OBJS:.o=.d)
diff --git a/include/kernel/container_of.h b/include/libk/container_of.h
index 1b5b444..fdc400c 100644
--- a/include/kernel/container_of.h
+++ b/include/libk/container_of.h
@@ -1,5 +1,5 @@
-#ifndef KERNEL_CONTAINER_OF_H
-#define KERNEL_CONTAINER_OF_H
+#ifndef LIBK_CONTAINER_OF_H
+#define LIBK_CONTAINER_OF_H
#ifndef container_of
#define container_of(ptr, type, member) ({ \
diff --git a/include/kernel/data/list.h b/include/libk/data/list.h
index b61cfaa..69eee3a 100644
--- a/include/kernel/data/list.h
+++ b/include/libk/data/list.h
@@ -1,5 +1,5 @@
-#ifndef KERNEL_LIST_H
-#define KERNEL_LIST_H
+#ifndef LIBK_LIST_H
+#define LIBK_LIST_H
#include <stddef.h>
diff --git a/include/kernel/data/ringbuf.h b/include/libk/data/ringbuf.h
index 25a7125..cc6d77b 100644
--- a/include/kernel/data/ringbuf.h
+++ b/include/libk/data/ringbuf.h
@@ -1,5 +1,5 @@
-#ifndef KERNEL_RINGBUF_H
-#define KERNEL_RINGBUF_H
+#ifndef LIBK_RINGBUF_H
+#define LIBK_RINGBUF_H
#include <kernel/kmalloc.h>
#include <kernel/string.h>
diff --git a/include/kernel/io.h b/include/libk/io.h
index cf8404a..72dc5d7 100644
--- a/include/kernel/io.h
+++ b/include/libk/io.h
@@ -1,8 +1,9 @@
-#ifndef KERNEL_IO_H
-#define KERNEL_IO_H
+#ifndef LIBK_IO_H
+#define LIBK_IO_H
#include <stdarg.h>
+char* convert(unsigned int num, int base);
int vkprintf(const char *fmt, va_list args);
int kprintf(const char *fmt, ...);
diff --git a/include/kernel/kmalloc.h b/include/libk/kmalloc.h
index d8debd1..6573d28 100644
--- a/include/kernel/kmalloc.h
+++ b/include/libk/kmalloc.h
@@ -1,14 +1,15 @@
-#ifndef KERNEL_KMALLOC_H
-#define KERNEL_KMALLOC_H
+#ifndef LIBK_KMALLOC_H
+#define LIBK_KMALLOC_H
#include <stdint.h>
#include <stddef.h>
struct mem_block {
- uintptr_t start;
+ void *start;
size_t size;
int alloc;
struct mem_block *next;
+ struct mem_block *prev;
};
void kmalloc_init(void);
diff --git a/include/kernel/string.h b/include/libk/string.h
index 76bf610..1fb4af1 100644
--- a/include/kernel/string.h
+++ b/include/libk/string.h
@@ -1,5 +1,5 @@
-#ifndef STRING_H
-#define STRING_H
+#ifndef LIBK_STRING_H
+#define LIBK_STRING_H
#include <stddef.h>
@@ -12,6 +12,7 @@ int strcmp(const char *str1, const char *str2);
size_t strlen(const char *str);
char* strncpy(char* __restrict dest, const char* __restrict src, size_t n);
char* strcpy(char* __restrict dest, const char* __restrict src);
+char* strncat(char* __restrict dest, const char* __restrict src, size_t n);
char* strcat(char* __restrict dest, const char* __restrict src);
char* strtok(char* __restrict str, const char* __restrict delim);
diff --git a/kernel/init.c b/kernel/init.c
index 98ac05c..d4a3638 100644
--- a/kernel/init.c
+++ b/kernel/init.c
@@ -1,31 +1,35 @@
-#include <kernel/io.h>
+#include <libk/io.h>
+#include <libk/string.h>
+#include <libk/kmalloc.h>
#include <kernel/sched.h>
-#include <kernel/kmalloc.h>
-#include <kernel/string.h>
+#include <kernel/kthread.h>
+#include <kernel/pci.h>
#include <kernel/serial.h>
void jump_userspace(void);
char rootfs[1024];
-
-int start_init(int argc, char* argv[]) {
- while (1);
- return 0;
-}
+char init_bin[1024];
+int gdbstub = 0;
void process_cmd(char *cmdline) {
char *token = strtok(cmdline, " ");
while (token != NULL) {
if (strncmp(token, "root=", 5) == 0)
- strcpy(rootfs, &token[6]);
+ strcpy(rootfs, &token[5]);
+ if (strncmp(token, "init=", 5) == 0)
+ strcpy(init_bin, &token[5]);
+ if (strncmp(token, "gdb", 3) == 0)
+ gdbstub = 1;
+ token = strtok(NULL, " ");
}
}
void kernel_main(char *cmdline) {
- kmalloc_init();
- kprintf("Box Kernel version %s\n", VERSION);
+ kprintf("Box kernel version %s\n", VERSION);
+ process_cmd(cmdline);
+
serial_init();
sched_init();
-
- //jump_userspace();
+ //pci_check_buses();
}
diff --git a/kernel/io.c b/libk/io.c
index 0a4ba6d..eedc171 100644
--- a/kernel/io.c
+++ b/libk/io.c
@@ -1,10 +1,7 @@
-#include <kernel/io.h>
-#include <kernel/framebuffer.h>
-#include <kernel/data/ringbuf.h>
-#include <kernel/string.h>
-
-static int ringbuf_init = 0;
-static struct ringbuf rb;
+#include <libk/io.h>
+#include <libk/string.h>
+#include <kernel/video/framebuffer.h>
+#include <kernel/tty/tty_vga.h>
char* convert(unsigned int num, int base) {
static char rep[] = "0123456789ABCDEF";
@@ -23,15 +20,11 @@ char* convert(unsigned int num, int base) {
}
int vkprintf(const char *fmt, va_list args) {
- if (ringbuf_init == 0) {
- rb_init(&rb, 1024, 4096);
- ringbuf_init = 1;
- }
-
char *s;
int i;
char buffer[4096];
+ memset(buffer, 0, 4096);
for (size_t n = 0; n < strlen(fmt); n++) {
if (fmt[n] != '%') {
buffer[strlen(buffer)] = fmt[n];
@@ -67,7 +60,8 @@ int vkprintf(const char *fmt, va_list args) {
break;
}
}
- rb_push_back(&rb, buffer, strlen(buffer));
+ //tty_write(buffer, strlen(buffer));
+ fb_write(buffer, strlen(buffer));
memset(buffer, 0, 4096);
return 0;
}
diff --git a/libk/stack_protector.c b/libk/stack_protector.c
new file mode 100644
index 0000000..78d0302
--- /dev/null
+++ b/libk/stack_protector.c
@@ -0,0 +1,11 @@
+#include <kernel/panic.h>
+#include <stdint.h>
+#include <stddef.h>
+
+#define STACK_CHK_GUARD 0x32E3429E
+
+uintptr_t __stack_chk_guard = STACK_CHK_GUARD;
+
+__attribute__((noreturn)) void __stack_chk_fail(void) {
+ panic("STACK SMASHING IN KERNEL ADDRESS SPACE");
+}
diff --git a/kernel/string.c b/libk/string.c
index fa0fd3f..be59397 100644
--- a/kernel/string.c
+++ b/libk/string.c
@@ -1,4 +1,4 @@
-#include <kernel/string.h>
+#include <libk/string.h>
int memcmp(const void *str1, const void *str2, size_t n) {
unsigned char const *p1 = str1;
@@ -32,16 +32,15 @@ void* memcpy(void* __restrict dest, const void* __restrict src, size_t n) {
}
char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) {
- for (int i = 0; i < n; i++) {
+ for (size_t i = 0; i < n; i++) {
if (src[i] == '\0')
break;
dest[i] = src[i];
}
+ return dest;
}
char* strcpy(char* __restrict dest, const char* __restrict src) {
- if (strlen(src) > strlen(dest))
- return NULL;
return (char*)memcpy(dest, src, strlen(src));
}
@@ -49,6 +48,10 @@ char* strcat(char* __restrict dest, const char* __restrict src) {
return (char*)memcpy(&dest[strlen(dest)], src, strlen(src));
}
+char* strncat(char* __restrict dest, const char* __restrict src, size_t n) {
+ return (char*)memcpy(&dest[strlen(dest)], src, n);
+}
+
void* memmove(void* __restrict dest, const void* __restrict src, size_t n) {
if (dest == src)
return dest;