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