From patchwork Thu Jan 11 11:04:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 761833 Delivered-To: patch@linaro.org Received: by 2002:a5d:6e02:0:b0:337:62d3:c6d5 with SMTP id h2csp2338303wrz; Thu, 11 Jan 2024 03:12:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IHbxThd1LfohwRjGN1gWOiG9bTbbFydQ1yzhc3dUmPJVNhQkWFYo5BweqvY4PDSu/1BcVZC X-Received: by 2002:ad4:4ea4:0:b0:67f:6ac:4bb8 with SMTP id ed4-20020ad44ea4000000b0067f06ac4bb8mr1106300qvb.14.1704971552693; Thu, 11 Jan 2024 03:12:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704971552; cv=none; d=google.com; s=arc-20160816; b=moRSYocnlOEcbCA28WtjV5CLm2umtdxAZoLGjPlXxols7GNTUsOcL1KU+/s0ObBazg 67OudEUMuVsxKz2gXemM9z0TsvvYO6g9PonLXDrOnT0qUBg5rWOUTiPIbe0iQwF3LBP7 EiKL6iRonO1vFK9wH6v8XC42UESYr8c5fCeM88tBAln3d57dMJ4DEiyHz77ubL1v/JgL TNJO0J059OfkQOmkGxybng5HWMwf5DC345I7rK2xM9kNLcWPTHoo1fHeh4zZjDfd3ahc 7iLAQYgNCLhECCE1uaEIv88zFw8UGufW/PWLX1Xv7BVDPb8Aa55J3+sSqPxES+FiBNj4 1lHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=ImpU1WCMbgzrW+ilWcVhrg2deIUxWHoXCQYEMwhrng4=; fh=PnYt+qEB9tAfMKoqBm2xjKOFpYyFFGPudh5cVIoieJM=; b=s6VSF2UKcpKRj2avJw/B+4+z2hBViKWqYiNItQunJUyEoofqZ2upJDhSxCZYa4zhHz 9HscPiFhEI59gArM2+YywC8g+PkSkKKU52ocYMBVWVlzv+td0jdbS9yGYl2u7U7GSyF1 rpAatrLzeBku2n4lisFgwQXIXqQtcRp4T9mxmSkjavFWB8ts/dkvoyVvaIDwA4N+08yQ 6Bj04PXc387W9RY2qrPyDhcXIf+8N92IpySCH+tcWTr90yB6mbVtfLub/AkxGdMQpkD+ RvHI8SNxlNMjW4SWQ2VQgcim8QeQN+eupvCysLBCW+NQAJ8sgp9aoo/c78REHYMBupsr DHVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wLutpyZB; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d19-20020a0caa13000000b006802faeff70si566396qvb.545.2024.01.11.03.12.32 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 11 Jan 2024 03:12:32 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wLutpyZB; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rNssj-0001gW-JN; Thu, 11 Jan 2024 06:05:53 -0500 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 1rNssL-0001PN-Cd for qemu-devel@nongnu.org; Thu, 11 Jan 2024 06:05:31 -0500 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rNssH-0004Ve-Gu for qemu-devel@nongnu.org; Thu, 11 Jan 2024 06:05:29 -0500 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-3377d45c178so1040446f8f.2 for ; Thu, 11 Jan 2024 03:05:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1704971121; x=1705575921; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=ImpU1WCMbgzrW+ilWcVhrg2deIUxWHoXCQYEMwhrng4=; b=wLutpyZBbNjMIQxPd38CenkgXl5EXOcbRW0rmnzyhAdF9amJTKWBakU001BFKK3A46 MYfPLTPonhx62IYrO5XpRXc/Dvl0EYBX8wVToZWJu64gazIbzlQNJFkG2UOCuJ7ArNtj vvN4QmfA0dFLJmWkLBAJEaWdNWkiQsdUMeMKGpvbQlznmQP9iCJDDIWGTvoijUrL7cWr QXuTUMQy0zRdE9gsR+D7eDNluuRHxL/bf7EmOY4DlK/GN9lPrj61gVKDU4TlFvkGaZsZ Be/IO8AQqD71fT+nAsjiviI1zO7MjQrA8Uo5y4gZa7CQipnldXTnL486DCDJQEG+qAwE DC9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704971121; x=1705575921; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ImpU1WCMbgzrW+ilWcVhrg2deIUxWHoXCQYEMwhrng4=; b=YfaTqYlX1enkLBDM6CQ8KRxbn3IDXX5kYXHSKtfa15XgmHYHBLnX4lJ5wTSCY8YNjf j8jeR2I7B8KoaDG/IKX0a6Gw1TS1SCH228srcw/O1eCs8Rjnl8S5I5v0tsGH8VObwk/L ZuwfOls915BHassF3nnf4mQElQQY3fExw5h/hQQUdhosLMSE3WJMUSQ7YXwFVBx8VewS WD0JHrqUJu6J1UNsv++tfkm5C0eHA66Xv/LjMppcRTKyXWYmdLYJzUtH5xUFaBMesOC0 V4URCMRmm77BCcDHcltcAQCpgoh7qT31212IvhnR61+5S+U64YlbVAh28nncwMuCHeh4 pl2g== X-Gm-Message-State: AOJu0YwI2QCZaIaPQ7vU6bBMp9WFjLpz9J4usmtuYOGUEuHwjtVnAY1I pQJDpdpmIDSBPiuZ2zR+B+yzUOLVEa0KKQJASuaTrv1OApc= X-Received: by 2002:adf:fd0e:0:b0:336:5964:ba7d with SMTP id e14-20020adffd0e000000b003365964ba7dmr565531wrr.85.1704971121208; Thu, 11 Jan 2024 03:05:21 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id v30-20020adf8b5e000000b0033690139ea5sm951323wra.44.2024.01.11.03.05.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 03:05:20 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 32/41] target/arm: Implement FEAT_NV2 redirection of sysregs to RAM Date: Thu, 11 Jan 2024 11:04:56 +0000 Message-Id: <20240111110505.1563291-33-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240111110505.1563291-1-peter.maydell@linaro.org> References: <20240111110505.1563291-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x429.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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org FEAT_NV2 requires that when HCR_EL2.{NV,NV2} == 0b11 then accesses by EL1 to certain system registers are redirected to RAM. The full list of affected registers is in the table in rule R_CSRPQ in the Arm ARM. The registers may be normally accessible at EL1 (like ACTLR_EL1), or normally UNDEF at EL1 (like HCR_EL2). Some registers redirect to RAM only when HCR_EL2.NV1 is 0, and some only when HCR_EL2.NV1 is 1; others trap in both cases. Add the infrastructure for identifying which registers should be redirected and turning them into memory accesses. This code does not set the correct syndrome or arrange for the exception to be taken to the correct target EL if the access via VNCR_EL2 faults; we will do that in the next commit. Subsequent commits will mark up the relevant regdefs to set their nv2_redirect_offset, and if relevant one of the two flags which indicates that the redirect happens only for a particular value of HCR_EL2.NV1. Signed-off-by: Peter Maydell Tested-by: Miguel Luis Reviewed-by: Richard Henderson --- target/arm/cpregs.h | 12 ++++++++ target/arm/cpu.h | 4 +++ target/arm/tcg/translate.h | 6 ++++ target/arm/tcg/hflags.c | 6 ++++ target/arm/tcg/translate-a64.c | 56 ++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+) diff --git a/target/arm/cpregs.h b/target/arm/cpregs.h index cb795bed75b..b6fdd0f3eb4 100644 --- a/target/arm/cpregs.h +++ b/target/arm/cpregs.h @@ -826,6 +826,11 @@ typedef void CPResetFn(CPUARMState *env, const ARMCPRegInfo *opaque); #define CP_ANY 0xff +/* Flags in the high bits of nv2_redirect_offset */ +#define NV2_REDIR_NV1 0x4000 /* Only redirect when HCR_EL2.NV1 == 1 */ +#define NV2_REDIR_NO_NV1 0x8000 /* Only redirect when HCR_EL2.NV1 == 0 */ +#define NV2_REDIR_FLAG_MASK 0xc000 + /* Definition of an ARM coprocessor register */ struct ARMCPRegInfo { /* Name of register (useful mainly for debugging, need not be unique) */ @@ -867,6 +872,13 @@ struct ARMCPRegInfo { * value encodes both the trap register and bit within it. */ FGTBit fgt; + + /* + * Offset from VNCR_EL2 when FEAT_NV2 redirects access to memory; + * may include an NV2_REDIR_* flag. + */ + uint32_t nv2_redirect_offset; + /* * The opaque pointer passed to define_arm_cp_regs_with_opaque() when * this register was defined: can be used to hand data through to the diff --git a/target/arm/cpu.h b/target/arm/cpu.h index f521219ea95..9281d74aa9d 100644 --- a/target/arm/cpu.h +++ b/target/arm/cpu.h @@ -3241,6 +3241,10 @@ FIELD(TBFLAG_A64, ATA0, 31, 1) FIELD(TBFLAG_A64, NV, 32, 1) FIELD(TBFLAG_A64, NV1, 33, 1) FIELD(TBFLAG_A64, NV2, 34, 1) +/* Set if FEAT_NV2 RAM accesses use the EL2&0 translation regime */ +FIELD(TBFLAG_A64, NV2_MEM_E20, 35, 1) +/* Set if FEAT_NV2 RAM accesses are big-endian */ +FIELD(TBFLAG_A64, NV2_MEM_BE, 36, 1) /* * Helpers for using the above. Note that only the A64 accessors use diff --git a/target/arm/tcg/translate.h b/target/arm/tcg/translate.h index 9e13c4ef7b6..93be745cf33 100644 --- a/target/arm/tcg/translate.h +++ b/target/arm/tcg/translate.h @@ -150,6 +150,10 @@ typedef struct DisasContext { bool nv1; /* True if NV enabled and HCR_EL2.NV2 is set */ bool nv2; + /* True if NV2 enabled and NV2 RAM accesses use EL2&0 translation regime */ + bool nv2_mem_e20; + /* True if NV2 enabled and NV2 RAM accesses are big-endian */ + bool nv2_mem_be; /* * >= 0, a copy of PSTATE.BTYPE, which will be 0 without v8.5-BTI. * < 0, set by the current instruction. @@ -165,6 +169,8 @@ typedef struct DisasContext { int c15_cpar; /* TCG op of the current insn_start. */ TCGOp *insn_start; + /* Offset from VNCR_EL2 when FEAT_NV2 redirects this reg to memory */ + uint32_t nv2_redirect_offset; } DisasContext; typedef struct DisasCompare { diff --git a/target/arm/tcg/hflags.c b/target/arm/tcg/hflags.c index d2b352663e8..8e5d35d9227 100644 --- a/target/arm/tcg/hflags.c +++ b/target/arm/tcg/hflags.c @@ -307,6 +307,12 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el, } if (hcr & HCR_NV2) { DP_TBFLAG_A64(flags, NV2, 1); + if (hcr & HCR_E2H) { + DP_TBFLAG_A64(flags, NV2_MEM_E20, 1); + } + if (env->cp15.sctlr_el[2] & SCTLR_EE) { + DP_TBFLAG_A64(flags, NV2_MEM_BE, 1); + } } } diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c index 2ada5b7e3f6..2938397d52c 100644 --- a/target/arm/tcg/translate-a64.c +++ b/target/arm/tcg/translate-a64.c @@ -2135,6 +2135,7 @@ static void handle_sys(DisasContext *s, bool isread, bool nv_trap_to_el2 = false; bool nv_redirect_reg = false; bool skip_fp_access_checks = false; + bool nv2_mem_redirect = false; TCGv_ptr tcg_ri = NULL; TCGv_i64 tcg_rt; uint32_t syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread); @@ -2167,6 +2168,21 @@ static void handle_sys(DisasContext *s, bool isread, return; } + if (s->nv2 && ri->nv2_redirect_offset) { + /* + * Some registers always redirect to memory; some only do so if + * HCR_EL2.NV1 is 0, and some only if NV1 is 1 (these come in + * pairs which share an offset; see the table in R_CSRPQ). + */ + if (ri->nv2_redirect_offset & NV2_REDIR_NV1) { + nv2_mem_redirect = s->nv1; + } else if (ri->nv2_redirect_offset & NV2_REDIR_NO_NV1) { + nv2_mem_redirect = !s->nv1; + } else { + nv2_mem_redirect = true; + } + } + /* Check access permissions */ if (!cp_access_ok(s->current_el, ri, isread)) { /* @@ -2182,6 +2198,12 @@ static void handle_sys(DisasContext *s, bool isread, * the EL2 register's accessfn. */ nv_redirect_reg = true; + assert(!nv2_mem_redirect); + } else if (nv2_mem_redirect) { + /* + * NV2 redirect-to-memory takes precedence over trap to EL2 or + * UNDEF to EL1. + */ } else if (s->nv && arm_cpreg_traps_in_nv(ri)) { /* * This register / instruction exists and is an EL2 register, so @@ -2255,6 +2277,38 @@ static void handle_sys(DisasContext *s, bool isread, assert(!(ri->type & ARM_CP_RAISES_EXC)); } + if (nv2_mem_redirect) { + /* + * This system register is being redirected into an EL2 memory access. + * This means it is not an IO operation, doesn't change hflags, + * and need not end the TB, because it has no side effects. + * + * The access is 64-bit single copy atomic, guaranteed aligned because + * of the definition of VCNR_EL2. Its endianness depends on + * SCTLR_EL2.EE, not on the data endianness of EL1. + * It is done under either the EL2 translation regime or the EL2&0 + * translation regime, depending on HCR_EL2.E2H. It behaves as if + * PSTATE.PAN is 0. + */ + TCGv_i64 ptr = tcg_temp_new_i64(); + MemOp mop = MO_64 | MO_ALIGN | MO_ATOM_IFALIGN; + ARMMMUIdx armmemidx = s->nv2_mem_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2; + int memidx = arm_to_core_mmu_idx(armmemidx); + + mop |= (s->nv2_mem_be ? MO_BE : MO_LE); + + tcg_gen_ld_i64(ptr, tcg_env, offsetof(CPUARMState, cp15.vncr_el2)); + tcg_gen_addi_i64(ptr, ptr, + (ri->nv2_redirect_offset & ~NV2_REDIR_FLAG_MASK)); + tcg_rt = cpu_reg(s, rt); + if (isread) { + tcg_gen_qemu_ld_i64(tcg_rt, ptr, memidx, mop); + } else { + tcg_gen_qemu_st_i64(tcg_rt, ptr, memidx, mop); + } + return; + } + /* Handle special cases first */ switch (ri->type & ARM_CP_SPECIAL_MASK) { case 0: @@ -14063,6 +14117,8 @@ static void aarch64_tr_init_disas_context(DisasContextBase *dcbase, dc->nv = EX_TBFLAG_A64(tb_flags, NV); dc->nv1 = EX_TBFLAG_A64(tb_flags, NV1); dc->nv2 = EX_TBFLAG_A64(tb_flags, NV2); + dc->nv2_mem_e20 = EX_TBFLAG_A64(tb_flags, NV2_MEM_E20); + dc->nv2_mem_be = EX_TBFLAG_A64(tb_flags, NV2_MEM_BE); dc->vec_len = 0; dc->vec_stride = 0; dc->cp_regs = arm_cpu->cp_regs;