kern: fix use of plr vs plr_heap, fix close/unlock order in ArbitrateLock

This commit is contained in:
Michael Scire 2021-10-19 01:19:31 -07:00
parent 52c914afcc
commit 42b6c2dd95
4 changed files with 21 additions and 15 deletions

View file

@ -284,6 +284,8 @@ namespace ams::kern {
constexpr KProcessAddress GetProcessLocalRegionAddress() const { return m_plr_address; } constexpr KProcessAddress GetProcessLocalRegionAddress() const { return m_plr_address; }
constexpr void *GetProcessLocalRegionHeapAddress() const { return m_plr_heap_address; }
void AddCpuTime(s64 diff) { m_cpu_time += diff; } void AddCpuTime(s64 diff) { m_cpu_time += diff; }
s64 GetCpuTime() { return m_cpu_time; } s64 GetCpuTime() { return m_cpu_time; }

View file

@ -126,7 +126,7 @@ namespace ams::kern::arch::arm64 {
const bool is_aarch64 = (context->psr & 0x10) == 0; const bool is_aarch64 = (context->psr & 0x10) == 0;
if (is_aarch64) { if (is_aarch64) {
/* 64-bit. */ /* 64-bit. */
ams::svc::aarch64::ExceptionInfo *info = std::addressof(GetPointer<ams::svc::aarch64::ProcessLocalRegion>(cur_process.GetProcessLocalRegionAddress())->exception_info); ams::svc::aarch64::ExceptionInfo *info = std::addressof(static_cast<ams::svc::aarch64::ProcessLocalRegion *>(cur_process.GetProcessLocalRegionHeapAddress())->exception_info);
for (size_t i = 0; i < util::size(info->r); ++i) { for (size_t i = 0; i < util::size(info->r); ++i) {
info->r[i] = context->x[i]; info->r[i] = context->x[i];
@ -141,7 +141,7 @@ namespace ams::kern::arch::arm64 {
info->far = far; info->far = far;
} else { } else {
/* 32-bit. */ /* 32-bit. */
ams::svc::aarch32::ExceptionInfo *info = std::addressof(GetPointer<ams::svc::aarch32::ProcessLocalRegion>(cur_process.GetProcessLocalRegionAddress())->exception_info); ams::svc::aarch32::ExceptionInfo *info = std::addressof(static_cast<ams::svc::aarch32::ProcessLocalRegion *>(cur_process.GetProcessLocalRegionHeapAddress())->exception_info);
for (size_t i = 0; i < util::size(info->r); ++i) { for (size_t i = 0; i < util::size(info->r); ++i) {
info->r[i] = context->x[i]; info->r[i] = context->x[i];
@ -201,17 +201,17 @@ namespace ams::kern::arch::arm64 {
context->pc = GetInteger(cur_process.GetEntryPoint()); context->pc = GetInteger(cur_process.GetEntryPoint());
context->x[0] = type; context->x[0] = type;
if (is_aarch64) { if (is_aarch64) {
context->x[1] = GetInteger(cur_process.GetProcessLocalRegionAddress() + AMS_OFFSETOF(ams::svc::aarch64::ProcessLocalRegion, exception_info)); context->x[1] = GetInteger(cur_process.GetProcessLocalRegionAddress() + AMS_OFFSETOF(ams::svc::aarch64::ProcessLocalRegion, exception_info));
auto *plr = GetPointer<ams::svc::aarch64::ProcessLocalRegion>(cur_process.GetProcessLocalRegionAddress()); const auto *plr = GetPointer<ams::svc::aarch64::ProcessLocalRegion>(cur_process.GetProcessLocalRegionAddress());
context->sp = util::AlignDown(reinterpret_cast<uintptr_t>(plr->data) + sizeof(plr->data), 0x10); context->sp = util::AlignDown(reinterpret_cast<uintptr_t>(plr->data) + sizeof(plr->data), 0x10);
context->psr = 0; context->psr = 0;
} else { } else {
context->x[1] = GetInteger(cur_process.GetProcessLocalRegionAddress() + AMS_OFFSETOF(ams::svc::aarch32::ProcessLocalRegion, exception_info)); context->x[1] = GetInteger(cur_process.GetProcessLocalRegionAddress() + AMS_OFFSETOF(ams::svc::aarch32::ProcessLocalRegion, exception_info));
auto *plr = GetPointer<ams::svc::aarch32::ProcessLocalRegion>(cur_process.GetProcessLocalRegionAddress()); const auto *plr = GetPointer<ams::svc::aarch32::ProcessLocalRegion>(cur_process.GetProcessLocalRegionAddress());
context->x[13] = util::AlignDown(reinterpret_cast<uintptr_t>(plr->data) + sizeof(plr->data), 0x10); context->x[13] = util::AlignDown(reinterpret_cast<uintptr_t>(plr->data) + sizeof(plr->data), 0x08);
context->psr = 0x10; context->psr = 0x10;
} }
/* Set exception SVC permissions. */ /* Set exception SVC permissions. */
@ -380,10 +380,10 @@ namespace ams::kern::arch::arm64 {
const bool is_aarch64 = (e_ctx->psr & 0x10) == 0; const bool is_aarch64 = (e_ctx->psr & 0x10) == 0;
if (is_aarch64) { if (is_aarch64) {
/* We're 64-bit. */ /* We're 64-bit. */
info.info64 = GetPointer<ams::svc::aarch64::ProcessLocalRegion>(cur_process.GetProcessLocalRegionAddress())->exception_info; info.info64 = static_cast<const ams::svc::aarch64::ProcessLocalRegion *>(cur_process.GetProcessLocalRegionHeapAddress())->exception_info;
} else { } else {
/* We're 32-bit. */ /* We're 32-bit. */
info.info32 = GetPointer<ams::svc::aarch32::ProcessLocalRegion>(cur_process.GetProcessLocalRegionAddress())->exception_info; info.info32 = static_cast<const ams::svc::aarch32::ProcessLocalRegion *>(cur_process.GetProcessLocalRegionHeapAddress())->exception_info;
} }
/* Try to leave the user exception. */ /* Try to leave the user exception. */

View file

@ -113,6 +113,7 @@ namespace ams::kern {
ThreadQueueImplForKConditionVariableWaitForAddress wait_queue; ThreadQueueImplForKConditionVariableWaitForAddress wait_queue;
/* Wait for the address. */ /* Wait for the address. */
KThread *owner_thread;
{ {
KScopedSchedulerLock sl; KScopedSchedulerLock sl;
@ -127,8 +128,8 @@ namespace ams::kern {
R_SUCCEED_IF(test_tag != (handle | ams::svc::HandleWaitMask)); R_SUCCEED_IF(test_tag != (handle | ams::svc::HandleWaitMask));
/* Get the lock owner thread. */ /* Get the lock owner thread. */
KScopedAutoObject owner_thread = GetCurrentProcess().GetHandleTable().GetObjectWithoutPseudoHandle<KThread>(handle); owner_thread = GetCurrentProcess().GetHandleTable().GetObjectWithoutPseudoHandle<KThread>(handle).ReleasePointerUnsafe();
R_UNLESS(owner_thread.IsNotNull(), svc::ResultInvalidHandle()); R_UNLESS(owner_thread != nullptr, svc::ResultInvalidHandle());
/* Update the lock. */ /* Update the lock. */
cur_thread->SetAddressKey(addr, value); cur_thread->SetAddressKey(addr, value);
@ -138,6 +139,9 @@ namespace ams::kern {
cur_thread->BeginWait(std::addressof(wait_queue)); cur_thread->BeginWait(std::addressof(wait_queue));
} }
/* Close our reference to the owner thread, now that the wait is over. */
owner_thread->Close();
/* Get the wait result. */ /* Get the wait result. */
return cur_thread->GetWaitResult(); return cur_thread->GetWaitResult();
} }

View file

@ -21,7 +21,7 @@ namespace ams::kern {
constexpr inline s32 TerminatingThreadPriority = ams::svc::SystemThreadPriorityHighest - 1; constexpr inline s32 TerminatingThreadPriority = ams::svc::SystemThreadPriorityHighest - 1;
constexpr bool IsKernelAddressKey(KProcessAddress key) { constexpr ALWAYS_INLINE bool IsKernelAddressKey(KProcessAddress key) {
const uintptr_t key_uptr = GetInteger(key); const uintptr_t key_uptr = GetInteger(key);
return KernelVirtualAddressSpaceBase <= key_uptr && key_uptr <= KernelVirtualAddressSpaceLast && (key_uptr & 1) == 0; return KernelVirtualAddressSpaceBase <= key_uptr && key_uptr <= KernelVirtualAddressSpaceLast && (key_uptr & 1) == 0;
} }