os/kernelhwsrv/kernel/eka/memmodel/epoc/multiple/mkernel.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) 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\multiple\mkernel.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "memmodel.h"
sl@0
    19
sl@0
    20
/********************************************
sl@0
    21
 * Thread
sl@0
    22
 ********************************************/
sl@0
    23
sl@0
    24
TInt DThread::AllocateSupervisorStack()
sl@0
    25
	{
sl@0
    26
	__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::AllocateSupervisorStack %O %x",this,iSupervisorStackSize));
sl@0
    27
	iSupervisorStackSize=Mmu::RoundToPageSize(iSupervisorStackSize);
sl@0
    28
	if (iThreadType!=EThreadInitial)
sl@0
    29
		{
sl@0
    30
		TInt offset=MM::SvStackChunk->Allocate(iSupervisorStackSize,PP::SupervisorThreadStackGuard);
sl@0
    31
		if (offset<0)
sl@0
    32
			{
sl@0
    33
			__KTRACE_FAIL(KErrNoMemory,Kern::Printf("ASS: %d",KErrNoMemory));
sl@0
    34
			return KErrNoMemory;
sl@0
    35
			}
sl@0
    36
		iSupervisorStack=MM::SvStackChunk->Base()+offset+PP::SupervisorThreadStackGuard;
sl@0
    37
		iSupervisorStackAllocated = TRUE;
sl@0
    38
		__KTRACE_OPT(KTHREAD,Kern::Printf("Supervisor stack at %x, size %x",iSupervisorStack,iSupervisorStackSize));
sl@0
    39
		}
sl@0
    40
	return KErrNone;
sl@0
    41
	}
sl@0
    42
sl@0
    43
void DMemModelThread::DoExit1()
sl@0
    44
	{
sl@0
    45
	// Regularize the thread state before main exit processing.
sl@0
    46
	// We must remove any existing alias before we do any operation 
sl@0
    47
	// which requires aliasing (e.g. logon completion).
sl@0
    48
	__KTRACE_OPT(KTHREAD,Kern::Printf("DMemModelThread %O DoExit1",this));
sl@0
    49
	NKern::LockSystem();
sl@0
    50
	RemoveAlias();
sl@0
    51
	NKern::UnlockSystem();
sl@0
    52
	}
sl@0
    53
sl@0
    54
void DThread::FreeSupervisorStack()
sl@0
    55
	{
sl@0
    56
	__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::FreeSupervisorStack %O",this));
sl@0
    57
	if (iSupervisorStackAllocated)
sl@0
    58
		{
sl@0
    59
		TAny* pStack = __e32_atomic_swp_ord_ptr(&iSupervisorStack, 0);
sl@0
    60
		if (pStack && iThreadType!=EThreadInitial)
sl@0
    61
			{
sl@0
    62
			__KTRACE_OPT(KTHREAD,Kern::Printf("Freeing supervisor stack at %08x, size %x",pStack,iSupervisorStackSize));
sl@0
    63
			TInt offset=(TUint8*)pStack-MM::SvStackChunk->Base();
sl@0
    64
			TInt r=MM::SvStackChunk->Decommit(offset-PP::SupervisorThreadStackGuard,iSupervisorStackSize+PP::SupervisorThreadStackGuard);
sl@0
    65
			__ASSERT_DEBUG(r==KErrNone, MM::Panic(MM::EDecommitFailed));
sl@0
    66
			(void)r; //Supress the warning in urel build
sl@0
    67
			}
sl@0
    68
		}
sl@0
    69
	iSupervisorStackAllocated = FALSE;
sl@0
    70
	}
sl@0
    71
sl@0
    72
