From patchwork Thu Apr 21 15:17:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 564407 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:6886:0:0:0:0 with SMTP id m6csp598591map; Thu, 21 Apr 2022 09:41:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzz7u7C4vCwzymvVALRmJ7pqUqzdbnszIW6wqfDfInZvxGrWOnqBQveFGI4YGH8qAzYl3hu X-Received: by 2002:a81:8310:0:b0:2ec:610:e6c6 with SMTP id t16-20020a818310000000b002ec0610e6c6mr561813ywf.9.1650559272329; Thu, 21 Apr 2022 09:41:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650559272; cv=none; d=google.com; s=arc-20160816; b=Z8Ft0EzTYMESClr5pCLKj4ra7Rq+WrIxmHYesju69hAWGXn8JJq1keniLzvY6EdHjy wYykeExwdhcJH4NbQqzhlj7ODALfPIxAzPv2hGWHb1PNb2EBgp/I6F8ULLSqA5ja6UH1 rMjK1m6uUBqxT/hogPEA3t9Dpdqyvot27iVT9nnrHKVLCloKAeUTYvwcZGjchjn0gF12 195wpmlFgjeWbAjc5MuqL6OpO/uTcmFc/VieZ4HdczDMsSEzsxsXhXzE4aj2YB/f7+0g D39VwK0KkPUJwHTceD3l44w+ic1qY4cdQwJMd7pRXuKRgjk+3TqqfvIqul9bOTAMPnOZ 2Xqw== 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=9K3mlYGGwaqfcU8Jeepe3E24P/PuHUJATMBSJPBwlMM=; b=aClZedIgN6XNPznm8y/uJ9l4a7YK28T7e3X+qkqR/xitndq47S+72fDqXxLXgEzbT5 rj1nZGPdEgkEz4z/GtGzCSUXFgWBVAzv3Vfr7Ks3UAWdwVdC0cO+HFHKg9PUxXdb2Fju hqX+DjGe07e85GHyhS3BRvg9/GokSuCXciJSYc2f76uyt5SR4wdZSLb3vEQPSPu37odw KGBRJWBgEaLMDvJ238dMUma5t4FSGm/E8rwH8coHiKX5EelPi1XQNoaF09XEgc4E2T+H 3A42cJVkzH+C/wIyQL03QLVojTmnubx++qwE2gEfKPkP8jVyk71Tk/34QLE/CWoqxqKe JtEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=I55h4Mzh; 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 j18-20020a5b0692000000b0064203a6fc1bsi2727676ybq.412.2022.04.21.09.41.12 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 21 Apr 2022 09:41:12 -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=pass header.i=@linaro.org header.s=google header.b=I55h4Mzh; 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]:58570 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nhZrj-0004z9-KZ for patch@linaro.org; Thu, 21 Apr 2022 12:41:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45014) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nhYd7-0005f3-Mz for qemu-devel@nongnu.org; Thu, 21 Apr 2022 11:22:01 -0400 Received: from mail-il1-x132.google.com ([2607:f8b0:4864:20::132]:46978) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nhYd4-00078s-PE for qemu-devel@nongnu.org; Thu, 21 Apr 2022 11:22:01 -0400 Received: by mail-il1-x132.google.com with SMTP id f5so3245125ilj.13 for ; Thu, 21 Apr 2022 08:21:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=9K3mlYGGwaqfcU8Jeepe3E24P/PuHUJATMBSJPBwlMM=; b=I55h4Mzhv1g+QTk+5cEVelctMms0baNOPcI5VT+phVBqMBSWP+seeQUznnf+SjzZZZ heciht+d/6/dBY4MjFd8iq0oTlsQrAUYQTS1bA20yyb1AXJWdObBtUi9XFTrJAxr9pqr WNphdv/QCsFIzEF5i4n4Zs3FhkzGPLBI5NWkXdJieuqPBpg9/hqTzC4OojY3HSCscXig P1KkEMgW4mnhzqdM47mXHCpSZM1uNxh4krsH14C9h5fVYT066/HS/P6V9n61LrG93HIX jpcyjF3d4iLvl2RTbY0bhqk89eXv+Gh3/hFJKXsS2Tt/odblIDrU4Uy56kGbKZupMqqj msWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9K3mlYGGwaqfcU8Jeepe3E24P/PuHUJATMBSJPBwlMM=; b=A84n480MW4S1zqY/Oz2jHaBH9koFKyKeFa5FBO7hS+fpswtIsfjpqxwYVlRGDhGH2g zPP8ZnEJyMrAuYkR/mcx94F01aRGivTeWJmtsCDhKFZNfU4orO4jJRiSCchJrFonS1sh b7uHXvVqaq2Ex4DtMWAZh6MgTjFUqe3NRTOtdFRg8aYBH+5xmGyyJMcFjdEohfRFkVGH cetMuPn22wK9y1cKttAM3+rLW+HASOYR2YGOSIBVOfogGjQ+Bc5eLt4gPxpWFte9Q4yx S2xRoU6aoZOz4x/UPDpkawzegBtRScmZDjKHTT98mewrUWx0DNq5/svJSYE4UmlF7ysQ pTdA== X-Gm-Message-State: AOAM533XIVFE9JjCgLz82d9soivhjOSGOKsgRXpWXMlq0Hp44fwS18yE rlKaktIbpSfqzE0zMjnsclVOZN1mUH8Tug== X-Received: by 2002:a92:6e0f:0:b0:2c6:4ed8:7bab with SMTP id j15-20020a926e0f000000b002c64ed87babmr73528ilc.41.1650554517571; Thu, 21 Apr 2022 08:21:57 -0700 (PDT) Received: from stoup.. ([2607:fb90:80c8:6cf3:a91a:4957:ba18:ac18]) by smtp.gmail.com with ESMTPSA id j1-20020a926e01000000b002cc52449a19sm3985402ilc.35.2022.04.21.08.21.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Apr 2022 08:21:57 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v7 53/64] target/nios2: Introduce shadow register sets Date: Thu, 21 Apr 2022 08:17:24 -0700 Message-Id: <20220421151735.31996-54-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220421151735.31996-1-richard.henderson@linaro.org> References: <20220421151735.31996-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::132; envelope-from=richard.henderson@linaro.org; helo=mail-il1-x132.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" Do not actually enable them so far, in terms of being able to change the current register set, but add all of the plumbing to address them. Do not enable them for user-only. Add an env->regs pointer that handles the indirection to the current register set. The naming of the pointer hides the difference between old and new, user-only and sysemu. >From the notes on wrprs, which states that r0 must be initialized before use in shadow register sets, infer that R_ZERO is *not* hardwired to zero in shadow register sets, but that it is still read-only. Introduce tbflags bit R0_0 to track that it has been properly set to zero. Adjust load_gpr to reflect this. At the same time we might as well special case crs == 0 to avoid the indirection through env->regs during translation as well; this is intended to be the most common case for non-interrupt handlers. Init env->regs at reset. Signed-off-by: Richard Henderson Reviewed-by: Peter Maydell --- target/nios2/cpu.h | 29 ++++++++++++++++++- target/nios2/cpu.c | 4 ++- target/nios2/translate.c | 61 ++++++++++++++++++++++++++++++++++------ 3 files changed, 83 insertions(+), 11 deletions(-) diff --git a/target/nios2/cpu.h b/target/nios2/cpu.h index 6417e9bbfc..08f0688628 100644 --- a/target/nios2/cpu.h +++ b/target/nios2/cpu.h @@ -60,6 +60,11 @@ struct Nios2CPUClass { #define NUM_GP_REGS 32 #define NUM_CR_REGS 32 +#ifndef CONFIG_USER_ONLY +/* 63 shadow register sets; index 0 is the primary register set. */ +#define NUM_REG_SETS 64 +#endif + /* General purpose register aliases */ enum { R_ZERO = 0, @@ -178,7 +183,13 @@ FIELD(CR_TLBMISC, EE, 24, 1) #define EXCP_MPUD 17 struct CPUArchState { +#ifdef CONFIG_USER_ONLY uint32_t regs[NUM_GP_REGS]; +#else + uint32_t shadow_regs[NUM_REG_SETS][NUM_GP_REGS]; + /* Pointer into shadow_regs for the current register set. */ + uint32_t *regs; +#endif uint32_t ctrl[NUM_CR_REGS]; uint32_t pc; @@ -229,6 +240,14 @@ static inline bool nios2_cr_reserved(const ControlRegState *s) return (s->writable | s->readonly) == 0; } +static inline void nios2_update_crs(CPUNios2State *env) +{ +#ifndef CONFIG_USER_ONLY + unsigned crs = FIELD_EX32(env->ctrl[CR_STATUS], CR_STATUS, CRS); + env->regs = env->shadow_regs[crs]; +#endif +} + void nios2_tcg_init(void); void nios2_cpu_do_interrupt(CPUState *cs); void dump_mmu(CPUNios2State *env); @@ -267,12 +286,20 @@ typedef Nios2CPU ArchCPU; #include "exec/cpu-all.h" +FIELD(TBFLAGS, CRS0, 0, 1) /* Set if CRS == 0. */ +FIELD(TBFLAGS, U, 1, 1) /* Overlaps CR_STATUS_U */ +FIELD(TBFLAGS, R0_0, 2, 1) /* Set if R0 == 0. */ + static inline void cpu_get_tb_cpu_state(CPUNios2State *env, target_ulong *pc, target_ulong *cs_base, uint32_t *flags) { + unsigned crs = FIELD_EX32(env->ctrl[CR_STATUS], CR_STATUS, CRS); + *pc = env->pc; *cs_base = 0; - *flags = env->ctrl[CR_STATUS] & CR_STATUS_U; + *flags = (env->ctrl[CR_STATUS] & CR_STATUS_U) + | (crs ? 0 : R_TBFLAGS_CRS0_MASK) + | (env->regs[0] ? 0 : R_TBFLAGS_R0_0_MASK); } #endif /* NIOS2_CPU_H */ diff --git a/target/nios2/cpu.c b/target/nios2/cpu.c index 54e7071907..d043c02fcd 100644 --- a/target/nios2/cpu.c +++ b/target/nios2/cpu.c @@ -48,15 +48,17 @@ static void nios2_cpu_reset(DeviceState *dev) ncc->parent_reset(dev); - memset(env->regs, 0, sizeof(env->regs)); memset(env->ctrl, 0, sizeof(env->ctrl)); env->pc = cpu->reset_addr; #if defined(CONFIG_USER_ONLY) /* Start in user mode with interrupts enabled. */ env->ctrl[CR_STATUS] = CR_STATUS_RSIE | CR_STATUS_U | CR_STATUS_PIE; + memset(env->regs, 0, sizeof(env->regs)); #else env->ctrl[CR_STATUS] = CR_STATUS_RSIE; + nios2_update_crs(env); + memset(env->shadow_regs, 0, sizeof(env->shadow_regs)); #endif } diff --git a/target/nios2/translate.c b/target/nios2/translate.c index 827d957719..a6f5d39f4a 100644 --- a/target/nios2/translate.c +++ b/target/nios2/translate.c @@ -121,12 +121,16 @@ typedef struct DisasContext { DisasContextBase base; target_ulong pc; int mem_idx; + uint32_t tb_flags; TCGv sink; const ControlRegState *cr_state; } DisasContext; static TCGv cpu_R[NUM_GP_REGS]; static TCGv cpu_pc; +#ifndef CONFIG_USER_ONLY +static TCGv cpu_crs_R[NUM_GP_REGS]; +#endif typedef struct Nios2Instruction { void (*handler)(DisasContext *dc, uint32_t code, uint32_t flags); @@ -148,22 +152,47 @@ static uint8_t get_opxcode(uint32_t code) static TCGv load_gpr(DisasContext *dc, unsigned reg) { assert(reg < NUM_GP_REGS); - if (unlikely(reg == R_ZERO)) { + + /* + * With shadow register sets, register r0 does not necessarily contain 0, + * but it is overwhelmingly likely that it does -- software is supposed + * to have set r0 to 0 in every shadow register set before use. + */ + if (unlikely(reg == R_ZERO) && FIELD_EX32(dc->tb_flags, TBFLAGS, R0_0)) { return tcg_constant_tl(0); } - return cpu_R[reg]; + if (FIELD_EX32(dc->tb_flags, TBFLAGS, CRS0)) { + return cpu_R[reg]; + } +#ifdef CONFIG_USER_ONLY + g_assert_not_reached(); +#else + return cpu_crs_R[reg]; +#endif } static TCGv dest_gpr(DisasContext *dc, unsigned reg) { assert(reg < NUM_GP_REGS); + + /* + * The spec for shadow register sets isn't clear, but we assume that + * writes to r0 are discarded regardless of CRS. + */ if (unlikely(reg == R_ZERO)) { if (dc->sink == NULL) { dc->sink = tcg_temp_new(); } return dc->sink; } - return cpu_R[reg]; + if (FIELD_EX32(dc->tb_flags, TBFLAGS, CRS0)) { + return cpu_R[reg]; + } +#ifdef CONFIG_USER_ONLY + g_assert_not_reached(); +#else + return cpu_crs_R[reg]; +#endif } static void t_gen_helper_raise_exception(DisasContext *dc, @@ -219,7 +248,7 @@ static void gen_excp(DisasContext *dc, uint32_t code, uint32_t flags) static bool gen_check_supervisor(DisasContext *dc) { - if (dc->base.tb->flags & CR_STATUS_U) { + if (FIELD_EX32(dc->tb_flags, TBFLAGS, U)) { /* CPU in user mode, privileged instruction called, stop. */ t_gen_helper_raise_exception(dc, EXCP_SUPERI); return false; @@ -330,7 +359,7 @@ static void do_i_math_logic(DisasContext *dc, uint32_t insn, val = imm(&instr); - if (instr.a == R_ZERO) { + if (instr.a == R_ZERO && FIELD_EX32(dc->tb_flags, TBFLAGS, R0_0)) { /* This catches the canonical expansions of movi and movhi. */ tcg_gen_movi_tl(dest_gpr(dc, instr.b), x_op_0_eq_x ? val : 0); } else { @@ -864,6 +893,7 @@ static void nios2_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) dc->mem_idx = cpu_mmu_index(env, false); dc->cr_state = cpu->cr_state; + dc->tb_flags = dc->base.tb->flags; /* Bound the number of insns to execute to those left on the page. */ page_insns = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4; @@ -998,13 +1028,26 @@ void nios2_cpu_dump_state(CPUState *cs, FILE *f, int flags) void nios2_tcg_init(void) { - int i; +#ifndef CONFIG_USER_ONLY + TCGv_ptr crs = tcg_global_mem_new_ptr(cpu_env, + offsetof(CPUNios2State, regs), "crs"); - for (i = 0; i < NUM_GP_REGS; i++) { - cpu_R[i] = tcg_global_mem_new(cpu_env, - offsetof(CPUNios2State, regs[i]), + for (int i = 0; i < NUM_GP_REGS; i++) { + cpu_crs_R[i] = tcg_global_mem_new(crs, 4 * i, gr_regnames[i]); + } + +#define offsetof_regs0(N) offsetof(CPUNios2State, shadow_regs[0][N]) +#else +#define offsetof_regs0(N) offsetof(CPUNios2State, regs[N]) +#endif + + for (int i = 0; i < NUM_GP_REGS; i++) { + cpu_R[i] = tcg_global_mem_new(cpu_env, offsetof_regs0(i), gr_regnames[i]); } + +#undef offsetof_regs0 + cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPUNios2State, pc), "pc"); }