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