Compare commits

...

3 Commits

5 changed files with 26 additions and 40 deletions

View File

@ -16,17 +16,12 @@ class CapabilityTable {
uint64_t AddNewCapability(const RefPtr<T>& object, uint64_t permissions); uint64_t AddNewCapability(const RefPtr<T>& object, uint64_t permissions);
uint64_t AddExistingCapability(const RefPtr<Capability>& cap); uint64_t AddExistingCapability(const RefPtr<Capability>& cap);
// FIXME: Remove reliance on this.
template <typename T>
void AddNewCapabilityWithId(uint64_t id, const RefPtr<T>& object,
uint64_t permissions);
RefPtr<Capability> GetCapability(uint64_t id); RefPtr<Capability> GetCapability(uint64_t id);
RefPtr<Capability> ReleaseCapability(uint64_t id); RefPtr<Capability> ReleaseCapability(uint64_t id);
private: private:
Mutex lock_{"cap table"}; Mutex lock_{"cap table"};
// FIXME: store this id here rather than in the capability. // TODO: Do some randomization.
uint64_t next_cap_id_ = 0x100; uint64_t next_cap_id_ = 0x100;
// FIXME: use a map data structure. // FIXME: use a map data structure.
struct CapEntry { struct CapEntry {
@ -45,11 +40,3 @@ uint64_t CapabilityTable::AddNewCapability(const RefPtr<T>& object,
{.id = id, .cap = MakeRefCounted<Capability>(object, permissions)}); {.id = id, .cap = MakeRefCounted<Capability>(object, permissions)});
return id; return id;
} }
template <typename T>
void CapabilityTable::AddNewCapabilityWithId(uint64_t id,
const RefPtr<T>& object,
uint64_t permissions) {
capabilities_.PushBack(
{.id = id, .cap = MakeRefCounted<Capability>(object, permissions)});
}

View File

