os/kernelhwsrv/kernel/eka/memmodel/epoc/moving/arm/xkernel.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) 1994-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\moving\arm\xkernel.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "arm_mem.h"
sl@0
    19
sl@0
    20
sl@0
    21
/********************************************
sl@0
    22
 * Thread
sl@0
    23
 ********************************************/
sl@0
    24
sl@0
    25
TInt DArmPlatThread::SetupContext(SThreadCreateInfo& anInfo)
sl@0
    26
	{
sl@0
    27
	switch(iThreadType)
sl@0
    28
		{
sl@0
    29
		case EThreadSupervisor:
sl@0
    30
		case EThreadMinimalSupervisor:
sl@0
    31
			iNThread.SetDacr(Arm::DefaultDomainAccess);
sl@0
    32
		case EThreadInitial:
sl@0
    33
			break;
sl@0
    34
		case EThreadUser:
sl@0
    35
			{
sl@0
    36
			DArmPlatProcess* pP=(DArmPlatProcess*)iOwningProcess;
sl@0
    37
			iNThread.SetDacr(pP->iDacr);
sl@0
    38
			if (pP->iAttributes & DMemModelProcess::EVariableAccess)
sl@0
    39
				iNThread.SetAttributes(KThreadAttImplicitSystemLock|KThreadAttAddressSpace);
sl@0
    40
#ifndef __SMP__
sl@0
    41
			iNThread.iSpare3 /*iUserContextType*/ = NThread::EContextUndefined;
sl@0
    42
#endif
sl@0
    43
			break;
sl@0
    44
			}
sl@0
    45
		}
sl@0
    46
	iNThread.SetAddressSpace(iOwningProcess);
sl@0
    47
	__KTRACE_OPT(KTHREAD,Kern::Printf("Thread %O DACR %08x Attrib %02x",this,iNThread.Dacr(),iNThread.Attributes()));
sl@0
    48
	return KErrNone;
sl@0
    49
	}
sl@0
    50
sl@0
    51
DArmPlatProcess::DArmPlatProcess()
sl@0
    52
	: iDomain(-1), iDacr(Arm::DefaultDomainAccess|0x3)	// manager access to domain 0
sl@0
    53
	{}
sl@0
    54
sl@0
    55
DArmPlatProcess::~DArmPlatProcess()
sl@0
    56
	{
sl@0
    57
	__KTRACE_OPT(KMMU,Kern::Printf("DArmPlatProcess destruct, Domain=%d",iDomain));
sl@0
    58
	if (iDomain>=0)
sl@0
    59
		ArmMmu::FreeDomain(iDomain);
sl@0
    60
	DMemModelProcess::Destruct();
sl@0
    61
	}
sl@0
    62
sl@0
    63
