source: mainline/kernel/arch/mips32/src/debug/stacktrace.c@ 343f2b7e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 343f2b7e was 343f2b7e, checked in by Jakub Jermar <jakub@…>, 15 years ago

Add extra bound checks into mips32 stack tracer.

  • Property mode set to 100644
File size: 6.6 KB
RevLine 
[e1f0fe9]1/*
2 * Copyright (c) 2010 Jakub Jermar
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
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT 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 OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup mips32
30 * @{
31 */
32/** @file
33 */
34
35#include <stacktrace.h>
36#include <syscall/copy.h>
37#include <typedefs.h>
[63bdde6]38#include <arch/debugger.h>
39#include <print.h>
[e1f0fe9]40
[63bdde6]41#define R0 0U
42#define SP 29U
43#define RA 31U
44
45#define OP_SHIFT 26
46#define RS_SHIFT 21
47#define RT_SHIFT 16
48#define RD_SHIFT 11
49
50#define HINT_SHIFT 6
51#define BASE_SHIFT RS_SHIFT
52#define IMM_SHIFT 0
53#define OFFSET_SHIFT IMM_SHIFT
54
55#define RS_MASK (0x1f << RS_SHIFT)
56#define RT_MASK (0x1f << RT_SHIFT)
57#define RD_MASK (0x1f << RD_SHIFT)
58#define HINT_MASK (0x1f << HINT_SHIFT)
59#define BASE_MASK RS_MASK
60#define IMM_MASK (0xffff << IMM_SHIFT)
61#define OFFSET_MASK IMM_MASK
62
63#define RS_GET(inst) (((inst) & RS_MASK) >> RS_SHIFT)
64#define RD_GET(inst) (((inst) & RD_MASK) >> RD_SHIFT)
65#define IMM_GET(inst) (int16_t)(((inst) & IMM_MASK) >> IMM_SHIFT)
66#define BASE_GET(inst) RS_GET(inst)
67#define OFFSET_GET(inst) IMM_GET(inst)
68
69#define ADDU_R_SP_R0_TEMPL \
70 ((0x0 << OP_SHIFT) | (SP << RS_SHIFT) | (R0 << RT_SHIFT) | 0x21)
71#define ADDU_SP_R_R0_TEMPL \
72 ((0x0 << OP_SHIFT) | (SP << RD_SHIFT) | (R0 << RT_SHIFT) | 0x21)
73#define ADDI_SP_SP_IMM_TEMPL \
74 ((0x8 << OP_SHIFT) | (SP << RS_SHIFT) | (SP << RT_SHIFT))
75#define ADDIU_SP_SP_IMM_TEMPL \
76 ((0x9 << OP_SHIFT) | (SP << RS_SHIFT) | (SP << RT_SHIFT))
77#define JR_RA_TEMPL \
78 ((0x0 << OP_SHIFT) | (RA << RS_SHIFT) | (0x0 << HINT_SHIFT) | 0x8)
79#define SW_RA_TEMPL \
80 ((0x2b << OP_SHIFT) | (RA << RT_SHIFT))
81
82#define IS_ADDU_R_SP_R0(inst) \
83 (((inst) & ~RD_MASK) == ADDU_R_SP_R0_TEMPL)
84#define IS_ADDU_SP_R_R0(inst) \
85 (((inst) & ~RS_MASK) == ADDU_SP_R_R0_TEMPL)
86#define IS_ADDI_SP_SP_IMM(inst) \
87 (((inst) & ~IMM_MASK) == ADDI_SP_SP_IMM_TEMPL)
88#define IS_ADDIU_SP_SP_IMM(inst) \
89 (((inst) & ~IMM_MASK) == ADDIU_SP_SP_IMM_TEMPL)
90#define IS_JR_RA(inst) \
91 (((inst) & ~HINT_MASK) == JR_RA_TEMPL)
92#define IS_SW_RA(inst) \
93 (((inst) & ~(BASE_MASK | OFFSET_MASK)) == SW_RA_TEMPL)
94
95extern char ktext_start;
96extern char ktext_end;
97
[343f2b7e]98static bool bounds_check(uintptr_t pc)
99{
100 return (pc >= (uintptr_t) &ktext_start) &&
101 (pc < (uintptr_t) &ktext_end);
102}
103
[63bdde6]104static bool
105scan(stack_trace_context_t *ctx, uintptr_t *prev_fp, uintptr_t *prev_ra)
[e1f0fe9]106{
[63bdde6]107 uint32_t *inst = (void *) ctx->pc;
108 bool has_fp = false;
109 size_t frame_size;
110 unsigned int fp = SP;
111
112 do {
113 inst--;
[343f2b7e]114 if (!bounds_check((uintptr_t) inst))
115 return false;
[63bdde6]116#if 0
117 /*
118 * This is one of the situations in which the theory (ABI) does
119 * not meet the practice (GCC). GCC simply does not place the
120 * JR $ra instruction as dictated by the ABI, rendering the
121 * official stack tracing algorithm somewhat unapplicable.
122 */
123
124 if (IS_ADDU_R_SP_R0(*inst)) {
125 uint32_t *cur;
126 fp = RD_GET(*inst);
127 /*
128 * We have a candidate for frame pointer.
129 */
130
131 /* Seek to the end of this function. */
132 for (cur = inst + 1; !IS_JR_RA(*cur); cur++)
133 ;
134 /* Scan the last basic block */
135 for (cur--; !is_jump(*(cur - 1)); cur--) {
136 if (IS_ADDU_SP_R_R0(*cur) &&
137 (fp == RS_GET(*cur))) {
138 has_fp = true;
139 }
140 }
141 continue;
142 }
143
144 if (IS_JR_RA(*inst)) {
145 if (!ctx->istate)
146 return false;
147 /*
148 * No stack frame has been allocated yet.
149 * Use the values stored in istate.
150 */
151 if (prev_fp)
152 *prev_fp = ctx->istate->sp;
153 if (prev_ra)
154 *prev_ra = ctx->istate->ra - 8;
155 ctx->istate = NULL;
156 return true;
157 }
158#endif
159
160 } while ((!IS_ADDIU_SP_SP_IMM(*inst) && !IS_ADDI_SP_SP_IMM(*inst)) ||
161 (IMM_GET(*inst) >= 0));
162
163 /*
164 * We are at the instruction which allocates the space for the current
165 * stack frame.
166 */
167 frame_size = -IMM_GET(*inst);
168 if (prev_fp)
169 *prev_fp = ctx->fp + frame_size;
170
171 /*
172 * Scan the first basic block for the occurrence of
173 * SW $ra, OFFSET($base).
174 */
175 for (inst++; !is_jump(*(inst - 1)) && (uintptr_t) inst < ctx->pc;
176 inst++) {
177 if (IS_SW_RA(*inst)) {
178 unsigned int base = BASE_GET(*inst);
179 int16_t offset = OFFSET_GET(*inst);
180
181 if (base == SP || (has_fp && base == fp)) {
182 uint32_t *addr = (void *) (ctx->fp + offset);
183
184 if (offset % 4 != 0)
185 return false;
186 /* cannot store below current stack pointer */
187 if (offset < 0)
188 return false;
189 /* too big offsets are suspicious */
[0c39b96]190 if ((size_t) offset > sizeof(istate_t))
[63bdde6]191 return false;
192
193 if (prev_ra)
194 *prev_ra = *addr;
195 return true;
196 }
197 }
198 }
199
200 /*
201 * The first basic block does not save the return address or saves it
202 * after ctx->pc, which means that the correct value is in istate.
203 */
204 if (prev_ra) {
205 if (!ctx->istate)
206 return false;
207 *prev_ra = ctx->istate->ra - 8;
208 ctx->istate = NULL;
209 }
210 return true;
[e1f0fe9]211}
212
[63bdde6]213
214bool kernel_stack_trace_context_validate(stack_trace_context_t *ctx)
[e1f0fe9]215{
[63bdde6]216 return !((ctx->fp == 0) || ((ctx->fp % 8) != 0) ||
[343f2b7e]217 (ctx->pc % 4 != 0) || !bounds_check(ctx->pc));
[e1f0fe9]218}
219
[63bdde6]220bool kernel_frame_pointer_prev(stack_trace_context_t *ctx, uintptr_t *prev)
[e1f0fe9]221{
[63bdde6]222 return scan(ctx, prev, NULL);
223}
224
225bool kernel_return_address_get(stack_trace_context_t *ctx, uintptr_t *ra)
226{
227 return scan(ctx, NULL, ra);
[e1f0fe9]228}
229
[63bdde6]230bool uspace_stack_trace_context_validate(stack_trace_context_t *ctx)
[e1f0fe9]231{
232 return false;
233}
234
[63bdde6]235bool uspace_frame_pointer_prev(stack_trace_context_t *ctx, uintptr_t *prev)
[e1f0fe9]236{
237 return false;
238}
239
[63bdde6]240bool uspace_return_address_get(stack_trace_context_t *ctx, uintptr_t *ra)
[e1f0fe9]241{
242 return false;
243}
244
245/** @}
246 */
Note: See TracBrowser for help on using the repository browser.