2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990 Carnegie Mellon University
31 * All Rights Reserved.
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.
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.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
57 #include <platforms.h>
60 #include <i386/proc_reg.h>
66 #include <sqt/asm_macros.h>
70 #include <i386/AT386/mp/mp.h>
73 #define CX(addr, reg) addr(,reg,4)
75 #else /* NCPUS == 1 */
77 #define CPU_NUMBER(reg)
78 #define CX(addr,reg) addr
80 #endif /* NCPUS == 1 */
83 * Context switch routines for i386.
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 */
92 movl %ecx,CX(EXT(active_stacks),%eax) /* store stack address */
93 movl %edx,CX(EXT(kernel_stack),%eax) /* store stack top */
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 */
104 * This really only has to save registers
105 * when there is no explicit continuation.
108 Entry(Switch_context)
110 movl CX(EXT(active_stacks),%edx),%ecx /* get old kernel stack */
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 */
119 movl 0(%esp),%eax /* get old thread */
120 movl 4(%esp),%ebx /* get continuation */
121 movl %ebx,TH_CONTINUATION(%eax) /* save continuation */
122 movl %ecx,TH_KERNEL_STACK(%eax) /* save kernel stack */
124 movl 8(%esp),%esi /* get new thread */
125 movl $ CPD_ACTIVE_THREAD,%ecx
126 movl %esi,%gs:(%ecx) /* new thread is active */
127 movl TH_KERNEL_STACK(%esi),%ecx /* get its kernel stack */
128 lea KERNEL_STACK_SIZE-IKS_SIZE-IEL_SIZE(%ecx),%ebx
129 /* point to stack top */
131 movl %ecx,CX(EXT(active_stacks),%edx) /* set current stack */
132 movl %ebx,CX(EXT(kernel_stack),%edx) /* set stack top */
134 movl TH_TOP_ACT(%esi),%esi /* get new_thread->top_act */
135 cmpl $0,ACT_KLOADED(%esi) /* check kernel-loaded flag */
137 movl %esi,CX(EXT(active_kloaded),%edx)
140 movl $0,CX(EXT(active_kloaded),%edx)
142 movl KSS_ESP(%ecx),%esp /* switch stacks */
143 movl KSS_ESI(%ecx),%esi /* restore registers */
144 movl KSS_EDI(%ecx),%edi
145 movl KSS_EBP(%ecx),%ebp
146 movl KSS_EBX(%ecx),%ebx
147 jmp *KSS_EIP(%ecx) /* return old thread */
149 Entry(Thread_continue)
150 pushl %eax /* push the thread argument */
151 xorl %ebp,%ebp /* zero frame pointer */
152 call *%ebx /* call real continuation */
156 * void switch_to_shutdown_context(thread_t thread,
157 * void (*routine)(processor_t),
158 * processor_t processor)
160 * saves the kernel context of the thread,
161 * switches to the interrupt stack,
162 * continues the thread (with thread_continue),
163 * then runs routine on the interrupt stack.
165 * Assumes that the thread is a kernel thread (thus
168 Entry(switch_to_shutdown_context)
170 movl EXT(active_stacks)(,%edx,4),%ecx /* get old kernel stack */
171 movl %ebx,KSS_EBX(%ecx) /* save registers */
172 movl %ebp,KSS_EBP(%ecx)
173 movl %edi,KSS_EDI(%ecx)
174 movl %esi,KSS_ESI(%ecx)
175 popl KSS_EIP(%ecx) /* save return PC */
176 movl %esp,KSS_ESP(%ecx) /* save SP */
178 movl 0(%esp),%eax /* get old thread */
179 movl $0,TH_CONTINUATION(%eax) /* clear continuation */
180 movl %ecx,TH_KERNEL_STACK(%eax) /* save old stack */
181 movl 4(%esp),%ebx /* get routine to run next */
182 movl 8(%esp),%esi /* get its argument */
184 movl CX(EXT(interrupt_stack),%edx),%ecx /* point to its intr stack */
185 lea INTSTACK_SIZE(%ecx),%esp /* switch to it (top) */
187 pushl %eax /* push thread */
188 call EXT(thread_dispatch) /* reschedule thread */
189 addl $4,%esp /* clean stack */
191 pushl %esi /* push argument */
192 call *%ebx /* call routine to run */
193 hlt /* (should never return) */
195 #endif /* NCPUS > 1 */
199 .globl EXT(locore_end)