1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19: #include <mach.h>
20: #include <thread_state.h>
21: #include <string.h>
22: #include <mach/machine/vm_param.h>
23: #include "sysdep.h"
24:
25: #define STACK_SIZE (16 * 1024 * 1024)
26:
27:
28:
29:
30:
31:
32:
33:
34:
35: kern_return_t
36: __mach_setup_thread (task_t task, thread_t thread, void *pc,
37: vm_address_t *stack_base, vm_size_t *stack_size)
38: {
39: kern_return_t error;
40: struct machine_thread_state ts;
41: mach_msg_type_number_t tssize = MACHINE_THREAD_STATE_COUNT;
42: vm_address_t stack;
43: vm_size_t size;
44: int anywhere;
45:
46: size = stack_size ? *stack_size ? : STACK_SIZE : STACK_SIZE;
47: stack = stack_base ? *stack_base ? : 0 : 0;
48: anywhere = !stack_base || !*stack_base;
49:
50: error = __vm_allocate (task, &stack, size + __vm_page_size, anywhere);
51: if (error)
52: return error;
53:
54: if (stack_size)
55: *stack_size = size;
56:
57: memset (&ts, 0, sizeof (ts));
58: MACHINE_THREAD_STATE_SET_PC (&ts, pc);
59: #ifdef STACK_GROWTH_DOWN
60: if (stack_base)
61: *stack_base = stack + __vm_page_size;
62: ts.SP = stack + __vm_page_size + size;
63: #elif defined (STACK_GROWTH_UP)
64: if (stack_base)
65: *stack_base = stack;
66: ts.SP = stack;
67: stack += size;
68: #else
69: #error stack direction unknown
70: #endif
71:
72:
73: if (error = __vm_protect (task, stack, __vm_page_size, 0, VM_PROT_NONE))
74: return error;
75:
76: return __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
77: (natural_t *) &ts, tssize);
78: }
79:
80: weak_alias (__mach_setup_thread, mach_setup_thread)