TInt DThread::AllocateUserStack(TInt aSize, TBool /*aPaged*/)
sl@0
    73
	{
sl@0
    74
	__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::AllocateUserStack %O %x",this,aSize));
sl@0
    75
sl@0
    76
	aSize=Mmu::RoundToPageSize(aSize);
sl@0
    77
	if (aSize>PP::MaxUserThreadStack)
sl@0
    78
		return KErrTooBig;
sl@0
    79
	TInt guard=PP::UserThreadStackGuard;
sl@0
    80
	DProcess* pP=iOwningProcess;
sl@0
    81
	NKern::LockSystem();
sl@0
    82
	DChunk* pC=pP->iDataBssStackChunk;
sl@0
    83
	if (!pC || pC->Open()!=KErrNone)	// so chunk doesn't disappear during Allocate()
sl@0
    84
		{
sl@0
    85
		NKern::UnlockSystem();
sl@0
    86
		__KTRACE_FAIL(KErrDied,Kern::Printf("AUS1: %d",KErrDied));
sl@0
    87
		return KErrDied;
sl@0
    88
		}
sl@0
    89
	NKern::UnlockSystem();
sl@0
    90
	TInt r=pC->Allocate(aSize,guard);
sl@0
    91
	TInt s=pC->Close(NULL);				// NULL since we didn't add chunk to process again
sl@0
    92
	if (s & EObjectDeleted)
sl@0
    93
		{
sl@0
    94
		__KTRACE_FAIL(KErrDied,Kern::Printf("AUS2: %d",KErrDied));
sl@0
    95
		return KErrDied;
sl@0
    96
		}
sl@0
    97
	if (r<0)
sl@0
    98
		{
sl@0
    99
		__KTRACE_FAIL(r,Kern::Printf("AUS3: %d",r));
sl@0
   100
		return r;
sl@0
   101
		}
sl@0
   102
	iUserStackSize=aSize;
sl@0
   103
	iUserStackRunAddress=pP->iDataBssRunAddress+r+guard;
sl@0
   104
	__KTRACE_OPT(KTHREAD,Kern::Printf("User stack run address at %x",iUserStackRunAddress));
sl@0
   105
	return KErrNone;
sl@0
   106
	}
sl@0
   107
sl@0
   108
void DThread::FreeUserStack()
sl@0
   109
	{
sl@0
   110
	__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::FreeUserStack %O",this));
sl@0
   111
	TLinAddr usr_stack = (TLinAddr)__e32_atomic_swp_ord_ptr(&iUserStackRunAddress, 0);
sl@0
   112
	if (usr_stack)
sl@0
   113
		{
sl@0
   114
		__KTRACE_OPT(KTHREAD,Kern::Printf("Freeing user stack at %x",usr_stack));
sl@0
   115
		DMemModelProcess* pP=(DMemModelProcess*)iOwningProcess;
sl@0
   116
		NKern::LockSystem();
sl@0
   117
		DMemModelChunk* pC=(DMemModelChunk*)pP->iDataBssStackChunk;
sl@0
   118
		if (!pC || pC->Open()!=KErrNone)	// so chunk doesn't disappear during Decommit()
sl@0
   119
			{
sl@0
   120
			NKern::UnlockSystem();
sl@0
   121
			__KTRACE_FAIL(KErrDied,Kern::Printf("FUS"));
sl@0
   122
			return;
sl@0
   123
			}
sl@0
   124
		NKern::UnlockSystem();
sl@0
   125
		TInt offset=usr_stack-pP->iDataBssRunAddress;
sl@0
   126
		TInt r=pC->Decommit(offset-PP::UserThreadStackGuard,iUserStackSize+PP::UserThreadStackGuard);
sl@0
   127
		__ASSERT_DEBUG(r==KErrNone, MM::Panic(MM::EDecommitFailed));
sl@0
   128
		pC->Close(NULL);			// NULL since we didn't add chunk to process again
sl@0
   129
		(void)r; //Supress the warning in urel build
sl@0
   130
		}
sl@0
   131
	}
sl@0
   132
sl@0
   133
void DThread::IpcExcHandler(TExcTrap* aTrap, DThread* aThread, TAny* aContext)
sl@0
   134
	{
sl@0
   135
	aThread->iIpcClient = 0;
sl@0
   136
	TIpcExcTrap& xt=*(TIpcExcTrap*)aTrap;
sl@0
   137
	switch (xt.ExcLocation(aThread, aContext))
sl@0
   138
		{
sl@0
   139
		case TIpcExcTrap::EExcRemote:
sl@0
   140
			// problem accessing remote address - 'leave' so an error code will be returned
sl@0
   141
			NKern::LockSystem();
sl@0
   142
			((DMemModelThread*)aThread)->RemoveAlias();
sl@0
   143
			xt.Exception(KErrBadDescriptor);  // does not return
sl@0
   144
sl@0
   145
		case TIpcExcTrap::EExcLocal:
sl@0
   146
			// problem accessing local address - return and panic current thread as usual
sl@0
   147
			NKern::LockSystem();
sl@0
   148
			((DMemModelThread*)aThread)->RemoveAlias();
sl@0
   149
			NKern::UnlockSystem();
sl@0
   150
			return;
sl@0
   151
sl@0
   152
		default:
sl@0
   153
			// otherwise return and fault kernel
sl@0
   154
			NKern::LockSystem();
sl@0
   155
			return;
sl@0
   156
		}
sl@0
   157
	}