1 /*
2  * Copyright 2010 Tilera Corporation. All Rights Reserved.
3  *
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.
7  *
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.
13  */
14 
15 #ifndef _TILE_BACKTRACE_H
16 #define _TILE_BACKTRACE_H
17 
18 
19 
20 #include <linux/types.h>
21 
22 #include <arch/chip.h>
23 
24 #if defined(__tile__)
25 typedef unsigned long VirtualAddress;
26 #elif CHIP_VA_WIDTH() > 32
27 typedef unsigned long long VirtualAddress;
28 #else
29 typedef unsigned int VirtualAddress;
30 #endif
31 
32 
33 /** Reads 'size' bytes from 'address' and writes the data to 'result'.
34  * Returns true if successful, else false (e.g. memory not readable).
35  */
36 typedef bool (*BacktraceMemoryReader)(void *result,
37 				      VirtualAddress address,
38 				      unsigned int size,
39 				      void *extra);
40 
41 typedef struct {
42 	/** Current PC. */
43 	VirtualAddress pc;
44 
45 	/** Current stack pointer value. */
46 	VirtualAddress sp;
47 
48 	/** Current frame pointer value (i.e. caller's stack pointer) */
49 	VirtualAddress fp;
50 
51 	/** Internal use only: caller's PC for first frame. */
52 	VirtualAddress initial_frame_caller_pc;
53 
54 	/** Internal use only: callback to read memory. */
55 	BacktraceMemoryReader read_memory_func;
56 
57 	/** Internal use only: arbitrary argument to read_memory_func. */
58 	void *read_memory_func_extra;
59 
60 } BacktraceIterator;
61 
62 
63 /** Initializes a backtracer to start from the given location.
64  *
65  * If the frame pointer cannot be determined it is set to -1.
66  *
67  * @param state The state to be filled in.
68  * @param read_memory_func A callback that reads memory. If NULL, a default
69  *        value is provided.
70  * @param read_memory_func_extra An arbitrary argument to read_memory_func.
71  * @param pc The current PC.
72  * @param lr The current value of the 'lr' register.
73  * @param sp The current value of the 'sp' register.
74  * @param r52 The current value of the 'r52' register.
75  */
76 extern void backtrace_init(BacktraceIterator *state,
77 			   BacktraceMemoryReader read_memory_func,
78 			   void *read_memory_func_extra,
79 			   VirtualAddress pc, VirtualAddress lr,
80 			   VirtualAddress sp, VirtualAddress r52);
81 
82 
83 /** Advances the backtracing state to the calling frame, returning
84  * true iff successful.
85  */
86 extern bool backtrace_next(BacktraceIterator *state);
87 
88 
89 typedef enum {
90 
91 	/* We have no idea what the caller's pc is. */
92 	PC_LOC_UNKNOWN,
93 
94 	/* The caller's pc is currently in lr. */
95 	PC_LOC_IN_LR,
96 
97 	/* The caller's pc can be found by dereferencing the caller's sp. */
98 	PC_LOC_ON_STACK
99 
100 } CallerPCLocation;
101 
102 
103 typedef enum {
104 
105 	/* We have no idea what the caller's sp is. */
106 	SP_LOC_UNKNOWN,
107 
108 	/* The caller's sp is currently in r52. */
109 	SP_LOC_IN_R52,
110 
111 	/* The caller's sp can be found by adding a certain constant
112 	 * to the current value of sp.
113 	 */
114 	SP_LOC_OFFSET
115 
116 } CallerSPLocation;
117 
118 
119 /* Bit values ORed into CALLER_* values for info ops. */
120 enum {
121 	/* Setting the low bit on any of these values means the info op
122 	 * applies only to one bundle ago.
123 	 */
124 	ONE_BUNDLE_AGO_FLAG = 1,
125 
126 	/* Setting this bit on a CALLER_SP_* value means the PC is in LR.
127 	 * If not set, PC is on the stack.
128 	 */
129 	PC_IN_LR_FLAG = 2,
130 
131 	/* This many of the low bits of a CALLER_SP_* value are for the
132 	 * flag bits above.
133 	 */
134 	NUM_INFO_OP_FLAGS = 2,
135 
136 	/* We cannot have one in the memory pipe so this is the maximum. */
137 	MAX_INFO_OPS_PER_BUNDLE = 2
138 };
139 
140 
141 /** Internal constants used to define 'info' operands. */
142 enum {
143 	/* 0 and 1 are reserved, as are all negative numbers. */
144 
145 	CALLER_UNKNOWN_BASE = 2,
146 
147 	CALLER_SP_IN_R52_BASE = 4,
148 
149 	CALLER_SP_OFFSET_BASE = 8,
150 
151 	/* Marks the entry point of certain functions. */
152 	ENTRY_POINT_INFO_OP = 16
153 };
154 
155 
156 /** Current backtracer state describing where it thinks the caller is. */
157 typedef struct {
158 	/*
159 	 * Public fields
160 	 */
161 
162 	/* How do we find the caller's PC? */
163 	CallerPCLocation pc_location : 8;
164 
165 	/* How do we find the caller's SP? */
166 	CallerSPLocation sp_location : 8;
167 
168 	/* If sp_location == SP_LOC_OFFSET, then caller_sp == sp +
169 	 * loc->sp_offset. Else this field is undefined.
170 	 */
171 	uint16_t sp_offset;
172 
173 	/* In the most recently visited bundle a terminating bundle? */
174 	bool at_terminating_bundle;
175 
176 	/*
177 	 * Private fields
178 	 */
179 
180 	/* Will the forward scanner see someone clobbering sp
181 	 * (i.e. changing it with something other than addi sp, sp, N?)
182 	 */
183 	bool sp_clobber_follows;
184 
185 	/* Operand to next "visible" info op (no more than one bundle past
186 	 * the next terminating bundle), or -32768 if none.
187 	 */
188 	int16_t next_info_operand;
189 
190 	/* Is the info of in next_info_op in the very next bundle? */
191 	bool is_next_info_operand_adjacent;
192 
193 } CallerLocation;
194 
195 
196 
197 
198 #endif /* _TILE_BACKTRACE_H */
199