Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp783511pxk; Thu, 3 Sep 2020 12:28:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYEuTxCi2FwJUsZbDdMczLLK5JO1mCxv+T5WJ9ioVjcDkPRHUhPCP9pGySy/QTYQK6EghH X-Received: by 2002:a17:906:82d1:: with SMTP id a17mr3693426ejy.385.1599161315612; Thu, 03 Sep 2020 12:28:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599161315; cv=none; d=google.com; s=arc-20160816; b=SydcwzuCkNPfr3Qe0bYUWufIUH+fCNpMR1onqbWWzD2REttEYv1k7TouWGlu5vPsEU 3D6284wLuSFc8SB9yroQBViDyPg1utFZ7SzrAT2VaiHLCiag3atwZ+L1l4UyB3YhAyXV JCrF5kfjES5C0z076vCcucgKdrSvzxSFgIDvI/+RoJr0ho/KFRpKSXlr6FmIBwm4aJ90 wXUD9DJIBtZg4Vbc/H7nx3De9ddkxAqhedCxslOcvpnzvgvXarXjO7FatpXZpimuuABk ETX123PZegWgMU9+HIq4eR2cUuR3jpcAMp2O1jnv6TMjUR6ETy14Ce3/q5Flb3ufwj// ZjUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=NtWumpD+gVz83viVaftbzeK1fRKTl6N3ZA9uIi3xpRI=; b=EtbKKAFQm/FVkgrHlcYmv+a90O3GmX1HMXue+t8lOTkgFaBtxmYHhdgbu1SA1P+s39 M/UnL/3C+9E4LIQwezMeEISRDTbma98B0lIjYB3S44sbPWuAMOXj4IbrjVV8pG7O2PQZ Atv91r9AjM4NdOXz6mCIOaARhq0SU880OHYNaqENDCnyGDfo4ohHM3P4RzJFyVZTGLXY CEfxDB1P522F9n6s1lSbWLIEeZvfWUPGQXPRNtMCtiTK0gKqL5gYQpSGTRj7Vt6/L8g7 1GIx7TxRczc9d+TsomW7dFYeS8g3QL85FASJDLGO7uWb0l2tkUT4yFPA8rllcvowGjXE 6UHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qeWO7m0t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id si25si2643025ejb.431.2020.09.03.12.28.12; Thu, 03 Sep 2020 12:28:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qeWO7m0t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729020AbgICT1a (ORCPT + 99 others); Thu, 3 Sep 2020 15:27:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728304AbgICT11 (ORCPT ); Thu, 3 Sep 2020 15:27:27 -0400 Received: from mail-ej1-x643.google.com (mail-ej1-x643.google.com [IPv6:2a00:1450:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE959C061244 for ; Thu, 3 Sep 2020 12:27:26 -0700 (PDT) Received: by mail-ej1-x643.google.com with SMTP id i22so5428558eja.5 for ; Thu, 03 Sep 2020 12:27:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=NtWumpD+gVz83viVaftbzeK1fRKTl6N3ZA9uIi3xpRI=; b=qeWO7m0tg65gHfjksmor08Atfeytv7cfsX8NXjQ2kjWr4o/Jrl8qCPCTjRxlozDT4v ZrxI6hhbwJvFjhvdqXkT9JUSpkS4BptfWt2jSR3DghbSt2fIEAOr7f43h0szfgRBUepc j4H6o1fwekk0X1DIfEXuA4hhL/9x4Yfjbmj73PK0UTRCnVuZnTUIZMISgN3Utt4zhGkx S/kDrowwwNWXTizwm+7TxQ9oT7OJ/r4tXyl2Y6/t8YEnaHn9Kql74p+X7t8B05BLZZqb S48pazJ95IzTwveRNMxz6UJdCDT2PtMo8uC1JKDVd666IEeWzYgSpF4g9fsfPgsLFoFn +3LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=NtWumpD+gVz83viVaftbzeK1fRKTl6N3ZA9uIi3xpRI=; b=ZdKjRH+SNX64cEQQHORzlJ9HT3JbMh7pOXkOGW7Lh9VRCyZnWgVrbo2f7reTCgospu S+w//5Wl5/wBnxwyCSD+s9TGT0QSB6QbaAepISJD31Dqz6Lmgp1GbGFmr7ZvJLsW2Zyi k0XINEv5KRDIzsUvCmbv0JwuwrG7mpgjW1e5Ny+5SG765TgXHfnUN93sh9LavJLuYlBY gIc/uRL12aUDN6gA/n1PhdnKFFk8LXKoDT56q0I4bbwacwfkGbyeHU+8JYhqKUl+T6KW 8md6G88Bu0jE9S4ULjyFFLGjk3peN9lc5Z4AJ/nOYE8V25PsGGKVG/e49ARZZmyf5Snb Qtzg== X-Gm-Message-State: AOAM530r7k5ZcHMRIsRrGW9AntKEOYrNVK0rH8ZHdpu5lNM0M9TCIj8W jXP1BiUBwYm/g3xFguyEMyq+gkUxs0tpUaqWs52kdw== X-Received: by 2002:a17:906:5856:: with SMTP id h22mr3705386ejs.480.1599161245100; Thu, 03 Sep 2020 12:27:25 -0700 (PDT) MIME-Version: 1.0 References: <20200902125935.20646-1-graf@amazon.com> <20200902125935.20646-2-graf@amazon.com> In-Reply-To: <20200902125935.20646-2-graf@amazon.com> From: Aaron Lewis Date: Thu, 3 Sep 2020 12:27:13 -0700 Message-ID: Subject: Re: [PATCH v6 1/7] KVM: x86: Deflect unknown MSR accesses to user space To: Alexander Graf Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , KarimAllah Raslan , Dan Carpenter , kvm list , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > +:: > + > + /* KVM_EXIT_X86_RDMSR / KVM_EXIT_X86_WRMSR */ > + struct { > + __u8 error; /* user -> kernel */ > + __u8 pad[3]; > + __u32 reason; /* kernel -> user */ > + __u32 index; /* kernel -> user */ > + __u64 data; /* kernel <-> user */ > + } msr; > + > +Used on x86 systems. When the VM capability KVM_CAP_X86_USER_SPACE_MSR i= s > +enabled, MSR accesses to registers that would invoke a #GP by KVM kernel= code > +will instead trigger a KVM_EXIT_X86_RDMSR exit for reads and KVM_EXIT_X8= 6_WRMSR > +exit for writes. > + > +The "reason" field specifies why the MSR trap occurred. User space will = only > +receive MSR exit traps when a particular reason was requested during thr= ough > +ENABLE_CAP. Currently valid exit reasons are: > + > + KVM_MSR_EXIT_REASON_INVAL - access to invalid MSRs or reserved bi= ts Can we also have ENOENT? KVM_MSR_EXIT_REASON_ENOENT - Unknown MSR > > + > +For KVM_EXIT_X86_RDMSR, the "index" field tells user space which MSR the= guest > +wants to read. To respond to this request with a successful read, user s= pace > +writes the respective data into the "data" field and must continue guest > +execution to ensure the read data is transferred into guest register sta= te. > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 88c593f83b28..4d285bf054fb 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -1549,12 +1549,88 @@ int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index,= u64 data) > } > EXPORT_SYMBOL_GPL(kvm_set_msr); > > +static int complete_emulated_msr(struct kvm_vcpu *vcpu, bool is_read) > +{ > + if (vcpu->run->msr.error) { > + kvm_inject_gp(vcpu, 0); Add return 1. The RIP doesn=E2=80=99t advance when the instruction raises a= fault. > > + } else if (is_read) { > + kvm_rax_write(vcpu, (u32)vcpu->run->msr.data); > + kvm_rdx_write(vcpu, vcpu->run->msr.data >> 32); > + } > + > + return kvm_skip_emulated_instruction(vcpu); > +} > + > +static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu) > +{ > + return complete_emulated_msr(vcpu, true); > +} > + > > /* For KVM_EXIT_INTERNAL_ERROR */ > /* Emulate instruction failed. */ > @@ -412,6 +414,15 @@ struct kvm_run { > __u64 esr_iss; > __u64 fault_ipa; > } arm_nisv; > + /* KVM_EXIT_X86_RDMSR / KVM_EXIT_X86_WRMSR */ > + struct { > + __u8 error; /* user -> kernel */ > + __u8 pad[3]; __u8 pad[7] to maintain 8 byte alignment? unless we can get away with fewer bits for 'reason' and get them from 'pad'. > +#define KVM_MSR_EXIT_REASON_INVAL (1 << 0) > + __u32 reason; /* kernel -> user */ > + __u32 index; /* kernel -> user */ > + __u64 data; /* kernel <-> user */ > + } msr; > /* Fix the size of the union. */ > char padding[256]; > }; > @@ -1031,6 +1042,7 @@ struct kvm_ppc_resize_hpt { > #define KVM_CAP_PPC_SECURE_GUEST 181 > #define KVM_CAP_HALT_POLL 182 > #define KVM_CAP_ASYNC_PF_INT 183 > +#define KVM_CAP_X86_USER_SPACE_MSR 184 > > #ifdef KVM_CAP_IRQ_ROUTING > > -- > 2.17.1 > > > > > Amazon Development Center Germany GmbH > Krausenstr. 38 > 10117 Berlin > Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss > Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B > Sitz: Berlin > Ust-ID: DE 289 237 879 > > >