os/kernelhwsrv/kernel/eka/memmodel/epoc/flexible/mmu/mrom.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <plat_priv.h>
sl@0
    17
#include <kernel/cache.h>
sl@0
    18
#include "mm.h"
sl@0
    19
#include "mmu.h"
sl@0
    20
#include "mrom.h"
sl@0
    21
#include "mpager.h"
sl@0
    22
#include "mmanager.h"
sl@0
    23
#include "mobject.h"
sl@0
    24
#include "mmapping.h"
sl@0
    25
#include "maddrcont.h"
sl@0
    26
#include "mptalloc.h"
sl@0
    27
#include "mlargemappings.h"
sl@0
    28
sl@0
    29
#include "cache_maintenance.inl"
sl@0
    30
sl@0
    31
sl@0
    32
/**
sl@0
    33
Class representing the resources allocated for a ROM shadow page.
sl@0
    34
sl@0
    35
A shadow page is a page of RAM which is mapped by the MMU to replace
sl@0
    36
a prior existing page at a particular virtual address.
sl@0
    37
*/
sl@0
    38
class DShadowPage : public DVirtualPinMapping
sl@0
    39
	{
sl@0
    40
public:
sl@0
    41
	/**
sl@0
    42
	Create a new #DShadowPage to shadow a specified memory page.
sl@0
    43
sl@0
    44
	On success, #iOriginalPage holds the physical address of the original page
sl@0
    45
	and #iNewPage the physical address of the newly allocated RAM page; the
sl@0
    46
	contents of this are a copy of the original.
sl@0
    47
sl@0
    48
	No MMU entries for the shadow page are changed - it is the responsibility
sl@0
    49
	of the caller to handle this. However, the new #DShadowPage object will
sl@0
    50
	have pinned the page table used by \a aMapping which maps the page being
sl@0
    51
	shadowed, prevent demand paging from discarding any modifications made to
sl@0
    52
	this.
sl@0
    53
sl@0
    54
	@param aMemory		The memory object whose memory is to be shadowed.
sl@0
    55
	@param aIndex		Page index, within the memory, of the page to shadow.
sl@0
    56
	@param aMapping		A memory mapping which currently maps the page to be
sl@0
    57
						shadowed.
sl@0
    58
sl@0
    59
	@return The newly created DShadowPage or the null pointer if there was
sl@0
    60
			insufficient memory.
sl@0
    61
	*/
sl@0
    62
	static DShadowPage* New(DMemoryObject* aMemory, TUint aIndex, DMemoryMappingBase* aMapping);
sl@0
    63
sl@0
    64
	/**
sl@0
    65
	Free the allocated shadow page (#iNewPage) and unpin any pages table which
sl@0
    66
	was pinned, then free this shadow page object.
sl@0
    67
sl@0
    68
	The called of this function must ensure that all references to the shadow
sl@0
    69
	RAM page have been removed from any MMU mappings.
sl@0
    70
	*/
sl@0
    71
	void Destroy();
sl@0
    72
sl@0
    73
private:
sl@0
    74
	DShadowPage();
sl@0
    75
	~DShadowPage();
sl@0
    76
sl@0
    77
	/**
sl@0
    78
	Second phase constructor. For arguments, see #New.
sl@0
    79
	*/
sl@0
    80
	TInt Construct(DMemoryObject* aMemory, TUint aIndex, DMemoryMappingBase* aMapping);
sl@0
    81
sl@0
    82
public:
sl@0
    83
	/**
sl@0
    84
	The physical address of the original page being shadowed.
sl@0
    85
	*/
sl@0
    86
	TPhysAddr iOriginalPage;
sl@0
    87
sl@0
    88
	/**
sl@0
    89
	The physical address of the allocated shadow page.
sl@0
    90
	*/
sl@0
    91
	TPhysAddr iNewPage;
sl@0
    92
	};
sl@0
    93
sl@0
    94
sl@0
    95
/**
sl@0
    96
Specialised manager for the memory object representing the system ROM.
sl@0
    97
This handles demand paging of the ROM contents if it is not stored in a memory
sl@0
    98
device capable of execute-in-place random access. E.g. when stored in NAND
sl@0
    99
flash.
sl@0
   100
*/
sl@0
   101
class DRomMemoryManager : public DPagedMemoryManager
sl@0
   102
	{
sl@0
   103
public:
sl@0
   104
	DRomMemoryManager();
sl@0
   105
sl@0
   106
	/**
sl@0
   107
	Allocate a shadow page for the specified ROM address.
sl@0
   108
sl@0
   109
	Shadow pages are pages of RAM which are mapped by the MMU so that 
sl@0
   110
	they replace the original ROM memory. The contents of a shadow page
sl@0
   111
	are initially the same as the ROM they replace, but may be modified with
sl@0
   112
	#CopyToShadowMemory.
sl@0
   113
sl@0
   114
	@param aRomAddr	An virtual address which lies within the ROM.
sl@0
   115
sl@0
   116
	@return KErrNone if successful,
sl@0
   117
			KErrAlreadyExists if the specified address already has a show page,
sl@0
   118
			otherwise one of the system wide error codes.
sl@0
   119
	*/
sl@0
   120
	TInt AllocShadowPage(TLinAddr aRomAddr);
sl@0
   121
sl@0
   122
	/**
sl@0
   123
	Free a shadow page previously allocated with #AllocShadowPage.
sl@0
   124
sl@0
   125
	The original ROM memory page is again mapped at the specified address.
sl@0
   126
sl@0
   127
	@param aRomAddr	An virtual address which lies within the ROM.
sl@0
   128
sl@0
   129
	@return KErrNone if successful,
sl@0
   130
			otherwise one of the system wide error codes.
sl@0
   131
	*/
sl@0
   132
	TInt FreeShadowPage(TLinAddr aRomAddr);
sl@0
   133
sl@0
   134
	/**
sl@0
   135
	Copy data into a shadow page, modifying its contents.
sl@0
   136
sl@0
   137
	@param aDst		An virtual address which lies within the ROM for which a shadow
sl@0
   138
					page has previously been allocated with #AllocShadowPage.
sl@0
   139
	@param aSrc		The start address of the data to copy to \a aDst.
sl@0
   140
	@param aSize	The size, in bytes, of the data to copy.
sl@0
   141
sl@0
   142
	@return KErrNone if successful,
sl@0
   143
			KErrNotFound if the specified address didn't have a shadow page,
sl@0
   144
			otherwise one of the system wide error codes.
sl@0
   145
	*/
sl@0
   146
	TInt CopyToShadowMemory(TLinAddr aDst, TLinAddr aSrc, TUint32 aSize);
sl@0
   147
sl@0
   148
protected:
sl@0
   149
sl@0
   150
	// from DPagedMemoryManager...
sl@0
   151
	virtual TInt PageInPinnedDone(DMemoryObject* aMemory, TUint aIndex, SPageInfo* aPageInfo, TPhysAddr* aPageArrayEntry, TPinArgs& aPinArgs);
sl@0
   152
sl@0
   153
private:
sl@0
   154
	// from DMemoryManager...
sl@0
   155
	virtual void Destruct(DMemoryObject* aMemory);
sl@0
   156
	virtual TInt HandleFault(	DMemoryObject* aMemory, TUint aIndex, DMemoryMapping* aMapping, 
sl@0
   157
								TUint aMapInstanceCount, TUint aAccessPermissions);
sl@0
   158
	virtual TInt Pin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs);
sl@0
   159
	virtual void Unpin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs);
sl@0
   160
sl@0
   161
	// methods inherited from DPagedMemoryManager
sl@0
   162
