Skip to content

Commit fac4124

Browse files
committed
cpuid: Add kvm features
1 parent 04ca28a commit fac4124

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

api/kernel/cpuid.hpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
88
// You may obtain a copy of the License at
9-
//
9+
//
1010
// http://www.apache.org/licenses/LICENSE-2.0
11-
//
11+
//
1212
// Unless required by applicable law or agreed to in writing, software
1313
// distributed under the License is distributed on an "AS IS" BASIS,
1414
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -96,9 +96,22 @@ namespace CPUID
9696
LM, // Long mode (64-bit Architecture)
9797
};
9898

99-
bool isAmdCpu();
100-
bool isIntelCpu();
101-
bool hasFeature(Feature f);
99+
bool is_amd_cpu();
100+
bool is_intel_cpu();
101+
bool has_feature(Feature f);
102+
103+
unsigned kvm_function();
104+
bool kvm_feature(unsigned id);
102105
} //< CPUID
103106

107+
#define KVM_FEATURE_CLOCKSOURCE 0
108+
#define KVM_FEATURE_NOP_IO_DELAY 1
109+
#define KVM_FEATURE_MMU_OP 2 /* deprecated */
110+
#define KVM_FEATURE_CLOCKSOURCE2 3
111+
#define KVM_FEATURE_ASYNC_PF 4
112+
#define KVM_FEATURE_STEAL_TIME 5
113+
#define KVM_FEATURE_PV_EOI 6
114+
#define KVM_FEATURE_PV_UNHALT 7
115+
#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24
116+
104117
#endif //< KERNEL_CPUID_HPP

src/kernel/cpuid.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
88
// You may obtain a copy of the License at
9-
//
9+
//
1010
// http://www.apache.org/licenses/LICENSE-2.0
11-
//
11+
//
1212
// Unless required by applicable law or agreed to in writing, software
1313
// distributed under the License is distributed on an "AS IS" BASIS,
1414
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -184,7 +184,7 @@ namespace
184184

185185
} //< namespace
186186

187-
bool CPUID::isAmdCpu()
187+
bool CPUID::is_amd_cpu()
188188
{
189189
auto result = cpuid(0, 0);
190190
return
@@ -193,7 +193,7 @@ bool CPUID::isAmdCpu()
193193
&& memcmp(reinterpret_cast<char*>(&result.ECX), "DMAc", 4) == 0;
194194
}
195195

196-
bool CPUID::isIntelCpu()
196+
bool CPUID::is_intel_cpu()
197197
{
198198
auto result = cpuid(0, 0);
199199
return
@@ -202,7 +202,7 @@ bool CPUID::isIntelCpu()
202202
&& memcmp(reinterpret_cast<char*>(&result.ECX), "ntel", 4) == 0;
203203
}
204204

205-
bool CPUID::hasFeature(Feature f)
205+
bool CPUID::has_feature(Feature f)
206206
{
207207
const auto feature_info = get_feature_info(f);
208208
const auto cpuid_result = cpuid(feature_info.func, feature_info.subfunc);
@@ -215,3 +215,21 @@ bool CPUID::hasFeature(Feature f)
215215
case Register::EDX: return (cpuid_result.EDX & feature_info.bitmask) != 0;
216216
}
217217
}
218+
219+
#define KVM_CPUID_SIGNATURE 0x40000000
220+
221+
unsigned CPUID::kvm_function()
222+
{
223+
auto res = cpuid(KVM_CPUID_SIGNATURE, 0);
224+
/// "KVMKVMKVM"
225+
if (res.EBX == 0x4b4d564b && res.ECX == 0x564b4d56 && res.EDX == 0x4d)
226+
return res.EAX;
227+
return 0;
228+
}
229+
bool CPUID::kvm_feature(unsigned id)
230+
{
231+
unsigned func = kvm_function();
232+
if (func == 0) return false;
233+
auto res = cpuid(func, 0);
234+
return (res.EAX & (1 << id)) != 0;
235+
}

0 commit comments

Comments
 (0)