]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/commpage/commpage.c
xnu-4570.41.2.tar.gz
[apple/xnu.git] / osfmk / i386 / commpage / commpage.c
CommitLineData
43866e37 1/*
6d2010ae 2 * Copyright (c) 2003-2010 Apple Inc. All rights reserved.
43866e37 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
43866e37 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
43866e37
A
27 */
28
55e303ae
A
29/*
30 * Here's what to do if you want to add a new routine to the comm page:
31 *
0c530ab8 32 * 1. Add a definition for it's address in osfmk/i386/cpu_capabilities.h,
55e303ae
A
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.
0c530ab8 39 * The source files should be in osfmk/i386/commpage/.
55e303ae
A
40 *
41 * 3. Add a ptr to your new commpage_descriptor(s) in the "routines"
0c530ab8
A
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.
55e303ae
A
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>
91447636 50#include <mach/vm_map.h>
b0d623f7 51#include <mach/mach_vm.h>
7e4a7d39
A
52#include <mach/machine.h>
53#include <i386/cpuid.h>
2d21ac55 54#include <i386/tsc.h>
6d2010ae 55#include <i386/rtclock_protos.h>
2d21ac55 56#include <i386/cpu_data.h>
b0d623f7
A
57#include <i386/machine_routines.h>
58#include <i386/misc_protos.h>
7e4a7d39 59#include <i386/cpuid.h>
43866e37
A
60#include <machine/cpu_capabilities.h>
61#include <machine/commpage.h>
55e303ae
A
62#include <machine/pmap.h>
63#include <vm/vm_kern.h>
91447636 64#include <vm/vm_map.h>
5ba3f43e 65#include <stdatomic.h>
b0d623f7 66
91447636
A
67#include <ipc/ipc_port.h>
68
0c530ab8 69#include <kern/page_decrypt.h>
6d2010ae 70#include <kern/processor.h>
4452a7af 71
a1c7dba1
A
72#include <sys/kdebug.h>
73
3e170ce0
A
74#if CONFIG_ATM
75#include <atm/atm_internal.h>
76#endif
77
0c530ab8
A
78/* the lists of commpage routines are in commpage_asm.s */
79extern commpage_descriptor* commpage_32_routines[];
80extern commpage_descriptor* commpage_64_routines[];
4452a7af 81
2d21ac55
A
82extern vm_map_t commpage32_map; // the shared submap, set up in vm init
83extern vm_map_t commpage64_map; // the shared submap, set up in vm init
316670eb
A
84extern vm_map_t commpage_text32_map; // the shared submap, set up in vm init
85extern vm_map_t commpage_text64_map; // the shared submap, set up in vm init
86
4452a7af 87
0c530ab8
A
88char *commPagePtr32 = NULL; // virtual addr in kernel map of 32-bit commpage
89char *commPagePtr64 = NULL; // ...and of 64-bit commpage
bd504ef0
A
90char *commPageTextPtr32 = NULL; // virtual addr in kernel map of 32-bit commpage
91char *commPageTextPtr64 = NULL; // ...and of 64-bit commpage
6601e61a 92
bd504ef0 93uint64_t _cpu_capabilities = 0; // define the capability vector
0c530ab8 94
b0d623f7
A
95typedef uint32_t commpage_address_t;
96
bd504ef0 97static commpage_address_t next; // next available address in comm page
0c530ab8
A
98
99static char *commPagePtr; // virtual addr in kernel map of commpage we are working on
b0d623f7 100static commpage_address_t commPageBaseOffset; // subtract from 32-bit runtime address to get offset in virtual commpage in kernel map
55e303ae 101
2d21ac55
A
102static commpage_time_data *time_data32 = NULL;
103static commpage_time_data *time_data64 = NULL;
5ba3f43e
A
104static new_commpage_timeofday_data_t *gtod_time_data32 = NULL;
105static new_commpage_timeofday_data_t *gtod_time_data64 = NULL;
106
2d21ac55 107
6d2010ae
A
108decl_simple_lock_data(static,commpage_active_cpus_lock);
109
55e303ae
A
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
117static void*
0c530ab8 118commpage_allocate(
2d21ac55 119 vm_map_t submap, // commpage32_map or commpage_map64
316670eb
A
120 size_t area_used, // _COMM_PAGE32_AREA_USED or _COMM_PAGE64_AREA_USED
121 vm_prot_t uperm)
55e303ae 122{
2d21ac55 123 vm_offset_t kernel_addr = 0; // address of commpage in kernel map
0c530ab8
A
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;
316670eb 128 kern_return_t kr;
0c530ab8
A
129
130 if (submap == NULL)
131 panic("commpage submap is null");
132
5ba3f43e 133 if ((kr = vm_map_kernel(kernel_map,
3e170ce0
A
134 &kernel_addr,
135 area_used,
136 0,
5ba3f43e
A
137 VM_FLAGS_ANYWHERE,
138 VM_KERN_MEMORY_OSFMK,
3e170ce0
A
139 NULL,
140 0,
141 FALSE,
142 VM_PROT_ALL,
143 VM_PROT_ALL,
144 VM_INHERIT_NONE)))
316670eb 145 panic("cannot allocate commpage %d", kr);
0c530ab8 146
5ba3f43e 147 if ((kr = vm_map_wire_kernel(kernel_map,
3e170ce0
A
148 kernel_addr,
149 kernel_addr+area_used,
5ba3f43e 150 VM_PROT_DEFAULT, VM_KERN_MEMORY_OSFMK,
3e170ce0 151 FALSE)))
316670eb 152 panic("cannot wire commpage: %d", kr);
0c530ab8
A
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 */
39236c6e 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))
316670eb 163 panic("cannot find commpage entry %d", kr);
3e170ce0 164 VME_OBJECT(entry)->copy_strategy = MEMORY_OBJECT_COPY_NONE;
0c530ab8 165
316670eb 166 if ((kr = mach_make_memory_entry( kernel_map, // target map
0c530ab8
A
167 &size, // size
168 kernel_addr, // offset (address in kernel map)
316670eb 169 uperm, // protections as specified
0c530ab8 170 &handle, // this is the object handle we get
316670eb
A
171 NULL ))) // parent_entry (what is this?)
172 panic("cannot make entry for commpage %d", kr);
0c530ab8 173
5ba3f43e 174 if ((kr = vm_map_64_kernel( submap, // target map (shared submap)
0c530ab8
A
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)
5ba3f43e 179 VM_KERN_MEMORY_NONE,
0c530ab8
A
180 handle, // port is the memory entry we just made
181 0, // offset (map 1st page in memory entry)
182 FALSE, // copy
316670eb
A
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);
0c530ab8
A
187
188 ipc_port_release(handle);
316670eb
A
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);
0c530ab8 195
b0d623f7 196 return (void*)(intptr_t)kernel_addr; // return address in kernel map
55e303ae
A
197}
198
199/* Get address (in kernel map) of a commpage field. */
200
91447636 201static void*
55e303ae 202commpage_addr_of(
b0d623f7 203 commpage_address_t addr_at_runtime )
55e303ae 204{
b0d623f7 205 return (void*) ((uintptr_t)commPagePtr + (addr_at_runtime - commPageBaseOffset));
55e303ae
A
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 */
211static int
212commpage_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}
43866e37 225
55e303ae 226/* Initialize kernel version of _cpu_capabilities vector (used by KEXTs.) */
43866e37 227
55e303ae
A
228static void
229commpage_init_cpu_capabilities( void )
230{
bd504ef0 231 uint64_t bits;
55e303ae
A
232 int cpus;
233 ml_cpu_info_t cpu_info;
43866e37 234
55e303ae
A
235 bits = 0;
236 ml_cpu_get_info(&cpu_info);
237
238 switch (cpu_info.vector_unit) {
6d2010ae
A
239 case 9:
240 bits |= kHasAVX1_0;
241 /* fall thru */
2d21ac55
A
242 case 8:
243 bits |= kHasSSE4_2;
244 /* fall thru */
245 case 7:
246 bits |= kHasSSE4_1;
247 /* fall thru */
0c530ab8
A
248 case 6:
249 bits |= kHasSupplementalSSE3;
250 /* fall thru */
55e303ae 251 case 5:
91447636 252 bits |= kHasSSE3;
55e303ae
A
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
55e303ae
A
280 bits |= (cpus << kNumCPUsShift);
281
91447636
A
282 bits |= kFastThreadLocalStorage; // we use %gs for TLS
283
bd504ef0
A
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);
a1c7dba1
A
310 setif(bits, kHasRDSEED, cpuid_features() &
311 CPUID_LEAF7_FEATURE_RDSEED);
312 setif(bits, kHasADX, cpuid_features() &
313 CPUID_LEAF7_FEATURE_ADX);
bd504ef0 314
5ba3f43e 315#if 0 /* The kernel doesn't support MPX or SGX */
2dced7af
A
316 setif(bits, kHasMPX, cpuid_leaf7_features() &
317 CPUID_LEAF7_FEATURE_MPX);
318 setif(bits, kHasSGX, cpuid_leaf7_features() &
319 CPUID_LEAF7_FEATURE_SGX);
5ba3f43e
A
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 */
bd504ef0
A
341 uint64_t misc_enable = rdmsr64(MSR_IA32_MISC_ENABLE);
342 setif(bits, kHasENFSTRG, (misc_enable & 1ULL) &&
343 (cpuid_leaf7_features() &
fe8ab488 344 CPUID_LEAF7_FEATURE_ERMS));
bd504ef0 345
55e303ae
A
346 _cpu_capabilities = bits; // set kernel version for use by drivers etc
347}
348
fe8ab488
A
349/* initialize the approx_time_supported flag and set the approx time to 0.
350 * Called during initial commpage population.
351 */
352static void
353commpage_mach_approximate_time_init(void)
354{
39037602 355 char *cp = commPagePtr32;
fe8ab488
A
356 uint8_t supported;
357
358#ifdef CONFIG_MACH_APPROXIMATE_TIME
359 supported = 1;
360#else
361 supported = 0;
362#endif
363 if ( cp ) {
39037602 364 cp += (_COMM_PAGE_APPROX_TIME_SUPPORTED - _COMM_PAGE32_BASE_ADDRESS);
fe8ab488
A
365 *(boolean_t *)cp = supported;
366 }
39037602
A
367
368 cp = commPagePtr64;
fe8ab488 369 if ( cp ) {
39037602 370 cp += (_COMM_PAGE_APPROX_TIME_SUPPORTED - _COMM_PAGE32_START_ADDRESS);
fe8ab488
A
371 *(boolean_t *)cp = supported;
372 }
373 commpage_update_mach_approximate_time(0);
374}
375
39037602
A
376static void
377commpage_mach_continuous_time_init(void)
378{
379 commpage_update_mach_continuous_time(0);
380}
381
382static void
383commpage_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}
fe8ab488 390
bd504ef0 391uint64_t
2d21ac55 392_get_cpu_capabilities(void)
0c530ab8
A
393{
394 return _cpu_capabilities;
395}
396
55e303ae
A
397/* Copy data into commpage. */
398
399static void
400commpage_stuff(
b0d623f7 401 commpage_address_t address,
0c530ab8 402 const void *source,
55e303ae
A
403 int length )
404{
405 void *dest = commpage_addr_of(address);
406
b0d623f7 407 if (address < next)
6d2010ae 408 panic("commpage overlap at address 0x%p, 0x%x < 0x%x", dest, address, next);
55e303ae
A
409
410 bcopy(source,dest,length);
43866e37 411
b0d623f7 412 next = address + length;
55e303ae
A
413}
414
415/* Copy a routine into comm page if it matches running machine.
416 */
417static void
418commpage_stuff_routine(
bd504ef0 419 commpage_descriptor *rd )
55e303ae 420{
bd504ef0 421 commpage_stuff(rd->commpage_address,rd->code_address,rd->code_length);
55e303ae
A
422}
423
0c530ab8 424/* Fill in the 32- or 64-bit commpage. Called once for each.
55e303ae
A
425 */
426
0c530ab8
A
427static void
428commpage_populate_one(
2d21ac55 429 vm_map_t submap, // commpage32_map or compage64_map
0c530ab8
A
430 char ** kernAddressPtr, // &commPagePtr32 or &commPagePtr64
431 size_t area_used, // _COMM_PAGE32_AREA_USED or _COMM_PAGE64_AREA_USED
b0d623f7 432 commpage_address_t base_offset, // will become commPageBaseOffset
2d21ac55 433 commpage_time_data** time_data, // &time_data32 or &time_data64
5ba3f43e 434 new_commpage_timeofday_data_t** gtod_time_data, // &gtod_time_data32 or &gtod_time_data64
316670eb
A
435 const char* signature, // "commpage 32-bit" or "commpage 64-bit"
436 vm_prot_t uperm)
55e303ae 437{
bd504ef0
A
438 uint8_t c1;
439 uint16_t c2;
440 int c4;
441 uint64_t c8;
6d2010ae 442 uint32_t cfamily;
55e303ae 443 short version = _COMM_PAGE_THIS_VERSION;
55e303ae 444
b0d623f7 445 next = 0;
316670eb 446 commPagePtr = (char *)commpage_allocate( submap, (vm_size_t) area_used, uperm );
0c530ab8
A
447 *kernAddressPtr = commPagePtr; // save address either in commPagePtr32 or 64
448 commPageBaseOffset = base_offset;
b0d623f7 449
2d21ac55 450 *time_data = commpage_addr_of( _COMM_PAGE_TIME_DATA_START );
5ba3f43e 451 *gtod_time_data = commpage_addr_of( _COMM_PAGE_NEWTIMEOFDAY_DATA );
55e303ae
A
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.
bd504ef0
A
455 * Note: the 32-bit cpu_capabilities vector is retained in addition to
456 * the expanded 64-bit vector.
55e303ae 457 */
bd504ef0
A
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));
6d2010ae 460 commpage_stuff(_COMM_PAGE_VERSION,&version,sizeof(short));
bd504ef0 461 commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES,&_cpu_capabilities,sizeof(uint32_t));
0c530ab8 462
6d2010ae
A
463 c2 = 32; // default
464 if (_cpu_capabilities & kCache64)
91447636
A
465 c2 = 64;
466 else if (_cpu_capabilities & kCache128)
467 c2 = 128;
468 commpage_stuff(_COMM_PAGE_CACHE_LINESIZE,&c2,2);
bd504ef0 469
b0d623f7
A
470 c4 = MP_SPIN_TRIES;
471 commpage_stuff(_COMM_PAGE_SPIN_COUNT,&c4,4);
91447636 472
6d2010ae
A
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);
6601e61a 484
b0d623f7
A
485 if (next > _COMM_PAGE_END)
486 panic("commpage overflow: next = 0x%08x, commPagePtr = 0x%p", next, commPagePtr);
91447636 487
43866e37 488}
91447636 489
0c530ab8
A
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 */
91447636
A
497
498void
0c530ab8 499commpage_populate( void )
91447636 500{
0c530ab8
A
501 commpage_init_cpu_capabilities();
502
2d21ac55 503 commpage_populate_one( commpage32_map,
0c530ab8
A
504 &commPagePtr32,
505 _COMM_PAGE32_AREA_USED,
506 _COMM_PAGE32_BASE_ADDRESS,
2d21ac55 507 &time_data32,
5ba3f43e 508 &gtod_time_data32,
316670eb
A
509 "commpage 32-bit",
510 VM_PROT_READ);
b0d623f7 511#ifndef __LP64__
0c530ab8
A
512 pmap_commpage32_init((vm_offset_t) commPagePtr32, _COMM_PAGE32_BASE_ADDRESS,
513 _COMM_PAGE32_AREA_USED/INTEL_PGBYTES);
b0d623f7 514#endif
2d21ac55 515 time_data64 = time_data32; /* if no 64-bit commpage, point to 32-bit */
5ba3f43e 516 gtod_time_data64 = gtod_time_data32;
0c530ab8
A
517
518 if (_cpu_capabilities & k64Bit) {
2d21ac55 519 commpage_populate_one( commpage64_map,
0c530ab8
A
520 &commPagePtr64,
521 _COMM_PAGE64_AREA_USED,
b0d623f7 522 _COMM_PAGE32_START_ADDRESS, /* commpage address are relative to 32-bit commpage placement */
2d21ac55 523 &time_data64,
5ba3f43e 524 &gtod_time_data64,
316670eb
A
525 "commpage 64-bit",
526 VM_PROT_READ);
b0d623f7 527#ifndef __LP64__
0c530ab8
A
528 pmap_commpage64_init((vm_offset_t) commPagePtr64, _COMM_PAGE64_BASE_ADDRESS,
529 _COMM_PAGE64_AREA_USED/INTEL_PGBYTES);
b0d623f7 530#endif
0c530ab8 531 }
6601e61a 532
6d2010ae
A
533 simple_lock_init(&commpage_active_cpus_lock, 0);
534
535 commpage_update_active_cpus();
a1c7dba1 536 commpage_mach_approximate_time_init();
39037602
A
537 commpage_mach_continuous_time_init();
538 commpage_boottime_init();
0c530ab8 539 rtc_nanotime_init_commpage();
39037602 540 commpage_update_kdebug_state();
3e170ce0
A
541#if CONFIG_ATM
542 commpage_update_atm_diagnostic_config(atm_get_diagnostic_config());
543#endif
91447636 544}
2d21ac55 545
316670eb
A
546/* Fill in the common routines during kernel initialization.
547 * This is called before user-mode code is running.
548 */
549void commpage_text_populate( void ){
550 commpage_descriptor **rd;
551
bd504ef0 552 next = 0;
316670eb
A
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 }
316670eb
A
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) {
bd504ef0 573 next = 0;
316670eb
A
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
316670eb
A
592 if (next > _COMM_PAGE_TEXT_END)
593 panic("commpage text overflow: next=0x%08x, commPagePtr=%p", next, commPagePtr);
594
595}
2d21ac55 596
bd504ef0 597/* Update commpage nanotime information.
2d21ac55
A
598 *
599 * This routine must be serialized by some external means, ie a lock.
600 */
601
602void
603commpage_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");
bd504ef0 621 if ((shift != 0) && ((_cpu_capabilities & kSlow)==0) )
2d21ac55
A
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
2d21ac55
A
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 */
5ba3f43e
A
653
654void
655commpage_set_timestamp(
656 uint64_t abstime,
657 uint64_t sec,
658 uint64_t frac,
659 uint64_t scale,
660 uint64_t tick_per_sec)
2d21ac55 661{
5ba3f43e
A
662 new_commpage_timeofday_data_t *p32 = gtod_time_data32;
663 new_commpage_timeofday_data_t *p64 = gtod_time_data64;
2d21ac55 664
5ba3f43e
A
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;
b0d623f7 673
5ba3f43e
A
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}
b0d623f7
A
683
684/* Update _COMM_PAGE_MEMORY_PRESSURE. Called periodically from vm's compute_memory_pressure() */
685
686void
687commpage_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);
bd504ef0 696 ip = (uint32_t*) (void *) cp;
b0d623f7
A
697 *ip = (uint32_t) pressure;
698 }
699
700 cp = commPagePtr64;
701 if ( cp ) {
702 cp += (_COMM_PAGE_MEMORY_PRESSURE - _COMM_PAGE32_START_ADDRESS);
bd504ef0 703 ip = (uint32_t*) (void *) cp;
b0d623f7
A
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
712void
713commpage_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);
bd504ef0 725 ip = (uint32_t*) (void *) cp;
b0d623f7
A
726 *ip = (uint32_t) count;
727 }
728
729 cp = commPagePtr64;
730 if ( cp ) {
731 cp += (_COMM_PAGE_SPIN_COUNT - _COMM_PAGE32_START_ADDRESS);
bd504ef0 732 ip = (uint32_t*) (void *) cp;
b0d623f7
A
733 *ip = (uint32_t) count;
734 }
735
736}
737
6d2010ae
A
738/* Updated every time a logical CPU goes offline/online */
739void
740commpage_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
a1c7dba1 766/*
39037602
A
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) { ... }"
a1c7dba1
A
773 */
774void
39037602 775commpage_update_kdebug_state(void)
a1c7dba1
A
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;
39037602 784 *saved_data_ptr = kdebug_commpage_state();
a1c7dba1
A
785 }
786
787 cp = commPagePtr64;
39037602 788 if (cp) {
a1c7dba1
A
789 cp += (_COMM_PAGE_KDEBUG_ENABLE - _COMM_PAGE32_START_ADDRESS);
790 saved_data_ptr = (volatile uint32_t *)cp;
39037602 791 *saved_data_ptr = kdebug_commpage_state();
a1c7dba1
A
792 }
793}
794
3e170ce0
A
795/* Ditto for atm_diagnostic_config */
796void
797commpage_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}
a1c7dba1 816
fe8ab488
A
817/*
818 * update the commpage data for last known value of mach_absolute_time()
819 */
820
821void
822commpage_update_mach_approximate_time(uint64_t abstime)
823{
824#ifdef CONFIG_MACH_APPROXIMATE_TIME
825 uint64_t saved_data;
a1c7dba1
A
826 char *cp;
827
828 cp = commPagePtr32;
fe8ab488 829 if ( cp ) {
a1c7dba1 830 cp += (_COMM_PAGE_APPROX_TIME - _COMM_PAGE32_BASE_ADDRESS);
5ba3f43e 831 saved_data = atomic_load_explicit((_Atomic uint64_t *)(uintptr_t)cp, memory_order_relaxed);
fe8ab488
A
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. */
5ba3f43e
A
837 atomic_compare_exchange_strong_explicit((_Atomic uint64_t *)(uintptr_t)cp,
838 &saved_data, abstime, memory_order_relaxed, memory_order_relaxed);
fe8ab488
A
839 }
840 }
a1c7dba1 841 cp = commPagePtr64;
fe8ab488 842 if ( cp ) {
a1c7dba1 843 cp += (_COMM_PAGE_APPROX_TIME - _COMM_PAGE32_START_ADDRESS);
5ba3f43e 844 saved_data = atomic_load_explicit((_Atomic uint64_t *)(uintptr_t)cp, memory_order_relaxed);
fe8ab488
A
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. */
5ba3f43e
A
850 atomic_compare_exchange_strong_explicit((_Atomic uint64_t *)(uintptr_t)cp,
851 &saved_data, abstime, memory_order_relaxed, memory_order_relaxed);
fe8ab488
A
852 }
853 }
854#else
855#pragma unused (abstime)
856#endif
857}
858
39037602
A
859void
860commpage_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
876void
877commpage_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
fe8ab488 893
316670eb
A
894extern user32_addr_t commpage_text32_location;
895extern user64_addr_t commpage_text64_location;
b0d623f7
A
896
897/* Check to see if a given address is in the Preemption Free Zone (PFZ) */
898
899uint32_t
900commpage_is_in_pfz32(uint32_t addr32)
901{
316670eb
A
902 if ( (addr32 >= (commpage_text32_location + _COMM_TEXT_PFZ_START_OFFSET))
903 && (addr32 < (commpage_text32_location+_COMM_TEXT_PFZ_END_OFFSET))) {
b0d623f7
A
904 return 1;
905 }
906 else
907 return 0;
908}
909
910uint32_t
911commpage_is_in_pfz64(addr64_t addr64)
912{
316670eb
A
913 if ( (addr64 >= (commpage_text64_location + _COMM_TEXT_PFZ_START_OFFSET))
914 && (addr64 < (commpage_text64_location + _COMM_TEXT_PFZ_END_OFFSET))) {
b0d623f7
A
915 return 1;
916 }
917 else
918 return 0;
919}
920