This repository was archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcommpage.c
174 lines (147 loc) · 4.07 KB
/
commpage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Darling Mach Linux Kernel Module
* Copyright (C) 2017 Lubos Dolezel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <duct/compiler/clang/asm-inline.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <linux/cpumask.h>
#include <linux/mm.h>
#include <asm/processor.h>
#include "commpage.h"
// Include commpage definitions
#include "../osfmk/i386/cpu_capabilities.h"
static const char* SIGNATURE32 = "commpage 32-bit";
static const char* SIGNATURE64 = "commpage 64-bit";
static uint64_t get_cpu_caps(void);
#define CGET(p) (commpage + ((p)-_COMM_PAGE_START_ADDRESS))
#define __cpuid(level, eax, ebx, ecx, edx) \
eax = level; \
native_cpuid(&(eax), &(ebx), &(ecx), &(edx))
void* commpage_setup(bool _64bit)
{
uint8_t* commpage;
uint64_t* cpu_caps64;
uint32_t* cpu_caps;
uint16_t* version;
char* signature;
uint64_t my_caps;
uint8_t *ncpus, *nactivecpus;
uint8_t *physcpus, *logcpus;
commpage = vmalloc_user(_64bit ? _COMM_PAGE64_AREA_LENGTH : _COMM_PAGE32_AREA_LENGTH);
if (!commpage)
return NULL;
signature = (char*)CGET(_COMM_PAGE_SIGNATURE);
version = (uint16_t*)CGET(_COMM_PAGE_VERSION);
cpu_caps64 = (uint64_t*)CGET(_COMM_PAGE_CPU_CAPABILITIES64);
cpu_caps = (uint32_t*)CGET(_COMM_PAGE_CPU_CAPABILITIES);
strcpy(signature, _64bit ? SIGNATURE64 : SIGNATURE32);
*version = _COMM_PAGE_THIS_VERSION;
ncpus = (uint8_t*)CGET(_COMM_PAGE_NCPUS);
*ncpus = num_online_cpus();
nactivecpus = (uint8_t*)CGET(_COMM_PAGE_ACTIVE_CPUS);
*nactivecpus = num_active_cpus();
// Better imprecise information than no information
physcpus = (uint8_t*)CGET(_COMM_PAGE_PHYSICAL_CPUS);
logcpus = (uint8_t*)CGET(_COMM_PAGE_LOGICAL_CPUS);
*physcpus = *logcpus = *ncpus;
my_caps = get_cpu_caps();
if (*ncpus == 1)
my_caps |= kUP;
*cpu_caps = (uint32_t) my_caps;
*cpu_caps64 = my_caps;
uint64_t* memsize = (uint64_t*)CGET(_COMM_PAGE_MEMORY_SIZE);
*memsize = get_num_physpages() * PAGE_SIZE;
return commpage;
}
unsigned long commpage_length(bool _64bit)
{
return _64bit ? _COMM_PAGE64_AREA_LENGTH : _COMM_PAGE32_AREA_LENGTH;
}
unsigned long commpage_address(bool _64bit)
{
return _64bit ? _COMM_PAGE64_BASE_ADDRESS : _COMM_PAGE32_BASE_ADDRESS;
}
void commpage_free(void* p)
{
vfree(p);
}
uint64_t get_cpu_caps(void)
{
uint64_t caps = 0;
{
union cpu_flags1 eax;
union cpu_flags2 ecx;
union cpu_flags3 edx;
uint32_t ebx;
eax.reg = ecx.reg = edx.reg = 0;
__cpuid(1, eax.reg, ebx, ecx.reg, edx.reg);
if (ecx.mmx)
caps |= kHasMMX;
if (ecx.sse)
caps |= kHasSSE;
if (ecx.sse2)
caps |= kHasSSE2;
if (edx.sse3)
caps |= kHasSSE3;
if (ecx.ia64)
caps |= k64Bit;
if (edx.ssse3)
caps |= kHasSupplementalSSE3;
if (edx.sse41)
caps |= kHasSSE4_1;
if (edx.sse42)
caps |= kHasSSE4_2;
if (edx.aes)
caps |= kHasAES;
if (edx.avx)
caps |= kHasAVX1_0;
if (edx.rdrnd)
caps |= kHasRDRAND;
if (edx.fma)
caps |= kHasFMA;
if (edx.f16c)
caps |= kHasF16C;
}
{
union cpu_flags4 ebx;
union cpu_flags5 ecx;
uint32_t edx = 0, eax = 7;
__cpuid(7, eax, ebx.reg, ecx.reg, edx);
if (ebx.erms)
caps |= kHasENFSTRG;
if (ebx.avx2)
caps |= kHasAVX2_0;
if (ebx.bmi1)
caps |= kHasBMI1;
if (ebx.bmi2)
caps |= kHasBMI2;
if (ebx.rtm)
caps |= kHasRTM;
if (ebx.hle)
caps |= kHasHLE;
if (ebx.rdseed)
caps |= kHasRDSEED;
if (ebx.adx)
caps |= kHasADX;
if (ebx.mpx)
caps |= kHasMPX;
if (ebx.sgx)
caps |= kHasSGX;
}
return caps;
}