Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2346515pxa; Fri, 7 Aug 2020 09:00:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywGq21t9p2fXTUthh0eKQ9bcJTwa7PMy39I61iKlwB60N6qt1SCRKC7jy5WAfJvYtyoDKt X-Received: by 2002:a17:906:7855:: with SMTP id p21mr9928338ejm.492.1596816004212; Fri, 07 Aug 2020 09:00:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596816004; cv=none; d=google.com; s=arc-20160816; b=bAYqiIQFd08Zs4i4eT5K0Vqu/Cvvz82NRhdXltUkPogXMtB7CigqNTcei7kLZ7V/0S g8Te6fNZ5Vl0s2RpkCR3antAZnzB06aFGy3gtZK79TBXHLcdATAg4kacC/cE05ck0KB9 pzyhgU5B7CdkXkBOPRzjHhc4wsMLCLs+0HIc3RgbqfGfLWyo7vfV8l0KLBW2vG2Xj99g aItYOrJ5TUzuDoEzIANNpzjYx0NBBSZvTrn9WJ9e88eVRnjWjORB7marF2VNUebvTJXf 2IP276b3sUeyRsz8ZasD/pVZRuEQaSbttkbHETyaUjNfYeVgTwROiMeMitxqfJiNyqjh y5tw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from:ironport-sdr:dkim-signature; bh=aRi9Uy05R53LS40Cl2TUQpYE2SoFuLjkjihkP3mfagk=; b=0IfMlhceK6CGi6feglXf/6cauBzuDtbDQN8zL0xF2le0U+ONh2FltcV6ZrZiO9Svuv JwE8XAPvkPd03BXy3nKiTPD7qA2OeoeZsNzlXrMQk+xim8XxQaq8xemkBWKrvq27RMee oPFDCYE6pFBWXdg+akWVSPpIttv9DSbs7r13i7VDxOy3va+H8HDUQWHF8P1O2BwSh3/g 1kN3bV3tctD9JrOUAHV7P/woktEZ6N1w5CfH0iU/WsJ3DOudNWWa7Hl7FSs+SveztdC5 2VZ/LCZqtbWSyjV1uDnX8xOfunnsBbS/zgnTCdUD+cnslGxzBpGfm6dYw/7r6rOr774T 3vwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=r9DSZhRn; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id du22si6499091ejc.24.2020.08.07.08.59.41; Fri, 07 Aug 2020 09:00:04 -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=@amazon.com header.s=amazon201209 header.b=r9DSZhRn; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726726AbgHGP5G (ORCPT + 99 others); Fri, 7 Aug 2020 11:57:06 -0400 Received: from smtp-fw-4101.amazon.com ([72.21.198.25]:65242 "EHLO smtp-fw-4101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725993AbgHGP5F (ORCPT ); Fri, 7 Aug 2020 11:57:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1596815825; x=1628351825; h=from:to:cc:subject:date:message-id:mime-version; bh=aRi9Uy05R53LS40Cl2TUQpYE2SoFuLjkjihkP3mfagk=; b=r9DSZhRnLAY3Lh6WxwtvX3DDXDaKH9jxGXflXD9pZy/Sz4SZcuitD84+ H2uvc+IM/6DDhlbPRTgmSN5ZYWafYjJcpgKUBYFR3UK82ujDWfJU7J9S9 tjn39cgpvI3go0c5c53dBMEDiuvCdz/wtZBKu0AhMrUvPc63PXyumoFty w=; IronPort-SDR: y4XqMm+0kzfvW4yIpAFwF1Tu+qCceBTtGffx1tonMt08r4cNIV5o41CNIGp5ooiDazwJVjVxQj gmheqMJRHdNg== X-IronPort-AV: E=Sophos;i="5.75,446,1589241600"; d="scan'208";a="46730315" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1a-e34f1ddc.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 07 Aug 2020 15:57:03 +0000 Received: from EX13MTAUWC001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1a-e34f1ddc.us-east-1.amazon.com (Postfix) with ESMTPS id 9A85FA211B; Fri, 7 Aug 2020 15:56:59 +0000 (UTC) Received: from EX13D20UWC001.ant.amazon.com (10.43.162.244) by EX13MTAUWC001.ant.amazon.com (10.43.162.135) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 7 Aug 2020 15:56:58 +0000 Received: from u79c5a0a55de558.ant.amazon.com (10.43.162.140) by EX13D20UWC001.ant.amazon.com (10.43.162.244) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 7 Aug 2020 15:56:55 +0000 From: Alexander Graf To: Paolo Bonzini CC: Jonathan Corbet , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , "Joerg Roedel" , KarimAllah Raslan , Aaron Lewis , , , Subject: [PATCH v5 0/3] Allow user space to restrict and augment MSR emulation Date: Fri, 7 Aug 2020 17:56:45 +0200 Message-ID: <20200807155648.8602-1-graf@amazon.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.43.162.140] X-ClientProxiedBy: EX13D29UWA001.ant.amazon.com (10.43.160.187) To EX13D20UWC001.ant.amazon.com (10.43.162.244) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org While tying to add support for the MSR_CORE_THREAD_COUNT MSR in KVM, I realized that we were still in a world where user space has no control over what happens with MSR emulation in KVM. That is bad for multiple reasons. In my case, I wanted to emulate the MSR in user space, because it's a CPU specific register that does not exist on older CPUs and that really only contains informational data that is on the package level, so it's a natural fit for user space to provide it. However, it is also bad on a platform compatibility level. Currrently, KVM has no way to expose different MSRs based on the selected target CPU type. This patch set introduces a way for user space to indicate to KVM which MSRs should be handled in kernel space. With that, we can solve part of the platform compatibility story. Or at least we can not handle AMD specific MSRs on an Intel platform and vice versa. In addition, it introduces a way for user space to get into the loop when an MSR access would generate a #GP fault, such as when KVM finds an MSR that is not handled by the in-kernel MSR emulation or when the guest is trying to access reserved registers. In combination with the allow list, the user space trapping allows us to emulate arbitrary MSRs in user space, paving the way for target CPU specific MSR implementations from user space. v1 -> v2: - s/ETRAP_TO_USER_SPACE/ENOENT/g - deflect all #GP injection events to user space, not just unknown MSRs. That was we can also deflect allowlist errors later - fix emulator case - new patch: KVM: x86: Introduce allow list for MSR emulation - new patch: KVM: selftests: Add test for user space MSR handling v2 -> v3: - return r if r == X86EMUL_IO_NEEDED - s/KVM_EXIT_RDMSR/KVM_EXIT_X86_RDMSR/g - s/KVM_EXIT_WRMSR/KVM_EXIT_X86_WRMSR/g - Use complete_userspace_io logic instead of reply field - Simplify trapping code - document flags for KVM_X86_ADD_MSR_ALLOWLIST - generalize exit path, always unlock when returning - s/KVM_CAP_ADD_MSR_ALLOWLIST/KVM_CAP_X86_MSR_ALLOWLIST/g - Add KVM_X86_CLEAR_MSR_ALLOWLIST - Add test to clear whitelist - Adjust to reply-less API - Fix asserts - Actually trap on MSR_IA32_POWER_CTL writes v3 -> v4: - Mention exit reasons in re-enter mandatory section of API documentation - Clear padding bytes - Generalize get/set deflect functions - Remove redundant pending_user_msr field - lock allow check and clearing - free bitmaps on clear v4 -> v5: - use srcu Alexander Graf (3): KVM: x86: Deflect unknown MSR accesses to user space KVM: x86: Introduce allow list for MSR emulation KVM: selftests: Add test for user space MSR handling Documentation/virt/kvm/api.rst | 157 ++++++++++- arch/x86/include/asm/kvm_host.h | 13 + arch/x86/include/uapi/asm/kvm.h | 15 + arch/x86/kvm/emulate.c | 18 +- arch/x86/kvm/x86.c | 266 +++++++++++++++++- include/trace/events/kvm.h | 2 +- include/uapi/linux/kvm.h | 15 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/user_msr_test.c | 221 +++++++++++++++ 9 files changed, 699 insertions(+), 9 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/user_msr_test.c -- 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