TInt DArmPlatProcess::GetNewChunk(DMemModelChunk*& aChunk, SChunkCreateInfo& aInfo)
sl@0
    64
	{
sl@0
    65
	aChunk=NULL;
sl@0
    66
	DArmPlatChunk* pC=new DArmPlatChunk;
sl@0
    67
	if (!pC)
sl@0
    68
		return KErrNoMemory;
sl@0
    69
	aChunk=pC;
sl@0
    70
	pC->iChunkType=aInfo.iType;
sl@0
    71
	switch(pC->iChunkType)
sl@0
    72
		{
sl@0
    73
		case EKernelData:
sl@0
    74
		case EKernelStack:
sl@0
    75
		case EKernelCode:
sl@0
    76
		case EKernelMessage:
sl@0
    77
			pC->iAttributes |= DMemModelChunk::EFixedAccess;
sl@0
    78
			break;
sl@0
    79
		case ERamDrive:
sl@0
    80
			pC->iDomain=3;
sl@0
    81
			pC->iAttributes |= DMemModelChunk::EFixedAccess;
sl@0
    82
			break;
sl@0
    83
		case EUserCode:
sl@0
    84
		case EUserSelfModCode:
sl@0
    85
			pC->iAttributes |= DMemModelChunk::EFixedAddress;
sl@0
    86
			if (iDomain>=0)
sl@0
    87
				{
sl@0
    88
				__KTRACE_OPT(KMMU,Kern::Printf("DArmPlatChunk create code chunk, owning process domain %d",iDomain));
sl@0
    89
				pC->iDomain=iDomain;
sl@0
    90
				pC->iAttributes |= DMemModelChunk::EFixedAccess;
sl@0
    91
				}
sl@0
    92
			break;
sl@0
    93
		case EDll:
sl@0
    94
			break;
sl@0
    95
		case EUserData:
sl@0
    96
		case EDllData:
sl@0
    97
			if (aInfo.iGlobal && (iAttributes & DMemModelProcess::EFixedAddress || aInfo.iForceFixed))
sl@0
    98
				{
sl@0
    99
				TInt domain=ArmMmu::AllocDomain();
sl@0
   100
				if (domain>=0)
sl@0
   101
					{
sl@0
   102
					__KTRACE_OPT(KMMU,Kern::Printf("DArmPlatChunk create global chunk, Domain %d allocated",domain));
sl@0
   103
					pC->iDomain=domain;
sl@0
   104
					pC->iAttributes |= DMemModelChunk::EFixedAccess;
sl@0
   105
					}
sl@0
   106
				}
sl@0
   107
			else if (!aInfo.iGlobal && iDomain>=0)
sl@0
   108
				{
sl@0
   109
				__KTRACE_OPT(KMMU,Kern::Printf("DArmPlatChunk create local chunk, owning process domain %d",iDomain));
sl@0
   110
				pC->iDomain=iDomain;
sl@0
   111
				pC->iAttributes |= DMemModelChunk::EFixedAccess;
sl@0
   112
				}
sl@0
   113
			break;
sl@0
   114
		case ESharedKernelSingle:
sl@0
   115
		case ESharedKernelMultiple:
sl@0
   116
		case ESharedIo:
sl@0
   117
			break;
sl@0
   118
		default:
sl@0
   119
			FAULT();
sl@0
   120
		}
sl@0
   121
	return KErrNone;
sl@0
   122
	}
sl@0
   123
sl@0
   124
DArmPlatChunk::DArmPlatChunk()
sl@0
   125
	: iDomain(-1)
sl@0
   126
	{}
sl@0
   127
sl@0
   128
DArmPlatChunk::~DArmPlatChunk()
sl@0
   129
	{
sl@0
   130
	if (!iOwningProcess && iDomain>=0)
sl@0
   131
		{
sl@0
   132
		__KTRACE_OPT(KMMU,Kern::Printf("DArmPlatChunk destruct, Domain %d freed",iDomain));
sl@0
   133
		ArmMmu::FreeDomain(iDomain);
sl@0
   134
		}
sl@0
   135
	DMemModelChunk::Destruct();
sl@0
   136
	}
sl@0
   137
sl@0
   138
TInt DArmPlatChunk::SetupPermissions()
sl@0
   139
	{
sl@0
   140
	Mmu& m = Mmu::Get();
sl@0
   141
	if(iChunkType==ESharedKernelSingle || iChunkType==ESharedKernelMultiple || iChunkType==ESharedIo)
sl@0
   142
		{
sl@0
   143
		// override map attributes for shared kernel chunks
sl@0
   144
		TUint ma = (iMapAttr &~ EMapAttrAccessMask) | EMapAttrSupRw;
sl@0
   145
		TPde pde;
sl@0
   146
		TInt r = m.PdePtePermissions(ma, pde, iPtePermissions);
sl@0
   147
		if (r != KErrNone)
sl@0
   148
			return r;
sl@0
   149
		iMapAttr = ma;
sl@0
   150
		}
sl@0
   151
	else
sl@0
   152
		iPtePermissions=m.PtePermissions(iChunkType);
sl@0
   153
	// PDE may need to set P bit for ECC in ARMv5 and later.
sl@0
   154
	if (iDomain<0)
sl@0
   155
		{
sl@0
   156
		iPdePermissions[0]=m.PdePermissions(iChunkType,ENotRunning);
sl@0
   157
		iPdePermissions[1]=m.PdePermissions(iChunkType,ERunningRO);
sl@0
   158
		iPdePermissions[2]=m.PdePermissions(iChunkType,ERunningRW);
sl@0
   159
		}
sl@0
   160
	else
sl@0
   161
		{
sl@0
   162
		TPde pdePermissions = PT_PDE(iDomain);
sl@0
   163
		iPdePermissions[0]=pdePermissions;
sl@0
   164
		iPdePermissions[1]=pdePermissions;
sl@0
   165
		iPdePermissions[2]=pdePermissions;
sl@0
   166
		}
sl@0
   167
	__KTRACE_OPT(KMMU,Kern::Printf("Chunk permissions PTE=%08x PDE0=%08x PDE1=%08x PDE2=%08x",
sl@0
   168
				iPtePermissions,iPdePermissions[0],iPdePermissions[1],iPdePermissions[2]));
sl@0
   169
	return KErrNone;
sl@0
   170
	}
