]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/commpage/commpage.c
xnu-792.tar.gz
[apple/xnu.git] / osfmk / i386 / commpage / commpage.c
CommitLineData
43866e37
A
1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
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.
43866e37 11 *
e5568f75
A
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
43866e37
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
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.
43866e37
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
55e303ae
A
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>
91447636 44#include <mach/vm_map.h>
55e303ae 45#include <i386/machine_routines.h>
43866e37
A
46#include <machine/cpu_capabilities.h>
47#include <machine/commpage.h>
55e303ae
A
48#include <machine/pmap.h>
49#include <vm/vm_kern.h>
91447636
A
50#include <vm/vm_map.h>
51#include <ipc/ipc_port.h>
52
53
54extern vm_map_t com_region_map32; // the shared submap, set up in vm init
55e303ae 55
91447636
A
56static uintptr_t next = 0; // next available byte in comm page
57static int cur_routine = 0; // comm page address of "current" routine
58static int matched; // true if we've found a match for "current" routine
55e303ae
A
59
60int _cpu_capabilities = 0; // define the capability vector
61
62char *commPagePtr = NULL; // virtual address of comm page in kernel map
63
64/* Allocate the commpage and add to the shared submap created by vm:
65 * 1. allocate a page in the kernel map (RW)
66 * 2. wire it down
67 * 3. make a memory entry out of it
68 * 4. map that entry into the shared comm region map (R-only)
69 */
70
71static void*
72commpage_allocate( void )
73{
55e303ae
A
74 vm_offset_t kernel_addr; // address of commpage in kernel map
75 vm_offset_t zero = 0;
76 vm_size_t size = _COMM_PAGE_AREA_LENGTH;
91447636 77 vm_map_entry_t entry;
55e303ae
A
78 ipc_port_t handle;
79
91447636 80 if (com_region_map32 == NULL)
55e303ae
A
81 panic("commpage map is null");
82
83 if (vm_allocate(kernel_map,&kernel_addr,_COMM_PAGE_AREA_LENGTH,VM_FLAGS_ANYWHERE))
84 panic("cannot allocate commpage");
85
86 if (vm_map_wire(kernel_map,kernel_addr,kernel_addr+_COMM_PAGE_AREA_LENGTH,VM_PROT_DEFAULT,FALSE))
87 panic("cannot wire commpage");
88
91447636
A
89 /*
90 * Now that the object is created and wired into the kernel map, mark it so that no delay
91 * copy-on-write will ever be performed on it as a result of mapping it into user-space.
92 * If such a delayed copy ever occurred, we could remove the kernel's wired mapping - and
93 * that would be a real disaster.
94 *
95 * JMM - What we really need is a way to create it like this in the first place.
96 */
97 if (!vm_map_lookup_entry( kernel_map, vm_map_trunc_page(kernel_addr), &entry) || entry->is_sub_map)
98 panic("cannot find commpage entry");
99 entry->object.vm_object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
100
55e303ae
A
101 if (mach_make_memory_entry( kernel_map, // target map
102 &size, // size
103 kernel_addr, // offset (address in kernel map)
104 VM_PROT_DEFAULT, // map it RW
105 &handle, // this is the object handle we get
106 NULL )) // parent_entry (what is this?)
107 panic("cannot make entry for commpage");
108
91447636 109 if (vm_map_64( com_region_map32, // target map (shared submap)
55e303ae
A
110 &zero, // address (map into 1st page in submap)
111 _COMM_PAGE_AREA_LENGTH, // size
112 0, // mask
113 VM_FLAGS_FIXED, // flags (it must be 1st page in submap)
114 handle, // port is the memory entry we just made
115 0, // offset (map 1st page in memory entry)
116 FALSE, // copy
117 VM_PROT_READ, // cur_protection (R-only in user map)
118 VM_PROT_READ, // max_protection
119 VM_INHERIT_SHARE )) // inheritance
120 panic("cannot map commpage");
121
122 ipc_port_release(handle);
123
124 return (void*) kernel_addr; // return address in kernel map
125}
126
127/* Get address (in kernel map) of a commpage field. */
128
91447636 129static void*
55e303ae
A
130commpage_addr_of(
131 int addr_at_runtime )
132{
133 return (void*) ((uintptr_t)commPagePtr + addr_at_runtime - _COMM_PAGE_BASE_ADDRESS);
134}
135
136/* Determine number of CPUs on this system. We cannot rely on
137 * machine_info.max_cpus this early in the boot.
138 */
139static int
140commpage_cpus( void )
141{
142 int cpus;
143
144 cpus = ml_get_max_cpus(); // NB: this call can block
145
146 if (cpus == 0)
147 panic("commpage cpus==0");
148 if (cpus > 0xFF)
149 cpus = 0xFF;
150
151 return cpus;
152}
43866e37 153
55e303ae 154/* Initialize kernel version of _cpu_capabilities vector (used by KEXTs.) */
43866e37 155
55e303ae
A
156static void
157commpage_init_cpu_capabilities( void )
158{
159 int bits;
160 int cpus;
161 ml_cpu_info_t cpu_info;
43866e37 162
55e303ae
A
163 bits = 0;
164 ml_cpu_get_info(&cpu_info);
165
166 switch (cpu_info.vector_unit) {
167 case 5:
91447636 168 bits |= kHasSSE3;
55e303ae
A
169 /* fall thru */
170 case 4:
171 bits |= kHasSSE2;
172 /* fall thru */
173 case 3:
174 bits |= kHasSSE;
175 /* fall thru */
176 case 2:
177 bits |= kHasMMX;
178 default:
179 break;
180 }
181 switch (cpu_info.cache_line_size) {
182 case 128:
183 bits |= kCache128;
184 break;
185 case 64:
186 bits |= kCache64;
187 break;
188 case 32:
189 bits |= kCache32;
190 break;
191 default:
192 break;
193 }
194 cpus = commpage_cpus(); // how many CPUs do we have
195
196 if (cpus == 1)
197 bits |= kUP;
198
199 bits |= (cpus << kNumCPUsShift);
200
91447636
A
201 bits |= kFastThreadLocalStorage; // we use %gs for TLS
202
55e303ae
A
203 _cpu_capabilities = bits; // set kernel version for use by drivers etc
204}
205
206/* Copy data into commpage. */
207
208static void
209commpage_stuff(
210 int address,
211 void *source,
212 int length )
213{
214 void *dest = commpage_addr_of(address);
215
216 if ((uintptr_t)dest < next)
91447636 217 panic("commpage overlap at address 0x%x, 0x%x < 0x%x", address, dest, next);
55e303ae
A
218
219 bcopy(source,dest,length);
43866e37 220
55e303ae
A
221 next = ((uintptr_t)dest + length);
222}
223
91447636
A
224
225static void
226commpage_stuff2(
227 int address,
228 void *source,
229 int length )
230{
231 commpage_stuff(address, source, length);
232}
233
55e303ae
A
234/* Copy a routine into comm page if it matches running machine.
235 */
236static void
237commpage_stuff_routine(
238 commpage_descriptor *rd )
239{
240 int must,cant;
241
242 if (rd->commpage_address != cur_routine) {
243 if ((cur_routine!=0) && (matched==0))
244 panic("commpage no match");
245 cur_routine = rd->commpage_address;
246 matched = 0;
247 }
248
249 must = _cpu_capabilities & rd->musthave;
250 cant = _cpu_capabilities & rd->canthave;
251
252 if ((must == rd->musthave) && (cant == 0)) {
253 if (matched)
254 panic("commpage duplicate matches");
255 matched = 1;
256
257 commpage_stuff(rd->commpage_address,rd->code_address,rd->code_length);
258 }
259}
260
91447636
A
261
262#define COMMPAGE_DESC(name) commpage_ ## name
263#define EXTERN_COMMPAGE_DESC(name) \
264 extern commpage_descriptor COMMPAGE_DESC(name)
265
266EXTERN_COMMPAGE_DESC(compare_and_swap32_mp);
267EXTERN_COMMPAGE_DESC(compare_and_swap32_up);
268EXTERN_COMMPAGE_DESC(compare_and_swap64_mp);
269EXTERN_COMMPAGE_DESC(compare_and_swap64_up);
270EXTERN_COMMPAGE_DESC(atomic_add32_mp);
271EXTERN_COMMPAGE_DESC(atomic_add32_up);
272EXTERN_COMMPAGE_DESC(mach_absolute_time);
273EXTERN_COMMPAGE_DESC(spin_lock_try_mp);
274EXTERN_COMMPAGE_DESC(spin_lock_try_up);
275EXTERN_COMMPAGE_DESC(spin_lock_mp);
276EXTERN_COMMPAGE_DESC(spin_lock_up);
277EXTERN_COMMPAGE_DESC(spin_unlock);
278EXTERN_COMMPAGE_DESC(pthread_getspecific);
279EXTERN_COMMPAGE_DESC(gettimeofday);
280EXTERN_COMMPAGE_DESC(sys_flush_dcache);
281EXTERN_COMMPAGE_DESC(sys_icache_invalidate);
282EXTERN_COMMPAGE_DESC(pthread_self);
283EXTERN_COMMPAGE_DESC(relinquish);
284EXTERN_COMMPAGE_DESC(bit_test_and_set_mp);
285EXTERN_COMMPAGE_DESC(bit_test_and_set_up);
286EXTERN_COMMPAGE_DESC(bit_test_and_clear_mp);
287EXTERN_COMMPAGE_DESC(bit_test_and_clear_up);
288EXTERN_COMMPAGE_DESC(bzero_scalar);
289EXTERN_COMMPAGE_DESC(bcopy_scalar);
290EXTERN_COMMPAGE_DESC(nanotime);
291
292static commpage_descriptor *routines[] = {
293 &COMMPAGE_DESC(compare_and_swap32_mp),
294 &COMMPAGE_DESC(compare_and_swap32_up),
295 &COMMPAGE_DESC(compare_and_swap64_mp),
296 &COMMPAGE_DESC(compare_and_swap64_up),
297 &COMMPAGE_DESC(atomic_add32_mp),
298 &COMMPAGE_DESC(atomic_add32_up),
299 &COMMPAGE_DESC(mach_absolute_time),
300 &COMMPAGE_DESC(spin_lock_try_mp),
301 &COMMPAGE_DESC(spin_lock_try_up),
302 &COMMPAGE_DESC(spin_lock_mp),
303 &COMMPAGE_DESC(spin_lock_up),
304 &COMMPAGE_DESC(spin_unlock),
305 &COMMPAGE_DESC(pthread_getspecific),
306 &COMMPAGE_DESC(gettimeofday),
307 &COMMPAGE_DESC(sys_flush_dcache),
308 &COMMPAGE_DESC(sys_icache_invalidate),
309 &COMMPAGE_DESC(pthread_self),
310 &COMMPAGE_DESC(relinquish),
311 &COMMPAGE_DESC(bit_test_and_set_mp),
312 &COMMPAGE_DESC(bit_test_and_set_up),
313 &COMMPAGE_DESC(bit_test_and_clear_mp),
314 &COMMPAGE_DESC(bit_test_and_clear_up),
315 &COMMPAGE_DESC(bzero_scalar),
316 &COMMPAGE_DESC(bcopy_scalar),
317 &COMMPAGE_DESC(nanotime),
318 NULL
319};
320
321
55e303ae
A
322/* Fill in commpage: called once, during kernel initialization, from the
323 * startup thread before user-mode code is running.
324 * See the top of this file for a list of what you have to do to add
325 * a new routine to the commpage.
326 */
327
328void
329commpage_populate( void )
330{
91447636
A
331 short c2;
332 static double two52 = 1048576.0 * 1048576.0 * 4096.0; // 2**52
333 static double ten6 = 1000000.0; // 10**6
55e303ae
A
334 commpage_descriptor **rd;
335 short version = _COMM_PAGE_THIS_VERSION;
55e303ae
A
336
337 commPagePtr = (char *)commpage_allocate();
338
339 commpage_init_cpu_capabilities();
340
341 /* Stuff in the constants. We move things into the comm page in strictly
342 * ascending order, so we can check for overlap and panic if so.
343 */
344
91447636 345 commpage_stuff2(_COMM_PAGE_VERSION,&version,sizeof(short));
55e303ae
A
346 commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES,&_cpu_capabilities,
347 sizeof(int));
348
91447636
A
349 if (_cpu_capabilities & kCache32)
350 c2 = 32;
351 else if (_cpu_capabilities & kCache64)
352 c2 = 64;
353 else if (_cpu_capabilities & kCache128)
354 c2 = 128;
355 commpage_stuff(_COMM_PAGE_CACHE_LINESIZE,&c2,2);
356
357 c2 = 32;
358
359 commpage_stuff2(_COMM_PAGE_2_TO_52,&two52,8);
360
361 commpage_stuff2(_COMM_PAGE_10_TO_6,&ten6,8);
362
55e303ae
A
363 for( rd = routines; *rd != NULL ; rd++ )
364 commpage_stuff_routine(*rd);
365
366 if (!matched)
367 panic("commpage no match on last routine");
368
91447636
A
369 if (next > (uintptr_t)_COMM_PAGE_END)
370 panic("commpage overflow: next = 0x%08x, commPagePtr = 0x%08x", next, (uintptr_t)commPagePtr);
371
372
373 pmap_commpage_init((vm_offset_t) commPagePtr, _COMM_PAGE_BASE_ADDRESS,
374 _COMM_PAGE_AREA_LENGTH/INTEL_PGBYTES);
43866e37 375}
91447636
A
376
377/*
378 * This macro prevents compiler instruction scheduling:
379 */
380#define NO_REORDERING asm volatile("" : : : "memory")
381
382void
383commpage_set_nanotime(commpage_nanotime_t *newp)
384{
385 commpage_nanotime_t *cnp;
386
387 /* Nop if commpage not set up yet */
388 if (commPagePtr == NULL)
389 return;
390
391 cnp = (commpage_nanotime_t *)commpage_addr_of(_COMM_PAGE_NANOTIME_INFO);
392
393 /*
394 * Update in reverse order:
395 * check_tsc first - it's read and compared with base_tsc last.
396 */
397 cnp->nt_check_tsc = newp->nt_base_tsc; NO_REORDERING;
398 cnp->nt_shift = newp->nt_shift; NO_REORDERING;
399 cnp->nt_scale = newp->nt_scale; NO_REORDERING;
400 cnp->nt_base_ns = newp->nt_base_ns; NO_REORDERING;
401 cnp->nt_base_tsc = newp->nt_base_tsc;
402}
403