]> git.saurik.com Git - apple/xnu.git/blame - libsyscall/mach/mach_init.c
xnu-2050.7.9.tar.gz
[apple/xnu.git] / libsyscall / mach / mach_init.c
CommitLineData
2d21ac55 1/*
6d2010ae 2 * Copyright (c) 1999-2010 Apple Inc. All rights reserved.
2d21ac55
A
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53
54#include <mach/mach.h>
55#include <mach/boolean.h>
56#include <mach/machine/ndr_def.h>
57#include <mach/mach_traps.h>
58#include <mach/mach_host.h>
59#include <mach/mach_init.h>
60#include <mach/vm_param.h>
6d2010ae 61#include <stdbool.h>
2d21ac55 62#include "externs.h"
6d2010ae 63#include "mig_reply_port.h"
2d21ac55
A
64
65mach_port_t mach_task_self_ = MACH_PORT_NULL;
6d2010ae
A
66#ifdef __i386__
67mach_port_t mach_host_self_ = MACH_PORT_NULL;
68#endif
2d21ac55 69
6d2010ae
A
70vm_size_t vm_page_size = PAGE_SIZE;
71vm_size_t vm_page_mask = PAGE_MASK;
72int vm_page_shift = PAGE_SHIFT;
2d21ac55 73
6d2010ae
A
74int mach_init(void);
75int _mach_fork_child(void);
2d21ac55 76
6d2010ae 77static int mach_init_doit(bool forkchild);
2d21ac55 78
2d21ac55
A
79extern void _pthread_set_self(void *);
80extern void cthread_set_self(void *);
2d21ac55
A
81
82kern_return_t
83host_page_size(__unused host_t host, vm_size_t *out_page_size)
84{
85 *out_page_size = PAGE_SIZE;
86 return KERN_SUCCESS;
87}
88
6d2010ae
A
89/*
90 * mach_init() must be called explicitly in static executables (including dyld).
91 * called by libSystem_initializer() in dynamic executables
92 */
93int
94mach_init(void)
2d21ac55 95{
6d2010ae
A
96 static bool mach_init_inited = false;
97
98 if (mach_init_inited) {
99 return 0;
2d21ac55 100 }
6d2010ae
A
101 mach_init_inited = true;
102
103 return mach_init_doit(false);
2d21ac55
A
104}
105
6d2010ae
A
106// called by libSystem_atfork_child()
107int
108_mach_fork_child(void)
2d21ac55 109{
6d2010ae 110 return mach_init_doit(true);
2d21ac55
A
111}
112
6d2010ae
A
113int
114mach_init_doit(bool forkchild)
2d21ac55 115{
2d21ac55
A
116 /*
117 * Get the important ports into the cached values,
118 * as required by "mach_init.h".
119 */
2d21ac55 120 mach_task_self_ = task_self_trap();
6d2010ae 121
2d21ac55
A
122 /*
123 * Initialize the single mig reply port
124 */
125
6d2010ae
A
126 _pthread_set_self(0);
127 _mig_init(0);
2d21ac55
A
128
129#if WE_REALLY_NEED_THIS_GDB_HACK
130 /*
131 * Check to see if GDB wants us to stop
132 */
133 {
6d2010ae
A
134 task_user_data_data_t user_data;
135 mach_msg_type_number_t user_data_count = TASK_USER_DATA_COUNT;
2d21ac55
A
136
137 user_data.user_data = 0;
138 (void)task_info(mach_task_self_, TASK_USER_DATA,
139 (task_info_t)&user_data, &user_data_count);
140#define MACH_GDB_RUN_MAGIC_NUMBER 1
141#ifdef MACH_GDB_RUN_MAGIC_NUMBER
6d2010ae
A
142 /* This magic number is set in mach-aware gdb
143 * for RUN command to allow us to suspend user's
144 * executable (linked with this libmach!)
145 * with the code below.
146 * This hack should disappear when gdb improves.
147 */
2d21ac55
A
148 if ((int)user_data.user_data == MACH_GDB_RUN_MAGIC_NUMBER) {
149 kern_return_t ret;
150 user_data.user_data = 0;
151
6d2010ae 152 ret = task_suspend(mach_task_self_);
2d21ac55 153 if (ret != KERN_SUCCESS) {
6d2010ae
A
154 while (1) {
155 (void)task_terminate(mach_task_self_);
156 }
2d21ac55
A
157 }
158 }
159#undef MACH_GDB_RUN_MAGIC_NUMBER
160#endif /* MACH_GDB_RUN_MAGIC_NUMBER */
161 }
162#endif /* WE_REALLY_NEED_THIS_GDB_HACK */
163
6d2010ae 164 return 0;
2d21ac55 165}