// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
// All rights reserved.
// This component and the accompanying materials are made available
// under the terms of the License "Eclipse Public License v1.0"
// which accompanies this distribution, and is available
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
//
// Initial Contributors:
// Nokia Corporation - initial contribution.
//
// Contributors:
//
// Description:
// e32\euser\epoc\x86\uc_exe.cia
// 
//

#include <u32std.h>
#include "x86hlp.inl"

static const TLinAddr User_HandleException = (TLinAddr)&User::HandleException;
static const TLinAddr User_Invariant = (TLinAddr)&User::Invariant;

extern "C" {
void __fastcall RunThread(TBool aNotFirst, SThreadCreateInfo& aInfo);

// NOTE: This MUST be the first function in this module
__NAKED__ TInt _E32Startup()
	{
	// Process entry point
	// EBX = entry reason
	// ESP points to information block
	BYTE(0xeb)			// JMP .+16
	BYTE(0x0e)
	BYTE(0x00)
	BYTE(0x00)
	BYTE(0x00)
	BYTE(0x00)
	BYTE(0x00)
	BYTE(0x00)
	BYTE(0x00)
	BYTE(0x00)
	BYTE(0x00)
	BYTE(0x00)
	BYTE(0x00)			// need zero word at offset 12 from entry point
	BYTE(0x00)
	BYTE(0x00)
	BYTE(0x00)

	asm("cmp ebx, 2");
	asm("jae not_thread_entry");
	asm("mov ecx, ebx");
	asm("mov edx, esp");
	asm("jmp %a0": :"i"(&RunThread));

	asm("not_thread_entry:");
	asm("cmp ebx, 4");
	asm("jne invalid_entry");
	asm("mov eax, esp");
	asm("push eax");
	asm("mov eax, %0": : "i"(User_HandleException));
	asm("call eax");
	asm("pop eax");	// parameter
	asm("pop eax");	// exc type
	asm("pop eax");	// exc id
	asm("pop eax");	// exc error code
	asm("pop eax");	// fault address
	asm("pop eax");
	asm("pop ecx");
	asm("pop edx");
	asm("pop ebx");
	asm("pop esi");	// ESP - ignore
	asm("pop ebp");
	asm("pop esi");
	asm("pop edi");
	asm("add esp, 4");	// SS - ignore
	asm("pop ds");
	asm("pop es");
	asm("pop fs");
	asm("pop gs");
	asm("popfd");
	BYTE(0xcb) //asm("retf"); GCC doesn't recognise retf

	asm("invalid_entry:");
	asm("call %a0": : "i"(User_Invariant));
	}

}


