From 248106d972aa63d06485044f67e818bc833bd8fa Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Mon, 12 Nov 2018 23:46:27 -0500 Subject: [PATCH 1/4] Skyeye: unstub cp15 virtual to physical address --- src/core/arm/skyeye_common/armstate.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/arm/skyeye_common/armstate.cpp b/src/core/arm/skyeye_common/armstate.cpp index a28f5a87f..a6920c89b 100644 --- a/src/core/arm/skyeye_common/armstate.cpp +++ b/src/core/arm/skyeye_common/armstate.cpp @@ -468,8 +468,7 @@ void ARMul_State::WriteCP15Register(u32 value, u32 crn, u32 opcode_1, u32 crm, u if (crm == 0 && opcode_2 == 4) { CP15[CP15_WAIT_FOR_INTERRUPT] = value; } else if (crm == 4 && opcode_2 == 0) { - // NOTE: Not entirely accurate. This should do permission checks. - CP15[CP15_PHYS_ADDRESS] = Memory::VirtualToPhysicalAddress(value); + LOG_ERROR(Core_ARM11, "Unimplemented virtual to physical address"); } else if (crm == 5) { if (opcode_2 == 0) CP15[CP15_INVALIDATE_INSTR_CACHE] = value; From 12daaeedf16f7377b7dc5eace42f39e040167679 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Mon, 12 Nov 2018 23:59:24 -0500 Subject: [PATCH 2/4] Memory: Use the fixed PAddr directly in RasterizerFlushVirtualRegion --- src/core/memory.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/core/memory.cpp b/src/core/memory.cpp index ed5a04c9a..8f082413d 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -426,7 +426,7 @@ void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode) { VAddr end = start + size; - auto CheckRegion = [&](VAddr region_start, VAddr region_end) { + auto CheckRegion = [&](VAddr region_start, VAddr region_end, PAddr paddr_region_start) { if (start >= region_end || end <= region_start) { // No overlap with region return; @@ -434,10 +434,7 @@ void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode) { VAddr overlap_start = std::max(start, region_start); VAddr overlap_end = std::min(end, region_end); - - auto maybe_paddr = TryVirtualToPhysicalAddress(overlap_start); - ASSERT(maybe_paddr); - PAddr physical_start = *maybe_paddr; + PAddr physical_start = paddr_region_start + (overlap_start - region_start); u32 overlap_size = overlap_end - overlap_start; auto* rasterizer = VideoCore::g_renderer->Rasterizer(); @@ -454,9 +451,9 @@ void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode) { } }; - CheckRegion(LINEAR_HEAP_VADDR, LINEAR_HEAP_VADDR_END); - CheckRegion(NEW_LINEAR_HEAP_VADDR, NEW_LINEAR_HEAP_VADDR_END); - CheckRegion(VRAM_VADDR, VRAM_VADDR_END); + CheckRegion(LINEAR_HEAP_VADDR, LINEAR_HEAP_VADDR_END, FCRAM_PADDR); + CheckRegion(NEW_LINEAR_HEAP_VADDR, NEW_LINEAR_HEAP_VADDR_END, FCRAM_PADDR); + CheckRegion(VRAM_VADDR, VRAM_VADDR_END, VRAM_PADDR); } u8 Read8(const VAddr addr) { From 1388a44c415abc94f9c981e2685548337940ae59 Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Tue, 13 Nov 2018 00:06:21 -0500 Subject: [PATCH 3/4] GSP: uses a dedicate VirtualToPhysical converter --- src/core/hle/service/gsp/gsp_gpu.cpp | 45 +++++++++++++++++++++------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/core/hle/service/gsp/gsp_gpu.cpp b/src/core/hle/service/gsp/gsp_gpu.cpp index 8ed79ebca..ae995ea86 100644 --- a/src/core/hle/service/gsp/gsp_gpu.cpp +++ b/src/core/hle/service/gsp/gsp_gpu.cpp @@ -56,6 +56,29 @@ constexpr u32 MaxGSPThreads = 4; /// Thread ids currently in use by the sessions connected to the GSPGPU service. static std::array used_thread_ids = {false, false, false, false}; +static PAddr VirtualToPhysicalAddress(VAddr addr) { + if (addr == 0) { + return 0; + } + + // Note: the region end check is inclusive because the game can pass in an address that + // represents an open right boundary + if (addr >= Memory::VRAM_VADDR && addr <= Memory::VRAM_VADDR_END) { + return addr - Memory::VRAM_VADDR + Memory::VRAM_PADDR; + } + if (addr >= Memory::LINEAR_HEAP_VADDR && addr <= Memory::LINEAR_HEAP_VADDR_END) { + return addr - Memory::LINEAR_HEAP_VADDR + Memory::FCRAM_PADDR; + } + if (addr >= Memory::NEW_LINEAR_HEAP_VADDR && addr <= Memory::NEW_LINEAR_HEAP_VADDR_END) { + return addr - Memory::NEW_LINEAR_HEAP_VADDR + Memory::FCRAM_PADDR; + } + + LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x{:08X}", addr); + // To help with debugging, set bit on address so that it's obviously invalid. + // TODO: find the correct way to handle this error + return addr | 0x80000000; +} + static u32 GetUnusedThreadId() { for (u32 id = 0; id < MaxGSPThreads; ++id) { if (!used_thread_ids[id]) @@ -258,8 +281,8 @@ void GSP_GPU::ReadHWRegs(Kernel::HLERequestContext& ctx) { ResultCode SetBufferSwap(u32 screen_id, const FrameBufferInfo& info) { u32 base_address = 0x400000; - PAddr phys_address_left = Memory::VirtualToPhysicalAddress(info.address_left); - PAddr phys_address_right = Memory::VirtualToPhysicalAddress(info.address_right); + PAddr phys_address_left = VirtualToPhysicalAddress(info.address_left); + PAddr phys_address_right = VirtualToPhysicalAddress(info.address_right); if (info.active_fb == 0) { WriteSingleHWReg(base_address + 4 * static_cast(GPU_REG_INDEX( framebuffer_config[screen_id].address_left1)), @@ -495,7 +518,7 @@ static void ExecuteCommand(const Command& command, u32 thread_id) { } WriteGPURegister(static_cast(GPU_REG_INDEX(command_processor_config.address)), - Memory::VirtualToPhysicalAddress(params.address) >> 3); + VirtualToPhysicalAddress(params.address) >> 3); WriteGPURegister(static_cast(GPU_REG_INDEX(command_processor_config.size)), params.size); @@ -515,9 +538,9 @@ static void ExecuteCommand(const Command& command, u32 thread_id) { if (params.start1 != 0) { WriteGPURegister(static_cast(GPU_REG_INDEX(memory_fill_config[0].address_start)), - Memory::VirtualToPhysicalAddress(params.start1) >> 3); + VirtualToPhysicalAddress(params.start1) >> 3); WriteGPURegister(static_cast(GPU_REG_INDEX(memory_fill_config[0].address_end)), - Memory::VirtualToPhysicalAddress(params.end1) >> 3); + VirtualToPhysicalAddress(params.end1) >> 3); WriteGPURegister(static_cast(GPU_REG_INDEX(memory_fill_config[0].value_32bit)), params.value1); WriteGPURegister(static_cast(GPU_REG_INDEX(memory_fill_config[0].control)), @@ -526,9 +549,9 @@ static void ExecuteCommand(const Command& command, u32 thread_id) { if (params.start2 != 0) { WriteGPURegister(static_cast(GPU_REG_INDEX(memory_fill_config[1].address_start)), - Memory::VirtualToPhysicalAddress(params.start2) >> 3); + VirtualToPhysicalAddress(params.start2) >> 3); WriteGPURegister(static_cast(GPU_REG_INDEX(memory_fill_config[1].address_end)), - Memory::VirtualToPhysicalAddress(params.end2) >> 3); + VirtualToPhysicalAddress(params.end2) >> 3); WriteGPURegister(static_cast(GPU_REG_INDEX(memory_fill_config[1].value_32bit)), params.value2); WriteGPURegister(static_cast(GPU_REG_INDEX(memory_fill_config[1].control)), @@ -540,9 +563,9 @@ static void ExecuteCommand(const Command& command, u32 thread_id) { case CommandId::SET_DISPLAY_TRANSFER: { auto& params = command.display_transfer; WriteGPURegister(static_cast(GPU_REG_INDEX(display_transfer_config.input_address)), - Memory::VirtualToPhysicalAddress(params.in_buffer_address) >> 3); + VirtualToPhysicalAddress(params.in_buffer_address) >> 3); WriteGPURegister(static_cast(GPU_REG_INDEX(display_transfer_config.output_address)), - Memory::VirtualToPhysicalAddress(params.out_buffer_address) >> 3); + VirtualToPhysicalAddress(params.out_buffer_address) >> 3); WriteGPURegister(static_cast(GPU_REG_INDEX(display_transfer_config.input_size)), params.in_buffer_size); WriteGPURegister(static_cast(GPU_REG_INDEX(display_transfer_config.output_size)), @@ -556,9 +579,9 @@ static void ExecuteCommand(const Command& command, u32 thread_id) { case CommandId::SET_TEXTURE_COPY: { auto& params = command.texture_copy; WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.input_address), - Memory::VirtualToPhysicalAddress(params.in_buffer_address) >> 3); + VirtualToPhysicalAddress(params.in_buffer_address) >> 3); WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.output_address), - Memory::VirtualToPhysicalAddress(params.out_buffer_address) >> 3); + VirtualToPhysicalAddress(params.out_buffer_address) >> 3); WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.texture_copy.size), params.size); WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.texture_copy.input_size), From 9e8d149ca7511285a362a0a3188d85ba65cb5b8b Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Tue, 13 Nov 2018 00:19:16 -0500 Subject: [PATCH 4/4] Memory: remove unused VirtualToPhysical --- src/core/memory.cpp | 30 ------------------------------ src/core/memory.h | 15 --------------- 2 files changed, 45 deletions(-) diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 8f082413d..2594678e0 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -795,36 +795,6 @@ void WriteMMIO(MMIORegionPointer mmio_handler, VAddr addr, const u64 data) mmio_handler->Write64(addr, data); } -std::optional TryVirtualToPhysicalAddress(const VAddr addr) { - if (addr == 0) { - return 0; - } else if (addr >= VRAM_VADDR && addr < VRAM_VADDR_END) { - return addr - VRAM_VADDR + VRAM_PADDR; - } else if (addr >= LINEAR_HEAP_VADDR && addr < LINEAR_HEAP_VADDR_END) { - return addr - LINEAR_HEAP_VADDR + FCRAM_PADDR; - } else if (addr >= NEW_LINEAR_HEAP_VADDR && addr < NEW_LINEAR_HEAP_VADDR_END) { - return addr - NEW_LINEAR_HEAP_VADDR + FCRAM_PADDR; - } else if (addr >= DSP_RAM_VADDR && addr < DSP_RAM_VADDR_END) { - return addr - DSP_RAM_VADDR + DSP_RAM_PADDR; - } else if (addr >= IO_AREA_VADDR && addr < IO_AREA_VADDR_END) { - return addr - IO_AREA_VADDR + IO_AREA_PADDR; - } else if (addr >= N3DS_EXTRA_RAM_VADDR && addr < N3DS_EXTRA_RAM_VADDR_END) { - return addr - N3DS_EXTRA_RAM_VADDR + N3DS_EXTRA_RAM_PADDR; - } - - return {}; -} - -PAddr VirtualToPhysicalAddress(const VAddr addr) { - auto paddr = TryVirtualToPhysicalAddress(addr); - if (!paddr) { - LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x{:08X}", addr); - // To help with debugging, set bit on address so that it's obviously invalid. - return addr | 0x80000000; - } - return *paddr; -} - u32 GetFCRAMOffset(u8* pointer) { ASSERT(pointer >= fcram.data() && pointer < fcram.data() + fcram.size()); return pointer - fcram.data(); diff --git a/src/core/memory.h b/src/core/memory.h index 58e8a6a80..aaa44aaa1 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -6,7 +6,6 @@ #include #include -#include #include #include #include "common/common_types.h" @@ -214,20 +213,6 @@ u8* GetPointer(VAddr vaddr); std::string ReadCString(VAddr vaddr, std::size_t max_length); -/** - * Converts a virtual address inside a region with 1:1 mapping to physical memory to a physical - * address. This should be used by services to translate addresses for use by the hardware. - */ -std::optional TryVirtualToPhysicalAddress(VAddr addr); - -/** - * Converts a virtual address inside a region with 1:1 mapping to physical memory to a physical - * address. This should be used by services to translate addresses for use by the hardware. - * - * @deprecated Use TryVirtualToPhysicalAddress(), which reports failure. - */ -PAddr VirtualToPhysicalAddress(VAddr addr); - /** * Gets a pointer to the memory region beginning at the specified physical address. */