diff --git a/include/buildnumber.h b/include/buildnumber.h index df55226..62f77fa 100644 --- a/include/buildnumber.h +++ b/include/buildnumber.h @@ -1,10 +1,10 @@ #ifndef BUILD_NUMBER -#define BUILD_NUMBER "276" +#define BUILD_NUMBER "349" #endif #ifndef VERSION_STR -#define VERSION_STR "0.0.2+276 - Sat 31 Aug 2019 05:43:54 PM CDT" +#define VERSION_STR "0.0.2+349 - Sun 01 Sep 2019 03:15:31 PM CDT" #endif #ifndef VERSION_STR_SHORT #define VERSION_STR_SHORT -unsigned char* version_str_short = "0.0.2+276"; +unsigned char* version_str_short = "0.0.2+349"; #endif diff --git a/include/memorymanager.h b/include/memorymanager.h index 9863004..42aca07 100644 --- a/include/memorymanager.h +++ b/include/memorymanager.h @@ -8,7 +8,7 @@ typedef struct MemoryChunk struct MemoryChunk* Prev; struct MemoryChunk* Next; size_t Size; - bool Allocated; + bool Allocated; }; static struct MemoryChunk* First; @@ -19,4 +19,4 @@ void* Malloc(size_t size); void Free(void* ptr); -#endif \ No newline at end of file +#endif diff --git a/include/system.h b/include/system.h index 6374e91..0901d40 100644 --- a/include/system.h +++ b/include/system.h @@ -6,6 +6,7 @@ typedef unsigned int size_t; typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; +typedef unsigned long long uint64_t; /* MAIN.C */ extern unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count); extern unsigned char *memset(unsigned char *dest, unsigned char val, int count); @@ -36,4 +37,4 @@ void timer_handler(struct regs *r); void timer_install(); /* KB.C */ void kb_install(); -#endif \ No newline at end of file +#endif diff --git a/makefile b/makefile index ecbe8eb..f896d40 100644 --- a/makefile +++ b/makefile @@ -15,7 +15,7 @@ objects = obj/start.o \ obj/shell.o \ obj/memorymanager.o \ obj/pci.o \ - obj/port.o + obj/port.o \ run:kernel.iso qemu-system-x86_64 -m 256M -cdrom kernel.iso & diff --git a/src/kernel.c b/src/kernel.c index f609c70..ffd5f1d 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -111,27 +111,17 @@ int string_to_int(char* str) return sum; } -//Not working unsigned char strconcat(unsigned char* dest, unsigned char* src) { - //char *temp = (char *)dest; int dest_length = strlen(dest); int src_length = strlen(src); - unsigned char* buffer = (unsigned char*)Malloc(1024);//dest_length + src_length + 1); - - for(uint32_t i = dest_length; i != 0; i--) - { - buffer[i] = dest - i; - puts(dest--); - } - puts("\n"); - for(uint32_t j = 0; j < src_length; j++) - { - buffer[j + dest_length - 1] = src + j; - puts(src--); - } - puts("\n"); - return buffer;// - (dest_length + src_length + 1); + + uint8_t* buffer = Malloc(dest_length + src_length); + unsigned char str = (unsigned char*) *buffer; + memcpy(str, dest, dest_length); + memcpy((str + dest_length), src, src_length + 1); + Free((void*) buffer); + return str; } /* We will use this later on for reading from the I/O ports to get data @@ -214,24 +204,15 @@ extern void main(uint32_t multiboot_magic, const void* multiboot_structure) puts("Heap size: "); puts(int_to_string(heap / 1024, 10)); puts(" in kilobytes.\n"); - //struct MemoryChunk* allocated = (struct MemoryChunk*) Malloc(2048); - //size_t size = allocated->Next->Size; - //if(First) puts("Yes"); - //else puts("no"); - puts("First next address: 0x"); - puts(int_to_string(First->Next, 16)); - //puts("\n"); - puts("\nFirst Size: 0x"); - puts(int_to_string(First->Size, 16)); - puts("\n"); - struct MemoryChunk* al = (struct MemoryChunk*) (Malloc(3096) - sizeof(struct MemoryChunk)); - puts("AL size: (0x)"); - puts(int_to_string(al->Size, 10)); - puts("\n"); - puts("First next address: 0x"); - puts(int_to_string(First->Next, 16)); - puts("\n"); + struct MemoryChunk* al = (struct MemoryChunk*) Malloc(2984); + struct MemoryChunk* al2 = (struct MemoryChunk*) Malloc(3984); + //for(int i = 0; i < 600; i++) + //{ + // target[i] = 9; + //} + Free((void*) al); + Free((void*) al2); idt_install(); puts("Interrupt Descriptor Table initialized.\n"); @@ -253,12 +234,17 @@ extern void main(uint32_t multiboot_magic, const void* multiboot_structure) puts("Compile Date "); puts(__DATE__); puts("\n"); + unsigned char result = strconcat("Hello", " World!\n"); + puts(result); + //Free(eh); //puts(strconcat("Hello ", "World!\n")); //puts(strconcat("Concat test ", "number 2\n")); //puts(__DATE__); //puts("\n"); //puts(__TIME__); //puts("\n"); + puts("Setting up paging\n"); + //setupstuff(); run_shell(); //puts(&__BUILD_DATE); //puts(&__BUILD_NUMBER); @@ -271,3 +257,34 @@ void idle(void) asm("hlt"); //__asm__ __volatile__ ("hlt"); } + +void setupstuff() +{ +uint64_t page_dir_ptr_tab[4] __attribute__((aligned(0x20))); // must be aligned to (at least)0x20, ... + // ... turning out that you can put more of them into one page, saving memory + + // 512 entries +uint64_t page_dir[512] __attribute__((aligned(0x1000))); // must be aligned to page boundary +uint64_t page_tab[512] __attribute__((aligned(0x1000))); + +page_dir_ptr_tab[0] = (uint64_t)&page_dir | 1; // set the page directory into the PDPT and mark it present +page_dir[0] = (uint64_t)&page_tab | 3; //set the page table into the PD and mark it present/writable + +unsigned int i, address = 0; +for(i = 0; i < 512; i++) +{ + page_tab[i] = address | 3; // map address and mark it present/writable + address = address + 0x1000; +} + +asm volatile ("movl %cr4, %eax; bts $5, %eax; movl %eax, %cr4"); // set bit5 in CR4 to enable PAE +asm volatile ("movl %%eax, %%cr3" :: "a" (&page_dir_ptr_tab)); // load PDPT into CR3 +asm volatile ("movl %cr0, %eax; orl $0x80000000, %eax; movl %eax, %cr0;"); + +//uint64_t * page_dir_temp = (uint64_t*) page_dir_ptr_tab[3]; // get the page directory (you should 'and' the flags away) +page_dir[511] = (uint64_t*) page_dir_ptr_tab[3];//(uint64_t)page_dir_temp; // map pd to itself +page_dir[510] = page_dir_ptr_tab[2]; // map pd3 to it +page_dir[509] = page_dir_ptr_tab[1]; // map pd2 to it +page_dir[508] = page_dir_ptr_tab[0]; // map pd1 to it +page_dir[507] = (uint64_t)&page_dir_ptr_tab; /* map the PDPT to the directory*/ +} diff --git a/src/memorymanager.c b/src/memorymanager.c index 3325326..fecac63 100644 --- a/src/memorymanager.c +++ b/src/memorymanager.c @@ -59,22 +59,34 @@ void* Malloc(size_t size) freeChunk->Allocated = true; puts("Address of new chunk: 0x"); - puts(int_to_string((size_t)freeChunk + sizeof(struct MemoryChunk), 16)); + puts(int_to_string((size_t)freeChunk, 16)); puts("\n"); return (void*)(((size_t) freeChunk) + sizeof(struct MemoryChunk)); } void Free(void* ptr) { - struct MemoryChunk* chunk = (struct MemoryChunk*)((size_t) ptr - sizeof(struct MemoryChunk)); + puts("Recieved pointer at 0x"); + puts(int_to_string(ptr - sizeof(struct MemoryChunk), 16)); + puts("\n"); + struct MemoryChunk* chunk = (struct MemoryChunk*)(((size_t) ptr) - sizeof(struct MemoryChunk)); chunk->Allocated = false; if(chunk->Prev != 0 && !chunk->Prev->Allocated) { chunk->Prev->Next = chunk->Next; + chunk->Prev->Size += chunk->Size + sizeof(struct MemoryChunk); + if(chunk->Next != 0) + chunk->Next->Prev = chunk->Prev; + } - + if(chunk->Next != 0 && !chunk->Next->Allocated) + { + chunk->Size += chunk->Next->Size + sizeof(struct MemoryChunk); + chunk->Next = chunk->Next->Next; + if(chunk->Next != 0) + chunk->Next->Prev = chunk; } } diff --git a/src/shell.c b/src/shell.c index f2143f4..4265a26 100644 --- a/src/shell.c +++ b/src/shell.c @@ -104,7 +104,19 @@ void execmd() void proccess_command(char *text) { - int length = strlen(text); + int cmdlets = 0; + int length = strlen(text); + for(int i = 0; i < length; i++) + { + if(text[i] == ' ') + { + puts("Whitespace found!\n"); + cmdlets++; + } + } + puts(strconcat("CMDLET count: ", int_to_string(cmdlets, 10))); + unsigned char *commands[cmdlets][length]; + return; int currentArrayIndex = 0; if(length == 0) return; unsigned char *array[length];