2 * Copyright (c) 2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <machine/asm.h>
29 #include <arm64/machine_machdep.h>
30 #include <arm64/machine_routines_asm.h>
31 #include <arm64/pac_asm.h>
32 #include <arm64/proc_reg.h>
36 * save_general_registers
38 * Saves variable registers to kernel PCB.
39 * arg0 - thread_kernel_state pointer
40 * arg1 - Scratch register
43 .macro save_general_registers
46 * A subroutine invocation must preserve the contents of the registers r19-r29
47 * and SP. We also save IP0 and IP1, as machine_idle uses IP0 for saving the LR.
49 stp x16, x17, [$0, SS64_KERNEL_X16]
50 stp x19, x20, [$0, SS64_KERNEL_X19]
51 stp x21, x22, [$0, SS64_KERNEL_X21]
52 stp x23, x24, [$0, SS64_KERNEL_X23]
53 stp x25, x26, [$0, SS64_KERNEL_X25]
54 stp x27, x28, [$0, SS64_KERNEL_X27]
55 stp fp, lr, [$0, SS64_KERNEL_FP]
56 str xzr, [$0, SS64_KERNEL_PC]
57 MOV32 w$1, PSR64_KERNEL_POISON
58 str w$1, [$0, SS64_KERNEL_CPSR]
60 stp x0, x1, [sp, #-16]!
61 stp x2, x3, [sp, #-16]!
62 stp x4, x5, [sp, #-16]!
65 * Arg0: The ARM context pointer
66 * Arg1: PC value to sign
67 * Arg2: CPSR value to sign
76 bl EXT(ml_sign_kernel_thread_state)
81 ldp fp, lr, [$0, SS64_KERNEL_FP]
82 #endif /* defined(HAS_APPLE_PAC) */
84 str x$1, [$0, SS64_KERNEL_SP]
88 * Registers d8-d15 (s8-s15) must be preserved by a callee across subroutine
89 * calls; the remaining registers (v0-v7, v16-v31) do not need to be preserved
90 * (or should be preserved by the caller).
92 str d8, [$0, NS64_KERNEL_D8]
93 str d9, [$0, NS64_KERNEL_D9]
94 str d10,[$0, NS64_KERNEL_D10]
95 str d11,[$0, NS64_KERNEL_D11]
96 str d12,[$0, NS64_KERNEL_D12]
97 str d13,[$0, NS64_KERNEL_D13]
98 str d14,[$0, NS64_KERNEL_D14]
99 str d15,[$0, NS64_KERNEL_D15]
102 str w$1, [$0, NS64_KERNEL_FPCR]
106 * load_general_registers
108 * Loads variable registers from kernel PCB.
109 * arg0 - thread_kernel_state pointer
110 * arg1 - Scratch register
112 .macro load_general_registers
118 AUTH_KERNEL_THREAD_STATE_IN_X0 x23, x24, x25, x26, x27
124 ldr w$1, [$0, NS64_KERNEL_FPCR]
126 CMSR FPCR, x19, x$1, 1
129 // Skip x16, x17 - already loaded + authed by AUTH_THREAD_STATE_IN_X0
130 ldp x19, x20, [$0, SS64_KERNEL_X19]
131 ldp x21, x22, [$0, SS64_KERNEL_X21]
132 ldp x23, x24, [$0, SS64_KERNEL_X23]
133 ldp x25, x26, [$0, SS64_KERNEL_X25]
134 ldp x27, x28, [$0, SS64_KERNEL_X27]
135 ldr fp, [$0, SS64_KERNEL_FP]
136 // Skip lr - already loaded + authed by AUTH_THREAD_STATE_IN_X0
137 ldr x$1, [$0, SS64_KERNEL_SP]
140 ldr d8, [$0, NS64_KERNEL_D8]
141 ldr d9, [$0, NS64_KERNEL_D9]
142 ldr d10,[$0, NS64_KERNEL_D10]
143 ldr d11,[$0, NS64_KERNEL_D11]
144 ldr d12,[$0, NS64_KERNEL_D12]
145 ldr d13,[$0, NS64_KERNEL_D13]
146 ldr d14,[$0, NS64_KERNEL_D14]
147 ldr d15,[$0, NS64_KERNEL_D15]
152 * set_thread_registers
154 * Updates thread registers during context switch
155 * arg0 - New thread pointer
156 * arg1 - Scratch register
157 * arg2 - Scratch register
159 .macro set_thread_registers
160 msr TPIDR_EL1, $0 // Write new thread pointer to TPIDR_EL1
161 ldr $1, [$0, ACT_CPUDATAP]
162 str $0, [$1, CPU_ACTIVE_THREAD]
163 ldr $1, [$0, TH_CTH_SELF] // Get cthread pointer
164 mrs $2, TPIDRRO_EL0 // Extract cpu number from TPIDRRO_EL0
165 and $2, $2, #(MACHDEP_CPUNUM_MASK)
166 orr $2, $1, $2 // Save new cthread/cpu to TPIDRRO_EL0
169 #if DEBUG || DEVELOPMENT
170 ldr $1, [$0, TH_THREAD_ID] // Save the bottom 32-bits of the thread ID into
171 msr CONTEXTIDR_EL1, $1 // CONTEXTIDR_EL1 (top 32-bits are RES0).
172 #endif /* DEBUG || DEVELOPMENT */
176 * set_process_dependent_keys_and_sync_context
178 * Updates process dependent keys and issues explicit context sync during context switch if necessary
179 * Per CPU Data rop_key is initialized in arm_init() for bootstrap processor
180 * and in cpu_data_init for slave processors
182 * thread - New thread pointer
183 * new_key - Scratch register: New Thread Key
184 * tmp_key - Scratch register: Current CPU Key
185 * cpudatap - Scratch register: Current CPU Data pointer
186 * wsync - Half-width scratch register: CPU sync required flag
188 * to save on ISBs, for ARMv8.5 we use the CPU_SYNC_ON_CSWITCH field, cached in wsync, for pre-ARMv8.5,
189 * we just use wsync to keep track of needing an ISB
191 .macro set_process_dependent_keys_and_sync_context thread, new_key, tmp_key, cpudatap, wsync
194 #if defined(__ARM_ARCH_8_5__) || defined(HAS_APPLE_PAC)
195 ldr \cpudatap, [\thread, ACT_CPUDATAP]
196 #endif /* defined(__ARM_ARCH_8_5__) || defined(HAS_APPLE_PAC) */
201 #if defined(HAS_APPLE_PAC)
202 ldr \new_key, [\thread, TH_ROP_PID]
203 ldr \tmp_key, [\cpudatap, CPU_ROP_KEY]
204 cmp \new_key, \tmp_key
206 str \new_key, [\cpudatap, CPU_ROP_KEY]
207 msr APIBKeyLo_EL1, \new_key
208 add \new_key, \new_key, #1
209 msr APIBKeyHi_EL1, \new_key
210 add \new_key, \new_key, #1
211 msr APDBKeyLo_EL1, \new_key
212 add \new_key, \new_key, #1
213 msr APDBKeyHi_EL1, \new_key
217 #if HAS_PAC_FAST_A_KEY_SWITCHING
218 IF_PAC_SLOW_A_KEY_SWITCHING Lskip_jop_keys_\@, \new_key
219 ldr \new_key, [\thread, TH_JOP_PID]
220 REPROGRAM_JOP_KEYS Lskip_jop_keys_\@, \new_key, \cpudatap, \tmp_key
223 #endif /* HAS_PAC_FAST_A_KEY_SWITCHING */
225 #endif /* defined(HAS_APPLE_PAC) */
234 * void machine_load_context(thread_t thread)
236 * Load the context for the first thread to run on a
241 .globl EXT(machine_load_context)
243 LEXT(machine_load_context)
244 set_thread_registers x0, x1, x2
245 ldr x1, [x0, TH_KSTACKPTR] // Get top of kernel stack
246 load_general_registers x1, 2
247 set_process_dependent_keys_and_sync_context x0, x1, x2, x3, w4
248 mov x0, #0 // Clear argument to thread_continue
252 * typedef void (*thread_continue_t)(void *param, wait_result_t)
254 * void Call_continuation( thread_continue_t continuation,
256 * wait_result_t wresult,
257 * bool enable interrupts)
261 .globl EXT(Call_continuation)
263 LEXT(Call_continuation)
264 mrs x4, TPIDR_EL1 // Get the current thread pointer
266 /* ARM64_TODO arm loads the kstack top instead of arg4. What should we use? */
267 ldr x5, [x4, TH_KSTACKPTR] // Get the top of the kernel stack
268 mov sp, x5 // Set stack pointer
269 mov fp, #0 // Clear the frame pointer
271 set_process_dependent_keys_and_sync_context x4, x5, x6, x7, w20
273 mov x20, x0 //continuation
274 mov x21, x1 //continuation parameter
275 mov x22, x2 //wait result
279 bl EXT(ml_set_interrupts_enabled)
282 mov x0, x21 // Set the first parameter
283 mov x1, x22 // Set the wait result arg
285 mov x21, THREAD_CONTINUE_T_DISC
286 blraa x20, x21 // Branch to the continuation
288 blr x20 // Branch to the continuation
290 mrs x0, TPIDR_EL1 // Get the current thread pointer
291 b EXT(thread_terminate) // Kill the thread
295 * thread_t Switch_context(thread_t old,
296 * void (*cont)(void),
301 .globl EXT(Switch_context)
304 cbnz x1, Lswitch_threads // Skip saving old state if blocking on continuation
305 ldr x3, [x0, TH_KSTACKPTR] // Get the old kernel stack top
306 save_general_registers x3, 4
308 set_thread_registers x2, x3, x4
309 ldr x3, [x2, TH_KSTACKPTR]
310 load_general_registers x3, 4
311 set_process_dependent_keys_and_sync_context x2, x3, x4, x5, w6
315 * thread_t Shutdown_context(void (*doshutdown)(processor_t), processor_t processor)
320 .globl EXT(Shutdown_context)
322 LEXT(Shutdown_context)
323 mrs x10, TPIDR_EL1 // Get thread pointer
324 ldr x11, [x10, TH_KSTACKPTR] // Get the top of the kernel stack
325 save_general_registers x11, 12
326 msr DAIFSet, #(DAIFSC_FIQF | DAIFSC_IRQF) // Disable interrupts
327 ldr x11, [x10, ACT_CPUDATAP] // Get current cpu
328 ldr x12, [x11, CPU_ISTACKPTR] // Switch to interrupt stack
330 b EXT(cpu_doshutdown)
333 * thread_t Idle_context(void)
338 .globl EXT(Idle_context)
341 mrs x0, TPIDR_EL1 // Get thread pointer
342 ldr x1, [x0, TH_KSTACKPTR] // Get the top of the kernel stack
343 save_general_registers x1, 2
344 ldr x1, [x0, ACT_CPUDATAP] // Get current cpu
345 ldr x2, [x1, CPU_ISTACKPTR] // Switch to interrupt stack
350 * thread_t Idle_context(void)
355 .globl EXT(Idle_load_context)
357 LEXT(Idle_load_context)
358 mrs x0, TPIDR_EL1 // Get thread pointer
359 ldr x1, [x0, TH_KSTACKPTR] // Get the top of the kernel stack
360 load_general_registers x1, 2
361 set_process_dependent_keys_and_sync_context x0, x1, x2, x3, w4
365 .globl EXT(machine_set_current_thread)
366 LEXT(machine_set_current_thread)
367 set_thread_registers x0, x1, x2