summaryrefslogtreecommitdiff
path: root/src/lrmi/backend-vm86.c
blob: d4539f5a39c0df16f5ca17735e1503042420b568 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
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 <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.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

#ifdef __sparc__
#define DEV_MEM "/dev/fb"
#else
#define DEV_MEM "/dev/mem"
#endif
#define ALLOC_ENTRIES(x) ((V_RAM / x) - 1)
#define SHMERRORPTR (void *)(-1)

extern xf86Int10InfoPtr Int10Current;
static int counter = 0;

typedef struct {
    int lowMem;
    int highMem;
    char* base;
    char* base_high;
    int screen;
    char* alloc;
} linuxInt10Priv;


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));
}

void
LRMI_fini(xf86Int10InfoPtr pInt)
{
    if (!pInt)
    return;

    if (Int10Current == pInt) {
    shmdt(0);
    if (((linuxInt10Priv*)pInt->private)->highMem >= 0)
        shmdt((char*)HIGH_MEM);
    else
        munmap((void *)V_BIOS, (SYS_BIOS - V_BIOS));
    Int10Current = NULL;
    }
    
    if (((linuxInt10Priv*)pInt->private)->base_high)
    shmdt(((linuxInt10Priv*)pInt->private)->base_high);
    shmdt(((linuxInt10Priv*)pInt->private)->base);
    shmctl(((linuxInt10Priv*)pInt->private)->lowMem, IPC_RMID, NULL);
    if (((linuxInt10Priv*)pInt->private)->highMem >= 0)
    shmctl(((linuxInt10Priv*)pInt->private)->highMem, IPC_RMID, NULL);
    free(((linuxInt10Priv*)pInt->private)->alloc);
    free(pInt->private);
    free(pInt);
}

