2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 #include <platforms.h>
57 #include <i386/proc_reg.h>
63 #include <sqt/asm_macros.h>
67 #include <i386/AT386/mp/mp.h>
70 #define CX(addr, reg) addr(,reg,4)
72 #else /* NCPUS == 1 */
74 #define CPU_NUMBER(reg)
75 #define CX(addr,reg) addr
77 #endif /* NCPUS == 1 */
80 * Context switch routines for i386.
84 movl S_ARG0,%ecx /* get thread */
85 movl TH_KERNEL_STACK(%ecx),%ecx /* get kernel stack */
86 lea KERNEL_STACK_SIZE-IKS_SIZE-IEL_SIZE(%ecx),%edx
87 /* point to stack top */
89 movl %ecx,CX(EXT(active_stacks),%eax) /* store stack address */
90 movl %edx,CX(EXT(kernel_stack),%eax) /* store stack top */
92 movl KSS_ESP(%ecx),%esp /* switch stacks */
93 movl KSS_ESI(%ecx),%esi /* restore registers */
94 movl KSS_EDI(%ecx),%edi
95 movl KSS_EBP(%ecx),%ebp
96 movl KSS_EBX(%ecx),%ebx
97 xorl %eax,%eax /* return zero (no old thread) */
98 jmp *KSS_EIP(%ecx) /* resume thread */
101 * This really only has to save registers
102 * when there is no explicit continuation.
105 Entry(Switch_context)
107 movl CX(EXT(active_stacks),%edx),%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 popl KSS_EIP(%ecx) /* save return PC */
114 movl %esp,KSS_ESP(%ecx) /* save SP */
116 movl 0(%esp),%eax /* get old thread */
117 movl 4(%esp),%ebx /* get continuation */
118 movl %ebx,TH_CONTINUATION(%eax) /* save continuation */
119 movl %ecx,TH_KERNEL_STACK(%eax) /* save kernel stack */
121 movl 8(%esp),%esi /* get new thread */
122 movl $ CPD_ACTIVE_THREAD,%ecx
123 movl %esi,%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 */
131 movl TH_TOP_ACT(%esi),%esi /* get new_thread->top_act */
132 cmpl $0,ACT_KLOADED(%esi) /* check kernel-loaded flag */
134 movl %esi,CX(EXT(active_kloaded),%edx)
137 movl $0,CX(EXT(active_kloaded),%edx)
139 movl KSS_ESP(%ecx),%esp /* switch stacks */
140 movl KSS_ESI(%ecx),%esi /* restore registers */
141 movl KSS_EDI(%ecx),%edi
142 movl KSS_EBP(%ecx),%ebp
143 movl KSS_EBX(%ecx),%ebx
144 jmp *KSS_EIP(%ecx) /* return old thread */
146 Entry(Thread_continue)
147 pushl %eax /* push the thread argument */
148 xorl %ebp,%ebp /* zero frame pointer */
149 call *%ebx /* call real continuation */
153 * void switch_to_shutdown_context(thread_t thread,
154 * void (*routine)(processor_t),
155 * processor_t processor)
157 * saves the kernel context of the thread,
158 * switches to the interrupt stack,
159 * continues the thread (with thread_continue),
160 * then runs routine on the interrupt stack.
162 * Assumes that the thread is a kernel thread (thus
165 Entry(switch_to_shutdown_context)
167 movl EXT(active_stacks)(,%edx,4),%ecx /* get old kernel stack */
168 movl %ebx,KSS_EBX(%ecx) /* save registers */
169 movl %ebp,KSS_EBP(%ecx)
170 movl %edi,KSS_EDI(%ecx)
171 movl %esi,KSS_ESI(%ecx)
172 popl KSS_EIP(%ecx) /* save return PC */
173 movl %esp,KSS_ESP(%ecx) /* save SP */
175 movl 0(%esp),%eax /* get old thread */
176 movl $0,TH_CONTINUATION(%eax) /* clear continuation */
177 movl %ecx,TH_KERNEL_STACK(%eax) /* save old stack */
178 movl 4(%esp),%ebx /* get routine to run next */
179 movl 8(%esp),%esi /* get its argument */
181 movl CX(EXT(interrupt_stack),%edx),%ecx /* point to its intr stack */
182 lea INTSTACK_SIZE(%ecx),%esp /* switch to it (top) */
184 pushl %eax /* push thread */
185 call EXT(thread_dispatch) /* reschedule thread */
186 addl $4,%esp /* clean stack */
188 pushl %esi /* push argument */
189 call *%ebx /* call routine to run */
190 hlt /* (should never return) */
192 #endif /* NCPUS > 1 */
196 .globl EXT(locore_end)