sl@0
   163
	/**
sl@0
   164
	@copydoc DPagedMemoryManager::Init3
sl@0
   165
	This acts as a second phase constructor for the manager which
sl@0
   166
	creates the memory objects and mappings to represent the ROM.
sl@0
   167
	*/
sl@0
   168
	virtual void Init3();
sl@0
   169
sl@0
   170
	virtual TInt InstallPagingDevice(DPagingDevice* aDevice);
sl@0
   171
	virtual TInt AcquirePageReadRequest(DPageReadRequest*& aRequest, DMemoryObject* aMemory, TUint aIndex, TUint aCount);
sl@0
   172
	virtual TInt ReadPages(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr* aPages, DPageReadRequest* aRequest);
sl@0
   173
	virtual TBool IsAllocated(DMemoryObject* aMemory, TUint aIndex, TUint aCount);
sl@0
   174
	virtual void DoUnpin(DMemoryObject* aMemory, TUint aIndex, TUint aCount, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs);
sl@0
   175
sl@0
   176
	/**
sl@0
   177
	Acquire the mutex used to protect shadow page allocation.
sl@0
   178
	*/
sl@0
   179
	void ShadowLock();
sl@0
   180
sl@0
   181
	/**
sl@0
   182
	Release the mutex used to protect shadow page allocation.
sl@0
   183
	*/
sl@0
   184
	void ShadowUnlock();
sl@0
   185
sl@0
   186
private:
sl@0
   187
	/**
sl@0
   188
	The ROM paging device which was passed to #InstallPagingDevice.
sl@0
   189
	*/
sl@0
   190
	DPagingDevice* iDevice;
sl@0
   191
sl@0
   192
	/**
sl@0
   193
	The memory object containing the ROM.
sl@0
   194
	*/
sl@0
   195
	DMemoryObject* iRomMemory;
sl@0
   196
sl@0
   197
	/**
sl@0
   198
	The memory mapping which maps the ROM into a global visible virtual address.
sl@0
   199
	*/
sl@0
   200
	DMemoryMapping* iRomMapping;
sl@0
   201
sl@0
   202
	/**
sl@0
   203
	The virtual address for the start of the ROM in the global memory region.
sl@0
   204
	*/
sl@0
   205
	TLinAddr iBase;
sl@0
   206
sl@0
   207
	/**
sl@0
   208
	The size, in bytes, of the ROM image.
sl@0
   209
	This may not be an exact multiple of a page size.
sl@0
   210
	*/
sl@0
   211
	TUint iSize;
sl@0
   212
sl@0
   213
	/**
sl@0
   214
	The size, in pages, of the ROM image.
sl@0
   215
	*/
sl@0
   216
	TUint iSizeInPages;
sl@0
   217
sl@0
   218
	/**
sl@0
   219
	The offset from the ROM start, in bytes, for the region of the
sl@0
   220
	ROM which is demand paged.
sl@0
   221
	*/
sl@0
   222
	TUint iPagedStart;
sl@0
   223
sl@0
   224
	/**
sl@0
   225
	The size, in bytes, for the region of the ROM which is demand paged.
sl@0
   226
	*/
sl@0
   227
	TUint iPagedSize;
sl@0
   228
sl@0
   229
	/**
sl@0
   230
	The address within the ROM for the ROM page index.
sl@0
   231
	@see TRomHeader::iRomPageIndex.
sl@0
   232
	*/
sl@0
   233
	SRomPageInfo* iRomPageIndex;
sl@0
   234
sl@0
   235
	/**
sl@0
   236
	The mutex used to protect shadow page allocation.
sl@0
   237
	*/
sl@0
   238
	DMutex* iShadowLock;
sl@0
   239
sl@0
   240
	/**
sl@0
   241
	Container for all allocated DShadowPage objects.
sl@0
   242
	*/
sl@0
   243
	RAddressedContainer iShadowPages;
sl@0
   244
sl@0
   245
#ifdef __SUPPORT_DEMAND_PAGING_EMULATION__
sl@0
   246
	TInt iOriginalRomPageCount;
sl@0
   247
	TPhysAddr* iOriginalRomPages;
sl@0
   248
	friend void RomOriginalPages(TPhysAddr*& aPages, TUint& aPageCount);
sl@0
   249
#endif
sl@0
   250
sl@0
   251
	friend TBool IsUnpagedRom(TLinAddr aBase, TUint aSize);
sl@0
   252
sl@0
   253
public:
sl@0
   254
	/**
sl@0
   255
	The single instance of this manager class.
sl@0
   256
	*/
sl@0
   257
	static DRomMemoryManager TheManager;
sl@0
   258
	};
sl@0
   259
sl@0
   260
sl@0
   261
DRomMemoryManager DRomMemoryManager::TheManager;
sl@0
   262
DPagedMemoryManager* TheRomMemoryManager = &DRomMemoryManager::TheManager;
sl@0
   263
sl@0
   264
sl@0
   265
const TInt KMutexOrdRomMemory = KMutexOrdPageIn+1;
sl@0
   266
sl@0
   267
sl@0
   268
#ifdef __SUPPORT_DEMAND_PAGING_EMULATION__
sl@0
   269
/**
sl@0
   270
For use by the emulated paging device to get the location and size of the ROM.
sl@0
   271
sl@0
   272
@param aPages		A reference to store a pointer to an array of the physical addresses of each ROM page.
sl@0
   273
@param aPageCount	A reference to store the number of rom pages.
sl@0
   274
*/
sl@0
   275
void RomOriginalPages(TPhysAddr*& aPages, TUint& aPageCount)
sl@0
   276
	{
sl@0
   277
	aPages = DRomMemoryManager::TheManager.iOriginalRomPages;
sl@0
   278
	aPageCount = DRomMemoryManager::TheManager.iOriginalRomPageCount;
sl@0
   279
	}
sl@0
   280
sl@0
   281
#endif
sl@0
   282
sl@0
   283
sl@0
   284
TBool IsUnpagedRom(TLinAddr aBase, TUint aSize)
sl@0
   285
	{
sl@0
   286
	TUint offset = aBase-DRomMemoryManager::TheManager.iBase;
sl@0
   287
	TUint limit = DRomMemoryManager::TheManager.iPagedStart;
sl@0
   288
	if(offset>=limit)
sl@0
   289
		return false;
sl@0
   290
	offset += aSize;
sl@0
   291
	if(offset>limit || offset<aSize)
sl@0
   292
		return false;
sl@0
   293
	return true;
sl@0
   294
	}
sl@0
   295
sl@0
   296
sl@0
   297
