0
|
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".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32\kernel\server.cpp
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
#include <kernel/kern_priv.h>
|
|
19 |
#include "execs.h"
|
|
20 |
|
|
21 |
|
|
22 |
/* Macro for checking whether function is called from Kern::DfcQue0 or Kern::DfcQue1 thread context.
|
|
23 |
Device drivers which access user memory should not run in shared DFC threads.
|
|
24 |
See Base_How_To_Migrate_Media_Drivers_To_Support_Demand Paging.doc
|
|
25 |
*/
|
|
26 |
|
|
27 |
#if (!defined _DEBUG || ((!defined _CHECK_DFCQ_CONTEXT_WARNING_) && (!defined _CHECK_DFCQ_CONTEXT_PANIC_)))
|
|
28 |
#define _CHECK_DFCQ01_CONTEXT(function)
|
|
29 |
#else
|
|
30 |
#ifdef _CHECK_DFCQ_CONTEXT_PANIC_
|
|
31 |
#define _CHECK_DFCQ01_CONTEXT(func ) \
|
|
32 |
{ \
|
|
33 |
NThread* nt=NKern::CurrentThread(); \
|
|
34 |
__ASSERT_DEBUG( (nt!=Kern::DfcQue0()->iThread && nt!=Kern::DfcQue1()->iThread), (\
|
|
35 |
KPrintf("Function: %s\n called from DfcQ0 or DfcQ1 thread context",func), \
|
|
36 |
NKFault(func, 0))); \
|
|
37 |
}
|
|
38 |
#else //WARRNING
|
|
39 |
#define _CHECK_DFCQ01_CONTEXT(func ) \
|
|
40 |
{ \
|
|
41 |
NThread* nt=NKern::CurrentThread(); \
|
|
42 |
__ASSERT_DEBUG( (nt!=Kern::DfcQue0()->iThread && nt!=Kern::DfcQue1()->iThread), (\
|
|
43 |
KPrintf("Function: %s\n called from DfcQ0 or DfcQ1 thread context",func))); \
|
|
44 |
}
|
|
45 |
#endif // _CHECK_DFCQ_CONTEXT_PANIC_
|
|
46 |
#endif // !defined _DEBUG || ((!defined _CHECK_DFCQ_CONTEXT_WARNING_) && (!defined _CHECK_DFCQ_CONTEXT_PANIC_)))
|
|
47 |
|
|
48 |
/********************************************
|
|
49 |
* Generic kernel message code
|
|
50 |
********************************************/
|
|
51 |
|
|
52 |
|
|
53 |
/** Sends a kernel message without waiting for a reply.
|
|
54 |
|
|
55 |
If the receiving queue is ready to receive its DFC will be queued; otherwise
|
|
56 |
the message is simply placed on the end of the queue.
|
|
57 |
|
|
58 |
@param aQ Message queue to which message should be sent.
|
|
59 |
|
|
60 |
@pre No fast mutex can be held.
|
|
61 |
@pre Call in a thread context.
|
|
62 |
@pre Kernel must be unlocked
|
|
63 |
@pre interrupts enabled
|
|
64 |
|
|
65 |
@post The message contains a reference counted pointer to the current thread.
|
|
66 |
*/
|
|
67 |
EXPORT_C void TMessageBase::Send(TMessageQue* aQ)
|
|
68 |
{
|
|
69 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"TMessageBase::Send");
|
|
70 |
__KTRACE_OPT(KSERVER,Kern::Printf("MsgB::Send %08x to %08x",this,aQ));
|
|
71 |
DThread* pC=TheCurrentThread;
|
|
72 |
TMessageQue::Lock();
|
|
73 |
__NK_ASSERT_ALWAYS(iState==EFree);
|
|
74 |
iQueue=aQ;
|
|
75 |
iSem.iCount=0;
|
|
76 |
iSem.iOwningThread=&pC->iNThread;
|
|
77 |
pC->Open();
|
|
78 |
if (aQ->iReady)
|
|
79 |
{
|
|
80 |
iState=EAccepted;
|
|
81 |
aQ->iMessage=this;
|
|
82 |
aQ->iReady=EFalse;
|
|
83 |
aQ->UnlockAndKick();
|
|
84 |
}
|
|
85 |
else
|
|
86 |
{
|
|
87 |
aQ->iQ.Add(this);
|
|
88 |
iState=EDelivered;
|
|
89 |
TMessageQue::Unlock();
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
|
|
94 |
/** Sends a kernel message and wait for a reply.
|
|
95 |
|
|
96 |
If the receiving queue is ready to receive its DFC will be queued; otherwise
|
|
97 |
the message is simply placed on the end of the queue.
|
|
98 |
When received the message will contain a reference counted pointer to the
|
|
99 |
current thread.
|
|
100 |
|
|
101 |
@param aQ Message queue to which message should be sent.
|
|
102 |
|
|
103 |
@return The result parameter passed to TMessageBase::Complete().
|
|
104 |
|
|
105 |
@pre No fast mutex can be held.
|
|
106 |
@pre Call in a thread context.
|
|
107 |
@pre Kernel must be unlocked
|
|
108 |
@pre interrupts enabled
|
|
109 |
|
|
110 |
@see TMessageBase::Complete()
|
|
111 |
*/
|
|
112 |
EXPORT_C TInt TMessageBase::SendReceive(TMessageQue* aQ)
|
|
113 |
{
|
|
114 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"TMessageBase::SendReceive");
|
|
115 |
__KTRACE_OPT(KSERVER,Kern::Printf(">MsgB::SendRcv %08x to %08x",this,aQ));
|
|
116 |
Send(aQ);
|
|
117 |
NKern::FSWait(&iSem);
|
|
118 |
__KTRACE_OPT(KSERVER,Kern::Printf("<MsgB::SendRcv ret %d",iValue));
|
|
119 |
return iValue;
|
|
120 |
}
|
|
121 |
|
|
122 |
|
|
123 |
/** Completes a kernel message and optionally receive the next one.
|
|
124 |
|
|
125 |
@param aResult Completion code to pass back to message sender.
|
|
126 |
@param aReceiveNext TRUE means receive the next message on the same queue (if any) immediately.
|
|
127 |
FALSE means don't receive any more messages on that queue yet.
|
|
128 |
|
|
129 |
@pre No fast mutex can be held.
|
|
130 |
@pre Call in a thread context.
|
|
131 |
@pre Kernel must be unlocked
|
|
132 |
@pre interrupts enabled
|
|
133 |
@pre Calling thread must be in a critical section
|
|
134 |
|
|
135 |
@post The reference count on the sending thread is closed asynchronously.
|
|
136 |
|
|
137 |
@see TMessageBase::SendReceive()
|
|
138 |
*/
|
|
139 |
EXPORT_C void TMessageBase::Complete(TInt aResult, TBool aReceiveNext)
|
|
140 |
{
|
|
141 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TMessageBase::Complete");
|
|
142 |
__KTRACE_OPT(KSERVER,Kern::Printf("MsgB::Complete %08x, %d",this,aResult));
|
|
143 |
TMessageQue::Lock();
|
|
144 |
__NK_ASSERT_ALWAYS(iState==EAccepted);
|
|
145 |
iValue=aResult;
|
|
146 |
iState=EFree;
|
|
147 |
DThread* pT=_LOFF(iSem.iOwningThread,DThread,iNThread);
|
|
148 |
if (aReceiveNext)
|
|
149 |
{
|
|
150 |
__NK_ASSERT_ALWAYS(!iQueue->iReady);
|
|
151 |
if (!iQueue->iQ.IsEmpty())
|
|
152 |
{
|
|
153 |
TMessageBase* pM=(TMessageBase*)iQueue->iQ.First()->Deque();
|
|
154 |
__KTRACE_OPT(KSERVER,Kern::Printf("rxnext: got %08x",pM));
|
|
155 |
pM->iState=EAccepted;
|
|
156 |
iQueue->iMessage=pM;
|
|
157 |
iQueue->Enque();
|
|
158 |
}
|
|
159 |
else
|
|
160 |
{
|
|
161 |
__KTRACE_OPT(KSERVER,Kern::Printf("rxnext"));
|
|
162 |
iQueue->iReady=ETrue;
|
|
163 |
iQueue->iMessage=NULL;
|
|
164 |
}
|
|
165 |
}
|
|
166 |
iQueue=NULL;
|
|
167 |
NKern::FSSignal(&iSem,&TMessageQue::MsgLock);
|
|
168 |
pT->AsyncClose();
|
|
169 |
}
|
|
170 |
|
|
171 |
|
|
172 |
/** Forwards a kernel message to another queue, and optionally receives the next
|
|
173 |
message on the original queue.
|
|
174 |
|
|
175 |
@param aQ The queue to which the message should be forwarded.
|
|
176 |
@param aReceiveNext TRUE means receive the next message on the original queue (if any) immediately
|
|
177 |
FALSE means don't receive any more messages on that queue yet.
|
|
178 |
|
|
179 |
@pre No fast mutex can be held.
|
|
180 |
@pre Call in a thread context.
|
|
181 |
@pre Kernel must be unlocked
|
|
182 |
@pre interrupts enabled
|
|
183 |
*/
|
|
184 |
EXPORT_C void TMessageBase::Forward(TMessageQue* aQ, TBool aReceiveNext)
|
|
185 |
{
|
|
186 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"TMessageBase::Forward");
|
|
187 |
__KTRACE_OPT(KSERVER,Kern::Printf("MsgB::Forward %08x->%08x",this,aQ));
|
|
188 |
TMessageQue::Lock();
|
|
189 |
__NK_ASSERT_ALWAYS(iState==EAccepted);
|
|
190 |
if (aReceiveNext)
|
|
191 |
{
|
|
192 |
__NK_ASSERT_ALWAYS(!iQueue->iReady);
|
|
193 |
if (!iQueue->iQ.IsEmpty())
|
|
194 |
{
|
|
195 |
TMessageBase* pM=(TMessageBase*)iQueue->iQ.First()->Deque();
|
|
196 |
__KTRACE_OPT(KSERVER,Kern::Printf("rxnext: got %08x",pM));
|
|
197 |
pM->iState=EAccepted;
|
|
198 |
iQueue->iMessage=pM;
|
|
199 |
iQueue->Enque();
|
|
200 |
}
|
|
201 |
else
|
|
202 |
{
|
|
203 |
__KTRACE_OPT(KSERVER,Kern::Printf("rxnext"));
|
|
204 |
iQueue->iReady=ETrue;
|
|
205 |
iQueue->iMessage=NULL;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
iQueue=aQ;
|
|
209 |
if (aQ->iReady)
|
|
210 |
{
|
|
211 |
iState=EAccepted;
|
|
212 |
aQ->iMessage=this;
|
|
213 |
aQ->iReady=EFalse;
|
|
214 |
aQ->UnlockAndKick();
|
|
215 |
}
|
|
216 |
else
|
|
217 |
{
|
|
218 |
aQ->iQ.Add(this);
|
|
219 |
iState=EDelivered;
|
|
220 |
TMessageQue::Unlock();
|
|
221 |
}
|
|
222 |
}
|
|
223 |
|
|
224 |
|
|
225 |
/** Called by kernel server if client thread exits with a message outstanding.
|
|
226 |
|
|
227 |
Only cancels the message if its state is DELIVERED - that is it is on a
|
|
228 |
queue and not currently being processed.
|
|
229 |
Other measures must be taken to avoid problems if threads exit while their
|
|
230 |
synchronous kernel messages are ACCEPTED.
|
|
231 |
|
|
232 |
@pre No fast mutex can be held.
|
|
233 |
@pre Call in a thread context.
|
|
234 |
@pre Kernel must be unlocked
|
|
235 |
@pre interrupts enabled
|
|
236 |
@pre Calling thread must be in a critical section
|
|
237 |
|
|
238 |
@post If the original message state was DELIVERED the reference count on
|
|
239 |
the sending thread is closed asynchronously.
|
|
240 |
|
|
241 |
@internalTechnology
|
|
242 |
*/
|
|
243 |
EXPORT_C void TMessageBase::Cancel()
|
|
244 |
{
|
|
245 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TMessageBase::Cancel");
|
|
246 |
__KTRACE_OPT(KSERVER,Kern::Printf("MsgB::Cancel %08x",this));
|
|
247 |
DThread* pT=NULL;
|
|
248 |
TMessageQue::Lock();
|
|
249 |
switch(iState)
|
|
250 |
{
|
|
251 |
case EDelivered:
|
|
252 |
Deque();
|
|
253 |
pT=_LOFF(iSem.iOwningThread,DThread,iNThread);
|
|
254 |
iState=EFree;
|
|
255 |
iQueue=NULL;
|
|
256 |
case EAccepted:
|
|
257 |
case EFree:
|
|
258 |
break;
|
|
259 |
}
|
|
260 |
TMessageQue::Unlock();
|
|
261 |
if (pT)
|
|
262 |
pT->AsyncClose();
|
|
263 |
}
|
|
264 |
|
|
265 |
|
|
266 |
/** Panics the sender of a kernel message.
|
|
267 |
|
|
268 |
Also completes the message with reason code KErrDied. This is done so that
|
|
269 |
the open reference on the client can be closed.
|
|
270 |
|
|
271 |
@param aCategory Category string for panic.
|
|
272 |
@param aReason Reason code for panic.
|
|
273 |
|
|
274 |
@pre No fast mutex can be held.
|
|
275 |
@pre Call in a thread context.
|
|
276 |
@pre Kernel must be unlocked
|
|
277 |
@pre interrupts enabled
|
|
278 |
*/
|
|
279 |
EXPORT_C void TMessageBase::PanicClient(const TDesC& aCategory, TInt aReason)
|
|
280 |
{
|
|
281 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"TMessageBase::PanicClient");
|
|
282 |
NKern::LockSystem();
|
|
283 |
DThread* pT=_LOFF(iSem.iOwningThread,DThread,iNThread);
|
|
284 |
pT->Die(EExitPanic,aReason,aCategory);
|
|
285 |
Complete(KErrDied,ETrue); // so reference on client is closed
|
|
286 |
}
|
|
287 |
|
|
288 |
|
|
289 |
/** Gets a pointer to the sending thread.
|
|
290 |
|
|
291 |
@return Pointer to the thread which sent this message.
|
|
292 |
This pointer is reference counted provided that the message has not
|
|
293 |
yet been completed.
|
|
294 |
|
|
295 |
@pre Call in any context.
|
|
296 |
@pre Message must be in ACCEPTED state.
|
|
297 |
*/
|
|
298 |
EXPORT_C DThread* TMessageBase::Client()
|
|
299 |
{
|
|
300 |
return _LOFF(iSem.iOwningThread,DThread,iNThread);
|
|
301 |
}
|
|
302 |
|
|
303 |
|
|
304 |
/** Gets the current thread's kernel message.
|
|
305 |
|
|
306 |
@return Current thread's kernel message.
|
|
307 |
|
|
308 |
@pre No fast mutex can be held.
|
|
309 |
@pre Call in a thread context.
|
|
310 |
@pre Kernel must be unlocked
|
|
311 |
@pre interrupts enabled
|
|
312 |
*/
|
|
313 |
EXPORT_C TThreadMessage& Kern::Message()
|
|
314 |
{
|
|
315 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::Message");
|
|
316 |
return TheCurrentThread->iKernMsg;
|
|
317 |
}
|
|
318 |
|
|
319 |
|
|
320 |
/** Constructs a kernel side message queue.
|
|
321 |
|
|
322 |
A message queue contains a DFC which runs whenever a message is available
|
|
323 |
and has been requested.
|
|
324 |
|
|
325 |
@param aFunction Function to be called on reception of a message.
|
|
326 |
@param aPtr Arbitrary parameter to be passed to above function.
|
|
327 |
@param aDfcQ Pointer to DFC queue to be used for processing messages.
|
|
328 |
@param aPriority Priority of this message queue within the DFC queue (0-7).
|
|
329 |
*/
|
|
330 |
EXPORT_C TMessageQue::TMessageQue(TDfcFn aFunction, TAny* aPtr, TDfcQue* aDfcQ, TInt aPriority)
|
|
331 |
: TDfc(aFunction,aPtr,aDfcQ,aPriority),
|
|
332 |
iReady(EFalse),
|
|
333 |
iMessage(NULL)
|
|
334 |
{
|
|
335 |
}
|
|
336 |
|
|
337 |
|
|
338 |
/** Requests the next message on this queue.
|
|
339 |
|
|
340 |
If the queue is nonempty the next message is removed from the queue, marked
|
|
341 |
as ACCEPTED and a pointer to it is placed in the iMessage member; the DFC is
|
|
342 |
then activated.
|
|
343 |
|
|
344 |
If the queue is empty it is marked as ready so that the next message to be
|
|
345 |
sent will be accepted immediately.
|
|
346 |
|
|
347 |
@pre No fast mutex can be held.
|
|
348 |
@pre Call in a thread context.
|
|
349 |
@pre Kernel must be unlocked
|
|
350 |
@pre interrupts enabled
|
|
351 |
*/
|
|
352 |
EXPORT_C void TMessageQue::Receive()
|
|
353 |
{
|
|
354 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"TMessageQue::Receive");
|
|
355 |
Lock();
|
|
356 |
__NK_ASSERT_ALWAYS(!iReady);
|
|
357 |
if (!iQ.IsEmpty())
|
|
358 |
{
|
|
359 |
iMessage=(TMessageBase*)iQ.First()->Deque();
|
|
360 |
__KTRACE_OPT(KSERVER,Kern::Printf("MsgQ:Rx got %08x",iMessage));
|
|
361 |
iMessage->iState=TMessageBase::EAccepted;
|
|
362 |
UnlockAndKick();
|
|
363 |
}
|
|
364 |
else
|
|
365 |
{
|
|
366 |
__KTRACE_OPT(KSERVER,Kern::Printf("MsgQ:Rx"));
|
|
367 |
iReady=ETrue;
|
|
368 |
iMessage=NULL;
|
|
369 |
Unlock();
|
|
370 |
}
|
|
371 |
}
|
|
372 |
|
|
373 |
|
|
374 |
/** Gets the next message synchronously if there is one.
|
|
375 |
|
|
376 |
If the queue is nonempty the next message is removed from the queue, marked
|
|
377 |
as ACCEPTED and a pointer to it returned.
|
|
378 |
If the queue is empty NULL is returned.
|
|
379 |
No asynchronous receive operation may be pending on the queue.
|
|
380 |
|
|
381 |
@return Pointer to next message or NULL if there is none.
|
|
382 |
|
|
383 |
@pre No fast mutex can be held.
|
|
384 |
@pre Call in a thread context.
|
|
385 |
@pre Queue must not be in asynchronous receive mode.
|
|
386 |
@pre Kernel must be unlocked
|
|
387 |
@pre interrupts enabled
|
|
388 |
*/
|
|
389 |
EXPORT_C TMessageBase* TMessageQue::Poll()
|
|
390 |
{
|
|
391 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"TMessageQue::Poll");
|
|
392 |
TMessageBase* pM=NULL;
|
|
393 |
Lock();
|
|
394 |
__NK_ASSERT_ALWAYS(!iReady);
|
|
395 |
if (!iQ.IsEmpty())
|
|
396 |
{
|
|
397 |
pM=(TMessageBase*)iQ.First()->Deque();
|
|
398 |
__KTRACE_OPT(KSERVER,Kern::Printf("MsgQ:Poll got %08x",pM));
|
|
399 |
pM->iState=TMessageBase::EAccepted;
|
|
400 |
}
|
|
401 |
Unlock();
|
|
402 |
return pM;
|
|
403 |
}
|
|
404 |
|
|
405 |
|
|
406 |
/** Finds the last outstanding message on this queue.
|
|
407 |
|
|
408 |
If the queue is nonempty a pointer to the last currently outstanding message
|
|
409 |
is returned. The message is NOT marked as ACCEPTED or removed from the queue.
|
|
410 |
If the queue is empty NULL is returned.
|
|
411 |
|
|
412 |
@return Pointer to last outstanding message or NULL if there is none.
|
|
413 |
|
|
414 |
@pre No fast mutex can be held.
|
|
415 |
@pre Call in a thread context.
|
|
416 |
@pre Kernel must be unlocked
|
|
417 |
@pre interrupts enabled
|
|
418 |
*/
|
|
419 |
EXPORT_C TMessageBase* TMessageQue::Last()
|
|
420 |
{
|
|
421 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"TMessageQue::Last");
|
|
422 |
TMessageBase* pM=NULL;
|
|
423 |
Lock();
|
|
424 |
if (!iQ.IsEmpty())
|
|
425 |
{
|
|
426 |
pM=(TMessageBase*)iQ.Last();
|
|
427 |
__KTRACE_OPT(KSERVER,Kern::Printf("MsgQ(%08x):Last=%08x",this,pM));
|
|
428 |
}
|
|
429 |
Unlock();
|
|
430 |
return pM;
|
|
431 |
}
|
|
432 |
|
|
433 |
|
|
434 |
/** Completes all outstanding messages on this queue.
|
|
435 |
|
|
436 |
No request is made to receive further messages.
|
|
437 |
|
|
438 |
@param aResult Completion code to pass back to message sender(s).
|
|
439 |
|
|
440 |
@pre No fast mutex can be held.
|
|
441 |
@pre Call in a thread context.
|
|
442 |
@pre Kernel must be unlocked
|
|
443 |
@pre interrupts enabled
|
|
444 |
@pre Calling thread must be in a critical section
|
|
445 |
|
|
446 |
@post The reference counts on all sending threads are closed asynchronously.
|
|
447 |
@post The queue is not ready to receive further messages.
|
|
448 |
*/
|
|
449 |
EXPORT_C void TMessageQue::CompleteAll(TInt aResult)
|
|
450 |
{
|
|
451 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"TMessageQue::CompleteAll");
|
|
452 |
TMessageBase* pM;
|
|
453 |
while ((pM=Poll())!=NULL)
|
|
454 |
pM->Complete(aResult,EFalse);
|
|
455 |
}
|
|
456 |
|
|
457 |
|
|
458 |
/** Reads a descriptor from a thread's process.
|
|
459 |
|
|
460 |
It reads a descriptor from a thread's address space, enforcing checks on validity of source and destination if necessary.
|
|
461 |
It is used especially by device drivers to transfer data from a user thread.
|
|
462 |
aDest might be accessed with user permission validation.
|
|
463 |
|
|
464 |
@param aThread Thread from which address space to read.
|
|
465 |
@param aSrc Pointer to a descriptor to read from. It will fail with KErrBadDescriptor if it's NULL.
|
|
466 |
@param aDest Descriptor to write into.
|
|
467 |
@param aOffset Offset in aPtr from where to start reading.
|
|
468 |
@param aMode Flags specifying how to read. KChunkShiftBy1 treats descriptors as 16bit while KChunkShiftBy0 treats descriptors as 8bit variants.
|
|
469 |
|
|
470 |
@return KErrNone, if succesful;
|
|
471 |
KErrBadDescriptor, if aSrc or aDest is an invalid decriptor;
|
|
472 |
KErrArgument, if aOffset is negative;
|
|
473 |
KErrDied, if aThread is dead.
|
|
474 |
|
|
475 |
@pre No fast mutex can be held.
|
|
476 |
@pre Call in a thread context.
|
|
477 |
@pre Kernel must be unlocked
|
|
478 |
@pre interrupts enabled
|
|
479 |
*/
|
|
480 |
EXPORT_C TInt Kern::ThreadDesRead(DThread* aThread, const TAny* aSrc, TDes8& aDest, TInt aOffset, TInt aMode)
|
|
481 |
{
|
|
482 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::ThreadDesRead");
|
|
483 |
_CHECK_DFCQ01_CONTEXT("Kern::ThreadDesRead");
|
|
484 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
485 |
NKern::LockSystem();
|
|
486 |
#endif
|
|
487 |
TInt r=aThread->DesRead(aSrc,(TUint8*)aDest.Ptr(),aDest.MaxLength(),aOffset,aMode);
|
|
488 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
489 |
NKern::UnlockSystem();
|
|
490 |
#endif
|
|
491 |
if (r<0)
|
|
492 |
return r;
|
|
493 |
aDest.SetLength(r);
|
|
494 |
return KErrNone;
|
|
495 |
}
|
|
496 |
|
|
497 |
|
|
498 |
/** Reads a raw buffer from a thread's process.
|
|
499 |
|
|
500 |
It reads a raw buffer from a thread's address space, enforcing checks on validity of source and destination if necessary.
|
|
501 |
It is used especially by device drivers to transfer data from a user thread.
|
|
502 |
aDest might be accessed with user permission validation.
|
|
503 |
|
|
504 |
@param aThread Thread from which address space to read.
|
|
505 |
@param aSrc Pointer to a buffer to read from. The buffer is in aThread's address space.
|
|
506 |
@param aDest Pointer to a buffer to write into. The buffer is in the current process's address space.
|
|
507 |
@param aSize Length in bytes to be read.
|
|
508 |
|
|
509 |
@return KErrNone, if successful;
|
|
510 |
KErrDied, if aThread is dead.
|
|
511 |
KErrBadDescriptor, if the attempt to read aSrc buffer causes exception.
|
|
512 |
|
|
513 |
@pre No fast mutex can be held.
|
|
514 |
@pre Call in a thread context.
|
|
515 |
@pre Kernel must be unlocked
|
|
516 |
@pre interrupts enabled
|
|
517 |
*/
|
|
518 |
EXPORT_C TInt Kern::ThreadRawRead(DThread* aThread, const TAny* aSrc, TAny* aDest, TInt aSize)
|
|
519 |
{
|
|
520 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::ThreadRawRead");
|
|
521 |
_CHECK_DFCQ01_CONTEXT("Kern::ThreadRawRead");
|
|
522 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
523 |
NKern::LockSystem();
|
|
524 |
#endif
|
|
525 |
TIpcExcTrap xt;
|
|
526 |
xt.iLocalBase=0;
|
|
527 |
xt.iRemoteBase=(TLinAddr)aSrc;
|
|
528 |
xt.iSize=aSize;
|
|
529 |
xt.iDir=0;
|
|
530 |
TInt r=xt.Trap(aThread);
|
|
531 |
if (r==0)
|
|
532 |
{
|
|
533 |
//On some memory models(such as moving), RawRead may update the content of xt. It happens if home address
|
|
534 |
// is accessed (instead of the provided run address) or if it reads/writes in chunks.
|
|
535 |
r=aThread->RawRead(aSrc,aDest,aSize,0, &xt);
|
|
536 |
xt.UnTrap();
|
|
537 |
}
|
|
538 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
539 |
NKern::UnlockSystem();
|
|
540 |
#endif
|
|
541 |
return r;
|
|
542 |
}
|
|
543 |
|
|
544 |
|
|
545 |
/** Writes a descriptor to a thread's process.
|
|
546 |
|
|
547 |
It writes a descriptor to a thread's address space, enforcing checks on validity of source and destination if necessary.
|
|
548 |
It is used especially by device drivers to transfer data to a user thread.
|
|
549 |
aDest might be accessed with user permission validation.
|
|
550 |
|
|
551 |
@param aThread Thread from which address space to read.
|
|
552 |
@param aDest Pointer to a descriptor to write into. It will fail with KErrBadDescriptor if it's NULL.
|
|
553 |
@param aSrc Descriptor to read from.
|
|
554 |
@param aOffset Offset in aDest from where to start writing.
|
|
555 |
@param aMode Flags specifying how to write:
|
|
556 |
KChunkShiftBy1 treats descriptors as 16bit;
|
|
557 |
KChunkShiftBy0 treats descriptors as 8bit variants;
|
|
558 |
KTruncateToMaxLength ensures that data in the target descriptor is truncated, if necessary,
|
|
559 |
to ensure that it does not exceed the target descriptor's maximum length.
|
|
560 |
@param aOrigThread The thread on behalf of which this operation is performed (eg client of device driver).
|
|
561 |
|
|
562 |
@return KErrNone, if successful;
|
|
563 |
KErrBadDescriptor, if aSrc or aDest is an invalid decriptor;
|
|
564 |
KErrArgument, if aOffset is negative;
|
|
565 |
KErrDied, if aThread is dead.
|
|
566 |
|
|
567 |
@pre No fast mutex can be held.
|
|
568 |
@pre Call in a thread context.
|
|
569 |
@pre Kernel must be unlocked
|
|
570 |
@pre interrupts enabled
|
|
571 |
*/
|
|
572 |
EXPORT_C TInt Kern::ThreadDesWrite(DThread* aThread, TAny* aDest, const TDesC8& aSrc, TInt aOffset, TInt aMode, DThread* aOrigThread)
|
|
573 |
{
|
|
574 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::ThreadDesWrite");
|
|
575 |
_CHECK_DFCQ01_CONTEXT("Kern::ThreadDesWrite");
|
|
576 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
577 |
NKern::LockSystem();
|
|
578 |
#endif
|
|
579 |
TInt r=aThread->DesWrite(aDest,aSrc.Ptr(),aSrc.Length(),aOffset,aMode,aOrigThread);
|
|
580 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
581 |
NKern::UnlockSystem();
|
|
582 |
#endif
|
|
583 |
return r;
|
|
584 |
}
|
|
585 |
|
|
586 |
|
|
587 |
/** Writes a raw buffer to a thread's process.
|
|
588 |
|
|
589 |
It writes a raw buffer to a thread's address space, enforcing checks on validity of source and destination if necessary.
|
|
590 |
It is used especially by device drivers to transfer data to a user thread.
|
|
591 |
aDest might be accessed with user permission validation.
|
|
592 |
|
|
593 |
@param aThread Thread to which address space to write.
|
|
594 |
@param aDest Pointer to a buffer to write into. It belongs to aThread's address space.
|
|
595 |
@param aSrc Pointer to a buffer to read from. It belongs to the current process's address space.
|
|
596 |
@param aSize Size in bytes of the copied data.
|
|
597 |
@param aOrigThread The thread on behalf of which this operation is performed (eg client of device driver). If NULL, current thread is assumed.
|
|
598 |
|
|
599 |
@return KErrNone, if successful;
|
|
600 |
KErrDied, if aThread is dead.
|
|
601 |
KErrBadDescriptor, if the attempt to write to aDest buffer causes exception.
|
|
602 |
|
|
603 |
@pre No fast mutex can be held.
|
|
604 |
@pre Call in a thread context.
|
|
605 |
@pre Kernel must be unlocked
|
|
606 |
@pre interrupts enabled
|
|
607 |
*/
|
|
608 |
EXPORT_C TInt Kern::ThreadRawWrite(DThread* aThread, TAny* aDest, const TAny* aSrc, TInt aSize, DThread* aOrigThread)
|
|
609 |
{
|
|
610 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::ThreadRawWrite");
|
|
611 |
_CHECK_DFCQ01_CONTEXT("Kern::ThreadRawWrite");
|
|
612 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
613 |
NKern::LockSystem();
|
|
614 |
#endif
|
|
615 |
TIpcExcTrap xt;
|
|
616 |
xt.iLocalBase=0;
|
|
617 |
xt.iRemoteBase=(TLinAddr)aDest;
|
|
618 |
xt.iSize=aSize;
|
|
619 |
xt.iDir=1;
|
|
620 |
TInt r=xt.Trap(aThread);
|
|
621 |
if (r==0)
|
|
622 |
{
|
|
623 |
//On some memory models(such as moving), RawRead may update the content of xt. It happens if home address
|
|
624 |
// is accessed (instead of the provided run address) or if it reads/writes in chunks.
|
|
625 |
r=aThread->RawWrite(aDest,aSrc,aSize,0,aOrigThread, &xt);
|
|
626 |
xt.UnTrap();
|
|
627 |
}
|
|
628 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
629 |
NKern::UnlockSystem();
|
|
630 |
#endif
|
|
631 |
return r;
|
|
632 |
}
|
|
633 |
|
|
634 |
|
|
635 |
/** Reads a descriptor from a thread's process.
|
|
636 |
|
|
637 |
It reads a descriptor from a thread's address space, enforcing checks on validity of source and destination if necessary.
|
|
638 |
It is used especially by device drivers to transfer data from a user thread.
|
|
639 |
aDest might be accessed with user permission validation.
|
|
640 |
|
|
641 |
@param aThread Thread from which address space to read.
|
|
642 |
@param aSrc A TClientBuffer object containing the descriptor information.
|
|
643 |
@param aDest Descriptor to write into.
|
|
644 |
@param aOffset Offset in aPtr from where to start reading.
|
|
645 |
@param aMode Flags specifying how to read. KChunkShiftBy1 treats descriptors as 16bit while KChunkShiftBy0 treats descriptors as 8bit variants.
|
|
646 |
|
|
647 |
@return KErrNone, if succesful;
|
|
648 |
KErrBadDescriptor, if aSrc or aDest is an invalid decriptor;
|
|
649 |
KErrArgument, if aOffset is negative;
|
|
650 |
KErrDied, if aThread is dead.
|
|
651 |
|
|
652 |
@pre No fast mutex can be held.
|
|
653 |
@pre Call in a thread context.
|
|
654 |
@pre Kernel must be unlocked
|
|
655 |
@pre interrupts enabled
|
|
656 |
|
|
657 |
@publishedPartner
|
|
658 |
@released
|
|
659 |
*/
|
|
660 |
EXPORT_C TInt Kern::ThreadBufRead(DThread* aThread, const TClientBuffer* aSrc, TDes8& aDest, TInt aOffset, TInt aMode)
|
|
661 |
{
|
|
662 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::ThreadBufRead");
|
|
663 |
_CHECK_DFCQ01_CONTEXT("Kern::ThreadBufRead");
|
|
664 |
__ASSERT_DEBUG(aSrc, K::Fault(K::EThreadBufReadWithNullPointer));
|
|
665 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
666 |
NKern::LockSystem();
|
|
667 |
#endif
|
|
668 |
TInt r=aThread->DoDesRead(aSrc->iHeader,(TUint8*)aDest.Ptr(),aDest.MaxLength(),aOffset,aMode);
|
|
669 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
670 |
NKern::UnlockSystem();
|
|
671 |
#endif
|
|
672 |
if (r<0)
|
|
673 |
return r;
|
|
674 |
aDest.SetLength(r);
|
|
675 |
return KErrNone;
|
|
676 |
}
|
|
677 |
|
|
678 |
|
|
679 |
/** Writes a descriptor to a thread's process.
|
|
680 |
|
|
681 |
It writes a descriptor to a thread's address space, enforcing checks on validity of source and destination if necessary.
|
|
682 |
It is used especially by device drivers to transfer data to a user thread.
|
|
683 |
aDest might be accessed with user permission validation.
|
|
684 |
|
|
685 |
@param aThread Thread from which address space to read.
|
|
686 |
@param aDest A TClientBuffer object containing information about the descriptorto write into.
|
|
687 |
@param aSrc Descriptor to read from.
|
|
688 |
@param aOffset Offset in aDest from where to start writing.
|
|
689 |
@param aMode Flags specifying how to write:
|
|
690 |
KChunkShiftBy1 treats descriptors as 16bit;
|
|
691 |
KChunkShiftBy0 treats descriptors as 8bit variants;
|
|
692 |
KTruncateToMaxLength ensures that data in the target descriptor is truncated, if necessary,
|
|
693 |
to ensure that it does not exceed the target descriptor's maximum length.
|
|
694 |
@param aOrigThread The thread on behalf of which this operation is performed (eg client of device driver).
|
|
695 |
|
|
696 |
@return KErrNone, if successful;
|
|
697 |
KErrBadDescriptor, if aSrc or aDest is an invalid decriptor;
|
|
698 |
KErrArgument, if aOffset is negative;
|
|
699 |
KErrDied, if aThread is dead.
|
|
700 |
|
|
701 |
@pre No fast mutex can be held.
|
|
702 |
@pre Call in a thread context.
|
|
703 |
@pre Kernel must be unlocked
|
|
704 |
@pre interrupts enabled
|
|
705 |
|
|
706 |
@publishedPartner
|
|
707 |
@released
|
|
708 |
*/
|
|
709 |
EXPORT_C TInt Kern::ThreadBufWrite(DThread* aThread, TClientBuffer* aDest, const TDesC8& aSrc, TInt aOffset, TInt aMode, DThread* aOrigThread)
|
|
710 |
{
|
|
711 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::ThreadBufWrite");
|
|
712 |
_CHECK_DFCQ01_CONTEXT("Kern::ThreadBufesWrite");
|
|
713 |
__ASSERT_DEBUG(aDest, K::Fault(K::EThreadBufWriteWithNullPointer));
|
|
714 |
if (!aDest->IsWriteable())
|
|
715 |
return KErrBadDescriptor;
|
|
716 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
717 |
NKern::LockSystem();
|
|
718 |
#endif
|
|
719 |
TInt r=aThread->DoDesWrite(aDest->DesPtr(),aDest->iHeader,aSrc.Ptr(),aSrc.Length(),aOffset,aMode|KDoNotUpdateDesLength,aOrigThread);
|
|
720 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
721 |
NKern::UnlockSystem();
|
|
722 |
#endif
|
|
723 |
if (r < KErrNone)
|
|
724 |
return r;
|
|
725 |
aDest->iHeader.SetTypeAndLength(r);
|
|
726 |
return KErrNone;
|
|
727 |
}
|
|
728 |
|
|
729 |
|
|
730 |
/** Gets the length of a descriptor in another thread's address space.
|
|
731 |
|
|
732 |
@param aThread Thread whose address space contains the descriptor.
|
|
733 |
@param aDes Descriptor whose length is to be fetched.
|
|
734 |
|
|
735 |
@return Length of descriptor or error code if it fails, specifically
|
|
736 |
KErrBadDescriptor if aDes is an invalid descriptor.
|
|
737 |
|
|
738 |
@pre No fast mutex can be held.
|
|
739 |
@pre Call in a thread context.
|
|
740 |
@pre Kernel must be unlocked
|
|
741 |
@pre interrupts enabled
|
|
742 |
*/
|
|
743 |
EXPORT_C TInt Kern::ThreadGetDesLength(DThread* aThread, const TAny* aDes)
|
|
744 |
{
|
|
745 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::ThreadGetDesLength");
|
|
746 |
_CHECK_DFCQ01_CONTEXT("Kern::ThreadGetDesLength");
|
|
747 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
748 |
NKern::LockSystem();
|
|
749 |
#endif
|
|
750 |
TInt r=aThread->GetDesLength(aDes);
|
|
751 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
752 |
NKern::UnlockSystem();
|
|
753 |
#endif
|
|
754 |
return r;
|
|
755 |
}
|
|
756 |
|
|
757 |
|
|
758 |
/** Gets the maximum length of a descriptor in another thread's address space.
|
|
759 |
|
|
760 |
@param aThread Thread whose address space contains the descriptor.
|
|
761 |
@param aDes Descriptor whose maximum length is to be fetched.
|
|
762 |
|
|
763 |
@return Maximum length of descriptor or error code if it fails,
|
|
764 |
specifically KErrBadDescriptor if aDes is an invalid descriptor.
|
|
765 |
|
|
766 |
@pre No fast mutex can be held.
|
|
767 |
@pre Call in a thread context.
|
|
768 |
@pre Kernel must be unlocked
|
|
769 |
@pre interrupts enabled
|
|
770 |
*/
|
|
771 |
EXPORT_C TInt Kern::ThreadGetDesMaxLength(DThread* aThread, const TAny* aDes)
|
|
772 |
{
|
|
773 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::ThreadGetDesMaxLength");
|
|
774 |
_CHECK_DFCQ01_CONTEXT("Kern::ThreadGetDesLMaxLength");
|
|
775 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
776 |
NKern::LockSystem();
|
|
777 |
#endif
|
|
778 |
TInt r=aThread->GetDesMaxLength(aDes);
|
|
779 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
780 |
NKern::UnlockSystem();
|
|
781 |
#endif
|
|
782 |
return r;
|
|
783 |
}
|
|
784 |
|
|
785 |
|
|
786 |
/** Gets information about a descriptor in another thread's address space.
|
|
787 |
|
|
788 |
It returns length, maximum length and pointer to the descriptor's buffer in the parameters sent by reference.
|
|
789 |
|
|
790 |
@param aThread Thread to read descriptor's maximum length from.
|
|
791 |
@param aDes Descriptor to get info about.
|
|
792 |
@param aLength Reference to a location where length of the descriptor will be copied.
|
|
793 |
@param aMaxLength Reference to a location where maximum length of the descriptor will be copied.
|
|
794 |
@param aPtr Reference to a location where the pointer to descriptor's buffer will be copied.
|
|
795 |
@param aWriteable ETrue if descriptor is writeable (eg. TBuf<>) and EFalse otherwise (eg. TBufC<>).
|
|
796 |
|
|
797 |
@return KErrNone, if successful;
|
|
798 |
KErrBadDescriptor, if aDes is an invalid descriptor, or if aWriteable is ETrue and aDes is a constant descriptor.
|
|
799 |
|
|
800 |
@pre No fast mutex can be held.
|
|
801 |
@pre Call in a thread context.
|
|
802 |
@pre Kernel must be unlocked
|
|
803 |
@pre interrupts enabled
|
|
804 |
*/
|
|
805 |
EXPORT_C TInt Kern::ThreadGetDesInfo(DThread* aThread, const TAny* aDes, TInt& aLength, TInt& aMaxLength, TUint8*& aPtr, TBool aWriteable)
|
|
806 |
{
|
|
807 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::ThreadGetDesInfo");
|
|
808 |
_CHECK_DFCQ01_CONTEXT("Kern::ThreadGetDesInfo");
|
|
809 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
810 |
NKern::LockSystem();
|
|
811 |
#endif
|
|
812 |
TInt r=aThread->GetDesInfo(aDes,aLength,aMaxLength,aPtr,aWriteable);
|
|
813 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
814 |
NKern::UnlockSystem();
|
|
815 |
#endif
|
|
816 |
return r;
|
|
817 |
}
|
|
818 |
|
|
819 |
|
|
820 |
/********************************************
|
|
821 |
* Kernel server client side
|
|
822 |
********************************************/
|
|
823 |
|
|
824 |
void GetCategory(TDes& aDest, const TDesC& aSrc)
|
|
825 |
{
|
|
826 |
TInt ulen, umax;
|
|
827 |
TUint8* kptr=(TUint8*)aDest.Ptr();
|
|
828 |
const TUint8* uptr=Kern::KUDesInfo(aSrc, ulen, umax);
|
|
829 |
if (ulen>KMaxExitCategoryName)
|
|
830 |
ulen=KMaxExitCategoryName;
|
|
831 |
aDest.SetLength(ulen);
|
|
832 |
kumemget(kptr,uptr,ulen);
|
|
833 |
}
|
|
834 |
|
|
835 |
void ExecHandler::ThreadKill(TInt aHandle, TExitType aType, TInt aReason, const TDesC8* aCategory)
|
|
836 |
//
|
|
837 |
// Enter and leave with system unlocked
|
|
838 |
//
|
|
839 |
{
|
|
840 |
TBuf<KMaxExitCategoryName> cat;
|
|
841 |
if (aType==EExitPanic && aCategory)
|
|
842 |
GetCategory(cat,*aCategory);
|
|
843 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ThreadKill %d,%d,%lS",aType,aReason,&cat));
|
|
844 |
K::CheckKernelUnlocked();
|
|
845 |
NKern::LockSystem();
|
|
846 |
DThread* pT=(DThread*)K::ObjectFromHandle(aHandle,EThread);
|
|
847 |
if (!pT)
|
|
848 |
K::PanicCurrentThread(EBadHandle);
|
|
849 |
if (pT->iOwningProcess->iSecurityZone != TheCurrentThread->iOwningProcess->iSecurityZone)
|
|
850 |
K::ProcessIsolationFailure(__PLATSEC_DIAGNOSTIC_STRING("Attempt to Kill, Terminate or Panic a thread in another process"));
|
|
851 |
pT->Die(aType,aReason,cat); // releases system lock
|
|
852 |
}
|
|
853 |
|
|
854 |
void ExecHandler::ProcessKill(TInt aHandle, TExitType aType, TInt aReason, const TDesC8* aCategory)
|
|
855 |
{
|
|
856 |
TBuf<KMaxExitCategoryName> cat;
|
|
857 |
if (aType==EExitPanic && aCategory)
|
|
858 |
GetCategory(cat,*aCategory);
|
|
859 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ProcessKill %d,%d,%lS",aType,aReason,&cat));
|
|
860 |
K::CheckKernelUnlocked();
|
|
861 |
NKern::LockSystem();
|
|
862 |
DProcess* pP=(DProcess*)K::ThreadEnterCS(aHandle,EProcess);
|
|
863 |
|
|
864 |
DProcess* currentProcess=TheCurrentThread->iOwningProcess;
|
|
865 |
if (pP->iSecurityZone != currentProcess->iSecurityZone // Not killing self...
|
|
866 |
&& currentProcess->iId!=pP->iCreatorId ) // and not creator
|
|
867 |
{
|
|
868 |
if(!currentProcess->HasCapability(ECapabilityPowerMgmt,__PLATSEC_DIAGNOSTIC_STRING("Checked when attempting to kill another process")))
|
|
869 |
{
|
|
870 |
// Doesn't have capability...
|
|
871 |
// Action not allowed
|
|
872 |
pP->Close(NULL);
|
|
873 |
K::ThreadLeaveCS();
|
|
874 |
K::LockedPlatformSecurityPanic();
|
|
875 |
}
|
|
876 |
}
|
|
877 |
|
|
878 |
pP->Die(aType,aReason,cat);
|
|
879 |
pP->Close(NULL);
|
|
880 |
NKern::ThreadLeaveCS();
|
|
881 |
}
|
|
882 |
|
|
883 |
void ExecHandler::ThreadSuspend(DThread* aThread)
|
|
884 |
//
|
|
885 |
// Suspend a thread. Enter and exit with system locked.
|
|
886 |
//
|
|
887 |
{
|
|
888 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ThreadSuspend %O",aThread));
|
|
889 |
if(aThread->iOwningProcess->iSecurityZone!=TheCurrentThread->iOwningProcess->iSecurityZone)
|
|
890 |
K::ProcessIsolationFailure(__PLATSEC_DIAGNOSTIC_STRING("Use of RThread::Suspend on a thread in another process"));
|
|
891 |
aThread->Suspend(1);
|
|
892 |
}
|
|
893 |
|
|
894 |
void K::PanicKernExec(TInt aReason)
|
|
895 |
{
|
|
896 |
// enter with system unlocked
|
|
897 |
NKern::LockSystem();
|
|
898 |
K::PanicCurrentThread(aReason);
|
|
899 |
}
|
|
900 |
|
|
901 |
void K::PanicCurrentThread(TInt aReason)
|
|
902 |
{
|
|
903 |
// enter with system locked
|
|
904 |
TheCurrentThread->Die(EExitPanic,aReason,KLitKernExec()); // doesn't return
|
|
905 |
}
|
|
906 |
|
|
907 |
|
|
908 |
/**
|
|
909 |
Panics the current thread.
|
|
910 |
|
|
911 |
If the kernel is locked or the current thread holds a fast mutex this will fault
|
|
912 |
the kernel.
|
|
913 |
It can be used in a device driver to panic the client thread in case of errors.
|
|
914 |
|
|
915 |
@param aCategory A descriptor that specifies the exit category to be set
|
|
916 |
in the current thread's exit information. It can't exceed
|
|
917 |
KMaxExitCategoryName characters in length.
|
|
918 |
@param aReason Reason for panic. Can be a standard kernel generated TKernelPanic
|
|
919 |
or it can be a custom reason (eg. if it comes from a device driver).
|
|
920 |
|
|
921 |
@pre Kernel must be unlocked.
|
|
922 |
@pre No fast mutex can be held.
|
|
923 |
@pre Call in a thread context.
|
|
924 |
@pre interrupts enabled
|
|
925 |
@pre Can be used in a device driver.
|
|
926 |
|
|
927 |
@post It doesn't return.
|
|
928 |
|
|
929 |
@see TKernelPanic
|
|
930 |
*/
|
|
931 |
EXPORT_C void Kern::PanicCurrentThread(const TDesC& aCategory, TInt aReason)
|
|
932 |
{
|
|
933 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::PanicCurrentThread");
|
|
934 |
// enter with system unlocked
|
|
935 |
__KTRACE_OPT(KEXEC,Kern::Printf("Kern::PanicCurrentThread %lS %d",&aCategory,aReason));
|
|
936 |
K::CheckKernelUnlocked();
|
|
937 |
NKern::LockSystem();
|
|
938 |
TheCurrentThread->Die(EExitPanic,aReason,aCategory); // doesn't return
|
|
939 |
}
|
|
940 |
|
|
941 |
|
|
942 |
/** Terminates the execution of a thread.
|
|
943 |
|
|
944 |
It terminates the specified thread and it sets its exit info.
|
|
945 |
This is an asynchronous operation.
|
|
946 |
|
|
947 |
This method can only be used by a thread to kill itself, or to kill
|
|
948 |
a user thread (iThreadType==EThreadUser). An attempt to kill a non-user
|
|
949 |
thread which is not the currently running thread will cause the system to fault
|
|
950 |
with KERN 94 (ENonUserThreadKilled).
|
|
951 |
|
|
952 |
@param aThread Thread to be terminated. If it's NULL then the current thread will be terminated.
|
|
953 |
@param aType Exit type. It can be one of EExitKill - if the thread was killed by another thread, EExitTerminate - usually for abnormal termination,EExitPanic - if the thread was terminated as the result of a panic.
|
|
954 |
@param aReason Exit code.
|
|
955 |
@param aCategory Exit category, this is relevant only if exit type is EExitPanic.
|
|
956 |
|
|
957 |
@pre No fast mutex can be held.
|
|
958 |
@pre Call in a thread context.
|
|
959 |
@pre Kernel must be unlocked
|
|
960 |
@pre interrupts enabled
|
|
961 |
*/
|
|
962 |
EXPORT_C void Kern::ThreadKill(DThread* aThread, TExitType aType, TInt aReason, const TDesC& aCategory)
|
|
963 |
{
|
|
964 |
CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::ThreadKill");
|
|
965 |
if (!aThread)
|
|
966 |
aThread=TheCurrentThread;
|
|
967 |
NKern::LockSystem();
|
|
968 |
aThread->Die(aType,aReason,aCategory);
|
|
969 |
}
|
|
970 |
|
|
971 |
/**
|
|
972 |
Closes a kernel side reference-counted object without blocking
|
|
973 |
the current thread.
|
|
974 |
|
|
975 |
The function decrements the object's reference count by one, and invokes
|
|
976 |
asynchronous deletion of the object when the final reference to the object
|
|
977 |
is being closed.
|
|
978 |
|
|
979 |
The function assumes that Close() has not been re-implemented.
|
|
980 |
|
|
981 |
@return The object's reference count value before the call to this function.
|
|
982 |
|
|
983 |
@pre Kernel must be unlocked.
|
|
984 |
@pre Call in a thread context.
|
|
985 |
@pre interrupts enabled
|
|
986 |
@pre Thread must be in a critical section
|
|
987 |
|
|
988 |
@see DObject::Close()
|
|
989 |
*/
|
|
990 |
EXPORT_C TInt DObject::AsyncClose()
|
|
991 |
{
|
|
992 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED | MASK_INTERRUPTS_ENABLED | MASK_NOT_ISR | MASK_NOT_IDFC | MASK_CRITICAL, "DObject::AsyncClose");
|
|
993 |
__KTRACE_OPT(KSERVER,Kern::Printf("DObject::AsyncClose() %O",this));
|
|
994 |
|
|
995 |
TInt r=Dec();
|
|
996 |
if (r==1)
|
|
997 |
AsyncDelete();
|
|
998 |
return r;
|
|
999 |
}
|
|
1000 |
|
|
1001 |
|
|
1002 |
/**
|
|
1003 |
Asynchronously delete a DBase-derived object.
|
|
1004 |
|
|
1005 |
@pre Kernel must be unlocked.
|
|
1006 |
@pre Call in a thread context.
|
|
1007 |
@pre interrupts enabled
|
|
1008 |
@pre Thread must be in a critical section
|
|
1009 |
*/
|
|
1010 |
EXPORT_C void DBase::AsyncDelete()
|
|
1011 |
{
|
|
1012 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED | MASK_INTERRUPTS_ENABLED | MASK_NOT_ISR | MASK_NOT_IDFC | MASK_CRITICAL, "DObject::AsyncDelete");
|
|
1013 |
__KTRACE_OPT(KSERVER,Kern::Printf("DBase::AsyncDelete() %08x",this));
|
|
1014 |
|
|
1015 |
DBase* oldHead = K::AsyncDeleteHead;
|
|
1016 |
do {
|
|
1017 |
iAsyncDeleteNext = oldHead;
|
|
1018 |
} while (!__e32_atomic_cas_rel_ptr(&K::AsyncDeleteHead, &oldHead, this));
|
|
1019 |
if (!oldHead)
|
|
1020 |
K::AsyncFreeDfc.Enque();
|
|
1021 |
}
|
|
1022 |
|
|
1023 |
|
|
1024 |
/** Frees a heap buffer asynchronously.
|
|
1025 |
|
|
1026 |
aPtr must point to a currently allocated cell on the kernel heap.
|
|
1027 |
|
|
1028 |
@param aPtr Pointer to the kernel heap cell to be freed.
|
|
1029 |
|
|
1030 |
@pre Calling thread must be in a critical section.
|
|
1031 |
@pre Kernel must be unlocked.
|
|
1032 |
@pre Call in a thread context.
|
|
1033 |
@pre interrupts enabled
|
|
1034 |
|
|
1035 |
@post Calling thread is in a critical section.
|
|
1036 |
*/
|
|
1037 |
EXPORT_C void Kern::AsyncFree(TAny* aPtr)
|
|
1038 |
//
|
|
1039 |
// Asynchronously free a kernel heap cell (must be >=4 bytes in length)
|
|
1040 |
//
|
|
1041 |
{
|
|
1042 |
CHECK_PRECONDITIONS(MASK_KERNEL_UNLOCKED | MASK_INTERRUPTS_ENABLED | MASK_NOT_ISR | MASK_NOT_IDFC | MASK_CRITICAL, "Kern::AsyncFree");
|
|
1043 |
__KTRACE_OPT(KSERVER,Kern::Printf("Kern::AsyncFree(%08x)",aPtr));
|
|
1044 |
DThread* pT=TheCurrentThread;
|
|
1045 |
|
|
1046 |
// if we are already running in the kernel server, just free the cell now
|
|
1047 |
if (pT==K::TheKernelThread)
|
|
1048 |
Kern::Free(aPtr);
|
|
1049 |
else
|
|
1050 |
{
|
|
1051 |
TAny* oldHead = K::AsyncFreeHead;
|
|
1052 |
do {
|
|
1053 |
*(TAny**)aPtr = oldHead; // use first word of cell as link field
|
|
1054 |
} while (!__e32_atomic_cas_rel_ptr(&K::AsyncFreeHead, &oldHead, aPtr));
|
|
1055 |
if (!oldHead)
|
|
1056 |
K::AsyncFreeDfc.Enque();
|
|
1057 |
}
|
|
1058 |
}
|
|
1059 |
|
|
1060 |
|
|
1061 |
/** Asynchronously notifies all change notifiers of a set of events.
|
|
1062 |
|
|
1063 |
This call queues a DFC executed by the supervisor thread. The DFC causes
|
|
1064 |
all DChangeNotifier objects to record the events indicated and to signal
|
|
1065 |
their owning thread if it is currently logged on.
|
|
1066 |
|
|
1067 |
@param aChanges The mask of events to be notified (TChanges enumeration).
|
|
1068 |
|
|
1069 |
@pre Call in a thread context.
|
|
1070 |
@pre Do not call from an ISR.
|
|
1071 |
@pre Calling thread must be in a critical section.
|
|
1072 |
@pre No fast mutex can be held.
|
|
1073 |
@pre Kernel must be unlocked.
|
|
1074 |
@pre interrupts enabled
|
|
1075 |
|
|
1076 |
@see Kern::NotifyChanges()
|
|
1077 |
@see TChanges
|
|
1078 |
*/
|
|
1079 |
EXPORT_C void Kern::AsyncNotifyChanges(TUint aChanges)
|
|
1080 |
{
|
|
1081 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::AsyncNotifyChanges");
|
|
1082 |
__KTRACE_OPT(KSERVER,Kern::Printf("Kern::AsyncNotifyChanges %08x",aChanges));
|
|
1083 |
TUint old = __e32_atomic_ior_ord32(&K::AsyncChanges, aChanges);
|
|
1084 |
if (old==0 && aChanges)
|
|
1085 |
K::AsyncChangeNotifierDfc.Enque();
|
|
1086 |
}
|
|
1087 |
|
|
1088 |
|
|
1089 |
/********************************************
|
|
1090 |
* Kernel server functions
|
|
1091 |
********************************************/
|
|
1092 |
void K::DoAsyncFree(TAny*)
|
|
1093 |
//
|
|
1094 |
// Free any kernel heap cells queued for asynchronous freeing
|
|
1095 |
// Delete any DBase-derived objects queued for asynchronous deletion
|
|
1096 |
//
|
|
1097 |
{
|
|
1098 |
__KTRACE_OPT(KSERVER,Kern::Printf("K::DoAsyncFree"));
|
|
1099 |
while (K::AsyncFreeHead || K::AsyncDeleteHead)
|
|
1100 |
{
|
|
1101 |
TAny* p = __e32_atomic_swp_acq_ptr(&K::AsyncFreeHead, 0);
|
|
1102 |
DBase* pD = (DBase*)__e32_atomic_swp_acq_ptr(&K::AsyncDeleteHead, 0);
|
|
1103 |
while (p)
|
|
1104 |
{
|
|
1105 |
TAny* next = *(TAny**)p;
|
|
1106 |
__KTRACE_OPT(KSERVER,Kern::Printf("AsyncFree %08x",p));
|
|
1107 |
Kern::Free(p);
|
|
1108 |
p = next;
|
|
1109 |
}
|
|
1110 |
if (pD)
|
|
1111 |
{
|
|
1112 |
NKern::LockSystem();
|
|
1113 |
NKern::UnlockSystem();
|
|
1114 |
do {
|
|
1115 |
DBase* next = pD->iAsyncDeleteNext;
|
|
1116 |
__KTRACE_OPT(KSERVER,Kern::Printf("AsyncDelete %08x",pD));
|
|
1117 |
DBase::Delete(pD);
|
|
1118 |
pD = next;
|
|
1119 |
} while(pD);
|
|
1120 |
}
|
|
1121 |
}
|
|
1122 |
}
|
|
1123 |
|
|
1124 |
void K::DoAsyncNotify(TAny*)
|
|
1125 |
//
|
|
1126 |
// Asynchronously signal change notifiers
|
|
1127 |
//
|
|
1128 |
{
|
|
1129 |
TUint changes = __e32_atomic_swp_rel32(&K::AsyncChanges, 0);
|
|
1130 |
__KTRACE_OPT(KSERVER,Kern::Printf("K::DoAsyncNotify %08x",changes));
|
|
1131 |
if (changes)
|
|
1132 |
Kern::NotifyChanges(changes);
|
|
1133 |
}
|
|
1134 |
|
|
1135 |
/********************************************
|
|
1136 |
* Kernel server entry point
|
|
1137 |
********************************************/
|
|
1138 |
void K::StartKernelServer()
|
|
1139 |
{
|
|
1140 |
__KTRACE_OPT(KSERVER,Kern::Printf("K::StartKernelServer"));
|
|
1141 |
Kern::SetThreadPriority(KKernelServerDefaultPriority);
|
|
1142 |
K::SvBarrierQ.Receive();
|
|
1143 |
__KTRACE_OPT(KSERVER,Kern::Printf("Server waiting"));
|
|
1144 |
TDfcQue::ThreadFunction(K::SvMsgQ);
|
|
1145 |
}
|
|
1146 |
|
|
1147 |
void K::DoSvBarrier(TAny* a)
|
|
1148 |
{
|
|
1149 |
TMessageQue& q = *(TMessageQue*)a;
|
|
1150 |
q.iMessage->Complete(KErrNone, ETrue);
|
|
1151 |
}
|
|
1152 |
|