uwx_env.c revision 160157
1/*
2Copyright (c) 2003-2006 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#include <stdlib.h>
26
27#include "uwx_env.h"
28#include "uwx_scoreboard.h"
29#include "uwx_str.h"
30#include "uwx_trace.h"
31
32alloc_cb uwx_allocate_cb = 0;
33free_cb uwx_free_cb = 0;
34
35int uwx_register_alloc_cb(alloc_cb alloc, free_cb free)
36{
37    uwx_allocate_cb = alloc;
38    uwx_free_cb = free;
39    return UWX_OK;
40}
41
42int uwx_init_history(struct uwx_env *env)
43{
44    int i;
45
46    if (env == 0)
47	return UWX_ERR_NOENV;
48
49    for (i = 0; i < NSPECIALREG; i++)
50	env->history.special[i] = UWX_DISP_REG(i);;
51    for (i = 0; i < NPRESERVEDGR; i++)
52	env->history.gr[i] = UWX_DISP_REG(UWX_REG_GR(4+i));
53    for (i = 0; i < NPRESERVEDBR; i++)
54	env->history.br[i] = UWX_DISP_REG(UWX_REG_BR(1+i));
55    for (i = 0; i < 4; i++)
56	env->history.fr[i] = UWX_DISP_REG(UWX_REG_FR(2+i));
57    for ( ; i < NPRESERVEDFR; i++)
58	env->history.fr[i] = UWX_DISP_REG(UWX_REG_FR(12+i));
59
60    return UWX_OK;
61}
62
63int uwx_init_env(struct uwx_env *env, size_t total_size)
64{
65    int i;
66    struct uwx_str_pool *str_pool;
67    struct uwx_scoreboard *scoreboards;
68
69    str_pool = (struct uwx_str_pool *)(env + 1);
70    scoreboards = (struct uwx_scoreboard *)(str_pool + 1);
71
72    if (sizeof(struct uwx_env) + sizeof(struct uwx_str_pool) > total_size)
73	return UWX_ERR_NOMEM;
74    total_size -= sizeof(struct uwx_env) + sizeof(struct uwx_str_pool);
75
76    env->context.valid_regs = 0;
77    env->context.valid_frs = 0;
78    for (i = 0; i < NSPECIALREG; i++)
79	env->context.special[i] = 0;
80    for (i = 0; i < NPRESERVEDGR; i++)
81	env->context.gr[i] = 0;
82    for (i = 0; i < NPRESERVEDBR; i++)
83	env->context.br[i] = 0;
84    for (i = 0; i < NPRESERVEDFR; i++) {
85	env->context.fr[i].part0 = 0;
86	env->context.fr[i].part1 = 0;
87    }
88    env->rstate = 0;
89    env->remapped_ip = 0;
90    env->function_offset = 0;
91    env->ptr_size = DWORDSZ;
92    env->uinfo_hdr = 0;
93    env->uinfo_end = 0;
94    env->code_start = 0;
95    env->text_base = 0;
96    (void)uwx_init_history(env);
97    if (uwx_allocate_cb != NULL)
98	env->allocate_cb = uwx_allocate_cb;
99    else
100	env->allocate_cb = malloc;
101    if (uwx_free_cb != NULL)
102	env->free_cb = uwx_free_cb;
103    else
104	env->free_cb = free;
105    env->free_scoreboards = 0;
106    env->used_scoreboards = 0;
107    env->labeled_scoreboards = 0;
108    (void)uwx_init_str_pool(env, str_pool);
109    env->module_name = 0;
110    env->function_name = 0;
111    env->cb_token = 0;
112    env->copyin = 0;
113    env->lookupip = 0;
114    env->remote = 0;
115    env->byte_swap = 0;
116    env->abi_context = 0;
117    env->nsbreg = NSBREG;
118    env->nscoreboards = 0;
119    env->on_heap = 0;
120    env->trace = 0;
121    TRACE_INIT
122    for (i = 0; total_size >= sizeof(struct uwx_scoreboard); i++) {
123	(void) uwx_prealloc_scoreboard(env, &scoreboards[i]);
124	total_size -= sizeof(struct uwx_scoreboard);
125    }
126    return UWX_OK;
127}
128
129int uwx_set_nofr(struct uwx_env *env)
130{
131    if (env == 0)
132	return UWX_ERR_NOENV;
133
134    env->nsbreg = NSBREG_NOFR;
135    return UWX_OK;
136}
137
138struct uwx_env *uwx_init()
139{
140    struct uwx_env *env;
141    size_t total_size;
142
143    total_size = sizeof(struct uwx_env) +
144		    sizeof(struct uwx_str_pool) +
145			NSCOREBOARDS * sizeof(struct uwx_scoreboard);
146
147    if (uwx_allocate_cb == 0)
148	env = (struct uwx_env *) malloc(total_size);
149    else
150	env = (struct uwx_env *) (*uwx_allocate_cb)(total_size);
151    if (env != 0) {
152	uwx_init_env(env, total_size);
153	env->on_heap = 1;
154    }
155    return env;
156}
157
158int uwx_set_remote(struct uwx_env *env, int is_big_endian_target)
159{
160    int is_big_endian_host;
161    char *p;
162
163    if (env == 0)
164	return UWX_ERR_NOENV;
165
166    env->remote = 1;
167
168    is_big_endian_host = 1;
169    p = (char *)&is_big_endian_host;
170    *p = 0;
171    if (is_big_endian_target == is_big_endian_host)
172	env->byte_swap = 0;
173    else
174	env->byte_swap = 1;
175
176    return UWX_OK;
177}
178
179int uwx_register_callbacks(
180    struct uwx_env *env,
181    intptr_t tok,
182    copyin_cb copyin,
183    lookupip_cb lookupip)
184{
185    if (env == 0)
186	return UWX_ERR_NOENV;
187    env->cb_token = tok;
188    env->copyin = copyin;
189    env->lookupip = lookupip;
190    return UWX_OK;
191}
192
193int uwx_get_abi_context_code(struct uwx_env *env)
194{
195    if (env == 0)
196	return UWX_ERR_NOENV;
197    return env->abi_context;
198}
199
200int uwx_free(struct uwx_env *env)
201{
202    if (env != 0) {
203	uwx_free_scoreboards(env);
204	uwx_free_str_pool(env);
205	if (env->on_heap) {
206	    if (env->free_cb == 0)
207		free((void *)env);
208	    else
209		(*env->free_cb)((void *)env);
210	}
211    }
212    return UWX_OK;
213}
214