From 4eca05fe67e6a61a30af9d168590f3f3aaf9f639 Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Wed, 6 Jan 2021 01:25:58 +1100 Subject: cpu: Allow recompilers to work with W^X policy --- src/osd/modules/lib/osdlib.h | 88 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) (limited to 'src/osd/modules/lib/osdlib.h') diff --git a/src/osd/modules/lib/osdlib.h b/src/osd/modules/lib/osdlib.h index 0095244d6fe..59b5291b0a8 100644 --- a/src/osd/modules/lib/osdlib.h +++ b/src/osd/modules/lib/osdlib.h @@ -16,6 +16,7 @@ #ifndef __OSDLIB__ #define __OSDLIB__ +#include #include #include #include @@ -58,6 +59,90 @@ int osd_setenv(const char *name, const char *value, int overwrite); -----------------------------------------------------------------------------*/ std::string osd_get_clipboard_text(void); +namespace osd { + +bool invalidate_instruction_cache(void const *start, std::size_t size); + + +class virtual_memory_allocation +{ +public: + enum : unsigned + { + NONE = 0x00, + READ = 0x01, + WRITE = 0x02, + EXECUTE = 0x04, + READ_WRITE = READ | WRITE, + READ_EXECUTE = READ | EXECUTE + }; + + virtual_memory_allocation(virtual_memory_allocation const &) = delete; + virtual_memory_allocation &operator=(virtual_memory_allocation const &) = delete; + + virtual_memory_allocation() { } + virtual_memory_allocation(std::initializer_list blocks) + { + m_memory = do_alloc(blocks, m_size, m_page_size); + } + virtual_memory_allocation(virtual_memory_allocation &&that) : m_memory(that.m_memory), m_size(that.m_size), m_page_size(that.m_page_size) + { + that.m_memory = nullptr; + that.m_size = that.m_page_size = 0U; + } + ~virtual_memory_allocation() + { + if (m_memory) + do_free(m_memory, m_size); + } + + explicit operator bool() const { return bool(m_memory); } + void *get() { return m_memory; } + std::size_t size() const { return m_size; } + std::size_t page_size() const { return m_page_size; } + + bool set_access(std::size_t start, std::size_t size, unsigned access) + { + if ((start % m_page_size) || (size % m_page_size) || (start > m_size) || ((m_size - start) < size)) + return false; + else + return do_set_access(reinterpret_cast(m_memory) + start, size, access); + } + + virtual_memory_allocation &operator=(std::nullptr_t) + { + if (m_memory) + do_free(m_memory, m_size); + m_memory = nullptr; + m_size = m_page_size = 0U; + return *this; + } + + virtual_memory_allocation &operator=(virtual_memory_allocation &&that) + { + if (&that != this) + { + if (m_memory) + do_free(m_memory, m_size); + m_memory = that.m_memory; + m_size = that.m_size; + m_page_size = that.m_page_size; + that.m_memory = nullptr; + that.m_size = that.m_page_size = 0U; + } + return *this; + } + +private: + static void *do_alloc(std::initializer_list blocks, std::size_t &size, std::size_t &page_size); + static void do_free(void *start, std::size_t size); + static bool do_set_access(void *start, std::size_t size, unsigned access); + + void *m_memory = nullptr; + std::size_t m_size = 0U, m_page_size = 0U; +}; + + /*----------------------------------------------------------------------------- dynamic_module: load functions from optional shared libraries @@ -69,7 +154,6 @@ std::string osd_get_clipboard_text(void); revisions of a same library) -----------------------------------------------------------------------------*/ -namespace osd { class dynamic_module { public: @@ -80,7 +164,7 @@ public: virtual ~dynamic_module() { }; template - typename std::enable_if::value, T>::type bind(char const *symbol) + typename std::enable_if_t, T> bind(char const *symbol) { return reinterpret_cast(get_symbol_address(symbol)); } -- cgit v1.2.3