From patchwork Thu Mar 2 15:15:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 9601539 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 854E660453 for ; Thu, 2 Mar 2017 21:06:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 73F0E285E0 for ; Thu, 2 Mar 2017 21:06:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 64C3B285F0; Thu, 2 Mar 2017 21:06:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 705F1285EE for ; Thu, 2 Mar 2017 21:06:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750778AbdCBVGA (ORCPT ); Thu, 2 Mar 2017 16:06:00 -0500 Received: from mail-by2nam03on0079.outbound.protection.outlook.com ([104.47.42.79]:19147 "EHLO NAM03-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750743AbdCBVF7 (ORCPT ); Thu, 2 Mar 2017 16:05:59 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=kd2xR2QzyZlChDbQconLsEirpaAJtcoPCCbew0yUuPk=; b=Bq0MieD/Cx5rPy82ZxGKmA1QrdhcDQdlDShabBM5wZPdm6BjGFo1S6SbbKtERFfo3fBz+YgTXAJKA28Z+t30ZRhsdUClZHCnx23PJPOBEAME6tI/XhkJpmIRm6WJ4frdcc17+TJBVniJsXhwBlvQOGTWaI0URIS+ssmRr5cl38c= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none; vger.kernel.org; dmarc=none action=none header.from=amd.com; Received: from [127.0.1.1] (165.204.77.1) by BN6PR12MB1601.namprd12.prod.outlook.com (10.172.20.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.947.12; Thu, 2 Mar 2017 15:15:52 +0000 Subject: [RFC PATCH v2 17/32] x86: kvmclock: Clear encryption attribute when SEV is active From: Brijesh Singh To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Date: Thu, 2 Mar 2017 10:15:48 -0500 Message-ID: <148846774841.2349.10333552865190947646.stgit@brijesh-build-machine> In-Reply-To: <148846752022.2349.13667498174822419498.stgit@brijesh-build-machine> References: <148846752022.2349.13667498174822419498.stgit@brijesh-build-machine> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: MWHPR14CA0024.namprd14.prod.outlook.com (10.173.227.34) To BN6PR12MB1601.namprd12.prod.outlook.com (10.172.20.23) X-MS-Office365-Filtering-Correlation-Id: 3e464e64-f068-404f-8744-08d4617f0899 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(48565401081); SRVR:BN6PR12MB1601; X-Microsoft-Exchange-Diagnostics: 1; BN6PR12MB1601; 3:YqkDANUJTqhFLrkhlNiuNEuOQtZbpuLVC9Q3ITMFxHZxoj8LGBNwZL+sP5aMV+TH93gnHOX1TsnnX3Cj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uzu87bbAaf3j7u0LwUzaWCxAk9SB6PKS8RXItttEpEeWdVZCTDLdBhbr/SCHhtLlzkPHVJZB4tECWNOWliEEvXybWBTwlpMXW9QD+H0j0KldbTl7srkk+EeTgD/bWUdwEo1ngC7yUNWSdQ6HcULPSu3HMX2EboYTXCuJO83T6fgRg/oAfPO/lkVTmZlKQDYACVfkzaOEIJ1XatwtKZT9uSBPE1sj34l0vb13gnivnbj82951mSRtFcXnGkBe5YP2+SC3siHfQeUn4hKPOnMZKKrE3hwAKMx1sOr2BY43WOsCsGmTzsHItAhk9iKjj6XIHQ6jMnsLFAzm/YwIiTQYLUV5LLlHh7yUQiFsLXn+J/FQZaUAzIHbrcoelsdUOAzSOH5cEWdOTEjuFb X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(6055026)(6041248)(20161123558025)(20161123555025)(20161123564025)(20161123560025)(20161123562025)(6072148); SRVR:BN6PR12MB1601; BCL:0; PCL:0; RULEID:; SRVR:BN6PR12MB1601; X-Microsoft-Exchange-Diagnostics: 1; BN6PR12MB1601; 4:jVS2tl1hfijhGgGMGmHBHRKca2iFLF+n+UenXpoe9vGvM1dwUjgWCxCSGipaU4nRpBTDMbs37wop4ZfL6wQu7ok1vORSkpu3YHN+qljWjh+82m9oMQrPuzMmA6ToTxZUjSl/TMdMNTyeHhYkx03whkfnXzDrAWohEMH9w9aV6jSlM53i+OHtvz/1fTegzVMczJtGyXu4V4lykrugTDCR74A42Wx/q44xVdbAlYyrb4xxY75uvb0OJYQbdrUHnEMudNiRgB7cTTjqcD08my37LAgOIGt43dWjB7i+XlPau/KwAVkC1pkvx0ho1JB7cFq3N12pHydgnK31i+znuxWTCIGVaO4tYF+gZHxpzS5dhps/Fx6zTYIi5Wpj6UQ8A3zYp+zdLLlixIIwYx2MmMUHWNibzc8vf+QAq9LUhjGVvMja+L8quN+aiizJ7uzoNjt4ZcnLJW5gTOZLRg1afbsd1wlY0T/fVAKkCt9d+mfR4OvmsbRqmRN6po9PaH1AX0GDl2iqPOUT6APbFh0SaFrLh8h6R1cEabfyZtbLVRGNArdVrhz5onMSm/xVX4VrGjlpAAiK48WHSpDXVDThzOYZoerXSzof7tGxnS3ZK8xVrpT8FHlLJmqiOYsM0HJPRhrIefwjU+ik+h+x8ApQ7iswIKGuXFw6tXro61UTek4R3W4= X-Forefront-PRVS: 023495660C X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6049001)(6009001)(7916002)(39410400002)(39450400003)(39840400002)(39860400002)(39850400002)(53936002)(33716001)(4001350100001)(38730400002)(92566002)(8676002)(3846002)(7416002)(23676002)(81166006)(6116002)(77096006)(25786008)(7406005)(2201001)(90366009)(54356999)(33646002)(76176999)(50986999)(6486002)(86362001)(9686003)(83506001)(2950100002)(6666003)(230700001)(47776003)(66066001)(1191002)(7736002)(42186005)(2906002)(305945005)(50466002)(5660300001)(189998001)(103116003)(921003)(2101003)(83996005)(84006005)(217873001)(1121003); DIR:OUT; SFP:1101; SCL:1; SRVR:BN6PR12MB1601; H:[127.0.1.1]; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtCTjZQUjEyTUIxNjAxOzIzOndBYlp6MCtlZ3lzWHE3bmFpdUYycVIrL1hi?= =?utf-8?B?eTRiTnJXcTFlMEQyZEJoSTlmTHBVVHZibm11Si9VSkUwZjJ6dDR2cEpwNWt1?= =?utf-8?B?YzE2djVhUHZBMUpXdG5qdmZJWlQ5c1l3SUhoS2kxcHQwQ3VtU0NPUmdrbUpq?= =?utf-8?B?ZXFaUXRqQUdOYXpHTjM5SjRXdlFLcks4V0hEUnVndjJ6Y0lTOGR5Z0JXSkRK?= =?utf-8?B?L1RYUDlvZ0FjVmNJeDEwZlNjOXV3SFk4Z1lkZjBDaFRabDIxVGNoTXBN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uwywwqrO5n6llYqkL9ofnshemMM18k2AC7BiuxZaLATzwooOjF0zvI4+mm9B9RaQIFOTQvs0s2MmG/uN7Dezer3FSWM=; 5:sMOyOD26ix5huA+LNe24UD+br22wrrDmz3gE1lan/aEfQPpSUzvMsHcl4Uai2xVb2rIHs/LeXhfxLhCWqt5BafQsx2Eu5CUftMjwjIrfNkWGOnMGGK+la+4rHGh/kJqvLom5fSbgmJrWdfkKc5fH6g==; 24:OYi+MA6H6bbZESy/fnIjdg7iWY8Lhv8pZPx3VS0+3cK7e3VSbgL5TX7K7jcB+1tJZMJwYpZQMNZ+Z/wnM1NmF6RFmcT6B8TwYD0ytuEHkps= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BN6PR12MB1601; 7:zNsFXoaFFxdXz1QLjzaBRQ/zAAwT/YtFddCMGE+Ldzr5EVp3djKqdfpGKu7do0Ng241VJGsr7lRbU+JsbjNSmtNTwygfi3uD6WY1LjOcZZoeE10rNTK3x/Eub81AUeOaqpYScr9Qdjl5VkkH2j8KS/kVK8/ONOySYPYotw9iC7pTlYwc6kqAF0uNbjltTeln5BuLuAAvoeMGorg4sycN7gLBaGIjyvxJ38xgZOc/NYmYcMjMqDSrqS7Um+mkU2X/IC+3PKHZszQNjul4JFAdYsMBd6ioDyc5qyFPgGRYhGQuFUttUi11p+Pv7BpR7MP9xVasNin67T2Z5TxNNOqhhQ==; 20:tl3Z2JU4Fgu/SKHpEFGS8fxuTExTmZEwPGnUDqXfHbvTsN1WncpERa3GpE+l7kBXEf+kbBHNad8zB90GLzw0dJQX9d/tCDwtOfXxbNu1c203Y2o7H0MVtj6GwqnyNOSiWhCd3syYxHF/ZVoCFKVYqARZ5D+X07rorTsfdewWTiXTP+KS2vZyXWxIK2c8FApkcCvBNGSI91axBzOPWgjHosjX4ofkd6HMrsnBxsjXTXrz/Kfnr6l4rckvgK6PNkMG X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Mar 2017 15:15:52.8894 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR12MB1601 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The guest physical memory area holding the struct pvclock_wall_clock and struct pvclock_vcpu_time_info are shared with the hypervisor. Hypervisor periodically updates the contents of the memory. When SEV is active we must clear the encryption attributes of the shared memory pages so that both hypervisor and guest can access the data. Signed-off-by: Brijesh Singh --- arch/x86/kernel/kvmclock.c | 65 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index 278de4f..3b38b3d 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -44,7 +45,7 @@ early_param("no-kvmclock", parse_no_kvmclock); /* The hypervisor will put information about time periodically here */ static struct pvclock_vsyscall_time_info *hv_clock; -static struct pvclock_wall_clock wall_clock; +static struct pvclock_wall_clock *wall_clock; struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void) { @@ -62,15 +63,18 @@ static void kvm_get_wallclock(struct timespec *now) int low, high; int cpu; - low = (int)__pa_symbol(&wall_clock); - high = ((u64)__pa_symbol(&wall_clock) >> 32); + if (!wall_clock) + return; + + low = (int)slow_virt_to_phys(wall_clock); + high = ((u64)slow_virt_to_phys(wall_clock) >> 32); native_write_msr(msr_kvm_wall_clock, low, high); cpu = get_cpu(); vcpu_time = &hv_clock[cpu].pvti; - pvclock_read_wallclock(&wall_clock, vcpu_time, now); + pvclock_read_wallclock(wall_clock, vcpu_time, now); put_cpu(); } @@ -246,11 +250,40 @@ static void kvm_shutdown(void) native_machine_shutdown(); } +static phys_addr_t kvm_memblock_alloc(phys_addr_t size, phys_addr_t align) +{ + phys_addr_t mem; + + mem = memblock_alloc(size, align); + if (!mem) + return 0; + + /* When SEV is active clear the encryption attributes of the pages */ + if (sev_active()) { + if (early_set_memory_decrypted(__va(mem), size)) + goto e_free; + } + + return mem; +e_free: + memblock_free(mem, size); + return 0; +} + +static void kvm_memblock_free(phys_addr_t addr, phys_addr_t size) +{ + /* When SEV is active restore the encryption attributes of the pages */ + if (sev_active()) + early_set_memory_encrypted(__va(addr), size); + + memblock_free(addr, size); +} + void __init kvmclock_init(void) { struct pvclock_vcpu_time_info *vcpu_time; - unsigned long mem; - int size, cpu; + unsigned long mem, mem_wall_clock; + int size, cpu, wall_clock_size; u8 flags; size = PAGE_ALIGN(sizeof(struct pvclock_vsyscall_time_info)*NR_CPUS); @@ -267,15 +300,29 @@ void __init kvmclock_init(void) printk(KERN_INFO "kvm-clock: Using msrs %x and %x", msr_kvm_system_time, msr_kvm_wall_clock); - mem = memblock_alloc(size, PAGE_SIZE); - if (!mem) + wall_clock_size = PAGE_ALIGN(sizeof(struct pvclock_wall_clock)); + mem_wall_clock = kvm_memblock_alloc(wall_clock_size, PAGE_SIZE); + if (!mem_wall_clock) return; + + wall_clock = __va(mem_wall_clock); + memset(wall_clock, 0, wall_clock_size); + + mem = kvm_memblock_alloc(size, PAGE_SIZE); + if (!mem) { + kvm_memblock_free(mem_wall_clock, wall_clock_size); + wall_clock = NULL; + return; + } + hv_clock = __va(mem); memset(hv_clock, 0, size); if (kvm_register_clock("primary cpu clock")) { hv_clock = NULL; - memblock_free(mem, size); + kvm_memblock_free(mem, size); + kvm_memblock_free(mem_wall_clock, wall_clock_size); + wall_clock = NULL; return; }