From patchwork Tue Jan 18 09:44:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kito Cheng X-Patchwork-Id: 50144 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 CD4AC3857C73 for ; Tue, 18 Jan 2022 09:44:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id 6F08C3857810 for ; Tue, 18 Jan 2022 09:44:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6F08C3857810 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pj1-x102e.google.com with SMTP id z17-20020a17090ab11100b001b4d8817e04so981385pjq.2 for ; Tue, 18 Jan 2022 01:44:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=svWUApm2jJ1pvPBee57iPVElHPUoqnyrzAjRaZj9v5w=; b=lsiZh5EotJiKYjAW1D43JEvKgBL/nUEHowdebIzipttwqLdyDOg6WwO4ezuL3QZlMm QSdCylOVL3j690MLiohatYqQxpuYeO8RzAQ2YPf0C0hugVahBT7TnTHRG3RgMtDRxK1Y yYXLGuvckIXtyLuxrkjxInAZp8SFqPF/VYopIEVVcW79+A71/6NJftJ80eInHUIcMbnK 840sl232WVve2D1wZKOBHhrQ7hwtle0PUGYb5WsHuNAkf/zvIwfWG9gXsje+Wc3+U2D6 pGB4JcMH0bu/hTeVNsLCSGFkMP9FqquEJnvSgwkBPeBAU329s7uxjEJn+sOz0bfqyw9a h6eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=svWUApm2jJ1pvPBee57iPVElHPUoqnyrzAjRaZj9v5w=; b=2vg+ijKu1biBH5JpM0Lxm1cCSPsL+pB75c8FTyDyjwGKohezfcg8KpynVj7qnaDxkF noaQZXh+VhH7DqsM078rxX3WpUBbAELcxI/aUQvd0X1W/SB0+bRKTZtX/vFGM8YjnA0W rUeD3oD3DAfILALWCYzYhvx4ercKJQByt7Ht4oLWdA/qBUyVMspG+0BaGfl6NVogdn6U zzP5PBjDBC/NI/pw6+ERTq3xHqvcNAhErx0BDclH7CF71ZfHXV361oHYQEAORT8bt8Uu q+UJ48kp1FH4nUYQJfvU457BLl/+Uhj38sxZOgb7fDd6DfOX4J2QI3M5gASpOgFJKDJf FDTQ== X-Gm-Message-State: AOAM5328xl1S+Dcm4v1SLgI45YDaRajTvyCcNq9oRiOnKSewES9oTUvW eX/+OPD3qlkBcE/IlQWx/igwzgZihZFhOw== X-Google-Smtp-Source: ABdhPJxL+MRF2TN88UxT2ckTPet8jcuW1+YwW1SZEBh6rj68nf5H6XB+tZUs6S8RsWwp04yS4V69YQ== X-Received: by 2002:a17:903:228b:b0:14a:d32a:ce17 with SMTP id b11-20020a170903228b00b0014ad32ace17mr2902990plh.35.1642499064103; Tue, 18 Jan 2022 01:44:24 -0800 (PST) Received: from hsinchu02.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id i3sm9688662pfu.36.2022.01.18.01.44.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jan 2022 01:44:23 -0800 (PST) From: Kito Cheng To: gcc-patches@gcc.gnu.org, kito.cheng@gmail.com, jim.wilson.gcc@gmail.com, palmer@dabbelt.com, andrew@sifive.com, vincent.chen@sifive.com Subject: [PATCH] RISC-V: Handle different sigcontext struct layout. Date: Tue, 18 Jan 2022 17:44:18 +0800 Message-Id: <20220118094418.4703-1-kito.cheng@sifive.com> X-Mailer: git-send-email 2.34.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kito Cheng Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" RISC-V glibc intend to removed its own `sigcontext.h`[1] and use the linux kernel's one, however the struct layout is slightly different between those two version, fortunately they have identical layout, so we just need a magical way to detect which one we are used. libgcc/ChangeLog: * config/riscv/linux-unwind.h (SIGCONTEXT_PC): New. (riscv_fallback_frame_state): Use SIGCONTEXT_PC rather than sc->gregs[i]. [1] https://sourceware.org/pipermail/libc-alpha/2022-January/135417.html --- libgcc/config/riscv/linux-unwind.h | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/libgcc/config/riscv/linux-unwind.h b/libgcc/config/riscv/linux-unwind.h index c86df2f85bc..4919facb36b 100644 --- a/libgcc/config/riscv/linux-unwind.h +++ b/libgcc/config/riscv/linux-unwind.h @@ -30,6 +30,16 @@ #define MD_FALLBACK_FRAME_STATE_FOR riscv_fallback_frame_state +/* RISC-V Glibc has removed its own sigcontext.h and use the linux kernel's + one, however the struct layout is little different between those two + version, fortunately they have identical layout, so we just need a + magical way to detect which one we are used. */ +#ifdef _ASM_RISCV_SIGCONTEXT_H +#define SIGCONTEXT_PC(SC) (SC)->sc_regs.pc +#else +#define SIGCONTEXT_PC(SC) (SC)->gregs[0] +#endif + static _Unwind_Reason_Code riscv_fallback_frame_state (struct _Unwind_Context *context, _Unwind_FrameState * fs) @@ -50,6 +60,8 @@ riscv_fallback_frame_state (struct _Unwind_Context *context, uint16_t *pc = context->ra; struct sigcontext *sc; int i; + /* Get regsister offest from register size. */ + _Unwind_Ptr reg_offset = __riscv_xlen / 8; /* A signal frame will have a return address pointing to __default_sa_restorer. This code is hardwired as: @@ -73,17 +85,23 @@ riscv_fallback_frame_state (struct _Unwind_Context *context, for (i = 0; i < 32; i++) { + /* Restore all registers value from kernel structures. + The corresponding bits in the Linux kernel are in + arch/riscv/include/asm/ptrace.h. */ fs->regs.reg[i].how = REG_SAVED_OFFSET; - fs->regs.reg[i].loc.offset = (_Unwind_Ptr) &sc->gregs[i] - new_cfa; + fs->regs.reg[i].loc.offset + = (_Unwind_Ptr) &SIGCONTEXT_PC (sc) + (i * reg_offset) - new_cfa; } fs->signal_frame = 1; fs->retaddr_column = __LIBGCC_DWARF_ALT_FRAME_RETURN_COLUMN__; fs->regs.reg[fs->retaddr_column].how = REG_SAVED_VAL_OFFSET; fs->regs.reg[fs->retaddr_column].loc.offset = - (_Unwind_Ptr) sc->gregs[0] - new_cfa; + (_Unwind_Ptr) SIGCONTEXT_PC (sc) - new_cfa; return _URC_NO_REASON; } +#undef SIGCONTEXT_PC + #endif