os/kernelhwsrv/kernel/eka/memmodel/epoc/moving/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\moving\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 DThread::FreeSupervisorStack()
sl@0
    44
	{
sl@0
    45
	__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::FreeSupervisorStack %O",this));
sl@0
    46
	if (iSupervisorStackAllocated)
sl@0
    47
		{
sl@0
    48
		TAny* pStack = __e32_atomic_swp_ord_ptr(&iSupervisorStack, 0);
sl@0
    49
		if (pStack && iThreadType!=EThreadInitial)
sl@0
    50
			{
sl@0
    51
			__KTRACE_OPT(KTHREAD,Kern::Printf("Freeing supervisor stack at %08x, size %x",pStack,iSupervisorStackSize));
sl@0
    52
			TInt offset=(TUint8*)pStack-MM::SvStackChunk->Base();
sl@0
    53
			MM::SvStackChunk->Decommit(offset-PP::SupervisorThreadStackGuard,iSupervisorStackSize+PP::SupervisorThreadStackGuard);
sl@0
    54
			}
sl@0
    55
		}
sl@0
    56
	iSupervisorStackAllocated = FALSE;
sl@0
    57
	}
sl@0
    58
sl@0
    59
TInt DThread::AllocateUserStack(TInt aSize, TBool /*aPaged*/)
sl@0
    60
	{
sl@0
    61
	__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::AllocateUserStack %O %x",this,aSize));
sl@0
    62
sl@0
    63
	aSize=Mmu::RoundToPageSize(aSize);
sl@0
    64
	if (aSize>PP::MaxUserThreadStack)
sl@0
    65
		return KErrTooBig;
sl@0
    66
	TInt guard=PP::UserThreadStackGuard;
sl@0
    67
	DProcess* pP=iOwningProcess;
sl@0
    68
	NKern::LockSystem();
sl@0
    69
	DChunk* pC=pP->iDataBssStackChunk;
sl@0
    70
	if (!pC || pC->Open()!=KErrNone)	// so chunk doesn't disappear during Allocate()
sl@0
    71
		{
sl@0
    72
		NKern::UnlockSystem();
sl@0
    73
		__KTRACE_FAIL(KErrDied,Kern::Printf("AUS1: %d",KErrDied));
sl@0
    74
		return KErrDied;
sl@0
    75
		}
sl@0
    76
	NKern::UnlockSystem();
sl@0
    77
	TInt r=pC->Allocate(aSize,guard);
sl@0
    78
	TInt s=pC->Close(NULL);				// NULL since we didn't add chunk to process again
sl@0
    79
	if (s & EObjectDeleted)
sl@0
    80
		{
sl@0
    81
		__KTRACE_FAIL(KErrDied,Kern::Printf("AUS2: %d",KErrDied));
sl@0
    82
		return KErrDied;
sl@0
    83
		}
sl@0
    84
	if (r<0)
sl@0
    85
		{
sl@0
    86
		__KTRACE_FAIL(r,Kern::Printf("AUS3: %d",r));
sl@0
    87
		return r;
sl@0
    88
		}
sl@0
    89
	iUserStackSize=aSize;
sl@0
    90
	iUserStackRunAddress=pP->iDataBssRunAddress+r+guard;
sl@0
    91
	__KTRACE_OPT(KTHREAD,Kern::Printf("User stack run address at %x",iUserStackRunAddress));
sl@0
    92
	return KErrNone;
sl@0
    93
	}
sl@0
    94
sl@0
    95
void DThread::FreeUserStack()
sl@0
    96
	{
sl@0
    97
	__KTRACE_OPT(KTHREAD,Kern::Printf("DThread::FreeUserStack %O",this));
sl@0
    98
	TLinAddr usr_stack = (TLinAddr)__e32_atomic_swp_ord_ptr(&iUserStackRunAddress, 0);
sl@0
    99
	if (usr_stack)
sl@0
   100
		{
sl@0
   101
		__KTRACE_OPT(KTHREAD,Kern::Printf("Freeing user stack at %x",usr_stack));
sl@0
   102
		DMemModelProcess* pP=(DMemModelProcess*)iOwningProcess;
sl@0
   103
		NKern::LockSystem();
sl@0
   104
		DMemModelChunk* pC=(DMemModelChunk*)pP->iDataBssStackChunk;
sl@0
   105
		if (!pC || pC->Open()!=KErrNone)	// so chunk doesn't disappear during Decommit()
sl@0
   106
			{
sl@0
   107
			NKern::UnlockSystem();
sl@0
   108
			__KTRACE_FAIL(KErrDied,Kern::Printf("FUS"));
sl@0
   109
			return;
sl@0
   110
			}
sl@0
   111
		NKern::UnlockSystem();
sl@0
   112
		TInt offset=usr_stack-pP->iDataBssRunAddress;
sl@0
   113
		pC->Decommit(offset-PP::UserThreadStackGuard,iUserStackSize+PP::UserThreadStackGuard);
sl@0
   114
		pC->Close(NULL);			// NULL since we didn't add chunk to process again
sl@0
   115
		}
sl@0
   116
	}
sl@0
   117
sl@0
   118
void DThread::IpcExcHandler(TExcTrap* aTrap, DThread* aThread, TAny* aContext)
sl@0
   119
	{
sl@0
   120
	aThread->iIpcClient = 0;
sl@0
   121
	TIpcExcTrap& xt=*(TIpcExcTrap*)aTrap;
sl@0
   122
	switch (xt.ExcLocation(aThread, aContext))
sl@0
   123
		{
sl@0
   124
		case TIpcExcTrap::EExcRemote:
sl@0
   125
			// problem accessing remote address - 'leave' so an error code will be returned
sl@0
   126
			xt.Exception(KErrBadDescriptor);  // does not return
sl@0
   127
sl@0
   128
		case TIpcExcTrap::EExcLocal:
sl@0
   129
			// problem accessing local address - return and panic current thread as usual
sl@0
   130
			NKern::UnlockSystem();
sl@0
   131
			return;
sl@0
   132
sl@0
   133
		default:
sl@0
   134
			// otherwise return and fault kernel
sl@0
   135
			return;
sl@0
   136
		}
sl@0
   137
	}