summaryrefslogtreecommitdiffstats
path: root/unwind.c
blob: e08b733993b51617518bd56812efbaedd4237971 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* 
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) Hewlett-Packard (Paul Bame) paul_bame@hp.com
 */
#include <stdio.h>

#define GET_REG(reg)	 ({			\
	unsigned long r;			\
	__asm__ __volatile__(			\
		"copy " #reg ",%0" : "=r" (r)	\
	);					\
	r;					\
})

#define GET_SP() GET_REG(30)
#define GET_RP() GET_REG(2)

#define GET_PC()	 ({			\
	unsigned long r;			\
	__asm__ __volatile__(			\
		"ldil L%%(.+8), %0\n\t"		\
		"ldo  R%%(.+4)(%0), %0" : "=r" (r)	\
	);					\
	r;					\
})

/* These two would be MUCH easier with a quick asm() instruction! */
/* extract some bits, least-sig bit is numbered 0, unsigned */
#define GETBITSLSB0(i, start, length)	\
	(((i) >> (start)) & ((1 << (length)) - 1))

/* extract some bits, least-sig bit is numbered 0, signed */
#define GETBITSLSB0S(i, start, length)	\
	(((signed)((i) << (32 - (start + length)))) >> (32 - length))

/* bl   xxx,%r2 */
#define BL2_BITS	0xe8400000
#define BL2_MASK	0xffe0e000
#define IS_BL2(i)	(((i) & BL2_MASK) == BL2_BITS)

/********** Not finished ************/
#define BLR_BITS	0xe8004
#define BLR_MASK	0xfc00e

union bl
{
    unsigned instr;
    struct
    {
	unsigned opcode:6;
	unsigned t:5;
	unsigned w1:5;
	unsigned dummy:3;
	unsigned w2_0_9:10;
	unsigned w2_10:1;
	unsigned n:1;
	unsigned w:1;
    } bits;
};

static int
assemble_17(unsigned instr)
{
    union bl bl;
    int x;
    int sx;

    bl.instr = instr;

    x = bl.bits.w2_0_9;
    x |= bl.bits.w2_10 << 10;
    x |= bl.bits.w1 << 11;
    x |= bl.bits.w << (5 + 11);

    sx = x << (32 - 17);
    sx >>= (32 - 17 - 2);

#if 0
    printf("op %x t %x w1 %x w20 %x n %x w %x x %x 4x %x sx %d\n",
	bl.bits.opcode,
	bl.bits.t,
	bl.bits.w1,
	bl.bits.w2_0_9,
	bl.bits.n,
	bl.bits.w, x, x << 2, sx);
#endif
    return sx;
}

static unsigned *
find_rp(unsigned *sp, unsigned *dot, unsigned **btargetp)
{
    unsigned *lim = sp - 1024;
    extern unsigned _etext, __text_start;

    while(sp > lim)
    {
	unsigned *maybe_rp = (unsigned *)*sp;

	if (0) printf("%p: %p\n", sp, maybe_rp);

	if (((unsigned)maybe_rp & 0x3) == 0x3
		&& maybe_rp < &_etext
		&& maybe_rp >= &__text_start)
	{
	    unsigned instr;
	    unsigned *btarget;

	    maybe_rp = (unsigned *)((unsigned)maybe_rp & ~0x3);

	    /* check if instr - 2 is a bl  ..., %r2 */
	    instr = maybe_rp[-2];
	    if (IS_BL2(instr))
	    {
		btarget = (unsigned *)((unsigned)maybe_rp + assemble_17(instr));
		printf("%p: %p: bl %p, %%r2\n",
		    sp,
		    maybe_rp - 2,
		    btarget
		    );
		if (btarget > dot)
		{
		    printf("unlikely bl, .+0x%x\n",
			(unsigned)btarget - (unsigned)maybe_rp);
		}
		else
		{
		    *btargetp = btarget;
		    return sp;
		}
	    }
	}
	sp--;
    }

    return 0;
}

