]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/i386/cswitch.s
xnu-517.7.7.tar.gz
[apple/xnu.git] / osfmk / i386 / cswitch.s
... / ...
CommitLineData
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 <cpus.h>
54#include <platforms.h>
55
56#include <i386/asm.h>
57#include <i386/proc_reg.h>
58#include <assym.s>
59
60#if NCPUS > 1
61
62#ifdef SYMMETRY
63#include <sqt/asm_macros.h>
64#endif
65
66#if AT386
67#include <i386/mp.h>
68#endif /* AT386 */
69
70#define CX(addr, reg) addr(,reg,4)
71
72#else /* NCPUS == 1 */
73
74#define CPU_NUMBER(reg)
75#define CX(addr,reg) addr
76
77#endif /* NCPUS == 1 */
78
79/*
80 * Context switch routines for i386.
81 */
82
83Entry(Load_context)
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 */
88 CPU_NUMBER(%eax)
89 movl %ecx,CX(EXT(active_stacks),%eax) /* store stack address */
90 movl %edx,CX(EXT(kernel_stack),%eax) /* store stack top */
91
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 */
99
100/*
101 * This really only has to save registers
102 * when there is no explicit continuation.
103 */
104
105Entry(Switch_context)
106 CPU_NUMBER(%edx)
107 movl CX(EXT(active_stacks),%edx),%ecx /* get old kernel stack */
108
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 */
115
116 movl 0(%esp),%eax /* return old thread */
117 movl 8(%esp),%esi /* get new thread */
118 movl TH_TOP_ACT(%esi),%ebx /* get new_thread->top_act */
119 movl $ CPD_ACTIVE_THREAD,%ecx
120 movl %ebx,%gs:(%ecx) /* new thread is active */
121 movl TH_KERNEL_STACK(%esi),%ecx /* get its kernel stack */
122 lea KERNEL_STACK_SIZE-IKS_SIZE-IEL_SIZE(%ecx),%ebx
123 /* point to stack top */
124
125 movl %ecx,CX(EXT(active_stacks),%edx) /* set current stack */
126 movl %ebx,CX(EXT(kernel_stack),%edx) /* set stack top */
127
128
129 movl $0,CX(EXT(active_kloaded),%edx)
130
131 movl KSS_ESP(%ecx),%esp /* switch stacks */
132 movl KSS_ESI(%ecx),%esi /* restore registers */
133 movl KSS_EDI(%ecx),%edi
134 movl KSS_EBP(%ecx),%ebp
135 movl KSS_EBX(%ecx),%ebx
136 jmp *KSS_EIP(%ecx) /* return old thread */
137
138Entry(Thread_continue)
139 pushl %eax /* push the thread argument */
140 xorl %ebp,%ebp /* zero frame pointer */
141 call *%ebx /* call real continuation */
142
143#if NCPUS > 1
144/*
145 * void switch_to_shutdown_context(thread_t thread,
146 * void (*routine)(processor_t),
147 * processor_t processor)
148 *
149 * saves the kernel context of the thread,
150 * switches to the interrupt stack,
151 * continues the thread (with thread_continue),
152 * then runs routine on the interrupt stack.
153 *
154 * Assumes that the thread is a kernel thread (thus
155 * has no FPU state)
156 */
157Entry(switch_to_shutdown_context)
158 CPU_NUMBER(%edx)
159 movl EXT(active_stacks)(,%edx,4),%ecx /* get old kernel stack */
160 movl %ebx,KSS_EBX(%ecx) /* save registers */
161 movl %ebp,KSS_EBP(%ecx)
162 movl %edi,KSS_EDI(%ecx)
163 movl %esi,KSS_ESI(%ecx)
164 popl KSS_EIP(%ecx) /* save return PC */
165 movl %esp,KSS_ESP(%ecx) /* save SP */
166
167 movl 0(%esp),%eax /* get old thread */
168 movl %ecx,TH_KERNEL_STACK(%eax) /* save old stack */
169 movl 4(%esp),%ebx /* get routine to run next */
170 movl 8(%esp),%esi /* get its argument */
171
172 movl CX(EXT(interrupt_stack),%edx),%ecx /* point to its intr stack */
173 lea INTSTACK_SIZE(%ecx),%esp /* switch to it (top) */
174
175 pushl %eax /* push thread */
176 call EXT(thread_dispatch) /* reschedule thread */
177 addl $4,%esp /* clean stack */
178
179 pushl %esi /* push argument */
180 call *%ebx /* call routine to run */
181 hlt /* (should never return) */
182
183#endif /* NCPUS > 1 */
184
185 .text
186
187 .globl EXT(locore_end)
188LEXT(locore_end)
189