]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
734aad71 A |
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. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
e9ce8d39 A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
734aad71 A |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
e9ce8d39 A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | /* | |
24 | * Mach Operating System | |
25 | * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University | |
26 | * All Rights Reserved. | |
27 | * | |
28 | * Permission to use, copy, modify and distribute this software and its | |
29 | * documentation is hereby granted, provided that both the copyright | |
30 | * notice and this permission notice appear in all copies of the | |
31 | * software, derivative works or modified versions, and any portions | |
32 | * thereof, and that both notices appear in supporting documentation. | |
33 | * | |
34 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
35 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
36 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
37 | * | |
38 | * Carnegie Mellon requests users of this software to return to | |
39 | * | |
40 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
41 | * School of Computer Science | |
42 | * Carnegie Mellon University | |
43 | * Pittsburgh PA 15213-3890 | |
44 | * | |
45 | * any improvements or extensions that they make and grant Carnegie Mellon | |
46 | * the rights to redistribute these changes. | |
47 | */ | |
48 | ||
49 | #include <mach/mach.h> | |
50 | #include <mach/boolean.h> | |
51 | #include <mach/machine/ndr_def.h> | |
52 | #include <mach/mach_traps.h> | |
53 | #include <mach/mach_host.h> | |
54 | #include <mach/mach_init.h> | |
55 | #include "externs.h" | |
56 | ||
57 | mach_port_t mach_task_self_ = MACH_PORT_NULL; | |
58 | mach_port_t mach_host_self_ = MACH_PORT_NULL; | |
5b2abdfb A |
59 | |
60 | __private_extern__ kern_return_t _host_mach_msg_trap_return_; | |
e9ce8d39 A |
61 | |
62 | vm_size_t vm_page_size; | |
59e0d9fe A |
63 | vm_size_t vm_page_mask; |
64 | int vm_page_shift; | |
e9ce8d39 A |
65 | |
66 | /* | |
67 | * Forward internal declarations for automatic mach_init during | |
68 | * fork() implementation. | |
69 | */ | |
70 | /* fork() calls through atfork_child_routine */ | |
71 | void (*_atfork_child_routine)(void); | |
72 | ||
73 | static void mach_atfork_child_routine(void); | |
74 | static boolean_t first = TRUE; | |
75 | static void (*previous_atfork_child_routine)(void); | |
3d9156a7 | 76 | static boolean_t mach_init_inited = FALSE; |
e9ce8d39 A |
77 | extern int mach_init(void); |
78 | extern void _pthread_set_self(void *); | |
79 | extern void cthread_set_self(void *); | |
3d9156a7 | 80 | extern void other_libc_init(void); |
e9ce8d39 A |
81 | |
82 | ||
83 | static void mach_atfork_child_routine(void) | |
84 | { | |
85 | /* | |
86 | * If an (*_atfork_child_routine)() was registered when | |
87 | * mach_init was first called, then call that routine | |
88 | * prior to performing our re-initialization. This ensures | |
89 | * that the post-fork handlers are called in exactly the | |
90 | * same order as the crt0 (exec) handlers. Any library | |
91 | * that makes use of the _atfork_child_routine must follow | |
92 | * the same technique. | |
93 | */ | |
94 | if (previous_atfork_child_routine) { | |
95 | (*previous_atfork_child_routine)(); | |
96 | } | |
3d9156a7 | 97 | mach_init_inited = FALSE; |
e9ce8d39 A |
98 | mach_init(); |
99 | } | |
100 | ||
101 | mach_port_t | |
102 | mach_host_self() | |
103 | { | |
104 | return(host_self_trap()); | |
105 | } | |
106 | ||
107 | int mach_init_doit(int forkchild) | |
108 | { | |
5b2abdfb | 109 | host_t host; |
e9ce8d39 A |
110 | |
111 | /* | |
112 | * Get the important ports into the cached values, | |
113 | * as required by "mach_init.h". | |
114 | */ | |
115 | ||
116 | mach_task_self_ = task_self_trap(); | |
5b2abdfb | 117 | host = host_self_trap(); |
e9ce8d39 A |
118 | |
119 | ||
120 | if (!forkchild) { | |
121 | /* | |
122 | * Set up the post-fork child handler in the libc stub | |
123 | * to invoke this routine if this process forks. Save the | |
124 | * previous value in order that we can call that handler | |
125 | * prior to performing our postfork work. | |
126 | */ | |
127 | ||
128 | first = FALSE; | |
129 | previous_atfork_child_routine = _atfork_child_routine; | |
130 | _atfork_child_routine = mach_atfork_child_routine; | |
131 | _pthread_set_self(0); | |
132 | cthread_set_self(0); | |
3b2a1fe8 | 133 | } |
e9ce8d39 A |
134 | |
135 | /* | |
136 | * Initialize the single mig reply port | |
137 | */ | |
138 | ||
139 | mig_init(0); | |
140 | ||
141 | /* | |
142 | * Cache some other valuable system constants | |
143 | */ | |
5b2abdfb | 144 | |
59e0d9fe A |
145 | #if defined(__ppc64__) /* NGK hack for now */ |
146 | vm_page_size = 4096; | |
147 | #else | |
5b2abdfb | 148 | (void)host_page_size(host, &vm_page_size); |
59e0d9fe A |
149 | #endif |
150 | vm_page_mask = vm_page_size - 1; | |
151 | if (vm_page_size == 0) { | |
152 | /* guard against unlikely craziness */ | |
153 | vm_page_shift = 0; | |
154 | } else { | |
155 | /* | |
156 | * Unfortunately there's no kernel interface to get the | |
157 | * vm_page_shift, but it's easy enough to calculate. | |
158 | */ | |
159 | for (vm_page_shift = 0; | |
160 | (vm_page_size & (1 << vm_page_shift)) == 0; | |
161 | vm_page_shift++) | |
162 | continue; | |
163 | } | |
e9ce8d39 | 164 | |
5b2abdfb | 165 | mach_port_deallocate(mach_task_self_, host); |
e9ce8d39 A |
166 | |
167 | mach_init_ports(); | |
168 | ||
169 | #if WE_REALLY_NEED_THIS_GDB_HACK | |
170 | /* | |
171 | * Check to see if GDB wants us to stop | |
172 | */ | |
173 | { | |
174 | task_user_data_data_t user_data; | |
175 | mach_msg_type_number_t user_data_count = TASK_USER_DATA_COUNT; | |
176 | ||
177 | user_data.user_data = 0; | |
178 | (void)task_info(mach_task_self_, TASK_USER_DATA, | |
179 | (task_info_t)&user_data, &user_data_count); | |
180 | #define MACH_GDB_RUN_MAGIC_NUMBER 1 | |
181 | #ifdef MACH_GDB_RUN_MAGIC_NUMBER | |
182 | /* This magic number is set in mach-aware gdb | |
183 | * for RUN command to allow us to suspend user's | |
184 | * executable (linked with this libmach!) | |
185 | * with the code below. | |
186 | * This hack should disappear when gdb improves. | |
187 | */ | |
188 | if ((int)user_data.user_data == MACH_GDB_RUN_MAGIC_NUMBER) { | |
189 | kern_return_t ret; | |
190 | user_data.user_data = 0; | |
191 | ||
192 | ret = task_suspend (mach_task_self_); | |
193 | if (ret != KERN_SUCCESS) { | |
194 | while(1) (void)task_terminate(mach_task_self_); | |
195 | } | |
196 | } | |
197 | #undef MACH_GDB_RUN_MAGIC_NUMBER | |
198 | #endif /* MACH_GDB_RUN_MAGIC_NUMBER */ | |
199 | } | |
200 | #endif /* WE_REALLY_NEED_THIS_GDB_HACK */ | |
201 | ||
202 | /* | |
203 | * Reserve page 0 so that the program doesn't get it as | |
204 | * the result of a vm_allocate() or whatever. | |
205 | */ | |
206 | { | |
207 | vm_offset_t zero_page_start; | |
208 | ||
209 | zero_page_start = 0; | |
210 | (void)vm_map(mach_task_self_, &zero_page_start, vm_page_size, | |
211 | 0, FALSE, MEMORY_OBJECT_NULL, 0, TRUE, | |
212 | VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY); | |
213 | /* ignore result, we don't care if it failed */ | |
214 | } | |
3d9156a7 | 215 | |
e9ce8d39 A |
216 | return(0); |
217 | } | |
218 | ||
3d9156a7 A |
219 | #ifdef __DYNAMIC__ |
220 | /* libc_initializer is the dyld initializer for libc (3760827) */ | |
221 | static void libc_initializer() __attribute__((constructor)); | |
222 | static void | |
223 | libc_initializer() | |
224 | { | |
225 | mach_init(); | |
226 | } | |
227 | #endif /* __DYNAMIC__ */ | |
228 | ||
229 | /* mach_init may get called from the initializer and from crt.c, but only | |
230 | * call mach_init_doit() once */ | |
e9ce8d39 A |
231 | int mach_init(void) |
232 | { | |
3d9156a7 A |
233 | int ret; |
234 | ||
235 | if (mach_init_inited) | |
236 | return(0); | |
237 | mach_init_inited = TRUE; | |
238 | ret = mach_init_doit(0); | |
239 | ||
240 | /* Do other Libc initialization */ | |
241 | other_libc_init(); | |
242 | ||
243 | return ret; | |
e9ce8d39 A |
244 | } |
245 | ||
246 | int (*mach_init_routine)(void) = mach_init; | |
247 | int fork_mach_init() | |
248 | { | |
249 | /* called only from child */ | |
250 | return(mach_init_doit(1)); | |
251 | } | |
252 | ||
253 | #undef mach_task_self | |
254 | ||
255 | mach_port_t | |
256 | mach_task_self() | |
257 | { | |
3b2a1fe8 | 258 | return(task_self_trap()); |
e9ce8d39 A |
259 | } |
260 | ||
261 | mach_port_t | |
262 | mach_thread_self() | |
263 | { | |
264 | return(thread_self_trap()); | |
3b2a1fe8 | 265 | } |