summary refs log tree commit diff
path: root/plugin_proto/src
diff options
context:
space:
mode:
authorDmitry Torokhov <dtor@chromium.org>2018-02-27 15:53:12 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-02-27 22:26:14 -0800
commitcd6a187de66ff56948312ae983ca12a12782e774 (patch)
treece1fd4992ca2e3a1a359350b0d8d4b5ba765e98d /plugin_proto/src
parent59cdd83fda0245e310cae018b71a4a8ba4c3b280 (diff)
downloadcrosvm-cd6a187de66ff56948312ae983ca12a12782e774.tar
crosvm-cd6a187de66ff56948312ae983ca12a12782e774.tar.gz
crosvm-cd6a187de66ff56948312ae983ca12a12782e774.tar.bz2
crosvm-cd6a187de66ff56948312ae983ca12a12782e774.tar.lz
crosvm-cd6a187de66ff56948312ae983ca12a12782e774.tar.xz
crosvm-cd6a187de66ff56948312ae983ca12a12782e774.tar.zst
crosvm-cd6a187de66ff56948312ae983ca12a12782e774.zip
plugin_proto: add helpers to convert CPUID data between KVM and protobuf
We need to convert between protobuf and KVM format of cpuid data in
several places, so let's add helpers to plugin_proto crate.

TEST=cargo test --features plugin; cargo test -p kvm
BUG=chromium:800626

Change-Id: Ida7b59825d2146b0d02711e441f477d90dd4263a
Signed-off-by: Dmitry Torokhov <dtor@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/939660
Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'plugin_proto/src')
-rw-r--r--plugin_proto/src/lib.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/plugin_proto/src/lib.rs b/plugin_proto/src/lib.rs
index 7f0d66c..c28635a 100644
--- a/plugin_proto/src/lib.rs
+++ b/plugin_proto/src/lib.rs
@@ -1,2 +1,34 @@
 extern crate protobuf;
+extern crate kvm_sys;
 include!(concat!(env!("OUT_DIR"), "/proto_include.rs"));
+
+/// Converts protobuf representation of CpuId data into KVM format.
+pub fn cpuid_proto_to_kvm(entry: &CpuidEntry) -> kvm_sys::kvm_cpuid_entry2 {
+    // Safe: C structures are expected to be zero-initialized.
+    let mut e: kvm_sys::kvm_cpuid_entry2 = unsafe { std::mem::zeroed() };
+    e.function = entry.function;
+    if entry.has_index {
+        e.flags = kvm_sys::KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+    }
+    e.index = entry.index;
+    e.eax = entry.eax;
+    e.ebx = entry.ebx;
+    e.ecx = entry.ecx;
+    e.edx = entry.edx;
+
+    e
+}
+
+/// Converts KVM representation of CpuId data into protobuf format.
+pub fn cpuid_kvm_to_proto(entry: &kvm_sys::kvm_cpuid_entry2) -> CpuidEntry {
+    let mut e = CpuidEntry::new();
+    e.function = entry.function;
+    e.has_index = entry.flags & kvm_sys::KVM_CPUID_FLAG_SIGNIFCANT_INDEX != 0;
+    e.index = entry.index;
+    e.eax = entry.eax;
+    e.ebx = entry.ebx;
+    e.ecx = entry.ecx;
+    e.edx = entry.edx;
+
+    e
+}