]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/thread.h
d29d7ac2660b2510f70dd7b4b0b4e9aebd13d1ca
[apple/xnu.git] / osfmk / i386 / thread.h
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58
59 /*
60 * File: machine/thread.h
61 *
62 * This file contains the structure definitions for the thread
63 * state as applied to I386 processors.
64 */
65
66 #ifndef _I386_THREAD_H_
67 #define _I386_THREAD_H_
68
69 #include <mach/boolean.h>
70 #include <mach/i386/vm_types.h>
71 #include <mach/i386/fp_reg.h>
72 #include <mach/thread_status.h>
73
74 #include <kern/lock.h>
75
76 #include <i386/iopb.h>
77 #include <i386/seg.h>
78 #include <i386/tss.h>
79 #include <i386/eflags.h>
80
81 /*
82 * i386_saved_state:
83 *
84 * Has been exported to servers. See: mach/i386/thread_status.h
85 *
86 * This structure corresponds to the state of user registers
87 * as saved upon kernel entry. It lives in the pcb.
88 * It is also pushed onto the stack for exceptions in the kernel.
89 * For performance, it is also used directly in syscall exceptions
90 * if the server has requested i386_THREAD_STATE flavor for the exception
91 * port.
92 */
93
94 /*
95 * Save area for user floating-point state.
96 * Allocated only when necessary.
97 */
98
99 struct x86_fpsave_state {
100 boolean_t fp_valid;
101 enum {
102 FXSAVE32 = 1,
103 FXSAVE64 = 2
104 } fp_save_layout;
105 struct x86_fx_save fx_save_state __attribute__ ((aligned (16)));
106 };
107
108
109 /*
110 * x86_kernel_state32:
111 *
112 * This structure corresponds to the state of kernel registers
113 * as saved in a context-switch. It lives at the base of the stack.
114 * kernel only runs in 32 bit mode for now
115 */
116
117 struct x86_kernel_state32 {
118 int k_ebx; /* kernel context */
119 int k_esp;
120 int k_ebp;
121 int k_edi;
122 int k_esi;
123 int k_eip;
124 /*
125 * Kernel stacks are 16-byte aligned with a 4-byte i386_exception_link at
126 * the top, followed by an x86_kernel_state32. After both structs have
127 * been pushed, we want to be 16-byte aligned. A dummy int gets us there.
128 */
129 int dummy;
130 };
131
132
133 typedef struct pcb {
134 void *sf;
135 x86_saved_state_t *iss;
136 struct x86_fpsave_state *ifps;
137 #ifdef MACH_BSD
138 uint64_t cthread_self; /* for use of cthread package */
139 struct real_descriptor cthread_desc;
140 unsigned long uldt_selector; /* user ldt selector to set */
141 struct real_descriptor uldt_desc; /* the actual user setable ldt data */
142 #endif
143 decl_simple_lock_data(,lock);
144 uint64_t iss_pte0;
145 uint64_t iss_pte1;
146 void *ids;
147 } *pcb_t;
148
149
150 /*
151 * Maps state flavor to number of words in the state:
152 */
153 __private_extern__ unsigned int _MachineStateCount[];
154
155 #define USER_STATE(ThrAct) ((ThrAct)->machine.pcb->iss)
156 #define USER_REGS32(ThrAct) (saved_state32(USER_STATE(ThrAct)))
157 #define USER_REGS64(ThrAct) (saved_state64(USER_STATE(ThrAct)))
158
159 #define user_pc(ThrAct) (is_saved_state32(USER_STATE(ThrAct)) ? \
160 USER_REGS32(ThrAct)->eip : \
161 USER_REGS64(ThrAct)->isf.rip )
162
163
164 struct machine_thread {
165 /*
166 * pointer to process control block
167 * (actual storage may as well be here, too)
168 */
169 struct pcb xxx_pcb;
170 pcb_t pcb;
171
172 uint32_t specFlags;
173 #define OnProc 0x1
174
175 struct {
176 user_addr_t user_base;
177 } copy_window[NCOPY_WINDOWS];
178 int nxt_window;
179 int copyio_state;
180 #define WINDOWS_DIRTY 0
181 #define WINDOWS_CLEAN 1
182 #define WINDOWS_CLOSED 2
183 #define WINDOWS_OPENED 3
184 uint64_t physwindow_pte;
185 int physwindow_busy;
186 };
187
188
189 extern void *get_user_regs(thread_t);
190
191 extern void *act_thread_csave(void);
192 extern void act_thread_catt(void *ctx);
193 extern void act_thread_cfree(void *ctx);
194
195 /*
196 * i386_exception_link:
197 *
198 * This structure lives at the high end of the kernel stack.
199 * It points to the current thread`s user registers.
200 */
201 struct i386_exception_link {
202 x86_saved_state_t *saved_state;
203 };
204
205
206 /*
207 * On the kernel stack is:
208 * stack: ...
209 * struct i386_exception_link
210 * struct i386_kernel_state
211 * stack+KERNEL_STACK_SIZE
212 */
213
214 #define STACK_IKS(stack) \
215 ((struct x86_kernel_state32 *)((stack) + KERNEL_STACK_SIZE) - 1)
216 #define STACK_IEL(stack) \
217 ((struct i386_exception_link *)STACK_IKS(stack) - 1)
218
219 /*
220 * Return address of the function that called current function, given
221 * address of the first parameter of current function.
222 */
223 #define GET_RETURN_PC(addr) (*((vm_offset_t *)addr - 1))
224
225 /*
226 * Defining this indicates that MD code will supply an exception()
227 * routine, conformant with kern/exception.c (dependency alert!)
228 * but which does wonderfully fast, machine-dependent magic.
229 */
230 #define MACHINE_FAST_EXCEPTION 1
231
232 #endif /* _I386_THREAD_H_ */