From 658a380f9af26ab803bdd949c5d21877fb862598 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Sat, 31 Aug 2019 17:50:14 -0500 Subject: [PATCH] Added a way to manually test the Malloc function from the shell's command line. --- include/buildnumber.h | 7 ++- include/memorymanager.h | 2 + make_buildnum.sh | 3 +- makefile | 5 +- src/kernel.c | 120 +++++++++++++++++++++++++++++----------- src/memorymanager.c | 33 ++++++++++- src/pci.c | 17 +++++- src/shell.c | 40 +++++++++++--- 8 files changed, 176 insertions(+), 51 deletions(-) diff --git a/include/buildnumber.h b/include/buildnumber.h index dd61b46..df55226 100644 --- a/include/buildnumber.h +++ b/include/buildnumber.h @@ -1,9 +1,10 @@ #ifndef BUILD_NUMBER -#define BUILD_NUMBER "117" +#define BUILD_NUMBER "276" #endif #ifndef VERSION_STR -#define VERSION_STR "0.0.2+117 - Sat Feb 9 21:35:00 CST 2019" +#define VERSION_STR "0.0.2+276 - Sat 31 Aug 2019 05:43:54 PM CDT" #endif #ifndef VERSION_STR_SHORT -#define VERSION_STR_SHORT "0.0.2+117" +#define VERSION_STR_SHORT +unsigned char* version_str_short = "0.0.2+276"; #endif diff --git a/include/memorymanager.h b/include/memorymanager.h index 72cac61..9863004 100644 --- a/include/memorymanager.h +++ b/include/memorymanager.h @@ -11,6 +11,8 @@ typedef struct MemoryChunk bool Allocated; }; +static struct MemoryChunk* First; + void MemoryManager(size_t start, size_t size); void* Malloc(size_t size); diff --git a/make_buildnum.sh b/make_buildnum.sh index 811749d..ae9435e 100644 --- a/make_buildnum.sh +++ b/make_buildnum.sh @@ -19,7 +19,8 @@ echo "#define VERSION_STR \"$version`sed 's/^ *//' build.number` - `date`\"" >> echo "#endif" >> include/buildnumber.h echo "#ifndef VERSION_STR_SHORT" >> include/buildnumber.h -echo "#define VERSION_STR_SHORT \"$version`sed 's/^ *//' build.number`\"" >> include/buildnumber.h +echo "#define VERSION_STR_SHORT" >> include/buildnumber.h +echo "unsigned char* version_str_short = \"$version`sed 's/^ *//' build.number`\";" >> include/buildnumber.h echo "#endif" >> include/buildnumber.h #echo "#ifndef COMPILE_DATE" >> include/buildnumber.h diff --git a/makefile b/makefile index 79a4bb6..ecbe8eb 100644 --- a/makefile +++ b/makefile @@ -40,7 +40,10 @@ kernel.iso: kernel.bin mkdir iso/boot mkdir iso/boot/grub cp obj/bin/kernel.bin iso/boot/kernel.bin - echo 'menuentry SDOS {' > iso/boot/grub/grub.cfg + echo 'set timeout=0' > iso/boot/grub/grub.cfg + echo 'set default=0' >> iso/boot/grub/grub.cfg + echo '' >> iso/boot/grub/grub.cfg + echo 'menuentry "SDOS" {' >> iso/boot/grub/grub.cfg echo ' multiboot /boot/kernel.bin' >> iso/boot/grub/grub.cfg echo ' boot' >> iso/boot/grub/grub.cfg echo '}' >> iso/boot/grub/grub.cfg diff --git a/src/kernel.c b/src/kernel.c index d402ddd..f609c70 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -12,7 +12,7 @@ unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count) { /* Add code here to copy 'count' bytes of data from 'src' to * 'dest', finally return 'dest' */ - const char *sp = (const char *)src; + const char *sp = (const char *)src; char *dp = (char *)dest; for(; count != 0; count--) *dp++ = *sp++; return dest; @@ -60,6 +60,18 @@ bool strcmp(char* str1, char* str2) return true; } + +bool strcmpstart(char* str1, char* str2) +{ + int len2 = strlen(str2); + + for(int i = 0; i < len2; i++) + { + if(str1[i] != str2[i]) return false; + } + + return true; +} //Code reference: //http://www.strudel.org.uk/itoa/ char* int_to_string(unsigned int value, int base) @@ -82,25 +94,44 @@ char* int_to_string(unsigned int value, int base) return &buf[i+1]; } -//Not working -unsigned char strconcat(unsigned char *dest, unsigned char* src) +int string_to_int(char* str) { + int i=0, sum=0; + + while(str[i]!='\0'){ + if(str[i]< 48 || str[i] > 57){ + return 0; + } + else{ + sum = sum*10 + (str[i] - 48); + i++; + } + } + + 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(dest_length + src_length + 1); + unsigned char* buffer = (unsigned char*)Malloc(1024);//dest_length + src_length + 1); - for(uint32_t i = 0; i < dest_length; i++) + for(uint32_t i = dest_length; i != 0; i--) { - *buffer++ = dest[i]; + buffer[i] = dest - i; + puts(dest--); } - + puts("\n"); for(uint32_t j = 0; j < src_length; j++) { - *buffer++ = src[j]; + buffer[j + dest_length - 1] = src + j; + puts(src--); } - *buffer++ = '\0'; - - return *buffer; + puts("\n"); + return buffer;// - (dest_length + src_length + 1); } /* We will use this later on for reading from the I/O ports to get data @@ -122,9 +153,21 @@ void outportb (unsigned short _port, unsigned char _data) __asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data)); } +//extern void* _sys_stack; + extern void main(uint32_t multiboot_magic, const void* multiboot_structure) { init_video(); + puts("Video initialized.\n"); + register int base asm("ebp"); + puts("Base pointer address: "); + puts(int_to_string(base, 10)); + puts("\n"); + register int pointer asm("esp"); + puts("Instruction pointer address: 0x"); + puts(int_to_string(pointer, 16)); + puts("\n"); + //Only for testing if the multiboot structure can be read or not. if(multiboot_magic == 0x2BADB002) { /* @@ -137,15 +180,16 @@ extern void main(uint32_t multiboot_magic, const void* multiboot_structure) puts("\n"); } else if (multiboot_magic == 0x36d76289) - { + { /* https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html */ puts("Multiboot 2 compliant bootloader detected.\n"); - } + //panic(); + } else { - puts("Unkown bootloader magic number detected:"); + puts("Unkown bootloader magic number detected: "); puts(int_to_string(multiboot_magic, 16)); puts("\n"); //panic(); @@ -158,27 +202,36 @@ extern void main(uint32_t multiboot_magic, const void* multiboot_structure) puts(int_to_string((*videoBuffer), 16)); puts("\n"); uint32_t* totalMem = (uint32_t*)(((size_t)multiboot_structure) + 8); - puts(int_to_string((*totalMem), 10)); + puts(int_to_string((*totalMem) * 1024, 10)); puts(" kilobytes of RAM installed on the system\n"); - puts("Video initialized.\n"); gdt_install(); puts("Global Descriptor Table initialized.\n"); - //size_t heap = 10 * 1024 * 1024; - //MemoryManager(heap, (*memupper)*1024 - heap - 10*1024); //second needs to be an address not size - //puts("heap: 0x"); - //puts(strconcat(int_to_string(heap, 16), "\n")); + //Set a new heap to the same size as the total system memory, in bytes. + size_t heap = ((*totalMem) - 10240) * 1024; + MemoryManager(heap, heap);//(*totalMem)*1024 - heap - 10*1024); //second needs to be an address not size + //puts("Mem lower: 0x"); + //puts(int_to_string(*(uint32_t*)(((size_t)multiboot_structure) + 4), 10)); + 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(allocated->Prev->Size) puts("Yes"); + //if(First) puts("Yes"); //else puts("no"); - //puts("\nAllocated: 0x"); - //puts(int_to_string(size, 16)); + 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"); - //void* al = Malloc(3096); - ///puts("\nAllocated: 0x"); - //puts(int_to_string(al, 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"); idt_install(); puts("Interrupt Descriptor Table initialized.\n"); @@ -187,15 +240,15 @@ extern void main(uint32_t multiboot_magic, const void* multiboot_structure) irq_install(); __asm__ __volatile__ ("sti"); puts("Interrupt Requests now allowed.\n"); - //puts("Probing PCI subsystem for devices.\n"); - //EnumPCISubsystem(); + puts("Probing PCI subsystem for devices.\n"); + EnumPCISubsystem(); //puts("Finished probing PCI subsystem.\n"); //timer_install(); //puts("Installed Timer.\n"); kb_install(); puts("Keyboard installed.\n"); puts("SDOS Version "); - puts(VERSION_STR_SHORT); + puts(version_str_short); puts(" Initialized.\n"); puts("Compile Date "); puts(__DATE__); @@ -213,7 +266,8 @@ extern void main(uint32_t multiboot_magic, const void* multiboot_structure) //puts(5 / 0); } -void idle() +void idle(void) { - __asm__ __volatile__ ("hlt"); -} \ No newline at end of file + asm("hlt"); + //__asm__ __volatile__ ("hlt"); +} diff --git a/src/memorymanager.c b/src/memorymanager.c index f9f5520..3325326 100644 --- a/src/memorymanager.c +++ b/src/memorymanager.c @@ -1,5 +1,6 @@ #include "../include/memorymanager.h" #include "../include/system.h" +#include "../include/scrn.h" static struct MemoryChunk* First; @@ -10,6 +11,7 @@ void MemoryManager(size_t start, size_t size) if(size < sizeof(struct MemoryChunk)) { First = 0; + puts("First memory block is null\n"); return; } @@ -19,6 +21,9 @@ void MemoryManager(size_t start, size_t size) First->Prev = 0; First->Next = 0; First->Size = size - sizeof(struct MemoryChunk); + puts("First block size "); + puts(int_to_string(First->Size, 10)); + puts(" bytes.\n"); } void* Malloc(size_t size) @@ -28,7 +33,9 @@ void* Malloc(size_t size) for(struct MemoryChunk* chunk = First; chunk != 0 && freeChunk == 0; chunk = chunk->Next) { if(chunk->Size > size && !chunk->Allocated) + { freeChunk = chunk; + } } if(freeChunk == 0) @@ -51,7 +58,9 @@ 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("\n"); return (void*)(((size_t) freeChunk) + sizeof(struct MemoryChunk)); } @@ -67,4 +76,24 @@ void Free(void* ptr) } -} \ No newline at end of file +} + +void ListBlocks() +{ + struct MemoryChunk *freeChunk = 0; + int count = 0; + + for(struct MemoryChunk* chunk = First; chunk != 0 && freeChunk == 0; chunk = chunk->Next) + { + puts("\nChunk "); + puts(int_to_string(count, 10)); + puts(" is "); + puts(int_to_string(chunk->Size, 10)); + puts(" bytes. Allocated? "); + if(chunk->Allocated) + puts("True"); + else + puts("False"); + count++; + } +} diff --git a/src/pci.c b/src/pci.c index 73406ba..151b723 100644 --- a/src/pci.c +++ b/src/pci.c @@ -5,9 +5,9 @@ uint32_t Read(uint16_t busNumber, uint16_t deviceNumber, uint16_t functionNumber, uint32_t registerOffset) { - puts("Read "); - puts(int_to_string(functionNumber, 10)); - puts("\n"); + //puts("Read "); + //puts(int_to_string(functionNumber, 10)); +// puts("\n"); uint32_t id = 0x1 << 31 | ((busNumber & 0xFF) << 16) @@ -39,6 +39,7 @@ bool DeviceHasFunctions(uint16_t bus, uint16_t device) void EnumPCISubsystem() { + int lastId = 0; for(int bus = 0; bus < 8; bus++) { for(int device = 0; device < 32; device++) @@ -53,7 +54,17 @@ void EnumPCISubsystem() dev = GetDeviceDescriptor(bus, device, function); if(dev.VendorId == 0x0000 || dev.VendorId == 0xFFFF) + { + puts("Vendor ID empty.\n"); break;//Break loop, no more funcitons. + } + else if(dev.VendorId != lastId) + { + puts("Vendor ID not empty: "); + puts(int_to_string(dev.VendorId, 10)); + puts("\n"); + lastId = dev.VendorId; + } //puts("PCI BUS "); //puts(int_to_string(bus, 16)); //puts("Device "); diff --git a/src/shell.c b/src/shell.c index 06e0b1a..f2143f4 100644 --- a/src/shell.c +++ b/src/shell.c @@ -9,10 +9,14 @@ 0.0.2.0: Fixed a bug allowing the user to delete all screen content by simply holding the backspace key. + + 0.0.2.1: Added in the ability to manually call Malloc() + for testing purposes. */ #include "../include/shell.h" #include "../include/scrn.h" #include "../include/system.h" +#include "../include/memorymanager.h" static char* Prompt = "\n#>"; @@ -44,17 +48,17 @@ void key_pressed(unsigned char* character) //Do not delete the shell prompt, simply return. return; } - //Otherwise clear the last character from the command - //buffer. + //Otherwise clear the last character from the command buffer. commandbuffer[len - 1] = '\0'; } putch(character); + //putch(221); } void execmd() { - proccess_command(commandbuffer); + //proccess_command(commandbuffer); if(strlen(commandbuffer) == 0) { puts(Prompt); @@ -62,17 +66,37 @@ void execmd() } if(strcmp(commandbuffer, "version")) { - puts("\nInteractive Shell Version 0.0.2.0"); + puts("\nInteractive Shell Version 0.0.2.1"); } else if(strcmp(commandbuffer,"cls")) { cls(); } + else if(strcmp(commandbuffer, "list")) + { + ListBlocks(); + } else { - puts("\nUnknown command: "); - puts(commandbuffer); - puts("."); + if(strcmpstart(commandbuffer, "malloc")) + { + int length = strlen(commandbuffer); + static char string_num[32]; + for(int i = 7; i <= length; i++) + { + string_num[i - 7] = commandbuffer[i]; + } + string_num[(length - 8) + 1] = '\0'; + + int allocAmount = string_to_int(string_num); + Malloc(allocAmount); + } + else + { + puts("\nUnknown command: "); + puts(commandbuffer); + puts("."); + } } puts(Prompt); commandbuffer[0] = '\0'; @@ -110,4 +134,4 @@ void proccess_command(char *text) puts('\n'); puts(array[currentArrayIndex]); } -} \ No newline at end of file +}