X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Farch%2Fx86%2Fvm8086.c;h=5253a62d430772200e82f3f89a371182b27d3edb;hb=2a05bcd81312a2885f824dac79e82c01a6e60c6c;hp=1816eacf1c1f5f98a8015986079ba37d1635f040;hpb=814b2d0009da73b56c6def5d70a9dd97c7b17e2e;p=tpg%2Facess2.git diff --git a/Kernel/arch/x86/vm8086.c b/Kernel/arch/x86/vm8086.c index 1816eacf..5253a62d 100644 --- a/Kernel/arch/x86/vm8086.c +++ b/Kernel/arch/x86/vm8086.c @@ -44,10 +44,10 @@ tVM8086 *VM8086_Init(void); // === GLOBALS === MODULE_DEFINE(0, 0x100, VM8086, VM8086_Install, NULL, NULL); -tSpinlock glVM8086_Process; +tMutex glVM8086_Process; tPID gVM8086_WorkerPID; tTID gVM8086_CallingThread; -tVM8086 * volatile gpVM8086_State = (void*)-1; // Set to -1 to avoid race conditions +tVM8086 volatile * volatile gpVM8086_State = (void*)-1; // Set to -1 to avoid race conditions // === FUNCTIONS === int VM8086_Install(char **Arguments) @@ -55,7 +55,7 @@ int VM8086_Install(char **Arguments) tPID pid; // Lock to avoid race conditions - LOCK( &glVM8086_Process ); + Mutex_Acquire( &glVM8086_Process ); // Create BIOS Call process pid = Proc_Clone(NULL, CLONE_VM); @@ -66,8 +66,8 @@ int VM8086_Install(char **Arguments) } if(pid == 0) { - Uint *stacksetup; // Initialising Stack - Uint16 *rmstack; // Real Mode Stack + Uint * volatile stacksetup; // Initialising Stack + Uint16 * volatile rmstack; // Real Mode Stack int i; // Set Image Name @@ -91,32 +91,32 @@ int VM8086_Install(char **Arguments) *(Uint8*)(0x100003) = 0xCB; // RET FAR rmstack = (Uint16*)(VM8086_STACK_SEG*16 + VM8086_STACK_OFS); - *rmstack-- = 0xFFFF; //CS - *rmstack-- = 0x0010; //IP + rmstack--; *rmstack = 0xFFFF; //CS + rmstack--; *rmstack = 0x0010; //IP // Setup Stack stacksetup = (Uint*)0x101000; - *--stacksetup = VM8086_STACK_SEG; // GS - *--stacksetup = VM8086_STACK_SEG; // FS - *--stacksetup = VM8086_STACK_SEG; // DS - *--stacksetup = VM8086_STACK_SEG; // ES - *--stacksetup = VM8086_STACK_SEG; // SS - *--stacksetup = VM8086_STACK_OFS-2; // SP - *--stacksetup = 0x20202; // FLAGS - *--stacksetup = 0xFFFF; // CS - *--stacksetup = 0x10; // IP - *--stacksetup = 0xAAAA; // AX - *--stacksetup = 0xCCCC; // CX - *--stacksetup = 0xDDDD; // DX - *--stacksetup = 0xBBBB; // BX - *--stacksetup = 0x5454; // SP - *--stacksetup = 0xB4B4; // BP - *--stacksetup = 0x5151; // SI - *--stacksetup = 0xD1D1; // DI - *--stacksetup = 0x20|3; // DS - Kernel - *--stacksetup = 0x20|3; // ES - Kernel - *--stacksetup = 0x20|3; // FS - *--stacksetup = 0x20|3; // GS + stacksetup--; *stacksetup = VM8086_STACK_SEG; // GS + stacksetup--; *stacksetup = VM8086_STACK_SEG; // FS + stacksetup--; *stacksetup = VM8086_STACK_SEG; // DS + stacksetup--; *stacksetup = VM8086_STACK_SEG; // ES + stacksetup--; *stacksetup = VM8086_STACK_SEG; // SS + stacksetup--; *stacksetup = VM8086_STACK_OFS-2; // SP + stacksetup--; *stacksetup = 0x20202; // FLAGS + stacksetup--; *stacksetup = 0xFFFF; // CS + stacksetup--; *stacksetup = 0x10; // IP + stacksetup--; *stacksetup = 0xAAAA; // AX + stacksetup--; *stacksetup = 0xCCCC; // CX + stacksetup--; *stacksetup = 0xDDDD; // DX + stacksetup--; *stacksetup = 0xBBBB; // BX + stacksetup--; *stacksetup = 0x5454; // SP + stacksetup--; *stacksetup = 0xB4B4; // BP + stacksetup--; *stacksetup = 0x5151; // SI + stacksetup--; *stacksetup = 0xD1D1; // DI + stacksetup--; *stacksetup = 0x20|3; // DS - Kernel + stacksetup--; *stacksetup = 0x20|3; // ES - Kernel + stacksetup--; *stacksetup = 0x20|3; // FS + stacksetup--; *stacksetup = 0x20|3; // GS __asm__ __volatile__ ( "mov %%eax,%%esp;\n\t" // Set stack pointer "pop %%gs;\n\t" @@ -130,7 +130,8 @@ int VM8086_Install(char **Arguments) gVM8086_WorkerPID = pid; Log_Log("VM8086", "gVM8086_WorkerPID = %i", pid); - Threads_Yield(); // Yield to allow the child to initialise + while( gpVM8086_State != NULL ) + Threads_Yield(); // Yield to allow the child to initialise return MODULE_ERR_OK; } @@ -146,9 +147,11 @@ void VM8086_GPF(tRegs *Regs) { if( gpVM8086_State == (void*)-1 ) { Log_Log("VM8086", "Worker thread ready and waiting"); - RELEASE( &glVM8086_Process ); // Release lock obtained in VM8086_Install + Mutex_Release( &glVM8086_Process ); // Release lock obtained in VM8086_Install gpVM8086_State = NULL; } + //Log_Log("VM8086", "gpVM8086_State = %p, gVM8086_CallingThread = %i", + // gpVM8086_State, gVM8086_CallingThread); if( gpVM8086_State ) { gpVM8086_State->AX = Regs->eax; gpVM8086_State->CX = Regs->ecx; gpVM8086_State->DX = Regs->edx; gpVM8086_State->BX = Regs->ebx; @@ -156,7 +159,9 @@ void VM8086_GPF(tRegs *Regs) gpVM8086_State->SI = Regs->esi; gpVM8086_State->DI = Regs->edi; gpVM8086_State->DS = Regs->ds; gpVM8086_State->ES = Regs->es; gpVM8086_State = NULL; - Threads_WakeTID(gVM8086_CallingThread); + // Ensure the caller wakes + //while(Threads_WakeTID(gVM8086_CallingThread) == -EALREADY) + // Threads_Yield(); } //Log_Log("VM8086", "Waiting for something to do"); @@ -168,12 +173,12 @@ void VM8086_GPF(tRegs *Regs) } //Log_Log("VM8086", "We have a task (%p)", gpVM8086_State); - Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = VM8086_MAGIC_CS; - Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = VM8086_MAGIC_IP; - Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->CS; - Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->IP; - Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->DS; - Regs->esp -= 2; *(Uint16*)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->ES; + Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = VM8086_MAGIC_CS; + Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = VM8086_MAGIC_IP; + Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->CS; + Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->IP; + Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->DS; + Regs->esp -= 2; *(Uint16*volatile)( (Regs->ss<<4) + (Regs->esp&0xFFFF) ) = gpVM8086_State->ES; // Set Registers Regs->eip = 0x11; Regs->cs = 0xFFFF; @@ -212,8 +217,8 @@ void VM8086_GPF(tRegs *Regs) id = *(Uint8*)( Regs->cs*16 +(Regs->eip&0xFFFF)); Regs->eip ++; - Regs->esp -= 2; *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->cs; - Regs->esp -= 2; *(Uint16*)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->eip; + Regs->esp -= 2; *(Uint16*volatile)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->cs; + Regs->esp -= 2; *(Uint16*volatile)( Regs->ss*16 + (Regs->esp&0xFFFF) ) = Regs->eip; Regs->cs = *(Uint16*)(4*id + 2); Regs->eip = *(Uint16*)(4*id); @@ -396,13 +401,13 @@ void VM8086_Int(tVM8086 *State, Uint8 Interrupt) State->IP = *(Uint16*)(KERNEL_BASE+4*Interrupt); State->CS = *(Uint16*)(KERNEL_BASE+4*Interrupt+2); - LOCK( &glVM8086_Process ); + Mutex_Acquire( &glVM8086_Process ); gpVM8086_State = State; gVM8086_CallingThread = Threads_GetTID(); Threads_WakeTID( gVM8086_WorkerPID ); while( gpVM8086_State != NULL ) - Threads_Sleep(); + Threads_Yield(); - RELEASE( &glVM8086_Process ); + Mutex_Release( &glVM8086_Process ); }