3 * - By John Hodge (thePowersGang)
10 #include <semaphore.h>
13 #define VM8086_MAGIC_CS 0xFFFF
14 #define VM8086_MAGIC_IP 0x0010
15 #define VM8086_STACK_SEG 0x9F00
16 #define VM8086_STACK_OFS 0x0AFE
19 VM8086_OP_PUSHF = 0x9C,
20 VM8086_OP_POPF = 0x9D,
21 VM8086_OP_INT_I = 0xCD,
22 VM8086_OP_IRET = 0xCF,
23 VM8086_OP_IN_AD = 0xEC,
24 VM8086_OP_IN_ADX = 0xED,
25 VM8086_OP_OUT_AD = 0xEE,
26 VM8086_OP_OUT_ADX = 0xEF
28 #define VM8086_PAGES_PER_INST 4
30 #define VM8086_BLOCKSIZE 128
31 #define VM8086_BLOCKCOUNT ((0x9F000-0x10000)/VM8086_BLOCKSIZE)
34 struct sVM8086_InternalData
37 Uint32 Bitmap; // 32 sections = 128 byte blocks
40 } AllocatedPages[VM8086_PAGES_PER_INST];
44 int VM8086_Install(char **Arguments);
45 void VM8086_GPF(tRegs *Regs);
46 //tVM8086 *VM8086_Init(void);
49 MODULE_DEFINE(0, 0x100, VM8086, VM8086_Install, NULL, NULL);
50 tMutex glVM8086_Process;
51 tSemaphore gVM8086_TaskComplete;
52 tSemaphore gVM8086_TasksToDo;
53 tPID gVM8086_WorkerPID;
54 tTID gVM8086_CallingThread;
55 tVM8086 volatile * volatile gpVM8086_State = (void*)-1; // Set to -1 to avoid race conditions
56 Uint32 gaVM8086_MemBitmap[VM8086_BLOCKCOUNT/32];
59 int VM8086_Install(char **Arguments)
63 // Lock to avoid race conditions
64 Mutex_Acquire( &glVM8086_Process );
66 // Create BIOS Call process
67 pid = Proc_Clone(CLONE_VM);
70 Log_Error("VM8086", "Unable to clone kernel into VM8086 worker");
71 return MODULE_ERR_MISC;
75 Uint * volatile stacksetup; // Initialising Stack
76 Uint16 * volatile rmstack; // Real Mode Stack
80 Threads_SetName("VM8086");
82 Log_Debug("VM8086", "Mapping memory");
85 for(i=0xA0;i<0x100;i++) {
86 MM_Map( i * 0x1000, i * 0x1000 );
88 Log_Debug("VM8086", "ROM area mapped");
89 MM_Map( 0, 0 ); // IVT / BDA
90 // Map (but allow allocation) of 0x1000 - 0x9F000
91 // - So much hack, it isn't funny
93 MM_Map( i * 0x1000, i * 0x1000 );
94 MM_DerefPhys( i * 0x1000 ); // Above
95 while(MM_GetRefCount(i*0x1000))
96 MM_DerefPhys( i * 0x1000 ); // Phys setup
98 MM_Map( 0x9F000, 0x9F000 ); // Stack / EBDA
99 // System Stack / Stub
100 if( MM_Allocate( 0x100000 ) == 0 ) {
101 Log_Error("VM8086", "Unable to allocate memory for stack/stub");
102 gVM8086_WorkerPID = 0;
105 Log_Debug("VM8086", "Mapped low memory");
107 *(Uint8*)(0x100000) = VM8086_OP_IRET;
108 *(Uint8*)(0x100001) = 0x07; // POP ES
109 *(Uint8*)(0x100002) = 0x1F; // POP DS
110 *(Uint8*)(0x100003) = 0xCB; // RET FAR
112 rmstack = (Uint16*)(VM8086_STACK_SEG*16 + VM8086_STACK_OFS);
113 rmstack--; *rmstack = 0xFFFF; //CS
114 rmstack--; *rmstack = 0x0010; //IP
117 stacksetup = (Uint*)0x101000;
118 stacksetup--; *stacksetup = VM8086_STACK_SEG; // GS
119 stacksetup--; *stacksetup = VM8086_STACK_SEG; // FS
120 stacksetup--; *stacksetup = VM8086_STACK_SEG; // DS
121 stacksetup--; *stacksetup = VM8086_STACK_SEG; // ES
122 stacksetup--; *stacksetup = VM8086_STACK_SEG; // SS
123 stacksetup--; *stacksetup = VM8086_STACK_OFS-2; // SP
124 stacksetup--; *stacksetup = 0x20202; // FLAGS
125 stacksetup--; *stacksetup = 0xFFFF; // CS
126 stacksetup--; *stacksetup = 0x10; // IP
127 stacksetup--; *stacksetup = 0xAAAA; // AX
128 stacksetup--; *stacksetup = 0xCCCC; // CX
129 stacksetup--; *stacksetup = 0xDDDD; // DX
130 stacksetup--; *stacksetup = 0xBBBB; // BX
131 stacksetup--; *stacksetup = 0x5454; // SP
132 stacksetup--; *stacksetup = 0xB4B4; // BP
133 stacksetup--; *stacksetup = 0x5151; // SI
134 stacksetup--; *stacksetup = 0xD1D1; // DI
135 stacksetup--; *stacksetup = 0x20|3; // DS - Kernel
136 stacksetup--; *stacksetup = 0x20|3; // ES - Kernel
137 stacksetup--; *stacksetup = 0x20|3; // FS
138 stacksetup--; *stacksetup = 0x20|3; // GS
139 __asm__ __volatile__ (
140 "mov %%eax,%%esp;\n\t" // Set stack pointer
146 "iret;\n\t" : : "a" (stacksetup));
147 for(;;); // Shouldn't be reached
150 gVM8086_WorkerPID = pid;
151 Log_Log("VM8086", "gVM8086_WorkerPID = %i", pid);
152 while( gpVM8086_State != NULL )
153 Threads_Yield(); // Yield to allow the child to initialise
155 // Worker killed itself
156 if( gVM8086_WorkerPID != pid ) {
157 return MODULE_ERR_MISC;
160 return MODULE_ERR_OK;
163 void VM8086_GPF(tRegs *Regs)
167 //Log_Log("VM8086", "GPF - %04x:%04x", Regs->cs, Regs->eip);
169 if(Regs->eip == VM8086_MAGIC_IP && Regs->cs == VM8086_MAGIC_CS
170 && Threads_GetPID() == gVM8086_WorkerPID)
172 if( gpVM8086_State == (void*)-1 ) {
173 Log_Log("VM8086", "Worker thread ready and waiting");
174 gpVM8086_State = NULL;
175 Mutex_Release( &glVM8086_Process ); // Release lock obtained in VM8086_Install
177 //Log_Log("VM8086", "gpVM8086_State = %p, gVM8086_CallingThread = %i",
178 // gpVM8086_State, gVM8086_CallingThread);
179 if( gpVM8086_State ) {
180 gpVM8086_State->AX = Regs->eax; gpVM8086_State->CX = Regs->ecx;
181 gpVM8086_State->DX = Regs->edx; gpVM8086_State->BX = Regs->ebx;
182 gpVM8086_State->BP = Regs->ebp;
183 gpVM8086_State->SI = Regs->esi; gpVM8086_State->DI = Regs->edi;
184 gpVM8086_State->DS = Regs->ds; gpVM8086_State->ES = Regs->es;
185 gpVM8086_State = NULL;
187 Semaphore_Signal(&gVM8086_TaskComplete, 1);
190 //Log_Log("VM8086", "Waiting for something to do");
191 __asm__ __volatile__ ("sti");
192 Semaphore_Wait(&gVM8086_TasksToDo, 1);
194 //Log_Log("VM8086", "We have a task (%p)", gpVM8086_State);
195 Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = VM8086_MAGIC_CS;
196 Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = VM8086_MAGIC_IP;
197 Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->CS;
198 Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->IP;
199 Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->DS;
200 Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->ES;
203 Regs->eip = 0x11; Regs->cs = 0xFFFF;
204 Regs->eax = gpVM8086_State->AX; Regs->ecx = gpVM8086_State->CX;
205 Regs->edx = gpVM8086_State->DX; Regs->ebx = gpVM8086_State->BX;
206 Regs->esi = gpVM8086_State->SI; Regs->edi = gpVM8086_State->DI;
207 Regs->ebp = gpVM8086_State->BP;
208 Regs->ds = 0x23; Regs->es = 0x23;
209 Regs->fs = 0x23; Regs->gs = 0x23;
213 opcode = *(Uint8*)( (Regs->cs*16) + (Regs->eip) );
217 case VM8086_OP_PUSHF: //PUSHF
219 *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->eflags & 0xFFFF;
221 Log_Debug("VM8086", "Emulated PUSHF");
224 case VM8086_OP_POPF: //POPF
225 Regs->eflags &= 0xFFFF0002;
226 Regs->eflags |= *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) & 0xFFFD; // Changing IF is not allowed
229 Log_Debug("VM8086", "Emulated POPF");
233 case VM8086_OP_INT_I: //INT imm8
236 id = *(Uint8*)( Regs->cs*16 +(Regs->eip&0xFFFF));
239 Regs->esp -= 2; *(Uint16*volatile)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->cs;
240 Regs->esp -= 2; *(Uint16*volatile)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->eip;
242 Regs->cs = *(Uint16*)(4*id + 2);
243 Regs->eip = *(Uint16*)(4*id);
245 Log_Debug("VM8086", "Emulated INT 0x%x", id);
250 case VM8086_OP_IRET: //IRET
251 Regs->eip = *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ); Regs->esp += 2;
252 Regs->cs = *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ); Regs->esp += 2;
254 Log_Debug("VM8086", "IRET to %04x:%04x", Regs->cs, Regs->eip);
259 case VM8086_OP_IN_AD: //IN AL, DX
260 Regs->eax &= 0xFFFFFF00;
261 Regs->eax |= inb(Regs->edx&0xFFFF);
263 Log_Debug("VM8086", "Emulated IN AL, DX (Port 0x%x)\n", Regs->edx&0xFFFF);
266 case VM8086_OP_IN_ADX: //IN AX, DX
267 Regs->eax &= 0xFFFF0000;
268 Regs->eax |= inw(Regs->edx&0xFFFF);
270 Log_Debug("VM8086", "Emulated IN AX, DX (Port 0x%x)\n", Regs->edx&0xFFFF);
274 case VM8086_OP_OUT_AD: //OUT DX, AL
275 outb(Regs->edx&0xFFFF, Regs->eax&0xFF);
277 Log_Debug("VM8086", "Emulated OUT DX, AL (*0x%04x = 0x%02x)\n", Regs->edx&0xFFFF, Regs->eax&0xFF);
280 case VM8086_OP_OUT_ADX: //OUT DX, AX
281 outw(Regs->edx&0xFFFF, Regs->eax&0xFFFF);
283 Log_Debug("VM8086", "Emulated OUT DX, AX (*0x%04x = 0x%04x)\n", Regs->edx&0xFFFF, Regs->eax&0xFFFF);
287 // TODO: Decide on allowing VM8086 Apps to enable/disable interrupts
294 opcode = *(Uint8*)( (Regs->cs*16) + (Regs->eip&0xFFFF));
297 case VM8086_OP_IN_ADX: //IN AX, DX
298 Regs->eax = ind(Regs->edx&0xFFFF);
300 Log_Debug("VM8086", "Emulated IN EAX, DX (Port 0x%x)\n", Regs->edx&0xFFFF);
303 case VM8086_OP_OUT_ADX: //OUT DX, AX
304 outd(Regs->edx&0xFFFF, Regs->eax);
306 Log_Debug("VM8086", "Emulated OUT DX, EAX (*0x%04x = 0x%08x)\n", Regs->edx&0xFFFF, Regs->eax);
310 Log_Error("VM8086", "Error - Unknown opcode 66 %02x caused a GPF at %04x:%04x",
314 // Force an end to the call
315 Regs->cs = VM8086_MAGIC_CS;
316 Regs->eip = VM8086_MAGIC_IP;
322 Log_Error("VM8086", "Error - Unknown opcode %02x caused a GPF at %04x:%04x",
323 opcode, Regs->cs, Regs->eip);
324 // Force an end to the call
325 Regs->cs = VM8086_MAGIC_CS;
326 Regs->eip = VM8086_MAGIC_IP;
332 * \brief Create an instance of the VM8086 Emulator
334 tVM8086 *VM8086_Init(void)
337 ret = calloc( 1, sizeof(tVM8086) + sizeof(struct sVM8086_InternalData) );
338 ret->Internal = (void*)((tVAddr)ret + sizeof(tVM8086));
342 void VM8086_Free(tVM8086 *State)
345 for( i = VM8086_PAGES_PER_INST; i --; )
346 MM_UnmapHWPages( State->Internal->AllocatedPages[i].VirtBase, 1);
350 void *VM8086_Allocate(tVM8086 *State, int Size, Uint16 *Segment, Uint16 *Offset)
355 Size = (Size + 127) & ~127;
356 nBlocks = Size / 128;
358 if(Size > 4096) return NULL;
360 for( i = 0; i < VM8086_PAGES_PER_INST; i++ )
362 if( State->Internal->AllocatedPages[i].VirtBase == 0 ) continue;
365 //Log_Debug("VM8086", "AllocatedPages[%i].Bitmap = 0b%b", i, State->Internal->AllocatedPages[i].Bitmap);
369 // Scan the bitmap for a free block
370 for( j = 0; j < 32; j++ ) {
371 if( State->Internal->AllocatedPages[i].Bitmap & (1 << j) ) {
377 if(rem == 0) // Goodie, there's a gap
379 for( j = 0; j < nBlocks; j++ )
380 State->Internal->AllocatedPages[i].Bitmap |= 1 << (base + j);
381 *Segment = State->Internal->AllocatedPages[i].PhysAddr / 16 + base * 8;
383 LOG("Allocated at #%i,%04x", i, base*128);
384 LOG(" - %x:%x", *Segment, *Offset);
385 return (void*)( State->Internal->AllocatedPages[i].VirtBase + base * 128 );
390 // No pages with free space?, allocate a new one
391 for( i = 0; i < VM8086_PAGES_PER_INST; i++ )
393 if( State->Internal->AllocatedPages[i].VirtBase == 0 ) break;
395 // Darn, we can't allocate any more
396 if( i == VM8086_PAGES_PER_INST ) {
397 Log_Warning("VM8086", "Out of pages in %p", State);
401 State->Internal->AllocatedPages[i].VirtBase = MM_AllocDMA(
402 1, 20, &State->Internal->AllocatedPages[i].PhysAddr);
403 State->Internal->AllocatedPages[i].Bitmap = 0;
405 for( j = 0; j < nBlocks; j++ )
406 State->Internal->AllocatedPages[i].Bitmap |= 1 << j;
407 LOG("AllocatedPages[%i].Bitmap = 0b%b", i, State->Internal->AllocatedPages[i].Bitmap);
408 *Segment = State->Internal->AllocatedPages[i].PhysAddr / 16;
410 LOG(" - %x:%x", *Segment, *Offset);
411 return (void*) State->Internal->AllocatedPages[i].VirtBase;
414 void *VM8086_GetPointer(tVM8086 *State, Uint16 Segment, Uint16 Offset)
416 return (void*)( KERNEL_BASE + Segment*16 + Offset );
419 void VM8086_Int(tVM8086 *State, Uint8 Interrupt)
421 State->IP = *(Uint16*)(KERNEL_BASE+4*Interrupt);
422 State->CS = *(Uint16*)(KERNEL_BASE+4*Interrupt+2);
424 // Log_Debug("VM8086", "Software interrupt %i to %04x:%04x", Interrupt, State->CS, State->IP);
426 Mutex_Acquire( &glVM8086_Process );
428 gpVM8086_State = State;
429 gVM8086_CallingThread = Threads_GetTID();
430 Semaphore_Signal(&gVM8086_TasksToDo, 1);
432 Semaphore_Wait(&gVM8086_TaskComplete, 1);
434 Mutex_Release( &glVM8086_Process );