Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp284850rwl; Thu, 6 Apr 2023 19:17:02 -0700 (PDT) X-Google-Smtp-Source: AKy350YZtD2xzzvmoGkkbMOYnxlQ3YXn8BeIMb7ATyYBxwqJoDJzPbFu1PT7/HgnjQrtj9vVjX2X X-Received: by 2002:a17:906:ae55:b0:920:7a99:dcd4 with SMTP id lf21-20020a170906ae5500b009207a99dcd4mr595339ejb.62.1680833822343; Thu, 06 Apr 2023 19:17:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680833822; cv=none; d=google.com; s=arc-20160816; b=qVbzMKbebbPTQ0aaKbnTdnjxKwhmAibzR0HRB18tMDen+xDVi5VDqmB62j2YLiIfKK JNq/VOpMDKJOv/4/fvYFle1+Ru6T0ihLFUpcdWzcMGMJDT59D8/LEl+4NsfbrvtPP6rv NqY+YQbkEeCf6umLCzl5Xo9+T3SYWWp9jt0PR5bBSazSKO20mxlP1O6ZyMPh/f5z20TN T6T0e0dbsm0lmVDXwISD5yAeE934O0yd7ezRxHvtB2ePoX3yFXwMrbXrN5XvlgU2HE8f 2CSn6lvViDKAAUIY0XcefAqIfzMy5Nc4FnXMCDzxoLQKJzYIw7BzVYx7mdzI/XTz60qB EVaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=j3z5xDoToIXo21D7+wJZq6NfRhKw7dng3qQzFNRUG4U=; b=c940UV+kPnsnSbFm1ZQmRgIFCZv5JD8XiymFl4UzHfvr2ZQd35vbOX8JXCayyEpLpA ikTKkfWXOnNKi1OJc6xuWL1zWy9lAVl/gsyhNfhjdan3YxTSrglmeCNTAu1T4gVWPX4P zP4arT9bavYOZVpfaF1KUMElZeysq+TiSwii6/9ehbpuNSGSybz8mLmnusMjs57Bxp/o QEtppLhcqMWY3C0/KSOwFGSNA7eQIM+QGCNl7tXK3dXjySxnN8UivTnHwVRp6m2kLy9Q r7fwiSJgW3IGs587yuHb3zsYWsdnazvTtChJhVXJUmnobgGc/CNIvwMQ5LhTJayBuhPe ou1w== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id tz3-20020a170907c78300b0093112944d07si2445898ejc.44.2023.04.06.19.16.37; Thu, 06 Apr 2023 19:17:02 -0700 (PDT) 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 S240303AbjDGCPO (ORCPT + 99 others); Thu, 6 Apr 2023 22:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239932AbjDGCPG (ORCPT ); Thu, 6 Apr 2023 22:15:06 -0400 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 580B610C1 for ; Thu, 6 Apr 2023 19:15:05 -0700 (PDT) Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8Axz_+ofC9kmakXAA--.36751S3; Fri, 07 Apr 2023 10:15:04 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxSL2lfC9k2cEXAA--.21395S4; Fri, 07 Apr 2023 10:15:04 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , WANG Xuerui Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, loongson-kernel@lists.loongnix.cn Subject: [RFC PATCH 2/3] LoongArch: Add larch_insn_gen_break() to generate break insn Date: Fri, 7 Apr 2023 10:15:00 +0800 Message-Id: <1680833701-1727-3-git-send-email-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1680833701-1727-1-git-send-email-yangtiezhu@loongson.cn> References: <1680833701-1727-1-git-send-email-yangtiezhu@loongson.cn> X-CM-TRANSID: AQAAf8DxSL2lfC9k2cEXAA--.21395S4 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBjvJXoWxKryDtr4ruF4xKry7Jw4DCFg_yoW7WF4UpF srAwn5Jr48Wr93Gr9rJws8Zr15trs3WwsFqFsIk3yfCFW7Xwn8tF18KryqvF90k3ykGFWI q3Waqw1UZ3W5A3DanT9S1TB71UUUUUJqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU b3kYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_Jrv_JF1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwA2z4 x0Y4vEx4A2jsIE14v26r4UJVWxJr1l84ACjcxK6I8E87Iv6xkF7I0E14v26F4UJVW0owAa w2AFwI0_Jrv_JF1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqjxCEc2xF0cIa020Ex4CE44 I27wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jw0_WrylYx0Ex4A2 jsIE14v26r4j6F4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCF04k20x vY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwCFI7km07C267AKxVWUXVWUAwC20s02 6c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF 0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVW8JVW5JwCI42IY6xIIjxv20xvE c7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14 v26r4j6F4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x 07jz2NtUUUUU= X-Spam-Status: No, score=-0.0 required=5.0 tests=SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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 There exist various break insns such as BRK_KPROBE_BP, BRK_KPROBE_SSTEPBP, BRK_UPROBE_BP and BRK_UPROBE_XOLBP, add larch_insn_gen_break() to generate break insns simpler and easier. This is preparation for later patch, no functionality change. Signed-off-by: Tiezhu Yang --- arch/loongarch/include/asm/inst.h | 30 +++++++++++++++++++++++++++--- arch/loongarch/include/asm/kprobes.h | 2 +- arch/loongarch/kernel/inst.c | 9 +++++++++ arch/loongarch/kernel/kprobes.c | 17 +++-------------- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/arch/loongarch/include/asm/inst.h b/arch/loongarch/include/asm/inst.h index af494b5..a0fce06 100644 --- a/arch/loongarch/include/asm/inst.h +++ b/arch/loongarch/include/asm/inst.h @@ -409,8 +409,12 @@ static inline bool is_self_loop_ins(union loongarch_instruction *ip, struct pt_r void simu_pc(struct pt_regs *regs, union loongarch_instruction insn); void simu_branch(struct pt_regs *regs, union loongarch_instruction insn); -static inline bool insns_not_supported(union loongarch_instruction insn) +static inline bool insns_not_supported(u32 code) { + union loongarch_instruction insn; + + insn.word = code; + switch (insn.reg2i14_format.opcode) { case llw_op: case lld_op: @@ -429,8 +433,12 @@ static inline bool insns_not_supported(union loongarch_instruction insn) return false; } -static inline bool insns_need_simulation(union loongarch_instruction insn) +static inline bool insns_need_simulation(u32 code) { + union loongarch_instruction insn; + + insn.word = code; + if (is_pc_ins(&insn)) return true; @@ -440,8 +448,12 @@ static inline bool insns_need_simulation(union loongarch_instruction insn) return false; } -static inline void arch_simulate_insn(union loongarch_instruction insn, struct pt_regs *regs) +static inline void arch_simulate_insn(u32 code, struct pt_regs *regs) { + union loongarch_instruction insn; + + insn.word = code; + if (is_pc_ins(&insn)) simu_pc(regs, insn); else if (is_branch_ins(&insn)) @@ -456,6 +468,8 @@ u32 larch_insn_gen_nop(void); u32 larch_insn_gen_b(unsigned long pc, unsigned long dest); u32 larch_insn_gen_bl(unsigned long pc, unsigned long dest); +u32 larch_insn_gen_break(int imm); + u32 larch_insn_gen_or(enum loongarch_gpr rd, enum loongarch_gpr rj, enum loongarch_gpr rk); u32 larch_insn_gen_move(enum loongarch_gpr rd, enum loongarch_gpr rj); @@ -474,6 +488,16 @@ static inline bool unsigned_imm_check(unsigned long val, unsigned int bit) return val < (1UL << bit); } +#define DEF_EMIT_REG0I15_FORMAT(NAME, OP) \ +static inline void emit_##NAME(union loongarch_instruction *insn, \ + int imm) \ +{ \ + insn->reg0i15_format.opcode = OP; \ + insn->reg0i15_format.immediate = imm; \ +} + +DEF_EMIT_REG0I15_FORMAT(break, break_op) + #define DEF_EMIT_REG0I26_FORMAT(NAME, OP) \ static inline void emit_##NAME(union loongarch_instruction *insn, \ int offset) \ diff --git a/arch/loongarch/include/asm/kprobes.h b/arch/loongarch/include/asm/kprobes.h index 798020a..7ef7a0f 100644 --- a/arch/loongarch/include/asm/kprobes.h +++ b/arch/loongarch/include/asm/kprobes.h @@ -22,7 +22,7 @@ do { \ #define kretprobe_blacklist_size 0 -typedef union loongarch_instruction kprobe_opcode_t; +typedef u32 kprobe_opcode_t; /* Architecture specific copy of original instruction */ struct arch_specific_insn { diff --git a/arch/loongarch/kernel/inst.c b/arch/loongarch/kernel/inst.c index 258ef26..31b8efe 100644 --- a/arch/loongarch/kernel/inst.c +++ b/arch/loongarch/kernel/inst.c @@ -208,6 +208,15 @@ u32 larch_insn_gen_bl(unsigned long pc, unsigned long dest) return insn.word; } +u32 larch_insn_gen_break(int imm) +{ + union loongarch_instruction insn; + + emit_break(&insn, imm); + + return insn.word; +} + u32 larch_insn_gen_or(enum loongarch_gpr rd, enum loongarch_gpr rj, enum loongarch_gpr rk) { union loongarch_instruction insn; diff --git a/arch/loongarch/kernel/kprobes.c b/arch/loongarch/kernel/kprobes.c index 08c78d2..a5c3712 100644 --- a/arch/loongarch/kernel/kprobes.c +++ b/arch/loongarch/kernel/kprobes.c @@ -4,19 +4,8 @@ #include #include -static const union loongarch_instruction breakpoint_insn = { - .reg0i15_format = { - .opcode = break_op, - .immediate = BRK_KPROBE_BP, - } -}; - -static const union loongarch_instruction singlestep_insn = { - .reg0i15_format = { - .opcode = break_op, - .immediate = BRK_KPROBE_SSTEPBP, - } -}; +#define breakpoint_insn larch_insn_gen_break(BRK_KPROBE_BP) +#define singlestep_insn larch_insn_gen_break(BRK_KPROBE_SSTEPBP) DEFINE_PER_CPU(struct kprobe *, current_kprobe); DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); @@ -253,7 +242,7 @@ bool kprobe_breakpoint_handler(struct pt_regs *regs) } } - if (addr->word != breakpoint_insn.word) { + if (*addr != breakpoint_insn) { /* * The breakpoint instruction was removed right * after we hit it. Another cpu has removed -- 2.1.0