From patchwork Mon Jul 31 16:21:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 13335006 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A4C4EC001DC for ; Mon, 31 Jul 2023 16:33:36 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQVpb-0002We-VF; Mon, 31 Jul 2023 12:33:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQVpI-00027C-02 for qemu-devel@nongnu.org; Mon, 31 Jul 2023 12:33:01 -0400 Received: from [134.134.136.126] (helo=mgamail.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQVpG-000165-5f for qemu-devel@nongnu.org; Mon, 31 Jul 2023 12:32:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690821174; x=1722357174; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4pSQ7+7eqxjS895xnE1dyhv7L8BA0IkSr37vKkLRLiM=; b=ZFPMHnWZ+n5yzwZn8QCtZk1s18km7Bis9JR5JVacdYdD5zuiJsHR556S 6+Ka9lrCaxd06jMwiiQZ2w2mxQ6HN1gYmqOs1fb7E5OMQ79Ec5NzKKnfw //cTHfLwbMetuEumeaIJMR87kYGmBhX4c0XPWDsVrAVV+wovRUU7CmqKt NDtBkoB1fg/Iw0uutDmeda9F7JaViefBQQ3cWIrtyGUFOqMsXTaTmhj3X wGKwRyUw0JuzSB9b/ltgAMhUp5zw9ddamG76UXfbNKdgmB68cnJQ/BmLg W2PqeP1MFhlq6jpRiw9croN/GeO6s1Bq12fc1Ke8cyEa1JUCwU8KMy4me w==; X-IronPort-AV: E=McAfee;i="6600,9927,10788"; a="353993454" X-IronPort-AV: E=Sophos;i="6.01,244,1684825200"; d="scan'208";a="353993454" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jul 2023 09:25:24 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10788"; a="757984151" X-IronPort-AV: E=Sophos;i="6.01,244,1684825200"; d="scan'208";a="757984151" Received: from lxy-clx-4s.sh.intel.com ([10.239.48.46]) by orsmga008.jf.intel.com with ESMTP; 31 Jul 2023 09:25:20 -0700 From: Xiaoyao Li To: Paolo Bonzini , Sean Christopherson , David Hildenbrand , Igor Mammedov , "Michael S. Tsirkin" , Marcel Apfelbaum , Richard Henderson , Marcelo Tosatti Cc: Markus Armbruster , Eric Blake , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Phil?= =?utf-8?q?ippe_Mathieu-Daud=C3=A9?= , Peter Xu , Chao Peng , Michael Roth , 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 Message-Id: <20230731162201.271114-8-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230731162201.271114-1-xiaoyao.li@intel.com> References: <20230731162201.271114-1-xiaoyao.li@intel.com> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 134.134.136.126 (failed) Received-SPF: pass client-ip=134.134.136.126; envelope-from=xiaoyao.li@intel.com; helo=mgamail.intel.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Implement mc->kvm_type() for i386 machines. It provides a way for user to create SW_PROTECTE_VM. Signed-off-by: Xiaoyao Li --- 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);