summaryrefslogtreecommitdiff
path: root/src/lrmi/backend-x86emu.c
blob: f65dc40b1f94d2abc87d3224f4fc03942d77f9b6 (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
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <pciaccess.h>

#include "common.h"
#include "common_helper.h"


xf86Int10InfoPtr Int10Current = NULL;

#define CARD8  unsigned char
#define CARD16 unsigned short
#define CARD32 unsigned long
#define pointer void *
#define IOADDRESS void *
#define Bool int

#define SHMERRORPTR (pointer)(-1)

#define __BUILDIO(bwl,bw,type) \
static inline void out##bwl##_local(unsigned long port, unsigned type value) {        __asm__ __volatile__("out" #bwl " %" #bw "0, %w1" : : "a"(value), "Nd"(port)); \
}\
static inline unsigned type in##bwl##_local(unsigned long port) { \
        unsigned type value; \
        __asm__ __volatile__("in" #bwl " %w1, %" #bw "0" : "=a"(value) : "Nd"(port)); \
        return value; \
}\

__BUILDIO(b,b,char)
__BUILDIO(w,w,short)
__BUILDIO(l,,int)


char *mmap_addr = SHMERRORPTR;
static void *stack;

/*
 * the emulator cannot pass a pointer to the current xf86Int10InfoRec
 * to the memory access functions therefore store it here.
 */

typedef struct {
    int shift;
    int entries;
    void* base;
    void* vRam;
    int highMemory;
    void* sysMem;
    char* alloc;
} genericInt10Priv;

#define INTPriv(x) ((genericInt10Priv*)x->private)

void
printk(const char *fmt, ...)
{
	va_list argptr;
	va_start(argptr, fmt);

	fprintf(stderr, fmt, argptr);
	va_end(argptr);
}

#define OFF(addr) ((addr) & 0xffff)
# define HIGH_OFFSET (INTPriv(pInt)->highMemory)
# define HIGH_BASE   V_BIOS
# define SYS(addr) ((addr) >= HIGH_OFFSET)
#define V_ADDR(addr) \
      (SYS(addr) ? ((char*)INTPriv(pInt)->sysMem) + (addr - HIGH_BASE) \
       : (((char*)(INTPriv(pInt)->base) + addr)))

#define V_ADDR_RB(addr) *(CARD8*) V_ADDR(addr)
//#define V_ADDR_RW(addr) ldw_u((pointer)V_ADDR(addr))
//#define V_ADDR_RL(addr) ldl_u((pointer)V_ADDR(addr))

#define V_ADDR_WB(addr,val) *(CARD8*) V_ADDR(addr) = val;
//#define V_ADDR_WW(addr,val) stw_u((val),(pointer)(V_ADDR(addr)));
//#define V_ADDR_WL(addr,val) stl_u(val,(pointer)(V_ADDR(addr)));

static CARD8
read_b(xf86Int10InfoPtr pInt, int addr)
{
    return V_ADDR_RB(addr);
}

static CARD16
read_w(xf86Int10InfoPtr pInt, int addr)
{
#if 0
X_BYTE_ORDER == X_LITTLE_ENDIAN
    if (OFF(addr + 1) > 0)
    return V_ADDR_RW(addr);
#endif
    return V_ADDR_RB(addr) | (V_ADDR_RB(addr + 1) << 8);
}

static CARD32
read_l(xf86Int10InfoPtr pInt, int addr)
{
#if 0
X_BYTE_ORDER == X_LITTLE_ENDIAN
    if (OFF(addr + 3) > 2)
    return V_ADDR_RL(addr);
#endif
    return V_ADDR_RB(addr) |
       (V_ADDR_RB(addr + 1) << 8) |
       (V_ADDR_RB(addr + 2) << 16) |
       (V_ADDR_RB(addr + 3) << 24);
}

static void
write_b(xf86Int10InfoPtr pInt, int addr, CARD8 val)
{
    V_ADDR_WB(addr,val);
}

static void
write_w(xf86Int10InfoPtr pInt, int addr, CARD16 val)
{
#if 0
X_BYTE_ORDER == X_LITTLE_ENDIAN
    if (OFF(addr + 1) > 0)
      { V_ADDR_WW(addr, val); }
#endif
    V_ADDR_WB(addr, val);
    V_ADDR_WB(addr + 1, val >> 8);
}

static void
write_l(xf86Int10InfoPtr pInt, int addr, CARD32 val)
{
#if 0
X_BYTE_ORDER == X_LITTLE_ENDIAN
    if (OFF(addr + 3) > 2)
      { V_ADDR_WL(addr, val); }
#endif
    V_ADDR_WB(addr, val);
    V_ADDR_WB(addr + 1, val >> 8);
    V_ADDR_WB(addr + 2, val >> 16);
    V_ADDR_WB(addr + 3, val >> 24);
}

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

void *
LRMI_base_addr(xf86Int10InfoPtr pInt, CARD32 addr)
{
    return V_ADDR(addr);
}

static CARD8
x_inb(CARD16 port)
{
    CARD8 val;

    val = inb_local(Int10Current->ioBase + port);
    return val;
}

static CARD16
x_inw(CARD16 port)
{
    CARD16 val;

    val = inw_local(Int10Current->ioBase + port);

    return val;
}

