2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
25 #include <mach_debug.h>
26 #include <mach_ldebug.h>
28 #include <mach/kern_return.h>
29 #include <mach/thread_status.h>
30 #include <mach/vm_param.h>
33 #include <kern/counters.h>
34 #include <kern/cpu_data.h>
35 #include <kern/mach_param.h>
36 #include <kern/task.h>
37 #include <kern/thread.h>
38 #include <kern/thread_swap.h>
39 #include <kern/sched_prim.h>
40 #include <kern/misc_protos.h>
41 #include <kern/assert.h>
43 #include <ipc/ipc_port.h>
44 #include <vm/vm_kern.h>
47 #include <i386/thread.h>
48 #include <i386/eflags.h>
49 #include <i386/proc_reg.h>
52 #include <i386/user_ldt.h>
54 #include <i386/iopb_entries.h>
55 #include <i386/machdep_call.h>
57 #define USRSTACK 0xc0000000
78 struct i386_saved_state
*
88 unsigned int get_msr_exportmask(void);
90 unsigned int get_msr_nbits(void);
92 unsigned int get_msr_rbits(void);
97 * Return the user stack pointer from the machine
98 * dependent thread state info.
104 thread_state_t tstate
,
106 vm_offset_t
*user_stack
,
110 struct i386_saved_state
*state
;
111 i386_thread_state_t
*state25
;
117 if (*user_stack
== 0)
118 *user_stack
= USRSTACK
;
123 case i386_THREAD_STATE
: /* FIXME */
124 state25
= (i386_thread_state_t
*) tstate
;
125 *user_stack
= state25
->esp
? state25
->esp
: USRSTACK
;
126 if (customstack
&& state25
->esp
)
132 case i386_NEW_THREAD_STATE
:
133 if (count
< i386_NEW_THREAD_STATE_COUNT
)
134 return (KERN_INVALID_ARGUMENT
);
136 state
= (struct i386_saved_state
*) tstate
;
141 * If a valid user stack is specified, use it.
143 *user_stack
= uesp
? uesp
: USRSTACK
;
144 if (customstack
&& uesp
)
150 return (KERN_INVALID_ARGUMENT
);
153 return (KERN_SUCCESS
);
160 thread_state_t tstate
,
162 vm_offset_t
*entry_point
165 struct i386_saved_state
*state
;
166 i386_thread_state_t
*state25
;
171 if (*entry_point
== 0)
172 *entry_point
= VM_MIN_ADDRESS
;
175 case i386_THREAD_STATE
:
176 state25
= (i386_thread_state_t
*) tstate
;
177 *entry_point
= state25
->eip
? state25
->eip
: VM_MIN_ADDRESS
;
180 case i386_NEW_THREAD_STATE
:
181 if (count
< i386_THREAD_STATE_COUNT
)
182 return (KERN_INVALID_ARGUMENT
);
184 state
= (struct i386_saved_state
*) tstate
;
187 * If a valid entry point is specified, use it.
189 *entry_point
= state
->eip
? state
->eip
: VM_MIN_ADDRESS
;
194 return (KERN_SUCCESS
);
197 struct i386_saved_state
*
198 get_user_regs(thread_act_t th
)
201 return(USER_REGS(th
));
203 printf("[get_user_regs: thread does not have pcb]");
209 * Duplicate parent state in child
218 struct i386_saved_state
*parent_state
, *child_state
;
219 struct i386_machine_state
*ims
;
220 struct i386_float_state floatregs
;
223 /* Save the FPU state */
224 if ((pcb_t
)(per_proc_info
[cpu_number()].fpu_pcb
) == parent
->mact
.pcb
) {
225 fp_state_save(parent
);
229 if (child
->mact
.pcb
== NULL
230 || parent
->mact
.pcb
== NULL
) {
231 panic("[thread_dup, child (%x) or parent (%x) is NULL!]",
232 child
->mact
.pcb
, parent
->mact
.pcb
);
236 /* Copy over the i386_saved_state registers */
237 child
->mact
.pcb
->iss
= parent
->mact
.pcb
->iss
;
239 /* Check to see if parent is using floating point
240 * and if so, copy the registers to the child
241 * FIXME - make sure this works.
244 if (parent
->mact
.pcb
->ims
.ifps
) {
245 if (fpu_get_state(parent
, &floatregs
) == KERN_SUCCESS
)
246 fpu_set_state(child
, &floatregs
);
249 /* FIXME - should a user specified LDT, TSS and V86 info
250 * be duplicated as well?? - probably not.
255 * FIXME - thread_set_child
258 void thread_set_child(thread_act_t child
, int pid
);
260 thread_set_child(thread_act_t child
, int pid
)
262 child
->mact
.pcb
->iss
.eax
= pid
;
263 child
->mact
.pcb
->iss
.edx
= 1;
264 child
->mact
.pcb
->iss
.efl
&= ~EFL_CF
;
266 void thread_set_parent(thread_act_t parent
, int pid
);
268 thread_set_parent(thread_act_t parent
, int pid
)
270 parent
->mact
.pcb
->iss
.eax
= pid
;
271 parent
->mact
.pcb
->iss
.edx
= 0;
272 parent
->mact
.pcb
->iss
.efl
&= ~EFL_CF
;
278 * Move pages from one kernel virtual address to another.
279 * Both addresses are assumed to reside in the Sysmap,
280 * and size must be a multiple of the page size.
284 register caddr_t from
,
288 pmap_movepage((unsigned long)from
, (unsigned long)to
, (vm_size_t
)size
);
292 * System Call handling code
295 #define ERESTART -1 /* restart syscall */
296 #define EJUSTRETURN -2 /* don't modify regs, just return */
298 struct sysent
{ /* system call table */
299 unsigned short sy_narg
; /* number of args */
300 char sy_parallel
; /* can execute in parallel */
301 char sy_funnel
; /* funnel type */
302 unsigned long (*sy_call
)(void *, void *, int *); /* implementing function */
305 #define KERNEL_FUNNEL 1
306 #define NETWORK_FUNNEL 2
308 extern funnel_t
* kernel_flock
;
309 extern funnel_t
* network_flock
;
311 extern struct sysent sysent
[];
314 int set_bsduthreadargs (thread_act_t
, struct i386_saved_state
*, void *);
316 void * get_bsduthreadarg(thread_act_t
);
318 void unix_syscall(struct i386_saved_state
*);
320 /* USED ONLY FROM VFORK/EXIT */
322 unix_syscall_return(int error
)
326 struct i386_saved_state
*regs
;
328 thread
= current_act();
329 rval
= (int *)get_bsduthreadrval(thread
);
331 regs
= USER_REGS(thread
);
333 if (error
== ERESTART
) {
336 else if (error
!= EJUSTRETURN
) {
339 regs
->efl
|= EFL_CF
; /* carry bit */
340 } else { /* (not error) */
343 regs
->efl
&= ~EFL_CF
;
347 (void) thread_funnel_set(current_thread()->funnel_lock
, FALSE
);
349 thread_exception_return();
355 unix_syscall(struct i386_saved_state
*regs
)
360 struct sysent
*callp
;
367 thread
= current_act();
369 rval
= (int *)get_bsduthreadrval(thread
);
371 //printf("[scall : eax %x]", regs->eax);
373 params
= (vm_offset_t
) ((caddr_t
)regs
->uesp
+ sizeof (int));
374 callp
= (code
>= nsysent
) ? &sysent
[63] : &sysent
[code
];
375 if (callp
== sysent
) {
376 code
= fuword(params
);
377 params
+= sizeof (int);
378 callp
= (code
>= nsysent
) ? &sysent
[63] : &sysent
[code
];
381 vt
= get_bsduthreadarg(thread
);
383 if ((nargs
= (callp
->sy_narg
* sizeof (int))) &&
384 (error
= copyin((char *) params
, (char *)vt
, nargs
)) != 0) {
387 thread_exception_return();
394 if(callp
->sy_funnel
== NETWORK_FUNNEL
) {
395 (void) thread_funnel_set(network_flock
, TRUE
);
398 (void) thread_funnel_set(kernel_flock
, TRUE
);
400 set_bsduthreadargs(thread
, regs
, NULL
);
402 if (callp
->sy_narg
> 8)
403 panic("unix_syscall max arg count exceeded (%d)", callp
->sy_narg
);
406 error
= (*(callp
->sy_call
))(p
, (void *) vt
, rval
);
409 /* May be needed with vfork changes */
410 regs
= USER_REGS(thread
);
412 if (error
== ERESTART
) {
415 else if (error
!= EJUSTRETURN
) {
418 regs
->efl
|= EFL_CF
; /* carry bit */
419 } else { /* (not error) */
422 regs
->efl
&= ~EFL_CF
;
426 (void) thread_funnel_set(current_thread()->funnel_lock
, FALSE
);
428 thread_exception_return();
434 machdep_syscall( struct i386_saved_state
*regs
)
437 machdep_call_t
*entry
;
441 if (trapno
< 0 || trapno
>= machdep_call_count
) {
442 regs
->eax
= (unsigned int)kern_invalid();
444 thread_exception_return();
448 entry
= &machdep_call_table
[trapno
];
449 nargs
= entry
->nargs
;
454 if (copyin((char *) regs
->uesp
+ sizeof (int),
456 nargs
* sizeof (int))) {
458 regs
->eax
= KERN_INVALID_ADDRESS
;
460 thread_exception_return();
477 "r" (&args
[nargs
- 1]),
479 : "ax", "cx", "dx", "sp");
482 regs
->eax
= (unsigned int)(*entry
->routine
)();
484 (void) thread_funnel_set(current_thread()->funnel_lock
, FALSE
);
486 thread_exception_return();
492 thread_set_cthread_self(int self
)
494 current_act()->mact
.pcb
->cthread_self
= (unsigned int)self
;
496 return (KERN_SUCCESS
);
500 thread_get_cthread_self(void)
502 return ((kern_return_t
)current_act()->mact
.pcb
->cthread_self
);
506 mach25_syscall(struct i386_saved_state
*regs
)
508 printf("*** Atttempt to execute a Mach 2.5 system call at EIP=%x EAX=%x(%d)\n",
509 regs
->eip
, regs
->eax
, -regs
->eax
);
513 #endif /* MACH_BSD */
515 #undef current_thread
519 return(current_thread_fast());