93ea63ae5b6022fdb6feff0a31d489fc6439b61b
[tpg/acess2.git] / vm8086.c
1 /*
2  * Acess2 Kernel (x86)
3  * - By John Hodge (thePowersGang)
4  *
5  * vm8086.c
6  * - Virtual 8086 Mode Monitor
7  */
8 #define DEBUG   0
9 #include <acess.h>
10 #include <vm8086.h>
11 #include <modules.h>
12 #include <hal_proc.h>
13 #include <semaphore.h>
14
15 // === CONSTANTS ===
16 #define TRACE_EMU       0
17
18 #define VM8086_USER_BASE        0x1000
19
20 #define VM8086_MAGIC_CS 0xFFFF
21 #define VM8086_MAGIC_IP 0x0010
22 #define VM8086_STACK_SEG        0x9F00
23 #define VM8086_STACK_OFS        0x0AFE
24 enum eVM8086_Opcodes
25 {
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
34 };
35 #define VM8086_PAGES_PER_INST   4
36
37 #define VM8086_BLOCKSIZE        128
38 #define VM8086_BLOCKCOUNT       ((0x9F000-0x10000)/VM8086_BLOCKSIZE)
39
40 // === TYPES ===
41 struct sVM8086_InternalPages
42 {
43         Uint32  Bitmap; // 32 sections = 128 byte blocks
44         char    *VirtBase;
45         tPAddr  PhysAddr;
46 };
47 struct sVM8086_InternalData
48 {
49         struct sVM8086_InternalPages    AllocatedPages[VM8086_PAGES_PER_INST];
50 };
51
52 // === PROTOTYPES ===
53  int    VM8086_Install(char **Arguments);
54 void    VM8086_GPF(tRegs *Regs);
55 //tVM8086       *VM8086_Init(void);
56
57 // === GLOBALS ===
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;
67
68 // === FUNCTIONS ===
69 int VM8086_Install(char **Arguments)
70 {
71         tPID    pid;    
72
73         Semaphore_Init(&gVM8086_TasksToDo, 0, 10, "VM8086", "TasksToDo");
74         
75         // Lock to avoid race conditions
76         Mutex_Acquire( &glVM8086_Process );
77         
78         // Create BIOS Call process
79         pid = Proc_Clone(CLONE_VM);
80         //Log_Debug("VM8086", "pid = %i", pid);
81         if(pid == -1)
82         {
83                 Log_Error("VM8086", "Unable to clone kernel into VM8086 worker");
84                 return MODULE_ERR_MISC;
85         }
86         if(pid == 0)
87         {
88                 Uint    * volatile stacksetup;  // Initialising Stack
89                 Uint16  * volatile rmstack;     // Real Mode Stack
90                  int    i;
91
92                 //Log_Debug("VM8086", "Initialising worker");   
93         
94                 // Set Image Name
95                 Threads_SetName("VM8086");
96
97                 // Map ROM Area
98                 for(i=0xA0;i<0x100;i++) {
99                         MM_RefPhys(i * 0x1000);
100                         MM_Map( (void*)(i * 0x1000), i * 0x1000 );
101                 }
102                 MM_RefPhys(0);
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));
107                 }
108                 MM_RefPhys(0x9F000);
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));
113                 }
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;
118                         Threads_Exit(0, 1);
119                 }
120                 
121                 *(Uint8*)(0x100000) = VM8086_OP_IRET;
122                 *(Uint8*)(0x100001) = 0x07;     // POP ES
123                 *(Uint8*)(0x100002) = 0x1F;     // POP DS
124                 *(Uint8*)(0x100003) = 0xCB;     // RET FAR
125                 
126                 rmstack = (Uint16*)(VM8086_STACK_SEG*16 + VM8086_STACK_OFS);
127                 rmstack--;      *rmstack = 0xFFFF;      //CS
128                 rmstack--;      *rmstack = 0x0010;      //IP
129                 
130                 // Setup Stack
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
155                 "pop %%gs;\n\t"
156                 "pop %%fs;\n\t"
157                 "pop %%es;\n\t"
158                 "pop %%ds;\n\t"
159                 "popa;\n\t"
160                 "iret;\n\t" : : "a" (stacksetup));
161                 for(;;);        // Shouldn't be reached
162         }
163         
164         gVM8086_WorkerPID = pid;
165
166         // It's released when the GPF fires
167         Mutex_Acquire( &glVM8086_Process );
168         Mutex_Release( &glVM8086_Process );
169         
170         // Worker killed itself
171         if( gVM8086_WorkerPID != pid ) {
172                 return MODULE_ERR_MISC;
173         }
174         
175         return MODULE_ERR_OK;
176 }
177
178 void VM8086_GPF(tRegs *Regs)
179 {
180         Uint8   opcode;
181         Uint16  newcs, newip;
182         
183 //      Log_Log("VM8086", "GPF - %04x:%04x", Regs->cs, Regs->eip);
184
185         LOG("VM8086 GPF at %04x:%04x", Regs->cs, Regs->eip);
186
187         if(Regs->eip == VM8086_MAGIC_IP && Regs->cs == VM8086_MAGIC_CS
188         && Threads_GetPID() == gVM8086_WorkerPID)
189         {
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
194                 }
195 //              Log_Log("VM8086", "gpVM8086_State = %p, gVM8086_CallingThread = %i",
196 //                      gpVM8086_State, gVM8086_CallingThread);
197                 if( gpVM8086_State )
198                 {
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;
204
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 ++ )
208                         {
209                                 if( !gpVM8086_State->Internal->AllocatedPages[i].VirtBase )
210                                         continue ;
211                                 MM_Deallocate( (tPage*)VM8086_USER_BASE + i );
212                         }
213
214                         gpVM8086_State = NULL;
215                                 
216                         // Wake the caller
217                         Semaphore_Signal(&gVM8086_TaskComplete, 1);
218                 }
219                 
220                 //Log_Log("VM8086", "Waiting for something to do");
221                 __asm__ __volatile__ ("sti");
222                 Semaphore_Wait(&gVM8086_TasksToDo, 1);
223                 
224                 for( Uint i = 0; i < VM8086_PAGES_PER_INST; i ++ )
225                 {
226                         if( !gpVM8086_State->Internal->AllocatedPages[i].VirtBase )
227                                 continue ;
228                         MM_RefPhys( gpVM8086_State->Internal->AllocatedPages[i].PhysAddr );
229                         MM_Map( (tPage*)VM8086_USER_BASE + i, gpVM8086_State->Internal->AllocatedPages[i].PhysAddr );
230                 }
231
232                 
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;
240                 
241                 // Set Registers
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;
249                 return ;
250         }
251         
252         opcode = *(Uint8*)( (Regs->cs*16) + (Regs->eip) );
253         Regs->eip ++;
254         switch(opcode)
255         {
256         case VM8086_OP_PUSHF:   //PUSHF
257                 Regs->esp -= 2;
258                 *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->eflags & 0xFFFF;
259                 if( gbVM8086_ShadowIF )
260                         *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) |= 0x200;
261                 else
262                         *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) &= ~0x200;
263                 #if TRACE_EMU
264                 Log_Debug("VM8086", "%04x:%04x Emulated PUSHF (value 0x%x)",
265                         Regs->cs, Regs->eip-1, Regs->eflags & 0xFFFF);
266                 #endif
267                 break;
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);
273                 Regs->esp += 2;
274                 #if TRACE_EMU
275                 Log_Debug("VM8086", "%04x:%04x Emulated POPF (new value 0x%x)",
276                         Regs->cs, Regs->eip-1, Regs->eflags & 0xFFFF);
277                 #endif
278                 break;
279         
280         case VM8086_OP_INT_I:   //INT imm8
281                 {
282                  int    id;
283                 id = *(Uint8*)( Regs->cs*16 +(Regs->eip&0xFFFF));
284                 Regs->eip ++;
285                 
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;
289                 
290                 newcs = *(Uint16*)(4*id + 2);
291                 newip = *(Uint16*)(4*id);
292                 #if TRACE_EMU
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);
295                 #endif
296                 Regs->cs = newcs;
297                 Regs->eip = newip;
298                 }
299                 break;
300         
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;
304                 #if TRACE_EMU
305                 Log_Debug("VM8086", "%04x:%04x IRET to %04x:%04x",
306                         Regs->cs, Regs->eip-1, newcs, newip);
307                 #endif
308                 Regs->cs = newcs;
309                 Regs->eip = newip;
310                 break;
311         
312         
313         case VM8086_OP_IN_AD:   //IN AL, DX
314                 Regs->eax &= 0xFFFFFF00;
315                 Regs->eax |= inb(Regs->edx&0xFFFF);
316                 #if TRACE_EMU
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);
319                 #endif
320                 break;
321         case VM8086_OP_IN_ADX:  //IN AX, DX
322                 Regs->eax &= 0xFFFF0000;
323                 Regs->eax |= inw(Regs->edx&0xFFFF);
324                 #if TRACE_EMU
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);
327                 #endif
328                 break;
329                 
330         case VM8086_OP_OUT_AD:  //OUT DX, AL
331                 outb(Regs->edx&0xFFFF, Regs->eax&0xFF);
332                 #if TRACE_EMU
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);
335                 #endif
336                 break;
337         case VM8086_OP_OUT_ADX: //OUT DX, AX
338                 outw(Regs->edx&0xFFFF, Regs->eax&0xFFFF);
339                 #if TRACE_EMU
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);
342                 #endif
343                 break;
344                 
345         // TODO: Decide on allowing VM8086 Apps to enable/disable interrupts
346         case 0xFA:      //CLI
347                 #if TRACE_EMU
348                 Log_Debug("VM8086", "%04x:%04x Ignored CLI",
349                         Regs->cs, Regs->eip);
350                 #endif
351                 gbVM8086_ShadowIF = 0;
352                 break;
353         case 0xFB:      //STI
354                 #if TRACE_EMU
355                 Log_Debug("VM8086", "%04x:%04x Ignored STI",
356                         Regs->cs, Regs->eip);
357                 #endif
358                 gbVM8086_ShadowIF = 1;
359                 break;
360         
361         case 0x66:
362                 opcode = *(Uint8*)( (Regs->cs*16) + (Regs->eip&0xFFFF));
363                 Regs->eip ++;
364                 switch( opcode )
365                 {
366                 case VM8086_OP_IN_ADX:  //IN AX, DX
367                         Regs->eax = ind(Regs->edx&0xFFFF);
368                         #if TRACE_EMU
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);
371                         #endif
372                         break;
373                 case VM8086_OP_OUT_ADX: //OUT DX, AX
374                         outd(Regs->edx&0xFFFF, Regs->eax);
375                         #if TRACE_EMU
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);
378                         #endif
379                         break;
380                 default:
381                         Log_Error("VM8086", "Error - Unknown opcode 66 %02x caused a GPF at %04x:%04x",
382                                 Regs->cs, Regs->eip-2,
383                                 opcode
384                                 );
385                         // Force an end to the call
386                         Regs->cs = VM8086_MAGIC_CS;
387                         Regs->eip = VM8086_MAGIC_IP;
388                         break;
389                 }
390                 break;
391         
392         case 0x0F:
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;
399                 break;
400
401         default:
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;
407                 break;
408         }
409 }
410
411 /**
412  * \brief Create an instance of the VM8086 Emulator
413  */
414 tVM8086 *VM8086_Init(void)
415 {
416         tVM8086 *ret;
417         ret = calloc( 1, sizeof(tVM8086) + sizeof(struct sVM8086_InternalData) );
418         ret->Internal = (void*)((tVAddr)ret + sizeof(tVM8086));
419         return ret;
420 }
421
422 void VM8086_Free(tVM8086 *State)
423 {
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);
427         free(State);
428 }
429
430 void *VM8086_Allocate(tVM8086 *State, int Size, Uint16 *Segment, Uint16 *Offset)
431 {
432         struct sVM8086_InternalPages    *pages = State->Internal->AllocatedPages;
433          int    i, j, base = 0;
434          int    nBlocks, rem;
435         
436         Size = (Size + 127) & ~127;
437         nBlocks = Size / 128;
438         
439         if(Size > 4096) return NULL;
440         
441         for( i = 0; i < VM8086_PAGES_PER_INST; i++ )
442         {
443                 if( pages[i].VirtBase == 0 )    continue;
444                 
445                 //Log_Debug("VM8086", "pages[%i].Bitmap = 0b%b", i, pages[i].Bitmap);
446                 
447                 rem = nBlocks;
448                 base = 0;
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++ )
452                 {
453                         if( pages[i].Bitmap & (1 << j) )
454                         {
455                                 base = j+1;
456                                 rem = nBlocks;
457                         }
458                         
459                         rem --;
460                         if(rem == 0)    // Goodie, there's a gap
461                         {
462                                 for( j = 0; j < nBlocks; j++ )
463                                         pages[i].Bitmap |= 1 << (base + j);
464                                 *Segment = (VM8086_USER_BASE + i * 0x1000) / 16 + base * 8;
465                                 *Offset = 0;
466                                 LOG("Allocated at #%i,%04x", i, base*8*16);
467                                 LOG(" - %x:%x", *Segment, *Offset);
468                                 return pages[i].VirtBase + base * 8 * 16;
469                         }
470                 }
471         }
472         
473         // No pages with free space?, allocate a new one
474         for( i = 0; i < VM8086_PAGES_PER_INST; i++ )
475         {
476                 if( pages[i].VirtBase == 0 )    break;
477         }
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);
481                 return NULL;
482         }
483         
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");
487                 return NULL;
488         }
489         pages[i].Bitmap = 0;
490         LOG("AllocatedPages[%i].VirtBase = %p", i, pages[i].VirtBase);
491         LOG("AllocatedPages[%i].PhysAddr = %P", i, pages[i].PhysAddr);
492                 
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;
497         *Offset = 0;
498         LOG(" - %04x:%04x", *Segment, *Offset);
499         return pages[i].VirtBase;
500 }
501
502 void *VM8086_GetPointer(tVM8086 *State, Uint16 Segment, Uint16 Offset)
503 {
504         Uint32  addr = Segment * 16 + Offset;
505         
506         if( VM8086_USER_BASE <= addr && addr < VM8086_USER_BASE + VM8086_PAGES_PER_INST*0x1000 )
507         {
508                 int pg = (addr - VM8086_USER_BASE) / 0x1000;
509                 if( State->Internal->AllocatedPages[pg].VirtBase == 0)
510                         return NULL;
511                 else
512                         return State->Internal->AllocatedPages[pg].VirtBase + (addr & 0xFFF);
513         }
514         else
515         {
516                 return (void*)( KERNEL_BASE + addr );
517         }
518 }
519
520 void VM8086_Int(tVM8086 *State, Uint8 Interrupt)
521 {
522         State->IP = *(Uint16*)(KERNEL_BASE+4*Interrupt);
523         State->CS = *(Uint16*)(KERNEL_BASE+4*Interrupt+2);
524
525 //      Log_Debug("VM8086", "Software interrupt %i to %04x:%04x", Interrupt, State->CS, State->IP);
526         
527         Mutex_Acquire( &glVM8086_Process );
528         
529         gpVM8086_State = State;
530         gVM8086_CallingThread = Threads_GetTID();
531         Semaphore_Signal(&gVM8086_TasksToDo, 1);
532
533         Semaphore_Wait(&gVM8086_TaskComplete, 1);
534         
535         Mutex_Release( &glVM8086_Process );
536 }

UCC git Repository :: git.ucc.asn.au