xref: /xnu-8020.121.3/osfmk/i386/cpu_capabilities.h (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
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 #ifdef  PRIVATE
29 
30 #ifndef _I386_CPU_CAPABILITIES_H
31 #define _I386_CPU_CAPABILITIES_H
32 
33 #if defined (__i386__) || defined (__x86_64__)
34 
35 #ifndef __ASSEMBLER__
36 #include <stdint.h>
37 #endif
38 
39 /*
40  * This API only supported for Apple internal use.
41  */
42 
43 /* Bit definitions for _cpu_capabilities: */
44 
45 #define kHasMMX                         0x00000001
46 #define kHasSSE                         0x00000002
47 #define kHasSSE2                        0x00000004
48 #define kHasSSE3                        0x00000008
49 #define kCache32                        0x00000010      /* cache line size is 32 bytes */
50 #define kCache64                        0x00000020
51 #define kCache128                       0x00000040
52 #define kFastThreadLocalStorage         0x00000080      /* TLS ptr is kept in a user-mode-readable register */
53 #define kHasSupplementalSSE3            0x00000100
54 #define k64Bit                          0x00000200      /* processor supports EM64T (not what mode you're running in) */
55 #define kHasSSE4_1                      0x00000400
56 #define kHasSSE4_2                      0x00000800
57 #define kHasAES                         0x00001000
58 #define kInOrderPipeline                0x00002000
59 #define kSlow                           0x00004000      /* tsc < nanosecond */
60 #define kUP                             0x00008000      /* set if (kNumCPUs == 1) */
61 #define kNumCPUs                        0x00FF0000      /* number of CPUs (see _NumCPUs() below) */
62 #define kNumCPUsShift                   16
63 #define kHasAVX1_0                      0x01000000
64 #define kHasRDRAND                      0x02000000
65 #define kHasF16C                        0x04000000
66 #define kHasENFSTRG                     0x08000000
67 #define kHasFMA                         0x10000000
68 #define kHasAVX2_0                      0x20000000
69 #define kHasBMI1                        0x40000000
70 #define kHasBMI2                        0x80000000
71 /* Extending into 64-bits from here: */
72 #define kHasRTM                 0x0000000100000000ULL
73 #define kHasHLE                 0x0000000200000000ULL
74 #define kHasRDSEED              0x0000000800000000ULL
75 #define kHasADX                 0x0000000400000000ULL
76 #define kHasMPX                 0x0000001000000000ULL
77 #define kHasSGX                 0x0000002000000000ULL
78 #define kHasAVX512F             0x0000004000000000ULL
79 #define kHasAVX512CD            0x0000008000000000ULL
80 #define kHasAVX512DQ            0x0000010000000000ULL
81 #define kHasAVX512BW            0x0000020000000000ULL
82 #define kHasAVX512IFMA          0x0000040000000000ULL
83 #define kHasAVX512VBMI          0x0000080000000000ULL
84 #define kHasAVX512VL            0x0000100000000000ULL
85 #define kHasVAES                0x0000200000000000ULL
86 #define kHasVPCLMULQDQ          0x0000400000000000ULL
87 #define kHasAVX512VNNI          0x0000800000000000ULL
88 #define kHasAVX512BITALG        0x0001000000000000ULL
89 #define kHasAVX512VPOPCNTDQ     0x0002000000000000ULL
90 
91 #define kIsTranslated           0x4000000000000000ULL
92 
93 #ifndef __ASSEMBLER__
94 #include <sys/cdefs.h>
95 #include <sys/commpage.h>
96 
97 __BEGIN_DECLS
98 extern uint64_t  _get_cpu_capabilities( void );
99 __END_DECLS
100 
101 __inline static
102 int
_NumCPUs(void)103 _NumCPUs( void )
104 {
105 	return (int) (_get_cpu_capabilities() & kNumCPUs) >> kNumCPUsShift;
106 }
107 
108 #endif /* __ASSEMBLER__ */
109 
110 /* The following macro is used to generate the 64-bit commpage address for a given
111  * routine, based on its 32-bit address.  This is used in the kernel to compile
112  * the 64-bit commpage.  Since the kernel can be a 32-bit object, cpu_capabilities.h
113  * only defines the 32-bit address.
114  */
115 #define _COMM_PAGE_32_TO_64( ADDRESS )  ( ADDRESS + _COMM_PAGE64_START_ADDRESS - _COMM_PAGE32_START_ADDRESS )
116 
117 
118 /*
119  * The shared kernel/user "comm page(s)":
120  *
121  * The last several pages of every address space are reserved for the kernel/user
122  * "comm area". During system initialization, the kernel populates the comm pages with
123  * code customized for the particular processor and platform.
124  *
125  * Because Mach VM cannot map the last page of an address space, we don't use it.
126  */
127 
128 #define _COMM_PAGE32_AREA_LENGTH        ( 1 * 4096 )                            /* reserved length of entire comm area */
129 #define _COMM_PAGE32_BASE_ADDRESS       ( 0xffff0000 )                          /* base address of allocated memory */
130 #define _COMM_PAGE32_START_ADDRESS      ( _COMM_PAGE32_BASE_ADDRESS )   /* address traditional commpage code starts on */
131 #define _COMM_PAGE32_AREA_USED          ( 1 * 4096 )                            /* this is the amt actually allocated */
132 #define _COMM_PAGE32_SIGS_OFFSET        0x8000                                      /* offset to routine signatures */
133 
134 #define _COMM_PAGE64_AREA_LENGTH        ( 1 * 4096 )                    /* reserved length of entire comm area (2MB) */
135 #ifdef __ASSEMBLER__
136 #define _COMM_PAGE64_BASE_ADDRESS       ( 0x00007fffffe00000 )   /* base address of allocated memory */
137 #else /* __ASSEMBLER__ */
138 #define _COMM_PAGE64_BASE_ADDRESS       ( 0x00007fffffe00000ULL )   /* base address of allocated memory */
139 #endif /* __ASSEMBLER__ */
140 #define _COMM_PAGE64_START_ADDRESS      ( _COMM_PAGE64_BASE_ADDRESS )   /* address traditional commpage code starts on */
141 #define _COMM_PAGE64_AREA_USED          ( 1 * 4096 )                            /* this is the amt actually populated */
142 
143 /* no need for an Objective-C area on Intel */
144 #define _COMM_PAGE32_OBJC_SIZE          0ULL
145 #define _COMM_PAGE32_OBJC_BASE          0ULL
146 #define _COMM_PAGE64_OBJC_SIZE          0ULL
147 #define _COMM_PAGE64_OBJC_BASE          0ULL
148 
149 #ifdef KERNEL_PRIVATE
150 
151 /* Inside the kernel, comm page addresses are absolute addresses
152  * assuming they are a part of the 32-bit commpage. They may
153  * be mapped somewhere else, especially for the 64-bit commpage.
154  */
155 #define _COMM_PAGE_START_ADDRESS        _COMM_PAGE32_START_ADDRESS
156 #define _COMM_PAGE_SIGS_OFFSET          _COMM_PAGE32_SIGS_OFFSET
157 
158 #else /* !KERNEL_PRIVATE */
159 
160 /*
161  * <sys/commpage.h> defines a couple of conveniency macros
162  * to help read data from the commpage.
163  */
164 #if defined(__i386__)
165 
166 #define _COMM_PAGE_AREA_LENGTH          _COMM_PAGE32_AREA_LENGTH
167 #define _COMM_PAGE_BASE_ADDRESS         _COMM_PAGE32_BASE_ADDRESS
168 #define _COMM_PAGE_START_ADDRESS        _COMM_PAGE32_START_ADDRESS
169 #define _COMM_PAGE_AREA_USED            _COMM_PAGE32_AREA_USED
170 #define _COMM_PAGE_SIGS_OFFSET          _COMM_PAGE32_SIGS_OFFSET
171 
172 #elif defined(__x86_64__)
173 
174 #define _COMM_PAGE_AREA_LENGTH          _COMM_PAGE64_AREA_LENGTH
175 #define _COMM_PAGE_BASE_ADDRESS         _COMM_PAGE64_BASE_ADDRESS
176 #define _COMM_PAGE_START_ADDRESS        _COMM_PAGE64_START_ADDRESS
177 #define _COMM_PAGE_AREA_USED            _COMM_PAGE64_AREA_USED
178 
179 #else
180 #error architecture not supported
181 #endif
182 
183 #endif /* !KERNEL_PRIVATE */
184 
185 /* data in the comm page */
186 
187 #define _COMM_PAGE_SIGNATURE            (_COMM_PAGE_START_ADDRESS+0x000)        /* first 16 bytes are a signature */
188 #define _COMM_PAGE_SIGNATURELEN         (0x10)
189 #define _COMM_PAGE_CPU_CAPABILITIES64   (_COMM_PAGE_START_ADDRESS+0x010)        /* uint64_t _cpu_capabilities */
190 #define _COMM_PAGE_UNUSED               (_COMM_PAGE_START_ADDRESS+0x018)        /* 6 unused bytes */
191 #define _COMM_PAGE_VERSION              (_COMM_PAGE_START_ADDRESS+0x01E)        /* 16-bit version# */
192 #define _COMM_PAGE_THIS_VERSION         14
193 
194 #define _COMM_PAGE_CPU_CAPABILITIES     (_COMM_PAGE_START_ADDRESS+0x020)        /* uint32_t _cpu_capabilities (retained for compatibility) */
195 #define _COMM_PAGE_NCPUS                (_COMM_PAGE_START_ADDRESS+0x022)        /* uint8_t number of configured CPUs (hw.logicalcpu at boot time) */
196 #define _COMM_PAGE_UNUSED0              (_COMM_PAGE_START_ADDRESS+0x024)        /* 2 unused bytes, previouly reserved for expansion of cpu_capabilities */
197 #define _COMM_PAGE_CACHE_LINESIZE       (_COMM_PAGE_START_ADDRESS+0x026)        /* uint16_t cache line size */
198 
199 #define _COMM_PAGE_UNUSED4              (_COMM_PAGE_START_ADDRESS+0x028)        /* used to be _COMM_PAGE_SCHED_GEN: uint32_t scheduler generation number (count of pre-emptions) */
200 #define _COMM_PAGE_MEMORY_PRESSURE      (_COMM_PAGE_START_ADDRESS+0x02c)        /* uint32_t copy of vm_memory_pressure */
201 #define _COMM_PAGE_UNUSED3              (_COMM_PAGE_START_ADDRESS+0x030)        /* used to be _COMM_PAGE_SPIN_COUNT: uint32_t max spin count for mutex's */
202 
203 #define _COMM_PAGE_ACTIVE_CPUS          (_COMM_PAGE_START_ADDRESS+0x034)        /* uint8_t number of active CPUs (hw.activecpu) */
204 #define _COMM_PAGE_PHYSICAL_CPUS        (_COMM_PAGE_START_ADDRESS+0x035)        /* uint8_t number of physical CPUs (hw.physicalcpu_max) */
205 #define _COMM_PAGE_LOGICAL_CPUS         (_COMM_PAGE_START_ADDRESS+0x036)        /* uint8_t number of logical CPUs (hw.logicalcpu_max) */
206 #define _COMM_PAGE_UNUSED1              (_COMM_PAGE_START_ADDRESS+0x037)        /* 1 unused bytes */
207 #define _COMM_PAGE_MEMORY_SIZE          (_COMM_PAGE_START_ADDRESS+0x038)        /* uint64_t max memory size */
208 
209 #define _COMM_PAGE_CPUFAMILY            (_COMM_PAGE_START_ADDRESS+0x040)        /* uint32_t hw.cpufamily, x86*/
210 #define _COMM_PAGE_KDEBUG_ENABLE        (_COMM_PAGE_START_ADDRESS+0x044)        /* uint32_t export "kdebug_enable" to userspace */
211 #define _COMM_PAGE_ATM_DIAGNOSTIC_CONFIG        (_COMM_PAGE_START_ADDRESS+0x48) /* uint32_t export "atm_diagnostic_config" to userspace */
212 
213 #define _COMM_PAGE_DTRACE_DOF_ENABLED   (_COMM_PAGE_START_ADDRESS+0x04C)        /* uint8_t 0 if userspace DOF disable, 1 if enabled */
214 #define _COMM_PAGE_KERNEL_PAGE_SHIFT    (_COMM_PAGE_START_ADDRESS+0x04D)        /* uint8_t kernel vm page shift. COMM_PAGE_VERSION >= 14 */
215 #define _COMM_PAGE_USER_PAGE_SHIFT_64   (_COMM_PAGE_START_ADDRESS+0x04E)        /* uint8_t user vm page shift. COMM_PAGE_VERSION >= 14 */
216 #define _COMM_PAGE_UNUSED2              (_COMM_PAGE_START_ADDRESS+0x04F)        /* 0x4F unused */
217 
218 #define _COMM_PAGE_TIME_DATA_START      (_COMM_PAGE_START_ADDRESS+0x050)        /* base of offsets below (_NT_SCALE etc) */
219 #define _COMM_PAGE_NT_TSC_BASE          (_COMM_PAGE_START_ADDRESS+0x050)        /* used by nanotime() */
220 #define _COMM_PAGE_NT_SCALE             (_COMM_PAGE_START_ADDRESS+0x058)        /* used by nanotime() */
221 #define _COMM_PAGE_NT_SHIFT             (_COMM_PAGE_START_ADDRESS+0x05c)        /* used by nanotime() */
222 #define _COMM_PAGE_NT_NS_BASE           (_COMM_PAGE_START_ADDRESS+0x060)        /* used by nanotime() */
223 #define _COMM_PAGE_NT_GENERATION        (_COMM_PAGE_START_ADDRESS+0x068)        /* used by nanotime() */
224 #define _COMM_PAGE_GTOD_GENERATION      (_COMM_PAGE_START_ADDRESS+0x06c)        /* used by gettimeofday() */
225 #define _COMM_PAGE_GTOD_NS_BASE         (_COMM_PAGE_START_ADDRESS+0x070)        /* used by gettimeofday() */
226 #define _COMM_PAGE_GTOD_SEC_BASE        (_COMM_PAGE_START_ADDRESS+0x078)        /* used by gettimeofday() */
227 
228 /* NOTE: APPROX_TIME must be aligned to 64-byte cache line size: */
229 #define _COMM_PAGE_APPROX_TIME          (_COMM_PAGE_START_ADDRESS+0x080)        /* used by mach_approximate_time() */
230 #define _COMM_PAGE_APPROX_TIME_SUPPORTED (_COMM_PAGE_START_ADDRESS+0x088)       /* used by mach_approximate_time() */
231 
232 /* Align following entries to next cache line */
233 #define _COMM_PAGE_CONT_TIMEBASE        (_COMM_PAGE_START_ADDRESS+0x0C0)        /* used by mach_continuous_time() */
234 #define _COMM_PAGE_BOOTTIME_USEC        (_COMM_PAGE_START_ADDRESS+0x0C8)        /* uint64_t boottime */
235 #define _COMM_PAGE_NEWTIMEOFDAY_DATA    (_COMM_PAGE_START_ADDRESS+0x0D0)        /* used by gettimeofday(). Currently, sizeof(new_commpage_timeofday_data_t) = 40 */
236 
237 /* Resume packed values to the next cacheline */
238 #define _COMM_PAGE_DYLD_FLAGS           (_COMM_PAGE_START_ADDRESS+0x100)        /* uint64_t export kern.dyld_system_flags to userspace */
239 
240 #define _COMM_PAGE_END                  (_COMM_PAGE_START_ADDRESS+0xfff)        /* end of common page */
241 
242 /* Warning: kernel commpage.h has a matching c typedef for the following.  They must be kept in sync.  */
243 /* These offsets are from _COMM_PAGE_TIME_DATA_START */
244 
245 #define _NT_TSC_BASE                    0
246 #define _NT_SCALE                       8
247 #define _NT_SHIFT                       12
248 #define _NT_NS_BASE                     16
249 #define _NT_GENERATION                  24
250 #define _GTOD_GENERATION                28
251 #define _GTOD_NS_BASE                   32
252 #define _GTOD_SEC_BASE                  40
253 
254 /* jump table (jmp to this address, which may be a branch to the actual code somewhere else) */
255 /* When new jump table entries are added, corresponding symbols should be added below        */
256 /* New slots should be allocated with at least 16-byte alignment. Some like bcopy require    */
257 /* 32-byte alignment, and should be aligned as such in the assembly source before they are relocated */
258 #define _COMM_PAGE_TEXT_START           (_COMM_PAGE_START_ADDRESS+0x1000)
259 #define _COMM_PAGE32_TEXT_START         (_COMM_PAGE32_BASE_ADDRESS+0x1000)    /* start of text section */
260 #define _COMM_PAGE64_TEXT_START         (_COMM_PAGE64_BASE_ADDRESS+0x1000)
261 #define _COMM_PAGE_TEXT_AREA_USED       ( 1 * 4096 )
262 #define _COMM_PAGE_TEXT_AREA_LENGTH     ( 1 * 4096 )
263 #define _PFZ32_SLIDE_RANGE              ( 14 ) /* pages between 0xfffff000 and _COMM_PAGE32_TEXT_START */
264 #define _PFZ64_SLIDE_RANGE              ( 510 ) /* pages between 0x00007ffffffff000 and _COMM_PAGE64_TEXT_START */
265 
266 /* setup start offset in the commpage text region for each jump table entry
267  * the Comm Page Offset is shortened to _COMM_TEXT_[label]_OFFSET
268  */
269 
270 #define _COMM_TEXT_PREEMPT_OFFSET               (0x5a0) /* called from withing pfz */
271 #define _COMM_TEXT_BACKOFF_OFFSET               (0x600) /* called from PFZ */
272 #define _COMM_TEXT_RET_OFFSET                   (0x680) /* called from PFZ */
273 #define _COMM_TEXT_PFZ_START_OFFSET             (0xc00) /* offset for Preemption Free Zone */
274 #define _COMM_TEXT_PFZ_ENQUEUE_OFFSET           (0xc00) /* internal FIFO enqueue */
275 #define _COMM_TEXT_PFZ_DEQUEUE_OFFSET           (0xc80) /* internal FIFO dequeue */
276 #define _COMM_TEXT_UNUSED_OFFSET                (0xd00) /* end of routines in text page */
277 #define _COMM_TEXT_PFZ_END_OFFSET               (0xd00) /* offset for end of PFZ */
278 
279 
280 #define _COMM_PAGE_PREEMPT              (_COMM_PAGE_TEXT_START+_COMM_TEXT_PREEMPT_OFFSET)
281 #define _COMM_PAGE_BACKOFF              (_COMM_PAGE_TEXT_START+_COMM_TEXT_BACKOFF_OFFSET)
282 #define _COMM_PAGE_RET                  (_COMM_PAGE_TEXT_START+_COMM_TEXT_RET_OFFSET)
283 
284 #define _COMM_PAGE_PFZ_START            (_COMM_PAGE_TEXT_START+_COMM_PAGE_PFZ_START_OFFSET)
285 
286 #define _COMM_PAGE_PFZ_ENQUEUE          (_COMM_PAGE_TEXT_START+_COMM_TEXT_PFZ_ENQUEUE_OFFSET)
287 #define _COMM_PAGE_PFZ_DEQUEUE          (_COMM_PAGE_TEXT_START+_COMM_TEXT_PFZ_DEQUEUE_OFFSET)
288 
289 #define _COMM_PAGE_UNUSED6              (_COMM_PAGE_TEXT_START+_COMM_TEXT_UNUSED_OFFSET)
290 #define _COMM_PAGE_PFZ_END              (_COMM_PAGE_TEXT_START+_COMM_TEXT_PFZ_END_OFFSET)
291 #define _COMM_PAGE_TEXT_END             (_COMM_PAGE_TEXT_START+_COMM_TEXT_PFZ_END_OFFSET) /* end of common text page */
292 
293 /* _COMM_PAGE_COMPARE_AND_SWAP{32,64}B are not used on x86 and are
294  * maintained here for source compatability.  These will be removed at
295  * some point, so don't go relying on them. */
296 #define _COMM_PAGE_COMPARE_AND_SWAP32B  (_COMM_PAGE_START_ADDRESS+0xf80)        /* compare-and-swap word w barrier */
297 #define _COMM_PAGE_COMPARE_AND_SWAP64B  (_COMM_PAGE_START_ADDRESS+0xfc0)        /* compare-and-swap doubleword w barrier */
298 
299 /*
300  * _COMM_PAGE_USER_PAGE_SHIFT32 and _COMM_PAGE_USER_PAGE_SHIFT64 are the same on x86.
301  * But both defined to maintain compatability with the arm commpage.
302  */
303 #define _COMM_PAGE_USER_PAGE_SHIFT_32 _COMM_PAGE_USER_PAGE_SHIFT_64
304 
305 #ifdef __ASSEMBLER__
306 #ifdef __COMM_PAGE_SYMBOLS
307 
308 #define CREATE_COMM_PAGE_SYMBOL(symbol_name, symbol_address)            \
309 	                        .org	(symbol_address - (_COMM_PAGE_START_ADDRESS & 0xFFFFE000)) ;\
310 symbol_name: nop
311 
312 .text
313 /* Required to make a well behaved symbol file */
314 
315 CREATE_COMM_PAGE_SYMBOL(___preempt, _COMM_PAGE_PREEMPT)
316 CREATE_COMM_PAGE_SYMBOL(___backoff, _COMM_PAGE_BACKOFF)
317 CREATE_COMM_PAGE_SYMBOL(___pfz_enqueue, _COMM_PAGE_PFZ_ENQUEUE)
318 CREATE_COMM_PAGE_SYMBOL(___pfz_dequeue, _COMM_PAGE_PFZ_DEQUEUE)
319 CREATE_COMM_PAGE_SYMBOL(___end_comm_page, _COMM_PAGE_END)
320 
321 .data                   /* Required to make a well behaved symbol file */
322 .long   0               /* Required to make a well behaved symbol file */
323 
324 #endif /* __COMM_PAGE_SYMBOLS */
325 #endif /* __ASSEMBLER__ */
326 
327 #endif /* defined (__i386__) || defined (__x86_64__) */
328 #endif /* _I386_CPU_CAPABILITIES_H */
329 #endif /* PRIVATE */
330