Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp894455ybe; Wed, 4 Sep 2019 09:16:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqxuK2S0xUmNFfF5vMktoXuZUjHgcShi6J18auVyrYrP7jOkQ8HhGP76jSkWGuknqXKUcZ97 X-Received: by 2002:a62:55c1:: with SMTP id j184mr27327968pfb.104.1567613802568; Wed, 04 Sep 2019 09:16:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1567613802; cv=pass; d=google.com; s=arc-20160816; b=0GYha5qMeYpn00oa+9iTPQ6kvYtr/+4+xKwALJ0xQ6MC6KSVrNpt2zH9YM7JB1bEAC 8Jq5ak1H6qNGD7vMhHMzVrgCnLepprEQ35cqsbi9TyF6sYsKYoYCxyczgd7Jo+hNr6tb E+m/H5D2vl0/Q0wg4aepgROTg5R08xb+78tKargH0P1yTKmST6TqsvDOW0Lp4pcj3x52 Mm1gbbLtvKWdLv+M/Avj0btIy8IiGpen/1diIe+tBnF9TNMHDt9n5nh+hhbHnDUX3B83 PaKEJqrYbdxnKGagz1sL+nbeWhvV0QzefVZI3Pagyg8VVNJ9qTLo6iYaE0G9EDT5IClN uw3w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :wdcipoutbound:content-language:accept-language:in-reply-to :references:message-id:date:thread-index:thread-topic:subject:cc:to :from:dkim-signature:ironport-sdr:dkim-signature; bh=jLpgtRZMw1Ojfk9bSlEqp/0+tvj0Ky+rChbEzaumqXQ=; b=i+Gu+0ShdJFuLc253/X1HPHeadqSjvWyaxLNEqiBkWI6QTqQ86zHTt0E0iLRHunrHI wBUQ1bl9+C5B2ZDCG6SKlWIzytrecw2eD8mtzGEGISB/utbWXscBZNvFFRClDfsQO3Js CfXR48NqBYrQfPTjfqDQX9A9fMb6D54HRKTw3B8FHD36YbwwQnksudjzMZ77F2NCX9T8 pHCVO1p5w9wRXbUz6TKFow5nfQndZcJBEIaDYyeRXzbytrNagTtKUxxTm1lZmVBeEyS4 /TuEon+gZ5E4MkThUkT1uijiIC+9I9N5hVe7JS3+BDw6d8NGb9ECgKk4RPNI5sOxqVhq RfvQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=bqDE9LNy; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=m1x+jxrD; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.com); spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v31si17725353plg.339.2019.09.04.09.16.26; Wed, 04 Sep 2019 09:16:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=bqDE9LNy; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=m1x+jxrD; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.com); spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387490AbfIDQPd (ORCPT + 99 others); Wed, 4 Sep 2019 12:15:33 -0400 Received: from esa1.hgst.iphmx.com ([68.232.141.245]:14013 "EHLO esa1.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732834AbfIDQPc (ORCPT ); Wed, 4 Sep 2019 12:15:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1567613732; x=1599149732; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=O86QEd2/Ap83HX4BVDcg/Sbp08Hv2IsfYfDeY1MjDxE=; b=bqDE9LNywdXOGvmmaXuH+Zac1n1HO+3qKeh8MqTkY8bq9vAUCWdEDr6f gZvIAB42lkOpaSwkkyQwuslnZ/LlN6UaNlbrTHKPYHjjrApZ2Y9fUIPWp 5yojEuohMpP29FgEuqapja9jqZTc+pH1knbICLXCkIxr6wzVldcC8D3hl GV2j35NG5P9JYDto7gi1QOkEjiN2/xFjHXIrjzYY7kWadnuNarTyPbmf1 ASxI9XyKxAeJFaai6I9LQGmkykAgYfrnQijhDvH5Pvrp+T+h/I1snnQXO dS78wz00OA9mmGkShe0mVSCUUa4IlBwiRHHoFp4GNoow26EhOZQqgA8Xl g==; IronPort-SDR: /x51QIDROhkuho/j4dIBN/rhu4Qs0GxGOH8SvhQPVxgb5dC0IC8t4hBvk5Rmt69l6DyBNe9rj9 FpIk9SWfnmksG+5dY6W6vyOfVMGwK42bs8tQFLcgeamthBlMdNaJF02gYEZZ1pEJ3wCWDHprh7 lGJuWQluJzsW9r+0sNPL7d+sHnbbjgTe2rNwcG6RBzjV065m/+8eep5INvmAPoA6AmGTZhqNEQ Zh6EvkHAZGQhWoszLTF/nFC62xwfC3vzFyfo84/NklG+KBAgH/QLCQKhW5R9vhIAqBit2tlBL3 VUk= X-IronPort-AV: E=Sophos;i="5.64,467,1559491200"; d="scan'208";a="224155172" Received: from mail-sn1nam01lp2057.outbound.protection.outlook.com (HELO NAM01-SN1-obe.outbound.protection.outlook.com) ([104.47.32.57]) by ob1.hgst.iphmx.com with ESMTP; 05 Sep 2019 00:15:30 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K0hJNn2RhdeTvamWaMSy0Ee2PUfYiG0C3tpQQjEnpbs6Xe6O3slv6zfQid1dw0Wie2uOAUBobSjChjF9zBLBq5Aj8aNphucuc3LOzf6/j7Ip93W9bvDmvM2PerAPFNebfXC5Uo2SpRijs4i6f1ZSfh9fyJCK1tBaBJj/pVFJG7viDqP+b8LEcCGuqLKcg6zdl3WM5SqFV7gGv8PDvnqgH6NHrA454iDw1Wk8daAm4rb/hw7v/1DY6HQohXJJH14JtGkO5yquHu3Q3N9Ai0CLiMfq433wpHxJa09LQtg46TsOwBZOQX7KIrYdfgWvX4RBLB+yu2+R/qhW/8MufI8Nyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=jLpgtRZMw1Ojfk9bSlEqp/0+tvj0Ky+rChbEzaumqXQ=; b=fpivooN5bbK1Y1zR5M51dC4jrF7TfHEEVbsL9lg6ZTOsh95W+ttmeCVdiLDwNq7mCppUVTkiMSWWlklHyYIN4x7bAhirGkwYSWuktsOk9PEiBuYI3YJac0VQycQ59TFAw7KlQcbSL0C9IUj9HGbXIfP/SXer2OLsOdnSQMdIBcTxlBybgZ5FjYJxjPqPUOI8xWoGtMe7I2LwybNYLUoi63AcFNR5N8cb3hp3rq0phLxQm5A2Vv+UYU9ln4ayNREfp0uD0xFDaHi52Coz8g8SZ6WWL76x43BaTFN5jhzlkto6xI75fAdcC0ojnCbka59ynEO0kB6OpA67AoMgkE/uyQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=jLpgtRZMw1Ojfk9bSlEqp/0+tvj0Ky+rChbEzaumqXQ=; b=m1x+jxrDMwHC/+uhzXA7X+nKe18b+1+13CO2PazDQ04gI7zORW3hiYreNcYKMPpLUW+5JNMMGPl2TY1icEHndSlLdpThJ5kdBMxXcRr7PSnsBl0PkoZj/zCogIMYCt5mzNcPbic5JroeE+JzCUjegGwosY8J9EKBi6gVY3Rn5Kk= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB5504.namprd04.prod.outlook.com (20.178.247.210) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2178.16; Wed, 4 Sep 2019 16:15:29 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::e1a5:8de2:c3b1:3fb0]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::e1a5:8de2:c3b1:3fb0%7]) with mapi id 15.20.2220.022; Wed, 4 Sep 2019 16:15:29 +0000 From: Anup Patel To: Palmer Dabbelt , Paul Walmsley , Paolo Bonzini , Radim K CC: Daniel Lezcano , Thomas Gleixner , Alexander Graf , Atish Patra , Alistair Francis , Damien Le Moal , Christoph Hellwig , Anup Patel , "kvm@vger.kernel.org" , "linux-riscv@lists.infradead.org" , "linux-kernel@vger.kernel.org" , Anup Patel Subject: [PATCH v7 13/21] RISC-V: KVM: Implement stage2 page table programming Thread-Topic: [PATCH v7 13/21] RISC-V: KVM: Implement stage2 page table programming Thread-Index: AQHVYzv3gyC2erR3F0CqPOvBPdBB+w== Date: Wed, 4 Sep 2019 16:15:28 +0000 Message-ID: <20190904161245.111924-15-anup.patel@wdc.com> References: <20190904161245.111924-1-anup.patel@wdc.com> In-Reply-To: <20190904161245.111924-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MA1PR01CA0084.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00::24) To MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Anup.Patel@wdc.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [49.207.53.222] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 8c8214c1-d861-4f10-b3ed-08d731531a0f x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(7168020)(4627221)(201703031133081)(201702281549075)(8990200)(5600166)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020);SRVR:MN2PR04MB5504; x-ms-traffictypediagnostic: MN2PR04MB5504: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:3044; x-forefront-prvs: 0150F3F97D x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(4636009)(376002)(136003)(396003)(346002)(366004)(39860400002)(199004)(189003)(66556008)(66446008)(7416002)(7736002)(8676002)(478600001)(25786009)(386003)(66946007)(99286004)(256004)(14444005)(50226002)(14454004)(66476007)(64756008)(6506007)(6512007)(102836004)(53946003)(6436002)(8936002)(54906003)(6116002)(3846002)(486006)(26005)(55236004)(86362001)(1076003)(53936002)(476003)(316002)(71200400001)(71190400001)(81156014)(76176011)(52116002)(305945005)(5660300002)(6486002)(36756003)(186003)(11346002)(2616005)(30864003)(66066001)(44832011)(4326008)(2906002)(446003)(81166006)(110136005);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR04MB5504;H:MN2PR04MB6061.namprd04.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: vPo6UdE86TbHNTs/1g0PHcKDKoFcT/tpit8zOvP/wBNSryrzegpmnkr/oGjgvT5QqOMYxqf2qklXwxvBGOR+rCq/AiHdm8+6hFZl4amcaZuCBZNwOFL1s/V4pf1dkU/IKmU8uKF7LG4cJky1Of6G40ChpHii0qFy+89sG3v1XRshGZJBhq+jkoun29z1X4mnS1ObZkDDqkPNU4ypU5A4EhZt9FjoYoYrjszabbvDKQH7zaaaad61oPYKDtAEbPWVanrmhi5DNgYqW1wYQWygw+8igbQBKVIjfcFHfBf2cmTl5RBx2+Fgqy95r91kWp/nKDWuddkJwLbCdjK/lFGJdl/PRdNNxvId94k6ryYcCNNcRhhT7wlLQfWi4Jkhl054QmU+9yiwmfulB5VB9x6sdXMdvOM7RNG6/x0eKZ4lvFo= Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8c8214c1-d861-4f10-b3ed-08d731531a0f X-MS-Exchange-CrossTenant-originalarrivaltime: 04 Sep 2019 16:15:28.9195 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: R0+VH/+lMJtuJGFBTLZBesy4mfHFBtEe9+IL1I4/AH83tPctrwScC6YgdeSE6UThgdDugCAhWR/N1G4T/OgACg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5504 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch implements all required functions for programming the stage2 page table for each Guest/VM. At high-level, the flow of stage2 related functions is similar from KVM ARM/ARM64 implementation but the stage2 page table format is quite different for KVM RISC-V. Signed-off-by: Anup Patel Acked-by: Paolo Bonzini Reviewed-by: Paolo Bonzini --- arch/riscv/include/asm/kvm_host.h | 10 + arch/riscv/include/asm/pgtable-bits.h | 1 + arch/riscv/kvm/mmu.c | 643 +++++++++++++++++++++++++- 3 files changed, 644 insertions(+), 10 deletions(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm= _host.h index 8aaf22a900be..bc27f664b443 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -73,6 +73,13 @@ struct kvm_mmio_decode { int return_handled; }; =20 +#define KVM_MMU_PAGE_CACHE_NR_OBJS 32 + +struct kvm_mmu_page_cache { + int nobjs; + void *objects[KVM_MMU_PAGE_CACHE_NR_OBJS]; +}; + struct kvm_cpu_context { unsigned long zero; unsigned long ra; @@ -164,6 +171,9 @@ struct kvm_vcpu_arch { /* MMIO instruction details */ struct kvm_mmio_decode mmio_decode; =20 + /* Cache pages needed to program page tables with spinlock held */ + struct kvm_mmu_page_cache mmu_page_cache; + /* VCPU power-off state */ bool power_off; =20 diff --git a/arch/riscv/include/asm/pgtable-bits.h b/arch/riscv/include/asm= /pgtable-bits.h index bbaeb5d35842..be49d62fcc2b 100644 --- a/arch/riscv/include/asm/pgtable-bits.h +++ b/arch/riscv/include/asm/pgtable-bits.h @@ -26,6 +26,7 @@ =20 #define _PAGE_SPECIAL _PAGE_SOFT #define _PAGE_TABLE _PAGE_PRESENT +#define _PAGE_LEAF (_PAGE_READ | _PAGE_WRITE | _PAGE_EXEC) =20 /* * _PAGE_PROT_NONE is set on not-present pages (and ignored by the hardwar= e) to diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 2b965f9aac07..590669290139 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -18,6 +18,438 @@ #include #include =20 +#ifdef CONFIG_64BIT +#define stage2_have_pmd true +#define stage2_gpa_size ((phys_addr_t)(1ULL << 39)) +#define stage2_cache_min_pages 2 +#else +#define pmd_index(x) 0 +#define pfn_pmd(x, y) ({ pmd_t __x =3D { 0 }; __x; }) +#define stage2_have_pmd false +#define stage2_gpa_size ((phys_addr_t)(1ULL << 32)) +#define stage2_cache_min_pages 1 +#endif + +static int stage2_cache_topup(struct kvm_mmu_page_cache *pcache, + int min, int max) +{ + void *page; + + BUG_ON(max > KVM_MMU_PAGE_CACHE_NR_OBJS); + if (pcache->nobjs >=3D min) + return 0; + while (pcache->nobjs < max) { + page =3D (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO); + if (!page) + return -ENOMEM; + pcache->objects[pcache->nobjs++] =3D page; + } + + return 0; +} + +static void stage2_cache_flush(struct kvm_mmu_page_cache *pcache) +{ + while (pcache && pcache->nobjs) + free_page((unsigned long)pcache->objects[--pcache->nobjs]); +} + +static void *stage2_cache_alloc(struct kvm_mmu_page_cache *pcache) +{ + void *p; + + if (!pcache) + return NULL; + + BUG_ON(!pcache->nobjs); + p =3D pcache->objects[--pcache->nobjs]; + + return p; +} + +struct local_guest_tlb_info { + struct kvm_vmid *vmid; + gpa_t addr; +}; + +static void local_guest_tlb_flush_vmid_gpa(void *info) +{ + struct local_guest_tlb_info *infop =3D info; + + __kvm_riscv_hfence_gvma_vmid_gpa(READ_ONCE(infop->vmid->vmid_version), + infop->addr); +} + +static void stage2_remote_tlb_flush(struct kvm *kvm, gpa_t addr) +{ + struct local_guest_tlb_info info; + struct kvm_vmid *vmid =3D &kvm->arch.vmid; + + /* + * Ideally, we should have a SBI call OR some remote TLB instruction + * but we don't have it so we explicitly flush TLBs using IPIs. + * + * TODO: Instead of cpu_online_mask, we should only target CPUs + * where the Guest/VM is running. + */ + info.vmid =3D vmid; + info.addr =3D addr; + preempt_disable(); + smp_call_function_many(cpu_online_mask, + local_guest_tlb_flush_vmid_gpa, &info, true); + preempt_enable(); +} + +static int stage2_set_pgd(struct kvm *kvm, gpa_t addr, const pgd_t *new_pg= d) +{ + pgd_t *pgdp =3D &kvm->arch.pgd[pgd_index(addr)]; + + *pgdp =3D *new_pgd; + if (pgd_val(*pgdp) & _PAGE_LEAF) + stage2_remote_tlb_flush(kvm, addr); + + return 0; +} + +static int stage2_set_pmd(struct kvm *kvm, struct kvm_mmu_page_cache *pcac= he, + gpa_t addr, const pmd_t *new_pmd) +{ + int rc; + pmd_t *pmdp; + pgd_t new_pgd; + pgd_t *pgdp =3D &kvm->arch.pgd[pgd_index(addr)]; + + if (!pgd_val(*pgdp)) { + pmdp =3D stage2_cache_alloc(pcache); + if (!pmdp) + return -ENOMEM; + new_pgd =3D pfn_pgd(PFN_DOWN(__pa(pmdp)), __pgprot(_PAGE_TABLE)); + rc =3D stage2_set_pgd(kvm, addr, &new_pgd); + if (rc) + return rc; + } + + if (pgd_val(*pgdp) & _PAGE_LEAF) + return -EEXIST; + + pmdp =3D (void *)pgd_page_vaddr(*pgdp); + pmdp =3D &pmdp[pmd_index(addr)]; + + *pmdp =3D *new_pmd; + if (pmd_val(*pmdp) & _PAGE_LEAF) + stage2_remote_tlb_flush(kvm, addr); + + return 0; +} + +static int stage2_set_pte(struct kvm *kvm, + struct kvm_mmu_page_cache *pcache, + gpa_t addr, const pte_t *new_pte) +{ + int rc; + pte_t *ptep; + pmd_t new_pmd; + pmd_t *pmdp; + pgd_t new_pgd; + pgd_t *pgdp =3D &kvm->arch.pgd[pgd_index(addr)]; + + if (!pgd_val(*pgdp)) { + pmdp =3D stage2_cache_alloc(pcache); + if (!pmdp) + return -ENOMEM; + new_pgd =3D pfn_pgd(PFN_DOWN(__pa(pmdp)), __pgprot(_PAGE_TABLE)); + rc =3D stage2_set_pgd(kvm, addr, &new_pgd); + if (rc) + return rc; + } + + if (pgd_val(*pgdp) & _PAGE_LEAF) + return -EEXIST; + + if (stage2_have_pmd) { + pmdp =3D (void *)pgd_page_vaddr(*pgdp); + pmdp =3D &pmdp[pmd_index(addr)]; + if (!pmd_present(*pmdp)) { + ptep =3D stage2_cache_alloc(pcache); + if (!ptep) + return -ENOMEM; + new_pmd =3D pfn_pmd(PFN_DOWN(__pa(ptep)), + __pgprot(_PAGE_TABLE)); + rc =3D stage2_set_pmd(kvm, pcache, addr, &new_pmd); + if (rc) + return rc; + } + + if (pmd_val(*pmdp) & _PAGE_LEAF) + return -EEXIST; + + ptep =3D (void *)pmd_page_vaddr(*pmdp); + } else { + ptep =3D (void *)pgd_page_vaddr(*pgdp); + } + + ptep =3D &ptep[pte_index(addr)]; + + *ptep =3D *new_pte; + if (pte_val(*ptep) & _PAGE_LEAF) + stage2_remote_tlb_flush(kvm, addr); + + return 0; +} + +static int stage2_map_page(struct kvm *kvm, + struct kvm_mmu_page_cache *pcache, + gpa_t gpa, phys_addr_t hpa, + unsigned long page_size, pgprot_t prot) +{ + pte_t new_pte; + pmd_t new_pmd; + pgd_t new_pgd; + + if (page_size =3D=3D PAGE_SIZE) { + new_pte =3D pfn_pte(PFN_DOWN(hpa), prot); + return stage2_set_pte(kvm, pcache, gpa, &new_pte); + } + + if (stage2_have_pmd && page_size =3D=3D PMD_SIZE) { + new_pmd =3D pfn_pmd(PFN_DOWN(hpa), prot); + return stage2_set_pmd(kvm, pcache, gpa, &new_pmd); + } + + if (page_size =3D=3D PGDIR_SIZE) { + new_pgd =3D pfn_pgd(PFN_DOWN(hpa), prot); + return stage2_set_pgd(kvm, gpa, &new_pgd); + } + + return -EINVAL; +} + +enum stage2_op { + STAGE2_OP_NOP =3D 0, /* Nothing */ + STAGE2_OP_CLEAR, /* Clear/Unmap */ + STAGE2_OP_WP, /* Write-protect */ +}; + +static void stage2_op_pte(struct kvm *kvm, gpa_t addr, pte_t *ptep, + enum stage2_op op) +{ + BUG_ON(addr & (PAGE_SIZE - 1)); + + if (!pte_present(*ptep)) + return; + + if (op =3D=3D STAGE2_OP_CLEAR) + set_pte(ptep, __pte(0)); + else if (op =3D=3D STAGE2_OP_WP) + set_pte(ptep, __pte(pte_val(*ptep) & ~_PAGE_WRITE)); + stage2_remote_tlb_flush(kvm, addr); +} + +static void stage2_op_pmd(struct kvm *kvm, gpa_t addr, pmd_t *pmdp, + enum stage2_op op) +{ + int i; + pte_t *ptep; + + BUG_ON(addr & (PMD_SIZE - 1)); + + if (!pmd_present(*pmdp)) + return; + + if (pmd_val(*pmdp) & _PAGE_LEAF) + ptep =3D NULL; + else + ptep =3D (pte_t *)pmd_page_vaddr(*pmdp); + + if (op =3D=3D STAGE2_OP_CLEAR) + set_pmd(pmdp, __pmd(0)); + + if (ptep) { + for (i =3D 0; i < PTRS_PER_PTE; i++) + stage2_op_pte(kvm, addr + i * PAGE_SIZE, &ptep[i], op); + if (op =3D=3D STAGE2_OP_CLEAR) + put_page(virt_to_page(ptep)); + } else { + if (op =3D=3D STAGE2_OP_WP) + set_pmd(pmdp, __pmd(pmd_val(*pmdp) & ~_PAGE_WRITE)); + stage2_remote_tlb_flush(kvm, addr); + } +} + +static void stage2_op_pgd(struct kvm *kvm, gpa_t addr, pgd_t *pgdp, + enum stage2_op op) +{ + int i; + pte_t *ptep; + pmd_t *pmdp; + + BUG_ON(addr & (PGDIR_SIZE - 1)); + + if (!pgd_val(*pgdp)) + return; + + ptep =3D NULL; + pmdp =3D NULL; + if (!(pgd_val(*pgdp) & _PAGE_LEAF)) { + if (stage2_have_pmd) + pmdp =3D (pmd_t *)pgd_page_vaddr(*pgdp); + else + ptep =3D (pte_t *)pgd_page_vaddr(*pgdp); + } + + if (op =3D=3D STAGE2_OP_CLEAR) + set_pgd(pgdp, __pgd(0)); + + if (pmdp) { + for (i =3D 0; i < PTRS_PER_PMD; i++) + stage2_op_pmd(kvm, addr + i * PMD_SIZE, &pmdp[i], op); + if (op =3D=3D STAGE2_OP_CLEAR) + put_page(virt_to_page(pmdp)); + } else if (ptep) { + for (i =3D 0; i < PTRS_PER_PTE; i++) + stage2_op_pte(kvm, addr + i * PAGE_SIZE, &ptep[i], op); + if (op =3D=3D STAGE2_OP_CLEAR) + put_page(virt_to_page(ptep)); + } else { + if (op =3D=3D STAGE2_OP_WP) + set_pgd(pgdp, __pgd(pgd_val(*pgdp) & ~_PAGE_WRITE)); + stage2_remote_tlb_flush(kvm, addr); + } +} + +static void stage2_unmap_range(struct kvm *kvm, gpa_t start, gpa_t size) +{ + pmd_t *pmdp; + pte_t *ptep; + pgd_t *pgdp; + gpa_t addr =3D start, end =3D start + size; + + while (addr < end) { + pgdp =3D &kvm->arch.pgd[pgd_index(addr)]; + if (!pgd_val(*pgdp)) { + addr +=3D PGDIR_SIZE; + continue; + } else if (!(addr & (PGDIR_SIZE - 1)) && + ((end - addr) >=3D PGDIR_SIZE)) { + stage2_op_pgd(kvm, addr, pgdp, STAGE2_OP_CLEAR); + addr +=3D PGDIR_SIZE; + continue; + } + + if (stage2_have_pmd) { + pmdp =3D (pmd_t *)pgd_page_vaddr(*pgdp); + if (!pmd_present(*pmdp)) { + addr +=3D PMD_SIZE; + continue; + } else if (!(addr & (PMD_SIZE - 1)) && + ((end - addr) >=3D PMD_SIZE)) { + stage2_op_pmd(kvm, addr, pmdp, + STAGE2_OP_CLEAR); + addr +=3D PMD_SIZE; + continue; + } + ptep =3D (pte_t *)pmd_page_vaddr(*pmdp); + } else { + ptep =3D (pte_t *)pgd_page_vaddr(*pgdp); + } + + stage2_op_pte(kvm, addr, ptep, STAGE2_OP_CLEAR); + addr +=3D PAGE_SIZE; + } +} + +static void stage2_wp_range(struct kvm *kvm, gpa_t start, gpa_t end) +{ + pmd_t *pmdp; + pte_t *ptep; + pgd_t *pgdp; + gpa_t addr =3D start; + + while (addr < end) { + pgdp =3D &kvm->arch.pgd[pgd_index(addr)]; + if (!pgd_val(*pgdp)) { + addr +=3D PGDIR_SIZE; + continue; + } else if (!(addr & (PGDIR_SIZE - 1)) && + ((end - addr) >=3D PGDIR_SIZE)) { + stage2_op_pgd(kvm, addr, pgdp, STAGE2_OP_WP); + addr +=3D PGDIR_SIZE; + continue; + } + + if (stage2_have_pmd) { + pmdp =3D (pmd_t *)pgd_page_vaddr(*pgdp); + if (!pmd_present(*pmdp)) { + addr +=3D PMD_SIZE; + continue; + } else if (!(addr & (PMD_SIZE - 1)) && + ((end - addr) >=3D PMD_SIZE)) { + stage2_op_pmd(kvm, addr, pmdp, STAGE2_OP_WP); + addr +=3D PMD_SIZE; + continue; + } + ptep =3D (pte_t *)pmd_page_vaddr(*pmdp); + } else { + ptep =3D (pte_t *)pgd_page_vaddr(*pgdp); + } + + stage2_op_pte(kvm, addr, ptep, STAGE2_OP_WP); + addr +=3D PAGE_SIZE; + } +} + +void stage2_wp_memory_region(struct kvm *kvm, int slot) +{ + struct kvm_memslots *slots =3D kvm_memslots(kvm); + struct kvm_memory_slot *memslot =3D id_to_memslot(slots, slot); + phys_addr_t start =3D memslot->base_gfn << PAGE_SHIFT; + phys_addr_t end =3D (memslot->base_gfn + memslot->npages) << PAGE_SHIFT; + + spin_lock(&kvm->mmu_lock); + stage2_wp_range(kvm, start, end); + spin_unlock(&kvm->mmu_lock); + kvm_flush_remote_tlbs(kvm); +} + +int stage2_ioremap(struct kvm *kvm, gpa_t gpa, phys_addr_t hpa, + unsigned long size, bool writable) +{ + pte_t pte; + int ret =3D 0; + unsigned long pfn; + phys_addr_t addr, end; + struct kvm_mmu_page_cache pcache =3D { 0, }; + + end =3D (gpa + size + PAGE_SIZE - 1) & PAGE_MASK; + pfn =3D __phys_to_pfn(hpa); + + for (addr =3D gpa; addr < end; addr +=3D PAGE_SIZE) { + pte =3D pfn_pte(pfn, PAGE_KERNEL); + + if (!writable) + pte =3D pte_wrprotect(pte); + + ret =3D stage2_cache_topup(&pcache, + stage2_cache_min_pages, + KVM_MMU_PAGE_CACHE_NR_OBJS); + if (ret) + goto out; + + spin_lock(&kvm->mmu_lock); + ret =3D stage2_set_pte(kvm, &pcache, addr, &pte); + spin_unlock(&kvm->mmu_lock); + if (ret) + goto out; + + pfn++; + } + +out: + stage2_cache_flush(&pcache); + return ret; + +} + void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, struct kvm_memory_slot *dont) { @@ -35,7 +467,7 @@ void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) =20 void kvm_arch_flush_shadow_all(struct kvm *kvm) { - /* TODO: */ + kvm_riscv_stage2_free_pgd(kvm); } =20 void kvm_arch_flush_shadow_memslot(struct kvm *kvm, @@ -49,7 +481,13 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, const struct kvm_memory_slot *new, enum kvm_mr_change change) { - /* TODO: */ + /* + * At this point memslot has been committed and there is an + * allocated dirty_bitmap[], dirty pages will be be tracked while the + * memory slot is write protected. + */ + if (change !=3D KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES) + stage2_wp_memory_region(kvm, mem->slot); } =20 int kvm_arch_prepare_memory_region(struct kvm *kvm, @@ -57,34 +495,219 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, const struct kvm_userspace_memory_region *mem, enum kvm_mr_change change) { - /* TODO: */ - return 0; + hva_t hva =3D mem->userspace_addr; + hva_t reg_end =3D hva + mem->memory_size; + bool writable =3D !(mem->flags & KVM_MEM_READONLY); + int ret =3D 0; + + if (change !=3D KVM_MR_CREATE && change !=3D KVM_MR_MOVE && + change !=3D KVM_MR_FLAGS_ONLY) + return 0; + + /* + * Prevent userspace from creating a memory region outside of the GPA + * space addressable by the KVM guest GPA space. + */ + if ((memslot->base_gfn + memslot->npages) >=3D + (stage2_gpa_size >> PAGE_SHIFT)) + return -EFAULT; + + down_read(¤t->mm->mmap_sem); + + /* + * A memory region could potentially cover multiple VMAs, and + * any holes between them, so iterate over all of them to find + * out if we can map any of them right now. + * + * +--------------------------------------------+ + * +---------------+----------------+ +----------------+ + * | : VMA 1 | VMA 2 | | VMA 3 : | + * +---------------+----------------+ +----------------+ + * | memory region | + * +--------------------------------------------+ + */ + do { + struct vm_area_struct *vma =3D find_vma(current->mm, hva); + hva_t vm_start, vm_end; + + if (!vma || vma->vm_start >=3D reg_end) + break; + + /* + * Mapping a read-only VMA is only allowed if the + * memory region is configured as read-only. + */ + if (writable && !(vma->vm_flags & VM_WRITE)) { + ret =3D -EPERM; + break; + } + + /* Take the intersection of this VMA with the memory region */ + vm_start =3D max(hva, vma->vm_start); + vm_end =3D min(reg_end, vma->vm_end); + + if (vma->vm_flags & VM_PFNMAP) { + gpa_t gpa =3D mem->guest_phys_addr + + (vm_start - mem->userspace_addr); + phys_addr_t pa; + + pa =3D (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT; + pa +=3D vm_start - vma->vm_start; + + /* IO region dirty page logging not allowed */ + if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) { + ret =3D -EINVAL; + goto out; + } + + ret =3D stage2_ioremap(kvm, gpa, pa, + vm_end - vm_start, writable); + if (ret) + break; + } + hva =3D vm_end; + } while (hva < reg_end); + + if (change =3D=3D KVM_MR_FLAGS_ONLY) + goto out; + + spin_lock(&kvm->mmu_lock); + if (ret) + stage2_unmap_range(kvm, mem->guest_phys_addr, + mem->memory_size); + spin_unlock(&kvm->mmu_lock); + +out: + up_read(¤t->mm->mmap_sem); + return ret; } =20 int kvm_riscv_stage2_map(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned long h= va, bool is_write) { - /* TODO: */ - return 0; + int ret; + short lsb; + kvm_pfn_t hfn; + bool writeable; + gfn_t gfn =3D gpa >> PAGE_SHIFT; + struct vm_area_struct *vma; + struct kvm *kvm =3D vcpu->kvm; + struct kvm_mmu_page_cache *pcache =3D &vcpu->arch.mmu_page_cache; + unsigned long vma_pagesize; + + down_read(¤t->mm->mmap_sem); + + vma =3D find_vma_intersection(current->mm, hva, hva + 1); + if (unlikely(!vma)) { + kvm_err("Failed to find VMA for hva 0x%lx\n", hva); + up_read(¤t->mm->mmap_sem); + return -EFAULT; + } + + vma_pagesize =3D vma_kernel_pagesize(vma); + + up_read(¤t->mm->mmap_sem); + + if (vma_pagesize !=3D PGDIR_SIZE && + vma_pagesize !=3D PMD_SIZE && + vma_pagesize !=3D PAGE_SIZE) { + kvm_err("Invalid VMA page size 0x%lx\n", vma_pagesize); + return -EFAULT; + } + + /* We need minimum second+third level pages */ + ret =3D stage2_cache_topup(pcache, stage2_cache_min_pages, + KVM_MMU_PAGE_CACHE_NR_OBJS); + if (ret) { + kvm_err("Failed to topup stage2 cache\n"); + return ret; + } + + hfn =3D gfn_to_pfn_prot(kvm, gfn, is_write, &writeable); + if (hfn =3D=3D KVM_PFN_ERR_HWPOISON) { + if (is_vm_hugetlb_page(vma)) + lsb =3D huge_page_shift(hstate_vma(vma)); + else + lsb =3D PAGE_SHIFT; + + send_sig_mceerr(BUS_MCEERR_AR, (void __user *)hva, + lsb, current); + return 0; + } + if (is_error_noslot_pfn(hfn)) + return -EFAULT; + if (!writeable && is_write) + return -EPERM; + + spin_lock(&kvm->mmu_lock); + + if (writeable) { + kvm_set_pfn_dirty(hfn); + ret =3D stage2_map_page(kvm, pcache, gpa, hfn << PAGE_SHIFT, + vma_pagesize, PAGE_WRITE_EXEC); + } else { + ret =3D stage2_map_page(kvm, pcache, gpa, hfn << PAGE_SHIFT, + vma_pagesize, PAGE_READ_EXEC); + } + + if (ret) + kvm_err("Failed to map in stage2\n"); + + spin_unlock(&kvm->mmu_lock); + kvm_set_pfn_accessed(hfn); + kvm_release_pfn_clean(hfn); + return ret; } =20 void kvm_riscv_stage2_flush_cache(struct kvm_vcpu *vcpu) { - /* TODO: */ + stage2_cache_flush(&vcpu->arch.mmu_page_cache); } =20 int kvm_riscv_stage2_alloc_pgd(struct kvm *kvm) { - /* TODO: */ + if (kvm->arch.pgd !=3D NULL) { + kvm_err("kvm_arch already initialized?\n"); + return -EINVAL; + } + + kvm->arch.pgd =3D alloc_pages_exact(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO); + if (!kvm->arch.pgd) + return -ENOMEM; + kvm->arch.pgd_phys =3D virt_to_phys(kvm->arch.pgd); + return 0; } =20 void kvm_riscv_stage2_free_pgd(struct kvm *kvm) { - /* TODO: */ + void *pgd =3D NULL; + + spin_lock(&kvm->mmu_lock); + if (kvm->arch.pgd) { + stage2_unmap_range(kvm, 0UL, stage2_gpa_size); + pgd =3D READ_ONCE(kvm->arch.pgd); + kvm->arch.pgd =3D NULL; + kvm->arch.pgd_phys =3D 0; + } + spin_unlock(&kvm->mmu_lock); + + /* Free the HW pgd, one page at a time */ + if (pgd) + free_pages_exact(pgd, PAGE_SIZE); } =20 void kvm_riscv_stage2_update_hgatp(struct kvm_vcpu *vcpu) { - /* TODO: */ + unsigned long hgatp =3D HGATP_MODE; + struct kvm_arch *k =3D &vcpu->kvm->arch; + + hgatp |=3D (READ_ONCE(k->vmid.vmid) << HGATP_VMID_SHIFT) & + HGATP_VMID_MASK; + hgatp |=3D (k->pgd_phys >> PAGE_SHIFT) & HGATP_PPN; + + csr_write(CSR_HGATP, hgatp); + + if (!kvm_riscv_stage2_vmid_bits()) + __kvm_riscv_hfence_gvma_all(); } --=20 2.17.1