Merge zizzer:/bk/m5
[gem5.git] / arch / alpha / stacktrace.cc
1 /*
2 * Copyright (c) 2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <string>
30
31 #include "arch/alpha/isa_traits.hh"
32 #include "arch/alpha/stacktrace.hh"
33 #include "arch/alpha/vtophys.hh"
34 #include "base/bitfield.hh"
35 #include "base/trace.hh"
36 #include "cpu/base.hh"
37 #include "cpu/exec_context.hh"
38
39 using namespace std;
40
41 ProcessInfo::ProcessInfo(ExecContext *_xc)
42 : xc(_xc)
43 {
44 Addr addr = 0;
45
46 if (!xc->system->kernelSymtab->findAddress("thread_info_size", addr))
47 panic("thread info not compiled into kernel\n");
48 thread_info_size = *(int32_t *)vtomem(xc, addr, sizeof(int32_t));
49
50 if (!xc->system->kernelSymtab->findAddress("task_struct_size", addr))
51 panic("thread info not compiled into kernel\n");
52 task_struct_size = *(int32_t *)vtomem(xc, addr, sizeof(int32_t));
53
54 if (!xc->system->kernelSymtab->findAddress("thread_info_task", addr))
55 panic("thread info not compiled into kernel\n");
56 task_off = *(int32_t *)vtomem(xc, addr, sizeof(int32_t));
57
58 if (!xc->system->kernelSymtab->findAddress("task_struct_pid", addr))
59 panic("thread info not compiled into kernel\n");
60 pid_off = *(int32_t *)vtomem(xc, addr, sizeof(int32_t));
61
62 if (!xc->system->kernelSymtab->findAddress("task_struct_comm", addr))
63 panic("thread info not compiled into kernel\n");
64 name_off = *(int32_t *)vtomem(xc, addr, sizeof(int32_t));
65 }
66
67 Addr
68 ProcessInfo::task(Addr ksp) const
69 {
70 Addr base = ksp & ~0x3fff;
71 if (base == ULL(0xfffffc0000000000))
72 return 0;
73
74 Addr task;
75 CopyOut(xc, &task, base + task_off, sizeof(task));
76 return task;
77 }
78
79 int
80 ProcessInfo::pid(Addr ksp) const
81 {
82 Addr task = this->task(ksp);
83 if (!task)
84 return -1;
85
86 uint16_t pid;
87 CopyOut(xc, &pid, task + pid_off, sizeof(pid));
88 return pid;
89 }
90
91 string
92 ProcessInfo::name(Addr ksp) const
93 {
94 Addr task = this->task(ksp);
95 if (!task)
96 return "console";
97
98 char comm[256];
99 CopyString(xc, comm, task + name_off, sizeof(comm));
100 if (!comm[0])
101 return "startup";
102
103 return comm;
104 }
105
106 StackTrace::StackTrace(ExecContext *_xc, bool is_call)
107 : xc(_xc)
108 {
109 bool usermode = (xc->regs.ipr[AlphaISA::IPR_DTB_CM] & 0x18) != 0;
110
111 Addr pc = xc->regs.npc;
112 bool kernel = xc->system->kernelStart <= pc && pc <= xc->system->kernelEnd;
113
114 if (usermode) {
115 stack.push_back(1);
116 return;
117 }
118
119 if (!kernel) {
120 stack.push_back(2);
121 return;
122 }
123
124 SymbolTable *symtab = xc->system->allSymtab;
125 Addr ksp = xc->regs.intRegFile[TheISA::StackPointerReg];
126 Addr bottom = ksp & ~0x3fff;
127 Addr addr;
128
129 if (is_call) {
130 if (!symtab->findNearestAddr(pc, addr))
131 panic("could not find address %#x", pc);
132
133 stack.push_back(addr);
134 pc = xc->regs.pc;
135 }
136
137 Addr ra;
138 int size;
139
140 while (ksp > bottom) {
141 if (!symtab->findNearestAddr(pc, addr))
142 panic("could not find symbol for pc=%#x", pc);
143 assert(pc >= addr && "symbol botch: callpc < func");
144
145 stack.push_back(addr);
146
147 if (isEntry(addr))
148 return;
149
150 if (decodePrologue(ksp, pc, addr, size, ra)) {
151 if (!ra)
152 return;
153
154 pc = ra;
155 ksp += size;
156 } else {
157 stack.push_back(3);
158 return;
159 }
160
161 bool kernel = xc->system->kernelStart <= pc &&
162 pc <= xc->system->kernelEnd;
163 if (!kernel)
164 return;
165 }
166
167 panic("unwinding too far");
168 }
169
170 StackTrace::~StackTrace()
171 {
172 }
173
174 bool
175 StackTrace::isEntry(Addr addr)
176 {
177 if (addr == xc->regs.ipr[AlphaISA::IPR_PALtemp12])
178 return true;
179
180 if (addr == xc->regs.ipr[AlphaISA::IPR_PALtemp7])
181 return true;
182
183 if (addr == xc->regs.ipr[AlphaISA::IPR_PALtemp11])
184 return true;
185
186 if (addr == xc->regs.ipr[AlphaISA::IPR_PALtemp21])
187 return true;
188
189 if (addr == xc->regs.ipr[AlphaISA::IPR_PALtemp9])
190 return true;
191
192 if (addr == xc->regs.ipr[AlphaISA::IPR_PALtemp2])
193 return true;
194
195 return false;
196 }
197
198 bool
199 StackTrace::decodeStack(MachInst inst, int &disp)
200 {
201 // lda $sp, -disp($sp)
202 //
203 // Opcode<31:26> == 0x08
204 // RA<25:21> == 30
205 // RB<20:16> == 30
206 // Disp<15:0>
207 const MachInst mem_mask = 0xffff0000;
208 const MachInst lda_pattern = 0x23de0000;
209 const MachInst lda_disp_mask = 0x0000ffff;
210
211 // subq $sp, disp, $sp
212 // addq $sp, disp, $sp
213 //
214 // Opcode<31:26> == 0x10
215 // RA<25:21> == 30
216 // Lit<20:13>
217 // One<12> = 1
218 // Func<11:5> == 0x20 (addq)
219 // Func<11:5> == 0x29 (subq)
220 // RC<4:0> == 30
221 const MachInst intop_mask = 0xffe01fff;
222 const MachInst addq_pattern = 0x43c0141e;
223 const MachInst subq_pattern = 0x43c0153e;
224 const MachInst intop_disp_mask = 0x001fe000;
225 const int intop_disp_shift = 13;
226
227 if ((inst & mem_mask) == lda_pattern)
228 disp = -sext<16>(inst & lda_disp_mask);
229 else if ((inst & intop_mask) == addq_pattern)
230 disp = -int((inst & intop_disp_mask) >> intop_disp_shift);
231 else if ((inst & intop_mask) == subq_pattern)
232 disp = int((inst & intop_disp_mask) >> intop_disp_shift);
233 else
234 return false;
235
236 return true;
237 }
238
239 bool
240 StackTrace::decodeSave(MachInst inst, int &reg, int &disp)
241 {
242 // lda $stq, disp($sp)
243 //
244 // Opcode<31:26> == 0x08
245 // RA<25:21> == ?
246 // RB<20:16> == 30
247 // Disp<15:0>
248 const MachInst stq_mask = 0xfc1f0000;
249 const MachInst stq_pattern = 0xb41e0000;
250 const MachInst stq_disp_mask = 0x0000ffff;
251 const MachInst reg_mask = 0x03e00000;
252 const int reg_shift = 21;
253
254 if ((inst & stq_mask) == stq_pattern) {
255 reg = (inst & reg_mask) >> reg_shift;
256 disp = sext<16>(inst & stq_disp_mask);
257 } else {
258 return false;
259 }
260
261 return true;
262 }
263
264 /*
265 * Decode the function prologue for the function we're in, and note
266 * which registers are stored where, and how large the stack frame is.
267 */
268 bool
269 StackTrace::decodePrologue(Addr sp, Addr callpc, Addr func,
270 int &size, Addr &ra)
271 {
272 size = 0;
273 ra = 0;
274
275 for (Addr pc = func; pc < callpc; pc += sizeof(MachInst)) {
276 MachInst inst;
277 CopyOut(xc, (uint8_t *)&inst, pc, sizeof(MachInst));
278
279 int reg, disp;
280 if (decodeStack(inst, disp)) {
281 if (size) {
282 // panic("decoding frame size again");
283 return true;
284 }
285 size += disp;
286 } else if (decodeSave(inst, reg, disp)) {
287 if (!ra && reg == ReturnAddressReg) {
288 CopyOut(xc, (uint8_t *)&ra, sp + disp, sizeof(Addr));
289 if (!ra) {
290 // panic("no return address value pc=%#x\n", pc);
291 return false;
292 }
293 }
294 }
295 }
296
297 return true;
298 }
299
300 #if TRACING_ON
301 void
302 StackTrace::dump()
303 {
304 StringWrap name(xc->cpu->name());
305 SymbolTable *symtab = xc->system->allSymtab;
306
307 DPRINTFN("------ Stack ------\n");
308
309 string symbol;
310 for (int i = 0, size = stack.size(); i < size; ++i) {
311 Addr addr = stack[size - i - 1];
312 if (addr == 1)
313 symbol = "user";
314 else if (addr == 2)
315 symbol = "console";
316 else if (addr == 3)
317 symbol = "unknown";
318 else
319 symtab->findSymbol(addr, symbol);
320
321 DPRINTFN("%#x: %s\n", addr, symbol);
322 }
323 }
324 #endif