TInt PagifyChunk(TLinAddr aAddress)
sl@0
   298
	{
sl@0
   299
	TRACE(("PagifyChunk(0x%08x)",aAddress));
sl@0
   300
sl@0
   301
	aAddress &= ~KChunkMask;
sl@0
   302
	TPde* pPde = Mmu::PageDirectoryEntry(KKernelOsAsid,aAddress);
sl@0
   303
sl@0
   304
retry:
sl@0
   305
	// check there is actually some memory mapped...
sl@0
   306
	TPde pde = *pPde;
sl@0
   307
	if(pde==KPdeUnallocatedEntry)
sl@0
   308
		{
sl@0
   309
		TRACE(("PagifyChunk returns %d",KErrNotFound));
sl@0
   310
		return KErrNotFound;
sl@0
   311
		}
sl@0
   312
sl@0
   313
	// end if memory is not a section mapping...
sl@0
   314
	TPhysAddr pdePhys = Mmu::PdePhysAddr(pde);
sl@0
   315
	if(pdePhys==KPhysAddrInvalid)
sl@0
   316
		{
sl@0
   317
		TRACE(("PagifyChunk returns %d",KErrAlreadyExists));
sl@0
   318
		return KErrAlreadyExists;
sl@0
   319
		}
sl@0
   320
sl@0
   321
	// get a new page table...
sl@0
   322
	::PageTables.Lock();
sl@0
   323
	TPte* pt = ::PageTables.Alloc(false);
sl@0
   324
	if(!pt)
sl@0
   325
		{
sl@0
   326
		TRACE(("PagifyChunk returns %d",KErrNoMemory));
sl@0
   327
		::PageTables.Unlock();
sl@0
   328
		return KErrNoMemory;
sl@0
   329
		}
sl@0
   330
sl@0
   331
	// fill page table so it maps the same physical addresses as the section mapping...
sl@0
   332
	TPte pte = Mmu::SectionToPageEntry(pde);
sl@0
   333
	pte |= pdePhys;
sl@0
   334
	TPte* pPte = pt;
sl@0
   335
	do
sl@0
   336
		{
sl@0
   337
		TRACE2(("!PTE %x=%x",pPte,pte));
sl@0
   338
		*pPte++ = pte;
sl@0
   339
		pte += KPageSize;
sl@0
   340
		}
sl@0
   341
	while(TLinAddr(pPte)&(KPageTableMask/sizeof(TPte)*sizeof(TPte)));
sl@0
   342
	CacheMaintenance::MultiplePtesUpdated((TLinAddr)pt,KPageTableSize);
sl@0
   343
sl@0
   344
	// check memory not changed...
sl@0
   345
	MmuLock::Lock();
sl@0
   346
	if(Mmu::PdePhysAddr(*pPde)!=pdePhys)
sl@0
   347
		{
sl@0
   348
		// pde was changed whilst we were creating a new page table, need to retry...
sl@0
   349
		MmuLock::Unlock();
sl@0
   350
		::PageTables.Free(pt);
sl@0
   351
		::PageTables.Unlock();
sl@0
   352
		goto retry;
sl@0
   353
		}
sl@0
   354
sl@0
   355
	// update page counts...
sl@0
   356
	SPageTableInfo* pti = SPageTableInfo::FromPtPtr(pt);
sl@0
   357
	TUint count = pti->IncPageCount(KPageTableSize/sizeof(TPte));
sl@0
   358
	(void)count;
sl@0
   359
	TRACE2(("pt %x page count=%d",pt,pti->PageCount()));
sl@0
   360
	__NK_ASSERT_DEBUG(pti->CheckPageCount());
sl@0
   361
sl@0
   362
	// swap pde entry to point to new page table...
sl@0
   363
	pde |= Mmu::PageTablePhysAddr(pt);
sl@0
   364
	TRACE2(("!PDE %x=%x",pPde,pde));
sl@0
   365
	*pPde = pde;
sl@0
   366
	SinglePdeUpdated(pPde);
sl@0
   367
	InvalidateTLB();
sl@0
   368
sl@0
   369
	// done...
sl@0
   370
	MmuLock::Unlock();
sl@0
   371
	::PageTables.Unlock();
sl@0
   372
	TRACE(("PagifyChunk returns %d",KErrNone));
sl@0
   373
	return KErrNone;
sl@0
   374
	}
sl@0
   375
sl@0
   376
sl@0
   377
void UnmapROM(TLinAddr aStart, TLinAddr aEnd)
sl@0
   378
	{
sl@0
   379
	TRACEB(("UnmapROM 0x%08x..0x%08x",aStart,aEnd));
sl@0
   380
sl@0
   381
	TLinAddr p = aStart;
sl@0
   382
	if(p>=aEnd)
sl@0
   383
		return;
sl@0
   384
sl@0
   385
	PagifyChunk(p);
sl@0
   386
sl@0
   387
	MmuLock::Lock(); // hold MmuLock for long time, shouldn't matter as this is only done during boot
sl@0
   388
sl@0
   389
	TPte* pPte = Mmu::PtePtrFromLinAddr(p,KKernelOsAsid);
sl@0
   390
	__NK_ASSERT_ALWAYS(pPte);
sl@0
   391
	while(p<aEnd && p&KChunkMask)
sl@0
   392
		{
sl@0
   393
		*pPte++ = KPteUnallocatedEntry;
sl@0
   394
		p += KPageSize;
sl@0
   395
		}
sl@0
   396
sl@0
   397
	if(p<aEnd)
sl@0
   398
		{
sl@0
   399
		TPde* pPde = Mmu::PageDirectoryEntry(KKernelOsAsid,p);
sl@0
   400
		while(p<aEnd)
sl@0
   401
			{
sl@0
   402
			*pPde++ = KPdeUnallocatedEntry;
sl@0
   403
			p += KChunkSize;
sl@0
   404
			}
sl@0
   405
		}
sl@0
   406
sl@0
   407
	MmuLock::Unlock();
sl@0
   408
sl@0
   409
	__NK_ASSERT_DEBUG(p==aEnd);
sl@0
   410
	}
sl@0
   411
sl@0
   412
sl@0
   413
DRomMemoryManager::DRomMemoryManager()
sl@0
   414
	: iShadowPages(0,iShadowLock)
sl@0
   415
	{
sl@0
   416
	}
sl@0
   417
sl@0
   418
sl@0
   419
