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