os/kernelhwsrv/kernel/eka/memmodel/epoc/multiple/mmu.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1998-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
// e32\memmodel\epoc\multiple\mmu.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "memmodel.h"
sl@0
    19
#include <ramalloc.h>
sl@0
    20
sl@0
    21
_LIT(KLitGlobalDollarCode,"GLOBAL$CODE");
sl@0
    22
sl@0
    23
/*******************************************************************************
sl@0
    24
 * "Independent" MMU code
sl@0
    25
 *******************************************************************************/
sl@0
    26
sl@0
    27
void Mmu::Panic(TPanic aPanic)
sl@0
    28
	{
sl@0
    29
	Kern::Fault("MMU",aPanic);
sl@0
    30
	}
sl@0
    31
sl@0
    32
TPde* Mmu::LocalPageDir(TInt aOsAsid)
sl@0
    33
	{
sl@0
    34
	__ASSERT_DEBUG(TUint32(aOsAsid)<TUint32(iNumOsAsids),Panic(ELocalPageDirBadAsid));
sl@0
    35
	return (TPde*)(iPdeBase+(aOsAsid<<iGlobalPdShift));
sl@0
    36
	}
sl@0
    37
sl@0
    38
TPde* Mmu::GlobalPageDir(TInt aOsAsid)
sl@0
    39
	{
sl@0
    40
	__ASSERT_DEBUG(TUint32(aOsAsid)<TUint32(iNumOsAsids),Panic(EGlobalPageDirBadAsid));
sl@0
    41
	if (iAsidInfo[aOsAsid]&1)
sl@0
    42
		return (TPde*)(iPdeBase+(aOsAsid<<iGlobalPdShift));
sl@0
    43
	return (TPde*)iPdeBase;
sl@0
    44
	}
sl@0
    45
/*
sl@0
    46
TPde& Mmu::PDE(TLinAddr aAddr, TInt aOsAsid)
sl@0
    47
	{
sl@0
    48
	__ASSERT_DEBUG(TUint32(aOsAsid)<TUint32(iNumOsAsids),Panic(EPDEBadAsid));
sl@0
    49
	TPde* p=(TPde*)(iPdeBase+(aOsAsid<<iGlobalPdShift));
sl@0
    50
	if (aAddr>=iUserSharedEnd && (iAsidInfo[aOsAsid]&1))
sl@0
    51
		p=(TPde*)iPdeBase;
sl@0
    52
	p+=(aAddr>>iChunkShift);
sl@0
    53
	__KTRACE_OPT(KMMU,Kern::Printf("PDE(%08x,%d) at %08x",aAddr,aOsAsid,p));
sl@0
    54
	return *p;
sl@0
    55
	}
sl@0
    56
*/
sl@0
    57
TInt Mmu::NewOsAsid(TBool aSeparateGlobal)
sl@0
    58
//
sl@0
    59
// Allocate a new OS ASID and page directory.
sl@0
    60
// Map the page directory at the expected linear address and initialise it.
sl@0
    61
//
sl@0
    62
	{
sl@0
    63
	__KTRACE_OPT(KMMU,Kern::Printf("Mmu::NewOsAsid(%x)",aSeparateGlobal));
sl@0
    64
	TInt os_asid=iOsAsidAllocator->Alloc();
sl@0
    65
	if (os_asid<0)
sl@0
    66
		return KErrNoMemory;
sl@0
    67
	TPhysAddr pdPhys;
sl@0
    68
	TInt pdPages=0;
sl@0
    69
	TInt r=NewPageDirectory(os_asid,aSeparateGlobal,pdPhys,pdPages);
sl@0
    70
	__KTRACE_OPT(KMMU,Kern::Printf("NewPageDirectory: %d %08x %d",r,pdPhys,pdPages));
sl@0
    71
	if (r!=KErrNone)
sl@0
    72
		{
sl@0
    73
		iOsAsidAllocator->Free(os_asid);
sl@0
    74
		return KErrNoMemory;
sl@0
    75
		}
sl@0
    76
	TBool global=(pdPages<<iPageShift==iGlobalPdSize)?1:0;
sl@0
    77
	TLinAddr pdLin=iPdeBase+(os_asid<<iGlobalPdShift);
sl@0
    78
	if (((os_asid & iAsidGroupMask)==0) && (!iOsAsidAllocator->NotFree(os_asid+1,iAsidGroupMask)) )
sl@0
    79
		{
sl@0
    80
		// expand page directory mapping
sl@0
    81
		TInt xptid=AllocPageTable();
sl@0
    82
		if (xptid<0)
sl@0
    83
			{
sl@0
    84
			iRamPageAllocator->FreePhysicalRam(pdPhys,pdPages<<iPageShift);
sl@0
    85
			iOsAsidAllocator->Free(os_asid);
sl@0
    86
			return KErrNoMemory;
sl@0
    87
			}
sl@0
    88
		AssignPageTable(xptid, SPageTableInfo::EGlobal, NULL, pdLin, iPdPdePerm);	// map XPT
sl@0
    89
		}
sl@0
    90
	TInt i;
sl@0
    91
	for (i=0; i<pdPages; ++i)
sl@0
    92
		MapRamPage(pdLin+(i<<iPageShift), pdPhys+(i<<iPageShift), iPdPtePerm);
sl@0
    93
	InitPageDirectory(os_asid, global);
sl@0
    94
	iNumGlobalPageDirs+=global;
sl@0
    95
	iAsidInfo[os_asid]=global;
sl@0
    96
	__KTRACE_OPT(KMMU,Kern::Printf("Mmu::NewOsAsid returns %d (%d)",os_asid,global));
sl@0
    97
	return os_asid;
sl@0
    98
	}
