From: Xiaoyao Li <xiaoyao.li@intel.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
David Hildenbrand <david@redhat.com>,
Igor Mammedov <imammedo@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,
Richard Henderson <richard.henderson@linaro.org>,
Marcelo Tosatti <mtosatti@redhat.com>
Cc: "Markus Armbruster" <armbru@redhat.com>,
"Eric Blake" <eblake@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Peter Xu" <peterx@redhat.com>,
"Chao Peng" <chao.p.peng@linux.intel.com>,
"Michael Roth" <michael.roth@amd.com>,
isaku.yamahata@gmail.com, xiaoyao.li@intel.com,
qemu-devel@nongnu.org, kvm@vger.kernel.org
Subject: [RFC PATCH 07/19] target/i386: Implement mc->kvm_type() to get VM type
Date: Mon, 31 Jul 2023 12:21:49 -0400 [thread overview]
Message-ID: <20230731162201.271114-8-xiaoyao.li@intel.com> (raw)
In-Reply-To: <20230731162201.271114-1-xiaoyao.li@intel.com>
Implement mc->kvm_type() for i386 machines. It provides a way for user
to create SW_PROTECTE_VM.
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
hw/i386/x86.c | 27 +++++++++++++++++++++++++++
include/hw/i386/x86.h | 4 ++++
target/i386/kvm/kvm.c | 38 ++++++++++++++++++++++++++++++++++++++
target/i386/kvm/kvm_i386.h | 1 +
4 files changed, 70 insertions(+)
diff --git a/hw/i386/x86.c b/hw/i386/x86.c
index a88a126123be..3ccd06154249 100644
--- a/hw/i386/x86.c
+++ b/hw/i386/x86.c
@@ -1382,6 +1382,26 @@ static void machine_set_sgx_epc(Object *obj, Visitor *v, const char *name,
qapi_free_SgxEPCList(list);
}
+static int x86_kvm_type(MachineState *ms, const char *vm_type)
+{
+ return kvm_get_vm_type(ms, vm_type);
+}
+
+static char *x86_machine_get_kvm_type(Object *obj, Error **errp)
+{
+ X86MachineState *x86ms = X86_MACHINE(obj);
+
+ return g_strdup(x86ms->kvm_type);
+}
+
+static void x86_machine_set_kvm_type(Object *obj, const char *value, Error **errp)
+{
+ X86MachineState *x86ms = X86_MACHINE(obj);
+
+ g_free(x86ms->kvm_type);
+ x86ms->kvm_type = g_strdup(value);
+}
+
static void x86_machine_initfn(Object *obj)
{
X86MachineState *x86ms = X86_MACHINE(obj);
@@ -1406,6 +1426,7 @@ static void x86_machine_class_init(ObjectClass *oc, void *data)
mc->cpu_index_to_instance_props = x86_cpu_index_to_props;
mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
+ mc->kvm_type = x86_kvm_type;
x86mc->save_tsc_khz = true;
x86mc->fwcfg_dma_enabled = true;
nc->nmi_monitor_handler = x86_nmi;
@@ -1464,6 +1485,12 @@ static void x86_machine_class_init(ObjectClass *oc, void *data)
NULL, NULL);
object_class_property_set_description(oc, "sgx-epc",
"SGX EPC device");
+
+ object_class_property_add_str(oc, X86_MACHINE_KVM_TYPE,
+ x86_machine_get_kvm_type,
+ x86_machine_set_kvm_type);
+ object_class_property_set_description(oc, X86_MACHINE_KVM_TYPE,
+ "KVM guest type (default, sw_protected_vm)");
}
static const TypeInfo x86_machine_info = {
diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h
index da19ae15463a..a3d03f78cefe 100644
--- a/include/hw/i386/x86.h
+++ b/include/hw/i386/x86.h
@@ -42,6 +42,9 @@ struct X86MachineState {
/*< public >*/
+ char *kvm_type;
+ unsigned int vm_type;
+
/* Pointers to devices and objects: */
ISADevice *rtc;
FWCfgState *fw_cfg;
@@ -91,6 +94,7 @@ struct X86MachineState {
#define X86_MACHINE_OEM_ID "x-oem-id"
#define X86_MACHINE_OEM_TABLE_ID "x-oem-table-id"
#define X86_MACHINE_BUS_LOCK_RATELIMIT "bus-lock-ratelimit"
+#define X86_MACHINE_KVM_TYPE "kvm-type"
#define TYPE_X86_MACHINE MACHINE_TYPE_NAME("x86")
OBJECT_DECLARE_TYPE(X86MachineState, X86MachineClass, X86_MACHINE)
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index f8cc8eb1fe70..7971f0fd74b1 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -154,6 +154,44 @@ static KVMMSRHandlers msr_handlers[KVM_MSR_FILTER_MAX_RANGES];
static RateLimit bus_lock_ratelimit_ctrl;
static int kvm_get_one_msr(X86CPU *cpu, int index, uint64_t *value);
+static const char* vm_type_name[] = {
+ [KVM_X86_DEFAULT_VM] = "default",
+ [KVM_X86_SW_PROTECTED_VM] = "sw-protected-vm",
+};
+
+int kvm_get_vm_type(MachineState *ms, const char *vm_type)
+{
+ X86MachineState *x86ms = X86_MACHINE(ms);
+ int kvm_type = KVM_X86_DEFAULT_VM;
+
+ if (vm_type) {
+ if (!g_ascii_strcasecmp(vm_type, "default") || !g_ascii_strcasecmp(vm_type, "")) {
+ kvm_type = KVM_X86_DEFAULT_VM;
+ } else if (!g_ascii_strcasecmp(vm_type, "sw-protected-vm")) {
+ kvm_type = KVM_X86_SW_PROTECTED_VM;
+ } else {
+ error_report("Unknown kvm-type specified '%s'", vm_type);
+ exit(1);
+ }
+ }
+
+ /*
+ * old KVM doesn't support KVM_CAP_VM_TYPES and KVM_X86_DEFAULT_VM
+ * is always supported
+ */
+ if (kvm_type == KVM_X86_DEFAULT_VM) {
+ return kvm_type;
+ }
+
+ if (!(kvm_check_extension(KVM_STATE(ms->accelerator), KVM_CAP_VM_TYPES) & BIT(kvm_type))) {
+ error_report("vm-type %s not supported by KVM", vm_type_name[kvm_type]);
+ exit(1);
+ }
+
+ x86ms->vm_type = kvm_type;
+ return kvm_type;
+}
+
int kvm_has_pit_state2(void)
{
return has_pit_state2;
diff --git a/target/i386/kvm/kvm_i386.h b/target/i386/kvm/kvm_i386.h
index e24753abfe6a..ea3a5b174ac0 100644
--- a/target/i386/kvm/kvm_i386.h
+++ b/target/i386/kvm/kvm_i386.h
@@ -37,6 +37,7 @@ bool kvm_has_adjust_clock(void);
bool kvm_has_adjust_clock_stable(void);
bool kvm_has_exception_payload(void);
void kvm_synchronize_all_tsc(void);
+int kvm_get_vm_type(MachineState *ms, const char *vm_type);
void kvm_arch_reset_vcpu(X86CPU *cs);
void kvm_arch_after_reset_vcpu(X86CPU *cpu);
void kvm_arch_do_init_vcpu(X86CPU *cs);
--
2.34.1
next prev parent reply other threads:[~2023-07-31 16:33 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-31 16:21 [RFC PATCH 00/19] QEMU gmem implemention Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 01/19] trace/kvm: Split address space and slot id in trace_kvm_set_user_memory() Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 02/19] *** HACK *** linux-headers: Update headers to pull in gmem APIs Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 03/19] RAMBlock: Support KVM gmemory Xiaoyao Li
2023-08-01 16:33 ` David Hildenbrand
2023-07-31 16:21 ` [RFC PATCH 04/19] memory: Introduce memory_region_can_be_private() Xiaoyao Li
2023-07-31 21:23 ` Peter Xu
2023-07-31 21:33 ` Michael S. Tsirkin
2023-07-31 21:34 ` Sean Christopherson
2023-07-31 21:36 ` Michael S. Tsirkin
2023-08-01 0:21 ` Peter Xu
2023-08-01 16:23 ` Sean Christopherson
2023-08-01 16:48 ` Claudio Fontana
2023-08-01 16:52 ` Claudio Fontana
2023-08-02 8:05 ` Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 05/19] kvm: Enable KVM_SET_USER_MEMORY_REGION2 for memslot Xiaoyao Li
2023-08-01 17:10 ` Claudio Fontana
2023-08-03 8:43 ` Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 06/19] i386/pc: Drop pc_machine_kvm_type() Xiaoyao Li
2023-08-02 23:00 ` Isaku Yamahata
2023-07-31 16:21 ` Xiaoyao Li [this message]
2023-07-31 16:21 ` [RFC PATCH 08/19] HostMem: Add private property to indicate to use kvm gmem Xiaoyao Li
2023-07-31 17:22 ` Markus Armbruster
2023-08-01 14:54 ` Xiaoyao Li
2023-08-01 14:57 ` Daniel P. Berrangé
2023-08-02 8:04 ` Xiaoyao Li
2023-08-01 17:21 ` David Hildenbrand
2023-08-02 8:03 ` Xiaoyao Li
2023-08-02 14:14 ` David Hildenbrand
2023-08-02 22:53 ` Isaku Yamahata
2023-08-03 13:05 ` David Hildenbrand
2023-07-31 16:21 ` [RFC PATCH 09/19] i386/kvm: Create gmem fd for KVM_X86_SW_PROTECTED_VM Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 10/19] kvm: Introduce support for memory_attributes Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 11/19] kvm/memory: Introduce the infrastructure to set the default shared/private value Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 12/19] i386/kvm: Set memory to default private for KVM_X86_SW_PROTECTED_VM Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 13/19] physmem: replace function name with __func__ in ram_block_discard_range() Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 14/19] physmem: Add ram_block_convert_range Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 15/19] kvm: handle KVM_EXIT_MEMORY_FAULT Xiaoyao Li
2023-08-02 22:25 ` Isaku Yamahata
2023-09-13 6:59 ` Xiaoyao Li
2023-08-09 15:02 ` Xu Yilun
2023-09-13 7:00 ` Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 16/19] trace/kvm: Add trace for page convertion between shared and private Xiaoyao Li
2023-07-31 16:21 ` [RFC PATCH 17/19] pci-host/q35: Move PAM initialization above SMRAM initialization Xiaoyao Li
2023-07-31 16:22 ` [RFC PATCH 18/19] q35: Introduce smm_ranges property for q35-pci-host Xiaoyao Li
2023-07-31 16:22 ` [RFC PATCH 19/19] i386: Disable SMM mode for X86_SW_PROTECTED_VM Xiaoyao Li
2023-08-02 22:27 ` Isaku Yamahata
2023-07-31 16:51 ` [RFC PATCH 00/19] QEMU gmem implemention Daniel P. Berrangé
2023-08-01 1:45 ` Xiaoyao Li
2023-08-10 15:58 ` Michael Roth via
2023-08-14 21:45 ` Isaku Yamahata
2023-07-31 17:10 ` Isaku Yamahata
2023-08-01 1:55 ` Xiaoyao Li
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230731162201.271114-8-xiaoyao.li@intel.com \
--to=xiaoyao.li@intel.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=chao.p.peng@linux.intel.com \
--cc=david@redhat.com \
--cc=eblake@redhat.com \
--cc=imammedo@redhat.com \
--cc=isaku.yamahata@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=marcel.apfelbaum@gmail.com \
--cc=michael.roth@amd.com \
--cc=mst@redhat.com \
--cc=mtosatti@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=seanjc@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).