void DRomMemoryManager::Init3()
sl@0
   420
	{
sl@0
   421
	// get ROM info...
sl@0
   422
	const TRomHeader& romHeader = TheRomHeader();
sl@0
   423
	iBase = (TLinAddr)&romHeader;
sl@0
   424
	iSize = romHeader.iUncompressedSize;
sl@0
   425
	iSizeInPages = MM::RoundToPageCount(iSize);
sl@0
   426
	TUint chunkSize = ((iSize+KChunkMask)&~KChunkMask);
sl@0
   427
	TUint committedSize = TheSuperPage().iTotalRomSize; // size of memory loaded by bootstrap
sl@0
   428
	TRACEB(("DRomMemoryManager::Init3 rom=0x%08x+0x%x",iBase,iSize));
sl@0
   429
sl@0
   430
	// get paged rom info...
sl@0
   431
	if(romHeader.iRomPageIndex)
sl@0
   432
		iRomPageIndex = (SRomPageInfo*)((TInt)&romHeader+romHeader.iRomPageIndex);
sl@0
   433
	iPagedSize = romHeader.iPageableRomSize;
sl@0
   434
	iPagedStart = iPagedSize ? romHeader.iPageableRomStart : 0;
sl@0
   435
	if(iPagedStart)
sl@0
   436
		{
sl@0
   437
		TRACEB(("DRomMemoryManager::Init3() paged=0x%08x+0x%x",(TLinAddr)&romHeader+iPagedStart,iPagedSize));
sl@0
   438
		__NK_ASSERT_ALWAYS(iPagedStart<iSize && iPagedStart+iPagedSize>iPagedStart && iPagedStart+iPagedSize<=iSize);
sl@0
   439
sl@0
   440
#ifdef __SUPPORT_DEMAND_PAGING_EMULATION__
sl@0
   441
		// get physical addresses of ROM pages...
sl@0
   442
		iOriginalRomPageCount = iSizeInPages;
sl@0
   443
		iOriginalRomPages = new TPhysAddr[iOriginalRomPageCount];
sl@0
   444
		__NK_ASSERT_ALWAYS(iOriginalRomPages);
sl@0
   445
		MmuLock::Lock(); // hold MmuLock for long time, shouldn't matter as this is only done during boot
sl@0
   446
		TInt i;
sl@0
   447
		for(i=0; i<iOriginalRomPageCount; i++)
sl@0
   448
			iOriginalRomPages[i] = Mmu::LinearToPhysical(iBase+i*KPageSize);
sl@0
   449
		MmuLock::Unlock();
sl@0
   450
sl@0
   451
		// unmap paged part of ROM as the bootstrap will have left it mapped.
sl@0
   452
		// See CFG_SupportEmulatedRomPaging in the bootstrap code.
sl@0
   453
		// todo: use FMM for this after memory object created
sl@0
   454
		UnmapROM(iBase+iPagedStart,iBase+chunkSize);
sl@0
   455
		committedSize = iPagedStart;
sl@0
   456
#endif
sl@0
   457
		}
sl@0
   458
sl@0
   459
	if(iPagedStart && committedSize!=iPagedStart)
sl@0
   460
		{
sl@0
   461
		// unmap any paged ROM which the bootstrap mapped...
sl@0
   462
		TRACEB(("DRomMemoryManager::Init3() unmapping unpaged ROM offsets 0x%x thru 0x%x",iPagedStart,committedSize));
sl@0
   463
		// todo: use FMM for this after memory object created
sl@0
   464
		UnmapROM(iBase+iPagedStart,iBase+committedSize);
sl@0
   465
		committedSize = iPagedStart;
sl@0
   466
		}
sl@0
   467
sl@0
   468
	// create memory object for ROM...
sl@0
   469
	TRACEB(("DRomMemoryManager::Init3() committed ROM memory 0x%x of 0x%x",committedSize,chunkSize));
sl@0
   470
	TMemoryCreateFlags flags = (TMemoryCreateFlags)(EMemoryCreateNoWipe | EMemoryCreateReadOnly | 
sl@0
   471
													EMemoryCreateDemandPaged | EMemoryCreateAllowExecution);
sl@0
   472
	iRomMemory = DLargeMappedMemory::New(&DRomMemoryManager::TheManager,chunkSize>>KPageShift,EMemoryAttributeStandard,flags);
sl@0
   473
	__NK_ASSERT_ALWAYS(iRomMemory);
sl@0
   474
	TInt r = MM::MemoryClaimInitialPages(iRomMemory,iBase,committedSize,EUserExecute,false,true);
sl@0
   475
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   476
	r = iRomMemory->iPages.Alloc(committedSize>>KPageShift,(chunkSize-committedSize)>>KPageShift);
sl@0
   477
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   478
sl@0
   479
	// create mapping for ROM...
sl@0
   480
	r = MM::MappingNew(iRomMapping, iRomMemory, EUserExecute, KKernelOsAsid, EMappingCreateExactVirtual, iBase);
sl@0
   481
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   482
	__NK_ASSERT_ALWAYS(iRomMapping->IsLarge());
sl@0
   483
sl@0
   484
	// Set the paging device to be uninstalled, i.e. NULL.
sl@0
   485
	iDevice = NULL;
sl@0
   486
sl@0
   487
	_LIT(KRomMemoryLockName,"RomMemory");
sl@0
   488
	r = K::MutexCreate(iShadowLock, KRomMemoryLockName, NULL, EFalse, KMutexOrdRomMemory);
sl@0
   489
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   490
	MM::MemorySetLock(iRomMemory,iShadowLock);
sl@0
   491
	}
sl@0
   492
sl@0
   493
sl@0
   494
TInt DRomMemoryManager::InstallPagingDevice(DPagingDevice* aDevice)
sl@0
   495
	{
sl@0
   496
	TRACEB(("DRomMemoryManager::InstallPagingDevice(0x%08x)",aDevice));
sl@0
   497
sl@0
   498
	if(!iPagedStart)
sl@0
   499
		{
sl@0
   500
		TRACEB(("ROM is not paged"));
sl@0
   501
		return KErrNone;
sl@0
   502
		}
sl@0
   503
sl@0
   504
	TAny* null = 0;
sl@0
   505
	if(!__e32_atomic_cas_ord_ptr(&iDevice, &null, aDevice)) // set iDevice=aDevice if it was originally 0
sl@0
   506
		{
sl@0
   507
		// ROM paging device already registered...
sl@0
   508
		TRACEB(("DRomMemoryManager::InstallPagingDevice returns ALREADY EXISTS!"));
sl@0
   509
		return KErrAlreadyExists;
sl@0
   510
		}
sl@0
   511
sl@0
   512
	__e32_atomic_ior_ord32(&K::MemModelAttributes, (TUint32)EMemModelAttrRomPaging);
sl@0
   513
sl@0
   514
	return KErrNone;
sl@0
   515
	}
sl@0
   516
sl@0
   517
sl@0
   518
TInt DRomMemoryManager::AcquirePageReadRequest(DPageReadRequest*& aRequest, DMemoryObject* aMemory, TUint aIndex, TUint aCount)
sl@0
   519
	{
sl@0
   520
	aRequest = iDevice->iRequestPool->AcquirePageReadRequest(aMemory,aIndex,aCount);
sl@0
   521
	return KErrNone;
sl@0
   522
	}
sl@0
   523
sl@0
   524
sl@0
   525
void DRomMemoryManager::Destruct(DMemoryObject* aMemory)
sl@0
   526
	{
sl@0
   527
	__NK_ASSERT_DEBUG(0);
sl@0
   528
	}
sl@0
   529
sl@0
   530
sl@0
   531
