2
* $Xorg: getretmips.c,v 1.4 2001/02/09 02:06:19 xorgcvs Exp $
4
Copyright 1992, 1998 The Open Group
6
Permission to use, copy, modify, distribute, and sell this software and its
7
documentation for any purpose is hereby granted without fee, provided that
8
the above copyright notice appear in all copies and that both that
9
copyright notice and this permission notice appear in supporting
12
The above copyright notice and this permission notice shall be included in
13
all copies or substantial portions of the Software.
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
Except as contained in this notice, the name of The Open Group shall not be
23
used in advertising or otherwise to promote the sale, use or other dealings
24
in this Software without prior written authorization from The Open Group.
26
* Author: Keith Packard, MIT X Consortium
29
/* Return stack generation for MIPS processors
30
* This is tricky as MIPS stack frames aren't
31
* easily unrolled -- we look for pc restoration
32
* and stack adjustment instructions beyond the return
33
* address to discover the correct values
36
/* lw $31,const($sp) is : 100 011 11101 11111 const */
37
/* 1000 1111 1011 1111 */
39
#define RESTORE_RETURNVAL 0x8fbf0000
40
#define RESTORE_RETURNVAL_MASK 0xffff0000
42
/* addiu $sp, $sp, const is 001 001 11101 11101 const */
43
/* 0010 0111 1011 1101 const */
45
#define ADJUST_STACKP_C 0x27bd0000
46
#define ADJUST_STACKP_C_MASK 0xffff0000
48
/* addu $sp, $sp, $at is 000 000 11101 00001 11101 00000 100 001 */
49
/* 0000 0011 1010 0001 1110 1000 0010 0001 */
51
#define ADJUST_STACKP_V 0x03a1e821
52
#define ADJUST_STACKP_V_MASK 0xffffffff
54
/* lui $at, const is 001 111 00000 00001 const */
55
/* 0011 1100 0000 0001 const */
57
#define SET_UPPER_C 0x3c010000
58
#define SET_UPPER_C_MASK 0xffff0000
60
/* ori $at, $at, const is 001 101 00001 00001 const */
61
/* 0011 0100 0010 0001 const */
63
#define OR_LOWER_C 0x34210000
64
#define OR_LOWER_C_MASK 0xffff0000
66
/* ori $at, $zero, const is 001 101 00000 00001 const */
67
/* 0011 0100 0000 0001 const */
69
#define SET_LOWER_C 0x34010000
70
#define SET_LOWER_C_MASK 0xffff0000
73
#define RETURN 0x03e00008
75
#define CALL(f) (0x0c000000 | (((int) (f)) >> 2))
78
* This computation is expensive, so we cache the results;
79
* a simple hash function and straight-forward replacement.
84
typedef struct _returnCache {
85
unsigned long *returnAddress;
88
} ReturnCacheRec, *ReturnCachePtr;
90
static ReturnCacheRec returnCache[HASH_SIZE];
92
#define HASH(ra) ((((int) (ra)) >> 2) & (HASH_SIZE - 1))
99
getStackTrace (results, max)
100
unsigned long *results;
103
extern unsigned long *getReturnAddress (), *getStackPointer ();
105
unsigned long *ra, *ra_limit;
108
unsigned long mainCall;
109
unsigned short const_upper;
110
unsigned short const_lower;
113
Bool found_ra_offset, found_sp_adjust;
114
Bool found_const_upper, found_const_lower;
117
ra = getReturnAddress ();
118
sp = getStackPointer ();
119
mainCall = CALL(main);
121
rc = &returnCache[HASH(ra)];
122
if (rc->returnAddress != ra)
124
found_ra_offset = FALSE;
125
found_sp_adjust = FALSE;
126
found_const_upper = FALSE;
127
found_const_lower = FALSE;
130
rc->returnAddress = ra;
131
ra_limit = (unsigned long *) 0x10000000;
134
while ((!found_ra_offset || !found_sp_adjust) && ra < ra_limit)
137
/* look for the offset of the PC in the stack frame */
138
if ((inst & RESTORE_RETURNVAL_MASK) == RESTORE_RETURNVAL)
140
ra_offset = inst & ~RESTORE_RETURNVAL_MASK;
141
found_ra_offset = TRUE;
143
else if ((inst & ADJUST_STACKP_C_MASK) == ADJUST_STACKP_C)
145
sp_adjust = inst & ~ADJUST_STACKP_C_MASK;
146
found_sp_adjust = TRUE;
148
else if ((inst & ADJUST_STACKP_V_MASK) == ADJUST_STACKP_V)
151
found_sp_adjust = TRUE;
153
else if ((inst & SET_UPPER_C_MASK) == SET_UPPER_C)
155
const_upper = inst & ~SET_UPPER_C_MASK;
157
found_const_upper = TRUE;
159
else if ((inst & OR_LOWER_C_MASK) == OR_LOWER_C)
161
const_lower = inst & ~OR_LOWER_C_MASK;
162
found_const_lower = TRUE;
164
else if ((inst & SET_LOWER_C_MASK) == SET_LOWER_C)
166
const_lower = inst & ~SET_LOWER_C_MASK;
168
found_const_lower = TRUE;
170
else if (inst == RETURN)
174
if (sp_adjust == 0 && (found_const_upper || found_const_lower))
175
sp_adjust = (const_upper << 16) | const_lower;
176
rc->raOffset = ra_offset;
177
rc->spAdjust = sp_adjust;
179
/* if something went wrong, punt */
180
if (rc->spAdjust <= 0)
185
ra = (unsigned long *) sp[rc->raOffset>>2];
186
sp += rc->spAdjust >> 2;
187
*results++ = ((unsigned long) ra) - 8;
188
if (ra[-2] == mainCall)