Working kernel written on C and userspace-ready #1
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Types.h>
|
#include <Types.h>
|
||||||
|
#include <VM/PMM.h>
|
||||||
|
|
||||||
typedef struct FDTHeader {
|
typedef struct FDTHeader {
|
||||||
UInt32 magic; // 0xd00dfeed
|
UInt32 magic; // 0xd00dfeed
|
||||||
@@ -32,4 +33,4 @@ enum {
|
|||||||
kFDTHeaderMagic = 0xd00dfeed,
|
kFDTHeaderMagic = 0xd00dfeed,
|
||||||
};
|
};
|
||||||
|
|
||||||
void DTBParse(Pointer dtb);
|
void DTBParse(Pointer dtb, VMBootMemoryMap* bootMap);
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Types.h>
|
||||||
|
|
||||||
|
void* memset(void* destination, int value, Size count);
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Types.h>
|
||||||
|
|
||||||
|
enum {
|
||||||
|
kVMPageSize = 4096,
|
||||||
|
kVMBlocksPerByte = 8,
|
||||||
|
kVMMaxReservedRegions = 128,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
UInt64 base;
|
||||||
|
Size size;
|
||||||
|
} VMMemoryRegion;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
VMMemoryRegion totalRAM;
|
||||||
|
VMMemoryRegion reserved[kVMMaxReservedRegions];
|
||||||
|
UInt32 reservedCount;
|
||||||
|
} VMBootMemoryMap;
|
||||||
+42
-11
@@ -4,8 +4,9 @@
|
|||||||
#include <Lib/Bytes.h>
|
#include <Lib/Bytes.h>
|
||||||
#include <Lib/Align.h>
|
#include <Lib/Align.h>
|
||||||
#include <Lib/String.h>
|
#include <Lib/String.h>
|
||||||
|
#include <VM/PMM.h>
|
||||||
|
|
||||||
void DTBParse(Pointer dtb) {
|
void DTBParse(Pointer dtb, VMBootMemoryMap* bootMap) {
|
||||||
FDTHeader* header = (FDTHeader*)dtb;
|
FDTHeader* header = (FDTHeader*)dtb;
|
||||||
if (BytesSwap32(header->magic) != kFDTHeaderMagic) {
|
if (BytesSwap32(header->magic) != kFDTHeaderMagic) {
|
||||||
OSPanic("Invalid DTB magic");
|
OSPanic("Invalid DTB magic");
|
||||||
@@ -16,7 +17,11 @@ void DTBParse(Pointer dtb) {
|
|||||||
|
|
||||||
BytePointer structs = (BytePointer)dtb + offStruct;
|
BytePointer structs = (BytePointer)dtb + offStruct;
|
||||||
ASCII* strings = (ASCII*)dtb + offStrings;
|
ASCII* strings = (ASCII*)dtb + offStrings;
|
||||||
|
|
||||||
ASCII* currentNode = "";
|
ASCII* currentNode = "";
|
||||||
|
UInt32 currentDepth = 0;
|
||||||
|
UInt32 reservedMemoryDepth = 0;
|
||||||
|
bool inReservedMemory = false;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
UInt32 token = BytesSwap32(*(UInt32*)structs);
|
UInt32 token = BytesSwap32(*(UInt32*)structs);
|
||||||
@@ -24,35 +29,61 @@ void DTBParse(Pointer dtb) {
|
|||||||
|
|
||||||
switch (token) {
|
switch (token) {
|
||||||
case FDTTokenBeginNode: {
|
case FDTTokenBeginNode: {
|
||||||
|
currentDepth++;
|
||||||
currentNode = (ASCII*)structs;
|
currentNode = (ASCII*)structs;
|
||||||
|
|
||||||
|
if (StringStartsWith(currentNode, "reserved-memory")) {
|
||||||
|
inReservedMemory = true;
|
||||||
|
reservedMemoryDepth = currentDepth;
|
||||||
|
}
|
||||||
|
|
||||||
UInt32 nameLength = StringGetLength(currentNode);
|
UInt32 nameLength = StringGetLength(currentNode);
|
||||||
structs += (nameLength + 1);
|
structs += (nameLength + 1);
|
||||||
structs = (BytePointer)AlignUp64((Address)structs, 4);
|
structs = (BytePointer)AlignUp64((Address)structs, 4);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case FDTTokenProperty: {
|
case FDTTokenProperty: {
|
||||||
UInt32 propertyLength = BytesSwap32(*(UInt32*)structs);
|
UInt32 propertyLength = BytesSwap32(*(UInt32*)structs);
|
||||||
UInt32 nameOffset = BytesSwap32(*(UInt32*)(structs + 4));
|
UInt32 nameOffset = BytesSwap32(*(UInt32*)(structs + 4));
|
||||||
structs += 8;
|
structs += 8;
|
||||||
|
|
||||||
ASCII* propertyName = strings + nameOffset;
|
ASCII* propertyName = strings + nameOffset;
|
||||||
if (StringCompare(propertyName, "reg") == 0 && StringStartsWith(currentNode, "memory")) {
|
|
||||||
|
if (StringCompare(propertyName, "reg") == 0) {
|
||||||
|
if (StringStartsWith(currentNode, "memory")) {
|
||||||
UInt32* cells = (UInt32*)structs;
|
UInt32* cells = (UInt32*)structs;
|
||||||
|
UInt64 base = Merge32To64(BytesSwap32(cells[1]), BytesSwap32(cells[0]));
|
||||||
UInt64 base = Merge32To64(BytesSwap32(cells[0]), BytesSwap32(cells[1]));
|
UInt64 size = Merge32To64(BytesSwap32(cells[3]), BytesSwap32(cells[2]));
|
||||||
UInt64 size = Merge32To64(BytesSwap32(cells[2]), BytesSwap32(cells[3]));
|
OSLog("Main Memory: base=0x%x, size=0x%x\n", base, size);
|
||||||
|
bootMap->totalRAM.base = base;
|
||||||
OSLog("Memory: base=0x%x, size=0x%x\n", base, size);
|
bootMap->totalRAM.size = size;
|
||||||
}
|
}
|
||||||
|
else if (inReservedMemory && currentDepth > reservedMemoryDepth) {
|
||||||
|
UInt32* cells = (UInt32*)structs;
|
||||||
|
UInt64 base = Merge32To64(BytesSwap32(cells[1]), BytesSwap32(cells[0]));
|
||||||
|
UInt64 size = Merge32To64(BytesSwap32(cells[3]), BytesSwap32(cells[2]));
|
||||||
|
|
||||||
|
UInt32 index = bootMap->reservedCount;
|
||||||
|
bootMap->reserved[index].base = base;
|
||||||
|
bootMap->reserved[index].size = size;
|
||||||
|
bootMap->reservedCount++;
|
||||||
|
|
||||||
|
OSLog("Reserved Region (%s): base=0x%x, size=0x%x\n", currentNode, base, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
structs += propertyLength;
|
structs += propertyLength;
|
||||||
structs = (BytePointer)AlignUp64((Address)structs, 4);
|
structs = (BytePointer)AlignUp64((Address)structs, 4);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case FDTTokenEndNode: break;
|
case FDTTokenEndNode: {
|
||||||
|
if (inReservedMemory && currentDepth == reservedMemoryDepth) {
|
||||||
|
inReservedMemory = false;
|
||||||
|
}
|
||||||
|
currentDepth--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case FDTTokenNOP: continue;
|
case FDTTokenNOP: continue;
|
||||||
case FDTTokenEnd: return;
|
case FDTTokenEnd: return;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "../Common/bootinfo.h"
|
#include "../Common/bootinfo.h"
|
||||||
|
#include <VM/PMM.h>
|
||||||
#include <Arch/DTB.h>
|
#include <Arch/DTB.h>
|
||||||
#include <OS/Log.h>
|
#include <OS/Log.h>
|
||||||
#include <OS/Panic.h>
|
#include <OS/Panic.h>
|
||||||
@@ -9,5 +10,8 @@ void KernelMain(Bootinfo* bootinfo) {
|
|||||||
OSPanic("Invalid bootinfo magic");
|
OSPanic("Invalid bootinfo magic");
|
||||||
}
|
}
|
||||||
|
|
||||||
DTBParse(bootinfo->dtb);
|
VMBootMemoryMap bootMap = {0};
|
||||||
|
bootMap.reservedCount = 0;
|
||||||
|
DTBParse(bootinfo->dtb, &bootMap);
|
||||||
|
OSLog("Meow...\n");
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include <Lib/Stubs.h>
|
||||||
|
#include <Lib/String.h>
|
||||||
|
|
||||||
|
void* memset(void* destination, int value, Size count) {
|
||||||
|
return StringSet(destination, value, count);
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#include <VM/PMM.h>
|
||||||
|
|
||||||
|
static inline Size BitmapGetByteIndex(Address address) {
|
||||||
|
return (address / kVMPageSize) / kVMBlocksPerByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline UInt8 BitmapGetBitOffset(Address address) {
|
||||||
|
return (UInt8)((address / kVMPageSize) % kVMBlocksPerByte);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline Boolean BitmapTest(const MemoryPointer bitmap, Address address) {
|
||||||
|
return (bitmap[BitmapGetByteIndex(address)] & (1U << BitmapGetBitOffset(address))) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void BitmapSet(MemoryPointer bitmap, Address address) {
|
||||||
|
bitmap[BitmapGetByteIndex(address)] |= (1U << BitmapGetBitOffset(address));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void BitmapUnset(MemoryPointer bitmap, Address address) {
|
||||||
|
bitmap[BitmapGetByteIndex(address)] &= ~(1U << BitmapGetBitOffset(address));
|
||||||
|
}
|
||||||
|
|
||||||
@@ -7,14 +7,22 @@ PHDRS
|
|||||||
SECTIONS
|
SECTIONS
|
||||||
{
|
{
|
||||||
. = 0x40100000;
|
. = 0x40100000;
|
||||||
|
_kernelStart = .;
|
||||||
|
|
||||||
.text : {
|
.text : {
|
||||||
*(.text.boot)
|
*(.text.boot)
|
||||||
*(.text*)
|
*(.text*)
|
||||||
} :text
|
} :text
|
||||||
|
|
||||||
|
. = ALIGN(8);
|
||||||
.rodata : { *(.rodata*) } :text
|
.rodata : { *(.rodata*) } :text
|
||||||
|
|
||||||
|
. = ALIGN(4096);
|
||||||
.data : { *(.data*) } :data
|
.data : { *(.data*) } :data
|
||||||
|
|
||||||
|
. = ALIGN(8);
|
||||||
.bss : { *(.bss*) *(COMMON) } :data
|
.bss : { *(.bss*) *(COMMON) } :data
|
||||||
|
|
||||||
|
. = ALIGN(4096);
|
||||||
|
_kernelEnd = .;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user