TInt DRomMemoryManager::ReadPages(DMemoryObject* aMemory, TUint aIndex, TUint aCount, TPhysAddr* aPages, DPageReadRequest* aRequest)
sl@0
   532
	{
sl@0
   533
	__NK_ASSERT_DEBUG(aRequest->CheckUse(aMemory,aIndex,aCount));
sl@0
   534
sl@0
   535
	TLinAddr linAddr = aRequest->MapPages(aIndex,aCount,aPages);
sl@0
   536
	TInt r = KErrNone;
sl@0
   537
sl@0
   538
	const TInt readUnitShift = iDevice->iReadUnitShift;
sl@0
   539
sl@0
   540
	for(; aCount; ++aIndex, --aCount, linAddr+=KPageSize)
sl@0
   541
		{
sl@0
   542
		START_PAGING_BENCHMARK;
sl@0
   543
		if(!iRomPageIndex)
sl@0
   544
			{
sl@0
   545
			// ROM not broken into pages, so just read it in directly.
sl@0
   546
			// KPageShift > readUnitShift so page size is exact multiple of read 
sl@0
   547
			// units.  Therefore it is ok to just shift offset and KPageSize 
sl@0
   548
			// by readUnitShift.
sl@0
   549
			const TInt dataOffset = aIndex << KPageShift;
sl@0
   550
			START_PAGING_BENCHMARK;
sl@0
   551
			r = iDevice->Read(	const_cast<TThreadMessage*>(&aRequest->iMessage), 
sl@0
   552
								linAddr, dataOffset >> readUnitShift, 
sl@0
   553
								KPageSize >> readUnitShift, DPagingDevice::EDriveRomPaging);
sl@0
   554
			__NK_ASSERT_DEBUG(r!=KErrNoMemory); // not allowed to allocated memory, therefore can't fail with KErrNoMemory
sl@0
   555
			END_PAGING_BENCHMARK(EPagingBmReadMedia);
sl@0
   556
			}
sl@0
   557
		else
sl@0
   558
			{
sl@0
   559
			// Work out where data for page is located
sl@0
   560
			SRomPageInfo* romPageInfo = iRomPageIndex + aIndex;
sl@0
   561
			const TInt dataOffset = romPageInfo->iDataStart;
sl@0
   562
			const TInt dataSize = romPageInfo->iDataSize;
sl@0
   563
			if(!dataSize)
sl@0
   564
				{
sl@0
   565
				// empty page, fill it with 0xff...
sl@0
   566
				memset((TAny*)linAddr, 0xff, KPageSize);
sl@0
   567
				r = KErrNone;
sl@0
   568
				}
sl@0
   569
			else
sl@0
   570
				{
sl@0
   571
				__NK_ASSERT_ALWAYS(romPageInfo->iPagingAttributes & SRomPageInfo::EPageable);
sl@0
   572
sl@0
   573
				// Read data for page...
sl@0
   574
				TThreadMessage* msg = const_cast<TThreadMessage*>(&aRequest->iMessage);
sl@0
   575
				const TLinAddr buffer = aRequest->iBuffer;
sl@0
   576
				const TUint readStart = dataOffset >> readUnitShift;
sl@0
   577
				const TUint readSize = ((dataOffset + dataSize - 1) >> readUnitShift) - readStart + 1;
sl@0
   578
				__NK_ASSERT_DEBUG((readSize << readUnitShift) <= (DPageReadRequest::EMaxPages << KPageShift));
sl@0
   579
				START_PAGING_BENCHMARK;
sl@0
   580
				r = iDevice->Read(msg, buffer, readStart, readSize, DPagingDevice::EDriveRomPaging);
sl@0
   581
				__NK_ASSERT_DEBUG(r!=KErrNoMemory); // not allowed to allocated memory, therefore can't fail with KErrNoMemory
sl@0
   582
				END_PAGING_BENCHMARK(EPagingBmReadMedia);
sl@0
   583
				if(r==KErrNone)
sl@0
   584
					{
sl@0
   585
					// Decompress data, remembering that the data to decompress may be offset from 
sl@0
   586
					// the start of the data just read in, due to reads having to be aligned by 
sl@0
   587
					// readUnitShift.
sl@0
   588
					const TLinAddr data = buffer + dataOffset - (readStart << readUnitShift);
sl@0
   589
					__ASSERT_COMPILE(SRomPageInfo::ENoCompression==0); // decompress assumes this
sl@0
   590
					r = Decompress(romPageInfo->iCompressionType, linAddr, KPageSize, data, dataSize);
sl@0
   591
					if(r >= 0)
sl@0
   592
						{
sl@0
   593
						if (r != KPageSize)
sl@0
   594
							__KTRACE_OPT(KPANIC, Kern::Printf("DRomMemoryManager::ReadPage: error decompressing page at %08x + %x: %d", dataOffset, dataSize, r));
sl@0
   595
						__NK_ASSERT_ALWAYS(r == KPageSize);
sl@0
   596
						r = KErrNone;
sl@0
   597
						}
sl@0
   598
					}
sl@0
   599
				else
sl@0
   600
					__KTRACE_OPT(KPANIC, Kern::Printf("DRomMemoryManager::ReadPage: error reading media at %08x + %x: %d", dataOffset, dataSize, r));
sl@0
   601
				}
sl@0
   602
			}
sl@0
   603
		END_PAGING_BENCHMARK(EPagingBmReadRomPage);
sl@0
   604
sl@0
   605
		if(r!=KErrNone)
sl@0
   606
			break;
sl@0
   607
		}
sl@0
   608
sl@0
   609
	aRequest->UnmapPages(true);
sl@0
   610
sl@0
   611
	return r;
sl@0
   612
	}
sl@0
   613
sl@0
   614
sl@0
   615
TBool DRomMemoryManager::IsAllocated(DMemoryObject* aMemory, TUint aIndex, TUint aCount)
sl@0
   616
	{
sl@0
   617
	// all pages in the ROM memory object are always allocated...
sl@0
   618
	return true;
sl@0
   619
	}
sl@0
   620
sl@0
   621
sl@0
   622
TInt DRomMemoryManager::HandleFault(DMemoryObject* aMemory, TUint aIndex, DMemoryMapping* aMapping, 
sl@0
   623
									TUint aMapInstanceCount, TUint aAccessPermissions)
sl@0
   624
	{
sl@0
   625
	__NK_ASSERT_DEBUG(aMemory==iRomMemory);
sl@0
   626
sl@0
   627
	TUint offset = aIndex*KPageSize;
sl@0
   628
	if(offset<iPagedStart || offset>=iPagedStart+iPagedSize)
sl@0
   629
		return KErrAbort;
sl@0
   630
sl@0
   631
	return DPagedMemoryManager::HandleFault(aMemory, aIndex, aMapping, aMapInstanceCount, aAccessPermissions);
sl@0
   632
	}
sl@0
   633
sl@0
   634
sl@0
   635
TInt DRomMemoryManager::Pin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs)
sl@0
   636
	{
sl@0
   637
	TRACE(("DRomMemoryManager::Pin %08x %08x", aMemory, aMapping));
sl@0
   638
	TUint index = aMapping->iStartIndex;
sl@0
   639
	TUint endIndex = index+aMapping->iSizeInPages;
sl@0
   640
	if(endIndex>iSizeInPages)
sl@0
   641
		return KErrNotFound;
sl@0
   642
sl@0
   643
	TInt r = KErrNone;
sl@0
   644
	TUint pagedIndex = iPagedStart>>KPageShift;
sl@0
   645
	if(pagedIndex && pagedIndex<endIndex)
sl@0
   646
		{
sl@0
   647
		TUint start = index;
sl@0
   648
		if(start<pagedIndex)
sl@0
   649
			start = pagedIndex;
sl@0
   650
		r = DoPin(aMemory,start,endIndex-start,aMapping,aPinArgs);
sl@0
   651
		}
sl@0
   652
sl@0
   653
	return r;
sl@0
   654
	}
sl@0
   655
sl@0
   656
sl@0
   657
TInt DRomMemoryManager::PageInPinnedDone(DMemoryObject* aMemory, TUint aIndex, SPageInfo* aPageInfo, TPhysAddr* aPageArrayEntry, TPinArgs& aPinArgs)
sl@0
   658
	{
sl@0
   659
	TRACE(("DRomMemoryManager::PageInPinnedDone %08x %d", aMemory, aIndex));
sl@0
   660
	
sl@0
   661
	// Only the paged part of rom should be pinned.
sl@0
   662
	__NK_ASSERT_DEBUG(aIndex >= iPagedStart >> KPageShift);
sl@0
   663
sl@0
   664
	TInt r = DoPageInDone(aMemory,aIndex,aPageInfo,aPageArrayEntry,true);
sl@0
   665
sl@0
   666
	// Rom page can't be decommitted so this must succeed.
sl@0
   667
	__NK_ASSERT_DEBUG(r >= 0);
sl@0
   668
sl@0
   669
	if (aPageInfo->Type() == SPageInfo::EShadow)
sl@0
   670
		{// The page is being shadowed so pin the original page.
sl@0
   671
		// This is safe as the original page was physically pinned when shadowed.
sl@0
   672
		__NK_ASSERT_DEBUG(RPageArray::IsPresent(*aPageArrayEntry));
sl@0
   673
		aPageInfo = aPageInfo->GetOriginalPage();
sl@0
   674
		}
sl@0
   675
sl@0
   676
	ThePager.PagedInPinned(aPageInfo,aPinArgs);
sl@0
   677
sl@0
   678
	// check page assigned correctly...
sl@0
   679
#ifdef _DEBUG
sl@0
   680
	if(RPageArray::IsPresent(*aPageArrayEntry))
sl@0
   681
		{
sl@0
   682
		SPageInfo* pi = SPageInfo::FromPhysAddr(*aPageArrayEntry);
sl@0
   683
		if (pi->Type() != SPageInfo::EShadow)
sl@0
   684
			{
sl@0
   685
			__NK_ASSERT_DEBUG(pi->Type() == SPageInfo::EManaged);
sl@0
   686
			__NK_ASSERT_DEBUG(pi->Owner()==aMemory);
sl@0
   687
			__NK_ASSERT_DEBUG(pi->Index()==aIndex);
sl@0
   688
			__NK_ASSERT_DEBUG(pi->PagedState()==SPageInfo::EPagedPinned);
sl@0
   689
			}
sl@0
   690
		}
sl@0
   691
#endif
sl@0
   692
	return r;
sl@0
   693
	}