@ -24,16 +24,6 @@
namespace { namespace {
uint64_t PageAlign(uint64_t addr) { return addr & ~0xFFF; } uint64_t PageAlign(uint64_t addr) { return addr & ~0xFFF; }
uint64_t* PageAlign(uint64_t* addr) {
return reinterpret_cast<uint64_t*>(reinterpret_cast<uint64_t>(addr) & ~0xFFF);
}
void ZeroOutPage(uint64_t* ptr) {
ptr = PageAlign(ptr);
for (uint64_t i = 0; i < 512; i++) {
ptr[i] = 0;
}
}
uint64_t ShiftForEntryIndexing(uint64_t addr, uint64_t offset) { uint64_t ShiftForEntryIndexing(uint64_t addr, uint64_t offset) {
addr &= ~0xFFFF0000'00000000; addr &= ~0xFFFF0000'00000000;
@ -126,21 +116,18 @@ void MapPage(uint64_t cr3, uint64_t vaddr, uint64_t paddr) {
uint64_t* pml4_entry = Pml4Entry(cr3, vaddr); uint64_t* pml4_entry = Pml4Entry(cr3, vaddr);
if (!(*pml4_entry & PRESENT_BIT)) { if (!(*pml4_entry & PRESENT_BIT)) {
uint64_t page = phys_mem::AllocatePage(); uint64_t page = phys_mem::AllocateAndZeroPage();
*pml4_entry = page | access_bits; *pml4_entry = page | access_bits;
ZeroOutPage(PageDirectoryPointerEntry(*pml4_entry, vaddr));
} }
uint64_t* pdp_entry = PageDirectoryPointerEntry(*pml4_entry, vaddr); uint64_t* pdp_entry = PageDirectoryPointerEntry(*pml4_entry, vaddr);
if (!(*pdp_entry & PRESENT_BIT)) { if (!(*pdp_entry & PRESENT_BIT)) {
uint64_t page = phys_mem::AllocatePage(); uint64_t page = phys_mem::AllocateAndZeroPage();
*pdp_entry = page | access_bits; *pdp_entry = page | access_bits;
ZeroOutPage(PageDirectoryEntry(*pdp_entry, vaddr));
} }
uint64_t* pd_entry = PageDirectoryEntry(*pdp_entry, vaddr); uint64_t* pd_entry = PageDirectoryEntry(*pdp_entry, vaddr);
if (!(*pd_entry & PRESENT_BIT)) { if (!(*pd_entry & PRESENT_BIT)) {
uint64_t page = phys_mem::AllocatePage(); uint64_t page = phys_mem::AllocateAndZeroPage();
*(pd_entry) = page | access_bits; *(pd_entry) = page | access_bits;
ZeroOutPage(PageTableEntry(*pd_entry, vaddr));
} }
uint64_t* pt_entry = PageTableEntry(*pd_entry, vaddr); uint64_t* pt_entry = PageTableEntry(*pd_entry, vaddr);
@ -158,9 +145,7 @@ uint64_t AllocatePageIfNecessary(uint64_t addr) {
return phys; return phys;
} }
phys = phys_mem::AllocatePage(); phys = phys_mem::AllocatePage();
// FIXME: Maybe move this to the physical memory allocator.
ZeroOutPage(
reinterpret_cast<uint64_t*>(boot::GetHigherHalfDirectMap() + phys));
MapPage(cr3, addr, phys); MapPage(cr3, addr, phys);
return phys; return phys;
} }

View File

@ -8,6 +8,17 @@
namespace phys_mem { namespace phys_mem {
namespace { namespace {
uint64_t* PageAlign(uint64_t* addr) {
return reinterpret_cast<uint64_t*>(reinterpret_cast<uint64_t>(addr) & ~0xFFF);
}
void ZeroOutPage(uint64_t* ptr) {
ptr = PageAlign(ptr);
for (uint64_t i = 0; i < 512; i++) {
ptr[i] = 0;
}
}
struct BootstrapMemory { struct BootstrapMemory {
uint64_t init_page = 0; uint64_t init_page = 0;
uint64_t next_page = 0; uint64_t next_page = 0;
@ -163,6 +174,13 @@ uint64_t AllocatePage() {
return page; return page;
} }
uint64_t AllocateAndZeroPage() {
uint64_t paddr = AllocatePage();
ZeroOutPage(
reinterpret_cast<uint64_t*>(boot::GetHigherHalfDirectMap() + paddr));
return paddr;
}
uint64_t AllocateContinuous(uint64_t num_continuous) { uint64_t AllocateContinuous(uint64_t num_continuous) {
if (gPmm == nullptr) { if (gPmm == nullptr) {
panic("No physical memory manager!"); panic("No physical memory manager!");

View File

@ -10,9 +10,12 @@ namespace phys_mem {
// to initialize so we need this first. // to initialize so we need this first.
void InitBootstrapPageAllocation(); void InitBootstrapPageAllocation();
// Initializes the main physical memory manager.
void InitPhysicalMemoryManager(); void InitPhysicalMemoryManager();
uint64_t AllocatePage(); uint64_t AllocatePage();
uint64_t AllocateAndZeroPage();
uint64_t AllocateContinuous(uint64_t num_pages); uint64_t AllocateContinuous(uint64_t num_pages);
void FreePage(uint64_t page); void FreePage(uint64_t page);

View File

@ -46,13 +46,6 @@ class Process : public KernelObject {
} }
uint64_t AddExistingCapability(const RefPtr<Capability>& cap); uint64_t AddExistingCapability(const RefPtr<Capability>& cap);
// FIXME: Eliminate reliance on this.
template <typename T>
void AddNewCapabilityWithId(uint64_t id, const RefPtr<T>& obj,
uint64_t permissions) {
return caps_.AddNewCapabilityWithId(id, obj, permissions);
}
// Checks the state of all child threads and transitions to // Checks the state of all child threads and transitions to
// finished if all have finished. // finished if all have finished.
void CheckState(); void CheckState();