Skip to content

Commit

Permalink
AK: Add global FlatPtr typedef. It's u32 or u64, based on sizeof(void*)
Browse files Browse the repository at this point in the history
Use this instead of uintptr_t throughout the codebase. This makes it
possible to pass a FlatPtr to something that has u32 and u64 overloads.
  • Loading branch information
awesomekling committed Mar 8, 2020
1 parent b98d8ad commit b1058b3
Show file tree
Hide file tree
Showing 36 changed files with 165 additions and 162 deletions.
4 changes: 2 additions & 2 deletions AK/HashFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ inline unsigned u64_hash(u64 key)
return pair_int_hash(first, last);
}

inline unsigned ptr_hash(uintptr_t ptr)
inline unsigned ptr_hash(FlatPtr ptr)
{
if constexpr(sizeof(ptr) == 8)
return u64_hash((u64)ptr);
Expand All @@ -61,5 +61,5 @@ inline unsigned ptr_hash(uintptr_t ptr)

inline unsigned ptr_hash(const void* ptr)
{
return ptr_hash((uintptr_t)(ptr));
return ptr_hash((FlatPtr)(ptr));
}
3 changes: 3 additions & 0 deletions AK/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <AK/IterationDecision.h>
#include <AK/Platform.h>
#include <AK/StdLibExtras.h>

#ifdef __serenity__
typedef unsigned char u8;
Expand Down Expand Up @@ -89,6 +90,8 @@ typedef __PTRDIFF_TYPE__ __ptrdiff_t;

#endif

typedef Conditional<sizeof(void*) == 8, u64, u32>::Type FlatPtr;

constexpr unsigned KB = 1024;
constexpr unsigned MB = KB * KB;
constexpr unsigned GB = KB * KB * KB;
Expand Down
4 changes: 2 additions & 2 deletions DevTools/Inspector/RemoteObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class RemoteObject {
RemoteObject* parent { nullptr };
NonnullOwnPtrVector<RemoteObject> children;

uintptr_t address { 0 };
uintptr_t parent_address { 0 };
FlatPtr address { 0 };
FlatPtr parent_address { 0 };
String class_name;
String name;

Expand Down
2 changes: 1 addition & 1 deletion DevTools/Inspector/RemoteObjectPropertyModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void RemoteObjectPropertyModel::update()
void RemoteObjectPropertyModel::set_data(const GUI::ModelIndex& index, const GUI::Variant& new_value)
{
auto& property = m_properties[index.row()];
uintptr_t address = m_object.address;
FlatPtr address = m_object.address;
RemoteProcess::the().set_property(address, property.name.to_string(), new_value.to_string());
property.value = new_value.to_string();
did_update();
Expand Down
10 changes: 5 additions & 5 deletions DevTools/Inspector/RemoteProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ void RemoteProcess::handle_get_all_objects_response(const JsonObject& response)
auto& object_array = objects.as_array();

NonnullOwnPtrVector<RemoteObject> remote_objects;
HashMap<uintptr_t, RemoteObject*> objects_by_address;
HashMap<FlatPtr, RemoteObject*> objects_by_address;

for (auto& value : object_array.values()) {
ASSERT(value.is_object());
auto& object = value.as_object();
auto remote_object = make<RemoteObject>();
remote_object->address = object.get("address").to_number<uintptr_t>();
remote_object->parent_address = object.get("parent").to_number<uintptr_t>();
remote_object->address = object.get("address").to_number<FlatPtr>();
remote_object->parent_address = object.get("parent").to_number<FlatPtr>();
remote_object->name = object.get("name").to_string();
remote_object->class_name = object.get("class_name").to_string();
remote_object->json = object;
Expand Down Expand Up @@ -105,15 +105,15 @@ void RemoteProcess::send_request(const JsonObject& request)
m_socket->write(serialized);
}

void RemoteProcess::set_inspected_object(uintptr_t address)
void RemoteProcess::set_inspected_object(FlatPtr address)
{
JsonObject request;
request.set("type", "SetInspectedObject");
request.set("address", address);
send_request(request);
}

void RemoteProcess::set_property(uintptr_t object, const StringView& name, const JsonValue& value)
void RemoteProcess::set_property(FlatPtr object, const StringView& name, const JsonValue& value)
{
JsonObject request;
request.set("type", "SetProperty");
Expand Down
4 changes: 2 additions & 2 deletions DevTools/Inspector/RemoteProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class RemoteProcess {
RemoteObjectGraphModel& object_graph_model() { return *m_object_graph_model; }
const NonnullOwnPtrVector<RemoteObject>& roots() const { return m_roots; }

void set_inspected_object(uintptr_t);
void set_inspected_object(FlatPtr);

void set_property(uintptr_t object, const StringView& name, const JsonValue& value);
void set_property(FlatPtr object, const StringView& name, const JsonValue& value);

Function<void()> on_update;

Expand Down
8 changes: 4 additions & 4 deletions DevTools/ProfileViewer/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void Profile::rebuild_tree()
return new_root;
};

HashTable<uintptr_t> live_allocations;
HashTable<FlatPtr> live_allocations;

for (auto& event : m_events) {
if (has_timestamp_filter_range()) {
Expand Down Expand Up @@ -207,10 +207,10 @@ OwnPtr<Profile> Profile::load_from_perfcore_file(const StringView& path)
event.type = perf_event.get("type").to_string();

if (event.type == "malloc") {
event.ptr = perf_event.get("ptr").to_number<uintptr_t>();
event.ptr = perf_event.get("ptr").to_number<FlatPtr>();
event.size = perf_event.get("size").to_number<size_t>();
} else if (event.type == "free") {
event.ptr = perf_event.get("ptr").to_number<uintptr_t>();
event.ptr = perf_event.get("ptr").to_number<FlatPtr>();
}

auto stack_array = perf_event.get("stack").as_array();
Expand Down Expand Up @@ -239,7 +239,7 @@ OwnPtr<Profile> Profile::load_from_perfcore_file(const StringView& path)
if (event.frames.size() < 2)
continue;

uintptr_t innermost_frame_address = event.frames.at(1).address;
FlatPtr innermost_frame_address = event.frames.at(1).address;
event.in_kernel = innermost_frame_address >= 0xc0000000;

events.append(move(event));
Expand Down
2 changes: 1 addition & 1 deletion DevTools/ProfileViewer/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Profile {
struct Event {
u64 timestamp { 0 };
String type;
uintptr_t ptr { 0 };
FlatPtr ptr { 0 };
size_t size { 0 };
bool in_kernel { false };
Vector<Frame> frames;
Expand Down
12 changes: 6 additions & 6 deletions Kernel/ACPI/ACPIStaticParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ namespace ACPI {
dbg() << "ACPI: Looking for RSDP in EBDA @ V " << (void*)rsdp_str << ", P " << (void*)p_rsdp_str;
#endif
if (!strncmp("RSD PTR ", rsdp_str, strlen("RSD PTR ")))
return PhysicalAddress((uintptr_t)p_rsdp_str);
return PhysicalAddress((FlatPtr)p_rsdp_str);
p_rsdp_str += 16;
}
return {};
Expand All @@ -262,7 +262,7 @@ namespace ACPI {
dbg() << "ACPI: Looking for RSDP in BIOS ROM area @ V " << (void*)rsdp_str << ", P " << (void*)p_rsdp_str;
#endif
if (!strncmp("RSD PTR ", rsdp_str, strlen("RSD PTR ")))
return PhysicalAddress((uintptr_t)p_rsdp_str);
return PhysicalAddress((FlatPtr)p_rsdp_str);
p_rsdp_str += 16;
}
return {};
Expand Down Expand Up @@ -320,8 +320,8 @@ namespace ACPI {
auto main_sdt_region = MM.allocate_kernel_region(xsdt.page_base(), PAGE_SIZE, "ACPI Static Parsing search_table_in_xsdt()", Region::Access::Read, false, true);
auto* xsdt_ptr = (volatile Structures::XSDT*)main_sdt_region->vaddr().offset(xsdt.offset_in_page().get()).as_ptr();
for (u32 i = 0; i < ((xsdt_ptr->h.length - sizeof(Structures::SDTHeader)) / sizeof(u64)); i++) {
if (match_table_signature(PhysicalAddress((uintptr_t)xsdt_ptr->table_ptrs[i]), signature))
return PhysicalAddress((uintptr_t)xsdt_ptr->table_ptrs[i]);
if (match_table_signature(PhysicalAddress((FlatPtr)xsdt_ptr->table_ptrs[i]), signature))
return PhysicalAddress((FlatPtr)xsdt_ptr->table_ptrs[i]);
}
return {};
}
Expand All @@ -347,8 +347,8 @@ namespace ACPI {
auto* rsdt_ptr = (volatile Structures::RSDT*)main_sdt_region->vaddr().offset(rsdt.offset_in_page().get()).as_ptr();

for (u32 i = 0; i < ((rsdt_ptr->h.length - sizeof(Structures::SDTHeader)) / sizeof(u32)); i++) {
if (match_table_signature(PhysicalAddress((uintptr_t)rsdt_ptr->table_ptrs[i]), signature))
return PhysicalAddress((uintptr_t)rsdt_ptr->table_ptrs[i]);
if (match_table_signature(PhysicalAddress((FlatPtr)rsdt_ptr->table_ptrs[i]), signature))
return PhysicalAddress((FlatPtr)rsdt_ptr->table_ptrs[i]);
}
return {};
}
Expand Down
8 changes: 4 additions & 4 deletions Kernel/ACPI/DMIDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void DMIDecoder::enumerate_smbios_tables()

size_t table_size = get_table_size(p_table);
p_table = p_table.offset(table_size);
v_table_ptr = (SMBIOS::TableHeader*)((uintptr_t)v_table_ptr + table_size);
v_table_ptr = (SMBIOS::TableHeader*)((FlatPtr)v_table_ptr + table_size);
#ifdef SMBIOS_DEBUG
dbg() << "DMIDecoder: Next table @ P 0x" << p_table.get();
#endif
Expand Down Expand Up @@ -221,7 +221,7 @@ PhysicalAddress DMIDecoder::find_entry64bit_point()
dbg() << "DMI Decoder: Looking for 64 bit Entry point @ V " << (void*)entry_str << " P " << (void*)tested_physical_ptr;
#endif
if (!strncmp("_SM3_", entry_str, strlen("_SM3_")))
return PhysicalAddress((uintptr_t)tested_physical_ptr);
return PhysicalAddress((FlatPtr)tested_physical_ptr);

tested_physical_ptr += 16;
}
Expand All @@ -239,7 +239,7 @@ PhysicalAddress DMIDecoder::find_entry32bit_point()
dbg() << "DMI Decoder: Looking for 32 bit Entry point @ V " << (void*)entry_str << " P " << (void*)tested_physical_ptr;
#endif
if (!strncmp("_SM_", entry_str, strlen("_SM_")))
return PhysicalAddress((uintptr_t)tested_physical_ptr);
return PhysicalAddress((FlatPtr)tested_physical_ptr);

tested_physical_ptr += 16;
}
Expand All @@ -264,7 +264,7 @@ u64 DMIDecoder::get_bios_characteristics()
auto* bios_info = (SMBIOS::BIOSInfo*)get_smbios_physical_table_by_type(0).as_ptr();
ASSERT(bios_info != nullptr);

klog() << "DMIDecoder: BIOS info @ " << PhysicalAddress((uintptr_t)bios_info);
klog() << "DMIDecoder: BIOS info @ " << PhysicalAddress((FlatPtr)bios_info);
return bios_info->bios_characteristics;
}

Expand Down
26 changes: 13 additions & 13 deletions Kernel/ACPI/MultiProcessorParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ void MultiProcessorParser::initialize()

MultiProcessorParser::MultiProcessorParser()
: m_floating_pointer(search_floating_pointer())
, m_operable((m_floating_pointer != (uintptr_t) nullptr))
, m_operable((m_floating_pointer != (FlatPtr) nullptr))
{
if (m_floating_pointer != (uintptr_t) nullptr) {
if (m_floating_pointer != (FlatPtr) nullptr) {
klog() << "MultiProcessor: Floating Pointer Structure @ " << PhysicalAddress(m_floating_pointer);
parse_floating_pointer_data();
parse_configuration_table();
Expand Down Expand Up @@ -89,7 +89,7 @@ void MultiProcessorParser::parse_configuration_table()
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Processor;
break;
case ((u8)MultiProcessor::ConfigurationTableEntryType::Bus):
m_bus_entries.append((uintptr_t)p_entry);
m_bus_entries.append((FlatPtr)p_entry);
entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Bus;
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::Bus;
break;
Expand All @@ -98,7 +98,7 @@ void MultiProcessorParser::parse_configuration_table()
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IOAPIC;
break;
case ((u8)MultiProcessor::ConfigurationTableEntryType::IO_Interrupt_Assignment):
m_io_interrupt_redirection_entries.append((uintptr_t)p_entry);
m_io_interrupt_redirection_entries.append((FlatPtr)p_entry);
entry = (MultiProcessor::EntryHeader*)(u32)entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IO_Interrupt_Assignment;
p_entry = (MultiProcessor::EntryHeader*)(u32)p_entry + (u8)MultiProcessor::ConfigurationTableEntryLength::IO_Interrupt_Assignment;
break;
Expand All @@ -124,20 +124,20 @@ void MultiProcessorParser::parse_configuration_table()
}
}

uintptr_t MultiProcessorParser::search_floating_pointer()
FlatPtr MultiProcessorParser::search_floating_pointer()
{
uintptr_t mp_floating_pointer = (uintptr_t) nullptr;
FlatPtr mp_floating_pointer = (FlatPtr) nullptr;
auto region = MM.allocate_kernel_region(PhysicalAddress(0), PAGE_SIZE, "MultiProcessor Parser Floating Pointer Structure Finding", Region::Access::Read);
u16 ebda_seg = (u16) * ((uint16_t*)((region->vaddr().get() & PAGE_MASK) + 0x40e));
klog() << "MultiProcessor: Probing EBDA, Segment 0x" << String::format("%x", ebda_seg);

mp_floating_pointer = search_floating_pointer_in_ebda(ebda_seg);
if (mp_floating_pointer != (uintptr_t) nullptr)
if (mp_floating_pointer != (FlatPtr) nullptr)
return mp_floating_pointer;
return search_floating_pointer_in_bios_area();
}

uintptr_t MultiProcessorParser::search_floating_pointer_in_ebda(u16 ebda_segment)
FlatPtr MultiProcessorParser::search_floating_pointer_in_ebda(u16 ebda_segment)
{
auto floating_pointer_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)(ebda_segment << 4))), PAGE_ROUND_UP(1024), "MultiProcessor Parser floating_pointer Finding #1", Region::Access::Read, false, true);
char* p_floating_pointer_str = (char*)(PhysicalAddress(ebda_segment << 4).as_ptr());
Expand All @@ -146,12 +146,12 @@ uintptr_t MultiProcessorParser::search_floating_pointer_in_ebda(u16 ebda_segment
dbg() << "MultiProcessor: Looking for floating pointer structure in EBDA @ V0x " << String::format("%x", floating_pointer_str) << ", P0x" << String::format("%x", p_floating_pointer_str);
#endif
if (!strncmp("_MP_", floating_pointer_str, strlen("_MP_")))
return (uintptr_t)p_floating_pointer_str;
return (FlatPtr)p_floating_pointer_str;
p_floating_pointer_str += 16;
}
return (uintptr_t) nullptr;
return (FlatPtr) nullptr;
}
uintptr_t MultiProcessorParser::search_floating_pointer_in_bios_area()
FlatPtr MultiProcessorParser::search_floating_pointer_in_bios_area()
{
auto floating_pointer_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)0xE0000)), PAGE_ROUND_UP(0xFFFFF - 0xE0000), "MultiProcessor Parser floating_pointer Finding #2", Region::Access::Read, false, true);
char* p_floating_pointer_str = (char*)(PhysicalAddress(0xE0000).as_ptr());
Expand All @@ -160,10 +160,10 @@ uintptr_t MultiProcessorParser::search_floating_pointer_in_bios_area()
dbg() << "MultiProcessor: Looking for floating pointer structure in BIOS area @ V0x " << String::format("%x", floating_pointer_str) << ", P0x" << String::format("%x", p_floating_pointer_str);
#endif
if (!strncmp("_MP_", floating_pointer_str, strlen("_MP_")))
return (uintptr_t)p_floating_pointer_str;
return (FlatPtr)p_floating_pointer_str;
p_floating_pointer_str += 16;
}
return (uintptr_t) nullptr;
return (FlatPtr) nullptr;
}

