From patchwork Fri Dec 9 08:52:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tiezhu Yang X-Patchwork-Id: 2807 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp659008wrr; Fri, 9 Dec 2022 01:00:42 -0800 (PST) X-Google-Smtp-Source: AA0mqf5+0lfH3meMJWdFyJsEAfxWGp95SVWbKkSWWIEVDIcca/GMZiXE/bbFa9omShbqVUhw+NRt X-Received: by 2002:a05:6a20:6f90:b0:a3:9ca0:b982 with SMTP id gv16-20020a056a206f9000b000a39ca0b982mr6780390pzb.26.1670576441761; Fri, 09 Dec 2022 01:00:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670576441; cv=none; d=google.com; s=arc-20160816; b=M1qICnu7EOs3IGw29lS52E2LaL/f/dh/6xOAdKRIXeHvuZMqBQdtfXqwjSrR2rT94t cMlJsognZaloCMR3LsT1eqjoYCNffp3w70Ujk+LT0JOURb/tuWef39M5WBjrVYGnGEf3 ju8P+Hj03xEhaRABYsJRz1HxV1BZQNIUg8eb0g875ijJvvykI3KQ8NrIESs5eidzCC4p i60xt/mhfKboJBauFsDrSCU+fA7AprAr0nWil5ligJbVGPjYs2RS3EuihYfTxOWlg6M4 fERTZh0VFPiFGZM7m179myD50cH5b4+pFkysPynr1vUDJrzkhDIpCroHWeFqN4xDjwZY rt+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=IlUloWDB/GiVY/KwwDC/vffMboC41ZCWiYg44s4g39U=; b=q4p91Ys8+L7hGi9LNsXZys00cHrtyvb9YApCEpnP8uC3aZLsgn5tllIvraPcMZ0w4q YKZeWQY3FTcMFOURrwnWg9R+jc9e1K35qclxg7rjfjvlurmaM2nXApwEvNTSiL/xNT5t tbNm8UM+hT1jJqTF8YZMHMav1Kp3WuPv77njCxI9ozjLNRBp5C9YUf87l89Mka80RTtb zVjTv3OHvivLciCzt+6IepAWWqyjWOEgWJIfJVJJ59/Vx6aWwzV2kMHPKZH7NuogmFdm nwWqQpg1f/9p/fQr8I5+cuKEhWliTe+H17BCkGtcuv8YT86tIe3KXCY63H1jtva1fhdD SgXw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n4-20020a170902f60400b00186a2d376afsi43396plg.273.2022.12.09.01.00.27; Fri, 09 Dec 2022 01:00:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229886AbiLIIxN (ORCPT + 99 others); Fri, 9 Dec 2022 03:53:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229890AbiLIIxJ (ORCPT ); Fri, 9 Dec 2022 03:53:09 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9ECFD31EE6 for ; Fri, 9 Dec 2022 00:53:06 -0800 (PST) Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8Bxnutw95JjEGAEAA--.10359S3; Fri, 09 Dec 2022 16:53:05 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxV1dt95Jj7xUpAA--.16661S2; Fri, 09 Dec 2022 16:53:02 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , WANG Xuerui , Masami Hiramatsu Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v10 0/4] Add kprobe and kretprobe support for LoongArch Date: Fri, 9 Dec 2022 16:52:57 +0800 Message-Id: <1670575981-14389-1-git-send-email-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.1.0 X-CM-TRANSID: AQAAf8BxV1dt95Jj7xUpAA--.16661S2 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBjvJXoWxJF4furW8Xr1rJry3KrW3Wrg_yoW5XFyDpF 9rAwn5Kr4rWrn3Zr9xta4Uur1Sqw4kG3y7W3W7JrWrZ3W7ZryUAr4xKrZxXFy8Gws0qr1S qr1fXrWagFyfGa7anT9S1TB71UUUUUJqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU b38YFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_JrI_Jryl8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVWUCVW8JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwA2z4 x0Y4vEx4A2jsIE14v26F4UJVW0owA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Cr1j6rxdM2kK e7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zVCFFI 0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUXVWUAwAv7VC2z280 aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JMxAIw28Icx kI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v26r1Y6r17MI8I3I0E 5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAV WUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY 1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI 0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjxU 4oGQDUUUU X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751726363014421923?= X-GMAIL-MSGID: =?utf-8?q?1751726363014421923?= v10: -- Remove sign_extend() based on the latest code -- Rename insns_are_not_supported() to insns_not_supported() -- Rename insns_are_not_simulated() to insns_not_simulated() -- Set KPROBE_HIT_SSDONE if cur->post_handler is not NULL -- Enable preemption for KPROBE_REENTER in kprobe_fault_handler() v9: -- Rename sign_extended() to sign_extend() -- Modify kprobe_fault_handler() to handle all of kprobe_status v8: -- Put "regs->csr_prmd &= ~CSR_PRMD_PIE;" ahead to save one line -- Add code comment of preempt_disable() -- Put kprobe_page_fault() in __do_page_fault() -- Modify the check condition of break insn in kprobe_breakpoint_handler() v7: -- Remove stop_machine_cpuslocked() related code v6: -- Add a new patch to redefine larch_insn_patch_text() with stop_machine_cpuslocked() -- Modify kprobe_breakpoint_handler() to consider the original insn is break and return the correct value -- Modify do_bp() to refresh bcode when original insn is break v5: -- Rebase on the latest code -- Use stop_machine_cpuslocked() to modify insn to avoid CPU race v4: -- Remove kprobe_exceptions_notify() in kprobes.c -- Call kprobe_breakpoint_handler() and kprobe_singlestep_handler() in do_bp() v3: -- Rebase on the latest code -- Check the alignment of PC in simu_branch() and simu_pc() -- Add ibar in flush_insn_slot() -- Rename kprobe_{pre,post}_handler() to {post_}kprobe_handler -- Add preempt_disable() and preempt_enable_no_resched() -- Remove r0 save/restore and do some minor changes in kprobes_trampoline.S -- Do not enable CONFIG_KPROBES by default v2: -- Split simu_branch() and simu_pc() into a single patch -- Call kprobe_page_fault() in do_page_fault() -- Add kprobes_trampoline.S for kretprobe Tiezhu Yang (4): LoongArch: Simulate branch and PC instructions LoongArch: Add kprobe support LoongArch: Add kretprobe support samples/kprobes: Add LoongArch support arch/loongarch/Kconfig | 2 + arch/loongarch/include/asm/inst.h | 20 ++ arch/loongarch/include/asm/kprobes.h | 59 +++++ arch/loongarch/include/asm/ptrace.h | 1 + arch/loongarch/kernel/Makefile | 2 + arch/loongarch/kernel/inst.c | 123 ++++++++++ arch/loongarch/kernel/kprobes.c | 364 +++++++++++++++++++++++++++++ arch/loongarch/kernel/kprobes_trampoline.S | 96 ++++++++ arch/loongarch/kernel/traps.c | 13 +- arch/loongarch/mm/fault.c | 3 + samples/kprobes/kprobe_example.c | 8 + 11 files changed, 687 insertions(+), 4 deletions(-) create mode 100644 arch/loongarch/include/asm/kprobes.h create mode 100644 arch/loongarch/kernel/kprobes.c create mode 100644 arch/loongarch/kernel/kprobes_trampoline.S