1 /* 2 * Copyright (c) 2003-2019 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 #ifndef _I386_COMMPAGE_H 30 #define _I386_COMMPAGE_H 31 32 #ifndef __ASSEMBLER__ 33 #include <stdint.h> 34 #include <mach/boolean.h> 35 #include <mach/vm_types.h> 36 #include <machine/cpu_capabilities.h> 37 #endif /* __ASSEMBLER__ */ 38 39 /* The following macro is used to generate the 64-bit commpage address for a given 40 * routine, based on its 32-bit address. This is used in the kernel to compile 41 * the 64-bit commpage. Since the kernel can be a 32-bit object, cpu_capabilities.h 42 * only defines the 32-bit address. 43 */ 44 #define _COMM_PAGE_32_TO_64( ADDRESS ) ( ADDRESS + _COMM_PAGE64_START_ADDRESS - _COMM_PAGE32_START_ADDRESS ) 45 46 47 #ifdef __ASSEMBLER__ 48 49 #define COMMPAGE_DESCRIPTOR_NAME(label) _commpage_ ## label 50 51 #define COMMPAGE_DESCRIPTOR_FIELD_POINTER .quad 52 #define COMMPAGE_DESCRIPTOR_REFERENCE(label) \ 53 .quad COMMPAGE_DESCRIPTOR_NAME(label) 54 55 #define COMMPAGE_FUNCTION_START(label, codetype, alignment) \ 56 .text ;\ 57 .code ## codetype ;\ 58 .align alignment, 0x90 ;\ 59 L ## label ## : 60 61 #define COMMPAGE_DESCRIPTOR(label, address) \ 62 L ## label ## _end: ;\ 63 .set L ## label ## _size, L ## label ## _end - L ## label ;\ 64 .const_data ;\ 65 .private_extern COMMPAGE_DESCRIPTOR_NAME(label) ;\ 66 COMMPAGE_DESCRIPTOR_NAME(label) ## : ;\ 67 COMMPAGE_DESCRIPTOR_FIELD_POINTER L ## label ;\ 68 .long L ## label ## _size ;\ 69 .long address ;\ 70 .text 71 72 73 /* COMMPAGE_CALL(target,from,start) 74 * 75 * This macro compiles a relative near call to one 76 * commpage routine from another. 77 * The assembler cannot handle this directly because the code 78 * is not being assembled at the address at which it will execute. 79 * The alternative to this macro would be to use an 80 * indirect call, which is slower because the target of an 81 * indirect branch is poorly predicted. 82 * The macro arguments are: 83 * target = the commpage routine we are calling 84 * from = the commpage routine we are in now 85 * start = the label at the start of the code for this func 86 * This is admitedly ugly and fragile. Is there a better way? 87 */ 88 #define COMMPAGE_CALL(target, from, start) \ 89 COMMPAGE_CALL_INTERNAL(target,from,start,__LINE__) 90 91 #define COMMPAGE_CALL_INTERNAL(target, from, start, unique) \ 92 .byte 0xe8 ;\ 93 .set UNIQUEID(unique), L ## start - . + target - from - 4 ;\ 94 .long UNIQUEID(unique) 95 96 #define UNIQUEID(name) L ## name 97 98 /* COMMPAGE_JMP(target,from,start) 99 * 100 * This macro perform a jump to another commpage routine. 101 * Used to return from the PFZ by jumping via a return outside the PFZ. 102 */ 103 #define COMMPAGE_JMP(target, from, start) \ 104 jmp L ## start - from + target 105 106 #else /* __ASSEMBLER__ */ 107 108 /* Each potential commpage routine is described by one of these. 109 * Note that the COMMPAGE_DESCRIPTOR macro (above), used in 110 * assembly language, must agree with this. 111 */ 112 113 typedef struct commpage_descriptor { 114 void *code_address; // address of code 115 uint32_t code_length; // length in bytes 116 uint32_t commpage_address; // put at this address (_COMM_PAGE_BCOPY etc) 117 } commpage_descriptor; 118 119 120 /* Warning: following structure must match the layout of the commpage. */ 121 /* This is the data starting at _COMM_PAGE_TIME_DATA_START, ie for nanotime() and gettimeofday() */ 122 123 typedef volatile struct commpage_time_data { 124 uint64_t nt_tsc_base; // _COMM_PAGE_NT_TSC_BASE 125 uint32_t nt_scale; // _COMM_PAGE_NT_SCALE 126 uint32_t nt_shift; // _COMM_PAGE_NT_SHIFT 127 uint64_t nt_ns_base; // _COMM_PAGE_NT_NS_BASE 128 uint32_t nt_generation; // _COMM_PAGE_NT_GENERATION 129 uint32_t gtod_generation; // _COMM_PAGE_GTOD_GENERATION 130 uint64_t gtod_ns_base; // _COMM_PAGE_GTOD_NS_BASE 131 uint64_t gtod_sec_base; // _COMM_PAGE_GTOD_SEC_BASE 132 } commpage_time_data; 133 134 extern char *commPagePtr32; // virt address of 32-bit commpage in kernel map 135 extern char *commPagePtr64; // ...and of 64-bit commpage 136 137 extern void commpage_set_timestamp(uint64_t abstime, uint64_t sec, uint64_t frac, uint64_t scale, uint64_t tick_per_sec); 138 #define commpage_disable_timestamp() commpage_set_timestamp( 0, 0, 0, 0, 0 ); 139 extern void commpage_set_nanotime(uint64_t tsc_base, uint64_t ns_base, uint32_t scale, uint32_t shift); 140 extern void commpage_set_memory_pressure(unsigned int pressure); 141 extern void commpage_set_spin_count(unsigned int count); 142 extern void commpage_sched_gen_inc(void); 143 extern void commpage_update_active_cpus(void); 144 extern void commpage_update_mach_approximate_time(uint64_t abstime); 145 extern void commpage_update_mach_continuous_time(uint64_t sleeptime); 146 extern void commpage_update_boottime(uint64_t boottime_usec); 147 extern void commpage_update_kdebug_state(void); 148 extern void commpage_update_atm_diagnostic_config(uint32_t); 149 extern void commpage_update_dof(boolean_t enabled); 150 extern void commpage_update_dyld_flags(uint64_t value); 151 extern void commpage_post_ucode_update(void); 152 153 extern uint32_t commpage_is_in_pfz32(uint32_t); 154 extern uint32_t commpage_is_in_pfz64(addr64_t); 155 156 #endif /* __ASSEMBLER__ */ 157 158 #endif /* _I386_COMMPAGE_H */ 159