diff --git a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h new file mode 100644 index 000000000..f756d85c1 --- /dev/null +++ b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#define THREAD_STACK_PARAMETERS_SIZE 0x30 +#define THREAD_STACK_PARAMETERS_SVC_PERMISSION 0x00 +#define THREAD_STACK_PARAMETERS_CONTEXT 0x18 +#define THREAD_STACK_PARAMETERS_CUR_THREAD 0x20 +#define THREAD_STACK_PARAMETERS_DISABLE_COUNT 0x28 +#define THREAD_STACK_PARAMETERS_DPC_FLAGS 0x2A +#define THREAD_STACK_PARAMETERS_CURRENT_SVC_ID 0x2B +#define THREAD_STACK_PARAMETERS_IS_CALLING_SVC 0x2C +#define THREAD_STACK_PARAMETERS_IS_IN_EXCEPTION_HANDLER 0x2D +#define THREAD_STACK_PARAMETERS_IS_PINNED 0x2E \ No newline at end of file diff --git a/libraries/libmesosphere/include/mesosphere/kern_common.hpp b/libraries/libmesosphere/include/mesosphere/kern_common.hpp index 091d4ba68..7e4bd00cd 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_common.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_common.hpp @@ -17,6 +17,7 @@ #include #include #include +#include namespace ams::kern { diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp index 184d55f37..0220b75ff 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp @@ -82,17 +82,28 @@ namespace ams::kern { }; struct StackParameters { - alignas(0x10) u8 svc_permission[0x10]; + alignas(0x10) u8 svc_permission[0x18]; + KThreadContext *context; + KThread *cur_thread; + s16 disable_count; std::atomic dpc_flags; u8 current_svc_id; bool is_calling_svc; bool is_in_exception_handler; bool is_pinned; - s32 disable_count; - KThreadContext *context; - KThread *cur_thread; }; static_assert(alignof(StackParameters) == 0x10); + static_assert(sizeof(StackParameters) == THREAD_STACK_PARAMETERS_SIZE); + + static_assert(__builtin_offsetof(StackParameters, svc_permission) == THREAD_STACK_PARAMETERS_SVC_PERMISSION); + static_assert(__builtin_offsetof(StackParameters, context) == THREAD_STACK_PARAMETERS_CONTEXT); + static_assert(__builtin_offsetof(StackParameters, cur_thread) == THREAD_STACK_PARAMETERS_CUR_THREAD); + static_assert(__builtin_offsetof(StackParameters, disable_count) == THREAD_STACK_PARAMETERS_DISABLE_COUNT); + static_assert(__builtin_offsetof(StackParameters, dpc_flags) == THREAD_STACK_PARAMETERS_DPC_FLAGS); + static_assert(__builtin_offsetof(StackParameters, current_svc_id) == THREAD_STACK_PARAMETERS_CURRENT_SVC_ID); + static_assert(__builtin_offsetof(StackParameters, is_calling_svc) == THREAD_STACK_PARAMETERS_IS_CALLING_SVC); + static_assert(__builtin_offsetof(StackParameters, is_in_exception_handler) == THREAD_STACK_PARAMETERS_IS_IN_EXCEPTION_HANDLER); + static_assert(__builtin_offsetof(StackParameters, is_pinned) == THREAD_STACK_PARAMETERS_IS_PINNED); struct QueueEntry { private: @@ -251,7 +262,7 @@ namespace ams::kern { return *(reinterpret_cast(m_kernel_stack_top) - 1); } public: - ALWAYS_INLINE s32 GetDisableDispatchCount() const { + ALWAYS_INLINE s16 GetDisableDispatchCount() const { MESOSPHERE_ASSERT_THIS(); return this->GetStackParameters().disable_count; } diff --git a/libraries/libmesosphere/include/mesosphere/kern_select_assembly_offsets.h b/libraries/libmesosphere/include/mesosphere/kern_select_assembly_offsets.h new file mode 100644 index 000000000..30d6e2c07 --- /dev/null +++ b/libraries/libmesosphere/include/mesosphere/kern_select_assembly_offsets.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#ifdef ATMOSPHERE_ARCH_ARM64 + + #include + +#else + + #error "Unknown architecture for CPU" + +#endif diff --git a/libraries/libmesosphere/include/mesosphere/svc/kern_svc_prototypes.hpp b/libraries/libmesosphere/include/mesosphere/svc/kern_svc_prototypes.hpp index dddff3139..078b2cd2b 100644 --- a/libraries/libmesosphere/include/mesosphere/svc/kern_svc_prototypes.hpp +++ b/libraries/libmesosphere/include/mesosphere/svc/kern_svc_prototypes.hpp @@ -20,7 +20,7 @@ namespace ams::kern::svc { - static constexpr size_t NumSupervisorCalls = 0x80; + static constexpr size_t NumSupervisorCalls = 0xC0; #define AMS_KERN_SVC_DECLARE_ENUM_ID(ID, RETURN_TYPE, NAME, ...) \ SvcId_##NAME = ID, diff --git a/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_exception_asm.s b/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_exception_asm.s index c3e2a74d5..e3f72e410 100644 --- a/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_exception_asm.s +++ b/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_exception_asm.s @@ -13,6 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include /* ams::kern::svc::CallReturnFromException64(Result result) */ .section .text._ZN3ams4kern3svc25CallReturnFromException64Ev, "ax", %progbits @@ -62,7 +63,7 @@ _ZN3ams4kern3svc14RestoreContextEm: 0: /* We should handle DPC. */ /* Check the dpc flags. */ - ldrb w8, [sp, #(0x120 + 0x10)] + ldrb w8, [sp, #(0x120 + THREAD_STACK_PARAMETERS_DPC_FLAGS)] cbz w8, 1f /* We have DPC to do! */ @@ -82,7 +83,7 @@ _ZN3ams4kern3svc14RestoreContextEm: 1: /* We're done with DPC, and should return from the svc. */ /* Clear our in-SVC note. */ - strb wzr, [sp, #(0x120 + 0x12)] + strb wzr, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_CALLING_SVC)] /* Restore registers. */ ldp x30, x8, [sp, #(8 * 30)] diff --git a/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_handlers_asm.s b/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_handlers_asm.s index 3de6e3c88..eaffea9cd 100644 --- a/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_handlers_asm.s +++ b/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_handlers_asm.s @@ -14,6 +14,7 @@ * along with this program. If not, see . */ #include +#include /* ams::kern::arch::arm64::SvcHandler64() */ .section .text._ZN3ams4kern4arch5arm6412SvcHandler64Ev, "ax", %progbits @@ -32,7 +33,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev: mrs x9, elr_el1 mrs x10, spsr_el1 mrs x11, tpidr_el0 - ldr x18, [sp, #(0x120 + 0x28)] + ldr x18, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CUR_THREAD)] /* Save callee-saved registers. */ stp x19, x20, [sp, #(8 * 19)] @@ -59,7 +60,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev: /* Check the specific SVC permission bit for allowal. */ mov x9, sp add x9, x9, x8, lsr#3 - ldrb w9, [x9, #0x120] + ldrb w9, [x9, #(0x120 + THREAD_STACK_PARAMETERS_SVC_PERMISSION)] and x10, x8, #0x7 lsr x10, x9, x10 tst x10, #1 @@ -71,7 +72,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev: cbz w10, 1f /* It might not, so check the stack params to see if we must not allow the SVC. */ - ldrb w10, [sp, #(0x120 + 0x14)] + ldrb w10, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_PINNED)] cbz w10, 3f 1: /* We can call the SVC. */ @@ -81,8 +82,8 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev: /* Note that we're calling the SVC. */ mov w10, #1 - strb w10, [sp, #(0x120 + 0x12)] - strb w8, [sp, #(0x120 + 0x11)] + strb w10, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_CALLING_SVC)] + strb w8, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CURRENT_SVC_ID)] /* If we should, trace the svc entry. */ #if defined(MESOSPHERE_BUILD_FOR_TRACING) @@ -109,7 +110,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev: 2: /* We completed the SVC, and we should handle DPC. */ /* Check the dpc flags. */ - ldrb w8, [sp, #(0x120 + 0x10)] + ldrb w8, [sp, #(0x120 + THREAD_STACK_PARAMETERS_DPC_FLAGS)] cbz w8, 4f /* We have DPC to do! */ @@ -179,7 +180,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev: 4: /* Return from SVC. */ /* Clear our in-SVC note. */ - strb wzr, [sp, #(0x120 + 0x12)] + strb wzr, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_CALLING_SVC)] /* If we should, trace the svc exit. */ #if defined(MESOSPHERE_BUILD_FOR_TRACING) @@ -245,7 +246,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev: mrs x17, elr_el1 mrs x20, spsr_el1 mrs x19, tpidr_el0 - ldr x18, [sp, #(0x120 + 0x28)] + ldr x18, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CUR_THREAD)] stp x17, x20, [sp, #(8 * 32)] str x19, [sp, #(8 * 34)] @@ -268,7 +269,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev: /* Check the specific SVC permission bit for allowal. */ mov x20, sp add x20, x20, x16, lsr#3 - ldrb w20, [x20, #0x120] + ldrb w20, [x20, #(0x120 + THREAD_STACK_PARAMETERS_SVC_PERMISSION)] and x17, x16, #0x7 lsr x17, x20, x17 tst x17, #1 @@ -280,7 +281,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev: cbz w15, 1f /* It might not, so check the stack params to see if we must not allow the SVC. */ - ldrb w15, [sp, #(0x120 + 0x14)] + ldrb w15, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_PINNED)] cbz w15, 3f 1: /* We can call the SVC. */ @@ -290,8 +291,8 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev: /* Note that we're calling the SVC. */ mov w15, #1 - strb w15, [sp, #(0x120 + 0x12)] - strb w16, [sp, #(0x120 + 0x11)] + strb w15, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_CALLING_SVC)] + strb w16, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CURRENT_SVC_ID)] /* If we should, trace the svc entry. */ #if defined(MESOSPHERE_BUILD_FOR_TRACING) @@ -318,7 +319,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev: 2: /* We completed the SVC, and we should handle DPC. */ /* Check the dpc flags. */ - ldrb w16, [sp, #(0x120 + 0x10)] + ldrb w16, [sp, #(0x120 + THREAD_STACK_PARAMETERS_DPC_FLAGS)] cbz w16, 4f /* We have DPC to do! */ @@ -376,7 +377,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev: 4: /* Return from SVC. */ /* Clear our in-SVC note. */ - strb wzr, [sp, #(0x120 + 0x12)] + strb wzr, [sp, #(0x120 + THREAD_STACK_PARAMETERS_IS_CALLING_SVC)] /* If we should, trace the svc exit. */ #if defined(MESOSPHERE_BUILD_FOR_TRACING) diff --git a/mesosphere/kernel/source/arch/arm64/kern_exception_handlers_asm.s b/mesosphere/kernel/source/arch/arm64/kern_exception_handlers_asm.s index 6b5ab1675..f7645ee1e 100644 --- a/mesosphere/kernel/source/arch/arm64/kern_exception_handlers_asm.s +++ b/mesosphere/kernel/source/arch/arm64/kern_exception_handlers_asm.s @@ -13,6 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include /* ams::kern::arch::arm64::EL1IrqExceptionHandler() */ .section .text._ZN3ams4kern4arch5arm6422EL1IrqExceptionHandlerEv, "ax", %progbits @@ -99,7 +100,7 @@ _ZN3ams4kern4arch5arm6422EL0IrqExceptionHandlerEv: str x23, [sp, #(8 * 34)] /* Invoke KInterruptManager::HandleInterrupt(bool user_mode). */ - ldr x18, [sp, #(0x120 + 0x28)] + ldr x18, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CUR_THREAD)] mov x0, #1 bl _ZN3ams4kern4arch5arm6417KInterruptManager15HandleInterruptEb @@ -196,7 +197,7 @@ _ZN3ams4kern4arch5arm6430EL0SynchronousExceptionHandlerEv: str x23, [sp, #(8 * 34)] /* Call ams::kern::arch::arm64::HandleException(ams::kern::arch::arm64::KExceptionContext *) */ - ldr x18, [sp, #(0x120 + 0x28)] + ldr x18, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CUR_THREAD)] mov x0, sp bl _ZN3ams4kern4arch5arm6415HandleExceptionEPNS2_17KExceptionContextE @@ -440,7 +441,7 @@ _ZN3ams4kern4arch5arm6425FpuAccessExceptionHandlerEv: stp x20, x21, [sp, #(8 * 32)] /* Invoke the FPU context switch handler. */ - ldr x18, [sp, #(0x120 + 0x28)] + ldr x18, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CUR_THREAD)] bl _ZN3ams4kern4arch5arm6423FpuContextSwitchHandlerEv /* Restore registers that we saved. */ @@ -554,7 +555,7 @@ _ZN3ams4kern4arch5arm6421EL0SystemErrorHandlerEv: str x23, [sp, #(8 * 34)] /* Invoke ams::kern::arch::arm64::HandleException(ams::kern::arch::arm64::KExceptionContext *). */ - ldr x18, [sp, #(0x120 + 0x28)] + ldr x18, [sp, #(0x120 + THREAD_STACK_PARAMETERS_CUR_THREAD)] mov x0, sp bl _ZN3ams4kern4arch5arm6415HandleExceptionEPNS2_17KExceptionContextE diff --git a/mesosphere/kernel/source/arch/arm64/kern_k_scheduler_asm.s b/mesosphere/kernel/source/arch/arm64/kern_k_scheduler_asm.s index 64e27a656..5ac0af70c 100644 --- a/mesosphere/kernel/source/arch/arm64/kern_k_scheduler_asm.s +++ b/mesosphere/kernel/source/arch/arm64/kern_k_scheduler_asm.s @@ -13,6 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include #define SAVE_THREAD_CONTEXT(ctx, tmp0, tmp1, done_label) \ /* Save the callee save registers + SP and cpacr. */ \ @@ -148,11 +149,11 @@ _ZN3ams4kern10KScheduler12ScheduleImplEv: and x2, x2, #~(0x1000-1) /* Check if the thread has terminated. We can do this by checking the DPC flags for DpcFlag_Terminated. */ - ldurb w3, [x2, #-0x20] + ldurb w3, [x2, #-(THREAD_STACK_PARAMETERS_SIZE - THREAD_STACK_PARAMETERS_DPC_FLAGS)] tbnz w3, #1, 3f /* The current thread hasn't terminated, so we want to save its context. */ - ldur x2, [x2, #-0x10] + ldur x2, [x2, #-(THREAD_STACK_PARAMETERS_SIZE - THREAD_STACK_PARAMETERS_CONTEXT)] SAVE_THREAD_CONTEXT(x2, x4, x5, 2f) 2: /* We're done saving this thread's context, so we need to unlock it. */ diff --git a/mesosphere/kernel/source/arch/arm64/kern_k_thread_context_asm.s b/mesosphere/kernel/source/arch/arm64/kern_k_thread_context_asm.s index c50e28dac..0e914eb60 100644 --- a/mesosphere/kernel/source/arch/arm64/kern_k_thread_context_asm.s +++ b/mesosphere/kernel/source/arch/arm64/kern_k_thread_context_asm.s @@ -13,6 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include /* ams::kern::arch::arm64::UserModeThreadStarter() */ .section .text._ZN3ams4kern4arch5arm6421UserModeThreadStarterEv, "ax", %progbits @@ -26,7 +27,7 @@ _ZN3ams4kern4arch5arm6421UserModeThreadStarterEv: /* | KExceptionContext (size 0x120) | KThread::StackParameters (size 0x30) | */ /* Clear the disable count for this thread's stack parameters. */ - str wzr, [sp, #(0x120 + 0x18)] + strh wzr, [sp, #(0x120 + THREAD_STACK_PARAMETERS_DISABLE_COUNT)] /* Call ams::kern::arch::arm64::OnThreadStart() */ bl _ZN3ams4kern4arch5arm6413OnThreadStartEv @@ -78,7 +79,7 @@ _ZN3ams4kern4arch5arm6427SupervisorModeThreadStarterEv: ldp x0, x1, [sp], #0x10 /* Clear the disable count for this thread's stack parameters. */ - str wzr, [sp, #(0x18)] + strh wzr, [sp, #(THREAD_STACK_PARAMETERS_DISABLE_COUNT)] /* Mask I bit in DAIF */ msr daifclr, #2