From patchwork Tue Oct 19 00:00:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 515939 Delivered-To: patch@linaro.org Received: by 2002:ac0:cd8c:0:0:0:0:0 with SMTP id d12csp70712imp; Mon, 18 Oct 2021 17:07:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZaHyOQiqjH6bUAyvg79aO1wBFaMlt3po55qbY8e43PvDM9GbStgOaa9so0vvg1DRFmm8w X-Received: by 2002:ab0:72d5:: with SMTP id g21mr29244637uap.102.1634602020367; Mon, 18 Oct 2021 17:07:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634602020; cv=none; d=google.com; s=arc-20160816; b=Ux7yMDijf+4KnMl7TBqLUZLumVoWivgr1YzZTcTN7RsWj69ANaMdAOtenVcwzVj+N5 P2L4xI/UG1I+S6KvxQ1F7MwQD85ADyrpdowQUBjHZ+iYncAfmrWi5yJe3lhBuZNPi9G5 7C2nmKqyb4cDlyDnxoJ3/s4K8/EnEB/9Aoz6Debmtl3DBhGr2nrEfugG6D5iUXpPaHd7 nuZsBso2bXRX2gVsXNEPN1BbaMlRfOh17A6jym+VY1dU1gis+O7ntM5ccLLpnUzNONTs UbUCL5TQ0F4X49JDhTzJ9sfuEaZgtuH3AIF9lvBy+ZltdfjyYFYKjQimRlT0Umt5ArRu 7j2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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=JntjpE4dbZf2jEil4I0MzTIE4C49WVyfW1GrdUDx7w8=; b=pn4MIKyV3ol62D+3NSFcA91RNPiKx6NKTxRiwZhCtpdKkyC55ThyvCPusRtJpzusCK GT24nA5pjuUGO+1IAfqOopa7YaeA2DZFIBatiSLn325R3QPRndF6LAGcQDUEjC/2rJEG xh9mGWecEZnoJQ9erWEuJusM0QZe9hd9+TWDcNQ1QWyS8aE+Gys+Gnb0+KJv6Nzr0KPY Eef78tV9jRsutZH2liHOmFhHe1QPe08aVmjZKZNMzK49PswOnhoSVe/WYIz2t01EE0gt STgvueGuYvjNtTygNVhvfPOJr1ilfuEvKBi7UE32ijHr6VeWP4p27IhhhbNy4bFaUPQ/ 5QXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=EpEJvLj0; 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=fail (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 h22si9432110vsl.360.2021.10.18.17.07.00 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 18 Oct 2021 17:07:00 -0700 (PDT) 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=fail header.i=@linaro.org header.s=google header.b=EpEJvLj0; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:56226 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mcceh-00039i-Ol for patch@linaro.org; Mon, 18 Oct 2021 20:06:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43198) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mccZF-0004ju-6I for qemu-devel@nongnu.org; Mon, 18 Oct 2021 20:01:21 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]:33513) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mccZA-0001ez-VD for qemu-devel@nongnu.org; Mon, 18 Oct 2021 20:01:20 -0400 Received: by mail-pj1-x1032.google.com with SMTP id q10-20020a17090a1b0a00b001a076a59640so882617pjq.0 for ; Mon, 18 Oct 2021 17:01:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JntjpE4dbZf2jEil4I0MzTIE4C49WVyfW1GrdUDx7w8=; b=EpEJvLj0PD95egCk2WB4x+DM4vQTtGq5QrMQmZ6G8gXWA7PcosXEFHw/exDVEJhkJ+ HLgwgoJ8RSeAU+wrg16xhBLLLg6k0b6xP6YpKKAJVWuigND2ROGOmLfgOrSaJnW1ojNo Mj2zCQbHTDPMwOUln1ecCLo29g0kc3aaQjlURdbdwZh7p4hD6GwsdRvv+dHKCP/uVLXo w2A082Wq0eREGtevdTkap34cYsMyRdzxSYDpvJHNZclPLe5aBTldfua2dvu+J9e5jphk N7jTYQNxrdOZ+ZrWfUdivst3b4nnkg/7pNr9bWCqlYS1EzrH6FzhbPQuP5na1HGdXZ3G jaEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JntjpE4dbZf2jEil4I0MzTIE4C49WVyfW1GrdUDx7w8=; b=1YygYfEq4iuWR2ZFbsDFFEPvXFHKMryKR4G5lD0e3N6mV26cqsaJOY0wTTAXYkxVwD KzyIusDzqB7L/dbtm2zN44sjMXmHCSTwdWRmVlWd0EVR6TRPfvsuS8L/uqp043Mm/qsZ z/fSDm8MOk4kjWBKOu/tlGkeft1qCHk80c5QkkA/w/vLfyvD786r4LmWz8pqKZ7tZ/lx VJYmVYQfAWmWAzb86HgTlZvrR1SXM57M3p5dW2ipwWtiQRh8j4WMR8NwDncDWT4LJjqQ 8lkPH/pDyulDTZ8ntrb1KeX9xSJtoNb5MoKrUG5gvp40x9E0FY4X9tHhvy8itQqSdxoP 8tTQ== X-Gm-Message-State: AOAM532Y+ELWktpRu8EFHXLAYsTEiE/xflbqjvjMcexVXIoVRv8kIuPS +G/0TlZXq5OYQec2JS7ETwqZR7Qze/vvPw== X-Received: by 2002:a17:90b:120c:: with SMTP id gl12mr2443975pjb.50.1634601672944; Mon, 18 Oct 2021 17:01:12 -0700 (PDT) Received: from localhost.localdomain ([71.212.134.125]) by smtp.gmail.com with ESMTPSA id q14sm3068220pfk.3.2021.10.18.17.01.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Oct 2021 17:01:12 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v4 05/16] target/riscv: Add MXL/SXL/UXL to TB_FLAGS Date: Mon, 18 Oct 2021 17:00:57 -0700 Message-Id: <20211019000108.3678724-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211019000108.3678724-1-richard.henderson@linaro.org> References: <20211019000108.3678724-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1032.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 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alistair.francis@wdc.com, qemu-riscv@nongnu.org, zhiwei_liu@c-sky.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Begin adding support for switching XLEN at runtime. Extract the effective XLEN from MISA and MSTATUS and store for use during translation. Reviewed-by: LIU Zhiwei Reviewed-by: Alistair Francis Signed-off-by: Richard Henderson --- target/riscv/cpu.h | 2 ++ target/riscv/cpu.c | 8 ++++++++ target/riscv/cpu_helper.c | 33 +++++++++++++++++++++++++++++++++ target/riscv/csr.c | 3 +++ target/riscv/translate.c | 2 +- 5 files changed, 47 insertions(+), 1 deletion(-) -- 2.25.1 diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index d0e82135a9..c24bc9a039 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -395,6 +395,8 @@ FIELD(TB_FLAGS, VILL, 8, 1) /* Is a Hypervisor instruction load/store allowed? */ FIELD(TB_FLAGS, HLSX, 9, 1) FIELD(TB_FLAGS, MSTATUS_HS_FS, 10, 2) +/* The combination of MXL/SXL/UXL that applies to the current cpu mode. */ +FIELD(TB_FLAGS, XL, 12, 2) #ifdef TARGET_RISCV32 #define riscv_cpu_mxl(env) ((void)(env), MXL_RV32) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 1857670a69..4e1920d5f0 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -355,6 +355,14 @@ static void riscv_cpu_reset(DeviceState *dev) env->misa_mxl = env->misa_mxl_max; env->priv = PRV_M; env->mstatus &= ~(MSTATUS_MIE | MSTATUS_MPRV); + if (env->misa_mxl > MXL_RV32) { + /* + * The reset status of SXL/UXL is undefined, but mstatus is WARL + * and we must ensure that the value after init is valid for read. + */ + env->mstatus = set_field(env->mstatus, MSTATUS64_SXL, env->misa_mxl); + env->mstatus = set_field(env->mstatus, MSTATUS64_UXL, env->misa_mxl); + } env->mcause = 0; env->pc = env->resetvec; env->two_stage_lookup = false; diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 403f54171d..429afd1f48 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -35,6 +35,37 @@ int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch) #endif } +static RISCVMXL cpu_get_xl(CPURISCVState *env) +{ +#if defined(TARGET_RISCV32) + return MXL_RV32; +#elif defined(CONFIG_USER_ONLY) + return MXL_RV64; +#else + RISCVMXL xl = riscv_cpu_mxl(env); + + /* + * When emulating a 32-bit-only cpu, use RV32. + * When emulating a 64-bit cpu, and MXL has been reduced to RV32, + * MSTATUSH doesn't have UXL/SXL, therefore XLEN cannot be widened + * back to RV64 for lower privs. + */ + if (xl != MXL_RV32) { + switch (env->priv) { + case PRV_M: + break; + case PRV_U: + xl = get_field(env->mstatus, MSTATUS64_UXL); + break; + default: /* PRV_S | PRV_H */ + xl = get_field(env->mstatus, MSTATUS64_SXL); + break; + } + } + return xl; +#endif +} + void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, target_ulong *cs_base, uint32_t *pflags) { @@ -78,6 +109,8 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, } #endif + flags = FIELD_DP32(flags, TB_FLAGS, XL, cpu_get_xl(env)); + *pflags = flags; } diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 9c0753bc8b..c4a479ddd2 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -526,6 +526,9 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno, mstatus = set_field(mstatus, MSTATUS32_SD, dirty); } else { mstatus = set_field(mstatus, MSTATUS64_SD, dirty); + /* SXL and UXL fields are for now read only */ + mstatus = set_field(mstatus, MSTATUS64_SXL, MXL_RV64); + mstatus = set_field(mstatus, MSTATUS64_UXL, MXL_RV64); } env->mstatus = mstatus; diff --git a/target/riscv/translate.c b/target/riscv/translate.c index 66857732e8..f7634c175a 100644 --- a/target/riscv/translate.c +++ b/target/riscv/translate.c @@ -514,7 +514,6 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) #else ctx->virt_enabled = false; #endif - ctx->xl = env->misa_mxl; ctx->misa_ext = env->misa_ext; ctx->frm = -1; /* unknown rounding mode */ ctx->ext_ifencei = cpu->cfg.ext_ifencei; @@ -526,6 +525,7 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->lmul = FIELD_EX32(tb_flags, TB_FLAGS, LMUL); ctx->mlen = 1 << (ctx->sew + 3 - ctx->lmul); ctx->vl_eq_vlmax = FIELD_EX32(tb_flags, TB_FLAGS, VL_EQ_VLMAX); + ctx->xl = FIELD_EX32(tb_flags, TB_FLAGS, XL); ctx->cs = cs; ctx->w = false; ctx->ntemp = 0;