summaryrefslogtreecommitdiff
path: root/src/lrmi/backend-vm86.c
blob: 558a9139ddaaee89ccb11b5d0f5f6bb155f9cdab (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
Linux Real Mode Interface - A library of DPMI-like functions for Linux.

Copyright (C) 1998 by Josh Vanderhoof

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 JOSH VANDERHOOF 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.
*/

#if defined(__i386__) && (defined(__linux__) || defined(__NetBSD__) \
    || defined(__FreeBSD__) || defined(__OpenBSD__))

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <fcntl.h>


#include "libx86.h"
#include "common.h"

#if defined(__linux__)
#define DEFAULT_VM86_FLAGS 	(IF_MASK | IOPL_MASK)
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#define DEFAULT_VM86_FLAGS  (PSL_I | PSL_IOPL)
#endif
#define DEFAULT_STACK_SIZE 	0x1000


static uint8_t
read_b(xf86Int10InfoPtr pInt, int addr)
{
    return *((uint8_t *)addr);
}

static uint16_t
read_w(xf86Int10InfoPtr pInt, int addr)
{
    return *((uint16_t *)addr);
}

static uint32_t
read_l(xf86Int10InfoPtr pInt, int addr)
{
    return *((uint32_t *)addr);
}

static void
write_b(xf86Int10InfoPtr pInt, int addr, uint8_t val)
{
    *((uint8_t *)addr) = val;
}

static void
write_w(xf86Int10InfoPtr pInt, int addr, uint16_t val)
{
    *((uint16_t *)addr) = val;
}

static void
write_l(xf86Int10InfoPtr pInt, int addr, uint32_t val)
{
    *((uint32_t *)addr) = val;
}

int10MemRec linuxMem = {
    read_b,
    read_w,
    read_l,
    write_b,
    write_w,
    write_l
};

static inline void
set_bit(unsigned int bit, void *array)
{
	unsigned char *a = array;

	a[bit / 8] |= (1 << (bit % 8));
}

#ifdef __sparc__
#define DEV_MEM "/dev/fb"
#else
#define DEV_MEM "/dev/mem"
#endif
void
LRMI_init(xf86Int10InfoPtr pInt)
{
    int fd;
    static void* vidMem = NULL;
    static void* sysMem = NULL;


    /* TODO: should put this in common interface and to just run once instead
     * use the "hack" of static variables */
    if ((!vidMem) || (!sysMem)) {
    if ((fd = open(DEV_MEM, O_RDWR, 0)) >= 0) {
        if (!sysMem) {
        if ((sysMem = mmap((void *)(SYS_BIOS), BIOS_SIZE,
                   PROT_READ | PROT_EXEC,
                   MAP_SHARED | MAP_FIXED, fd, SYS_BIOS))
            == MAP_FAILED) {
            fprintf(stderr, "Cannot map SYS BIOS\n");
            close(fd);
            goto error;
        }
        }
        if (!vidMem) {
        if ((vidMem = mmap((void *)(V_RAM), VRAM_SIZE,
                   PROT_READ | PROT_WRITE | PROT_EXEC,
                   MAP_SHARED | MAP_FIXED, fd, V_RAM))
            == MAP_FAILED) {
            fprintf(stderr, "Cannot map V_RAM\n");
            close(fd);
            goto error;
        }
        }
        close(fd);
    } else {
        fprintf(stderr, "Cannot open %s\n", DEV_MEM);
        goto error;
    }
    }

    xxxf86Int10ExecSetup(pInt);

    pInt->mem = &linuxMem;

    return;

error:
    free(pInt);
    return;
#if 0
	void *m;

	if (context.ready)
		return;

	if (!LRMI_common_init())
		return;

	/*
	 Allocate a stack
	*/
	m = LRMI_alloc_real(DEFAULT_STACK_SIZE);

	context.stack_seg = (unsigned int)m >> 4;
	context.stack_off = DEFAULT_STACK_SIZE;

	/*
	 Allocate the return to 32 bit routine
	*/
	m = LRMI_alloc_real(2);

	context.ret_seg = (unsigned int)m >> 4;
	context.ret_off = (unsigned int)m & 0xf;

	((unsigned char *)m)[0] = 0xcd; 	/* int opcode */
	((unsigned char *)m)[1] = RETURN_TO_32_INT;

	memset(&context.vm, 0, sizeof(context.vm));

	/*
	 Enable kernel emulation of all ints except RETURN_TO_32_INT
	*/
#if defined(__linux__)
	memset(&context.vm.int_revectored, 0, sizeof(context.vm.int_revectored));
	set_bit(RETURN_TO_32_INT, &context.vm.int_revectored);
#elif defined(__NetBSD__) || defined(__OpenBSD__)
	set_bit(RETURN_TO_32_INT, &context.vm.int_byuser);
#elif defined(__FreeBSD__)
	set_bit(RETURN_TO_32_INT, &context.vm.init.int_map);
#endif

	context.ready = 1;
#endif
	return;
}

