summaryrefslogtreecommitdiff
path: root/arch/blackfin/kernel/pseudodbg.c
blob: e57ce2f64bfeac8a272208bf1c3902dcd6b1cf24 (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
/* The fake debug assert instructions
 *
 * Copyright 2010 Analog Devices Inc.
 *
 * Licensed under the GPL-2 or later
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/ptrace.h>

const char * const greg_names[] = {
	"R0",    "R1",      "R2",     "R3",    "R4",    "R5",    "R6",     "R7",
	"P0",    "P1",      "P2",     "P3",    "P4",    "P5",    "SP",     "FP",
	"I0",    "I1",      "I2",     "I3",    "M0",    "M1",    "M2",     "M3",
	"B0",    "B1",      "B2",     "B3",    "L0",    "L1",    "L2",     "L3",
	"A0.X",  "A0.W",    "A1.X",   "A1.W",  "<res>", "<res>", "ASTAT",  "RETS",
	"<res>", "<res>",   "<res>",  "<res>", "<res>", "<res>", "<res>",  "<res>",
	"LC0",   "LT0",     "LB0",    "LC1",   "LT1",   "LB1",   "CYCLES", "CYCLES2",
	"USP",   "SEQSTAT", "SYSCFG", "RETI",  "RETX",  "RETN",  "RETE",   "EMUDAT",
};

static const char *get_allreg_name(int grp, int reg)
{
	return greg_names[(grp << 3) | reg];
}

/*
 * Unfortunately, the pt_regs structure is not laid out the same way as the
 * hardware register file, so we need to do some fix ups.
 *
 * CYCLES is not stored in the pt_regs structure - so, we just read it from
 * the hardware.
 *
 * Don't support:
 *  - All reserved registers
 *  - All in group 7 are (supervisors only)
 */

static bool fix_up_reg(struct pt_regs *fp, long *value, int grp, int reg)
{
	long *val = &fp->r0;
	unsigned long tmp;

	/* Only do Dregs and Pregs for now */
	if (grp == 5 ||
	   (grp == 4 && (reg == 4 || reg == 5)) ||
	   (grp == 7))
		return false;

	if (grp == 0 || (grp == 1 && reg < 6))
		val -= (reg + 8 * grp);
	else if (grp == 1 && reg == 6)
		val = &fp->usp;
	else if (grp == 1 && reg == 7)
		val = &fp->fp;
	else if (grp == 2) {
		val = &fp->i0;
		val -= reg;
	} else if (grp == 3 && reg >= 4) {
		val = &fp->l0;
		val -= (reg - 4);
	} else if (grp == 3 && reg < 4) {
		val = &fp->b0;
		val -= reg;
	} else if (grp == 4 && reg < 4) {
		val = &fp->a0x;
		val -= reg;
	} else if (grp == 4 && reg == 6)
		val = &fp->astat;
	else if (grp == 4 && reg == 7)
		val = &fp->rets;
	else if (grp == 6 && reg < 6) {
		val = &fp->lc0;
		val -= reg;
	} else if (grp == 6 && reg == 6) {
		__asm__ __volatile__("%0 = cycles;\n" : "=d"(tmp));
		val = &tmp;
	} else if (grp == 6 && reg == 7) {
		__asm__ __volatile__("%0 = cycles2;\n" : "=d"(tmp));
		val = &tmp;
	}

	*value = *val;
	return true;

}

#define PseudoDbg_Assert_opcode         0xf0000000
#define PseudoDbg_Assert_expected_bits  0
#define PseudoDbg_Assert_expected_mask  0xffff
#define PseudoDbg_Assert_regtest_bits   16
#define PseudoDbg_Assert_regtest_mask   0x7
#define PseudoDbg_Assert_grp_bits       19
#define PseudoDbg_Assert_grp_mask       0x7
#define PseudoDbg_Assert_dbgop_bits     22
#define PseudoDbg_Assert_dbgop_mask     0x3
#define PseudoDbg_Assert_dontcare_bits  24
#define PseudoDbg_Assert_dontcare_mask  0x7
#define PseudoDbg_Assert_code_bits      27
#define PseudoDbg_Assert_code_mask      0x1f

/*
 * DBGA - debug assert
 */
bool execute_pseudodbg_assert(struct pt_regs *fp, unsigned int opcode)
{
	int expected = ((opcode >> PseudoDbg_Assert_expected_bits) & PseudoDbg_Assert_expected_mask);
	int dbgop    = ((opcode >> (PseudoDbg_Assert_dbgop_bits)) & PseudoDbg_Assert_dbgop_mask);
	int grp      = ((opcode >> (PseudoDbg_Assert_grp_bits)) & PseudoDbg_Assert_grp_mask);
	int regtest  = ((opcode >> (PseudoDbg_Assert_regtest_bits)) & PseudoDbg_Assert_regtest_mask);
	long value;

	if ((opcode & 0xFF000000) != PseudoDbg_Assert_opcode)
		return false;

	if (!fix_up_reg(fp, &value, grp, regtest))
		return false;

	if (dbgop == 0 || dbgop == 2) {
		/* DBGA ( regs_lo , uimm16 ) */
		/* DBGAL ( regs , uimm16 ) */
		if (expected != (value & 0xFFFF)) {
			pr_notice("DBGA (%s.L,0x%x) failure, got 0x%x\n",
				get_allreg_name(grp, regtest),
				expected, (unsigned int)(value & 0xFFFF));
			return false;
		}

	} else if (dbgop == 1 || dbgop == 3) {
		/* DBGA ( regs_hi , uimm16 ) */
		/* DBGAH ( regs , uimm16 ) */
		if (expected != ((value >> 16) & 0xFFFF)) {
			pr_notice("DBGA (%s.H,0x%x) failure, got 0x%x\n",
				get_allreg_name(grp, regtest),
				expected, (unsigned int)((value >> 16) & 0xFFFF));
			return false;
		}
	}

	fp->pc += 4;
	return true;
}

#define PseudoDbg_opcode        0xf8000000
#define PseudoDbg_reg_bits      0
#define PseudoDbg_reg_mask      0x7
#define PseudoDbg_grp_bits      3
#define PseudoDbg_grp_mask      0x7
#define PseudoDbg_fn_bits       6
#define PseudoDbg_fn_mask       0x3
#define PseudoDbg_code_bits     8
#define PseudoDbg_code_mask     0xff

/*
 * DBG - debug (dump a register value out)
 */
bool execute_pseudodbg(struct pt_regs *fp, unsigned int opcode)
{
	int grp, fn, reg;
	long value;

	if ((opcode & 0xFF000000) != PseudoDbg_opcode)
		return false;

	opcode >>= 16;
	grp = ((opcode >> PseudoDbg_grp_bits) & PseudoDbg_reg_mask);
	fn  = ((opcode >> PseudoDbg_fn_bits)  & PseudoDbg_fn_mask);
	reg = ((opcode >> PseudoDbg_reg_bits) & PseudoDbg_reg_mask);

	if (!fix_up_reg(fp, &value, grp, reg))
		return false;

	pr_notice("DBG %s = %08lx\n", get_allreg_name(grp, reg), value);

	fp->pc += 2;
	return true;
}