From patchwork Mon Jan 29 17:41:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 10190293 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 98F6960388 for ; Mon, 29 Jan 2018 17:54:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A3B421F61 for ; Mon, 29 Jan 2018 17:54:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7E69F25D99; Mon, 29 Jan 2018 17:54:29 +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.8 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 480E321F61 for ; Mon, 29 Jan 2018 17:54:28 +0000 (UTC) Received: from localhost ([::1]:53717 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1egDdT-0007bN-Ef for patchwork-qemu-devel@patchwork.kernel.org; Mon, 29 Jan 2018 12:54:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49276) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1egDRt-0005xQ-Ig for qemu-devel@nongnu.org; Mon, 29 Jan 2018 12:42:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1egDRp-0003mG-Ge for qemu-devel@nongnu.org; Mon, 29 Jan 2018 12:42:29 -0500 Received: from mail-dm3nam03on0070.outbound.protection.outlook.com ([104.47.41.70]:22464 helo=NAM03-DM3-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1egDRp-0003ko-1Y for qemu-devel@nongnu.org; Mon, 29 Jan 2018 12:42:25 -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=VicULQEp07AxJLBRA0+wcgnbxTeOYvPh2LjOq3uzn1I=; b=kO+jQkgwqgRNj4aAZjU1mMM9x8ZgfDKgCbP3r1jo2TK14sJVsrk74eWSQWq5Xenb/nv0RPxgYcTCYLa9aJVnk1hKLtTzA/7R61GkuimahDFfGgWZDg7HC5drwvktoQjRY0KQ1DMS5TYre/wiDHx/AFFvdoUMU15Mks85UHS9hnk= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; Received: from wsp141597wss.amd.com (165.204.78.1) by DM2PR12MB0154.namprd12.prod.outlook.com (2a01:111:e400:50ce::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.444.14; Mon, 29 Jan 2018 17:42:21 +0000 From: Brijesh Singh To: qemu-devel@nongnu.org Date: Mon, 29 Jan 2018 11:41:31 -0600 Message-Id: <20180129174132.108925-23-brijesh.singh@amd.com> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20180129174132.108925-1-brijesh.singh@amd.com> References: <20180129174132.108925-1-brijesh.singh@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.78.1] X-ClientProxiedBy: BN6PR17CA0018.namprd17.prod.outlook.com (2603:10b6:404:65::28) To DM2PR12MB0154.namprd12.prod.outlook.com (2a01:111:e400:50ce::17) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 028acd86-cac2-4121-3a3d-08d5673fa6e9 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(4534165)(4627221)(201703031133081)(201702281549075)(48565401081)(5600026)(4604075)(2017052603307)(7153060)(7193020); SRVR:DM2PR12MB0154; X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0154; 3:b7GXdzUuA0nHF9a+uqVI/KJg6YRdSmQZlQQRSgK+maQfusKYvsSABYkNl0fVoZan0KU1wnceE4WpdJKomhwK5rUA+ju1coVLMf4ebKhxXssX8V0u9rgrPslGHlkBTxeQdS05nWOqkXZKjZEmjEw4dKY9veE6wWFPAF/33J87YZ4HZ8DkZL2eeLMubQfKrIVGOxcnquEQcVU7PkKftJkbEhNaAa77COG5wPS8F/vTYKeh8RXW73319XA4E8K/VP9U; 25:GsmoNxzXr8PfiRYwC1SH0o/djx3m1AIep2MPgEWJjL2RJR0d2qkkN3kRkNzDQYhcr3ibYq3mfoLHqQzyd/bOhZHrTrdmE1c1VqykcIq2NwrMYKnS6iPq9U9PxEQz16sLX0N8Bac4MP7/7MGsbhzIObSBGeXwaLpoJVDBK/lj2zde4nMYYDSnFQnW9EYINK0f85DKv15H7VQNjQaw42RG89DcntD3mXtB8/62HEJLZNhvHIFYXi4uSEWbYblqTqO1Uxh87GECEB80Z9PypbemME2y89xW3IiLX6wemUtwJgdK3qokM7/dg+JMxnI/yUIqQVZHGBkF4wvwbCeM4QC7zw==; 31:H16lHFN6NYPkEvPRHkvKPEV+s+q+FNMASF9bMN0pKEicpki12VigrqBclXd2pCgjCygHcDCygTP77dV15nsGSyGYgkkaijSxrYyIIdfQhIKWKM0a54TenoQEo+ikshbuQl+cC2AZmxCjMjW+g2r1XQzbxqpO/trKOlAC/d4cBUYfokSV70TX9BQmFDd6hJ1X8nXXbrvgawwdMxRX/TDIzsY5vjUulj0WVIu0fUmkDPA= X-MS-TrafficTypeDiagnostic: DM2PR12MB0154: X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0154; 20:JMDQ8Jjiz5zzRFTcqq7z2bYHAdkIauqBrI4127yxxKteLEOt0homlT3r5GZusm+9/4qc0CNFsgD9QZnTpgOc0NN/grrLyN23RHp7C9S7pUK9pJaOFJF7tOCMOgSDZvErPpk/GU/fYGMLweptgM++12LQDNjYcMprgREPO/jtzhUSim+tcXvW2pJCNcrLXr/HD3sHY87bas0ztPKGC6qnBPx+C8ay8GRpH81ma3omNBaZNIosh595NeZgzeKKkOkX+BV+Q54TlP11JfHLHz25VX7uQUi0zmJ4E29EzqQC3PF2nQh8r5Avla9tRwNnqo0HLyiPTpkZbbyR2DtbvnDFgziIfjbORfSBREXI89Nf3lc1iYUyWMZ3fARv/bdsCBO+MAMQQWFCVI4cSFbEGImwMWqlsYkcM2ed+OSPp3U5WZDL01ItagtafejgtUO8O6iw/6Z6vQPxh02ADp+wqcwRiQI/KbvZc1HtVGGZpItW08rtT/9NAy5JFxPrKgz4wDXF; 4:XovDxsq9cRsMpPVpXIOD/XbUHJ2ELwF4377HXzeqqNoChYTOxhcEphentBFIDpAKPCoh3073HGQSf4l8P8szbqJw58Aj1L66GECgsodgJcSAuwKQsOx+QgKIoIobekUECRH4vlsGc7c2eeHC4k8RvoIlOqRaUa8GaPg7G3OKKPCqonidERp/d6Q/f9XWlzw6t0RCxa+4SFowzZbo/hpgGh44rISX3/okfWnh+myb0DWdVQgiKhsC44rDGAxjoMGHMbHwtWOI9zP0TGCk/oaxlMDzyxaLdXsyKi7Q++0tAY6NpaLgAuPeeV8Eqr/CgWV+ X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040501)(2401047)(8121501046)(5005006)(10201501046)(3231101)(944501161)(93006095)(93001095)(3002001)(6055026)(6041288)(20161123560045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123564045)(6072148)(201708071742011); SRVR:DM2PR12MB0154; BCL:0; PCL:0; RULEID:; SRVR:DM2PR12MB0154; X-Forefront-PRVS: 0567A15835 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(979002)(1496009)(39860400002)(376002)(39380400002)(396003)(366004)(346002)(199004)(189003)(48376002)(8656006)(39060400002)(4326008)(6486002)(68736007)(36756003)(478600001)(26005)(97736004)(16526019)(53936002)(386003)(50466002)(86362001)(25786009)(8666007)(16586007)(54906003)(186003)(47776003)(59450400001)(316002)(7416002)(305945005)(7696005)(105586002)(106356001)(76176011)(51416003)(2906002)(81156014)(66066001)(81166006)(8676002)(2361001)(53416004)(3846002)(50226002)(52116002)(6116002)(2351001)(1076002)(8936002)(7736002)(6666003)(6916009)(5660300001)(2950100002)(969003)(989001)(999001)(1009001)(1019001); DIR:OUT; SFP:1101; SCL:1; SRVR:DM2PR12MB0154; H:wsp141597wss.amd.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; DM2PR12MB0154; 23:OxU8ZIvqptv32eVkg4zEL+Ij1Nzd6CRlTg+fqzGv7?= =?us-ascii?Q?ugSxsdjNjpKXXgCc0EnYM6cG3cWNWWF0LVra9SbNwba13SnlXsE1XuWllBaD?= =?us-ascii?Q?NsJPyz3CYpFJq6EauH9uDElNDOJqyy13bSyWFkqG+H8ZEfBarK81GztzTUOO?= =?us-ascii?Q?cyJjCb/xUCqoSjnKNvgogk7EnF5+BmKNlwF9V0KLW4vbN3kBXYFEftGmFwTF?= =?us-ascii?Q?/AoOEoIIurVwr5Ogzhgsx3lH/B3CbB9d/5IZjPCdFbrfydc/E+tmsCg88bIv?= =?us-ascii?Q?lF55fW+e6cmYx55hSO/zb30MTAAHWwOqvHFqYot3hXoPuBL3jKIP50i5Si42?= =?us-ascii?Q?Ypg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mdmM6SeK+nyOuxb0t4by9yS/qNDcGPC38AldP3F7O3uHcYE44PpBCS36juBwKJ5ofuzYIOrz7e7O7UhACNk0vhvuCJzq3IAhybVJ0/ SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0154; 20:dyyTOn+HlJWnwoRLnqueLNetrpFB8eebOyT5EBTT8NO/2ww0VLxgKuSKUdirzYx/vsu98c1wEFX90PxsByE2/J9NOaT7o2puSxt8cZ5T0jcR+Fh2Qyhaa9X4fplMqu7HgjDu0kGQBic9B5fPD7B6nmeT11+irJKO1jMxW/f+NsoAiV+4DouW0gZkDODIGMKw3vmH/tDSFoYCynwcbN3qox9Pi5aHGBDv2AMEQrsInZvd2CcljygbM3pBCeleP4d6 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Jan 2018 17:42:21.0258 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 028acd86-cac2-4121-3a3d-08d5673fa6e9 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR12MB0154 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.41.70 Subject: [Qemu-devel] [PATCH v6 22/23] target/i386: clear C-bit when walking SEV guest page table X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Edgar E. Iglesias" , Peter Maydell , Eduardo Habkost , kvm@vger.kernel.org, Tom Lendacky , Stefan Hajnoczi , "Michael S. Tsirkin" , Richard Henderson , "Dr. David Alan Gilbert" , Brijesh Singh , Paolo Bonzini , Richard Henderson Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP In SEV-enabled guest the pte entry will have C-bit set, we need to clear the C-bit when walking the page table. The C-bit position should be available in cpuid Fn8000_001f[EBX] Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Signed-off-by: Brijesh Singh --- target/i386/helper.c | 46 +++++++++++++++++++-------- target/i386/monitor.c | 86 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 94 insertions(+), 38 deletions(-) diff --git a/target/i386/helper.c b/target/i386/helper.c index 5dc9e8839bc8..7dbbb9812950 100644 --- a/target/i386/helper.c +++ b/target/i386/helper.c @@ -723,6 +723,22 @@ void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4) } #if !defined(CONFIG_USER_ONLY) +static uint64_t get_me_mask(void) +{ + uint64_t me_mask = 0; + + /* + * When SEV is active, Fn8000_001F[EBX] Bit 0:5 contains the C-bit position + */ + if (kvm_memcrypt_enabled()) { + uint32_t pos; + pos = kvm_arch_get_supported_cpuid(kvm_state, 0x8000001f, 0, R_EBX); + me_mask = (1UL << (pos & 0x3f)); + } + + return ~me_mask; +} + hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) { X86CPU *cpu = X86_CPU(cs); @@ -732,6 +748,9 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) int32_t a20_mask; uint32_t page_offset; int page_size; + uint64_t me_mask; + + me_mask = get_me_mask(); a20_mask = x86_get_a20_mask(env); if (!(env->cr[0] & CR0_PG_MASK)) { @@ -755,25 +774,25 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) } if (la57) { - pml5e_addr = ((env->cr[3] & ~0xfff) + + pml5e_addr = ((env->cr[3] & ~0xfff & me_mask) + (((addr >> 48) & 0x1ff) << 3)) & a20_mask; - pml5e = ldq_phys_debug(cs, pml5e_addr); + pml5e = ldq_phys_debug(cs, pml5e_addr) & me_mask; if (!(pml5e & PG_PRESENT_MASK)) { return -1; } } else { - pml5e = env->cr[3]; + pml5e = env->cr[3] & me_mask; } pml4e_addr = ((pml5e & PG_ADDRESS_MASK) + (((addr >> 39) & 0x1ff) << 3)) & a20_mask; - pml4e = ldq_phys_debug(cs, pml4e_addr); + pml4e = ldq_phys_debug(cs, pml4e_addr) & me_mask; if (!(pml4e & PG_PRESENT_MASK)) { return -1; } pdpe_addr = ((pml4e & PG_ADDRESS_MASK) + (((addr >> 30) & 0x1ff) << 3)) & a20_mask; - pdpe = x86_ldq_phys(cs, pdpe_addr); + pdpe = ldq_phys_debug(cs, pdpe_addr) & me_mask; if (!(pdpe & PG_PRESENT_MASK)) { return -1; } @@ -786,16 +805,16 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) } else #endif { - pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) & - a20_mask; - pdpe = ldq_phys_debug(cs, pdpe_addr); + pdpe_addr = ((env->cr[3] & ~0x1f & me_mask) + ((addr >> 27) & 0x18)) + & a20_mask; + pdpe = ldq_phys_debug(cs, pdpe_addr) & me_mask; if (!(pdpe & PG_PRESENT_MASK)) return -1; } pde_addr = ((pdpe & PG_ADDRESS_MASK) + (((addr >> 21) & 0x1ff) << 3)) & a20_mask; - pde = ldq_phys_debug(cs, pde_addr); + pde = ldq_phys_debug(cs, pde_addr) & me_mask; if (!(pde & PG_PRESENT_MASK)) { return -1; } @@ -808,7 +827,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) pte_addr = ((pde & PG_ADDRESS_MASK) + (((addr >> 12) & 0x1ff) << 3)) & a20_mask; page_size = 4096; - pte = ldq_phys_debug(cs, pte_addr); + pte = ldq_phys_debug(cs, pte_addr) & me_mask; } if (!(pte & PG_PRESENT_MASK)) { return -1; @@ -817,8 +836,9 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) uint32_t pde; /* page directory entry */ - pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & a20_mask; - pde = ldl_phys_debug(cs, pde_addr); + pde_addr = ((env->cr[3] & ~0xfff & me_mask) + ((addr >> 20) & 0xffc)) + & a20_mask; + pde = ldl_phys_debug(cs, pde_addr) & me_mask; if (!(pde & PG_PRESENT_MASK)) return -1; if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { @@ -827,7 +847,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) } else { /* page directory entry */ pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & a20_mask; - pte = ldl_phys_debug(cs, pte_addr); + pte = ldl_phys_debug(cs, pte_addr) & me_mask; if (!(pte & PG_PRESENT_MASK)) { return -1; } diff --git a/target/i386/monitor.c b/target/i386/monitor.c index 96890547f6b4..0b290f004a21 100644 --- a/target/i386/monitor.c +++ b/target/i386/monitor.c @@ -59,6 +59,22 @@ static void print_pte(Monitor *mon, CPUArchState *env, hwaddr addr, pte & PG_RW_MASK ? 'W' : '-'); } +static uint64_t get_me_mask(void) +{ + uint64_t me_mask = 0; + + /* + * When SEV is active, Fn8000_001F[EBX] Bit 0:5 contains the C-bit position + */ + if (kvm_memcrypt_enabled()) { + uint32_t pos; + pos = kvm_arch_get_supported_cpuid(kvm_state, 0x8000001f, 0, R_EBX); + me_mask = (1UL << (pos & 0x3f)); + } + + return ~me_mask; +} + static void tlb_info_32(Monitor *mon, CPUArchState *env) { unsigned int l1, l2; @@ -93,16 +109,20 @@ static void tlb_info_pae32(Monitor *mon, CPUArchState *env) unsigned int l1, l2, l3; uint64_t pdpe, pde, pte; uint64_t pdp_addr, pd_addr, pt_addr; + uint64_t me_mask; + + me_mask = get_me_mask(); pdp_addr = env->cr[3] & ~0x1f; + pdp_addr &= me_mask; for (l1 = 0; l1 < 4; l1++) { cpu_physical_memory_read_debug(pdp_addr + l1 * 8, &pdpe, 8); - pdpe = le64_to_cpu(pdpe); + pdpe = le64_to_cpu(pdpe & me_mask); if (pdpe & PG_PRESENT_MASK) { pd_addr = pdpe & 0x3fffffffff000ULL; for (l2 = 0; l2 < 512; l2++) { cpu_physical_memory_read_debug(pd_addr + l2 * 8, &pde, 8); - pde = le64_to_cpu(pde); + pde = le64_to_cpu(pde & me_mask); if (pde & PG_PRESENT_MASK) { if (pde & PG_PSE_MASK) { /* 2M pages with PAE, CR4.PSE is ignored */ @@ -113,7 +133,7 @@ static void tlb_info_pae32(Monitor *mon, CPUArchState *env) for (l3 = 0; l3 < 512; l3++) { cpu_physical_memory_read_debug(pt_addr + l3 * 8, &pte, 8); - pte = le64_to_cpu(pte); + pte = le64_to_cpu(pte & me_mask); if (pte & PG_PRESENT_MASK) { print_pte(mon, env, (l1 << 30) + (l2 << 21) + (l3 << 12), @@ -135,10 +155,13 @@ static void tlb_info_la48(Monitor *mon, CPUArchState *env, uint64_t l1, l2, l3, l4; uint64_t pml4e, pdpe, pde, pte; uint64_t pdp_addr, pd_addr, pt_addr; + uint64_t me_mask; + + me_mask = get_me_mask(); for (l1 = 0; l1 < 512; l1++) { cpu_physical_memory_read_debug(pml4_addr + l1 * 8, &pml4e, 8); - pml4e = le64_to_cpu(pml4e); + pml4e = le64_to_cpu(pml4e & me_mask); if (!(pml4e & PG_PRESENT_MASK)) { continue; } @@ -146,7 +169,7 @@ static void tlb_info_la48(Monitor *mon, CPUArchState *env, pdp_addr = pml4e & 0x3fffffffff000ULL; for (l2 = 0; l2 < 512; l2++) { cpu_physical_memory_read_debug(pdp_addr + l2 * 8, &pdpe, 8); - pdpe = le64_to_cpu(pdpe); + pdpe = le64_to_cpu(pdpe & me_mask); if (!(pdpe & PG_PRESENT_MASK)) { continue; } @@ -161,7 +184,7 @@ static void tlb_info_la48(Monitor *mon, CPUArchState *env, pd_addr = pdpe & 0x3fffffffff000ULL; for (l3 = 0; l3 < 512; l3++) { cpu_physical_memory_read_debug(pd_addr + l3 * 8, &pde, 8); - pde = le64_to_cpu(pde); + pde = le64_to_cpu(pde & me_mask); if (!(pde & PG_PRESENT_MASK)) { continue; } @@ -176,7 +199,7 @@ static void tlb_info_la48(Monitor *mon, CPUArchState *env, pt_addr = pde & 0x3fffffffff000ULL; for (l4 = 0; l4 < 512; l4++) { cpu_physical_memory_read_debug(pt_addr + l4 * 8, &pte, 8); - pte = le64_to_cpu(pte); + pte = le64_to_cpu(pte & me_mask); if (pte & PG_PRESENT_MASK) { print_pte(mon, env, (l0 << 48) + (l1 << 39) + (l2 << 30) + (l3 << 21) + (l4 << 12), @@ -193,11 +216,14 @@ static void tlb_info_la57(Monitor *mon, CPUArchState *env) uint64_t l0; uint64_t pml5e; uint64_t pml5_addr; + uint64_t me_mask; + + me_mask = get_me_mask(); - pml5_addr = env->cr[3] & 0x3fffffffff000ULL; + pml5_addr = env->cr[3] & 0x3fffffffff000ULL & me_mask; for (l0 = 0; l0 < 512; l0++) { cpu_physical_memory_read_debug(pml5_addr + l0 * 8, &pml5e, 8); - pml5e = le64_to_cpu(pml5e); + pml5e = le64_to_cpu(pml5e & me_mask); if (pml5e & PG_PRESENT_MASK) { tlb_info_la48(mon, env, l0, pml5e & 0x3fffffffff000ULL); } @@ -225,7 +251,8 @@ void hmp_info_tlb(Monitor *mon, const QDict *qdict) if (env->cr[4] & CR4_LA57_MASK) { tlb_info_la57(mon, env); } else { - tlb_info_la48(mon, env, 0, env->cr[3] & 0x3fffffffff000ULL); + tlb_info_la48(mon, env, 0, env->cr[3] & 0x3fffffffff000ULL & + get_me_mask()); } } else #endif @@ -309,19 +336,22 @@ static void mem_info_pae32(Monitor *mon, CPUArchState *env) uint64_t pdpe, pde, pte; uint64_t pdp_addr, pd_addr, pt_addr; hwaddr start, end; + uint64_t me_mask; - pdp_addr = env->cr[3] & ~0x1f; + me_mask = get_me_mask(); + + pdp_addr = env->cr[3] & ~0x1f & me_mask; last_prot = 0; start = -1; for (l1 = 0; l1 < 4; l1++) { cpu_physical_memory_read_debug(pdp_addr + l1 * 8, &pdpe, 8); - pdpe = le64_to_cpu(pdpe); + pdpe = le64_to_cpu(pdpe & me_mask); end = l1 << 30; if (pdpe & PG_PRESENT_MASK) { pd_addr = pdpe & 0x3fffffffff000ULL; for (l2 = 0; l2 < 512; l2++) { cpu_physical_memory_read_debug(pd_addr + l2 * 8, &pde, 8); - pde = le64_to_cpu(pde); + pde = le64_to_cpu(pde & me_mask); end = (l1 << 30) + (l2 << 21); if (pde & PG_PRESENT_MASK) { if (pde & PG_PSE_MASK) { @@ -333,7 +363,7 @@ static void mem_info_pae32(Monitor *mon, CPUArchState *env) for (l3 = 0; l3 < 512; l3++) { cpu_physical_memory_read_debug(pt_addr + l3 * 8, &pte, 8); - pte = le64_to_cpu(pte); + pte = le64_to_cpu(pte & me_mask); end = (l1 << 30) + (l2 << 21) + (l3 << 12); if (pte & PG_PRESENT_MASK) { prot = pte & pde & (PG_USER_MASK | PG_RW_MASK | @@ -366,19 +396,22 @@ static void mem_info_la48(Monitor *mon, CPUArchState *env) uint64_t l1, l2, l3, l4; uint64_t pml4e, pdpe, pde, pte; uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end; + uint64_t me_mask; + + me_mask = get_me_mask(); - pml4_addr = env->cr[3] & 0x3fffffffff000ULL; + pml4_addr = env->cr[3] & 0x3fffffffff000ULL & me_mask; last_prot = 0; start = -1; for (l1 = 0; l1 < 512; l1++) { cpu_physical_memory_read_debug(pml4_addr + l1 * 8, &pml4e, 8); - pml4e = le64_to_cpu(pml4e); + pml4e = le64_to_cpu(pml4e & me_mask); end = l1 << 39; if (pml4e & PG_PRESENT_MASK) { pdp_addr = pml4e & 0x3fffffffff000ULL; for (l2 = 0; l2 < 512; l2++) { cpu_physical_memory_read_debug(pdp_addr + l2 * 8, &pdpe, 8); - pdpe = le64_to_cpu(pdpe); + pdpe = le64_to_cpu(pdpe & me_mask); end = (l1 << 39) + (l2 << 30); if (pdpe & PG_PRESENT_MASK) { if (pdpe & PG_PSE_MASK) { @@ -391,7 +424,7 @@ static void mem_info_la48(Monitor *mon, CPUArchState *env) for (l3 = 0; l3 < 512; l3++) { cpu_physical_memory_read_debug(pd_addr + l3 * 8, &pde, 8); - pde = le64_to_cpu(pde); + pde = le64_to_cpu(pde & me_mask); end = (l1 << 39) + (l2 << 30) + (l3 << 21); if (pde & PG_PRESENT_MASK) { if (pde & PG_PSE_MASK) { @@ -405,7 +438,7 @@ static void mem_info_la48(Monitor *mon, CPUArchState *env) cpu_physical_memory_read_debug(pt_addr + l4 * 8, &pte, 8); - pte = le64_to_cpu(pte); + pte = le64_to_cpu(pte & me_mask); end = (l1 << 39) + (l2 << 30) + (l3 << 21) + (l4 << 12); if (pte & PG_PRESENT_MASK) { @@ -444,13 +477,16 @@ static void mem_info_la57(Monitor *mon, CPUArchState *env) uint64_t l0, l1, l2, l3, l4; uint64_t pml5e, pml4e, pdpe, pde, pte; uint64_t pml5_addr, pml4_addr, pdp_addr, pd_addr, pt_addr, start, end; + uint64_t me_mask; + + me_mask = get_me_mask(); - pml5_addr = env->cr[3] & 0x3fffffffff000ULL; + pml5_addr = env->cr[3] & 0x3fffffffff000ULL & me_mask; last_prot = 0; start = -1; for (l0 = 0; l0 < 512; l0++) { cpu_physical_memory_read_debug(pml5_addr + l0 * 8, &pml5e, 8); - pml5e = le64_to_cpu(pml5e); + pml5e = le64_to_cpu(pml5e & me_mask); end = l0 << 48; if (!(pml5e & PG_PRESENT_MASK)) { prot = 0; @@ -461,7 +497,7 @@ static void mem_info_la57(Monitor *mon, CPUArchState *env) pml4_addr = pml5e & 0x3fffffffff000ULL; for (l1 = 0; l1 < 512; l1++) { cpu_physical_memory_read_debug(pml4_addr + l1 * 8, &pml4e, 8); - pml4e = le64_to_cpu(pml4e); + pml4e = le64_to_cpu(pml4e & me_mask); end = (l0 << 48) + (l1 << 39); if (!(pml4e & PG_PRESENT_MASK)) { prot = 0; @@ -472,7 +508,7 @@ static void mem_info_la57(Monitor *mon, CPUArchState *env) pdp_addr = pml4e & 0x3fffffffff000ULL; for (l2 = 0; l2 < 512; l2++) { cpu_physical_memory_read_debug(pdp_addr + l2 * 8, &pdpe, 8); - pdpe = le64_to_cpu(pdpe); + pdpe = le64_to_cpu(pdpe & me_mask); end = (l0 << 48) + (l1 << 39) + (l2 << 30); if (pdpe & PG_PRESENT_MASK) { prot = 0; @@ -491,7 +527,7 @@ static void mem_info_la57(Monitor *mon, CPUArchState *env) pd_addr = pdpe & 0x3fffffffff000ULL; for (l3 = 0; l3 < 512; l3++) { cpu_physical_memory_read_debug(pd_addr + l3 * 8, &pde, 8); - pde = le64_to_cpu(pde); + pde = le64_to_cpu(pde & me_mask); end = (l0 << 48) + (l1 << 39) + (l2 << 30) + (l3 << 21); if (pde & PG_PRESENT_MASK) { prot = 0; @@ -510,7 +546,7 @@ static void mem_info_la57(Monitor *mon, CPUArchState *env) pt_addr = pde & 0x3fffffffff000ULL; for (l4 = 0; l4 < 512; l4++) { cpu_physical_memory_read_debug(pt_addr + l4 * 8, &pte, 8); - pte = le64_to_cpu(pte); + pte = le64_to_cpu(pte & me_mask); end = (l0 << 48) + (l1 << 39) + (l2 << 30) + (l3 << 21) + (l4 << 12); if (pte & PG_PRESENT_MASK) {