Rev 444 | Rev 477 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* Project: OSLib |
2 | * Description: The OS Construction Kit |
||
3 | * Date: 1.6.2000 |
||
4 | * Idea by: Luca Abeni & Gerardo Lamastra |
||
5 | * |
||
6 | * OSLib is an SO project aimed at developing a common, easy-to-use |
||
7 | * low-level infrastructure for developing OS kernels and Embedded |
||
8 | * Applications; it partially derives from the HARTIK project but it |
||
9 | * currently is independently developed. |
||
10 | * |
||
11 | * OSLib is distributed under GPL License, and some of its code has |
||
12 | * been derived from the Linux kernel source; also some important |
||
13 | * ideas come from studying the DJGPP go32 extender. |
||
14 | * |
||
15 | * We acknowledge the Linux Community, Free Software Foundation, |
||
16 | * D.J. Delorie and all the other developers who believe in the |
||
17 | * freedom of software and ideas. |
||
18 | * |
||
19 | * For legalese, check out the included GPL license. |
||
20 | */ |
||
21 | |||
22 | /* File: Vm86.C |
||
23 | * |
||
24 | * VM86 mode switch routines! |
||
25 | * This is basically an alternative way of invoking the |
||
26 | * BIOS service routines; it is very useful to support |
||
27 | * native VBE compliant Video card, without writing an explicit driver |
||
28 | */ |
||
29 | |||
40 | pj | 30 | #include <ll/i386/hw-data.h> |
31 | #include <ll/i386/hw-instr.h> |
||
32 | #include <ll/i386/hw-func.h> |
||
2 | pj | 33 | #include <ll/i386/mem.h> |
34 | #include <ll/i386/x-bios.h> |
||
40 | pj | 35 | #include <ll/i386/x-dosmem.h> |
2 | pj | 36 | #include <ll/i386/cons.h> |
37 | #include <ll/i386/error.h> |
||
451 | giacomo | 38 | #include <ll/i386/apic.h> |
39 | #include <ll/i386/advtimer.h> |
||
2 | pj | 40 | |
40 | pj | 41 | FILE(VM-86); |
2 | pj | 42 | |
43 | /* |
||
44 | #define __LL_DEBUG__ |
||
45 | #define __DUMB_CODE__ |
||
46 | #define __CHK_IO__ |
||
47 | */ |
||
48 | |||
443 | giacomo | 49 | //#define __LL_DEBUG__ |
313 | giacomo | 50 | |
444 | giacomo | 51 | #define VM86_STACK_SIZE 8192 |
2 | pj | 52 | |
40 | pj | 53 | extern DWORD ll_irq_table[256]; |
54 | |||
2 | pj | 55 | /* TSS optional section */ |
40 | pj | 56 | static BYTE vm86_stack0[VM86_STACK_SIZE]; |
2 | pj | 57 | |
58 | static struct { |
||
59 | TSS t; |
||
60 | DWORD io_map[2048]; |
||
61 | } vm86_TSS; |
||
62 | static LIN_ADDR vm86_stack; |
||
63 | static LIN_ADDR vm86_iretAddress; |
||
64 | |||
40 | pj | 65 | struct registers *global_regs; |
443 | giacomo | 66 | WORD VM86_ret_ctx; |
2 | pj | 67 | |
68 | #ifdef __DUMB_CODE__ |
||
69 | static LIN_ADDR vm86_code; |
||
70 | static BYTE prova86[] = { |
||
40 | pj | 71 | 0x1e, /* push ds */ |
72 | 0xb8,0x00,0xb8, /* mov ax,0xb800 */ |
||
73 | 0x8e,0xd8, /* mov ds,ax */ |
||
74 | 0xbf,0x9e,0x00, /* mov di,0x009e (158) */ |
||
75 | 0xb0,0x2a, /* mov ax,'*' */ |
||
76 | 0x88,0x05, /* mov ds:[di],al */ |
||
77 | 0x1f, /* pop ds */ |
||
78 | 0xcd, 0x40, /*???*/ |
||
79 | #ifdef __CHK_IO__ |
||
80 | 0xb0, 0x00, /* movb $0x0,%al*/ |
||
81 | 0x66,0xba, 0x80, 0x00, /* movw $0x80,%dx */ |
||
82 | 0x66,0xef, /* outw %ax, (%dx) */ |
||
83 | #endif |
||
84 | 0xcf, /* iret */ |
||
85 | 0xf4, /* hlt */ |
||
86 | 0}; |
||
2 | pj | 87 | #endif |
88 | |||
444 | giacomo | 89 | static BYTE vm86_retAddr[] = {0xcd, 0x48, /* int 48h */ |
90 | 0xf4, |
||
91 | 0}; |
||
2 | pj | 92 | |
40 | pj | 93 | TSS *vm86_get_tss(void) |
94 | { |
||
95 | return &(vm86_TSS.t); |
||
96 | } |
||
97 | |||
2 | pj | 98 | /* Just a debugging function; it dumps the status of the TSS */ |
99 | void vm86_dump_TSS(void) |
||
100 | { |
||
40 | pj | 101 | BYTE acc,gran; |
102 | DWORD base,lim; |
||
2 | pj | 103 | message("vm86_TSS.t dump\n"); |
40 | pj | 104 | message("Flag: %lx\n",vm86_TSS.t.eflags); |
105 | message("SS: %hx SP:%lx\n", vm86_TSS.t.ss,vm86_TSS.t.esp); |
||
106 | message("Stack0: %hx:%lx\n",vm86_TSS.t.ss0,vm86_TSS.t.esp0); |
||
107 | message("Stack1: %hx:%lx\n",vm86_TSS.t.ss1,vm86_TSS.t.esp1); |
||
108 | message("Stack2: %hx:%lx\n",vm86_TSS.t.ss2,vm86_TSS.t.esp2); |
||
109 | message("CS: %hx IP: %lx",vm86_TSS.t.cs, vm86_TSS.t.eip); |
||
110 | message("DS: %hx\n",vm86_TSS.t.ds); |
||
111 | base = GDT_read(X_VM86_TSS,&lim,&acc,&gran); |
||
2 | pj | 112 | message("Base : %lx Lim : %lx Acc : %x Gran %x\n", |
40 | pj | 113 | base,lim,(unsigned)(acc),(unsigned)(gran)); |
2 | pj | 114 | } |
115 | |||
116 | void vm86_init(void) |
||
117 | { |
||
118 | int register i; |
||
40 | pj | 119 | |
2 | pj | 120 | /* First of all, we need to setup a GDT entries to |
121 | * allow vm86 task execution. We just need a free 386 TSS, which |
||
122 | * will be used to store the execution context of the virtual 8086 |
||
123 | * task |
||
124 | */ |
||
40 | pj | 125 | GDT_place(X_VM86_TSS,(DWORD)(&vm86_TSS), |
126 | sizeof(vm86_TSS),FREE_TSS386,GRAN_16); |
||
2 | pj | 127 | |
443 | giacomo | 128 | /* Return Registers */ |
129 | global_regs = DOS_alloc(sizeof(struct registers)); |
||
40 | pj | 130 | |
2 | pj | 131 | /* Prepare a real-mode stack, obtaining it from the |
132 | * DOS memory allocator! |
||
133 | * 8K should be OK! Stack top is vm86_stack + SIZE! |
||
134 | */ |
||
40 | pj | 135 | vm86_stack = DOS_alloc(VM86_STACK_SIZE*2); |
136 | vm86_stack += VM86_STACK_SIZE/2; |
||
137 | |||
2 | pj | 138 | vm86_iretAddress = DOS_alloc(sizeof(vm86_retAddr)); |
40 | pj | 139 | memcpy(vm86_iretAddress,vm86_retAddr,sizeof(vm86_retAddr)); |
2 | pj | 140 | #ifdef __LL_DEBUG__ |
40 | pj | 141 | message("PM reentry linear address=0x%lx\n", (DWORD)vm86_iretAddress); |
2 | pj | 142 | #endif |
143 | #ifdef __DUMB_CODE__ |
||
144 | vm86_code = DOS_alloc(2048); |
||
40 | pj | 145 | lmemcpy(vm86_code,prova86,sizeof(prova86)); |
2 | pj | 146 | #endif |
147 | /* Zero the PM/Ring[1,2] ss:esp; they're unused! */ |
||
148 | vm86_TSS.t.esp1 = 0; |
||
149 | vm86_TSS.t.esp2 = 0; |
||
150 | vm86_TSS.t.ss1 = 0; |
||
151 | vm86_TSS.t.ss2 = 0; |
||
152 | /* Use only the GDT */ |
||
153 | vm86_TSS.t.ldt = 0; |
||
154 | /* No paging activated */ |
||
155 | vm86_TSS.t.cr3 = 0; |
||
156 | vm86_TSS.t.trap = 0; |
||
157 | /* Yeah, free access to any I/O port; we trust BIOS anyway! */ |
||
158 | /* Here is the explanation: we have 65536 I/O ports... each bit |
||
159 | * in the io_map masks/unmasks the exception for the given I/O port |
||
160 | * If the bit is set, an exception is generated; otherwise, if the bit |
||
161 | * is clear, everythings works fine... |
||
162 | * Because of alignment problem, we need to add an extra byte all set |
||
163 | * to 1, according to Intel manuals |
||
164 | */ |
||
40 | pj | 165 | vm86_TSS.t.io_base = (DWORD)(&(vm86_TSS.io_map)) - |
166 | (DWORD)(&(vm86_TSS)); |
||
167 | for (i = 0; i < 2047; i++) vm86_TSS.io_map[i] = 0; |
||
2 | pj | 168 | vm86_TSS.io_map[2047] = 0xFF000000; |
169 | } |
||
170 | |||
40 | pj | 171 | int vm86_callBIOS(int service,X_REGS16 *in,X_REGS16 *out,X_SREGS16 *s) |
2 | pj | 172 | { |
173 | DWORD vm86_tmpAddr; |
||
40 | pj | 174 | DWORD vm86_flags, vm86_cs,vm86_ip; |
2 | pj | 175 | LIN_ADDR vm86_stackPtr; |
176 | DWORD *IRQTable_entry; |
||
444 | giacomo | 177 | BYTE p1,p2; |
443 | giacomo | 178 | |
451 | giacomo | 179 | #ifdef __APIC__ |
180 | DWORD msr1 = 0,msr2 = 0; |
||
181 | #endif |
||
182 | |||
443 | giacomo | 183 | SYS_FLAGS f; |
444 | giacomo | 184 | |
443 | giacomo | 185 | if (service < 0x10 || in == NULL) return -1; |
40 | pj | 186 | |
443 | giacomo | 187 | f = ll_fsave(); |
188 | |||
2 | pj | 189 | /* Setup the stack frame */ |
40 | pj | 190 | vm86_tmpAddr = (DWORD)(vm86_stack); |
2 | pj | 191 | vm86_TSS.t.ss = (vm86_tmpAddr & 0xFF000) >> 4; |
192 | vm86_TSS.t.ebp = vm86_TSS.t.esp = (vm86_tmpAddr & 0x0FFF) |
||
40 | pj | 193 | + VM86_STACK_SIZE - 6; |
2 | pj | 194 | /* Build an iret stack frame which returns to vm86_iretAddress */ |
40 | pj | 195 | vm86_tmpAddr = (DWORD)(vm86_iretAddress); |
2 | pj | 196 | vm86_cs = (vm86_tmpAddr & 0xFF000) >> 4; |
197 | vm86_ip = (vm86_tmpAddr & 0xFFF); |
||
444 | giacomo | 198 | vm86_flags = 0; |
2 | pj | 199 | vm86_stackPtr = vm86_stack + VM86_STACK_SIZE; |
40 | pj | 200 | lmempokew(vm86_stackPtr-6,vm86_ip); |
201 | lmempokew(vm86_stackPtr-4,vm86_cs); |
||
202 | lmempokew(vm86_stackPtr-2,vm86_flags); |
||
2 | pj | 203 | #ifdef __LL_DEBUG__ |
204 | message("Stack: %lx SS: %lx SP: %lx\n", |
||
40 | pj | 205 | vm86_tmpAddr + VM86_STACK_SIZE,(DWORD)vm86_TSS.t.ss,vm86_TSS.t.esp); |
2 | pj | 206 | #endif |
207 | /* Wanted VM86 mode + IOPL = 3! */ |
||
443 | giacomo | 208 | vm86_TSS.t.eflags = CPU_FLAG_VM + CPU_FLAG_IOPL; |
2 | pj | 209 | /* Preload some standard values into the registers */ |
210 | vm86_TSS.t.ss0 = X_FLATDATA_SEL; |
||
40 | pj | 211 | vm86_TSS.t.esp0 = (DWORD)&(vm86_stack0[VM86_STACK_SIZE-1]); |
212 | |||
2 | pj | 213 | #ifdef __DUMB_CODE__ |
40 | pj | 214 | vm86_TSS.t.cs = ((DWORD)(vm86_code) & 0xFFFF0) >> 4; |
215 | vm86_TSS.t.eip = ((DWORD)(vm86_code) & 0x000F); |
||
2 | pj | 216 | #ifdef __LL_DEBUG_ |
217 | message("(DUMB CODE) CS:%x IP:%x/%x\n", |
||
40 | pj | 218 | (DWORD)vm86_TSS.t.cs,vm86_TSS.t.eip,&prova86); |
2 | pj | 219 | message("(DUMB CODE) Go...\n"); |
220 | #endif |
||
443 | giacomo | 221 | |
222 | p1 = inp(0x21); |
||
223 | p2 = inp(0xA1); |
||
224 | outp(0x21,0xFF); |
||
225 | outp(0xA1,0xFF); |
||
444 | giacomo | 226 | |
451 | giacomo | 227 | #ifdef __APIC__ |
228 | rdmsr(APIC_BASE_MSR,msr1,msr2); |
||
229 | disable_APIC_timer(); |
||
230 | #endif |
||
231 | |||
444 | giacomo | 232 | vm86_TSS.t.back_link = ll_context_save(); |
233 | VM86_ret_ctx = vm86_TSS.t.back_link |
||
451 | giacomo | 234 | sti(); |
2 | pj | 235 | ll_context_load(X_VM86_TSS); |
451 | giacomo | 236 | cli(); |
2 | pj | 237 | |
451 | giacomo | 238 | #ifdef __APIC__ |
239 | wrmsr(APIC_BASE_MSR,msr1,msr2); |
||
240 | enable_APIC_timer(); |
||
241 | #endif |
||
242 | |||
443 | giacomo | 243 | outp(0x21,p1); |
244 | outp(0xA1,p2); |
||
245 | |||
2 | pj | 246 | #ifdef __LL_DEBUG_ |
247 | message("(DUMB CODE) I am back...\n"); |
||
248 | #endif |
||
249 | #else |
||
250 | /* Copy the parms from the X_*REGS structures in the vm86 TSS */ |
||
40 | pj | 251 | vm86_TSS.t.eax = (DWORD)in->x.ax; |
252 | vm86_TSS.t.ebx = (DWORD)in->x.bx; |
||
253 | vm86_TSS.t.ecx = (DWORD)in->x.cx; |
||
254 | vm86_TSS.t.edx = (DWORD)in->x.dx; |
||
255 | vm86_TSS.t.esi = (DWORD)in->x.si; |
||
256 | vm86_TSS.t.edi = (DWORD)in->x.di; |
||
2 | pj | 257 | /* IF Segment registers are required, copy them... */ |
258 | if (s != NULL) { |
||
40 | pj | 259 | vm86_TSS.t.es = (WORD)s->es; |
260 | vm86_TSS.t.ds = (WORD)s->ds; |
||
2 | pj | 261 | } else { |
40 | pj | 262 | vm86_TSS.t.ds = vm86_TSS.t.ss; |
263 | vm86_TSS.t.es = vm86_TSS.t.ss; |
||
2 | pj | 264 | } |
40 | pj | 265 | vm86_TSS.t.gs = vm86_TSS.t.ss; |
266 | vm86_TSS.t.fs = vm86_TSS.t.ss; |
||
2 | pj | 267 | /* Execute the BIOS call, fetching the CS:IP of the real interrupt |
268 | * handler from 0:0 (DOS irq table!) |
||
269 | */ |
||
40 | pj | 270 | IRQTable_entry = (void *)(0L); |
271 | vm86_TSS.t.cs= ((IRQTable_entry[service]) & 0xFFFF0000) >> 16; |
||
2 | pj | 272 | vm86_TSS.t.eip = ((IRQTable_entry[service]) & 0x0000FFFF); |
40 | pj | 273 | #ifdef __LL_DEBUG__ |
274 | message("CS:%x IP:%lx\n", vm86_TSS.t.cs, vm86_TSS.t.eip); |
||
2 | pj | 275 | #endif |
276 | /* Let's use the ll standard call... */ |
||
443 | giacomo | 277 | |
278 | p1 = inp(0x21); |
||
279 | p2 = inp(0xA1); |
||
280 | outp(0x21,0xFF); |
||
281 | outp(0xA1,0xFF); |
||
444 | giacomo | 282 | |
451 | giacomo | 283 | #ifdef __APIC__ |
284 | rdmsr(APIC_BASE_MSR,msr1,msr2); |
||
285 | disable_APIC_timer(); |
||
286 | #endif |
||
287 | |||
444 | giacomo | 288 | vm86_TSS.t.back_link = ll_context_save(); |
289 | VM86_ret_ctx = vm86_TSS.t.back_link; |
||
443 | giacomo | 290 | sti(); |
2 | pj | 291 | ll_context_load(X_VM86_TSS); |
451 | giacomo | 292 | cli(); |
443 | giacomo | 293 | |
451 | giacomo | 294 | #ifdef __APIC__ |
295 | wrmsr(APIC_BASE_MSR,msr1,msr2); |
||
296 | enable_APIC_timer(); |
||
297 | #endif |
||
298 | |||
443 | giacomo | 299 | outp(0x21,p1); |
300 | outp(0xA1,p2); |
||
301 | |||
40 | pj | 302 | #ifdef __LL_DEBUG__ |
2 | pj | 303 | message("I am back...\n"); |
304 | message("TSS CS=%hx IP=%lx\n", vm86_TSS.t.cs, vm86_TSS.t.eip); |
||
305 | #endif |
||
306 | /* Send back in the X_*REGS structure the value obtained with |
||
307 | * the real-mode interrupt call |
||
308 | */ |
||
309 | if (out != NULL) { |
||
443 | giacomo | 310 | |
40 | pj | 311 | out->x.ax = global_regs->eax; |
312 | out->x.bx = global_regs->ebx; |
||
313 | out->x.cx = global_regs->ecx; |
||
314 | out->x.dx = global_regs->edx; |
||
315 | out->x.si = global_regs->esi; |
||
316 | out->x.di = global_regs->edi; |
||
317 | out->x.cflag = global_regs->flags; |
||
443 | giacomo | 318 | |
319 | //message("ax = %d bx = %d cx = %d dx = %d\n",out->x.ax,out->x.bx,out->x.cx,out->x.dx); |
||
320 | //message("si = %d di = %d\n",out->x.si,out->x.di); |
||
321 | |||
2 | pj | 322 | } |
323 | if (s != NULL) { |
||
40 | pj | 324 | s->es = vm86_TSS.t.es; |
325 | s->ds = vm86_TSS.t.ds; |
||
2 | pj | 326 | } |
327 | #endif |
||
443 | giacomo | 328 | |
444 | giacomo | 329 | ll_frestore(f); |
443 | giacomo | 330 | |
444 | giacomo | 331 | return 1; |
332 | |||
2 | pj | 333 | } |