sl@0
   694
sl@0
   695
sl@0
   696
void DRomMemoryManager::Unpin(DMemoryObject* aMemory, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs)
sl@0
   697
	{
sl@0
   698
	TRACE(("DRomMemoryManager::Unpin %08x %08x", aMemory, aMapping));
sl@0
   699
	
sl@0
   700
	__ASSERT_CRITICAL;
sl@0
   701
	TUint index = aMapping->iStartIndex;
sl@0
   702
	TUint endIndex = index+aMapping->iSizeInPages;
sl@0
   703
	__NK_ASSERT_DEBUG(endIndex<=iSizeInPages); // Pin() should have already ensured this
sl@0
   704
sl@0
   705
	TUint pagedIndex = iPagedStart>>KPageShift;
sl@0
   706
	if(pagedIndex && pagedIndex<endIndex)
sl@0
   707
		{
sl@0
   708
		TUint start = index;
sl@0
   709
		if(start<pagedIndex)
sl@0
   710
			start = pagedIndex;
sl@0
   711
		// unpin pages (but only if they were successfully pinned)...
sl@0
   712
		if(aMapping->Flags()&DMemoryMapping::EPagesPinned)
sl@0
   713
			DoUnpin(aMemory,start,endIndex-start,aMapping,aPinArgs);
sl@0
   714
		}
sl@0
   715
sl@0
   716
	__NK_ASSERT_DEBUG((aMapping->Flags()&DMemoryMapping::EPageUnmapVetoed)==0); // we shouldn't have tried to Free paged ROM
sl@0
   717
	}
sl@0
   718
sl@0
   719
sl@0
   720
void DRomMemoryManager::DoUnpin(DMemoryObject* aMemory, TUint aIndex, TUint aCount, DMemoryMappingBase* aMapping, TPinArgs& aPinArgs)
sl@0
   721
	{
sl@0
   722
	TRACE(("DRomMemoryManager::DoUnpin(0x%08x,0x%08x,0x%08x,0x%08x,?)",aMemory, aIndex, aCount, aMapping));
sl@0
   723
sl@0
   724
	// This should only be invoked on the paged part of rom.
sl@0
   725
	__NK_ASSERT_DEBUG(iPagedStart && aIndex >= (iPagedStart >> KPageShift));
sl@0
   726
sl@0
   727
	MmuLock::Lock();
sl@0
   728
	TUint endIndex = aIndex+aCount;
sl@0
   729
	for(TUint i = aIndex; i < endIndex; ++i)
sl@0
   730
		{
sl@0
   731
		TPhysAddr page = aMemory->iPages.Page(i);
sl@0
   732
		__NK_ASSERT_DEBUG(RPageArray::IsPresent(page));
sl@0
   733
		SPageInfo* pi = SPageInfo::FromPhysAddr(page);
sl@0
   734
		if(pi->Type() == SPageInfo::EShadow)
sl@0
   735
			{
sl@0
   736
			pi = pi->GetOriginalPage();
sl@0
   737
			}
sl@0
   738
		ThePager.Unpin(pi,aPinArgs);
sl@0
   739
		MmuLock::Flash();
sl@0
   740
		}
sl@0
   741
sl@0
   742
	MmuLock::Unlock();
sl@0
   743
sl@0
   744
	// clear EPagesPinned flag...
sl@0
   745
	__e32_atomic_and_ord8(&aMapping->Flags(), TUint8(~DMemoryMapping::EPagesPinned));
sl@0
   746
	}
sl@0
   747
sl@0
   748
sl@0
   749
void DRomMemoryManager::ShadowLock()
sl@0
   750
	{
sl@0
   751
	MM::MemoryLock(iRomMemory);
sl@0
   752
	}
sl@0
   753
sl@0
   754
sl@0
   755
void DRomMemoryManager::ShadowUnlock()
sl@0
   756
	{
sl@0
   757
	MM::MemoryUnlock(iRomMemory);
sl@0
   758
	}
sl@0
   759
sl@0
   760
sl@0
   761
TInt DRomMemoryManager::AllocShadowPage(TLinAddr aRomAddr)
sl@0
   762
	{
sl@0
   763
	TRACE(("DRomMemoryManager::AllocShadowPage %08x", aRomAddr));
sl@0
   764
	
sl@0
   765
	TUint index = (aRomAddr-iBase)>>KPageShift;
sl@0
   766
	if (index >= iSizeInPages)
sl@0
   767
		return KErrArgument;
sl@0
   768
	__NK_ASSERT_DEBUG(iRomMemory->CheckRegion(index,1));
sl@0
   769
sl@0
   770
	TInt r;
sl@0
   771
sl@0
   772
	ShadowLock();
sl@0
   773
sl@0
   774
	DShadowPage* shadow = (DShadowPage*)iShadowPages.Find(index);
sl@0
   775
	if(shadow)
sl@0
   776
		r = KErrAlreadyExists;
sl@0
   777
	else
sl@0
   778
		{
sl@0
   779
		shadow = DShadowPage::New(iRomMemory,index,iRomMapping);
sl@0
   780
		if(!shadow)
sl@0
   781
			r = KErrNoMemory;
sl@0
   782
		else
sl@0
   783
			{
sl@0
   784
			r = iShadowPages.Add(index,shadow);
sl@0
   785
			if(r!=KErrNone)
sl@0
   786
				{
sl@0
   787
				shadow->Destroy();
sl@0
   788
				}
sl@0
   789
			else
sl@0
   790
				{
sl@0
   791
				// Remap the shadowed rom page to the shadow page.  Update the 
sl@0
   792
				// page array entry for the page being shadowed, this ensures 
sl@0
   793
				// that any page moving attempts will remap the shadow page when
sl@0
   794
				// they realise that the page is physically pinned.
sl@0
   795
				MmuLock::Lock();
sl@0
   796
				TPhysAddr& pageEntry = *iRomMemory->iPages.PageEntry(index);
sl@0
   797
				TPhysAddr newPageAddr = shadow->iNewPage;
sl@0
   798
				pageEntry = (pageEntry & KPageMask) | newPageAddr;
sl@0
   799
sl@0
   800
				// Mark the SPageInfo of the shadow page with pointer to the original page's
sl@0
   801
				// SPageInfo, this is safe as we've physically pinned the original page
sl@0
   802
				// so it can't be freed or reused until this shadow page is destroyed.
sl@0
   803
				SPageInfo* origPi = SPageInfo::FromPhysAddr(shadow->iOriginalPage);
sl@0
   804
				SPageInfo* newPi = SPageInfo::FromPhysAddr(newPageAddr);
sl@0
   805
				newPi->SetOriginalPage(origPi);
sl@0
   806
				MmuLock::Unlock();
sl@0
   807
sl@0
   808
				iRomMemory->RemapPage(pageEntry, index, ETrue);
sl@0
   809
				}
sl@0
   810
			}
sl@0
   811
		}
sl@0
   812
sl@0
   813
	ShadowUnlock();
sl@0
   814
sl@0
   815
	return r;
sl@0
   816
	}