Vector<unsigned> MultiProcessorParser::get_pci_bus_ids()
Expand Down
16 changes: 8 additions & 8 deletions Kernel/ACPI/MultiProcessorParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,14 @@ class MultiProcessorParser {

Vector<unsigned> get_pci_bus_ids();

uintptr_t search_floating_pointer();
uintptr_t search_floating_pointer_in_ebda(u16 ebda_segment);
uintptr_t search_floating_pointer_in_bios_area();

uintptr_t m_floating_pointer;
uintptr_t m_configuration_table;
Vector<uintptr_t> m_io_interrupt_redirection_entries;
Vector<uintptr_t> m_bus_entries;
FlatPtr search_floating_pointer();
FlatPtr search_floating_pointer_in_ebda(u16 ebda_segment);
FlatPtr search_floating_pointer_in_bios_area();

FlatPtr m_floating_pointer;
FlatPtr m_configuration_table;
Vector<FlatPtr> m_io_interrupt_redirection_entries;
Vector<FlatPtr> m_bus_entries;
bool m_operable;

size_t m_configuration_table_length;
Expand Down
14 changes: 7 additions & 7 deletions Kernel/Arch/i386/CPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#define PAGE_SIZE 4096
#define GENERIC_INTERRUPT_HANDLERS_COUNT 128
#define PAGE_MASK ((uintptr_t)0xfffff000u)
#define PAGE_MASK ((FlatPtr)0xfffff000u)

namespace Kernel {

Expand Down Expand Up @@ -451,24 +451,24 @@ struct [[gnu::aligned(16)]] FPUState
u8 buffer[512];
};

inline constexpr uintptr_t page_base_of(uintptr_t address)
inline constexpr FlatPtr page_base_of(FlatPtr address)
{
return address & PAGE_MASK;
}

inline uintptr_t page_base_of(const void* address)
inline FlatPtr page_base_of(const void* address)
{
return page_base_of((uintptr_t)address);
return page_base_of((FlatPtr)address);
}

inline constexpr uintptr_t offset_in_page(uintptr_t address)
inline constexpr FlatPtr offset_in_page(FlatPtr address)
{
return address & (~PAGE_MASK);
}

inline uintptr_t offset_in_page(const void* address)
inline FlatPtr offset_in_page(const void* address)
{
return offset_in_page((uintptr_t)address);
return offset_in_page((FlatPtr)address);
}

u32 read_cr3();
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Heap/kmalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void kfree(void* ptr)
++g_kfree_call_count;

auto* a = (AllocationHeader*)((((u8*)ptr) - sizeof(AllocationHeader)));
uintptr_t start = ((uintptr_t)a - (uintptr_t)BASE_PHYSICAL) / CHUNK_SIZE;
FlatPtr start = ((FlatPtr)a - (FlatPtr)BASE_PHYSICAL) / CHUNK_SIZE;

for (size_t k = start; k < (start + a->allocation_size_in_chunks); ++k)
alloc_map[k / 8] &= ~(1 << (k % 8));
Expand Down
Loading

0 comments on commit b1058b3

Please sign in to comment.