kern: write cpu tick differential to tls +0x108 on thread switch

This commit is contained in:
Michael Scire 2025-11-11 10:50:17 -07:00 committed by SciresM
parent 18bb1fdea0
commit e36051359c
3 changed files with 13 additions and 3 deletions

View File

@ -270,7 +270,13 @@ namespace ams::kern {
m_current_thread = next_thread;
/* Set the new Thread Local region. */
cpu::SwitchThreadLocalRegion(GetInteger(next_thread->GetThreadLocalRegionAddress()));
const auto tls_address = GetInteger(next_thread->GetThreadLocalRegionAddress());
cpu::SwitchThreadLocalRegion(tls_address);
/* Update the thread's cpu time differential in TLS, if relevant. */
if (tls_address != 0) {
static_cast<ams::svc::ThreadLocalRegion *>(next_thread->GetThreadLocalRegionHeapAddress())->thread_cpu_time = next_thread->GetCpuTime() - cur_tick;
}
}
void KScheduler::ClearPreviousThread(KThread *thread) {

View File

@ -25,8 +25,10 @@ namespace ams::svc::arch::arm {
u32 message_buffer[MessageBufferSize / sizeof(u32)];
volatile u16 disable_count;
volatile u16 interrupt_flag;
volatile u8 cache_maintenance_flag;
volatile s64 thread_cpu_time;
/* TODO: Should we bother adding the Nintendo aarch32 thread local context here? */
uintptr_t TODO[(0x200 - 0x104) / sizeof(uintptr_t)];
uintptr_t TODO[(0x200 - 0x110) / sizeof(uintptr_t)];
};
ALWAYS_INLINE ThreadLocalRegion *GetThreadLocalRegion() {

View File

@ -26,12 +26,14 @@ namespace ams::svc::arch::arm64 {
volatile u16 disable_count;
volatile u16 interrupt_flag;
volatile u8 cache_maintenance_flag;
volatile s64 thread_cpu_time;
/* TODO: How should we handle libnx vs Nintendo user thread local space? */
uintptr_t TODO[(0x200 - 0x108) / sizeof(uintptr_t)];
uintptr_t TODO[(0x200 - 0x110) / sizeof(uintptr_t)];
};
static_assert(__builtin_offsetof(ThreadLocalRegion, disable_count) == 0x100);
static_assert(__builtin_offsetof(ThreadLocalRegion, interrupt_flag) == 0x102);
static_assert(__builtin_offsetof(ThreadLocalRegion, cache_maintenance_flag) == 0x104);
static_assert(__builtin_offsetof(ThreadLocalRegion, thread_cpu_time) == 0x108);
ALWAYS_INLINE ThreadLocalRegion *GetThreadLocalRegion() {
ThreadLocalRegion *tlr;