]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/thread.h
xnu-1504.9.26.tar.gz
[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 #include <i386/cpu_data.h>
82
83
84 /*
85 * i386_saved_state:
86 *
87 * Has been exported to servers. See: mach/i386/thread_status.h
88 *
89 * This structure corresponds to the state of user registers
90 * as saved upon kernel entry. It lives in the pcb.
91 * It is also pushed onto the stack for exceptions in the kernel.
92 * For performance, it is also used directly in syscall exceptions
93 * if the server has requested i386_THREAD_STATE flavor for the exception
94 * port.
95 */
96
97 /*
98 * Save area for user floating-point state.
99 * Allocated only when necessary.
100 */
101
102 struct x86_fpsave_state {
103 boolean_t fp_valid;
104 enum {
105 FXSAVE32 = 1,
106 FXSAVE64 = 2
107 } fp_save_layout;
108 struct x86_fx_save fx_save_state __attribute__ ((aligned (16)));
109 };
110
111
112 /*
113 * x86_kernel_state:
114 *
115 * This structure corresponds to the state of kernel registers
116 * as saved in a context-switch. It lives at the base of the stack.
117 */
118
119 #ifdef __i386__
120 struct x86_kernel_state {
121 int k_ebx; /* kernel context */
122 int k_esp;
123 int k_ebp;
124 int k_edi;
125 int k_esi;
126 int k_eip;
127 /*
128 * Kernel stacks are 16-byte aligned with a 4-byte i386_exception_link at
129 * the top, followed by an x86_kernel_state. After both structs have
130 * been pushed, we want to be 16-byte aligned. A dummy int gets us there.
131 */
132 int dummy;
133 };
134 #else
135 struct x86_kernel_state {
136 unsigned long k_rbx; /* kernel context */
137 unsigned long k_rsp;
138 unsigned long k_rbp;
139 unsigned long k_r12;
140 unsigned long k_r13;
141 unsigned long k_r14;
142 unsigned long k_r15;
143 unsigned long k_rip;
144 unsigned long dummy;
145 };
146 #endif
147
148 typedef struct pcb {
149 void *sf;
150 x86_saved_state_t *iss;
151 struct x86_fpsave_state *ifps;
152 #ifdef MACH_BSD
153 uint64_t cthread_self; /* for use of cthread package */
154 struct real_descriptor cthread_desc;
155 unsigned long uldt_selector; /* user ldt selector to set */
156 struct real_descriptor uldt_desc; /* the actual user setable ldt data */
157 #endif
158 decl_simple_lock_data(,lock);
159 uint64_t iss_pte0;
160 uint64_t iss_pte1;
161 void *ids;
162 uint32_t arg_store_valid;
163 } *pcb_t;
164
165 /*
166 * Maps state flavor to number of words in the state:
167 */
168 __private_extern__ unsigned int _MachineStateCount[];
169
170 #define USER_STATE(ThrAct) ((ThrAct)->machine.pcb->iss)
171 #define USER_REGS32(ThrAct) (saved_state32(USER_STATE(ThrAct)))
172 #define USER_REGS64(ThrAct) (saved_state64(USER_STATE(ThrAct)))
173
174 #define user_pc(ThrAct) (is_saved_state32(USER_STATE(ThrAct)) ? \
175 USER_REGS32(ThrAct)->eip : \
176 USER_REGS64(ThrAct)->isf.rip )
177
178
179 struct machine_thread {
180 /*
181 * pointer to process control block
182 * (actual storage may as well be here, too)
183 */
184 struct pcb xxx_pcb;
185 pcb_t pcb;
186
187 uint32_t specFlags;
188 #define OnProc 0x1
189 #define CopyIOActive 0x2 /* Checked to ensure DTrace actions do not re-enter copyio(). */
190
191 #if NCOPY_WINDOWS > 0
192
193 struct {
194 user_addr_t user_base;
195 } copy_window[NCOPY_WINDOWS];
196 int nxt_window;
197 int copyio_state;
198 #define WINDOWS_DIRTY 0
199 #define WINDOWS_CLEAN 1
200 #define WINDOWS_CLOSED 2
201 #define WINDOWS_OPENED 3
202 uint64_t physwindow_pte;
203 int physwindow_busy;
204 #endif
205 };
206
207
208 extern void *get_user_regs(thread_t);
209
210 extern void *act_thread_csave(void);
211 extern void act_thread_catt(void *ctx);
212 extern void act_thread_cfree(void *ctx);
213
214 /*
215 * i386_exception_link:
216 *
217 * This structure lives at the high end of the kernel stack.
218 * It points to the current thread`s user registers.
219 */
220 struct i386_exception_link {
221 x86_saved_state_t *saved_state;
222 };
223
224
225 /*
226 * On the kernel stack is:
227 * stack: ...
228 * struct i386_exception_link (pointer to user state)
229 * struct x86_kernel_state
230 * stack+kernel_stack_size
231 */
232
233 #define STACK_IKS(stack) \
234 ((struct x86_kernel_state *)((stack) + kernel_stack_size) - 1)
235 #define STACK_IEL(stack) \
236 ((struct i386_exception_link *)STACK_IKS(stack) - 1)
237
238 /*
239 * Return the current stack depth
240 * including x86_kernel_state and i386_exception_link
241 */
242 static inline vm_offset_t
243 current_stack_depth(void)
244 {
245 vm_offset_t stack_ptr;
246
247 assert(get_preemption_level() > 0 || !ml_get_interrupts_enabled());
248
249 #if defined(__x86_64__)
250 __asm__ volatile("mov %%rsp, %0" : "=m" (stack_ptr));
251 #else
252 __asm__ volatile("mov %%esp, %0" : "=m" (stack_ptr));
253 #endif
254 return (current_cpu_datap()->cpu_kernel_stack
255 + sizeof(struct x86_kernel_state)
256 + sizeof(struct i386_exception_link *)
257 - stack_ptr);
258 }
259
260 /*
261 * Return address of the function that called current function, given
262 * address of the first parameter of current function.
263 */
264 #define GET_RETURN_PC(addr) (__builtin_return_address(0))
265
266 /*
267 * Defining this indicates that MD code will supply an exception()
268 * routine, conformant with kern/exception.c (dependency alert!)
269 * but which does wonderfully fast, machine-dependent magic.
270 */
271 #define MACHINE_FAST_EXCEPTION 1
272
273 #endif /* _I386_THREAD_H_ */