]> git.saurik.com Git - apple/libc.git/blob - mach/mach_init.c
3b8a352c95f73c44091c71d669f8b14f0e5a4545
[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 * 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
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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;
59
60 __private_extern__ kern_return_t _host_mach_msg_trap_return_;
61
62 vm_size_t vm_page_size;
63
64 /*
65 * Forward internal declarations for automatic mach_init during
66 * fork() implementation.
67 */
68 /* fork() calls through atfork_child_routine */
69 void (*_atfork_child_routine)(void);
70
71 static void mach_atfork_child_routine(void);
72 static boolean_t first = TRUE;
73 static void (*previous_atfork_child_routine)(void);
74 extern int mach_init(void);
75 extern void _pthread_set_self(void *);
76 extern void cthread_set_self(void *);
77
78
79 static void mach_atfork_child_routine(void)
80 {
81 /*
82 * If an (*_atfork_child_routine)() was registered when
83 * mach_init was first called, then call that routine
84 * prior to performing our re-initialization. This ensures
85 * that the post-fork handlers are called in exactly the
86 * same order as the crt0 (exec) handlers. Any library
87 * that makes use of the _atfork_child_routine must follow
88 * the same technique.
89 */
90 if (previous_atfork_child_routine) {
91 (*previous_atfork_child_routine)();
92 }
93 mach_init();
94 }
95
96 mach_port_t
97 mach_host_self()
98 {
99 return(host_self_trap());
100 }
101
102 int mach_init_doit(int forkchild)
103 {
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 (void)host_page_size(host, &vm_page_size);
141
142 mach_port_deallocate(mach_task_self_, host);
143
144 mach_init_ports();
145
146 #if WE_REALLY_NEED_THIS_GDB_HACK
147 /*
148 * Check to see if GDB wants us to stop
149 */
150 {
151 task_user_data_data_t user_data;
152 mach_msg_type_number_t user_data_count = TASK_USER_DATA_COUNT;
153
154 user_data.user_data = 0;
155 (void)task_info(mach_task_self_, TASK_USER_DATA,
156 (task_info_t)&user_data, &user_data_count);
157 #define MACH_GDB_RUN_MAGIC_NUMBER 1
158 #ifdef MACH_GDB_RUN_MAGIC_NUMBER
159 /* This magic number is set in mach-aware gdb
160 * for RUN command to allow us to suspend user's
161 * executable (linked with this libmach!)
162 * with the code below.
163 * This hack should disappear when gdb improves.
164 */
165 if ((int)user_data.user_data == MACH_GDB_RUN_MAGIC_NUMBER) {
166 kern_return_t ret;
167 user_data.user_data = 0;
168
169 ret = task_suspend (mach_task_self_);
170 if (ret != KERN_SUCCESS) {
171 while(1) (void)task_terminate(mach_task_self_);
172 }
173 }
174 #undef MACH_GDB_RUN_MAGIC_NUMBER
175 #endif /* MACH_GDB_RUN_MAGIC_NUMBER */
176 }
177 #endif /* WE_REALLY_NEED_THIS_GDB_HACK */
178
179 /*
180 * Reserve page 0 so that the program doesn't get it as
181 * the result of a vm_allocate() or whatever.
182 */
183 {
184 vm_offset_t zero_page_start;
185
186 zero_page_start = 0;
187 (void)vm_map(mach_task_self_, &zero_page_start, vm_page_size,
188 0, FALSE, MEMORY_OBJECT_NULL, 0, TRUE,
189 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
190 /* ignore result, we don't care if it failed */
191 }
192 return(0);
193 }
194
195 int mach_init(void)
196 {
197 return(mach_init_doit(0));
198 }
199
200 int (*mach_init_routine)(void) = mach_init;
201 int fork_mach_init()
202 {
203 /* called only from child */
204 return(mach_init_doit(1));
205 }
206
207 #undef mach_task_self
208
209 mach_port_t
210 mach_task_self()
211 {
212 return(task_self_trap());
213 }
214
215 mach_port_t
216 mach_thread_self()
217 {
218 return(thread_self_trap());
219 }