From d761c5024b20cb09a4052b3f0aaff60ea9d03b5e Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Tue, 13 Jul 2021 17:17:12 +0200 Subject: [PATCH] AK: Generalize ByteReader Also use it instead of CPU.h's possibly_unaligned_data interface --- AK/ByteReader.h | 59 +++++---------------------- Kernel/Arch/x86/CPU.h | 56 ------------------------- Kernel/Bus/PCI/MMIOAccess.cpp | 9 ++-- Kernel/Bus/PCI/WindowedMMIOAccess.cpp | 9 ++-- 4 files changed, 20 insertions(+), 113 deletions(-) diff --git a/AK/ByteReader.h b/AK/ByteReader.h index c0da52fe9b5327..71d451f76dbd24 100644 --- a/AK/ByteReader.h +++ b/AK/ByteReader.h @@ -6,68 +6,29 @@ #pragma once +#include #include namespace AK { struct ByteReader { - static void store(u8* address, u16 value) - { - union { - u16 _16; - u8 _8[2]; - } const v { ._16 = value }; - __builtin_memcpy(address, v._8, 2); - } - - static void store(u8* address, u32 value) - { - union { - u32 _32; - u8 _8[4]; - } const v { ._32 = value }; - __builtin_memcpy(address, v._8, 4); - } - - static void load(const u8* address, u16& value) - { - union { - u16 _16; - u8 _8[2]; - } v { ._16 = 0 }; - __builtin_memcpy(&v._8, address, 2); - value = v._16; - } - - static void load(const u8* address, u32& value) + template + requires(IsTriviallyCopyable) static void store(u8* addr, T value) { - union { - u32 _32; - u8 _8[4]; - } v { ._32 = 0 }; - __builtin_memcpy(&v._8, address, 4); - value = v._32; + __builtin_memcpy(addr, &value, sizeof(T)); } - - static void load(const u8* address, u64& value) + template + requires(IsTriviallyConstructible) static void load(const u8* addr, T& value) { - union { - u64 _64; - u8 _8[8]; - } v { ._64 = 0 }; - __builtin_memcpy(&v._8, address, 8); - value = v._64; + __builtin_memcpy(&value, addr, sizeof(T)); } template static T* load_pointer(const u8* address) { - if constexpr (sizeof(T*) == 4) { - return reinterpret_cast(load32(address)); - } else { - static_assert(sizeof(T*) == 8, "sizeof(T*) must be either 4 or 8"); - return reinterpret_cast(load64(address)); - } + FlatPtr value; + load(address, value); + return reinterpret_cast(value); } static u16 load16(const u8* address) diff --git a/Kernel/Arch/x86/CPU.h b/Kernel/Arch/x86/CPU.h index cd21abeaffd1ac..77312bfbb9a736 100644 --- a/Kernel/Arch/x86/CPU.h +++ b/Kernel/Arch/x86/CPU.h @@ -30,62 +30,6 @@ inline u32 get_iopl_from_eflags(u32 eflags) return (eflags & iopl_mask) >> 12; } -template -void read_possibly_unaligned_data(u8* where, T& data) requires(sizeof(T) == 8 || sizeof(T) == 4 || sizeof(T) == 2) -{ - if (((FlatPtr)where % alignof(T)) == 0) { - data = *(T*)where; - return; - } - if constexpr (sizeof(T) == 2) { - data = *where | ((u16)(*(where + 1)) << 8); - return; - } - if constexpr (sizeof(T) == 4) { - data = *where | (((u32) * (where + 1)) << 8) | (((u32) * (where + 2)) << 16) | (((u32) * (where + 3)) << 24); - return; - } - if constexpr (sizeof(T) == 8) { - data = *where | (((u32) * (where + 1)) << 8) | (((u64) * (where + 2)) << 16) | (((u64) * (where + 3)) << 24) - | (((u64) * (where + 4)) << 32) | (((u64) * (where + 5)) << 40) | (((u64) * (where + 6)) << 48) | (((u64) * (where + 7)) << 56); - return; - } - VERIFY_NOT_REACHED(); -} - -template -void write_possibly_unaligned_data(u8* where, T data) requires(sizeof(T) == 8 || sizeof(T) == 4 || sizeof(T) == 2) -{ - if (((FlatPtr)where % alignof(T)) == 0) { - *(T*)where = data; - return; - } - if constexpr (sizeof(T) == 2) { - where[0] = (u8)(data & 0xFF); - where[1] = (u8)((data >> 8) & 0xFF); - return; - } - if constexpr (sizeof(T) == 4) { - where[0] = (u8)(data & 0xFF); - where[1] = (u8)((data >> 8) & 0xFF); - where[2] = (u8)((data >> 16) & 0xFF); - where[3] = (u8)((data >> 24) & 0xFF); - return; - } - if constexpr (sizeof(T) == 8) { - where[0] = (u8)(data & 0xFF); - where[1] = (u8)((data >> 8) & 0xFF); - where[2] = (u8)((data >> 16) & 0xFF); - where[3] = (u8)((data >> 24) & 0xFF); - where[5] = (u8)(data >> 32 & 0xFF); - where[5] = (u8)((data >> 40) & 0xFF); - where[6] = (u8)((data >> 48) & 0xFF); - where[7] = (u8)((data >> 52) & 0xFF); - return; - } - VERIFY_NOT_REACHED(); -} - const DescriptorTablePointer& get_gdtr(); const DescriptorTablePointer& get_idtr(); diff --git a/Kernel/Bus/PCI/MMIOAccess.cpp b/Kernel/Bus/PCI/MMIOAccess.cpp index d807cf71fb9356..f8d0945a1c546d 100644 --- a/Kernel/Bus/PCI/MMIOAccess.cpp +++ b/Kernel/Bus/PCI/MMIOAccess.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -128,7 +129,7 @@ u16 MMIOAccess::read16_field(Address address, u32 field) VERIFY(field < 0xfff); dbgln_if(PCI_DEBUG, "PCI: MMIO Reading 16-bit field {:#08x} for {}", field, address); u16 data = 0; - read_possibly_unaligned_data(get_device_configuration_space(address).offset(field & 0xfff).as_ptr(), data); + ByteReader::load(get_device_configuration_space(address).offset(field & 0xfff).as_ptr(), data); return data; } @@ -138,7 +139,7 @@ u32 MMIOAccess::read32_field(Address address, u32 field) VERIFY(field <= 0xffc); dbgln_if(PCI_DEBUG, "PCI: MMIO Reading 32-bit field {:#08x} for {}", field, address); u32 data = 0; - read_possibly_unaligned_data(get_device_configuration_space(address).offset(field & 0xfff).as_ptr(), data); + ByteReader::load(get_device_configuration_space(address).offset(field & 0xfff).as_ptr(), data); return data; } @@ -154,14 +155,14 @@ void MMIOAccess::write16_field(Address address, u32 field, u16 value) ScopedSpinLock lock(m_access_lock); VERIFY(field < 0xfff); dbgln_if(PCI_DEBUG, "PCI: MMIO Writing 16-bit field {:#08x}, value={:#02x} for {}", field, value, address); - write_possibly_unaligned_data(get_device_configuration_space(address).offset(field & 0xfff).as_ptr(), value); + ByteReader::store(get_device_configuration_space(address).offset(field & 0xfff).as_ptr(), value); } void MMIOAccess::write32_field(Address address, u32 field, u32 value) { ScopedSpinLock lock(m_access_lock); VERIFY(field <= 0xffc); dbgln_if(PCI_DEBUG, "PCI: MMIO Writing 32-bit field {:#08x}, value={:#02x} for {}", field, value, address); - write_possibly_unaligned_data(get_device_configuration_space(address).offset(field & 0xfff).as_ptr(), value); + ByteReader::store(get_device_configuration_space(address).offset(field & 0xfff).as_ptr(), value); } void MMIOAccess::enumerate_hardware(Function callback) diff --git a/Kernel/Bus/PCI/WindowedMMIOAccess.cpp b/Kernel/Bus/PCI/WindowedMMIOAccess.cpp index 181d8e9920260c..ef1503553f3408 100644 --- a/Kernel/Bus/PCI/WindowedMMIOAccess.cpp +++ b/Kernel/Bus/PCI/WindowedMMIOAccess.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -79,7 +80,7 @@ u16 WindowedMMIOAccess::read16_field(Address address, u32 field) VERIFY(field < 0xfff); dbgln_if(PCI_DEBUG, "PCI: MMIO Reading 16-bit field {:#08x} for {}", field, address); u16 data = 0; - read_possibly_unaligned_data(get_device_configuration_space(address).value().offset(field & 0xfff).as_ptr(), data); + ByteReader::load(get_device_configuration_space(address).value().offset(field & 0xfff).as_ptr(), data); return data; } @@ -89,7 +90,7 @@ u32 WindowedMMIOAccess::read32_field(Address address, u32 field) VERIFY(field <= 0xffc); dbgln_if(PCI_DEBUG, "PCI: MMIO Reading 32-bit field {:#08x} for {}", field, address); u32 data = 0; - read_possibly_unaligned_data(get_device_configuration_space(address).value().offset(field & 0xfff).as_ptr(), data); + ByteReader::load(get_device_configuration_space(address).value().offset(field & 0xfff).as_ptr(), data); return data; } @@ -105,14 +106,14 @@ void WindowedMMIOAccess::write16_field(Address address, u32 field, u16 value) InterruptDisabler disabler; VERIFY(field < 0xfff); dbgln_if(PCI_DEBUG, "PCI: MMIO Writing 16-bit field {:#08x}, value={:#02x} for {}", field, value, address); - write_possibly_unaligned_data(get_device_configuration_space(address).value().offset(field & 0xfff).as_ptr(), value); + ByteReader::store(get_device_configuration_space(address).value().offset(field & 0xfff).as_ptr(), value); } void WindowedMMIOAccess::write32_field(Address address, u32 field, u32 value) { InterruptDisabler disabler; VERIFY(field <= 0xffc); dbgln_if(PCI_DEBUG, "PCI: MMIO Writing 32-bit field {:#08x}, value={:#02x} for {}", field, value, address); - write_possibly_unaligned_data(get_device_configuration_space(address).value().offset(field & 0xfff).as_ptr(), value); + ByteReader::store(get_device_configuration_space(address).value().offset(field & 0xfff).as_ptr(), value); } }