From c31adb77e783adcecda586b5feddea3d7dc863cf Mon Sep 17 00:00:00 2001 From: Johannes Schlatow Date: Mon, 15 Apr 2024 14:41:29 +0200 Subject: [PATCH] base: add shared page-table code for x86_64 Issue #5217 --- .../include/spec/x86_64/cpu}/clflush.h | 10 ++--- .../spec/x86_64/page_table}/page_flags.h | 12 +++--- .../spec/x86_64/page_table}/page_table_base.h | 35 +++++++--------- repos/pc/src/drivers/platform/pc/hw/util.h | 42 ------------------- .../drivers/platform/pc/intel/context_table.h | 10 ++--- .../drivers/platform/pc/intel/page_table.h | 2 +- .../drivers/platform/pc/intel/root_table.h | 6 +-- 7 files changed, 33 insertions(+), 84 deletions(-) rename repos/{pc/src/drivers/platform/pc => base/include/spec/x86_64/cpu}/clflush.h (62%) rename repos/{pc/src/drivers/platform/pc/hw => base/include/spec/x86_64/page_table}/page_flags.h (88%) rename repos/{pc/src/drivers/platform/pc/spec/x86_64 => base/include/spec/x86_64/page_table}/page_table_base.h (95%) delete mode 100644 repos/pc/src/drivers/platform/pc/hw/util.h diff --git a/repos/pc/src/drivers/platform/pc/clflush.h b/repos/base/include/spec/x86_64/cpu/clflush.h similarity index 62% rename from repos/pc/src/drivers/platform/pc/clflush.h rename to repos/base/include/spec/x86_64/cpu/clflush.h index 6c4d240370..7d816f2269 100644 --- a/repos/pc/src/drivers/platform/pc/clflush.h +++ b/repos/base/include/spec/x86_64/cpu/clflush.h @@ -1,5 +1,5 @@ /* - * \brief Helper for flushing translation-table entries from cache + * \brief Helper for flushing cache lines * \author Johannes Schlatow * \date 2023-09-20 */ @@ -11,14 +11,14 @@ * under the terms of the GNU Affero General Public License version 3. */ -#ifndef _SRC__DRIVERS__PLATFORM__CLFLUSH_H_ -#define _SRC__DRIVERS__PLATFORM__CLFLUSH_H_ +#ifndef _INCLUDE__SPEC__X86_64__CLFLUSH_H_ +#define _INCLUDE__SPEC__X86_64__CLFLUSH_H_ -namespace Utils { +namespace Genode { inline void clflush(volatile void *addr) { asm volatile("clflush %0" : "+m" (*(volatile char *)addr)); } } -#endif /* _SRC__DRIVERS__PLATFORM__CLFLUSH_H_ */ +#endif /* _INCLUDE__SPEC__X86_64__CLFLUSH_H_ */ diff --git a/repos/pc/src/drivers/platform/pc/hw/page_flags.h b/repos/base/include/spec/x86_64/page_table/page_flags.h similarity index 88% rename from repos/pc/src/drivers/platform/pc/hw/page_flags.h rename to repos/base/include/spec/x86_64/page_table/page_flags.h index 4bfb43f83d..5b4c871e4f 100644 --- a/repos/pc/src/drivers/platform/pc/hw/page_flags.h +++ b/repos/base/include/spec/x86_64/page_table/page_flags.h @@ -11,13 +11,13 @@ * under the terms of the GNU Affero General Public License version 3. */ -#ifndef _SRC__DRIVERS__PLATFORM__PC__HW__PAGE_FLAGS_H_ -#define _SRC__DRIVERS__PLATFORM__PC__HW__PAGE_FLAGS_H_ +#ifndef _INCLUDE__SPEC__X86_64__PAGE_TABLE__PAGE_FLAGS_H_ +#define _INCLUDE__SPEC__X86_64__PAGE_TABLE__PAGE_FLAGS_H_ #include #include -namespace Hw { +namespace Genode { enum Writeable { RO, RW }; enum Executeable { NO_EXEC, EXEC }; @@ -29,7 +29,7 @@ namespace Hw { } -struct Hw::Page_flags +struct Genode::Page_flags { Writeable writeable; Executeable executable; @@ -57,7 +57,7 @@ struct Hw::Page_flags }; -namespace Hw { +namespace Genode { static constexpr Page_flags PAGE_FLAGS_KERN_IO { RW, NO_EXEC, KERN, GLOBAL, DEVICE, Genode::UNCACHED }; @@ -71,4 +71,4 @@ namespace Hw { { RW, NO_EXEC, USER, NO_GLOBAL, RAM, Genode::CACHED }; } -#endif /* _SRC__DRIVERS__PLATFORM__PC__HW__PAGE_FLAGS_H_ */ +#endif /* _INCLUDE__SPEC__X86_64__PAGE_TABLE__PAGE_FLAGS_H_ */ diff --git a/repos/pc/src/drivers/platform/pc/spec/x86_64/page_table_base.h b/repos/base/include/spec/x86_64/page_table/page_table_base.h similarity index 95% rename from repos/pc/src/drivers/platform/pc/spec/x86_64/page_table_base.h rename to repos/base/include/spec/x86_64/page_table/page_table_base.h index 5d6ad76c3f..278cff14f3 100644 --- a/repos/pc/src/drivers/platform/pc/spec/x86_64/page_table_base.h +++ b/repos/base/include/spec/x86_64/page_table/page_table_base.h @@ -12,23 +12,18 @@ * under the terms of the GNU Affero General Public License version 3. */ -#ifndef _SRC__DRIVERS__PLATFORM__PC__SPEC__X86_64__PAGE_TABLE_BASE_H_ -#define _SRC__DRIVERS__PLATFORM__PC__SPEC__X86_64__PAGE_TABLE_BASE_H_ +#ifndef _INCLUDE__SPEC__X86_64__PAGE_TABLE__PAGE_TABLE_BASE_H_ +#define _INCLUDE__SPEC__X86_64__PAGE_TABLE__PAGE_TABLE_BASE_H_ #include -#include -#include +#include #include - -#include -#include +#include #define assert(expression) namespace Genode { - using namespace Hw; - /** * (Generic) 4-level translation structures. */ @@ -114,7 +109,7 @@ class Genode::Final_table desc = table_entry; if (flush) - Utils::clflush(&desc); + clflush(&desc); } }; @@ -130,7 +125,7 @@ class Genode::Final_table desc = 0; if (flush) - Utils::clflush(&desc); + clflush(&desc); } }; @@ -165,7 +160,7 @@ class Genode::Final_table */ Final_table() { - if (!Hw::aligned(this, ALIGNM_LOG2)) throw Misaligned(); + if (!aligned((addr_t)this, ALIGNM_LOG2)) throw Misaligned(); Genode::memset(&_entries, 0, sizeof(_entries)); } @@ -280,7 +275,7 @@ class Genode::Page_directory desc = table_entry; if (flush) - Utils::clflush(&desc); + clflush(&desc); return; } @@ -292,7 +287,7 @@ class Genode::Page_directory desc = (access_t) Td::create(table_phys); if (flush) - Utils::clflush(&desc); + clflush(&desc); } else if (Descriptor::maps_page(desc)) { throw Double_insertion(); @@ -349,7 +344,7 @@ class Genode::Page_directory } if (desc == 0 && flush) - Utils::clflush(&desc); + clflush(&desc); } } }; @@ -382,7 +377,7 @@ class Genode::Page_directory Page_directory() { - if (!Hw::aligned(this, ALIGNM_LOG2)) throw Misaligned(); + if (!aligned((addr_t)this, ALIGNM_LOG2)) throw Misaligned(); Genode::memset(&_entries, 0, sizeof(_entries)); } @@ -487,7 +482,7 @@ class Genode::Pml4_table desc = Descriptor::create(table_phys); if (flush) - Utils::clflush(&desc); + clflush(&desc); } /* insert translation */ @@ -530,7 +525,7 @@ class Genode::Pml4_table desc = 0; if (flush) - Utils::clflush(&desc); + clflush(&desc); } }, [&] () { @@ -579,7 +574,7 @@ class Genode::Pml4_table Pml4_table() { - if (!Hw::aligned(this, ALIGNM_LOG2)) throw Misaligned(); + if (!aligned((addr_t)this, ALIGNM_LOG2)) throw Misaligned(); Genode::memset(&_entries, 0, sizeof(_entries)); } @@ -648,4 +643,4 @@ class Genode::Pml4_table } __attribute__((aligned(1 << ALIGNM_LOG2))); -#endif /* _SRC__DRIVERS__PLATFORM__PC__SPEC__X86_64__PAGE_TABLE_BASE_H_ */ +#endif /* _INCLUDE__SPEC__X86_64__PAGE_TABLE__PAGE_TABLE_BASE_H_ */ diff --git a/repos/pc/src/drivers/platform/pc/hw/util.h b/repos/pc/src/drivers/platform/pc/hw/util.h deleted file mode 100644 index ee2cbb9d00..0000000000 --- a/repos/pc/src/drivers/platform/pc/hw/util.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * \brief Common utilities - * \author Martin Stein - * \author Stefan Kalkowski - * \date 2012-01-02 - */ - -/* - * Copyright (C) 2012-2017 Genode Labs GmbH - * - * This file is part of the Genode OS framework, which is distributed - * under the terms of the GNU Affero General Public License version 3. - */ - -#ifndef _SRC__LIB__HW__UTIL_H_ -#define _SRC__LIB__HW__UTIL_H_ - -namespace Hw { - - using Genode::addr_t; - using Genode::size_t; - - /** - * Return an address rounded down to a specific alignment - * - * \param addr original address - * \param alignm_log2 log2 of the required alignment - */ - constexpr addr_t trunc(addr_t addr, addr_t alignm_log2) { - return (addr >> alignm_log2) << alignm_log2; } - - /** - * Return wether a pointer fullfills an alignment - * - * \param p pointer - * \param alignm_log2 log2 of the required alignment - */ - inline bool aligned(void * const p, addr_t alignm_log2) { - return (addr_t)p == trunc((addr_t)p, alignm_log2); } -} - -#endif /* _SRC__LIB__HW__UTIL_H_ */ diff --git a/repos/pc/src/drivers/platform/pc/intel/context_table.h b/repos/pc/src/drivers/platform/pc/intel/context_table.h index 2a0bf8fe14..1aeccd616d 100644 --- a/repos/pc/src/drivers/platform/pc/intel/context_table.h +++ b/repos/pc/src/drivers/platform/pc/intel/context_table.h @@ -22,9 +22,7 @@ #include #include #include - -/* local includes */ -#include +#include namespace Intel { using namespace Genode; @@ -138,7 +136,7 @@ class Intel::Context_table _entries[_lo_index(rid)] = lo_val; if (flush) - Utils::clflush(&_entries[_lo_index(rid)]); + clflush(&_entries[_lo_index(rid)]); } void remove(Pci::rid_t rid, bool flush) @@ -148,7 +146,7 @@ class Intel::Context_table _entries[_lo_index(rid)] = val; if (flush) - Utils::clflush(&_entries[_lo_index(rid)]); + clflush(&_entries[_lo_index(rid)]); } void generate(Xml_generator &, Env &, Intel::Report_helper &); @@ -156,7 +154,7 @@ class Intel::Context_table void flush_all() { for (Genode::size_t i=0; i < 512; i+=8) - Utils::clflush(&_entries[i]); + clflush(&_entries[i]); } Context_table() diff --git a/repos/pc/src/drivers/platform/pc/intel/page_table.h b/repos/pc/src/drivers/platform/pc/intel/page_table.h index a5cfdbfb95..b028299015 100644 --- a/repos/pc/src/drivers/platform/pc/intel/page_table.h +++ b/repos/pc/src/drivers/platform/pc/intel/page_table.h @@ -17,7 +17,7 @@ #include #include -#include +#include #include namespace Intel { diff --git a/repos/pc/src/drivers/platform/pc/intel/root_table.h b/repos/pc/src/drivers/platform/pc/intel/root_table.h index 84660cb22c..f67b1ff541 100644 --- a/repos/pc/src/drivers/platform/pc/intel/root_table.h +++ b/repos/pc/src/drivers/platform/pc/intel/root_table.h @@ -22,9 +22,7 @@ #include #include #include - -/* local includes */ -#include +#include namespace Intel { using namespace Genode; @@ -71,7 +69,7 @@ class Intel::Root_table _entries[bus*2] = Entry::Address::masked(addr) | Entry::Present::bits(1); if (flush) - Utils::clflush(&_entries[bus*2]); + clflush(&_entries[bus*2]); } void generate(Xml_generator &, Env &, Report_helper &);