sl@0
    99
sl@0
   100
void Mmu::FreeOsAsid(TInt aOsAsid)
sl@0
   101
//
sl@0
   102
// Free an OS ASID and the corresponding page directory.
sl@0
   103
// Assumes any local PDEs have already been unmapped.
sl@0
   104
//
sl@0
   105
	{
sl@0
   106
	__KTRACE_OPT(KMMU,Kern::Printf("Mmu::FreeOsAsid(%d)",aOsAsid));
sl@0
   107
	__ASSERT_DEBUG(TUint32(aOsAsid)<TUint32(iNumOsAsids),Panic(EFreeOsAsidBadAsid));
sl@0
   108
	TBool global=iAsidInfo[aOsAsid]&1;
sl@0
   109
	iAsidInfo[aOsAsid]=0;
sl@0
   110
	iOsAsidAllocator->Free(aOsAsid);
sl@0
   111
	iNumGlobalPageDirs-=global;
sl@0
   112
	TLinAddr pdLin=iPdeBase+(aOsAsid<<iGlobalPdShift);
sl@0
   113
	TUint32 size=global?iGlobalPdSize:iLocalPdSize;
sl@0
   114
	UnmapAndFree(pdLin,size>>iPageShift);
sl@0
   115
#ifdef BTRACE_KERNEL_MEMORY
sl@0
   116
	BTrace4(BTrace::EKernelMemory, BTrace::EKernelMemoryMiscFree, size);
sl@0
   117
	Epoc::KernelMiscPages -= size>>iPageShift;
sl@0
   118
#endif
sl@0
   119
	TInt asid_group=aOsAsid&~iAsidGroupMask;
sl@0
   120
	if (!iOsAsidAllocator->NotFree(asid_group,iAsidGroupSize))
sl@0
   121
		{
sl@0
   122
		// shrink page directory mapping
sl@0
   123
		TInt xptid=PageTableId(pdLin,0);
sl@0
   124
		DoUnassignPageTable(pdLin, GLOBAL_MAPPING);
sl@0
   125
		FreePageTable(xptid);
sl@0
   126
		}
sl@0
   127
	}
sl@0
   128
sl@0
   129
TPhysAddr Mmu::LinearToPhysical(TLinAddr aLinAddr)
sl@0
   130
//
sl@0
   131
// Find the physical address corresponding to a given linear address
sl@0
   132
// Call with system locked
sl@0
   133
//
sl@0
   134
	{
sl@0
   135
	DMemModelProcess* pP=(DMemModelProcess*)TheCurrentThread->iOwningProcess;
sl@0
   136
	return LinearToPhysical(aLinAddr, pP->iOsAsid);
sl@0
   137
	}
sl@0
   138
sl@0
   139
TInt Mmu::LinearToPhysical(TLinAddr aLinAddr, TInt aSize, TPhysAddr& aPhysicalAddress, TPhysAddr* aPhysicalPageList)
sl@0
   140
	{
sl@0
   141
	DMemModelProcess* pP=(DMemModelProcess*)TheCurrentThread->iOwningProcess;
sl@0
   142
	return LinearToPhysical(aLinAddr, aSize, aPhysicalAddress, aPhysicalPageList, pP->iOsAsid);
sl@0
   143
	}
sl@0
   144
sl@0
   145
TInt Mmu::PageTableId(TLinAddr aAddr)
sl@0
   146
	{
sl@0
   147
	DMemModelProcess* pP=(DMemModelProcess*)TheCurrentThread->iOwningProcess;
sl@0
   148
	return PageTableId(aAddr, pP->iOsAsid);
sl@0
   149
	}
sl@0
   150
sl@0
   151
void Mmu::Init1()
sl@0
   152
	{
sl@0
   153
	__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("Mmu::Init1"));
sl@0
   154
	iMaxPageTables=65535;		// possibly reduced when RAM size known
sl@0
   155
	memclr(iAsidInfo, iNumOsAsids*sizeof(TUint32));
