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\sipc.cpp
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
#include <kernel/kern_priv.h>
|
|
19 |
#include "execs.h"
|
|
20 |
#include "memmodel.h"
|
|
21 |
|
|
22 |
#define iMState iWaitLink.iSpare1
|
|
23 |
|
|
24 |
const TInt KMaxMsgLimit=512;
|
|
25 |
|
|
26 |
extern "C" void __SendDiscMsg(DSession* aSession);
|
|
27 |
|
|
28 |
/********************************************
|
|
29 |
* User side message formats
|
|
30 |
********************************************/
|
|
31 |
|
|
32 |
// User side message version
|
|
33 |
// This must match the layout of class RMessage2
|
|
34 |
class RMessageU2
|
|
35 |
{
|
|
36 |
public:
|
|
37 |
inline RMessageU2(const RMessageK& a);
|
|
38 |
public:
|
|
39 |
// from RMessagePtr2
|
|
40 |
TInt iHandle;
|
|
41 |
// from RMessage2
|
|
42 |
TInt iFunction;
|
|
43 |
TInt iArgs[KMaxMessageArguments];
|
|
44 |
TUint32 iSpare1;
|
|
45 |
const TAny* iSessionPtr;
|
|
46 |
#if 0
|
|
47 |
// not used or copied by IPC, so not required ...
|
|
48 |
mutable TInt iFlags;
|
|
49 |
TInt iSpare3;
|
|
50 |
#endif
|
|
51 |
};
|
|
52 |
|
|
53 |
inline RMessageU2::RMessageU2(const RMessageK& a)
|
|
54 |
{
|
|
55 |
iHandle = (TInt)&a;
|
|
56 |
iFunction = a.iFunction;
|
|
57 |
if (iFunction == RMessage2::EDisConnect)
|
|
58 |
{
|
|
59 |
iArgs[0] = 0;
|
|
60 |
iArgs[1] = 0;
|
|
61 |
iArgs[2] = 0;
|
|
62 |
iArgs[3] = 0;
|
|
63 |
}
|
|
64 |
else
|
|
65 |
{
|
|
66 |
iArgs[0] = a.Arg(0);
|
|
67 |
iArgs[1] = a.Arg(1);
|
|
68 |
iArgs[2] = a.Arg(2);
|
|
69 |
iArgs[3] = a.Arg(3);
|
|
70 |
}
|
|
71 |
iSpare1 = 0;
|
|
72 |
iSessionPtr = a.iSession->iSessionCookie;
|
|
73 |
#ifdef KIPC
|
|
74 |
if (KDebugNum(KIPC))
|
|
75 |
Kern::Printf("RMessageU2(%08X): %08X %08X; %08X->%08X",
|
|
76 |
&a, iHandle, iFunction, a.iSession, iSessionPtr);
|
|
77 |
#endif //KIPC
|
|
78 |
}
|
|
79 |
|
|
80 |
TServerMessage::TServerMessage() :
|
|
81 |
TClientRequest(CallbackFunc)
|
|
82 |
{
|
|
83 |
}
|
|
84 |
|
|
85 |
#ifndef __CLIENT_REQUEST_MACHINE_CODED__
|
|
86 |
|
|
87 |
void TServerMessage::CallbackFunc(TAny* aData, TUserModeCallbackReason aReason)
|
|
88 |
{
|
|
89 |
TServerMessage* req = (TServerMessage*)aData;
|
|
90 |
if (aReason == EUserModeCallbackRun && req->iResult == KErrNone)
|
|
91 |
{
|
|
92 |
RMessageU2 userData(*req->iMessageData);
|
|
93 |
K::USafeWrite(req->iMessagePtr, &userData, sizeof(userData));
|
|
94 |
}
|
|
95 |
TClientRequest::CallbackFunc(aData, aReason);
|
|
96 |
}
|
|
97 |
|
|
98 |
#endif
|
|
99 |
|
|
100 |
RMessageK::RMessageK() :
|
|
101 |
TClientRequest(CallbackFunc),
|
|
102 |
iAccessCount(0)
|
|
103 |
{
|
|
104 |
}
|
|
105 |
|
|
106 |
void RMessageK::CallbackFunc(TAny* aData, TUserModeCallbackReason aReason)
|
|
107 |
{
|
|
108 |
RMessageK* msg = (RMessageK*)aData;
|
|
109 |
|
|
110 |
TBool ok = ETrue;
|
|
111 |
if (aReason == EUserModeCallbackRun)
|
|
112 |
{
|
|
113 |
// Write back updated descriptor lengths
|
|
114 |
TInt flags = msg->iMsgArgs.AllDesWritten();
|
|
115 |
for (TInt i = 0 ; flags != 0 ; ++i, flags >>= 1)
|
|
116 |
{
|
|
117 |
if (flags & 1)
|
|
118 |
{
|
|
119 |
const TDesHeader& des = msg->Descriptor(i);
|
|
120 |
TAny* exc = K::USafeWrite(msg->Ptr(i), &des.TypeAndLength(), sizeof(TUint32));
|
|
121 |
if (exc != NULL)
|
|
122 |
ok = EFalse;
|
|
123 |
if (ok && des.Type() == EBufCPtr)
|
|
124 |
{
|
|
125 |
TInt len = des.Length();
|
|
126 |
TUint8* pL = (TUint8*)(des.DataPtr() - sizeof(TDesC));
|
|
127 |
exc = K::USafeWrite(pL, &len, sizeof(len));
|
|
128 |
if (exc != NULL)
|
|
129 |
ok = EFalse;
|
|
130 |
}
|
|
131 |
}
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
TClientRequest::CallbackFunc(aData, aReason);
|
|
136 |
|
|
137 |
NKern::LockSystem();
|
|
138 |
__NK_ASSERT_DEBUG(msg->IsCompleting());
|
|
139 |
msg->CloseRef();
|
|
140 |
NKern::UnlockSystem();
|
|
141 |
|
|
142 |
if (!ok)
|
|
143 |
{
|
|
144 |
NKern::ThreadLeaveCS();
|
|
145 |
K::PanicKernExec(EBadIpcDescriptor);
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
void RMessageK::OpenRef()
|
|
150 |
{
|
|
151 |
// iAccess count will probably never be more than 2?
|
|
152 |
__NK_ASSERT_DEBUG(iAccessCount < 0xfe);
|
|
153 |
__e32_atomic_add_ord8(&iAccessCount, 1);
|
|
154 |
}
|
|
155 |
|
|
156 |
void RMessageK::CloseRef()
|
|
157 |
{
|
|
158 |
__ASSERT_SYSTEM_LOCK;
|
|
159 |
if (__e32_atomic_add_ord8(&iAccessCount, (TUint8)-1) == 1)
|
|
160 |
Free();
|
|
161 |
}
|
|
162 |
|
|
163 |
#if 0
|
|
164 |
// Just to exercise the chunk-adjustment code and check that we
|
|
165 |
// manage the system lock and thread critical sections correctly.
|
|
166 |
// Enter and return with system unlocked and caller in a critical section
|
|
167 |
static void JiggleMsgChunk()
|
|
168 |
{
|
|
169 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL, "RMessageK::JiggleMsgChunk");
|
|
170 |
DChunk* msgChunk = K::MsgInfo.iChunk;
|
|
171 |
if (msgChunk)
|
|
172 |
{
|
|
173 |
__ASSERT_DEBUG((TUint)msgChunk->Size() == K::MsgInfo.iCurrSize, K::Fault(K::EMsgFreeBadPool));
|
|
174 |
msgChunk->Adjust(K::MsgInfo.iCurrSize+Kern::RoundToPageSize(1));
|
|
175 |
msgChunk->Adjust(K::MsgInfo.iCurrSize);
|
|
176 |
__ASSERT_DEBUG((TUint)msgChunk->Size() == K::MsgInfo.iCurrSize, K::Fault(K::EMsgFreeBadPool));
|
|
177 |
}
|
|
178 |
}
|
|
179 |
|
|
180 |
// We must drop the system lock before calling JiggleMsgChunk(),
|
|
181 |
// but as our caller may be holding resources we have to enter a
|
|
182 |
// critical section before doing so. Note that K::ThreadEnterCS()
|
|
183 |
// (as opposed to NKern::ThreadEnterCS()) both enters a critical
|
|
184 |
// section AND then releases the lock; likewise K::ThreadLeaveCS()
|
|
185 |
// reclaims the system lock before leaving the critical section,
|
|
186 |
// so there is no window for the thread to be killed while holding
|
|
187 |
// allocated resources ...
|
|
188 |
K::ThreadEnterCS();
|
|
189 |
JiggleMsgChunk();
|
|
190 |
K::ThreadLeaveCS();
|
|
191 |
#endif
|
|
192 |
|
|
193 |
#ifdef _DEBUG
|
|
194 |
// Check consistency of the specified list
|
|
195 |
// Enter and return with system locked
|
|
196 |
static TBool MessagePoolIntact(RMessageK* p = K::MsgInfo.iNextMessage, TInt count = K::MsgInfo.iFreeMessageCount)
|
|
197 |
{
|
|
198 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED, "DSession::MessagePoolIntact");
|
|
199 |
TInt k = 0;
|
|
200 |
while (p)
|
|
201 |
{
|
|
202 |
k += 1;
|
|
203 |
p = (RMessageK*)p->iSessionLink.iNext;
|
|
204 |
}
|
|
205 |
|
|
206 |
return (k == count);
|
|
207 |
}
|
|
208 |
|
|
209 |
// Ditto, but enter and return with system unlocked
|
|
210 |
static TBool MessagePoolIntactUnlocked()
|
|
211 |
{
|
|
212 |
NKern::LockSystem();
|
|
213 |
TInt r = MessagePoolIntact();
|
|
214 |
NKern::UnlockSystem();
|
|
215 |
return r;
|
|
216 |
}
|
|
217 |
#endif
|
|
218 |
|
|
219 |
#ifndef __MESSAGE_MACHINE_CODED_2__
|
|
220 |
EXPORT_C RMessageK* RMessageK::MessageK(TInt aMsgHandle)
|
|
221 |
//
|
|
222 |
// Validates a message handle and converts it into a pointer.
|
|
223 |
// Enter and leave with system locked.
|
|
224 |
//
|
|
225 |
{
|
|
226 |
RMessageK* m = MessageK(aMsgHandle, TheCurrentThread);
|
|
227 |
if (m == NULL || m->iFunction == RMessage2::EDisConnect)
|
|
228 |
K::PanicCurrentThread(EBadMessageHandle);
|
|
229 |
return m;
|
|
230 |
}
|
|
231 |
|
|
232 |
RMessageK* RMessageK::MessageK(TInt aMsgHandle, DThread* aThread)
|
|
233 |
//
|
|
234 |
// Validates a message handle and converts it into a pointer.
|
|
235 |
// Enter and leave with system locked.
|
|
236 |
//
|
|
237 |
{
|
|
238 |
RMessageK& m = *(RMessageK*)aMsgHandle;
|
|
239 |
SDblQueLink lnk;
|
|
240 |
|
|
241 |
// Handle must point into kernel message chunk, and be correctly aligned
|
|
242 |
TInt offset = (TUint8*)&m - K::MsgInfo.iBase;
|
|
243 |
if (offset < 0 || offset+sizeof(RMessageK) > (TUint)K::MsgInfo.iMaxSize)
|
|
244 |
return NULL;
|
|
245 |
if (offset & (KMessageSize-1))
|
|
246 |
return NULL;
|
|
247 |
|
|
248 |
// Message must be readable, in the correct state and owned by the right process
|
|
249 |
if (Kern::SafeRead(&m.iServerLink, &lnk, sizeof(lnk)) != NULL
|
|
250 |
|| TUint32(lnk.iNext) != ~TUint32(&m)
|
|
251 |
|| TUint32(lnk.iPrev) != ~TUint32(aThread->iOwningProcess))
|
|
252 |
return NULL;
|
|
253 |
|
|
254 |
return &m;
|
|
255 |
}
|
|
256 |
#endif
|
|
257 |
|
|
258 |
// Claim one page of memory, carve it up into message blocks,
|
|
259 |
// and add them to the global pool
|
|
260 |
// Enter and return with system lock, may be temporarily released though
|
|
261 |
TInt RMessageK::ExpandMessagePool()
|
|
262 |
{
|
|
263 |
// KMessageSize must have been defined as a power of two at least as big as an RMessageK
|
|
264 |
__ASSERT_COMPILE((KMessageSize & (KMessageSize-1)) == 0);
|
|
265 |
__ASSERT_COMPILE(KMessageSize >= sizeof(RMessageK));
|
|
266 |
|
|
267 |
// This function can be called during the creation of the very first thread, which is
|
|
268 |
// before the msg chunk has been created (the first thread will own the chunk, so must
|
|
269 |
// be created first). In this situation we cannot expand the pool, and so just return.
|
|
270 |
// This first thread is thus unique in not having a preallocated message for synchronous
|
|
271 |
// use; but it's destined to become the idle thread and as such never makes use of IPC.
|
|
272 |
// So that's alright then :)
|
|
273 |
DChunk* msgChunk = K::MsgInfo.iChunk;
|
|
274 |
if (!msgChunk)
|
|
275 |
return KErrNotReady;
|
|
276 |
|
|
277 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED, "DSession::ExpandMessagePool");
|
|
278 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool));
|
|
279 |
|
|
280 |
RMessageK* first = NULL;
|
|
281 |
RMessageK* m = NULL;
|
|
282 |
TInt r = KErrNone;
|
|
283 |
TInt k = 0;
|
|
284 |
|
|
285 |
// We must drop the system lock before calling DChunk::Adjust().
|
|
286 |
// This DMutex ensures single-threading while adjusting the pool.
|
|
287 |
// Note that the system lock may be dropped and reacquired here ...
|
|
288 |
TUint oldSize = K::MsgInfo.iCurrSize;
|
|
289 |
NKern::ThreadEnterCS();
|
|
290 |
K::MsgInfo.iMsgChunkLock->Wait();
|
|
291 |
NKern::UnlockSystem();
|
|
292 |
|
|
293 |
// Someone may have got in first while we were waiting for the DMutex.
|
|
294 |
// So we only expand the chunk if its size is unchanged from above;
|
|
295 |
// otherwise we just do nothing and let the caller retry if necessary
|
|
296 |
if (K::MsgInfo.iCurrSize == oldSize)
|
|
297 |
{
|
|
298 |
__ASSERT_DEBUG((TUint)msgChunk->Size() == K::MsgInfo.iCurrSize, K::Fault(K::EMsgFreeBadPool));
|
|
299 |
__KTRACE_OPT(KSERVER,Kern::Printf("ExpandMessagePool(): MsgChunk %08X, base %08X, size %08X, max %08X",
|
|
300 |
msgChunk, K::MsgInfo.iBase, K::MsgInfo.iCurrSize, K::MsgInfo.iMaxSize));
|
|
301 |
|
|
302 |
r = msgChunk->Adjust(K::MsgInfo.iCurrSize+Kern::RoundToPageSize(KMessageSize));
|
|
303 |
__KTRACE_OPT(KSERVER,Kern::Printf("ExpandMessagePool(): Adjust returns %d, size now %08X", r, msgChunk->Size()));
|
|
304 |
|
|
305 |
if (r == KErrNone)
|
|
306 |
{
|
|
307 |
// Turn the allocated memory info a list of messages
|
|
308 |
// This can (and should) be done without the system lock
|
|
309 |
TUint8* p = K::MsgInfo.iBase + K::MsgInfo.iCurrSize;
|
|
310 |
K::MsgInfo.iCurrSize = (TUint)msgChunk->Size();
|
|
311 |
TUint8* top = K::MsgInfo.iBase + K::MsgInfo.iCurrSize;
|
|
312 |
__ASSERT_DEBUG(p < top, K::Fault(K::EMsgFreeBadPool));
|
|
313 |
memclr(p, top-p);
|
|
314 |
|
|
315 |
for (first = (RMessageK*)p, k = 0; p < top; ++k)
|
|
316 |
{
|
|
317 |
m = (RMessageK*)p;
|
|
318 |
p += KMessageSize;
|
|
319 |
new (m) RMessageK;
|
|
320 |
m->iMsgType = EGlobal;
|
|
321 |
m->iSessionLink.iNext = (SDblQueLink*)p;
|
|
322 |
}
|
|
323 |
|
|
324 |
__ASSERT_DEBUG(p == top, K::Fault(K::EMsgFreeBadPool));
|
|
325 |
__ASSERT_DEBUG((TAny*)(m+1) <= top, K::Fault(K::EMsgFreeBadPool));
|
|
326 |
}
|
|
327 |
}
|
|
328 |
|
|
329 |
// Reacquire system lock before adding the new messages to the pool
|
|
330 |
// Note that K::MsgInfo.iNextMessage may no longer be NULL
|
|
331 |
NKern::LockSystem();
|
|
332 |
|
|
333 |
if (first)
|
|
334 |
{
|
|
335 |
// We expanded the pool; m is the last allocated message
|
|
336 |
m->iSessionLink.iNext = (SDblQueLink*)K::MsgInfo.iNextMessage;
|
|
337 |
K::MsgInfo.iNextMessage = first;
|
|
338 |
K::MsgInfo.iFreeMessageCount += k;
|
|
339 |
}
|
|
340 |
|
|
341 |
// Now we can release the pool mutex; unfortunately this also releases
|
|
342 |
// the system lock, so we have to acquire it again before returning.
|
|
343 |
// This makes it possible -- though very unlikely -- that after adding
|
|
344 |
// messages to the pool, they *all* get claimed by other threads in the
|
|
345 |
// window between dropping the system lock and reacquiring it, so that
|
|
346 |
// our caller finds the pool still empty. In such cases, it's up to
|
|
347 |
// the caller to retry ...
|
|
348 |
K::MsgInfo.iMsgChunkLock->Signal();
|
|
349 |
NKern::LockSystem();
|
|
350 |
NKern::ThreadLeaveCS();
|
|
351 |
return r;
|
|
352 |
}
|
|
353 |
|
|
354 |
// Claim a chain of aCount RMessageK objects from the global pool,
|
|
355 |
// and set their type to aType (e.g. ESession or ESync)
|
|
356 |
// Enter and return with system unlocked and caller in a critical section
|
|
357 |
RMessageK* RMessageK::ClaimMessagePool(enum TMsgType aType, TInt aCount, DSession *aSession)
|
|
358 |
{
|
|
359 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL, "RMessageK::ClaimMessagePool");
|
|
360 |
|
|
361 |
RMessageK* chain = NULL;
|
|
362 |
TInt wanted = aCount;
|
|
363 |
|
|
364 |
// In order not to hold the system lock for too long at one time,
|
|
365 |
// we release it between iterations of this loop.
|
|
366 |
#define KMaxMessagesInOneGo 16
|
|
367 |
for (NKern::LockSystem(); ; NKern::FlashSystem())
|
|
368 |
{
|
|
369 |
TInt n = Min(wanted, KMaxMessagesInOneGo);
|
|
370 |
while (K::MsgInfo.iFreeMessageCount < n)
|
|
371 |
if (ExpandMessagePool() != KErrNone)
|
|
372 |
goto fail;
|
|
373 |
|
|
374 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool));
|
|
375 |
RMessageK* first = K::MsgInfo.iNextMessage;
|
|
376 |
RMessageK* last = first;
|
|
377 |
RMessageK* p = first;
|
|
378 |
|
|
379 |
// Count out n message blocks
|
|
380 |
__ASSERT_DEBUG(K::MsgInfo.iFreeMessageCount >= n, K::Fault(K::EMsgFreeBadPool));
|
|
381 |
for (TInt k = 0; k < n; ++k)
|
|
382 |
{
|
|
383 |
last = p;
|
|
384 |
p->iSession = aSession;
|
|
385 |
p->iMsgType = (TUint8)aType;
|
|
386 |
p = (RMessageK*)p->iSessionLink.iNext;
|
|
387 |
}
|
|
388 |
|
|
389 |
// Add them to the chain we're building
|
|
390 |
if (chain)
|
|
391 |
last->iSessionLink.iNext = (SDblQueLink*)chain;
|
|
392 |
else
|
|
393 |
last->iSessionLink.iNext = NULL;
|
|
394 |
chain = first;
|
|
395 |
|
|
396 |
// Synchronise the global pool
|
|
397 |
K::MsgInfo.iNextMessage = p;
|
|
398 |
K::MsgInfo.iFreeMessageCount -= n;
|
|
399 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool));
|
|
400 |
if ((wanted -= n) == 0)
|
|
401 |
break;
|
|
402 |
}
|
|
403 |
|
|
404 |
__ASSERT_DEBUG(MessagePoolIntact(chain, aCount), K::Fault(K::EMsgFreeBadPool));
|
|
405 |
NKern::UnlockSystem();
|
|
406 |
|
|
407 |
// Return this chain of message blocks
|
|
408 |
return chain;
|
|
409 |
|
|
410 |
fail:
|
|
411 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool));
|
|
412 |
NKern::UnlockSystem();
|
|
413 |
if (chain)
|
|
414 |
chain->ReleaseMessagePool(aType, KMaxTInt);
|
|
415 |
return NULL;
|
|
416 |
}
|
|
417 |
|
|
418 |
// Release a chain of at most aMax RMessageK objects back to the
|
|
419 |
// global pool, resetting their type to EGlobal in the process
|
|
420 |
// Enter and return with system unlocked and caller in a critical section
|
|
421 |
void RMessageK::ReleaseMessagePool(enum TMsgType aType, TInt aMax)
|
|
422 |
{
|
|
423 |
CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL, "RMessageK::ReleaseMessagePool");
|
|
424 |
__ASSERT_DEBUG(MessagePoolIntactUnlocked(), K::Fault(K::EMsgFreeBadPool));
|
|
425 |
|
|
426 |
RMessageK* next = this;
|
|
427 |
RMessageK* p;
|
|
428 |
TInt k = 0;
|
|
429 |
|
|
430 |
do
|
|
431 |
{
|
|
432 |
p = next;
|
|
433 |
next = (RMessageK*)p->iSessionLink.iNext;
|
|
434 |
__ASSERT_DEBUG(p->iMsgType == aType, K::Fault(K::EMsgFreeBadPool));
|
|
435 |
p->iMsgType = EGlobal;
|
|
436 |
}
|
|
437 |
while (++k < aMax && next != NULL);
|
|
438 |
|
|
439 |
// The linked list may not be NULL-terminated -- in DEBUG builds, the link
|
|
440 |
// may be deliberately set to an invalid value. Here, we assert that if
|
|
441 |
// exit was reached because the count reached zero, rather than because we
|
|
442 |
// reached a NULL link, then the next link was in fact just such a value.
|
|
443 |
#ifndef KILL_LINK_VALUE
|
|
444 |
#define KILL_LINK_VALUE ((SDblQueLink*)0xdfdfdfdf)
|
|
445 |
#endif
|
|
446 |
__ASSERT_DEBUG((next == NULL || (k == aMax && next == (TAny*)KILL_LINK_VALUE)), K::Fault(K::EMsgFreeBadPool));
|
|
447 |
__ASSERT_DEBUG((k == aMax || aMax == KMaxTInt), K::Fault(K::EMsgFreeBadPool));
|
|
448 |
|
|
449 |
// At the end of the chain
|
|
450 |
NKern::LockSystem();
|
|
451 |
p->iSessionLink.iNext = (SDblQueLink*)K::MsgInfo.iNextMessage;
|
|
452 |
K::MsgInfo.iNextMessage = this;
|
|
453 |
K::MsgInfo.iFreeMessageCount += k;
|
|
454 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool));
|
|
455 |
NKern::UnlockSystem();
|
|
456 |
(void)aType; // Suppress compiler whinge in non-DEBUG build
|
|
457 |
}
|
|
458 |
|
|
459 |
// Get a free message from the global pool. Expand the global pool if
|
|
460 |
// it's empty; but return NULL if that fails to add more messages.
|
|
461 |
//
|
|
462 |
// Enter and return with system lock, may be temporarily released though
|
|
463 |
RMessageK* RMessageK::GetNextFreeMessage(DSession* aSession)
|
|
464 |
{
|
|
465 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED, "RMessageK::GetNextFreeMessage");
|
|
466 |
|
|
467 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool));
|
|
468 |
RMessageK* volatile* p = &K::MsgInfo.iNextMessage;
|
|
469 |
RMessageK* m;
|
|
470 |
|
|
471 |
// If there are no free messages, expand the global pool. Keep trying
|
|
472 |
// until we successfully get a message or fail with an error.
|
|
473 |
while ((m = *p) == NULL)
|
|
474 |
if (ExpandMessagePool() != KErrNone)
|
|
475 |
return NULL;
|
|
476 |
|
|
477 |
*p = (RMessageK*)m->iSessionLink.iNext;
|
|
478 |
m->iSessionLink.iNext = NULL;
|
|
479 |
m->iSession = aSession;
|
|
480 |
K::MsgInfo.iFreeMessageCount -= 1;
|
|
481 |
|
|
482 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool));
|
|
483 |
__KTRACE_OPT(KSERVER,Kern::Printf("RMessageK::GetNextFreeMessage returns %08x", m));
|
|
484 |
return m;
|
|
485 |
}
|
|
486 |
|
|
487 |
// Enter and return with system unlocked.
|
|
488 |
void RMessageK::TMsgArgs::ReadDesHeaders(const TInt aArgsPtr[KMaxMessageArguments+1])
|
|
489 |
{
|
|
490 |
// Copy the args into the structure; the extra word contains the IPC flags
|
|
491 |
TInt i;
|
|
492 |
for (i = 0; i < KMaxMessageArguments; ++i)
|
|
493 |
iArgs[i] = *aArgsPtr++;
|
|
494 |
iArgFlags = *aArgsPtr & KAllIpcFlagsMask;
|
|
495 |
|
|
496 |
TInt descFlags = AllDescriptorFlags();
|
|
497 |
for (i = 0; descFlags != 0; ++i, descFlags >>= TIpcArgs::KBitsPerType)
|
|
498 |
if (descFlags & TIpcArgs::EFlagDes)
|
|
499 |
{
|
|
500 |
// Read descriptor header in current process. May take page fault
|
|
501 |
TInt r = K::USafeReadAndParseDesHeader((TAny*)iArgs[i], iDesInfo[i]);
|
|
502 |
if (r != KErrNone)
|
|
503 |
{
|
|
504 |
// If the descriptor is bad (unreadable or unknown type), clear
|
|
505 |
// the parameter type and mark the descriptor header as invalid
|
|
506 |
SetArgUndefined(i);
|
|
507 |
iDesInfo[i].Unset();
|
|
508 |
}
|
|
509 |
}
|
|
510 |
}
|
|
511 |
|
|
512 |
// Enter and return with system unlocked and caller in a critical section
|
|
513 |
void UnpinMessageArguments(RMessageK::TPinArray *aPinArray)
|
|
514 |
{
|
|
515 |
for (TInt i = 0; i < KMaxMessageArguments; ++i)
|
|
516 |
{
|
|
517 |
if (aPinArray->iPinPtrs[i])
|
|
518 |
{
|
|
519 |
// This will unpin the pinned memory and set iPinPtrs[i] to NULL.
|
|
520 |
M::DestroyVirtualPinObject(aPinArray->iPinPtrs[i]);
|
|
521 |
}
|
|
522 |
__NK_ASSERT_DEBUG(!aPinArray->iPinPtrs[i]);
|
|
523 |
}
|
|
524 |
}
|
|
525 |
|
|
526 |
// Enter and return with system lock, may be temporarily released though
|
|
527 |
TInt RMessageK::PinDescriptors(DSession* aSession, TBool aPinningServer)
|
|
528 |
{
|
|
529 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED, "RMessageK::PinDescriptors");
|
|
530 |
__NK_ASSERT_DEBUG(!iPinArray); // Free messages should always have this nulled.
|
|
531 |
|
|
532 |
// If none of the args are descriptors, there's obviously nothing to do
|
|
533 |
TUint descFlags = iMsgArgs.AllDescriptorFlags();
|
|
534 |
if (!descFlags)
|
|
535 |
return KErrNone;
|
|
536 |
|
|
537 |
// If neither the server nor the client have requested pinning, there's nothing to do
|
|
538 |
TUint argPinFlags = aPinningServer ? ~0u : iMsgArgs.AllPinFlags();
|
|
539 |
if (!argPinFlags)
|
|
540 |
return KErrNone;
|
|
541 |
|
|
542 |
// The session can only ever belong to one server so ok to release system lock
|
|
543 |
// during pinning as need to pin won't change unless the session is closed.
|
|
544 |
// Need a reference on the session, though, in case it goes away.
|
|
545 |
aSession->TotalAccessInc();
|
|
546 |
NKern::ThreadEnterCS();
|
|
547 |
NKern::UnlockSystem();
|
|
548 |
TInt r = KErrNone;
|
|
549 |
|
|
550 |
for (TInt i = 0; descFlags != 0; ++i, argPinFlags >>= 1, descFlags >>= TIpcArgs::KBitsPerType)
|
|
551 |
{
|
|
552 |
__NK_ASSERT_DEBUG(i < KMaxMessageArguments); // Should stop after max args processed.
|
|
553 |
|
|
554 |
// Is this arg a descriptor that should be pinned?
|
|
555 |
if ((descFlags & TIpcArgs::EFlagDes) && (argPinFlags & 1))
|
|
556 |
{
|
|
557 |
TDesHeader& desInfo = Descriptor(i);
|
|
558 |
|
|
559 |
// Pin the max length for modifiable descriptors, but only
|
|
560 |
// the current length for non-modifiable descriptors.
|
|
561 |
TUint pinLength = desInfo.IsWriteable() ? desInfo.MaxLength() : desInfo.Length();
|
|
562 |
if (pinLength)
|
|
563 |
{
|
|
564 |
if (!iPinArray)
|
|
565 |
{
|
|
566 |
iPinArray = new TPinArray;
|
|
567 |
if (!iPinArray)
|
|
568 |
{
|
|
569 |
r = KErrNoMemory;
|
|
570 |
break;
|
|
571 |
}
|
|
572 |
}
|
|
573 |
|
|
574 |
// This will only create and pin if the descriptor data is paged.
|
|
575 |
// An out-of-memory error here means we fail the whole operation.
|
|
576 |
r = Kern::CreateAndPinVirtualMemory(iPinArray->iPinPtrs[i], desInfo.DataPtr(), pinLength);
|
|
577 |
if (r == KErrNoMemory)
|
|
578 |
break;
|
|
579 |
if (r != KErrNone)
|
|
580 |
{
|
|
581 |
// For any other error, clear the parameter type and mark the
|
|
582 |
// descriptor header as invalid, so the server will see a problem
|
|
583 |
// on access, but then suppress the error so we can continue ...
|
|
584 |
iMsgArgs.SetArgUndefined(i);
|
|
585 |
desInfo.Unset();
|
|
586 |
r = KErrNone;
|
|
587 |
}
|
|
588 |
}
|
|
589 |
}
|
|
590 |
}
|
|
591 |
|
|
592 |
if (r == KErrNoMemory)
|
|
593 |
{
|
|
594 |
// Failed to pin everything so clean up any pin objects created.
|
|
595 |
// This will also unpin any pinned memory.
|
|
596 |
if (iPinArray)
|
|
597 |
{
|
|
598 |
UnpinMessageArguments(iPinArray);
|
|
599 |
delete iPinArray;
|
|
600 |
iPinArray = NULL;
|
|
601 |
}
|
|
602 |
}
|
|
603 |
|
|
604 |
NKern::LockSystem();
|
|
605 |
|
|
606 |
// Remove the access on the session.
|
|
607 |
if (aSession->TotalAccessDec() == DObject::EObjectDeleted)
|
|
608 |
{// This was the last access on the session and it has been deleted so
|
|
609 |
// don't access any of its members.
|
|
610 |
r = KErrDisconnected;
|
|
611 |
}
|
|
612 |
NKern::ThreadLeaveCS();
|
|
613 |
return r;
|
|
614 |
}
|
|
615 |
|
|
616 |
// Free a single message back to the pool it belongs in
|
|
617 |
// Enter and leave with system lock, may be temporarily released though
|
|
618 |
void RMessageK::Free()
|
|
619 |
{
|
|
620 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED, "RMessageK::Free");
|
|
621 |
__KTRACE_OPT(KSERVER, Kern::Printf("RMessageK(%08x)::Free(), type %d", this, iMsgType));
|
|
622 |
__ASSERT_SYSTEM_LOCK;
|
|
623 |
__NK_ASSERT_DEBUG(!IsFree());
|
|
624 |
__NK_ASSERT_DEBUG(State()==EFree); // TClientRequest should be free
|
|
625 |
__NK_ASSERT_DEBUG(iAccessCount==0);
|
|
626 |
|
|
627 |
DThread* closeThread = 0;
|
|
628 |
if (iSession)
|
|
629 |
{
|
|
630 |
--iSession->iMsgCount;
|
|
631 |
iSessionLink.Deque();
|
|
632 |
if (--iClient->iIpcCount == 0x80000000u)
|
|
633 |
closeThread = iClient;
|
|
634 |
}
|
|
635 |
|
|
636 |
// take ownership of any pin objects...
|
|
637 |
TPinArray* pinArray = iPinArray;
|
|
638 |
iPinArray = NULL;
|
|
639 |
|
|
640 |
// free the message...
|
|
641 |
SetFree();
|
|
642 |
switch (iMsgType)
|
|
643 |
{
|
|
644 |
default:
|
|
645 |
case EDisc:
|
|
646 |
// The Disconnect message is owned by the session and should never be freed
|
|
647 |
K::Fault(K::EMsgFreeBadPool);
|
|
648 |
break;
|
|
649 |
|
|
650 |
case ESync:
|
|
651 |
// The Synchronous message is owned by the thread, so we don't actually free it
|
|
652 |
break;
|
|
653 |
|
|
654 |
case ESession:
|
|
655 |
if (iSession)
|
|
656 |
{
|
|
657 |
// Put this message back on the session's freelist
|
|
658 |
iSessionLink.iNext = (SDblQueLink*)iSession->iNextFreeMessage;
|
|
659 |
iSession->iNextFreeMessage = this;
|
|
660 |
break;
|
|
661 |
}
|
|
662 |
// Session has gone away; return message to global pool instead
|
|
663 |
iMsgType = EGlobal;
|
|
664 |
/*FALLTHRU*/
|
|
665 |
case EGlobal:
|
|
666 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool));
|
|
667 |
// Put this message back on the global freelist
|
|
668 |
iSessionLink.iNext = (SDblQueLink*)K::MsgInfo.iNextMessage;
|
|
669 |
K::MsgInfo.iNextMessage = this;
|
|
670 |
K::MsgInfo.iFreeMessageCount += 1;
|
|
671 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool));
|
|
672 |
break;
|
|
673 |
}
|
|
674 |
|
|
675 |
if (closeThread)
|
|
676 |
{
|
|
677 |
K::ThreadEnterCS();
|
|
678 |
closeThread->AsyncClose();
|
|
679 |
K::ThreadLeaveCS();
|
|
680 |
}
|
|
681 |
|
|
682 |
if (pinArray)
|
|
683 |
{
|
|
684 |
// unpin any pinned descriptors and free the pin objects...
|
|
685 |
K::ThreadEnterCS();
|
|
686 |
UnpinMessageArguments(pinArray);
|
|
687 |
delete pinArray;
|
|
688 |
K::ThreadLeaveCS();
|
|
689 |
}
|
|
690 |
}
|
|
691 |
|
|
692 |
EXPORT_C DThread* RMessageK::Thread() const
|
|
693 |
{
|
|
694 |
return iClient;
|
|
695 |
}
|
|
696 |
|
|
697 |
|
|
698 |
/********************************************
|
|
699 |
* Server control block
|
|
700 |
********************************************/
|
|
701 |
DServer::DServer()
|
|
702 |
{
|
|
703 |
}
|
|
704 |
|
|
705 |
TInt DServer::Create()
|
|
706 |
{
|
|
707 |
iMessage = new TServerMessage;
|
|
708 |
return iMessage ? KErrNone : KErrNoMemory;
|
|
709 |
}
|
|
710 |
|
|
711 |
DServer::~DServer()
|
|
712 |
//
|
|
713 |
// Destructor
|
|
714 |
// Note that no-one else will try to access this server in here, since
|
|
715 |
// the access count will now be zero.
|
|
716 |
//
|
|
717 |
{
|
|
718 |
__KTRACE_OPT(KSERVER,Kern::Printf("DServer %O Destruct", this));
|
|
719 |
__ASSERT_ALWAYS(iSessionQ.IsEmpty(), K::Fault(K::EServerDestructSessionsRemain));
|
|
720 |
__ASSERT_ALWAYS(iDeliveredQ.IsEmpty(), K::Fault(K::EServerDestructMessagesRemain));
|
|
721 |
if (iMessage)
|
|
722 |
iMessage->Close();
|
|
723 |
Kern::SafeClose((DObject*&)iOwningThread,NULL);
|
|
724 |
}
|
|
725 |
|
|
726 |
TInt DServer::Close(TAny*)
|
|
727 |
{
|
|
728 |
__KTRACE_OPT(KSERVER,Kern::Printf("DServer %O Close AC=%d", this, AccessCount()));
|
|
729 |
TInt r = Dec();
|
|
730 |
if (r==1)
|
|
731 |
{
|
|
732 |
if (iOwningThread)
|
|
733 |
{
|
|
734 |
Kern::QueueRequestComplete(iOwningThread, iMessage, KErrCancel);
|
|
735 |
NKern::LockSystem();
|
|
736 |
for (; !iSessionQ.IsEmpty(); NKern::FlashSystem())
|
|
737 |
{
|
|
738 |
DSession* s = _LOFF(iSessionQ.First(), DSession, iServerLink);
|
|
739 |
s->Detach(KErrServerTerminated);
|
|
740 |
}
|
|
741 |
__ASSERT_ALWAYS(iDeliveredQ.IsEmpty(), K::Fault(K::EServerCloseLeftoverMsg));
|
|
742 |
#ifdef BTRACE_CLIENT_SERVER
|
|
743 |
BTrace4(BTrace::EClientServer,BTrace::EServerDestroy,this);
|
|
744 |
#endif
|
|
745 |
NKern::UnlockSystem();
|
|
746 |
}
|
|
747 |
K::ObjDelete(this);
|
|
748 |
return EObjectDeleted;
|
|
749 |
}
|
|
750 |
return 0;
|
|
751 |
}
|
|
752 |
|
|
753 |
#ifndef __MESSAGE_MACHINE_CODED__
|
|
754 |
void DServer::Receive(TRequestStatus& aStatus, TAny* aMessage)
|
|
755 |
//
|
|
756 |
// Receive a message asynchronously.
|
|
757 |
// Enter and leave with system locked
|
|
758 |
//
|
|
759 |
{
|
|
760 |
|
|
761 |
if (iMessage->SetStatus(&aStatus) != KErrNone)
|
|
762 |
K::PanicCurrentThread(EMesAlreadyPending);
|
|
763 |
iMessage->iMessagePtr = aMessage;
|
|
764 |
if (!iDeliveredQ.IsEmpty())
|
|
765 |
{
|
|
766 |
RMessageK* m = _LOFF(iDeliveredQ.First()->Deque(), RMessageK, iServerLink);
|
|
767 |
Accept(m);
|
|
768 |
}
|
|
769 |
}
|
|
770 |
#endif
|
|
771 |
|
|
772 |
void DServer::Cancel()
|
|
773 |
//
|
|
774 |
// Cancel a message receive request.
|
|
775 |
// Enter and leave with system locked
|
|
776 |
//
|
|
777 |
{
|
|
778 |
Kern::QueueRequestComplete(iOwningThread, iMessage, KErrCancel);
|
|
779 |
}
|
|
780 |
|
|
781 |
#ifndef __MESSAGE_MACHINE_CODED__
|
|
782 |
void DServer::Accept(RMessageK* aMsg)
|
|
783 |
//
|
|
784 |
// Accepts a message, assumes one is pending.
|
|
785 |
// Enter and leave with system locked
|
|
786 |
//
|
|
787 |
{
|
|
788 |
// set the ACCEPTED state
|
|
789 |
aMsg->SetAccepted(iOwningThread->iOwningProcess);
|
|
790 |
if (iOwningThread->iMState==DThread::EDead)
|
|
791 |
return; // server has already died
|
|
792 |
#ifdef BTRACE_CLIENT_SERVER
|
|
793 |
BTrace4(BTrace::EClientServer,BTrace::EMessageReceive,aMsg);
|
|
794 |
#endif
|
|
795 |
iMessage->iMessageData = aMsg;
|
|
796 |
#ifdef KIPC
|
|
797 |
if (KDebugNum(KIPC))
|
|
798 |
{
|
|
799 |
TInt f = aMsg->iFunction;
|
|
800 |
if (f == RMessage2::EDisConnect)
|
|
801 |
Kern::Printf("MsgAcD: %O->%O", aMsg->iSession, this);
|
|
802 |
else
|
|
803 |
Kern::Printf("MsgAc: M:%d %O->%O", f, aMsg->iClient, this);
|
|
804 |
}
|
|
805 |
#endif //KIPC
|
|
806 |
Kern::QueueRequestComplete(iOwningThread, iMessage, KErrNone);
|
|
807 |
}
|
|
808 |
|
|
809 |
void DServer::Deliver(RMessageK* aMsg)
|
|
810 |
//
|
|
811 |
// Delivers a message to the server.
|
|
812 |
// Enter and leave with system locked
|
|
813 |
//
|
|
814 |
{
|
|
815 |
if (iMessage->IsReady())
|
|
816 |
Accept(aMsg);
|
|
817 |
else
|
|
818 |
aMsg->SetDelivered(iDeliveredQ);
|
|
819 |
}
|
|
820 |
#endif //__MESSAGE_MACHINE_CODED__
|
|
821 |
|
|
822 |
TInt DServer::RequestUserHandle(DThread* aThread, TOwnerType aType)
|
|
823 |
{
|
|
824 |
(void)aType;
|
|
825 |
return (aThread->iOwningProcess==iOwningThread->iOwningProcess) ? KErrNone : KErrPermissionDenied;
|
|
826 |
}
|
|
827 |
|
|
828 |
TInt ExecHandler::ServerCreateWithOptions(const TDesC8* aName, TInt aMode, TInt aRole, TInt aOpts)
|
|
829 |
//
|
|
830 |
// Create a server belonging to the current thread. UNPROTECTED exec call.
|
|
831 |
//
|
|
832 |
{
|
|
833 |
DThread* t = TheCurrentThread;
|
|
834 |
TInt r = KErrNone;
|
|
835 |
TKName n;
|
|
836 |
if (aName)
|
|
837 |
Kern::KUDesGet(n, *aName);
|
|
838 |
__KTRACE_OPT(KEXEC, Kern::Printf("Exec::ServerCreate %S", &n));
|
|
839 |
TInt nameLen = n.Length();
|
|
840 |
if (nameLen && n[0] == KProtectedServerNamePrefix && !Kern::CurrentThreadHasCapability(ECapabilityProtServ, __PLATSEC_DIAGNOSTIC_STRING("Attempt to create a server with a '!' as the first character of the name")))
|
|
841 |
K::UnlockedPlatformSecurityPanic();
|
|
842 |
|
|
843 |
// Fully decode & validate the arguments before allocating the server object
|
|
844 |
switch (aMode)
|
|
845 |
{
|
|
846 |
case EIpcSession_Unsharable:
|
|
847 |
case EIpcSession_Sharable:
|
|
848 |
case EIpcSession_GlobalSharable:
|
|
849 |
break;
|
|
850 |
default:
|
|
851 |
r = KErrArgument;
|
|
852 |
break;
|
|
853 |
}
|
|
854 |
|
|
855 |
switch (aRole)
|
|
856 |
{
|
|
857 |
case EServerRole_Default:
|
|
858 |
aRole = EServerRole_Standalone;
|
|
859 |
break;
|
|
860 |
case EServerRole_Standalone:
|
|
861 |
case EServerRole_Master:
|
|
862 |
case EServerRole_Slave:
|
|
863 |
break;
|
|
864 |
default:
|
|
865 |
r = KErrArgument;
|
|
866 |
break;
|
|
867 |
}
|
|
868 |
|
|
869 |
TUint pinMode = (aOpts & EServerOpt_PinClientDescriptorsMask);
|
|
870 |
aOpts &= ~EServerOpt_PinClientDescriptorsMask;
|
|
871 |
switch (pinMode)
|
|
872 |
{
|
|
873 |
case EServerOpt_PinClientDescriptorsDisable:
|
|
874 |
pinMode = EFalse;
|
|
875 |
break;
|
|
876 |
case EServerOpt_PinClientDescriptorsEnable:
|
|
877 |
pinMode = ETrue;
|
|
878 |
break;
|
|
879 |
case EServerOpt_PinClientDescriptorsDefault:
|
|
880 |
pinMode = EFalse;
|
|
881 |
if (K::MemModelAttributes & EMemModelAttrDataPaging)
|
|
882 |
if (!(t->iOwningProcess->iAttributes & DProcess::EDataPaged))
|
|
883 |
{
|
|
884 |
// If the platform supports data-paging, and the server process
|
|
885 |
// *isn't* data-paged, then enable descriptor pinning by default
|
|
886 |
#if 0
|
|
887 |
// Worried about the impact of this causing new failure modes in
|
|
888 |
// existing code - for now we are not going to turn on pinning by
|
|
889 |
// default for unpaged servers.
|
|
890 |
pinMode = ETrue;
|
|
891 |
#endif
|
|
892 |
}
|
|
893 |
break;
|
|
894 |
default:
|
|
895 |
r = KErrArgument;
|
|
896 |
break;
|
|
897 |
}
|
|
898 |
|
|
899 |
if (aOpts)
|
|
900 |
r = KErrArgument;
|
|
901 |
if (r != KErrNone)
|
|
902 |
return r; // An invalid server mode, role or option was specified.
|
|
903 |
|
|
904 |
r = KErrNoMemory;
|
|
905 |
NKern::ThreadEnterCS();
|
|
906 |
DServer *pS = new DServer;
|
|
907 |
if (pS)
|
|
908 |
{
|
|
909 |
r = pS->Create();
|
|
910 |
if (r == KErrNone)
|
|
911 |
{
|
|
912 |
t->Open();
|
|
913 |
pS->iOwningThread = t;
|
|
914 |
pS->iSessionType = (TUint8)aMode;
|
|
915 |
pS->iServerRole = (TUint8)aRole;
|
|
916 |
pS->iPinClientDescriptors = (TUint8)pinMode;
|
|
917 |
if (nameLen)
|
|
918 |
r = pS->SetName(&n);
|
|
919 |
#ifdef BTRACE_CLIENT_SERVER
|
|
920 |
BTraceContextN(BTrace::EClientServer, BTrace::EServerCreate, pS, 0, n.Ptr(), nameLen);
|
|
921 |
#endif
|
|
922 |
}
|
|
923 |
if (r == KErrNone)
|
|
924 |
{
|
|
925 |
r = K::AddObject(pS, EServer);
|
|
926 |
if (r == KErrNone)
|
|
927 |
r = K::MakeHandle(nameLen ? EOwnerThread : EOwnerProcess, pS);
|
|
928 |
}
|
|
929 |
if (r < KErrNone)
|
|
930 |
pS->Close(NULL);
|
|
931 |
}
|
|
932 |
NKern::ThreadLeaveCS();
|
|
933 |
__KTRACE_OPT(KEXEC, Kern::Printf("Exec::ServerCreate returns %d", r));
|
|
934 |
return r;
|
|
935 |
}
|
|
936 |
|
|
937 |
TInt ExecHandler::ServerCreate(const TDesC8* aName, TInt aMode)
|
|
938 |
{
|
|
939 |
return ServerCreateWithOptions(aName, aMode, EServerRole_Default, 0);
|
|
940 |
}
|
|
941 |
|
|
942 |
|
|
943 |
/********************************************
|
|
944 |
* Session control block
|
|
945 |
********************************************/
|
|
946 |
DSession::DSession()
|
|
947 |
: iTotalAccessCount(1), iMsgLimit(KMaxMsgLimit)
|
|
948 |
{
|
|
949 |
}
|
|
950 |
|
|
951 |
DSession::~DSession()
|
|
952 |
//
|
|
953 |
// Destroy
|
|
954 |
//
|
|
955 |
{
|
|
956 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::Destruct"));
|
|
957 |
|
|
958 |
// DObject access count and total access count should both be zero
|
|
959 |
__ASSERT_ALWAYS(AccessCount()==0 && iTotalAccessCount==0, K::Fault(K::ESessionDestructStillRef));
|
|
960 |
|
|
961 |
// session should already have been unlinked from server
|
|
962 |
__ASSERT_ALWAYS(!iServer, K::Fault(K::ESessionDestruct));
|
|
963 |
|
|
964 |
// there should be no messages outstanding on this session
|
|
965 |
__ASSERT_ALWAYS(iMsgCount==0, K::Fault(K::ESessionDestructMsgCount));
|
|
966 |
__ASSERT_ALWAYS(iMsgQ.IsEmpty(), K::Fault(K::ESessionDestructMsgQ));
|
|
967 |
|
|
968 |
if (iDisconnectMsgPtr)
|
|
969 |
{
|
|
970 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::~DSession(%08X) releasing disconnect message at %08X", this, iDisconnectMsgPtr));
|
|
971 |
iDisconnectMsgPtr->ReleaseMessagePool(RMessageK::EDisc, 1);
|
|
972 |
}
|
|
973 |
iDisconnectMsgPtr = NULL;
|
|
974 |
if (iNextFreeMessage)
|
|
975 |
{
|
|
976 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::~DSession(%08X) releasing session pool at %08X", this, iNextFreeMessage));
|
|
977 |
__ASSERT_DEBUG(iHasSessionPool, K::Fault(K::ESessionDestructMsgCount));
|
|
978 |
__ASSERT_DEBUG(iPoolSize > 0, K::Fault(K::ESessionDestructMsgCount));
|
|
979 |
iNextFreeMessage->ReleaseMessagePool(RMessageK::ESession, iPoolSize);
|
|
980 |
}
|
|
981 |
iNextFreeMessage = NULL;
|
|
982 |
}
|
|
983 |
|
|
984 |
|
|
985 |
/**
|
|
986 |
Decrement the total access count, deleting the session object if this is the last
|
|
987 |
reference on the session.
|
|
988 |
|
|
989 |
Note - This may release the system lock temporarily.
|
|
990 |
|
|
991 |
@return 0 if the session still exists, EObjectDeleted if the session has been deleted.
|
|
992 |
|
|
993 |
@pre Enter with system locked
|
|
994 |
@post Leave with system locked
|
|
995 |
*/
|
|
996 |
TInt DSession::TotalAccessDec()
|
|
997 |
{
|
|
998 |
__ASSERT_SYSTEM_LOCK;
|
|
999 |
TInt r = 0;
|
|
1000 |
if (--iTotalAccessCount == 0)
|
|
1001 |
{// The session must have been closed.
|
|
1002 |
__NK_ASSERT_DEBUG(AccessCount()==0);
|
|
1003 |
NKern::UnlockSystem();
|
|
1004 |
K::ObjDelete(this);
|
|
1005 |
r = EObjectDeleted;
|
|
1006 |
NKern::LockSystem();
|
|
1007 |
}
|
|
1008 |
return r;
|
|
1009 |
}
|
|
1010 |
|
|
1011 |
/**
|
|
1012 |
As DSession::TotalAccessDec() except it returns with the system lock released.
|
|
1013 |
|
|
1014 |
@pre Enter with system locked
|
|
1015 |
@post Leave with system unlocked
|
|
1016 |
*/
|
|
1017 |
TInt DSession::TotalAccessDecRel()
|
|
1018 |
{
|
|
1019 |
__ASSERT_SYSTEM_LOCK;
|
|
1020 |
TInt r = 0;
|
|
1021 |
TUint tac = --iTotalAccessCount;
|
|
1022 |
NKern::UnlockSystem();
|
|
1023 |
|
|
1024 |
if (tac == 0)
|
|
1025 |
{// The session must have been closed.
|
|
1026 |
__NK_ASSERT_DEBUG(AccessCount()==0);
|
|
1027 |
K::ObjDelete(this);
|
|
1028 |
r = EObjectDeleted;
|
|
1029 |
}
|
|
1030 |
return r;
|
|
1031 |
}
|
|
1032 |
|
|
1033 |
|
|
1034 |
void DSession::Transfer(DServer* aServer, RMessageK* aMsg)
|
|
1035 |
//
|
|
1036 |
// Enter and leave with system locked
|
|
1037 |
//
|
|
1038 |
{
|
|
1039 |
(void)aMsg; // avoid non-DEBUG whinge about unused parameter
|
|
1040 |
__KTRACE_OPT(KSERVER, Kern::Printf("Session %O Transfer From %O To %O, msg %08x",
|
|
1041 |
this, iServer, aServer, aMsg));
|
|
1042 |
|
|
1043 |
// The current server must be a master, and the new one a designated slave
|
|
1044 |
if (iServer->iServerRole != EServerRole_Master)
|
|
1045 |
K::PanicCurrentThread(ESessionInvalidCookieMsg);
|
|
1046 |
if (aServer->iServerRole != EServerRole_Slave)
|
|
1047 |
K::PanicCurrentThread(ESessionInvalidCookieMsg);
|
|
1048 |
|
|
1049 |
// Both servers must be part of the same process
|
|
1050 |
if (iServer->iOwningThread->iOwningProcess != aServer->iOwningThread->iOwningProcess)
|
|
1051 |
K::PanicCurrentThread(ESessionInvalidCookieMsg);
|
|
1052 |
|
|
1053 |
// Transfers must not involve moribund servers
|
|
1054 |
if (iServer->IsClosing() || aServer->IsClosing())
|
|
1055 |
K::PanicCurrentThread(ESessionInvalidCookieMsg);
|
|
1056 |
if (iServer->iOwningThread->iMState == DThread::EDead)
|
|
1057 |
K::PanicCurrentThread(ESessionInvalidCookieMsg);
|
|
1058 |
if (aServer->iOwningThread->iMState == DThread::EDead)
|
|
1059 |
K::PanicCurrentThread(ESessionInvalidCookieMsg);
|
|
1060 |
|
|
1061 |
// Unlink from the current server, and link to the new one
|
|
1062 |
iServerLink.Deque();
|
|
1063 |
iServer = aServer;
|
|
1064 |
iServer->iSessionQ.Add(&iServerLink);
|
|
1065 |
|
|
1066 |
// The message queue must contain at least the Connect message
|
|
1067 |
__ASSERT_DEBUG(!iMsgQ.IsEmpty(),
|
|
1068 |
K::Fault(K::EMsgFreeBadPool));
|
|
1069 |
|
|
1070 |
RMessageK* connectMsg = _LOFF(iMsgQ.First(), RMessageK, iSessionLink);
|
|
1071 |
// RMessageK* lastMsg = _LOFF(iMsgQ.Last(), RMessageK, iSessionLink);
|
|
1072 |
|
|
1073 |
__ASSERT_DEBUG(connectMsg == aMsg,
|
|
1074 |
K::Fault(K::EKernelMsgNotAccepted));
|
|
1075 |
__ASSERT_DEBUG(connectMsg->iFunction == RMessage2::EConnect,
|
|
1076 |
K::Fault(K::EKernelMsgNotAccepted));
|
|
1077 |
__ASSERT_DEBUG(connectMsg->IsAccepted(),
|
|
1078 |
K::Fault(K::EKernelMsgNotAccepted));
|
|
1079 |
|
|
1080 |
// Transfer all pending messages except the first (which is the Connect)
|
|
1081 |
SDblQueLink* p;
|
|
1082 |
TInt msgCount = 0;
|
|
1083 |
for (RMessageK* m = connectMsg; (p = m->iSessionLink.iNext) != &iMsgQ.iA; )
|
|
1084 |
{
|
|
1085 |
msgCount += 1;
|
|
1086 |
m = _LOFF(p, RMessageK, iSessionLink);
|
|
1087 |
__ASSERT_DEBUG(!m->IsAccepted(),
|
|
1088 |
K::Fault(K::EMessageAlreadyPending));
|
|
1089 |
if (m->IsDelivered())
|
|
1090 |
m->iServerLink.Deque();
|
|
1091 |
iServer->Deliver(m);
|
|
1092 |
}
|
|
1093 |
|
|
1094 |
// Transfer the disconnect message too?
|
|
1095 |
if (iDisconnectMsgPtr->IsDelivered())
|
|
1096 |
{
|
|
1097 |
__ASSERT_DEBUG(!iDisconnectMsgPtr->IsAccepted(),
|
|
1098 |
K::Fault(K::EMessageAlreadyPending));
|
|
1099 |
iDisconnectMsgPtr->iServerLink.Deque();
|
|
1100 |
__SendDiscMsg(this);
|
|
1101 |
}
|
|
1102 |
}
|
|
1103 |
|
|
1104 |
// Detach a session from the server
|
|
1105 |
// Called either when server terminates or when server completes disconnect message
|
|
1106 |
// Enter and leave with system locked
|
|
1107 |
//
|
|
1108 |
void DSession::Detach(TInt aReason)
|
|
1109 |
{
|
|
1110 |
__KTRACE_OPT(KSERVER,Kern::Printf("Session %O Detach From %O reason %d", this, iServer, aReason));
|
|
1111 |
iServer = NULL;
|
|
1112 |
iServerLink.Deque();
|
|
1113 |
|
|
1114 |
if (iDisconnectMsgPtr->IsDelivered())
|
|
1115 |
iDisconnectMsgPtr->iServerLink.Deque();
|
|
1116 |
iDisconnectMsgPtr->SetFree();
|
|
1117 |
|
|
1118 |
for (; !iMsgQ.IsEmpty(); NKern::LockSystem())
|
|
1119 |
{
|
|
1120 |
--iMsgCount;
|
|
1121 |
RMessageK* m = _LOFF(iMsgQ.First()->Deque(), RMessageK, iSessionLink);
|
|
1122 |
__ASSERT_ALWAYS(m->iSession == this, K::Fault(K::EInvalidSessionAccessCount));
|
|
1123 |
m->iSession = NULL; // message now detached from session
|
|
1124 |
if (m->iMsgType == RMessageK::ESession)
|
|
1125 |
iPoolSize -= 1; // one less message in the pool
|
|
1126 |
|
|
1127 |
// Note whether session connect message has been discarded
|
|
1128 |
if (m->iFunction == RMessage2::EConnect)
|
|
1129 |
iConnectMsgPtr = NULL;
|
|
1130 |
if (m->IsDelivered())
|
|
1131 |
m->iServerLink.Deque();
|
|
1132 |
|
|
1133 |
DThread* t = m->iClient;
|
|
1134 |
TUint32 c = --t->iIpcCount;
|
|
1135 |
|
|
1136 |
if(m->IsDelivered() || m->IsAccepted())
|
|
1137 |
{
|
|
1138 |
if (!IsClosing())
|
|
1139 |
{
|
|
1140 |
m->SetCompleting();
|
|
1141 |
Kern::QueueRequestComplete(t, m, aReason);
|
|
1142 |
}
|
|
1143 |
else
|
|
1144 |
{
|
|
1145 |
m->Reset();
|
|
1146 |
m->CloseRef();
|
|
1147 |
}
|
|
1148 |
}
|
|
1149 |
|
|
1150 |
NKern::UnlockSystem();
|
|
1151 |
if (c == 0x80000000u)
|
|
1152 |
t->AsyncClose();
|
|
1153 |
}
|
|
1154 |
|
|
1155 |
// Output total access count here before the session object may potentially
|
|
1156 |
// be deleted. Give the value after the detach has completed.
|
|
1157 |
__KTRACE_OPT(KSERVER,Kern::Printf("TAC: %d", iTotalAccessCount - 1));
|
|
1158 |
#ifdef BTRACE_CLIENT_SERVER
|
|
1159 |
BTrace8(BTrace::EClientServer,BTrace::ESessionDetach,this,aReason);
|
|
1160 |
#endif
|
|
1161 |
// This may temporarily release the system lock if it deletes the session.
|
|
1162 |
// Don't access any members after this call as the session may be deleted.
|
|
1163 |
TotalAccessDec();
|
|
1164 |
}
|
|
1165 |
|
|
1166 |
void DSession::CloseFromDisconnect()
|
|
1167 |
//
|
|
1168 |
// Called when server completes the disconnect message
|
|
1169 |
// Enter and leave with system locked
|
|
1170 |
//
|
|
1171 |
{
|
|
1172 |
__KTRACE_OPT(KIPC,Kern::Printf("MsgCoD: %O->%O",TheCurrentThread,this));
|
|
1173 |
__KTRACE_OPT(KTHREAD,Kern::Printf("DSession::CloseFromDisconnect AC:%d TAC:%d", AccessCount(), iTotalAccessCount));
|
|
1174 |
__ASSERT_ALWAYS(AccessCount()==0, K::Fault(K::EInvalidSessionAccessCount));
|
|
1175 |
|
|
1176 |
// there should not be any DELIVERED messages outstanding
|
|
1177 |
// there may be outstanding ACCEPTED messages
|
|
1178 |
// no other thread can access the session here
|
|
1179 |
|
|
1180 |
NKern::ThreadEnterCS();
|
|
1181 |
iDisconnectMsgPtr->SetFree();
|
|
1182 |
if (iServer && !iServer->IsClosing())
|
|
1183 |
Detach(KErrSessionClosed);
|
|
1184 |
// otherwise server is closing, so let that handle it
|
|
1185 |
NKern::ThreadLeaveCS();
|
|
1186 |
}
|
|
1187 |
|
|
1188 |
TInt DSession::Close(TAny*)
|
|
1189 |
//
|
|
1190 |
// Session close called from client side.
|
|
1191 |
// Enter and leave with system unlocked.
|
|
1192 |
//
|
|
1193 |
{
|
|
1194 |
__KTRACE_OPT(KTHREAD,Kern::Printf("DSession::Close %O AC:%d TAC:%d", this, AccessCount(), iTotalAccessCount));
|
|
1195 |
if (Dec() == 1)
|
|
1196 |
{
|
|
1197 |
// Last client access has been closed. Only the server may now influence this session.
|
|
1198 |
// Thus no more messages can be added to the session's queue.
|
|
1199 |
|
|
1200 |
NKern::LockSystem();
|
|
1201 |
|
|
1202 |
if (!iDisconnectMsgPtr)
|
|
1203 |
{
|
|
1204 |
// Allocation of disconnect message failed during session create; there's no cleanup to do
|
|
1205 |
}
|
|
1206 |
else if (!iDisconnectMsgPtr->IsFree())
|
|
1207 |
{
|
|
1208 |
// Disconnect message has already been sent and is pending completion by the server
|
|
1209 |
}
|
|
1210 |
else if (iSessionCookie)
|
|
1211 |
{
|
|
1212 |
// Deliver a disconnect message now if a user-side session has already been created
|
|
1213 |
__SendDiscMsg(this);
|
|
1214 |
}
|
|
1215 |
else if (!iConnectMsgPtr)
|
|
1216 |
{
|
|
1217 |
// Deliver a disconnect message now if there is no connect message outstanding
|
|
1218 |
__SendDiscMsg(this);
|
|
1219 |
}
|
|
1220 |
else if (iConnectMsgPtr->IsDelivered())
|
|
1221 |
{
|
|
1222 |
// We'll remove the pending message, to prevent it from being accepted and creating
|
|
1223 |
// an orphan session object. Then, completing the connect message will result in a
|
|
1224 |
// disconnect message being sent, as this is equivalent to the case where a connect
|
|
1225 |
// message was accepted but then subsequently rejected.
|
|
1226 |
iConnectMsgPtr->iServerLink.Deque();
|
|
1227 |
iConnectMsgPtr->SetCompleting();
|
|
1228 |
ExecHandler::MessageComplete(iConnectMsgPtr, KErrSessionClosed);
|
|
1229 |
}
|
|
1230 |
else
|
|
1231 |
{
|
|
1232 |
// There's an accepted connect message, but no user session (yet). However
|
|
1233 |
// one could still be created, so delay sending the disconnect message.
|
|
1234 |
// It will be sent from SetSessionPtr() or MessageComplete() instead.
|
|
1235 |
}
|
|
1236 |
|
|
1237 |
// Remove client contribution to the total access count. This may
|
|
1238 |
// delete the object so don't access any of its members after this call.
|
|
1239 |
// This will release the system lock.
|
|
1240 |
return TotalAccessDecRel();
|
|
1241 |
}
|
|
1242 |
|
|
1243 |
return 0;
|
|
1244 |
}
|
|
1245 |
|
|
1246 |
// Enter and return with system unlocked and caller in a critical section
|
|
1247 |
TInt DSession::New(DSession*& aS, TInt aMsgSlots, TInt aMode)
|
|
1248 |
{
|
|
1249 |
__KTRACE_OPT(KEXEC,Kern::Printf("DSession::New MS:%d Mode %d", aMsgSlots, aMode));
|
|
1250 |
if (aMsgSlots<-1 || aMsgSlots>KMaxMsgLimit)
|
|
1251 |
return KErrArgument;
|
|
1252 |
if ( (TUint)aMode > (TUint)EIpcSession_GlobalSharable)
|
|
1253 |
return KErrArgument;
|
|
1254 |
aS = new DSession;
|
|
1255 |
if (!aS)
|
|
1256 |
return KErrNoMemory;
|
|
1257 |
|
|
1258 |
RMessageK* mp = RMessageK::ClaimMessagePool(RMessageK::EDisc, 1, aS);
|
|
1259 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::New(%08X) claimed disconnect message at %08X", aS, mp));
|
|
1260 |
if (!mp)
|
|
1261 |
return KErrNoMemory;
|
|
1262 |
mp->iFunction = RMessage2::EDisConnect;
|
|
1263 |
aS->iDisconnectMsgPtr = mp;
|
|
1264 |
|
|
1265 |
aS->iSessionType = (TUint8)aMode;
|
|
1266 |
if (aMsgSlots == 0)
|
|
1267 |
{
|
|
1268 |
aS->iHasSessionPool = 1; // pretend it has its own pool
|
|
1269 |
aS->iNextFreeMessage = NULL; // but never any free messages
|
|
1270 |
}
|
|
1271 |
else if (aMsgSlots > 0)
|
|
1272 |
{
|
|
1273 |
aS->iHasSessionPool = 1; // remember that it has its own pool
|
|
1274 |
aS->iNextFreeMessage = RMessageK::ClaimMessagePool(RMessageK::ESession, aMsgSlots, aS);
|
|
1275 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::New(%08X) claimed %d messages at %08X", aS, aMsgSlots, aS->iNextFreeMessage));
|
|
1276 |
if (!aS->iNextFreeMessage)
|
|
1277 |
return KErrNoMemory;
|
|
1278 |
aS->iPoolSize = aMsgSlots; // remember that it has its own pool
|
|
1279 |
}
|
|
1280 |
return KErrNone;
|
|
1281 |
}
|
|
1282 |
|
|
1283 |
// Add a new session to a server
|
|
1284 |
// Enter with system locked, return with system unlocked
|
|
1285 |
TInt DSession::Add(DServer* aSvr, const TSecurityPolicy* aSecurityPolicy)
|
|
1286 |
{
|
|
1287 |
__KTRACE_OPT(KSERVER,Kern::Printf("Session %O Add to server %O", this, aSvr));
|
|
1288 |
if (aSvr->IsClosing())
|
|
1289 |
{
|
|
1290 |
NKern::UnlockSystem();
|
|
1291 |
return KErrServerTerminated;
|
|
1292 |
}
|
|
1293 |
if (aSecurityPolicy && !aSecurityPolicy->CheckPolicy(aSvr->iOwningThread,__PLATSEC_DIAGNOSTIC_STRING("Checked during RSessionBase::CreateSession")))
|
|
1294 |
{
|
|
1295 |
NKern::UnlockSystem();
|
|
1296 |
return KErrPermissionDenied;
|
|
1297 |
}
|
|
1298 |
iSvrSessionType = aSvr->iSessionType;
|
|
1299 |
if (iSessionType > iSvrSessionType)
|
|
1300 |
{
|
|
1301 |
NKern::UnlockSystem();
|
|
1302 |
return iSvrSessionType ? KErrPermissionDenied : KErrAccessDenied;
|
|
1303 |
}
|
|
1304 |
iServer = aSvr;
|
|
1305 |
aSvr->iSessionQ.Add(&iServerLink); // Add it to the server
|
|
1306 |
TotalAccessInc(); // give the server an access on this session
|
|
1307 |
#ifdef BTRACE_CLIENT_SERVER
|
|
1308 |
BTraceContext8(BTrace::EClientServer,BTrace::ESessionAttach,this,aSvr);
|
|
1309 |
#endif
|
|
1310 |
NKern::UnlockSystem(); // server could be deleted after this line
|
|
1311 |
|
|
1312 |
if (iSvrSessionType == EIpcSession_Unsharable)
|
|
1313 |
SetOwner(TheCurrentThread);
|
|
1314 |
else if (iSvrSessionType == EIpcSession_Sharable)
|
|
1315 |
SetOwner(TheCurrentThread->iOwningProcess);
|
|
1316 |
if (iSessionType == EIpcSession_GlobalSharable)
|
|
1317 |
SetProtection(EProtected);
|
|
1318 |
return KErrNone;
|
|
1319 |
}
|
|
1320 |
|
|
1321 |
TInt DSession::MakeHandle()
|
|
1322 |
{
|
|
1323 |
TInt r = K::AddObject(this, ESession);
|
|
1324 |
if (r==KErrNone)
|
|
1325 |
{
|
|
1326 |
TOwnerType htype = iSessionType == EIpcSession_Unsharable ? EOwnerThread : EOwnerProcess;
|
|
1327 |
r = K::MakeHandle(htype, this);
|
|
1328 |
}
|
|
1329 |
return r;
|
|
1330 |
}
|
|
1331 |
|
|
1332 |
TInt DSession::RequestUserHandle(DThread* aThread, TOwnerType aType)
|
|
1333 |
{
|
|
1334 |
if (iSvrSessionType == EIpcSession_Unsharable)
|
|
1335 |
return (aThread==Owner() && aType==EOwnerThread) ? KErrNone : KErrPermissionDenied;
|
|
1336 |
if (iSvrSessionType == EIpcSession_Sharable)
|
|
1337 |
return (aThread->iOwningProcess==Owner()) ? KErrNone : KErrPermissionDenied;
|
|
1338 |
return KErrNone;
|
|
1339 |
}
|
|
1340 |
|
|
1341 |
TInt ExecHandler::SessionCreate(const TDesC& aServer, TInt aMsgSlots, const TSecurityPolicy* aSecurityPolicy, TInt aMode)
|
|
1342 |
//
|
|
1343 |
// Create a new session. UNPROTECTED exec call.
|
|
1344 |
//
|
|
1345 |
{
|
|
1346 |
TKName n;
|
|
1347 |
TKName svrName;
|
|
1348 |
DServer* svr = NULL;
|
|
1349 |
DObjectCon& servers = *K::Containers[EServer];
|
|
1350 |
Kern::KUDesGet(n, aServer);
|
|
1351 |
|
|
1352 |
TSecurityPolicy policy;
|
|
1353 |
if(aSecurityPolicy)
|
|
1354 |
{
|
|
1355 |
kumemget32(&policy,aSecurityPolicy,sizeof(policy));
|
|
1356 |
if(!policy.Validate())
|
|
1357 |
return KErrArgument;
|
|
1358 |
aSecurityPolicy=&policy;
|
|
1359 |
}
|
|
1360 |
|
|
1361 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionCreate %S MS:%d Mode %d", &n, aMsgSlots, aMode));
|
|
1362 |
NKern::ThreadEnterCS();
|
|
1363 |
DSession* s=NULL;
|
|
1364 |
TInt r = DSession::New(s, aMsgSlots, aMode);
|
|
1365 |
if (r==KErrNone)
|
|
1366 |
{
|
|
1367 |
servers.Wait();
|
|
1368 |
TFindHandle fh;
|
|
1369 |
r = servers.FindByName(fh, n, svrName);
|
|
1370 |
if (r==KErrNone)
|
|
1371 |
{
|
|
1372 |
svr = (DServer*)servers.At(fh); // can't return NULL since we just found the server
|
|
1373 |
NKern::LockSystem();
|
|
1374 |
r = s->Add(svr, aSecurityPolicy);
|
|
1375 |
}
|
|
1376 |
servers.Signal();
|
|
1377 |
}
|
|
1378 |
if (r==KErrNone)
|
|
1379 |
r = s->MakeHandle();
|
|
1380 |
if (r<KErrNone && s)
|
|
1381 |
s->Close(NULL);
|
|
1382 |
NKern::ThreadLeaveCS();
|
|
1383 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionCreate returns %d",r));
|
|
1384 |
if (r==KErrAccessDenied)
|
|
1385 |
K::PanicKernExec(EUnsharableSession);
|
|
1386 |
return r;
|
|
1387 |
}
|
|
1388 |
|
|
1389 |
TInt ExecHandler::SessionCreateFromHandle(TInt aSvrHandle, TInt aMsgSlots, const TSecurityPolicy* aSecurityPolicy, TInt aMode)
|
|
1390 |
//
|
|
1391 |
// Create a new session to the given server.
|
|
1392 |
// Enter and return with system unlocked.
|
|
1393 |
//
|
|
1394 |
{
|
|
1395 |
TSecurityPolicy policy;
|
|
1396 |
if(aSecurityPolicy)
|
|
1397 |
{
|
|
1398 |
kumemget32(&policy,aSecurityPolicy,sizeof(policy));
|
|
1399 |
if(!policy.Validate())
|
|
1400 |
return KErrArgument;
|
|
1401 |
aSecurityPolicy=&policy;
|
|
1402 |
}
|
|
1403 |
|
|
1404 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionCreate %08x MS:%d Mode %d", aSvrHandle, aMsgSlots, aMode));
|
|
1405 |
NKern::ThreadEnterCS();
|
|
1406 |
DSession* s;
|
|
1407 |
DThread* t = TheCurrentThread;
|
|
1408 |
TInt r = DSession::New(s, aMsgSlots, aMode);
|
|
1409 |
if (r==KErrNone)
|
|
1410 |
{
|
|
1411 |
NKern::LockSystem();
|
|
1412 |
DServer* svr = (DServer*)t->ObjectFromHandle(aSvrHandle, EServer);
|
|
1413 |
if (svr)
|
|
1414 |
r = s->Add(svr, aSecurityPolicy);
|
|
1415 |
else
|
|
1416 |
{
|
|
1417 |
r = KErrBadHandle;
|
|
1418 |
NKern::UnlockSystem();
|
|
1419 |
}
|
|
1420 |
}
|
|
1421 |
if (r==KErrNone)
|
|
1422 |
r = s->MakeHandle();
|
|
1423 |
if (r<KErrNone && s)
|
|
1424 |
s->Close(NULL);
|
|
1425 |
NKern::ThreadLeaveCS();
|
|
1426 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionCreate returns %d",r));
|
|
1427 |
if (r==KErrAccessDenied)
|
|
1428 |
K::PanicKernExec(EUnsharableSession);
|
|
1429 |
if (r==KErrBadHandle)
|
|
1430 |
K::PanicKernExec(EBadHandle);
|
|
1431 |
return r;
|
|
1432 |
}
|
|
1433 |
|
|
1434 |
// If this session doesn't have its own pool, get a free message from the
|
|
1435 |
// global pool. This will attempt to expand the pool if necessary, and may
|
|
1436 |
// temporarily release the system lock.
|
|
1437 |
//
|
|
1438 |
// If this session does have its own message pool, get a free message from
|
|
1439 |
// it; if there are none, just return NULL, without attempting to expand it
|
|
1440 |
// or releasing the system lock.
|
|
1441 |
//
|
|
1442 |
// Enter and return with system lock, may be temporarily released though
|
|
1443 |
RMessageK* DSession::GetNextFreeMessage()
|
|
1444 |
{
|
|
1445 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED, "DSession::GetNextFreeMessage");
|
|
1446 |
|
|
1447 |
if (!iHasSessionPool)
|
|
1448 |
return RMessageK::GetNextFreeMessage(this);
|
|
1449 |
|
|
1450 |
RMessageK* volatile* p = &iNextFreeMessage;
|
|
1451 |
RMessageK* m = *p;
|
|
1452 |
if (m)
|
|
1453 |
{
|
|
1454 |
*p = (RMessageK*)m->iSessionLink.iNext;
|
|
1455 |
m->iSessionLink.iNext = NULL;
|
|
1456 |
m->iSession = this;
|
|
1457 |
}
|
|
1458 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::GetNextFreeMessage returns %08x", m));
|
|
1459 |
return m;
|
|
1460 |
}
|
|
1461 |
|
|
1462 |
TInt DSession::Send(TInt aHandle, TInt aFunction, const TInt* aPtr, TRequestStatus* aStatus)
|
|
1463 |
//
|
|
1464 |
// Send a message to a server. Assumes aStatus
|
|
1465 |
// has already been set to KRequestPending.
|
|
1466 |
// Enter and return with system unlocked.
|
|
1467 |
//
|
|
1468 |
{
|
|
1469 |
if (aFunction == RMessage2::EDisConnect)
|
|
1470 |
return KErrArgument;
|
|
1471 |
|
|
1472 |
RMessageK::TMsgArgs msgArgs;
|
|
1473 |
if (aPtr)
|
|
1474 |
msgArgs.ReadDesHeaders(aPtr);
|
|
1475 |
|
|
1476 |
NKern::LockSystem();
|
|
1477 |
DSession* session = (DSession*)K::ObjectFromHandle(aHandle, ESession);
|
|
1478 |
RMessageK* m = session->GetNextFreeMessage();
|
|
1479 |
if (!m)
|
|
1480 |
{
|
|
1481 |
NKern::UnlockSystem();
|
|
1482 |
return KErrServerBusy;
|
|
1483 |
}
|
|
1484 |
__ASSERT_DEBUG(m->IsFree(), K::Fault(K::EMessageNotFree));
|
|
1485 |
return session->Send(m, aFunction, aPtr ? &msgArgs : NULL, aStatus);
|
|
1486 |
}
|
|
1487 |
|
|
1488 |
TInt DSession::SendSync(TInt aHandle, TInt aFunction, const TInt* aPtr, TRequestStatus* aStatus)
|
|
1489 |
//
|
|
1490 |
// Send a SYNCHRONOUS message to a server using current thread's
|
|
1491 |
// dedicated synchronous message. Assumes aStatus has already been set to KRequestPending.
|
|
1492 |
// Enter and return with system unlocked.
|
|
1493 |
//
|
|
1494 |
{
|
|
1495 |
if (aFunction == RMessage2::EDisConnect)
|
|
1496 |
return KErrArgument;
|
|
1497 |
|
|
1498 |
RMessageK::TMsgArgs msgArgs;
|
|
1499 |
if (aPtr)
|
|
1500 |
msgArgs.ReadDesHeaders(aPtr);
|
|
1501 |
|
|
1502 |
NKern::LockSystem();
|
|
1503 |
DSession* session = (DSession*)K::ObjectFromHandle(aHandle, ESession);
|
|
1504 |
RMessageK* m = TheCurrentThread->iSyncMsgPtr;
|
|
1505 |
__ASSERT_ALWAYS(m->IsFree(), K::PanicCurrentThread(ESyncMsgSentTwice));
|
|
1506 |
return session->Send(m, aFunction, aPtr ? &msgArgs : NULL, aStatus);
|
|
1507 |
}
|
|
1508 |
|
|
1509 |
TInt DSession::Send(RMessageK* aMsg, TInt aFunction, const RMessageK::TMsgArgs* aArgs, TRequestStatus* aStatus)
|
|
1510 |
//
|
|
1511 |
// Send a message to a server.
|
|
1512 |
// Enter with system locked, return with system unlocked.
|
|
1513 |
//
|
|
1514 |
{
|
|
1515 |
TInt panicReason = KErrNone;
|
|
1516 |
TInt r = KErrNone;
|
|
1517 |
|
|
1518 |
__NK_ASSERT_DEBUG(aMsg->IsFree());
|
|
1519 |
|
|
1520 |
aMsg->OpenRef();
|
|
1521 |
aMsg->SetInitialising();
|
|
1522 |
aMsg->iSession = this;
|
|
1523 |
aMsg->iClient = TheCurrentThread;
|
|
1524 |
TheCurrentThread->iIpcCount += 1;
|
|
1525 |
iMsgQ.Add(&aMsg->iSessionLink);
|
|
1526 |
if (++iMsgCount > iMsgLimit)
|
|
1527 |
{
|
|
1528 |
r = KErrOverflow;
|
|
1529 |
goto error;
|
|
1530 |
}
|
|
1531 |
|
|
1532 |
// Check server is still alive ...
|
|
1533 |
if (!iServer || iServer->IsClosing())
|
|
1534 |
{
|
|
1535 |
r = KErrServerTerminated;
|
|
1536 |
goto error;
|
|
1537 |
}
|
|
1538 |
|
|
1539 |
if (aFunction == RMessage2::EConnect)
|
|
1540 |
{
|
|
1541 |
// Only one connect message is allowed at once
|
|
1542 |
if (iConnectMsgPtr)
|
|
1543 |
{
|
|
1544 |
panicReason = ERequestAlreadyPending;
|
|
1545 |
goto error;
|
|
1546 |
}
|
|
1547 |
|
|
1548 |
// Further connect messages not allowed after successful session creation
|
|
1549 |
if (iSessionCookie)
|
|
1550 |
{
|
|
1551 |
panicReason = ESessionAlreadyConnected;
|
|
1552 |
goto error;
|
|
1553 |
}
|
|
1554 |
|
|
1555 |
// Keep track of connect message
|
|
1556 |
iConnectMsgPtr = aMsg;
|
|
1557 |
}
|
|
1558 |
|
|
1559 |
// Set message contents
|
|
1560 |
aMsg->iFunction = aFunction;
|
|
1561 |
if (aArgs == NULL)
|
|
1562 |
{
|
|
1563 |
aMsg->iMsgArgs.iArgFlags = 0;
|
|
1564 |
aMsg->iMsgArgs.iArgs[0] = 0;
|
|
1565 |
aMsg->iMsgArgs.iArgs[1] = 0;
|
|
1566 |
aMsg->iMsgArgs.iArgs[2] = 0;
|
|
1567 |
aMsg->iMsgArgs.iArgs[3] = 0;
|
|
1568 |
}
|
|
1569 |
else
|
|
1570 |
{
|
|
1571 |
aMsg->iMsgArgs = *aArgs;
|
|
1572 |
|
|
1573 |
// Attempt to pin the descriptors if required. This may release the system lock temporarily
|
|
1574 |
r = aMsg->PinDescriptors(this, iServer->iPinClientDescriptors);
|
|
1575 |
if (r != KErrNone)
|
|
1576 |
goto error;
|
|
1577 |
|
|
1578 |
if (!iServer || iServer->IsClosing())
|
|
1579 |
{
|
|
1580 |
// Server went away while we weren't holding the system lock
|
|
1581 |
r = KErrServerTerminated;
|
|
1582 |
goto error;
|
|
1583 |
}
|
|
1584 |
}
|
|
1585 |
|
|
1586 |
// don't release system lock again after here until message is deliver/accepted
|
|
1587 |
// else session may have become detached
|
|
1588 |
|
|
1589 |
#ifdef BTRACE_CLIENT_SERVER
|
|
1590 |
BTraceContext12(BTrace::EClientServer,BTrace::EMessageSend,aMsg,aFunction,this);
|
|
1591 |
#endif
|
|
1592 |
|
|
1593 |
// NB: aStatus is NULL for blind messages
|
|
1594 |
r = aMsg->SetStatus(aStatus);
|
|
1595 |
__ASSERT_DEBUG(r == KErrNone, K::Fault(K::EMessageInUse));
|
|
1596 |
iServer->Deliver(aMsg);
|
|
1597 |
NKern::UnlockSystem();
|
|
1598 |
return r;
|
|
1599 |
|
|
1600 |
error:
|
|
1601 |
aMsg->CloseRef();
|
|
1602 |
if (panicReason)
|
|
1603 |
K::PanicCurrentThread(panicReason); // doesn't return!
|
|
1604 |
NKern::UnlockSystem();
|
|
1605 |
return r;
|
|
1606 |
}
|
|
1607 |
|
|
1608 |
|
|
1609 |
void ExecHandler::SetSessionPtr(RMessageK* aMsg, const TAny* aSessionCookie)
|
|
1610 |
//
|
|
1611 |
// Enter with system locked, return with system unlocked
|
|
1612 |
//
|
|
1613 |
{
|
|
1614 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SetSessionPtr"));
|
|
1615 |
|
|
1616 |
// Session cookie must not be set to NULL
|
|
1617 |
if (!aSessionCookie)
|
|
1618 |
K::PanicCurrentThread(ESessionNullCookie);
|
|
1619 |
|
|
1620 |
// Session cookie must be set from the connect message
|
|
1621 |
if (aMsg->iFunction != RMessage2::EConnect)
|
|
1622 |
K::PanicCurrentThread(ESessionInvalidCookieMsg);
|
|
1623 |
|
|
1624 |
DSession* pS = aMsg->iSession;
|
|
1625 |
|
|
1626 |
// Must not attempt to set the cookie more than once
|
|
1627 |
if (pS->iSessionCookie)
|
|
1628 |
K::PanicCurrentThread(ESessionCookieAlreadySet);
|
|
1629 |
|
|
1630 |
pS->iSessionCookie = aSessionCookie;
|
|
1631 |
|
|
1632 |
if (pS->IsClosing() && pS->iDisconnectMsgPtr->IsFree())
|
|
1633 |
{
|
|
1634 |
// NB: pS->iServer is set to NULL during CloseFromDisconnect(), called
|
|
1635 |
// when a disconnect message has been completed (and hence is free again),
|
|
1636 |
// so this cannot send the disconnect message again.
|
|
1637 |
__SendDiscMsg(pS);
|
|
1638 |
}
|
|
1639 |
NKern::UnlockSystem();
|
|
1640 |
}
|
|
1641 |
|
|
1642 |
void ExecHandler::TransferSession(RMessageK* aMsg, TInt aHandle)
|
|
1643 |
//
|
|
1644 |
// Enter and return with system locked
|
|
1645 |
//
|
|
1646 |
{
|
|
1647 |
__KTRACE_OPT(KEXEC, Kern::Printf("Exec::TransferSession"));
|
|
1648 |
|
|
1649 |
// Session transfer must done using the Connect message
|
|
1650 |
if (aMsg->iFunction != RMessage2::EConnect)
|
|
1651 |
K::PanicCurrentThread(ESessionInvalidCookieMsg);
|
|
1652 |
|
|
1653 |
// Session cookie must already have been set
|
|
1654 |
DSession* pSession = aMsg->iSession;
|
|
1655 |
if (pSession->iSessionCookie == NULL)
|
|
1656 |
K::PanicCurrentThread(ESessionNullCookie);
|
|
1657 |
|
|
1658 |
// Find the new server and transfer the session to it
|
|
1659 |
DServer* pServer = (DServer*)K::ObjectFromHandle(aHandle, EServer);
|
|
1660 |
pSession->Transfer(pServer, aMsg);
|
|
1661 |
|
|
1662 |
// This call also completes the original message with no error
|
|
1663 |
ExecHandler::MessageComplete(aMsg, KErrNone);
|
|
1664 |
}
|
|
1665 |
|
|
1666 |
void ExecHandler::ServerReceive(DServer* aServer, TRequestStatus& aStatus, TAny* aMsg)
|
|
1667 |
//
|
|
1668 |
// Enter and leave with system locked
|
|
1669 |
//
|
|
1670 |
{
|
|
1671 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ServerReceive"));
|
|
1672 |
aServer->Receive(aStatus, aMsg);
|
|
1673 |
}
|
|
1674 |
|
|
1675 |
void ExecHandler::ServerCancel(DServer* aServer)
|
|
1676 |
//
|
|
1677 |
// Enter and leave with system locked
|
|
1678 |
//
|
|
1679 |
{
|
|
1680 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ServerCancel"));
|
|
1681 |
aServer->Cancel();
|
|
1682 |
}
|
|
1683 |
|
|
1684 |
TInt ExecHandler::SessionSend(TInt aHandle, TInt aFunction, TAny* aPtr, TRequestStatus* aStatus)
|
|
1685 |
//
|
|
1686 |
// Enter with system locked, return with system unlocked.
|
|
1687 |
//
|
|
1688 |
{
|
|
1689 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionSend"));
|
|
1690 |
return DSession::Send(aHandle, aFunction, (const TInt*)aPtr, aStatus);
|
|
1691 |
}
|
|
1692 |
|
|
1693 |
TInt ExecHandler::SessionSendSync(TInt aHandle, TInt aFunction, TAny* aPtr, TRequestStatus* aStatus)
|
|
1694 |
//
|
|
1695 |
// Enter with system locked, return with system unlocked.
|
|
1696 |
//
|
|
1697 |
{
|
|
1698 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionSendSync"));
|
|
1699 |
return DSession::SendSync(aHandle, aFunction, (const TInt*)aPtr, aStatus);
|
|
1700 |
}
|
|
1701 |
|
|
1702 |
#ifndef __MESSAGE_MACHINE_CODED__
|
|
1703 |
void ExecHandler::MessageComplete(RMessageK* aMsg, TInt aReason)
|
|
1704 |
//
|
|
1705 |
// Enter and leave with system locked.
|
|
1706 |
//
|
|
1707 |
{
|
|
1708 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageComplete"));
|
|
1709 |
RMessageK& m = *aMsg;
|
|
1710 |
|
|
1711 |
#ifdef BTRACE_CLIENT_SERVER
|
|
1712 |
BTraceContext8(BTrace::EClientServer,BTrace::EMessageComplete,aMsg,aReason);
|
|
1713 |
#endif
|
|
1714 |
|
|
1715 |
DSession* s = m.iSession;
|
|
1716 |
|
|
1717 |
// First check for disconnect message
|
|
1718 |
if (m.iFunction == RMessage2::EDisConnect)
|
|
1719 |
{
|
|
1720 |
s->CloseFromDisconnect();
|
|
1721 |
return;
|
|
1722 |
}
|
|
1723 |
|
|
1724 |
// Note whether session connect message has completed
|
|
1725 |
if (m.iFunction == RMessage2::EConnect)
|
|
1726 |
s->iConnectMsgPtr = NULL;
|
|
1727 |
|
|
1728 |
__KTRACE_OPT(KIPC,Kern::Printf("MsgCo: M:%d r:%d %O->%O", m.iFunction, aReason, TheCurrentThread, m.iClient));
|
|
1729 |
if (!s->IsClosing())
|
|
1730 |
{
|
|
1731 |
m.SetCompleting();
|
|
1732 |
Kern::QueueRequestComplete(m.iClient, &m, aReason);
|
|
1733 |
}
|
|
1734 |
else
|
|
1735 |
{
|
|
1736 |
// Pending disconnect, is it a connect message?
|
|
1737 |
if(m.iFunction == RMessage2::EConnect)
|
|
1738 |
{
|
|
1739 |
// If a session has been created, then a disconnect message should already have been sent
|
|
1740 |
__ASSERT_DEBUG(!s->iSessionCookie || !s->iServer || !s->iDisconnectMsgPtr->IsFree(),
|
|
1741 |
K::Fault(K::EMsgCompleteDiscNotSent));
|
|
1742 |
|
|
1743 |
// if no server-side session object to clean up, send disconnect message
|
|
1744 |
// anyway to prevent message lifetime issues for the server for any other
|
|
1745 |
// messages on this unconnected session the server may have accepted
|
|
1746 |
if (!s->iSessionCookie)
|
|
1747 |
__SendDiscMsg(s);
|
|
1748 |
}
|
|
1749 |
m.Reset();
|
|
1750 |
m.CloseRef(); // Return message to appropriate pool
|
|
1751 |
}
|
|
1752 |
}
|
|
1753 |
#else
|
|
1754 |
#ifdef _DEBUG
|
|
1755 |
extern "C" void __FaultBadMsgPool()
|
|
1756 |
{
|
|
1757 |
K::Fault(K::EMsgFreeBadPool);
|
|
1758 |
}
|
|
1759 |
|
|
1760 |
extern "C" void __FaultMsgNotFree()
|
|
1761 |
{
|
|
1762 |
K::Fault(K::EMessageNotFree);
|
|
1763 |
}
|
|
1764 |
|
|
1765 |
extern "C" void __FaultMsgInUse()
|
|
1766 |
{
|
|
1767 |
K::Fault(K::EMessageInUse);
|
|
1768 |
}
|
|
1769 |
|
|
1770 |
extern "C" void __FaultMsgCompleteDiscNotSent()
|
|
1771 |
{
|
|
1772 |
K::Fault(K::EMsgCompleteDiscNotSent);
|
|
1773 |
}
|
|
1774 |
#endif
|
|
1775 |
|
|
1776 |
extern "C" void __PanicSyncMsgSentTwice()
|
|
1777 |
{
|
|
1778 |
K::PanicCurrentThread(ESyncMsgSentTwice);
|
|
1779 |
}
|
|
1780 |
|
|
1781 |
extern "C" void __PanicMesAlreadyPending()
|
|
1782 |
{
|
|
1783 |
K::PanicCurrentThread(EMesAlreadyPending);
|
|
1784 |
}
|
|
1785 |
|
|
1786 |
#endif
|
|
1787 |
|
|
1788 |
//
|
|
1789 |
// Enter and leave with system locked
|
|
1790 |
//
|
|
1791 |
extern "C" void __SendDiscMsg(DSession* aSession)
|
|
1792 |
{
|
|
1793 |
DServer* server = aSession->iServer;
|
|
1794 |
if (server && !server->IsClosing())
|
|
1795 |
{
|
|
1796 |
// Send the preallocated disconnect message, as the session is still attached
|
|
1797 |
// and the server isn't closing so it won't detach the session itself.
|
|
1798 |
RMessageK* disc = aSession->iDisconnectMsgPtr;
|
|
1799 |
__ASSERT_DEBUG(disc->iSession == aSession && disc->IsFree(),
|
|
1800 |
K::Fault(K::EMsgCompleteDiscNotSent));
|
|
1801 |
|
|
1802 |
#ifdef BTRACE_CLIENT_SERVER
|
|
1803 |
BTraceContext12(BTrace::EClientServer,BTrace::EMessageSend,disc,disc->iFunction,aSession);
|
|
1804 |
#endif
|
|
1805 |
disc->iSession = aSession;
|
|
1806 |
server->Deliver(disc);
|
|
1807 |
}
|
|
1808 |
}
|
|
1809 |
|
|
1810 |
TInt ExecHandler::SessionShare(TInt& aHandle, TInt aMode)
|
|
1811 |
//
|
|
1812 |
// Mutate a single-threaded session into a multi-threaded session
|
|
1813 |
//
|
|
1814 |
{
|
|
1815 |
if ( (TUint)aMode > (TUint)EIpcSession_GlobalSharable || aMode == EIpcSession_Unsharable)
|
|
1816 |
return KErrArgument;
|
|
1817 |
TInt oldHandle;
|
|
1818 |
TInt newHandle = 0;
|
|
1819 |
kumemget32(&oldHandle, &aHandle, sizeof(TInt));
|
|
1820 |
NKern::LockSystem();
|
|
1821 |
DSession* s = (DSession*)K::ObjectFromHandle(oldHandle, ESession);
|
|
1822 |
s->CheckedOpen();
|
|
1823 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionShare %O server %O mode %08x", s, s->iServer, aMode));
|
|
1824 |
TInt r = KErrNone;
|
|
1825 |
TUint ct = s->iSessionType;
|
|
1826 |
if (s->iSvrSessionType == EIpcSession_Unsharable)
|
|
1827 |
{
|
|
1828 |
r = EUnsharableSession;
|
|
1829 |
goto end;
|
|
1830 |
}
|
|
1831 |
if ((TUint)s->iSvrSessionType < (TUint)aMode)
|
|
1832 |
{
|
|
1833 |
r = KErrPermissionDenied;
|
|
1834 |
goto end;
|
|
1835 |
}
|
|
1836 |
if (aMode == EIpcSession_GlobalSharable)
|
|
1837 |
s->SetProtection(DObject::EProtected);
|
|
1838 |
if (ct >= (TUint)aMode)
|
|
1839 |
goto end; // nothing to do
|
|
1840 |
s->iSessionType = (TUint8)aMode;
|
|
1841 |
if (ct >= EIpcSession_Sharable)
|
|
1842 |
goto end; // nothing more to do
|
|
1843 |
K::ThreadEnterCS();
|
|
1844 |
r = K::MakeHandle(EOwnerProcess, s);
|
|
1845 |
if (r>=0)
|
|
1846 |
{
|
|
1847 |
newHandle = r;
|
|
1848 |
r = KErrNone;
|
|
1849 |
K::HandleClose(oldHandle);
|
|
1850 |
if (Kern::KUSafeWrite(&aHandle, &newHandle, sizeof(newHandle))) // don't let thread die before updating handle
|
|
1851 |
r = ECausedException;
|
|
1852 |
}
|
|
1853 |
if (r==KErrNone)
|
|
1854 |
{
|
|
1855 |
NKern::ThreadLeaveCS();
|
|
1856 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionShare returns %d",r));
|
|
1857 |
return r;
|
|
1858 |
}
|
|
1859 |
goto end2;
|
|
1860 |
end:
|
|
1861 |
K::ThreadEnterCS();
|
|
1862 |
end2:
|
|
1863 |
s->Close(NULL);
|
|
1864 |
NKern::ThreadLeaveCS();
|
|
1865 |
if (r>0)
|
|
1866 |
K::PanicKernExec(r);
|
|
1867 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionShare returns %d",r));
|
|
1868 |
return r;
|
|
1869 |
}
|
|
1870 |
|
|
1871 |
#ifndef __MESSAGE_MACHINE_CODED_2__
|
|
1872 |
void ExecHandler::MessageConstructFromPtr(RMessageK* aMsgK, TAny* aMsgU)
|
|
1873 |
{
|
|
1874 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageConstructFromPtr"));
|
|
1875 |
RMessageU2 umsg(*aMsgK);
|
|
1876 |
NKern::UnlockSystem();
|
|
1877 |
kumemput32(aMsgU, &umsg, sizeof(umsg));
|
|
1878 |
}
|
|
1879 |
#endif
|
|
1880 |
|
|
1881 |
TInt ExecHandler::MessageGetDesLength(RMessageK* aMsg, TInt aParam)
|
|
1882 |
{
|
|
1883 |
__KTRACE_OPT(KEXEC, Kern::Printf("Exec::MessageGetDesLength"));
|
|
1884 |
if (TUint(aParam) >= TUint(KMaxMessageArguments))
|
|
1885 |
return KErrArgument;
|
|
1886 |
if (!aMsg->IsDescriptor(aParam))
|
|
1887 |
return KErrBadDescriptor;
|
|
1888 |
return aMsg->DesLength(aParam);
|
|
1889 |
}
|
|
1890 |
|
|
1891 |
TInt ExecHandler::MessageGetDesMaxLength(RMessageK* aMsg, TInt aParam)
|
|
1892 |
{
|
|
1893 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageGetDesMaxLength"));
|
|
1894 |
if (TUint(aParam) >= TUint(KMaxMessageArguments))
|
|
1895 |
return KErrArgument;
|
|
1896 |
if (!aMsg->IsDescriptor(aParam))
|
|
1897 |
return KErrBadDescriptor;
|
|
1898 |
return aMsg->DesMaxLength(aParam);
|
|
1899 |
}
|
|
1900 |
|
|
1901 |
const TRequestStatus* ExecHandler::MessageClientStatus(RMessageK* aMsg)
|
|
1902 |
{
|
|
1903 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageClientStatus"));
|
|
1904 |
if (aMsg->iMsgType == RMessageK::ESync)
|
|
1905 |
return NULL;
|
|
1906 |
return aMsg->StatusPtr();
|
|
1907 |
}
|
|
1908 |
|
|
1909 |
#ifndef __REMOVE_PLATSEC_DIAGNOSTICS__
|
|
1910 |
#define __PSIF(msg, diag) PlatSec::ProcessIsolationIPCFail(msg, diag)
|
|
1911 |
#else //__REMOVE_PLATSEC_DIAGNOSTICS__
|
|
1912 |
#define __PSIF(msg, diag) PlatSec::EmitDiagnostic()
|
|
1913 |
#endif // !__REMOVE_PLATSEC_DIAGNOSTICS__
|
|
1914 |
|
|
1915 |
TInt ExecHandler::MessageIpcCopy(RMessageK* aMsg, TInt aParam, SIpcCopyInfo& aInfo, TInt aOffset)
|
|
1916 |
{
|
|
1917 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageIpcCopy flags=%08x",aInfo.iFlags));
|
|
1918 |
if (TUint(aParam) >= TUint(KMaxMessageArguments))
|
|
1919 |
return KErrArgument;
|
|
1920 |
if (!aMsg->IsDescriptor(aParam) || !aMsg->Descriptor(aParam).IsSet())
|
|
1921 |
{
|
|
1922 |
__PSIF(aMsg, __PLATSEC_DIAGNOSTIC_STRING("Server attempted to use RMessagePtr2::Read/Write on a non descriptor message argument"));
|
|
1923 |
return KErrBadDescriptor;
|
|
1924 |
}
|
|
1925 |
|
|
1926 |
TInt argType = aMsg->ArgType(aParam);
|
|
1927 |
if ((aInfo.iFlags & KIpcDirWrite) && (argType & TIpcArgs::EFlagConst))
|
|
1928 |
if (__PSIF(aMsg, __PLATSEC_DIAGNOSTIC_STRING("Server attempted to use RMessagePtr2::Write on a const client descriptor argument")))
|
|
1929 |
return KErrBadDescriptor;
|
|
1930 |
if ((aInfo.iFlags & KChunkShiftBy1) && !(argType & TIpcArgs::EFlag16Bit))
|
|
1931 |
if (__PSIF(aMsg, __PLATSEC_DIAGNOSTIC_STRING("Server attempted to use 16bit RMessagePtr2::Read/Write on a 8 bit client descriptor argument")))
|
|
1932 |
return KErrBadDescriptor;
|
|
1933 |
if ((argType & TIpcArgs::EFlag16Bit) && !(aInfo.iFlags & KChunkShiftBy1))
|
|
1934 |
if (__PSIF(aMsg, __PLATSEC_DIAGNOSTIC_STRING("Server attempted to use 8bit RMessagePtr2::Read/Write on a 16 bit client descriptor argument")))
|
|
1935 |
return KErrBadDescriptor;
|
|
1936 |
|
|
1937 |
DThread& t = *TheCurrentThread;
|
|
1938 |
t.iTempMsg = aMsg;
|
|
1939 |
aMsg->OpenRef();
|
|
1940 |
|
|
1941 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
1942 |
NKern::FlashSystem();
|
|
1943 |
#else
|
|
1944 |
NKern::UnlockSystem();
|
|
1945 |
#endif
|
|
1946 |
|
|
1947 |
TInt r = KErrNone;
|
|
1948 |
DThread* pT = aMsg->iClient;
|
|
1949 |
TInt mode = (aInfo.iFlags & KChunkShiftBy1) | KCheckLocalAddress | KDoNotUpdateDesLength; // assume called from user mode
|
|
1950 |
|
|
1951 |
if ((aInfo.iFlags & KIpcDirWrite) == 0)
|
|
1952 |
{
|
|
1953 |
r = pT->DoDesRead(aMsg->Descriptor(aParam), aInfo.iLocalPtr, aInfo.iLocalLen, aOffset, mode);
|
|
1954 |
}
|
|
1955 |
else
|
|
1956 |
{
|
|
1957 |
TAny* ptr = aMsg->Ptr(aParam);
|
|
1958 |
r = pT->DoDesWrite(ptr, aMsg->Descriptor(aParam), aInfo.iLocalPtr, aInfo.iLocalLen, aOffset, mode, NULL);
|
|
1959 |
if (r >= 0)
|
|
1960 |
{
|
|
1961 |
// Update cached flags + length word for all matching descriptors and set written bit
|
|
1962 |
TInt descFlags = aMsg->iMsgArgs.AllDescriptorFlags();
|
|
1963 |
for (TInt i = 0; descFlags != 0; ++i, descFlags >>= TIpcArgs::KBitsPerType)
|
|
1964 |
if ((descFlags & TIpcArgs::EFlagDes) && aMsg->Ptr(i) == ptr)
|
|
1965 |
{
|
|
1966 |
aMsg->Descriptor(i).SetTypeAndLength(r);
|
|
1967 |
aMsg->iMsgArgs.SetDesWritten(i);
|
|
1968 |
}
|
|
1969 |
r = KErrNone;
|
|
1970 |
}
|
|
1971 |
}
|
|
1972 |
|
|
1973 |
#ifndef __MEMMODEL_FLEXIBLE__
|
|
1974 |
#else
|
|
1975 |
NKern::LockSystem();
|
|
1976 |
#endif
|
|
1977 |
|
|
1978 |
__NK_ASSERT_DEBUG(aMsg->IsAccepted() || aMsg->IsCompleting());
|
|
1979 |
t.iTempMsg = NULL;
|
|
1980 |
aMsg->CloseRef();
|
|
1981 |
return r;
|
|
1982 |
}
|
|
1983 |
|
|
1984 |
TInt ExecHandler::MessageClient(DThread* aClient, TOwnerType aType)
|
|
1985 |
{
|
|
1986 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageClient"));
|
|
1987 |
NKern::ThreadEnterCS();
|
|
1988 |
TInt r=aClient->Open();
|
|
1989 |
NKern::UnlockSystem();
|
|
1990 |
if (r==KErrNone)
|
|
1991 |
{
|
|
1992 |
r=K::MakeHandle(aType,aClient);
|
|
1993 |
if (r<KErrNone)
|
|
1994 |
aClient->Close(NULL);
|
|
1995 |
}
|
|
1996 |
NKern::ThreadLeaveCS();
|
|
1997 |
return r;
|
|
1998 |
}
|
|
1999 |
|
|
2000 |
TInt ExecHandler::MessageSetProcessPriority(DThread* aClient, TProcessPriority aPriority)
|
|
2001 |
{
|
|
2002 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageSetProcessPriority"));
|
|
2003 |
DProcess* pP = aClient->iOwningProcess;
|
|
2004 |
if (pP->iFlags & KProcessFlagPriorityControl)
|
|
2005 |
if (aPriority==EPriorityBackground || aPriority==EPriorityForeground)
|
|
2006 |
{
|
|
2007 |
ExecHandler::ProcessSetPriority(pP,aPriority);
|
|
2008 |
return KErrNone;
|
|
2009 |
}
|
|
2010 |
NKern::UnlockSystem();
|
|
2011 |
return KErrPermissionDenied;
|
|
2012 |
}
|
|
2013 |
|
|
2014 |
void GetCategory(TDes& aDest, const TDesC& aSrc); // in server.cpp
|
|
2015 |
|
|
2016 |
void ExecHandler::MessageKill(TInt aHandle, TExitType aType, TInt aReason, const TDesC* aCategory)
|
|
2017 |
{
|
|
2018 |
TBuf<KMaxExitCategoryName> cat;
|
|
2019 |
if (aType==EExitPanic && aCategory)
|
|
2020 |
GetCategory(cat,*aCategory);
|
|
2021 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageKill %d,%d,%lS",aType,aReason,&cat));
|
|
2022 |
K::CheckKernelUnlocked();
|
|
2023 |
NKern::LockSystem();
|
|
2024 |
RMessageK* pM = RMessageK::MessageK(aHandle);
|
|
2025 |
DThread* pT = pM->iClient;
|
|
2026 |
pT->Die(aType,aReason,cat); // releases system lock
|
|
2027 |
}
|
|
2028 |
|
|
2029 |
TInt ExecHandler::MessageOpenObject(RMessageK* aMsg, TObjectType aObjType, TInt aParam, TOwnerType aOwnerType)
|
|
2030 |
{
|
|
2031 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageOpenObject"));
|
|
2032 |
NKern::ThreadEnterCS();
|
|
2033 |
DObject* pO;
|
|
2034 |
pO=0; // To shut up the compiler
|
|
2035 |
TInt r;
|
|
2036 |
if ( TUint(aParam)>=TUint(KMaxMessageArguments) )
|
|
2037 |
{
|
|
2038 |
r = KErrArgument;
|
|
2039 |
goto done;
|
|
2040 |
}
|
|
2041 |
r = KErrBadHandle;
|
|
2042 |
if ( aMsg->ArgType(aParam)!=TIpcArgs::EHandle )
|
|
2043 |
goto done;
|
|
2044 |
pO=aMsg->iClient->ObjectFromHandle(aMsg->Arg(aParam),aObjType);
|
|
2045 |
if (pO)
|
|
2046 |
if(pO->Protection()!=DObject::ELocal)
|
|
2047 |
r=pO->Open();
|
|
2048 |
done:
|
|
2049 |
NKern::UnlockSystem();
|
|
2050 |
if (r==KErrNone)
|
|
2051 |
{
|
|
2052 |
r=K::MakeHandle(aOwnerType,pO); // this will add to process if necessary
|
|
2053 |
if (r<KErrNone)
|
|
2054 |
pO->Close(NULL); // can't have been added to process so NULL
|
|
2055 |
}
|
|
2056 |
NKern::ThreadLeaveCS();
|
|
2057 |
return r;
|
|
2058 |
}
|
|
2059 |
|
|
2060 |
void ExecHandler::MessageCompleteWithHandle(RMessageK* aMsg, TInt aHandle)
|
|
2061 |
//
|
|
2062 |
// Enter and leave with system locked.
|
|
2063 |
//
|
|
2064 |
{
|
|
2065 |
DObject* pO = K::ObjectFromHandle(aHandle);
|
|
2066 |
pO->CheckedOpen();
|
|
2067 |
DThread* pT=aMsg->iClient;
|
|
2068 |
TInt r = pT->Open();
|
|
2069 |
K::ThreadEnterCS();
|
|
2070 |
TInt h;
|
|
2071 |
if(r==KErrNone)
|
|
2072 |
{
|
|
2073 |
// always EOwnerThreads so it doesn't leak if client thread dies
|
|
2074 |
r = pT->MakeHandleAndOpen(EOwnerThread,pO,h);
|
|
2075 |
if (r==KErrNone)
|
|
2076 |
r = h;
|
|
2077 |
pT->Close(NULL);
|
|
2078 |
}
|
|
2079 |
pO->Close(NULL);
|
|
2080 |
K::ThreadLeaveCS();
|
|
2081 |
RMessageK::MessageK((TInt)aMsg);
|
|
2082 |
ExecHandler::MessageComplete(aMsg,r);
|
|
2083 |
}
|