Make ARCH_PAGE_SIZE and ARCH_TIMER_FREQ known at compile-time

This commit is contained in:
apio 2022-12-05 16:36:41 +01:00
parent 4f183958e3
commit 1e3706ac01
Signed by: apio
GPG Key ID: B8A7D06E42258954
9 changed files with 75 additions and 60 deletions

View File

@ -18,6 +18,10 @@ set(CMAKE_FIND_ROOT_PATH ${LUNA_ROOT}/toolchain/x86-64-luna)
set(ARCH $ENV{ARCH})
if(NOT DEFINED $ENV{ARCH})
set(ARCH "x86_64")
endif()
message(STATUS "Configuring Luna for ${ARCH}")
add_subdirectory(luna)

View File

@ -1,9 +1,11 @@
#pragma once
#include <luna/Result.h>
struct PageDirectory;
extern const usize ARCH_PAGE_SIZE;
#ifdef ARCH_X86_64
#include "arch/x86_64/MMU.h"
#else
#error "Unknown architecture."
#endif
namespace MMU
{

View File

@ -73,9 +73,9 @@ namespace Timer
usize ticks_us() // We want a bit of precision; if there are 10 ticks/ms, do not return the truncated ms value *
// 1000, but ticks * 100 (1000/10), which is more precise
{
if (ARCH_TIMER_FREQ > 1000) [[unlikely]]
return timer_ticks / (ARCH_TIMER_FREQ / 1000);
return timer_ticks * (1000 / ARCH_TIMER_FREQ);
if constexpr (ARCH_TIMER_FREQ > 1000) return timer_ticks / (ARCH_TIMER_FREQ / 1000);
else
return timer_ticks * (1000 / ARCH_TIMER_FREQ);
}
usize ticks_ns()

View File

@ -1,7 +1,11 @@
#pragma once
#include <luna/Types.h>
extern const usize ARCH_TIMER_FREQ; // How many timer ticks in one millisecond?
#ifdef ARCH_X86_64
#include "arch/x86_64/Timer.h"
#else
#error "Unknown architecture."
#endif
static const usize MS_PER_SECOND = 1000;
static const usize US_PER_SECOND = MS_PER_SECOND * 1000;

View File

@ -3,41 +3,6 @@
#include <luna/String.h>
#include <luna/SystemError.h>
#define PAGE_SIZE 4096
const usize ARCH_PAGE_SIZE = PAGE_SIZE;
const u64 rindex = 0776; // recursive index
const u64 sign = 0177777UL << 48; // sign extension
struct [[gnu::packed]] PageTableEntry
{
union {
struct [[gnu::packed]]
{
bool present : 1;
bool read_write : 1;
bool user : 1;
bool write_through : 1;
bool cache_disabled : 1;
bool accessed : 1;
bool ignore0 : 1;
bool larger_pages : 1;
bool ignore1 : 1;
u8 available : 3;
u64 address : 48;
u8 available2 : 3;
bool no_execute : 1;
};
u64 raw;
};
void set_address(u64 addr);
u64 get_address() const;
void clear();
};
#pragma GCC push_options
#pragma GCC diagnostic ignored "-Wconversion"
@ -58,14 +23,6 @@ void PageTableEntry::clear()
raw = 0;
}
struct alignas(PAGE_SIZE) PageDirectory
{
PageTableEntry entries[512];
};
static_assert(sizeof(PageTableEntry) == 8UL);
static_assert(sizeof(PageDirectory) == PAGE_SIZE);
namespace MMU
{
@ -213,7 +170,7 @@ namespace MMU
u64 addr = TRY(MemoryManager::alloc_frame());
l4.present = true;
l4.set_address(addr);
memset(l3_table(virt), 0, PAGE_SIZE);
memset(l3_table(virt), 0, ARCH_PAGE_SIZE);
l4.ignore0 = l4.ignore1 = 0;
}
if (flags & Flags::ReadWrite) l4.read_write = true;
@ -225,7 +182,7 @@ namespace MMU
u64 addr = TRY(MemoryManager::alloc_frame());
l3.present = true;
l3.set_address(addr);
memset(l2_table(virt), 0, PAGE_SIZE);
memset(l2_table(virt), 0, ARCH_PAGE_SIZE);
l3.ignore0 = l3.ignore1 = 0;
}
if (flags & Flags::ReadWrite) l3.read_write = true;
@ -239,7 +196,7 @@ namespace MMU
u64 addr = TRY(MemoryManager::alloc_frame());
l2.present = true;
l2.set_address(addr);
memset(l1_table(virt), 0, PAGE_SIZE);
memset(l1_table(virt), 0, ARCH_PAGE_SIZE);
l2.ignore0 = l2.ignore1 = 0;
}
if (flags & Flags::ReadWrite) l2.read_write = true;

