From patchwork Thu Dec 8 07:41:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tiezhu Yang X-Patchwork-Id: 2760 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp56329wrr; Wed, 7 Dec 2022 23:43:11 -0800 (PST) X-Google-Smtp-Source: AA0mqf54XZxft9AkJpbc58U71Etsh/QsKXkIwvD4A1IUlaVAHO2FVJtQDTu5JyWsXC9ELev4E03s X-Received: by 2002:aa7:d5c8:0:b0:46d:a00e:1ac0 with SMTP id d8-20020aa7d5c8000000b0046da00e1ac0mr545111eds.154.1670485391032; Wed, 07 Dec 2022 23:43:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670485391; cv=none; d=google.com; s=arc-20160816; b=JwRt8NViyYcmrec6Q/1SChYlYYuoRIJFICanTY5Jg2++dxlOhCC6gPtfQIyD0NAneh dji7UB9oCYhdtPUqArk1Iz5T2uDHkMdai8f00VhD+CvEYfUqPt/uVyVHBD6lF617ivHh A3k57SzK9/dRlS+TqDgGHocYraIPYVNlt1eeGsbULWPIbqXMKBvgVSzRmZE0v3Hoev2T o+UKQwY7aMhiN4ue5cqK4K1EUUte3ikMZUEX4g5gblQxjW0Njn8xBJms9u3+qxNkZL4R /LVJP+KXK1pOpTNgbs4xrh1bP2wEs9Kvj7k8SgE9LB3gihtiXgvlkMvsPKYSMhl5D3yb obvg== 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=i4r9/HKry5YAqDbrloiTT2eaFkfaHgHSkqU3euZsed4=; b=FY/5JaRr78ODqukqdEZxgZlTpioDnKtuOSxwZkMdYNUzpSk1KNnQqhSWu8Z8Jd24sQ NpmpS339dnEG/HDZen8BIwW0BSEPkLgITP3qRouPeEjKidcq6xe26/MEbxLAMsCU/KCW NdYwKFfCyRlgkN7I0mUKOT4krEfvGm1WqKU15xsUl5fD1BeCtelxWDs6zdvQ3QyWtsr8 gZsi9mgaT+OxjxKpB3Z8g1nUzLboEpflCOwmIgqfKI5SmBEgvWjoFmOssAmQVI/DQbCa bXjcU7RoGZy2nz+6EXygDh5hu71REk2+WffEIFK+Zs4f3RR4V2zMWIYy7fzPl4QMJVJ2 DPYQ== 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 cn5-20020a0564020ca500b0046ad8dfcfbasi5015505edb.344.2022.12.07.23.42.48; Wed, 07 Dec 2022 23:43:11 -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 S229745AbiLHHla (ORCPT + 99 others); Thu, 8 Dec 2022 02:41:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbiLHHl1 (ORCPT ); Thu, 8 Dec 2022 02:41:27 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 92FEA4667A for ; Wed, 7 Dec 2022 23:41:25 -0800 (PST) Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8AxCekklZFjjg0EAA--.6053S3; Thu, 08 Dec 2022 15:41:24 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8AxPuAjlZFjn8gnAA--.32821S2; Thu, 08 Dec 2022 15:41:24 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , WANG Xuerui , Masami Hiramatsu Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v8 0/4] Add kprobe and kretprobe support for LoongArch Date: Thu, 8 Dec 2022 15:41:09 +0800 Message-Id: <1670485273-15717-1-git-send-email-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.1.0 X-CM-TRANSID: AQAAf8AxPuAjlZFjn8gnAA--.32821S2 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBjvJXoW7ZFy8JFW7uw47Ww4DZFyrZwb_yoW8Kr1DpF 9rAwn5Kr4rWr93Ar9xJw1Uur1fta1kC3y7W3W7JrW8Z3W7ZryUArs3KrZrXa48Gws0qr1S qr1rXrWagFy7J37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU b7AYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_Jrv_JF1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVWUCVW8JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwA2z4 x0Y4vEx4A2jsIE14v26r4UJVWxJr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4UJVWxJr1l e2I262IYc4CY6c8Ij28IcVAaY2xG8wAqjxCEc2xF0cIa020Ex4CE44I27wAqx4xG64xvF2 IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_JrI_JrylYx0Ex4A2jsIE14v26r1j6r4U McvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCF04k20xvY0x0EwIxGrwCFx2 IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v2 6r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67 AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IY s7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Jr 0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU8j-e5UUUUU== X-Spam-Status: No, score=1.4 required=5.0 tests=BAYES_00,RCVD_IN_SBL_CSS, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: * 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?1751630889615514985?= 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 | 333 +++++++++++++++++++++++++++++ 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, 663 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