summaryrefslogtreecommitdiffstats
path: root/arch/parisc/kernel/kgdb.c
blob: c4554ac13eac797e68200ff1fe0e7738faa15c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// SPDX-License-Identifier: GPL-2.0
/*
 * PA-RISC KGDB support
 *
 * Copyright (c) 2019 Sven Schnelle <svens@stackframe.org>
 *
 */

#include <linux/kgdb.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/notifier.h>
#include <linux/kdebug.h>
#include <linux/uaccess.h>
#include <asm/ptrace.h>
#include <asm/traps.h>
#include <asm/processor.h>
#include <asm/patch.h>
#include <asm/cacheflush.h>

const struct kgdb_arch arch_kgdb_ops = {
	.gdb_bpt_instr = { 0x03, 0xff, 0xa0, 0x1f }
};

static int __kgdb_notify(struct die_args *args, unsigned long cmd)
{
	struct pt_regs *regs = args->regs;

	if (kgdb_handle_exception(1, args->signr, cmd, regs))
		return NOTIFY_DONE;
	return NOTIFY_STOP;
}

static int kgdb_notify(struct notifier_block *self,
		       unsigned long cmd, void *ptr)
{
	unsigned long flags;
	int ret;

	local_irq_save(flags);
	ret = __kgdb_notify(ptr, cmd);
	local_irq_restore(flags);

	return ret;
}

static struct notifier_block kgdb_notifier = {
	.notifier_call	= kgdb_notify,
	.priority	= -INT_MAX,
};

int kgdb_arch_init(void)
{
	return register_die_notifier(&kgdb_notifier);
}

void kgdb_arch_exit(void)
{
	unregister_die_notifier(&kgdb_notifier);
}

void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
{
	struct parisc_gdb_regs *gr = (struct parisc_gdb_regs *)gdb_regs;

	memset(gr, 0, sizeof(struct parisc_gdb_regs));

	memcpy(gr->gpr, regs->gr, sizeof(gr->gpr));
	memcpy(gr->fr, regs->fr, sizeof(gr->fr));

	gr->sr0 = regs->sr[0];
	gr->sr1 = regs->sr[1];
	gr->sr2 = regs->sr[2];
	gr->sr3 = regs->sr[3];
	gr->sr4 = regs->sr[4];
	gr->sr5 = regs->sr[5];
	gr->sr6 = regs->sr[6];
	gr->sr7 = regs->sr[7];

	gr->sar = regs->sar;
	gr->iir = regs->iir;
	gr->isr = regs->isr;
	gr->ior = regs->ior;
	gr->ipsw = regs->ipsw;
	gr->cr27 = regs->cr27;

	gr->iaoq_f = regs->iaoq[0];
	gr->iasq_f = regs->iasq[0];

	gr->iaoq_b = regs->iaoq[1];
	gr->iasq_b = regs->iasq[1];
}

void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
{
	struct parisc_gdb_regs *gr = (struct parisc_gdb_regs *)gdb_regs;


	memcpy(regs->gr, gr->gpr, sizeof(regs->gr));
	memcpy(regs->fr, gr->fr, sizeof(regs->fr));

	regs->sr[0] = gr->sr0;
	regs->sr[1] = gr->sr1;
	regs->sr[2] = gr->sr2;
	regs->sr[3] = gr->sr3;
	regs->sr[4] = gr->sr4;
	regs->sr[5] = gr->sr5;
	regs->sr[6] = gr->sr6;
	regs->sr[7] = gr->sr7;

	regs->sar = gr->sar;
	regs->iir = gr->iir;
	regs->isr = gr->isr;
	regs->ior = gr->ior;
	regs->ipsw = gr->ipsw;
	regs->cr27 = gr->cr27;

	regs->iaoq[0] = gr->iaoq_f;
	regs->iasq[0] = gr->iasq_f;

	regs->iaoq[1] = gr->iaoq_b;
	regs->iasq[1] = gr->iasq_b;
}

void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
				struct task_struct *task)
{
	struct pt_regs *regs = task_pt_regs(task);
	unsigned long gr30, iaoq;

	gr30 = regs->gr[30];
	iaoq = regs->iaoq[0];

	regs->gr[30] = regs->ksp;
	regs->iaoq[0] = regs->kpc;
	pt_regs_to_gdb_regs(gdb_regs, regs);

	regs->gr[30] = gr30;
	regs->iaoq[0] = iaoq;

}

static void step_instruction_queue(struct pt_regs *regs)
{
	regs->iaoq[0] = regs->iaoq[1];
	regs->iaoq[1] += 4;
}

void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
{
	regs->iaoq[0] = ip;
	regs->iaoq[1] = ip + 4;
}

int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
{
	int ret = copy_from_kernel_nofault(bpt->saved_instr,
			(char *)bpt->bpt_addr, BREAK_INSTR_SIZE);
	if (ret)
		return ret;

	__patch_text((void *)bpt->bpt_addr,
			*(unsigned int *)&arch_kgdb_ops.gdb_bpt_instr);
	return ret;
}

int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
{
	__patch_text((void *)bpt->bpt_addr, *(unsigned int *)&bpt->saved_instr);
	return 0;
}

int kgdb_arch_handle_exception(int trap, int signo,
		int err_code, char *inbuf, char *outbuf,
		struct pt_regs *regs)
{
	unsigned long addr;
	char *p = inbuf + 1;

	switch (inbuf[0]) {
	case 'D':
	case 'c':
	case 'k':
		kgdb_contthread = NULL;
		kgdb_single_step = 0;

		if (kgdb_hex2long(&p, &addr))
			kgdb_arch_set_pc(regs, addr);
		else if (trap == 9 && regs->iir ==
				PARISC_KGDB_COMPILED_BREAK_INSN)
			step_instruction_queue(regs);
		return 0;
	case 's':
		kgdb_single_step = 1;
		if (kgdb_hex2long(&p, &addr)) {
			kgdb_arch_set_pc(regs, addr);
		} else if (trap == 9 && regs->iir ==
				PARISC_KGDB_COMPILED_BREAK_INSN) {
			step_instruction_queue(regs);
			mtctl(-1, 0);
		} else {
			mtctl(0, 0);
		}
		regs->gr[0] |= PSW_R;
		return 0;

	}
	return -1;
}