diff --git a/Kernel/ACPI/ACPIStaticParser.cpp b/Kernel/ACPI/ACPIStaticParser.cpp index 064fcaa8f9a..c9b25dfed37 100644 --- a/Kernel/ACPI/ACPIStaticParser.cpp +++ b/Kernel/ACPI/ACPIStaticParser.cpp @@ -63,8 +63,8 @@ ACPI_RAW::SDTHeader* ACPIStaticParser::find_table(const char* sig) dbgprintf("ACPI: Calling Find Table method!\n"); #endif for (auto* physical_sdt_ptr : m_main_sdt->get_sdt_pointers()) { - auto region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((uintptr_t)physical_sdt_ptr)), (PAGE_SIZE * 2), "ACPI Static Parser Tables Finding", Region::Access::Read); - ACPI_RAW::SDTHeader* sdt = (ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page((uintptr_t)physical_sdt_ptr)).as_ptr(); + auto region = MM.allocate_kernel_region(PhysicalAddress(page_base_of(physical_sdt_ptr)), (PAGE_SIZE * 2), "ACPI Static Parser Tables Finding", Region::Access::Read); + auto* sdt = (const ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page(physical_sdt_ptr)).as_ptr(); #ifdef ACPI_DEBUG dbgprintf("ACPI: Examining Table @ P 0x%x\n", physical_sdt_ptr); #endif @@ -85,20 +85,20 @@ void ACPIStaticParser::init_fadt() ASSERT(find_table("FACP") != nullptr); auto* fadt_ptr = find_table("FACP"); - auto checkup_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((uintptr_t)(fadt_ptr))), (PAGE_SIZE * 2), "ACPI Static Parser", Region::Access::Read); + auto checkup_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((fadt_ptr))), (PAGE_SIZE * 2), "ACPI Static Parser", Region::Access::Read); #ifdef ACPI_DEBUG dbgprintf("ACPI: Checking FADT Length to choose the correct mapping size\n"); #endif - ACPI_RAW::SDTHeader* sdt = (ACPI_RAW::SDTHeader*)checkup_region->vaddr().offset(offset_in_page((uintptr_t)(fadt_ptr))).as_ptr(); + auto* sdt = (const ACPI_RAW::SDTHeader*)checkup_region->vaddr().offset(offset_in_page((fadt_ptr))).as_ptr(); #ifdef ACPI_DEBUG dbgprintf("ACPI: FADT @ V 0x%x, P 0x%x\n", sdt, fadt_ptr); #endif u32 length = sdt->length; kprintf("ACPI: Fixed ACPI data, Revision %u\n", sdt->revision); - auto fadt_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((uintptr_t)(fadt_ptr))), PAGE_ROUND_UP(length) + PAGE_SIZE, "ACPI Static Parser", Region::Access::Read); - m_fadt = make(*(ACPI_RAW::FADT*)fadt_region->vaddr().offset(offset_in_page((uintptr_t)(fadt_ptr))).as_ptr()); + auto fadt_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((fadt_ptr))), PAGE_ROUND_UP(length) + PAGE_SIZE, "ACPI Static Parser", Region::Access::Read); + m_fadt = make(*(ACPI_RAW::FADT*)fadt_region->vaddr().offset(offset_in_page((fadt_ptr))).as_ptr()); #ifdef ACPI_DEBUG dbgprintf("ACPI: Finished to initialize Fixed ACPI data\n"); #endif @@ -143,8 +143,8 @@ size_t ACPIStaticParser::get_table_size(ACPI_RAW::SDTHeader& p_header) #ifdef ACPI_DEBUG dbgprintf("ACPI: Checking SDT Length\n"); #endif - auto region = MM.allocate_kernel_region(PhysicalAddress((uintptr_t)&p_header & PAGE_MASK), (PAGE_SIZE * 2), "ACPI get_table_size()", Region::Access::Read); - volatile auto* sdt = (ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page((uintptr_t)&p_header)).as_ptr(); + auto region = MM.allocate_kernel_region(PhysicalAddress((uintptr_t)&p_header).page_base(), (PAGE_SIZE * 2), "ACPI get_table_size()", Region::Access::Read); + auto* sdt = (volatile ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page(&p_header)).as_ptr(); return sdt->length; } @@ -154,8 +154,8 @@ u8 ACPIStaticParser::get_table_revision(ACPI_RAW::SDTHeader& p_header) #ifdef ACPI_DEBUG dbgprintf("ACPI: Checking SDT Revision\n"); #endif - auto region = MM.allocate_kernel_region(PhysicalAddress((uintptr_t)&p_header & PAGE_MASK), (PAGE_SIZE * 2), "ACPI get_table_revision()", Region::Access::Read); - volatile auto* sdt = (ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page((uintptr_t)&p_header)).as_ptr(); + auto region = MM.allocate_kernel_region(PhysicalAddress((uintptr_t)&p_header).page_base(), (PAGE_SIZE * 2), "ACPI get_table_revision()", Region::Access::Read); + auto* sdt = (volatile ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page(&p_header)).as_ptr(); return sdt->revision; } @@ -175,8 +175,8 @@ void ACPIStaticParser::initialize_main_system_description_table() revision = get_table_revision(*m_main_system_description_table); } - auto main_sdt_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((uintptr_t)m_main_system_description_table)), PAGE_ROUND_UP(length) + PAGE_SIZE, "ACPI Static Parser Initialization", Region::Access::Read, false, true); - volatile auto* sdt = (ACPI_RAW::SDTHeader*)main_sdt_region->vaddr().offset(offset_in_page((uintptr_t)m_main_system_description_table)).as_ptr(); + auto main_sdt_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of(m_main_system_description_table)), PAGE_ROUND_UP(length) + PAGE_SIZE, "ACPI Static Parser Initialization", Region::Access::Read, false, true); + auto* sdt = (volatile ACPI_RAW::SDTHeader*)main_sdt_region->vaddr().offset(offset_in_page(m_main_system_description_table)).as_ptr(); kprintf("ACPI: Main Description Table valid? 0x%x\n", validate_acpi_table(const_cast(*sdt), length)); Vector sdt_pointers; @@ -236,8 +236,8 @@ void ACPIStaticParser::locate_all_aml_tables() kprintf("ACPI: Searching for AML Tables\n"); m_aml_tables_ptrs.append(m_fadt->get_dsdt()); for (auto* sdt_ptr : m_main_sdt->get_sdt_pointers()) { - auto region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((uintptr_t)sdt_ptr)), (PAGE_SIZE * 2), "ACPI Static Parser AML Tables Finding", Region::Access::Read); - auto* sdt = (ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page((uintptr_t)sdt_ptr)).as_ptr(); + auto region = MM.allocate_kernel_region(PhysicalAddress(page_base_of(sdt_ptr)), (PAGE_SIZE * 2), "ACPI Static Parser AML Tables Finding", Region::Access::Read); + auto* sdt = (ACPI_RAW::SDTHeader*)region->vaddr().offset(offset_in_page(sdt_ptr)).as_ptr(); #ifdef ACPI_DEBUG dbgprintf("ACPI: Examining Table @ P 0x%x\n", sdt_ptr); #endif @@ -267,7 +267,7 @@ ACPIStaticParser::ACPIStaticParser() ACPI_RAW::RSDPDescriptor20* ACPIStaticParser::search_rsdp() { - auto region = MM.allocate_kernel_region(PhysicalAddress(0), PAGE_SIZE, "ACPI Static Parser RSDP Finding", Region::Access::Read); + auto region = MM.allocate_kernel_region(PhysicalAddress((uintptr_t)0), PAGE_SIZE, "ACPI Static Parser RSDP Finding", Region::Access::Read); u16 ebda_seg = (u16) * ((uint16_t*)((region->vaddr().get() & PAGE_MASK) + 0x40e)); kprintf("ACPI: Probing EBDA, Segment 0x%x\n", ebda_seg); diff --git a/Kernel/Arch/i386/CPU.h b/Kernel/Arch/i386/CPU.h index 2d52d5ac647..11fb73881c0 100644 --- a/Kernel/Arch/i386/CPU.h +++ b/Kernel/Arch/i386/CPU.h @@ -457,11 +457,21 @@ inline constexpr uintptr_t page_base_of(uintptr_t address) return address & PAGE_MASK; } +inline uintptr_t page_base_of(const void* address) +{ + return page_base_of((uintptr_t)address); +} + inline constexpr uintptr_t offset_in_page(uintptr_t address) { return address & (~PAGE_MASK); } +inline uintptr_t offset_in_page(const void* address) +{ + return offset_in_page((uintptr_t)address); +} + class CPUID { public: CPUID(u32 function) { asm volatile("cpuid" diff --git a/Kernel/KSyms.cpp b/Kernel/KSyms.cpp index 1b167a8e72b..221545242c9 100644 --- a/Kernel/KSyms.cpp +++ b/Kernel/KSyms.cpp @@ -134,13 +134,13 @@ static void load_ksyms_from_data(const ByteBuffer& buffer) int recognized_symbol_count = 0; if (use_ksyms) { for (u32* stack_ptr = (u32*)ebp; - (current ? current->process().validate_read_from_kernel(VirtualAddress((uintptr_t)stack_ptr), sizeof(void*) * 2) : 1) && recognized_symbol_count < max_recognized_symbol_count; stack_ptr = (u32*)*stack_ptr) { + (current ? current->process().validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1) && recognized_symbol_count < max_recognized_symbol_count; stack_ptr = (u32*)*stack_ptr) { u32 retaddr = stack_ptr[1]; recognized_symbols[recognized_symbol_count++] = { retaddr, ksymbolicate(retaddr) }; } } else { for (u32* stack_ptr = (u32*)ebp; - (current ? current->process().validate_read_from_kernel(VirtualAddress((uintptr_t)stack_ptr), sizeof(void*) * 2) : 1); stack_ptr = (u32*)*stack_ptr) { + (current ? current->process().validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1); stack_ptr = (u32*)*stack_ptr) { u32 retaddr = stack_ptr[1]; dbgprintf("%x (next: %x)\n", retaddr, stack_ptr ? (u32*)*stack_ptr : 0); } diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index a800a78ceb5..e60b7197277 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -274,7 +274,7 @@ int Process::sys$set_mmap_name(const Syscall::SC_set_mmap_name_params* user_para if (name.is_null()) return -EFAULT; - auto* region = region_from_range({ VirtualAddress((uintptr_t)params.addr), params.size }); + auto* region = region_from_range({ VirtualAddress(params.addr), params.size }); if (!region) return -EINVAL; if (!region->is_mmap()) @@ -390,11 +390,11 @@ void* Process::sys$mmap(const Syscall::SC_mmap_params* user_params) if (map_purgeable) { auto vmobject = PurgeableVMObject::create_with_size(size); - region = allocate_region_with_vmobject(VirtualAddress((uintptr_t)addr), size, vmobject, 0, !name.is_null() ? name : "mmap (purgeable)", prot); + region = allocate_region_with_vmobject(VirtualAddress(addr), size, vmobject, 0, !name.is_null() ? name : "mmap (purgeable)", prot); if (!region && (!map_fixed && addr != 0)) region = allocate_region_with_vmobject({}, size, vmobject, 0, !name.is_null() ? name : "mmap (purgeable)", prot); } else if (map_anonymous) { - region = allocate_region(VirtualAddress((uintptr_t)addr), size, !name.is_null() ? name : "mmap", prot, false); + region = allocate_region(VirtualAddress(addr), size, !name.is_null() ? name : "mmap", prot, false); if (!region && (!map_fixed && addr != 0)) region = allocate_region({}, size, !name.is_null() ? name : "mmap", prot, false); } else { @@ -418,7 +418,7 @@ void* Process::sys$mmap(const Syscall::SC_mmap_params* user_params) if (!validate_inode_mmap_prot(*this, prot, *description->inode())) return (void*)-EACCES; } - auto region_or_error = description->mmap(*this, VirtualAddress((uintptr_t)addr), static_cast(offset), size, prot); + auto region_or_error = description->mmap(*this, VirtualAddress(addr), static_cast(offset), size, prot); if (region_or_error.is_error()) { // Fail if MAP_FIXED or address is 0, retry otherwise if (map_fixed || addr == 0) @@ -445,7 +445,7 @@ void* Process::sys$mmap(const Syscall::SC_mmap_params* user_params) int Process::sys$munmap(void* addr, size_t size) { REQUIRE_PROMISE(stdio); - Range range_to_unmap { VirtualAddress((uintptr_t)addr), size }; + Range range_to_unmap { VirtualAddress(addr), size }; if (auto* whole_region = region_from_range(range_to_unmap)) { if (!whole_region->is_mmap()) return -EPERM; @@ -482,7 +482,7 @@ int Process::sys$munmap(void* addr, size_t size) int Process::sys$mprotect(void* addr, size_t size, int prot) { REQUIRE_PROMISE(stdio); - Range range_to_mprotect = { VirtualAddress((uintptr_t)addr), size }; + Range range_to_mprotect = { VirtualAddress(addr), size }; if (auto* whole_region = region_from_range(range_to_mprotect)) { if (!whole_region->is_mmap()) @@ -545,7 +545,7 @@ int Process::sys$mprotect(void* addr, size_t size, int prot) int Process::sys$madvise(void* address, size_t size, int advice) { REQUIRE_PROMISE(stdio); - auto* region = region_from_range({ VirtualAddress((uintptr_t)address), size }); + auto* region = region_from_range({ VirtualAddress(address), size }); if (!region) return -EINVAL; if (!region->is_mmap()) diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index f2cf0e9588e..838fa78d78d 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -597,8 +597,8 @@ void Thread::set_default_signal_dispositions() { // FIXME: Set up all the right default actions. See signal(7). memset(&m_signal_action_data, 0, sizeof(m_signal_action_data)); - m_signal_action_data[SIGCHLD].handler_or_sigaction = VirtualAddress((uintptr_t)SIG_IGN); - m_signal_action_data[SIGWINCH].handler_or_sigaction = VirtualAddress((uintptr_t)SIG_IGN); + m_signal_action_data[SIGCHLD].handler_or_sigaction = VirtualAddress(SIG_IGN); + m_signal_action_data[SIGWINCH].handler_or_sigaction = VirtualAddress(SIG_IGN); } void Thread::push_value_on_stack(u32 value) @@ -772,7 +772,7 @@ String Thread::backtrace_impl() const uintptr_t stack_ptr = start_frame; for (;;) { - if (!process.validate_read_from_kernel(VirtualAddress((uintptr_t)stack_ptr), sizeof(void*) * 2)) + if (!process.validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2)) break; uintptr_t retaddr; @@ -801,7 +801,7 @@ Vector Thread::raw_backtrace(uintptr_t ebp) const ProcessPagingScope paging_scope(process); Vector backtrace; backtrace.append(ebp); - for (uintptr_t* stack_ptr = (uintptr_t*)ebp; process.validate_read_from_kernel(VirtualAddress((uintptr_t)stack_ptr), sizeof(uintptr_t) * 2); stack_ptr = (uintptr_t*)*stack_ptr) { + for (uintptr_t* stack_ptr = (uintptr_t*)ebp; process.validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(uintptr_t) * 2); stack_ptr = (uintptr_t*)*stack_ptr) { uintptr_t retaddr = stack_ptr[1]; backtrace.append(retaddr); if (backtrace.size() == Profiling::max_stack_frame_count) @@ -818,7 +818,7 @@ void Thread::make_thread_specific_region(Badge) SmapDisabler disabler; auto* thread_specific_data = (ThreadSpecificData*)region->vaddr().offset(align_up_to(process().m_master_tls_size, thread_specific_region_alignment)).as_ptr(); auto* thread_local_storage = (u8*)((u8*)thread_specific_data) - align_up_to(process().m_master_tls_size, process().m_master_tls_alignment); - m_thread_specific_data = VirtualAddress((uintptr_t)thread_specific_data); + m_thread_specific_data = VirtualAddress(thread_specific_data); thread_specific_data->self = thread_specific_data; if (process().m_master_tls_size) memcpy(thread_local_storage, process().m_master_tls_region->vaddr().as_ptr(), process().m_master_tls_size); diff --git a/Kernel/VM/AnonymousVMObject.cpp b/Kernel/VM/AnonymousVMObject.cpp index e96d3dd0c36..44ad79cc904 100644 --- a/Kernel/VM/AnonymousVMObject.cpp +++ b/Kernel/VM/AnonymousVMObject.cpp @@ -45,7 +45,7 @@ AnonymousVMObject::AnonymousVMObject(size_t size) AnonymousVMObject::AnonymousVMObject(PhysicalAddress paddr, size_t size) : VMObject(size) { - ASSERT(paddr.page_base() == paddr.get()); + ASSERT(paddr.page_base() == paddr); for (size_t i = 0; i < page_count(); ++i) physical_pages()[i] = PhysicalPage::create(paddr.offset(i * PAGE_SIZE), false, false); } diff --git a/Kernel/VM/PhysicalAddress.h b/Kernel/VM/PhysicalAddress.h index 4f96b787921..4b51a4fd18c 100644 --- a/Kernel/VM/PhysicalAddress.h +++ b/Kernel/VM/PhysicalAddress.h @@ -47,7 +47,7 @@ public: u8* as_ptr() { return reinterpret_cast(m_address); } const u8* as_ptr() const { return reinterpret_cast(m_address); } - uintptr_t page_base() const { return m_address & 0xfffff000; } + PhysicalAddress page_base() const { return PhysicalAddress(m_address & 0xfffff000); } bool operator==(const PhysicalAddress& other) const { return m_address == other.m_address; } bool operator!=(const PhysicalAddress& other) const { return m_address != other.m_address; }