-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppc.c
161 lines (128 loc) · 3.29 KB
/
ppc.c
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
/*
* ppc.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdarg.h>
#include <string.h>
#include <dis-asm.h>
#include "kdumpid.h"
#define MAX_INSN_LEN 100
struct disas_state {
unsigned long flags;
};
#define SRR0_SET 1
#define SRR1_SET 2
struct disas_priv {
char *iptr;
struct disas_state state;
char insn[MAX_INSN_LEN];
};
static const char sep[] = ", \t\r\n";
#define wsep (sep+1)
static disassembler_ftype print_insn;
static void
append_insn(void *data, const char *fmt, va_list va)
{
struct disas_priv *priv = data;
size_t remain;
int len;
remain = priv->insn + sizeof(priv->insn) - priv->iptr;
len = vsnprintf(priv->iptr, remain, fmt, va);
if (len > 0)
priv->iptr += len;
}
static int
disas_fn(void *data, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
append_insn(data, fmt, va);
va_end(va);
return 0;
}
#ifdef DIS_ASM_STYLED_PRINTF
static int
disas_styled_fn(void *data, enum disassembler_style style,
const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
append_insn(data, fmt, va);
va_end(va);
return 0;
}
#endif /* DIS_ASM_STYLED_PRINTF */
static void error_func(int status, bfd_vma memaddr,
struct disassemble_info *dinfo)
{
/* intentionally empty */
}
static int
disas_at(struct dump_desc *dd, struct disassemble_info *info, unsigned pc)
{
struct disas_priv *priv = info->stream;
char *toksave;
char *insn;
int count;
do {
priv->iptr = priv->insn;
count = print_insn(info->buffer_vma + pc, info);
if (count < 0)
break;
pc += count;
insn = strtok_r(priv->insn, wsep, &toksave);
/* for historical reasons, ppc starts with 3 NOPs */
if (pc <= 3 * 4 && strcmp(insn, "nop"))
break;
/* MMU is switched on with an rfi */
if (priv->state.flags & SRR0_SET &&
priv->state.flags & SRR1_SET &&
!strcmp(insn, "rfi"))
return 1;
if (!strcmp(insn, "mtsrr0"))
priv->state.flags |= SRR0_SET;
if (!strcmp(insn, "mtsrr1"))
priv->state.flags |= SRR1_SET;
/* invalid instruction? */
if (!strcmp(insn, ".long"))
break;
} while (count > 0);
return 0;
}
int
looks_like_kcode_ppc(struct dump_desc *dd, uint64_t addr)
{
struct disassemble_info info;
struct disas_priv priv;
/* check ppc startup code */
if (read_page(dd, addr / dd->page_size))
return -1;
memset(&priv, 0, sizeof priv);
#ifdef DIS_ASM_STYLED_PRINTF
init_disassemble_info(&info, &priv, disas_fn, disas_styled_fn);
#else
init_disassemble_info(&info, &priv, disas_fn);
#endif
info.memory_error_func = error_func;
info.buffer = dd->page;
info.buffer_vma = addr;
info.buffer_length = dd->page_size;
info.arch = bfd_arch_powerpc;
info.mach = bfd_mach_ppc;
disassemble_init_for_target(&info);
print_insn = disassembler(bfd_arch_powerpc,
dd->endian != KDUMP_LITTLE_ENDIAN,
bfd_mach_ppc, NULL);
if (!print_insn)
return 0;
return disas_at(dd, &info, 0);
}