os/kernelhwsrv/kernel/eka/memmodel/epoc/flexible/mmu/maddressspace.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 "mm.h"
sl@0
    18
#include "mmu.h"
sl@0
    19
sl@0
    20
#include "maddressspace.h"
sl@0
    21
#include "mpdalloc.h"
sl@0
    22
#include "mmapping.h"
sl@0
    23
sl@0
    24
sl@0
    25
sl@0
    26
/**
sl@0
    27
Allocator for OS Address Space IDs (OS ASIDs).
sl@0
    28
This is a simple bitmap allocator for KNumOsAsids integers with an
sl@0
    29
associated mutex to guard against concurrency when allocating and
sl@0
    30
freeing.
sl@0
    31
*/
sl@0
    32
class OsAsidAllocator
sl@0
    33
	{
sl@0
    34
public:
sl@0
    35
	void Init2()
sl@0
    36
		{
sl@0
    37
		iAllocator = TBitMapAllocator::New(KNumOsAsids,ETrue);
sl@0
    38
		__NK_ASSERT_ALWAYS(iAllocator);
sl@0
    39
		iAllocator->Alloc(KKernelOsAsid,1); // make kernel OS ASID already allocated
sl@0
    40
		}
sl@0
    41
sl@0
    42
	TInt Alloc()
sl@0
    43
		{
sl@0
    44
		NKern::FMWait(&iLock);
sl@0
    45
		TInt osAsid = iAllocator->Alloc();
sl@0
    46
		NKern::FMSignal(&iLock);
sl@0
    47
		if(osAsid<0)
sl@0
    48
			return KErrNoMemory;
sl@0
    49
		return osAsid;
sl@0
    50
		}
sl@0
    51
sl@0
    52
	void Free(TInt aOsAsid)
sl@0
    53
		{
sl@0
    54
		NKern::FMWait(&iLock);
sl@0
    55
		iAllocator->Free(aOsAsid);
sl@0
    56
		NKern::FMSignal(&iLock);
sl@0
    57
		}
sl@0
    58
sl@0
    59
private:
sl@0
    60
	TBitMapAllocator* iAllocator;
sl@0
    61
	NFastMutex iLock;
sl@0
    62
	}
sl@0
    63
OsAsidAllocator;
sl@0
    64
sl@0
    65
sl@0
    66
//
sl@0
    67
// DAddressSpace
sl@0
    68
//
sl@0
    69
sl@0
    70
DAddressSpace KernelAddressSpace;	///< The kernel's address space object.
sl@0
    71
sl@0
    72
__ASSERT_COMPILE(KKernelOsAsid==0);
sl@0
    73
DAddressSpace* AddressSpace[KNumOsAsids] = { &KernelAddressSpace };
sl@0
    74
sl@0
    75
RVirtualAllocator DAddressSpace::UserGlobalVirtualAllocator;
sl@0
    76
RBackwardsVirtualAllocator DAddressSpace::UserCommonVirtualAllocator;
sl@0
    77
sl@0
    78
/**
sl@0
    79
The read lock used for protecting the mappings container in address spaces (DAddressSpace::iMappings).
sl@0
    80
A single global lock is used for all processes - this isn't required but it is the simplest
sl@0
    81
implementation if we want to avoid the memory overhead of allocating a mutex per address space.
sl@0
    82
*/
sl@0
    83
NFastMutex TheAddressSpaceMappingLock;
sl@0
    84
sl@0
    85
sl@0
    86
/**
sl@0
    87
A pool of mutexes which are used to protect an address space's virtual address allocation
sl@0
    88
and acts as a write lock for the mappings container (DAddressSpace::iMappings).
sl@0
    89
*/
sl@0
    90
DMutexPool AddressSpaceMutexPool;
sl@0
    91
sl@0
    92
sl@0
    93