sl@0
   817
sl@0
   818
sl@0
   819
TInt DRomMemoryManager::FreeShadowPage(TLinAddr aRomAddr)
sl@0
   820
	{
sl@0
   821
	TUint index = (aRomAddr-iBase)>>KPageShift;
sl@0
   822
	if(!iRomMemory->CheckRegion(index,1))
sl@0
   823
		return KErrArgument;
sl@0
   824
sl@0
   825
	TInt r;
sl@0
   826
sl@0
   827
	ShadowLock();
sl@0
   828
sl@0
   829
	DShadowPage* shadow = (DShadowPage*)iShadowPages.Remove(index);
sl@0
   830
	if(!shadow)
sl@0
   831
		{
sl@0
   832
		r = KErrNotFound;
sl@0
   833
		}
sl@0
   834
	else
sl@0
   835
		{
sl@0
   836
		// Remap the rom page and update the page array entry for the page
sl@0
   837
		// back to the original rom page.  This is safe as the page is physically 
sl@0
   838
		// pinned until shadow is destroyed.
sl@0
   839
		MmuLock::Lock();
sl@0
   840
		TPhysAddr& pageEntry = *iRomMemory->iPages.PageEntry(index);
sl@0
   841
		pageEntry = (pageEntry & KPageMask) | shadow->iOriginalPage;
sl@0
   842
		MmuLock::Unlock();
sl@0
   843
sl@0
   844
		iRomMemory->RemapPage(pageEntry, index, ETrue);
sl@0
   845
		
sl@0
   846
		shadow->Destroy();
sl@0
   847
		r = KErrNone;
sl@0
   848
		}
sl@0
   849
sl@0
   850
	ShadowUnlock();
sl@0
   851
sl@0
   852
	return r;
sl@0
   853
	}
sl@0
   854
sl@0
   855
sl@0
   856
TInt DRomMemoryManager::CopyToShadowMemory(TLinAddr aDst, TLinAddr aSrc, TUint32 aSize)
sl@0
   857
	{
sl@0
   858
	TRACE(("DRomMemoryManager::CopyToShadowMemory(0x%08x,0x%08x,0x%x)",aDst,aSrc,aSize));
sl@0
   859
	Mmu& m = TheMmu;
sl@0
   860
	TLinAddr offset = aDst-iBase;
sl@0
   861
	TLinAddr end = offset+aSize;
sl@0
   862
	if(end<offset || end>iSize)
sl@0
   863
		return KErrArgument;
sl@0
   864
sl@0
   865
	while(aSize)
sl@0
   866
		{
sl@0
   867
		TUint size = KPageSize-(offset&KPageMask); // bytes left in page at 'offset'
sl@0
   868
		if(size>aSize)
sl@0
   869
			size = aSize;
sl@0
   870
sl@0
   871
		TInt r;
sl@0
   872
sl@0
   873
		ShadowLock();
sl@0
   874
sl@0
   875
		DShadowPage* shadow = (DShadowPage*)iShadowPages.Find(offset>>KPageShift);
sl@0
   876
		if(!shadow)
sl@0
   877
			{
sl@0
   878
			r = KErrNotFound;
sl@0
   879
			}
sl@0
   880
		else
sl@0
   881
			{
sl@0
   882
			RamAllocLock::Lock();
sl@0
   883
			TLinAddr dst = m.MapTemp(shadow->iNewPage,offset>>KPageShift);
sl@0
   884
			dst += offset&KPageMask;
sl@0
   885
			memcpy((TAny*)dst,(TAny*)aSrc,size);
sl@0
   886
			m.UnmapTemp();
sl@0
   887
			RamAllocLock::Unlock();
sl@0
   888
sl@0
   889
			r = KErrNone;
sl@0
   890
			}
sl@0
   891
sl@0
   892
		ShadowUnlock();
sl@0
   893
sl@0
   894
		if(r!=KErrNone)
sl@0
   895
			return r;
sl@0
   896
sl@0
   897
		offset += size;
sl@0
   898
		aSrc += size;
sl@0
   899
		aSize -= size;
sl@0
   900
		}
sl@0
   901
sl@0
   902
	return KErrNone;
sl@0
   903
	}
sl@0
   904
sl@0
   905
sl@0
   906
//
sl@0
   907
// DShadowPage
sl@0
   908
//
sl@0
   909
sl@0
   910
DShadowPage* DShadowPage::New(DMemoryObject* aMemory, TUint aIndex, DMemoryMappingBase* aMapping)
sl@0
   911
	{
sl@0
   912
	TRACE(("DShadowPage::New(0x%08x,0x%x,0x%08x)",aMemory, aIndex, aMapping));
sl@0
   913
	__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory));
sl@0
   914
sl@0
   915
	DShadowPage* self = new DShadowPage;
sl@0
   916
	if(self)
sl@0
   917
		if(self->Construct(aMemory,aIndex,aMapping)!=KErrNone)
sl@0
   918
			{
sl@0
   919
			self->Destroy();
sl@0
   920
			self = 0;
sl@0
   921
			}
sl@0
   922
sl@0
   923
	TRACE(("DShadowPage::New(0x%08x,0x%x,0x%08x) returns 0x%08x",aMemory, aIndex, aMapping, self));
sl@0
   924
	return self;
sl@0
   925
	}
sl@0
   926
sl@0
   927
sl@0
   928
DShadowPage::DShadowPage()
sl@0
   929
	: iOriginalPage(KPhysAddrInvalid), iNewPage(KPhysAddrInvalid)
sl@0
   930
	{
sl@0
   931
	// Set flag so that the rom page that is being shadowed can't be moved, 
sl@0
   932
	// otherwise iOriginalPage will become invalid if the page is moved.
sl@0
   933
	Flags() |= EPhysicalPinningMapping;
sl@0
   934
	}
sl@0
   935
sl@0
   936
sl@0
   937
sl@0
   938
sl@0
   939
TInt DShadowPage::Construct(DMemoryObject* aMemory, TUint aIndex, DMemoryMappingBase* aMapping)
sl@0
   940
	{
sl@0
   941
	__NK_ASSERT_DEBUG(MemoryObjectLock::IsHeld(aMemory));
sl@0
   942
sl@0
   943
	// Pin the page.  It is ok to get the mapping instance count here without
sl@0
   944
	// MmuLock as there is only one permenant mapping used for the ROM.
sl@0
   945
	TInt r = Pin(aMemory,aIndex,1,EUserReadOnly,aMapping,aMapping->MapInstanceCount());
sl@0
   946
	if(r!=KErrNone)
sl@0
   947
		return r;
sl@0
   948
sl@0
   949
	r = PhysAddr(0,1,iOriginalPage,0);
sl@0
   950
	__NK_ASSERT_DEBUG(r>=0);
sl@0
   951
	if(r<0)
sl@0
   952
		return r;
sl@0
   953
sl@0
   954
	RamAllocLock::Lock();
sl@0
   955
sl@0
   956
	Mmu& m = TheMmu;
sl@0
   957
	r = m.AllocRam(&iNewPage, 1, aMemory->RamAllocFlags(), EPageFixed);
sl@0
   958
	if(r==KErrNone)
sl@0
   959
		{
sl@0
   960
		TLinAddr dst = m.MapTemp(iNewPage,aIndex,0);
sl@0
   961
		TLinAddr src = m.MapTemp(iOriginalPage,aIndex,1);
sl@0
   962
		pagecpy((TAny*)dst,(TAny*)src);
sl@0
   963
		CacheMaintenance::CodeChanged(dst,KPageSize); // IMB not needed, just clean to PoU (but we don't have a function to do that)
sl@0
   964
sl@0
   965
		m.UnmapTemp(0);
sl@0
   966
		m.UnmapTemp(1);
sl@0
   967
		MmuLock::Lock();
sl@0
   968
		SPageInfo::FromPhysAddr(iNewPage)->SetShadow(aIndex,aMemory->PageInfoFlags());
sl@0
   969
		MmuLock::Unlock();
sl@0
   970
		}
sl@0
   971
sl@0
   972
	RamAllocLock::Unlock();
sl@0
   973
sl@0
   974
	if(r!=KErrNone)
sl@0
   975
		return r;
sl@0
   976
sl@0
   977
	return r;
sl@0
   978
	}
