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.
21 /********************************************
23 ********************************************/
25 TInt DThread::AllocateSupervisorStack()
27 __KTRACE_OPT(KTHREAD,Kern::Printf("DThread::AllocateSupervisorStack %O %x",this,iSupervisorStackSize));
28 iSupervisorStackSize = MM::RoundToPageSize(iSupervisorStackSize);
29 if(iThreadType==EThreadInitial)
32 TUint guardSize = PP::SupervisorThreadStackGuard;
33 TUint virtualSize = guardSize+iSupervisorStackSize;
34 DMemoryObject* memory;
35 TInt r = MM::MemoryNew(memory,EMemoryObjectUnpaged,MM::BytesToPages(virtualSize));
38 r = MM::MemoryAlloc(memory,MM::BytesToPages(guardSize),MM::BytesToPages(iSupervisorStackSize));
41 DMemoryMapping* mapping;
42 r = MM::MappingNew(mapping,memory,ESupervisorReadWrite,KKernelOsAsid);
45 __NK_ASSERT_DEBUG(!((DMemModelThread*)this)->iKernelStackMapping);
46 ((DMemModelThread*)this)->iKernelStackMapping = mapping;
47 iSupervisorStack = (TAny*)(MM::MappingBase(mapping)+guardSize);
48 __KTRACE_OPT(KTHREAD,Kern::Printf("Supervisor stack at %x, size %x",iSupervisorStack,iSupervisorStackSize));
52 MM::MemoryDestroy(memory);
55 #ifdef BTRACE_FLEXIBLE_MEM_MODEL
56 BTrace8(BTrace::EFlexibleMemModel,BTrace::EMemoryObjectIsSupervisorStack,memory,&iNThread);
64 void DThread::FreeSupervisorStack()
66 __KTRACE_OPT(KTHREAD,Kern::Printf("DThread::FreeSupervisorStack %O",this));
67 if(iThreadType==EThreadInitial) // !!!???
69 MM::MappingAndMemoryDestroy(((DMemModelThread*)this)->iKernelStackMapping);
73 TInt DThread::AllocateUserStack(TInt aSize, TBool aPaged)
75 __KTRACE_OPT(KTHREAD,Kern::Printf("DThread::AllocateUserStack %O %x",this,aSize));
76 aSize = MM::RoundToPageSize(aSize);
77 if(aSize>PP::MaxUserThreadStack)
80 TMemoryObjectType memoryType = (aPaged)? EMemoryObjectPaged : EMemoryObjectMovable;
81 TUint guardSize = PP::UserThreadStackGuard;
82 TUint virtualSize = guardSize+aSize;
83 // wipe user thread stack with 0x29292929
84 TMemoryCreateFlags flags = (TMemoryCreateFlags)(EMemoryCreateDefault | EMemoryCreateUseCustomWipeByte | (0x29 << EMemoryCreateWipeByteShift));
85 DMemoryObject* memory;
86 TInt r = MM::MemoryNew(memory, memoryType, MM::BytesToPages(virtualSize),flags);
89 r = MM::MemoryAlloc(memory,MM::BytesToPages(guardSize),MM::BytesToPages(aSize));
92 DMemoryMapping* mapping;
93 // Get os asid, no need to open a reference as this only invoked where
94 // the current thread is owned by iOwningProcess, iOwningProcess is
95 // the kernel process or this is the first thread of a process that
96 // isn't fully created yet.
97 TUint osAsid = ((DMemModelProcess*)iOwningProcess)->OsAsid();
98 r = MM::MappingNew(mapping,memory,EUserReadWrite,osAsid);
101 __NK_ASSERT_DEBUG(!((DMemModelThread*)this)->iUserStackMapping);
102 ((DMemModelThread*)this)->iUserStackMapping = mapping;
103 iUserStackSize = aSize;
104 iUserStackRunAddress = MM::MappingBase(mapping)+guardSize;
105 __KTRACE_OPT(KTHREAD,Kern::Printf("User stack at %x, size %x",iUserStackRunAddress,iUserStackSize));
109 MM::MemoryDestroy(memory);
112 #ifdef BTRACE_FLEXIBLE_MEM_MODEL
113 BTrace8(BTrace::EFlexibleMemModel,BTrace::EMemoryObjectIsUserStack,memory,&iNThread);
121 void DThread::FreeUserStack()
123 __KTRACE_OPT(KTHREAD,Kern::Printf("DThread::FreeUserStack %O",this));
124 MM::MappingAndMemoryDestroy(((DMemModelThread*)this)->iUserStackMapping);
128 void DMemModelThread::SetPaging(TUint& aCreateFlags)
130 TUint pagingAtt = aCreateFlags & EThreadCreateFlagPagingMask;
131 TUint dataPolicy = TheSuperPage().KernelConfigFlags() & EKernelConfigDataPagingPolicyMask;
132 if (dataPolicy == EKernelConfigDataPagingPolicyNoPaging ||
133 !(K::MemModelAttributes & EMemModelAttrDataPaging))
134 {// No paging policy set or no data paging device installed.
135 pagingAtt = EThreadCreateFlagUnpaged;
137 else if (dataPolicy == EKernelConfigDataPagingPolicyAlwaysPage)
139 pagingAtt = EThreadCreateFlagPaged;
141 else if (pagingAtt == EThreadCreateFlagPagingUnspec)
142 {// No data paging attribute specified for this chunk so use the process's.
143 if (iOwningProcess->iAttributes & DProcess::EDataPaged)
144 pagingAtt = EThreadCreateFlagPaged;
146 pagingAtt = EThreadCreateFlagUnpaged;
151 __NK_ASSERT_DEBUG( pagingAtt == EThreadCreateFlagPaged ||
152 pagingAtt == EThreadCreateFlagUnpaged);
155 // Save the paging attributes for when the stack and heap are created later.
156 aCreateFlags &= ~EThreadCreateFlagPagingMask;
157 aCreateFlags |= pagingAtt;
161 void DMemModelThread::DoExit1()
163 // Regularize the thread state before main exit processing.
164 // We must remove any existing alias before we do any operation
165 // which requires aliasing (e.g. logon completion).
166 __KTRACE_OPT(KTHREAD,Kern::Printf("DMemModelThread %O DoExit1",this));
171 void DThread::IpcExcHandler(TExcTrap* aTrap, DThread* aThread, TAny* aContext)
173 aThread->iIpcClient = 0;
174 TIpcExcTrap& xt=*(TIpcExcTrap*)aTrap;
175 switch (xt.ExcLocation(aThread, aContext))
177 case TIpcExcTrap::EExcRemote:
178 // problem accessing remote address - 'leave' so an error code will be returned
179 ((DMemModelThread*)aThread)->RemoveAlias();
180 xt.Exception(KErrBadDescriptor); // does not return
182 case TIpcExcTrap::EExcLocal:
183 // problem accessing local address - return and panic current thread as usual
184 ((DMemModelThread*)aThread)->RemoveAlias();
188 // otherwise return and fault kernel
195 void DMemModelThread::BTracePrime(TInt aCategory)
197 DThread::BTracePrime(aCategory);
198 #ifdef BTRACE_FLEXIBLE_MEM_MODEL
199 if(aCategory==BTrace::EFlexibleMemModel || aCategory==-1)
201 if (iKernelStackMapping)
203 DMemoryObject* memory = MM::MappingGetAndOpenMemory(iKernelStackMapping);
206 MM::MemoryBTracePrime(memory);
207 BTrace8(BTrace::EFlexibleMemModel,BTrace::EMemoryObjectIsSupervisorStack,memory,&iNThread);
208 MM::MemoryClose(memory);
211 if (iUserStackMapping)
213 DMemoryObject* memory = MM::MappingGetAndOpenMemory(iUserStackMapping);
216 MM::MemoryBTracePrime(memory);
217 BTrace8(BTrace::EFlexibleMemModel,BTrace::EMemoryObjectIsUserStack,memory,&iNThread);
218 MM::MemoryClose(memory);