From nobody Fri Mar 14 13:30:45 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1738754484; cv=none; d=zohomail.com; s=zohoarc; b=DjddjlCtlwi57KEvBjN06PCEHiBCDDou8OMZWpxkVzO1R263CrlpqKUMhOFVEaj7vTYcdUFCMFJRXitqvB/ok46Geer7Nf1xzx/6CNjbMA008gPD9/Z61tNOxQMf0ewmREzME2VksnGOvGo1Jc+xpZmWytKpeoJgWaynkU1vQIA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1738754484; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xsv8OTxNBbAm6aqdb0W3B09cV5UTwY9Ck6BkBSDYH3w=; b=cpaDulzh37YQOzHMmApoOO67pF8duOKHRBh9dRm5QvP8UpDOeHPOtEe7z5lJt2BkEpc0hf8IM56SqNkZlbbnMEFPoNKaGLeM7iQgabXcDL/rhgOZMeiGhsQ0RXj/wcAkmz42JwmfNlKLRpnvNKyl4UgHh1cpAAWIbc489aaMtak= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1738754484888555.0701013793517; Wed, 5 Feb 2025 03:21:24 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tfdRU-0004UI-Su; Wed, 05 Feb 2025 06:19:40 -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 1tfdRR-0004R3-TT for qemu-devel@nongnu.org; Wed, 05 Feb 2025 06:19:37 -0500 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tfdRM-0003yL-Hl for qemu-devel@nongnu.org; Wed, 05 Feb 2025 06:19:37 -0500 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-4361b6f9faeso4429315e9.1 for ; Wed, 05 Feb 2025 03:19:32 -0800 (PST) Received: from localhost.localdomain ([51.52.155.79]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4390d93382dsm18256575e9.4.2025.02.05.03.19.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Feb 2025 03:19:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1738754371; x=1739359171; 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=xsv8OTxNBbAm6aqdb0W3B09cV5UTwY9Ck6BkBSDYH3w=; b=ceIiwpn/xI0tIA3PJfrYcg9Omo+mSwWXOsbfMpvvd/XjpnolzWdDEc8K2Q7Pg0nbDO V1CJK75Epz/C2sSZBcsTGySZeWBQHMbWIS6PS+y7L1k+dwSpirrfA1WftkQ73VM2WyyA bBJGabyakksjVOPfDMyltq2BYbsyK9Eh+lvjTA60cszAZ8Toby+TX64tHEgldeHWZWZ+ ictZhw0uBKZKJpbwcYJv6tVdIi3OQ97TAsw8KQo9jRy61dYwwB3TWpadACDsZiMeJQ5q NvrV1ji3bkl/aaXD2Ov+X0ngm81h6+aULmvVbhfxqO27Wr21g9EIICEcAznCler0hQRW GhYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738754371; x=1739359171; 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=xsv8OTxNBbAm6aqdb0W3B09cV5UTwY9Ck6BkBSDYH3w=; b=PVbSGdQI9AZ5hE4i9dnW/GfmppWOah7D4cHSAt1gyjwbTMoGXPnLejnbKnZKFSMrmS jJ0h4OeB8EaNW0gkliy1UHzYZcpbhyAOWSWW5xAZTQ4w/w8FTcU0mtFzhhgZBnDkpefw ctj806SWQBk1UQ91ZFK4VjE0Bn6NlZZXs6yWKShUtUp0kz6K8feZi6VHdYAThqz+cT4d XTJHje6AZ6FKg6kXDagX/t+MUCA/BGjiRKmD9luaggfjIrDNvMjob6ZaME2HkfpiDrTo K1TvUyQNDgMV45MXa4At5Eu2GY5OaTwGnpuwPJUa4LA5ROCBcUd4tC/pLdUxlU0lozU+ oBVQ== X-Forwarded-Encrypted: i=1; AJvYcCWZBDacF4rS68y5kcAqmy7WaDJqD3rXs2BhPb0VEY/KAQ7IFSopwXdOc/JWN3P+yStv8uSAgBoD+mZL@nongnu.org X-Gm-Message-State: AOJu0YwLoI2rl27jhTujzx9Pq3kCCyACcnJTFUqZTHB5yEcOQ5r5CbMb vIGRi9UfGXsGPbcMDiwh3Iv9ZmQJq8O/gjBoImz9dzX9arv1sLaeN4qLk3L8BxY= X-Gm-Gg: ASbGncst7YQnvZHAboRdcZ0JntpRtEjd5GCLFTNRzm0ppqbAnw5Cbph/ZBwX29xmqXN Lj0tPRrheW/Je0e22l9ops6Qvqtf1+biMlBhj74i5dKPEJ8JnEmi0WK4nkY6jd8YCyNyyuZaD5l Jb4rZJop6gdvPkV28+0909b0+7uQnEMrUKLDT12WsELUdlx4K90I4IaHBQMZauY3RcV7IsIJwRJ I8yVmUqzYM6XjIV4Zig3+GaDbmdmYG2YEEZXinzcVOfzKMqlCjBI3sj2/7eWFbwyDqJWIqixhWe ze2V3C22+eIei6NH4Qhg5+iTddE= X-Google-Smtp-Source: AGHT+IEXUM7/wrps9L4m2xPudggNZDtLiD7P1OCd9L26Yn8a+oqo8NygoAoC3vfjXSjeHIGNANdokA== X-Received: by 2002:a05:600c:4589:b0:436:e3ea:64dd with SMTP id 5b1f17b1804b1-43905f8da7fmr53600845e9.11.1738754370659; Wed, 05 Feb 2025 03:19:30 -0800 (PST) From: Rajnesh Kanwal To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, bin.meng@windriver.com, liweiwei@iscas.ac.cn, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, atishp@rivosinc.com, apatel@ventanamicro.com, rkanwal@rivosinc.com, beeman@rivosinc.com, jason.chien@sifive.com, frank.chang@sifive.com, richard.henderson@linaro.org, bmeng.cn@gmail.com Subject: [PATCH v6 4/7] target/riscv: Add support to record CTR entries. Date: Wed, 5 Feb 2025 11:18:48 +0000 Message-Id: <20250205-b4-ctr_upstream_v6-v6-4-439d8e06c8ef@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250205-b4-ctr_upstream_v6-v6-0-439d8e06c8ef@rivosinc.com> References: <20250205-b4-ctr_upstream_v6-v6-0-439d8e06c8ef@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.14.2 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::333; envelope-from=rkanwal@rivosinc.com; helo=mail-wm1-x333.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1738754486563019000 This commit adds logic to records CTR entries of different types and adds required hooks in TCG and interrupt/Exception logic to record events. This commit also adds support to invoke freeze CTR logic for breakpoint exceptions and counter overflow interrupts. Signed-off-by: Rajnesh Kanwal Acked-by: Alistair Francis --- target/riscv/cpu.h | 7 + target/riscv/cpu_helper.c | 259 +++++++++++++++++++++= ++++ target/riscv/helper.h | 1 + target/riscv/insn_trans/trans_privileged.c.inc | 2 + target/riscv/insn_trans/trans_rvi.c.inc | 75 +++++++ target/riscv/insn_trans/trans_rvzce.c.inc | 21 ++ target/riscv/op_helper.c | 19 ++ target/riscv/translate.c | 46 +++++ 8 files changed, 430 insertions(+) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index e51a4bc4a535276d1b348f9ed50443be898d538f..2d8212e2fefe21defb4ca007d89= 4f02592ed3c89 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -317,6 +317,10 @@ struct CPUArchState { uint32_t sctrstatus; uint64_t vsctrctl; =20 + uint64_t ctr_src[16 << SCTRDEPTH_MAX]; + uint64_t ctr_dst[16 << SCTRDEPTH_MAX]; + uint64_t ctr_data[16 << SCTRDEPTH_MAX]; + /* Machine and Supervisor interrupt priorities */ uint8_t miprio[64]; uint8_t siprio[64]; @@ -612,6 +616,9 @@ RISCVException smstateen_acc_ok(CPURISCVState *env, int= index, uint64_t bit); =20 void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv, bool vir= t_en); =20 +void riscv_ctr_add_entry(CPURISCVState *env, target_long src, target_long = dst, + enum CTRType type, target_ulong prev_priv, bool prev_virt); + void riscv_translate_init(void); void riscv_translate_code(CPUState *cs, TranslationBlock *tb, int *max_insns, vaddr pc, void *host_pc); diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index e1dfc4ecbfc5da5204b261cb24d1afa312dba323..afecc822ae7d07a0904f1ec8c22= 3845bbac92523 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -875,6 +875,247 @@ void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env= , uint32_t priv, } } =20 +static void riscv_ctr_freeze(CPURISCVState *env, uint64_t freeze_mask, + bool virt) +{ + uint64_t ctl =3D virt ? env->vsctrctl : env->mctrctl; + + assert((freeze_mask & (~(XCTRCTL_BPFRZ | XCTRCTL_LCOFIFRZ))) =3D=3D 0); + + if (ctl & freeze_mask) { + env->sctrstatus |=3D SCTRSTATUS_FROZEN; + } +} + +static uint64_t riscv_ctr_priv_to_mask(target_ulong priv, bool virt) +{ + switch (priv) { + case PRV_M: + return MCTRCTL_M; + case PRV_S: + if (virt) { + return XCTRCTL_S; + } + return XCTRCTL_S; + case PRV_U: + if (virt) { + return XCTRCTL_U; + } + return XCTRCTL_U; + } + + g_assert_not_reached(); +} + +static uint64_t riscv_ctr_get_control(CPURISCVState *env, target_long priv, + bool virt) +{ + switch (priv) { + case PRV_M: + return env->mctrctl; + case PRV_S: + case PRV_U: + if (virt) { + return env->vsctrctl; + } + return env->mctrctl; + } + + g_assert_not_reached(); +} + +/* + * This function assumes that src privilege and target privilege are not s= ame + * and src privilege is less than target privilege. This includes the virt= ual + * state as well. + */ +static bool riscv_ctr_check_xte(CPURISCVState *env, target_long src_prv, + bool src_virt) +{ + target_long tgt_prv =3D env->priv; + bool res =3D true; + + /* + * VS and U mode are same in terms of xTE bits required to record an + * external trap. See 6.1.2. External Traps, table 8 External Trap Ena= ble + * Requirements. This changes VS to U to simplify the logic a bit. + */ + if (src_virt && src_prv =3D=3D PRV_S) { + src_prv =3D PRV_U; + } else if (env->virt_enabled && tgt_prv =3D=3D PRV_S) { + tgt_prv =3D PRV_U; + } + + /* VU mode is an outlier here. */ + if (src_virt && src_prv =3D=3D PRV_U) { + res &=3D !!(env->vsctrctl & XCTRCTL_STE); + } + + switch (src_prv) { + case PRV_U: + if (tgt_prv =3D=3D PRV_U) { + break; + } + res &=3D !!(env->mctrctl & XCTRCTL_STE); + /* fall-through */ + case PRV_S: + if (tgt_prv =3D=3D PRV_S) { + break; + } + res &=3D !!(env->mctrctl & MCTRCTL_MTE); + /* fall-through */ + case PRV_M: + break; + } + + return res; +} + +/* + * Special cases for traps and trap returns: + * + * 1- Traps, and trap returns, between enabled modes are recorded as norma= l. + * 2- Traps from an inhibited mode to an enabled mode, and trap returns fr= om an + * enabled mode back to an inhibited mode, are partially recorded. In such + * cases, the PC from the inhibited mode (source PC for traps, and target = PC + * for trap returns) is 0. + * + * 3- Trap returns from an inhibited mode to an enabled mode are not recor= ded. + * Traps from an enabled mode to an inhibited mode, known as external trap= s, + * receive special handling. + * By default external traps are not recorded, but a handshake mechanism e= xists + * to allow partial recording. Software running in the target mode of the= trap + * can opt-in to allowing CTR to record traps into that mode even when the= mode + * is inhibited. The MTE, STE, and VSTE bits allow M-mode, S-mode, and VS= -mode, + * respectively, to opt-in. When an External Trap occurs, and xTE=3D1, suc= h that + * x is the target privilege mode of the trap, will CTR record the trap. I= n such + * cases, the target PC is 0. + */ +/* + * CTR arrays are implemented as circular buffers and new entry is stored = at + * sctrstatus.WRPTR, but they are presented to software as moving circular + * buffers. Which means, software get's the illusion that whenever a new e= ntry + * is added the whole buffer is moved by one place and the new entry is ad= ded at + * the start keeping new entry at idx 0 and older ones follow. + * + * Depth =3D 16. + * + * buffer [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F] + * WRPTR W + * entry 7 6 5 4 3 2 1 0 F E D C B A 9 8 + * + * When a new entry is added: + * buffer [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [A] [B] [C] [D] [E] [F] + * WRPTR W + * entry 8 7 6 5 4 3 2 1 0 F E D C B A 9 + * + * entry here denotes the logical entry number that software can access + * using ctrsource, ctrtarget and ctrdata registers. So xiselect 0x200 + * will return entry 0 i-e buffer[8] and 0x201 will return entry 1 i-e + * buffer[7]. Here is how we convert entry to buffer idx. + * + * entry =3D isel - CTR_ENTRIES_FIRST; + * idx =3D (sctrstatus.WRPTR - entry - 1) & (depth - 1); + */ +void riscv_ctr_add_entry(CPURISCVState *env, target_long src, target_long = dst, + enum CTRType type, target_ulong src_priv, bool src_virt) +{ + bool tgt_virt =3D env->virt_enabled; + uint64_t src_mask =3D riscv_ctr_priv_to_mask(src_priv, src_virt); + uint64_t tgt_mask =3D riscv_ctr_priv_to_mask(env->priv, tgt_virt); + uint64_t src_ctrl =3D riscv_ctr_get_control(env, src_priv, src_virt); + uint64_t tgt_ctrl =3D riscv_ctr_get_control(env, env->priv, tgt_virt); + uint64_t depth, head; + bool ext_trap =3D false; + + /* + * Return immediately if both target and src recording is disabled or = if + * CTR is in frozen state. + */ + if ((!(src_ctrl & src_mask) && !(tgt_ctrl & tgt_mask)) || + env->sctrstatus & SCTRSTATUS_FROZEN) { + return; + } + + /* + * With RAS Emul enabled, only allow Indirect, direct calls, Function + * returns and Co-routine swap types. + */ + if (tgt_ctrl & XCTRCTL_RASEMU && + type !=3D CTRDATA_TYPE_INDIRECT_CALL && + type !=3D CTRDATA_TYPE_DIRECT_CALL && + type !=3D CTRDATA_TYPE_RETURN && + type !=3D CTRDATA_TYPE_CO_ROUTINE_SWAP) { + return; + } + + if (type =3D=3D CTRDATA_TYPE_EXCEPTION || type =3D=3D CTRDATA_TYPE_INT= ERRUPT) { + /* Case 2 for traps. */ + if (!(src_ctrl & src_mask)) { + src =3D 0; + } else if (!(tgt_ctrl & tgt_mask)) { + /* Check if target priv-mode has allowed external trap recordi= ng. */ + if (!riscv_ctr_check_xte(env, src_priv, src_virt)) { + return; + } + + ext_trap =3D true; + dst =3D 0; + } + } else if (type =3D=3D CTRDATA_TYPE_EXCEP_INT_RET) { + /* + * Case 3 for trap returns. Trap returns from inhibited mode are = not + * recorded. + */ + if (!(src_ctrl & src_mask)) { + return; + } + + /* Case 2 for trap returns. */ + if (!(tgt_ctrl & tgt_mask)) { + dst =3D 0; + } + } + + /* Ignore filters in case of RASEMU mode or External trap. */ + if (!(tgt_ctrl & XCTRCTL_RASEMU) && !ext_trap) { + /* + * Check if the specific type is inhibited. Not taken branch filte= r is + * an enable bit and needs to be checked separatly. + */ + bool check =3D tgt_ctrl & BIT_ULL(type + XCTRCTL_INH_START); + if ((type =3D=3D CTRDATA_TYPE_NONTAKEN_BRANCH && !check) || + (type !=3D CTRDATA_TYPE_NONTAKEN_BRANCH && check)) { + return; + } + } + + head =3D get_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK); + + depth =3D 16 << get_field(env->sctrdepth, SCTRDEPTH_MASK); + if (tgt_ctrl & XCTRCTL_RASEMU && type =3D=3D CTRDATA_TYPE_RETURN) { + head =3D (head - 1) & (depth - 1); + + env->ctr_src[head] &=3D ~CTRSOURCE_VALID; + env->sctrstatus =3D + set_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK, head); + return; + } + + /* In case of Co-routine SWAP we overwrite latest entry. */ + if (tgt_ctrl & XCTRCTL_RASEMU && type =3D=3D CTRDATA_TYPE_CO_ROUTINE_S= WAP) { + head =3D (head - 1) & (depth - 1); + } + + env->ctr_src[head] =3D src | CTRSOURCE_VALID; + env->ctr_dst[head] =3D dst & ~CTRTARGET_MISP; + env->ctr_data[head] =3D set_field(0, CTRDATA_TYPE_MASK, type); + + head =3D (head + 1) & (depth - 1); + + env->sctrstatus =3D set_field(env->sctrstatus, SCTRSTATUS_WRPTR_MASK, = head); +} + void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv, bool vir= t_en) { g_assert(newpriv <=3D PRV_M && newpriv !=3D PRV_RESERVED); @@ -1986,10 +2227,13 @@ void riscv_cpu_do_interrupt(CPUState *cs) !(env->mip & (1ULL << cause)); bool smode_double_trap =3D false; uint64_t hdeleg =3D async ? env->hideleg : env->hedeleg; + const bool prev_virt =3D env->virt_enabled; + const target_ulong prev_priv =3D env->priv; target_ulong tval =3D 0; target_ulong tinst =3D 0; target_ulong htval =3D 0; target_ulong mtval2 =3D 0; + target_ulong src; int sxlen =3D 0; int mxlen =3D 16 << riscv_cpu_mxl(env); bool nnmi_excep =3D false; @@ -2173,6 +2417,8 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->pc =3D (env->stvec >> 2 << 2) + ((async && (env->stvec & 3) =3D=3D 1) ? cause * 4 : 0); riscv_cpu_set_mode(env, PRV_S, virt); + + src =3D env->sepc; } else { /* * If the hart encounters an exception while executing in M-mode @@ -2257,6 +2503,19 @@ void riscv_cpu_do_interrupt(CPUState *cs) ((async && (env->mtvec & 3) =3D=3D 1) ? cause * 4 : = 0); } riscv_cpu_set_mode(env, PRV_M, virt); + src =3D env->mepc; + } + + if (riscv_cpu_cfg(env)->ext_smctr || riscv_cpu_cfg(env)->ext_ssctr) { + if (async && cause =3D=3D IRQ_PMU_OVF) { + riscv_ctr_freeze(env, XCTRCTL_LCOFIFRZ, virt); + } else if (!async && cause =3D=3D RISCV_EXCP_BREAKPOINT) { + riscv_ctr_freeze(env, XCTRCTL_BPFRZ, virt); + } + + riscv_ctr_add_entry(env, src, env->pc, + async ? CTRDATA_TYPE_INTERRUPT : CTRDATA_TYPE_EXCE= PTION, + prev_priv, prev_virt); } =20 /* diff --git a/target/riscv/helper.h b/target/riscv/helper.h index 16ea240d26d31b103dd5da0ac4c7f7d67869af71..163121ade599f5ec2682ecea25a= 59b664593fba3 100644 --- a/target/riscv/helper.h +++ b/target/riscv/helper.h @@ -136,6 +136,7 @@ DEF_HELPER_1(wfi, void, env) DEF_HELPER_1(wrs_nto, void, env) DEF_HELPER_1(tlb_flush, void, env) DEF_HELPER_1(tlb_flush_all, void, env) +DEF_HELPER_4(ctr_add_entry, void, env, tl, tl, tl) /* Native Debug */ DEF_HELPER_1(itrigger_match, void, env) #endif diff --git a/target/riscv/insn_trans/trans_privileged.c.inc b/target/riscv/= insn_trans/trans_privileged.c.inc index 27d5558d63edf10a516c068a69023ad9035414fd..ca52405d7da411907f1d52234df= 955940efdc25d 100644 --- a/target/riscv/insn_trans/trans_privileged.c.inc +++ b/target/riscv/insn_trans/trans_privileged.c.inc @@ -86,6 +86,7 @@ static bool trans_sret(DisasContext *ctx, arg_sret *a) if (has_ext(ctx, RVS)) { decode_save_opc(ctx, 0); translator_io_start(&ctx->base); + gen_update_pc(ctx, 0); gen_helper_sret(cpu_pc, tcg_env); exit_tb(ctx); /* no chaining */ ctx->base.is_jmp =3D DISAS_NORETURN; @@ -103,6 +104,7 @@ static bool trans_mret(DisasContext *ctx, arg_mret *a) #ifndef CONFIG_USER_ONLY decode_save_opc(ctx, 0); translator_io_start(&ctx->base); + gen_update_pc(ctx, 0); gen_helper_mret(cpu_pc, tcg_env); exit_tb(ctx); /* no chaining */ ctx->base.is_jmp =3D DISAS_NORETURN; diff --git a/target/riscv/insn_trans/trans_rvi.c.inc b/target/riscv/insn_tr= ans/trans_rvi.c.inc index 96c218a9d7875c6419287ac3aa9746251be3f442..b55f56a5eb2c72eee004e16817a= 6df13c60446de 100644 --- a/target/riscv/insn_trans/trans_rvi.c.inc +++ b/target/riscv/insn_trans/trans_rvi.c.inc @@ -93,6 +93,51 @@ static bool trans_jal(DisasContext *ctx, arg_jal *a) return true; } =20 +#ifndef CONFIG_USER_ONLY +/* + * Indirect calls + * - jalr x1, rs where rs !=3D x5; + * - jalr x5, rs where rs !=3D x1; + * - c.jalr rs1 where rs1 !=3D x5; + * + * Indirect jumps + * - jalr x0, rs where rs !=3D x1 and rs !=3D x5; + * - c.jr rs1 where rs1 !=3D x1 and rs1 !=3D x5. + * + * Returns + * - jalr rd, rs where (rs =3D=3D x1 or rs =3D=3D x5) and rd !=3D x1 and r= d !=3D x5; + * - c.jr rs1 where rs1 =3D=3D x1 or rs1 =3D=3D x5. + * + * Co-routine swap + * - jalr x1, x5; + * - jalr x5, x1; + * - c.jalr x5. + * + * Other indirect jumps + * - jalr rd, rs where rs !=3D x1, rs !=3D x5, rd !=3D x0, rd !=3D x1 and = rd !=3D x5. + */ +static void gen_ctr_jalr(DisasContext *ctx, arg_jalr *a, TCGv dest) +{ + TCGv src =3D tcg_temp_new(); + TCGv type; + + if ((a->rd =3D=3D 1 && a->rs1 !=3D 5) || (a->rd =3D=3D 5 && a->rs1 != =3D 1)) { + type =3D tcg_constant_tl(CTRDATA_TYPE_INDIRECT_CALL); + } else if (a->rd =3D=3D 0 && a->rs1 !=3D 1 && a->rs1 !=3D 5) { + type =3D tcg_constant_tl(CTRDATA_TYPE_INDIRECT_JUMP); + } else if ((a->rs1 =3D=3D 1 || a->rs1 =3D=3D 5) && (a->rd !=3D 1 && a-= >rd !=3D 5)) { + type =3D tcg_constant_tl(CTRDATA_TYPE_RETURN); + } else if ((a->rs1 =3D=3D 1 && a->rd =3D=3D 5) || (a->rs1 =3D=3D 5 && = a->rd =3D=3D 1)) { + type =3D tcg_constant_tl(CTRDATA_TYPE_CO_ROUTINE_SWAP); + } else { + type =3D tcg_constant_tl(CTRDATA_TYPE_OTHER_INDIRECT_JUMP); + } + + gen_pc_plus_diff(src, ctx, 0); + gen_helper_ctr_add_entry(tcg_env, src, dest, type); +} +#endif + static bool trans_jalr(DisasContext *ctx, arg_jalr *a) { TCGLabel *misaligned =3D NULL; @@ -117,6 +162,12 @@ static bool trans_jalr(DisasContext *ctx, arg_jalr *a) gen_pc_plus_diff(succ_pc, ctx, ctx->cur_insn_len); gen_set_gpr(ctx, a->rd, succ_pc); =20 +#ifndef CONFIG_USER_ONLY + if (ctx->cfg_ptr->ext_smctr || ctx->cfg_ptr->ext_ssctr) { + gen_ctr_jalr(ctx, a, target_pc); + } +#endif + tcg_gen_mov_tl(cpu_pc, target_pc); if (ctx->fcfi_enabled) { /* @@ -231,6 +282,19 @@ static bool gen_branch(DisasContext *ctx, arg_b *a, TC= GCond cond) } else { tcg_gen_brcond_tl(cond, src1, src2, l); } + +#ifndef CONFIG_USER_ONLY + if (ctx->cfg_ptr->ext_smctr || ctx->cfg_ptr->ext_ssctr) { + TCGv type =3D tcg_constant_tl(CTRDATA_TYPE_NONTAKEN_BRANCH); + TCGv dest =3D tcg_temp_new(); + TCGv src =3D tcg_temp_new(); + + gen_pc_plus_diff(src, ctx, 0); + gen_pc_plus_diff(dest, ctx, ctx->cur_insn_len); + gen_helper_ctr_add_entry(tcg_env, src, dest, type); + } +#endif + gen_goto_tb(ctx, 1, ctx->cur_insn_len); ctx->pc_save =3D orig_pc_save; =20 @@ -243,6 +307,17 @@ static bool gen_branch(DisasContext *ctx, arg_b *a, TC= GCond cond) gen_pc_plus_diff(target_pc, ctx, a->imm); gen_exception_inst_addr_mis(ctx, target_pc); } else { +#ifndef CONFIG_USER_ONLY + if (ctx->cfg_ptr->ext_smctr || ctx->cfg_ptr->ext_ssctr) { + TCGv type =3D tcg_constant_tl(CTRDATA_TYPE_TAKEN_BRANCH); + TCGv dest =3D tcg_temp_new(); + TCGv src =3D tcg_temp_new(); + + gen_pc_plus_diff(src, ctx, 0); + gen_pc_plus_diff(dest, ctx, a->imm); + gen_helper_ctr_add_entry(tcg_env, src, dest, type); + } +#endif gen_goto_tb(ctx, 0, a->imm); } ctx->pc_save =3D -1; diff --git a/target/riscv/insn_trans/trans_rvzce.c.inc b/target/riscv/insn_= trans/trans_rvzce.c.inc index cd234ad960724c936b92afb6fd1f3c7c2a37cb80..c77c2b927b0cfdf7d21dca76577= 12015494e1b46 100644 --- a/target/riscv/insn_trans/trans_rvzce.c.inc +++ b/target/riscv/insn_trans/trans_rvzce.c.inc @@ -203,6 +203,14 @@ static bool gen_pop(DisasContext *ctx, arg_cmpp *a, bo= ol ret, bool ret_val) =20 if (ret) { TCGv ret_addr =3D get_gpr(ctx, xRA, EXT_SIGN); +#ifndef CONFIG_USER_ONLY + if (ctx->cfg_ptr->ext_smctr || ctx->cfg_ptr->ext_ssctr) { + TCGv type =3D tcg_constant_tl(CTRDATA_TYPE_RETURN); + TCGv src =3D tcg_temp_new(); + gen_pc_plus_diff(src, ctx, 0); + gen_helper_ctr_add_entry(tcg_env, src, ret_addr, type); + } +#endif tcg_gen_mov_tl(cpu_pc, ret_addr); tcg_gen_lookup_and_goto_ptr(); ctx->base.is_jmp =3D DISAS_NORETURN; @@ -309,6 +317,19 @@ static bool trans_cm_jalt(DisasContext *ctx, arg_cm_ja= lt *a) gen_set_gpr(ctx, xRA, succ_pc); } =20 +#ifndef CONFIG_USER_ONLY + if (ctx->cfg_ptr->ext_smctr || ctx->cfg_ptr->ext_ssctr) { + if (a->index >=3D 32) { + TCGv type =3D tcg_constant_tl(CTRDATA_TYPE_DIRECT_CALL); + gen_helper_ctr_add_entry(tcg_env, cpu_pc, addr, type); + } else { + TCGv type =3D tcg_constant_tl(CTRDATA_TYPE_DIRECT_JUMP); + gen_helper_ctr_add_entry(tcg_env, cpu_pc, addr, type); + } + } +#endif + + tcg_gen_mov_tl(cpu_pc, addr); =20 tcg_gen_lookup_and_goto_ptr(); diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index ce1256f439c69c32335193dcdcb98f0c38b10b39..5a99c47b128caf847df76cd1b53= 4f9fdf15a28a8 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -270,6 +270,8 @@ target_ulong helper_sret(CPURISCVState *env) { uint64_t mstatus; target_ulong prev_priv, prev_virt =3D env->virt_enabled; + const target_ulong src_priv =3D env->priv; + const bool src_virt =3D env->virt_enabled; =20 if (!(env->priv >=3D PRV_S)) { riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); @@ -339,6 +341,11 @@ target_ulong helper_sret(CPURISCVState *env) } env->mstatus =3D set_field(env->mstatus, MSTATUS_SPELP, 0); =20 + if (riscv_cpu_cfg(env)->ext_smctr || riscv_cpu_cfg(env)->ext_ssctr) { + riscv_ctr_add_entry(env, env->pc, retpc, CTRDATA_TYPE_EXCEP_INT_RE= T, + src_priv, src_virt); + } + return retpc; } =20 @@ -416,6 +423,11 @@ target_ulong helper_mret(CPURISCVState *env) } env->mstatus =3D set_field(env->mstatus, MSTATUS_MPELP, 0); =20 + if (riscv_cpu_cfg(env)->ext_smctr || riscv_cpu_cfg(env)->ext_ssctr) { + riscv_ctr_add_entry(env, env->pc, retpc, CTRDATA_TYPE_EXCEP_INT_RE= T, + PRV_M, false); + } + return retpc; } =20 @@ -466,6 +478,13 @@ target_ulong helper_mnret(CPURISCVState *env) return retpc; } =20 +void helper_ctr_add_entry(CPURISCVState *env, target_ulong src, + target_ulong dest, target_ulong type) +{ + riscv_ctr_add_entry(env, src, dest, (enum CTRType)type, + env->priv, env->virt_enabled); +} + void helper_wfi(CPURISCVState *env) { CPUState *cs =3D env_cpu(env); diff --git a/target/riscv/translate.c b/target/riscv/translate.c index 698b74f7a8fab0b55616dedb06e46805b34a3743..eaa5d86eaeedd53b2bc27142124= 129b0fb23674f 100644 --- a/target/riscv/translate.c +++ b/target/riscv/translate.c @@ -561,6 +561,46 @@ static void gen_set_fpr_d(DisasContext *ctx, int reg_n= um, TCGv_i64 t) } } =20 +#ifndef CONFIG_USER_ONLY +/* + * Direct calls + * - jal x1; + * - jal x5; + * - c.jal. + * - cm.jalt. + * + * Direct jumps + * - jal x0; + * - c.j; + * - cm.jt. + * + * Other direct jumps + * - jal rd where rd !=3D x1 and rd !=3D x5 and rd !=3D x0; + */ +static void gen_ctr_jal(DisasContext *ctx, int rd, target_ulong imm) +{ + TCGv dest =3D tcg_temp_new(); + TCGv src =3D tcg_temp_new(); + TCGv type; + + /* + * If rd is x1 or x5 link registers, treat this as direct call otherwi= se + * its a direct jump. + */ + if (rd =3D=3D 1 || rd =3D=3D 5) { + type =3D tcg_constant_tl(CTRDATA_TYPE_DIRECT_CALL); + } else if (rd =3D=3D 0) { + type =3D tcg_constant_tl(CTRDATA_TYPE_DIRECT_JUMP); + } else { + type =3D tcg_constant_tl(CTRDATA_TYPE_OTHER_DIRECT_JUMP); + } + + gen_pc_plus_diff(dest, ctx, imm); + gen_pc_plus_diff(src, ctx, 0); + gen_helper_ctr_add_entry(tcg_env, src, dest, type); +} +#endif + static void gen_jal(DisasContext *ctx, int rd, target_ulong imm) { TCGv succ_pc =3D dest_gpr(ctx, rd); @@ -575,6 +615,12 @@ static void gen_jal(DisasContext *ctx, int rd, target_= ulong imm) } } =20 +#ifndef CONFIG_USER_ONLY + if (ctx->cfg_ptr->ext_smctr || ctx->cfg_ptr->ext_ssctr) { + gen_ctr_jal(ctx, rd, imm); + } +#endif + gen_pc_plus_diff(succ_pc, ctx, ctx->cur_insn_len); gen_set_gpr(ctx, rd, succ_pc); =20 --=20 2.34.1