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.
53 #include <platforms.h>
56 #include <i386/proc_reg.h>
60 #include <sqt/asm_macros.h>
67 #define CX(addr, reg) addr(,reg,4)
71 * Context switch routines for i386.
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 */
85 xorl %eax,%eax /* return zero (no old thread) */
87 call EXT(thread_continue)
90 * This has to save registers only
91 * when there is no explicit continuation.
95 popl %eax /* pop return PC */
97 /* Test for a continuation and skip all state saving if so... */
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 */
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 */
115 movl %ecx,%gs:CPU_ACTIVE_STACK /* set current stack */
116 movl %ebx,%gs:CPU_KERNEL_STACK /* set stack top */
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 */
126 Entry(Thread_continue)
127 pushl %eax /* push the thread argument */
128 xorl %ebp,%ebp /* zero frame pointer */
129 call *%ebx /* call real continuation */
132 * thread_t Shutdown_context(thread_t thread,
133 * void (*routine)(processor_t),
134 * processor_t processor)
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.
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 */
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 */
156 movl %gs:CPU_INT_STACK_TOP,%esp /* switch to interrupt stack */
158 pushl %esi /* push argument */
159 call *%ebx /* call routine to run */
160 hlt /* (should never return) */