sl@0
   171
sl@0
   172
sl@0
   173
// must hold process lock while iterating through thread list
sl@0
   174
void DArmPlatProcess::AdjustDomainAccess(TUint32 aClearMask, TUint32 aSetMask)
sl@0
   175
	{
sl@0
   176
	__KTRACE_OPT(KMMU,Kern::Printf("Process %O AdjustDomainAccess, clear %08x set %08x",
sl@0
   177
			this,aClearMask,aSetMask));
sl@0
   178
	iDacr=(iDacr & ~aClearMask)|aSetMask;
sl@0
   179
	__KTRACE_OPT(KMMU,Kern::Printf("DACR set to %08x",iDacr));
sl@0
   180
	SDblQueLink* pLink=iThreadQ.First();
sl@0
   181
	while (pLink!=&iThreadQ.iA)
sl@0
   182
		{
sl@0
   183
		DArmPlatThread* pT=_LOFF(pLink,DArmPlatThread,iProcessLink);
sl@0
   184
		pLink=pLink->iNext;
sl@0
   185
		pT->iNThread.ModifyDacr(aClearMask,aSetMask);
sl@0
   186
		}
sl@0
   187
	}
sl@0
   188
sl@0
   189
// must hold process lock while iterating through thread list
sl@0
   190
void DArmPlatProcess::AdjustThreadAttributes(TUint8 aClearMask, TUint8 aSetMask)
sl@0
   191
	{
sl@0
   192
	__KTRACE_OPT(KMMU,Kern::Printf("Process %O AdjustThreadAttributes, clear %02x set %02x",
sl@0
   193
			this,aClearMask,aSetMask));
sl@0
   194
	SDblQueLink* pLink=iThreadQ.First();
sl@0
   195
	NKern::LockSystem();
sl@0
   196
	while (pLink!=&iThreadQ.iA)
sl@0
   197
		{
sl@0
   198
		DArmPlatThread* pT=_LOFF(pLink,DArmPlatThread,iProcessLink);
sl@0
   199
		pLink=pLink->iNext;
sl@0
   200
		pT->iNThread.ModifyAttributes(aClearMask,aSetMask);
sl@0
   201
		NKern::FlashSystem();
sl@0
   202
		}
sl@0
   203
	NKern::UnlockSystem();
sl@0
   204
	}
sl@0
   205
sl@0
   206
TInt DArmPlatProcess::AddFixedAccessChunk(DMemModelChunk *aChunk)
sl@0
   207
	{
sl@0
   208
	DArmPlatChunk* pC=(DArmPlatChunk*)aChunk;
sl@0
   209
	if (pC->iChunkType==ESharedKernelSingle || pC->iChunkType==ESharedIo)
sl@0
   210
		{
sl@0
   211
		if (iDomain<0)
sl@0
   212
			return KErrGeneral;
sl@0
   213
		pC->iDomain = iDomain;
sl@0
   214
		TInt r = pC->SetupPermissions();
sl@0
   215
		__ASSERT_ALWAYS(r==KErrNone, MM::Panic(MM::EAddFixedBadPerm));
sl@0
   216
		}
sl@0
   217
sl@0
   218
	__KTRACE_OPT(KMMU,Kern::Printf("Add fixed access chunk, domain=%d",pC->iDomain));
sl@0
   219
sl@0
   220
	// if this is one of process's local chunks, nothing to do
sl@0
   221
	if (pC->iDomain!=iDomain && !(iAttributes&ESupervisor))
sl@0
   222
		{
sl@0
   223
		AdjustDomainAccess(0,2<<(pC->iDomain<<1));	// grant manager access to chunk's domain
sl@0
   224
		}
sl@0
   225
	return KErrNone;
sl@0
   226
	}
sl@0
   227
sl@0
   228
