Update data types. Update Makefile. Update kernel. Implement Process::allocate.

This commit is contained in:
Xnoe 2021-10-28 14:14:36 +01:00
parent 768ceddef0
commit 4a271865e0
10 changed files with 79 additions and 23 deletions

View File

@ -1,11 +1,11 @@
CFLAGS = -std=gnu11 -m32 -mgeneral-regs-only -nostdlib -fno-builtin -fno-exceptions -fno-leading-underscore -fno-pie -fno-stack-protector -Wno-pointer-to-int-cast
CXXFLAGS = -m32 -fno-use-cxa-atexit -mgeneral-regs-only -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -fpermissive -fno-pie -fno-stack-protector
CXXFLAGS = -m32 -fno-use-cxa-atexit -mgeneral-regs-only -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -fpermissive -fno-pie -fno-stack-protector -I.
LDFLAGS =
DISK_IMG_FILES = build/kernel/kernel.bin
DISK_IMG_FILES = build/kernel/kernel.bin hello.txt alpha.txt
KERNEL_OBJS = build/kernel/entry.o build/kernel/screenstuff.o build/kernel/io.o build/kernel/idt.o build/kernel/keyboard.o \
build/kernel/strings.o build/kernel/atapio.o build/kernel/kernel.o build/kernel/paging.o build/kernel/allocate.o \
build/kernel/gdt.o build/kernel/memory.o build/kernel/process.o
build/kernel/gdt.o build/kernel/memory.o build/kernel/process.o build/kernel/datatypes/hash.o
STAGE2_OBS = build/c_code_entry.o build/boot_stage2/io.o build/boot_stage2/atapio.o build/boot_stage2/strings.o build/boot_stage2/screenstuff.o build/boot_stage2/stage2.o build/boot_stage2/paging.o
run: disk.img
@ -17,7 +17,6 @@ disk.img: clean prepare build/boot/boot.bin build/boot_stage2/boot.bin $(DISK_IM
dd obs=512 seek=1 if=build/boot_stage2/boot.bin of=disk.img conv=notrunc
mount disk.img img.d
cp $(DISK_IMG_FILES) img.d/
cp hello.txt img.d/
umount img.d
chmod 777 disk.img

1
alpha.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,11 @@
#include "hash.h"
namespace xnoe {
template<class T>
uint32_t hash(T t){}
template<>
uint32_t hash<void*>(void* t) {
return (uint32_t) t;
}
}

View File

@ -1,14 +1,14 @@
#ifndef HASH_H
#define HASH_H
#include "../types.h"
namespace xnoe {
template<class T>
uint32_t hash(T t);
template<>
uint32_t hash<void*>(void* t) {
return (uint32_t) t;
}
uint32_t hash<void*>(void* t);
}
#endif

View File

@ -3,7 +3,7 @@
#include "hash.h"
#include "linkedlist.h"
#include "memory.h"
#include "../memory.h"
namespace xnoe {
template <class key, class value>

View File

@ -1,7 +1,7 @@
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "memory.h"
#include "../memory.h"
namespace xnoe {
template<typename T>
@ -10,10 +10,10 @@ namespace xnoe {
linkedlistelem<T>* previous;
linkedlistelem<T>* next;
linkedlistelem(T t, linkedlistelem<T>* p, linkedlistelem<T>* n) {
linkedlistelem(T t) {
this->elem = t;
this->previous = p;
this->next = n;
this->previous = 0;
this->next = 0;
}
};
@ -31,6 +31,8 @@ namespace xnoe {
void prepend(linkedlistelem<T>* t) {
this->start->previous = t;
t->next = this->start;
t->prev = 0;
this->start = t;
}

View File

@ -16,6 +16,7 @@ namespace xnoe {
T head;
tuple<Tail...> tail;
tuple() {}
tuple(const T& head, const Tail& ... tail)
: head(head), tail(tail...) {}
};

View File

@ -9,9 +9,12 @@
#include "paging.h"
#include "allocate.h"
#include "memory.h"
#include "process.h"
#include "datatypes/hashtable.h"
int main() {
init_gdt();
init_term();
PageDirectory kernel_pd = PageDirectory(0xc0100000, 0x120000, 0xbffe0000);
kernel_pd.select();
@ -22,17 +25,25 @@ int main() {
Allocator kernel_allocator = Allocator(&kernel_pd, &phys_pm, &virt_pm, 0xd0000000);
uint8_t* test2 = new(&kernel_allocator)uint8_t;
uint8_t* test = new(&kernel_allocator)uint8_t[1024];
xnoe::hashtable<void*, uint32_t>* kproc_hashtable = new (&kernel_allocator) xnoe::hashtable<void*, uint32_t>(&kernel_allocator);
Process kernel_process = Process(0, kproc_hashtable, &kernel_pd, &phys_pm, &virt_pm, 0xd0000000);
uint32_t* test = new(&kernel_process)uint32_t;
uint32_t* test2 = new(&kernel_process)uint32_t;
uint32_t* test3 = new(&kernel_process)uint32_t[1024];
*test = 0xdead;
*test2 = 0xbeef;
init_idt();
init_term();
printf("Hello, World!\n\nWe are running XnoeOS Code in C now, Protected Mode has been achieved (as well as Virtual Memory / Paging!!!) and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~\n");
printf("KERNEL OK!\n");
printf("Test: %x\nTest 2:%x\n", test, test2);
printf("Test :%x\nTest 2:%x\nTest 3:%x\n", test, test2, test3);
printf("Test value :%x\nTest 2 value:%x\n", *test, *test2);
init_keyboard();
@ -43,7 +54,7 @@ int main() {
read_sector(0, sector);
uint8_t* filebuffer = (uint8_t*)dumb_alloc(0x3000);
uint8_t* filebuffer = new(&kernel_process)uint8_t[0x3000];
while (1) {
printf(">>> ");

View File

@ -1,14 +1,42 @@
#include "process.h"
Process::Process(uint32_t PID, xnoe::hashtable<void*, xnoe::tuple<uint32_t, uint32_t>>* table, PageDirectory* page_directory, PageMap* phys, PageMap* virt, uint32_t virt_alloc_base)
Process::Process(uint32_t PID, xnoe::hashtable<void*, uint32_t>* table, PageDirectory* page_directory, PageMap* phys, PageMap* virt, uint32_t virt_alloc_base)
: Allocator(page_directory, phys, virt, virt_alloc_base) {
this->PID = PID;
this->allocmap = table;
this->last_page_pointer = 0;
this->page_remaining = 0;
this->last_page_pointer = virt_alloc_base;
}
void* Process::allocate(uint32_t size) {
uint32_t elem_size = sizeof(xnoe::linkedlistelem<xnoe::tuple<uint32_t, uint32_t>>);
printf("Elem size: %d\n", elem_size);
uint32_t actualsize = size + elem_size;
void* ptr = 0;
if (this->page_remaining > actualsize) {
ptr = last_page_pointer + (4096 - page_remaining);
page_remaining -= actualsize;
printf("Non-allocating return\nptr: %x\nremaining: %d\n", ptr, page_remaining);
} else if (actualsize % 4096 == 0) {
ptr = Allocator::allocate(actualsize);
page_remaining = 0;
printf("Whole allocating return\nptr: %x\n", ptr);
} else {
uint32_t remaining = 4096 - (actualsize % 4096);
uint32_t fpindex = actualsize >> 12;
void* alloc_base = Allocator::allocate(actualsize);
ptr = last_page_pointer + (4096 - page_remaining);
last_page_pointer = alloc_base + (fpindex<<12);
page_remaining = remaining;
printf("Partially allocating return\nptr: %x\nremaining: %d\n", ptr, page_remaining);
}
*(xnoe::linkedlistelem<xnoe::tuple<uint32_t, uint32_t>>*)ptr = xnoe::linkedlistelem<xnoe::tuple<uint32_t, uint32_t>>(xnoe::tuple<uint32_t, uint32_t>((uint32_t)ptr, actualsize));
return ptr + elem_size;
}
void Process::deallocate(uint32_t virt_addr) {

View File

@ -5,19 +5,22 @@
#include "memory.h"
#include "datatypes/linkedlist.h"
#include "datatypes/hashtable.h"
#include "screenstuff.h"
class Process : protected Allocator {
class Process : public Allocator {
private:
uint32_t PID;
uint32_t last_page_pointer;
uint32_t page_usage;
uint32_t page_remaining;
// List of tuples tracking allocation base addresses and lengths.
xnoe::linkedlist<xnoe::tuple<uint32_t, uint32_t>> allocations;
xnoe::hashtable<void*, xnoe::tuple<uint32_t, uint32_t>>* allocmap;
// Maps pointers to the position in the linked list.
xnoe::hashtable<void*, uint32_t>* allocmap;
public:
Process(uint32_t PID, xnoe::hashtable<void*, xnoe::tuple<uint32_t, uint32_t>>* table, PageDirectory* page_directory, PageMap* phys, PageMap* virt, uint32_t virt_alloc_base);
Process(uint32_t PID, xnoe::hashtable<void*, uint32_t>* table, PageDirectory* page_directory, PageMap* phys, PageMap* virt, uint32_t virt_alloc_base);
void* allocate(uint32_t size) override;
void deallocate(uint32_t virt_addr) override;