From 1f90a5d862d5a80fef4fe78953f6bd8d8fdf8913 Mon Sep 17 00:00:00 2001 From: Nigel Date: Sat, 11 Feb 2023 12:22:45 +0100 Subject: [PATCH] Starting to move towards proper HAL and ring3 - slight clean up of PCI driver - Added TaskSegment header - Rename some folders --- .gitmodules | 3 + Makefile | 4 +- mlibc | 1 + source/kernel/boot/boot.s | 2 +- source/kernel/drivers/pci/pci.cpp | 42 ++++++----- .../idt => drivers/ps-2}/scancodes/set1.h | 0 source/kernel/interrupts/{idt => }/idt.cpp | 10 +-- source/kernel/interrupts/{idt => }/idt.h | 8 +- source/kernel/interrupts/{idt => }/idt.s | 0 source/kernel/kernel.cpp | 24 +++--- source/kernel/memory/TaskStateSegment.h | 59 +++++++++++++++ source/kernel/memory/gdt/gdtc.cpp | 16 ---- source/kernel/memory/gdt/gdtc.h | 16 +++- .../mbr}/MasterBootRecord.h | 0 .../mbr}/PartitionTableEntry.h | 0 .../supervisorterminal/superVisorTerminal.cpp | 37 ++++------ source/lib/include/stack.h | 73 +++++++++++++++++++ 17 files changed, 209 insertions(+), 86 deletions(-) create mode 100644 .gitmodules create mode 160000 mlibc rename source/kernel/{interrupts/idt => drivers/ps-2}/scancodes/set1.h (100%) rename source/kernel/interrupts/{idt => }/idt.cpp (98%) rename source/kernel/interrupts/{idt => }/idt.h (93%) rename source/kernel/interrupts/{idt => }/idt.s (100%) create mode 100644 source/kernel/memory/TaskStateSegment.h rename source/kernel/{PartitionTable/MBR => partitiontable/mbr}/MasterBootRecord.h (100%) rename source/kernel/{PartitionTable/MBR => partitiontable/mbr}/PartitionTableEntry.h (100%) create mode 100644 source/lib/include/stack.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..82ee6dd --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "mlibc"] + path = mlibc + url = https://github.com/managarm/mlibc.git diff --git a/Makefile b/Makefile index e4bb2ff..c48fc85 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CC = ${HOME}/opt/cross/bin/i686-elf-gcc CPP = ${HOME}/opt/cross/bin/i686-elf-g++ CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra -OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/cpu.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o +OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/cpu.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o SRC_DIR = source BUILD_DIR = build @@ -68,7 +68,7 @@ $(BUILD_DIR)/io.o: $(CPP) -c $(SRC_DIR)/kernel/drivers/io/io.cpp -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/idt.o: - $(CPP) -c $(SRC_DIR)/kernel/interrupts/idt/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti + $(CPP) -c $(SRC_DIR)/kernel/interrupts/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti $(BUILD_DIR)/gdtc.o: $(CPP) -c $(SRC_DIR)/kernel/memory/gdt/gdtc.cpp -o $(BUILD_DIR)/gdtc.o $(CFLAGS) -fno-exceptions -fno-rtti diff --git a/mlibc b/mlibc new file mode 160000 index 0000000..aad4e7f --- /dev/null +++ b/mlibc @@ -0,0 +1 @@ +Subproject commit aad4e7f64b8de2c113cf7fc08943d0f005b517f9 diff --git a/source/kernel/boot/boot.s b/source/kernel/boot/boot.s index 6013482..c5e9902 100644 --- a/source/kernel/boot/boot.s +++ b/source/kernel/boot/boot.s @@ -108,6 +108,6 @@ isPaging: .include "./source/kernel/memory/gdt/gdt.s" .include "./source/kernel/irs_table.s" .include "./source/kernel/irq_table.s" -.include "./source/kernel/interrupts/idt/idt.s" +.include "./source/kernel/interrupts/idt.s" diff --git a/source/kernel/drivers/pci/pci.cpp b/source/kernel/drivers/pci/pci.cpp index e45314c..25c5f45 100644 --- a/source/kernel/drivers/pci/pci.cpp +++ b/source/kernel/drivers/pci/pci.cpp @@ -114,6 +114,14 @@ const char* getVendor( uint32_t VendorID){ return "Advanced Micor Devices, Inc.[AMD/ATI]"; break; + case 0xbeef: + return "VirtualBox Graphics Adapter"; + break; + + case 0xcafe: + return "VirtualBox Guest Service"; + break; + default: return "Vendor Unkown"; break; @@ -126,6 +134,16 @@ uint32_t ConfigReadWord ( PCIBusAddress& PCIDeviceAddress , uint8_t offset){ return inl(CONFIG_DATA); } +uint8_t GetProgIF (PCIBusAddress& PCIDeviceAddress){ + uint32_t data = ConfigReadWord(PCIDeviceAddress, 0x8); + return ((data >> 8) & 0xFF); +} + +uint32_t ReadBAR ( PCIBusAddress& PCIDeviceAddress, int bar_number){ + int offsetToBar = 0x10 + (bar_number* 0x4); + return ConfigReadWord(PCIDeviceAddress, offsetToBar); +} + uint32_t ConfigReadWord (uint8_t bus, uint8_t device, uint8_t func, uint8_t offset){ uint32_t address; @@ -185,8 +203,7 @@ void PrintPCIDeviceInfo (PCIBusAddress& PCIDeviceAddress) } void PCI_Enumerate(){ - - + int devicesFound = 0; printf("Start finding devices, Found: %d devices"); @@ -196,18 +213,14 @@ void PCI_Enumerate(){ for(int device = 0; device < 32 ; device ++) { - - int function = 0; - //uint64_t DeviceIdentify = ConfigReadWord(bus, device, function,0x0); + uint64_t DeviceIdentify = ConfigReadWord(bus, device, function,0x0); uint32_t DeviceID = GetDevice(bus, device, function) >> 16; - - if( DeviceID != 0xFFFF){ PCIBusAddress busAddress = - PCIBusAddress{bus, device, function }; + PCIBusAddress{bus, device, function }; PrintPCIDeviceInfo(busAddress); @@ -228,9 +241,7 @@ void PCI_Enumerate(){ } - - - + devicesFound++; } } @@ -240,12 +251,3 @@ void PCI_Enumerate(){ printf("Found %d PCI devices!\n", devicesFound); } -uint8_t GetProgIF (PCIBusAddress& PCIDeviceAddress){ - uint32_t data = ConfigReadWord(PCIDeviceAddress, 0x8); - return ((data >> 8) & 0xFF); -} - -uint32_t ReadBAR ( PCIBusAddress& PCIDeviceAddress, int bar_number){ - int offsetToBar = 0x10 + (bar_number* 0x4); - return ConfigReadWord(PCIDeviceAddress, offsetToBar); -} \ No newline at end of file diff --git a/source/kernel/interrupts/idt/scancodes/set1.h b/source/kernel/drivers/ps-2/scancodes/set1.h similarity index 100% rename from source/kernel/interrupts/idt/scancodes/set1.h rename to source/kernel/drivers/ps-2/scancodes/set1.h diff --git a/source/kernel/interrupts/idt/idt.cpp b/source/kernel/interrupts/idt.cpp similarity index 98% rename from source/kernel/interrupts/idt/idt.cpp rename to source/kernel/interrupts/idt.cpp index 3000fae..7d99ca3 100644 --- a/source/kernel/interrupts/idt/idt.cpp +++ b/source/kernel/interrupts/idt.cpp @@ -1,8 +1,8 @@ #include "idt.h" -#include "../../drivers/pit/pit.h" -#include "../../drivers/ps-2/keyboard.h" -#include "../../cpu.h" -#include "../../memory/VirtualMemoryManager.h" +#include "../drivers/pit/pit.h" +#include "../drivers/ps-2/keyboard.h" +#include "../cpu.h" +#include "../memory/VirtualMemoryManager.h" IDT_entry idt_table[256]; IDT_ptr idt_ptr; @@ -327,7 +327,7 @@ void irq_handler (registers regs) { } -void init_idt(){ +void initidt(){ // Initialise the IDT pointer idt_ptr.length = sizeof(IDT_entry) * 255; idt_ptr.base = (uint32_t)&idt_table; diff --git a/source/kernel/interrupts/idt/idt.h b/source/kernel/interrupts/idt.h similarity index 93% rename from source/kernel/interrupts/idt/idt.h rename to source/kernel/interrupts/idt.h index e2a0bd5..0a87d8d 100644 --- a/source/kernel/interrupts/idt/idt.h +++ b/source/kernel/interrupts/idt.h @@ -2,10 +2,10 @@ #include #include -#include "../../drivers/vga/colors.h" -#include "../../drivers/pic/pic.h" +#include "../drivers/vga/colors.h" +#include "../drivers/pic/pic.h" -#include "../../terminal/kterm.h" +#include "../terminal/kterm.h" extern "C" { @@ -32,7 +32,7 @@ extern "C" { extern void idt_flush(uint32_t); void set_id_entry (uint8_t num , uint32_t base, uint16_t sel, uint8_t flags); - void init_idt(); + void initidt(); void irq_handler (registers regs); diff --git a/source/kernel/interrupts/idt/idt.s b/source/kernel/interrupts/idt.s similarity index 100% rename from source/kernel/interrupts/idt/idt.s rename to source/kernel/interrupts/idt.s diff --git a/source/kernel/kernel.cpp b/source/kernel/kernel.cpp index b83f2a6..2972314 100644 --- a/source/kernel/kernel.cpp +++ b/source/kernel/kernel.cpp @@ -12,6 +12,7 @@ extern "C" #include "memory/VirtualMemoryManager.h" #include "memory/KernelHeap.h" #include "memory/gdt/gdtc.h" +#include "memory/TaskStateSegment.h" #include "supervisorterminal/superVisorTerminal.h" @@ -19,7 +20,8 @@ extern "C" #include "drivers/vga/VBE.h" #include "drivers/pci/pci.h" #include "drivers/pit/pit.h" - +#include "drivers/acpi/acpi.h" +#include "drivers/ide/ide.h" #include "terminal/kterm.h" @@ -28,16 +30,13 @@ extern "C" #include "bootcheck.h" -#include "interrupts/idt/idt.h" +#include "interrupts/idt.h" #include "time.h" #include "cpu.h" #include "serial.h" #include "time.h" #include "definitions.h" - - - /* Copyright © Nigel Barink 2023 */ @@ -55,16 +54,22 @@ extern "C" void kernel_main () extern "C" void early_main() { init_serial(); - kterm_init(); - printf("Allocated blocks: 0x%x \n", GetUsedBlocks()); + initGDT(); - init_idt(); + //setup_tss(); + initidt(); + // Enable interrupts asm volatile("STI"); - + + ACPI::initialize(); + PCI_Enumerate(); + + TestIDEController(); + initHeap(); printf("Enable Protected mode and jump to kernel main\n"); @@ -84,7 +89,6 @@ extern "C" void early_main() pit_initialise(); - kernel_main(); } diff --git a/source/kernel/memory/TaskStateSegment.h b/source/kernel/memory/TaskStateSegment.h new file mode 100644 index 0000000..c4860c1 --- /dev/null +++ b/source/kernel/memory/TaskStateSegment.h @@ -0,0 +1,59 @@ +#pragma once +#include "gdt/gdtc.h" +#include "../../lib/include/string.h" + +struct TaskStateSegment { + uint32_t prev_tss; + uint32_t esp0; + uint32_t ss0; + // everythinge else is unused + uint32_t esp1; + uint32_t ss1; + uint32_t esp2; + uint32_t ss2; + uint32_t cr3; + uint32_t eip; + uint32_t eflags; + uint32_t eax; + uint32_t ecx; + uint32_t edx; + uint32_t ebx; + uint32_t esp; + uint32_t ebp; + uint32_t esi; + uint32_t edi; + uint32_t es; + uint32_t cs; + uint32_t ss; + uint32_t ds; + uint32_t fs; + uint32_t gs; + uint32_t ldt; + uint16_t trap; + uint16_t iomap_base; +}__attribute__((packed)); + + +TaskStateSegment tss0 = {}; + +inline void flush_tss(){ + asm volatile("mov (5 * 8) |0 , %eax; ltr %ax"); +} + + +void setup_tss(){ + + + // ensure the tss is zero'd + //memset((void*)&tss0, 0, sizeof(tss0)); + tss0.ss0 = (uint32_t) &(GlobalDescriptorTable[KERNEL_DATA_SEGMENT]); + uint32_t esp_addr =0 ; + asm volatile ("movl %%esp, %0" : "=a"(esp_addr)); + tss0.esp0 = esp_addr; + + + // Task Segment Descriptor + add_descriptor(TASK_STATE_SEGMENT, (unsigned long)&tss0, sizeof(tss0), 0x89, 0x0); + flush_tss(); +} + diff --git a/source/kernel/memory/gdt/gdtc.cpp b/source/kernel/memory/gdt/gdtc.cpp index 55e0b92..c7f15c9 100644 --- a/source/kernel/memory/gdt/gdtc.cpp +++ b/source/kernel/memory/gdt/gdtc.cpp @@ -1,17 +1,9 @@ #include "gdtc.h" #include "../../terminal/kterm.h" -#define NULL_SEGMENT 0 -#define KERNEL_CODE_SEGMENT 1 -#define KERNEL_DATA_SEGMENT 2 -#define USER_CODE_SEGMENT 3 -#define USER_DATA_SEGMENT 4 -#define TASK_STATE_SEGMENT 5 SegmentDescriptor GlobalDescriptorTable[6]; GlobalDescriptorTableDescriptor gdtDescriptor; -tss32 TaskStateSegment = {}; - void add_descriptor(int which , unsigned long base, unsigned long limit, unsigned char access, unsigned char granularity ){ @@ -32,9 +24,6 @@ void add_descriptor(int which , unsigned long base, unsigned long limit, unsigne void initGDT(){ - - printf("Init GDT!\n"); - // NULL segment add_descriptor(NULL_SEGMENT, 0,0,0,0); @@ -50,14 +39,9 @@ void initGDT(){ // User Data Segement add_descriptor(USER_DATA_SEGMENT, 0, 0xFFFFFFFF, 0xF2, 0xCF); - // Task Segment Descriptor - add_descriptor(TASK_STATE_SEGMENT, (unsigned long)&TaskStateSegment, sizeof(TaskStateSegment), 0x89, 0x0); - // init Gdt Descriptor gdtDescriptor.limit = ((sizeof(SegmentDescriptor ) * 5 ) - 1); gdtDescriptor.base = (unsigned int) (&GlobalDescriptorTable); - LoadGlobalDescriptorTable(); - } diff --git a/source/kernel/memory/gdt/gdtc.h b/source/kernel/memory/gdt/gdtc.h index adca78e..17c8ec0 100644 --- a/source/kernel/memory/gdt/gdtc.h +++ b/source/kernel/memory/gdt/gdtc.h @@ -1,5 +1,16 @@ #pragma once #include + + +#define NULL_SEGMENT 0 +#define KERNEL_CODE_SEGMENT 1 +#define KERNEL_DATA_SEGMENT 2 +#define USER_CODE_SEGMENT 3 +#define USER_DATA_SEGMENT 4 +#define TASK_STATE_SEGMENT 5 + + + struct SegmentDescriptor { unsigned short limit_low; unsigned short base_low; @@ -9,10 +20,7 @@ struct SegmentDescriptor { unsigned char base_high; }__attribute__((packed)); -struct tss32 { - uint64_t bits; - uint8_t other_bits :5; -}__attribute__((packed)); +extern SegmentDescriptor GlobalDescriptorTable[6]; struct GlobalDescriptorTableDescriptor{ unsigned short limit; diff --git a/source/kernel/PartitionTable/MBR/MasterBootRecord.h b/source/kernel/partitiontable/mbr/MasterBootRecord.h similarity index 100% rename from source/kernel/PartitionTable/MBR/MasterBootRecord.h rename to source/kernel/partitiontable/mbr/MasterBootRecord.h diff --git a/source/kernel/PartitionTable/MBR/PartitionTableEntry.h b/source/kernel/partitiontable/mbr/PartitionTableEntry.h similarity index 100% rename from source/kernel/PartitionTable/MBR/PartitionTableEntry.h rename to source/kernel/partitiontable/mbr/PartitionTableEntry.h diff --git a/source/kernel/supervisorterminal/superVisorTerminal.cpp b/source/kernel/supervisorterminal/superVisorTerminal.cpp index 73ee676..a178fd8 100644 --- a/source/kernel/supervisorterminal/superVisorTerminal.cpp +++ b/source/kernel/supervisorterminal/superVisorTerminal.cpp @@ -1,8 +1,6 @@ #include "superVisorTerminal.h" #include "../drivers/ata/ataDevice.h" -#include "../drivers/acpi/acpi.h" -#include "../drivers/ide/ide.h" -#include "../PartitionTable/MBR/MasterBootRecord.h" +#include "../partitiontable/mbr/MasterBootRecord.h" #include "../filesystem/FAT/BiosParameterBlock.h" #include "../filesystem/FAT/DirectoryEntry.h" bool isRunning = true; @@ -43,18 +41,10 @@ void startSuperVisorTerminal(){ if( strncmp ("MEMORY" , command , characterCount) == 0 ) { // Show memory layout - printf("========= Memory ==========\n"); + printf("========= Memory (very inaccurate) ==========\n"); printf("Kernel MemoryMap:\n"); printf("kernel: 0x%x - 0x%x\n", &kernel_begin , &kernel_end); - printf("Frames used: 0x%x blocks of 4 KiB\n", 0); - const int bytesInGiB = 1073741824; - //int64_t bytesLeft = (bootinfo->memory->TotalMemory % bytesInGiB) / bytesInGiB; - //int64_t effectiveNumberOfGib = bootinfo->memory->TotalMemory / bytesInGiB; - - //int64_t GiBs = effectiveNumberOfGib + bytesLeft; - - //printf("Available Memory: %d bytes, %d GiB\n", bootinfo->memory->TotalMemory, GiBs ); - //printf("Reserved Memory: %d bytes\n", bootinfo->memory->ReservedMemory); + printf("Frames used: %d blocks of 4 KiB\n", GetUsedBlocks()); } if(strncmp("TEST", command, characterCount) == 0) @@ -76,14 +66,6 @@ void startSuperVisorTerminal(){ } if(strncmp("FAT", command, characterCount) == 0) { - printf("ACPI initialize!\n"); - ///ACPI::initialize(); - - // Enumerate the PCI bus - printf("PCI Enumeration\n"); - PCI_Enumerate(); - printf("TEST IDE Controller"); - TestIDEController(); int devNumber = 0 ; for ( auto device : ide_devices){ @@ -129,7 +111,7 @@ void startSuperVisorTerminal(){ i, PT.Number_sectors_inPartition, PT.PartitionType, mbr->uniqueID, PT.LBA_partition_start ); } - // Find the BiosParameter block + // Find the BiosParameter block uint16_t biosparameterblock[256]; ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mbr->TableEntries[0].LBA_partition_start, biosparameterblock); @@ -224,8 +206,15 @@ void startSuperVisorTerminal(){ - if(strncmp("glg", command, characterCount) == 0){ - printf("Why???"); + if(strncmp("DEVICES", command, characterCount) == 0){ + printf("================ CONNECTED DEVICES ===============\n"); + + + + + + + } diff --git a/source/lib/include/stack.h b/source/lib/include/stack.h new file mode 100644 index 0000000..0128bf6 --- /dev/null +++ b/source/lib/include/stack.h @@ -0,0 +1,73 @@ +#pragma once +#include "../../kernel/memory/KernelHeap.h" +#include + +template +class Stack { + public: + inline Stack() { + elements = (T[MAX_STACK_SIZE]) malloc(MAX_STACK_SIZE * sizeof(T)); + num = 0; + + } + + inline void Push(T element){ + num++; + if(num > MAX_STACK_SIZE) + grow(); + + element[num] = element; + } + + + inline T Pop() + { + T temp = elements[num]; + num --; + + return temp; + } + + inline bool isEmpty() + { + return num == 0; + } + + inline bool isFull() + { + return num == MAX_STACK_SIZE; + } + + + inline int count() + { + return num; + } + + inline ~Stack() + { + free(elements); + } + + + private: + unsigned int MAX_STACK_SIZE; + T[MAX_STACK_SIZE] elements; + unsigned int num; + + inline void grow (){ + MAX_STACK_SIZE = MAX_STACK_SIZE + (int)(MAX_STACK_SIZE / 4); + T[] new_elements =(T[MAX_STACK_SIZE]) malloc(MAX_STACK_SIZE * sizeof(T)); + + for ( int i = 0; i < num ; i++){ + new_elements[i] = elements[i]; + } + free(elements); + elements = new_elements; + + } + + + +}; +