]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cswitch.s
xnu-517.tar.gz
[apple/xnu.git] / osfmk / i386 / cswitch.s
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55
56 #include <cpus.h>
57 #include <platforms.h>
58
59 #include <i386/asm.h>
60 #include <i386/proc_reg.h>
61 #include <assym.s>
62
63 #if NCPUS > 1
64
65 #ifdef SYMMETRY
66 #include <sqt/asm_macros.h>
67 #endif
68
69 #if AT386
70 #include <i386/mp.h>
71 #endif /* AT386 */
72
73 #define CX(addr, reg) addr(,reg,4)
74
75 #else /* NCPUS == 1 */
76
77 #define CPU_NUMBER(reg)
78 #define CX(addr,reg) addr
79
80 #endif /* NCPUS == 1 */
81
82 /*
83 * Context switch routines for i386.
84 */
85
86 Entry(Load_context)
87 movl S_ARG0,%ecx /* get thread */
88 movl TH_KERNEL_STACK(%ecx),%ecx /* get kernel stack */
89 lea KERNEL_STACK_SIZE-IKS_SIZE-IEL_SIZE(%ecx),%edx
90 /* point to stack top */
91 CPU_NUMBER(%eax)
92 movl %ecx,CX(EXT(active_stacks),%eax) /* store stack address */
93 movl %edx,CX(EXT(kernel_stack),%eax) /* store stack top */
94
95 movl KSS_ESP(%ecx),%esp /* switch stacks */
96 movl KSS_ESI(%ecx),%esi /* restore registers */
97 movl KSS_EDI(%ecx),%edi
98 movl KSS_EBP(%ecx),%ebp
99 movl KSS_EBX(%ecx),%ebx
100 xorl %eax,%eax /* return zero (no old thread) */
101 jmp *KSS_EIP(%ecx) /* resume thread */
102
103 /*
104 * This really only has to save registers
105 * when there is no explicit continuation.
106 */
107
108 Entry(Switch_context)
109 CPU_NUMBER(%edx)
110 movl CX(EXT(active_stacks),%edx),%ecx /* get old kernel stack */
111
112 movl %ebx,KSS_EBX(%ecx) /* save registers */
113 movl %ebp,KSS_EBP(%ecx)
114 movl %edi,KSS_EDI(%ecx)
115 movl %esi,KSS_ESI(%ecx)
116 popl KSS_EIP(%ecx) /* save return PC */
117 movl %esp,KSS_ESP(%ecx) /* save SP */
118
119 movl 0(%esp),%eax /* return old thread */
120 movl 8(%esp),%esi /* get new thread */
121 movl TH_TOP_ACT(%esi),%ebx /* get new_thread->top_act */
122 movl $ CPD_ACTIVE_THREAD,%ecx
123 movl %ebx,%gs:(%ecx) /* new thread is active */
124 movl TH_KERNEL_STACK(%esi),%ecx /* get its kernel stack */
125 lea KERNEL_STACK_SIZE-IKS_SIZE-IEL_SIZE(%ecx),%ebx
126 /* point to stack top */
127
128 movl %ecx,CX(EXT(active_stacks),%edx) /* set current stack */
129 movl %ebx,CX(EXT(kernel_stack),%edx) /* set stack top */
130
131
132 movl $0,CX(EXT(active_kloaded),%edx)
133
134 movl KSS_ESP(%ecx),%esp /* switch stacks */
135 movl KSS_ESI(%ecx),%esi /* restore registers */
136 movl KSS_EDI(%ecx),%edi
137 movl KSS_EBP(%ecx),%ebp
138 movl KSS_EBX(%ecx),%ebx
139 jmp *KSS_EIP(%ecx) /* return old thread */
140
141 Entry(Thread_continue)
142 pushl %eax /* push the thread argument */
143 xorl %ebp,%ebp /* zero frame pointer */
144 call *%ebx /* call real continuation */
145
146 #if NCPUS > 1
147 /*
148 * void switch_to_shutdown_context(thread_t thread,
149 * void (*routine)(processor_t),
150 * processor_t processor)
151 *
152 * saves the kernel context of the thread,
153 * switches to the interrupt stack,
154 * continues the thread (with thread_continue),
155 * then runs routine on the interrupt stack.
156 *
157 * Assumes that the thread is a kernel thread (thus
158 * has no FPU state)
159 */
160 Entry(switch_to_shutdown_context)
161 CPU_NUMBER(%edx)
162 movl EXT(active_stacks)(,%edx,4),%ecx /* get old kernel stack */
163 movl %ebx,KSS_EBX(%ecx) /* save registers */
164 movl %ebp,KSS_EBP(%ecx)
165 movl %edi,KSS_EDI(%ecx)
166 movl %esi,KSS_ESI(%ecx)
167 popl KSS_EIP(%ecx) /* save return PC */
168 movl %esp,KSS_ESP(%ecx) /* save SP */
169
170 movl 0(%esp),%eax /* get old thread */
171 movl %ecx,TH_KERNEL_STACK(%eax) /* save old stack */
172 movl 4(%esp),%ebx /* get routine to run next */
173 movl 8(%esp),%esi /* get its argument */
174
175 movl CX(EXT(interrupt_stack),%edx),%ecx /* point to its intr stack */
176 lea INTSTACK_SIZE(%ecx),%esp /* switch to it (top) */
177
178 pushl %eax /* push thread */
179 call EXT(thread_dispatch) /* reschedule thread */
180 addl $4,%esp /* clean stack */
181
182 pushl %esi /* push argument */
183 call *%ebx /* call routine to run */
184 hlt /* (should never return) */
185
186 #endif /* NCPUS > 1 */
187
188 .text
189
190 .globl EXT(locore_end)
191 LEXT(locore_end)
192