2 Copyright (c) 2003-2006 Hewlett-Packard Development Company, L.P.
3 Permission is hereby granted, free of charge, to any person
4 obtaining a copy of this software and associated documentation
5 files (the "Software"), to deal in the Software without
6 restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
26 #include "uwx_utable.h"
28 #include "uwx_trace.h"
33 * This file contains the routines for searching an unwind table.
34 * The main entry point, uwx_search_utable(), gets the
35 * necessary information from the lookup ip callback's result
36 * vector, determines whether the table is 32-bit or 64-bit,
37 * then invokes the binary search routine for that format.
41 /* Forward declarations */
43 int uwx_search_utable32(
47 uint32_t unwind_start,
49 struct uwx_utable_entry *uentry);
51 int uwx_search_utable64(
55 uint64_t unwind_start,
57 struct uwx_utable_entry *uentry);
60 /* uwx_search_utable: Searches an unwind table for IP in current context */
62 int uwx_search_utable(
66 struct uwx_utable_entry *uentry)
69 uint64_t unwind_flags;
70 uint64_t unwind_start;
75 /* Get unwind table information from the result vector. */
76 /* Make sure all three required values are given. */
84 switch ((int)*uvec++) {
87 env->text_base = text_base = *uvec++;
90 unwind_flags = *uvec++;
94 unwind_start = *uvec++;
101 uwx_set_reg(env, UWX_REG_GP, *uvec++);
104 return UWX_ERR_BADKEY;
108 return UWX_ERR_BADKEY;
110 /* Copy the unwind flags into the unwind entry. */
111 /* (uwx_decode_uinfo needs to know whether it's 32-bit or 64-bit.) */
113 uentry->unwind_flags = unwind_flags;
115 /* Call the appropriate binary search routine. */
117 if (unwind_flags & UNWIND_TBL_32BIT)
118 status = uwx_search_utable32(env,
120 (uint32_t) text_base,
121 (uint32_t) unwind_start,
122 (uint32_t) unwind_end,
125 status = uwx_search_utable64(env,
126 ip, text_base, unwind_start, unwind_end, uentry);
132 /* uwx_search_utable32: Binary search of 32-bit unwind table */
134 #define COPYIN_UINFO_4(dest, src) \
136 (*env->copyin)(UWX_COPYIN_UINFO, (dest), (src), \
137 WORDSZ, env->cb_token) : \
138 (*(uint32_t *)(dest) = *(uint32_t *)(src), WORDSZ) )
140 #define SWIZZLE(x) (((uint64_t)((x) & 0xc0000000) << 31) | (x))
142 int uwx_search_utable32(
146 uint32_t unwind_start,
148 struct uwx_utable_entry *uentry)
156 uint32_t unwind_info;
158 /* Since the unwind table uses segment-relative offsets, convert */
159 /* the IP in the current context to a segment-relative offset. */
165 /* Standard binary search. */
166 /* Might modify this to do interpolation in the future. */
169 ub = (unwind_end - unwind_start) / (3 * WORDSZ);
173 len = COPYIN_UINFO_4((char *)&code_start,
174 (uintptr_t)(unwind_start+mid*3*WORDSZ));
175 len += COPYIN_UINFO_4((char *)&code_end,
176 (uintptr_t)(unwind_start+mid*3*WORDSZ+WORDSZ));
177 if (len != 2 * WORDSZ)
178 return UWX_ERR_COPYIN_UTBL;
179 if (env->byte_swap) {
180 uwx_swap4(&code_start);
181 uwx_swap4(&code_end);
183 TRACE_T_BINSEARCH32(lb, ub, mid, code_start, code_end)
186 else if (ip < code_start)
192 return UWX_ERR_NOUENTRY;
193 len = COPYIN_UINFO_4((char *)&unwind_info,
194 (uintptr_t)(unwind_start+mid*3*WORDSZ+2*WORDSZ));
196 return UWX_ERR_COPYIN_UTBL;
198 uwx_swap4(&unwind_info);
199 uentry->ptr_size = WORDSZ;
200 uentry->code_start = SWIZZLE(text_base + code_start);
201 uentry->code_end = SWIZZLE(text_base + code_end);
202 uentry->unwind_info = SWIZZLE(text_base + unwind_info);
207 /* uwx_search_utable64: Binary search of 64-bit unwind table */
209 #define COPYIN_UINFO_8(dest, src) \
211 (*env->copyin)(UWX_COPYIN_UINFO, (dest), (src), \
212 DWORDSZ, env->cb_token) : \
213 (*(uint64_t *)(intptr_t)(dest) = *(uint64_t *)(intptr_t)(src), DWORDSZ) )
215 int uwx_search_utable64(
219 uint64_t unwind_start,
221 struct uwx_utable_entry *uentry)
229 uint64_t unwind_info;
231 /* Since the unwind table uses segment-relative offsets, convert */
232 /* the IP in the current context to a segment-relative offset. */
236 /* Standard binary search. */
237 /* Might modify this to do interpolation in the future. */
240 ub = (unwind_end - unwind_start) / (3 * DWORDSZ);
244 len = COPYIN_UINFO_8((char *)&code_start, unwind_start+mid*3*DWORDSZ);
245 len += COPYIN_UINFO_8((char *)&code_end,
246 unwind_start+mid*3*DWORDSZ+DWORDSZ);
247 if (len != 2 * DWORDSZ)
248 return UWX_ERR_COPYIN_UTBL;
249 if (env->byte_swap) {
250 uwx_swap8(&code_start);
251 uwx_swap8(&code_end);
255 else if (ip < code_start)
261 return UWX_ERR_NOUENTRY;
262 len = COPYIN_UINFO_8((char *)&unwind_info,
263 unwind_start+mid*3*DWORDSZ+2*DWORDSZ);
265 return UWX_ERR_COPYIN_UTBL;
267 uwx_swap8(&unwind_info);
268 uentry->ptr_size = DWORDSZ;
269 uentry->code_start = text_base + code_start;
270 uentry->code_end = text_base + code_end;
271 uentry->unwind_info = text_base + unwind_info;