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 <arm/proc_reg.h>
35 * Expects a pointer to the VFP save area in r3; saves the callee-saved registers to that save area.
38 .macro save_vfp_registers
40 fmrx r2, fpscr // Get the current FPSCR...
41 str r2, [r3, VSS_FPSCR] // ...and save it to the save area
42 add r3, r3, #64 // Only s16-s31 are callee-saved
43 #if (__ARM_VFP__ >= 3)
44 vstmia.64 r3!, {d8-d11}
45 vstmia.64 r3!, {d12-d15}
47 fstmias r3!, {s16-s31}
48 #endif /* __ARM_VFP__ >= 3 */
49 #endif /* __ARM_VFP__ */
55 * Expects a pointer to the VFP save area in r3; loads the callee-saved registers from that save area.
58 .macro load_vfp_registers
60 add r2, r3, #64 // Only s16-s31 are callee-saved
61 #if (__ARM_VFP__ >= 3)
62 vldmia.64 r2!, {d8-d11}
63 vldmia.64 r2!, {d12-d15}
65 fldmias r2!, {s16-s31}
66 #endif /* __ARM_VFP__ >= 3 */
67 ldr r3, [r3, VSS_FPSCR] // Get our saved FPSCR value...
68 fmxr fpscr, r3 // ...and restore it
69 #endif /* __ARM_VFP__ */
73 * void machine_load_context(thread_t thread)
75 * Load the context for the first thread to run on a
81 .globl EXT(machine_load_context)
83 LEXT(machine_load_context)
84 mcr p15, 0, r0, c13, c0, 4 // Write TPIDRPRW
85 ldr r1, [r0, TH_CTH_SELF]
86 mrc p15, 0, r2, c13, c0, 3 // Read TPIDRURO
87 and r2, r2, #3 // Extract cpu number
89 mcr p15, 0, r1, c13, c0, 3 // Write TPIDRURO
90 ldr r1, [r0, TH_CTH_DATA]
91 mcr p15, 0, r1, c13, c0, 2 // Write TPIDRURW
92 mov r7, #0 // Clear frame pointer
93 ldr r3, [r0, TH_KSTACKPTR] // Get kernel stack top
94 mov r0, #0 // no param
96 ldmia r3!, {r4-r14} // Load thread status
100 * typedef void (*thread_continue_t)(void *param, wait_result_t)
102 * void Call_continuation( thread_continue_t continuation,
104 * wait_result_t wresult,
105 * bool enable interrupts)
109 .globl EXT(Call_continuation)
111 LEXT(Call_continuation)
112 mrc p15, 0, r9, c13, c0, 4 // Read TPIDRPRW
113 ldr sp, [r9, TH_KSTACKPTR] // Set stack pointer
114 mov r7, #0 // Clear frame pointer
116 mov r4,r0 // Load continuation
117 mov r5,r1 // continuation parameter
118 mov r6,r2 // Set wait result arg
123 bl _ml_set_interrupts_enabled
126 mov r0,r5 // Set first parameter
127 mov r1,r6 // Set wait result arg
128 blx r4 // Branch to continuation
130 mrc p15, 0, r0, c13, c0, 4 // Read TPIDRPRW
131 LOAD_ADDR_PC(thread_terminate)
136 * thread_t Switch_context(thread_t old,
137 * void (*cont)(void),
142 .globl EXT(Switch_context)
145 teq r1, #0 // Test if blocking on continuaton
146 bne switch_threads // No need to save GPR/NEON state if we are
148 mov r1, r2 // r2 will be clobbered by the save, so preserve it
149 add r3, r0, ACT_KVFP // Get the kernel VFP save area for the old thread...
150 save_vfp_registers // ...and save our VFP state to it
151 mov r2, r1 // Restore r2 (the new thread pointer)
152 #endif /* __ARM_VFP__ */
153 ldr r3, [r0, TH_KSTACKPTR] // Get old kernel stack top
155 stmia r3!, {r4-r14} // Save general registers to pcb
157 ldr r3, [r2, TH_KSTACKPTR] // get kernel stack top
158 mcr p15, 0, r2, c13, c0, 4 // Write TPIDRPRW
159 ldr r6, [r2, TH_CTH_SELF]
160 mrc p15, 0, r5, c13, c0, 3 // Read TPIDRURO
161 and r5, r5, #3 // Extract cpu number
163 mcr p15, 0, r6, c13, c0, 3 // Write TPIDRURO
164 ldr r6, [r2, TH_CTH_DATA]
165 mcr p15, 0, r6, c13, c0, 2 // Write TPIDRURW
168 ldmia r3!, {r4-r14} // Restore new thread status
170 add r3, r2, ACT_KVFP // Get the kernel VFP save area for the new thread...
171 load_vfp_registers // ...and load the saved state
172 #endif /* __ARM_VFP__ */
176 * thread_t Shutdown_context(void (*doshutdown)(processor_t), processor_t processor)
181 .globl EXT(Shutdown_context)
183 LEXT(Shutdown_context)
184 mrc p15, 0, r9, c13, c0, 4 // Read TPIDRPRW
186 add r3, r9, ACT_KVFP // Get the kernel VFP save area for the current thread...
187 save_vfp_registers // ...and save our VFP state to it
189 ldr r3, [r9, TH_KSTACKPTR] // Get kernel stack top
191 stmia r3!, {r4-r14} // Save general registers to pcb
192 cpsid if // Disable FIQ IRQ
194 ldr r12, [r9, ACT_CPUDATAP] // Get current cpu
195 ldr sp, [r12, CPU_ISTACKPTR] // Switch to interrupt stack
196 LOAD_ADDR_PC(cpu_doshutdown)
199 * thread_t Idle_context(void)
204 .globl EXT(Idle_context)
208 mrc p15, 0, r9, c13, c0, 4 // Read TPIDRPRW
210 add r3, r9, ACT_KVFP // Get the kernel VFP save area for the current thread...
211 save_vfp_registers // ...and save our VFP state to it
213 ldr r3, [r9, TH_KSTACKPTR] // Get kernel stack top
215 stmia r3!, {r4-r14} // Save general registers to pcb
217 ldr r12, [r9, ACT_CPUDATAP] // Get current cpu
218 ldr sp, [r12, CPU_ISTACKPTR] // Switch to interrupt stack
219 LOAD_ADDR_PC(cpu_idle)
222 * thread_t Idle_context(void)
227 .globl EXT(Idle_load_context)
229 LEXT(Idle_load_context)
231 mrc p15, 0, r12, c13, c0, 4 // Read TPIDRPRW
232 ldr r3, [r12, TH_KSTACKPTR] // Get kernel stack top
234 ldmia r3!, {r4-r14} // Restore new thread status
236 add r3, r9, ACT_KVFP // Get the kernel VFP save area for the current thread...
237 load_vfp_registers // ...and load the saved state
242 * void vfp_save(struct arm_vfpsaved_state *vfp_ss)
250 fmrx r1, fpscr // Get the current FPSCR...
251 str r1, [r0, VSS_FPSCR] // ...and save it to the save area
252 #if (__ARM_VFP__ >= 3)
253 vstmia.64 r0!, {d0-d3} // Save vfp registers
254 vstmia.64 r0!, {d4-d7}
255 vstmia.64 r0!, {d8-d11}
256 vstmia.64 r0!, {d12-d15}
257 vstmia.64 r0!, {d16-d19}
258 vstmia.64 r0!, {d20-d23}
259 vstmia.64 r0!, {d24-d27}
260 vstmia.64 r0!, {d28-d31}
262 fstmias r0!, {s0-s31} // Save vfp registers
264 #endif /* __ARM_VFP__ */
268 * void vfp_load(struct arm_vfpsaved_state *vfp_ss)
270 * Loads the state in vfp_ss into the VFP registers.
277 /* r0: vfp_ss, r1: unused, r2: unused, r3: unused */
279 #if (__ARM_VFP__ >= 3)
280 vldmia.64 r0!, {d0-d3} // Restore vfp registers
281 vldmia.64 r0!, {d4-d7}
282 vldmia.64 r0!, {d8-d11}
283 vldmia.64 r0!, {d12-d15}
284 vldmia.64 r0!, {d16-d19}
285 vldmia.64 r0!, {d20-d23}
286 vldmia.64 r0!, {d24-d27}
287 vldmia.64 r0!, {d28-d31}
289 fldmias r0!, {s0-s31} // Restore vfp registers
290 #endif /* __ARM_VFP__ >= 3 */
291 ldr r1, [r1, VSS_FPSCR] // Get fpscr from the save state...
292 fmxr fpscr, r1 // ...and load it into the register
293 #endif /* __ARM_VFP__ */
296 #include "globals_asm.h"
298 LOAD_ADDR_GEN_DEF(thread_terminate)
299 LOAD_ADDR_GEN_DEF(cpu_doshutdown)
300 LOAD_ADDR_GEN_DEF(cpu_idle)