void unwind()
{
    unsigned *rp = (unsigned *)(GET_RP() & ~0x3);
    unsigned *sp = (unsigned *)GET_SP();
    /* lie about these data types... _end could be 1-byte aligned */
    extern unsigned _end;
    extern unsigned _etext;
    extern unsigned __text_start;
    unsigned *lastrpsp = sp;
    extern unsigned dotlabel;
    unsigned *dot;
    unsigned *our_entry;
    unsigned *lim;
    unsigned *btarget;

    asm("\ndotlabel:");

    dot = &dotlabel;

    printf("Initial rp: %p\tsp: %p\tetext: %p\t.: %p\n", rp, sp, &_etext, dot);

    /* find where my RP is stored, hence my caller's stack frame */
    for (   ; *sp != rp; sp--)
    {
    }
    printf("Found my RP on stack at %p\n", sp);

    /* stack frames are 16 words minimum, rp is stored in word -5 relative */
    /* to stack pointer on entry to a routine, so we can calculate the sp */
    /* on entry to the current routine */
    sp += 5;
    printf("My entry SP was %p\n", sp);

    /* by disassembling the target of the branch which brought us here, */
    /* we can learn our entry point address */
    our_entry = (unsigned *)(assemble_17(rp[-2]) + (unsigned)rp);
    printf("Entry point to this routine is %p\n", our_entry);

    /* can't be sure about args, but some may be on stack... */
    printf("%p: %p(%08x, %08x, %08x, %08x)\n",
					rp - 2,
					our_entry,
					sp[-9],
					sp[-10],
					sp[-11],
					sp[-12]);

    /* become the previous procedure */
    dot = rp;

    /* this is the minimum stack frame size -- skip over it */
    sp -= 16;

    sp = find_rp(sp, dot, &btarget);
    if (sp != 0)
    {
	rp = (unsigned *)((unsigned)*sp & ~0x3);
	printf("Found my RP (%p) on stack at %p\n", rp, sp);
	/* stack frames are 16 words minimum, rp is stored in word -5 relative */
	/* to stack pointer on entry to a routine, so we can calculate the sp */
	/* on entry to the current routine */
	sp += 5;
	printf("My entry SP was %p\n", sp);

	/* by disassembling the target of the branch which brought us here, */
	/* we can learn our entry point address */
	if (0) our_entry = (unsigned *)(assemble_17(rp[-2]) + (unsigned)rp);
	printf("Entry point to this routine is %p\n", btarget);

	/* can't be sure about args, but some may be on stack... */
	printf("%p: %p()\n", rp - 2, our_entry);

	/* become the previous procedure */
	dot = rp;
    }
}
#if 0
void ounwind()
{
    unsigned *sp = (unsigned *)GET_SP();
    unsigned *rp = (unsigned *)GET_RP();
    /* lie about these data types... _end could be 1-byte aligned */
    extern unsigned _end;
    extern unsigned _etext;
    extern unsigned __text_start;
    unsigned *lim = sp - 1024*4;
    unsigned *lastrpsp = sp;
    extern unsigned dotlabel;
    unsigned *dot;

    asm("\ndotlabel:");

    dot = &dotlabel;

    printf("Initial rp: %p\tsp: %p\tetext: %p\t.: %p\n", rp, sp, &_etext, dot);

    /* walk backwards in stack looking for likely RPs */
    while(sp > lim)
    {
	unsigned *maybe_rp = (unsigned *)*sp;

	if (0) printf("%p: %p\n", sp, maybe_rp);

	if (((unsigned)maybe_rp & 0x3) == 0x3
		&& maybe_rp < &_etext
		&& maybe_rp >= &__text_start)
	{
	    unsigned instr;
	    unsigned *btarget;

	    maybe_rp = (unsigned *)((unsigned)maybe_rp & ~0x3);

	    /* check if instr - 2 is a bl  ..., %r2 */
	    instr = maybe_rp[-2];
	    if (IS_BL2(instr))
	    {
		btarget = (unsigned)maybe_rp + assemble_17(instr);
		printf("%p: %p: bl %p, %%r2, stack delta 0x%x\n",
		    sp,
		    maybe_rp - 2,
		    btarget,
		    (unsigned)lastrpsp - (unsigned)sp
		    );
		if (btarget > dot)
		{
		    printf("unlikely bl, .+0x%x\n",
			(unsigned)btarget - (unsigned)maybe_rp);
		}
		lastrpsp = sp;
	    }
	}
	sp--;
    }
    exit(77);
}
#endif