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
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>
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
;
46 #define OPCODE_STORE TILE_OPC_SW
47 typedef int bt_int_reg_t
;
50 /** A decoded bundle used for backtracer analysis. */
51 struct BacktraceBundle
{
52 tile_bundle_bits bits
;
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
);
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
)
88 for (i
= 0; i
< bundle
->num_insns
; i
++) {
89 const struct tile_decoded_instruction
*insn
=
92 if (insn
->opcode
->mnemonic
!= mnemonic
)
96 for (j
= 0; j
< num_operands_to_match
; j
++) {
97 if (operand_values
[j
] != insn
->operand_values
[j
]) {
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);
126 insn
= find_matching_insn(bundle
, TILE_OPC_ADDLI
, vals
, 2);
129 insn
= find_matching_insn(bundle
, TILEGX_OPC_ADDXLI
, vals
, 2);
131 insn
= find_matching_insn(bundle
, TILEGX_OPC_ADDXI
, vals
, 2);
136 *adjust
= insn
->operand_values
[2];
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
])
151 for (i
= 0; i
< bundle
->num_insns
; i
++) {
152 const struct tile_decoded_instruction
*insn
=
155 if (insn
->opcode
->mnemonic
== TILE_OPC_INFO
||
156 insn
->opcode
->mnemonic
== TILE_OPC_INFOL
) {
157 operands
[num_ops
++] = insn
->operand_values
[0];
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);
174 *target_reg
= insn
->operand_values
[0];
178 /** Does this bundle modify the specified register in any way? */
179 static bool bt_modifies_reg(const struct BacktraceBundle
*bundle
, int reg
)
182 for (i
= 0; i
< bundle
->num_insns
; i
++) {
183 const struct tile_decoded_instruction
*insn
=
186 if (insn
->opcode
->implicitly_written_register
== reg
)
189 for (j
= 0; j
< insn
->opcode
->num_operands
; j
++)
190 if (insn
->operands
[j
]->is_dest_reg
&&
191 insn
->operand_values
[j
] == reg
)
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
;
225 /** Track moveli values placed into registers. */
226 static inline void bt_update_moveli(const struct BacktraceBundle
*bundle
,
230 for (i
= 0; i
< bundle
->num_insns
; i
++) {
231 const struct tile_decoded_instruction
*insn
=
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
,
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);
253 int reg
= insn
->operand_values
[2];
254 if (moveli_args
[reg
]) {
255 *adjust
= moveli_args
[reg
];
263 /** Locates the caller's PC and SP for a program starting at the
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
288 tile_bundle_bits prefetched_bundles
[32];
289 int num_bundles_prefetched
= 0;
294 /* Naively try to track moveli values to support addx for -m32. */
295 int moveli_args
[TILEGX_NUM_REGISTERS
] = { 0 };
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)
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
;
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
,
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
;
345 /* Unreadable address. Give up. */
350 num_bundles_prefetched
=
351 bytes_to_prefetch
/ sizeof(tile_bundle_bits
);
354 /* Decode the next bundle. */
355 bundle
.bits
= prefetched_bundles
[next_bundle
++];
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
--) {
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. */
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)
380 /* Clear the flag to make later checking
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
=
389 location
->pc_location
=
392 switch (info_operand
) {
393 case CALLER_UNKNOWN_BASE
:
394 location
->pc_location
= PC_LOC_UNKNOWN
;
395 location
->sp_location
= SP_LOC_UNKNOWN
;
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
;
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
412 location
->sp_location
=
414 location
->sp_offset
=
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.
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.
438 if (bundle
.bits
== 0) {
439 /* Wacky terminating bundle. Stop looping, and hope
440 * we've already seen enough to find the caller.
446 * Try to determine caller's SP.
449 if (!sp_determined
) {
451 if (bt_has_addi_sp(&bundle
, &adjust
)
453 || bt_has_add_sp(&bundle
, &adjust
, moveli_args
)
456 location
->sp_location
= SP_LOC_OFFSET
;
459 /* We are in prolog about to adjust
461 location
->sp_offset
= 0;
463 /* We are in epilog restoring SP. */
464 location
->sp_offset
= adjust
;
467 sp_determined
= true;
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
487 location
->sp_location
=
489 location
->sp_offset
= 0;
491 /* Someone must have saved
492 * aside the caller's SP value
493 * into r52, so r52 holds the
496 location
->sp_location
=
499 sp_determined
= true;
504 /* Track moveli arguments for -m32 mode. */
505 bt_update_moveli(&bundle
, moveli_args
);
509 if (bt_has_iret(&bundle
)) {
510 /* This is a terminating bundle. */
511 seen_terminating_bundle
= true;
516 * Try to determine caller's PC.
520 has_jrp
= bt_has_jrp(&bundle
, &jrp_reg
);
522 seen_terminating_bundle
= true;
524 if (location
->pc_location
== PC_LOC_UNKNOWN
) {
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
=
532 location
->pc_location
=
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
)) {
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
) {
573 fp
= sp
+ location
.sp_offset
;
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)
588 /* -1 means "don't know initial_frame_caller_pc". */
589 initial_frame_caller_pc
= -1;
591 switch (location
.pc_location
) {
598 if (lr
== 0 || lr
% TILE_BUNDLE_ALIGNMENT_IN_BYTES
!= 0) {
602 initial_frame_caller_pc
= lr
;
606 case PC_LOC_ON_STACK
:
607 /* Leave initial_frame_caller_pc as -1,
608 * meaning check the stack.
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. */
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
)) {
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. */
655 if (state
->initial_frame_caller_pc
!= -1) {
656 /* We must be in the initial stack frame and already know the
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;
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. */
676 /* Update state to become the caller's stack frame. */
678 state
->sp
= state
->fp
;