void DAddressSpace::Init2()
sl@0
    94
	{
sl@0
    95
	// create allocator for ASIDs...
sl@0
    96
	OsAsidAllocator.Init2();
sl@0
    97
sl@0
    98
	// construct the kernel's address space...
sl@0
    99
	TInt r = KernelAddressSpace.Construct(0, KKernelSectionBase, KKernelSectionEnd);
sl@0
   100
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   101
sl@0
   102
	// mark primary i/o region as already allocated...
sl@0
   103
	__ASSERT_COMPILE(((KPrimaryIOBase|KPrimaryIOEnd)&KChunkMask)==0); // region must be chunk aligned to avoid PDE type conflicts with any new allocations
sl@0
   104
	TLinAddr addr;
sl@0
   105
	TUint size;
sl@0
   106
	r = KernelAddressSpace.AllocateVirtualMemory(addr,size,KPrimaryIOBase,KPrimaryIOEnd-KPrimaryIOBase,0);
sl@0
   107
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   108
sl@0
   109
	// construct user global memory allocator...
sl@0
   110
	r = UserGlobalVirtualAllocator.Construct(KGlobalMemoryBase,KUserMemoryLimit,ENumVirtualAllocTypes,AddressSpace[KKernelOsAsid]->iLock);
sl@0
   111
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   112
sl@0
   113
	// construct user common memory allocator (two slab types, one each for paged and unpaged memory)...
sl@0
   114
	r = UserCommonVirtualAllocator.Construct(KUserLocalDataBase,KUserLocalDataEnd,ENumVirtualAllocTypes,AddressSpace[KKernelOsAsid]->iLock);
sl@0
   115
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   116
sl@0
   117
	// reserve virtual memory for XIP user code...
sl@0
   118
	TUint romDataSize = TheRomHeader().iTotalUserDataSize;
sl@0
   119
	TLinAddr romDataBase = TheRomHeader().iUserDataAddress-romDataSize;
sl@0
   120
	__NK_ASSERT_DEBUG(TheRomHeader().iUserDataAddress==KUserLocalDataEnd);
sl@0
   121
	if(romDataSize)
sl@0
   122
		{
sl@0
   123
		r = UserCommonVirtualAllocator.Alloc(addr,size,romDataBase,romDataSize,0);
sl@0
   124
		__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
   125
		}
sl@0
   126
	}
sl@0
   127
sl@0
   128
sl@0
   129
DAddressSpace::DAddressSpace()
sl@0
   130
	: iMappings(&TheAddressSpaceMappingLock,iLock)
sl@0
   131
	{
sl@0
   132
	}
sl@0
   133
sl@0
   134
sl@0
   135
TInt DAddressSpace::New(TPhysAddr& aPageDirectory)
sl@0
   136
	{
sl@0
   137
	TRACE(("DAddressSpace::New(?)"));
sl@0
   138
	TInt r;
sl@0
   139
	TInt osAsid = OsAsidAllocator.Alloc();
sl@0
   140
	if(osAsid<0)
sl@0
   141
		r = KErrNoMemory;
sl@0
   142
	else
sl@0
   143
		{
sl@0
   144
		r = PageDirectories.Alloc(osAsid,aPageDirectory);
sl@0
   145
		if(r!=KErrNone)
sl@0
   146
			OsAsidAllocator.Free(osAsid);
sl@0
   147
		else
sl@0
   148
			{
sl@0
   149
			DAddressSpace*& info = AddressSpace[osAsid];
sl@0
   150
			__NK_ASSERT_DEBUG(!info);
sl@0
   151
			info = new DAddressSpace();
sl@0
   152
			if(!info)
sl@0
   153
				{
sl@0
   154
				PageDirectories.Free(osAsid);
sl@0
   155
				OsAsidAllocator.Free(osAsid);
sl@0
   156
				r = KErrNoMemory;
sl@0
   157
				}
sl@0
   158
			else
sl@0
   159
				{
sl@0
   160
				r = info->Construct(osAsid,KUserLocalDataBase,KUserLocalDataEnd);
sl@0
   161
				if(r!=KErrNone)
sl@0
   162
					{
sl@0
   163
					info->Close();
sl@0
   164
					info = 0;
sl@0
   165
					}
sl@0
   166
				}
sl@0
   167
			}
sl@0
   168
		}
sl@0
   169
sl@0
   170
	if(r==KErrNone)
sl@0
   171
		r = osAsid;
sl@0
   172
	else
sl@0
   173
		aPageDirectory = KPhysAddrInvalid;
sl@0
   174
sl@0
   175
	TRACE(("DAddressSpace::New returns %d",r));
sl@0
   176
	return r;
sl@0
   177
	}
sl@0
   178
sl@0
   179
sl@0
   180
sl@0
   181
