Kernel/armv7 - Added IRQ stack, changed IRQs to be disabled until after handling
[tpg/acess2.git] / Kernel / arch / x86 / vm8086.c
1 /*
2  * Acess2 VM8086 Driver
3  * - By John Hodge (thePowersGang)
4  */
5 #define DEBUG   0
6 #include <acess.h>
7 #include <vm8086.h>
8 #include <modules.h>
9 #include <hal_proc.h>
10 #include <semaphore.h>
11
12 // === CONSTANTS ===
13 #define VM8086_MAGIC_CS 0xFFFF
14 #define VM8086_MAGIC_IP 0x0010
15 #define VM8086_STACK_SEG        0x9F00
16 #define VM8086_STACK_OFS        0x0AFE
17 enum eVM8086_Opcodes
18 {
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
27 };
28 #define VM8086_PAGES_PER_INST   4
29
30 #define VM8086_BLOCKSIZE        128
31 #define VM8086_BLOCKCOUNT       ((0x9F000-0x10000)/VM8086_BLOCKSIZE)
32
33 // === TYPES ===
34 struct sVM8086_InternalData
35 {
36         struct {
37                 Uint32  Bitmap; // 32 sections = 128 byte blocks
38                 tVAddr  VirtBase;
39                 tPAddr  PhysAddr;
40         }       AllocatedPages[VM8086_PAGES_PER_INST];
41 };
42
43 // === PROTOTYPES ===
44  int    VM8086_Install(char **Arguments);
45 void    VM8086_GPF(tRegs *Regs);
46 //tVM8086       *VM8086_Init(void);
47
48 // === GLOBALS ===
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];
57
58 // === FUNCTIONS ===
59 int VM8086_Install(char **Arguments)
60 {
61         tPID    pid;    
62         
63         // Lock to avoid race conditions
64         Mutex_Acquire( &glVM8086_Process );
65         
66         // Create BIOS Call process
67         pid = Proc_Clone(CLONE_VM);
68         if(pid == -1)
69         {
70                 Log_Error("VM8086", "Unable to clone kernel into VM8086 worker");
71                 return MODULE_ERR_MISC;
72         }
73         if(pid == 0)
74         {
75                 Uint    * volatile stacksetup;  // Initialising Stack
76                 Uint16  * volatile rmstack;     // Real Mode Stack
77                  int    i;
78                 
79                 // Set Image Name
80                 Threads_SetName("VM8086");
81
82                 Log_Debug("VM8086", "Mapping memory");  
83         
84                 // Map ROM Area
85                 for(i=0xA0;i<0x100;i++) {
86                         MM_Map( i * 0x1000, i * 0x1000 );
87                 }
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
92                 for(i=1;i<0x9F;i++) {
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
97                 }
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;
103                         Threads_Exit(0, 1);
104                 }
105                 Log_Debug("VM8086", "Mapped low memory");
106                 
107                 *(Uint8*)(0x100000) = VM8086_OP_IRET;
108                 *(Uint8*)(0x100001) = 0x07;     // POP ES
109                 *(Uint8*)(0x100002) = 0x1F;     // POP DS
110                 *(Uint8*)(0x100003) = 0xCB;     // RET FAR
111                 
112                 rmstack = (Uint16*)(VM8086_STACK_SEG*16 + VM8086_STACK_OFS);
113                 rmstack--;      *rmstack = 0xFFFF;      //CS
114                 rmstack--;      *rmstack = 0x0010;      //IP
115                 
116                 // Setup Stack
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
141                 "pop %%gs;\n\t"
142                 "pop %%fs;\n\t"
143                 "pop %%es;\n\t"
144                 "pop %%ds;\n\t"
145                 "popa;\n\t"
146                 "iret;\n\t" : : "a" (stacksetup));
147                 for(;;);        // Shouldn't be reached
148         }
149         
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
154         
155         // Worker killed itself
156         if( gVM8086_WorkerPID != pid ) {
157                 return MODULE_ERR_MISC;
158         }
159         
160         return MODULE_ERR_OK;
161 }
162
163 void VM8086_GPF(tRegs *Regs)
164 {
165         Uint8   opcode;
166         
167         //Log_Log("VM8086", "GPF - %04x:%04x", Regs->cs, Regs->eip);
168         
169         if(Regs->eip == VM8086_MAGIC_IP && Regs->cs == VM8086_MAGIC_CS
170         && Threads_GetPID() == gVM8086_WorkerPID)
171         {
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
176                 }
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;
186                         // Wake the caller
187                         Semaphore_Signal(&gVM8086_TaskComplete, 1);
188                 }
189                 
190                 //Log_Log("VM8086", "Waiting for something to do");
191                 __asm__ __volatile__ ("sti");
192                 Semaphore_Wait(&gVM8086_TasksToDo, 1);
193                 
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;
201                 
202                 // Set Registers
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;
210                 return ;
211         }
212         
213         opcode = *(Uint8*)( (Regs->cs*16) + (Regs->eip) );
214         Regs->eip ++;
215         switch(opcode)
216         {
217         case VM8086_OP_PUSHF:   //PUSHF
218                 Regs->esp -= 2;
219                 *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->eflags & 0xFFFF;
220                 #if TRACE_EMU
221                 Log_Debug("VM8086", "Emulated PUSHF");
222                 #endif
223                 break;
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
227                 Regs->esp += 2;
228                 #if TRACE_EMU
229                 Log_Debug("VM8086", "Emulated POPF");
230                 #endif
231                 break;
232         
233         case VM8086_OP_INT_I:   //INT imm8
234                 {
235                  int    id;
236                 id = *(Uint8*)( Regs->cs*16 +(Regs->eip&0xFFFF));
237                 Regs->eip ++;
238                 
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;
241                 
242                 Regs->cs = *(Uint16*)(4*id + 2);
243                 Regs->eip = *(Uint16*)(4*id);
244                 #if TRACE_EMU
245                 Log_Debug("VM8086", "Emulated INT 0x%x", id);
246                 #endif
247                 }
248                 break;
249         
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;
253                 #if TRACE_EMU
254                 Log_Debug("VM8086", "IRET to %04x:%04x", Regs->cs, Regs->eip);
255                 #endif
256                 break;
257         
258         
259         case VM8086_OP_IN_AD:   //IN AL, DX
260                 Regs->eax &= 0xFFFFFF00;
261                 Regs->eax |= inb(Regs->edx&0xFFFF);
262                 #if TRACE_EMU
263                 Log_Debug("VM8086", "Emulated IN AL, DX (Port 0x%x)\n", Regs->edx&0xFFFF);
264                 #endif
265                 break;
266         case VM8086_OP_IN_ADX:  //IN AX, DX
267                 Regs->eax &= 0xFFFF0000;
268                 Regs->eax |= inw(Regs->edx&0xFFFF);
269                 #if TRACE_EMU
270                 Log_Debug("VM8086", "Emulated IN AX, DX (Port 0x%x)\n", Regs->edx&0xFFFF);
271                 #endif
272                 break;
273                 
274         case VM8086_OP_OUT_AD:  //OUT DX, AL
275                 outb(Regs->edx&0xFFFF, Regs->eax&0xFF);
276                 #if TRACE_EMU
277                 Log_Debug("VM8086", "Emulated OUT DX, AL (*0x%04x = 0x%02x)\n", Regs->edx&0xFFFF, Regs->eax&0xFF);
278                 #endif
279                 break;
280         case VM8086_OP_OUT_ADX: //OUT DX, AX
281                 outw(Regs->edx&0xFFFF, Regs->eax&0xFFFF);
282                 #if TRACE_EMU
283                 Log_Debug("VM8086", "Emulated OUT DX, AX (*0x%04x = 0x%04x)\n", Regs->edx&0xFFFF, Regs->eax&0xFFFF);
284                 #endif
285                 break;
286                 
287         // TODO: Decide on allowing VM8086 Apps to enable/disable interrupts
288         case 0xFA:      //CLI
289                 break;
290         case 0xFB:      //STI
291                 break;
292         
293         case 0x66:
294                 opcode = *(Uint8*)( (Regs->cs*16) + (Regs->eip&0xFFFF));
295                 switch( opcode )
296                 {
297                 case VM8086_OP_IN_ADX:  //IN AX, DX
298                         Regs->eax = ind(Regs->edx&0xFFFF);
299                         #if TRACE_EMU
300                         Log_Debug("VM8086", "Emulated IN EAX, DX (Port 0x%x)\n", Regs->edx&0xFFFF);
301                         #endif
302                         break;
303                 case VM8086_OP_OUT_ADX: //OUT DX, AX
304                         outd(Regs->edx&0xFFFF, Regs->eax);
305                         #if TRACE_EMU
306                         Log_Debug("VM8086", "Emulated OUT DX, EAX (*0x%04x = 0x%08x)\n", Regs->edx&0xFFFF, Regs->eax);
307                         #endif
308                         break;
309                 default:
310                         Log_Error("VM8086", "Error - Unknown opcode 66 %02x caused a GPF at %04x:%04x",
311                                 Regs->cs, Regs->eip,
312                                 opcode
313                                 );
314                         // Force an end to the call
315                         Regs->cs = VM8086_MAGIC_CS;
316                         Regs->eip = VM8086_MAGIC_IP;
317                         break;
318                 }
319                 break;
320         
321         default:
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;
327                 break;
328         }
329 }
330
331 /**
332  * \brief Create an instance of the VM8086 Emulator
333  */
334 tVM8086 *VM8086_Init(void)
335 {
336         tVM8086 *ret;
337         ret = calloc( 1, sizeof(tVM8086) + sizeof(struct sVM8086_InternalData) );
338         ret->Internal = (void*)((tVAddr)ret + sizeof(tVM8086));
339         return ret;
340 }
341
342 void VM8086_Free(tVM8086 *State)
343 {
344          int    i;
345         for( i = VM8086_PAGES_PER_INST; i --; )
346                 MM_UnmapHWPages( State->Internal->AllocatedPages[i].VirtBase, 1);
347         free(State);
348 }
349
350 void *VM8086_Allocate(tVM8086 *State, int Size, Uint16 *Segment, Uint16 *Offset)
351 {
352          int    i, j, base = 0;
353          int    nBlocks, rem;
354         
355         Size = (Size + 127) & ~127;
356         nBlocks = Size / 128;
357         
358         if(Size > 4096) return NULL;
359         
360         for( i = 0; i < VM8086_PAGES_PER_INST; i++ )
361         {
362                 if( State->Internal->AllocatedPages[i].VirtBase == 0 )  continue;
363                 
364                 
365                 //Log_Debug("VM8086", "AllocatedPages[%i].Bitmap = 0b%b", i, State->Internal->AllocatedPages[i].Bitmap);
366                 
367                 rem = nBlocks;
368                 base = 0;
369                 // Scan the bitmap for a free block
370                 for( j = 0; j < 32; j++ ) {
371                         if( State->Internal->AllocatedPages[i].Bitmap & (1 << j) ) {
372                                 base = j+1;
373                                 rem = nBlocks;
374                         }
375                         
376                         rem --;
377                         if(rem == 0)    // Goodie, there's a gap
378                         {
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;
382                                 *Offset = 0;
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 );
386                         }
387                 }
388         }
389         
390         // No pages with free space?, allocate a new one
391         for( i = 0; i < VM8086_PAGES_PER_INST; i++ )
392         {
393                 if( State->Internal->AllocatedPages[i].VirtBase == 0 )  break;
394         }
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);
398                 return NULL;
399         }
400         
401         State->Internal->AllocatedPages[i].VirtBase = MM_AllocDMA(
402                 1, 20, &State->Internal->AllocatedPages[i].PhysAddr);
403         State->Internal->AllocatedPages[i].Bitmap = 0;
404                 
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;
409         *Offset = 0;
410         LOG(" - %x:%x", *Segment, *Offset);
411         return (void*) State->Internal->AllocatedPages[i].VirtBase;
412 }
413
414 void *VM8086_GetPointer(tVM8086 *State, Uint16 Segment, Uint16 Offset)
415 {
416         return (void*)( KERNEL_BASE + Segment*16 + Offset );
417 }
418
419 void VM8086_Int(tVM8086 *State, Uint8 Interrupt)
420 {
421         State->IP = *(Uint16*)(KERNEL_BASE+4*Interrupt);
422         State->CS = *(Uint16*)(KERNEL_BASE+4*Interrupt+2);
423
424 //      Log_Debug("VM8086", "Software interrupt %i to %04x:%04x", Interrupt, State->CS, State->IP);
425         
426         Mutex_Acquire( &glVM8086_Process );
427         
428         gpVM8086_State = State;
429         gVM8086_CallingThread = Threads_GetTID();
430         Semaphore_Signal(&gVM8086_TasksToDo, 1);
431
432         Semaphore_Wait(&gVM8086_TaskComplete, 1);
433         
434         Mutex_Release( &glVM8086_Process );
435 }

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