Kernel: Add PageTable::Flags::CacheDisable

Also fix multiple places where we were using uint8_t as page table
flags instead of PageTable::flags_t which we promoted to uint16_t
while back.
This commit is contained in:
Bananymous 2023-08-06 23:59:30 +03:00
parent d8a9d4a24e
commit 008c777a9f
7 changed files with 36 additions and 18 deletions

View File

@ -48,7 +48,22 @@ namespace Kernel
static inline PageTable::flags_t parse_flags(uint64_t entry)
{
return (s_has_nxe && !(entry & (1ull << 63)) ? PageTable::Flags::Execute : 0) | (entry & 0b100000111);
using Flags = PageTable::Flags;
PageTable::flags_t result = 0;
if (s_has_nxe && !(entry & (1ull << 63)))
result |= Flags::Execute;
if (entry & Flags::Reserved)
result |= Flags::Reserved;
if (entry & Flags::CacheDisable)
result |= Flags::CacheDisable;
if (entry & Flags::UserSupervisor)
result |= Flags::UserSupervisor;
if (entry & Flags::ReadWrite)
result |= Flags::ReadWrite;
if (entry & Flags::Present)
result |= Flags::Present;
return result;
}
void PageTable::initialize()
@ -258,13 +273,15 @@ namespace Kernel
uint64_t pte = (uc_vaddr >> 12) & 0x1FF;
uint64_t extra_flags = 0;
if (s_has_pge && pml4e == 511) // Map kernel memory as global
extra_flags |= 1ull << 8;
if (s_has_nxe && !(flags & Flags::Execute))
extra_flags |= 1ull << 63;
if (s_has_pge && pml4e == 511)
extra_flags |= 1ull << 8;
if (flags & Flags::Reserved)
extra_flags |= Flags::Reserved;
if (flags & Flags::CacheDisable)
extra_flags |= Flags::CacheDisable;
// NOTE: we add present here, since it has to be available in higher level structures
flags_t uwr_flags = (flags & (Flags::UserSupervisor | Flags::ReadWrite)) | Flags::Present;

View File

@ -13,12 +13,13 @@ namespace Kernel
using flags_t = uint16_t;
enum Flags : flags_t
{
Present = 1,
ReadWrite = 2,
UserSupervisor = 4,
Execute = 8,
Reserved = 256,
Present = (1 << 0),
ReadWrite = (1 << 1),
UserSupervisor = (1 << 2),
CacheDisable = (1 << 4),
Reserved = (1 << 9),
Execute = (1 << 15),
Used = Present | Reserved,
};

View File

@ -15,9 +15,9 @@ namespace Kernel
public:
// Create virtual range to fixed virtual address
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_to_vaddr(PageTable&, vaddr_t, size_t, uint8_t flags);
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_to_vaddr(PageTable&, vaddr_t, size_t, PageTable::flags_t flags);
// Create virtual range to virtual address range
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_to_vaddr_range(PageTable&, vaddr_t vaddr_start, vaddr_t vaddr_end, size_t, uint8_t flags);
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_to_vaddr_range(PageTable&, vaddr_t vaddr_start, vaddr_t vaddr_end, size_t, PageTable::flags_t flags);
// Create virtual range in kernel memory with kmalloc
static BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> create_kmalloc(size_t);
~VirtualRange();
@ -26,7 +26,7 @@ namespace Kernel
vaddr_t vaddr() const { return m_vaddr; }
size_t size() const { return m_size; }
uint8_t flags() const { return m_flags; }
PageTable::flags_t flags() const { return m_flags; }
void set_zero();
void copy_from(size_t offset, const uint8_t* buffer, size_t bytes);
@ -39,7 +39,7 @@ namespace Kernel
bool m_kmalloc { false };
vaddr_t m_vaddr { 0 };
size_t m_size { 0 };
uint8_t m_flags { 0 };
PageTable::flags_t m_flags { 0 };
BAN::Vector<paddr_t> m_physical_pages;
};

View File

@ -263,7 +263,7 @@ namespace Kernel
vaddr_t vaddr = address_of_node(node);
vaddr_t page_begin = vaddr & PAGE_ADDR_MASK;
uint8_t flags = m_page_table.get_page_flags(page_begin);
PageTable::flags_t flags = m_page_table.get_page_flags(page_begin);
// Allocate and copy all data from this allocation to the new one
if (allocator->allocate_page_if_needed(page_begin, flags))

View File

@ -97,7 +97,7 @@ namespace Kernel
new_allocation.address = allocation.address;
MUST(new_allocation.pages.reserve(allocation.pages.size()));
uint8_t flags = m_page_table.get_page_flags(allocation.address);
PageTable::flags_t flags = m_page_table.get_page_flags(allocation.address);
for (size_t i = 0; i < allocation.pages.size(); i++)
{
paddr_t paddr = Heap::get().take_free_page();

View File

@ -6,7 +6,7 @@
namespace Kernel
{
BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> VirtualRange::create_to_vaddr(PageTable& page_table, vaddr_t vaddr, size_t size, uint8_t flags)
BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> VirtualRange::create_to_vaddr(PageTable& page_table, vaddr_t vaddr, size_t size, PageTable::flags_t flags)
{
ASSERT(size % PAGE_SIZE == 0);
ASSERT(vaddr % PAGE_SIZE == 0);
@ -45,7 +45,7 @@ namespace Kernel
return result;
}
BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> VirtualRange::create_to_vaddr_range(PageTable& page_table, vaddr_t vaddr_start, vaddr_t vaddr_end, size_t size, uint8_t flags)
BAN::ErrorOr<BAN::UniqPtr<VirtualRange>> VirtualRange::create_to_vaddr_range(PageTable& page_table, vaddr_t vaddr_start, vaddr_t vaddr_end, size_t size, PageTable::flags_t flags)
{
ASSERT(size % PAGE_SIZE == 0);
ASSERT(vaddr_start > 0);

View File

@ -499,7 +499,7 @@ namespace Kernel
break;
case LibELF::PT_LOAD:
{
uint8_t flags = PageTable::Flags::UserSupervisor | PageTable::Flags::Present;
PageTable::flags_t flags = PageTable::Flags::UserSupervisor | PageTable::Flags::Present;
if (elf_program_header.p_flags & LibELF::PF_W)
flags |= PageTable::Flags::ReadWrite;
if (elf_program_header.p_flags & LibELF::PF_X)