]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/commpage/commpage.c
14785f5bb64b55f5c0359371e1d836614852aa22
[apple/xnu.git] / osfmk / i386 / commpage / commpage.c
1 /*
2 * Copyright (c) 2003-2008 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 /*
30 * Here's what to do if you want to add a new routine to the comm page:
31 *
32 * 1. Add a definition for it's address in osfmk/i386/cpu_capabilities.h,
33 * being careful to reserve room for future expansion.
34 *
35 * 2. Write one or more versions of the routine, each with it's own
36 * commpage_descriptor. The tricky part is getting the "special",
37 * "musthave", and "canthave" fields right, so that exactly one
38 * version of the routine is selected for every machine.
39 * The source files should be in osfmk/i386/commpage/.
40 *
41 * 3. Add a ptr to your new commpage_descriptor(s) in the "routines"
42 * array in osfmk/i386/commpage/commpage_asm.s. There are two
43 * arrays, one for the 32-bit and one for the 64-bit commpage.
44 *
45 * 4. Write the code in Libc to use the new routine.
46 */
47
48 #include <mach/mach_types.h>
49 #include <mach/machine.h>
50 #include <mach/vm_map.h>
51 #include <mach/mach_vm.h>
52 #include <mach/machine.h>
53 #include <i386/cpuid.h>
54 #include <i386/tsc.h>
55 #include <i386/rtclock.h>
56 #include <i386/cpu_data.h>
57 #include <i386/machine_routines.h>
58 #include <i386/misc_protos.h>
59 #include <i386/cpuid.h>
60 #include <machine/cpu_capabilities.h>
61 #include <machine/commpage.h>
62 #include <machine/pmap.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_map.h>
65
66 #include <ipc/ipc_port.h>
67
68 #include <kern/page_decrypt.h>
69
70 /* the lists of commpage routines are in commpage_asm.s */
71 extern commpage_descriptor* commpage_32_routines[];
72 extern commpage_descriptor* commpage_64_routines[];
73
74 /* translated commpage descriptors from commpage_sigs.c */
75 extern commpage_descriptor sigdata_descriptor;
76 extern commpage_descriptor *ba_descriptors[];
77
78 extern vm_map_t commpage32_map; // the shared submap, set up in vm init
79 extern vm_map_t commpage64_map; // the shared submap, set up in vm init
80
81 char *commPagePtr32 = NULL; // virtual addr in kernel map of 32-bit commpage
82 char *commPagePtr64 = NULL; // ...and of 64-bit commpage
83 int _cpu_capabilities = 0; // define the capability vector
84
85 int noVMX = 0; /* if true, do not set kHasAltivec in ppc _cpu_capabilities */
86
87 typedef uint32_t commpage_address_t;
88
89 static commpage_address_t next; // next available address in comm page
90 static commpage_address_t cur_routine; // comm page address of "current" routine
91 static boolean_t matched; // true if we've found a match for "current" routine
92
93 static char *commPagePtr; // virtual addr in kernel map of commpage we are working on
94 static commpage_address_t commPageBaseOffset; // subtract from 32-bit runtime address to get offset in virtual commpage in kernel map
95
96 static commpage_time_data *time_data32 = NULL;
97 static commpage_time_data *time_data64 = NULL;
98
99 /* Allocate the commpage and add to the shared submap created by vm:
100 * 1. allocate a page in the kernel map (RW)
101 * 2. wire it down
102 * 3. make a memory entry out of it
103 * 4. map that entry into the shared comm region map (R-only)
104 */
105
106 static void*
107 commpage_allocate(
108 vm_map_t submap, // commpage32_map or commpage_map64
109 size_t area_used ) // _COMM_PAGE32_AREA_USED or _COMM_PAGE64_AREA_USED
110 {
111 vm_offset_t kernel_addr = 0; // address of commpage in kernel map
112 vm_offset_t zero = 0;
113 vm_size_t size = area_used; // size actually populated
114 vm_map_entry_t entry;
115 ipc_port_t handle;
116
117 if (submap == NULL)
118 panic("commpage submap is null");
119
120 if (vm_map(kernel_map,&kernel_addr,area_used,0,VM_FLAGS_ANYWHERE,NULL,0,FALSE,VM_PROT_ALL,VM_PROT_ALL,VM_INHERIT_NONE))
121 panic("cannot allocate commpage");
122
123 if (vm_map_wire(kernel_map,kernel_addr,kernel_addr+area_used,VM_PROT_DEFAULT,FALSE))
124 panic("cannot wire commpage");
125
126 /*
127 * Now that the object is created and wired into the kernel map, mark it so that no delay
128 * copy-on-write will ever be performed on it as a result of mapping it into user-space.
129 * If such a delayed copy ever occurred, we could remove the kernel's wired mapping - and
130 * that would be a real disaster.
131 *
132 * JMM - What we really need is a way to create it like this in the first place.
133 */
134 if (!vm_map_lookup_entry( kernel_map, vm_map_trunc_page(kernel_addr), &entry) || entry->is_sub_map)
135 panic("cannot find commpage entry");
136 entry->object.vm_object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
137
138 if (mach_make_memory_entry( kernel_map, // target map
139 &size, // size
140 kernel_addr, // offset (address in kernel map)
141 VM_PROT_ALL, // map it RWX
142 &handle, // this is the object handle we get
143 NULL )) // parent_entry (what is this?)
144 panic("cannot make entry for commpage");
145
146 if (vm_map_64( submap, // target map (shared submap)
147 &zero, // address (map into 1st page in submap)
148 area_used, // size
149 0, // mask
150 VM_FLAGS_FIXED, // flags (it must be 1st page in submap)
151 handle, // port is the memory entry we just made
152 0, // offset (map 1st page in memory entry)
153 FALSE, // copy
154 VM_PROT_READ|VM_PROT_EXECUTE, // cur_protection (R-only in user map)
155 VM_PROT_READ|VM_PROT_EXECUTE, // max_protection
156 VM_INHERIT_SHARE )) // inheritance
157 panic("cannot map commpage");
158
159 ipc_port_release(handle);
160
161 return (void*)(intptr_t)kernel_addr; // return address in kernel map
162 }
163
164 /* Get address (in kernel map) of a commpage field. */
165
166 static void*
167 commpage_addr_of(
168 commpage_address_t addr_at_runtime )
169 {
170 return (void*) ((uintptr_t)commPagePtr + (addr_at_runtime - commPageBaseOffset));
171 }
172
173 /* Determine number of CPUs on this system. We cannot rely on
174 * machine_info.max_cpus this early in the boot.
175 */
176 static int
177 commpage_cpus( void )
178 {
179 int cpus;
180
181 cpus = ml_get_max_cpus(); // NB: this call can block
182
183 if (cpus == 0)
184 panic("commpage cpus==0");
185 if (cpus > 0xFF)
186 cpus = 0xFF;
187
188 return cpus;
189 }
190
191 /* Initialize kernel version of _cpu_capabilities vector (used by KEXTs.) */
192
193 static void
194 commpage_init_cpu_capabilities( void )
195 {
196 int bits;
197 int cpus;
198 ml_cpu_info_t cpu_info;
199
200 bits = 0;
201 ml_cpu_get_info(&cpu_info);
202
203 switch (cpu_info.vector_unit) {
204 case 8:
205 bits |= kHasSSE4_2;
206 /* fall thru */
207 case 7:
208 bits |= kHasSSE4_1;
209 /* fall thru */
210 case 6:
211 bits |= kHasSupplementalSSE3;
212 /* fall thru */
213 case 5:
214 bits |= kHasSSE3;
215 /* fall thru */
216 case 4:
217 bits |= kHasSSE2;
218 /* fall thru */
219 case 3:
220 bits |= kHasSSE;
221 /* fall thru */
222 case 2:
223 bits |= kHasMMX;
224 default:
225 break;
226 }
227 switch (cpu_info.cache_line_size) {
228 case 128:
229 bits |= kCache128;
230 break;
231 case 64:
232 bits |= kCache64;
233 break;
234 case 32:
235 bits |= kCache32;
236 break;
237 default:
238 break;
239 }
240 cpus = commpage_cpus(); // how many CPUs do we have
241
242 if (cpus == 1)
243 bits |= kUP;
244
245 bits |= (cpus << kNumCPUsShift);
246
247 bits |= kFastThreadLocalStorage; // we use %gs for TLS
248
249 if (cpu_mode_is64bit()) // k64Bit means processor is 64-bit capable
250 bits |= k64Bit;
251
252 if (tscFreq <= SLOW_TSC_THRESHOLD) /* is TSC too slow for _commpage_nanotime? */
253 bits |= kSlow;
254
255 _cpu_capabilities = bits; // set kernel version for use by drivers etc
256 }
257
258 int
259 _get_cpu_capabilities(void)
260 {
261 return _cpu_capabilities;
262 }
263
264 /* Copy data into commpage. */
265
266 static void
267 commpage_stuff(
268 commpage_address_t address,
269 const void *source,
270 int length )
271 {
272 void *dest = commpage_addr_of(address);
273
274 if (address < next)
275 panic("commpage overlap at address 0x%p, 0x%x < 0x%x", dest, address, next);
276
277 bcopy(source,dest,length);
278
279 next = address + length;
280 }
281
282 static void
283 commpage_stuff_swap(
284 commpage_address_t address,
285 void *source,
286 int length,
287 int legacy )
288 {
289 if ( legacy ) {
290 void *dest = commpage_addr_of(address);
291 dest = (void *)((uintptr_t) dest + _COMM_PAGE_SIGS_OFFSET);
292 switch (length) {
293 case 2:
294 OSWriteSwapInt16(dest, 0, *(uint16_t *)source);
295 break;
296 case 4:
297 OSWriteSwapInt32(dest, 0, *(uint32_t *)source);
298 break;
299 case 8:
300 OSWriteSwapInt64(dest, 0, *(uint64_t *)source);
301 break;
302 }
303 }
304 }
305
306 static void
307 commpage_stuff2(
308 commpage_address_t address,
309 void *source,
310 int length,
311 int legacy )
312 {
313 commpage_stuff_swap(address, source, length, legacy);
314 commpage_stuff(address, source, length);
315 }
316
317 /* Copy a routine into comm page if it matches running machine.
318 */
319 static void
320 commpage_stuff_routine(
321 commpage_descriptor *rd )
322 {
323 uint32_t must,cant;
324
325 if (rd->commpage_address != cur_routine) {
326 if ((cur_routine!=0) && (matched==0))
327 panic("commpage no match for last, next address %08x", rd->commpage_address);
328 cur_routine = rd->commpage_address;
329 matched = 0;
330 }
331
332 must = _cpu_capabilities & rd->musthave;
333 cant = _cpu_capabilities & rd->canthave;
334
335 if ((must == rd->musthave) && (cant == 0)) {
336 if (matched)
337 panic("commpage multiple matches for address %08x", rd->commpage_address);
338 matched = 1;
339
340 commpage_stuff(rd->commpage_address,rd->code_address,rd->code_length);
341 }
342 }
343
344 /* Fill in the 32- or 64-bit commpage. Called once for each.
345 * The 32-bit ("legacy") commpage has a bunch of stuff added to it
346 * for translated processes, some of which is byte-swapped.
347 */
348
349 static void
350 commpage_populate_one(
351 vm_map_t submap, // commpage32_map or compage64_map
352 char ** kernAddressPtr, // &commPagePtr32 or &commPagePtr64
353 size_t area_used, // _COMM_PAGE32_AREA_USED or _COMM_PAGE64_AREA_USED
354 commpage_address_t base_offset, // will become commPageBaseOffset
355 commpage_descriptor** commpage_routines, // list of routine ptrs for this commpage
356 boolean_t legacy, // true if 32-bit commpage
357 commpage_time_data** time_data, // &time_data32 or &time_data64
358 const char* signature ) // "commpage 32-bit" or "commpage 64-bit"
359 {
360 short c2;
361 int c4;
362 static double two52 = 1048576.0 * 1048576.0 * 4096.0; // 2**52
363 static double ten6 = 1000000.0; // 10**6
364 commpage_descriptor **rd;
365 short version = _COMM_PAGE_THIS_VERSION;
366 int swapcaps;
367
368 next = 0;
369 cur_routine = 0;
370 commPagePtr = (char *)commpage_allocate( submap, (vm_size_t) area_used );
371 *kernAddressPtr = commPagePtr; // save address either in commPagePtr32 or 64
372 commPageBaseOffset = base_offset;
373
374 *time_data = commpage_addr_of( _COMM_PAGE_TIME_DATA_START );
375
376 /* Stuff in the constants. We move things into the comm page in strictly
377 * ascending order, so we can check for overlap and panic if so.
378 */
379 commpage_stuff(_COMM_PAGE_SIGNATURE,signature,(int)strlen(signature));
380 commpage_stuff2(_COMM_PAGE_VERSION,&version,sizeof(short),legacy);
381 commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES,&_cpu_capabilities,sizeof(int));
382
383 /* excuse our magic constants, we cannot include ppc/cpu_capabilities.h */
384 /* always set kCache32 and kDcbaAvailable */
385 swapcaps = 0x44;
386 if ( _cpu_capabilities & kUP )
387 swapcaps |= (kUP + (1 << kNumCPUsShift));
388 else
389 swapcaps |= 2 << kNumCPUsShift; /* limit #cpus to 2 */
390 if ( ! noVMX ) /* if rosetta will be emulating altivec... */
391 swapcaps |= 0x101; /* ...then set kHasAltivec and kDataStreamsAvailable too */
392 commpage_stuff_swap(_COMM_PAGE_CPU_CAPABILITIES, &swapcaps, sizeof(int), legacy);
393 c2 = 32;
394 commpage_stuff_swap(_COMM_PAGE_CACHE_LINESIZE,&c2,2,legacy);
395
396 if (_cpu_capabilities & kCache32)
397 c2 = 32;
398 else if (_cpu_capabilities & kCache64)
399 c2 = 64;
400 else if (_cpu_capabilities & kCache128)
401 c2 = 128;
402 commpage_stuff(_COMM_PAGE_CACHE_LINESIZE,&c2,2);
403
404 c4 = MP_SPIN_TRIES;
405 commpage_stuff(_COMM_PAGE_SPIN_COUNT,&c4,4);
406
407 if ( legacy ) {
408 commpage_stuff2(_COMM_PAGE_2_TO_52,&two52,8,legacy);
409 commpage_stuff2(_COMM_PAGE_10_TO_6,&ten6,8,legacy);
410 }
411
412 for( rd = commpage_routines; *rd != NULL ; rd++ )
413 commpage_stuff_routine(*rd);
414
415 if (!matched)
416 panic("commpage no match on last routine");
417
418 if (next > _COMM_PAGE_END)
419 panic("commpage overflow: next = 0x%08x, commPagePtr = 0x%p", next, commPagePtr);
420
421 if ( legacy ) {
422 next = 0;
423 for( rd = ba_descriptors; *rd != NULL ; rd++ )
424 commpage_stuff_routine(*rd);
425
426 next = 0;
427 commpage_stuff_routine(&sigdata_descriptor);
428 }
429 }
430
431
432 /* Fill in commpages: called once, during kernel initialization, from the
433 * startup thread before user-mode code is running.
434 *
435 * See the top of this file for a list of what you have to do to add
436 * a new routine to the commpage.
437 */
438
439 void
440 commpage_populate( void )
441 {
442 commpage_init_cpu_capabilities();
443
444 commpage_populate_one( commpage32_map,
445 &commPagePtr32,
446 _COMM_PAGE32_AREA_USED,
447 _COMM_PAGE32_BASE_ADDRESS,
448 commpage_32_routines,
449 TRUE, /* legacy (32-bit) commpage */
450 &time_data32,
451 "commpage 32-bit");
452 #ifndef __LP64__
453 pmap_commpage32_init((vm_offset_t) commPagePtr32, _COMM_PAGE32_BASE_ADDRESS,
454 _COMM_PAGE32_AREA_USED/INTEL_PGBYTES);
455 #endif
456 time_data64 = time_data32; /* if no 64-bit commpage, point to 32-bit */
457
458 if (_cpu_capabilities & k64Bit) {
459 commpage_populate_one( commpage64_map,
460 &commPagePtr64,
461 _COMM_PAGE64_AREA_USED,
462 _COMM_PAGE32_START_ADDRESS, /* commpage address are relative to 32-bit commpage placement */
463 commpage_64_routines,
464 FALSE, /* not a legacy commpage */
465 &time_data64,
466 "commpage 64-bit");
467 #ifndef __LP64__
468 pmap_commpage64_init((vm_offset_t) commPagePtr64, _COMM_PAGE64_BASE_ADDRESS,
469 _COMM_PAGE64_AREA_USED/INTEL_PGBYTES);
470 #endif
471 }
472
473 rtc_nanotime_init_commpage();
474 }
475
476
477 /* Update commpage nanotime information. Note that we interleave
478 * setting the 32- and 64-bit commpages, in order to keep nanotime more
479 * nearly in sync between the two environments.
480 *
481 * This routine must be serialized by some external means, ie a lock.
482 */
483
484 void
485 commpage_set_nanotime(
486 uint64_t tsc_base,
487 uint64_t ns_base,
488 uint32_t scale,
489 uint32_t shift )
490 {
491 commpage_time_data *p32 = time_data32;
492 commpage_time_data *p64 = time_data64;
493 static uint32_t generation = 0;
494 uint32_t next_gen;
495
496 if (p32 == NULL) /* have commpages been allocated yet? */
497 return;
498
499 if ( generation != p32->nt_generation )
500 panic("nanotime trouble 1"); /* possibly not serialized */
501 if ( ns_base < p32->nt_ns_base )
502 panic("nanotime trouble 2");
503 if ((shift != 32) && ((_cpu_capabilities & kSlow)==0) )
504 panic("nanotime trouble 3");
505
506 next_gen = ++generation;
507 if (next_gen == 0)
508 next_gen = ++generation;
509
510 p32->nt_generation = 0; /* mark invalid, so commpage won't try to use it */
511 p64->nt_generation = 0;
512
513 p32->nt_tsc_base = tsc_base;
514 p64->nt_tsc_base = tsc_base;
515
516 p32->nt_ns_base = ns_base;
517 p64->nt_ns_base = ns_base;
518
519 p32->nt_scale = scale;
520 p64->nt_scale = scale;
521
522 p32->nt_shift = shift;
523 p64->nt_shift = shift;
524
525 p32->nt_generation = next_gen; /* mark data as valid */
526 p64->nt_generation = next_gen;
527 }
528
529
530 /* Disable commpage gettimeofday(), forcing commpage to call through to the kernel. */
531
532 void
533 commpage_disable_timestamp( void )
534 {
535 time_data32->gtod_generation = 0;
536 time_data64->gtod_generation = 0;
537 }
538
539
540 /* Update commpage gettimeofday() information. As with nanotime(), we interleave
541 * updates to the 32- and 64-bit commpage, in order to keep time more nearly in sync
542 * between the two environments.
543 *
544 * This routine must be serializeed by some external means, ie a lock.
545 */
546
547 void
548 commpage_set_timestamp(
549 uint64_t abstime,
550 uint64_t secs )
551 {
552 commpage_time_data *p32 = time_data32;
553 commpage_time_data *p64 = time_data64;
554 static uint32_t generation = 0;
555 uint32_t next_gen;
556
557 next_gen = ++generation;
558 if (next_gen == 0)
559 next_gen = ++generation;
560
561 p32->gtod_generation = 0; /* mark invalid, so commpage won't try to use it */
562 p64->gtod_generation = 0;
563
564 p32->gtod_ns_base = abstime;
565 p64->gtod_ns_base = abstime;
566
567 p32->gtod_sec_base = secs;
568 p64->gtod_sec_base = secs;
569
570 p32->gtod_generation = next_gen; /* mark data as valid */
571 p64->gtod_generation = next_gen;
572 }
573
574
575 /* Update _COMM_PAGE_MEMORY_PRESSURE. Called periodically from vm's compute_memory_pressure() */
576
577 void
578 commpage_set_memory_pressure(
579 unsigned int pressure )
580 {
581 char *cp;
582 uint32_t *ip;
583
584 cp = commPagePtr32;
585 if ( cp ) {
586 cp += (_COMM_PAGE_MEMORY_PRESSURE - _COMM_PAGE32_BASE_ADDRESS);
587 ip = (uint32_t*) cp;
588 *ip = (uint32_t) pressure;
589 }
590
591 cp = commPagePtr64;
592 if ( cp ) {
593 cp += (_COMM_PAGE_MEMORY_PRESSURE - _COMM_PAGE32_START_ADDRESS);
594 ip = (uint32_t*) cp;
595 *ip = (uint32_t) pressure;
596 }
597
598 }
599
600
601 /* Update _COMM_PAGE_SPIN_COUNT. We might want to reduce when running on a battery, etc. */
602
603 void
604 commpage_set_spin_count(
605 unsigned int count )
606 {
607 char *cp;
608 uint32_t *ip;
609
610 if (count == 0) /* we test for 0 after decrement, not before */
611 count = 1;
612
613 cp = commPagePtr32;
614 if ( cp ) {
615 cp += (_COMM_PAGE_SPIN_COUNT - _COMM_PAGE32_BASE_ADDRESS);
616 ip = (uint32_t*) cp;
617 *ip = (uint32_t) count;
618 }
619
620 cp = commPagePtr64;
621 if ( cp ) {
622 cp += (_COMM_PAGE_SPIN_COUNT - _COMM_PAGE32_START_ADDRESS);
623 ip = (uint32_t*) cp;
624 *ip = (uint32_t) count;
625 }
626
627 }
628
629
630 /* Check to see if a given address is in the Preemption Free Zone (PFZ) */
631
632 uint32_t
633 commpage_is_in_pfz32(uint32_t addr32)
634 {
635 if ( (addr32 >= _COMM_PAGE_PFZ_START) && (addr32 < _COMM_PAGE_PFZ_END)) {
636 return 1;
637 }
638 else
639 return 0;
640 }
641
642 uint32_t
643 commpage_is_in_pfz64(addr64_t addr64)
644 {
645 if ( (addr64 >= _COMM_PAGE_32_TO_64(_COMM_PAGE_PFZ_START))
646 && (addr64 < _COMM_PAGE_32_TO_64(_COMM_PAGE_PFZ_END))) {
647 return 1;
648 }
649 else
650 return 0;
651 }
652