View File

@ -0,0 +1,43 @@
#pragma once
#include <luna/Types.h>
const usize ARCH_PAGE_SIZE = 4096;
const u64 rindex = 0776; // recursive index
const u64 sign = 0177777UL << 48; // sign extension
struct [[gnu::packed]] PageTableEntry
{
union {
struct [[gnu::packed]]
{
bool present : 1;
bool read_write : 1;
bool user : 1;
bool write_through : 1;
bool cache_disabled : 1;
bool accessed : 1;
bool ignore0 : 1;
bool larger_pages : 1;
bool ignore1 : 1;
u8 available : 3;
u64 address : 48;
u8 available2 : 3;
bool no_execute : 1;
};
u64 raw;
};
void set_address(u64 addr);
u64 get_address() const;
void clear();
};
struct alignas(ARCH_PAGE_SIZE) PageDirectory
{
PageTableEntry entries[512];
};
static_assert(sizeof(PageTableEntry) == 8UL);
static_assert(sizeof(PageDirectory) == ARCH_PAGE_SIZE);

View File

@ -4,7 +4,6 @@
#define PIT_CHANNEL_0 0x40
const u64 base_frequency = 1193182;
const usize ARCH_TIMER_FREQ = 5;
void Timer::arch_init()
{

View File

@ -0,0 +1,4 @@
#pragma once
#include <luna/Types.h>
const usize ARCH_TIMER_FREQ = 5;

View File

@ -24,11 +24,6 @@ target_compile_options(luna-freestanding PRIVATE -fno-rtti -ffreestanding -fno-e
target_compile_options(luna-freestanding PRIVATE -fno-asynchronous-unwind-tables -fno-omit-frame-pointer)
target_compile_options(luna-freestanding PRIVATE -nostdlib -mcmodel=kernel)
if("${ARCH}" MATCHES "x86_64")
target_compile_options(luna-freestanding PRIVATE -mno-red-zone)
target_compile_options(luna-freestanding PRIVATE -mno-80387 -mno-mmx -mno-sse -mno-sse2)
endif()
target_include_directories(luna-freestanding PUBLIC include/)
set_target_properties(luna-freestanding PROPERTIES CXX_STANDARD 20)
@ -40,3 +35,10 @@ target_compile_options(luna PRIVATE -Wcast-align -Wwrite-strings -Wlogical-op -W
target_compile_options(luna PRIVATE -fno-asynchronous-unwind-tables -fno-omit-frame-pointer)
target_include_directories(luna PUBLIC include/)
set_target_properties(luna PROPERTIES CXX_STANDARD 20)
if("${ARCH}" MATCHES "x86_64")
target_compile_options(luna-freestanding PRIVATE -mno-red-zone)
target_compile_options(luna-freestanding PRIVATE -mno-80387 -mno-mmx -mno-sse -mno-sse2)
target_compile_definitions(luna-freestanding PUBLIC ARCH_X86_64)
target_compile_definitions(luna PUBLIC ARCH_X86_64)
endif()