From patchwork Thu Dec 8 13:41:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tiezhu Yang X-Patchwork-Id: 2775 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp204848wrr; Thu, 8 Dec 2022 05:50:54 -0800 (PST) X-Google-Smtp-Source: AA0mqf4224qzuMGqGdhPWqk/wviRxtZKjMMCLEKEF+q0OSAhMb+3mN2XN6Qn7MCdqtQbV12FVZYU X-Received: by 2002:a17:902:e313:b0:189:aee3:a185 with SMTP id q19-20020a170902e31300b00189aee3a185mr35850323plc.21.1670507453721; Thu, 08 Dec 2022 05:50:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670507453; cv=none; d=google.com; s=arc-20160816; b=kxh/JhoGqRvJKFC/BGpfIvUn3f+dQoNKNLzR8StNk5FDtH0IwnnmkW4gKmAnU9KozQ O/ncBh5dBIqiUfgiuLPslD+VpeeHXzO3gFihEVYl1Z9FxBk67IAMoz2MRtM6W9ZG47xK 0UiEjobOdDdkrIpDQtvfxb/zYcQsRk2ALxUiznJ6LE4uNk1msEhMjWvmjEcHR3/6DnGf 6rrN4bRcAP9SpZj1OnvIMIYwJ7aq9lReT/m1ImuD7q9PRsEpNZRj2QJciclN6BwW0tpj 7YpXyq/yc4VhKMPdfrO8h/cqECpZpoQU6PkzXjOe3dWQtLGC963A/wb0UZ/OYe5tk+FY 1Ntw== 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=bRWvUvT8u9ANGyBtL0QiDKPBNUzWKW33tjBFoN3ne6o=; b=rPZUm69cfUbzB/xc3hA6V4gzKvo/rbao4gFFjYUT3ubvq/ueLcQAhKbz4R+a+2eZ5m jjFm0R8cDJeEfrxzy9vkUL9JNDzdJ8k2oOgN53haJ6e3uY1jy/FX9CBnQyNLo7HaSg+o HHn1y40f3uHSPz133MH9JN9gHVgpfkvuMtGjDwmas54XwIC+ULP2xuJKKbj4x5VL/Z59 YSWPUtHK8moof/xRlnM7LGsKXhQWGSWNCGIZ5OAMhw8YnSADGB1H8pMSDzB7hI7HDa48 cyGyCkEHJhxddL234hoJDhRWC8ED/93CXmPk4aBKNdpevZV8EU4Oaw2f+Tn2uG4o7if7 jqqQ== 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 pb17-20020a17090b3c1100b00213dc988b24si4481015pjb.13.2022.12.08.05.50.39; Thu, 08 Dec 2022 05:50:53 -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 S229784AbiLHNlQ (ORCPT + 99 others); Thu, 8 Dec 2022 08:41:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229807AbiLHNlO (ORCPT ); Thu, 8 Dec 2022 08:41:14 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B25118B387 for ; Thu, 8 Dec 2022 05:41:12 -0800 (PST) Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8BxE_B16ZFjFR8EAA--.10025S3; Thu, 08 Dec 2022 21:41:10 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxTuB06ZFj8BwoAA--.33104S2; Thu, 08 Dec 2022 21:41:08 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , WANG Xuerui , Masami Hiramatsu Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v9 0/4] Add kprobe and kretprobe support for LoongArch Date: Thu, 8 Dec 2022 21:41:04 +0800 Message-Id: <1670506868-15771-1-git-send-email-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.1.0 X-CM-TRANSID: AQAAf8BxTuB06ZFj8BwoAA--.33104S2 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBjvJXoW7ZFy8JFW7uw47Ww4DZFyrZwb_yoW5Jr1rpF 9rAwn5Kr4rGr93Ar9xJ3WUur1fta1kC3y7W3W7JrW8Z3W7ZryUArs3KrZxXa48Gws0qr1S qr1rX3yagFy7J37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU b7kYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_JrI_Jryl8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVWUCVW8JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwA2z4 x0Y4vEx4A2jsIE14v26F4UJVW0owA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Cr1j6rxdM2AI xVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx1l5I8CrVACY4xI64 kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVW8JVWxJwAm 72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l4I8I3I 0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWU GVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI 0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0 rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r 4UJbIYCTnIWIevJa73UjIFyTuYvjxUcyxRUUUUU 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?1751630889615514985?= X-GMAIL-MSGID: =?utf-8?q?1751654023723043650?= 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 | 27 +++ 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 | 356 +++++++++++++++++++++++++++++ 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, 686 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