]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
734aad71 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
e9ce8d39 | 7 | * |
734aad71 A |
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 | |
e9ce8d39 A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
734aad71 A |
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. | |
e9ce8d39 A |
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; | |
5b2abdfb A |
61 | |
62 | __private_extern__ kern_return_t _host_mach_msg_trap_return_; | |
e9ce8d39 A |
63 | |
64 | vm_size_t vm_page_size; | |
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); | |
76 | extern int mach_init(void); | |
77 | extern void _pthread_set_self(void *); | |
78 | extern void cthread_set_self(void *); | |
79 | ||
80 | ||
81 | static void mach_atfork_child_routine(void) | |
82 | { | |
83 | /* | |
84 | * If an (*_atfork_child_routine)() was registered when | |
85 | * mach_init was first called, then call that routine | |
86 | * prior to performing our re-initialization. This ensures | |
87 | * that the post-fork handlers are called in exactly the | |
88 | * same order as the crt0 (exec) handlers. Any library | |
89 | * that makes use of the _atfork_child_routine must follow | |
90 | * the same technique. | |
91 | */ | |
92 | if (previous_atfork_child_routine) { | |
93 | (*previous_atfork_child_routine)(); | |
94 | } | |
95 | mach_init(); | |
96 | } | |
97 | ||
98 | mach_port_t | |
99 | mach_host_self() | |
100 | { | |
101 | return(host_self_trap()); | |
102 | } | |
103 | ||
104 | int mach_init_doit(int forkchild) | |
105 | { | |
5b2abdfb A |
106 | mach_msg_type_number_t host_info_size; |
107 | host_t host; | |
e9ce8d39 A |
108 | |
109 | /* | |
110 | * Get the important ports into the cached values, | |
111 | * as required by "mach_init.h". | |
112 | */ | |
113 | ||
114 | mach_task_self_ = task_self_trap(); | |
5b2abdfb | 115 | host = host_self_trap(); |
e9ce8d39 A |
116 | |
117 | ||
118 | if (!forkchild) { | |
119 | /* | |
120 | * Set up the post-fork child handler in the libc stub | |
121 | * to invoke this routine if this process forks. Save the | |
122 | * previous value in order that we can call that handler | |
123 | * prior to performing our postfork work. | |
124 | */ | |
125 | ||
126 | first = FALSE; | |
127 | previous_atfork_child_routine = _atfork_child_routine; | |
128 | _atfork_child_routine = mach_atfork_child_routine; | |
129 | _pthread_set_self(0); | |
130 | cthread_set_self(0); | |
3b2a1fe8 | 131 | } |
e9ce8d39 A |
132 | |
133 | /* | |
134 | * Initialize the single mig reply port | |
135 | */ | |
136 | ||
137 | mig_init(0); | |
138 | ||
139 | /* | |
140 | * Cache some other valuable system constants | |
141 | */ | |
5b2abdfb A |
142 | |
143 | #ifdef HOST_MACH_MSG_TRAP | |
144 | host_info_size = 0; | |
145 | _host_mach_msg_trap_return_ = host_info(host, | |
146 | HOST_MACH_MSG_TRAP, | |
e9ce8d39 A |
147 | 0, |
148 | &host_info_size); | |
5b2abdfb A |
149 | #endif |
150 | ||
151 | (void)host_page_size(host, &vm_page_size); | |
e9ce8d39 | 152 | |
5b2abdfb | 153 | mach_port_deallocate(mach_task_self_, host); |
e9ce8d39 A |
154 | |
155 | mach_init_ports(); | |
156 | ||
157 | #if WE_REALLY_NEED_THIS_GDB_HACK | |
158 | /* | |
159 | * Check to see if GDB wants us to stop | |
160 | */ | |
161 | { | |
162 | task_user_data_data_t user_data; | |
163 | mach_msg_type_number_t user_data_count = TASK_USER_DATA_COUNT; | |
164 | ||
165 | user_data.user_data = 0; | |
166 | (void)task_info(mach_task_self_, TASK_USER_DATA, | |
167 | (task_info_t)&user_data, &user_data_count); | |
168 | #define MACH_GDB_RUN_MAGIC_NUMBER 1 | |
169 | #ifdef MACH_GDB_RUN_MAGIC_NUMBER | |
170 | /* This magic number is set in mach-aware gdb | |
171 | * for RUN command to allow us to suspend user's | |
172 | * executable (linked with this libmach!) | |
173 | * with the code below. | |
174 | * This hack should disappear when gdb improves. | |
175 | */ | |
176 | if ((int)user_data.user_data == MACH_GDB_RUN_MAGIC_NUMBER) { | |
177 | kern_return_t ret; | |
178 | user_data.user_data = 0; | |
179 | ||
180 | ret = task_suspend (mach_task_self_); | |
181 | if (ret != KERN_SUCCESS) { | |
182 | while(1) (void)task_terminate(mach_task_self_); | |
183 | } | |
184 | } | |
185 | #undef MACH_GDB_RUN_MAGIC_NUMBER | |
186 | #endif /* MACH_GDB_RUN_MAGIC_NUMBER */ | |
187 | } | |
188 | #endif /* WE_REALLY_NEED_THIS_GDB_HACK */ | |
189 | ||
190 | /* | |
191 | * Reserve page 0 so that the program doesn't get it as | |
192 | * the result of a vm_allocate() or whatever. | |
193 | */ | |
194 | { | |
195 | vm_offset_t zero_page_start; | |
196 | ||
197 | zero_page_start = 0; | |
198 | (void)vm_map(mach_task_self_, &zero_page_start, vm_page_size, | |
199 | 0, FALSE, MEMORY_OBJECT_NULL, 0, TRUE, | |
200 | VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY); | |
201 | /* ignore result, we don't care if it failed */ | |
202 | } | |
203 | return(0); | |
204 | } | |
205 | ||
206 | int mach_init(void) | |
207 | { | |
208 | return(mach_init_doit(0)); | |
209 | } | |
210 | ||
211 | int (*mach_init_routine)(void) = mach_init; | |
212 | int fork_mach_init() | |
213 | { | |
214 | /* called only from child */ | |
215 | return(mach_init_doit(1)); | |
216 | } | |
217 | ||
218 | #undef mach_task_self | |
219 | ||
220 | mach_port_t | |
221 | mach_task_self() | |
222 | { | |
3b2a1fe8 | 223 | return(task_self_trap()); |
e9ce8d39 A |
224 | } |
225 | ||
226 | mach_port_t | |
227 | mach_thread_self() | |
228 | { | |
229 | return(thread_self_trap()); | |
3b2a1fe8 | 230 | } |