uwx.h 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 <inttypes.h>
28#else
29#include <sys/param.h>
30#include <sys/systm.h>
31#endif
32
33/* Unwind environment structure (opaque) */
34struct uwx_env;
35
36/* Allocate and free callbacks */
37typedef void *(*alloc_cb)(size_t size);
38typedef void (*free_cb)(void *ptr);
39extern int uwx_register_alloc_cb(alloc_cb alloc, free_cb free);
40
41/* Allocate and initialize an unwind environment */
42extern struct uwx_env *uwx_init(void);
43
44/* Free an unwind environment */
45extern int uwx_free(struct uwx_env *env);
46
47/* Put unwind express into cross-process mode */
48extern int uwx_set_remote(struct uwx_env *env, int is_big_endian_target);
49
50/* Copy-in callback */
51typedef int (*copyin_cb)(
52    int request,		/* request code (see below) */
53    char *loc,			/* local (destination) address */
54    uint64_t rem,		/* remote (source) address */
55    int len,			/* number of bytes to copy */
56    intptr_t tok);		/* callback token */
57
58/* Lookup IP callback */
59typedef int (*lookupip_cb)(
60    int request,		/* request code (see below) */
61    uint64_t ip,		/* IP of current frame */
62    intptr_t tok,		/* callback token */
63    uint64_t **vecp);		/* parameter vector (in/out) */
64
65/* Register copy-in and lookup IP callbacks */
66extern int uwx_register_callbacks(
67    struct uwx_env *env,	/* unwind environment */
68    intptr_t tok,		/* callback token */
69    copyin_cb copyin,		/* copy-in callback */
70    lookupip_cb lookupip);	/* lookup IP callback */
71
72/* Initialize a context with the basic info needed to start an unwind */
73extern int uwx_init_context(
74    struct uwx_env *env,	/* unwind environment */
75    uint64_t ip,		/* IP (instruction pointer) */
76    uint64_t sp,		/* SP (stack pointer) */
77    uint64_t bsp,		/* BSP (backing store pointer) */
78    uint64_t cfm);		/* CFM (current frame marker) */
79
80/* Set the value of a specific register in the current context (non fp) */
81extern int uwx_set_reg(
82    struct uwx_env *env,	/* unwind environment */
83    int regid,			/* register id (see below) */
84    uint64_t val);		/* register value */
85
86/* Set the value of a floating-point register in the current context */
87extern int uwx_set_fr(
88    struct uwx_env *env,	/* unwind environment */
89    int regid,			/* register id (see below) */
90    uint64_t *val);		/* register value (ptr to 16 bytes) */
91				/*   (memory spill format) */
92
93/* Initialize the unwind history */
94extern int uwx_init_history(struct uwx_env *env);
95
96/* Step one frame */
97extern int uwx_step(struct uwx_env *env);
98
99/* Get symbol information, if available, for current frame */
100extern int uwx_get_sym_info(
101    struct uwx_env *env,	/* unwind environment */
102    char **modp,		/* load module name (out)  */
103    char **symp,		/* function name (out)  */
104    uint64_t *offsetp);		/* offset from start of function (out)  */
105
106/* Get the value of a register from the current context */
107extern int uwx_get_reg(
108    struct uwx_env *env,	/* unwind environment */
109    int regid,			/* register id (see below) */
110    uint64_t *valp);		/* register value (out) */
111
112/* Get the NaT bit of a GR from the current context */
113extern int uwx_get_nat(
114    struct uwx_env *env,	/* unwind environment */
115    int regid,			/* register id (see below) */
116    int *natp);			/* NaT value (out: 0 or 1) */
117
118/* Get the spill location for a register in the current context */
119extern int uwx_get_spill_loc(
120    struct uwx_env *env,	/* unwind environment */
121    int regid,			/* register id (see below) */
122    uint64_t *dispp);		/* disposition code (see below) (out) */
123
124/* Get the ABI context code (if uwx_step returned UWX_ABI_FRAME) */
125extern int uwx_get_abi_context_code(struct uwx_env *env);
126
127/* Return status codes for uwx_ APIs */
128#define UWX_OK			0
129#define UWX_BOTTOM		1	/* Hit bottom of stack */
130#define UWX_ABI_FRAME		2	/* Hit ABI-dependent frame */
131#define UWX_ERR_NOENV		(-1)	/* No uwx_env allocated */
132#define UWX_ERR_IPNOTFOUND	(-2)	/* Lookup IP c/b returned NOTFOUND */
133#define UWX_ERR_LOOKUPERR	(-3)	/* Lookup IP c/b returned ERR */
134#define UWX_ERR_BADKEY		(-4)	/* Bad result vector key */
135#define UWX_ERR_COPYIN_UTBL	(-5)	/* Error reading unwind table */
136#define UWX_ERR_COPYIN_UINFO	(-6)	/* Error reading unwind info */
137#define UWX_ERR_COPYIN_MSTK	(-7)	/* Error reading memory stack */
138#define UWX_ERR_COPYIN_RSTK	(-8)	/* Error reading register stack */
139#define UWX_ERR_COPYIN_REG	(-9)	/* Error reading context register */
140#define UWX_ERR_NOUENTRY	(-10)	/* No unwind table entry for ip */
141#define UWX_ERR_NOUDESC		(-11)	/* No unwind descriptor covers ip */
142#define UWX_ERR_BADUDESC	(-12)	/* Bad unwind descriptor */
143#define UWX_ERR_NOMEM		(-13)	/* Out of memory */
144#define UWX_ERR_PROLOG_UF	(-14)	/* Prologue underflow */
145#define UWX_ERR_UNDEFLABEL	(-15)	/* Undefined label in copy_state */
146#define UWX_ERR_BADREGID	(-16)	/* Bad register identifier */
147#define UWX_ERR_CANTUNWIND	(-17)	/* Can't unwind */
148#define UWX_ERR_NOCALLBACKS	(-18)	/* No callbacks registered */
149#define UWX_ERR_NOCONTEXT	(-19)	/* Context not initialized */
150
151/* Request codes for copyin callback */
152#define UWX_COPYIN_UINFO	1	/* Reading unwind info */
153#define UWX_COPYIN_MSTACK	2	/* Reading memory stack */
154#define UWX_COPYIN_RSTACK	3	/* Reading RSE backing store */
155#define UWX_COPYIN_REG		4	/* Reading initial register state */
156
157/* Request codes for lookup IP callback */
158#define UWX_LKUP_LOOKUP		1	/* Lookup IP */
159#define UWX_LKUP_FREE		2	/* Free result vector */
160#define UWX_LKUP_SYMBOLS	3	/* Lookup symbolic information */
161
162/* Return status codes for lookup IP callback */
163#define UWX_LKUP_NOTFOUND	0	/* IP not found */
164#define UWX_LKUP_ERR		1	/* Other error */
165#define UWX_LKUP_UTABLE		2	/* Returned ref to unwind table */
166#define UWX_LKUP_FDESC		3	/* Returned frame description */
167#define UWX_LKUP_SYMINFO	4	/* Returned symbolic information */
168
169/* The lookup IP callback receives a parameter vector, and returns */
170/* one on success. This vector is a series of key/value pairs; each */
171/* even-numbered slot is a key, and each odd-numbered slot is a */
172/* corresponding value. The vector is terminated by a pair whose */
173/* key is 0. */
174#define UWX_KEY_END		0	/* End of vector */
175
176/* Keys passed to lookup IP callback */
177#define UWX_KEY_PREDS		1	/* Predicate registers */
178
179/* Keys returned with UWX_LKUP_UTABLE */
180/* These key/value pairs describe the unwind table corresponding */
181/* to the load module in which the current IP resides. */
182#define UWX_KEY_TBASE		1	/* Base address of text seg */
183#define UWX_KEY_UFLAGS		2	/* Unwind flags */
184#define UWX_KEY_USTART		3	/* Base of unwind tbl */
185#define UWX_KEY_UEND		4	/* End of unwind tbl */
186
187/* Keys returned with UWX_LKUP_FDESC */
188/* These key/value pairs describe the state of the frame at the */
189/* given IP. They are typically used for dynamically-generated code. */
190#define UWX_KEY_FSIZE		1			/* Frame size */
191#define UWX_KEY_SPILL(reg_id)	(2 | ((reg_id) << 4))	/* Reg spilled */
192#define UWX_KEY_CONTEXT		3 			/* ABI-dep. context */
193
194/* Keys returned with UWX_LKUP_FDESC or UWX_LKUP_SYMINFO */
195#define UWX_KEY_MODULE		5	/* Name of load module */
196#define UWX_KEY_FUNC		6	/* Name of function */
197#define UWX_KEY_FUNCSTART	7	/* Address of start of function */
198
199/* Register identifiers */
200/* For use in UWX_LKUP_FDESC result vectors and context access APIs. */
201/* "no spill info": These regs aren't spilled directly, so */
202/*    result vectors must not describe these registers. */
203/*    The result vector must describe the related register or */
204/*    pseudo register instead (ip:rp, sp:psp, bsp/cfm:pfs). */
205/* "pseudo register": Not a machine register, but treated as */
206/*    one for unwind purposes. */
207#define UWX_REG_IP		0	/* ip (no spill info) */
208#define UWX_REG_SP		1	/* sp (no spill info) */
209#define UWX_REG_BSP		2	/* ar.bsp (no spill info) */
210#define UWX_REG_CFM		3	/* cfm (no spill info) */
211#define UWX_REG_RP		4	/* rp (pseudo-register) */
212#define UWX_REG_PSP		5	/* psp (pseudo-register) */
213#define UWX_REG_PFS		6	/* pfs (pseudo-register) */
214#define UWX_REG_PREDS		7	/* p0 - p63 */
215#define UWX_REG_PRIUNAT		8	/* primary unat (pseudo-register) */
216#define UWX_REG_AR_BSPSTORE	9	/* ar.bspstore */
217#define UWX_REG_AR_RNAT		10	/* ar.rnat */
218#define UWX_REG_AR_UNAT		11	/* ar.unat */
219#define UWX_REG_AR_FPSR		12	/* ar.fpsr */
220#define UWX_REG_AR_LC		13	/* ar.lc */
221#define UWX_REG_AR_PFS		14	/* ar.pfs */
222#define UWX_REG_GR(gr)		(0x100 | (gr))
223#define UWX_REG_FR(fr)		(0x200 | (fr))
224#define UWX_REG_BR(br)		(0x300 | (br))
225
226/* for backwards compatibility with previous releases... */
227#define UWX_REG_BSPSTORE	UWX_REG_AR_BSPSTORE
228#define UWX_REG_RNAT		UWX_REG_AR_RNAT
229#define UWX_REG_UNAT		UWX_REG_AR_UNAT
230#define UWX_REG_FPSR		UWX_REG_AR_FPSR
231#define UWX_REG_LC		UWX_REG_AR_LC
232
233/* Values corresponding to UWX_KEY_SPILL keys indicate the disposition */
234/* of the spilled register -- either in the memory stack or in another */
235/* register. The PSP register may also have a disposition of "SPPLUS", */
236/* indicating that its value is SP plus a fixed constant. */
237#define UWX_DISP_NONE		0		/* Not spilled */
238#define UWX_DISP_SPPLUS(k)	(1 | (k))	/* PSP = SP+constant */
239#define UWX_DISP_SPREL(disp)	(2 | (disp))	/* Spilled at [SP+disp] */
240#define UWX_DISP_PSPREL(disp)	(3 | (disp))	/* Spilled at [PSP+16-disp] */
241#define UWX_DISP_REG(reg)	(4 | ((reg) << 4)) /* Saved to another reg. */
242
243/* The uwx_get_spill_loc() routine returns a spill location for a */
244/* given register in the current context. It will return a disposition */
245/* code of UWX_DISP_NONE, UWX_DISP_REG(reg), or one of the following */
246/* to indicate that the spilled value can be found in the memory */
247/* stack or the register stack backing store. */
248#define UWX_DISP_MSTK(addr)	(5 | (addr))	/* Spilled in mem. stack */
249#define UWX_DISP_RSTK(addr)	(6 | (addr))	/* Spilled in reg. stack */
250
251/* Extract the disposition code, offset, address, or register id */
252/* from a disposition returned from uwx_get_spill_loc(). */
253/* Compare the extracted disp code against UWX_DISP_REG(0), etc. */
254#define UWX_GET_DISP_CODE(disp)		((int)(disp) & 0x07)
255#define UWX_GET_DISP_OFFSET(disp)	((disp) & ~(uint64_t)0x07)
256#define UWX_GET_DISP_ADDR(disp)		((disp) & ~(uint64_t)0x07)
257#define UWX_GET_DISP_REGID(disp)	((int)(disp) >> 4)
258