3 * - By John Hodge (thePowersGang)
6 * - Virtual 8086 Mode Monitor
13 #include <semaphore.h>
18 #define VM8086_USER_BASE 0x1000
20 #define VM8086_MAGIC_CS 0xFFFF
21 #define VM8086_MAGIC_IP 0x0010
22 #define VM8086_STACK_SEG 0x9F00
23 #define VM8086_STACK_OFS 0x0AFE
26 VM8086_OP_PUSHF = 0x9C,
27 VM8086_OP_POPF = 0x9D,
28 VM8086_OP_INT_I = 0xCD,
29 VM8086_OP_IRET = 0xCF,
30 VM8086_OP_IN_AD = 0xEC,
31 VM8086_OP_IN_ADX = 0xED,
32 VM8086_OP_OUT_AD = 0xEE,
33 VM8086_OP_OUT_ADX = 0xEF
35 #define VM8086_PAGES_PER_INST 4
37 #define VM8086_BLOCKSIZE 128
38 #define VM8086_BLOCKCOUNT ((0x9F000-0x10000)/VM8086_BLOCKSIZE)
41 struct sVM8086_InternalPages
43 Uint32 Bitmap; // 32 sections = 128 byte blocks
47 struct sVM8086_InternalData
49 struct sVM8086_InternalPages AllocatedPages[VM8086_PAGES_PER_INST];
53 int VM8086_Install(char **Arguments);
54 void VM8086_GPF(tRegs *Regs);
55 //tVM8086 *VM8086_Init(void);
58 MODULE_DEFINE(0, 0x100, VM8086, VM8086_Install, NULL, NULL);
59 tMutex glVM8086_Process;
60 tSemaphore gVM8086_TaskComplete;
61 tSemaphore gVM8086_TasksToDo;
62 tPID gVM8086_WorkerPID;
63 tTID gVM8086_CallingThread;
64 tVM8086 volatile * volatile gpVM8086_State = (void*)-1; // Set to -1 to avoid race conditions
65 Uint32 gaVM8086_MemBitmap[VM8086_BLOCKCOUNT/32];
66 int gbVM8086_ShadowIF = 0;
69 int VM8086_Install(char **Arguments)
73 Semaphore_Init(&gVM8086_TasksToDo, 0, 10, "VM8086", "TasksToDo");
75 // Lock to avoid race conditions
76 Mutex_Acquire( &glVM8086_Process );
78 // Create BIOS Call process
79 pid = Proc_Clone(CLONE_VM);
80 //Log_Debug("VM8086", "pid = %i", pid);
83 Log_Error("VM8086", "Unable to clone kernel into VM8086 worker");
84 return MODULE_ERR_MISC;
88 Uint * volatile stacksetup; // Initialising Stack
89 Uint16 * volatile rmstack; // Real Mode Stack
92 //Log_Debug("VM8086", "Initialising worker");
95 Threads_SetName("VM8086");
98 for(i=0xA0;i<0x100;i++) {
99 MM_RefPhys(i * 0x1000);
100 MM_Map( (void*)(i * 0x1000), i * 0x1000 );
103 MM_Map( (void*)0, 0 ); // IVT / BDA
104 if( MM_GetRefCount(0x00000) > 2 ) {
105 Log_Notice("VM8086", "Ok, who's touched the IVT? (%i)",
106 MM_GetRefCount(0x00000));
109 MM_Map( (void*)0x9F000, 0x9F000 ); // Stack / EBDA
110 if( MM_GetRefCount(0x9F000) > 2 ) {
111 Log_Notice("VM8086", "And who's been playing with my EBDA? (%i)",
112 MM_GetRefCount(0x9F000));
114 // System Stack / Stub
115 if( MM_Allocate( (void*)0x100000 ) == 0 ) {
116 Log_Error("VM8086", "Unable to allocate memory for stack/stub");
117 gVM8086_WorkerPID = 0;
121 *(Uint8*)(0x100000) = VM8086_OP_IRET;
122 *(Uint8*)(0x100001) = 0x07; // POP ES
123 *(Uint8*)(0x100002) = 0x1F; // POP DS
124 *(Uint8*)(0x100003) = 0xCB; // RET FAR
126 rmstack = (Uint16*)(VM8086_STACK_SEG*16 + VM8086_STACK_OFS);
127 rmstack--; *rmstack = 0xFFFF; //CS
128 rmstack--; *rmstack = 0x0010; //IP
131 stacksetup = (Uint*)0x101000;
132 stacksetup--; *stacksetup = VM8086_STACK_SEG; // GS
133 stacksetup--; *stacksetup = VM8086_STACK_SEG; // FS
134 stacksetup--; *stacksetup = VM8086_STACK_SEG; // DS
135 stacksetup--; *stacksetup = VM8086_STACK_SEG; // ES
136 stacksetup--; *stacksetup = VM8086_STACK_SEG; // SS
137 stacksetup--; *stacksetup = VM8086_STACK_OFS-2; // SP
138 stacksetup--; *stacksetup = 0x20202; // FLAGS
139 stacksetup--; *stacksetup = 0xFFFF; // CS
140 stacksetup--; *stacksetup = 0x10; // IP
141 stacksetup--; *stacksetup = 0xAAAA; // AX
142 stacksetup--; *stacksetup = 0xCCCC; // CX
143 stacksetup--; *stacksetup = 0xDDDD; // DX
144 stacksetup--; *stacksetup = 0xBBBB; // BX
145 stacksetup--; *stacksetup = 0x5454; // SP
146 stacksetup--; *stacksetup = 0xB4B4; // BP
147 stacksetup--; *stacksetup = 0x5151; // SI
148 stacksetup--; *stacksetup = 0xD1D1; // DI
149 stacksetup--; *stacksetup = 0x20|3; // DS - Kernel
150 stacksetup--; *stacksetup = 0x20|3; // ES - Kernel
151 stacksetup--; *stacksetup = 0x20|3; // FS
152 stacksetup--; *stacksetup = 0x20|3; // GS
153 __asm__ __volatile__ (
154 "mov %%eax,%%esp;\n\t" // Set stack pointer
160 "iret;\n\t" : : "a" (stacksetup));
161 for(;;); // Shouldn't be reached
164 gVM8086_WorkerPID = pid;
166 // It's released when the GPF fires
167 Mutex_Acquire( &glVM8086_Process );
168 Mutex_Release( &glVM8086_Process );
170 // Worker killed itself
171 if( gVM8086_WorkerPID != pid ) {
172 return MODULE_ERR_MISC;
175 return MODULE_ERR_OK;
178 void VM8086_GPF(tRegs *Regs)
183 // Log_Log("VM8086", "GPF - %04x:%04x", Regs->cs, Regs->eip);
185 LOG("VM8086 GPF at %04x:%04x", Regs->cs, Regs->eip);
187 if(Regs->eip == VM8086_MAGIC_IP && Regs->cs == VM8086_MAGIC_CS
188 && Threads_GetPID() == gVM8086_WorkerPID)
190 if( gpVM8086_State == (void*)-1 ) {
191 Log_Log("VM8086", "Worker thread ready and waiting");
192 gpVM8086_State = NULL;
193 Mutex_Release( &glVM8086_Process ); // Release lock obtained in VM8086_Install
195 // Log_Log("VM8086", "gpVM8086_State = %p, gVM8086_CallingThread = %i",
196 // gpVM8086_State, gVM8086_CallingThread);
199 gpVM8086_State->AX = Regs->eax; gpVM8086_State->CX = Regs->ecx;
200 gpVM8086_State->DX = Regs->edx; gpVM8086_State->BX = Regs->ebx;
201 gpVM8086_State->BP = Regs->ebp;
202 gpVM8086_State->SI = Regs->esi; gpVM8086_State->DI = Regs->edi;
203 gpVM8086_State->DS = Regs->ds; gpVM8086_State->ES = Regs->es;
205 LOG("gpVM8086_State = %p", gpVM8086_State);
206 LOG("gpVM8086_State->Internal = %p", gpVM8086_State->Internal);
207 for( Uint i = 0; i < VM8086_PAGES_PER_INST; i ++ )
209 if( !gpVM8086_State->Internal->AllocatedPages[i].VirtBase )
211 MM_Deallocate( (tPage*)VM8086_USER_BASE + i );
214 gpVM8086_State = NULL;
217 Semaphore_Signal(&gVM8086_TaskComplete, 1);
220 //Log_Log("VM8086", "Waiting for something to do");
221 __asm__ __volatile__ ("sti");
222 Semaphore_Wait(&gVM8086_TasksToDo, 1);
224 for( Uint i = 0; i < VM8086_PAGES_PER_INST; i ++ )
226 if( !gpVM8086_State->Internal->AllocatedPages[i].VirtBase )
228 MM_RefPhys( gpVM8086_State->Internal->AllocatedPages[i].PhysAddr );
229 MM_Map( (tPage*)VM8086_USER_BASE + i, gpVM8086_State->Internal->AllocatedPages[i].PhysAddr );
233 //Log_Log("VM8086", "We have a task (%p)", gpVM8086_State);
234 Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = VM8086_MAGIC_CS;
235 Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = VM8086_MAGIC_IP;
236 Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->CS;
237 Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->IP;
238 Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->DS;
239 Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->ES;
242 Regs->eip = 0x11; Regs->cs = 0xFFFF;
243 Regs->eax = gpVM8086_State->AX; Regs->ecx = gpVM8086_State->CX;
244 Regs->edx = gpVM8086_State->DX; Regs->ebx = gpVM8086_State->BX;
245 Regs->esi = gpVM8086_State->SI; Regs->edi = gpVM8086_State->DI;
246 Regs->ebp = gpVM8086_State->BP;
247 Regs->ds = 0x23; Regs->es = 0x23;
248 Regs->fs = 0x23; Regs->gs = 0x23;
252 opcode = *(Uint8*)( (Regs->cs*16) + (Regs->eip) );
256 case VM8086_OP_PUSHF: //PUSHF
258 *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->eflags & 0xFFFF;
259 if( gbVM8086_ShadowIF )
260 *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) |= 0x200;
262 *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) &= ~0x200;
264 Log_Debug("VM8086", "%04x:%04x Emulated PUSHF (value 0x%x)",
265 Regs->cs, Regs->eip-1, Regs->eflags & 0xFFFF);
268 case VM8086_OP_POPF: //POPF
269 // Changing IF is not allowed
270 Regs->eflags &= 0xFFFF0202;
271 Regs->eflags |= *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) );
272 gbVM8086_ShadowIF = !!(*(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) & 0x200);
275 Log_Debug("VM8086", "%04x:%04x Emulated POPF (new value 0x%x)",
276 Regs->cs, Regs->eip-1, Regs->eflags & 0xFFFF);
280 case VM8086_OP_INT_I: //INT imm8
283 id = *(Uint8*)( Regs->cs*16 +(Regs->eip&0xFFFF));
286 Regs->esp -= 2; *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->eflags;
287 Regs->esp -= 2; *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->cs;
288 Regs->esp -= 2; *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->eip;
290 newcs = *(Uint16*)(4*id + 2);
291 newip = *(Uint16*)(4*id);
293 Log_Debug("VM8086", "%04x:%04x Emulated INT 0x%x (%04x:%04x) - AX=%04x,BX=%04x",
294 Regs->cs, Regs->eip-2, id, newcs, newip, Regs->eax, Regs->ebx);
301 case VM8086_OP_IRET: //IRET
302 newip = *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ); Regs->esp += 2;
303 newcs = *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ); Regs->esp += 2;
305 Log_Debug("VM8086", "%04x:%04x IRET to %04x:%04x",
306 Regs->cs, Regs->eip-1, newcs, newip);
313 case VM8086_OP_IN_AD: //IN AL, DX
314 Regs->eax &= 0xFFFFFF00;
315 Regs->eax |= inb(Regs->edx&0xFFFF);
317 Log_Debug("VM8086", "%04x:%04x Emulated IN AL, DX (Port 0x%x [Val 0x%02x])",
318 Regs->cs, Regs->eip-1, Regs->edx&0xFFFF, Regs->eax&0xFF);
321 case VM8086_OP_IN_ADX: //IN AX, DX
322 Regs->eax &= 0xFFFF0000;
323 Regs->eax |= inw(Regs->edx&0xFFFF);
325 Log_Debug("VM8086", "%04x:%04x Emulated IN AX, DX (Port 0x%x [Val 0x%04x])",
326 Regs->cs, Regs->eip-1, Regs->edx&0xFFFF, Regs->eax&0xFFFF);
330 case VM8086_OP_OUT_AD: //OUT DX, AL
331 outb(Regs->edx&0xFFFF, Regs->eax&0xFF);
333 Log_Debug("VM8086", "%04x:%04x Emulated OUT DX, AL (*0x%04x = 0x%02x)",
334 Regs->cs, Regs->eip-1, Regs->edx&0xFFFF, Regs->eax&0xFF);
337 case VM8086_OP_OUT_ADX: //OUT DX, AX
338 outw(Regs->edx&0xFFFF, Regs->eax&0xFFFF);
340 Log_Debug("VM8086", "%04x:%04x Emulated OUT DX, AX (*0x%04x = 0x%04x)",
341 Regs->cs, Regs->eip-1, Regs->edx&0xFFFF, Regs->eax&0xFFFF);
345 // TODO: Decide on allowing VM8086 Apps to enable/disable interrupts
348 Log_Debug("VM8086", "%04x:%04x Ignored CLI",
349 Regs->cs, Regs->eip);
351 gbVM8086_ShadowIF = 0;
355 Log_Debug("VM8086", "%04x:%04x Ignored STI",
356 Regs->cs, Regs->eip);
358 gbVM8086_ShadowIF = 1;
362 opcode = *(Uint8*)( (Regs->cs*16) + (Regs->eip&0xFFFF));
366 case VM8086_OP_IN_ADX: //IN AX, DX
367 Regs->eax = ind(Regs->edx&0xFFFF);
369 Log_Debug("VM8086", "%04x:%04x Emulated IN EAX, DX (Port 0x%x [Val 0x%08x])",
370 Regs->cs, Regs->eip-1, Regs->edx&0xFFFF, Regs->eax);
373 case VM8086_OP_OUT_ADX: //OUT DX, AX
374 outd(Regs->edx&0xFFFF, Regs->eax);
376 Log_Debug("VM8086", "%04x:%04x Emulated OUT DX, EAX (*0x%04x = 0x%08x)",
377 Regs->cs, Regs->eip-1, Regs->edx&0xFFFF, Regs->eax);
381 Log_Error("VM8086", "Error - Unknown opcode 66 %02x caused a GPF at %04x:%04x",
382 Regs->cs, Regs->eip-2,
385 // Force an end to the call
386 Regs->cs = VM8086_MAGIC_CS;
387 Regs->eip = VM8086_MAGIC_IP;
393 opcode = *(Uint8*)( (Regs->cs*16) + (Regs->eip&0xFFFF));
394 Log_Error("VM8086", "Error - Unknown opcode 0F %02x caused a GPF at %04x:%04x",
395 opcode, Regs->cs, Regs->eip);
396 // Force an end to the call
397 Regs->cs = VM8086_MAGIC_CS;
398 Regs->eip = VM8086_MAGIC_IP;
402 Log_Error("VM8086", "Error - Unknown opcode %02x caused a GPF at %04x:%04x",
403 opcode, Regs->cs, Regs->eip-1);
404 // Force an end to the call
405 Regs->cs = VM8086_MAGIC_CS;
406 Regs->eip = VM8086_MAGIC_IP;
412 * \brief Create an instance of the VM8086 Emulator
414 tVM8086 *VM8086_Init(void)
417 ret = calloc( 1, sizeof(tVM8086) + sizeof(struct sVM8086_InternalData) );
418 ret->Internal = (void*)((tVAddr)ret + sizeof(tVM8086));
422 void VM8086_Free(tVM8086 *State)
424 // TODO: Make sure the state isn't in use currently
425 for( Uint i = VM8086_PAGES_PER_INST; i --; )
426 MM_UnmapHWPages( State->Internal->AllocatedPages[i].VirtBase, 1);
430 void *VM8086_Allocate(tVM8086 *State, int Size, Uint16 *Segment, Uint16 *Offset)
432 struct sVM8086_InternalPages *pages = State->Internal->AllocatedPages;
436 Size = (Size + 127) & ~127;
437 nBlocks = Size / 128;
439 if(Size > 4096) return NULL;
441 for( i = 0; i < VM8086_PAGES_PER_INST; i++ )
443 if( pages[i].VirtBase == 0 ) continue;
445 //Log_Debug("VM8086", "pages[%i].Bitmap = 0b%b", i, pages[i].Bitmap);
449 // Scan the bitmap for a free block
450 // - 32 blocks per page == 128 bytes per block == 8 segments
451 for( j = 0; j < 32; j++ )
453 if( pages[i].Bitmap & (1 << j) )
460 if(rem == 0) // Goodie, there's a gap
462 for( j = 0; j < nBlocks; j++ )
463 pages[i].Bitmap |= 1 << (base + j);
464 *Segment = (VM8086_USER_BASE + i * 0x1000) / 16 + base * 8;
466 LOG("Allocated at #%i,%04x", i, base*8*16);
467 LOG(" - %x:%x", *Segment, *Offset);
468 return pages[i].VirtBase + base * 8 * 16;
473 // No pages with free space?, allocate a new one
474 for( i = 0; i < VM8086_PAGES_PER_INST; i++ )
476 if( pages[i].VirtBase == 0 ) break;
478 // Darn, we can't allocate any more
479 if( i == VM8086_PAGES_PER_INST ) {
480 Log_Warning("VM8086", "Out of pages in %p", State);
484 pages[i].VirtBase = MM_AllocDMA(1, -1, &pages[i].PhysAddr);
485 if( pages[i].VirtBase == 0 ) {
486 Log_Warning("VM8086", "Unable to allocate data page");
490 LOG("AllocatedPages[%i].VirtBase = %p", i, pages[i].VirtBase);
491 LOG("AllocatedPages[%i].PhysAddr = %P", i, pages[i].PhysAddr);
493 for( j = 0; j < nBlocks; j++ )
494 pages[i].Bitmap |= 1 << j;
495 LOG("AllocatedPages[%i].Bitmap = 0b%b", i, pages[i].Bitmap);
496 *Segment = (VM8086_USER_BASE + i * 0x1000) / 16;
498 LOG(" - %04x:%04x", *Segment, *Offset);
499 return pages[i].VirtBase;
502 void *VM8086_GetPointer(tVM8086 *State, Uint16 Segment, Uint16 Offset)
504 Uint32 addr = Segment * 16 + Offset;
506 if( VM8086_USER_BASE <= addr && addr < VM8086_USER_BASE + VM8086_PAGES_PER_INST*0x1000 )
508 int pg = (addr - VM8086_USER_BASE) / 0x1000;
509 if( State->Internal->AllocatedPages[pg].VirtBase == 0)
512 return State->Internal->AllocatedPages[pg].VirtBase + (addr & 0xFFF);
516 return (void*)( KERNEL_BASE + addr );
520 void VM8086_Int(tVM8086 *State, Uint8 Interrupt)
522 State->IP = *(Uint16*)(KERNEL_BASE+4*Interrupt);
523 State->CS = *(Uint16*)(KERNEL_BASE+4*Interrupt+2);
525 // Log_Debug("VM8086", "Software interrupt %i to %04x:%04x", Interrupt, State->CS, State->IP);
527 Mutex_Acquire( &glVM8086_Process );
529 gpVM8086_State = State;
530 gVM8086_CallingThread = Threads_GetTID();
531 Semaphore_Signal(&gVM8086_TasksToDo, 1);
533 Semaphore_Wait(&gVM8086_TaskComplete, 1);
535 Mutex_Release( &glVM8086_Process );