2 * Copyright 2011 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>
17 #include <asm/backtrace.h>
18 #include <asm/tile-desc.h>
22 #define TILE_MAX_INSTRUCTIONS_PER_BUNDLE TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE
23 #define tile_decoded_instruction tilegx_decoded_instruction
24 #define tile_mnemonic tilegx_mnemonic
25 #define parse_insn_tile parse_insn_tilegx
26 #define TILE_OPC_IRET TILEGX_OPC_IRET
27 #define TILE_OPC_ADDI TILEGX_OPC_ADDI
28 #define TILE_OPC_ADDLI TILEGX_OPC_ADDLI
29 #define TILE_OPC_INFO TILEGX_OPC_INFO
30 #define TILE_OPC_INFOL TILEGX_OPC_INFOL
31 #define TILE_OPC_JRP TILEGX_OPC_JRP
32 #define TILE_OPC_MOVE TILEGX_OPC_MOVE
33 #define OPCODE_STORE TILEGX_OPC_ST
34 typedef long long bt_int_reg_t
;
36 #define TILE_MAX_INSTRUCTIONS_PER_BUNDLE TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE
37 #define tile_decoded_instruction tilepro_decoded_instruction
38 #define tile_mnemonic tilepro_mnemonic
39 #define parse_insn_tile parse_insn_tilepro
40 #define TILE_OPC_IRET TILEPRO_OPC_IRET
41 #define TILE_OPC_ADDI TILEPRO_OPC_ADDI
42 #define TILE_OPC_ADDLI TILEPRO_OPC_ADDLI
43 #define TILE_OPC_INFO TILEPRO_OPC_INFO
44 #define TILE_OPC_INFOL TILEPRO_OPC_INFOL
45 #define TILE_OPC_JRP TILEPRO_OPC_JRP
46 #define TILE_OPC_MOVE TILEPRO_OPC_MOVE
47 #define OPCODE_STORE TILEPRO_OPC_SW
48 typedef int bt_int_reg_t
;
51 /* A decoded bundle used for backtracer analysis. */
52 struct BacktraceBundle
{
53 tile_bundle_bits bits
;
55 struct tile_decoded_instruction
56 insns
[TILE_MAX_INSTRUCTIONS_PER_BUNDLE
];
60 /* Locates an instruction inside the given bundle that
61 * has the specified mnemonic, and whose first 'num_operands_to_match'
62 * operands exactly match those in 'operand_values'.
64 static const struct tile_decoded_instruction
*find_matching_insn(
65 const struct BacktraceBundle
*bundle
,
66 tile_mnemonic mnemonic
,
67 const int *operand_values
,
68 int num_operands_to_match
)
73 for (i
= 0; i
< bundle
->num_insns
; i
++) {
74 const struct tile_decoded_instruction
*insn
=
77 if (insn
->opcode
->mnemonic
!= mnemonic
)
81 for (j
= 0; j
< num_operands_to_match
; j
++) {
82 if (operand_values
[j
] != insn
->operand_values
[j
]) {
95 /* Does this bundle contain an 'iret' instruction? */
96 static inline bool bt_has_iret(const struct BacktraceBundle
*bundle
)
98 return find_matching_insn(bundle
, TILE_OPC_IRET
, NULL
, 0) != NULL
;
101 /* Does this bundle contain an 'addi sp, sp, OFFSET' or
102 * 'addli sp, sp, OFFSET' instruction, and if so, what is OFFSET?
104 static bool bt_has_addi_sp(const struct BacktraceBundle
*bundle
, int *adjust
)
106 static const int vals
[2] = { TREG_SP
, TREG_SP
};
108 const struct tile_decoded_instruction
*insn
=
109 find_matching_insn(bundle
, TILE_OPC_ADDI
, vals
, 2);
111 insn
= find_matching_insn(bundle
, TILE_OPC_ADDLI
, vals
, 2);
114 insn
= find_matching_insn(bundle
, TILEGX_OPC_ADDXLI
, vals
, 2);
116 insn
= find_matching_insn(bundle
, TILEGX_OPC_ADDXI
, vals
, 2);
121 *adjust
= insn
->operand_values
[2];
125 /* Does this bundle contain any 'info OP' or 'infol OP'
126 * instruction, and if so, what are their OP? Note that OP is interpreted
127 * as an unsigned value by this code since that's what the caller wants.
128 * Returns the number of info ops found.
130 static int bt_get_info_ops(const struct BacktraceBundle
*bundle
,
131 int operands
[MAX_INFO_OPS_PER_BUNDLE
])
136 for (i
= 0; i
< bundle
->num_insns
; i
++) {
137 const struct tile_decoded_instruction
*insn
=
140 if (insn
->opcode
->mnemonic
== TILE_OPC_INFO
||
141 insn
->opcode
->mnemonic
== TILE_OPC_INFOL
) {
142 operands
[num_ops
++] = insn
->operand_values
[0];
149 /* Does this bundle contain a jrp instruction, and if so, to which
150 * register is it jumping?
152 static bool bt_has_jrp(const struct BacktraceBundle
*bundle
, int *target_reg
)
154 const struct tile_decoded_instruction
*insn
=
155 find_matching_insn(bundle
, TILE_OPC_JRP
, NULL
, 0);
159 *target_reg
= insn
->operand_values
[0];
163 /* Does this bundle modify the specified register in any way? */
164 static bool bt_modifies_reg(const struct BacktraceBundle
*bundle
, int reg
)
167 for (i
= 0; i
< bundle
->num_insns
; i
++) {
168 const struct tile_decoded_instruction
*insn
=
171 if (insn
->opcode
->implicitly_written_register
== reg
)
174 for (j
= 0; j
< insn
->opcode
->num_operands
; j
++)
175 if (insn
->operands
[j
]->is_dest_reg
&&
176 insn
->operand_values
[j
] == reg
)
183 /* Does this bundle modify sp? */
184 static inline bool bt_modifies_sp(const struct BacktraceBundle
*bundle
)
186 return bt_modifies_reg(bundle
, TREG_SP
);
189 /* Does this bundle modify lr? */
190 static inline bool bt_modifies_lr(const struct BacktraceBundle
*bundle
)
192 return bt_modifies_reg(bundle
, TREG_LR
);
195 /* Does this bundle contain the instruction 'move fp, sp'? */
196 static inline bool bt_has_move_r52_sp(const struct BacktraceBundle
*bundle
)
198 static const int vals
[2] = { 52, TREG_SP
};
199 return find_matching_insn(bundle
, TILE_OPC_MOVE
, vals
, 2) != NULL
;
202 /* Does this bundle contain a store of lr to sp? */
203 static inline bool bt_has_sw_sp_lr(const struct BacktraceBundle
*bundle
)
205 static const int vals
[2] = { TREG_SP
, TREG_LR
};
206 return find_matching_insn(bundle
, OPCODE_STORE
, vals
, 2) != NULL
;
210 /* Track moveli values placed into registers. */
211 static inline void bt_update_moveli(const struct BacktraceBundle
*bundle
,
215 for (i
= 0; i
< bundle
->num_insns
; i
++) {
216 const struct tile_decoded_instruction
*insn
=
219 if (insn
->opcode
->mnemonic
== TILEGX_OPC_MOVELI
) {
220 int reg
= insn
->operand_values
[0];
221 moveli_args
[reg
] = insn
->operand_values
[1];
226 /* Does this bundle contain an 'add sp, sp, reg' instruction
227 * from a register that we saw a moveli into, and if so, what
228 * is the value in the register?
230 static bool bt_has_add_sp(const struct BacktraceBundle
*bundle
, int *adjust
,
233 static const int vals
[2] = { TREG_SP
, TREG_SP
};
235 const struct tile_decoded_instruction
*insn
=
236 find_matching_insn(bundle
, TILEGX_OPC_ADDX
, vals
, 2);
238 int reg
= insn
->operand_values
[2];
239 if (moveli_args
[reg
]) {
240 *adjust
= moveli_args
[reg
];
248 /* Locates the caller's PC and SP for a program starting at the
251 static void find_caller_pc_and_caller_sp(CallerLocation
*location
,
252 const unsigned long start_pc
,
253 BacktraceMemoryReader read_memory_func
,
254 void *read_memory_func_extra
)
256 /* Have we explicitly decided what the sp is,
257 * rather than just the default?
259 bool sp_determined
= false;
261 /* Has any bundle seen so far modified lr? */
262 bool lr_modified
= false;
264 /* Have we seen a move from sp to fp? */
265 bool sp_moved_to_r52
= false;
267 /* Have we seen a terminating bundle? */
268 bool seen_terminating_bundle
= false;
270 /* Cut down on round-trip reading overhead by reading several
273 tile_bundle_bits prefetched_bundles
[32];
274 int num_bundles_prefetched
= 0;
279 /* Naively try to track moveli values to support addx for -m32. */
280 int moveli_args
[TILEGX_NUM_REGISTERS
] = { 0 };
283 /* Default to assuming that the caller's sp is the current sp.
284 * This is necessary to handle the case where we start backtracing
285 * right at the end of the epilog.
287 location
->sp_location
= SP_LOC_OFFSET
;
288 location
->sp_offset
= 0;
290 /* Default to having no idea where the caller PC is. */
291 location
->pc_location
= PC_LOC_UNKNOWN
;
293 /* Don't even try if the PC is not aligned. */
294 if (start_pc
% TILE_BUNDLE_ALIGNMENT_IN_BYTES
!= 0)
297 for (pc
= start_pc
;; pc
+= sizeof(tile_bundle_bits
)) {
299 struct BacktraceBundle bundle
;
300 int num_info_ops
, info_operands
[MAX_INFO_OPS_PER_BUNDLE
];
301 int one_ago
, jrp_reg
;
304 if (next_bundle
>= num_bundles_prefetched
) {
305 /* Prefetch some bytes, but don't cross a page
306 * boundary since that might cause a read failure we
307 * don't care about if we only need the first few
308 * bytes. Note: we don't care what the actual page
309 * size is; using the minimum possible page size will
310 * prevent any problems.
312 unsigned int bytes_to_prefetch
= 4096 - (pc
& 4095);
313 if (bytes_to_prefetch
> sizeof prefetched_bundles
)
314 bytes_to_prefetch
= sizeof prefetched_bundles
;
316 if (!read_memory_func(prefetched_bundles
, pc
,
318 read_memory_func_extra
)) {
319 if (pc
== start_pc
) {
320 /* The program probably called a bad
321 * address, such as a NULL pointer.
322 * So treat this as if we are at the
323 * start of the function prolog so the
324 * backtrace will show how we got here.
326 location
->pc_location
= PC_LOC_IN_LR
;
330 /* Unreadable address. Give up. */
335 num_bundles_prefetched
=
336 bytes_to_prefetch
/ sizeof(tile_bundle_bits
);
339 /* Decode the next bundle. */
340 bundle
.bits
= prefetched_bundles
[next_bundle
++];
342 parse_insn_tile(bundle
.bits
, pc
, bundle
.insns
);
343 num_info_ops
= bt_get_info_ops(&bundle
, info_operands
);
345 /* First look at any one_ago info ops if they are interesting,
346 * since they should shadow any non-one-ago info ops.
348 for (one_ago
= (pc
!= start_pc
) ? 1 : 0;
349 one_ago
>= 0; one_ago
--) {
351 for (i
= 0; i
< num_info_ops
; i
++) {
352 int info_operand
= info_operands
[i
];
353 if (info_operand
< CALLER_UNKNOWN_BASE
) {
354 /* Weird; reserved value, ignore it. */
358 /* Skip info ops which are not in the
359 * "one_ago" mode we want right now.
361 if (((info_operand
& ONE_BUNDLE_AGO_FLAG
) != 0)
365 /* Clear the flag to make later checking
367 info_operand
&= ~ONE_BUNDLE_AGO_FLAG
;
369 /* Default to looking at PC_IN_LR_FLAG. */
370 if (info_operand
& PC_IN_LR_FLAG
)
371 location
->pc_location
=
374 location
->pc_location
=
377 switch (info_operand
) {
378 case CALLER_UNKNOWN_BASE
:
379 location
->pc_location
= PC_LOC_UNKNOWN
;
380 location
->sp_location
= SP_LOC_UNKNOWN
;
383 case CALLER_SP_IN_R52_BASE
:
384 case CALLER_SP_IN_R52_BASE
| PC_IN_LR_FLAG
:
385 location
->sp_location
= SP_LOC_IN_R52
;
390 const unsigned int val
= info_operand
391 - CALLER_SP_OFFSET_BASE
;
392 const unsigned int sp_offset
=
393 (val
>> NUM_INFO_OP_FLAGS
) * 8;
394 if (sp_offset
< 32768) {
395 /* This is a properly encoded
397 location
->sp_location
=
399 location
->sp_offset
=
403 /* This looked like an SP
404 * offset, but it's outside
405 * the legal range, so this
406 * must be an unrecognized
407 * info operand. Ignore it.
416 if (seen_terminating_bundle
) {
417 /* We saw a terminating bundle during the previous
418 * iteration, so we were only looking for an info op.
423 if (bundle
.bits
== 0) {
424 /* Wacky terminating bundle. Stop looping, and hope
425 * we've already seen enough to find the caller.
431 * Try to determine caller's SP.
434 if (!sp_determined
) {
436 if (bt_has_addi_sp(&bundle
, &adjust
)
438 || bt_has_add_sp(&bundle
, &adjust
, moveli_args
)
441 location
->sp_location
= SP_LOC_OFFSET
;
444 /* We are in prolog about to adjust
446 location
->sp_offset
= 0;
448 /* We are in epilog restoring SP. */
449 location
->sp_offset
= adjust
;
452 sp_determined
= true;
454 if (bt_has_move_r52_sp(&bundle
)) {
455 /* Maybe in prolog, creating an
456 * alloca-style frame. But maybe in
457 * the middle of a fixed-size frame
458 * clobbering r52 with SP.
460 sp_moved_to_r52
= true;
463 if (bt_modifies_sp(&bundle
)) {
464 if (sp_moved_to_r52
) {
465 /* We saw SP get saved into
466 * r52 earlier (or now), which
467 * must have been in the
468 * prolog, so we now know that
469 * SP is still holding the
472 location
->sp_location
=
474 location
->sp_offset
= 0;
476 /* Someone must have saved
477 * aside the caller's SP value
478 * into r52, so r52 holds the
481 location
->sp_location
=
484 sp_determined
= true;
489 /* Track moveli arguments for -m32 mode. */
490 bt_update_moveli(&bundle
, moveli_args
);
494 if (bt_has_iret(&bundle
)) {
495 /* This is a terminating bundle. */
496 seen_terminating_bundle
= true;
501 * Try to determine caller's PC.
505 has_jrp
= bt_has_jrp(&bundle
, &jrp_reg
);
507 seen_terminating_bundle
= true;
509 if (location
->pc_location
== PC_LOC_UNKNOWN
) {
511 if (jrp_reg
== TREG_LR
&& !lr_modified
) {
512 /* Looks like a leaf function, or else
513 * lr is already restored. */
514 location
->pc_location
=
517 location
->pc_location
=
520 } else if (bt_has_sw_sp_lr(&bundle
)) {
521 /* In prolog, spilling initial lr to stack. */
522 location
->pc_location
= PC_LOC_IN_LR
;
523 } else if (bt_modifies_lr(&bundle
)) {
530 /* Initializes a backtracer to start from the given location.
532 * If the frame pointer cannot be determined it is set to -1.
534 * state: The state to be filled in.
535 * read_memory_func: A callback that reads memory.
536 * read_memory_func_extra: An arbitrary argument to read_memory_func.
537 * pc: The current PC.
538 * lr: The current value of the 'lr' register.
539 * sp: The current value of the 'sp' register.
540 * r52: The current value of the 'r52' register.
542 void backtrace_init(BacktraceIterator
*state
,
543 BacktraceMemoryReader read_memory_func
,
544 void *read_memory_func_extra
,
545 unsigned long pc
, unsigned long lr
,
546 unsigned long sp
, unsigned long r52
)
548 CallerLocation location
;
549 unsigned long fp
, initial_frame_caller_pc
;
551 /* Find out where we are in the initial frame. */
552 find_caller_pc_and_caller_sp(&location
, pc
,
553 read_memory_func
, read_memory_func_extra
);
555 switch (location
.sp_location
) {
566 fp
= sp
+ location
.sp_offset
;
575 /* If the frame pointer is not aligned to the basic word size
576 * something terrible happened and we should mark it as invalid.
578 if (fp
% sizeof(bt_int_reg_t
) != 0)
581 /* -1 means "don't know initial_frame_caller_pc". */
582 initial_frame_caller_pc
= -1;
584 switch (location
.pc_location
) {
591 if (lr
== 0 || lr
% TILE_BUNDLE_ALIGNMENT_IN_BYTES
!= 0) {
595 initial_frame_caller_pc
= lr
;
599 case PC_LOC_ON_STACK
:
600 /* Leave initial_frame_caller_pc as -1,
601 * meaning check the stack.
614 state
->initial_frame_caller_pc
= initial_frame_caller_pc
;
615 state
->read_memory_func
= read_memory_func
;
616 state
->read_memory_func_extra
= read_memory_func_extra
;
619 /* Handle the case where the register holds more bits than the VA. */
620 static bool valid_addr_reg(bt_int_reg_t reg
)
622 return ((unsigned long)reg
== reg
);
625 /* Advances the backtracing state to the calling frame, returning
626 * true iff successful.
628 bool backtrace_next(BacktraceIterator
*state
)
630 unsigned long next_fp
, next_pc
;
631 bt_int_reg_t next_frame
[2];
633 if (state
->fp
== -1) {
634 /* No parent frame. */
638 /* Try to read the frame linkage data chaining to the next function. */
639 if (!state
->read_memory_func(&next_frame
, state
->fp
, sizeof next_frame
,
640 state
->read_memory_func_extra
)) {
644 next_fp
= next_frame
[1];
645 if (!valid_addr_reg(next_frame
[1]) ||
646 next_fp
% sizeof(bt_int_reg_t
) != 0) {
647 /* Caller's frame pointer is suspect, so give up. */
651 if (state
->initial_frame_caller_pc
!= -1) {
652 /* We must be in the initial stack frame and already know the
655 next_pc
= state
->initial_frame_caller_pc
;
657 /* Force reading stack next time, in case we were in the
658 * initial frame. We don't do this above just to paranoidly
659 * avoid changing the struct at all when we return false.
661 state
->initial_frame_caller_pc
= -1;
663 /* Get the caller PC from the frame linkage area. */
664 next_pc
= next_frame
[0];
665 if (!valid_addr_reg(next_frame
[0]) || next_pc
== 0 ||
666 next_pc
% TILE_BUNDLE_ALIGNMENT_IN_BYTES
!= 0) {
667 /* The PC is suspect, so give up. */
672 /* Update state to become the caller's stack frame. */
674 state
->sp
= state
->fp
;