^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2020 SiFive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kgdb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/irqflags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/gdb_xml.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/parse_asm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) NOT_KGDB_BREAK = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) KGDB_SW_BREAK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) KGDB_COMPILED_BREAK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) KGDB_SW_SINGLE_STEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static unsigned long stepped_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static unsigned int stepped_opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #if __riscv_xlen == 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* C.JAL is an RV32C-only instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) DECLARE_INSN(c_jal, MATCH_C_JAL, MASK_C_JAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define is_c_jal_insn(opcode) 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) DECLARE_INSN(jalr, MATCH_JALR, MASK_JALR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) DECLARE_INSN(jal, MATCH_JAL, MASK_JAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) DECLARE_INSN(c_jr, MATCH_C_JR, MASK_C_JR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) DECLARE_INSN(c_jalr, MATCH_C_JALR, MASK_C_JALR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) DECLARE_INSN(c_j, MATCH_C_J, MASK_C_J)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) DECLARE_INSN(beq, MATCH_BEQ, MASK_BEQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) DECLARE_INSN(bne, MATCH_BNE, MASK_BNE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) DECLARE_INSN(blt, MATCH_BLT, MASK_BLT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) DECLARE_INSN(bge, MATCH_BGE, MASK_BGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) DECLARE_INSN(bltu, MATCH_BLTU, MASK_BLTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) DECLARE_INSN(bgeu, MATCH_BGEU, MASK_BGEU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) DECLARE_INSN(c_beqz, MATCH_C_BEQZ, MASK_C_BEQZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) DECLARE_INSN(c_bnez, MATCH_C_BNEZ, MASK_C_BNEZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) DECLARE_INSN(sret, MATCH_SRET, MASK_SRET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int decode_register_index(unsigned long opcode, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return (opcode >> offset) & 0x1F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static int decode_register_index_short(unsigned long opcode, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return ((opcode >> offset) & 0x7) + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* Calculate the new address for after a step */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int get_step_address(struct pt_regs *regs, unsigned long *next_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned long pc = regs->epc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned long *regs_ptr = (unsigned long *)regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned int rs1_num, rs2_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int op_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (get_kernel_nofault(op_code, (void *)pc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if ((op_code & __INSN_LENGTH_MASK) != __INSN_LENGTH_GE_32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (is_c_jalr_insn(op_code) || is_c_jr_insn(op_code)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) rs1_num = decode_register_index(op_code, RVC_C2_RS1_OPOFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *next_addr = regs_ptr[rs1_num];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) } else if (is_c_j_insn(op_code) || is_c_jal_insn(op_code)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *next_addr = EXTRACT_RVC_J_IMM(op_code) + pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) } else if (is_c_beqz_insn(op_code)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) rs1_num = decode_register_index_short(op_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) RVC_C1_RS1_OPOFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!rs1_num || regs_ptr[rs1_num] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *next_addr = EXTRACT_RVC_B_IMM(op_code) + pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) *next_addr = pc + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) } else if (is_c_bnez_insn(op_code)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) rs1_num =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) decode_register_index_short(op_code, RVC_C1_RS1_OPOFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (rs1_num && regs_ptr[rs1_num] != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) *next_addr = EXTRACT_RVC_B_IMM(op_code) + pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) *next_addr = pc + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *next_addr = pc + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if ((op_code & __INSN_OPCODE_MASK) == __INSN_BRANCH_OPCODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) bool result = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) long imm = EXTRACT_BTYPE_IMM(op_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned long rs1_val = 0, rs2_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) rs1_num = decode_register_index(op_code, RVG_RS1_OPOFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) rs2_num = decode_register_index(op_code, RVG_RS2_OPOFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (rs1_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) rs1_val = regs_ptr[rs1_num];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (rs2_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) rs2_val = regs_ptr[rs2_num];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (is_beq_insn(op_code))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) result = (rs1_val == rs2_val) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) else if (is_bne_insn(op_code))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) result = (rs1_val != rs2_val) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) else if (is_blt_insn(op_code))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) result =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ((long)rs1_val <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) (long)rs2_val) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) else if (is_bge_insn(op_code))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) result =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ((long)rs1_val >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) (long)rs2_val) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) else if (is_bltu_insn(op_code))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) result = (rs1_val < rs2_val) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) else if (is_bgeu_insn(op_code))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) result = (rs1_val >= rs2_val) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) *next_addr = imm + pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) *next_addr = pc + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) } else if (is_jal_insn(op_code)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) *next_addr = EXTRACT_JTYPE_IMM(op_code) + pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) } else if (is_jalr_insn(op_code)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) rs1_num = decode_register_index(op_code, RVG_RS1_OPOFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (rs1_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) *next_addr = ((unsigned long *)regs)[rs1_num];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) *next_addr += EXTRACT_ITYPE_IMM(op_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } else if (is_sret_insn(op_code)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *next_addr = pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) *next_addr = pc + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int do_single_step(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* Determine where the target instruction will send us to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) unsigned long addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int error = get_step_address(regs, &addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* Store the op code in the stepped address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) error = get_kernel_nofault(stepped_opcode, (void *)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) stepped_address = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* Replace the op code with the break instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) error = copy_to_kernel_nofault((void *)stepped_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) arch_kgdb_ops.gdb_bpt_instr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* Flush and return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) kgdb_single_step = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) atomic_set(&kgdb_cpu_doing_single_step,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) raw_smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) stepped_address = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) stepped_opcode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* Undo a single step */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static void undo_single_step(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (stepped_opcode != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) copy_to_kernel_nofault((void *)stepped_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) (void *)&stepped_opcode, BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) flush_icache_range(stepped_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) stepped_address + BREAK_INSTR_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) stepped_address = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) stepped_opcode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) kgdb_single_step = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) atomic_set(&kgdb_cpu_doing_single_step, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {DBG_REG_ZERO, GDB_SIZEOF_REG, -1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {DBG_REG_RA, GDB_SIZEOF_REG, offsetof(struct pt_regs, ra)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {DBG_REG_SP, GDB_SIZEOF_REG, offsetof(struct pt_regs, sp)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {DBG_REG_GP, GDB_SIZEOF_REG, offsetof(struct pt_regs, gp)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {DBG_REG_TP, GDB_SIZEOF_REG, offsetof(struct pt_regs, tp)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {DBG_REG_T0, GDB_SIZEOF_REG, offsetof(struct pt_regs, t0)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {DBG_REG_T1, GDB_SIZEOF_REG, offsetof(struct pt_regs, t1)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {DBG_REG_T2, GDB_SIZEOF_REG, offsetof(struct pt_regs, t2)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {DBG_REG_FP, GDB_SIZEOF_REG, offsetof(struct pt_regs, s0)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {DBG_REG_S1, GDB_SIZEOF_REG, offsetof(struct pt_regs, a1)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {DBG_REG_A0, GDB_SIZEOF_REG, offsetof(struct pt_regs, a0)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {DBG_REG_A1, GDB_SIZEOF_REG, offsetof(struct pt_regs, a1)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {DBG_REG_A2, GDB_SIZEOF_REG, offsetof(struct pt_regs, a2)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {DBG_REG_A3, GDB_SIZEOF_REG, offsetof(struct pt_regs, a3)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {DBG_REG_A4, GDB_SIZEOF_REG, offsetof(struct pt_regs, a4)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {DBG_REG_A5, GDB_SIZEOF_REG, offsetof(struct pt_regs, a5)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {DBG_REG_A6, GDB_SIZEOF_REG, offsetof(struct pt_regs, a6)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {DBG_REG_A7, GDB_SIZEOF_REG, offsetof(struct pt_regs, a7)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {DBG_REG_S2, GDB_SIZEOF_REG, offsetof(struct pt_regs, s2)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {DBG_REG_S3, GDB_SIZEOF_REG, offsetof(struct pt_regs, s3)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {DBG_REG_S4, GDB_SIZEOF_REG, offsetof(struct pt_regs, s4)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {DBG_REG_S5, GDB_SIZEOF_REG, offsetof(struct pt_regs, s5)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {DBG_REG_S6, GDB_SIZEOF_REG, offsetof(struct pt_regs, s6)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {DBG_REG_S7, GDB_SIZEOF_REG, offsetof(struct pt_regs, s7)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {DBG_REG_S8, GDB_SIZEOF_REG, offsetof(struct pt_regs, s8)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {DBG_REG_S9, GDB_SIZEOF_REG, offsetof(struct pt_regs, s9)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {DBG_REG_S10, GDB_SIZEOF_REG, offsetof(struct pt_regs, s10)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {DBG_REG_S11, GDB_SIZEOF_REG, offsetof(struct pt_regs, s11)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {DBG_REG_T3, GDB_SIZEOF_REG, offsetof(struct pt_regs, t3)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {DBG_REG_T4, GDB_SIZEOF_REG, offsetof(struct pt_regs, t4)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {DBG_REG_T5, GDB_SIZEOF_REG, offsetof(struct pt_regs, t5)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {DBG_REG_T6, GDB_SIZEOF_REG, offsetof(struct pt_regs, t6)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {DBG_REG_EPC, GDB_SIZEOF_REG, offsetof(struct pt_regs, epc)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {DBG_REG_STATUS, GDB_SIZEOF_REG, offsetof(struct pt_regs, status)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {DBG_REG_BADADDR, GDB_SIZEOF_REG, offsetof(struct pt_regs, badaddr)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {DBG_REG_CAUSE, GDB_SIZEOF_REG, offsetof(struct pt_regs, cause)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (regno >= DBG_MAX_REG_NUM || regno < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (dbg_reg_def[regno].offset != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) memset(mem, 0, dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return dbg_reg_def[regno].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (regno >= DBG_MAX_REG_NUM || regno < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (dbg_reg_def[regno].offset != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dbg_reg_def[regno].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* Initialize to zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) memset((char *)gdb_regs, 0, NUMREGBYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) gdb_regs[DBG_REG_SP_OFF] = task->thread.sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) gdb_regs[DBG_REG_FP_OFF] = task->thread.s[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) gdb_regs[DBG_REG_S1_OFF] = task->thread.s[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) gdb_regs[DBG_REG_S2_OFF] = task->thread.s[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) gdb_regs[DBG_REG_S3_OFF] = task->thread.s[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) gdb_regs[DBG_REG_S4_OFF] = task->thread.s[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) gdb_regs[DBG_REG_S5_OFF] = task->thread.s[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) gdb_regs[DBG_REG_S6_OFF] = task->thread.s[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) gdb_regs[DBG_REG_S7_OFF] = task->thread.s[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) gdb_regs[DBG_REG_S8_OFF] = task->thread.s[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) gdb_regs[DBG_REG_S9_OFF] = task->thread.s[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) gdb_regs[DBG_REG_S10_OFF] = task->thread.s[11];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) gdb_regs[DBG_REG_EPC_OFF] = task->thread.ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) regs->epc = pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) void kgdb_arch_handle_qxfer_pkt(char *remcom_in_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) char *remcom_out_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (!strncmp(remcom_in_buffer, gdb_xfer_read_target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) sizeof(gdb_xfer_read_target)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) strcpy(remcom_out_buffer, riscv_gdb_stub_target_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) else if (!strncmp(remcom_in_buffer, gdb_xfer_read_cpuxml,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) sizeof(gdb_xfer_read_cpuxml)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) strcpy(remcom_out_buffer, riscv_gdb_stub_cpuxml);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static inline void kgdb_arch_update_addr(struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) char *remcom_in_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) ptr = &remcom_in_buffer[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (kgdb_hex2long(&ptr, &addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) regs->epc = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) int kgdb_arch_handle_exception(int vector, int signo, int err_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) char *remcom_in_buffer, char *remcom_out_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) undo_single_step(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) switch (remcom_in_buffer[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) case 'c':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) case 'D':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) case 'k':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (remcom_in_buffer[0] == 'c')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) kgdb_arch_update_addr(regs, remcom_in_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) kgdb_arch_update_addr(regs, remcom_in_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) err = do_single_step(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static int kgdb_riscv_kgdbbreak(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (stepped_address == addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return KGDB_SW_SINGLE_STEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (atomic_read(&kgdb_setting_breakpoint))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (addr == (unsigned long)&kgdb_compiled_break)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return KGDB_COMPILED_BREAK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return kgdb_has_hit_break(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static int kgdb_riscv_notify(struct notifier_block *self, unsigned long cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct die_args *args = (struct die_args *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct pt_regs *regs = args->regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (user_mode(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) type = kgdb_riscv_kgdbbreak(regs->epc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (type == NOT_KGDB_BREAK && cmd == DIE_TRAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (kgdb_handle_exception(type == KGDB_SW_SINGLE_STEP ? 0 : 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) args->signr, cmd, regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (type == KGDB_COMPILED_BREAK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) regs->epc += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return NOTIFY_STOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static struct notifier_block kgdb_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) .notifier_call = kgdb_riscv_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int kgdb_arch_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) register_die_notifier(&kgdb_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) void kgdb_arch_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) unregister_die_notifier(&kgdb_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * Global data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) #ifdef CONFIG_RISCV_ISA_C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) const struct kgdb_arch arch_kgdb_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) .gdb_bpt_instr = {0x02, 0x90}, /* c.ebreak */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) const struct kgdb_arch arch_kgdb_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) .gdb_bpt_instr = {0x73, 0x00, 0x10, 0x00}, /* ebreak */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) #endif