#if 0
static void
set_regs(struct LRMI_regs *r)
{
	CONTEXT_REGS.REG(edi) = r->edi;
	CONTEXT_REGS.REG(esi) = r->esi;
	CONTEXT_REGS.REG(ebp) = r->ebp;
	CONTEXT_REGS.REG(ebx) = r->ebx;
	CONTEXT_REGS.REG(edx) = r->edx;
	CONTEXT_REGS.REG(ecx) = r->ecx;
	CONTEXT_REGS.REG(eax) = r->eax;
	CONTEXT_REGS.REG(eflags) = DEFAULT_VM86_FLAGS;
	CONTEXT_REGS.REG(es) = r->es;
	CONTEXT_REGS.REG(ds) = r->ds;
	CONTEXT_REGS.REG(fs) = r->fs;
	CONTEXT_REGS.REG(gs) = r->gs;
}


static void
get_regs(struct LRMI_regs *r)
{
	r->edi = CONTEXT_REGS.REG(edi);
	r->esi = CONTEXT_REGS.REG(esi);
	r->ebp = CONTEXT_REGS.REG(ebp);
	r->ebx = CONTEXT_REGS.REG(ebx);
	r->edx = CONTEXT_REGS.REG(edx);
	r->ecx = CONTEXT_REGS.REG(ecx);
	r->eax = CONTEXT_REGS.REG(eax);
	r->flags = CONTEXT_REGS.REG(eflags);
	r->es = CONTEXT_REGS.REG(es);
	r->ds = CONTEXT_REGS.REG(ds);
	r->fs = CONTEXT_REGS.REG(fs);
	r->gs = CONTEXT_REGS.REG(gs);
}
#endif

#if 0
int
LRMI_call(struct LRMI_regs *r)
{
	unsigned int vret;

	memset(&CONTEXT_REGS, 0, sizeof(CONTEXT_REGS));

	set_regs(r);

	CONTEXT_REGS.REG(cs) = r->cs;
	CONTEXT_REGS.REG(eip) = r->ip;

	if (r->ss == 0 && r->sp == 0) {
		CONTEXT_REGS.REG(ss) = context.stack_seg;
		CONTEXT_REGS.REG(esp) = context.stack_off;
	} else {
		CONTEXT_REGS.REG(ss) = r->ss;
		CONTEXT_REGS.REG(esp) = r->sp;
	}

	pushw(context.ret_seg);
	pushw(context.ret_off);

	vret = run_vm86();

	get_regs(r);

	return vret;
}
#endif

void
LRMI_int(xf86Int10InfoPtr pInt)
{
    int num = pInt->num;
    int ret = 0;

    pre_int(pInt);

    switch (num) {
    case 0xe6:
    ret = intE6_handler(pInt);
    break;
    default:
    break;
    }

    if (!ret)
    ret = run_bios_int(num, pInt);

    if (!ret) {
    fprintf(stderr, "Halting on int 0x%2.2x!\n", num);
    }

    while(do_vm86(pInt)) {};

    pos_int(pInt);

#if  0
int
LRMI_int(int i, struct LRMI_regs *r)
{
	unsigned int vret;
	unsigned int seg, off;

	seg = get_int_seg(i);
	off = get_int_off(i);

	/*
	 If the interrupt is in regular memory, it's probably
	 still pointing at a dos TSR (which is now gone).
	*/
	if (seg < 0xa000 || (seg << 4) + off >= 0x100000) {
#ifdef LRMI_DEBUG
		fprintf(stderr, "Int 0x%x is not in rom (%04x:%04x)\n", i, seg, off);
#endif
		return 0;
	}

	memset(&CONTEXT_REGS, 0, sizeof(CONTEXT_REGS));

	set_regs(r);

	CONTEXT_REGS.REG(cs) = seg;
	CONTEXT_REGS.REG(eip) = off;

	if (r->ss == 0 && r->sp == 0) {
		CONTEXT_REGS.REG(ss) = context.stack_seg;
		CONTEXT_REGS.REG(esp) = context.stack_off;
	} else {
		CONTEXT_REGS.REG(ss) = r->ss;
		CONTEXT_REGS.REG(esp) = r->sp;
	}

	pushw(DEFAULT_VM86_FLAGS);
	pushw(context.ret_seg);
	pushw(context.ret_off);

	vret = run_vm86();

	get_regs(r);

	return vret;
#endif
}

void *
LRMI_base_addr(xf86Int10InfoPtr pInt, unsigned long addr)
{
	return NULL;
}

#else /* (__linux__ || __NetBSD__ || __FreeBSD__ || __OpenBSD__) && __i386__ */
#error "VM86 is not supported on your system!"
#endif