]>
Commit | Line | Data |
---|---|---|
2d21ac55 | 1 | /* |
39236c6e | 2 | * Copyright (c) 1999-2012 Apple Inc. All rights reserved. |
2d21ac55 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
0a7de745 | 28 | /* |
2d21ac55 A |
29 | * Mach Operating System |
30 | * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University | |
31 | * All Rights Reserved. | |
0a7de745 | 32 | * |
2d21ac55 A |
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. | |
0a7de745 | 38 | * |
2d21ac55 A |
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. | |
0a7de745 | 42 | * |
2d21ac55 | 43 | * Carnegie Mellon requests users of this software to return to |
0a7de745 | 44 | * |
2d21ac55 A |
45 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU |
46 | * School of Computer Science | |
47 | * Carnegie Mellon University | |
48 | * Pittsburgh PA 15213-3890 | |
0a7de745 | 49 | * |
2d21ac55 A |
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> | |
fe8ab488 | 61 | #include <machine/cpu_capabilities.h> |
6d2010ae | 62 | #include <stdbool.h> |
2d21ac55 A |
63 | #include "externs.h" |
64 | ||
39236c6e A |
65 | mach_port_t bootstrap_port = MACH_PORT_NULL; |
66 | mach_port_t mach_task_self_ = MACH_PORT_NULL; | |
6d2010ae | 67 | #ifdef __i386__ |
39236c6e | 68 | mach_port_t mach_host_self_ = MACH_PORT_NULL; |
6d2010ae | 69 | #endif |
39236c6e | 70 | extern mach_port_t _task_reply_port; |
2d21ac55 | 71 | |
fe8ab488 A |
72 | vm_size_t vm_kernel_page_size = 0; |
73 | vm_size_t vm_kernel_page_mask = 0; | |
74 | int vm_kernel_page_shift = 0; | |
39236c6e | 75 | |
fe8ab488 A |
76 | vm_size_t vm_page_size = 0; |
77 | vm_size_t vm_page_mask = 0; | |
78 | int vm_page_shift = 0; | |
2d21ac55 | 79 | |
6d2010ae A |
80 | int mach_init(void); |
81 | int _mach_fork_child(void); | |
2d21ac55 | 82 | |
39236c6e | 83 | static void mach_init_doit(void); |
2d21ac55 | 84 | |
2d21ac55 | 85 | extern void _pthread_set_self(void *); |
39236c6e | 86 | extern void _init_cpu_capabilities(void); |
2d21ac55 A |
87 | |
88 | kern_return_t | |
89 | host_page_size(__unused host_t host, vm_size_t *out_page_size) | |
90 | { | |
fe8ab488 | 91 | *out_page_size = vm_kernel_page_size; |
2d21ac55 A |
92 | return KERN_SUCCESS; |
93 | } | |
94 | ||
0a7de745 | 95 | /* |
6d2010ae A |
96 | * mach_init() must be called explicitly in static executables (including dyld). |
97 | * called by libSystem_initializer() in dynamic executables | |
98 | */ | |
99 | int | |
100 | mach_init(void) | |
2d21ac55 | 101 | { |
6d2010ae | 102 | static bool mach_init_inited = false; |
39236c6e A |
103 | if (!mach_init_inited) { |
104 | mach_init_doit(); | |
105 | mach_init_inited = true; | |
2d21ac55 | 106 | } |
39236c6e | 107 | return 0; |
2d21ac55 A |
108 | } |
109 | ||
6d2010ae A |
110 | // called by libSystem_atfork_child() |
111 | int | |
112 | _mach_fork_child(void) | |
2d21ac55 | 113 | { |
39236c6e A |
114 | mach_init_doit(); |
115 | return 0; | |
2d21ac55 A |
116 | } |
117 | ||
5ba3f43e A |
118 | #if defined(__arm__) || defined(__arm64__) |
119 | #if !defined(_COMM_PAGE_USER_PAGE_SHIFT_64) && defined(_COMM_PAGE_UNUSED0) | |
120 | #define _COMM_PAGE_USER_PAGE_SHIFT_32 (_COMM_PAGE_UNUSED0) | |
121 | #define _COMM_PAGE_USER_PAGE_SHIFT_64 (_COMM_PAGE_UNUSED0+1) | |
122 | #endif | |
123 | #endif | |
3e170ce0 | 124 | |
f427ee49 A |
125 | #if defined(__x86_64__) || defined(__i386__) |
126 | #define COMM_PAGE_KERNEL_PAGE_SHIFT_MIN_VERSION 14 | |
127 | #else | |
128 | #define COMM_PAGE_KERNEL_PAGE_SHIFT_MIN_VERSION 3 | |
129 | #endif | |
130 | ||
39236c6e A |
131 | void |
132 | mach_init_doit(void) | |
2d21ac55 | 133 | { |
39236c6e | 134 | // Initialize cached mach ports defined in mach_init.h |
2d21ac55 | 135 | mach_task_self_ = task_self_trap(); |
39236c6e | 136 | _task_reply_port = mach_reply_port(); |
2d21ac55 | 137 | |
fe8ab488 | 138 | if (vm_kernel_page_shift == 0) { |
f427ee49 A |
139 | #if defined(__x86_64__) || defined(__i386__) |
140 | if ((*((uint16_t *)_COMM_PAGE_VERSION) >= COMM_PAGE_KERNEL_PAGE_SHIFT_MIN_VERSION)) { | |
141 | vm_kernel_page_shift = *(uint8_t*) _COMM_PAGE_KERNEL_PAGE_SHIFT; | |
142 | } else { | |
143 | vm_kernel_page_shift = I386_PGSHIFT; | |
144 | } | |
145 | #else | |
fe8ab488 | 146 | vm_kernel_page_shift = *(uint8_t*) _COMM_PAGE_KERNEL_PAGE_SHIFT; |
f427ee49 | 147 | #endif |
fe8ab488 A |
148 | vm_kernel_page_size = 1 << vm_kernel_page_shift; |
149 | vm_kernel_page_mask = vm_kernel_page_size - 1; | |
fe8ab488 | 150 | } |
0a7de745 | 151 | |
fe8ab488 | 152 | if (vm_page_shift == 0) { |
5ba3f43e A |
153 | #if defined(__arm64__) |
154 | vm_page_shift = *(uint8_t*) _COMM_PAGE_USER_PAGE_SHIFT_64; | |
155 | #elif defined(__arm__) | |
156 | vm_page_shift = *(uint8_t*) _COMM_PAGE_USER_PAGE_SHIFT_32; | |
157 | #else | |
f427ee49 A |
158 | if ((*((uint16_t *)_COMM_PAGE_VERSION) >= COMM_PAGE_KERNEL_PAGE_SHIFT_MIN_VERSION)) { |
159 | vm_page_shift = *(uint8_t*) _COMM_PAGE_USER_PAGE_SHIFT_64; | |
160 | } else { | |
161 | vm_page_shift = vm_kernel_page_shift; | |
162 | } | |
5ba3f43e | 163 | #endif |
fe8ab488 A |
164 | vm_page_size = 1 << vm_page_shift; |
165 | vm_page_mask = vm_page_size - 1; | |
166 | } | |
167 | ||
39236c6e | 168 | _init_cpu_capabilities(); |
6d2010ae | 169 | _pthread_set_self(0); |
2d21ac55 | 170 | } |