From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3608DCCA471 for ; Fri, 3 Oct 2025 17:10:08 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v4jHF-0001fq-Em; Fri, 03 Oct 2025 13:09:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v4jHB-0001f8-TP for qemu-devel@nongnu.org; Fri, 03 Oct 2025 13:09:01 -0400 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1v4jGU-0006FK-23 for qemu-devel@nongnu.org; Fri, 03 Oct 2025 13:09:01 -0400 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-27eceb38eb1so24687615ad.3 for ; Fri, 03 Oct 2025 10:08:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1759511290; x=1760116090; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QXrhoVWiP6521Zwr48CBNkcUwHTaC1HLv7JHbARSmq8=; b=TGi8/xIU5GlrywxWxHkAWjYb7hJLvtsIZlQ1a6bkPhPcao6pG/yCNPaQ6CHvVkoVDt ttWmHUXkBIK++su+xNK9jZg1RnXtxnXFXlj2Si9AGs60HbHF1LHYm6KftQ79Vu/IMCHP bIgESIaxCok0RgJ1puBN0op82tlPSscJ7g5iNDuYsXCHrnlgE4Pvkd/vxq0Od81v0z8I 0ryF1gEOsMQD27zOwy+je1hySyb15nfNJOE5QmED/EzVqhhb4DyDVEfp7GUSR1EwDVnC fSJG8XyUCPDYdTbDQtP9W0vUxeYMEDIqyd8lIN0KFcxLmni0Zjklts+1LJED0JgfrOIi 96dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759511290; x=1760116090; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QXrhoVWiP6521Zwr48CBNkcUwHTaC1HLv7JHbARSmq8=; b=MCbGeDXxnBN8ebNciaNJTBUSfFvM6iADX1jVDaWXuevq/25YzY/1EV5ntceWyySj/r 8FMGpHGNsJLMbeKJqzVRmvIXXjJ787ZlktNc1IUHBW6PvPMCCzQ+kjfbD3GenDa4deQ0 MEaYIQjZmKYfa+vGOCPY50C4jiSDH956P4sW/ongn7Xpmoyli/GK7SbWwxkJVbukR7Hp qr2bgdQa60MsN2LWkUYwZfqFkrBMEPREyQzWv31LETxo749axgYwSLCvR4yVB4vHQjuw 23fMaqQTy07B2XHwjVt3MNIMl63cSU+DLA9hjs9T4+QPmQYBZICr0n0SEeOvXcbF8ClO rmHg== X-Gm-Message-State: AOJu0Yxt+C4+jmPpsi4uoDnMr0Odm6Fa7LV0iktFVGK8YzBiMQz3Ds43 c+LY937XVZgI6RXend1bVQLkPDl9U/vqwmTRrIxg+zmcbvoyU0lqc8pK66ivJsctbgEeywK24Dv XtbB0ez0= X-Gm-Gg: ASbGnctC1ZKEq/d7nn1XJTSfApg1nPW4ahVYaeppJdH4xyZaN1O1feB4T/c2qQ7zPxH 3exWoPfET24XTb3LM36TCpASoa2LBji/BmQbhIjlD4ufAq8b5RuodqlmG15VzhzaCDhBLhyIUO9 Sna2w2ck1FjyxMVvNngxQTE/USRL3K6MLzoCJq9Yv1QgT9oFsZ/j2+iB70OZdxk2sHQxJdn7u+U +DvxS2ytG5RsoQE089KFcep1bfxoGxgWbjXTp2uSbir3xjo/Dx3eL1u9ZQYciaZ80cvIMfx8WlR 5ZM4o6YJ1E+y0vYdIZgvzXVXG0JiovjQw9W2Ffsi3pAKZUp9gsLse5rlYmb7ddanBXnVP4W5LQm +0B7V147XN0KnNp4BCEnEfew+MzsGY2YWWCLahSgW8SZ4uUm6d2ME55yd X-Google-Smtp-Source: AGHT+IGApyDNKwzoOPMCBTZFkEHGYyh3Q26Gz67aTD3prDJSuonO0By5yT+afbqHvVuuQAH7fIm10Q== X-Received: by 2002:a17:903:1b6f:b0:240:3b9e:dd65 with SMTP id d9443c01a7336-28e9a5fa9d5mr46617655ad.38.1759511289948; Fri, 03 Oct 2025 10:08:09 -0700 (PDT) Received: from stoup.. ([71.212.157.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-28e8d110d86sm55327115ad.6.2025.10.03.10.08.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Oct 2025 10:08:09 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, Pierrick Bouvier Subject: [PATCH v6 09/73] target/arm: Implement get_S2prot_indirect Date: Fri, 3 Oct 2025 10:06:56 -0700 Message-ID: <20251003170800.997167-10-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251003170800.997167-1-richard.henderson@linaro.org> References: <20251003170800.997167-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::62e; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Move the stage2 permissions for normal accesses to GetPhysAddrResult.s2prot. Put the stage2 permissions for page table walking in CPUTLBEntryFull.prot. This allows the permission checks in S1_ptw_translate and arm_casq_ptw to see the right permission. Reviewed-by: Pierrick Bouvier Signed-off-by: Richard Henderson --- target/arm/internals.h | 7 ++++ target/arm/ptw.c | 81 +++++++++++++++++++++++++++++++----------- 2 files changed, 68 insertions(+), 20 deletions(-) diff --git a/target/arm/internals.h b/target/arm/internals.h index face1019f5..22947c4b78 100644 --- a/target/arm/internals.h +++ b/target/arm/internals.h @@ -1577,6 +1577,13 @@ typedef struct ARMCacheAttrs { typedef struct GetPhysAddrResult { CPUTLBEntryFull f; ARMCacheAttrs cacheattrs; + /* + * For ARMMMUIdx_Stage2*, the protection installed into f.prot + * is the result for AccessType_TTW, i.e. the page table walk itself. + * The protection installed info s2prot is the one to be merged + * with the stage1 protection. + */ + int s2prot; } GetPhysAddrResult; /** diff --git a/target/arm/ptw.c b/target/arm/ptw.c index 4fb4436db1..6dcd3adbdf 100644 --- a/target/arm/ptw.c +++ b/target/arm/ptw.c @@ -1280,7 +1280,7 @@ do_fault: * @xn: XN (execute-never) bits * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0 */ -static int get_S2prot_noexecute(int s2ap) +static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0) { int prot = 0; @@ -1290,12 +1290,6 @@ static int get_S2prot_noexecute(int s2ap) if (s2ap & 2) { prot |= PAGE_WRITE; } - return prot; -} - -static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0) -{ - int prot = get_S2prot_noexecute(s2ap); if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) { switch (xn) { @@ -1327,6 +1321,44 @@ static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0) return prot; } +static int get_S2prot_indirect(CPUARMState *env, GetPhysAddrResult *result, + int pi_index, int po_index, bool s1_is_el0) +{ + /* Last index is (priv, unpriv, ttw) */ + static const uint8_t perm_table[16][3] = { + /* 0 */ { 0, 0, 0 }, /* no access */ + /* 1 */ { 0, 0, 0 }, /* reserved */ + /* 2 */ { PAGE_READ, PAGE_READ, PAGE_READ | PAGE_WRITE }, + /* 3 */ { PAGE_READ, PAGE_READ, PAGE_READ | PAGE_WRITE }, + /* 4 */ { PAGE_WRITE, PAGE_WRITE, 0 }, + /* 5 */ { 0, 0, 0 }, /* reserved */ + /* 6 */ { PAGE_READ, PAGE_READ, PAGE_READ | PAGE_WRITE }, + /* 7 */ { PAGE_READ, PAGE_READ, PAGE_READ | PAGE_WRITE }, + /* 8 */ { PAGE_READ, PAGE_READ, PAGE_READ }, + /* 9 */ { PAGE_READ, PAGE_READ | PAGE_EXEC, PAGE_READ }, + /* A */ { PAGE_READ | PAGE_EXEC, PAGE_READ, PAGE_READ }, + /* B */ { PAGE_READ | PAGE_EXEC, PAGE_READ | PAGE_EXEC, PAGE_READ }, + /* C */ { PAGE_READ | PAGE_WRITE, + PAGE_READ | PAGE_WRITE, + PAGE_READ | PAGE_WRITE }, + /* D */ { PAGE_READ | PAGE_WRITE, + PAGE_READ | PAGE_WRITE | PAGE_EXEC, + PAGE_READ | PAGE_WRITE }, + /* E */ { PAGE_READ | PAGE_WRITE | PAGE_EXEC, + PAGE_READ | PAGE_WRITE, + PAGE_READ | PAGE_WRITE }, + /* F */ { PAGE_READ | PAGE_WRITE | PAGE_EXEC, + PAGE_READ | PAGE_WRITE | PAGE_EXEC, + PAGE_READ | PAGE_WRITE }, + }; + + uint64_t pir = (env->cp15.scr_el3 & SCR_PIEN ? env->cp15.s2pir_el2 : 0); + int s2pi = extract64(pir, pi_index * 4, 4); + + result->f.prot = perm_table[s2pi][2]; + return perm_table[s2pi][s1_is_el0]; +} + /* * Translate section/page access permissions to protection flags * @env: CPUARMState @@ -1777,7 +1809,7 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw, int32_t stride; int addrsize, inputsize, outputsize; uint64_t tcr = regime_tcr(env, mmu_idx); - int ap; + int ap, prot; uint32_t el = regime_el(env, mmu_idx); uint64_t descaddrmask; bool aarch64 = arm_el_is_aa64(env, el); @@ -2101,6 +2133,18 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw, ap = extract32(attrs, 6, 2); out_space = ptw->in_space; if (regime_is_stage2(mmu_idx)) { + if (param.pie) { + int pi = extract64(attrs, 6, 1) + | (extract64(attrs, 51, 1) << 1) + | (extract64(attrs, 53, 2) << 2); + int po = extract64(attrs, 60, 3); + prot = get_S2prot_indirect(env, result, pi, po, ptw->in_s1_is_el0); + } else { + int xn = extract64(attrs, 53, 2); + prot = get_S2prot(env, ap, xn, ptw->in_s1_is_el0); + /* Install TTW permissions in f.prot. */ + result->f.prot = prot & (PAGE_READ | PAGE_WRITE); + } /* * R_GYNXY: For stage2 in Realm security state, bit 55 is NS. * The bit remains ignored for other security states. @@ -2109,11 +2153,9 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw, */ if (out_space == ARMSS_Realm && extract64(attrs, 55, 1)) { out_space = ARMSS_NonSecure; - result->f.prot = get_S2prot_noexecute(ap); - } else { - int xn = extract64(attrs, 53, 2); - result->f.prot = get_S2prot(env, ap, xn, ptw->in_s1_is_el0); + prot &= ~PAGE_EXEC; } + result->s2prot = prot; result->cacheattrs.is_s2_format = true; result->cacheattrs.attrs = extract32(attrs, 2, 4); @@ -2185,9 +2227,8 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw, * Note that we modified ptw->in_space earlier for NSTable, but * result->f.attrs retains a copy of the original security space. */ - result->f.prot = get_S1prot_indirect(env, ptw, mmu_idx, pi, po, - result->f.attrs.space, - out_space); + prot = get_S1prot_indirect(env, ptw, mmu_idx, pi, po, + result->f.attrs.space, out_space); } else { int xn = extract64(attrs, 54, 1); int pxn = extract64(attrs, 53, 1); @@ -2216,10 +2257,10 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw, * Note that we modified ptw->in_space earlier for NSTable, but * result->f.attrs retains a copy of the original security space. */ - result->f.prot = get_S1prot(env, mmu_idx, aarch64, - user_rw, prot_rw, xn, pxn, - result->f.attrs.space, out_space); + prot = get_S1prot(env, mmu_idx, aarch64, user_rw, prot_rw, xn, pxn, + result->f.attrs.space, out_space); } + result->f.prot = prot; /* Index into MAIR registers for cache attributes */ attrindx = extract32(attrs, 2, 3); @@ -2265,7 +2306,7 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw, result->f.tlb_fill_flags = 0; } - if (ptw->in_prot_check & ~result->f.prot) { + if (ptw->in_prot_check & ~prot) { fi->type = ARMFault_Permission; goto do_fault; } @@ -3463,7 +3504,7 @@ static bool get_phys_addr_twostage(CPUARMState *env, S1Translate *ptw, fi->s2addr = ipa; /* Combine the S1 and S2 perms. */ - result->f.prot &= s1_prot; + result->f.prot = s1_prot & result->s2prot; /* If S2 fails, return early. */ if (ret) { -- 2.43.0