sl@0
   156
	MmuBase::Init1();
sl@0
   157
	}
sl@0
   158
sl@0
   159
void Mmu::CreateUserGlobalSection(TLinAddr aBase, TLinAddr aEnd)
sl@0
   160
	{
sl@0
   161
	iUserGlobalSection=TLinearSection::New(aBase, aEnd);
sl@0
   162
	__ASSERT_ALWAYS(iUserGlobalSection,Panic(ECreateUserGlobalSectionFailed));
sl@0
   163
	}
sl@0
   164
sl@0
   165
void Mmu::DoInit2()
sl@0
   166
	{
sl@0
   167
	__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("Mmu::DoInit2"));
sl@0
   168
	iSharedSection=TLinearSection::New(iUserSharedBase, iUserSharedEnd);
sl@0
   169
	__ASSERT_ALWAYS(iSharedSection,Panic(ECreateSharedSectionFailed));
sl@0
   170
	iOsAsidAllocator=TBitMapAllocator::New(iNumOsAsids,ETrue);
sl@0
   171
	__ASSERT_ALWAYS(iOsAsidAllocator,Panic(EOsAsidAllocCreateFailed));
sl@0
   172
	iOsAsidAllocator->Alloc(0,1);	// 0=kernel process
sl@0
   173
	DMemModelProcess* pP=(DMemModelProcess*)K::TheKernelProcess;
sl@0
   174
	if (iLocalPdSize)
sl@0
   175
		pP->iLocalPageDir=LinearToPhysical(TLinAddr(LocalPageDir(0)));
sl@0
   176
	pP->iGlobalPageDir=LinearToPhysical(TLinAddr(GlobalPageDir(0)));
sl@0
   177
	__KTRACE_OPT(KMMU,Kern::Printf("Kernel process: LPD=%08x GPD=%08x",pP->iLocalPageDir,pP->iGlobalPageDir));
sl@0
   178
	MM::UserCodeAllocator=TBitMapAllocator::New(iMaxUserCodeSize>>iAliasShift, ETrue);	// code is aligned to alias size
sl@0
   179
	__ASSERT_ALWAYS(MM::UserCodeAllocator,Panic(EUserCodeAllocatorCreateFailed));
sl@0
   180
	MM::DllDataAllocator=TBitMapAllocator::New(iMaxDllDataSize>>iPageShift, ETrue);
sl@0
   181
	__ASSERT_ALWAYS(MM::DllDataAllocator,Panic(EDllDataAllocatorCreateFailed));
sl@0
   182
	__ASSERT_ALWAYS(TheRomHeader().iUserDataAddress==iDllDataBase+iMaxDllDataSize,Panic(ERomUserDataAddressInvalid));
sl@0
   183
	__ASSERT_ALWAYS((TheRomHeader().iTotalUserDataSize&iPageMask)==0,Panic(ERomUserDataSizeInvalid));
sl@0
   184
	TInt rom_dll_pages=TheRomHeader().iTotalUserDataSize>>iPageShift;
sl@0
   185
	__KTRACE_OPT2(KBOOT,KMMU,Kern::Printf("UserCodeAllocator @ %08x DllDataAllocator @ %08x, %d ROM DLL Data Pages",
sl@0
   186
		MM::UserCodeAllocator, MM::DllDataAllocator, rom_dll_pages));
sl@0
   187
	if (rom_dll_pages)
sl@0
   188
		MM::DllDataAllocator->Alloc(0, rom_dll_pages);	// low bit numbers represent high addresses
sl@0
   189
	}
sl@0
   190
sl@0
   191
void Mmu::SetupInitialPageInfo(SPageInfo* aPageInfo, TLinAddr aChunkAddr, TInt aPdeIndex)
sl@0
   192
	{
sl@0
   193
	__ASSERT_ALWAYS(aChunkAddr>=iUserSharedEnd,Panic(EBadInitialPageAddr));
sl@0
   194
	TLinAddr addr=aChunkAddr+(aPdeIndex<<iPageShift);
sl@0
   195
	if (aPageInfo->Type()!=SPageInfo::EUnused)
sl@0
   196
		return;	// already set (page table)
sl@0
   197
	if (addr==(TLinAddr)iPtInfo)
sl@0
   198
		{
sl@0
   199
		aPageInfo->SetPtInfo(0);
sl@0
   200
		aPageInfo->Lock();
sl@0
   201
		}
sl@0
   202
	else if (addr>=iPdeBase && addr<iPdeBase+iGlobalPdSize)
sl@0
   203
		{
sl@0
   204
		aPageInfo->SetPageDir(0,aPdeIndex);
sl@0
   205
		aPageInfo->Lock();
sl@0
   206
		}
sl@0
   207
	else
sl@0
   208
		aPageInfo->SetFixed();
sl@0
   209
	}
