]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/bsd_i386.c
xnu-344.49.tar.gz
[apple/xnu.git] / osfmk / i386 / bsd_i386.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 #ifdef MACH_BSD
26 #include <cpus.h>
27 #include <mach_rt.h>
28 #include <mach_debug.h>
29 #include <mach_ldebug.h>
30
31 #include <mach/kern_return.h>
32 #include <mach/thread_status.h>
33 #include <mach/vm_param.h>
34
35 #include <kern/counters.h>
36 #include <kern/cpu_data.h>
37 #include <kern/mach_param.h>
38 #include <kern/task.h>
39 #include <kern/thread.h>
40 #include <kern/thread_swap.h>
41 #include <kern/sched_prim.h>
42 #include <kern/misc_protos.h>
43 #include <kern/assert.h>
44 #include <kern/spl.h>
45 #include <ipc/ipc_port.h>
46 #include <vm/vm_kern.h>
47 #include <vm/pmap.h>
48
49 #include <i386/thread.h>
50 #include <i386/eflags.h>
51 #include <i386/proc_reg.h>
52 #include <i386/seg.h>
53 #include <i386/tss.h>
54 #include <i386/user_ldt.h>
55 #include <i386/fpu.h>
56 #include <i386/iopb_entries.h>
57 #include <i386/machdep_call.h>
58
59 #include <sys/syscall.h>
60 #include <sys/ktrace.h>
61 struct proc;
62
63 kern_return_t
64 thread_userstack(
65 thread_t,
66 int,
67 thread_state_t,
68 unsigned int,
69 vm_offset_t *,
70 int *
71 );
72
73 kern_return_t
74 thread_entrypoint(
75 thread_t,
76 int,
77 thread_state_t,
78 unsigned int,
79 vm_offset_t *
80 );
81
82 struct i386_saved_state *
83 get_user_regs(
84 thread_act_t);
85
86 void
87 act_thread_dup(
88 thread_act_t,
89 thread_act_t
90 );
91
92 unsigned int get_msr_exportmask(void);
93
94 unsigned int get_msr_nbits(void);
95
96 unsigned int get_msr_rbits(void);
97
98 /*
99 * thread_userstack:
100 *
101 * Return the user stack pointer from the machine
102 * dependent thread state info.
103 */
104 kern_return_t
105 thread_userstack(
106 thread_t thread,
107 int flavor,
108 thread_state_t tstate,
109 unsigned int count,
110 vm_offset_t *user_stack,
111 int *customstack
112 )
113 {
114 struct i386_saved_state *state;
115 i386_thread_state_t *state25;
116 vm_offset_t uesp;
117
118 if (customstack)
119 *customstack = 0;
120
121 switch (flavor) {
122 case i386_THREAD_STATE: /* FIXME */
123 state25 = (i386_thread_state_t *) tstate;
124 if (state25->esp)
125 *user_stack = state25->esp;
126 if (customstack && state25->esp)
127 *customstack = 1;
128 else
129 *customstack = 0;
130 break;
131
132 case i386_NEW_THREAD_STATE:
133 if (count < i386_NEW_THREAD_STATE_COUNT)
134 return (KERN_INVALID_ARGUMENT);
135 else {
136 state = (struct i386_saved_state *) tstate;
137 uesp = state->uesp;
138 }
139
140 /* If a valid user stack is specified, use it. */
141 if (uesp)
142 *user_stack = uesp;
143 if (customstack && uesp)
144 *customstack = 1;
145 else
146 *customstack = 0;
147 break;
148 default :
149 return (KERN_INVALID_ARGUMENT);
150 }
151
152 return (KERN_SUCCESS);
153 }
154
155 kern_return_t
156 thread_entrypoint(
157 thread_t thread,
158 int flavor,
159 thread_state_t tstate,
160 unsigned int count,
161 vm_offset_t *entry_point
162 )
163 {
164 struct i386_saved_state *state;
165 i386_thread_state_t *state25;
166
167 /*
168 * Set a default.
169 */
170 if (*entry_point == 0)
171 *entry_point = VM_MIN_ADDRESS;
172
173 switch (flavor) {
174 case i386_THREAD_STATE:
175 state25 = (i386_thread_state_t *) tstate;
176 *entry_point = state25->eip ? state25->eip: VM_MIN_ADDRESS;
177 break;
178
179 case i386_NEW_THREAD_STATE:
180 if (count < i386_THREAD_STATE_COUNT)
181 return (KERN_INVALID_ARGUMENT);
182 else {
183 state = (struct i386_saved_state *) tstate;
184
185 /*
186 * If a valid entry point is specified, use it.
187 */
188 *entry_point = state->eip ? state->eip: VM_MIN_ADDRESS;
189 }
190 break;
191 }
192
193 return (KERN_SUCCESS);
194 }
195
196 struct i386_saved_state *
197 get_user_regs(thread_act_t th)
198 {
199 if (th->mact.pcb)
200 return(USER_REGS(th));
201 else {
202 printf("[get_user_regs: thread does not have pcb]");
203 return NULL;
204 }
205 }
206
207 /*
208 * Duplicate parent state in child
209 * for U**X fork.
210 */
211 void
212 act_thread_dup(
213 thread_act_t parent,
214 thread_act_t child
215 )
216 {
217 struct i386_saved_state *parent_state, *child_state;
218 struct i386_machine_state *ims;
219 struct i386_float_state floatregs;
220
221 #ifdef XXX
222 /* Save the FPU state */
223 if ((pcb_t)(per_proc_info[cpu_number()].fpu_pcb) == parent->mact.pcb) {
224 fp_state_save(parent);
225 }
226 #endif
227
228 if (child->mact.pcb == NULL
229 || parent->mact.pcb == NULL) {
230 panic("[thread_dup, child (%x) or parent (%x) is NULL!]",
231 child->mact.pcb, parent->mact.pcb);
232 return;
233 }
234
235 /* Copy over the i386_saved_state registers */
236 child->mact.pcb->iss = parent->mact.pcb->iss;
237
238 /* Check to see if parent is using floating point
239 * and if so, copy the registers to the child
240 * FIXME - make sure this works.
241 */
242
243 if (parent->mact.pcb->ims.ifps) {
244 if (fpu_get_state(parent, &floatregs) == KERN_SUCCESS)
245 fpu_set_state(child, &floatregs);
246 }
247
248 /* FIXME - should a user specified LDT, TSS and V86 info
249 * be duplicated as well?? - probably not.
250 */
251 }
252
253 /*
254 * FIXME - thread_set_child
255 */
256
257 void thread_set_child(thread_act_t child, int pid);
258 void
259 thread_set_child(thread_act_t child, int pid)
260 {
261 child->mact.pcb->iss.eax = pid;
262 child->mact.pcb->iss.edx = 1;
263 child->mact.pcb->iss.efl &= ~EFL_CF;
264 }
265 void thread_set_parent(thread_act_t parent, int pid);
266 void
267 thread_set_parent(thread_act_t parent, int pid)
268 {
269 parent->mact.pcb->iss.eax = pid;
270 parent->mact.pcb->iss.edx = 0;
271 parent->mact.pcb->iss.efl &= ~EFL_CF;
272 }
273
274
275
276 /*
277 * Move pages from one kernel virtual address to another.
278 * Both addresses are assumed to reside in the Sysmap,
279 * and size must be a multiple of the page size.
280 */
281 void
282 pagemove(
283 register caddr_t from,
284 register caddr_t to,
285 int size)
286 {
287 pmap_movepage((unsigned long)from, (unsigned long)to, (vm_size_t)size);
288 }
289
290 /*
291 * System Call handling code
292 */
293
294 #define ERESTART -1 /* restart syscall */
295 #define EJUSTRETURN -2 /* don't modify regs, just return */
296
297 struct sysent { /* system call table */
298 unsigned short sy_narg; /* number of args */
299 char sy_parallel; /* can execute in parallel */
300 char sy_funnel; /* funnel type */
301 unsigned long (*sy_call)(void *, void *, int *); /* implementing function */
302 };
303
304 #define NO_FUNNEL 0
305 #define KERNEL_FUNNEL 1
306 #define NETWORK_FUNNEL 2
307
308 extern funnel_t * kernel_flock;
309 extern funnel_t * network_flock;
310
311 extern struct sysent sysent[];
312
313 int set_bsduthreadargs (thread_act_t, struct i386_saved_state *, void *);
314
315 void * get_bsduthreadarg(thread_act_t);
316
317 void unix_syscall(struct i386_saved_state *);
318
319 void
320 unix_syscall_return(int error)
321 {
322 thread_act_t thread;
323 volatile int *rval;
324 struct i386_saved_state *regs;
325 struct proc *p;
326 struct proc *current_proc();
327 unsigned short code;
328 vm_offset_t params;
329 struct sysent *callp;
330 extern int nsysent;
331
332 thread = current_act();
333 rval = (int *)get_bsduthreadrval(thread);
334 p = current_proc();
335
336 regs = USER_REGS(thread);
337
338 /* reconstruct code for tracing before blasting eax */
339 code = regs->eax;
340 params = (vm_offset_t) ((caddr_t)regs->uesp + sizeof (int));
341 callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
342 if (callp == sysent) {
343 code = fuword(params);
344 }
345
346 if (error == ERESTART) {
347 regs->eip -= 7;
348 }
349 else if (error != EJUSTRETURN) {
350 if (error) {
351 regs->eax = error;
352 regs->efl |= EFL_CF; /* carry bit */
353 } else { /* (not error) */
354 regs->eax = rval[0];
355 regs->edx = rval[1];
356 regs->efl &= ~EFL_CF;
357 }
358 }
359
360 ktrsysret(p, code, error, rval[0], callp->sy_funnel);
361
362 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
363 error, rval[0], rval[1], 0, 0);
364
365 if (callp->sy_funnel != NO_FUNNEL) {
366 assert(thread_funnel_get() == THR_FUNNEL_NULL);
367 (void) thread_funnel_set(current_thread()->funnel_lock, FALSE);
368 }
369
370 thread_exception_return();
371 /* NOTREACHED */
372 }
373
374
375 void
376 unix_syscall(struct i386_saved_state *regs)
377 {
378 thread_act_t thread;
379 void *vt;
380 unsigned short code;
381 struct sysent *callp;
382 int nargs, error;
383 volatile int *rval;
384 int funnel_type;
385 vm_offset_t params;
386 extern int nsysent;
387 struct proc *p;
388 struct proc *current_proc();
389
390 thread = current_act();
391 p = current_proc();
392 rval = (int *)get_bsduthreadrval(thread);
393
394 //printf("[scall : eax %x]", regs->eax);
395 code = regs->eax;
396 params = (vm_offset_t) ((caddr_t)regs->uesp + sizeof (int));
397 callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
398 if (callp == sysent) {
399 code = fuword(params);
400 params += sizeof (int);
401 callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
402 }
403
404 vt = get_bsduthreadarg(thread);
405
406 if ((nargs = (callp->sy_narg * sizeof (int))) &&
407 (error = copyin((char *) params, (char *)vt , nargs)) != 0) {
408 regs->eax = error;
409 regs->efl |= EFL_CF;
410 thread_exception_return();
411 /* NOTREACHED */
412 }
413
414 rval[0] = 0;
415 rval[1] = regs->edx;
416
417 funnel_type = callp->sy_funnel;
418 if(funnel_type == KERNEL_FUNNEL)
419 (void) thread_funnel_set(kernel_flock, TRUE);
420 else if (funnel_type == NETWORK_FUNNEL)
421 (void) thread_funnel_set(network_flock, TRUE);
422
423 set_bsduthreadargs(thread, regs, NULL);
424
425 if (callp->sy_narg > 8)
426 panic("unix_syscall max arg count exceeded (%d)", callp->sy_narg);
427
428 ktrsyscall(p, code, callp->sy_narg, vt, funnel_type);
429
430 {
431 int *ip = (int *)vt;
432 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
433 *ip, *(ip+1), *(ip+2), *(ip+3), 0);
434 }
435
436 error = (*(callp->sy_call))(p, (void *) vt, rval);
437
438 #if 0
439 /* May be needed with vfork changes */
440 regs = USER_REGS(thread);
441 #endif
442 if (error == ERESTART) {
443 regs->eip -= 7;
444 }
445 else if (error != EJUSTRETURN) {
446 if (error) {
447 regs->eax = error;
448 regs->efl |= EFL_CF; /* carry bit */
449 } else { /* (not error) */
450 regs->eax = rval[0];
451 regs->edx = rval[1];
452 regs->efl &= ~EFL_CF;
453 }
454 }
455
456 ktrsysret(p, code, error, rval[0], funnel_type);
457
458 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
459 error, rval[0], rval[1], 0, 0);
460
461 if(funnel_type != NO_FUNNEL)
462 (void) thread_funnel_set(current_thread()->funnel_lock, FALSE);
463
464 thread_exception_return();
465 /* NOTREACHED */
466 }
467
468
469 void
470 machdep_syscall( struct i386_saved_state *regs)
471 {
472 int trapno, nargs;
473 machdep_call_t *entry;
474 thread_t thread;
475 struct proc *p;
476 struct proc *current_proc();
477
478 trapno = regs->eax;
479 if (trapno < 0 || trapno >= machdep_call_count) {
480 regs->eax = (unsigned int)kern_invalid();
481
482 thread_exception_return();
483 /* NOTREACHED */
484 }
485
486 entry = &machdep_call_table[trapno];
487 nargs = entry->nargs;
488
489 if (nargs > 0) {
490 int args[nargs];
491
492 if (copyin((char *) regs->uesp + sizeof (int),
493 (char *) args,
494 nargs * sizeof (int))) {
495
496 regs->eax = KERN_INVALID_ADDRESS;
497
498 thread_exception_return();
499 /* NOTREACHED */
500 }
501
502 asm volatile("
503 1:
504 mov (%2),%%eax;
505 pushl %%eax;
506 sub $4,%2;
507 dec %1;
508 jne 1b;
509 mov %3,%%eax;
510 call *%%eax;
511 mov %%eax,%0"
512
513 : "=r" (regs->eax)
514 : "r" (nargs),
515 "r" (&args[nargs - 1]),
516 "g" (entry->routine)
517 : "ax", "cx", "dx", "sp");
518 }
519 else
520 regs->eax = (unsigned int)(*entry->routine)();
521
522 if (current_thread()->funnel_lock)
523 (void) thread_funnel_set(current_thread()->funnel_lock, FALSE);
524
525 thread_exception_return();
526 /* NOTREACHED */
527 }
528
529
530 kern_return_t
531 thread_set_cthread_self(int self)
532 {
533 current_act()->mact.pcb->cthread_self = (unsigned int)self;
534
535 return (KERN_SUCCESS);
536 }
537
538 kern_return_t
539 thread_get_cthread_self(void)
540 {
541 return ((kern_return_t)current_act()->mact.pcb->cthread_self);
542 }
543
544 void
545 mach25_syscall(struct i386_saved_state *regs)
546 {
547 printf("*** Atttempt to execute a Mach 2.5 system call at EIP=%x EAX=%x(%d)\n",
548 regs->eip, regs->eax, -regs->eax);
549 panic("FIXME!");
550 }
551
552 #endif /* MACH_BSD */
553
554 #undef current_thread
555 thread_t
556 current_thread(void)
557 {
558 return(current_thread_fast());
559 }