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>
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 /* 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 */
128 movl %ecx,CX(EXT(active_stacks),%edx) /* set current stack */
129 movl %ebx,CX(EXT(kernel_stack),%edx) /* set stack top */
132 movl $0,CX(EXT(active_kloaded),%edx)
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 */
141 Entry(Thread_continue)
142 pushl %eax /* push the thread argument */
143 xorl %ebp,%ebp /* zero frame pointer */
144 call *%ebx /* call real continuation */
148 * void switch_to_shutdown_context(thread_t thread,
149 * void (*routine)(processor_t),
150 * processor_t processor)
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.
157 * Assumes that the thread is a kernel thread (thus
160 Entry(switch_to_shutdown_context)
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 */
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 */
175 movl CX(EXT(interrupt_stack),%edx),%ecx /* point to its intr stack */
176 lea INTSTACK_SIZE(%ecx),%esp /* switch to it (top) */
178 pushl %eax /* push thread */
179 call EXT(thread_dispatch) /* reschedule thread */
180 addl $4,%esp /* clean stack */
182 pushl %esi /* push argument */
183 call *%ebx /* call routine to run */
184 hlt /* (should never return) */
186 #endif /* NCPUS > 1 */
190 .globl EXT(locore_end)