| 0 |      1 | // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
 | 
|  |      2 | // All rights reserved.
 | 
|  |      3 | // This component and the accompanying materials are made available
 | 
|  |      4 | // under the terms of the License "Eclipse Public License v1.0"
 | 
|  |      5 | // which accompanies this distribution, and is available
 | 
|  |      6 | // at the URL "http://www.eclipse.org/legal/epl-v10.html".
 | 
|  |      7 | //
 | 
|  |      8 | // Initial Contributors:
 | 
|  |      9 | // Nokia Corporation - initial contribution.
 | 
|  |     10 | //
 | 
|  |     11 | // Contributors:
 | 
|  |     12 | //
 | 
|  |     13 | // Description:
 | 
|  |     14 | // e32\memmodel\epoc\direct\mcodeseg.cpp
 | 
|  |     15 | // 
 | 
|  |     16 | //
 | 
|  |     17 | 
 | 
|  |     18 | #include <memmodel.h>
 | 
|  |     19 | #include <kernel/cache.h>
 | 
|  |     20 | 
 | 
|  |     21 | DCodeSeg* M::NewCodeSeg(TCodeSegCreateInfo&)
 | 
|  |     22 | 	{
 | 
|  |     23 | 	__KTRACE_OPT(KDLL,Kern::Printf("M::NewCodeSeg"));
 | 
|  |     24 | 	return new DMemModelCodeSeg;
 | 
|  |     25 | 	}
 | 
|  |     26 | 
 | 
|  |     27 | 
 | 
|  |     28 | DEpocCodeSegMemory* DEpocCodeSegMemory::New(DEpocCodeSeg* aCodeSeg)
 | 
|  |     29 | 	{
 | 
|  |     30 | 	return new DMemModelCodeSegMemory(aCodeSeg);
 | 
|  |     31 | 	}
 | 
|  |     32 | 
 | 
|  |     33 | 	
 | 
|  |     34 | DMemModelCodeSegMemory::DMemModelCodeSegMemory(DEpocCodeSeg* aCodeSeg)
 | 
|  |     35 | 	: DEpocCodeSegMemory(aCodeSeg)
 | 
|  |     36 | 	{
 | 
|  |     37 | 	}
 | 
|  |     38 | 
 | 
|  |     39 | 
 | 
|  |     40 | DMemModelCodeSeg::DMemModelCodeSeg()
 | 
|  |     41 | 	{
 | 
|  |     42 | 	}
 | 
|  |     43 | 
 | 
|  |     44 | 
 | 
|  |     45 | DMemModelCodeSeg::~DMemModelCodeSeg()
 | 
|  |     46 | //
 | 
|  |     47 | // Destructor
 | 
|  |     48 | //
 | 
|  |     49 | 	{
 | 
|  |     50 | 	__KTRACE_OPT(KDLL,Kern::Printf("DMemModelCodeSeg::Destruct %C", this));
 | 
|  |     51 | 	if (!iXIP && iMemory)
 | 
|  |     52 | 		{
 | 
|  |     53 | 		SRamCodeInfo& ri=RamInfo();
 | 
|  |     54 | 		if (ri.iCodeLoadAddr)
 | 
|  |     55 | 			{
 | 
|  |     56 | 			TInt code_size=MM::RoundToBlockSize(ri.iCodeSize+ri.iDataSize);
 | 
|  |     57 | 			MM::FreeRegion(ri.iCodeLoadAddr, code_size);
 | 
|  |     58 | 			}
 | 
|  |     59 | 		if (iDataAlloc)
 | 
|  |     60 | 			{
 | 
|  |     61 | 			TInt data_size=MM::RoundToBlockSize(ri.iDataSize+ri.iBssSize);
 | 
|  |     62 | 			MM::FreeRegion(iDataAlloc, data_size);
 | 
|  |     63 | 			}
 | 
|  |     64 | 		}
 | 
|  |     65 | 	Kern::Free(iKernelData);
 | 
|  |     66 | 	DEpocCodeSeg::Destruct();
 | 
|  |     67 | 	}
 | 
|  |     68 | 
 | 
|  |     69 | TInt DMemModelCodeSeg::DoCreateRam(TCodeSegCreateInfo& aInfo, DProcess* aProcess)
 | 
|  |     70 | 	{
 | 
|  |     71 | 	__KTRACE_OPT(KDLL,Kern::Printf("DMemModelCodeSeg::DoCreateRam %C proc %O", this, aProcess));
 | 
|  |     72 | 	DMemModelProcess* p=(DMemModelProcess*)aProcess;
 | 
|  |     73 | 	TBool kernel=( (iAttr&(ECodeSegAttKernel|ECodeSegAttGlobal)) == ECodeSegAttKernel );
 | 
|  |     74 | 	SRamCodeInfo& ri=RamInfo();
 | 
|  |     75 | 	iSize = MM::RoundToBlockSize(ri.iCodeSize+ri.iDataSize);
 | 
|  |     76 | 	TInt total_data_size=ri.iDataSize+ri.iBssSize;
 | 
|  |     77 | 	TInt r=MM::AllocRegion(ri.iCodeLoadAddr, iSize);
 | 
|  |     78 | 	if (r!=KErrNone)
 | 
|  |     79 | 		return r;
 | 
|  |     80 | 	ri.iCodeRunAddr=ri.iCodeLoadAddr;
 | 
|  |     81 | 	if (ri.iDataSize)
 | 
|  |     82 | 		ri.iDataLoadAddr=ri.iCodeLoadAddr+ri.iCodeSize;
 | 
|  |     83 | 	if (kernel)
 | 
|  |     84 | 		{
 | 
|  |     85 | 		if (total_data_size)
 | 
|  |     86 | 			{
 | 
|  |     87 | 			iKernelData=Kern::Alloc(total_data_size);
 | 
|  |     88 | 			if (!iKernelData)
 | 
|  |     89 | 				return KErrNoMemory;
 | 
|  |     90 | 			ri.iDataRunAddr=(TLinAddr)iKernelData;
 | 
|  |     91 | 			}
 | 
|  |     92 | 		return KErrNone;
 | 
|  |     93 | 		}
 | 
|  |     94 | 	if (total_data_size && p)
 | 
|  |     95 | 		SetAttachProcess(p);
 | 
|  |     96 | 	if (total_data_size && !IsExe())
 | 
|  |     97 | 		{
 | 
|  |     98 | 		TInt data_alloc_size=MM::RoundToBlockSize(total_data_size);
 | 
|  |     99 | 		TInt r=MM::AllocRegion(iDataAlloc, data_alloc_size);
 | 
|  |    100 | 		if (r!=KErrNone)
 | 
|  |    101 | 			return r;
 | 
|  |    102 | 		ri.iDataRunAddr=iDataAlloc;
 | 
|  |    103 | 		}
 | 
|  |    104 | 	return r;
 | 
|  |    105 | 	}
 | 