sl@0
   210
sl@0
   211
void Mmu::SetupInitialPageTableInfo(TInt aId, TLinAddr aChunkAddr, TInt aNumPtes)
sl@0
   212
	{
sl@0
   213
	__ASSERT_ALWAYS(aChunkAddr>=iUserSharedEnd || aChunkAddr==0,Panic(EBadInitialPageAddr));
sl@0
   214
	SPageTableInfo& pti=PtInfo(aId);
sl@0
   215
	pti.iCount=aNumPtes;
sl@0
   216
	pti.SetGlobal(aChunkAddr>>iChunkShift);
sl@0
   217
	}
sl@0
   218
sl@0
   219
void Mmu::AssignPageTable(TInt aId, TInt aUsage, TAny* aObject, TLinAddr aAddr, TPde aPdePerm)
sl@0
   220
	{
sl@0
   221
	__KTRACE_OPT(KMMU,Kern::Printf("Mmu::AssignPageTable id=%d, u=%08x, obj=%08x, addr=%08x, perm=%08x",
sl@0
   222
					aId, aUsage, aObject, aAddr, aPdePerm));
sl@0
   223
	const TAny* asids=GLOBAL_MAPPING;
sl@0
   224
	SPageTableInfo& pti=PtInfo(aId);
sl@0
   225
	switch (aUsage)
sl@0
   226
		{
sl@0
   227
		case SPageTableInfo::EChunk:
sl@0
   228
			{
sl@0
   229
			DMemModelChunk* pC=(DMemModelChunk*)aObject;
sl@0
   230
			TUint32 ccp=K::CompressKHeapPtr(pC);
sl@0
   231
			TUint32 offset=(aAddr-TLinAddr(pC->iBase))>>iChunkShift;
sl@0
   232
			pti.SetChunk(ccp,offset);
sl@0
   233
			if (pC->iOsAsids)
sl@0
   234
				asids=pC->iOsAsids;
sl@0
   235
			else if (pC->iOwningProcess)
sl@0
   236
				asids=(const TAny*)((DMemModelProcess*)pC->iOwningProcess)->iOsAsid;
sl@0
   237
			break;
sl@0
   238
			}
sl@0
   239
//		case SPageTableInfo::EHwChunk:
sl@0
   240
//			break;
sl@0
   241
		case SPageTableInfo::EGlobal:
sl@0
   242
			pti.SetGlobal(aAddr>>iChunkShift);
sl@0
   243
			break;
sl@0
   244
		default:
sl@0
   245
			Panic(EAssignPageTableInvalidUsage);
sl@0
   246
		}
sl@0
   247
	DoAssignPageTable(aId, aAddr, aPdePerm, asids);
sl@0
   248
	}
sl@0
   249
sl@0
   250
TInt Mmu::UnassignPageTable(TLinAddr aAddr)
sl@0
   251
	{
sl@0
   252
	__KTRACE_OPT(KMMU,Kern::Printf("Mmu::UnassignPageTable addr=%08x", aAddr));
sl@0
   253
	TInt id=PageTableId(aAddr, 0);
sl@0
   254
	if (id>=0)
sl@0
   255
		DoUnassignPageTable(aAddr, GLOBAL_MAPPING);
sl@0
   256
	return id;
sl@0
   257
	}
sl@0
   258
sl@0
   259
TInt Mmu::CreateGlobalCodeChunk()
sl@0
   260
//
sl@0
   261
// Enter and return with neither system lock nor MMU mutex held
sl@0
   262
//
sl@0
   263
	{
sl@0
   264
	__KTRACE_OPT(KDLL,Kern::Printf("Mmu::CreateGlobalCodeChunk"));
sl@0
   265
	TInt maxsize=Min(TheSuperPage().iTotalRamSize/2, 0x01000000);
sl@0
   266
	SChunkCreateInfo c;
sl@0
   267
	c.iGlobal=ETrue;
sl@0
   268
	c.iAtt=TChunkCreate::EDisconnected;
sl@0
   269
	c.iForceFixed=EFalse;
sl@0
   270
	c.iOperations=SChunkCreateInfo::EAdjust|SChunkCreateInfo::EAdd;
sl@0
   271
	c.iRunAddress=0;
sl@0
   272
	c.iPreallocated=0;
sl@0
   273
	c.iType=EDll;
sl@0
   274
	c.iMaxSize=maxsize;
sl@0
   275
	c.iName.Set(KLitGlobalDollarCode);
sl@0
   276
	c.iOwner=NULL;
sl@0
   277
	c.iInitialBottom=0;
sl@0
   278
	c.iInitialTop=0;
sl@0
   279
	TLinAddr runAddr;
sl@0
   280
	return K::TheKernelProcess->NewChunk((DChunk*&)iGlobalCode,c,runAddr);
sl@0
   281
	}