summaryrefslogtreecommitdiffstats
path: root/sys/contrib/ia64/libuwx/src/uwx_env.c
blob: 252e3972cfc8e4df63d6315971947f4290df0930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Copyright (c) 2003-2006 Hewlett-Packard Development Company, L.P.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef _KERNEL
#include <stdlib.h>
#endif

#include "uwx_env.h"
#include "uwx_scoreboard.h"
#include "uwx_str.h"
#include "uwx_trace.h"

#ifdef _KERNEL
static struct uwx_env uwx_env;
#define	free(p)		/* nullified */
#define	malloc(sz)	((sz == sizeof(uwx_env)) ? &uwx_env : NULL)
#endif

alloc_cb uwx_allocate_cb = 0;
free_cb uwx_free_cb = 0;

int uwx_register_alloc_cb(alloc_cb alloc, free_cb free)
{
    uwx_allocate_cb = alloc;
    uwx_free_cb = free;
    return UWX_OK;
}

int uwx_init_history(struct uwx_env *env)
{
    int i;

    if (env == 0)
	return UWX_ERR_NOENV;

    for (i = 0; i < NSPECIALREG; i++)
	env->history.special[i] = UWX_DISP_REG(i);;
    for (i = 0; i < NPRESERVEDGR; i++)
	env->history.gr[i] = UWX_DISP_REG(UWX_REG_GR(4+i));
    for (i = 0; i < NPRESERVEDBR; i++)
	env->history.br[i] = UWX_DISP_REG(UWX_REG_BR(1+i));
    for (i = 0; i < 4; i++)
	env->history.fr[i] = UWX_DISP_REG(UWX_REG_FR(2+i));
    for ( ; i < NPRESERVEDFR; i++)
	env->history.fr[i] = UWX_DISP_REG(UWX_REG_FR(12+i));

    return UWX_OK;
}

int uwx_init_env(struct uwx_env *env, size_t total_size)
{
    int i;
    struct uwx_str_pool *str_pool;
    struct uwx_scoreboard *scoreboards;

    str_pool = (struct uwx_str_pool *)(env + 1);
    scoreboards = (struct uwx_scoreboard *)(str_pool + 1);

    if (sizeof(struct uwx_env) + sizeof(struct uwx_str_pool) > total_size)
	return UWX_ERR_NOMEM;
    total_size -= sizeof(struct uwx_env) + sizeof(struct uwx_str_pool);

    env->context.valid_regs = 0;
    env->context.valid_frs = 0;
    for (i = 0; i < NSPECIALREG; i++)
	env->context.special[i] = 0;
    for (i = 0; i < NPRESERVEDGR; i++)
	env->context.gr[i] = 0;
    for (i = 0; i < NPRESERVEDBR; i++)
	env->context.br[i] = 0;
    for (i = 0; i < NPRESERVEDFR; i++) {
	env->context.fr[i].part0 = 0;
	env->context.fr[i].part1 = 0;
    }
    env->rstate = 0;
    env->remapped_ip = 0;
    env->function_offset = 0;
    env->ptr_size = DWORDSZ;
    env->uinfo_hdr = 0;
    env->uinfo_end = 0;
    env->code_start = 0;
    env->text_base = 0;
    (void)uwx_init_history(env);
    if (uwx_allocate_cb != NULL)
	env->allocate_cb = uwx_allocate_cb;
    else
	env->allocate_cb = NULL;
    if (uwx_free_cb != NULL)
	env->free_cb = uwx_free_cb;
    else
	env->free_cb = NULL;
    env->free_scoreboards = 0;
    env->used_scoreboards = 0;
    env->labeled_scoreboards = 0;
    (void)uwx_init_str_pool(env, str_pool);
    env->module_name = 0;
    env->function_name = 0;
    env->cb_token = 0;
    env->copyin = 0;
    env->lookupip = 0;
    env->remote = 0;
    env->byte_swap = 0;
    env->abi_context = 0;
    env->nsbreg = NSBREG;
    env->nscoreboards = 0;
    env->on_heap = 0;
    env->trace = 0;
    TRACE_INIT
    for (i = 0; total_size >= sizeof(struct uwx_scoreboard); i++) {
	(void) uwx_prealloc_scoreboard(env, &scoreboards[i]);
	total_size -= sizeof(struct uwx_scoreboard);
    }
    return UWX_OK;
}

int uwx_set_nofr(struct uwx_env *env)
{
    if (env == 0)
	return UWX_ERR_NOENV;

    env->nsbreg = NSBREG_NOFR;
    return UWX_OK;
}

struct uwx_env *uwx_init()
{
    struct uwx_env *env;
    size_t total_size;

    total_size = sizeof(struct uwx_env) +
		    sizeof(struct uwx_str_pool) +
			NSCOREBOARDS * sizeof(struct uwx_scoreboard);

    if (uwx_allocate_cb == 0)
	env = (struct uwx_env *) malloc(total_size);
    else
	env = (struct uwx_env *) (*uwx_allocate_cb)(total_size);
    if (env != 0) {
	uwx_init_env(env, total_size);
	env->on_heap = 1;
    }
    return env;
}

int uwx_set_remote(struct uwx_env *env, int is_big_endian_target)
{
    int is_big_endian_host;
    char *p;

    if (env == 0)
	return UWX_ERR_NOENV;

    env->remote = 1;

    is_big_endian_host = 1;
    p = (char *)&is_big_endian_host;
    *p = 0;
    if (is_big_endian_target == is_big_endian_host)
	env->byte_swap = 0;
    else
	env->byte_swap = 1;

    return UWX_OK;
}

int uwx_register_callbacks(
    struct uwx_env *env,
    intptr_t tok,
    copyin_cb copyin,
    lookupip_cb lookupip)
{
    if (env == 0)
	return UWX_ERR_NOENV;
    env->cb_token = tok;
    env->copyin = copyin;
    env->lookupip = lookupip;
    return UWX_OK;
}

int uwx_get_abi_context_code(struct uwx_env *env)
{
    if (env == 0)
	return UWX_ERR_NOENV;
    return env->abi_context;
}

int uwx_free(struct uwx_env *env)
{
    if (env != 0) {
	uwx_free_scoreboards(env);
	uwx_free_str_pool(env);
	if (env->on_heap) {
	    if (env->free_cb == 0)
		free((void *)env);
	    else
		(*env->free_cb)((void *)env);
	}
    }
    return UWX_OK;
}
OpenPOWER on IntegriCloud