|  |    106 | 
 | 
|  |    107 | TInt DMemModelCodeSeg::DoCreateXIP(DProcess* aProcess)
 | 
|  |    108 | 	{
 | 
|  |    109 | 	__KTRACE_OPT(KDLL,Kern::Printf("DMemModelCodeSeg::DoCreateXIP %C proc %O", this, aProcess));
 | 
|  |    110 | 	DMemModelProcess* p=(DMemModelProcess*)aProcess;
 | 
|  |    111 | 	TInt r=KErrNone;
 | 
|  |    112 | 	TBool kernel=( (iAttr&(ECodeSegAttKernel|ECodeSegAttGlobal)) == ECodeSegAttKernel );
 | 
|  |    113 | 	const TRomImageHeader& rih=RomInfo();
 | 
|  |    114 | 	if (!kernel && aProcess)
 | 
|  |    115 | 		{
 | 
|  |    116 | 		// XIP images with static data are specific to a single process
 | 
|  |    117 | 		if (rih.iFlags&KRomImageFlagDataPresent)
 | 
|  |    118 | 			SetAttachProcess(p);
 | 
|  |    119 | 		}
 | 
|  |    120 | 	return r;
 | 
|  |    121 | 	}
 | 
|  |    122 | 
 | 
|  |    123 | TInt DMemModelCodeSeg::Loaded(TCodeSegCreateInfo& aInfo)
 | 
|  |    124 | 	{
 | 
|  |    125 | 	if (!iXIP)
 | 
|  |    126 | 		{
 | 
|  |    127 | 		// Clean DCache for specified area, Invalidate ICache/BTB for specified area
 | 
|  |    128 | 		TLinAddr code_base=RamInfo().iCodeRunAddr;
 | 
|  |    129 | 		Cache::IMB_Range(code_base, RamInfo().iCodeSize);
 | 
|  |    130 | 		}
 | 
|  |    131 | 	return DEpocCodeSeg::Loaded(aInfo);
 | 
|  |    132 | 	}
 | 
|  |    133 | 
 | 
|  |    134 | void DMemModelCodeSeg::ReadExportDir(TUint32* aDest)
 | 
|  |    135 | 	{
 | 
|  |    136 | 	__KTRACE_OPT(KDLL,Kern::Printf("DMemModelCodeSeg::ReadExportDir %08x",aDest));
 | 
|  |    137 | 	if (!iXIP)
 | 
|  |    138 | 		{
 | 
|  |    139 | 		SRamCodeInfo& ri=RamInfo();
 | 
|  |    140 | 		TInt size=(ri.iExportDirCount+1)*sizeof(TLinAddr);
 | 
|  |    141 | 		kumemput32(aDest, (const TUint32*)(ri.iExportDir-sizeof(TUint32)), size);
 | 
|  |    142 | 		}
 | 
|  |    143 | 	}
 | 
|  |    144 | 
 | 
|  |    145 | TBool DMemModelCodeSeg::OpenCheck(DProcess* aProcess)
 | 
|  |    146 | 	{
 | 
|  |    147 | 	return FindCheck(aProcess);
 | 
|  |    148 | 	}
 | 
|  |    149 | 
 | 
|  |    150 | TBool DMemModelCodeSeg::FindCheck(DProcess* aProcess)
 | 
|  |    151 | 	{
 | 
|  |    152 | 	__KTRACE_OPT(KDLL,Kern::Printf("CSEG:%08x Compat? proc=%O",this,aProcess));
 | 
|  |    153 | 	if (!aProcess)
 | 
|  |    154 | 		return !iAttachProcess;	// can't reuse same code segment for a new instance of the process
 | 
|  |    155 | 	DMemModelProcess& p=*(DMemModelProcess*)aProcess;
 | 
|  |    156 | 	DCodeSeg* pPSeg=p.CodeSeg();
 | 
|  |    157 | 	if (iAttachProcess && iAttachProcess!=aProcess)
 | 
|  |    158 | 		return EFalse;
 | 
|  |    159 | 	if (iExeCodeSeg && iExeCodeSeg!=pPSeg)
 | 
|  |    160 | 		return EFalse;
 | 
|  |    161 | 	__ASSERT_DEBUG( (iAttachProcess || !(iMark & EMarkDataPresent)), MM::Panic(MM::ECodeSegCheckInconsistent));
 | 
|  |    162 | 	return ETrue;
 | 
|  |    163 | 	}
 | 
|  |    164 | 
 |