summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksa Vuckovic <aleksav013@gmail.com>2022-08-16 18:03:58 +0200
committerAleksa Vuckovic <aleksav013@gmail.com>2022-08-16 18:03:58 +0200
commit501a706643a056863b6ea214882a2be270966f87 (patch)
tree7f1ff025e7aa37b936ce15ebfe21783ae312d7f2
parent5c1c4eb0a4084b666342e6f8eb348eb80be6214b (diff)
libk/list.h; reading mmap from multiboot2
-rw-r--r--Makefile21
-rw-r--r--kernel/Makefile1
-rw-r--r--kernel/include/libk/list.h14
-rw-r--r--kernel/include/multiboot2.h22
-rw-r--r--kernel/src/boot/multiboot2.c64
-rw-r--r--kernel/src/libk/list.c37
-rw-r--r--kernel/src/main.c18
7 files changed, 138 insertions, 39 deletions
diff --git a/Makefile b/Makefile
index cd64db1..af294e8 100644
--- a/Makefile
+++ b/Makefile
@@ -7,16 +7,17 @@ LD = $(ARCH)ld
OBJDUMP = $(ARCH)objcopy
OBJCOPY = $(ARCH)objdump
-WARNINGS := -Wall -Werror -Wextra -pedantic -Wshadow -Wpointer-arith \
- -Wcast-align -Wwrite-strings -Wmissing-prototypes \
- -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline \
- -Wno-long-long -Wconversion -Wstrict-prototypes
-
-CFLAGS = $(WARNINGS) -Wno-error=infinite-recursion -O -fno-omit-frame-pointer
-# -mgeneral-regs-only disables SIMD instructions
-CFLAGS += -MD -O3 -mgeneral-regs-only -mcmodel=large
-CFLAGS += -ffreestanding -fno-common -nostdlib
-CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
+W := -Wall -Werror -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align
+W += -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations
+W += -Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wconversion
+W += -Wstrict-prototypes
+WNO := -Wno-error=unused-parameter -Wno-error=unused-variable
+WNO += -Wno-error=infinite-recursion
+
+CFLAGS = $(W) $(WNO) -fno-omit-frame-pointer -mcmodel=large
+CFLAGS += -mgeneral-regs-only # disables SIMD instructions
+CFLAGS += -MD -O3 -ffreestanding -nostdlib
+CFLAGS += -fno-common -fno-stack-protector
CFLAGS += -fno-pie -no-pie -fno-pic
LDFLAGS = -z max-page-size=4096
diff --git a/kernel/Makefile b/kernel/Makefile
index df8ece7..03138fa 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -11,6 +11,7 @@ OBJS = \
src/cpu/irq_stub.o \
src/cpu/pic.o \
src/devices/keyboard.o \
+ src/libk/list.o \
src/libk/math.o \
src/libk/stdio.o \
src/libk/string.o \
diff --git a/kernel/include/libk/list.h b/kernel/include/libk/list.h
new file mode 100644
index 0000000..f6a97f8
--- /dev/null
+++ b/kernel/include/libk/list.h
@@ -0,0 +1,14 @@
+#ifndef LIST_H
+#define LIST_H
+
+struct list_t {
+ struct list_t* next;
+ void* data;
+};
+typedef struct list_t list_t;
+
+void add_to_list_head(list_t** ptr, void* data);
+void add_to_list_tail(list_t** ptr, void* data);
+void free_list(list_t** ptr);
+
+#endif
diff --git a/kernel/include/multiboot2.h b/kernel/include/multiboot2.h
index e7935a6..6d4a86f 100644
--- a/kernel/include/multiboot2.h
+++ b/kernel/include/multiboot2.h
@@ -21,6 +21,23 @@ struct mb2_tag_fb {
} __attribute__((packed, aligned(8)));
typedef struct mb2_tag_fb mb2_tag_fb;
+struct mb2_tag_mmap {
+ uint32_t type;
+ uint32_t size;
+ uint32_t entry_size;
+ uint32_t entry_version;
+};
+typedef struct mb2_tag_mmap mb2_tag_mmap;
+
+struct mb2_tag_mmap_entry {
+ uint64_t base_addr;
+ uint64_t length;
+ uint32_t type;
+ uint32_t reserved;
+};
+typedef struct mb2_tag_mmap_entry mb2_tag_mmap_entry;
+
+
// multiboot2 magic check
#define MB2_MAGIC 0x36D76289
@@ -35,6 +52,9 @@ typedef struct mb2_tag_fb mb2_tag_fb;
#define MB2_TAG_VBE 7
#define MB2_TAG_FB 8
-void init_fb(mb2_tag_header* multiboot_bootinfo, uint32_t multiboot_magic);
+
+void read_mb2(mb2_tag_header* multiboot_bootinfo, uint32_t multiboot_magic);
+void init_fb(mb2_tag_fb* tag_fb);
+void init_mmap(mb2_tag_mmap* tag_mmap);
#endif
diff --git a/kernel/src/boot/multiboot2.c b/kernel/src/boot/multiboot2.c
index 9c2998f..d0b0667 100644
--- a/kernel/src/boot/multiboot2.c
+++ b/kernel/src/boot/multiboot2.c
@@ -2,14 +2,59 @@
#include <multiboot2.h>
#include <graphics.h>
-#include <debug.h>
#include <paging.h>
+#include <libk/stdio.h>
+#include <libk/string.h>
+#include <libk/list.h>
+#include <heap.h>
+
#define KERNEL_VMA 0xc0000000
/* https://www.gnu.org/software/grub/manual/multiboot2/html_node/Boot-information-format.html */
-void init_fb(mb2_tag_header* multiboot_bootinfo, uint32_t multiboot_magic)
+void init_fb(mb2_tag_fb* tag_fb)
+{
+ main_fb.addr = tag_fb->framebuffer_addr;
+ main_fb.width = tag_fb->framebuffer_width;
+ main_fb.height = tag_fb->framebuffer_height;
+ main_fb.pitch = tag_fb->framebuffer_pitch;
+ main_fb.bpp = tag_fb->framebuffer_bpp;
+
+ // identity map framebuffer address
+ map_addr(main_fb.addr, main_fb.addr, FLAG_PRESENT + FLAG_WRITABLE + FLAG_HUGE);
+ map_addr(main_fb.addr + PAGE_SIZE, main_fb.addr + PAGE_SIZE, FLAG_PRESENT + FLAG_WRITABLE + FLAG_HUGE);
+}
+
+void init_mmap(mb2_tag_mmap* tag_mmap)
+{
+ list_t* mmap = NULL;
+
+ // get data and store it into list
+ for (size_t i = sizeof(mb2_tag_mmap); i < tag_mmap->size; i += sizeof(mb2_tag_mmap_entry)) {
+ mb2_tag_mmap_entry* mmap_entry;
+ mmap_entry = (mb2_tag_mmap_entry*)kalloc(sizeof(mb2_tag_mmap_entry));
+ memcpy(mmap_entry, (char*)tag_mmap + i, sizeof(mb2_tag_mmap_entry));
+ add_to_list_head(&mmap, mmap_entry);
+ }
+
+ // print data from list
+ for (list_t* tmp = mmap; tmp != NULL; tmp = tmp->next) {
+ mb2_tag_mmap_entry* mmap_entry;
+ mmap_entry = tmp->data;
+ printf("base_addr: 0x%x, length: 0x%x, type: %d\n", mmap_entry->base_addr, mmap_entry->length, mmap_entry->type);
+ }
+
+ // free data
+ for (list_t* tmp = mmap; tmp != NULL; tmp = tmp->next) {
+ kfree(tmp->data);
+ }
+
+ // free list
+ free_list(&mmap);
+}
+
+void read_mb2(mb2_tag_header* multiboot_bootinfo, uint32_t multiboot_magic)
{
if (multiboot_magic != MB2_MAGIC) {
// not loaded by multiboot2 bootloader
@@ -18,6 +63,7 @@ void init_fb(mb2_tag_header* multiboot_bootinfo, uint32_t multiboot_magic)
// we will store framebuffer information here
static mb2_tag_fb* tag_fb;
+ static mb2_tag_mmap* tag_mmap;
// skip first 8 bytes (total_size + reserved)
mb2_tag_header* tag_header = (mb2_tag_header*)((char*)multiboot_bootinfo + 8 + KERNEL_VMA);
@@ -28,6 +74,9 @@ void init_fb(mb2_tag_header* multiboot_bootinfo, uint32_t multiboot_magic)
case MB2_TAG_FB:
tag_fb = (mb2_tag_fb*)tag_header;
break;
+ case MB2_TAG_MMAP:
+ tag_mmap = (mb2_tag_mmap*)tag_header;
+ break;
default:
break;
}
@@ -36,13 +85,6 @@ void init_fb(mb2_tag_header* multiboot_bootinfo, uint32_t multiboot_magic)
tag_header += tag_header->size / 8 + ((tag_header->size % 8) > 0);
}
- main_fb.addr = tag_fb->framebuffer_addr;
- main_fb.width = tag_fb->framebuffer_width;
- main_fb.height = tag_fb->framebuffer_height;
- main_fb.pitch = tag_fb->framebuffer_pitch;
- main_fb.bpp = tag_fb->framebuffer_bpp;
-
- // identity map framebuffer address
- map_addr(main_fb.addr, main_fb.addr, FLAG_PRESENT + FLAG_WRITABLE + FLAG_HUGE);
- map_addr(main_fb.addr + PAGE_SIZE, main_fb.addr + PAGE_SIZE, FLAG_PRESENT + FLAG_WRITABLE + FLAG_HUGE);
+ init_fb(tag_fb);
+ init_mmap(tag_mmap);
}
diff --git a/kernel/src/libk/list.c b/kernel/src/libk/list.c
new file mode 100644
index 0000000..5f75845
--- /dev/null
+++ b/kernel/src/libk/list.c
@@ -0,0 +1,37 @@
+#include <libk/list.h>
+#include <heap.h>
+
+void add_to_list_head(list_t** ptr, void* data)
+{
+ list_t* node = (list_t*)kalloc(sizeof(list_t));
+ node->data = data;
+
+ node->next = *ptr;
+ *ptr = node;
+}
+
+void add_to_list_tail(list_t** ptr, void* data)
+{
+ list_t* node = (list_t*)kalloc(sizeof(list_t));
+ node->data = data;
+
+ if (*ptr == NULL) {
+ *ptr = node;
+ } else {
+ list_t* tmp = *ptr;
+ while (tmp->next != NULL)
+ tmp = tmp->next;
+ tmp->next = node;
+ }
+}
+
+void free_list(list_t** ptr)
+{
+ if (*ptr == NULL)
+ return;
+
+ for (list_t* tmp = (*ptr)->next; tmp != NULL; tmp = tmp->next) {
+ kfree(*ptr);
+ *ptr = tmp;
+ }
+}
diff --git a/kernel/src/main.c b/kernel/src/main.c
index 95951bd..a3303e4 100644
--- a/kernel/src/main.c
+++ b/kernel/src/main.c
@@ -16,23 +16,7 @@ int kernel_main(mb2_tag_header* multiboot_bootinfo, uint32_t multiboot_magic)
init_paging();
init_idt();
init_heap();
- init_fb(multiboot_bootinfo, multiboot_magic);
-
- size_t n = 15;
- uint16_t* a = (uint16_t*)kalloc(sizeof(uint16_t) * (uint32_t)n);
- for (size_t i = 0; i < n; i++) {
- a[i] = (uint16_t)i + 250;
- }
-
- uint8_t* b = (uint8_t*)kalloc(sizeof(uint16_t) * (uint32_t)n);
- memcpy(b, a, sizeof(uint16_t) * n);
-
- for (size_t i = 0; i < 2 * n; i++) {
- printf("b[%d] = %d\n", i, b[i]);
- }
-
- __asm__ volatile ("movq $4, 0x1000000;");
-
+ read_mb2(multiboot_bootinfo, multiboot_magic);
for(;;) {
__asm__ volatile ("hlt;");