BKL: Remove BKL from ecryptfs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / tile / kernel / backtrace.c
blobd3c41c1ff6bd9dae73908276cd7a41b1c70696a6
1 /*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
15 #include <linux/kernel.h>
16 #include <linux/string.h>
18 #include <asm/backtrace.h>
20 #include <arch/chip.h>
22 #include <asm/opcode-tile.h>
25 #define TREG_SP 54
26 #define TREG_LR 55
29 #if TILE_CHIP >= 10
30 #define tile_bundle_bits tilegx_bundle_bits
31 #define TILE_MAX_INSTRUCTIONS_PER_BUNDLE TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE
32 #define TILE_BUNDLE_ALIGNMENT_IN_BYTES TILEGX_BUNDLE_ALIGNMENT_IN_BYTES
33 #define tile_decoded_instruction tilegx_decoded_instruction
34 #define tile_mnemonic tilegx_mnemonic
35 #define parse_insn_tile parse_insn_tilegx
36 #define TILE_OPC_IRET TILEGX_OPC_IRET
37 #define TILE_OPC_ADDI TILEGX_OPC_ADDI
38 #define TILE_OPC_ADDLI TILEGX_OPC_ADDLI
39 #define TILE_OPC_INFO TILEGX_OPC_INFO
40 #define TILE_OPC_INFOL TILEGX_OPC_INFOL
41 #define TILE_OPC_JRP TILEGX_OPC_JRP
42 #define TILE_OPC_MOVE TILEGX_OPC_MOVE
43 #define OPCODE_STORE TILEGX_OPC_ST
44 typedef long long bt_int_reg_t;
45 #else
46 #define OPCODE_STORE TILE_OPC_SW
47 typedef int bt_int_reg_t;
48 #endif
50 /** A decoded bundle used for backtracer analysis. */
51 struct BacktraceBundle {
52 tile_bundle_bits bits;
53 int num_insns;
54 struct tile_decoded_instruction
55 insns[TILE_MAX_INSTRUCTIONS_PER_BUNDLE];
59 /* This implementation only makes sense for native tools. */
60 /** Default function to read memory. */
61 static bool bt_read_memory(void *result, VirtualAddress addr,
62 unsigned int size, void *extra)
64 /* FIXME: this should do some horrible signal stuff to catch
65 * SEGV cleanly and fail.
67 * Or else the caller should do the setjmp for efficiency.
70 memcpy(result, (const void *)addr, size);
71 return true;
75 /** Locates an instruction inside the given bundle that
76 * has the specified mnemonic, and whose first 'num_operands_to_match'
77 * operands exactly match those in 'operand_values'.
79 static const struct tile_decoded_instruction *find_matching_insn(
80 const struct BacktraceBundle *bundle,
81 tile_mnemonic mnemonic,
82 const int *operand_values,
83 int num_operands_to_match)
85 int i, j;
86 bool match;
88 for (i = 0; i < bundle->num_insns; i++) {
89 const struct tile_decoded_instruction *insn =
90 &bundle->insns[i];
92 if (insn->opcode->mnemonic != mnemonic)
93 continue;
95 match = true;
96 for (j = 0; j < num_operands_to_match; j++) {
97 if (operand_values[j] != insn->operand_values[j]) {
98 match = false;
99 break;
103 if (match)
104 return insn;
107 return NULL;
110 /** Does this bundle contain an 'iret' instruction? */
111 static inline bool bt_has_iret(const struct BacktraceBundle *bundle)
113 return find_matching_insn(bundle, TILE_OPC_IRET, NULL, 0) != NULL;
116 /** Does this bundle contain an 'addi sp, sp, OFFSET' or
117 * 'addli sp, sp, OFFSET' instruction, and if so, what is OFFSET?
119 static bool bt_has_addi_sp(const struct BacktraceBundle *bundle, int *adjust)
121 static const int vals[2] = { TREG_SP, TREG_SP };
123 const struct tile_decoded_instruction *insn =
124 find_matching_insn(bundle, TILE_OPC_ADDI, vals, 2);
125 if (insn == NULL)
126 insn = find_matching_insn(bundle, TILE_OPC_ADDLI, vals, 2);
127 #if TILE_CHIP >= 10
128 if (insn == NULL)
129 insn = find_matching_insn(bundle, TILEGX_OPC_ADDXLI, vals, 2);
130 if (insn == NULL)
131 insn = find_matching_insn(bundle, TILEGX_OPC_ADDXI, vals, 2);
132 #endif
133 if (insn == NULL)
134 return false;
136 *adjust = insn->operand_values[2];
137 return true;
140 /** Does this bundle contain any 'info OP' or 'infol OP'
141 * instruction, and if so, what are their OP? Note that OP is interpreted
142 * as an unsigned value by this code since that's what the caller wants.
143 * Returns the number of info ops found.
145 static int bt_get_info_ops(const struct BacktraceBundle *bundle,
146 int operands[MAX_INFO_OPS_PER_BUNDLE])
148 int num_ops = 0;
149 int i;
151 for (i = 0; i < bundle->num_insns; i++) {
152 const struct tile_decoded_instruction *insn =
153 &bundle->insns[i];
155 if (insn->opcode->mnemonic == TILE_OPC_INFO ||
156 insn->opcode->mnemonic == TILE_OPC_INFOL) {
157 operands[num_ops++] = insn->operand_values[0];
161 return num_ops;
164 /** Does this bundle contain a jrp instruction, and if so, to which
165 * register is it jumping?
167 static bool bt_has_jrp(const struct BacktraceBundle *bundle, int *target_reg)
169 const struct tile_decoded_instruction *insn =
170 find_matching_insn(bundle, TILE_OPC_JRP, NULL, 0);
171 if (insn == NULL)
172 return false;
174 *target_reg = insn->operand_values[0];
175 return true;
178 /** Does this bundle modify the specified register in any way? */
179 static bool bt_modifies_reg(const struct BacktraceBundle *bundle, int reg)
181 int i, j;
182 for (i = 0; i < bundle->num_insns; i++) {
183 const struct tile_decoded_instruction *insn =
184 &bundle->insns[i];
186 if (insn->opcode->implicitly_written_register == reg)
187 return true;
189 for (j = 0; j < insn->opcode->num_operands; j++)
190 if (insn->operands[j]->is_dest_reg &&
191 insn->operand_values[j] == reg)
192 return true;
195 return false;
198 /** Does this bundle modify sp? */
199 static inline bool bt_modifies_sp(const struct BacktraceBundle *bundle)
201 return bt_modifies_reg(bundle, TREG_SP);
204 /** Does this bundle modify lr? */
205 static inline bool bt_modifies_lr(const struct BacktraceBundle *bundle)
207 return bt_modifies_reg(bundle, TREG_LR);
210 /** Does this bundle contain the instruction 'move fp, sp'? */
211 static inline bool bt_has_move_r52_sp(const struct BacktraceBundle *bundle)
213 static const int vals[2] = { 52, TREG_SP };
214 return find_matching_insn(bundle, TILE_OPC_MOVE, vals, 2) != NULL;
217 /** Does this bundle contain a store of lr to sp? */
218 static inline bool bt_has_sw_sp_lr(const struct BacktraceBundle *bundle)
220 static const int vals[2] = { TREG_SP, TREG_LR };
221 return find_matching_insn(bundle, OPCODE_STORE, vals, 2) != NULL;
224 #if TILE_CHIP >= 10
225 /** Track moveli values placed into registers. */
226 static inline void bt_update_moveli(const struct BacktraceBundle *bundle,
227 int moveli_args[])
229 int i;
230 for (i = 0; i < bundle->num_insns; i++) {
231 const struct tile_decoded_instruction *insn =
232 &bundle->insns[i];
234 if (insn->opcode->mnemonic == TILEGX_OPC_MOVELI) {
235 int reg = insn->operand_values[0];
236 moveli_args[reg] = insn->operand_values[1];
241 /** Does this bundle contain an 'add sp, sp, reg' instruction
242 * from a register that we saw a moveli into, and if so, what
243 * is the value in the register?
245 static bool bt_has_add_sp(const struct BacktraceBundle *bundle, int *adjust,
246 int moveli_args[])
248 static const int vals[2] = { TREG_SP, TREG_SP };
250 const struct tile_decoded_instruction *insn =
251 find_matching_insn(bundle, TILEGX_OPC_ADDX, vals, 2);
252 if (insn) {
253 int reg = insn->operand_values[2];
254 if (moveli_args[reg]) {
255 *adjust = moveli_args[reg];
256 return true;
259 return false;
261 #endif
263 /** Locates the caller's PC and SP for a program starting at the
264 * given address.
266 static void find_caller_pc_and_caller_sp(CallerLocation *location,
267 const VirtualAddress start_pc,
268 BacktraceMemoryReader read_memory_func,
269 void *read_memory_func_extra)
271 /* Have we explicitly decided what the sp is,
272 * rather than just the default?
274 bool sp_determined = false;
276 /* Has any bundle seen so far modified lr? */
277 bool lr_modified = false;
279 /* Have we seen a move from sp to fp? */
280 bool sp_moved_to_r52 = false;
282 /* Have we seen a terminating bundle? */
283 bool seen_terminating_bundle = false;
285 /* Cut down on round-trip reading overhead by reading several
286 * bundles at a time.
288 tile_bundle_bits prefetched_bundles[32];
289 int num_bundles_prefetched = 0;
290 int next_bundle = 0;
291 VirtualAddress pc;
293 #if TILE_CHIP >= 10
294 /* Naively try to track moveli values to support addx for -m32. */
295 int moveli_args[TILEGX_NUM_REGISTERS] = { 0 };
296 #endif
298 /* Default to assuming that the caller's sp is the current sp.
299 * This is necessary to handle the case where we start backtracing
300 * right at the end of the epilog.
302 location->sp_location = SP_LOC_OFFSET;
303 location->sp_offset = 0;
305 /* Default to having no idea where the caller PC is. */
306 location->pc_location = PC_LOC_UNKNOWN;
308 /* Don't even try if the PC is not aligned. */
309 if (start_pc % TILE_BUNDLE_ALIGNMENT_IN_BYTES != 0)
310 return;
312 for (pc = start_pc;; pc += sizeof(tile_bundle_bits)) {
314 struct BacktraceBundle bundle;
315 int num_info_ops, info_operands[MAX_INFO_OPS_PER_BUNDLE];
316 int one_ago, jrp_reg;
317 bool has_jrp;
319 if (next_bundle >= num_bundles_prefetched) {
320 /* Prefetch some bytes, but don't cross a page
321 * boundary since that might cause a read failure we
322 * don't care about if we only need the first few
323 * bytes. Note: we don't care what the actual page
324 * size is; using the minimum possible page size will
325 * prevent any problems.
327 unsigned int bytes_to_prefetch = 4096 - (pc & 4095);
328 if (bytes_to_prefetch > sizeof prefetched_bundles)
329 bytes_to_prefetch = sizeof prefetched_bundles;
331 if (!read_memory_func(prefetched_bundles, pc,
332 bytes_to_prefetch,
333 read_memory_func_extra)) {
334 if (pc == start_pc) {
335 /* The program probably called a bad
336 * address, such as a NULL pointer.
337 * So treat this as if we are at the
338 * start of the function prolog so the
339 * backtrace will show how we got here.
341 location->pc_location = PC_LOC_IN_LR;
342 return;
345 /* Unreadable address. Give up. */
346 break;
349 next_bundle = 0;
350 num_bundles_prefetched =
351 bytes_to_prefetch / sizeof(tile_bundle_bits);
354 /* Decode the next bundle. */
355 bundle.bits = prefetched_bundles[next_bundle++];
356 bundle.num_insns =
357 parse_insn_tile(bundle.bits, pc, bundle.insns);
358 num_info_ops = bt_get_info_ops(&bundle, info_operands);
360 /* First look at any one_ago info ops if they are interesting,
361 * since they should shadow any non-one-ago info ops.
363 for (one_ago = (pc != start_pc) ? 1 : 0;
364 one_ago >= 0; one_ago--) {
365 int i;
366 for (i = 0; i < num_info_ops; i++) {
367 int info_operand = info_operands[i];
368 if (info_operand < CALLER_UNKNOWN_BASE) {
369 /* Weird; reserved value, ignore it. */
370 continue;
373 /* Skip info ops which are not in the
374 * "one_ago" mode we want right now.
376 if (((info_operand & ONE_BUNDLE_AGO_FLAG) != 0)
377 != (one_ago != 0))
378 continue;
380 /* Clear the flag to make later checking
381 * easier. */
382 info_operand &= ~ONE_BUNDLE_AGO_FLAG;
384 /* Default to looking at PC_IN_LR_FLAG. */
385 if (info_operand & PC_IN_LR_FLAG)
386 location->pc_location =
387 PC_LOC_IN_LR;
388 else
389 location->pc_location =
390 PC_LOC_ON_STACK;
392 switch (info_operand) {
393 case CALLER_UNKNOWN_BASE:
394 location->pc_location = PC_LOC_UNKNOWN;
395 location->sp_location = SP_LOC_UNKNOWN;
396 return;
398 case CALLER_SP_IN_R52_BASE:
399 case CALLER_SP_IN_R52_BASE | PC_IN_LR_FLAG:
400 location->sp_location = SP_LOC_IN_R52;
401 return;
403 default:
405 const unsigned int val = info_operand
406 - CALLER_SP_OFFSET_BASE;
407 const unsigned int sp_offset =
408 (val >> NUM_INFO_OP_FLAGS) * 8;
409 if (sp_offset < 32768) {
410 /* This is a properly encoded
411 * SP offset. */
412 location->sp_location =
413 SP_LOC_OFFSET;
414 location->sp_offset =
415 sp_offset;
416 return;
417 } else {
418 /* This looked like an SP
419 * offset, but it's outside
420 * the legal range, so this
421 * must be an unrecognized
422 * info operand. Ignore it.
426 break;
431 if (seen_terminating_bundle) {
432 /* We saw a terminating bundle during the previous
433 * iteration, so we were only looking for an info op.
435 break;
438 if (bundle.bits == 0) {
439 /* Wacky terminating bundle. Stop looping, and hope
440 * we've already seen enough to find the caller.
442 break;
446 * Try to determine caller's SP.
449 if (!sp_determined) {
450 int adjust;
451 if (bt_has_addi_sp(&bundle, &adjust)
452 #if TILE_CHIP >= 10
453 || bt_has_add_sp(&bundle, &adjust, moveli_args)
454 #endif
456 location->sp_location = SP_LOC_OFFSET;
458 if (adjust <= 0) {
459 /* We are in prolog about to adjust
460 * SP. */
461 location->sp_offset = 0;
462 } else {
463 /* We are in epilog restoring SP. */
464 location->sp_offset = adjust;
467 sp_determined = true;
468 } else {
469 if (bt_has_move_r52_sp(&bundle)) {
470 /* Maybe in prolog, creating an
471 * alloca-style frame. But maybe in
472 * the middle of a fixed-size frame
473 * clobbering r52 with SP.
475 sp_moved_to_r52 = true;
478 if (bt_modifies_sp(&bundle)) {
479 if (sp_moved_to_r52) {
480 /* We saw SP get saved into
481 * r52 earlier (or now), which
482 * must have been in the
483 * prolog, so we now know that
484 * SP is still holding the
485 * caller's sp value.
487 location->sp_location =
488 SP_LOC_OFFSET;
489 location->sp_offset = 0;
490 } else {
491 /* Someone must have saved
492 * aside the caller's SP value
493 * into r52, so r52 holds the
494 * current value.
496 location->sp_location =
497 SP_LOC_IN_R52;
499 sp_determined = true;
503 #if TILE_CHIP >= 10
504 /* Track moveli arguments for -m32 mode. */
505 bt_update_moveli(&bundle, moveli_args);
506 #endif
509 if (bt_has_iret(&bundle)) {
510 /* This is a terminating bundle. */
511 seen_terminating_bundle = true;
512 continue;
516 * Try to determine caller's PC.
519 jrp_reg = -1;
520 has_jrp = bt_has_jrp(&bundle, &jrp_reg);
521 if (has_jrp)
522 seen_terminating_bundle = true;
524 if (location->pc_location == PC_LOC_UNKNOWN) {
525 if (has_jrp) {
526 if (jrp_reg == TREG_LR && !lr_modified) {
527 /* Looks like a leaf function, or else
528 * lr is already restored. */
529 location->pc_location =
530 PC_LOC_IN_LR;
531 } else {
532 location->pc_location =
533 PC_LOC_ON_STACK;
535 } else if (bt_has_sw_sp_lr(&bundle)) {
536 /* In prolog, spilling initial lr to stack. */
537 location->pc_location = PC_LOC_IN_LR;
538 } else if (bt_modifies_lr(&bundle)) {
539 lr_modified = true;
545 void backtrace_init(BacktraceIterator *state,
546 BacktraceMemoryReader read_memory_func,
547 void *read_memory_func_extra,
548 VirtualAddress pc, VirtualAddress lr,
549 VirtualAddress sp, VirtualAddress r52)
551 CallerLocation location;
552 VirtualAddress fp, initial_frame_caller_pc;
554 if (read_memory_func == NULL) {
555 read_memory_func = bt_read_memory;
558 /* Find out where we are in the initial frame. */
559 find_caller_pc_and_caller_sp(&location, pc,
560 read_memory_func, read_memory_func_extra);
562 switch (location.sp_location) {
563 case SP_LOC_UNKNOWN:
564 /* Give up. */
565 fp = -1;
566 break;
568 case SP_LOC_IN_R52:
569 fp = r52;
570 break;
572 case SP_LOC_OFFSET:
573 fp = sp + location.sp_offset;
574 break;
576 default:
577 /* Give up. */
578 fp = -1;
579 break;
582 /* If the frame pointer is not aligned to the basic word size
583 * something terrible happened and we should mark it as invalid.
585 if (fp % sizeof(bt_int_reg_t) != 0)
586 fp = -1;
588 /* -1 means "don't know initial_frame_caller_pc". */
589 initial_frame_caller_pc = -1;
591 switch (location.pc_location) {
592 case PC_LOC_UNKNOWN:
593 /* Give up. */
594 fp = -1;
595 break;
597 case PC_LOC_IN_LR:
598 if (lr == 0 || lr % TILE_BUNDLE_ALIGNMENT_IN_BYTES != 0) {
599 /* Give up. */
600 fp = -1;
601 } else {
602 initial_frame_caller_pc = lr;
604 break;
606 case PC_LOC_ON_STACK:
607 /* Leave initial_frame_caller_pc as -1,
608 * meaning check the stack.
610 break;
612 default:
613 /* Give up. */
614 fp = -1;
615 break;
618 state->pc = pc;
619 state->sp = sp;
620 state->fp = fp;
621 state->initial_frame_caller_pc = initial_frame_caller_pc;
622 state->read_memory_func = read_memory_func;
623 state->read_memory_func_extra = read_memory_func_extra;
626 /* Handle the case where the register holds more bits than the VA. */
627 static bool valid_addr_reg(bt_int_reg_t reg)
629 return ((VirtualAddress)reg == reg);
632 bool backtrace_next(BacktraceIterator *state)
634 VirtualAddress next_fp, next_pc;
635 bt_int_reg_t next_frame[2];
637 if (state->fp == -1) {
638 /* No parent frame. */
639 return false;
642 /* Try to read the frame linkage data chaining to the next function. */
643 if (!state->read_memory_func(&next_frame, state->fp, sizeof next_frame,
644 state->read_memory_func_extra)) {
645 return false;
648 next_fp = next_frame[1];
649 if (!valid_addr_reg(next_frame[1]) ||
650 next_fp % sizeof(bt_int_reg_t) != 0) {
651 /* Caller's frame pointer is suspect, so give up. */
652 return false;
655 if (state->initial_frame_caller_pc != -1) {
656 /* We must be in the initial stack frame and already know the
657 * caller PC.
659 next_pc = state->initial_frame_caller_pc;
661 /* Force reading stack next time, in case we were in the
662 * initial frame. We don't do this above just to paranoidly
663 * avoid changing the struct at all when we return false.
665 state->initial_frame_caller_pc = -1;
666 } else {
667 /* Get the caller PC from the frame linkage area. */
668 next_pc = next_frame[0];
669 if (!valid_addr_reg(next_frame[0]) || next_pc == 0 ||
670 next_pc % TILE_BUNDLE_ALIGNMENT_IN_BYTES != 0) {
671 /* The PC is suspect, so give up. */
672 return false;
676 /* Update state to become the caller's stack frame. */
677 state->pc = next_pc;
678 state->sp = state->fp;
679 state->fp = next_fp;
681 return true;