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