aboutsummaryrefslogtreecommitdiffstats
path: root/sys/contrib/ia64/libuwx/src/uwx_env.c
blob: 4149a276af0c216aba2adee908a861cfc41c86a7 (plain) (blame)
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
/*
Copyright (c) 2003 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;
}

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

    if (uwx_allocate_cb == 0)
	env = (struct uwx_env *) malloc(sizeof(struct uwx_env));
    else
	env = (struct uwx_env *) (*uwx_allocate_cb)(sizeof(struct uwx_env));
    if (env != 0) {
	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;
	(void)uwx_init_history(env);
	env->allocate_cb = uwx_allocate_cb;
	env->free_cb = uwx_free_cb;
	env->free_scoreboards = 0;
	env->used_scoreboards = 0;
	env->labeled_scoreboards = 0;
	(void)uwx_init_str_pool(env);
	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_NOFR;
	env->nscoreboards = 0;
	env->trace = 0;
	TRACE_INIT
	for (i = 0; i < NSCOREBOARDS; i++)
	    (void) uwx_alloc_scoreboard(env);
    }
    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->free_cb == 0)
	    free((void *)env);
	else
	    (*env->free_cb)((void *)env);
    }
    return UWX_OK;
}