int
MapCurrentInt10(xf86Int10InfoPtr pInt)
{
    void *addr;
    int fd = -1;
    
    if (Int10Current) {
    shmdt(0);
    if (((linuxInt10Priv*)Int10Current->private)->highMem >= 0)
        shmdt((char*)HIGH_MEM);
    else
        munmap((void *)V_BIOS, (SYS_BIOS - V_BIOS));
    }
    addr = shmat(((linuxInt10Priv*)pInt->private)->lowMem, (char*)1, SHM_RND);
    if (addr == SHMERRORPTR) {
    fprintf(stderr, "Cannot shmat() low memory\n");
    fprintf(stderr, "shmat(low_mem) error: %s\n",strerror(errno));
    return 0;
    }
    if (mprotect((void*)0, V_RAM, PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
        fprintf(stderr,
           "Cannot set EXEC bit on low memory: %s\n", strerror(errno));

    if (((linuxInt10Priv*)pInt->private)->highMem >= 0) {
    addr = shmat(((linuxInt10Priv*)pInt->private)->highMem,
             (char*)HIGH_MEM, 0);
    if (addr == SHMERRORPTR) {
        fprintf(stderr, "Cannot shmat() high memory\n");
        fprintf(stderr, "shmget error: %s\n",strerror(errno));
        return 0;
    }
    if (mprotect((void*)HIGH_MEM, HIGH_MEM_SIZE,
             PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
        fprintf(stderr, "Cannot set EXEC bit on high memory: %s\n",
               strerror(errno));
    } else {
    if ((fd = open(DEV_MEM, O_RDWR, 0)) >= 0) {
        if (mmap((void *)(V_BIOS), SYS_BIOS - V_BIOS,
                 PROT_READ | PROT_WRITE | PROT_EXEC,
                 MAP_SHARED | MAP_FIXED, fd, V_BIOS)
        == MAP_FAILED) {
        fprintf(stderr, "Cannot map V_BIOS\n");
        close (fd);
        return 0;
        }
    } else {
        fprintf(stderr, "Cannot open %s\n",DEV_MEM);
        return 0;
    }
    close (fd);
    }
    
    return 1;
}

void
LRMI_init(xf86Int10InfoPtr pInt)
{
    int fd;
    static void* vidMem = NULL;
    static void* sysMem = NULL;
    void* vMem = NULL;
    int low_mem;
    int high_mem = -1;
    char *base = SHMERRORPTR;
    char *base_high = SHMERRORPTR;
    int videoBiosMapped = 0;


    /* 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;


    pInt->private = (void *)calloc(1, sizeof(linuxInt10Priv));
    ((linuxInt10Priv*)pInt->private)->screen = pInt->scrnIndex;
    ((linuxInt10Priv*)pInt->private)->alloc =
    (void *)calloc(1, ALLOC_ENTRIES(getpagesize()));

    if (!pci_device_is_boot_vga(pInt->dev)) {
    fprintf(stderr, "Mapping high memory area\n");
    if ((high_mem = shmget(counter++, HIGH_MEM_SIZE,
                   IPC_CREAT | SHM_R | SHM_W)) == -1) {
        if (errno == ENOSYS)
        fprintf(stderr, "shmget error\n Please reconfigure"
               " your kernel to include System V IPC support\n");
        else
        fprintf(stderr, "shmget(highmem) error: %s\n",strerror(errno));
        goto error1;
    }
    } else {
    fprintf(stderr, "Mapping Video BIOS\n");
    videoBiosMapped = 1;
    if ((fd = open(DEV_MEM, O_RDWR, 0)) >= 0) {
        if ((vMem = mmap((void *)(V_BIOS), SYS_BIOS - V_BIOS,
                 PROT_READ | PROT_WRITE | PROT_EXEC,
                 MAP_SHARED | MAP_FIXED, fd, V_BIOS))
        == MAP_FAILED) {
        fprintf(stderr, "Cannot map V_BIOS\n");
        close(fd);
        goto error1;
        }
        close (fd);
    } else
        goto error1;
    }
    ((linuxInt10Priv*)pInt->private)->highMem = high_mem;

    fprintf(stderr, "Mapping 640kB area\n");
    if ((low_mem = shmget(counter++, V_RAM,
              IPC_CREAT | SHM_R | SHM_W)) == -1) {
    fprintf(stderr, "shmget(lowmem) error: %s\n",strerror(errno));
    goto error2;
    }

    ((linuxInt10Priv*)pInt->private)->lowMem = low_mem;
    base = shmat(low_mem, 0, 0);
    if (base == SHMERRORPTR) {
    fprintf(stderr, "shmat(low_mem) error: %s\n",strerror(errno));
    goto error3;
    }
    ((linuxInt10Priv *)pInt->private)->base = base;
    if (high_mem > -1) {
    base_high = shmat(high_mem, 0, 0);
    if (base_high == SHMERRORPTR) {
        fprintf(stderr, "shmat(high_mem) error: %s\n",strerror(errno));
        goto error3;
    }
    ((linuxInt10Priv*)pInt->private)->base_high = base_high;
    } else
    ((linuxInt10Priv*)pInt->private)->base_high = NULL;

    if (!MapCurrentInt10(pInt))
    goto error3;

    Int10Current = pInt;

    return;

error3:
    if (base_high)
    shmdt(base_high);
    shmdt(base);
    shmdt(0);
    if (base_high)
    shmdt((char*)HIGH_MEM);
    shmctl(low_mem, IPC_RMID, NULL);
    Int10Current = NULL;
error2:
    if (high_mem > -1)
    shmctl(high_mem, IPC_RMID,NULL);
error1:
    if (vMem)
    munmap(vMem, SYS_BIOS - V_BIOS);
    free(((linuxInt10Priv*)pInt->private)->alloc);
    free(pInt->private);
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_alloc_real(xf86Int10InfoPtr pInt, int num, int *off)
{
    int pagesize = getpagesize();
    int num_pages = ALLOC_ENTRIES(pagesize);
    int i, j;

    for (i = 0; i < (num_pages - num); i++) {
    if (((linuxInt10Priv*)pInt->private)->alloc[i] == 0) {
        for (j = i; j < (num + i); j++)
        if ((((linuxInt10Priv*)pInt->private)->alloc[j] != 0))
            break;
        if (j == (num + i))
        break;
        else
        i = i + num;
    }
    }
    if (i == (num_pages - num))
    return NULL;

    for (j = i; j < (i + num); j++)
    ((linuxInt10Priv*)pInt->private)->alloc[j] = 1;

    *off = (i + 1) * pagesize;

    return ((linuxInt10Priv*)pInt->private)->base + ((i + 1) * pagesize);
}

void
LRMI_free_real(xf86Int10InfoPtr pInt, void *pbase, int num)
{
    int pagesize = getpagesize();
    int first = (((unsigned long)pbase
         - (unsigned long)((linuxInt10Priv*)pInt->private)->base)
    / pagesize) - 1;
    int i;

    for (i = first; i < (first + num); i++)
    ((linuxInt10Priv*)pInt->private)->alloc[i] = 0;
}

void *
LRMI_base_addr(xf86Int10InfoPtr pInt, unsigned long addr)
{
    if (addr < V_RAM)
    return ((linuxInt10Priv*)pInt->private)->base + addr;
    else if (addr < V_BIOS)
    return (void *)addr;
    else if (addr < SYS_BIOS) {
    if (((linuxInt10Priv*)pInt->private)->base_high)
        return (((linuxInt10Priv*)pInt->private)->base_high
                 - V_BIOS + addr);
    else
        return (void *)addr;
    } else
    return (void *)addr;
}

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