xref: /openbmc/linux/arch/hexagon/kernel/kgdb.c (revision 2c6467d2)
1 /*
2  * arch/hexagon/kernel/kgdb.c - Hexagon KGDB Support
3  *
4  * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 and
8  * only version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  */
20 
21 #include <linux/irq.h>
22 #include <linux/sched.h>
23 #include <linux/sched/task_stack.h>
24 #include <linux/kdebug.h>
25 #include <linux/kgdb.h>
26 
27 /* All registers are 4 bytes, for now */
28 #define GDB_SIZEOF_REG 4
29 
30 /* The register names are used during printing of the regs;
31  * Keep these at three letters to pretty-print. */
32 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
33 	{ " r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, r00)},
34 	{ " r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, r01)},
35 	{ " r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, r02)},
36 	{ " r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, r03)},
37 	{ " r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, r04)},
38 	{ " r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, r05)},
39 	{ " r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, r06)},
40 	{ " r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, r07)},
41 	{ " r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, r08)},
42 	{ " r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, r09)},
43 	{ "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, r10)},
44 	{ "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, r11)},
45 	{ "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, r12)},
46 	{ "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, r13)},
47 	{ "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, r14)},
48 	{ "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, r15)},
49 	{ "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, r16)},
50 	{ "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, r17)},
51 	{ "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, r18)},
52 	{ "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, r19)},
53 	{ "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, r20)},
54 	{ "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, r21)},
55 	{ "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, r22)},
56 	{ "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, r23)},
57 	{ "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, r24)},
58 	{ "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, r25)},
59 	{ "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, r26)},
60 	{ "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, r27)},
61 	{ "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, r28)},
62 	{ "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, r29)},
63 	{ "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, r30)},
64 	{ "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, r31)},
65 
66 	{ "usr", GDB_SIZEOF_REG, offsetof(struct pt_regs, usr)},
67 	{ "preds", GDB_SIZEOF_REG, offsetof(struct pt_regs, preds)},
68 	{ " m0", GDB_SIZEOF_REG, offsetof(struct pt_regs, m0)},
69 	{ " m1", GDB_SIZEOF_REG, offsetof(struct pt_regs, m1)},
70 	{ "sa0", GDB_SIZEOF_REG, offsetof(struct pt_regs, sa0)},
71 	{ "sa1", GDB_SIZEOF_REG, offsetof(struct pt_regs, sa1)},
72 	{ "lc0", GDB_SIZEOF_REG, offsetof(struct pt_regs, lc0)},
73 	{ "lc1", GDB_SIZEOF_REG, offsetof(struct pt_regs, lc1)},
74 	{ " gp", GDB_SIZEOF_REG, offsetof(struct pt_regs, gp)},
75 	{ "ugp", GDB_SIZEOF_REG, offsetof(struct pt_regs, ugp)},
76 	{ "cs0", GDB_SIZEOF_REG, offsetof(struct pt_regs, cs0)},
77 	{ "cs1", GDB_SIZEOF_REG, offsetof(struct pt_regs, cs1)},
78 	{ "psp", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmpsp)},
79 	{ "elr", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmel)},
80 	{ "est", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmest)},
81 	{ "badva", GDB_SIZEOF_REG, offsetof(struct pt_regs, hvmer.vmbadva)},
82 	{ "restart_r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, restart_r0)},
83 	{ "syscall_nr", GDB_SIZEOF_REG, offsetof(struct pt_regs, syscall_nr)},
84 };
85 
86 const struct kgdb_arch arch_kgdb_ops = {
87 	/* trap0(#0xDB) 0x0cdb0054 */
88 	.gdb_bpt_instr = {0x54, 0x00, 0xdb, 0x0c},
89 };
90 
91 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
92 {
93 	if (regno >= DBG_MAX_REG_NUM || regno < 0)
94 		return NULL;
95 
96 	*((unsigned long *) mem) = *((unsigned long *) ((void *)regs +
97 		dbg_reg_def[regno].offset));
98 
99 	return dbg_reg_def[regno].name;
100 }
101 
102 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
103 {
104 	if (regno >= DBG_MAX_REG_NUM || regno < 0)
105 		return -EINVAL;
106 
107 	*((unsigned long *) ((void *)regs + dbg_reg_def[regno].offset)) =
108 		*((unsigned long *) mem);
109 
110 	return 0;
111 }
112 
113 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
114 {
115 	instruction_pointer(regs) = pc;
116 }
117 
118 
119 /*  Not yet working  */
120 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
121 				 struct task_struct *task)
122 {
123 	struct pt_regs *thread_regs;
124 
125 	if (task == NULL)
126 		return;
127 
128 	/* Initialize to zero */
129 	memset(gdb_regs, 0, NUMREGBYTES);
130 
131 	/* Otherwise, we have only some registers from switch_to() */
132 	thread_regs = task_pt_regs(task);
133 	gdb_regs[0] = thread_regs->r00;
134 }
135 
136 /**
137  * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
138  * @vector: The error vector of the exception that happened.
139  * @signo: The signal number of the exception that happened.
140  * @err_code: The error code of the exception that happened.
141  * @remcom_in_buffer: The buffer of the packet we have read.
142  * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
143  * @regs: The &struct pt_regs of the current process.
144  *
145  * This function MUST handle the 'c' and 's' command packets,
146  * as well packets to set / remove a hardware breakpoint, if used.
147  * If there are additional packets which the hardware needs to handle,
148  * they are handled here.  The code should return -1 if it wants to
149  * process more packets, and a %0 or %1 if it wants to exit from the
150  * kgdb callback.
151  *
152  * Not yet working.
153  */
154 int kgdb_arch_handle_exception(int vector, int signo, int err_code,
155 			       char *remcom_in_buffer, char *remcom_out_buffer,
156 			       struct pt_regs *linux_regs)
157 {
158 	switch (remcom_in_buffer[0]) {
159 	case 's':
160 	case 'c':
161 		return 0;
162 	}
163 	/* Stay in the debugger. */
164 	return -1;
165 }
166 
167 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
168 {
169 	/* cpu roundup */
170 	if (atomic_read(&kgdb_active) != -1) {
171 		kgdb_nmicallback(smp_processor_id(), args->regs);
172 		return NOTIFY_STOP;
173 	}
174 
175 	if (user_mode(args->regs))
176 		return NOTIFY_DONE;
177 
178 	if (kgdb_handle_exception(args->trapnr & 0xff, args->signr, args->err,
179 				    args->regs))
180 		return NOTIFY_DONE;
181 
182 	return NOTIFY_STOP;
183 }
184 
185 static int
186 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
187 {
188 	unsigned long flags;
189 	int ret;
190 
191 	local_irq_save(flags);
192 	ret = __kgdb_notify(ptr, cmd);
193 	local_irq_restore(flags);
194 
195 	return ret;
196 }
197 
198 static struct notifier_block kgdb_notifier = {
199 	.notifier_call = kgdb_notify,
200 
201 	/*
202 	 * Lowest-prio notifier priority, we want to be notified last:
203 	 */
204 	.priority = -INT_MAX,
205 };
206 
207 /**
208  * kgdb_arch_init - Perform any architecture specific initialization.
209  *
210  * This function will handle the initialization of any architecture
211  * specific callbacks.
212  */
213 int kgdb_arch_init(void)
214 {
215 	return register_die_notifier(&kgdb_notifier);
216 }
217 
218 /**
219  * kgdb_arch_exit - Perform any architecture specific uninitalization.
220  *
221  * This function will handle the uninitalization of any architecture
222  * specific callbacks, for dynamic registration and unregistration.
223  */
224 void kgdb_arch_exit(void)
225 {
226 	unregister_die_notifier(&kgdb_notifier);
227 }
228