TInt DArmPlatProcess::RemoveFixedAccessChunk(DMemModelChunk *aChunk)
sl@0
   229
	{
sl@0
   230
	DArmPlatChunk* pC=(DArmPlatChunk*)aChunk;
sl@0
   231
	__KTRACE_OPT(KMMU,Kern::Printf("Remove fixed access chunk, domain=%d",pC->iDomain));
sl@0
   232
	if (pC->iChunkType==ESharedKernelSingle || pC->iChunkType==ESharedIo)
sl@0
   233
		{
sl@0
   234
		if (iDomain<0)
sl@0
   235
			return KErrGeneral;
sl@0
   236
		pC->iDomain = -1;
sl@0
   237
		TInt r = pC->SetupPermissions();
sl@0
   238
		__ASSERT_ALWAYS(r==KErrNone, MM::Panic(MM::ERemoveFixedBadPerm));
sl@0
   239
		return KErrNone;
sl@0
   240
		}
sl@0
   241
sl@0
   242
	// if this is one of process's local chunks, nothing to do
sl@0
   243
	if (pC->iDomain!=iDomain && !(iAttributes&ESupervisor))
sl@0
   244
		{
sl@0
   245
		AdjustDomainAccess(2<<(pC->iDomain<<1),0);	// remove manager access to chunk's domain
sl@0
   246
		}
sl@0
   247
	return KErrNone;
sl@0
   248
	}
sl@0
   249
sl@0
   250
void DArmPlatProcess::CheckForFixedAccess()
sl@0
   251
	{
sl@0
   252
	TInt domain=ArmMmu::AllocDomain();
sl@0
   253
	if (domain>=0)
sl@0
   254
		{
sl@0
   255
		__KTRACE_OPT(KMMU,Kern::Printf("DArmPlatProcess create, Domain %d allocated",domain));
sl@0
   256
		iDomain=domain;
sl@0
   257
		iDacr |= (2<<(domain<<1));	// grant manager access to allocated domain
sl@0
   258
		iDacr &= ~2;				// demote domain 0 to client access
sl@0
   259
		}
sl@0
   260
	}
sl@0
   261
sl@0
   262
void DArmPlatProcess::DoAttributeChange()
sl@0
   263
	{
sl@0
   264
	// Called when a process changes from fixed to variable access or vice-versa.
sl@0
   265
	TBool variable=iAttributes & EVariableAccess;
sl@0
   266
	if (variable)
sl@0
   267
		{
sl@0
   268
		// process changing from fixed access to variable access
sl@0
   269
		__KTRACE_OPT(KMMU,Kern::Printf("Process %O becomes variable access",this));
sl@0
   270
		AdjustThreadAttributes(0,KThreadAttImplicitSystemLock|KThreadAttAddressSpace);
sl@0
   271
		AdjustDomainAccess(0,2);		// promote domain 0 to manager access
sl@0
   272
		}
sl@0
   273
	else
sl@0
   274
		{
sl@0
   275
		// process changing from variable access to fixed access
sl@0
   276
		__KTRACE_OPT(KMMU,Kern::Printf("Process %O becomes fixed access",this));
sl@0
   277
		AdjustDomainAccess(2,0);		// demote domain 0 to client access
sl@0
   278
		AdjustThreadAttributes(KThreadAttImplicitSystemLock|KThreadAttAddressSpace,0);
sl@0
   279
		}
sl@0
   280
	}
sl@0
   281
sl@0
   282
TIpcExcTrap::TExcLocation TIpcExcTrap::ExcLocation(DThread* /*aThread*/, TAny* aContext)
sl@0
   283
	{
sl@0
   284
	TArmExcInfo& info=*(TArmExcInfo*)aContext;
sl@0
   285
sl@0
   286
	if (info.iExcCode==EArmExceptionDataAbort)
sl@0
   287
		{
sl@0
   288
		TLinAddr va=(TLinAddr)info.iFaultAddress;
sl@0
   289
		if (va>=iRemoteBase && (va-iRemoteBase)<iSize)
sl@0
   290
			return EExcRemote;
sl@0
   291
		if (iLocalBase && va>=iLocalBase && (va-iLocalBase)<iSize)
sl@0
   292
			return EExcLocal;
sl@0
   293
		}
sl@0
   294
	return EExcUnknown;
sl@0
   295
	}