uwx_env.c revision 129059
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#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
71struct uwx_env *uwx_init()
72{
73    int i;
74    struct uwx_env *env;
75
76    if (uwx_allocate_cb == 0)
77	env = (struct uwx_env *) malloc(sizeof(struct uwx_env));
78    else
79	env = (struct uwx_env *) (*uwx_allocate_cb)(sizeof(struct uwx_env));
80    if (env != 0) {
81	env->context.valid_regs = 0;
82	env->context.valid_frs = 0;
83	for (i = 0; i < NSPECIALREG; i++)
84	    env->context.special[i] = 0;
85	for (i = 0; i < NPRESERVEDGR; i++)
86	    env->context.gr[i] = 0;
87	for (i = 0; i < NPRESERVEDBR; i++)
88	    env->context.br[i] = 0;
89	for (i = 0; i < NPRESERVEDFR; i++) {
90	    env->context.fr[i].part0 = 0;
91	    env->context.fr[i].part1 = 0;
92	}
93	env->rstate = 0;
94	env->remapped_ip = 0;
95	env->function_offset = 0;
96	(void)uwx_init_history(env);
97	env->allocate_cb = uwx_allocate_cb;
98	env->free_cb = uwx_free_cb;
99	env->free_scoreboards = 0;
100	env->used_scoreboards = 0;
101	env->labeled_scoreboards = 0;
102	(void)uwx_init_str_pool(env);
103	env->module_name = 0;
104	env->function_name = 0;
105	env->cb_token = 0;
106	env->copyin = 0;
107	env->lookupip = 0;
108	env->remote = 0;
109	env->byte_swap = 0;
110	env->abi_context = 0;
111	env->nsbreg = NSBREG_NOFR;
112	env->nscoreboards = 0;
113	env->trace = 0;
114	TRACE_INIT
115	for (i = 0; i < NSCOREBOARDS; i++)
116	    (void) uwx_alloc_scoreboard(env);
117    }
118    return env;
119}
120
121int uwx_set_remote(struct uwx_env *env, int is_big_endian_target)
122{
123    int is_big_endian_host;
124    char *p;
125
126    if (env == 0)
127	return UWX_ERR_NOENV;
128
129    env->remote = 1;
130
131    is_big_endian_host = 1;
132    p = (char *)&is_big_endian_host;
133    *p = 0;
134    if (is_big_endian_target == is_big_endian_host)
135	env->byte_swap = 0;
136    else
137	env->byte_swap = 1;
138
139    return UWX_OK;
140}
141
142int uwx_register_callbacks(
143    struct uwx_env *env,
144    intptr_t tok,
145    copyin_cb copyin,
146    lookupip_cb lookupip)
147{
148    if (env == 0)
149	return UWX_ERR_NOENV;
150    env->cb_token = tok;
151    env->copyin = copyin;
152    env->lookupip = lookupip;
153    return UWX_OK;
154}
155
156int uwx_get_abi_context_code(struct uwx_env *env)
157{
158    if (env == 0)
159	return UWX_ERR_NOENV;
160    return env->abi_context;
161}
162
163int uwx_free(struct uwx_env *env)
164{
165    if (env != 0) {
166	uwx_free_scoreboards(env);
167	uwx_free_str_pool(env);
168	if (env->free_cb == 0)
169	    free((void *)env);
170	else
171	    (*env->free_cb)((void *)env);
172    }
173    return UWX_OK;
174}
175