From patchwork Thu Jan 16 00:35:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Monk Chiang X-Patchwork-Id: 104870 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 648A4385E01D for ; Thu, 16 Jan 2025 00:38:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 648A4385E01D Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=OjQKozl5 X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by sourceware.org (Postfix) with ESMTPS id CCFE73857BA0 for ; Thu, 16 Jan 2025 00:35:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CCFE73857BA0 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CCFE73857BA0 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::631 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736987732; cv=none; b=M0W3csL6hRh5m5PxlWPbbLnkwVsDQPDyGIVYRkXOoCXfGGoMXp0CK5Jv64FMxJAsV/Ui9NRziyAbv2UIcXGrhsgUXxzPVScnp/Qv1aFZHhOriUdX2Lb3Gsra+1bcIcja9VideHlCAKifUlpq9w3kQeVfMoEcTnRB3nJ3dGA3fuU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736987732; c=relaxed/simple; bh=trgdyIl7080Qx/W4mpDRYZ76cAd/7wv+0SLHd471cR0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=H9NGtE3uOF47YWhPEXWICLhOhetAaedTi054ca7oywOUK8BV9PEPpJ6WfMs0tTOAbXU5cmI9SiTRkeyEDcKBJWf9jUz9uVF9XTohxIoQxcpYUJZUeD123ocsl4GQL6IE6iy74HMbKw8OE8UNYk4ZUGl4qa2xKC+P/6FX9C6Jx0E= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CCFE73857BA0 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-216728b1836so4795715ad.0 for ; Wed, 15 Jan 2025 16:35:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1736987731; x=1737592531; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=bRZMO10pBQf2S+j7n7cUelI3itW+AshM/rwLBUbH+dw=; b=OjQKozl5L4LsMFp0TFjI75FNAdbd+gH2QEGR9+j0S2gxqUEVIpW4nHTAS5WYEjMKDS CLehRW/2vgebPYKy9f7gQgP634KDketWohqFhVCcWAaw/mz3KXvGxIP9gFnQPMMBsoPA aOYaVCbJjT//FUdt4CQ9WbfAOFMmKqDn4Uft+HaBCif4aJGDSPbhPjcTmzqwbIFF3s6P sHYf5n9XzYpy99LDdEgwauQlA+q7OcprgjqnaBWG9hlis5X+Erv2vt7MWyrvji6LJzdX VkXd6kv5p96VcF9v7CaLcRuwdinrJJtyZbYr6u8KIyZgfeyWTRGB2wqxkPfurOA+m/yW sb9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736987731; x=1737592531; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bRZMO10pBQf2S+j7n7cUelI3itW+AshM/rwLBUbH+dw=; b=Nsr4+2hYtF0MvRGECh9WNWFub+28EuldbsFx7fwdsGQ31M8Pt96GRYKrMSrMoc37e4 E9ZKBK4zxMCfY4Bojlp/r1TlY/TDOZEJvPTEBqn7JzXo2/9WYZW87iA+DcbpmuoKs1mU 1V/MpO6r7LUN0a42rMZH2hDHa4wv5gCLnJP6dVG8u7X2jIVp463WLDAnv4a7glAzEtz8 LoKcXqhcciTSW8sClFgjYbtz58lHHEcUEnlkPkPAo/EC0hXKNrCVW8OJzDc4xEvsr4by DflB2tUvSKOIXgIGQ9hNVesMzwLy5dsnRUlyLOqczXLDbZJgk9HlhTrjVfjcDumzjjem tBtg== X-Gm-Message-State: AOJu0YybTJecN9SH6xmzeuiLqBcfmEww2RpmEFKftZJqnZWa7Ljf5Q4H SIJX+ynd4c/5kTV3EnH65vmBZIgnc4oDpBBXD6fBadDLKQjz3fxDNYmjODQyb8507d1xfRatPYj 0mWsOnW61dJH+xdU+WyJZaTcoCU6t6zqIUslgCgLd1bl3bRJH7shTqsmaPpCGi5oPYJcUnyNARf Cy15O+kekOX9NpINshTXhNCMVvqcDvySW5u/gKce9CSg== X-Gm-Gg: ASbGnctWIyejpvjf3kfnqgJ9WwC2Dyl/CNn63CQ73Bmt2Sr2DBDFRlSqayHk9Q+CTBE tmCRcshnqGmctnlaB5V36XzAMVSgLnsBff6xNGO8p/mKUEz52EBqrjg6zIZ9AfCKG0dj2R2x9Ar MZQ6J8BkzcFZszYPFZU1Obi+yuPN7dgLnz+zP7xHsgj5c25KacMEbuLd6L7CJlo0ahhIs+1/4SG QcpEwHW3566WYBoGplf/R/bazBoy/07pwYWcYhfzD5mqstdezop58UYQlcxKOu+TQ1BmruUHoYH V6ALMnPp X-Google-Smtp-Source: AGHT+IHZ7CLlySfYsQlw5G+HrnIWZ2CM30kYk66DOiUtkFF8ZdyHJWNE4xndVYbYaDHq1sxfkoFscA== X-Received: by 2002:a17:902:d4d2:b0:215:9642:4d6d with SMTP id d9443c01a7336-21a83f55157mr452116255ad.17.1736987730213; Wed, 15 Jan 2025 16:35:30 -0800 (PST) Received: from hsinchu02.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f2538e8sm86869125ad.218.2025.01.15.16.35.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 16:35:29 -0800 (PST) From: Monk Chiang To: gcc-patches@gcc.gnu.org, kito@gcc.gnu.org Cc: Monk Chiang , Greg McGary Subject: [PATCH v4 1/4] RISC-V: Add Zicfiss ISA extension. Date: Thu, 16 Jan 2025 08:35:20 +0800 Message-ID: <20250116003524.84399-1-monk.chiang@sifive.com> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org This patch is implemented according to the RISC-V CFI specification. It supports the generation of shadow stack instructions in the prologue, epilogue, non-local gotos, and unwinding. RISC-V CFI SPEC: https://github.com/riscv/riscv-cfi gcc/ChangeLog: * common/config/riscv/riscv-common.cc: Add ZICFISS ISA string. * gcc/config/riscv/predicates.md: New predicate x1x5_operand. * gcc/config/riscv/riscv.cc (riscv_expand_prologue): Insert shadow stack instructions. (riscv_expand_epilogue): Likewise. (riscv_for_each_saved_reg): Assign t0 or ra register for sspopchk instruction. (need_shadow_stack_push_pop_p): New function. Omit shadow stack operation on leaf function. * gcc/config/riscv/riscv.h (need_shadow_stack_push_pop_p): Define. * gcc/config/riscv/riscv.md: Add shadow stack patterns. (save_stack_nonlocal): Add shadow stack instructions for setjump. (restore_stack_nonlocal): Add shadow stack instructions for longjump. libgcc/ChangeLog: * gcc/config/riscv/riscv.opt (TARGET_ZICFISS): Define. * libgcc/config/riscv/linux-unwind.h: Include shadow-stack-unwind.h. * libgcc/config/riscv/shadow-stack-unwind.h (_Unwind_Frames_Extra): Define. (_Unwind_Frames_Increment): Define. gcc/testsuite/ChangeLog: * gcc/testsuite/gcc.target/riscv/ssp-1.c: New test. * gcc/testsuite/gcc.target/riscv/ssp-2.c: New test. Co-Developed-by: Greg McGary , Kito Cheng --- gcc/common/config/riscv/riscv-common.cc | 7 ++ gcc/config/riscv/predicates.md | 6 ++ gcc/config/riscv/riscv.cc | 58 ++++++++-- gcc/config/riscv/riscv.h | 1 + gcc/config/riscv/riscv.md | 125 +++++++++++++++++++++- gcc/config/riscv/riscv.opt | 2 + gcc/testsuite/gcc.target/riscv/ssp-1.c | 41 +++++++ gcc/testsuite/gcc.target/riscv/ssp-2.c | 10 ++ libgcc/config/riscv/linux-unwind.h | 5 + libgcc/config/riscv/shadow-stack-unwind.h | 74 +++++++++++++ 10 files changed, 320 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.target/riscv/ssp-1.c create mode 100644 gcc/testsuite/gcc.target/riscv/ssp-2.c create mode 100644 libgcc/config/riscv/shadow-stack-unwind.h diff --git a/gcc/common/config/riscv/riscv-common.cc b/gcc/common/config/riscv/riscv-common.cc index bfc8aa559c5..8e8b6107a6d 100644 --- a/gcc/common/config/riscv/riscv-common.cc +++ b/gcc/common/config/riscv/riscv-common.cc @@ -111,6 +111,9 @@ static const riscv_implied_info_t riscv_implied_info[] = {"zfinx", "zicsr"}, {"zdinx", "zicsr"}, + {"zicfiss", "zicsr"}, + {"zicfiss", "zimop"}, + {"zk", "zkn"}, {"zk", "zkr"}, {"zk", "zkt"}, @@ -325,6 +328,8 @@ static const struct riscv_ext_version riscv_ext_version_table[] = {"zicclsm", ISA_SPEC_CLASS_NONE, 1, 0}, {"ziccrse", ISA_SPEC_CLASS_NONE, 1, 0}, + {"zicfiss", ISA_SPEC_CLASS_NONE, 1, 0}, + {"zimop", ISA_SPEC_CLASS_NONE, 1, 0}, {"zcmop", ISA_SPEC_CLASS_NONE, 1, 0}, @@ -1647,6 +1652,8 @@ static const riscv_ext_flag_table_t riscv_ext_flag_table[] = RISCV_EXT_FLAG_ENTRY ("zicbop", x_riscv_zicmo_subext, MASK_ZICBOP), RISCV_EXT_FLAG_ENTRY ("zic64b", x_riscv_zicmo_subext, MASK_ZIC64B), + RISCV_EXT_FLAG_ENTRY ("zicfiss", x_riscv_zi_subext, MASK_ZICFISS), + RISCV_EXT_FLAG_ENTRY ("zimop", x_riscv_mop_subext, MASK_ZIMOP), RISCV_EXT_FLAG_ENTRY ("zcmop", x_riscv_mop_subext, MASK_ZCMOP), diff --git a/gcc/config/riscv/predicates.md b/gcc/config/riscv/predicates.md index cda7502a62a..1f67d30be9d 100644 --- a/gcc/config/riscv/predicates.md +++ b/gcc/config/riscv/predicates.md @@ -679,3 +679,9 @@ return (riscv_symbolic_constant_p (op, &type) && type == SYMBOL_PCREL); }) + +;; Shadow stack operands only allow x1, x5 registers +(define_predicate "x1x5_operand" + (and (match_operand 0 "register_operand") + (match_test "REGNO (op) == RETURN_ADDR_REGNUM + || REGNO (op) == T0_REGNUM"))) diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 65e09842fde..cd37b492183 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -7496,6 +7496,9 @@ riscv_save_reg_p (unsigned int regno) if (regno == GP_REGNUM || regno == THREAD_POINTER_REGNUM) return false; + if (regno == RETURN_ADDR_REGNUM && TARGET_ZICFISS) + return true; + /* We must save every register used in this function. If this is not a leaf function, then we must save all temporary registers. */ if (df_regs_ever_live_p (regno) @@ -8049,7 +8052,7 @@ riscv_is_eh_return_data_register (unsigned int regno) static void riscv_for_each_saved_reg (poly_int64 sp_offset, riscv_save_restore_fn fn, - bool epilogue, bool maybe_eh_return) + bool epilogue, bool maybe_eh_return, bool sibcall_p) { HOST_WIDE_INT offset, first_fp_offset; unsigned int regno, num_masked_fp = 0; @@ -8135,7 +8138,14 @@ riscv_for_each_saved_reg (poly_int64 sp_offset, riscv_save_restore_fn fn, } } - riscv_save_restore_reg (word_mode, regno, offset, fn); + if (need_shadow_stack_push_pop_p () && epilogue && !sibcall_p + && !(maybe_eh_return && crtl->calls_eh_return) + && (regno == RETURN_ADDR_REGNUM) + && !cfun->machine->interrupt_handler_p) + riscv_save_restore_reg (word_mode, RISCV_PROLOGUE_TEMP_REGNUM, + offset, fn); + else + riscv_save_restore_reg (word_mode, regno, offset, fn); } /* This loop must iterate over the same space as its companion in @@ -8729,6 +8739,9 @@ riscv_expand_prologue (void) if (cfun->machine->naked_p) return; + if (need_shadow_stack_push_pop_p ()) + emit_insn (gen_sspush (Pmode, gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM))); + /* prefer multi-push to save-restore libcall. */ if (riscv_use_multi_push (frame)) { @@ -8772,7 +8785,7 @@ riscv_expand_prologue (void) = get_multi_push_fpr_mask (multi_push_additional / UNITS_PER_WORD); frame->fmask &= mask_fprs_push; riscv_for_each_saved_reg (remaining_size, riscv_save_reg, false, - false); + false, false); frame->fmask = fmask & ~mask_fprs_push; /* mask for the rest FPRs. */ } } @@ -8823,7 +8836,8 @@ riscv_expand_prologue (void) GEN_INT (-step1)); RTX_FRAME_RELATED_P (emit_insn (insn)) = 1; } - riscv_for_each_saved_reg (remaining_size, riscv_save_reg, false, false); + riscv_for_each_saved_reg (remaining_size, riscv_save_reg, + false, false, false); } /* Undo the above fib. */ @@ -8985,6 +8999,7 @@ riscv_expand_epilogue (int style) = use_multi_pop ? frame->multi_push_adj_base + frame->multi_push_adj_addi : 0; rtx ra = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM); + rtx t0 = gen_rtx_REG (Pmode, RISCV_PROLOGUE_TEMP_REGNUM); unsigned th_int_mask = 0; rtx insn; @@ -9194,7 +9209,8 @@ riscv_expand_epilogue (int style) riscv_for_each_saved_v_reg (step2, riscv_restore_reg, false); riscv_for_each_saved_reg (frame->total_size - step2 - libcall_size - multipop_size, - riscv_restore_reg, true, style == EXCEPTION_RETURN); + riscv_restore_reg, true, style == EXCEPTION_RETURN, + style == SIBCALL_RETURN); if (th_int_mask && TH_INT_INTERRUPT (cfun)) { @@ -9234,7 +9250,8 @@ riscv_expand_epilogue (int style) riscv_for_each_saved_reg (frame->total_size - libcall_size - multipop_size, riscv_restore_reg, true, - style == EXCEPTION_RETURN); + style == EXCEPTION_RETURN, false); + /* Undo the above fib. */ frame->mask = mask; frame->fmask = fmask; @@ -9259,6 +9276,17 @@ riscv_expand_epilogue (int style) emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx, EH_RETURN_STACKADJ_RTX)); + if (need_shadow_stack_push_pop_p () + && !((style == EXCEPTION_RETURN) && crtl->calls_eh_return)) + { + if (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM) + && style != SIBCALL_RETURN + && !cfun->machine->interrupt_handler_p) + emit_insn (gen_sspopchk (Pmode, t0)); + else + emit_insn (gen_sspopchk (Pmode, ra)); + } + /* Return from interrupt. */ if (cfun->machine->interrupt_handler_p) { @@ -9276,7 +9304,15 @@ riscv_expand_epilogue (int style) emit_jump_insn (gen_riscv_uret ()); } else if (style != SIBCALL_RETURN) - emit_jump_insn (gen_simple_return_internal (ra)); + { + if (need_shadow_stack_push_pop_p () + && !((style == EXCEPTION_RETURN) && crtl->calls_eh_return) + && BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM) + && !cfun->machine->interrupt_handler_p) + emit_jump_insn (gen_simple_return_internal (t0)); + else + emit_jump_insn (gen_simple_return_internal (ra)); + } } /* Implement EPILOGUE_USES. */ @@ -9473,7 +9509,8 @@ bool riscv_can_use_return_insn (void) { return (reload_completed && known_eq (cfun->machine->frame.total_size, 0) - && ! cfun->machine->interrupt_handler_p); + && ! cfun->machine->interrupt_handler_p + && ! need_shadow_stack_push_pop_p ()); } /* Given that there exists at least one variable that is set (produced) @@ -13786,6 +13823,11 @@ expand_reversed_crc_using_clmul (scalar_mode crc_mode, scalar_mode data_mode, riscv_emit_move (operands[0], gen_lowpart (crc_mode, a0)); } +bool need_shadow_stack_push_pop_p () +{ + return TARGET_ZICFISS && riscv_save_return_addr_reg_p (); +} + /* Initialize the GCC target structure. */ #undef TARGET_ASM_ALIGNED_HI_OP #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t" diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index 4ff883af16b..93e88fe885d 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -1189,6 +1189,7 @@ extern poly_int64 riscv_v_adjust_nunits (enum machine_mode, int); extern poly_int64 riscv_v_adjust_nunits (machine_mode, bool, int, int); extern poly_int64 riscv_v_adjust_precision (enum machine_mode, int); extern poly_int64 riscv_v_adjust_bytesize (enum machine_mode, int); +extern bool need_shadow_stack_push_pop_p (); /* The number of bits and bytes in a RVV vector. */ #define BITS_PER_RISCV_VECTOR (poly_uint16 (riscv_vector_chunks * riscv_bytes_per_vector_chunk * 8)) #define BYTES_PER_RISCV_VECTOR (poly_uint16 (riscv_vector_chunks * riscv_bytes_per_vector_chunk)) diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index 0922cab4402..21094395184 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -99,6 +99,12 @@ ;; CRC unspecs UNSPEC_CRC UNSPEC_CRC_REV + + ;; ZICFISS + UNSPECV_SSPUSH + UNSPECV_SSPOPCHK + UNSPECV_SSRDP + UNSPECV_SSP ]) (define_c_enum "unspecv" [ @@ -4116,6 +4122,30 @@ (set_attr "length" "0")] ) +(define_expand "save_stack_nonlocal" + [(set (match_operand 0 "memory_operand") + (match_operand 1 "register_operand"))] + "" +{ + rtx stack_slot; + + if (need_shadow_stack_push_pop_p ()) + { + /* Copy shadow stack pointer to the first slot + and stack pointer to the second slot. */ + rtx ssp_slot = adjust_address (operands[0], word_mode, 0); + stack_slot = adjust_address (operands[0], Pmode, UNITS_PER_WORD); + + rtx reg_ssp = force_reg (word_mode, const0_rtx); + emit_insn (gen_ssrdp (word_mode, reg_ssp)); + emit_move_insn (ssp_slot, reg_ssp); + } + else + stack_slot = adjust_address (operands[0], Pmode, 0); + emit_move_insn (stack_slot, operands[1]); + DONE; +}) + ;; This fixes a failure with gcc.c-torture/execute/pr64242.c at -O2 for a ;; 32-bit target when using -mtune=sifive-7-series. The first sched pass ;; runs before register elimination, and we have a non-obvious dependency @@ -4126,7 +4156,70 @@ (match_operand 1 "memory_operand")] "" { - emit_move_insn (operands[0], operands[1]); + rtx stack_slot; + + if (need_shadow_stack_push_pop_p ()) + { + rtx t0 = gen_rtx_REG (Pmode, RISCV_PROLOGUE_TEMP_REGNUM); + /* Restore shadow stack pointer from the first slot + and stack pointer from the second slot. */ + rtx ssp_slot = adjust_address (operands[1], word_mode, 0); + stack_slot = adjust_address (operands[1], Pmode, UNITS_PER_WORD); + + /* Get the current shadow stack pointer. */ + rtx cur_ssp = force_reg (word_mode, const0_rtx); + emit_insn (gen_ssrdp (word_mode, cur_ssp)); + + /* Compare and jump over adjustment code. */ + rtx noadj_label = gen_label_rtx (); + emit_cmp_and_jump_insns (cur_ssp, const0_rtx, EQ, NULL_RTX, + word_mode, 1, noadj_label); + + rtx loop_label = gen_label_rtx (); + emit_label (loop_label); + LABEL_NUSES (loop_label) = 1; + + /* Check if current ssp less than jump buffer ssp, + so no loop is needed. */ + emit_cmp_and_jump_insns (ssp_slot, cur_ssp, LE, NULL_RTX, + ptr_mode, 1, noadj_label); + + /* Advance by a maximum of 4K at a time to avoid unwinding + past bounds of the shadow stack. */ + rtx reg_4096 = force_reg (word_mode, GEN_INT (4096)); + rtx cmp_ssp = gen_reg_rtx (word_mode); + cmp_ssp = expand_simple_binop (ptr_mode, MINUS, + ssp_slot, cur_ssp, + cmp_ssp, 1, OPTAB_DIRECT); + + /* Update curr_ssp from jump buffer ssp. */ + emit_move_insn (cur_ssp, ssp_slot); + emit_insn (gen_write_ssp (word_mode, cur_ssp)); + emit_jump_insn (gen_jump (loop_label)); + emit_barrier (); + + /* Adjust the ssp in a loop. */ + rtx cmp_4k_label = gen_label_rtx (); + emit_label (cmp_4k_label); + LABEL_NUSES (cmp_4k_label) = 1; + + /* Add 4k for curr_ssp. */ + cur_ssp = expand_simple_binop (ptr_mode, PLUS, + cur_ssp, reg_4096, + cur_ssp, 1, OPTAB_DIRECT); + emit_insn (gen_write_ssp (word_mode, cur_ssp)); + emit_insn (gen_sspush (Pmode, t0)); + emit_insn (gen_sspopchk (Pmode, t0)); + emit_jump_insn (gen_jump (loop_label)); + emit_barrier (); + + emit_label (noadj_label); + LABEL_NUSES (noadj_label) = 1; + } + else + stack_slot = adjust_address (operands[1], Pmode, 0); + + emit_move_insn (operands[0], stack_slot); /* Prevent the following hard fp restore from being moved before the move insn above which uses a copy of the soft fp reg. */ emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx)); @@ -4594,6 +4687,36 @@ }" [(set_attr "type" "arith")]) +;; Shadow stack + +(define_insn "@sspush" + [(unspec_volatile [(match_operand:P 0 "x1x5_operand" "r")] UNSPECV_SSPUSH)] + "TARGET_ZICFISS" + "sspush\t%0" + [(set_attr "type" "arith") + (set_attr "mode" "")]) + +(define_insn "@sspopchk" + [(unspec_volatile [(match_operand:P 0 "x1x5_operand" "r")] UNSPECV_SSPOPCHK)] + "TARGET_ZICFISS" + "sspopchk\t%0" + [(set_attr "type" "arith") + (set_attr "mode" "")]) + +(define_insn "@ssrdp" + [(set (match_operand:P 0 "register_operand" "=r") + (unspec_volatile [(const_int 0)] UNSPECV_SSRDP))] + "TARGET_ZICFISS" + "ssrdp\t%0" + [(set_attr "type" "arith") + (set_attr "mode" "")]) + +(define_insn "@write_ssp" + [(unspec_volatile [(match_operand:P 0 "register_operand" "r")] UNSPECV_SSP)] + "TARGET_ZICFISS" + "csrw\tssp, %0" + [(set_attr "type" "arith") + (set_attr "mode" "")]) (include "bitmanip.md") (include "crypto.md") diff --git a/gcc/config/riscv/riscv.opt b/gcc/config/riscv/riscv.opt index 88fec1c8280..4a43bfd7bc6 100644 --- a/gcc/config/riscv/riscv.opt +++ b/gcc/config/riscv/riscv.opt @@ -253,6 +253,8 @@ Mask(ZICCLSM) Var(riscv_zi_subext) Mask(ZICCRSE) Var(riscv_zi_subext) +Mask(ZICFISS) Var(riscv_zi_subext) + TargetVariable int riscv_za_subext diff --git a/gcc/testsuite/gcc.target/riscv/ssp-1.c b/gcc/testsuite/gcc.target/riscv/ssp-1.c new file mode 100644 index 00000000000..abf47ec6442 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/ssp-1.c @@ -0,0 +1,41 @@ +/* { dg-do compile { target { riscv64*-*-* } } } */ +/* { dg-options "-O2 -march=rv64gc_zicfiss -mabi=lp64d" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" } } */ +struct ad { + void *ae; +}; +struct af { + union { + int *ai; + int *aj; + struct ad *ak; + } u; + struct { + struct { + long al : 1; + long am : 1; + long : 21; + } b; + long i; + } s; +}; +void fdes (struct af *, void *, long *); + +void foo (struct af *bv, long *bw) { + bw[0] = bw[1] = 0; + if (bv->s.b.al) + fdes (bv, bv->u.ak->ae, bw); + else if (bv->s.b.am) { + int **p = (int**)bv->u.aj; + for (; *p; ++p) + fdes (bv, *p, bw); + } else + fdes (bv, bv->u.ai, bw); +} + +/* { dg-final { scan-assembler-times "ld\tt0" 1 } } */ +/* { dg-final { scan-assembler-times "sspopchk\tt0" 1 } } */ +/* { dg-final { scan-assembler-times "jr\tt0" 1 } } */ +/* { dg-final { scan-assembler-times "ld\tra" 2 } } */ +/* { dg-final { scan-assembler-times "sspopchk\tra" 2 } } */ +/* { dg-final { scan-assembler-times "tail" 2 } } */ diff --git a/gcc/testsuite/gcc.target/riscv/ssp-2.c b/gcc/testsuite/gcc.target/riscv/ssp-2.c new file mode 100644 index 00000000000..7c6098357e9 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/ssp-2.c @@ -0,0 +1,10 @@ +/* { dg-do compile { target { riscv64*-*-* } } } */ +/* { dg-options "-O0 -march=rv64gc_zicfiss -mabi=lp64d" } */ + +void __attribute__ ((interrupt)) +foo (void) +{ +} +/* { dg-final { scan-assembler-times "sd\tra" 1 } } */ +/* { dg-final { scan-assembler-times "ld\tra" 1 } } */ +/* { dg-final { scan-assembler-times "sspopchk\tra" 1 } } */ diff --git a/libgcc/config/riscv/linux-unwind.h b/libgcc/config/riscv/linux-unwind.h index 40c8f51d6fc..db755cdc6a8 100644 --- a/libgcc/config/riscv/linux-unwind.h +++ b/libgcc/config/riscv/linux-unwind.h @@ -19,6 +19,11 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see . */ +/* Unwind shadow stack. */ +#if defined(__riscv_zicfiss) +# include "config/riscv/shadow-stack-unwind.h" +#endif + #ifndef inhibit_libc #include diff --git a/libgcc/config/riscv/shadow-stack-unwind.h b/libgcc/config/riscv/shadow-stack-unwind.h new file mode 100644 index 00000000000..a978a54a4ca --- /dev/null +++ b/libgcc/config/riscv/shadow-stack-unwind.h @@ -0,0 +1,74 @@ +/* _Unwind_Frames_Extra with shadow stack. + Copyright (C) 2016-2025 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +Under Section 7 of GPL version 3, you are granted additional +permissions described in the GCC Runtime Library Exception, version +3.1, as published by the Free Software Foundation. + +You should have received a copy of the GNU General Public License and +a copy of the GCC Runtime Library Exception along with this program; +see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +. */ + +#define LIBGCC2_UNITS_PER_WORD (__riscv_xlen / 8) + +/* Unwind the shadow stack for EH. */ +#undef _Unwind_Frames_Extra +#define _Unwind_Frames_Extra(x) \ + do \ + { \ + _Unwind_Word ssp = 0; \ + asm volatile ("ssrdp %0" : "=r"(ssp)); \ + if (ssp != 0) \ + { \ + _Unwind_Word tmp = (x); \ + tmp = tmp * LIBGCC2_UNITS_PER_WORD; \ + while (tmp > 4096) \ + { \ + ssp += 4096; \ + tmp -= 4096; \ + asm volatile ("csrw ssp, %0" :: "r"(ssp)); \ + asm volatile ("sspush x5\n" \ + "sspopchk x5" : : : "x5"); \ + } \ + \ + if (tmp > 0) \ + { \ + ssp += tmp; \ + asm volatile ("csrw ssp, %0" :: "r"(ssp)); \ + } \ + } \ + } \ + while (0) + +#undef _Unwind_Frames_Increment +#define _Unwind_Frames_Increment(exc, context, frames) \ + { \ + frames++; \ + if (exc->exception_class != 0 \ + && _Unwind_GetIP (context) != 0 \ + && !_Unwind_IsSignalFrame (context)) \ + { \ + _Unwind_Word ssp; \ + asm volatile ("ssrdp %0" : "=r"(ssp)); \ + if (ssp != 0) \ + { \ + ssp += LIBGCC2_UNITS_PER_WORD * frames; \ + _Unwind_Word ra = *(_Unwind_Word *) ssp; \ + if (ra != _Unwind_GetIP (context)) \ + return _URC_FATAL_PHASE2_ERROR; \ + } \ + } \ + } From patchwork Thu Jan 16 00:35:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Monk Chiang X-Patchwork-Id: 104869 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8185E3857BA0 for ; Thu, 16 Jan 2025 00:38:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8185E3857BA0 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=BxRQqMdo X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by sourceware.org (Postfix) with ESMTPS id 958A7385780C for ; Thu, 16 Jan 2025 00:35:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 958A7385780C Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 958A7385780C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736987735; cv=none; b=Ypp2S8fG5BggSwc/6GXB8P2yMgAtm0we8ZmRrDklGmseRsmBVYlcIHEfQMNrFuQHeYCnHhAo6pcA8aqfVWQCCWn7fwZY3ZcmPQZfcsydyDQyEnCCjuJPLJwnHKRl/mxSwCqQTdDCnrVL6K+fTa9+j1bj694oW13SrmqijAbSmRQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736987735; c=relaxed/simple; bh=Gen8harlE2py5S0ruhWd7OzdQm86SFndrueBe6rrZO4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=IhEz+LMsZHcpkw/3nLK1Lo20Zcade6KJljaVYJPXXR3tjpNe/Bh7vZnFAy6cQbG7dAINttzRNZJdQfjflE1Feo/XldtUcBWY0oZlv2N/J2XYkDq/8pyZIxc6wnQaPRqEEI8UxbRVERU7RXPYV/LJAB08U/NHTglnAwKfG4Xl+/w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 958A7385780C Received: by mail-pj1-x102c.google.com with SMTP id 98e67ed59e1d1-2ee76befe58so693822a91.2 for ; Wed, 15 Jan 2025 16:35:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1736987734; x=1737592534; darn=gcc.gnu.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=O/IJO6oMsjj2D01adPd3wW0Te2i4B04kGMyoTiazK0U=; b=BxRQqMdoTT2yUCKCipq3r5mc548nwf7pkGW5EFTUWi+8/kGnA4/xCyQwbyS6Ur/o3/ 0aJR4S17os/STPMx9YxbroQaSgTg4vwAS/6ezLyPdA2Bz1/3vjP11rQWcyE2sNIjY4r2 2cvRjYITgUaHNHPpxs4GCb8vgR2hA+/9MB3OalkJPsZ20Y69oyBZIe05Hd/7H6fhcJ0n UjLgChIt2SBZXtvYkOVd1VA5hYDWByW/vE6UnnOjeXhBvUKlifw8FtgfJNyEm1rZkFXX LWmi61z+oljDiy2r0pJESqnz4sUWhi8cNMIiNShdKkg+YgJ/oHmmI14OiLYXk+Yv5b7c Ahug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736987734; x=1737592534; 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=O/IJO6oMsjj2D01adPd3wW0Te2i4B04kGMyoTiazK0U=; b=i/m0wjH8O3ZxJAwlyz9NicfiwlALgHgMZtuVjwvtLeiJ01b5/NQYaV97p+kCdnYE+O G988vMawaVmhW8a1QWVCOqcxsdPENPeO1RD1RKAmxPC/9UwNU8RkP2CdTzw2wIAJulNT uUUyELd4isZ/2nKLTVubXxRIPqXKQO2vWVJv/dv6aECmLUgbvJskmfo936yLqp0Zdw9N q/jSx+fAsydOXFkjXrQ76wpd4BXiDKFJoDOBnXkoDzhwn31K6BRdMc+yIl3DqTZpX7I2 DrQmltpGejqRWSt29fjmVjkNNozVrS5J8S8tabzLtiinvLhGs2ZJzLaceufjMGxTQXnd 797Q== X-Gm-Message-State: AOJu0Yzc+CyKat5p6MEv3BePo9vEATu9eL7a+SX5ahvC++/f8n70+LNt RVYZQWBQk505h1A7f1a2MrxoJeYyR6a8KMjfYl97yXdd5rzEmeZYN64zqwm4Kurz2bqrfPYSF4W D1PAvidWkJ5nuf/RAZ9UYS+swPo85hmyPGxsOUaiW5q9ygfULdi+nUOFDneF8fcpkaJvOxrcxIP wVzQLRkDGdfZYmV1CrCH4yQJ5/7MsNjhnExraXeJG/uA== X-Gm-Gg: ASbGncs3G/x0dbkanOalDM6mZ/Wp5dQBz5u3DD9HqO66boYlRz3OA3y8HidDaNqSj4R 4maOG6tp6Dc9rB5+rP9V3gyjh/lL5HXNG4jcIrHzUab/FsTrPnzG5IwBD3YswUJ6C4zUTZ5BlbQ N0l9CguZQrbBcbqGLrwzbpajwvnPF1S1wxLR41SpEyVeMFoUCSHbzjBssKgkensjwkUlc/hz000 DdPV8dYmX1cY8tV/ZFBMqDUfedGmmMI0mIXbJyYeDhwue9O+SF/llJLRD2F4I9aig2wFlLZ6tIc uJHzEB8/ X-Google-Smtp-Source: AGHT+IGxU01TAVi7ooPuKd5Yq4ZTsbB87MVfvPtUApa53zKtRfsRimRJNTmnTRhGofBxZz71XvPXhQ== X-Received: by 2002:a17:90b:50c3:b0:2ea:4c8d:c7a2 with SMTP id 98e67ed59e1d1-2f548f5f6f8mr49092508a91.24.1736987734037; Wed, 15 Jan 2025 16:35:34 -0800 (PST) Received: from hsinchu02.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f2538e8sm86869125ad.218.2025.01.15.16.35.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 16:35:33 -0800 (PST) From: Monk Chiang To: gcc-patches@gcc.gnu.org, kito@gcc.gnu.org Cc: Monk Chiang , Greg McGary Subject: [PATCH v4 2/4] RISC-V: Add Zicfilp ISA extension. Date: Thu, 16 Jan 2025 08:35:21 +0800 Message-ID: <20250116003524.84399-2-monk.chiang@sifive.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116003524.84399-1-monk.chiang@sifive.com> References: <20250116003524.84399-1-monk.chiang@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org This patch only support landing pad value is 0. The next version will implement function signature based labeling scheme. RISC-V CFI SPEC: https://github.com/riscv/riscv-cfi gcc/ChangeLog: * gcc/common/config/riscv/riscv-common.cc: Add ZICFILP ISA string. * gcc/config.gcc: Add riscv-zicfilp.o * gcc/config/riscv/riscv-passes.def (INSERT_PASS_BEFORE): Insert landing pad instructions. * gcc/config/riscv/riscv-protos.h (make_pass_insert_landing_pad): Declare. * gcc/config/riscv/riscv-zicfilp.cc: New file. * gcc/config/riscv/riscv.cc (riscv_trampoline_init): Add landing pad instructions. (riscv_legitimize_call_address): Likewise. (riscv_output_mi_thunk): Likewise. * gcc/config/riscv/riscv.h: Update. * gcc/config/riscv/riscv.md: Add landing pad patterns. * gcc/config/riscv/riscv.opt (TARGET_ZICFILP): Define. * gcc/config/riscv/t-riscv: Add build rule for riscv-zicfilp.o gcc/testsuite/ChangeLog: * gcc/testsuite/gcc.target/riscv/interrupt-no-lpad.c: New test. * gcc/testsuite/gcc.target/riscv/zicfilp-call.c: New test. Co-Developed-by: Greg McGary Kito Cheng --- gcc/common/config/riscv/riscv-common.cc | 3 + gcc/config.gcc | 2 +- gcc/config/riscv/riscv-passes.def | 1 + gcc/config/riscv/riscv-protos.h | 1 + gcc/config/riscv/riscv-zicfilp.cc | 169 ++++++++++++++++++ gcc/config/riscv/riscv.cc | 145 ++++++++++++--- gcc/config/riscv/riscv.h | 13 +- gcc/config/riscv/riscv.md | 73 +++++++- gcc/config/riscv/riscv.opt | 2 + gcc/config/riscv/t-riscv | 9 + .../gcc.target/riscv/interrupt-no-lpad.c | 7 + gcc/testsuite/gcc.target/riscv/zicfilp-call.c | 14 ++ 12 files changed, 402 insertions(+), 37 deletions(-) create mode 100644 gcc/config/riscv/riscv-zicfilp.cc create mode 100644 gcc/testsuite/gcc.target/riscv/interrupt-no-lpad.c create mode 100644 gcc/testsuite/gcc.target/riscv/zicfilp-call.c diff --git a/gcc/common/config/riscv/riscv-common.cc b/gcc/common/config/riscv/riscv-common.cc index 8e8b6107a6d..5038f0eb959 100644 --- a/gcc/common/config/riscv/riscv-common.cc +++ b/gcc/common/config/riscv/riscv-common.cc @@ -113,6 +113,7 @@ static const riscv_implied_info_t riscv_implied_info[] = {"zicfiss", "zicsr"}, {"zicfiss", "zimop"}, + {"zicfilp", "zicsr"}, {"zk", "zkn"}, {"zk", "zkr"}, @@ -329,6 +330,7 @@ static const struct riscv_ext_version riscv_ext_version_table[] = {"ziccrse", ISA_SPEC_CLASS_NONE, 1, 0}, {"zicfiss", ISA_SPEC_CLASS_NONE, 1, 0}, + {"zicfilp", ISA_SPEC_CLASS_NONE, 1, 0}, {"zimop", ISA_SPEC_CLASS_NONE, 1, 0}, {"zcmop", ISA_SPEC_CLASS_NONE, 1, 0}, @@ -1653,6 +1655,7 @@ static const riscv_ext_flag_table_t riscv_ext_flag_table[] = RISCV_EXT_FLAG_ENTRY ("zic64b", x_riscv_zicmo_subext, MASK_ZIC64B), RISCV_EXT_FLAG_ENTRY ("zicfiss", x_riscv_zi_subext, MASK_ZICFISS), + RISCV_EXT_FLAG_ENTRY ("zicfilp", x_riscv_zi_subext, MASK_ZICFILP), RISCV_EXT_FLAG_ENTRY ("zimop", x_riscv_mop_subext, MASK_ZIMOP), RISCV_EXT_FLAG_ENTRY ("zcmop", x_riscv_mop_subext, MASK_ZCMOP), diff --git a/gcc/config.gcc b/gcc/config.gcc index 55e37146ee0..87fed823118 100644 --- a/gcc/config.gcc +++ b/gcc/config.gcc @@ -553,7 +553,7 @@ riscv*) extra_objs="riscv-builtins.o riscv-c.o riscv-sr.o riscv-shorten-memrefs.o riscv-selftests.o riscv-string.o" extra_objs="${extra_objs} riscv-v.o riscv-vsetvl.o riscv-vector-costs.o riscv-avlprop.o" extra_objs="${extra_objs} riscv-vector-builtins.o riscv-vector-builtins-shapes.o riscv-vector-builtins-bases.o sifive-vector-builtins-bases.o" - extra_objs="${extra_objs} thead.o riscv-target-attr.o" + extra_objs="${extra_objs} thead.o riscv-target-attr.o riscv-zicfilp.o" d_target_objs="riscv-d.o" extra_headers="riscv_vector.h riscv_crypto.h riscv_bitmanip.h riscv_th_vector.h riscv_cmo.h" target_gtfiles="$target_gtfiles \$(srcdir)/config/riscv/riscv-vector-builtins.cc" diff --git a/gcc/config/riscv/riscv-passes.def b/gcc/config/riscv/riscv-passes.def index cbea23c8b44..7e6a2a0e53d 100644 --- a/gcc/config/riscv/riscv-passes.def +++ b/gcc/config/riscv/riscv-passes.def @@ -20,3 +20,4 @@ INSERT_PASS_AFTER (pass_rtl_store_motion, 1, pass_shorten_memrefs); INSERT_PASS_AFTER (pass_split_all_insns, 1, pass_avlprop); INSERT_PASS_BEFORE (pass_fast_rtl_dce, 1, pass_vsetvl); +INSERT_PASS_BEFORE (pass_shorten_branches, 1, pass_insert_landing_pad); diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h index dd3b36d47a6..6362380ec6c 100644 --- a/gcc/config/riscv/riscv-protos.h +++ b/gcc/config/riscv/riscv-protos.h @@ -200,6 +200,7 @@ extern bool riscv_hard_regno_rename_ok (unsigned, unsigned); rtl_opt_pass * make_pass_shorten_memrefs (gcc::context *ctxt); rtl_opt_pass * make_pass_avlprop (gcc::context *ctxt); rtl_opt_pass * make_pass_vsetvl (gcc::context *ctxt); +rtl_opt_pass * make_pass_insert_landing_pad (gcc::context *ctxt); /* Routines implemented in riscv-string.c. */ extern bool riscv_expand_block_compare (rtx, rtx, rtx, rtx); diff --git a/gcc/config/riscv/riscv-zicfilp.cc b/gcc/config/riscv/riscv-zicfilp.cc new file mode 100644 index 00000000000..42b129920b3 --- /dev/null +++ b/gcc/config/riscv/riscv-zicfilp.cc @@ -0,0 +1,169 @@ +/* Branch Target Identification for RISCV architecture. + Copyright (C) 2019-2025 Free Software Foundation, Inc. + Based on ARM target. + + This file is part of GCC. + + GCC is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GCC is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GCC; see the file COPYING3. If not see + . */ + +#define IN_TARGET_CODE 1 + +#include "config.h" +#define INCLUDE_STRING +#include "system.h" +#include "coretypes.h" +#include "backend.h" +#include "target.h" +#include "rtl.h" +#include "tree.h" +#include "memmodel.h" +#include "gimple.h" +#include "tm_p.h" +#include "stringpool.h" +#include "attribs.h" +#include "expr.h" +#include "emit-rtl.h" +#include "gimplify.h" +#include "gimple-iterator.h" +#include "dumpfile.h" +#include "rtl-iter.h" +#include "cfgrtl.h" +#include "tree-pass.h" +#include "cgraph.h" +#include "output.h" + +/* This pass implements forward-CFI landing pad checks for RISCV. This is + a security feature similar to BTI (branch target identification) in + AArch64 and IBT (indirect branch tracking)in X86. A LPAD (landing-pad + check) instruction is used to guard against the execution of + instructions which are not the intended target of an indirect branch. + + When forward-CFI is disabled or unimplemented in the CPU, the + landing-pad check label instructions behave as NOP. When implemented in + the CPU, and enabled, the destination of an indirect branch must be + LPAD insn. Otherwise, the CPU reaises an exception. + + In order to enable this mechanism, this pass iterates through the + control flow of the code and adds appropriate LPAD instructions at the + beginning of any function that can be called indirectly, and for targets + of indirect jumps, i.e., jump table targets, non-local goto targets, and + labels that might be referenced by variables, constant pools, etc + (NOTE_INSN_DELETED_LABEL). */ + +namespace { + +const pass_data pass_data_insert_landing_pad = +{ + RTL_PASS, /* type. */ + "zisslpcfi", /* name. */ + OPTGROUP_NONE, /* optinfo_flags. */ + TV_MACH_DEP, /* tv_id. */ + 0, /* properties_required. */ + 0, /* properties_provided. */ + 0, /* properties_destroyed. */ + 0, /* todo_flags_start. */ + 0, /* todo_flags_finish. */ +}; + +static bool +is_interrupt_handler_p (tree type) +{ + return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL; +} + +/* Insert landing-pad check instructions. This is a late RTL pass that runs + before branch shortening. */ +static unsigned int +rest_of_insert_landing_pad (void) +{ + timevar_push (TV_MACH_DEP); + + struct cgraph_node *c_node; + rtx lpad_insn; + rtx_insn *insn; + basic_block bb; + + bb = 0; + FOR_EACH_BB_FN (bb, cfun) + { + for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); + insn = NEXT_INSN (insn)) + { + /* If a label is marked to be preserved or can be a non-local goto + target, it must be protected with a lpad instruction. */ + if (LABEL_P (insn) + && (LABEL_PRESERVE_P (insn) + || bb->flags & BB_NON_LOCAL_GOTO_TARGET)) + { + emit_insn_before (gen_lpad_align (), insn); + emit_insn_after (gen_lpad (const0_rtx), insn); + continue; + } + + if (INSN_P (insn) && INSN_CODE (insn) == CODE_FOR_gpr_save) + { + emit_move_insn (RISCV_CALL_ADDRESS_LPAD (Pmode), const0_rtx); + emit_insn_before (gen_lpad_align (), insn); + emit_insn_after (gen_lpad (const0_rtx), insn); + continue; + } + + if (INSN_P (insn) && INSN_CODE (insn) == CODE_FOR_gpr_restore) + emit_move_insn (RISCV_CALL_ADDRESS_LPAD (Pmode), const0_rtx); + + } + } + + c_node = cgraph_node::get (cfun->decl); + if (!c_node->only_called_directly_p () + && !is_interrupt_handler_p (TREE_TYPE (cfun->decl))) + { + bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; + insn = BB_HEAD (bb); + lpad_insn = gen_lpad (const0_rtx); + emit_insn_before (lpad_insn, insn); + } + + timevar_pop (TV_MACH_DEP); + return 0; +} + +class pass_insert_landing_pad : public rtl_opt_pass +{ +public: + pass_insert_landing_pad (gcc::context *ctxt) + : rtl_opt_pass (pass_data_insert_landing_pad, ctxt) + {} + + /* opt_pass methods: */ + virtual bool gate (function *) + { + return TARGET_ZICFILP; + } + + virtual unsigned int execute (function *) + { + return rest_of_insert_landing_pad (); + } + +}; // class pass_insert_landing_pad + +} // anon namespace + +rtl_opt_pass * +make_pass_insert_landing_pad (gcc::context *ctxt) +{ + return new pass_insert_landing_pad (ctxt); +} diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index cd37b492183..4afb0b95839 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -6681,8 +6681,20 @@ riscv_legitimize_call_address (rtx addr) { rtx reg = RISCV_CALL_ADDRESS_TEMP (Pmode); riscv_emit_move (reg, addr); + + if (TARGET_ZICFILP) + { + rtx sw_guarded = RISCV_CALL_ADDRESS_LPAD (Pmode); + emit_insn (gen_set_guarded (Pmode, reg)); + return sw_guarded; + } + return reg; } + + if (TARGET_ZICFILP && REG_P (addr)) + emit_insn (gen_set_lpl (Pmode, const0_rtx)); + return addr; } @@ -10342,6 +10354,9 @@ riscv_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED, /* Mark the end of the (empty) prologue. */ emit_note (NOTE_INSN_PROLOGUE_END); + if (TARGET_ZICFILP) + emit_insn(gen_lpad (const0_rtx)); + /* Determine if we can use a sibcall to call FUNCTION directly. */ fnaddr = gen_rtx_MEM (FUNCTION_MODE, XEXP (DECL_RTL (function), 0)); @@ -10853,12 +10868,17 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) { rtx addr, end_addr, mem; uint32_t trampoline[4]; + uint32_t trampoline_cfi[6]; unsigned int i; HOST_WIDE_INT static_chain_offset, target_function_offset; + HOST_WIDE_INT lp_value = 0; /* Work out the offsets of the pointers from the start of the trampoline code. */ - gcc_assert (ARRAY_SIZE (trampoline) * 4 == TRAMPOLINE_CODE_SIZE); + if (!TARGET_ZICFILP) + gcc_assert (ARRAY_SIZE (trampoline) * 4 == TRAMPOLINE_CODE_SIZE); + else + gcc_assert (ARRAY_SIZE (trampoline_cfi) * 4 == TRAMPOLINE_CODE_SIZE); /* Get pointers to the beginning and end of the code block. */ addr = force_reg (Pmode, XEXP (m_tramp, 0)); @@ -10880,6 +10900,17 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) unsigned HOST_WIDE_INT lo_chain_code, lo_func_code; rtx uimm_mask = force_reg (SImode, gen_int_mode (-IMM_REACH, SImode)); + unsigned insn_count = 0; + + /* Insert lpad, if zicfilp is enabled. */ + if (TARGET_ZICFILP) + { + unsigned HOST_WIDE_INT lpad_code; + lpad_code = OPCODE_AUIPC | (0 << SHIFT_RD) | (lp_value << IMM_BITS); + mem = adjust_address (m_tramp, SImode, 0); + riscv_emit_move (mem, gen_int_mode (lpad_code, SImode)); + insn_count++; + } /* 0xfff. */ rtx imm12_mask = gen_reg_rtx (SImode); @@ -10893,11 +10924,14 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) hi_chain = riscv_force_binary (SImode, AND, hi_chain, uimm_mask); lui_hi_chain_code = OPCODE_LUI | (STATIC_CHAIN_REGNUM << SHIFT_RD); + rtx lui_hi_chain_value = force_reg (SImode, gen_int_mode (lui_hi_chain_code, + SImode)); rtx lui_hi_chain = riscv_force_binary (SImode, IOR, hi_chain, - gen_int_mode (lui_hi_chain_code, SImode)); - - mem = adjust_address (m_tramp, SImode, 0); + lui_hi_chain_value); + mem = adjust_address (m_tramp, SImode, + insn_count * GET_MODE_SIZE (SImode)); riscv_emit_move (mem, riscv_swap_instruction (lui_hi_chain)); + insn_count++; /* Gen lui t0, hi(func). */ rtx hi_func = riscv_force_binary (SImode, PLUS, target_function, @@ -10908,8 +10942,10 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) rtx lui_hi_func = riscv_force_binary (SImode, IOR, hi_func, gen_int_mode (lui_hi_func_code, SImode)); - mem = adjust_address (m_tramp, SImode, 1 * GET_MODE_SIZE (SImode)); + mem = adjust_address (m_tramp, SImode, + insn_count * GET_MODE_SIZE (SImode)); riscv_emit_move (mem, riscv_swap_instruction (lui_hi_func)); + insn_count++; /* Gen addi t2, t2, lo(chain). */ rtx lo_chain = riscv_force_binary (SImode, AND, chain_value, @@ -10923,8 +10959,23 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) rtx addi_lo_chain = riscv_force_binary (SImode, IOR, lo_chain, force_reg (SImode, GEN_INT (lo_chain_code))); - mem = adjust_address (m_tramp, SImode, 2 * GET_MODE_SIZE (SImode)); + mem = adjust_address (m_tramp, SImode, + insn_count * GET_MODE_SIZE (SImode)); riscv_emit_move (mem, riscv_swap_instruction (addi_lo_chain)); + insn_count++; + + /* For zicfilp only, insert lui t2, 1, because use jr t0. */ + if (TARGET_ZICFILP) + { + unsigned HOST_WIDE_INT set_lpl_code; + set_lpl_code = OPCODE_LUI + | (RISCV_CALL_ADDRESS_LPAD_REGNUM << SHIFT_RD) + | (lp_value << IMM_BITS); + mem = adjust_address (m_tramp, SImode, + insn_count * GET_MODE_SIZE (SImode)); + riscv_emit_move (mem, gen_int_mode (set_lpl_code, SImode)); + insn_count++; + } /* Gen jr t0, lo(func). */ rtx lo_func = riscv_force_binary (SImode, AND, target_function, @@ -10936,7 +10987,7 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) rtx jr_lo_func = riscv_force_binary (SImode, IOR, lo_func, force_reg (SImode, GEN_INT (lo_func_code))); - mem = adjust_address (m_tramp, SImode, 3 * GET_MODE_SIZE (SImode)); + mem = adjust_address (m_tramp, SImode, insn_count * GET_MODE_SIZE (SImode)); riscv_emit_move (mem, riscv_swap_instruction (jr_lo_func)); } else @@ -10944,29 +10995,65 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) static_chain_offset = TRAMPOLINE_CODE_SIZE; target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode); - /* auipc t2, 0 - l[wd] t0, target_function_offset(t2) - l[wd] t2, static_chain_offset(t2) - jr t0 - */ - trampoline[0] = OPCODE_AUIPC | (STATIC_CHAIN_REGNUM << SHIFT_RD); - trampoline[1] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW) - | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RD) - | (STATIC_CHAIN_REGNUM << SHIFT_RS1) - | (target_function_offset << SHIFT_IMM); - trampoline[2] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW) - | (STATIC_CHAIN_REGNUM << SHIFT_RD) - | (STATIC_CHAIN_REGNUM << SHIFT_RS1) - | (static_chain_offset << SHIFT_IMM); - trampoline[3] = OPCODE_JALR | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RS1); - - /* Copy the trampoline code. */ - for (i = 0; i < ARRAY_SIZE (trampoline); i++) + if (!TARGET_ZICFILP) { - if (BYTES_BIG_ENDIAN) - trampoline[i] = __builtin_bswap32(trampoline[i]); - mem = adjust_address (m_tramp, SImode, i * GET_MODE_SIZE (SImode)); - riscv_emit_move (mem, gen_int_mode (trampoline[i], SImode)); + /* auipc t2, 0 + l[wd] t0, (target_function_offset)(t2) + l[wd] t2, (static_chain_offset)(t2) + jr t0 + */ + trampoline[0] = OPCODE_AUIPC | (STATIC_CHAIN_REGNUM << SHIFT_RD); + trampoline[1] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW) + | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RD) + | (STATIC_CHAIN_REGNUM << SHIFT_RS1) + | (target_function_offset << SHIFT_IMM); + trampoline[2] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW) + | (STATIC_CHAIN_REGNUM << SHIFT_RD) + | (STATIC_CHAIN_REGNUM << SHIFT_RS1) + | (static_chain_offset << SHIFT_IMM); + trampoline[3] = OPCODE_JALR | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RS1); + + /* Copy the trampoline code. */ + for (i = 0; i < ARRAY_SIZE (trampoline); i++) + { + if (BYTES_BIG_ENDIAN) + trampoline[i] = __builtin_bswap32 (trampoline[i]); + mem = adjust_address (m_tramp, SImode, i * GET_MODE_SIZE (SImode)); + riscv_emit_move (mem, gen_int_mode (trampoline[i], SImode)); + } + } + else + { + /* lpad 1 + auipc t3, 0 + l[wd] t0, (target_function_offset - 4)(t3) + l[wd] t3, (static_chain_offset - 4)(t3) + lui t2, 1 + jr t0 + */ + trampoline_cfi[0] = OPCODE_AUIPC | (0 << SHIFT_RD) | (lp_value << IMM_BITS); + trampoline_cfi[1] = OPCODE_AUIPC | (STATIC_CHAIN_REGNUM << SHIFT_RD); + trampoline_cfi[2] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW) + | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RD) + | (STATIC_CHAIN_REGNUM << SHIFT_RS1) + | ((target_function_offset - 4) << SHIFT_IMM); + trampoline_cfi[3] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW) + | (STATIC_CHAIN_REGNUM << SHIFT_RD) + | (STATIC_CHAIN_REGNUM << SHIFT_RS1) + | ((static_chain_offset - 4) << SHIFT_IMM); + trampoline_cfi[4] = OPCODE_LUI + | (RISCV_CALL_ADDRESS_LPAD_REGNUM << SHIFT_RD) + | (lp_value << IMM_BITS); + trampoline_cfi[5] = OPCODE_JALR | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RS1); + + /* Copy the trampoline code. */ + for (i = 0; i < ARRAY_SIZE (trampoline_cfi); i++) + { + if (BYTES_BIG_ENDIAN) + trampoline_cfi[i] = __builtin_bswap32 (trampoline_cfi[i]); + mem = adjust_address (m_tramp, SImode, i * GET_MODE_SIZE (SImode)); + riscv_emit_move (mem, gen_int_mode (trampoline_cfi[i], SImode)); + } } /* Set up the static chain pointer field. */ diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index 93e88fe885d..073b9e4f2bd 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -190,7 +190,8 @@ ASM_MISA_SPEC #define PARM_BOUNDARY BITS_PER_WORD /* Allocation boundary (in *bits*) for the code of a function. */ -#define FUNCTION_BOUNDARY ((TARGET_RVC || TARGET_ZCA) ? 16 : 32) +#define FUNCTION_BOUNDARY \ + (((TARGET_RVC || TARGET_ZCA) && !TARGET_ZICFILP) ? 16 : 32) /* The smallest supported stack boundary the calling convention supports. */ #define STACK_BOUNDARY \ @@ -413,7 +414,8 @@ ASM_MISA_SPEC #define RISCV_DWARF_VLENB (4096 + 0xc22) /* Register in which static-chain is passed to a function. */ -#define STATIC_CHAIN_REGNUM (GP_TEMP_FIRST + 2) +#define STATIC_CHAIN_REGNUM \ + ((TARGET_ZICFILP) ? (GP_TEMP_FIRST + 23) : (GP_TEMP_FIRST + 2)) /* Registers used as temporaries in prologue/epilogue code. @@ -436,6 +438,10 @@ ASM_MISA_SPEC #define RISCV_CALL_ADDRESS_TEMP(MODE) \ gen_rtx_REG (MODE, RISCV_CALL_ADDRESS_TEMP_REGNUM) +#define RISCV_CALL_ADDRESS_LPAD_REGNUM (GP_TEMP_FIRST + 2) +#define RISCV_CALL_ADDRESS_LPAD(MODE) \ + gen_rtx_REG (MODE, RISCV_CALL_ADDRESS_LPAD_REGNUM) + #define RETURN_ADDR_MASK (1 << RETURN_ADDR_REGNUM) #define S0_MASK (1 << S0_REGNUM) #define S1_MASK (1 << S1_REGNUM) @@ -821,7 +827,8 @@ extern enum riscv_cc get_riscv_cc (const rtx use); /* Trampolines are a block of code followed by two pointers. */ -#define TRAMPOLINE_CODE_SIZE 16 +#define TRAMPOLINE_CODE_SIZE ((TARGET_ZICFILP) ? 24 : 16) + #define TRAMPOLINE_SIZE \ ((Pmode == SImode) \ ? TRAMPOLINE_CODE_SIZE \ diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index 21094395184..5b4c81bb9e0 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -105,6 +105,12 @@ UNSPECV_SSPOPCHK UNSPECV_SSRDP UNSPECV_SSP + + ;; ZICFILP + UNSPECV_LPAD + UNSPECV_SETLPL + UNSPECV_LPAD_ALIGN + UNSPECV_SET_GUARDED ]) (define_c_enum "unspecv" [ @@ -155,6 +161,7 @@ (TP_REGNUM 4) (T0_REGNUM 5) (T1_REGNUM 6) + (T2_REGNUM 7) (S0_REGNUM 8) (S1_REGNUM 9) (A0_REGNUM 10) @@ -3709,11 +3716,18 @@ [(set (pc) (match_operand 0 "register_operand"))] "" { + if (TARGET_ZICFILP) + emit_insn (gen_set_lpl (Pmode, const0_rtx)); + operands[0] = force_reg (Pmode, operands[0]); + if (TARGET_ZICFILP) + emit_use (gen_rtx_REG (Pmode, T2_REGNUM)); + if (Pmode == SImode) emit_jump_insn (gen_indirect_jumpsi (operands[0])); else emit_jump_insn (gen_indirect_jumpdi (operands[0])); + DONE; }) @@ -3734,21 +3748,42 @@ gen_rtx_LABEL_REF (Pmode, operands[1]), NULL_RTX, 0, OPTAB_DIRECT); - if (CASE_VECTOR_PC_RELATIVE && Pmode == DImode) - emit_jump_insn (gen_tablejumpdi (operands[0], operands[1])); + if (TARGET_ZICFILP) + { + rtx t2 = RISCV_CALL_ADDRESS_LPAD (GET_MODE (operands[0])); + emit_move_insn (t2, operands[0]); + + if (CASE_VECTOR_PC_RELATIVE && Pmode == DImode) + emit_jump_insn (gen_tablejump_cfidi (operands[1])); + else + emit_jump_insn (gen_tablejump_cfisi (operands[1])); + } else - emit_jump_insn (gen_tablejumpsi (operands[0], operands[1])); + { + if (CASE_VECTOR_PC_RELATIVE && Pmode == DImode) + emit_jump_insn (gen_tablejumpdi (operands[0], operands[1])); + else + emit_jump_insn (gen_tablejumpsi (operands[0], operands[1])); + } DONE; }) (define_insn "tablejump" [(set (pc) (match_operand:GPR 0 "register_operand" "l")) (use (label_ref (match_operand 1 "" "")))] - "" + "!TARGET_ZICFILP" "jr\t%0" [(set_attr "type" "jalr") (set_attr "mode" "none")]) +(define_insn "tablejump_cfi" + [(set (pc) (reg:GPR T2_REGNUM)) + (use (label_ref (match_operand 0 "")))] + "TARGET_ZICFILP" + "jr\tt2" + [(set_attr "type" "jalr") + (set_attr "mode" "none")]) + ;; ;; .................... ;; @@ -4718,6 +4753,36 @@ [(set_attr "type" "arith") (set_attr "mode" "")]) +;; Lading pad. + +(define_insn "lpad" + [(unspec_volatile [(match_operand 0 "immediate_operand" "i")] UNSPECV_LPAD)] + "TARGET_ZICFILP" + "lpad\t%0" + [(set_attr "type" "auipc")]) + +(define_insn "@set_lpl" + [(set (reg:GPR T2_REGNUM) + (unspec_volatile [(match_operand:GPR 0 "immediate_operand" "i")] UNSPECV_SETLPL))] + "TARGET_ZICFILP" + "lui\tt2,%0" + [(set_attr "type" "const") + (set_attr "mode" "")]) + +(define_insn "lpad_align" + [(unspec_volatile [(const_int 0)] UNSPECV_LPAD_ALIGN)] + "TARGET_ZICFILP" + ".align 2" + [(set_attr "type" "nop")]) + +(define_insn "@set_guarded" + [(set (reg:GPR T2_REGNUM) + (unspec_volatile [(match_operand:GPR 0 "register_operand" "r")] UNSPECV_SET_GUARDED))] + "TARGET_ZICFILP" + "mv\tt2,%0" + [(set_attr "type" "move") + (set_attr "mode" "")]) + (include "bitmanip.md") (include "crypto.md") (include "sync.md") diff --git a/gcc/config/riscv/riscv.opt b/gcc/config/riscv/riscv.opt index 4a43bfd7bc6..f51f8fd1cdf 100644 --- a/gcc/config/riscv/riscv.opt +++ b/gcc/config/riscv/riscv.opt @@ -255,6 +255,8 @@ Mask(ZICCRSE) Var(riscv_zi_subext) Mask(ZICFISS) Var(riscv_zi_subext) +Mask(ZICFILP) Var(riscv_zi_subext) + TargetVariable int riscv_za_subext diff --git a/gcc/config/riscv/t-riscv b/gcc/config/riscv/t-riscv index bc67d8719fa..6493087fe51 100644 --- a/gcc/config/riscv/t-riscv +++ b/gcc/config/riscv/t-riscv @@ -145,6 +145,15 @@ thead.o: $(srcdir)/config/riscv/thead.cc \ $(COMPILE) $< $(POSTCOMPILE) +riscv-zicfilp.o: $(srcdir)/config/riscv/riscv-zicfilp.cc \ + $(CONFIG_H) $(SYSTEM_H) $(TM_H) $(REGS_H) insn-config.h $(RTL_BASE_H) \ + dominance.h cfg.h cfganal.h $(BASIC_BLOCK_H) $(INSN_ATTR_H) $(RECOG_H) \ + output.h hash-map.h $(DF_H) $(OBSTACK_H) $(TARGET_H) $(RTL_H) \ + $(CONTEXT_H) $(TREE_PASS_H) regrename.h \ + $(srcdir)/config/riscv/riscv-protos.h + $(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \ + $(srcdir)/config/riscv/riscv-zicfilp.cc + PASSES_EXTRA += $(srcdir)/config/riscv/riscv-passes.def $(common_out_file): $(srcdir)/config/riscv/riscv-cores.def \ diff --git a/gcc/testsuite/gcc.target/riscv/interrupt-no-lpad.c b/gcc/testsuite/gcc.target/riscv/interrupt-no-lpad.c new file mode 100644 index 00000000000..ff512b98c61 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/interrupt-no-lpad.c @@ -0,0 +1,7 @@ +/* { dg-do compile { target { riscv64*-*-* } } } */ +/* { dg-options "-march=rv64gc_zicfilp -mabi=lp64d" } */ +void __attribute__ ((interrupt)) +foo (void) +{ +} +/* { dg-final { scan-assembler-not "lpad\t" } } */ diff --git a/gcc/testsuite/gcc.target/riscv/zicfilp-call.c b/gcc/testsuite/gcc.target/riscv/zicfilp-call.c new file mode 100644 index 00000000000..75c8b32faa3 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zicfilp-call.c @@ -0,0 +1,14 @@ +/* { dg-do compile { target { riscv64*-*-* } } } */ +/* { dg-options "-O2 -fPIE -march=rv64gc_zicfilp -mabi=lp64d" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" } } */ + +extern void _dl_find_object_init (void); + +void +_dl_non_dynamic_init (void) +{ + extern __typeof__ (_dl_find_object_init) _dl_find_object_init __attribute__ ((weak)); + (_dl_find_object_init != ((void *) 0) ? _dl_find_object_init () : (void)0); +} + +/* { dg-final { scan-assembler-times "mv\tt2" 1 } } */ From patchwork Thu Jan 16 00:35:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Monk Chiang X-Patchwork-Id: 104871 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9696F385382C for ; Thu, 16 Jan 2025 00:39:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9696F385382C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=fMzh7I6v X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 58FED385E00C for ; Thu, 16 Jan 2025 00:35:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 58FED385E00C Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 58FED385E00C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736987739; cv=none; b=BYw0Qj48OZRjjou34MIkM5Ex9145MKZyN2PEfS9GCk6Ho892X5OAzJqWU3bwLYYxPqvpczJPKTyCkkUHvRGB4Hkfz0JTSSBgAi5CP3QagIAidh45O0lD/alR1KOlKJLdbKugI7g9wkcOris9/TJI1sQrlI/uEBky7jowpnbnxq4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736987739; c=relaxed/simple; bh=Sqpxh6m3m3mDpXKWFWnt9sL6gcNS1iXF1TZR0JJLtWk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WXqom1CRVxRYorCClVkYPBpI9j6p05kxgrhSIV8Ok/xranMT8fge+L3cSOUq5nGmW9oumgMyyxjmeT8T9WImtT8Ryl5S6pNEQ2TMKq9MGWq1EerCHzvfnSrsgZREAKWepX7ctIkw0KWRXj28HrathAbzTjik1qan8ADjuEauFZw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 58FED385E00C Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-2166360285dso5159425ad.1 for ; Wed, 15 Jan 2025 16:35:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1736987738; x=1737592538; darn=gcc.gnu.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=zQ5CfOBy+Gch9V8pXT/g9/F1lzY6m5Gu7kdIHNFtATk=; b=fMzh7I6vOz9fZcitJYKxDuIQ/xzmZKjXVBLCcz1F11zRdSNLVMp7O9UWz5aSZJp+Vo gObz+yJFjtlKTqCp+SzL0Wg/PDoekAqyfsCCsIeVagqI8MEB5LMFuKIHC1rfZJBTAYrw UpLOXCOsl53wAuBCAGusATbEqvY7rDLC6MbR0iQUlviSr26sI92QSlY/iramLXVGRB2N UNFN0Gfav8jU2hEzGNpN59AtTOxOgRjEvSMzB+6j5aXcTDJmJw+Xe741Czs1Ix+nnYBp 67kfKa9har6M5ZPvnaggIJK+dmvRyJYeBVk1+gQ0J4lN7qDLB6SIRzc9SBp6BK21yImK 5HmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736987738; x=1737592538; 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=zQ5CfOBy+Gch9V8pXT/g9/F1lzY6m5Gu7kdIHNFtATk=; b=YIilbyBaImNOkj1N5aNwSgH/+Wyho55TaJDdXxkKpe+PIpItpBH24TjiXDtt7enpnf MuZ3xWryJEAEG+mpw2maUc/QogIFbWrwp+sI7K8wa2ebDPjEg4GLHGZGkYRbCOlqhffb rvTvFQji2rWcnVnUl7NY/KZ10ag16nB/VRRfePD/z8i8hTtbukLsvfUQlpOB7+nqDKuU fZ+vaYe4d7FZAkKas37wTaVlPH4tdfRzWG1EinmYyHKxrP9OHqUKnodLabIyuavWtnnm Cy/gKIbxp1melxOCzeQAEaaD0MnKkVgKTpDWns4/UWXFsSsqVdKtwqI7AoYc8EpRKTUG XVZQ== X-Gm-Message-State: AOJu0YyPbDP/r7rXpY3uIeQpdxG9TuRnCGApmkW2Wp6UP7Y60y8pcWHT 7XXYdGq1qu7kxCKXv+dYF5kIK/tfZv8K6QgMUAYRyqd0GZhfSHQ6Cjl6LMAUcK2Nlj84wb1ViSf 1Cb94w3n47Unft8u8HKDfqoSSAlfuuwKVIrWyJOysPIho8M0W8MobHVFWCuTuOTiQxKL4SUq7IY HloQAwJIem6aes8dp7KyS0IRVI180VDayoB6nl3Yowig== X-Gm-Gg: ASbGncuPZ0fFJfaEVjfdM/8GbNIRB8ZgczUiZKr1fCc2O0HYd8zWgj4lThOlJ6woIcB eAt9WpWoQRqci2FgUkNMdb4mNos3kodQcwxWzoIPCJ8XoHIUl9ycJXHvAKKsHNxZheBvgIk/jFW iYErwNS/Aw7oDD4J5YPQy6Oab+q0v7Qlqqa24YeppzDQaH+68UIFXEFqsm4NL7CS8plcdxoi/h7 vks3n4WRc93FZoFDzZQp4HXcYo7HAPsM/159hkHsmZ9HgBCJpHfMs5Jv3sdDwYplJ9dCwgG0Gke 2BcQV+Aa X-Google-Smtp-Source: AGHT+IF8zM3W/hVIhJmbf157keo5zJo9nK+p7wq8Bv3EfHhZTXsrHxDAb0TTePzyyylg6Yf6js3MNw== X-Received: by 2002:a17:902:ecce:b0:20b:8a71:b5c1 with SMTP id d9443c01a7336-21a83f339e6mr499868385ad.1.1736987737931; Wed, 15 Jan 2025 16:35:37 -0800 (PST) Received: from hsinchu02.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f2538e8sm86869125ad.218.2025.01.15.16.35.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 16:35:37 -0800 (PST) From: Monk Chiang To: gcc-patches@gcc.gnu.org, kito@gcc.gnu.org Cc: Monk Chiang , Jesse Huang Subject: [PATCH v4 3/4] RISC-V: Add .note.gnu.property for ZICFILP and ZICFISS ISA extension Date: Thu, 16 Jan 2025 08:35:22 +0800 Message-ID: <20250116003524.84399-3-monk.chiang@sifive.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116003524.84399-1-monk.chiang@sifive.com> References: <20250116003524.84399-1-monk.chiang@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org gcc/ChangeLog: * gcc/config/riscv/riscv.cc (riscv_file_end_indicate_exec_stack): Add .note.gnu.property. * gcc/config/riscv/linux.h (TARGET_ASM_FILE_END): Define. libgcc/ChangeLog: * libgcc/config/riscv/crti.S: Add lpad instructions. * libgcc/config/riscv/crtn.S: Likewise. * libgcc/config/riscv/save-restore.S: Likewise. * libgcc/config/riscv/riscv-asm.h: Add GNU_PROPERTY for ZICFILP, ZICFISS. Co-Developed-by: Jesse Huang --- gcc/config/riscv/riscv.cc | 52 +++++++++++++++++++++- libgcc/config/riscv/crti.S | 2 + libgcc/config/riscv/crtn.S | 2 + libgcc/config/riscv/riscv-asm.h | 69 +++++++++++++++++++++++++++++- libgcc/config/riscv/save-restore.S | 5 +++ 5 files changed, 128 insertions(+), 2 deletions(-) diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 4afb0b95839..cb448aba9c0 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -10334,6 +10334,56 @@ riscv_file_start (void) riscv_emit_attribute (); } +void +riscv_file_end () +{ + file_end_indicate_exec_stack (); + long GNU_PROPERTY_RISCV_FEATURE_1_AND = 0; + unsigned long feature_1_and = 0; + + if (TARGET_ZICFISS) + feature_1_and |= 0x1 << 0; + + if (TARGET_ZICFILP) + feature_1_and |= 0x1 << 1; + + if (feature_1_and) + { + /* Generate .note.gnu.property section. */ + switch_to_section (get_section (".note.gnu.property", + SECTION_NOTYPE, NULL)); + + /* The program property descriptor is aligned to 4 bytes in 32-bit + objects and 8 bytes in 64-bit objects. */ + unsigned p2align = TARGET_64BIT ? 3 : 2; + + fprintf (asm_out_file, "\t.p2align\t%u\n", p2align); + /* name length. */ + fprintf (asm_out_file, "\t.long\t1f - 0f\n"); + /* data length. */ + fprintf (asm_out_file, "\t.long\t5f - 2f\n"); + /* note type. */ + fprintf (asm_out_file, "\t.long\t5\n"); + fprintf (asm_out_file, "0:\n"); + /* vendor name: "GNU". */ + fprintf (asm_out_file, "\t.asciz\t\"GNU\"\n"); + fprintf (asm_out_file, "1:\n"); + + /* pr_type. */ + fprintf (asm_out_file, "\t.p2align\t3\n"); + fprintf (asm_out_file, "2:\n"); + fprintf (asm_out_file, "\t.long\t0xc0000000\n"); + /* pr_datasz. */ + fprintf (asm_out_file, "\t.long\t4f - 3f\n"); + fprintf (asm_out_file, "3:\n"); + /* zicfiss, zicfilp. */ + fprintf (asm_out_file, "\t.long\t%x\n", feature_1_and); + fprintf (asm_out_file, "4:\n"); + fprintf (asm_out_file, "\t.p2align\t%u\n", p2align); + fprintf (asm_out_file, "5:\n"); + } +} + /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text in order to avoid duplicating too much logic from elsewhere. */ @@ -13975,7 +14025,7 @@ bool need_shadow_stack_push_pop_p () #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true #undef TARGET_ASM_FILE_END -#define TARGET_ASM_FILE_END file_end_indicate_exec_stack +#define TARGET_ASM_FILE_END riscv_file_end #undef TARGET_EXPAND_BUILTIN_VA_START #define TARGET_EXPAND_BUILTIN_VA_START riscv_va_start diff --git a/libgcc/config/riscv/crti.S b/libgcc/config/riscv/crti.S index 89bac706c63..3a67fd77156 100644 --- a/libgcc/config/riscv/crti.S +++ b/libgcc/config/riscv/crti.S @@ -1 +1,3 @@ /* crti.S is empty because .init_array/.fini_array are used exclusively. */ + +#include "riscv-asm.h" diff --git a/libgcc/config/riscv/crtn.S b/libgcc/config/riscv/crtn.S index ca6ee7b6fba..cb80782bb55 100644 --- a/libgcc/config/riscv/crtn.S +++ b/libgcc/config/riscv/crtn.S @@ -1 +1,3 @@ /* crtn.S is empty because .init_array/.fini_array are used exclusively. */ + +#include "riscv-asm.h" diff --git a/libgcc/config/riscv/riscv-asm.h b/libgcc/config/riscv/riscv-asm.h index b6dbeaedc20..73bddb3f9e7 100644 --- a/libgcc/config/riscv/riscv-asm.h +++ b/libgcc/config/riscv/riscv-asm.h @@ -23,9 +23,11 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #define FUNC_SIZE(X) .size X,.-X #define FUNC_BEGIN(X) \ + .align 2; \ .globl X; \ FUNC_TYPE (X); \ -X: +X: \ + LPAD #define FUNC_END(X) \ FUNC_SIZE(X) @@ -39,3 +41,68 @@ X: #define HIDDEN_JUMPTARGET(X) CONCAT1(__hidden_, X) #define HIDDEN_DEF(X) FUNC_ALIAS(HIDDEN_JUMPTARGET(X), X); \ .hidden HIDDEN_JUMPTARGET(X) + +/* GNU_PROPERTY_RISCV64_* macros from elf.h for use in asm code. */ +#define FEATURE_1_AND 0xc0000000 +#define FEATURE_1_FCFI 1 +#define FEATURE_1_BCFI 2 + +/* Add a NT_GNU_PROPERTY_TYPE_0 note. */ +#if __riscv_xlen == 32 +# define GNU_PROPERTY(type, value) \ + .section .note.gnu.property, "a"; \ + .p2align 2; \ + .word 4; \ + .word 12; \ + .word 5; \ + .asciz "GNU"; \ + .word type; \ + .word 4; \ + .word value; \ + .text +#else +# define GNU_PROPERTY(type, value) \ + .section .note.gnu.property, "a"; \ + .p2align 3; \ + .word 4; \ + .word 16; \ + .word 5; \ + .asciz "GNU"; \ + .word type; \ + .word 4; \ + .word value; \ + .word 0; \ + .text +#endif + +/* Add GNU property note with the supported features to all asm code + where sysdep.h is included. */ +#undef __VALUE_FOR_FEATURE_1_AND +#if defined (__riscv_zicfilp) || defined (__riscv_zicfiss) +# if defined (__riscv_zicfilp) +# if defined (__riscv_zicfiss) +# define __VALUE_FOR_FEATURE_1_AND 0x3 +# else +# define __VALUE_FOR_FEATURE_1_AND 0x1 +# endif +# else +# if defined (__riscv_zicfiss) +# define __VALUE_FOR_FEATURE_1_AND 0x2 +# else +# error "What?" +# endif +# endif +#endif + +#if defined (__VALUE_FOR_FEATURE_1_AND) +GNU_PROPERTY (FEATURE_1_AND, __VALUE_FOR_FEATURE_1_AND) +#endif +#undef __VALUE_FOR_FEATURE_1_AND + +#ifdef __riscv_zicfilp +# define SET_LPAD lui t2, 0 +# define LPAD lpad 0 +#else +# define SET_LPAD +# define LPAD +#endif diff --git a/libgcc/config/riscv/save-restore.S b/libgcc/config/riscv/save-restore.S index ae8312d27db..fc654472a4c 100644 --- a/libgcc/config/riscv/save-restore.S +++ b/libgcc/config/riscv/save-restore.S @@ -137,6 +137,7 @@ FUNC_BEGIN (__riscv_save_2) # CFA info is not correct in next 2 instruction since t1's # value is depend on how may register really save. add sp, sp, t1 + SET_LPAD jr t0 .cfi_endproc FUNC_END (__riscv_save_12) @@ -162,6 +163,7 @@ FUNC_BEGIN (__riscv_save_0) .cfi_offset 8, -16 sd ra, 8(sp) .cfi_offset 1, -8 + SET_LPAD jr t0 .cfi_endproc FUNC_END (__riscv_save_1) @@ -310,6 +312,7 @@ FUNC_BEGIN(__riscv_save_0) .cfi_offset 8, -8 sw ra, 8(sp) .cfi_offset 1, -4 + SET_LPAD jr t0 .cfi_endproc FUNC_END(__riscv_save_2) @@ -399,6 +402,7 @@ FUNC_BEGIN (__riscv_save_4) # CFA info is not correct in next 2 instruction since t1's # value is depend on how may register really save. sub sp, sp, t1 + SET_LPAD jr t0 .cfi_endproc FUNC_END (__riscv_save_12) @@ -428,6 +432,7 @@ FUNC_BEGIN (__riscv_save_0) .cfi_offset 8, -8 sw ra, 12(sp) .cfi_offset 1, -4 + SET_LPAD jr t0 .cfi_endproc FUNC_END (__riscv_save_3) From patchwork Thu Jan 16 00:35:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Monk Chiang X-Patchwork-Id: 104872 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 960833857833 for ; Thu, 16 Jan 2025 00:42:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 960833857833 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=gNMaqDWL X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id E2E0F385114A for ; Thu, 16 Jan 2025 00:35:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E2E0F385114A Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E2E0F385114A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::633 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736987745; cv=none; b=r3ahxEuJxAXCY00Zn1atEk5Xyo7sDAXd97rhZPKt72kZzjb41Xs8MBdzA90gm3k3fgew2SXPuKSE2cg6//NgVHhE1yY4CvyV60CPggc2jw3YrJokEl+Hv++Qhk2hPj9e2Dd10hmu9WZgSirY3OfwvsVnvOUB7XZQKPQmkV2Z7E0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736987745; c=relaxed/simple; bh=aCtGp0r+iwv9imy0zDNnU9WDQlr5DvdbzW1bp5TJCFY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=hiJRUyOGmJ1dpQDsYrm1WAvPs0ViuWHku4l+ggFU8Dg3as2E/CWEISVX5mEDWoDMobay8KSwEKTHPBCr/B/FR91mYc3cSUimoY/OidPo15hoDkGMIyd8kdPA+PJJoYMO+p9lizWcxJPz2Xo2Uxt6qg7WdrPVQqOygaSis2EJMvI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E2E0F385114A Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-2166360285dso5160525ad.1 for ; Wed, 15 Jan 2025 16:35:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1736987744; x=1737592544; darn=gcc.gnu.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=lGXRu8b+C2nMvcxaNjtW6bO5c/By1nYJ0jT1lXCcor0=; b=gNMaqDWLr1RjocaC2bLc04brfmUXaxi8wGCemT+PupgTzFoSQZLFWpdGryckctxi4Q U1qhELWD33gVwrJEZg4i2Zf+XN/XiJKLu0I7bprGB9OOUo48hyZYsbqzOyULB0zcTsKW weuPt7MAdUzW28TV2UWJBXhl0n8Bh2vCDu+jASC8mJDm2fxsZJ6X7BLpjTtzstshEEmM FbxwfAFccXEBKXWp5+pUhanvsyUuT5MQ/6ujcLy7uOET5j515eaNltcKHwEq6oS9uvmF 5+M8r8gdedzbnUZIDEt5reCi91L9bFaCFPVaP2XTV5RHdtjXY8so/hTAZaP8Kc5l+w5I pioQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736987744; x=1737592544; 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=lGXRu8b+C2nMvcxaNjtW6bO5c/By1nYJ0jT1lXCcor0=; b=dbkcYpzpP2xVTzRM7E0/02l/vLPE42iy/FPgEIDZjREf+hXcNouMuMgsyEKPeWnyPF hbKjXeYkyfA55AJgxU1dQw9RslF1R3PyV22BM0fNDiuBzyo6/1jA9PrUAjOhWsc8Hiik 4ThaHhAuQejH7l3+L7GAtutY550w1f595wXzKfE/UdhHmet8XUg32mMzriHBdtZb4EKv 8pzsC0IAxCZkr4HPmgSQqX1Ny7qsWRXczDG4F8TjXYay4D5KqpiV+bROvJGh1Wb7NLEj JgvxlbsTKjfnkUJJf3U/gTs95XcCfjWjZpRs52Sx1SeH1/oI85FpWdBml01cDmEEstVB XNSw== X-Gm-Message-State: AOJu0YwIFhxQVSIMh8Lqf74vyf4rzXR5uQ3joy+h/4nVe4PVXA/bcM7Z erazi/ssatPeADR9egTe+v4R3BRuetT8P1T2WGPrhgSTOtKG/nbplaK3dOBgMnAyVxWobh7cdX0 062zc6+NKhT/lmf7fCF5RDmr6Sk9VhD3PZPZD/ynUjRtQM7JrZLBk98U1S/5dpA6t9K4ZOmTI2o jn/QMfSgiLI59RV4cx54mvztnHp1I9lG55cnNXWerneA== X-Gm-Gg: ASbGnct+i5kPRrGW1WatHiWDSOV5tDMn9ibUBq82EFdXgzcqltN5P47vfJdZhOPKb+R FSdvjERn/IubxVvhQgovv23DSbQ0dp4XNl8Vo9HB/NALNd1zs5RCGrpKE2O1WQJr8IVngYWuzoN lt+qWP4yZHe36+0AQWwqAhzBxuVCDk08EqXZII3Wj+efcdUxSuGzBtHOhoUkOUs99SCGJziPB4U CwqqeGaw9Cq/6g1GS3fDZ9QHaY1mClPNp+8dxhlAjgB2aCgd52qznoKL50c0tyDhVdanFYzsEqR LWWltXSJ X-Google-Smtp-Source: AGHT+IEyGO+f7vc5jj1wHmsAZcNKvy6KAgKHewLiraWwCoUxaqv9lq+VgZLWjx+wujSwKIMVh1ZwJw== X-Received: by 2002:a17:902:d48b:b0:216:26f1:530b with SMTP id d9443c01a7336-21a83fd0bd9mr435892595ad.51.1736987741879; Wed, 15 Jan 2025 16:35:41 -0800 (PST) Received: from hsinchu02.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f2538e8sm86869125ad.218.2025.01.15.16.35.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 16:35:41 -0800 (PST) From: Monk Chiang To: gcc-patches@gcc.gnu.org, kito@gcc.gnu.org Cc: Monk Chiang Subject: [PATCH v4 4/4] RISC-V: Add -fcf-protection=[full|branch|return] to enable zicfiss, zicfilp. Date: Thu, 16 Jan 2025 08:35:23 +0800 Message-ID: <20250116003524.84399-4-monk.chiang@sifive.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116003524.84399-1-monk.chiang@sifive.com> References: <20250116003524.84399-1-monk.chiang@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org gcc/ChangeLog: * gcc/config/riscv/riscv.cc (is_zicfilp_p): New function. (is_zicfiss_p): New function. * gcc/config/riscv/riscv-zicfilp.cc: Update. * gcc/config/riscv/riscv.h: Update. * gcc/config/riscv/riscv.md: Update. gcc/testsuite/ChangeLog: * gcc/testsuite/c-c++-common/fcf-protection-1.c: Update. * gcc/testsuite/c-c++-common/fcf-protection-2.c: Update. * gcc/testsuite/c-c++-common/fcf-protection-3.c: Update. * gcc/testsuite/c-c++-common/fcf-protection-4.c: Update. * gcc/testsuite/c-c++-common/fcf-protection-5.c: Update. * gcc/testsuite/c-c++-common/fcf-protection-6.c: Update. * gcc/testsuite/c-c++-common/fcf-protection-7.c: Update. * gcc/testsuite/gcc.target/riscv/ssp-1.c: Update. * gcc/testsuite/gcc.target/riscv/ssp-2.c: Update. * gcc/testsuite/gcc.target/riscv/zicfilp-call.c: Update. * gcc/testsuite/gcc.target/riscv/interrupt-no-lpad.c: Update. --- gcc/config/riscv/riscv-zicfilp.cc | 2 +- gcc/config/riscv/riscv.cc | 52 +++++++++++++++---- gcc/config/riscv/riscv.h | 8 +-- gcc/config/riscv/riscv.md | 10 ++-- gcc/testsuite/c-c++-common/fcf-protection-1.c | 1 + gcc/testsuite/c-c++-common/fcf-protection-2.c | 1 + gcc/testsuite/c-c++-common/fcf-protection-3.c | 1 + gcc/testsuite/c-c++-common/fcf-protection-4.c | 1 + gcc/testsuite/c-c++-common/fcf-protection-5.c | 1 + gcc/testsuite/c-c++-common/fcf-protection-6.c | 1 + gcc/testsuite/c-c++-common/fcf-protection-7.c | 1 + .../gcc.target/riscv/interrupt-no-lpad.c | 2 +- gcc/testsuite/gcc.target/riscv/ssp-1.c | 2 +- gcc/testsuite/gcc.target/riscv/ssp-2.c | 2 +- gcc/testsuite/gcc.target/riscv/zicfilp-call.c | 2 +- 15 files changed, 63 insertions(+), 24 deletions(-) diff --git a/gcc/config/riscv/riscv-zicfilp.cc b/gcc/config/riscv/riscv-zicfilp.cc index 42b129920b3..834d6e5c778 100644 --- a/gcc/config/riscv/riscv-zicfilp.cc +++ b/gcc/config/riscv/riscv-zicfilp.cc @@ -150,7 +150,7 @@ public: /* opt_pass methods: */ virtual bool gate (function *) { - return TARGET_ZICFILP; + return is_zicfilp_p (); } virtual unsigned int execute (function *) diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index cb448aba9c0..8a4e23851d7 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -6682,7 +6682,7 @@ riscv_legitimize_call_address (rtx addr) rtx reg = RISCV_CALL_ADDRESS_TEMP (Pmode); riscv_emit_move (reg, addr); - if (TARGET_ZICFILP) + if (is_zicfilp_p ()) { rtx sw_guarded = RISCV_CALL_ADDRESS_LPAD (Pmode); emit_insn (gen_set_guarded (Pmode, reg)); @@ -6692,7 +6692,7 @@ riscv_legitimize_call_address (rtx addr) return reg; } - if (TARGET_ZICFILP && REG_P (addr)) + if (is_zicfilp_p () && REG_P (addr)) emit_insn (gen_set_lpl (Pmode, const0_rtx)); return addr; @@ -7508,7 +7508,7 @@ riscv_save_reg_p (unsigned int regno) if (regno == GP_REGNUM || regno == THREAD_POINTER_REGNUM) return false; - if (regno == RETURN_ADDR_REGNUM && TARGET_ZICFISS) + if (regno == RETURN_ADDR_REGNUM && is_zicfiss_p ()) return true; /* We must save every register used in this function. If this is not a @@ -10341,10 +10341,10 @@ riscv_file_end () long GNU_PROPERTY_RISCV_FEATURE_1_AND = 0; unsigned long feature_1_and = 0; - if (TARGET_ZICFISS) + if (is_zicfilp_p ()) feature_1_and |= 0x1 << 0; - if (TARGET_ZICFILP) + if (is_zicfiss_p ()) feature_1_and |= 0x1 << 1; if (feature_1_and) @@ -10404,7 +10404,7 @@ riscv_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED, /* Mark the end of the (empty) prologue. */ emit_note (NOTE_INSN_PROLOGUE_END); - if (TARGET_ZICFILP) + if (is_zicfilp_p ()) emit_insn(gen_lpad (const0_rtx)); /* Determine if we can use a sibcall to call FUNCTION directly. */ @@ -10631,6 +10631,20 @@ riscv_override_options_internal (struct gcc_options *opts) /* Convert -march and -mrvv-vector-bits to a chunks count. */ riscv_vector_chunks = riscv_convert_vector_chunks (opts); + + if (opts->x_flag_cf_protection != CF_NONE) + { + if ((opts->x_flag_cf_protection & CF_RETURN) == CF_RETURN + && !TARGET_ZICFISS) + error ("%<-fcf-protection%> is not compatible with this target"); + + if ((opts->x_flag_cf_protection & CF_BRANCH) == CF_BRANCH + && !TARGET_ZICFILP) + error ("%<-fcf-protection%> is not compatible with this target"); + + opts->x_flag_cf_protection + = (cf_protection_level) (opts->x_flag_cf_protection | CF_SET); + } } /* Implement TARGET_OPTION_OVERRIDE. */ @@ -10925,7 +10939,7 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) /* Work out the offsets of the pointers from the start of the trampoline code. */ - if (!TARGET_ZICFILP) + if (!is_zicfilp_p ()) gcc_assert (ARRAY_SIZE (trampoline) * 4 == TRAMPOLINE_CODE_SIZE); else gcc_assert (ARRAY_SIZE (trampoline_cfi) * 4 == TRAMPOLINE_CODE_SIZE); @@ -10953,7 +10967,7 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) unsigned insn_count = 0; /* Insert lpad, if zicfilp is enabled. */ - if (TARGET_ZICFILP) + if (is_zicfilp_p ()) { unsigned HOST_WIDE_INT lpad_code; lpad_code = OPCODE_AUIPC | (0 << SHIFT_RD) | (lp_value << IMM_BITS); @@ -11015,7 +11029,7 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) insn_count++; /* For zicfilp only, insert lui t2, 1, because use jr t0. */ - if (TARGET_ZICFILP) + if (is_zicfilp_p ()) { unsigned HOST_WIDE_INT set_lpl_code; set_lpl_code = OPCODE_LUI @@ -11045,7 +11059,7 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value) static_chain_offset = TRAMPOLINE_CODE_SIZE; target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode); - if (!TARGET_ZICFILP) + if (!is_zicfilp_p ()) { /* auipc t2, 0 l[wd] t0, (target_function_offset)(t2) @@ -13960,9 +13974,25 @@ expand_reversed_crc_using_clmul (scalar_mode crc_mode, scalar_mode data_mode, riscv_emit_move (operands[0], gen_lowpart (crc_mode, a0)); } +bool is_zicfiss_p () +{ + if (TARGET_ZICFISS && (flag_cf_protection & CF_RETURN)) + return true; + + return false; +} + +bool is_zicfilp_p () +{ + if (TARGET_ZICFILP && (flag_cf_protection & CF_BRANCH)) + return true; + + return false; +} + bool need_shadow_stack_push_pop_p () { - return TARGET_ZICFISS && riscv_save_return_addr_reg_p (); + return is_zicfiss_p () && riscv_save_return_addr_reg_p (); } /* Initialize the GCC target structure. */ diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index 073b9e4f2bd..2bcabd03517 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -191,7 +191,7 @@ ASM_MISA_SPEC /* Allocation boundary (in *bits*) for the code of a function. */ #define FUNCTION_BOUNDARY \ - (((TARGET_RVC || TARGET_ZCA) && !TARGET_ZICFILP) ? 16 : 32) + (((TARGET_RVC || TARGET_ZCA) && !is_zicfilp_p ()) ? 16 : 32) /* The smallest supported stack boundary the calling convention supports. */ #define STACK_BOUNDARY \ @@ -415,7 +415,7 @@ ASM_MISA_SPEC /* Register in which static-chain is passed to a function. */ #define STATIC_CHAIN_REGNUM \ - ((TARGET_ZICFILP) ? (GP_TEMP_FIRST + 23) : (GP_TEMP_FIRST + 2)) + ((is_zicfilp_p ()) ? (GP_TEMP_FIRST + 23) : (GP_TEMP_FIRST + 2)) /* Registers used as temporaries in prologue/epilogue code. @@ -827,7 +827,7 @@ extern enum riscv_cc get_riscv_cc (const rtx use); /* Trampolines are a block of code followed by two pointers. */ -#define TRAMPOLINE_CODE_SIZE ((TARGET_ZICFILP) ? 24 : 16) +#define TRAMPOLINE_CODE_SIZE ((is_zicfilp_p ()) ? 24 : 16) #define TRAMPOLINE_SIZE \ ((Pmode == SImode) \ @@ -1196,6 +1196,8 @@ extern poly_int64 riscv_v_adjust_nunits (enum machine_mode, int); extern poly_int64 riscv_v_adjust_nunits (machine_mode, bool, int, int); extern poly_int64 riscv_v_adjust_precision (enum machine_mode, int); extern poly_int64 riscv_v_adjust_bytesize (enum machine_mode, int); +extern bool is_zicfiss_p (); +extern bool is_zicfilp_p (); extern bool need_shadow_stack_push_pop_p (); /* The number of bits and bytes in a RVV vector. */ #define BITS_PER_RISCV_VECTOR (poly_uint16 (riscv_vector_chunks * riscv_bytes_per_vector_chunk * 8)) diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index 5b4c81bb9e0..d626a350ee4 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -3716,11 +3716,11 @@ [(set (pc) (match_operand 0 "register_operand"))] "" { - if (TARGET_ZICFILP) + if (is_zicfilp_p ()) emit_insn (gen_set_lpl (Pmode, const0_rtx)); operands[0] = force_reg (Pmode, operands[0]); - if (TARGET_ZICFILP) + if (is_zicfilp_p ()) emit_use (gen_rtx_REG (Pmode, T2_REGNUM)); if (Pmode == SImode) @@ -3748,7 +3748,7 @@ gen_rtx_LABEL_REF (Pmode, operands[1]), NULL_RTX, 0, OPTAB_DIRECT); - if (TARGET_ZICFILP) + if (is_zicfilp_p ()) { rtx t2 = RISCV_CALL_ADDRESS_LPAD (GET_MODE (operands[0])); emit_move_insn (t2, operands[0]); @@ -3771,7 +3771,7 @@ (define_insn "tablejump" [(set (pc) (match_operand:GPR 0 "register_operand" "l")) (use (label_ref (match_operand 1 "" "")))] - "!TARGET_ZICFILP" + "!is_zicfilp_p ()" "jr\t%0" [(set_attr "type" "jalr") (set_attr "mode" "none")]) @@ -3779,7 +3779,7 @@ (define_insn "tablejump_cfi" [(set (pc) (reg:GPR T2_REGNUM)) (use (label_ref (match_operand 0 "")))] - "TARGET_ZICFILP" + "is_zicfilp_p ()" "jr\tt2" [(set_attr "type" "jalr") (set_attr "mode" "none")]) diff --git a/gcc/testsuite/c-c++-common/fcf-protection-1.c b/gcc/testsuite/c-c++-common/fcf-protection-1.c index f59a8fbdfdc..cc30c921a16 100644 --- a/gcc/testsuite/c-c++-common/fcf-protection-1.c +++ b/gcc/testsuite/c-c++-common/fcf-protection-1.c @@ -1,3 +1,4 @@ /* { dg-do compile } */ /* { dg-options "-fcf-protection=full" } */ +/* { dg-skip-if "" { "riscv*-*-*" } } */ /* { dg-error "'-fcf-protection=full' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */ diff --git a/gcc/testsuite/c-c++-common/fcf-protection-2.c b/gcc/testsuite/c-c++-common/fcf-protection-2.c index 61059725af6..e318989f290 100644 --- a/gcc/testsuite/c-c++-common/fcf-protection-2.c +++ b/gcc/testsuite/c-c++-common/fcf-protection-2.c @@ -1,3 +1,4 @@ /* { dg-do compile } */ /* { dg-options "-fcf-protection=branch" } */ +/* { dg-skip-if "" { "riscv*-*-*" } } */ /* { dg-error "'-fcf-protection=branch' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */ diff --git a/gcc/testsuite/c-c++-common/fcf-protection-3.c b/gcc/testsuite/c-c++-common/fcf-protection-3.c index 257e944c4a6..dd60017dde0 100644 --- a/gcc/testsuite/c-c++-common/fcf-protection-3.c +++ b/gcc/testsuite/c-c++-common/fcf-protection-3.c @@ -1,3 +1,4 @@ /* { dg-do compile } */ /* { dg-options "-fcf-protection=return" } */ +/* { dg-skip-if "" { "riscv*-*-*" } } */ /* { dg-error "'-fcf-protection=return' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */ diff --git a/gcc/testsuite/c-c++-common/fcf-protection-4.c b/gcc/testsuite/c-c++-common/fcf-protection-4.c index af4fc0b2812..2bde6e1e767 100644 --- a/gcc/testsuite/c-c++-common/fcf-protection-4.c +++ b/gcc/testsuite/c-c++-common/fcf-protection-4.c @@ -1,2 +1,3 @@ /* { dg-do compile } */ +/* { dg-skip-if "" { "riscv*-*-*" } } */ /* { dg-options "-fcf-protection=none" } */ diff --git a/gcc/testsuite/c-c++-common/fcf-protection-5.c b/gcc/testsuite/c-c++-common/fcf-protection-5.c index dc317f84b07..8c675c5f483 100644 --- a/gcc/testsuite/c-c++-common/fcf-protection-5.c +++ b/gcc/testsuite/c-c++-common/fcf-protection-5.c @@ -1,3 +1,4 @@ /* { dg-do compile } */ /* { dg-options "-fcf-protection" } */ +/* { dg-skip-if "" { "riscv*-*-*" } } */ /* { dg-error "'-fcf-protection=full' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */ diff --git a/gcc/testsuite/c-c++-common/fcf-protection-6.c b/gcc/testsuite/c-c++-common/fcf-protection-6.c index 61059725af6..e318989f290 100644 --- a/gcc/testsuite/c-c++-common/fcf-protection-6.c +++ b/gcc/testsuite/c-c++-common/fcf-protection-6.c @@ -1,3 +1,4 @@ /* { dg-do compile } */ /* { dg-options "-fcf-protection=branch" } */ +/* { dg-skip-if "" { "riscv*-*-*" } } */ /* { dg-error "'-fcf-protection=branch' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */ diff --git a/gcc/testsuite/c-c++-common/fcf-protection-7.c b/gcc/testsuite/c-c++-common/fcf-protection-7.c index 257e944c4a6..dd60017dde0 100644 --- a/gcc/testsuite/c-c++-common/fcf-protection-7.c +++ b/gcc/testsuite/c-c++-common/fcf-protection-7.c @@ -1,3 +1,4 @@ /* { dg-do compile } */ /* { dg-options "-fcf-protection=return" } */ +/* { dg-skip-if "" { "riscv*-*-*" } } */ /* { dg-error "'-fcf-protection=return' is not supported for this target" "" { target { ! "i?86-*-* x86_64-*-*" } } 0 } */ diff --git a/gcc/testsuite/gcc.target/riscv/interrupt-no-lpad.c b/gcc/testsuite/gcc.target/riscv/interrupt-no-lpad.c index ff512b98c61..1290b373f08 100644 --- a/gcc/testsuite/gcc.target/riscv/interrupt-no-lpad.c +++ b/gcc/testsuite/gcc.target/riscv/interrupt-no-lpad.c @@ -1,5 +1,5 @@ /* { dg-do compile { target { riscv64*-*-* } } } */ -/* { dg-options "-march=rv64gc_zicfilp -mabi=lp64d" } */ +/* { dg-options "-march=rv64gc_zicfilp -mabi=lp64d -fcf-protection=branch" } */ void __attribute__ ((interrupt)) foo (void) { diff --git a/gcc/testsuite/gcc.target/riscv/ssp-1.c b/gcc/testsuite/gcc.target/riscv/ssp-1.c index abf47ec6442..24e3010c3cc 100644 --- a/gcc/testsuite/gcc.target/riscv/ssp-1.c +++ b/gcc/testsuite/gcc.target/riscv/ssp-1.c @@ -1,5 +1,5 @@ /* { dg-do compile { target { riscv64*-*-* } } } */ -/* { dg-options "-O2 -march=rv64gc_zicfiss -mabi=lp64d" } */ +/* { dg-options "-O2 -march=rv64gc_zicfiss -mabi=lp64d -fcf-protection=return" } */ /* { dg-skip-if "" { *-*-* } { "-O0" } } */ struct ad { void *ae; diff --git a/gcc/testsuite/gcc.target/riscv/ssp-2.c b/gcc/testsuite/gcc.target/riscv/ssp-2.c index 7c6098357e9..9d3d6b27038 100644 --- a/gcc/testsuite/gcc.target/riscv/ssp-2.c +++ b/gcc/testsuite/gcc.target/riscv/ssp-2.c @@ -1,5 +1,5 @@ /* { dg-do compile { target { riscv64*-*-* } } } */ -/* { dg-options "-O0 -march=rv64gc_zicfiss -mabi=lp64d" } */ +/* { dg-options "-O0 -march=rv64gc_zicfiss -mabi=lp64d -fcf-protection=return" } */ void __attribute__ ((interrupt)) foo (void) diff --git a/gcc/testsuite/gcc.target/riscv/zicfilp-call.c b/gcc/testsuite/gcc.target/riscv/zicfilp-call.c index 75c8b32faa3..eb9e1468e7f 100644 --- a/gcc/testsuite/gcc.target/riscv/zicfilp-call.c +++ b/gcc/testsuite/gcc.target/riscv/zicfilp-call.c @@ -1,5 +1,5 @@ /* { dg-do compile { target { riscv64*-*-* } } } */ -/* { dg-options "-O2 -fPIE -march=rv64gc_zicfilp -mabi=lp64d" } */ +/* { dg-options "-O2 -fPIE -march=rv64gc_zicfilp -mabi=lp64d -fcf-protection=branch" } */ /* { dg-skip-if "" { *-*-* } { "-O0" } } */ extern void _dl_find_object_init (void);