// Copyright (c) 1996-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\kernel\arm\k_entry.cia
// 
//

#include <e32cia.h>
#include <kernel/kernel.h>
#include <arm_mem.h>

#include "entry.h"

extern "C" {

extern void KernelMain();


/*
 * The main startup program
 * aRomHeader is address of ROM header passed in by bootstrap
 * aSuperPage is address of super page passed in by bootstrap
 *
 * *** NOTE ***
 * This is written in assembler in order that kc_exe.lib can be built entirely
 * from one source file. This is necessary to ensure that the vector table above
 * appears at the very beginning of the ekern.exe code section.
 * ************
 */
#if defined(__GCC32__)
GLDEF_C __NAKED__ void _E32Startup(TLinAddr /*aRomHeader*/, TLinAddr /*aSuperPage*/)
	{
	EKA2_ENTRY_POINT_VERSION_IDENTIFIER;	// DUMMY INSTRUCTION TO INDICATE EKA2 ENTRY POINT
	asm("b 1f ");				// branch over space for unique ID
	asm(".word 0 ");			// padding

	asm(".word 0 ");			// loader will replace with code seg unique ID
								// for RAM-loaded code segment
								// MUST BE AT OFFSET 12 FROM ENTRY POINT

	asm("1: ");
	asm("bl " CSM_ZN2PP20InitSuperPageFromRom);

#ifdef	__CPU_HAS_CP15_THREAD_ID_REG
	asm("mov	r0, #0 ");
	SET_RWNO_TID(,r0);			// initialise SubScheduler() pointer to 0
	SET_RWRO_TID(,r0);
	SET_RWRW_TID(,r0);
#endif

	asm("ldr r4, __CtorList ");
	
	asm("1: ");
	asm("ldr r0, [r4, #4]! ");
	asm("adr lr, 1b ");
	asm("cmp r0, #0 ");
	__JUMP(ne,r0);

	asm("b KernelMain ");

	asm("__CtorList: ");
	asm(".word __CTOR_LIST__ ");
	}
#elif defined(__EABI__)
void __DLL_Export_Table__(void);
void __cpp_initialize__aeabi_();
  
// The compiler generates calls to this when it reckons a top-level construction
// needs destruction. But the kernel never will need this so, define it as a nop
void __record_needed_destruction (void * d){}
// 2.1 calls __aeabi_atexit passing __dso_handle. This can be a dummy (i.e. just a label)

__NAKED__ void __dso_handle(void) {}
void __aeabi_atexit(void *object, void (*dtor)(void *), void *handle){}

void _E32Startup_Body(TLinAddr aRomHeader, TLinAddr aSuperPage);

__NAKED__ void _E32Startup(TLinAddr aRomHeader, TLinAddr aSuperPage)
	{
	EKA2_ENTRY_POINT_VERSION_IDENTIFIER;	// DUMMY INSTRUCTION TO INDICATE EKA2 ENTRY POINT
	asm("b		1f ");
	asm(".word	0 ");			// padding

	asm(".word	0 ");			// loader will replace with code seg unique ID
								// for RAM-loaded code segment
								// MUST BE AT OFFSET 12 FROM ENTRY POINT
	asm("1:		");
#ifdef	__CPU_HAS_CP15_THREAD_ID_REG
	asm("mov	r12, #0 ");
	SET_RWNO_TID(,r12);			// initialise SubScheduler() pointer to 0
	SET_RWRO_TID(,r12);
	SET_RWRW_TID(,r12);
#endif
	asm("b		_E32Startup_Body ");
	}

GLDEF_C void _E32Startup_Body(TLinAddr aRomHeader, TLinAddr aSuperPage)
	{
	PP::InitSuperPageFromRom(aRomHeader, aSuperPage);

	// RVCT-specific initialisation

	// Make sure we get an export table
	__DLL_Export_Table__();

	// Initialise static data
	__cpp_initialize__aeabi_();

	KernelMain();
	}
#else
#error Not supported
#endif

}

