diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 20 | ||||
-rw-r--r-- | src/boot.h | 24 | ||||
-rw-r--r-- | src/conf.h.in | 4 | ||||
-rw-r--r-- | src/kernel.cpp | 59 | ||||
-rw-r--r-- | src/mem.h | 4 | ||||
-rw-r--r-- | src/mem/vmm.c | 49 | ||||
-rw-r--r-- | src/mmap.c | 45 | ||||
-rw-r--r-- | src/mmap.h | 7 | ||||
-rw-r--r-- | src/multiboot2.c | 49 | ||||
-rw-r--r-- | src/sched.hpp | 8 | ||||
-rw-r--r-- | src/sched/roundrobin.cpp | 21 | ||||
-rw-r--r-- | src/task.h | 78 | ||||
-rw-r--r-- | src/tst/roundrobin.cc | 50 | ||||
-rw-r--r-- | src/tst/taskqueue.cc | 122 |
14 files changed, 0 insertions, 540 deletions
diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index f9ab6fe..0000000 --- a/src/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -include ../Makefile.config - -INCLUDES := -isystem../grub -I../${ARCH} -I../devices -${ARCH}_CFLAGS += ${INCLUDES} -${ARCH}_CXXFLAGS += ${INCLUDES} - -TARGETLIB += kernel -kernel.SRCS := multiboot2.c mmap.c kernel.cpp mem/vmm.c -kernel.OBJS := conf.h - -TESTS += tst/taskqueue tst/roundrobin - -include ../rules.mk - -conf.h: conf.h.in - @echo ' GEN $@' - @cp conf.h.in conf.h - @sed -i 's/@VERSION@/$(shell git describe)/' conf.h - @sed -i 's/@CC@/${${ARCH}_CCID}/' conf.h - diff --git a/src/boot.h b/src/boot.h deleted file mode 100644 index 8d62bdc..0000000 --- a/src/boot.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - // kernel command line - char cmdline[64]; - - // memory map - unsigned bitmap[1024 * 32]; - - // module - unsigned module_start; - unsigned module_end; - char module_cmdline[64]; -} boot_info_t; - -_Static_assert((1024 * 32 * sizeof(unsigned) * 8) == (1024 * 1024), "bitmap size check"); - -#ifdef __cplusplus -} -#endif diff --git a/src/conf.h.in b/src/conf.h.in deleted file mode 100644 index 52093a8..0000000 --- a/src/conf.h.in +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#define VERSION "@VERSION@" -#define CC "@CC@" diff --git a/src/kernel.cpp b/src/kernel.cpp deleted file mode 100644 index 063fe11..0000000 --- a/src/kernel.cpp +++ /dev/null @@ -1,59 +0,0 @@ -//===================================================================== -// glitch kernel -// spdx-license-identifier: ISC -// description: kernel entry point -//===================================================================== - -extern "C" { -#include "conf.h" -#include "mem.h" -#include <keyboard.h> -#include <mouse.h> -#include <pic.h> -#include <ps2_controller.h> -#include <stdio.h> -#include <sys/cpuid.h> -} -#include <uart.hpp> -#include <vga.hpp> - -FILE *stdin; -FILE *stdout; -FILE *stderr; - -extern "C" void -kmain() -{ - stderr = uart_init<COM1>(); - vmm_map(0xb8000, 0xc03ff000); - stdout = vga_init((void *)0xc03ff000); - - printf("glitch [version " VERSION "] [" CC "]\n"); - fprintf(stderr, "glitch [version " VERSION "] [" CC "]\n"); - { - char vendor[13] = {'\0'}; - unsigned int eax; - __get_cpuid(0, &eax, (unsigned int *)vendor, (unsigned int *)(vendor + 8), (unsigned int *)(vendor + 4)); - struct CPUVersion v; - __get_cpuid(1, (unsigned int *)&v, &eax, &eax, &eax); - printf("CPU: %s family %u model %u stepping %u\n", vendor, family(v), model(v), v.stepping); - fprintf(stderr, "CPU: %s family %u model %u stepping %u\n", vendor, family(v), model(v), v.stepping); - } - - pic_init(); - - ps2_ctrl_init(); - ps2_keyboard_init(); - mouse_init(); - - pic_enable(); - fprintf(stderr, "interrupts enabled\n"); - - /* - alloc4M(); - char *c = (char *)0xc0700000; - if (*c == 0) printf("c is 0\r\n"); - */ - - while (1) {} -} diff --git a/src/mem.h b/src/mem.h deleted file mode 100644 index e06dd21..0000000 --- a/src/mem.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -unsigned int vmm_map(unsigned int paddr, unsigned int vaddr); -void alloc4M(); diff --git a/src/mem/vmm.c b/src/mem/vmm.c deleted file mode 100644 index 77b06a8..0000000 --- a/src/mem/vmm.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "../mem.h" -#include <paging.h> - -extern struct DirectoryEntry k_pagedir[1024]; -extern struct TableEntry k_ptable0x300[1024]; - -extern const unsigned MULTIBOOT_SIZE; -extern const unsigned VADDR_BASE; - -unsigned -to_vaddr(unsigned paddr) -{ - return paddr + (unsigned)&VADDR_BASE - (unsigned)&MULTIBOOT_SIZE; -} - -unsigned int -vmm_map(unsigned int paddr, unsigned int vaddr) -{ - if (paddr & 0xfff || vaddr & 0xfff) return 0; - - const unsigned table_idx = vaddr >> 22; // high 10 bits - const unsigned entry_idx = (vaddr >> 12) & 0x3ff; // low 10 bits - - if (k_pagedir[table_idx].present == 0) return 0; - struct TableEntry *table = (struct TableEntry *)to_vaddr(k_pagedir[table_idx].address << 12); - - table[entry_idx].address = paddr >> 12; - table[entry_idx].present = 1; - table[entry_idx].writeable = 1; - - return vaddr; -} - -void -alloc4M() -{ - // enable pse in cr4 - asm volatile(R"( - movl %cr4, %eax - orl $0x10, %eax - movl %eax, %cr4 -)"); - - struct DirectoryEntry4MB *directory = (struct DirectoryEntry4MB *)&k_pagedir[0x301]; - directory->address_low = 0x1; - directory->present = 1; - directory->writeable = 1; - directory->pagesize = 1; -} diff --git a/src/mmap.c b/src/mmap.c deleted file mode 100644 index 3fe35b5..0000000 --- a/src/mmap.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "mmap.h" -#ifdef DEBUG -#include <stdio.h> -#endif - -__attribute__((section(".multiboot.text"))) unsigned -multiboot2_mmap(const struct multiboot_mmap_entry entries[], unsigned entry_count, unsigned bitmap[1024 * 32]) -{ - // clear out the bitmap - for (unsigned i = 0; i < 1024 * 32; ++i) bitmap[i] = 0; - unsigned avail_frames = 0; - - // loop through all the mmap_entry structures where type is MULTIBOOT_MEMORY_AVAILABLE - for (unsigned i = 0; i < entry_count; ++i) { - if (entries[i].type != MULTIBOOT_MEMORY_AVAILABLE) continue; - - // number of frames in this entry - unsigned n_frames = entries[i].len / 4096; - avail_frames += n_frames; - -#ifdef DEBUG - printf("mmap_entry: 0x%16llx\tlen=%12llu\t%d frames (%d blocks + %d)\n", entries[i].addr, entries[i].len, n_frames, - n_frames / 32, n_frames % 32); -#endif - - // the bitmap is an array of blocks, each holding 32 (2^5) values - unsigned table_idx = (entries[i].addr >> 17); // get the upper 15 bits - - while (n_frames != 0) { - if (n_frames >= 32) { - bitmap[table_idx] = 0xffffffff; - table_idx++; - n_frames -= 32; - } - else { - unsigned block = bitmap[table_idx]; - for (unsigned l = 0; l < n_frames; ++l) block |= (1 << l); - bitmap[table_idx] = block; - n_frames = 0; - } - } - } - - return avail_frames; -} diff --git a/src/mmap.h b/src/mmap.h deleted file mode 100644 index 13f40f2..0000000 --- a/src/mmap.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include "boot.h" -#include <multiboot2.h> - -__attribute__((section(".multiboot.text"))) unsigned multiboot2_mmap(const struct multiboot_mmap_entry entries[], - unsigned entry_count, unsigned bitmap[1024 * 32]); diff --git a/src/multiboot2.c b/src/multiboot2.c deleted file mode 100644 index ea06e96..0000000 --- a/src/multiboot2.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "mmap.h" - -#define ADDR(x) ((unsigned)&x - 0xc0000000 + 0x2000) - -boot_info_t info __attribute__((section(".init"))); - -__attribute__((section(".multiboot.text"))) void -multiboot_strncpy(char *dest, const char *src, unsigned n) -{ - for (unsigned i = 0; i < n && src[i] != '\0'; ++i) dest[i] = src[i]; -} - -__attribute__((section(".multiboot.text"))) void -multiboot2_module(struct multiboot_tag_module *tag, boot_info_t *tag_info) -{ - tag_info->module_start = tag->mod_start; - tag_info->module_end = tag->mod_end; - multiboot_strncpy(tag_info->module_cmdline, tag->cmdline, 64); -} - -/** - * parse multiboot2 structures - */ -__attribute__((section(".multiboot.text"))) void -__multiboot2(multiboot_uint32_t addr) -{ - boot_info_t *__info = (boot_info_t *)ADDR(info); - - struct multiboot_tag *tag; - for (tag = (struct multiboot_tag *)(addr + 8); tag->type != MULTIBOOT_TAG_TYPE_END; - tag = (struct multiboot_tag *)((multiboot_uint8_t *)tag + ((tag->size + 7u) & ~7u))) { - - switch (tag->type) { - case MULTIBOOT_TAG_TYPE_CMDLINE: - multiboot_strncpy(__info->cmdline, ((struct multiboot_tag_string *)tag)->string, 64); - break; - case MULTIBOOT_TAG_TYPE_MODULE: - multiboot2_module((struct multiboot_tag_module *)tag, __info); - break; - case MULTIBOOT_TAG_TYPE_MMAP: - multiboot2_mmap(((struct multiboot_tag_mmap *)tag)->entries, - ((struct multiboot_tag_mmap *)tag)->size / ((struct multiboot_tag_mmap *)tag)->entry_size, - __info->bitmap); - break; - default: - break; - } // switch - } // for -} diff --git a/src/sched.hpp b/src/sched.hpp deleted file mode 100644 index cfa3ff0..0000000 --- a/src/sched.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include "task.h" - -class RoundRobinQueue : public Queue<Task> { -public: - [[nodiscard]] Task *next(int slice); -}; diff --git a/src/sched/roundrobin.cpp b/src/sched/roundrobin.cpp deleted file mode 100644 index c3d6cb6..0000000 --- a/src/sched/roundrobin.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "../sched.hpp" - -/// Each task is run for a time quantum or the remainder of its cpu burst -Task * -RoundRobinQueue::next(int slice) -{ - if (head == nullptr) return nullptr; - if (head->node->burst <= 0) { - delete head->node; - remove(head->node); - return next(slice); - } - - auto *it = head; - it->node->burst -= slice; - if (head->next) head = head->next; - it->next = nullptr; - tail->next = it; - tail = it; - return it->node; -} diff --git a/src/task.h b/src/task.h deleted file mode 100644 index 0d59bb1..0000000 --- a/src/task.h +++ /dev/null @@ -1,78 +0,0 @@ -#pragma once - -/** - * Representation of a task in the system - */ -struct Task { - const char *name; - int id; - int priority; - int burst; -}; - -#ifdef __cplusplus -template <typename T> struct Queue { - struct Item { - Item(T *p_node) : node(p_node) {} - T *node; - Item *next = nullptr; - - [[nodiscard]] bool - operator==(const T *other) const - { - return node == other; - } - }; - - ~Queue() noexcept - { - for (auto *it = head; it != nullptr;) { - auto *current = it; - it = it->next; - delete current->node; - delete current; - } - } - - /// Insert item at the end of the queue - void - insert(T *item) - { - if (head == nullptr) { - head = new Item(item); - tail = head; - } - else { - tail->next = new Item(item); - tail = tail->next; - } - } - - void - remove(T *item) - { - if (head == nullptr) return; - if (item == head->node) { - auto *it = head; - head = head->next; - if (*tail == item) tail = nullptr; - delete it; - return; - } - - Item *prev = nullptr; - for (auto *it = head; it != nullptr; it = it->next) { - if (it->node == item) { - if (prev) { prev->next = it->next; } - if (tail == it) { tail = prev; } - delete it; - return; - } - prev = it; - } - } - - Item *head = nullptr; - Item *tail = nullptr; -}; -#endif diff --git a/src/tst/roundrobin.cc b/src/tst/roundrobin.cc deleted file mode 100644 index 1431788..0000000 --- a/src/tst/roundrobin.cc +++ /dev/null @@ -1,50 +0,0 @@ -#include <chrono> -#include <gtest/gtest.h> -#include <iomanip> -#include <iostream> -#include <valgrind/valgrind.h> - -#include "../sched/roundrobin.cpp" - -void -run(Task *task, int slice) -{ - std::cout << "Running task " << task->name << " id=" << std::setw(2) << task->id << " prio=" << std::setw(2) - << task->priority << " burst=" << std::setw(2) << task->burst << " slice=" << slice << " "; -} - -struct DebugRoundRobinQueue : public RoundRobinQueue { -public: - void - print() const - { - for (auto *it = head; it != nullptr; it = it->next) { - std::cout << it->node->name << '(' << std::setw(2) << it->node->burst << ") "; - } - std::cout << std::endl; - } -}; - -TEST(roundrobin, RoundRobinQueue) -{ - DebugRoundRobinQueue queue; - queue.insert(new Task{"P1", 1, 1, 50}); - queue.insert(new Task{"P2", 2, 1, 40}); - queue.insert(new Task{"P3", 3, 1, 50}); - queue.insert(new Task{"P4", 4, 1, 40}); - - const auto begin = std::chrono::system_clock::now(); - for (auto *t = queue.next(10); t != nullptr; t = queue.next(10)) { - run(t, 10); - queue.print(); - } - const auto end = std::chrono::system_clock::now(); - const auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count(); - - std::cout << "Completed in (us): " << duration << std::endl; - // test should complete in 250us unless running on valgrind - if (!RUNNING_ON_VALGRIND) EXPECT_LE(duration, 250); - - EXPECT_EQ(queue.head, nullptr); - EXPECT_EQ(queue.tail, nullptr); -} diff --git a/src/tst/taskqueue.cc b/src/tst/taskqueue.cc deleted file mode 100644 index 217c44d..0000000 --- a/src/tst/taskqueue.cc +++ /dev/null @@ -1,122 +0,0 @@ -#include <gtest/gtest.h> - -#include "../sched.hpp" - -struct DebugQueue : public Queue<Task> { -public: - void - expect_ordered() const - { - int id = 1; - for (auto *it = head; it != nullptr; it = it->next) { - // std::cout << it->node->name << std::endl; - EXPECT_EQ(it->node->id, id++); - } - } -}; - -TEST(taskqueue, insert) -{ - DebugQueue queue; - auto *p1 = new Task{"P1", 1, 1, 10}; - queue.insert(p1); - queue.insert(new Task{"P2", 2, 1, 10}); - queue.insert(new Task{"P3", 3, 1, 10}); - auto *p4 = new Task{"P4", 4, 1, 10}; - queue.insert(p4); - queue.expect_ordered(); - - EXPECT_EQ(queue.head->node, p1); - EXPECT_EQ(queue.tail->node, p4); -} - -TEST(taskqueue, removeHead) -{ - DebugQueue queue; - auto *p0 = new Task{"P0", 0, 1, 10}; - queue.insert(p0); - auto *p1 = new Task{"P1", 1, 1, 10}; - queue.insert(p1); - queue.insert(new Task{"P2", 2, 1, 10}); - queue.insert(new Task{"P3", 3, 1, 10}); - auto *p4 = new Task{"P4", 4, 1, 10}; - queue.insert(p4); - queue.remove(p0); - delete p0; - - EXPECT_EQ(queue.head->node, p1); - EXPECT_EQ(queue.tail->node, p4); - queue.expect_ordered(); -} - -TEST(taskqueue, removeTail) -{ - DebugQueue queue; - auto *p1 = new Task{"P1", 1, 1, 10}; - queue.insert(p1); - queue.insert(new Task{"P2", 2, 1, 10}); - queue.insert(new Task{"P3", 3, 1, 10}); - auto *p4 = new Task{"P4", 4, 1, 10}; - queue.insert(p4); - auto *p5 = new Task{"P5", 5, 1, 10}; - queue.insert(p5); - EXPECT_EQ(queue.head->node, p1); - EXPECT_EQ(queue.tail->node, p5); - - queue.remove(p5); - delete p5; - - EXPECT_EQ(queue.head->node, p1); - EXPECT_EQ(queue.tail->node, p4); - queue.expect_ordered(); -} - -TEST(taskqueue, removeLast) -{ - DebugQueue queue; - - auto *p0 = new Task{"P0", 0, 1, 10}; - queue.insert(p0); - EXPECT_EQ(queue.head->node, p0); - EXPECT_EQ(queue.tail->node, p0); - - queue.remove(p0); - delete p0; - - EXPECT_EQ(queue.head, nullptr); - EXPECT_EQ(queue.tail, nullptr); -} - -TEST(taskqueue, removeNullptr) -{ - DebugQueue queue; - queue.insert(new Task{"P1", 1, 1, 10}); - queue.insert(new Task{"P2", 2, 1, 10}); - queue.insert(new Task{"P3", 3, 1, 10}); - queue.insert(new Task{"P4", 4, 1, 10}); - queue.remove(nullptr); - queue.expect_ordered(); -} - -TEST(taskqueue, remove) -{ - DebugQueue queue; - auto *p1 = new Task{"P1", 1, 1, 10}; - queue.insert(p1); - queue.insert(new Task{"P2", 2, 1, 10}); - auto *p0 = new Task{"P0", 0, 1, 10}; - queue.insert(p0); - queue.insert(new Task{"P3", 3, 1, 10}); - auto *p4 = new Task{"P4", 4, 1, 10}; - queue.insert(p4); - - EXPECT_EQ(queue.head->node, p1); - EXPECT_EQ(queue.tail->node, p4); - - queue.remove(p0); - delete p0; - - EXPECT_EQ(queue.head->node, p1); - EXPECT_EQ(queue.tail->node, p4); - queue.expect_ordered(); -} |