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