]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/cswitch.s
xnu-792.10.96.tar.gz
[apple/xnu.git] / osfmk / i386 / cswitch.s
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
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.
1c79356b
A
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
1c79356b
A
53#include <platforms.h>
54
55#include <i386/asm.h>
56#include <i386/proc_reg.h>
57#include <assym.s>
58
1c79356b
A
59#ifdef SYMMETRY
60#include <sqt/asm_macros.h>
61#endif
62
63#if AT386
55e303ae 64#include <i386/mp.h>
1c79356b
A
65#endif /* AT386 */
66
67#define CX(addr, reg) addr(,reg,4)
68
c0fea474 69 .text
1c79356b
A
70/*
71 * Context switch routines for i386.
72 */
73
74Entry(Load_context)
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 */
91447636
A
79 movl %ecx,%gs:CPU_ACTIVE_STACK /* store stack address */
80 movl %edx,%gs:CPU_KERNEL_STACK /* store stack top */
81
82 movl %edx,%esp
83 movl %edx,%ebp
1c79356b 84
1c79356b 85 xorl %eax,%eax /* return zero (no old thread) */
91447636
A
86 pushl %eax
87 call EXT(thread_continue)
1c79356b
A
88
89/*
c0fea474 90 * This has to save registers only
1c79356b
A
91 * when there is no explicit continuation.
92 */
93
94Entry(Switch_context)
c0fea474 95 popl %eax /* pop return PC */
1c79356b 96
c0fea474
A
97 /* Test for a continuation and skip all state saving if so... */
98 cmpl $0,4(%esp)
99 jne 5f
100 movl %gs:CPU_ACTIVE_STACK,%ecx /* get old kernel stack */
1c79356b
A
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)
c0fea474 105 movl %eax,KSS_EIP(%ecx) /* save return PC */
1c79356b 106 movl %esp,KSS_ESP(%ecx) /* save SP */
c0fea474 1075:
55e303ae 108 movl 0(%esp),%eax /* return old thread */
91447636 109 movl 8(%esp),%ebx /* get new thread */
c0fea474 110 movl %ebx,%gs:CPU_ACTIVE_THREAD /* new thread is active */
91447636 111 movl TH_KERNEL_STACK(%ebx),%ecx /* get its kernel stack */
1c79356b
A
112 lea KERNEL_STACK_SIZE-IKS_SIZE-IEL_SIZE(%ecx),%ebx
113 /* point to stack top */
114
91447636
A
115 movl %ecx,%gs:CPU_ACTIVE_STACK /* set current stack */
116 movl %ebx,%gs:CPU_KERNEL_STACK /* set stack top */
1c79356b 117
c0fea474 118
1c79356b
A
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 */
125
126Entry(Thread_continue)
127 pushl %eax /* push the thread argument */
128 xorl %ebp,%ebp /* zero frame pointer */
129 call *%ebx /* call real continuation */
130
1c79356b 131/*
c0fea474
A
132 * thread_t Shutdown_context(thread_t thread,
133 * void (*routine)(processor_t),
134 * processor_t processor)
1c79356b
A
135 *
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.
140 *
1c79356b 141 */
c0fea474 142Entry(Shutdown_context)
91447636 143 movl %gs:CPU_ACTIVE_STACK,%ecx /* get old kernel stack */
1c79356b
A
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 */
150
151 movl 0(%esp),%eax /* get old thread */
1c79356b
A
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 */
155
91447636 156 movl %gs:CPU_INT_STACK_TOP,%esp /* switch to interrupt stack */
1c79356b
A
157
158 pushl %esi /* push argument */
159 call *%ebx /* call routine to run */
160 hlt /* (should never return) */