]> git.saurik.com Git - apple/libc.git/blob - mach/mach_init.c
518c64a4982252b3f5bb444fbe2aa926ec15b102
[apple/libc.git] / mach / mach_init.c
1 /*
2 * Copyright (c) 1999 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 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50
51 #include <mach/mach.h>
52 #include <mach/boolean.h>
53 #include <mach/machine/ndr_def.h>
54 #include <mach/mach_traps.h>
55 #include <mach/mach_host.h>
56 #include <mach/mach_init.h>
57 #include "externs.h"
58
59 mach_port_t mach_task_self_ = MACH_PORT_NULL;
60 mach_port_t mach_host_self_ = MACH_PORT_NULL;
61
62 __private_extern__ kern_return_t _host_mach_msg_trap_return_;
63
64 vm_size_t vm_page_size;
65 vm_size_t vm_page_mask;
66 int vm_page_shift;
67
68 /*
69 * Forward internal declarations for automatic mach_init during
70 * fork() implementation.
71 */
72 /* fork() calls through atfork_child_routine */
73 void (*_atfork_child_routine)(void);
74
75 static void mach_atfork_child_routine(void);
76 static boolean_t first = TRUE;
77 static void (*previous_atfork_child_routine)(void);
78 extern int mach_init(void);
79 extern void _pthread_set_self(void *);
80 extern void cthread_set_self(void *);
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 }
97 mach_init();
98 }
99
100 mach_port_t
101 mach_host_self()
102 {
103 return(host_self_trap());
104 }
105
106 int mach_init_doit(int forkchild)
107 {
108 host_t host;
109
110 /*
111 * Get the important ports into the cached values,
112 * as required by "mach_init.h".
113 */
114
115 mach_task_self_ = task_self_trap();
116 host = host_self_trap();
117
118
119 if (!forkchild) {
120 /*
121 * Set up the post-fork child handler in the libc stub
122 * to invoke this routine if this process forks. Save the
123 * previous value in order that we can call that handler
124 * prior to performing our postfork work.
125 */
126
127 first = FALSE;
128 previous_atfork_child_routine = _atfork_child_routine;
129 _atfork_child_routine = mach_atfork_child_routine;
130 _pthread_set_self(0);
131 cthread_set_self(0);
132 }
133
134 /*
135 * Initialize the single mig reply port
136 */
137
138 mig_init(0);
139
140 /*
141 * Cache some other valuable system constants
142 */
143
144 #if defined(__ppc64__) /* NGK hack for now */
145 vm_page_size = 4096;
146 #else
147 (void)host_page_size(host, &vm_page_size);
148 #endif
149 vm_page_mask = vm_page_size - 1;
150 if (vm_page_size == 0) {
151 /* guard against unlikely craziness */
152 vm_page_shift = 0;
153 } else {
154 /*
155 * Unfortunately there's no kernel interface to get the
156 * vm_page_shift, but it's easy enough to calculate.
157 */
158 for (vm_page_shift = 0;
159 (vm_page_size & (1 << vm_page_shift)) == 0;
160 vm_page_shift++)
161 continue;
162 }
163
164 mach_port_deallocate(mach_task_self_, host);
165
166 mach_init_ports();
167
168 #if WE_REALLY_NEED_THIS_GDB_HACK
169 /*
170 * Check to see if GDB wants us to stop
171 */
172 {
173 task_user_data_data_t user_data;
174 mach_msg_type_number_t user_data_count = TASK_USER_DATA_COUNT;
175
176 user_data.user_data = 0;
177 (void)task_info(mach_task_self_, TASK_USER_DATA,
178 (task_info_t)&user_data, &user_data_count);
179 #define MACH_GDB_RUN_MAGIC_NUMBER 1
180 #ifdef MACH_GDB_RUN_MAGIC_NUMBER
181 /* This magic number is set in mach-aware gdb
182 * for RUN command to allow us to suspend user's
183 * executable (linked with this libmach!)
184 * with the code below.
185 * This hack should disappear when gdb improves.
186 */
187 if ((int)user_data.user_data == MACH_GDB_RUN_MAGIC_NUMBER) {
188 kern_return_t ret;
189 user_data.user_data = 0;
190
191 ret = task_suspend (mach_task_self_);
192 if (ret != KERN_SUCCESS) {
193 while(1) (void)task_terminate(mach_task_self_);
194 }
195 }
196 #undef MACH_GDB_RUN_MAGIC_NUMBER
197 #endif /* MACH_GDB_RUN_MAGIC_NUMBER */
198 }
199 #endif /* WE_REALLY_NEED_THIS_GDB_HACK */
200
201 /*
202 * Reserve page 0 so that the program doesn't get it as
203 * the result of a vm_allocate() or whatever.
204 */
205 {
206 vm_offset_t zero_page_start;
207
208 zero_page_start = 0;
209 (void)vm_map(mach_task_self_, &zero_page_start, vm_page_size,
210 0, FALSE, MEMORY_OBJECT_NULL, 0, TRUE,
211 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
212 /* ignore result, we don't care if it failed */
213 }
214 return(0);
215 }
216
217 int mach_init(void)
218 {
219 return(mach_init_doit(0));
220 }
221
222 int (*mach_init_routine)(void) = mach_init;
223 int fork_mach_init()
224 {
225 /* called only from child */
226 return(mach_init_doit(1));
227 }
228
229 #undef mach_task_self
230
231 mach_port_t
232 mach_task_self()
233 {
234 return(task_self_trap());
235 }
236
237 mach_port_t
238 mach_thread_self()
239 {
240 return(thread_self_trap());
241 }