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