DAddressSpace::~DAddressSpace()
sl@0
   182
	{
sl@0
   183
	TRACE(("DAddressSpace[0x%08x]::~DAddressSpace() osAsid = %d",this,iOsAsid));
sl@0
   184
#ifdef _DEBUG
sl@0
   185
	if(iMappings.Count())
sl@0
   186
		Dump();
sl@0
   187
#endif
sl@0
   188
	__NK_ASSERT_DEBUG(iMappings.Count()==0);
sl@0
   189
sl@0
   190
	TInt osAsid = iOsAsid;
sl@0
   191
	AddressSpace[osAsid] = 0;
sl@0
   192
	PageDirectories.Free(osAsid);
sl@0
   193
	InvalidateTLBForAsid(osAsid);
sl@0
   194
	OsAsidAllocator.Free(osAsid);
sl@0
   195
	}
sl@0
   196
sl@0
   197
sl@0
   198
TInt DAddressSpace::Construct(TInt aOsAsid, TLinAddr aStart, TLinAddr aEnd)
sl@0
   199
	{
sl@0
   200
	TRACE(("DAddressSpace::Construct(%d,0x%08x,0x%08x)",aOsAsid,aStart,aEnd));
sl@0
   201
	iOsAsid = aOsAsid;
sl@0
   202
	return iVirtualAllocator.Construct(aStart,aEnd,ENumVirtualAllocTypes,iLock);
sl@0
   203
	}
sl@0
   204
sl@0
   205
sl@0
   206
void DAddressSpace::Lock()
sl@0
   207
	{
sl@0
   208
	AddressSpaceMutexPool.Wait(iLock);
sl@0
   209
	}
sl@0
   210
sl@0
   211
sl@0
   212
void DAddressSpace::Unlock()
sl@0
   213
	{
sl@0
   214
	AddressSpaceMutexPool.Signal(iLock);
sl@0
   215
	}
sl@0
   216
sl@0
   217
sl@0
   218
TInt DAddressSpace::AllocateVirtualMemory(TLinAddr& aAddr, TUint& aSize, TLinAddr aRequestedAddr, TUint aRequestedSize, TUint aPdeType)
sl@0
   219
	{
sl@0
   220
	TRACE(("DAddressSpace::AllocateVirtualMemory(?,?,0x%08x,0x%08x,%d) osAsid=%d",aRequestedAddr,aRequestedSize,aPdeType,iOsAsid));
sl@0
   221
	__NK_ASSERT_DEBUG(aPdeType<ENumVirtualAllocTypes);
sl@0
   222
	Lock();
sl@0
   223
	TInt r = iVirtualAllocator.Alloc(aAddr,aSize,aRequestedAddr,aRequestedSize,aPdeType);
sl@0
   224
	if(r==KErrNone)
sl@0
   225
		Open();
sl@0
   226
	Unlock();
sl@0
   227
	TRACE(("DAddressSpace::AllocateVirtualMemory returns %d region=0x%08x+0x%08x",r,aAddr,aSize));
sl@0
   228
	return r;
sl@0
   229
	}
sl@0
   230
sl@0
   231
sl@0
   232
TInt DAddressSpace::AllocateUserGlobalVirtualMemory(TLinAddr& aAddr, TUint& aSize, TLinAddr aRequestedAddr, TUint aRequestedSize, TUint aPdeType)
sl@0
   233
	{
sl@0
   234
	TRACE(("DAddressSpace::AllocateUserGlobalVirtualMemory(?,?,0x%08x,0x%08x,%d)",aRequestedAddr,aRequestedSize,aPdeType));
sl@0
   235
	__NK_ASSERT_DEBUG(aPdeType<ENumVirtualAllocTypes);
sl@0
   236
	KernelAddressSpace.Lock();
sl@0
   237
	TInt r = UserGlobalVirtualAllocator.Alloc(aAddr,aSize,aRequestedAddr,aRequestedSize,aPdeType);
sl@0
   238
	KernelAddressSpace.Unlock();
sl@0
   239
	TRACE(("DAddressSpace::AllocateUserGlobalVirtualMemory returns %d region=0x%08x+0x%08x",r,aAddr,aSize));
sl@0
   240
	return r;
sl@0
   241
	}
sl@0
   242
sl@0
   243
sl@0
   244
