]> git.saurik.com Git - apple/xnu.git/blob - osfmk/arm/cswitch.s
xnu-6153.11.26.tar.gz
[apple/xnu.git] / osfmk / arm / cswitch.s
1 /*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <machine/asm.h>
29 #include <arm/proc_reg.h>
30 #include "assym.s"
31
32 /*
33 * save_vfp_registers
34 *
35 * Expects a pointer to the VFP save area in r3; saves the callee-saved registers to that save area.
36 * Clobbers r2 and r3.
37 */
38 .macro save_vfp_registers
39 #if __ARM_VFP__
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}
46 #else
47 fstmias r3!, {s16-s31}
48 #endif /* __ARM_VFP__ >= 3 */
49 #endif /* __ARM_VFP__ */
50 .endmacro
51
52 /*
53 * load_vfp_registers
54 *
55 * Expects a pointer to the VFP save area in r3; loads the callee-saved registers from that save area.
56 * Clobbers r2 and r3.
57 */
58 .macro load_vfp_registers
59 #if __ARM_VFP__
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}
64 #else
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__ */
70 .endmacro
71
72 /*
73 * void machine_load_context(thread_t thread)
74 *
75 * Load the context for the first thread to run on a
76 * cpu, and go.
77 */
78 .syntax unified
79 .text
80 .align 2
81 .globl EXT(machine_load_context)
82
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
88 orr r1, r1, r2 //
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
95 add r3, r3, SS_R4
96 ldmia r3!, {r4-r14} // Load thread status
97 bx lr // Return
98
99 /*
100 * typedef void (*thread_continue_t)(void *param, wait_result_t)
101 *
102 * void Call_continuation( thread_continue_t continuation,
103 * void *param,
104 * wait_result_t wresult,
105 * bool enable interrupts)
106 */
107 .text
108 .align 5
109 .globl EXT(Call_continuation)
110
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
115
116 mov r4,r0 // Load continuation
117 mov r5,r1 // continuation parameter
118 mov r6,r2 // Set wait result arg
119
120 teq r3, #0
121 beq 1f
122 mov r0, #1
123 bl _ml_set_interrupts_enabled
124 1:
125
126 mov r0,r5 // Set first parameter
127 mov r1,r6 // Set wait result arg
128 blx r4 // Branch to continuation
129
130 mrc p15, 0, r0, c13, c0, 4 // Read TPIDRPRW
131 LOAD_ADDR_PC(thread_terminate)
132 b . // Not reach
133
134
135 /*
136 * thread_t Switch_context(thread_t old,
137 * void (*cont)(void),
138 * thread_t new)
139 */
140 .text
141 .align 5
142 .globl EXT(Switch_context)
143
144 LEXT(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
147 #if __ARM_VFP__
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
154 add r3, r3, SS_R4
155 stmia r3!, {r4-r14} // Save general registers to pcb
156 switch_threads:
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
162 orr r6, r6, r5
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
166 load_reg:
167 add r3, r3, SS_R4
168 ldmia r3!, {r4-r14} // Restore new thread status
169 #if __ARM_VFP__
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__ */
173 bx lr // Return
174
175 /*
176 * thread_t Shutdown_context(void (*doshutdown)(processor_t), processor_t processor)
177 *
178 */
179 .text
180 .align 5
181 .globl EXT(Shutdown_context)
182
183 LEXT(Shutdown_context)
184 mrc p15, 0, r9, c13, c0, 4 // Read TPIDRPRW
185 #if __ARM_VFP__
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
188 #endif
189 ldr r3, [r9, TH_KSTACKPTR] // Get kernel stack top
190 add r3, r3, SS_R4
191 stmia r3!, {r4-r14} // Save general registers to pcb
192 cpsid if // Disable FIQ IRQ
193
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)
197
198 /*
199 * thread_t Idle_context(void)
200 *
201 */
202 .text
203 .align 5
204 .globl EXT(Idle_context)
205
206 LEXT(Idle_context)
207
208 mrc p15, 0, r9, c13, c0, 4 // Read TPIDRPRW
209 #if __ARM_VFP__
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
212 #endif
213 ldr r3, [r9, TH_KSTACKPTR] // Get kernel stack top
214 add r3, r3, SS_R4
215 stmia r3!, {r4-r14} // Save general registers to pcb
216
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)
220
221 /*
222 * thread_t Idle_context(void)
223 *
224 */
225 .text
226 .align 5
227 .globl EXT(Idle_load_context)
228
229 LEXT(Idle_load_context)
230
231 mrc p15, 0, r12, c13, c0, 4 // Read TPIDRPRW
232 ldr r3, [r12, TH_KSTACKPTR] // Get kernel stack top
233 add r3, r3, SS_R4
234 ldmia r3!, {r4-r14} // Restore new thread status
235 #if __ARM_VFP__
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
238 #endif
239 bx lr // Return
240
241 /*
242 * void vfp_save(struct arm_vfpsaved_state *vfp_ss)
243 */
244 .text
245 .align 2
246 .globl EXT(vfp_save)
247
248 LEXT(vfp_save)
249 #if __ARM_VFP__
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}
261 #else
262 fstmias r0!, {s0-s31} // Save vfp registers
263 #endif
264 #endif /* __ARM_VFP__ */
265 bx lr // Return
266
267 /*
268 * void vfp_load(struct arm_vfpsaved_state *vfp_ss)
269 *
270 * Loads the state in vfp_ss into the VFP registers.
271 */
272 .text
273 .align 2
274 .globl EXT(vfp_load)
275 LEXT(vfp_load)
276 #if __ARM_VFP__
277 /* r0: vfp_ss, r1: unused, r2: unused, r3: unused */
278 mov r1, r0
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}
288 #else
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__ */
294 bx lr // Return
295
296 #include "globals_asm.h"
297
298 LOAD_ADDR_GEN_DEF(thread_terminate)
299 LOAD_ADDR_GEN_DEF(cpu_doshutdown)
300 LOAD_ADDR_GEN_DEF(cpu_idle)
301
302 /* vim: set ts=4: */
303