uwx_self.c revision 121642
1/*
2Copyright (c) 2003 Hewlett-Packard Development Company, L.P.
3Permission is hereby granted, free of charge, to any person
4obtaining a copy of this software and associated documentation
5files (the "Software"), to deal in the Software without
6restriction, including without limitation the rights to use,
7copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the
9Software is furnished to do so, subject to the following
10conditions:
11
12The above copyright notice and this permission notice shall be
13included in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22OTHER DEALINGS IN THE SOFTWARE.
23*/
24
25#ifndef _KERNEL
26#include <stdlib.h>
27#include <crt0.h>
28#include <dlfcn.h>
29#include <sys/uc_access.h>
30#endif
31
32#include "uwx_env.h"
33#include "uwx_context.h"
34#include "uwx_trace.h"
35#include "uwx_self.h"
36
37#define UWX_ABI_HPUX_SIGCONTEXT 0x0101	/* abi = HP-UX, context = 1 */
38
39struct uwx_self_info {
40    ucontext_t *ucontext;
41    uint64_t bspstore;
42    uint64_t rvec[10];
43    uint64_t sendsig_start;
44    uint64_t sendsig_end;
45    alloc_cb allocate_cb;
46    free_cb free_cb;
47    int trace;
48};
49
50struct uwx_self_info *uwx_self_init_info(struct uwx_env *env)
51{
52    struct uwx_self_info *info;
53
54    if (env->allocate_cb == 0)
55	info = (struct uwx_self_info *)
56			malloc(sizeof(struct uwx_self_info));
57    else
58	info = (struct uwx_self_info *)
59			(*env->allocate_cb)(sizeof(struct uwx_self_info));
60    if (info == 0)
61	return 0;
62
63    info->ucontext = 0;
64    info->bspstore = 0;
65    info->sendsig_start = __load_info->li_sendsig_txt;
66    info->sendsig_end = __load_info->li_sendsig_txt +
67				__load_info->li_sendsig_tsz;
68    info->allocate_cb = env->allocate_cb;
69    info->free_cb = env->free_cb;
70    info->trace = env->trace;
71    return info;
72}
73
74int uwx_self_free_info(struct uwx_self_info *info)
75{
76    if (info->free_cb == 0)
77	free((void *)info);
78    else
79	(*info->free_cb)((void *)info);
80    return UWX_OK;
81}
82
83int uwx_self_init_from_sigcontext(
84    struct uwx_env *env,
85    struct uwx_self_info *info,
86    ucontext_t *ucontext)
87{
88    int status;
89    uint16_t reason;
90    uint64_t ip;
91    uint64_t sp;
92    uint64_t bsp;
93    uint64_t cfm;
94    unsigned int nat;
95    uint64_t ec;
96    int adj;
97
98    info->ucontext = ucontext;
99    status = __uc_get_reason(ucontext, &reason);
100    status = __uc_get_ip(ucontext, &ip);
101    status = __uc_get_grs(ucontext, 12, 1, &sp, &nat);
102    status = __uc_get_cfm(ucontext, &cfm);
103#ifdef NEW_UC_GET_AR
104    status = __uc_get_ar_bsp(ucontext, &bsp);
105    status = __uc_get_ar_bspstore(ucontext, &info->bspstore);
106    status = __uc_get_ar_ec(ucontext, &ec);
107#else
108    status = __uc_get_ar(ucontext, 17, &bsp);
109    status = __uc_get_ar(ucontext, 18, &info->bspstore);
110    status = __uc_get_ar(ucontext, 66, &ec);
111#endif
112    /* The returned bsp needs to be adjusted. */
113    /* For interrupt frames, where bsp was advanced by a cover */
114    /* instruction, subtract sof (size of frame). For non-interrupt */
115    /* frames, where bsp was advanced by br.call, subtract sol */
116    /* (size of locals). */
117    if (reason != 0)
118	adj = (unsigned int)cfm & 0x7f;		/* interrupt frame */
119    else
120	adj = ((unsigned int)cfm >> 7) & 0x7f;	/* non-interrupt frame */
121    bsp = uwx_add_to_bsp(bsp, -adj);
122    cfm |= ec << 52;
123    uwx_init_context(env, ip, sp, bsp, cfm);
124    return UWX_OK;
125}
126
127int uwx_self_do_context_frame(
128    struct uwx_env *env,
129    struct uwx_self_info *info)
130{
131    int abi_context;
132    int status;
133    uint64_t ucontext;
134
135    abi_context = uwx_get_abi_context_code(env);
136    if (abi_context != UWX_ABI_HPUX_SIGCONTEXT)
137	return UWX_SELF_ERR_BADABICONTEXT;
138    status = uwx_get_reg(env, UWX_REG_GR(32), (uint64_t *)&ucontext);
139    if (status != 0)
140	return status;
141    return uwx_self_init_from_sigcontext(env, info, (ucontext_t *)ucontext);
142}
143
144int uwx_self_copyin(
145    int request,
146    char *loc,
147    uint64_t rem,
148    int len,
149    intptr_t tok)
150{
151    int status;
152    int regid;
153    unsigned int nat;
154    struct uwx_self_info *info = (struct uwx_self_info *) tok;
155    unsigned long *wp;
156    uint64_t *dp;
157
158    dp = (uint64_t *) loc;
159
160    if (request == UWX_COPYIN_UINFO ||
161	    request == UWX_COPYIN_MSTACK) {
162	if (len == 4) {
163	    wp = (unsigned long *) loc;
164	    *wp = *(unsigned long *)rem;
165	    TRACE_SELF_COPYIN4(rem, len, wp)
166	}
167	else if (len == 8) {
168	    *dp = *(uint64_t *)rem;
169	    TRACE_SELF_COPYIN4(rem, len, dp)
170	}
171	else
172	    return 0;
173    }
174    else if (request == UWX_COPYIN_RSTACK && len == 8) {
175	if (info->ucontext == 0 || rem < info->bspstore) {
176	    *dp = *(uint64_t *)rem;
177	    TRACE_SELF_COPYIN4(rem, len, dp)
178	}
179	else {
180	    status = __uc_get_rsebs(info->ucontext, (uint64_t *)rem, 1, dp);
181	    if (status != 0)
182		return 0;
183	}
184    }
185    else if (request == UWX_COPYIN_REG && len == 8) {
186	if (info->ucontext == 0)
187	    return 0;
188	regid = (int)rem;
189	if (rem < UWX_REG_GR(0)) {
190	    switch (regid) {
191		case UWX_REG_PREDS:
192		    status = __uc_get_prs(info->ucontext, dp);
193		    break;
194		case UWX_REG_AR_PFS:
195		    status = __uc_get_ar(info->ucontext, 64, dp);
196		    break;
197		case UWX_REG_AR_RNAT:
198		    status = __uc_get_ar(info->ucontext, 19, dp);
199		    break;
200		case UWX_REG_AR_UNAT:
201		    status = __uc_get_ar(info->ucontext, 36, dp);
202		    break;
203		case UWX_REG_AR_FPSR:
204		    status = __uc_get_ar(info->ucontext, 40, dp);
205		    break;
206		case UWX_REG_AR_LC:
207		    status = __uc_get_ar(info->ucontext, 65, dp);
208		    break;
209		default:
210		    return 0;
211	    }
212	}
213	else if (regid >= UWX_REG_GR(1) && regid <= UWX_REG_GR(31)) {
214	    status = __uc_get_grs(info->ucontext,
215				regid - UWX_REG_GR(0), 1, dp, &nat);
216	}
217	else if (regid >= UWX_REG_BR(0) && regid <= UWX_REG_BR(7)) {
218	    status = __uc_get_brs(info->ucontext,
219				regid - UWX_REG_BR(0), 1, dp);
220	}
221	if (status != 0)
222	    return 0;
223    }
224    return len;
225}
226
227
228int uwx_self_lookupip(
229    int request,
230    uint64_t ip,
231    intptr_t tok,
232    uint64_t **resultp)
233{
234    struct uwx_self_info *info = (struct uwx_self_info *) tok;
235    UINT64 handle;
236    struct load_module_desc desc;
237    uint64_t *unwind_base;
238    uint64_t *rvec;
239    int i;
240
241    if (request == UWX_LKUP_LOOKUP) {
242	TRACE_SELF_LOOKUP(ip)
243	if (ip >= info->sendsig_start && ip < info->sendsig_end) {
244	    i = 0;
245	    rvec = info->rvec;
246	    rvec[i++] = UWX_KEY_CONTEXT;
247	    rvec[i++] = UWX_ABI_HPUX_SIGCONTEXT;
248	    rvec[i++] = 0;
249	    rvec[i++] = 0;
250	    *resultp = rvec;
251	    return UWX_LKUP_FDESC;
252	}
253	else {
254	    handle = dlmodinfo(ip, &desc, sizeof(desc), 0, 0, 0);
255	    if (handle == 0)
256		return UWX_LKUP_ERR;
257	    unwind_base = (uint64_t *) desc.unwind_base;
258	    TRACE_SELF_LOOKUP_DESC(desc.text_base, unwind_base)
259	    i = 0;
260	    rvec = info->rvec;
261	    rvec[i++] = UWX_KEY_TBASE;
262	    rvec[i++] = desc.text_base;
263	    rvec[i++] = UWX_KEY_UFLAGS;
264	    rvec[i++] = unwind_base[0];
265	    rvec[i++] = UWX_KEY_USTART;
266	    rvec[i++] = desc.text_base + unwind_base[1];
267	    rvec[i++] = UWX_KEY_UEND;
268	    rvec[i++] = desc.text_base + unwind_base[2];
269	    rvec[i++] = 0;
270	    rvec[i++] = 0;
271	    *resultp = rvec;
272	    return UWX_LKUP_UTABLE;
273	}
274    }
275    else if (request == UWX_LKUP_FREE) {
276	return 0;
277    }
278}
279