]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/commpage/commpage.c
xnu-2782.30.5.tar.gz
[apple/xnu.git] / osfmk / i386 / commpage / commpage.c
1 /*
2 * Copyright (c) 2003-2010 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_protos.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 #include <kern/processor.h>
70
71 #include <sys/kdebug.h>
72
73 /* the lists of commpage routines are in commpage_asm.s */
74 extern commpage_descriptor* commpage_32_routines[];
75 extern commpage_descriptor* commpage_64_routines[];
76
77 extern vm_map_t commpage32_map; // the shared submap, set up in vm init
78 extern vm_map_t commpage64_map; // the shared submap, set up in vm init
79 extern vm_map_t commpage_text32_map; // the shared submap, set up in vm init
80 extern vm_map_t commpage_text64_map; // the shared submap, set up in vm init
81
82
83 char *commPagePtr32 = NULL; // virtual addr in kernel map of 32-bit commpage
84 char *commPagePtr64 = NULL; // ...and of 64-bit commpage
85 char *commPageTextPtr32 = NULL; // virtual addr in kernel map of 32-bit commpage
86 char *commPageTextPtr64 = NULL; // ...and of 64-bit commpage
87
88 uint64_t _cpu_capabilities = 0; // define the capability vector
89
90 typedef uint32_t commpage_address_t;
91
92 static commpage_address_t next; // next available address in comm page
93
94 static char *commPagePtr; // virtual addr in kernel map of commpage we are working on
95 static commpage_address_t commPageBaseOffset; // subtract from 32-bit runtime address to get offset in virtual commpage in kernel map
96
97 static commpage_time_data *time_data32 = NULL;
98 static commpage_time_data *time_data64 = NULL;
99
100 decl_simple_lock_data(static,commpage_active_cpus_lock);
101
102 /* Allocate the commpage and add to the shared submap created by vm:
103 * 1. allocate a page in the kernel map (RW)
104 * 2. wire it down
105 * 3. make a memory entry out of it
106 * 4. map that entry into the shared comm region map (R-only)
107 */
108
109 static void*
110 commpage_allocate(
111 vm_map_t submap, // commpage32_map or commpage_map64
112 size_t area_used, // _COMM_PAGE32_AREA_USED or _COMM_PAGE64_AREA_USED
113 vm_prot_t uperm)
114 {
115 vm_offset_t kernel_addr = 0; // address of commpage in kernel map
116 vm_offset_t zero = 0;
117 vm_size_t size = area_used; // size actually populated
118 vm_map_entry_t entry;
119 ipc_port_t handle;
120 kern_return_t kr;
121
122 if (submap == NULL)
123 panic("commpage submap is null");
124
125 if ((kr = vm_map(kernel_map,&kernel_addr,area_used,0,VM_FLAGS_ANYWHERE,NULL,0,FALSE,VM_PROT_ALL,VM_PROT_ALL,VM_INHERIT_NONE)))
126 panic("cannot allocate commpage %d", kr);
127
128 if ((kr = vm_map_wire(kernel_map,kernel_addr,kernel_addr+area_used,VM_PROT_DEFAULT,FALSE)))
129 panic("cannot wire commpage: %d", kr);
130
131 /*
132 * Now that the object is created and wired into the kernel map, mark it so that no delay
133 * copy-on-write will ever be performed on it as a result of mapping it into user-space.
134 * If such a delayed copy ever occurred, we could remove the kernel's wired mapping - and
135 * that would be a real disaster.
136 *
137 * JMM - What we really need is a way to create it like this in the first place.
138 */
139 if (!(kr = vm_map_lookup_entry( kernel_map, vm_map_trunc_page(kernel_addr, VM_MAP_PAGE_MASK(kernel_map)), &entry) || entry->is_sub_map))
140 panic("cannot find commpage entry %d", kr);
141 entry->object.vm_object->copy_strategy = MEMORY_OBJECT_COPY_NONE;
142
143 if ((kr = mach_make_memory_entry( kernel_map, // target map
144 &size, // size
145 kernel_addr, // offset (address in kernel map)
146 uperm, // protections as specified
147 &handle, // this is the object handle we get
148 NULL ))) // parent_entry (what is this?)
149 panic("cannot make entry for commpage %d", kr);
150
151 if ((kr = vm_map_64( submap, // target map (shared submap)
152 &zero, // address (map into 1st page in submap)
153 area_used, // size
154 0, // mask
155 VM_FLAGS_FIXED, // flags (it must be 1st page in submap)
156 handle, // port is the memory entry we just made
157 0, // offset (map 1st page in memory entry)
158 FALSE, // copy
159 uperm, // cur_protection (R-only in user map)
160 uperm, // max_protection
161 VM_INHERIT_SHARE ))) // inheritance
162 panic("cannot map commpage %d", kr);
163
164 ipc_port_release(handle);
165 /* Make the kernel mapping non-executable. This cannot be done
166 * at the time of map entry creation as mach_make_memory_entry
167 * cannot handle disjoint permissions at this time.
168 */
169 kr = vm_protect(kernel_map, kernel_addr, area_used, FALSE, VM_PROT_READ | VM_PROT_WRITE);
170 assert (kr == KERN_SUCCESS);
171
172 return (void*)(intptr_t)kernel_addr; // return address in kernel map
173 }
174
175 /* Get address (in kernel map) of a commpage field. */
176
177 static void*
178 commpage_addr_of(
179 commpage_address_t addr_at_runtime )
180 {
181 return (void*) ((uintptr_t)commPagePtr + (addr_at_runtime - commPageBaseOffset));
182 }
183
184 /* Determine number of CPUs on this system. We cannot rely on
185 * machine_info.max_cpus this early in the boot.
186 */
187 static int
188 commpage_cpus( void )
189 {
190 int cpus;
191
192 cpus = ml_get_max_cpus(); // NB: this call can block
193
194 if (cpus == 0)
195 panic("commpage cpus==0");
196 if (cpus > 0xFF)
197 cpus = 0xFF;
198
199 return cpus;
200 }
201
202 /* Initialize kernel version of _cpu_capabilities vector (used by KEXTs.) */
203
204 static void
205 commpage_init_cpu_capabilities( void )
206 {
207 uint64_t bits;
208 int cpus;
209 ml_cpu_info_t cpu_info;
210
211 bits = 0;
212 ml_cpu_get_info(&cpu_info);
213
214 switch (cpu_info.vector_unit) {
215 case 9:
216 bits |= kHasAVX1_0;
217 /* fall thru */
218 case 8:
219 bits |= kHasSSE4_2;
220 /* fall thru */
221 case 7:
222 bits |= kHasSSE4_1;
223 /* fall thru */
224 case 6:
225 bits |= kHasSupplementalSSE3;
226 /* fall thru */
227 case 5:
228 bits |= kHasSSE3;
229 /* fall thru */
230 case 4:
231 bits |= kHasSSE2;
232 /* fall thru */
233 case 3:
234 bits |= kHasSSE;
235 /* fall thru */
236 case 2:
237 bits |= kHasMMX;
238 default:
239 break;
240 }
241 switch (cpu_info.cache_line_size) {
242 case 128:
243 bits |= kCache128;
244 break;
245 case 64:
246 bits |= kCache64;
247 break;
248 case 32:
249 bits |= kCache32;
250 break;
251 default:
252 break;
253 }
254 cpus = commpage_cpus(); // how many CPUs do we have
255
256 bits |= (cpus << kNumCPUsShift);
257
258 bits |= kFastThreadLocalStorage; // we use %gs for TLS
259
260 #define setif(_bits, _bit, _condition) \
261 if (_condition) _bits |= _bit
262
263 setif(bits, kUP, cpus == 1);
264 setif(bits, k64Bit, cpu_mode_is64bit());
265 setif(bits, kSlow, tscFreq <= SLOW_TSC_THRESHOLD);
266
267 setif(bits, kHasAES, cpuid_features() &
268 CPUID_FEATURE_AES);
269 setif(bits, kHasF16C, cpuid_features() &
270 CPUID_FEATURE_F16C);
271 setif(bits, kHasRDRAND, cpuid_features() &
272 CPUID_FEATURE_RDRAND);
273 setif(bits, kHasFMA, cpuid_features() &
274 CPUID_FEATURE_FMA);
275
276 setif(bits, kHasBMI1, cpuid_leaf7_features() &
277 CPUID_LEAF7_FEATURE_BMI1);
278 setif(bits, kHasBMI2, cpuid_leaf7_features() &
279 CPUID_LEAF7_FEATURE_BMI2);
280 setif(bits, kHasRTM, cpuid_leaf7_features() &
281 CPUID_LEAF7_FEATURE_RTM);
282 setif(bits, kHasHLE, cpuid_leaf7_features() &
283 CPUID_LEAF7_FEATURE_HLE);
284 setif(bits, kHasAVX2_0, cpuid_leaf7_features() &
285 CPUID_LEAF7_FEATURE_AVX2);
286 setif(bits, kHasRDSEED, cpuid_features() &
287 CPUID_LEAF7_FEATURE_RDSEED);
288 setif(bits, kHasADX, cpuid_features() &
289 CPUID_LEAF7_FEATURE_ADX);
290
291 uint64_t misc_enable = rdmsr64(MSR_IA32_MISC_ENABLE);
292 setif(bits, kHasENFSTRG, (misc_enable & 1ULL) &&
293 (cpuid_leaf7_features() &
294 CPUID_LEAF7_FEATURE_ERMS));
295
296 _cpu_capabilities = bits; // set kernel version for use by drivers etc
297 }
298
299 /* initialize the approx_time_supported flag and set the approx time to 0.
300 * Called during initial commpage population.
301 */
302 static void
303 commpage_mach_approximate_time_init(void)
304 {
305 char *cp = commPagePtr32;
306 uint8_t supported;
307
308 #ifdef CONFIG_MACH_APPROXIMATE_TIME
309 supported = 1;
310 #else
311 supported = 0;
312 #endif
313 if ( cp ) {
314 cp += (_COMM_PAGE_APPROX_TIME_SUPPORTED - _COMM_PAGE32_BASE_ADDRESS);
315 *(boolean_t *)cp = supported;
316 }
317 cp = commPagePtr64;
318 if ( cp ) {
319 cp += (_COMM_PAGE_APPROX_TIME_SUPPORTED - _COMM_PAGE32_START_ADDRESS);
320 *(boolean_t *)cp = supported;
321 }
322 commpage_update_mach_approximate_time(0);
323 }
324
325
326 uint64_t
327 _get_cpu_capabilities(void)
328 {
329 return _cpu_capabilities;
330 }
331
332 /* Copy data into commpage. */
333
334 static void
335 commpage_stuff(
336 commpage_address_t address,
337 const void *source,
338 int length )
339 {
340 void *dest = commpage_addr_of(address);
341
342 if (address < next)
343 panic("commpage overlap at address 0x%p, 0x%x < 0x%x", dest, address, next);
344
345 bcopy(source,dest,length);
346
347 next = address + length;
348 }
349
350 /* Copy a routine into comm page if it matches running machine.
351 */
352 static void
353 commpage_stuff_routine(
354 commpage_descriptor *rd )
355 {
356 commpage_stuff(rd->commpage_address,rd->code_address,rd->code_length);
357 }
358
359 /* Fill in the 32- or 64-bit commpage. Called once for each.
360 */
361
362 static void
363 commpage_populate_one(
364 vm_map_t submap, // commpage32_map or compage64_map
365 char ** kernAddressPtr, // &commPagePtr32 or &commPagePtr64
366 size_t area_used, // _COMM_PAGE32_AREA_USED or _COMM_PAGE64_AREA_USED
367 commpage_address_t base_offset, // will become commPageBaseOffset
368 commpage_time_data** time_data, // &time_data32 or &time_data64
369 const char* signature, // "commpage 32-bit" or "commpage 64-bit"
370 vm_prot_t uperm)
371 {
372 uint8_t c1;
373 uint16_t c2;
374 int c4;
375 uint64_t c8;
376 uint32_t cfamily;
377 short version = _COMM_PAGE_THIS_VERSION;
378
379 next = 0;
380 commPagePtr = (char *)commpage_allocate( submap, (vm_size_t) area_used, uperm );
381 *kernAddressPtr = commPagePtr; // save address either in commPagePtr32 or 64
382 commPageBaseOffset = base_offset;
383
384 *time_data = commpage_addr_of( _COMM_PAGE_TIME_DATA_START );
385
386 /* Stuff in the constants. We move things into the comm page in strictly
387 * ascending order, so we can check for overlap and panic if so.
388 * Note: the 32-bit cpu_capabilities vector is retained in addition to
389 * the expanded 64-bit vector.
390 */
391 commpage_stuff(_COMM_PAGE_SIGNATURE,signature,(int)MIN(_COMM_PAGE_SIGNATURELEN, strlen(signature)));
392 commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES64,&_cpu_capabilities,sizeof(_cpu_capabilities));
393 commpage_stuff(_COMM_PAGE_VERSION,&version,sizeof(short));
394 commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES,&_cpu_capabilities,sizeof(uint32_t));
395
396 c2 = 32; // default
397 if (_cpu_capabilities & kCache64)
398 c2 = 64;
399 else if (_cpu_capabilities & kCache128)
400 c2 = 128;
401 commpage_stuff(_COMM_PAGE_CACHE_LINESIZE,&c2,2);
402
403 c4 = MP_SPIN_TRIES;
404 commpage_stuff(_COMM_PAGE_SPIN_COUNT,&c4,4);
405
406 /* machine_info valid after ml_get_max_cpus() */
407 c1 = machine_info.physical_cpu_max;
408 commpage_stuff(_COMM_PAGE_PHYSICAL_CPUS,&c1,1);
409 c1 = machine_info.logical_cpu_max;
410 commpage_stuff(_COMM_PAGE_LOGICAL_CPUS,&c1,1);
411
412 c8 = ml_cpu_cache_size(0);
413 commpage_stuff(_COMM_PAGE_MEMORY_SIZE, &c8, 8);
414
415 cfamily = cpuid_info()->cpuid_cpufamily;
416 commpage_stuff(_COMM_PAGE_CPUFAMILY, &cfamily, 4);
417
418 if (next > _COMM_PAGE_END)
419 panic("commpage overflow: next = 0x%08x, commPagePtr = 0x%p", next, commPagePtr);
420
421 }
422
423
424 /* Fill in commpages: called once, during kernel initialization, from the
425 * startup thread before user-mode code is running.
426 *
427 * See the top of this file for a list of what you have to do to add
428 * a new routine to the commpage.
429 */
430
431 void
432 commpage_populate( void )
433 {
434 commpage_init_cpu_capabilities();
435
436 commpage_populate_one( commpage32_map,
437 &commPagePtr32,
438 _COMM_PAGE32_AREA_USED,
439 _COMM_PAGE32_BASE_ADDRESS,
440 &time_data32,
441 "commpage 32-bit",
442 VM_PROT_READ);
443 #ifndef __LP64__
444 pmap_commpage32_init((vm_offset_t) commPagePtr32, _COMM_PAGE32_BASE_ADDRESS,
445 _COMM_PAGE32_AREA_USED/INTEL_PGBYTES);
446 #endif
447 time_data64 = time_data32; /* if no 64-bit commpage, point to 32-bit */
448
449 if (_cpu_capabilities & k64Bit) {
450 commpage_populate_one( commpage64_map,
451 &commPagePtr64,
452 _COMM_PAGE64_AREA_USED,
453 _COMM_PAGE32_START_ADDRESS, /* commpage address are relative to 32-bit commpage placement */
454 &time_data64,
455 "commpage 64-bit",
456 VM_PROT_READ);
457 #ifndef __LP64__
458 pmap_commpage64_init((vm_offset_t) commPagePtr64, _COMM_PAGE64_BASE_ADDRESS,
459 _COMM_PAGE64_AREA_USED/INTEL_PGBYTES);
460 #endif
461 }
462
463 simple_lock_init(&commpage_active_cpus_lock, 0);
464
465 commpage_update_active_cpus();
466 commpage_mach_approximate_time_init();
467 rtc_nanotime_init_commpage();
468 commpage_update_kdebug_enable();
469 }
470
471 /* Fill in the common routines during kernel initialization.
472 * This is called before user-mode code is running.
473 */
474 void commpage_text_populate( void ){
475 commpage_descriptor **rd;
476
477 next = 0;
478 commPagePtr = (char *) commpage_allocate(commpage_text32_map, (vm_size_t) _COMM_PAGE_TEXT_AREA_USED, VM_PROT_READ | VM_PROT_EXECUTE);
479 commPageTextPtr32 = commPagePtr;
480
481 char *cptr = commPagePtr;
482 int i=0;
483 for(; i< _COMM_PAGE_TEXT_AREA_USED; i++){
484 cptr[i]=0xCC;
485 }
486
487 commPageBaseOffset = _COMM_PAGE_TEXT_START;
488 for (rd = commpage_32_routines; *rd != NULL; rd++) {
489 commpage_stuff_routine(*rd);
490 }
491
492 #ifndef __LP64__
493 pmap_commpage32_init((vm_offset_t) commPageTextPtr32, _COMM_PAGE_TEXT_START,
494 _COMM_PAGE_TEXT_AREA_USED/INTEL_PGBYTES);
495 #endif
496
497 if (_cpu_capabilities & k64Bit) {
498 next = 0;
499 commPagePtr = (char *) commpage_allocate(commpage_text64_map, (vm_size_t) _COMM_PAGE_TEXT_AREA_USED, VM_PROT_READ | VM_PROT_EXECUTE);
500 commPageTextPtr64 = commPagePtr;
501
502 cptr=commPagePtr;
503 for(i=0; i<_COMM_PAGE_TEXT_AREA_USED; i++){
504 cptr[i]=0xCC;
505 }
506
507 for (rd = commpage_64_routines; *rd !=NULL; rd++) {
508 commpage_stuff_routine(*rd);
509 }
510
511 #ifndef __LP64__
512 pmap_commpage64_init((vm_offset_t) commPageTextPtr64, _COMM_PAGE_TEXT_START,
513 _COMM_PAGE_TEXT_AREA_USED/INTEL_PGBYTES);
514 #endif
515 }
516
517 if (next > _COMM_PAGE_TEXT_END)
518 panic("commpage text overflow: next=0x%08x, commPagePtr=%p", next, commPagePtr);
519
520 }
521
522 /* Update commpage nanotime information.
523 *
524 * This routine must be serialized by some external means, ie a lock.
525 */
526
527 void
528 commpage_set_nanotime(
529 uint64_t tsc_base,
530 uint64_t ns_base,
531 uint32_t scale,
532 uint32_t shift )
533 {
534 commpage_time_data *p32 = time_data32;
535 commpage_time_data *p64 = time_data64;
536 static uint32_t generation = 0;
537 uint32_t next_gen;
538
539 if (p32 == NULL) /* have commpages been allocated yet? */
540 return;
541
542 if ( generation != p32->nt_generation )
543 panic("nanotime trouble 1"); /* possibly not serialized */
544 if ( ns_base < p32->nt_ns_base )
545 panic("nanotime trouble 2");
546 if ((shift != 0) && ((_cpu_capabilities & kSlow)==0) )
547 panic("nanotime trouble 3");
548
549 next_gen = ++generation;
550 if (next_gen == 0)
551 next_gen = ++generation;
552
553 p32->nt_generation = 0; /* mark invalid, so commpage won't try to use it */
554 p64->nt_generation = 0;
555
556 p32->nt_tsc_base = tsc_base;
557 p64->nt_tsc_base = tsc_base;
558
559 p32->nt_ns_base = ns_base;
560 p64->nt_ns_base = ns_base;
561
562 p32->nt_scale = scale;
563 p64->nt_scale = scale;
564
565 p32->nt_shift = shift;
566 p64->nt_shift = shift;
567
568 p32->nt_generation = next_gen; /* mark data as valid */
569 p64->nt_generation = next_gen;
570 }
571
572
573 /* Disable commpage gettimeofday(), forcing commpage to call through to the kernel. */
574
575 void
576 commpage_disable_timestamp( void )
577 {
578 time_data32->gtod_generation = 0;
579 time_data64->gtod_generation = 0;
580 }
581
582
583 /* Update commpage gettimeofday() information. As with nanotime(), we interleave
584 * updates to the 32- and 64-bit commpage, in order to keep time more nearly in sync
585 * between the two environments.
586 *
587 * This routine must be serializeed by some external means, ie a lock.
588 */
589
590 void
591 commpage_set_timestamp(
592 uint64_t abstime,
593 uint64_t secs )
594 {
595 commpage_time_data *p32 = time_data32;
596 commpage_time_data *p64 = time_data64;
597 static uint32_t generation = 0;
598 uint32_t next_gen;
599
600 next_gen = ++generation;
601 if (next_gen == 0)
602 next_gen = ++generation;
603
604 p32->gtod_generation = 0; /* mark invalid, so commpage won't try to use it */
605 p64->gtod_generation = 0;
606
607 p32->gtod_ns_base = abstime;
608 p64->gtod_ns_base = abstime;
609
610 p32->gtod_sec_base = secs;
611 p64->gtod_sec_base = secs;
612
613 p32->gtod_generation = next_gen; /* mark data as valid */
614 p64->gtod_generation = next_gen;
615 }
616
617
618 /* Update _COMM_PAGE_MEMORY_PRESSURE. Called periodically from vm's compute_memory_pressure() */
619
620 void
621 commpage_set_memory_pressure(
622 unsigned int pressure )
623 {
624 char *cp;
625 uint32_t *ip;
626
627 cp = commPagePtr32;
628 if ( cp ) {
629 cp += (_COMM_PAGE_MEMORY_PRESSURE - _COMM_PAGE32_BASE_ADDRESS);
630 ip = (uint32_t*) (void *) cp;
631 *ip = (uint32_t) pressure;
632 }
633
634 cp = commPagePtr64;
635 if ( cp ) {
636 cp += (_COMM_PAGE_MEMORY_PRESSURE - _COMM_PAGE32_START_ADDRESS);
637 ip = (uint32_t*) (void *) cp;
638 *ip = (uint32_t) pressure;
639 }
640
641 }
642
643
644 /* Update _COMM_PAGE_SPIN_COUNT. We might want to reduce when running on a battery, etc. */
645
646 void
647 commpage_set_spin_count(
648 unsigned int count )
649 {
650 char *cp;
651 uint32_t *ip;
652
653 if (count == 0) /* we test for 0 after decrement, not before */
654 count = 1;
655
656 cp = commPagePtr32;
657 if ( cp ) {
658 cp += (_COMM_PAGE_SPIN_COUNT - _COMM_PAGE32_BASE_ADDRESS);
659 ip = (uint32_t*) (void *) cp;
660 *ip = (uint32_t) count;
661 }
662
663 cp = commPagePtr64;
664 if ( cp ) {
665 cp += (_COMM_PAGE_SPIN_COUNT - _COMM_PAGE32_START_ADDRESS);
666 ip = (uint32_t*) (void *) cp;
667 *ip = (uint32_t) count;
668 }
669
670 }
671
672 /* Updated every time a logical CPU goes offline/online */
673 void
674 commpage_update_active_cpus(void)
675 {
676 char *cp;
677 volatile uint8_t *ip;
678
679 /* At least 32-bit commpage must be initialized */
680 if (!commPagePtr32)
681 return;
682
683 simple_lock(&commpage_active_cpus_lock);
684
685 cp = commPagePtr32;
686 cp += (_COMM_PAGE_ACTIVE_CPUS - _COMM_PAGE32_BASE_ADDRESS);
687 ip = (volatile uint8_t*) cp;
688 *ip = (uint8_t) processor_avail_count;
689
690 cp = commPagePtr64;
691 if ( cp ) {
692 cp += (_COMM_PAGE_ACTIVE_CPUS - _COMM_PAGE32_START_ADDRESS);
693 ip = (volatile uint8_t*) cp;
694 *ip = (uint8_t) processor_avail_count;
695 }
696
697 simple_unlock(&commpage_active_cpus_lock);
698 }
699
700 /*
701 * Update the commpage data with the value of the "kdebug_enable"
702 * global so that userspace can avoid trapping into the kernel
703 * for kdebug_trace() calls. Serialization is handled
704 * by the caller in bsd/kern/kdebug.c.
705 */
706 void
707 commpage_update_kdebug_enable(void)
708 {
709 volatile uint32_t *saved_data_ptr;
710 char *cp;
711
712 cp = commPagePtr32;
713 if (cp) {
714 cp += (_COMM_PAGE_KDEBUG_ENABLE - _COMM_PAGE32_BASE_ADDRESS);
715 saved_data_ptr = (volatile uint32_t *)cp;
716 *saved_data_ptr = kdebug_enable;
717 }
718
719 cp = commPagePtr64;
720 if ( cp ) {
721 cp += (_COMM_PAGE_KDEBUG_ENABLE - _COMM_PAGE32_START_ADDRESS);
722 saved_data_ptr = (volatile uint32_t *)cp;
723 *saved_data_ptr = kdebug_enable;
724 }
725 }
726
727
728 /*
729 * update the commpage data for last known value of mach_absolute_time()
730 */
731
732 void
733 commpage_update_mach_approximate_time(uint64_t abstime)
734 {
735 #ifdef CONFIG_MACH_APPROXIMATE_TIME
736 uint64_t saved_data;
737 char *cp;
738
739 cp = commPagePtr32;
740 if ( cp ) {
741 cp += (_COMM_PAGE_APPROX_TIME - _COMM_PAGE32_BASE_ADDRESS);
742 saved_data = *(uint64_t *)cp;
743 if (saved_data < abstime) {
744 /* ignoring the success/fail return value assuming that
745 * if the value has been updated since we last read it,
746 * "someone" has a newer timestamp than us and ours is
747 * now invalid. */
748 OSCompareAndSwap64(saved_data, abstime, (uint64_t *)cp);
749 }
750 }
751 cp = commPagePtr64;
752 if ( cp ) {
753 cp += (_COMM_PAGE_APPROX_TIME - _COMM_PAGE32_START_ADDRESS);
754 saved_data = *(uint64_t *)cp;
755 if (saved_data < abstime) {
756 /* ignoring the success/fail return value assuming that
757 * if the value has been updated since we last read it,
758 * "someone" has a newer timestamp than us and ours is
759 * now invalid. */
760 OSCompareAndSwap64(saved_data, abstime, (uint64_t *)cp);
761 }
762 }
763 #else
764 #pragma unused (abstime)
765 #endif
766 }
767
768
769 extern user32_addr_t commpage_text32_location;
770 extern user64_addr_t commpage_text64_location;
771
772 /* Check to see if a given address is in the Preemption Free Zone (PFZ) */
773
774 uint32_t
775 commpage_is_in_pfz32(uint32_t addr32)
776 {
777 if ( (addr32 >= (commpage_text32_location + _COMM_TEXT_PFZ_START_OFFSET))
778 && (addr32 < (commpage_text32_location+_COMM_TEXT_PFZ_END_OFFSET))) {
779 return 1;
780 }
781 else
782 return 0;
783 }
784
785 uint32_t
786 commpage_is_in_pfz64(addr64_t addr64)
787 {
788 if ( (addr64 >= (commpage_text64_location + _COMM_TEXT_PFZ_START_OFFSET))
789 && (addr64 < (commpage_text64_location + _COMM_TEXT_PFZ_END_OFFSET))) {
790 return 1;
791 }
792 else
793 return 0;
794 }
795