gem5 v24.0.0.0
Loading...
Searching...
No Matches
cpuid.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "arch/x86/cpuid.hh"
30
31#include "arch/x86/isa.hh"
32#include "base/bitfield.hh"
33#include "cpu/thread_context.hh"
34#include "debug/X86.hh"
35
36namespace gem5
37{
38
39namespace X86ISA
40{
41
42X86CPUID::X86CPUID(const std::string& vendor, const std::string& name)
43 : vendorString(vendor), nameString(name)
44{
45 fatal_if(vendorString.size() != 12,
46 "CPUID vendor string must be 12 characters\n");
47}
48
49void
51{
52 capabilities[func] = values;
53}
54
55void
57{
58 // Extended functions begin with 8000_0000h, but the enum is based from
59 // zero, so we need to add that to the function value.
60 capabilities[func | 0x80000000] = values;
61}
62
63bool
64X86CPUID::doCpuid(ThreadContext * tc, uint32_t function, uint32_t index,
65 CpuidResult &result)
66{
67 constexpr uint32_t ext = 0x80000000;
68
69 DPRINTF(X86, "Calling CPUID function %x with index %d\n", function, index);
70
71 // Handle the string-related CPUID functions specially
72 if (function == VendorAndLargestStdFunc) {
75 stringToRegister(vendorString.c_str() + 4),
76 stringToRegister(vendorString.c_str() + 8));
77
78 return true;
79 } else if (function == (ext | VendorAndLargestExtFunc)) {
80 result = CpuidResult(0x80000000 + NumExtendedCpuidFuncs - 1,
82 stringToRegister(vendorString.c_str() + 4),
83 stringToRegister(vendorString.c_str() + 8));
84
85 return true;
86 } else if ((function == (ext | NameString1)) ||
87 (function == (ext | NameString2)) ||
88 (function == (ext | NameString3))) {
89 // Zero fill anything beyond the end of the string. This
90 // should go away once the string is a vetted parameter.
91 char cleanName[nameStringSize];
92 memset(cleanName, '\0', nameStringSize);
93 strncpy(cleanName, nameString.c_str(), nameStringSize-1);
94
95 int funcNum = bits(function, 15, 0);
96 int offset = (funcNum - NameString1) * 16;
97 assert(nameStringSize >= offset + 16);
98 result = CpuidResult(
99 stringToRegister(cleanName + offset + 0),
100 stringToRegister(cleanName + offset + 4),
101 stringToRegister(cleanName + offset + 12),
102 stringToRegister(cleanName + offset + 8));
103
104 return true;
105 }
106
107 // Ignore anything not in the map of supported CPUID functions.
108 // This is checked after the string-related functions as those are not
109 // in the capabilities map.
110 if (!capabilities.count(function)) {
111 return false;
112 }
113
114 int cap_offset = 0;
115
116 // Ignore index values for functions that do not take index values.
117 if (hasSignificantIndex(function)) {
118 cap_offset = index * 4;
119 }
120
121 // Ensure we have the offset and 4 dwords after it.
122 assert(capabilities[function].size() >= (cap_offset + 4));
123
124 auto &cap_vec = capabilities[function];
125 result = CpuidResult(cap_vec[cap_offset + 0], cap_vec[cap_offset + 1],
126 cap_vec[cap_offset + 2], cap_vec[cap_offset + 3]);
127 DPRINTF(X86, "CPUID function %x returning (%x, %x, %x, %x)\n",
128 function, result.rax, result.rbx, result.rdx, result.rcx);
129
130 return true;
131}
132
133uint64_t
135{
136 uint64_t reg = 0;
137 for (int pos = 3; pos >=0; pos--) {
138 reg <<= 8;
139 reg |= str[pos];
140 }
141 return reg;
142}
143
144// Return true if the CPUID function takes ECX index as an input AND
145// those multiple index values are supported in gem5.
146bool
148{
149 uint16_t family = bits(function, 31, 16);
150 uint16_t funcNum = bits(function, 15, 0);
151
152 if (family == 0x0000) {
153 switch (funcNum) {
154 case ExtendedState:
155 return true;
156 default:
157 return false;
158 }
159 }
160
161 return false;
162}
163
164} // namespace X86ISA
165} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
ThreadContext is the external interface to all thread state for anything outside of the CPU.
bool hasSignificantIndex(uint32_t function)
Definition cpuid.cc:147
std::unordered_map< uint32_t, std::vector< uint32_t > > capabilities
Definition cpuid.hh:108
void addStandardFunc(uint32_t func, std::vector< uint32_t > values)
Definition cpuid.cc:50
void addExtendedFunc(uint32_t func, std::vector< uint32_t > values)
Definition cpuid.cc:56
const std::string nameString
Definition cpuid.hh:107
bool doCpuid(ThreadContext *tc, uint32_t function, uint32_t index, CpuidResult &result)
Definition cpuid.cc:64
const std::string vendorString
Definition cpuid.hh:106
uint64_t stringToRegister(const char *str)
Definition cpuid.cc:134
X86CPUID(const std::string &vendor, const std::string &name)
Definition cpuid.cc:42
STL vector class.
Definition stl.hh:37
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition bitfield.hh:79
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
Bitfield< 12 > ext
Bitfield< 5, 3 > reg
Definition types.hh:92
@ NameString3
Definition cpuid.hh:65
@ VendorAndLargestExtFunc
Definition cpuid.hh:61
@ NameString1
Definition cpuid.hh:63
@ NumExtendedCpuidFuncs
Definition cpuid.hh:70
@ NameString2
Definition cpuid.hh:64
@ VendorAndLargestStdFunc
Definition cpuid.hh:47
@ ExtendedState
Definition cpuid.hh:55
@ NumStandardCpuidFuncs
Definition cpuid.hh:56
constexpr int nameStringSize
Definition cpuid.hh:73
Bitfield< 5, 3 > index
Definition types.hh:98
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0