From nobody Sun Nov 24 02:34:05 2024 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=1727980695; cv=none; d=zohomail.com; s=zohoarc; b=FNT0s/B1nsV3yAXxDMgf3frC9LgDQ4df0wT3CMT+B+EVxncVT//5f1Z/J3w8ahyJWPqfYv32OkfBdM1VEp8niCK/cz70bI5WgxJkJhqb96L6ddQNXCnaYiUZFTL87XQuc1Ne6MeBxpElzfUuBsAptY8wnad3HyQcx1qqRXvEHSA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1727980695; h=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=4w8EQzzS8x/AP9HynEBDyPrYuUiwLotAgsmDd3Ae1DI=; b=PE81BgnZ4RbrWmFgUNsdCXHBLO3ThiPj4bK5siWCpv80Ox3UdEEJZZZd2bfZD6ystPM4cxpMvZYPwiLZb68zcVXN/7pZVHhDTxy/wzFHJigJup5v83aknZtLeIhGXKbK+/bqsnNtZjVCHznThuB9jnqtQsgOr8s87v+oXSCZfQo= 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 1727980695068759.9420755612518; Thu, 3 Oct 2024 11:38:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1swQeq-0007Ug-8h; Thu, 03 Oct 2024 14:34:36 -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 1swQeM-0007Ew-Af for qemu-devel@nongnu.org; Thu, 03 Oct 2024 14:34:07 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1swQeE-00051s-MU for qemu-devel@nongnu.org; Thu, 03 Oct 2024 14:34:05 -0400 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2e09fe0a878so1000687a91.1 for ; Thu, 03 Oct 2024 11:33:57 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20beefad16asm11796245ad.193.2024.10.03.11.33.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Oct 2024 11:33:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1727980436; x=1728585236; 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=4w8EQzzS8x/AP9HynEBDyPrYuUiwLotAgsmDd3Ae1DI=; b=iUvSL0AKdIgkF6X4AmhkL3s0V9pEWJme4D3/sj4VeXya1EA5/kVMitPJJ8JURrOj3i j7jaMYC8yqNyWedMVo09bg2LbyJ4znx+QHOzzX2iq8L5QqqeqfBELiI859M/ufauWCjc /pIqXdlvYAFKG9Xo/mKfFBrPK2k6WVjtfj4QyyHqhqJ+ZoWCv92pFot9XYtI3s9qKX7h KkwQxz4nUhh1vKDcEftdkoqGDO67p4EN9gQXlIIAIHIjuJSXrgL8k6HunnXkiD3X9sOj AX7/33XuN7lldT/DNz0W2FZqjRhkNg9V5H6Nxi7B0TImpkO9pDxX9kTJDxVpvC9iW9ak SkRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727980436; x=1728585236; 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=4w8EQzzS8x/AP9HynEBDyPrYuUiwLotAgsmDd3Ae1DI=; b=VH30y5U7i8bf5toFtccpJfDUbPDxCTc6eXv78LOGsYdLwl1tyP7gCre/5xVNqI4XuB DJfUZlaGhVrJy4Kbrs1NiQb9XPgvf5HaodtbcEUTrCUJGNY8uLjzV/F91vDvoVewwd9n P0N4pgzAaXPw9pd9M5ncW78usUzgfUm9lwPV0xO0ey7e/qVNtXmMl0aOTKZ9YCVtbEm8 nLbyq/KIOa0bvTl5kjAnVV3jy45LiRSXDSy1nY9bE8iECuT69E9nuehSy8yD9AJvxYio JZcpzMSh0j6IhNRH59bpCxsP6DOZZLhzUFnWbW5IwHv8iX4r/c9pwxI2LGPtwW8gpHpj YR+Q== X-Forwarded-Encrypted: i=1; AJvYcCWYcIGtmcAOUkLzE1l93Pdb4ZXQ3EDty4WpcFoTS4YYcesfqXnRO1ElUjS0lLYbB6sU0A7vzQyGd8lB@nongnu.org X-Gm-Message-State: AOJu0YyUwfJYlVt3+mJf4kveiNGR95mHvlG17Dna2tXGtvvZui0kk6EY dceC3khJPWBu5WAAeLg7hbd/m3UeG9m9P8ajyDMNU3l4xGQmAzI+r5EiSsdHeCA= X-Google-Smtp-Source: AGHT+IE7Br4IvG1fqC99jOJ9z03th4tLDNyAxF/XT/j6ylDS6cA51e9O7T6Q9OlPRJtq3dWxG0UT6w== X-Received: by 2002:a17:90a:71c4:b0:2e0:8733:6c78 with SMTP id 98e67ed59e1d1-2e1e62283d1mr34295a91.15.1727980435955; Thu, 03 Oct 2024 11:33:55 -0700 (PDT) From: Deepak Gupta To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, Alistair.Francis@wdc.com, bmeng.cn@gmail.com, liwei1518@gmail.com, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, jim.shu@sifive.com, kito.cheng@sifive.com, Deepak Gupta , Andy Chiu , Richard Henderson , Alistair Francis Subject: [PATCH v15 07/21] target/riscv: zicfilp `lpad` impl and branch tracking Date: Thu, 3 Oct 2024 11:33:28 -0700 Message-ID: <20241003183342.679249-8-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241003183342.679249-1-debug@rivosinc.com> References: <20241003183342.679249-1-debug@rivosinc.com> MIME-Version: 1.0 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=2607:f8b0:4864:20::1031; envelope-from=debug@rivosinc.com; helo=mail-pj1-x1031.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=unavailable 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: 1727980695935116600 Content-Type: text/plain; charset="utf-8" Implements setting lp expected when `jalr` is encountered and implements `lpad` instruction of zicfilp. `lpad` instruction is taken out of auipc x0, . This is an existing HINTNOP space. If `lpad` is target of an indirect branch, cpu checks for 20 bit value in x7 upper with 20 bit value embedded in `lpad`. If they don't match, cpu raises a sw check exception with tval =3D 2. Signed-off-by: Deepak Gupta Co-developed-by: Jim Shu Co-developed-by: Andy Chiu Reviewed-by: Richard Henderson Reviewed-by: Alistair Francis --- target/riscv/cpu_user.h | 1 + target/riscv/insn32.decode | 5 ++- target/riscv/insn_trans/trans_rvi.c.inc | 55 +++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu_user.h b/target/riscv/cpu_user.h index 02afad608b..e6927ff847 100644 --- a/target/riscv/cpu_user.h +++ b/target/riscv/cpu_user.h @@ -15,5 +15,6 @@ #define xA6 16 #define xA7 17 /* syscall number for RVI ABI */ #define xT0 5 /* syscall number for RVE ABI */ +#define xT2 7 =20 #endif diff --git a/target/riscv/insn32.decode b/target/riscv/insn32.decode index c45b8fa1d8..27108b992b 100644 --- a/target/riscv/insn32.decode +++ b/target/riscv/insn32.decode @@ -123,7 +123,10 @@ sfence_vm 0001000 00100 ..... 000 00000 1110011 @= sfence_vm =20 # *** RV32I Base Instruction Set *** lui .................... ..... 0110111 @u -auipc .................... ..... 0010111 @u +{ + lpad label:20 00000 0010111 + auipc .................... ..... 0010111 @u +} jal .................... ..... 1101111 @j jalr ............ ..... 000 ..... 1100111 @i beq ....... ..... ..... 000 ..... 1100011 @b diff --git a/target/riscv/insn_trans/trans_rvi.c.inc b/target/riscv/insn_tr= ans/trans_rvi.c.inc index fab5c06719..638fc0fb7b 100644 --- a/target/riscv/insn_trans/trans_rvi.c.inc +++ b/target/riscv/insn_trans/trans_rvi.c.inc @@ -36,6 +36,49 @@ static bool trans_lui(DisasContext *ctx, arg_lui *a) return true; } =20 +static bool trans_lpad(DisasContext *ctx, arg_lpad *a) +{ + /* + * fcfi_lp_expected can set only if fcfi was eanbled. + * translate further only if fcfi_lp_expected set. + * lpad comes from NOP space anyways, so return true if + * fcfi_lp_expected is false. + */ + if (!ctx->fcfi_lp_expected) { + return true; + } + + ctx->fcfi_lp_expected =3D false; + if ((ctx->base.pc_next) & 0x3) { + /* + * misaligned, according to spec we should raise sw check exception + */ + tcg_gen_st_tl(tcg_constant_tl(RISCV_EXCP_SW_CHECK_FCFI_TVAL), + tcg_env, offsetof(CPURISCVState, sw_check_code)); + gen_helper_raise_exception(tcg_env, + tcg_constant_i32(RISCV_EXCP_SW_CHECK)); + return true; + } + + /* per spec, label check performed only when embedded label non-zero */ + if (a->label !=3D 0) { + TCGLabel *skip =3D gen_new_label(); + TCGv tmp =3D tcg_temp_new(); + tcg_gen_extract_tl(tmp, get_gpr(ctx, xT2, EXT_NONE), 12, 20); + tcg_gen_brcondi_tl(TCG_COND_EQ, tmp, a->label, skip); + tcg_gen_st_tl(tcg_constant_tl(RISCV_EXCP_SW_CHECK_FCFI_TVAL), + tcg_env, offsetof(CPURISCVState, sw_check_code)); + gen_helper_raise_exception(tcg_env, + tcg_constant_i32(RISCV_EXCP_SW_CHECK)); + gen_set_label(skip); + } + + tcg_gen_st8_tl(tcg_constant_tl(0), tcg_env, + offsetof(CPURISCVState, elp)); + + return true; +} + static bool trans_auipc(DisasContext *ctx, arg_auipc *a) { TCGv target_pc =3D dest_gpr(ctx, a->rd); @@ -75,6 +118,18 @@ static bool trans_jalr(DisasContext *ctx, arg_jalr *a) gen_set_gpr(ctx, a->rd, succ_pc); =20 tcg_gen_mov_tl(cpu_pc, target_pc); + if (ctx->fcfi_enabled) { + /* + * return from functions (i.e. rs1 =3D=3D xRA || rs1 =3D=3D xT0) a= re not + * tracked. zicfilp introduces sw guarded branch as well. sw guard= ed + * branch are not tracked. rs1 =3D=3D xT2 is a sw guarded branch. + */ + if (a->rs1 !=3D xRA && a->rs1 !=3D xT0 && a->rs1 !=3D xT2) { + tcg_gen_st8_tl(tcg_constant_tl(1), + tcg_env, offsetof(CPURISCVState, elp)); + } + } + lookup_and_goto_ptr(ctx); =20 if (misaligned) { --=20 2.45.0