1/*
2 * Copyright (c) 1999-2012 Apple Inc. All rights reserved.
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>
61#include <machine/cpu_capabilities.h>
62#include <stdbool.h>
63#include "externs.h"
64
65mach_port_t bootstrap_port = MACH_PORT_NULL;
66mach_port_t mach_task_self_ = MACH_PORT_NULL;
67#ifdef __i386__
68mach_port_t mach_host_self_ = MACH_PORT_NULL;
69#endif
70extern mach_port_t _task_reply_port;
71
72vm_size_t vm_kernel_page_size = 0;
73vm_size_t vm_kernel_page_mask = 0;
74int vm_kernel_page_shift = 0;
75
76vm_size_t vm_page_size = 0;
77vm_size_t vm_page_mask = 0;
78int vm_page_shift = 0;
79
80int mach_init(void);
81int _mach_fork_child(void);
82
83static void mach_init_doit(void);
84
85extern void _pthread_set_self(void *);
86extern void _init_cpu_capabilities(void);
87
88kern_return_t
89host_page_size(__unused host_t host, vm_size_t *out_page_size)
90{
91	*out_page_size = vm_kernel_page_size;
92	return KERN_SUCCESS;
93}
94
95/*
96 * mach_init() must be called explicitly in static executables (including dyld).
97 * called by libSystem_initializer() in dynamic executables
98 */
99int
100mach_init(void)
101{
102	static bool mach_init_inited = false;
103	if (!mach_init_inited) {
104		mach_init_doit();
105		mach_init_inited = true;
106	}
107	return 0;
108}
109
110// called by libSystem_atfork_child()
111int
112_mach_fork_child(void)
113{
114	mach_init_doit();
115	return 0;
116}
117
118void
119mach_init_doit(void)
120{
121	// Initialize cached mach ports defined in mach_init.h
122	mach_task_self_ = task_self_trap();
123	_task_reply_port = mach_reply_port();
124
125	if (vm_kernel_page_shift == 0) {
126#ifdef	_COMM_PAGE_KERNEL_PAGE_SHIFT
127		vm_kernel_page_shift = *(uint8_t*) _COMM_PAGE_KERNEL_PAGE_SHIFT;
128		vm_kernel_page_size = 1 << vm_kernel_page_shift;
129		vm_kernel_page_mask = vm_kernel_page_size - 1;
130#else
131		vm_kernel_page_size = PAGE_SIZE;
132		vm_kernel_page_mask = PAGE_MASK;
133		vm_kernel_page_shift = PAGE_SHIFT;
134#endif /* _COMM_PAGE_KERNEL_PAGE_SHIFT */
135	}
136
137	if (vm_page_shift == 0) {
138		vm_page_shift = vm_kernel_page_shift;
139		vm_page_size = 1 << vm_page_shift;
140		vm_page_mask = vm_page_size - 1;
141	}
142
143	_init_cpu_capabilities();
144	_pthread_set_self(0);
145}
146