From 000876858d52d7e4fa8e21bc4407d43d548eff30 Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 10 May 2015 18:35:37 -0500 Subject: Core/Memory: Give every emulated thread it's own TLS area. The TLS area for thread T with id Ti is located at TLS_AREA_VADDR + (Ti - 1) * 0x200. This allows some games like Mario Kart 7 to continue further. --- src/core/arm/arm_interface.h | 3 ++- src/core/arm/dyncom/arm_dyncom.cpp | 5 ++++- src/core/arm/dyncom/arm_dyncom.h | 2 +- src/core/core.cpp | 4 ---- src/core/core.h | 2 ++ src/core/hle/kernel/session.h | 10 +++++++--- src/core/hle/kernel/thread.cpp | 10 +++++++++- src/core/hle/kernel/thread.h | 6 ++++++ 8 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/core/arm/arm_interface.h b/src/core/arm/arm_interface.h index 85ed2c698..976c339e8 100644 --- a/src/core/arm/arm_interface.h +++ b/src/core/arm/arm_interface.h @@ -99,8 +99,9 @@ public: * @param stack_top Pointer to the top of the stack * @param entry_point Entry point for execution * @param arg User argument for thread + * @param tls_address Address of the Thread Local Storage for the thread */ - virtual void ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg) = 0; + virtual void ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg, u32 tls_address) = 0; /** * Saves the current CPU context diff --git a/src/core/arm/dyncom/arm_dyncom.cpp b/src/core/arm/dyncom/arm_dyncom.cpp index 0072ae533..1de1d2612 100644 --- a/src/core/arm/dyncom/arm_dyncom.cpp +++ b/src/core/arm/dyncom/arm_dyncom.cpp @@ -90,13 +90,14 @@ void ARM_DynCom::ExecuteInstructions(int num_instructions) { AddTicks(ticks_executed); } -void ARM_DynCom::ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg) { +void ARM_DynCom::ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg, u32 tls_address) { memset(&context, 0, sizeof(Core::ThreadContext)); context.cpu_registers[0] = arg; context.pc = entry_point; context.sp = stack_top; context.cpsr = 0x1F; // Usermode + context.tls = tls_address; } void ARM_DynCom::SaveContext(Core::ThreadContext& ctx) { @@ -123,6 +124,8 @@ void ARM_DynCom::LoadContext(const Core::ThreadContext& ctx) { state->VFP[1] = ctx.fpscr; state->VFP[2] = ctx.fpexc; + + SetCP15Register(CP15_THREAD_URO, ctx.tls); } void ARM_DynCom::PrepareReschedule() { diff --git a/src/core/arm/dyncom/arm_dyncom.h b/src/core/arm/dyncom/arm_dyncom.h index 2488c879c..b3fd708f1 100644 --- a/src/core/arm/dyncom/arm_dyncom.h +++ b/src/core/arm/dyncom/arm_dyncom.h @@ -27,7 +27,7 @@ public: void AddTicks(u64 ticks) override; - void ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg) override; + void ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg, u32 tls_address) override; void SaveContext(Core::ThreadContext& ctx) override; void LoadContext(const Core::ThreadContext& ctx) override; diff --git a/src/core/core.cpp b/src/core/core.cpp index bb2ed7a92..b5c258230 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -61,10 +61,6 @@ int Init() { g_sys_core = new ARM_DynCom(USER32MODE); g_app_core = new ARM_DynCom(USER32MODE); - // TODO: Whenever TLS is implemented, this should contain - // the address of the 0x200-byte TLS - g_app_core->SetCP15Register(CP15_THREAD_URO, Memory::TLS_AREA_VADDR); - LOG_DEBUG(Core, "Initialized OK"); return 0; } diff --git a/src/core/core.h b/src/core/core.h index 278f0f1cc..215b5a49f 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -21,6 +21,8 @@ struct ThreadContext { u32 fpu_registers[32]; u32 fpscr; u32 fpexc; + + u32 tls; }; extern ARM_Interface* g_app_core; ///< ARM11 application core diff --git a/src/core/hle/kernel/session.h b/src/core/hle/kernel/session.h index 0fd18148a..8c3886ffd 100644 --- a/src/core/hle/kernel/session.h +++ b/src/core/hle/kernel/session.h @@ -5,6 +5,7 @@ #pragma once #include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/thread.h" #include "core/mem_map.h" namespace Kernel { @@ -12,12 +13,15 @@ namespace Kernel { static const int kCommandHeaderOffset = 0x80; ///< Offset into command buffer of header /** - * Returns a pointer to the command buffer in kernel memory + * Returns a pointer to the command buffer in the current thread's TLS + * TODO(Subv): This is not entirely correct, the command buffer should be copied from + * the thread's TLS to an intermediate buffer in kernel memory, and then copied again to + * the service handler process' memory. * @param offset Optional offset into command buffer * @return Pointer to command buffer */ -inline static u32* GetCommandBuffer(const int offset=0) { - return (u32*)Memory::GetPointer(Memory::TLS_AREA_VADDR + kCommandHeaderOffset + offset); +inline static u32* GetCommandBuffer(const int offset = 0) { + return (u32*)Memory::GetPointer(GetCurrentThread()->GetTLSAddress() + kCommandHeaderOffset + offset); } /** diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 0a3fd7cb1..61199c12a 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -402,9 +402,13 @@ ResultVal> Thread::Create(std::string name, VAddr entry_point, thread->name = std::move(name); thread->callback_handle = wakeup_callback_handle_table.Create(thread).MoveFrom(); + VAddr tls_address = Memory::TLS_AREA_VADDR + (thread->thread_id - 1) * 0x200; + + ASSERT_MSG(tls_address < Memory::TLS_AREA_VADDR_END, "Too many threads"); + // TODO(peachum): move to ScheduleThread() when scheduler is added so selected core is used // to initialize the context - Core::g_app_core->ResetContext(thread->context, stack_top, entry_point, arg); + Core::g_app_core->ResetContext(thread->context, stack_top, entry_point, arg, tls_address); ready_queue.push_back(thread->current_priority, thread.get()); thread->status = THREADSTATUS_READY; @@ -495,6 +499,10 @@ void Thread::SetWaitSynchronizationOutput(s32 output) { context.cpu_registers[1] = output; } +VAddr Thread::GetTLSAddress() const { + return context.tls; +} + //////////////////////////////////////////////////////////////////////////////////////////////////// void ThreadingInit() { diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 9958b16e6..17bb69f45 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -135,6 +135,12 @@ public: */ void Stop(); + /* + * Returns the Thread Local Storage address of the current thread + * @returns VAddr of the thread's TLS + */ + VAddr GetTLSAddress() const; + Core::ThreadContext context; u32 thread_id; -- cgit v1.2.3 From 115ad8e16a69c8823118f210654fc9ea70a03213 Mon Sep 17 00:00:00 2001 From: Subv Date: Sun, 10 May 2015 18:43:59 -0500 Subject: fixup! Set the TLS address in the scheduler --- src/core/arm/arm_interface.h | 3 +-- src/core/arm/dyncom/arm_dyncom.cpp | 5 +---- src/core/arm/dyncom/arm_dyncom.h | 2 +- src/core/core.h | 2 -- src/core/hle/kernel/thread.cpp | 7 +++++-- src/core/hle/kernel/thread.h | 2 ++ 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/core/arm/arm_interface.h b/src/core/arm/arm_interface.h index 976c339e8..85ed2c698 100644 --- a/src/core/arm/arm_interface.h +++ b/src/core/arm/arm_interface.h @@ -99,9 +99,8 @@ public: * @param stack_top Pointer to the top of the stack * @param entry_point Entry point for execution * @param arg User argument for thread - * @param tls_address Address of the Thread Local Storage for the thread */ - virtual void ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg, u32 tls_address) = 0; + virtual void ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg) = 0; /** * Saves the current CPU context diff --git a/src/core/arm/dyncom/arm_dyncom.cpp b/src/core/arm/dyncom/arm_dyncom.cpp index 1de1d2612..0072ae533 100644 --- a/src/core/arm/dyncom/arm_dyncom.cpp +++ b/src/core/arm/dyncom/arm_dyncom.cpp @@ -90,14 +90,13 @@ void ARM_DynCom::ExecuteInstructions(int num_instructions) { AddTicks(ticks_executed); } -void ARM_DynCom::ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg, u32 tls_address) { +void ARM_DynCom::ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg) { memset(&context, 0, sizeof(Core::ThreadContext)); context.cpu_registers[0] = arg; context.pc = entry_point; context.sp = stack_top; context.cpsr = 0x1F; // Usermode - context.tls = tls_address; } void ARM_DynCom::SaveContext(Core::ThreadContext& ctx) { @@ -124,8 +123,6 @@ void ARM_DynCom::LoadContext(const Core::ThreadContext& ctx) { state->VFP[1] = ctx.fpscr; state->VFP[2] = ctx.fpexc; - - SetCP15Register(CP15_THREAD_URO, ctx.tls); } void ARM_DynCom::PrepareReschedule() { diff --git a/src/core/arm/dyncom/arm_dyncom.h b/src/core/arm/dyncom/arm_dyncom.h index b3fd708f1..2488c879c 100644 --- a/src/core/arm/dyncom/arm_dyncom.h +++ b/src/core/arm/dyncom/arm_dyncom.h @@ -27,7 +27,7 @@ public: void AddTicks(u64 ticks) override; - void ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg, u32 tls_address) override; + void ResetContext(Core::ThreadContext& context, u32 stack_top, u32 entry_point, u32 arg) override; void SaveContext(Core::ThreadContext& ctx) override; void LoadContext(const Core::ThreadContext& ctx) override; diff --git a/src/core/core.h b/src/core/core.h index 215b5a49f..278f0f1cc 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -21,8 +21,6 @@ struct ThreadContext { u32 fpu_registers[32]; u32 fpscr; u32 fpexc; - - u32 tls; }; extern ARM_Interface* g_app_core; ///< ARM11 application core diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 61199c12a..5de8f9a73 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -197,6 +197,7 @@ static void SwitchContext(Thread* new_thread) { new_thread->current_priority = new_thread->nominal_priority; Core::g_app_core->LoadContext(new_thread->context); + Core::g_app_core->SetCP15Register(CP15_THREAD_URO, new_thread->GetTLSAddress()); } else { current_thread = nullptr; } @@ -406,9 +407,11 @@ ResultVal> Thread::Create(std::string name, VAddr entry_point, ASSERT_MSG(tls_address < Memory::TLS_AREA_VADDR_END, "Too many threads"); + thread->tls_address = tls_address; + // TODO(peachum): move to ScheduleThread() when scheduler is added so selected core is used // to initialize the context - Core::g_app_core->ResetContext(thread->context, stack_top, entry_point, arg, tls_address); + Core::g_app_core->ResetContext(thread->context, stack_top, entry_point, arg); ready_queue.push_back(thread->current_priority, thread.get()); thread->status = THREADSTATUS_READY; @@ -500,7 +503,7 @@ void Thread::SetWaitSynchronizationOutput(s32 output) { } VAddr Thread::GetTLSAddress() const { - return context.tls; + return tls_address; } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 17bb69f45..6891c8c2f 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -156,6 +156,8 @@ public: s32 processor_id; + VAddr tls_address; ///< Address of the Thread Local Storage of the thread + /// Mutexes currently held by this thread, which will be released when it exits. boost::container::flat_set> held_mutexes; -- cgit v1.2.3