Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2154336imn; Mon, 1 Aug 2022 13:14:55 -0700 (PDT) X-Google-Smtp-Source: AA6agR7+0R+h89SApSLkt7ihMfiYbDuGYcVJb8Zh2Pu3qdrWu8Eu/ay+v1+7yXX9etBnDeyO2JD6 X-Received: by 2002:a17:907:7f21:b0:730:996d:c5fe with SMTP id qf33-20020a1709077f2100b00730996dc5femr2033907ejc.8.1659384895285; Mon, 01 Aug 2022 13:14:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659384895; cv=none; d=google.com; s=arc-20160816; b=MO43WDbaoB/spnIUknvPYgZpcZETvm5hXRJVUNoAceeyjGp7w/u+A6hboxwzOJZY6I 7Gprf7L3kazaPgvdLlNlFGAbS40GcXayxahHVh8XIkWmrKRTAC7Pp9Tlplp4YACC4lHp pzL61ZMi1vEdEPQ+0thLP1zQqFLJP0gHocR4nfWbogHFWPltgkyQQSRHkOICayZ9fMjy lWN47a2/RafpPzcLzlrw8tD1mAm4pN7pgS/xmRKMHvF6CAMZn3PTYC2R0cpBnifciEg7 nzAFZVrF7/wAGSIiMtY2ddHIt4gvzaoZH0PEZ3sRj35cnIclz+pkyqbr5UdtSbIHy5BE DmQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=elXFRMl+TPWVw4RsGCOtI4aBZkoW99z5/v1N8JJ5B2o=; b=n7/Y3Gxn3vhRL1I+NNza8twmzK43GuZzGWygPepDWH60cY59ormgILaK/EMDiYX9Aw F/qG9ynvHkXoGBriix2w84jY8LocPyuwV3GAYWy/eKmta+AUjQ+yOJKjYFg5uZb6ZzEc 98CnkVXlLlFuVdioGDONiFRCOLqFEvPL3CZUlEKEIvp1duPjD45sSQIkzA9++7p8ENbv YmARhpq2ZM2Ja+kgTqHd8Oh9NwX7XRPVtB3lgkjQsitT/RyC/7DvOQ7phDDWBeOJDyoc sLQ6bwjkUscRqEVZU9zFks4y5GGUHbPwSuakVB/UYhCvfmoUi8nwY7EbO06LqmrBo4+a zlUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=lmikIVKD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ba12-20020a0564021acc00b0043cdf660c18si6170380edb.323.2022.08.01.13.14.31; Mon, 01 Aug 2022 13:14:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=lmikIVKD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S234816AbiHAULa (ORCPT + 99 others); Mon, 1 Aug 2022 16:11:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234243AbiHAULT (ORCPT ); Mon, 1 Aug 2022 16:11:19 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A0431F2C7 for ; Mon, 1 Aug 2022 13:11:18 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id k11-20020a170902ce0b00b0016a15fe2627so7840298plg.22 for ; Mon, 01 Aug 2022 13:11:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=elXFRMl+TPWVw4RsGCOtI4aBZkoW99z5/v1N8JJ5B2o=; b=lmikIVKDCFkRraqjGkbRJBXmPX11R6NT64Z89TO/DkDCQfeSRb2e9KbUCRmiBFcotz pLvXeD6v4X0xmkdlDFIyiW+gpVD4fURZXxx/Bk4ZZlupYgWJk3pgQVB4w9LWVjAM2mEp 8EAc6IBtZU0CvWXH4zki2RzwkTUYMAai/jamYJLF2S/WVetZ/nLgcujsPf0IDU8V7bGN zhxTCE2u+j6k4UJ8F0NbtW4qiQm8pd0gKXML6DJz8tGl5DPcovhnhley3ANRlR1z5Z6v uGPUyKzyO6b26nl1RrsvH098E+c+dAi+kpJ4Xa7u4BHbOB3oz51Lejl2dORuRkXkn+as 1rPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=elXFRMl+TPWVw4RsGCOtI4aBZkoW99z5/v1N8JJ5B2o=; b=Oe/bfHaMDxPWjHiJTgeC6baPIz/aVfIv8o/OiDJaVeYC1m8i3Je/tLYM1HT9gTcsuk 1o7bJMWxVntPgGAV1rNSYt0K0/6EYkB2lATpEL4mZ0RTc/97pFhj3klu9H2q/8vpooiS D8F+ozz4Fny6X4uaMmQLXBKV4Wo8+iFyN9SuRnrJoYUxksiH/mV9XyDJsXA0yOAec+Xr zWPCnnwkWkagSCehguaQWFGZxDTMbiV18t7ZzoLmhpSbLcBWX4KrdEiQ8/UzG1Utq5YW ApohTpec4yUwDvchd9hegq/Ov75kqLfPZSISCqWZhzhbjVANbTYSg2MVUIChPIy6ehCB 4U3A== X-Gm-Message-State: AJIora+C6JfA/eVtiRioSl26kYQeQMMK04eb5Ut2SmlSdLgK8yOD/Hxl vBKUEpY9Q4B7yiX1TQbGzDZ7AsRX1P4= X-Received: from pgonda1.kir.corp.google.com ([2620:15c:29:203:6be2:f99c:e23c:fa12]) (user=pgonda job=sendgmr) by 2002:a05:6a00:1747:b0:52b:1cee:1384 with SMTP id j7-20020a056a00174700b0052b1cee1384mr17266082pfc.41.1659384677806; Mon, 01 Aug 2022 13:11:17 -0700 (PDT) Date: Mon, 1 Aug 2022 13:11:01 -0700 In-Reply-To: <20220801201109.825284-1-pgonda@google.com> Message-Id: <20220801201109.825284-4-pgonda@google.com> Mime-Version: 1.0 References: <20220801201109.825284-1-pgonda@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [V2 03/11] KVM: selftests: add hooks for managing encrypted guest memory From: Peter Gonda To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, marcorr@google.com, seanjc@google.com, michael.roth@amd.com, thomas.lendacky@amd.com, joro@8bytes.org, mizhang@google.com, pbonzini@redhat.com, Peter Gonda Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Michael Roth VM implementations that make use of encrypted memory need a way to configure things like the encryption/shared bit position for page table handling, the default encryption policy for internal allocations made by the core library, and a way to fetch the list/bitmap of encrypted pages to do the actual memory encryption. Add an interface to configure these parameters. Also introduce a sparsebit map to track allocations/mappings that should be treated as encrypted, and provide a way for VM implementations to retrieve it to handle operations related memory encryption. Reviewed-by: Mingwei Zhang Signed-off-by: Michael Roth Signed-off-by: Peter Gonda --- .../selftests/kvm/include/kvm_util_base.h | 17 ++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 52 +++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 24fde97f6121..3928351e497e 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -32,6 +32,7 @@ typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */ struct userspace_mem_region { struct kvm_userspace_memory_region region; struct sparsebit *unused_phy_pages; + struct sparsebit *encrypted_phy_pages; int fd; off_t offset; void *host_mem; @@ -64,6 +65,14 @@ struct userspace_mem_regions { DECLARE_HASHTABLE(slot_hash, 9); }; +/* Memory encryption policy/configuration. */ +struct vm_memcrypt { + bool enabled; + int8_t enc_by_default; + bool has_enc_bit; + int8_t enc_bit; +}; + struct kvm_vm { int mode; unsigned long type; @@ -87,6 +96,7 @@ struct kvm_vm { vm_vaddr_t idt; vm_vaddr_t handlers; uint32_t dirty_ring_size; + struct vm_memcrypt memcrypt; /* Cache of information for binary stats interface */ int stats_fd; @@ -834,4 +844,11 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm) return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0); } +void vm_set_memory_encryption(struct kvm_vm *vm, bool enc_by_default, bool has_enc_bit, + uint8_t enc_bit); + +const struct sparsebit *vm_get_encrypted_phy_pages(struct kvm_vm *vm, int slot, + vm_paddr_t *gpa_start, + uint64_t *size); + #endif /* SELFTEST_KVM_UTIL_BASE_H */ diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index cb3a5f8a53b7..c6b87b411186 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -542,6 +542,7 @@ static void __vm_mem_region_delete(struct kvm_vm *vm, vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION, ®ion->region); sparsebit_free(®ion->unused_phy_pages); + sparsebit_free(®ion->encrypted_phy_pages); ret = munmap(region->mmap_start, region->mmap_size); TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret)); @@ -882,6 +883,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, } region->unused_phy_pages = sparsebit_alloc(); + region->encrypted_phy_pages = sparsebit_alloc(); sparsebit_set_num(region->unused_phy_pages, guest_paddr >> vm->page_shift, npages); region->region.slot = slot; @@ -1097,6 +1099,7 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) * num - number of pages * paddr_min - Physical address minimum * memslot - Memory region to allocate page from + * encrypt - Whether to treat the pages as encrypted * * Output Args: None * @@ -1108,8 +1111,9 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) * and their base address is returned. A TEST_ASSERT failure occurs if * not enough pages are available at or above paddr_min. */ -vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, - vm_paddr_t paddr_min, uint32_t memslot) +static vm_paddr_t +_vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, vm_paddr_t paddr_min, + uint32_t memslot, bool encrypt) { struct userspace_mem_region *region; sparsebit_idx_t pg, base; @@ -1141,12 +1145,22 @@ vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, abort(); } - for (pg = base; pg < base + num; ++pg) + for (pg = base; pg < base + num; ++pg) { sparsebit_clear(region->unused_phy_pages, pg); + if (encrypt) + sparsebit_set(region->encrypted_phy_pages, pg); + } return base * vm->page_size; } +vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, + vm_paddr_t paddr_min, uint32_t memslot) +{ + return _vm_phy_pages_alloc(vm, num, paddr_min, memslot, + vm->memcrypt.enc_by_default); +} + vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min, uint32_t memslot) { @@ -1730,6 +1744,10 @@ void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) region->host_mem); fprintf(stream, "%*sunused_phy_pages: ", indent + 2, ""); sparsebit_dump(stream, region->unused_phy_pages, 0); + if (vm->memcrypt.enabled) { + fprintf(stream, "%*sencrypted_phy_pages: ", indent + 2, ""); + sparsebit_dump(stream, region->encrypted_phy_pages, 0); + } } fprintf(stream, "%*sMapped Virtual Pages:\n", indent, ""); sparsebit_dump(stream, vm->vpages_mapped, indent + 2); @@ -1978,3 +1996,31 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, break; } } + +void vm_set_memory_encryption(struct kvm_vm *vm, bool enc_by_default, bool has_enc_bit, + uint8_t enc_bit) +{ + vm->memcrypt.enabled = true; + vm->memcrypt.enc_by_default = enc_by_default; + vm->memcrypt.has_enc_bit = has_enc_bit; + vm->memcrypt.enc_bit = enc_bit; +} + +const struct sparsebit * +vm_get_encrypted_phy_pages(struct kvm_vm *vm, int slot, vm_paddr_t *gpa_start, + uint64_t *size) +{ + struct userspace_mem_region *region; + + if (!vm->memcrypt.enabled) + return NULL; + + region = memslot2region(vm, slot); + if (!region) + return NULL; + + *size = region->region.memory_size; + *gpa_start = region->region.guest_phys_addr; + + return region->encrypted_phy_pages; +} -- 2.37.1.455.g008518b4e5-goog