sl@0
   979
sl@0
   980
sl@0
   981
DShadowPage::~DShadowPage()
sl@0
   982
	{
sl@0
   983
	}
sl@0
   984
sl@0
   985
sl@0
   986
void DShadowPage::Destroy()
sl@0
   987
	{
sl@0
   988
	TRACE2(("DShadowPage[%x]::Destroy()",this));
sl@0
   989
	if(iNewPage!=KPhysAddrInvalid)
sl@0
   990
		{
sl@0
   991
		RamAllocLock::Lock();
sl@0
   992
		TheMmu.FreeRam(&iNewPage, 1, EPageFixed);
sl@0
   993
		RamAllocLock::Unlock();
sl@0
   994
		}
sl@0
   995
	if(IsAttached())
sl@0
   996
		Unpin();
sl@0
   997
	Close();
sl@0
   998
	}
sl@0
   999
sl@0
  1000
sl@0
  1001
/**
sl@0
  1002
Replace a page of the system's execute-in-place (XIP) ROM image with a page of
sl@0
  1003
RAM having the same contents. This RAM can subsequently be written to in order
sl@0
  1004
to apply patches to the XIP ROM or to insert software breakpoints for debugging
sl@0
  1005
purposes.
sl@0
  1006
Call Epoc::FreeShadowPage() when you wish to revert to the original ROM page.
sl@0
  1007
sl@0
  1008
@param	aRomAddr	The virtual address of the ROM page to be replaced.
sl@0
  1009
@return	KErrNone if the operation completed successfully.
sl@0
  1010
		KErrArgument if the specified address is not a valid XIP ROM address.
sl@0
  1011
		KErrNoMemory if the operation failed due to insufficient free RAM.
sl@0
  1012
		KErrAlreadyExists if the XIP ROM page at the specified address has
sl@0
  1013
			already been shadowed by a RAM page.
sl@0
  1014
sl@0
  1015
@pre Calling thread must be in a critical section.
sl@0
  1016
@pre Interrupts must be enabled.
sl@0
  1017
@pre Kernel must be unlocked.
sl@0
  1018
@pre No fast mutex can be held.
sl@0
  1019
@pre Call in a thread context.
sl@0
  1020
*/
sl@0
  1021
EXPORT_C TInt Epoc::AllocShadowPage(TLinAddr aRomAddr)
sl@0
  1022
	{
sl@0
  1023
	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Epoc::AllocShadowPage");
sl@0
  1024
	return DRomMemoryManager::TheManager.AllocShadowPage(aRomAddr);
sl@0
  1025
	}
sl@0
  1026
sl@0
  1027
sl@0
  1028
/**
sl@0
  1029
Copies data into shadow memory. Source data is presumed to be in Kernel memory.
sl@0
  1030
sl@0
  1031
@param	aSrc	Data to copy from.
sl@0
  1032
@param	aDest	Address to copy into.
sl@0
  1033
@param	aLength	Number of bytes to copy. Maximum of 32 bytes of data can be copied.
sl@0
  1034
sl@0
  1035
@return	KErrNone 		if the operation completed successfully.
sl@0
  1036
		KErrArgument 	if any part of destination region is not shadow page or
sl@0
  1037
						if aLength is greater then 32 bytes.
sl@0
  1038
sl@0
  1039
@pre Calling thread must be in a critical section.
sl@0
  1040
@pre Interrupts must be enabled.
sl@0
  1041
@pre Kernel must be unlocked.
sl@0
  1042
@pre No fast mutex can be held.
sl@0
  1043
@pre Call in a thread context.
sl@0
  1044
*/
sl@0
  1045
EXPORT_C TInt Epoc::CopyToShadowMemory(TLinAddr aDest, TLinAddr aSrc, TUint32 aLength)
sl@0
  1046
	{
sl@0
  1047
	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Epoc::CopyToShadowMemory");
sl@0
  1048
	return DRomMemoryManager::TheManager.CopyToShadowMemory(aDest,aSrc,aLength);
sl@0
  1049
	}
sl@0
  1050
sl@0
  1051
sl@0
  1052
/**
sl@0
  1053
Revert an XIP ROM address which has previously been shadowed to the original
sl@0
  1054
page of ROM.
sl@0
  1055
sl@0
  1056
@param	aRomAddr	The virtual address of the ROM page to be reverted.
sl@0
  1057
@return	KErrNone if the operation completed successfully.
sl@0
  1058
		KErrArgument if the specified address is not a valid XIP ROM address.
sl@0
  1059
		KErrGeneral if the specified address has not previously been shadowed
sl@0
  1060
			using Epoc::AllocShadowPage().
sl@0
  1061
sl@0
  1062
@pre Calling thread must be in a critical section.
sl@0
  1063
@pre Interrupts must be enabled.
sl@0
  1064
@pre Kernel must be unlocked.
sl@0
  1065
@pre No fast mutex can be held.
sl@0
  1066
@pre Call in a thread context.
sl@0
  1067
*/
sl@0
  1068
EXPORT_C TInt Epoc::FreeShadowPage(TLinAddr aRomAddr)
sl@0
  1069
	{
sl@0
  1070
	return DRomMemoryManager::TheManager.FreeShadowPage(aRomAddr);
sl@0
  1071
	}
sl@0
  1072
sl@0
  1073
sl@0
  1074
/**
sl@0
  1075
Change the permissions on an XIP ROM address which has previously been shadowed
sl@0
  1076
by a RAM page so that the RAM page may no longer be written to.
sl@0
  1077
sl@0
  1078
Note: Shadow page on the latest platforms (that use the reduced set of access permissions:
sl@0
  1079
arm11mpcore, arm1176, cortex) is implemented with read only permissions. Therefore, calling
sl@0
  1080
this function in not necessary, as shadow page is already created as 'frozen'.
sl@0
  1081
sl@0
  1082
@param	aRomAddr	The virtual address of the shadow RAM page to be frozen.
sl@0
  1083
@return	KErrNone if the operation completed successfully.
sl@0
  1084
		KErrArgument if the specified address is not a valid XIP ROM address.
sl@0
  1085
		KErrGeneral if the specified address has not previously been shadowed
sl@0
  1086
			using Epoc::AllocShadowPage().
sl@0
  1087
sl@0
  1088
@pre Calling thread must be in a critical section.
sl@0
  1089
@pre Interrupts must be enabled.
sl@0
  1090
@pre Kernel must be unlocked.
sl@0
  1091
@pre No fast mutex can be held.
sl@0
  1092
@pre Call in a thread context.
sl@0
  1093
*/
sl@0
  1094
EXPORT_C TInt Epoc::FreezeShadowPage(TLinAddr aRomAddr)
sl@0
  1095
	{
sl@0
  1096
	// Null operation for flexible memory model...
sl@0
  1097
	return KErrNone;
sl@0
  1098
	}
sl@0
  1099
sl@0
  1100