Starting to move towards proper HAL and ring3

- slight clean up of PCI driver
- Added TaskSegment header
- Rename some folders
dev
Nigel Barink 2023-02-11 12:22:45 +01:00
parent 520104a43a
commit 1f90a5d862
17 changed files with 209 additions and 86 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "mlibc"]
path = mlibc
url = https://github.com/managarm/mlibc.git

View File

@ -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

1
mlibc Submodule

@ -0,0 +1 @@
Subproject commit aad4e7f64b8de2c113cf7fc08943d0f005b517f9

View File

@ -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"

View File

@ -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);
}

View File

@ -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;

View File

@ -2,10 +2,10 @@
#include <stdint.h>
#include <stddef.h>
#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);

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -1,5 +1,16 @@
#pragma once
#include <stdint.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
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;

View File

@ -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");
}

View File

@ -0,0 +1,73 @@
#pragma once
#include "../../kernel/memory/KernelHeap.h"
#include <stdint.h>
template <typename T>
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;
}
};