xref: /freebsd/sys/crypto/openssl/ossl_x86.c (revision 1603881667360c015f6685131f2f25474fa67a72)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2020 Netflix, Inc
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    without modification.
12  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14  *    redistribution must be conditioned upon including a substantially
15  *    similar Disclaimer requirement for further binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGES.
29  *
30  * $FreeBSD$
31  */
32 
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 
36 #include <machine/cpufunc.h>
37 #include <machine/md_var.h>
38 #include <x86/cputypes.h>
39 #include <x86/specialreg.h>
40 
41 #include <crypto/openssl/ossl.h>
42 
43 /*
44  * See OPENSSL_ia32cap(3).
45  *
46  * [0] = cpu_feature but with a few custom bits
47  * [1] = cpu_feature2 but with AMD XOP in bit 11
48  * [2] = cpu_stdext_feature
49  * [3] = 0
50  */
51 unsigned int OPENSSL_ia32cap_P[4];
52 
53 void
54 ossl_cpuid(void)
55 {
56 	uint64_t xcr0;
57 	u_int regs[4];
58 	u_int max_cores;
59 
60 	/* Derived from OpenSSL_ia32_cpuid. */
61 
62 	OPENSSL_ia32cap_P[0] = cpu_feature & ~(CPUID_B20 | CPUID_IA64);
63 	if (cpu_vendor_id == CPU_VENDOR_INTEL) {
64 		OPENSSL_ia32cap_P[0] |= CPUID_IA64;
65 		if ((cpu_id & 0xf00) != 0xf00)
66 			OPENSSL_ia32cap_P[0] |= CPUID_B20;
67 	}
68 
69 	/* Only leave CPUID_HTT on if HTT is present. */
70 	if (cpu_vendor_id == CPU_VENDOR_AMD && cpu_exthigh >= 0x80000008) {
71 		max_cores = (cpu_procinfo2 & AMDID_CMP_CORES) + 1;
72 		if (cpu_feature & CPUID_HTT) {
73 			if ((cpu_procinfo & CPUID_HTT_CORES) >> 16 <= max_cores)
74 				OPENSSL_ia32cap_P[0] &= ~CPUID_HTT;
75 		}
76 	} else {
77 		if (cpu_high >= 4) {
78 			cpuid_count(4, 0, regs);
79 			max_cores = (regs[0] >> 26) & 0xfff;
80 		} else
81 			max_cores = -1;
82 	}
83 	if (max_cores == 0)
84 		OPENSSL_ia32cap_P[0] &= ~CPUID_HTT;
85 	else if ((cpu_procinfo & CPUID_HTT_CORES) >> 16 == 0)
86 		OPENSSL_ia32cap_P[0] &= ~CPUID_HTT;
87 
88 	OPENSSL_ia32cap_P[1] = cpu_feature2 & ~AMDID2_XOP;
89 	if (cpu_vendor_id == CPU_VENDOR_AMD)
90 		OPENSSL_ia32cap_P[1] |= amd_feature2 & AMDID2_XOP;
91 
92 	OPENSSL_ia32cap_P[2] = cpu_stdext_feature;
93 	if ((OPENSSL_ia32cap_P[1] & CPUID2_XSAVE) == 0)
94 		OPENSSL_ia32cap_P[2] &= ~(CPUID_STDEXT_AVX512F |
95 		    CPUID_STDEXT_AVX512DQ);
96 
97 	/* Disable AVX512F on Skylake-X. */
98 	if ((cpu_id & 0x0fff0ff0) == 0x00050650)
99 		OPENSSL_ia32cap_P[2] &= ~(CPUID_STDEXT_AVX512F);
100 
101 	if (cpu_feature2 & CPUID2_OSXSAVE)
102 		xcr0 = rxcr(0);
103 	else
104 		xcr0 = 0;
105 
106 	if ((xcr0 & (XFEATURE_AVX512 | XFEATURE_AVX)) !=
107 	    (XFEATURE_AVX512 | XFEATURE_AVX))
108 		OPENSSL_ia32cap_P[2] &= ~(CPUID_STDEXT_AVX512VL |
109 		    CPUID_STDEXT_AVX512BW | CPUID_STDEXT_AVX512IFMA |
110 		    CPUID_STDEXT_AVX512F);
111 	if ((xcr0 & XFEATURE_AVX) != XFEATURE_AVX) {
112 		OPENSSL_ia32cap_P[1] &= ~(CPUID2_AVX | AMDID2_XOP | CPUID2_FMA);
113 		OPENSSL_ia32cap_P[2] &= ~CPUID_STDEXT_AVX2;
114 	}
115 }
116