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