Compare commits

...

2 Commits

Author SHA1 Message Date
ecab248cd6 Clean up jump into RING 3 2023-02-17 14:46:44 +01:00
4ce7cc093b Cleanup of PCI API 2023-02-17 14:42:42 +01:00
8 changed files with 236 additions and 301 deletions

View File

@ -99,11 +99,9 @@ isPaging:
# Unmap the identity mapping as it is now unnecessary
# movl $0, boot_page_directory + 0
call early_main
call kernel
cli
1: hlt
@ -115,4 +113,18 @@ isPaging:
.include "./source/kernel/irq_table.s"
.include "./source/kernel/interrupts/idt.s"
.globl jump_usermode
jump_usermode:
mov $((4*8) | 3) , %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %esp, %eax
push $( (3*8) | 3)
push %eax
pushf
push $( ( 3 * 8) | 3)
push startSuperVisorTerminal
iret

View File

@ -4,6 +4,6 @@
*/
#define __DEBUG__ false
#define KERNEL_VERSION 0
#define KERNEL_VERSION 0.03
#define ARCHITECTURE "I386"

View File

@ -56,7 +56,7 @@ inline void TestIDEController(){
int device =1 , function = 1;
PCIBusAddress IDEControllerPCIAddress = PCIBusAddress{bus,device, function};
uint8_t ProgIF = GetProgIF(IDEControllerPCIAddress);
uint8_t ProgIF = PCI::GetProgIF(IDEControllerPCIAddress);
printf( "ProgIF: 0x%x\n" ,ProgIF);
//CheckProgIF(ProgIF);
@ -66,15 +66,15 @@ inline void TestIDEController(){
uint32_t BAR0,BAR1,BAR2,BAR3, BAR4;
BAR0 = ReadBAR(IDEControllerPCIAddress, 0);
BAR0 = PCI::ReadBAR(IDEControllerPCIAddress, 0);
BAR1 = ReadBAR(IDEControllerPCIAddress, 1);
BAR1 = PCI::ReadBAR(IDEControllerPCIAddress, 1);
BAR2 = ReadBAR(IDEControllerPCIAddress, 2);
BAR2 = PCI::ReadBAR(IDEControllerPCIAddress, 2);
BAR3 = ReadBAR(IDEControllerPCIAddress, 3);
BAR3 = PCI::ReadBAR(IDEControllerPCIAddress, 3);
BAR4 = ReadBAR(IDEControllerPCIAddress, 4);
BAR4 = PCI::ReadBAR(IDEControllerPCIAddress, 4);
// All bars are return 0xffffff for some as of yet mysterious reason!
printf( "BAR 0: 0x%x\n", BAR0);

View File

@ -1,107 +1,66 @@
#include "pci.h"
#define PCI_BUS_ADDR_SHIFT 16
#define PCI_DEVICE_ADDR_SHIFT 11
#define PCI_FUNCTION_ADDR_SHIFT 8
#define PCI_ENABLE_ADDR_SHIFT 31
void PCI::Scan(){
int devicesFound = 0;
const char* GetClassCodeName (uint64_t ClassCode ) {
switch (ClassCode)
{
case 0x0 :
return "Unclassified";
break;
printf("Start finding devices, Found: %d devices");
// loop through all possible busses, devices and their functions;
for( int bus = 0 ; bus < 256 ; bus++)
{
for(int device = 0; device < 32 ; device ++)
{
int function = 0;
case 0x1:
return "Mass Storage Controller";
break;
uint64_t DeviceIdentify = PCI::ConfigReadWord(bus, device, function,0x0);
uint32_t DeviceID = GetDevice(bus, device, function) >> 16;
case 0x2:
return "Network Controller";
break;
if( DeviceID != 0xFFFF){
PCIBusAddress busAddress =
PCIBusAddress{bus, device, function };
case 0x3:
return "Display Controller";
break;
PrintPCIDevice(busAddress);
case 0x4:
return "Multimedia Controller";
break;
// iterate over the functions if it is a multi function device!
if( PCI::IsMultiFunctionDevice(busAddress) ){
printf("Multi function device! \n");
printf("Check remaining Functions\n");
for ( function = 1 ; function < 8; function++)
{
uint32_t DeviceID = GetDevice(bus, device, function) >> 16;
case 0x5:
return "Memory Controller";
break;
case 0x6:
return "Bridge";
break;
if( DeviceID != 0xFFFF){
PCIBusAddress busAddress2 = PCIBusAddress{bus, device, function};
PrintPCIDevice(busAddress2);
devicesFound++;
}
}
}
case 0x7 :
return "Simple Communication Controller";
break;
case 0x8:
return "Base System Peripheral";
break;
case 0x9:
return "Input Device Controller";
break;
case 0xA:
return "Docking station";
break;
case 0xB:
return "Processor";
break;
case 0xC:
return "Serial Bus Controller";
break;
case 0xD:
return "Wireless Controller";
break;
case 0xE:
return "Intelligent Controller";
break;
case 0xF:
return "Satellite Communication Controller";
break;
case 0x10:
return "Encryption Controller";
break;
case 0x11:
return "Signal Processing Controller";
break;
case 0x12:
return "Processing Accelerator";
break;
case 0x13:
return "Non-Essential Instrumentation";
break;
default:
return "Unknown";
break;
devicesFound++;
}
}
}
printf("Found %d PCI devices!\n", devicesFound);
}
const char* getVendor( uint32_t VendorID){
const char* PCI::getClassName (uint8_t ClassCode){
bool isKnown = (ClassCode < PCI::KnownClassCodes);
return isKnown ? PCI::ClassCodeNames[ClassCode].name : "Unknown ClassCode";
}
const char* PCI::getVendor( uint32_t VendorID){
switch (VendorID)
{
case 0x8086:
return "Intel Corporation";
break;
case 0x10DE:
return "NVIDIA Corporation";
break;
@ -129,125 +88,77 @@ const char* getVendor( uint32_t VendorID){
}
uint32_t ConfigReadWord ( PCIBusAddress& PCIDeviceAddress , uint8_t offset){
outl(CONFIG_ADDRESS , PCIDeviceAddress.getAddress() | offset );
uint64_t PCI::GetDevice (int bus, int device, int function ){
return PCI::ConfigReadWord(bus, device, function,0x0);
}
bool PCI::IsMultiFunctionDevice(PCIBusAddress& PCIDeviceAddress)
{
uint32_t header_information = ConfigReadWord(PCIDeviceAddress, 0xC);
return (((header_information>>16)
& 0x80)
>> 7 );
}
uint16_t PCI::GetClassCodes( PCIBusAddress& PCIDeviceAddress ){
return (uint16_t)(ConfigReadWord(PCIDeviceAddress, 0x8) >> 16);
}
uint8_t PCI::GetHeaderType( PCIBusAddress& PCIDeviceAddress ){
uint32_t header_information = ConfigReadWord(PCIDeviceAddress , 0xC);
return (uint8_t) (
((header_information >> 16) //Get higher half
& 0x00FF) // Select the last two bytes
& 0x7F ); // Mask bit 7 as it indicates if the device is a mulit function device!
}
uint32_t PCI::ConfigReadWord (uint8_t bus, uint8_t device, uint8_t func, uint8_t offset){
uint32_t address;
address = (uint32_t) (
((uint32_t) 1 << PCI_ENABLE_ADDR_SHIFT) |
((uint32_t)bus << PCI_BUS_ADDR_SHIFT) |
((uint32_t)device << PCI_DEVICE_ADDR_SHIFT) |
((uint32_t)func << PCI_FUNCTION_ADDR_SHIFT) |
offset );
outl(CONFIG_ADDRESS, address);
return inl(CONFIG_DATA);
}
uint8_t GetProgIF (PCIBusAddress& PCIDeviceAddress){
uint8_t PCI::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;
address = (uint32_t) (
((uint32_t) 1 << PCI_ENABLE_ADDR_SHIFT) |
((uint32_t)bus << PCI_BUS_ADDR_SHIFT) |
((uint32_t)device << PCI_DEVICE_ADDR_SHIFT) |
((uint32_t)func << PCI_FUNCTION_ADDR_SHIFT) |
offset );
outl(CONFIG_ADDRESS, address);
uint32_t PCI::ConfigReadWord ( PCIBusAddress& PCIDeviceAddress , uint8_t offset){
outl(CONFIG_ADDRESS , PCIDeviceAddress.getAddress() | offset );
return inl(CONFIG_DATA);
}
uint8_t GetHeaderType( PCIBusAddress& PCIDeviceAddress ){
uint32_t header_information = ConfigReadWord(PCIDeviceAddress , 0xC);
return (uint8_t) (
((header_information >> 16) //Get higher half
& 0x00FF) // Select the last two bytes
& 0x7F ); // Mask bit 7 as it indicates if the device is a mulit function device!
uint32_t PCI::ReadBAR ( PCIBusAddress& PCIDeviceAddress, int bar_number){
int offsetToBar = 0x10 + (bar_number* 0x4);
return ConfigReadWord(PCIDeviceAddress, offsetToBar);
}
uint16_t GetClassCodes( PCIBusAddress& PCIDeviceAddress ){
uint32_t classcodes = ConfigReadWord(PCIDeviceAddress, 0x8);
return (uint16_t)((uint32_t)classcodes >> 16);
}
bool IsMultiFunctionDevice(PCIBusAddress& PCIDeviceAddress){
uint32_t header_information = ConfigReadWord(PCIDeviceAddress, 0xC);
return (((header_information>>16)
& 0x80)
>> 7 );
}
void PrintPCIDeviceInfo (PCIBusAddress& PCIDeviceAddress)
void PCI::PrintPCIDevice (PCIBusAddress& PCIDeviceAddress)
{
uint32_t DeviceID = (GetDevice(PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function) >> 16);
uint32_t VendorID = GetDevice(PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function) & 0xFFFF;
uint32_t DeviceID = (PCI::GetDevice(PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function) >> 16);
uint32_t VendorID = PCI::GetDevice(PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function) & 0xFFFF;
printf("Device found!\n");
printf("Bus: %d, Device: %d, function: %d \n", PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function);
printf("DeviceID: 0x%x, Vendor: %s\n",
DeviceID
, getVendor(VendorID) );
printf("DeviceID: 0x%x, Vendor: %s\n",
DeviceID
, PCI::getVendor(VendorID) );
uint8_t header_type = GetHeaderType(PCIDeviceAddress);
uint8_t header_type = PCI::GetHeaderType(PCIDeviceAddress);
printf( "Header type: 0x%x\n", header_type);
uint16_t deviceClasses = GetClassCodes(PCIDeviceAddress);
printf("class: %s, subClass: %d\n\n", GetClassCodeName((deviceClasses >>8)), deviceClasses & 0xFF);
uint16_t deviceClasses = PCI::GetClassCodes(PCIDeviceAddress);
}
void PCI_Enumerate(){
int devicesFound = 0;
printf("Start finding devices, Found: %d devices");
// loop through all possible busses, devices and their functions;
for( int bus = 0 ; bus < 256 ; bus++)
{
for(int device = 0; device < 32 ; device ++)
{
int function = 0;
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 };
PrintPCIDeviceInfo(busAddress);
// iterate over the functions if it is a multi function device!
if( IsMultiFunctionDevice(busAddress) ){
printf("Multi function device! \n");
printf("Check remaining Functions\n");
for ( function = 1 ; function < 8; function++)
{
uint32_t DeviceID = GetDevice(bus, device, function) >> 16;
if( DeviceID != 0xFFFF){
PCIBusAddress busAddress2 = PCIBusAddress{bus, device, function};
PrintPCIDeviceInfo(busAddress2);
devicesFound++;
}
}
}
devicesFound++;
}
}
}
printf("Found %d PCI devices!\n", devicesFound);
}
printf("class: %s, subClass: %d\n\n", PCI::getClassName((deviceClasses >> 8)), deviceClasses & 0xFF);
}

View File

@ -7,30 +7,55 @@
// Configuration Space Access Mechanism #1
#define CONFIG_ADDRESS 0xCF8 // Configuration adress that is to be accessed
#define CONFIG_DATA 0xCFC // Will do the actual configuration operation
#define PCI_BUS_ADDR_SHIFT 16
#define PCI_DEVICE_ADDR_SHIFT 11
#define PCI_FUNCTION_ADDR_SHIFT 8
#define PCI_ENABLE_ADDR_SHIFT 31
extern const char* ClassCodeTable [0x13];
class PCI {
public:
static void Scan();
static uint32_t ConfigReadWord ( PCIBusAddress& PCIDeviceAddress , uint8_t offset);
static uint8_t GetProgIF (PCIBusAddress& PCIDeviceAddress);
static uint32_t ReadBAR ( PCIBusAddress& PCIDeviceAddress, int bar_number);
static uint32_t ConfigReadWord (uint8_t bus, uint8_t device, uint8_t func, uint8_t offset);
static uint8_t GetHeaderType( PCIBusAddress& PCIDeviceAddress );
static uint16_t GetClassCodes( PCIBusAddress& PCIDeviceAddress );
static bool IsMultiFunctionDevice(PCIBusAddress& PCIDeviceAddress);
static uint64_t GetDevice (int bus, int device, int function );
// Note: this could be used to make the api for receiving PCI class codes a bit
// nicer.
struct ClassCodes {
uint8_t ClassCode;
uint8_t DeviceClass;
}__attribute__((packed));
uint32_t ConfigReadWord (uint8_t bus, uint8_t device, uint8_t func, uint8_t offset);
uint32_t ConfigReadWord ( PCIBusAddress& PCIDeviceAddress , uint8_t offset);
static const char* getClassName (uint8_t ClassCode);
static const char* getVendor( uint32_t VendorID);
static void PrintPCIDevice(PCIBusAddress& PCIDevice);
inline uint64_t GetDevice (int bus, int device, int function ){
return ConfigReadWord(bus, device, function,0x0);
}
private:
struct ClassCode {
const char* name;
uint8_t code;
};
static constexpr ClassCode ClassCodeNames []= {
{"Unclassified", 0x0},
{"MassStorage Controller", 0x1},
{"Network Controller", 0x2},
{"Display Controller", 0x3},
{"Multimedia Controller", 0x4},
{"Memory Controller", 0x5},
{"Bridge", 0x6},
{"Simple Communication Controller", 0x7},
{"Base System Peripheral", 0x8},
{"Input Device Controller", 0x9},
{"Docking Station", 0xA},
{"Processor", 0xB},
{"Serial Bus Controller", 0xC},
{ "Wireless Controller", 0xD},
{"Intelligent Controller", 0xE},
{"Satellite Communication Controller", 0xF},
{"Encryption Controller", 0x10},
{"Signal Processing Controller", 0x11},
{ "Processing Accelerator", 0x12},
{ "Non-Essential Instrumentation", 0x13}
};
static const uint8_t KnownClassCodes = sizeof(ClassCodeNames) / sizeof(ClassCode);
};
uint8_t GetHeaderType( PCIBusAddress& PCIDeviceAddress );
uint16_t GetClassCodes( PCIBusAddress& PICDeviceAddress );
const char* getVendor( uint64_t VendorID);
const char* GetClassCodeName (uint64_t ClassCode );
uint8_t GetProgIF (PCIBusAddress& PCIDeviceAddress);
void PCI_Enumerate();
uint32_t ReadBAR ( PCIBusAddress& PCIDeviceAddress, int bar_number);

View File

@ -1,22 +1,16 @@
extern "C"
{
#include "../lib/include/string.h"
/*
Copyright © Nigel Barink 2023
*/
extern "C"{
#include "../lib/include/string.h"
}
#include "prekernel/bootstructure.h"
#include "memory/memory.h"
#include "memory/memoryinfo.h"
#include "memory/memory.h"
#include "memory/VirtualMemoryManager.h"
#include "memory/KernelHeap.h"
#include "memory/gdt/gdtc.h"
#include "memory/TaskStateSegment.h"
#include "supervisorterminal/superVisorTerminal.h"
#include "drivers/io/io.h"
#include "drivers/vga/VBE.h"
#include "drivers/pci/pci.h"
#include "drivers/pit/pit.h"
@ -24,74 +18,59 @@ extern "C"
#include "drivers/ide/ide.h"
#include "terminal/kterm.h"
#include "prekernel/multiboot.h"
#include "bootinfo.h"
#include "bootcheck.h"
#include "interrupts/idt.h"
#include "time.h"
#include "cpu.h"
#include "serial.h"
#include "time.h"
#include "definitions.h"
extern "C" void LoadGlobalDescriptorTable();
extern "C" void jump_usermode();
/*
Copyright © Nigel Barink 2023
*/
extern "C" void kernel_main ()
void set_protected_bit()
{
/*
* Show a little banner for cuteness
*/
printf("|=== BarinkOS ===|\n");
startSuperVisorTerminal();
}
// Set the protected bit of control register 0
// this will put the CPU into protected mode
// NOTE: This should really be a assembly procedure
// We cant directly write to control register 0
// therefor we copy the value of control register 0 into eax
// once we are done manipulating the value we write the value in
// eax back to control register 0
extern "C" void early_main()
asm volatile("mov %cr0, %eax ");
asm volatile("or $1, %eax");
asm volatile("mov %eax, %cr0");
}
extern "C" void kernel ()
{
init_serial();
kterm_init();
setup_tss();
initGDT();
initidt();
LoadGlobalDescriptorTable();
flush_tss();
printf("Memory setup complete!\n");
// Enable interrupts
asm volatile("STI");
ACPI::initialize();
PCI_Enumerate();
TestIDEController();
initHeap();
printf("Enable Protected mode and jump to kernel main\n");
// Set the protected bit of control register 0
// this will put the CPU into protected mode
// NOTE: This should really be a assembly procedure
// We cant directly write to control register 0
// therefor we copy the value of control register 0 into eax
// once we are done manipulating the value we write the value in
// eax back to control register 0
asm volatile("mov %cr0, %eax ");
asm volatile("or $1, %eax");
asm volatile("mov %eax, %cr0");
initHeap();
pit_initialise();
kernel_main();
}
ACPI::initialize();
PCI::Scan();
TestIDEController();
printf("Enable Protected mode and jump to kernel main\n");
set_protected_bit();
#ifdef USERMODE_RELEASE
// Lets jump into user mode
jump_usermode();
#else
startSuperVisorTerminal();
#endif
}

View File

@ -4,7 +4,13 @@
#include "../filesystem/FAT/BiosParameterBlock.h"
#include "../filesystem/FAT/DirectoryEntry.h"
bool isRunning = true;
void startSuperVisorTerminal(){
extern "C" void startSuperVisorTerminal()
{
/*
* Show a little banner for cuteness
*/
printf("|=== BarinkOS ===|\n");
while (isRunning){
printf("SUPERVISOR:>$ " );
@ -115,7 +121,7 @@ void startSuperVisorTerminal(){
uint16_t biosparameterblock[256];
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mbr->TableEntries[0].LBA_partition_start, biosparameterblock);
BiosParameterBlock* bpb = (BiosParameterBlock*) biosparameterblock;
auto* bpb = (BiosParameterBlock*) biosparameterblock;
printf("\nBPB: Bytes per Sector %d\n", bpb->BytesPerSector );
@ -138,8 +144,8 @@ void startSuperVisorTerminal(){
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FATAddress, FAT );
// Show data in terminal
for(int i = 0; i < 256; i++ ) {
printf("%x ", FAT[i]);
for(unsigned short i : FAT) {
printf("%x ", i);
}
kterm_put('\n');
@ -149,33 +155,35 @@ void startSuperVisorTerminal(){
uint16_t data2 [256];
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, RootDirectoryRegion, data2 );
DirectoryEntry* RootDirectory = (DirectoryEntry*) data2;
auto* RootDirectory = (DirectoryEntry*) data2;
// List files in root
for(int i= 0; i < bpb->NumberOfDirectoryEntries ; i++ )
{
DirectoryEntry* entry = (DirectoryEntry*)((uint32_t) RootDirectory + (i * sizeof(DirectoryEntry)));
auto* entry = (DirectoryEntry*)((uint32_t) RootDirectory + (i * sizeof(DirectoryEntry)));
if( entry->filename[0] == (uint8_t) 0x00 )
break; // There are no more entries in this directory or the entry is free
if( entry->attribute & 0x01 == 0x01 || entry->attribute & 0x20 == 0x20)
if( (entry->attribute & 0x01) == 0x01 || (entry->attribute & 0x20) == 0x20)
continue; // Skip listing if hidden or Achieve flag is set
// Print the filename;
for( int n = 0; n < 8; n++ ){
if(entry->filename[n] == 0x20)
for(char n : entry->filename){
if(n == 0x20)
break;
kterm_put(entry->filename[n]);
}kterm_put('\n');
kterm_put(n);
}
kterm_put('\n');
for( int n = 0; n < 3; n++){
kterm_put(entry->Extension[n]);
}kterm_put('\n');
for(unsigned char n : entry->Extension){
kterm_put(n);
}
kterm_put('\n');
printf("Attribute: %x \n" , entry->attribute);
printf("FileSize: %d Bytes\n", entry->FilesizeInBytes);
if( entry->FilesizeInBytes != 0x0 || entry->attribute & 0x8 == 0x0){
if( entry->FilesizeInBytes != 0x0 || (entry->attribute & 0x8) == 0x0){
printf("Show contents");
printf( "Start cluster of the file: 0x%x\n" , entry->StartingCluster);
@ -187,11 +195,11 @@ void startSuperVisorTerminal(){
uint16_t dataBlob [256];
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, sector, dataBlob );
for( int n = 0; n < 256; n++)
for(unsigned short n : dataBlob)
{
kterm_put(dataBlob[n] & 0x00ff);
kterm_put(n & 0x00ff);
kterm_put(dataBlob[n] >> 8);
kterm_put(n >> 8);
}kterm_put('\n');

View File

@ -8,4 +8,4 @@ extern "C" {
#include "../../lib/include/string.h"
}
void startSuperVisorTerminal();
extern "C" void startSuperVisorTerminal();