xref: /PHP-7.3/Zend/zend_cpuinfo.c (revision 5a361c3a)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2018-2018 Zend Technologies Ltd. (http://www.zend.com) |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Xinchen Hui <xinchen.h@zend.com>                            |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "zend_cpuinfo.h"
20 
21 typedef struct _zend_cpu_info {
22 	uint32_t eax;
23 	uint32_t ebx;
24 	uint32_t ecx;
25 	uint32_t edx;
26 	uint32_t initialized;
27 } zend_cpu_info;
28 
29 static zend_cpu_info cpuinfo = {0};
30 
31 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
32 # if defined(HAVE_CPUID_H) && defined(HAVE_CPUID_COUNT)
33 # include <cpuid.h>
__zend_cpuid(uint32_t func,uint32_t subfunc,zend_cpu_info * cpuinfo)34 static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
35 	__cpuid_count(func, subfunc, cpuinfo->eax, cpuinfo->ebx, cpuinfo->ecx, cpuinfo->edx);
36 }
37 # else
__zend_cpuid(uint32_t func,uint32_t subfunc,zend_cpu_info * cpuinfo)38 static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
39 #if defined(__i386__) && (defined(__pic__) || defined(__PIC__))
40 	/* PIC on i386 uses %ebx, so preserve it. */
41 	__asm__ __volatile__ (
42 		"pushl  %%ebx\n"
43 		"cpuid\n"
44 		"mov    %%ebx,%1\n"
45 		"popl   %%ebx"
46 		: "=a"(cpuinfo->eax), "=r"(cpuinfo->ebx), "=c"(cpuinfo->ecx), "=d"(cpuinfo->edx)
47 		: "a"(func), "c"(subfunc)
48 	);
49 #else
50 	__asm__ __volatile__ (
51 		"cpuid"
52 		: "=a"(cpuinfo->eax), "=b"(cpuinfo->ebx), "=c"(cpuinfo->ecx), "=d"(cpuinfo->edx)
53 		: "a"(func), "c"(subfunc)
54 	);
55 #endif
56 }
57 # endif
58 #elif defined(ZEND_WIN32) && !defined(__clang__)
59 # include <intrin.h>
__zend_cpuid(uint32_t func,uint32_t subfunc,zend_cpu_info * cpuinfo)60 static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
61 	int regs[4];
62 
63 	__cpuidex(regs, func, subfunc);
64 
65 	cpuinfo->eax = regs[0];
66 	cpuinfo->ebx = regs[1];
67 	cpuinfo->ecx = regs[2];
68 	cpuinfo->edx = regs[3];
69 }
70 #else
__zend_cpuid(uint32_t func,uint32_t subfunc,zend_cpu_info * cpuinfo)71 static void __zend_cpuid(uint32_t func, uint32_t subfunc, zend_cpu_info *cpuinfo) {
72 	cpuinfo->eax = 0;
73 }
74 #endif
75 
zend_cpu_startup(void)76 void zend_cpu_startup(void)
77 {
78 	if (!cpuinfo.initialized) {
79 		zend_cpu_info ebx;
80 		int max_feature;
81 
82 		cpuinfo.initialized = 1;
83 		__zend_cpuid(0, 0, &cpuinfo);
84 		max_feature = cpuinfo.eax;
85 		if (max_feature == 0) {
86 			return;
87 		}
88 
89 		__zend_cpuid(1, 0, &cpuinfo);
90 
91 		/* for avx2 */
92 		if (max_feature >= 7) {
93 			__zend_cpuid(7, 0, &ebx);
94 			cpuinfo.ebx = ebx.ebx;
95 		} else {
96 			cpuinfo.ebx = 0;
97 		}
98 	}
99 }
100 
zend_cpu_supports(zend_cpu_feature feature)101 ZEND_API int zend_cpu_supports(zend_cpu_feature feature) {
102 	if (feature & ZEND_CPU_EDX_MASK) {
103 		return (cpuinfo.edx & (feature & ~ZEND_CPU_EDX_MASK));
104 	} else if (feature & ZEND_CPU_EBX_MASK) {
105 		return (cpuinfo.ebx & (feature & ~ZEND_CPU_EBX_MASK));
106 	} else {
107 		return (cpuinfo.ecx & feature);
108 	}
109 }
110 
111 /*
112  * Local variables:
113  * tab-width: 4
114  * c-basic-offset: 4
115  * indent-tabs-mode: t
116  * End:
117  */
118