]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/commpage/commpage.c
xnu-517.7.7.tar.gz
[apple/xnu.git] / osfmk / i386 / commpage / commpage.c
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 /*
24 * Here's what to do if you want to add a new routine to the comm page:
25 *
26 * 1. Add a definition for it's address in osfmk/ppc/cpu_capabilities.h,
27 * being careful to reserve room for future expansion.
28 *
29 * 2. Write one or more versions of the routine, each with it's own
30 * commpage_descriptor. The tricky part is getting the "special",
31 * "musthave", and "canthave" fields right, so that exactly one
32 * version of the routine is selected for every machine.
33 * The source files should be in osfmk/ppc/commpage/.
34 *
35 * 3. Add a ptr to your new commpage_descriptor(s) in the "routines"
36 * array in commpage_populate(). Of course, you'll also have to
37 * declare them "extern" in commpage_populate().
38 *
39 * 4. Write the code in Libc to use the new routine.
40 */
41
42 #include <mach/mach_types.h>
43 #include <mach/machine.h>
44 #include <i386/machine_routines.h>
45 #include <machine/cpu_capabilities.h>
46 #include <machine/commpage.h>
47 #include <machine/pmap.h>
48 #include <vm/vm_kern.h>
49 #include <mach/vm_map.h>
50
51 static uintptr_t next = 0; // next available byte in comm page
52 static int cur_routine = 0; // comm page address of "current" routine
53 static int matched; // true if we've found a match for "current" routine
54
55 int _cpu_capabilities = 0; // define the capability vector
56
57 char *commPagePtr = NULL; // virtual address of comm page in kernel map
58
59 /* Allocate the commpage and add to the shared submap created by vm:
60 * 1. allocate a page in the kernel map (RW)
61 * 2. wire it down
62 * 3. make a memory entry out of it
63 * 4. map that entry into the shared comm region map (R-only)
64 */
65
66 static void*
67 commpage_allocate( void )
68 {
69 extern vm_map_t com_region_map; // the shared submap, set up in vm init
70 vm_offset_t kernel_addr; // address of commpage in kernel map
71 vm_offset_t zero = 0;
72 vm_size_t size = _COMM_PAGE_AREA_LENGTH;
73 ipc_port_t handle;
74
75 if (com_region_map == NULL)
76 panic("commpage map is null");
77
78 if (vm_allocate(kernel_map,&kernel_addr,_COMM_PAGE_AREA_LENGTH,VM_FLAGS_ANYWHERE))
79 panic("cannot allocate commpage");
80
81 if (vm_map_wire(kernel_map,kernel_addr,kernel_addr+_COMM_PAGE_AREA_LENGTH,VM_PROT_DEFAULT,FALSE))
82 panic("cannot wire commpage");
83
84 if (mach_make_memory_entry( kernel_map, // target map
85 &size, // size
86 kernel_addr, // offset (address in kernel map)
87 VM_PROT_DEFAULT, // map it RW
88 &handle, // this is the object handle we get
89 NULL )) // parent_entry (what is this?)
90 panic("cannot make entry for commpage");
91
92 if (vm_map_64( com_region_map, // target map (shared submap)
93 &zero, // address (map into 1st page in submap)
94 _COMM_PAGE_AREA_LENGTH, // size
95 0, // mask
96 VM_FLAGS_FIXED, // flags (it must be 1st page in submap)
97 handle, // port is the memory entry we just made
98 0, // offset (map 1st page in memory entry)
99 FALSE, // copy
100 VM_PROT_READ, // cur_protection (R-only in user map)
101 VM_PROT_READ, // max_protection
102 VM_INHERIT_SHARE )) // inheritance
103 panic("cannot map commpage");
104
105 ipc_port_release(handle);
106
107 return (void*) kernel_addr; // return address in kernel map
108 }
109
110 /* Get address (in kernel map) of a commpage field. */
111
112 static void*
113 commpage_addr_of(
114 int addr_at_runtime )
115 {
116 return (void*) ((uintptr_t)commPagePtr + addr_at_runtime - _COMM_PAGE_BASE_ADDRESS);
117 }
118
119 /* Determine number of CPUs on this system. We cannot rely on
120 * machine_info.max_cpus this early in the boot.
121 */
122 static int
123 commpage_cpus( void )
124 {
125 int cpus;
126
127 cpus = ml_get_max_cpus(); // NB: this call can block
128
129 if (cpus == 0)
130 panic("commpage cpus==0");
131 if (cpus > 0xFF)
132 cpus = 0xFF;
133
134 return cpus;
135 }
136
137 /* Initialize kernel version of _cpu_capabilities vector (used by KEXTs.) */
138
139 static void
140 commpage_init_cpu_capabilities( void )
141 {
142 int bits;
143 int cpus;
144 ml_cpu_info_t cpu_info;
145
146 bits = 0;
147 ml_cpu_get_info(&cpu_info);
148
149 switch (cpu_info.vector_unit) {
150 case 5:
151 bits |= kHasPNI;
152 /* fall thru */
153 case 4:
154 bits |= kHasSSE2;
155 /* fall thru */
156 case 3:
157 bits |= kHasSSE;
158 /* fall thru */
159 case 2:
160 bits |= kHasMMX;
161 default:
162 break;
163 }
164 switch (cpu_info.cache_line_size) {
165 case 128:
166 bits |= kCache128;
167 break;
168 case 64:
169 bits |= kCache64;
170 break;
171 case 32:
172 bits |= kCache32;
173 break;
174 default:
175 break;
176 }
177 cpus = commpage_cpus(); // how many CPUs do we have
178
179 if (cpus == 1)
180 bits |= kUP;
181
182 bits |= (cpus << kNumCPUsShift);
183
184 _cpu_capabilities = bits; // set kernel version for use by drivers etc
185 }
186
187 /* Copy data into commpage. */
188
189 static void
190 commpage_stuff(
191 int address,
192 void *source,
193 int length )
194 {
195 void *dest = commpage_addr_of(address);
196
197 if ((uintptr_t)dest < next)
198 panic("commpage overlap");
199
200 bcopy(source,dest,length);
201
202 next = ((uintptr_t)dest + length);
203 }
204
205 /* Copy a routine into comm page if it matches running machine.
206 */
207 static void
208 commpage_stuff_routine(
209 commpage_descriptor *rd )
210 {
211 int must,cant;
212
213 if (rd->commpage_address != cur_routine) {
214 if ((cur_routine!=0) && (matched==0))
215 panic("commpage no match");
216 cur_routine = rd->commpage_address;
217 matched = 0;
218 }
219
220 must = _cpu_capabilities & rd->musthave;
221 cant = _cpu_capabilities & rd->canthave;
222
223 if ((must == rd->musthave) && (cant == 0)) {
224 if (matched)
225 panic("commpage duplicate matches");
226 matched = 1;
227
228 commpage_stuff(rd->commpage_address,rd->code_address,rd->code_length);
229 }
230 }
231
232 /* Fill in commpage: called once, during kernel initialization, from the
233 * startup thread before user-mode code is running.
234 * See the top of this file for a list of what you have to do to add
235 * a new routine to the commpage.
236 */
237
238 void
239 commpage_populate( void )
240 {
241 commpage_descriptor **rd;
242 short version = _COMM_PAGE_THIS_VERSION;
243 void *sig_addr;
244
245 extern char commpage_sigs_begin[];
246 extern char commpage_sigs_end[];
247
248 extern commpage_descriptor commpage_mach_absolute_time;
249 extern commpage_descriptor commpage_spin_lock_try_mp;
250 extern commpage_descriptor commpage_spin_lock_try_up;
251 extern commpage_descriptor commpage_spin_lock_mp;
252 extern commpage_descriptor commpage_spin_lock_up;
253 extern commpage_descriptor commpage_spin_unlock;
254 extern commpage_descriptor commpage_pthread_getspecific;
255 extern commpage_descriptor commpage_gettimeofday;
256 extern commpage_descriptor commpage_sys_flush_dcache;
257 extern commpage_descriptor commpage_sys_icache_invalidate;
258 extern commpage_descriptor commpage_pthread_self;
259 extern commpage_descriptor commpage_relinquish;
260 extern commpage_descriptor commpage_bzero_scalar;
261 extern commpage_descriptor commpage_bcopy_scalar;
262
263 static commpage_descriptor *routines[] = {
264 &commpage_mach_absolute_time,
265 &commpage_spin_lock_try_mp,
266 &commpage_spin_lock_try_up,
267 &commpage_spin_lock_mp,
268 &commpage_spin_lock_up,
269 &commpage_spin_unlock,
270 &commpage_pthread_getspecific,
271 &commpage_gettimeofday,
272 &commpage_sys_flush_dcache,
273 &commpage_sys_icache_invalidate,
274 &commpage_pthread_self,
275 &commpage_relinquish,
276 &commpage_bzero_scalar,
277 &commpage_bcopy_scalar,
278 NULL
279 };
280
281 commPagePtr = (char *)commpage_allocate();
282
283 commpage_init_cpu_capabilities();
284
285 /* Stuff in the constants. We move things into the comm page in strictly
286 * ascending order, so we can check for overlap and panic if so.
287 */
288
289 commpage_stuff(_COMM_PAGE_VERSION,&version,sizeof(short));
290 commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES,&_cpu_capabilities,
291 sizeof(int));
292
293 for( rd = routines; *rd != NULL ; rd++ )
294 commpage_stuff_routine(*rd);
295
296 if (!matched)
297 panic("commpage no match on last routine");
298
299 if (next > ((uintptr_t)commPagePtr + PAGE_SIZE))
300 panic("commpage overflow");
301
302 #define STUFF_SIG(addr, func) \
303 extern char commpage_sig_ ## func []; \
304 sig_addr = (void *)( (uintptr_t)_COMM_PAGE_BASE_ADDRESS + \
305 (uintptr_t)_COMM_PAGE_SIGS_OFFSET + 0x1000 + \
306 (uintptr_t)&commpage_sig_ ## func - \
307 (uintptr_t)&commpage_sigs_begin ); \
308 commpage_stuff(addr + _COMM_PAGE_SIGS_OFFSET, &sig_addr, sizeof(void *));
309
310 STUFF_SIG(_COMM_PAGE_ABSOLUTE_TIME, mach_absolute_time);
311 STUFF_SIG(_COMM_PAGE_SPINLOCK_TRY, spin_lock_try);
312 STUFF_SIG(_COMM_PAGE_SPINLOCK_LOCK, spin_lock);
313 STUFF_SIG(_COMM_PAGE_SPINLOCK_UNLOCK, spin_unlock);
314 STUFF_SIG(_COMM_PAGE_PTHREAD_GETSPECIFIC, pthread_getspecific);
315 STUFF_SIG(_COMM_PAGE_GETTIMEOFDAY, gettimeofday);
316 STUFF_SIG(_COMM_PAGE_FLUSH_DCACHE, sys_dcache_flush);
317 STUFF_SIG(_COMM_PAGE_FLUSH_ICACHE, sys_icache_invalidate);
318 STUFF_SIG(_COMM_PAGE_PTHREAD_SELF, pthread_self);
319 STUFF_SIG(_COMM_PAGE_BZERO, bzero);
320 STUFF_SIG(_COMM_PAGE_BCOPY, bcopy);
321 STUFF_SIG(_COMM_PAGE_MEMCPY, memmove);
322
323 commpage_stuff(_COMM_PAGE_BASE_ADDRESS + _COMM_PAGE_SIGS_OFFSET + 0x1000, &commpage_sigs_begin,
324 (uintptr_t)&commpage_sigs_end - (uintptr_t)&commpage_sigs_begin);
325 }