-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppc64.c
177 lines (143 loc) · 3.82 KB
/
ppc64.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* ppc64.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
print_address(bfd_vma addr, struct disassemble_info *info)
{
struct disas_priv *priv = info->stream;
size_t remain = priv->insn + sizeof(priv->insn) - priv->iptr;
int len = snprintf(priv->iptr, remain,
"0x%llx", (unsigned long long) addr);
if (len > 0)
priv->iptr += len;
}
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);
/* ppc64 starts with a jump instruction, but it
* may be NOPped out at runtime */
if (pc == 4 && strcmp(insn, "b") && strcmp(insn, "nop"))
return 0;
/* The next instruction should be a trap */
if (pc == 8 && strcmp(insn, "trap"))
return 0;
/* MSR can be modiied only in supervisor mode */
if (!strcmp(insn, "mtmsrd"))
return 1;
/* Alternatively, a good rfid will also serve */
if (priv->state.flags & SRR0_SET &&
priv->state.flags & SRR1_SET &&
!strcmp(insn, "rfid"))
return 1;
if (!strcmp(insn, "mtsrr0"))
priv->state.flags |= SRR0_SET;
if (!strcmp(insn, "mtsrr1"))
priv->state.flags |= SRR1_SET;
} while (count > 0);
return 0;
}
int
looks_like_kcode_ppc64(struct dump_desc *dd, uint64_t addr)
{
struct disassemble_info info;
struct disas_priv priv;
/* check ppc64 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.print_address_func = print_address;
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_ppc64;
disassemble_init_for_target(&info);
print_insn = disassembler(bfd_arch_powerpc,
dd->endian != KDUMP_LITTLE_ENDIAN,
bfd_mach_ppc64, NULL);
if (!print_insn)
return 0;
return disas_at(dd, &info, 0);
}