]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cswitch.s
xnu-792.13.8.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 .text
78 /*
79 * Context switch routines for i386.
80 */
81
82 Entry(Load_context)
83 movl S_ARG0,%ecx /* get thread */
84 movl TH_KERNEL_STACK(%ecx),%ecx /* get kernel stack */
85 lea KERNEL_STACK_SIZE-IKS_SIZE-IEL_SIZE(%ecx),%edx
86 /* point to stack top */
87 movl %ecx,%gs:CPU_ACTIVE_STACK /* store stack address */
88 movl %edx,%gs:CPU_KERNEL_STACK /* store stack top */
89
90 movl %edx,%esp
91 movl %edx,%ebp
92
93 xorl %eax,%eax /* return zero (no old thread) */
94 pushl %eax
95 call EXT(thread_continue)
96
97 /*
98 * This has to save registers only
99 * when there is no explicit continuation.
100 */
101
102 Entry(Switch_context)
103 popl %eax /* pop return PC */
104
105 /* Test for a continuation and skip all state saving if so... */
106 cmpl $0,4(%esp)
107 jne 5f
108 movl %gs:CPU_ACTIVE_STACK,%ecx /* get old kernel stack */
109 movl %ebx,KSS_EBX(%ecx) /* save registers */
110 movl %ebp,KSS_EBP(%ecx)
111 movl %edi,KSS_EDI(%ecx)
112 movl %esi,KSS_ESI(%ecx)
113 movl %eax,KSS_EIP(%ecx) /* save return PC */
114 movl %esp,KSS_ESP(%ecx) /* save SP */
115 5:
116 movl 0(%esp),%eax /* return old thread */
117 movl 8(%esp),%ebx /* get new thread */
118 movl %ebx,%gs:CPU_ACTIVE_THREAD /* new thread is active */
119 movl TH_KERNEL_STACK(%ebx),%ecx /* get its kernel stack */
120 lea KERNEL_STACK_SIZE-IKS_SIZE-IEL_SIZE(%ecx),%ebx
121 /* point to stack top */
122
123 movl %ecx,%gs:CPU_ACTIVE_STACK /* set current stack */
124 movl %ebx,%gs:CPU_KERNEL_STACK /* set stack top */
125
126
127 movl KSS_ESP(%ecx),%esp /* switch stacks */
128 movl KSS_ESI(%ecx),%esi /* restore registers */
129 movl KSS_EDI(%ecx),%edi
130 movl KSS_EBP(%ecx),%ebp
131 movl KSS_EBX(%ecx),%ebx
132 jmp *KSS_EIP(%ecx) /* return old thread */
133
134 Entry(Thread_continue)
135 pushl %eax /* push the thread argument */
136 xorl %ebp,%ebp /* zero frame pointer */
137 call *%ebx /* call real continuation */
138
139 /*
140 * thread_t Shutdown_context(thread_t thread,
141 * void (*routine)(processor_t),
142 * processor_t processor)
143 *
144 * saves the kernel context of the thread,
145 * switches to the interrupt stack,
146 * continues the thread (with thread_continue),
147 * then runs routine on the interrupt stack.
148 *
149 */
150 Entry(Shutdown_context)
151 movl %gs:CPU_ACTIVE_STACK,%ecx /* get old kernel stack */
152 movl %ebx,KSS_EBX(%ecx) /* save registers */
153 movl %ebp,KSS_EBP(%ecx)
154 movl %edi,KSS_EDI(%ecx)
155 movl %esi,KSS_ESI(%ecx)
156 popl KSS_EIP(%ecx) /* save return PC */
157 movl %esp,KSS_ESP(%ecx) /* save SP */
158
159 movl 0(%esp),%eax /* get old thread */
160 movl %ecx,TH_KERNEL_STACK(%eax) /* save old stack */
161 movl 4(%esp),%ebx /* get routine to run next */
162 movl 8(%esp),%esi /* get its argument */
163
164 movl %gs:CPU_INT_STACK_TOP,%esp /* switch to interrupt stack */
165
166 pushl %esi /* push argument */
167 call *%ebx /* call routine to run */
168 hlt /* (should never return) */