-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpuid.hpp
192 lines (164 loc) · 6.6 KB
/
cpuid.hpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* This project is licensed under the MIT license. See the LICENSE file for more details.
*
* Copyright 2023. Derek (Daax) Rynd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#pragma once
#include <array>
#include <intrin.h>
#include <stdexcept>
#include "bitfield.hpp"
#include "cpuid_schema.hpp"
constexpr int32_t initial_cpuid_function_id = 0x0000;
constexpr int32_t max_cpuid_function_id = 0x0021;
constexpr uint32_t ext_cpuid_function_id = 0x80000000;
constexpr uint32_t ext_cpuid_function_id_max = 0x80000009;
enum e_cpuid_registers
{
eax = 0,
ebx = 1,
ecx = 2,
edx = 3
};
struct cpu_query
{
private:
int32_t _function_id;
int32_t _subfunction_id;
int32_t info[ 4 ]{};
std::array<bitfield<int32_t>, 4> bf;
function_schema schema;
public:
cpu_query( int32_t function_id, int32_t subfunction_id = 0 ) : _function_id( function_id ), _subfunction_id( subfunction_id )
{
__cpuidex( info, function_id, subfunction_id );
for ( int i = 0; i < 4; ++i )
bf[ i ] = bitfield( info[ i ] );
int32_t composite_key = 0xffff0000;
composite_key |= function_id;
if ( subfunction_id > 0 && function_id < 0x40000000 )
{
// Iterate through the map to find the first schema that matches
for ( const auto& [mask, schema_func] : cpuid_schema )
{
if ( ( subfunction_id == ( mask >> 16 ) ) &&
( function_id == ( mask & 0x0000FFFF ) ) )
{
schema = schema_func;
break;
}
schema = cpuid_schema[ composite_key ];
}
}
else
{
schema = cpuid_schema[ function_id ];
}
}
static std::size_t to_index( const std::string& name )
{
if ( name == "eax" ) return eax;
if ( name == "ebx" ) return ebx;
if ( name == "ecx" ) return ecx;
if ( name == "edx" ) return edx;
throw std::out_of_range( "Invalid register name" );
}
static const char* to_string( e_cpuid_registers e )
{
switch ( e )
{
case eax: return "eax";
case ebx: return "ebx";
case ecx: return "ecx";
case edx: return "edx";
default: return "unknown";
}
}
struct field_value
{
std::string field_name;
uint32_t value;
};
[[nodiscard]] int32_t get_by_name( const std::string& reg_name ) const
{
return info[ to_index( reg_name ) ];
}
field_value get_by_name_index( const std::pair<std::string, std::size_t>& p )
{
const register_schema& reg_schema = schema[ p.first ];
const cpuid_field& field = reg_schema.fields[ p.second ];
const int32_t reg_value = info[ to_index( p.first ) ];
if ( field.bit_start == field.bit_end )
{
return { field.name, uint32_t( reg_value >> field.bit_start ) & 1 };
}
uint32_t mask = ( ( 1U << ( field.bit_end - field.bit_start + 1 ) ) - 1 ) << field.bit_start;
return { field.name, ( reg_value & mask ) >> field.bit_start };
}
auto& get_bitfield( const std::string& reg_name ) { return bf[ to_index( reg_name ) ]; }
auto& operator[]( const std::string& reg_name ) { return bf[ to_index( reg_name ) ]; }
uint32_t get_bit( const std::string& reg_name, std::size_t bit_position ) { return bf[ to_index( reg_name ) ][ bit_position ]; }
uint32_t get_bit_range( const std::string& reg_name, std::size_t bit_start, std::size_t bit_end )
{
uint32_t result = 0;
for ( std::size_t i = bit_start; i <= bit_end; ++i )
{
result |= ( get_bit( reg_name, i ) << ( i - bit_start ) );
}
return result;
}
auto find_field_iter( const register_schema& reg_schema, auto predicate )
{
const auto field_iter = std::find_if( reg_schema.fields.begin(), reg_schema.fields.end(), predicate );
if ( field_iter == reg_schema.fields.end() )
throw std::out_of_range( "Invalid field or bit range" );
return field_iter;
}
bitfield<int32_t>::proxy_range get_field( const std::string& reg_name, const char* field_name )
{
const register_schema& reg_schema = schema[ reg_name ];
const auto field_iter = find_field_iter( reg_schema, [ &field_name ] ( const cpuid_field& field )
{
return field.name == field_name;
} );
return bf[ to_index( reg_name ) ].get_range( field_iter->bit_start, field_iter->bit_end );
}
field_value get_field( const std::string& reg_name, std::pair<std::size_t, std::size_t> bit_range )
{
const register_schema& reg_schema = schema[ reg_name ];
const auto field_iter = find_field_iter( reg_schema, [ &bit_range ] ( const cpuid_field& field )
{
return field.bit_start == bit_range.first && field.bit_end == bit_range.second;
} );
const uint32_t value = get_bit_range( reg_name, bit_range.first, bit_range.second );
return { field_iter->name, value };
}
field_value get_field( const std::string& reg_name, std::size_t bit_pos )
{
const register_schema& reg_schema = schema[ reg_name ];
const auto field_iter = find_field_iter( reg_schema, [ &bit_pos ] ( const cpuid_field& field )
{
return field.bit_start == bit_pos && field.bit_end == bit_pos;
} );
const uint32_t value = get_bit( reg_name, bit_pos );
return { field_iter->name, value };
}
[[nodiscard]] function_schema get_schema() const { return schema; }
};