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
77 struct i386_saved_state
*
87 unsigned int get_msr_exportmask(void);
89 unsigned int get_msr_nbits(void);
91 unsigned int get_msr_rbits(void);
96 * Return the user stack pointer from the machine
97 * dependent thread state info.
103 thread_state_t tstate
,
105 vm_offset_t
*user_stack
108 struct i386_saved_state
*state
;
109 i386_thread_state_t
*state25
;
115 if (*user_stack
== 0)
116 *user_stack
= USRSTACK
;
119 case i386_THREAD_STATE
: /* FIXME */
120 state25
= (i386_thread_state_t
*) tstate
;
121 *user_stack
= state25
->esp
? state25
->esp
: USRSTACK
;
124 case i386_NEW_THREAD_STATE
:
125 if (count
< i386_NEW_THREAD_STATE_COUNT
)
126 return (KERN_INVALID_ARGUMENT
);
128 state
= (struct i386_saved_state
*) tstate
;
133 * If a valid user stack is specified, use it.
135 *user_stack
= uesp
? uesp
: USRSTACK
;
138 return (KERN_INVALID_ARGUMENT
);
141 return (KERN_SUCCESS
);
148 thread_state_t tstate
,
150 vm_offset_t
*entry_point
153 struct i386_saved_state
*state
;
154 i386_thread_state_t
*state25
;
159 if (*entry_point
== 0)
160 *entry_point
= VM_MIN_ADDRESS
;
163 case i386_THREAD_STATE
:
164 state25
= (i386_thread_state_t
*) tstate
;
165 *entry_point
= state25
->eip
? state25
->eip
: VM_MIN_ADDRESS
;
168 case i386_NEW_THREAD_STATE
:
169 if (count
< i386_THREAD_STATE_COUNT
)
170 return (KERN_INVALID_ARGUMENT
);
172 state
= (struct i386_saved_state
*) tstate
;
175 * If a valid entry point is specified, use it.
177 *entry_point
= state
->eip
? state
->eip
: VM_MIN_ADDRESS
;
182 return (KERN_SUCCESS
);
185 struct i386_saved_state
*
186 get_user_regs(thread_act_t th
)
189 return(USER_REGS(th
));
191 printf("[get_user_regs: thread does not have pcb]");
197 * Duplicate parent state in child
206 struct i386_saved_state
*parent_state
, *child_state
;
207 struct i386_machine_state
*ims
;
208 struct i386_float_state floatregs
;
211 /* Save the FPU state */
212 if ((pcb_t
)(per_proc_info
[cpu_number()].fpu_pcb
) == parent
->mact
.pcb
) {
213 fp_state_save(parent
);
217 if (child
->mact
.pcb
== NULL
218 || parent
->mact
.pcb
== NULL
) {
219 panic("[thread_dup, child (%x) or parent (%x) is NULL!]",
220 child
->mact
.pcb
, parent
->mact
.pcb
);
224 /* Copy over the i386_saved_state registers */
225 child
->mact
.pcb
->iss
= parent
->mact
.pcb
->iss
;
227 /* Check to see if parent is using floating point
228 * and if so, copy the registers to the child
229 * FIXME - make sure this works.
232 if (parent
->mact
.pcb
->ims
.ifps
) {
233 if (fpu_get_state(parent
, &floatregs
) == KERN_SUCCESS
)
234 fpu_set_state(child
, &floatregs
);
237 /* FIXME - should a user specified LDT, TSS and V86 info
238 * be duplicated as well?? - probably not.
243 * FIXME - thread_set_child
246 void thread_set_child(thread_act_t child
, int pid
);
248 thread_set_child(thread_act_t child
, int pid
)
250 child
->mact
.pcb
->iss
.eax
= pid
;
251 child
->mact
.pcb
->iss
.edx
= 1;
252 child
->mact
.pcb
->iss
.efl
&= ~EFL_CF
;
258 * Move pages from one kernel virtual address to another.
259 * Both addresses are assumed to reside in the Sysmap,
260 * and size must be a multiple of the page size.
264 register caddr_t from
,
268 pmap_movepage((unsigned long)from
, (unsigned long)to
, (vm_size_t
)size
);
272 * System Call handling code
275 #define ERESTART -1 /* restart syscall */
276 #define EJUSTRETURN -2 /* don't modify regs, just return */
278 struct sysent
{ /* system call table */
279 unsigned short sy_narg
; /* number of args */
280 char sy_parallel
; /* can execute in parallel */
281 char sy_funnel
; /* funnel type */
282 unsigned long (*sy_call
)(void *, void *, int *); /* implementing function */
285 #define KERNEL_FUNNEL 1
286 #define NETWORK_FUNNEL 2
288 extern funnel_t
* kernel_flock
;
289 extern funnel_t
* network_flock
;
291 extern struct sysent sysent
[];
293 void *get_bsdtask_info(
296 int set_bsduthreadargs (thread_act_t
, struct i386_saved_state
*, void *);
298 void * get_bsduthreadarg(thread_act_t
);
300 void unix_syscall(struct i386_saved_state
*);
303 unix_syscall_return(int error
)
305 panic("unix_syscall_return not implemented yet!!");
310 unix_syscall(struct i386_saved_state
*regs
)
315 struct sysent
*callp
;
322 thread
= current_act();
323 p
= get_bsdtask_info(current_task());
324 rval
= (int *)get_bsduthreadrval(thread
);
326 //printf("[scall : eax %x]", regs->eax);
328 params
= (vm_offset_t
) ((caddr_t
)regs
->uesp
+ sizeof (int));
329 callp
= (code
>= nsysent
) ? &sysent
[63] : &sysent
[code
];
330 if (callp
== sysent
) {
331 code
= fuword(params
);
332 params
+= sizeof (int);
333 callp
= (code
>= nsysent
) ? &sysent
[63] : &sysent
[code
];
336 vt
= get_bsduthreadarg(thread
);
338 if ((nargs
= (callp
->sy_narg
* sizeof (int))) &&
339 (error
= copyin((char *) params
, (char *)vt
, nargs
)) != 0) {
342 thread_exception_return();
349 if(callp
->sy_funnel
== NETWORK_FUNNEL
) {
350 (void) thread_funnel_set(network_flock
, TRUE
);
353 (void) thread_funnel_set(kernel_flock
, TRUE
);
355 set_bsduthreadargs(thread
, regs
, NULL
);
357 if (callp
->sy_narg
> 8)
358 panic("unix_syscall max arg count exceeded (%d)", callp
->sy_narg
);
361 error
= (*(callp
->sy_call
))(p
, (void *) vt
, rval
);
363 if (error
== ERESTART
) {
366 else if (error
!= EJUSTRETURN
) {
369 regs
->efl
|= EFL_CF
; /* carry bit */
370 } else { /* (not error) */
373 regs
->efl
&= ~EFL_CF
;
377 (void) thread_funnel_set(current_thread()->funnel_lock
, FALSE
);
379 thread_exception_return();
385 machdep_syscall( struct i386_saved_state
*regs
)
388 machdep_call_t
*entry
;
392 if (trapno
< 0 || trapno
>= machdep_call_count
) {
393 regs
->eax
= (unsigned int)kern_invalid();
395 thread_exception_return();
399 entry
= &machdep_call_table
[trapno
];
400 nargs
= entry
->nargs
;
405 if (copyin((char *) regs
->uesp
+ sizeof (int),
407 nargs
* sizeof (int))) {
409 regs
->eax
= KERN_INVALID_ADDRESS
;
411 thread_exception_return();
428 "r" (&args
[nargs
- 1]),
430 : "ax", "cx", "dx", "sp");
433 regs
->eax
= (unsigned int)(*entry
->routine
)();
435 (void) thread_funnel_set(current_thread()->funnel_lock
, FALSE
);
437 thread_exception_return();
443 thread_set_cthread_self(int self
)
445 current_act()->mact
.pcb
->cthread_self
= (unsigned int)self
;
447 return (KERN_SUCCESS
);
451 thread_get_cthread_self(void)
453 return ((kern_return_t
)current_act()->mact
.pcb
->cthread_self
);
457 mach25_syscall(struct i386_saved_state
*regs
)
459 printf("*** Atttempt to execute a Mach 2.5 system call at EIP=%x EAX=%x(%d)\n",
460 regs
->eip
, regs
->eax
, -regs
->eax
);
464 #endif /* MACH_BSD */
466 #undef current_thread
470 return(current_thread_fast());