void DAddressSpace::FreeVirtualMemory(TLinAddr aAddr, TUint aSize)
sl@0
   245
	{
sl@0
   246
	TRACE(("DAddressSpace::FreeVirtualMemory(0x%08x,0x%08x) osAsid=%d",aAddr, aSize, iOsAsid));
sl@0
   247
	Lock();
sl@0
   248
	if(iOsAsid==(TInt)KKernelOsAsid && UserGlobalVirtualAllocator.InRange(aAddr,aSize))
sl@0
   249
		UserGlobalVirtualAllocator.Free(aAddr,aSize);
sl@0
   250
	else
sl@0
   251
		{
sl@0
   252
		iVirtualAllocator.Free(aAddr,aSize);
sl@0
   253
		AsyncClose();
sl@0
   254
		}
sl@0
   255
	Unlock();
sl@0
   256
	}
sl@0
   257
sl@0
   258
sl@0
   259
TInt DAddressSpace::AllocateUserCommonVirtualMemory(TLinAddr& aAddr, TUint& aSize, TLinAddr aRequestedAddr, TUint aRequestedSize, TUint aPdeType)
sl@0
   260
	{
sl@0
   261
	TRACE(("DAddressSpace::AllocateUserCommonVirtualMemory(?,?,0x%08x,0x%08x,%d)",aRequestedAddr,aRequestedSize,aPdeType));
sl@0
   262
	__NK_ASSERT_DEBUG(aPdeType<ENumVirtualAllocTypes);
sl@0
   263
	KernelAddressSpace.Lock();
sl@0
   264
	TInt r = UserCommonVirtualAllocator.Alloc(aAddr,aSize,aRequestedAddr,aRequestedSize,aPdeType);
sl@0
   265
	KernelAddressSpace.Unlock();
sl@0
   266
	TRACE(("DAddressSpace::AllocateUserCommonVirtualMemory returns %d region=0x%08x+0x%08x",r,aAddr,aSize));
sl@0
   267
	return r;
sl@0
   268
	}
sl@0
   269
sl@0
   270
sl@0
   271
void DAddressSpace::FreeUserCommonVirtualMemory(TLinAddr aAddr, TUint aSize)
sl@0
   272
	{
sl@0
   273
	TRACE(("DAddressSpace::FreeUserCommonVirtualMemory(0x%08x,0x%08x)",aAddr,aSize));
sl@0
   274
	KernelAddressSpace.Lock();
sl@0
   275
	UserCommonVirtualAllocator.Free(aAddr,aSize);
sl@0
   276
	KernelAddressSpace.Unlock();
sl@0
   277
	}
sl@0
   278
sl@0
   279
sl@0
   280
TInt DAddressSpace::AddMapping(TLinAddr aAddr, DMemoryMapping* aMapping)
sl@0
   281
	{
sl@0
   282
	Lock();
sl@0
   283
	TRACE(("DAddressSpace::AddMapping(0x%08x,0x%08x) osAsid=%d",aAddr, aMapping, iOsAsid));
sl@0
   284
	TInt r = iMappings.Add(aAddr,aMapping);
sl@0
   285
	TRACE(("DAddressSpace::AddMapping osAsid=%d returns %d",iOsAsid, r));
sl@0
   286
	Unlock();
sl@0
   287
	return r;
sl@0
   288
	}
sl@0
   289
sl@0
   290
sl@0
   291
DMemoryMapping* DAddressSpace::RemoveMapping(TLinAddr aAddr)
sl@0
   292
	{
sl@0
   293
	Lock();
sl@0
   294
	DMemoryMapping* removed = (DMemoryMapping*)iMappings.Remove(aAddr);
sl@0
   295
	TRACE(("DAddressSpace::RemoveMapping(0x%08x) osAsid=%d returns 0x%08x",aAddr, iOsAsid, removed));
sl@0
   296
	Unlock();
sl@0
   297
	return removed;
sl@0
   298
	}
sl@0
   299
sl@0
   300
sl@0
   301
DMemoryMapping* DAddressSpace::GetMapping(TLinAddr aAddr)
sl@0
   302
	{
sl@0
   303
	iMappings.ReadLock();
sl@0
   304
	DMemoryMapping* mapping = (DMemoryMapping*)iMappings.Find(aAddr);
sl@0
   305
	TRACE(("DAddressSpace::GetMapping(0x%08x) osAsid=%d returns 0x%08x",aAddr, iOsAsid, mapping));
sl@0
   306
	__NK_ASSERT_DEBUG(mapping); // caller must know there is a mapping
sl@0
   307
	iMappings.ReadUnlock();
sl@0
   308
	return mapping;
sl@0
   309
	}