static CARD32
x_inl(CARD16 port)
{
    CARD32 val;

    val = inl_local(Int10Current->ioBase + port);
    return val;
}

static void
x_outl(CARD16 port, CARD32 val)
{
    outl_local(Int10Current->ioBase + port, val);
}

static void
x_outb(CARD16 port, CARD8 val)
{
    outb_local(Int10Current->ioBase + port, val);
}

static void
x_outw(CARD16 port, CARD16 val)
{
    outw_local(Int10Current->ioBase + port, val);
}

static CARD8
Mem_rb(CARD32 addr)
{
    return (*Int10Current->mem->rb)(Int10Current, addr);
}

static CARD16
Mem_rw(CARD32 addr)
{
    return (*Int10Current->mem->rw)(Int10Current, addr);
}

static CARD32
Mem_rl(CARD32 addr)
{
    return (*Int10Current->mem->rl)(Int10Current, addr);
}

static void
Mem_wb(CARD32 addr, CARD8 val)
{
    (*Int10Current->mem->wb)(Int10Current, addr, val);
}

static void
Mem_ww(CARD32 addr, CARD16 val)
{
    (*Int10Current->mem->ww)(Int10Current, addr, val);
}

static void
Mem_wl(CARD32 addr, CARD32 val)
{
    (*Int10Current->mem->wl)(Int10Current, addr, val);
}

static void
pushw(xf86Int10InfoPtr pInt, CARD16 val)
{
    X86_ESP -= 2;
    MEM_WW(pInt, ((CARD32) X86_SS << 4) + X86_SP, val);
}

static void
x86emu_do_int(int num)
{
    Int10Current->num = num;

    run_bios_int(num, Int10Current);
}

static void
pre_int(xf86Int10InfoPtr pInt)
{
    Int10Current = pInt;
    X86_EAX = (CARD32) pInt->ax;
    X86_EBX = (CARD32) pInt->bx;
    X86_ECX = (CARD32) pInt->cx;
    X86_EDX = (CARD32) pInt->dx;
    X86_ESI = (CARD32) pInt->si;
    X86_EDI = (CARD32) pInt->di;
    X86_EBP = (CARD32) pInt->bp;
    X86_ESP = 0x1000; X86_SS = pInt->stackseg >> 4;
    X86_EIP = 0x0600; X86_CS = 0x0; /* address of 'hlt' */
    X86_DS = 0x40;          /* standard pc ds */
    X86_ES = pInt->es;
    X86_FS = 0;
    X86_GS = 0;
    X86_EFLAGS = X86_IF_MASK | X86_IOPL_MASK;
}

static void
pos_int(xf86Int10InfoPtr pInt)
{
    pInt->ax = (CARD32) X86_EAX;
    pInt->bx = (CARD32) X86_EBX;
    pInt->cx = (CARD32) X86_ECX;
    pInt->dx = (CARD32) X86_EDX;
    pInt->si = (CARD32) X86_ESI;
    pInt->di = (CARD32) X86_EDI;
    pInt->es = (CARD16) X86_ES;
    pInt->bp = (CARD32) X86_EBP;
    pInt->flags = (CARD32) X86_FLAGS;
}

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

    X86EMU_exec();

    pos_int(pInt);
}

void
LRMI_init(xf86Int10InfoPtr pInt)
{
    int i;
    X86EMU_intrFuncs intFuncs[256];
    X86EMU_pioFuncs pioFuncs = {
    (&x_inb),
    (&x_inw),
    (&x_inl),
    (&x_outb),
    (&x_outw),
    (&x_outl)
    };

    X86EMU_memFuncs memFuncs = {
    (&Mem_rb),
    (&Mem_rw),
    (&Mem_rl),
    (&Mem_wb),
    (&Mem_ww),
    (&Mem_wl)
    };

    X86EMU_setupMemFuncs(&memFuncs);

    M.mem_base = 0;
    M.mem_size = 1024*1024 + 1024;
    X86EMU_setupPioFuncs(&pioFuncs);

    for (i=0;i<256;i++)
    intFuncs[i] = x86emu_do_int;
    X86EMU_setupIntrFuncs(intFuncs);

    pInt->mem = &genericMem;

#if 0
	int i;

	if (!LRMI_common_init())
		return 0;

	mmap_addr = 0;
	X86_EFLAGS = X86_IF_MASK | X86_IOPL_MASK;

	/*
	 * Allocate a 64k stack.
	 */
	stack = LRMI_alloc_real(64 * 1024);
	X86_SS = (unsigned int) stack >> 4;
	X86_ESP = 0xFFF9;
	memset (stack, 0, 64*1024);

	*((char *)0) = 0xf4; /* Make sure that we end up jumping back to a
				halt instruction */
#endif
}

void
LRMI_set_exit_condition(xf86Int10InfoPtr pInt)
{
    /*
     * Here we set the exit condition:  We return when we encounter
     * 'hlt' (=0xf4), which we locate at address 0x600 in x86 memory.
     */
    MEM_WB(pInt, 0x0600, 0xf4);
}

void
LRMI_fini(xf86Int10InfoPtr pInt)
{
    if (Int10Current == pInt)
    Int10Current = NULL;
}