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