Update contrib.
1 // Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\memmodel\epoc\moving\mkernel.cpp
20 /********************************************
22 ********************************************/
24 TInt DThread::AllocateSupervisorStack()
26 __KTRACE_OPT(KTHREAD,Kern::Printf("DThread::AllocateSupervisorStack %O %x",this,iSupervisorStackSize));
27 iSupervisorStackSize=Mmu::RoundToPageSize(iSupervisorStackSize);
28 if (iThreadType!=EThreadInitial)
30 TInt offset=MM::SvStackChunk->Allocate(iSupervisorStackSize,PP::SupervisorThreadStackGuard);
33 __KTRACE_FAIL(KErrNoMemory,Kern::Printf("ASS: %d",KErrNoMemory));
36 iSupervisorStack=MM::SvStackChunk->Base()+offset+PP::SupervisorThreadStackGuard;
37 iSupervisorStackAllocated = TRUE;
38 __KTRACE_OPT(KTHREAD,Kern::Printf("Supervisor stack at %x, size %x",iSupervisorStack,iSupervisorStackSize));
43 void DThread::FreeSupervisorStack()
45 __KTRACE_OPT(KTHREAD,Kern::Printf("DThread::FreeSupervisorStack %O",this));
46 if (iSupervisorStackAllocated)
48 TAny* pStack = __e32_atomic_swp_ord_ptr(&iSupervisorStack, 0);
49 if (pStack && iThreadType!=EThreadInitial)
51 __KTRACE_OPT(KTHREAD,Kern::Printf("Freeing supervisor stack at %08x, size %x",pStack,iSupervisorStackSize));
52 TInt offset=(TUint8*)pStack-MM::SvStackChunk->Base();
53 MM::SvStackChunk->Decommit(offset-PP::SupervisorThreadStackGuard,iSupervisorStackSize+PP::SupervisorThreadStackGuard);
56 iSupervisorStackAllocated = FALSE;
59 TInt DThread::AllocateUserStack(TInt aSize, TBool /*aPaged*/)
61 __KTRACE_OPT(KTHREAD,Kern::Printf("DThread::AllocateUserStack %O %x",this,aSize));
63 aSize=Mmu::RoundToPageSize(aSize);
64 if (aSize>PP::MaxUserThreadStack)
66 TInt guard=PP::UserThreadStackGuard;
67 DProcess* pP=iOwningProcess;
69 DChunk* pC=pP->iDataBssStackChunk;
70 if (!pC || pC->Open()!=KErrNone) // so chunk doesn't disappear during Allocate()
72 NKern::UnlockSystem();
73 __KTRACE_FAIL(KErrDied,Kern::Printf("AUS1: %d",KErrDied));
76 NKern::UnlockSystem();
77 TInt r=pC->Allocate(aSize,guard);
78 TInt s=pC->Close(NULL); // NULL since we didn't add chunk to process again
79 if (s & EObjectDeleted)
81 __KTRACE_FAIL(KErrDied,Kern::Printf("AUS2: %d",KErrDied));
86 __KTRACE_FAIL(r,Kern::Printf("AUS3: %d",r));
90 iUserStackRunAddress=pP->iDataBssRunAddress+r+guard;
91 __KTRACE_OPT(KTHREAD,Kern::Printf("User stack run address at %x",iUserStackRunAddress));
95 void DThread::FreeUserStack()
97 __KTRACE_OPT(KTHREAD,Kern::Printf("DThread::FreeUserStack %O",this));
98 TLinAddr usr_stack = (TLinAddr)__e32_atomic_swp_ord_ptr(&iUserStackRunAddress, 0);
101 __KTRACE_OPT(KTHREAD,Kern::Printf("Freeing user stack at %x",usr_stack));
102 DMemModelProcess* pP=(DMemModelProcess*)iOwningProcess;
104 DMemModelChunk* pC=(DMemModelChunk*)pP->iDataBssStackChunk;
105 if (!pC || pC->Open()!=KErrNone) // so chunk doesn't disappear during Decommit()
107 NKern::UnlockSystem();
108 __KTRACE_FAIL(KErrDied,Kern::Printf("FUS"));
111 NKern::UnlockSystem();
112 TInt offset=usr_stack-pP->iDataBssRunAddress;
113 pC->Decommit(offset-PP::UserThreadStackGuard,iUserStackSize+PP::UserThreadStackGuard);
114 pC->Close(NULL); // NULL since we didn't add chunk to process again
118 void DThread::IpcExcHandler(TExcTrap* aTrap, DThread* aThread, TAny* aContext)
120 aThread->iIpcClient = 0;
121 TIpcExcTrap& xt=*(TIpcExcTrap*)aTrap;
122 switch (xt.ExcLocation(aThread, aContext))
124 case TIpcExcTrap::EExcRemote:
125 // problem accessing remote address - 'leave' so an error code will be returned
126 xt.Exception(KErrBadDescriptor); // does not return
128 case TIpcExcTrap::EExcLocal:
129 // problem accessing local address - return and panic current thread as usual
130 NKern::UnlockSystem();
134 // otherwise return and fault kernel