sl@0
   310
sl@0
   311
sl@0
   312
DMemoryMapping* DAddressSpace::FindMapping(TLinAddr aAddr, TUint aSize, TUint& aOffsetInMapping, TUint& aInstanceCount)
sl@0
   313
	{
sl@0
   314
	__ASSERT_CRITICAL;
sl@0
   315
sl@0
   316
	DMemoryMapping* result = NULL;
sl@0
   317
sl@0
   318
	// find mapping...
sl@0
   319
	iMappings.ReadLock();
sl@0
   320
	TUint dummy;
sl@0
   321
	DMemoryMapping* mapping = (DMemoryMapping*)iMappings.Find(aAddr,dummy);
sl@0
   322
	if(mapping && mapping->IsAttached())
sl@0
   323
		{
sl@0
   324
		// found mapping, check addresses are in range...
sl@0
   325
		TUint offset = aAddr-mapping->Base();
sl@0
   326
		TUint end = offset+aSize;
sl@0
   327
		if(offset<end && end<=mapping->iSizeInPages<<KPageShift)
sl@0
   328
			{
sl@0
   329
			// addresses OK, get a reference on the mapping before releasing list lock...
sl@0
   330
			aOffsetInMapping = offset;
sl@0
   331
			aInstanceCount = mapping->MapInstanceCount();
sl@0
   332
			mapping->Open(); // can't fail because mapping IsAttached
sl@0
   333
			result = mapping;
sl@0
   334
			}
sl@0
   335
		}
sl@0
   336
	iMappings.ReadUnlock();
sl@0
   337
sl@0
   338
	return result;
sl@0
   339
	}
sl@0
   340
sl@0
   341
sl@0
   342
TBool DAddressSpace::CheckPdeType(TLinAddr aAddr, TUint aSize, TUint aPdeType)
sl@0
   343
	{
sl@0
   344
	TRACE(("DAddressSpace::CheckPdeType(0x%08x,0x%08x,%d) osAsid=%d",aAddr, aSize, aPdeType, iOsAsid));
sl@0
   345
	TBool r;
sl@0
   346
	Lock();
sl@0
   347
	if(iOsAsid==(TInt)KKernelOsAsid && UserGlobalVirtualAllocator.InRange(aAddr,aSize))
sl@0
   348
		r = UserGlobalVirtualAllocator.CheckSlabType(aAddr,aSize,aPdeType);
sl@0
   349
	else
sl@0
   350
		r = iVirtualAllocator.CheckSlabType(aAddr,aSize,aPdeType);
sl@0
   351
	TRACE(("DAddressSpace::CheckPdeType returns %d",r));
sl@0
   352
	Unlock();
sl@0
   353
	return r;
sl@0
   354
	}
sl@0
   355
sl@0
   356
sl@0
   357
sl@0
   358
//
sl@0
   359
// Debug
sl@0
   360
//
sl@0
   361
sl@0
   362
#ifdef _DEBUG
sl@0
   363
sl@0
   364
void DAddressSpace::Dump()
sl@0
   365
	{
sl@0
   366
	Kern::Printf("DAddressSpace[0x%08x]::Dump() osAsid = %d",this,iOsAsid);
sl@0
   367
	TLinAddr virt = 0;
sl@0
   368
	do
sl@0
   369
		{
sl@0
   370
		--virt;
sl@0
   371
		iMappings.ReadLock();
sl@0
   372
		TUint offsetInMapping = 0;
sl@0
   373
		DMemoryMapping* mapping = (DMemoryMapping*)iMappings.Find(virt,offsetInMapping);
sl@0
   374
		if(mapping)
sl@0
   375
			{
sl@0
   376
			if(!mapping->TryOpen())
sl@0
   377
				mapping = NULL;
sl@0
   378
			virt -= offsetInMapping;
sl@0
   379
			}
sl@0
   380
		iMappings.ReadUnlock();
sl@0
   381
		if(!mapping)
sl@0
   382
			break;
sl@0
   383
		mapping->Dump();
sl@0
   384
		mapping->Close();
sl@0
   385
		}
sl@0
   386
	while(virt);
sl@0
   387
	}
sl@0
   388
sl@0
   389
#endif // _DEBUG