author | Mike Kinghan <mikek@symbian.org> |
Sun, 18 Jul 2010 10:20:49 +0100 | |
branch | GCC_SURGE |
changeset 206 | ced41fd9a298 |
parent 102 | ef2a444a7410 |
child 152 | 657f875b013e |
permissions | -rw-r--r-- |
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; |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
549 |
TBool anyPins = EFalse; |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
550 |
TPinArray pinArray = { { 0, 0, 0, 0 } }; // local, copy to heap later if used |
0 | 551 |
|
552 |
for (TInt i = 0; descFlags != 0; ++i, argPinFlags >>= 1, descFlags >>= TIpcArgs::KBitsPerType) |
|
553 |
{ |
|
554 |
__NK_ASSERT_DEBUG(i < KMaxMessageArguments); // Should stop after max args processed. |
|
555 |
||
556 |
// Is this arg a descriptor that should be pinned? |
|
557 |
if ((descFlags & TIpcArgs::EFlagDes) && (argPinFlags & 1)) |
|
558 |
{ |
|
559 |
TDesHeader& desInfo = Descriptor(i); |
|
560 |
||
561 |
// Pin the max length for modifiable descriptors, but only |
|
562 |
// the current length for non-modifiable descriptors. |
|
563 |
TUint pinLength = desInfo.IsWriteable() ? desInfo.MaxLength() : desInfo.Length(); |
|
564 |
if (pinLength) |
|
565 |
{ |
|
566 |
// This will only create and pin if the descriptor data is paged. |
|
567 |
// An out-of-memory error here means we fail the whole operation. |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
568 |
r = Kern::CreateAndPinVirtualMemory(pinArray.iPinPtrs[i], desInfo.DataPtr(), pinLength); |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
569 |
if (pinArray.iPinPtrs[i]) |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
570 |
anyPins = ETrue; |
0 | 571 |
if (r == KErrNoMemory) |
572 |
break; |
|
573 |
if (r != KErrNone) |
|
574 |
{ |
|
575 |
// For any other error, clear the parameter type and mark the |
|
576 |
// descriptor header as invalid, so the server will see a problem |
|
577 |
// on access, but then suppress the error so we can continue ... |
|
578 |
iMsgArgs.SetArgUndefined(i); |
|
579 |
desInfo.Unset(); |
|
580 |
r = KErrNone; |
|
581 |
} |
|
582 |
} |
|
583 |
} |
|
584 |
} |
|
585 |
||
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
586 |
if (anyPins && r != KErrNoMemory) |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
587 |
{ |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
588 |
iPinArray = new TPinArray (pinArray); |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
589 |
if (!iPinArray) |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
590 |
r = KErrNoMemory; |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
591 |
} |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
592 |
|
0 | 593 |
if (r == KErrNoMemory) |
594 |
{ |
|
595 |
// Failed to pin everything so clean up any pin objects created. |
|
596 |
// This will also unpin any pinned memory. |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
597 |
UnpinMessageArguments(&pinArray); |
0 | 598 |
} |
599 |
||
600 |
NKern::LockSystem(); |
|
601 |
||
602 |
// Remove the access on the session. |
|
603 |
if (aSession->TotalAccessDec() == DObject::EObjectDeleted) |
|
36
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
604 |
{ |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
605 |
// This was the last access on the session and it has been deleted |
538db54a451d
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
31
diff
changeset
|
606 |
// so don't access any of its members. |
0 | 607 |
r = KErrDisconnected; |
608 |
} |
|
609 |
NKern::ThreadLeaveCS(); |
|
610 |
return r; |
|
611 |
} |
|
612 |
||
613 |
// Free a single message back to the pool it belongs in |
|
614 |
// Enter and leave with system lock, may be temporarily released though |
|
615 |
void RMessageK::Free() |
|
616 |
{ |
|
617 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED, "RMessageK::Free"); |
|
618 |
__KTRACE_OPT(KSERVER, Kern::Printf("RMessageK(%08x)::Free(), type %d", this, iMsgType)); |
|
619 |
__ASSERT_SYSTEM_LOCK; |
|
620 |
__NK_ASSERT_DEBUG(!IsFree()); |
|
621 |
__NK_ASSERT_DEBUG(State()==EFree); // TClientRequest should be free |
|
622 |
__NK_ASSERT_DEBUG(iAccessCount==0); |
|
623 |
||
624 |
DThread* closeThread = 0; |
|
625 |
if (iSession) |
|
626 |
{ |
|
627 |
--iSession->iMsgCount; |
|
628 |
iSessionLink.Deque(); |
|
629 |
if (--iClient->iIpcCount == 0x80000000u) |
|
630 |
closeThread = iClient; |
|
631 |
} |
|
632 |
||
633 |
// take ownership of any pin objects... |
|
634 |
TPinArray* pinArray = iPinArray; |
|
635 |
iPinArray = NULL; |
|
636 |
||
637 |
// free the message... |
|
638 |
SetFree(); |
|
639 |
switch (iMsgType) |
|
640 |
{ |
|
641 |
default: |
|
642 |
case EDisc: |
|
643 |
// The Disconnect message is owned by the session and should never be freed |
|
644 |
K::Fault(K::EMsgFreeBadPool); |
|
645 |
break; |
|
646 |
||
647 |
case ESync: |
|
648 |
// The Synchronous message is owned by the thread, so we don't actually free it |
|
649 |
break; |
|
650 |
||
651 |
case ESession: |
|
652 |
if (iSession) |
|
653 |
{ |
|
654 |
// Put this message back on the session's freelist |
|
655 |
iSessionLink.iNext = (SDblQueLink*)iSession->iNextFreeMessage; |
|
656 |
iSession->iNextFreeMessage = this; |
|
657 |
break; |
|
658 |
} |
|
659 |
// Session has gone away; return message to global pool instead |
|
660 |
iMsgType = EGlobal; |
|
661 |
/*FALLTHRU*/ |
|
662 |
case EGlobal: |
|
663 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool)); |
|
664 |
// Put this message back on the global freelist |
|
665 |
iSessionLink.iNext = (SDblQueLink*)K::MsgInfo.iNextMessage; |
|
666 |
K::MsgInfo.iNextMessage = this; |
|
667 |
K::MsgInfo.iFreeMessageCount += 1; |
|
668 |
__ASSERT_DEBUG(MessagePoolIntact(), K::Fault(K::EMsgFreeBadPool)); |
|
669 |
break; |
|
670 |
} |
|
671 |
||
672 |
if (closeThread) |
|
673 |
{ |
|
674 |
K::ThreadEnterCS(); |
|
675 |
closeThread->AsyncClose(); |
|
676 |
K::ThreadLeaveCS(); |
|
677 |
} |
|
678 |
||
679 |
if (pinArray) |
|
680 |
{ |
|
681 |
// unpin any pinned descriptors and free the pin objects... |
|
682 |
K::ThreadEnterCS(); |
|
683 |
UnpinMessageArguments(pinArray); |
|
684 |
delete pinArray; |
|
685 |
K::ThreadLeaveCS(); |
|
686 |
} |
|
687 |
} |
|
688 |
||
689 |
EXPORT_C DThread* RMessageK::Thread() const |
|
690 |
{ |
|
691 |
return iClient; |
|
692 |
} |
|
693 |
||
694 |
||
695 |
/******************************************** |
|
696 |
* Server control block |
|
697 |
********************************************/ |
|
698 |
DServer::DServer() |
|
699 |
{ |
|
700 |
} |
|
701 |
||
702 |
TInt DServer::Create() |
|
703 |
{ |
|
704 |
iMessage = new TServerMessage; |
|
705 |
return iMessage ? KErrNone : KErrNoMemory; |
|
706 |
} |
|
707 |
||
708 |
DServer::~DServer() |
|
709 |
// |
|
710 |
// Destructor |
|
711 |
// Note that no-one else will try to access this server in here, since |
|
712 |
// the access count will now be zero. |
|
713 |
// |
|
714 |
{ |
|
715 |
__KTRACE_OPT(KSERVER,Kern::Printf("DServer %O Destruct", this)); |
|
716 |
__ASSERT_ALWAYS(iSessionQ.IsEmpty(), K::Fault(K::EServerDestructSessionsRemain)); |
|
717 |
__ASSERT_ALWAYS(iDeliveredQ.IsEmpty(), K::Fault(K::EServerDestructMessagesRemain)); |
|
718 |
if (iMessage) |
|
719 |
iMessage->Close(); |
|
720 |
Kern::SafeClose((DObject*&)iOwningThread,NULL); |
|
721 |
} |
|
722 |
||
723 |
TInt DServer::Close(TAny*) |
|
724 |
{ |
|
725 |
__KTRACE_OPT(KSERVER,Kern::Printf("DServer %O Close AC=%d", this, AccessCount())); |
|
726 |
TInt r = Dec(); |
|
727 |
if (r==1) |
|
728 |
{ |
|
729 |
if (iOwningThread) |
|
730 |
{ |
|
731 |
Kern::QueueRequestComplete(iOwningThread, iMessage, KErrCancel); |
|
732 |
NKern::LockSystem(); |
|
733 |
for (; !iSessionQ.IsEmpty(); NKern::FlashSystem()) |
|
734 |
{ |
|
735 |
DSession* s = _LOFF(iSessionQ.First(), DSession, iServerLink); |
|
736 |
s->Detach(KErrServerTerminated); |
|
737 |
} |
|
738 |
__ASSERT_ALWAYS(iDeliveredQ.IsEmpty(), K::Fault(K::EServerCloseLeftoverMsg)); |
|
739 |
#ifdef BTRACE_CLIENT_SERVER |
|
740 |
BTrace4(BTrace::EClientServer,BTrace::EServerDestroy,this); |
|
741 |
#endif |
|
742 |
NKern::UnlockSystem(); |
|
743 |
} |
|
744 |
K::ObjDelete(this); |
|
745 |
return EObjectDeleted; |
|
746 |
} |
|
747 |
return 0; |
|
748 |
} |
|
749 |
||
750 |
#ifndef __MESSAGE_MACHINE_CODED__ |
|
751 |
void DServer::Receive(TRequestStatus& aStatus, TAny* aMessage) |
|
752 |
// |
|
753 |
// Receive a message asynchronously. |
|
754 |
// Enter and leave with system locked |
|
755 |
// |
|
756 |
{ |
|
757 |
||
758 |
if (iMessage->SetStatus(&aStatus) != KErrNone) |
|
759 |
K::PanicCurrentThread(EMesAlreadyPending); |
|
760 |
iMessage->iMessagePtr = aMessage; |
|
761 |
if (!iDeliveredQ.IsEmpty()) |
|
762 |
{ |
|
763 |
RMessageK* m = _LOFF(iDeliveredQ.First()->Deque(), RMessageK, iServerLink); |
|
764 |
Accept(m); |
|
765 |
} |
|
766 |
} |
|
767 |
#endif |
|
768 |
||
769 |
void DServer::Cancel() |
|
770 |
// |
|
771 |
// Cancel a message receive request. |
|
772 |
// Enter and leave with system locked |
|
773 |
// |
|
774 |
{ |
|
775 |
Kern::QueueRequestComplete(iOwningThread, iMessage, KErrCancel); |
|
776 |
} |
|
777 |
||
778 |
#ifndef __MESSAGE_MACHINE_CODED__ |
|
779 |
void DServer::Accept(RMessageK* aMsg) |
|
780 |
// |
|
781 |
// Accepts a message, assumes one is pending. |
|
782 |
// Enter and leave with system locked |
|
783 |
// |
|
784 |
{ |
|
785 |
// set the ACCEPTED state |
|
786 |
aMsg->SetAccepted(iOwningThread->iOwningProcess); |
|
787 |
if (iOwningThread->iMState==DThread::EDead) |
|
788 |
return; // server has already died |
|
789 |
#ifdef BTRACE_CLIENT_SERVER |
|
790 |
BTrace4(BTrace::EClientServer,BTrace::EMessageReceive,aMsg); |
|
791 |
#endif |
|
792 |
iMessage->iMessageData = aMsg; |
|
793 |
#ifdef KIPC |
|
794 |
if (KDebugNum(KIPC)) |
|
795 |
{ |
|
796 |
TInt f = aMsg->iFunction; |
|
797 |
if (f == RMessage2::EDisConnect) |
|
798 |
Kern::Printf("MsgAcD: %O->%O", aMsg->iSession, this); |
|
799 |
else |
|
800 |
Kern::Printf("MsgAc: M:%d %O->%O", f, aMsg->iClient, this); |
|
801 |
} |
|
802 |
#endif //KIPC |
|
803 |
Kern::QueueRequestComplete(iOwningThread, iMessage, KErrNone); |
|
804 |
} |
|
805 |
||
806 |
void DServer::Deliver(RMessageK* aMsg) |
|
807 |
// |
|
808 |
// Delivers a message to the server. |
|
809 |
// Enter and leave with system locked |
|
810 |
// |
|
811 |
{ |
|
812 |
if (iMessage->IsReady()) |
|
813 |
Accept(aMsg); |
|
814 |
else |
|
815 |
aMsg->SetDelivered(iDeliveredQ); |
|
816 |
} |
|
817 |
#endif //__MESSAGE_MACHINE_CODED__ |
|
818 |
||
819 |
TInt DServer::RequestUserHandle(DThread* aThread, TOwnerType aType) |
|
820 |
{ |
|
821 |
(void)aType; |
|
822 |
return (aThread->iOwningProcess==iOwningThread->iOwningProcess) ? KErrNone : KErrPermissionDenied; |
|
823 |
} |
|
824 |
||
825 |
TInt ExecHandler::ServerCreateWithOptions(const TDesC8* aName, TInt aMode, TInt aRole, TInt aOpts) |
|
826 |
// |
|
827 |
// Create a server belonging to the current thread. UNPROTECTED exec call. |
|
828 |
// |
|
829 |
{ |
|
830 |
DThread* t = TheCurrentThread; |
|
831 |
TInt r = KErrNone; |
|
832 |
TKName n; |
|
833 |
if (aName) |
|
834 |
Kern::KUDesGet(n, *aName); |
|
835 |
__KTRACE_OPT(KEXEC, Kern::Printf("Exec::ServerCreate %S", &n)); |
|
836 |
TInt nameLen = n.Length(); |
|
837 |
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"))) |
|
838 |
K::UnlockedPlatformSecurityPanic(); |
|
839 |
||
840 |
// Fully decode & validate the arguments before allocating the server object |
|
841 |
switch (aMode) |
|
842 |
{ |
|
843 |
case EIpcSession_Unsharable: |
|
844 |
case EIpcSession_Sharable: |
|
845 |
case EIpcSession_GlobalSharable: |
|
846 |
break; |
|
847 |
default: |
|
848 |
r = KErrArgument; |
|
849 |
break; |
|
850 |
} |
|
851 |
||
852 |
switch (aRole) |
|
853 |
{ |
|
854 |
case EServerRole_Default: |
|
855 |
aRole = EServerRole_Standalone; |
|
856 |
break; |
|
857 |
case EServerRole_Standalone: |
|
858 |
case EServerRole_Master: |
|
859 |
case EServerRole_Slave: |
|
860 |
break; |
|
861 |
default: |
|
862 |
r = KErrArgument; |
|
863 |
break; |
|
864 |
} |
|
865 |
||
866 |
TUint pinMode = (aOpts & EServerOpt_PinClientDescriptorsMask); |
|
867 |
aOpts &= ~EServerOpt_PinClientDescriptorsMask; |
|
868 |
switch (pinMode) |
|
869 |
{ |
|
870 |
case EServerOpt_PinClientDescriptorsDisable: |
|
871 |
pinMode = EFalse; |
|
872 |
break; |
|
873 |
case EServerOpt_PinClientDescriptorsEnable: |
|
874 |
pinMode = ETrue; |
|
875 |
break; |
|
876 |
case EServerOpt_PinClientDescriptorsDefault: |
|
877 |
pinMode = EFalse; |
|
878 |
if (K::MemModelAttributes & EMemModelAttrDataPaging) |
|
879 |
if (!(t->iOwningProcess->iAttributes & DProcess::EDataPaged)) |
|
880 |
{ |
|
881 |
// If the platform supports data-paging, and the server process |
|
882 |
// *isn't* data-paged, then enable descriptor pinning by default |
|
883 |
#if 0 |
|
884 |
// Worried about the impact of this causing new failure modes in |
|
885 |
// existing code - for now we are not going to turn on pinning by |
|
886 |
// default for unpaged servers. |
|
887 |
pinMode = ETrue; |
|
888 |
#endif |
|
889 |
} |
|
890 |
break; |
|
891 |
default: |
|
892 |
r = KErrArgument; |
|
893 |
break; |
|
894 |
} |
|
895 |
||
896 |
if (aOpts) |
|
897 |
r = KErrArgument; |
|
898 |
if (r != KErrNone) |
|
899 |
return r; // An invalid server mode, role or option was specified. |
|
900 |
||
901 |
r = KErrNoMemory; |
|
902 |
NKern::ThreadEnterCS(); |
|
903 |
DServer *pS = new DServer; |
|
904 |
if (pS) |
|
905 |
{ |
|
906 |
r = pS->Create(); |
|
907 |
if (r == KErrNone) |
|
908 |
{ |
|
909 |
t->Open(); |
|
910 |
pS->iOwningThread = t; |
|
911 |
pS->iSessionType = (TUint8)aMode; |
|
912 |
pS->iServerRole = (TUint8)aRole; |
|
913 |
pS->iPinClientDescriptors = (TUint8)pinMode; |
|
914 |
if (nameLen) |
|
915 |
r = pS->SetName(&n); |
|
916 |
#ifdef BTRACE_CLIENT_SERVER |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
917 |
BTraceN(BTrace::EClientServer, BTrace::EServerCreate, pS, pS->iOwningThread, n.Ptr(), n.Size()); |
0 | 918 |
#endif |
919 |
} |
|
920 |
if (r == KErrNone) |
|
921 |
{ |
|
922 |
r = K::AddObject(pS, EServer); |
|
923 |
if (r == KErrNone) |
|
924 |
r = K::MakeHandle(nameLen ? EOwnerThread : EOwnerProcess, pS); |
|
925 |
} |
|
926 |
if (r < KErrNone) |
|
927 |
pS->Close(NULL); |
|
928 |
} |
|
929 |
NKern::ThreadLeaveCS(); |
|
930 |
__KTRACE_OPT(KEXEC, Kern::Printf("Exec::ServerCreate returns %d", r)); |
|
931 |
return r; |
|
932 |
} |
|
933 |
||
934 |
TInt ExecHandler::ServerCreate(const TDesC8* aName, TInt aMode) |
|
935 |
{ |
|
936 |
return ServerCreateWithOptions(aName, aMode, EServerRole_Default, 0); |
|
937 |
} |
|
938 |
||
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
939 |
void DServer::BTracePrime(TInt aCategory) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
940 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
941 |
#ifdef BTRACE_CLIENT_SERVER |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
942 |
if (aCategory == BTrace::EClientServer || aCategory == -1) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
943 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
944 |
TKName nameBuf; |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
945 |
Name(nameBuf); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
946 |
BTraceN(BTrace::EClientServer, BTrace::EServerCreate, this, iOwningThread, nameBuf.Ptr(), nameBuf.Size()); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
947 |
} |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
948 |
#endif |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
949 |
} |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
950 |
|
0 | 951 |
|
952 |
/******************************************** |
|
953 |
* Session control block |
|
954 |
********************************************/ |
|
955 |
DSession::DSession() |
|
956 |
: iTotalAccessCount(1), iMsgLimit(KMaxMsgLimit) |
|
957 |
{ |
|
958 |
} |
|
959 |
||
960 |
DSession::~DSession() |
|
961 |
// |
|
962 |
// Destroy |
|
963 |
// |
|
964 |
{ |
|
965 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::Destruct")); |
|
966 |
||
967 |
// DObject access count and total access count should both be zero |
|
968 |
__ASSERT_ALWAYS(AccessCount()==0 && iTotalAccessCount==0, K::Fault(K::ESessionDestructStillRef)); |
|
969 |
||
970 |
// session should already have been unlinked from server |
|
971 |
__ASSERT_ALWAYS(!iServer, K::Fault(K::ESessionDestruct)); |
|
972 |
||
973 |
// there should be no messages outstanding on this session |
|
974 |
__ASSERT_ALWAYS(iMsgCount==0, K::Fault(K::ESessionDestructMsgCount)); |
|
975 |
__ASSERT_ALWAYS(iMsgQ.IsEmpty(), K::Fault(K::ESessionDestructMsgQ)); |
|
976 |
||
977 |
if (iDisconnectMsgPtr) |
|
978 |
{ |
|
979 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::~DSession(%08X) releasing disconnect message at %08X", this, iDisconnectMsgPtr)); |
|
980 |
iDisconnectMsgPtr->ReleaseMessagePool(RMessageK::EDisc, 1); |
|
981 |
} |
|
982 |
iDisconnectMsgPtr = NULL; |
|
983 |
if (iNextFreeMessage) |
|
984 |
{ |
|
985 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::~DSession(%08X) releasing session pool at %08X", this, iNextFreeMessage)); |
|
986 |
__ASSERT_DEBUG(iHasSessionPool, K::Fault(K::ESessionDestructMsgCount)); |
|
987 |
__ASSERT_DEBUG(iPoolSize > 0, K::Fault(K::ESessionDestructMsgCount)); |
|
988 |
iNextFreeMessage->ReleaseMessagePool(RMessageK::ESession, iPoolSize); |
|
989 |
} |
|
990 |
iNextFreeMessage = NULL; |
|
991 |
} |
|
992 |
||
993 |
||
994 |
/** |
|
995 |
Decrement the total access count, deleting the session object if this is the last |
|
996 |
reference on the session. |
|
997 |
||
998 |
Note - This may release the system lock temporarily. |
|
999 |
||
1000 |
@return 0 if the session still exists, EObjectDeleted if the session has been deleted. |
|
1001 |
||
1002 |
@pre Enter with system locked |
|
1003 |
@post Leave with system locked |
|
1004 |
*/ |
|
1005 |
TInt DSession::TotalAccessDec() |
|
1006 |
{ |
|
1007 |
__ASSERT_SYSTEM_LOCK; |
|
1008 |
TInt r = 0; |
|
1009 |
if (--iTotalAccessCount == 0) |
|
1010 |
{// The session must have been closed. |
|
1011 |
__NK_ASSERT_DEBUG(AccessCount()==0); |
|
1012 |
NKern::UnlockSystem(); |
|
1013 |
K::ObjDelete(this); |
|
1014 |
r = EObjectDeleted; |
|
1015 |
NKern::LockSystem(); |
|
1016 |
} |
|
1017 |
return r; |
|
1018 |
} |
|
1019 |
||
1020 |
/** |
|
1021 |
As DSession::TotalAccessDec() except it returns with the system lock released. |
|
1022 |
||
1023 |
@pre Enter with system locked |
|
1024 |
@post Leave with system unlocked |
|
1025 |
*/ |
|
1026 |
TInt DSession::TotalAccessDecRel() |
|
1027 |
{ |
|
1028 |
__ASSERT_SYSTEM_LOCK; |
|
1029 |
TInt r = 0; |
|
1030 |
TUint tac = --iTotalAccessCount; |
|
1031 |
NKern::UnlockSystem(); |
|
1032 |
||
1033 |
if (tac == 0) |
|
1034 |
{// The session must have been closed. |
|
1035 |
__NK_ASSERT_DEBUG(AccessCount()==0); |
|
1036 |
K::ObjDelete(this); |
|
1037 |
r = EObjectDeleted; |
|
1038 |
} |
|
1039 |
return r; |
|
1040 |
} |
|
1041 |
||
1042 |
||
1043 |
void DSession::Transfer(DServer* aServer, RMessageK* aMsg) |
|
1044 |
// |
|
1045 |
// Enter and leave with system locked |
|
1046 |
// |
|
1047 |
{ |
|
1048 |
(void)aMsg; // avoid non-DEBUG whinge about unused parameter |
|
1049 |
__KTRACE_OPT(KSERVER, Kern::Printf("Session %O Transfer From %O To %O, msg %08x", |
|
1050 |
this, iServer, aServer, aMsg)); |
|
1051 |
||
1052 |
// The current server must be a master, and the new one a designated slave |
|
1053 |
if (iServer->iServerRole != EServerRole_Master) |
|
1054 |
K::PanicCurrentThread(ESessionInvalidCookieMsg); |
|
1055 |
if (aServer->iServerRole != EServerRole_Slave) |
|
1056 |
K::PanicCurrentThread(ESessionInvalidCookieMsg); |
|
1057 |
||
1058 |
// Both servers must be part of the same process |
|
1059 |
if (iServer->iOwningThread->iOwningProcess != aServer->iOwningThread->iOwningProcess) |
|
1060 |
K::PanicCurrentThread(ESessionInvalidCookieMsg); |
|
1061 |
||
1062 |
// Transfers must not involve moribund servers |
|
1063 |
if (iServer->IsClosing() || aServer->IsClosing()) |
|
1064 |
K::PanicCurrentThread(ESessionInvalidCookieMsg); |
|
1065 |
if (iServer->iOwningThread->iMState == DThread::EDead) |
|
1066 |
K::PanicCurrentThread(ESessionInvalidCookieMsg); |
|
1067 |
if (aServer->iOwningThread->iMState == DThread::EDead) |
|
1068 |
K::PanicCurrentThread(ESessionInvalidCookieMsg); |
|
1069 |
||
1070 |
// Unlink from the current server, and link to the new one |
|
1071 |
iServerLink.Deque(); |
|
1072 |
iServer = aServer; |
|
1073 |
iServer->iSessionQ.Add(&iServerLink); |
|
1074 |
||
1075 |
// The message queue must contain at least the Connect message |
|
1076 |
__ASSERT_DEBUG(!iMsgQ.IsEmpty(), |
|
1077 |
K::Fault(K::EMsgFreeBadPool)); |
|
1078 |
||
1079 |
RMessageK* connectMsg = _LOFF(iMsgQ.First(), RMessageK, iSessionLink); |
|
1080 |
// RMessageK* lastMsg = _LOFF(iMsgQ.Last(), RMessageK, iSessionLink); |
|
1081 |
||
1082 |
__ASSERT_DEBUG(connectMsg == aMsg, |
|
1083 |
K::Fault(K::EKernelMsgNotAccepted)); |
|
1084 |
__ASSERT_DEBUG(connectMsg->iFunction == RMessage2::EConnect, |
|
1085 |
K::Fault(K::EKernelMsgNotAccepted)); |
|
1086 |
__ASSERT_DEBUG(connectMsg->IsAccepted(), |
|
1087 |
K::Fault(K::EKernelMsgNotAccepted)); |
|
1088 |
||
1089 |
// Transfer all pending messages except the first (which is the Connect) |
|
1090 |
SDblQueLink* p; |
|
1091 |
TInt msgCount = 0; |
|
1092 |
for (RMessageK* m = connectMsg; (p = m->iSessionLink.iNext) != &iMsgQ.iA; ) |
|
1093 |
{ |
|
1094 |
msgCount += 1; |
|
1095 |
m = _LOFF(p, RMessageK, iSessionLink); |
|
1096 |
__ASSERT_DEBUG(!m->IsAccepted(), |
|
1097 |
K::Fault(K::EMessageAlreadyPending)); |
|
1098 |
if (m->IsDelivered()) |
|
1099 |
m->iServerLink.Deque(); |
|
1100 |
iServer->Deliver(m); |
|
1101 |
} |
|
1102 |
||
1103 |
// Transfer the disconnect message too? |
|
1104 |
if (iDisconnectMsgPtr->IsDelivered()) |
|
1105 |
{ |
|
1106 |
__ASSERT_DEBUG(!iDisconnectMsgPtr->IsAccepted(), |
|
1107 |
K::Fault(K::EMessageAlreadyPending)); |
|
1108 |
iDisconnectMsgPtr->iServerLink.Deque(); |
|
1109 |
__SendDiscMsg(this); |
|
1110 |
} |
|
1111 |
} |
|
1112 |
||
1113 |
// Detach a session from the server |
|
1114 |
// Called either when server terminates or when server completes disconnect message |
|
1115 |
// Enter and leave with system locked |
|
1116 |
// |
|
1117 |
void DSession::Detach(TInt aReason) |
|
1118 |
{ |
|
1119 |
__KTRACE_OPT(KSERVER,Kern::Printf("Session %O Detach From %O reason %d", this, iServer, aReason)); |
|
1120 |
iServer = NULL; |
|
1121 |
iServerLink.Deque(); |
|
1122 |
||
1123 |
if (iDisconnectMsgPtr->IsDelivered()) |
|
1124 |
iDisconnectMsgPtr->iServerLink.Deque(); |
|
1125 |
iDisconnectMsgPtr->SetFree(); |
|
1126 |
||
1127 |
for (; !iMsgQ.IsEmpty(); NKern::LockSystem()) |
|
1128 |
{ |
|
1129 |
--iMsgCount; |
|
1130 |
RMessageK* m = _LOFF(iMsgQ.First()->Deque(), RMessageK, iSessionLink); |
|
1131 |
__ASSERT_ALWAYS(m->iSession == this, K::Fault(K::EInvalidSessionAccessCount)); |
|
1132 |
m->iSession = NULL; // message now detached from session |
|
1133 |
if (m->iMsgType == RMessageK::ESession) |
|
1134 |
iPoolSize -= 1; // one less message in the pool |
|
1135 |
||
1136 |
// Note whether session connect message has been discarded |
|
1137 |
if (m->iFunction == RMessage2::EConnect) |
|
1138 |
iConnectMsgPtr = NULL; |
|
1139 |
if (m->IsDelivered()) |
|
1140 |
m->iServerLink.Deque(); |
|
1141 |
||
1142 |
DThread* t = m->iClient; |
|
1143 |
TUint32 c = --t->iIpcCount; |
|
1144 |
||
1145 |
if(m->IsDelivered() || m->IsAccepted()) |
|
1146 |
{ |
|
102
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
1147 |
if (!IsClosing() && t->iMState != DThread::EDead) |
0 | 1148 |
{ |
1149 |
m->SetCompleting(); |
|
1150 |
Kern::QueueRequestComplete(t, m, aReason); |
|
1151 |
} |
|
1152 |
else |
|
1153 |
{ |
|
1154 |
m->Reset(); |
|
1155 |
m->CloseRef(); |
|
1156 |
} |
|
1157 |
} |
|
1158 |
||
1159 |
NKern::UnlockSystem(); |
|
1160 |
if (c == 0x80000000u) |
|
1161 |
t->AsyncClose(); |
|
1162 |
} |
|
1163 |
||
1164 |
// Output total access count here before the session object may potentially |
|
1165 |
// be deleted. Give the value after the detach has completed. |
|
1166 |
__KTRACE_OPT(KSERVER,Kern::Printf("TAC: %d", iTotalAccessCount - 1)); |
|
1167 |
#ifdef BTRACE_CLIENT_SERVER |
|
1168 |
BTrace8(BTrace::EClientServer,BTrace::ESessionDetach,this,aReason); |
|
1169 |
#endif |
|
1170 |
// This may temporarily release the system lock if it deletes the session. |
|
1171 |
// Don't access any members after this call as the session may be deleted. |
|
1172 |
TotalAccessDec(); |
|
1173 |
} |
|
1174 |
||
1175 |
void DSession::CloseFromDisconnect() |
|
1176 |
// |
|
1177 |
// Called when server completes the disconnect message |
|
1178 |
// Enter and leave with system locked |
|
1179 |
// |
|
1180 |
{ |
|
1181 |
__KTRACE_OPT(KIPC,Kern::Printf("MsgCoD: %O->%O",TheCurrentThread,this)); |
|
1182 |
__KTRACE_OPT(KTHREAD,Kern::Printf("DSession::CloseFromDisconnect AC:%d TAC:%d", AccessCount(), iTotalAccessCount)); |
|
1183 |
__ASSERT_ALWAYS(AccessCount()==0, K::Fault(K::EInvalidSessionAccessCount)); |
|
1184 |
||
1185 |
// there should not be any DELIVERED messages outstanding |
|
1186 |
// there may be outstanding ACCEPTED messages |
|
1187 |
// no other thread can access the session here |
|
1188 |
||
1189 |
NKern::ThreadEnterCS(); |
|
1190 |
iDisconnectMsgPtr->SetFree(); |
|
1191 |
if (iServer && !iServer->IsClosing()) |
|
1192 |
Detach(KErrSessionClosed); |
|
1193 |
// otherwise server is closing, so let that handle it |
|
1194 |
NKern::ThreadLeaveCS(); |
|
1195 |
} |
|
1196 |
||
1197 |
TInt DSession::Close(TAny*) |
|
1198 |
// |
|
1199 |
// Session close called from client side. |
|
1200 |
// Enter and leave with system unlocked. |
|
1201 |
// |
|
1202 |
{ |
|
1203 |
__KTRACE_OPT(KTHREAD,Kern::Printf("DSession::Close %O AC:%d TAC:%d", this, AccessCount(), iTotalAccessCount)); |
|
1204 |
if (Dec() == 1) |
|
1205 |
{ |
|
1206 |
// Last client access has been closed. Only the server may now influence this session. |
|
1207 |
// Thus no more messages can be added to the session's queue. |
|
1208 |
||
1209 |
NKern::LockSystem(); |
|
1210 |
||
1211 |
if (!iDisconnectMsgPtr) |
|
1212 |
{ |
|
1213 |
// Allocation of disconnect message failed during session create; there's no cleanup to do |
|
1214 |
} |
|
1215 |
else if (!iDisconnectMsgPtr->IsFree()) |
|
1216 |
{ |
|
1217 |
// Disconnect message has already been sent and is pending completion by the server |
|
1218 |
} |
|
1219 |
else if (iSessionCookie) |
|
1220 |
{ |
|
1221 |
// Deliver a disconnect message now if a user-side session has already been created |
|
1222 |
__SendDiscMsg(this); |
|
1223 |
} |
|
1224 |
else if (!iConnectMsgPtr) |
|
1225 |
{ |
|
1226 |
// Deliver a disconnect message now if there is no connect message outstanding |
|
1227 |
__SendDiscMsg(this); |
|
1228 |
} |
|
1229 |
else if (iConnectMsgPtr->IsDelivered()) |
|
1230 |
{ |
|
1231 |
// We'll remove the pending message, to prevent it from being accepted and creating |
|
1232 |
// an orphan session object. Then, completing the connect message will result in a |
|
1233 |
// disconnect message being sent, as this is equivalent to the case where a connect |
|
1234 |
// message was accepted but then subsequently rejected. |
|
1235 |
iConnectMsgPtr->iServerLink.Deque(); |
|
1236 |
iConnectMsgPtr->SetCompleting(); |
|
1237 |
ExecHandler::MessageComplete(iConnectMsgPtr, KErrSessionClosed); |
|
1238 |
} |
|
1239 |
else |
|
1240 |
{ |
|
1241 |
// There's an accepted connect message, but no user session (yet). However |
|
1242 |
// one could still be created, so delay sending the disconnect message. |
|
1243 |
// It will be sent from SetSessionPtr() or MessageComplete() instead. |
|
1244 |
} |
|
1245 |
||
1246 |
// Remove client contribution to the total access count. This may |
|
1247 |
// delete the object so don't access any of its members after this call. |
|
1248 |
// This will release the system lock. |
|
1249 |
return TotalAccessDecRel(); |
|
1250 |
} |
|
1251 |
||
1252 |
return 0; |
|
1253 |
} |
|
1254 |
||
1255 |
// Enter and return with system unlocked and caller in a critical section |
|
1256 |
TInt DSession::New(DSession*& aS, TInt aMsgSlots, TInt aMode) |
|
1257 |
{ |
|
1258 |
__KTRACE_OPT(KEXEC,Kern::Printf("DSession::New MS:%d Mode %d", aMsgSlots, aMode)); |
|
1259 |
if (aMsgSlots<-1 || aMsgSlots>KMaxMsgLimit) |
|
1260 |
return KErrArgument; |
|
1261 |
if ( (TUint)aMode > (TUint)EIpcSession_GlobalSharable) |
|
1262 |
return KErrArgument; |
|
1263 |
aS = new DSession; |
|
1264 |
if (!aS) |
|
1265 |
return KErrNoMemory; |
|
1266 |
||
1267 |
RMessageK* mp = RMessageK::ClaimMessagePool(RMessageK::EDisc, 1, aS); |
|
1268 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::New(%08X) claimed disconnect message at %08X", aS, mp)); |
|
1269 |
if (!mp) |
|
1270 |
return KErrNoMemory; |
|
1271 |
mp->iFunction = RMessage2::EDisConnect; |
|
1272 |
aS->iDisconnectMsgPtr = mp; |
|
1273 |
||
1274 |
aS->iSessionType = (TUint8)aMode; |
|
1275 |
if (aMsgSlots == 0) |
|
1276 |
{ |
|
1277 |
aS->iHasSessionPool = 1; // pretend it has its own pool |
|
1278 |
aS->iNextFreeMessage = NULL; // but never any free messages |
|
1279 |
} |
|
1280 |
else if (aMsgSlots > 0) |
|
1281 |
{ |
|
1282 |
aS->iHasSessionPool = 1; // remember that it has its own pool |
|
1283 |
aS->iNextFreeMessage = RMessageK::ClaimMessagePool(RMessageK::ESession, aMsgSlots, aS); |
|
1284 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::New(%08X) claimed %d messages at %08X", aS, aMsgSlots, aS->iNextFreeMessage)); |
|
1285 |
if (!aS->iNextFreeMessage) |
|
1286 |
return KErrNoMemory; |
|
1287 |
aS->iPoolSize = aMsgSlots; // remember that it has its own pool |
|
1288 |
} |
|
1289 |
return KErrNone; |
|
1290 |
} |
|
1291 |
||
1292 |
// Add a new session to a server |
|
1293 |
// Enter with system locked, return with system unlocked |
|
1294 |
TInt DSession::Add(DServer* aSvr, const TSecurityPolicy* aSecurityPolicy) |
|
1295 |
{ |
|
1296 |
__KTRACE_OPT(KSERVER,Kern::Printf("Session %O Add to server %O", this, aSvr)); |
|
1297 |
if (aSvr->IsClosing()) |
|
1298 |
{ |
|
1299 |
NKern::UnlockSystem(); |
|
1300 |
return KErrServerTerminated; |
|
1301 |
} |
|
1302 |
if (aSecurityPolicy && !aSecurityPolicy->CheckPolicy(aSvr->iOwningThread,__PLATSEC_DIAGNOSTIC_STRING("Checked during RSessionBase::CreateSession"))) |
|
1303 |
{ |
|
1304 |
NKern::UnlockSystem(); |
|
1305 |
return KErrPermissionDenied; |
|
1306 |
} |
|
1307 |
iSvrSessionType = aSvr->iSessionType; |
|
1308 |
if (iSessionType > iSvrSessionType) |
|
1309 |
{ |
|
1310 |
NKern::UnlockSystem(); |
|
1311 |
return iSvrSessionType ? KErrPermissionDenied : KErrAccessDenied; |
|
1312 |
} |
|
1313 |
iServer = aSvr; |
|
1314 |
aSvr->iSessionQ.Add(&iServerLink); // Add it to the server |
|
1315 |
TotalAccessInc(); // give the server an access on this session |
|
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1316 |
|
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1317 |
DObject* owner = NULL; |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1318 |
if (iSvrSessionType == EIpcSession_Unsharable) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1319 |
owner = TheCurrentThread; |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1320 |
else if (iSvrSessionType == EIpcSession_Sharable) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1321 |
owner = TheCurrentThread->iOwningProcess; |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1322 |
|
0 | 1323 |
#ifdef BTRACE_CLIENT_SERVER |
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1324 |
BTraceContext12(BTrace::EClientServer, BTrace::ESessionAttach, this, iServer, owner); |
0 | 1325 |
#endif |
1326 |
NKern::UnlockSystem(); // server could be deleted after this line |
|
1327 |
||
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1328 |
if (owner) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1329 |
SetOwner(owner); |
0 | 1330 |
if (iSessionType == EIpcSession_GlobalSharable) |
1331 |
SetProtection(EProtected); |
|
1332 |
return KErrNone; |
|
1333 |
} |
|
1334 |
||
1335 |
TInt DSession::MakeHandle() |
|
1336 |
{ |
|
1337 |
TInt r = K::AddObject(this, ESession); |
|
1338 |
if (r==KErrNone) |
|
1339 |
{ |
|
1340 |
TOwnerType htype = iSessionType == EIpcSession_Unsharable ? EOwnerThread : EOwnerProcess; |
|
1341 |
r = K::MakeHandle(htype, this); |
|
1342 |
} |
|
1343 |
return r; |
|
1344 |
} |
|
1345 |
||
1346 |
TInt DSession::RequestUserHandle(DThread* aThread, TOwnerType aType) |
|
1347 |
{ |
|
1348 |
if (iSvrSessionType == EIpcSession_Unsharable) |
|
1349 |
return (aThread==Owner() && aType==EOwnerThread) ? KErrNone : KErrPermissionDenied; |
|
1350 |
if (iSvrSessionType == EIpcSession_Sharable) |
|
1351 |
return (aThread->iOwningProcess==Owner()) ? KErrNone : KErrPermissionDenied; |
|
1352 |
return KErrNone; |
|
1353 |
} |
|
1354 |
||
1355 |
TInt ExecHandler::SessionCreate(const TDesC& aServer, TInt aMsgSlots, const TSecurityPolicy* aSecurityPolicy, TInt aMode) |
|
1356 |
// |
|
1357 |
// Create a new session. UNPROTECTED exec call. |
|
1358 |
// |
|
1359 |
{ |
|
1360 |
TKName n; |
|
1361 |
TKName svrName; |
|
1362 |
DServer* svr = NULL; |
|
1363 |
DObjectCon& servers = *K::Containers[EServer]; |
|
1364 |
Kern::KUDesGet(n, aServer); |
|
1365 |
||
1366 |
TSecurityPolicy policy; |
|
1367 |
if(aSecurityPolicy) |
|
1368 |
{ |
|
1369 |
kumemget32(&policy,aSecurityPolicy,sizeof(policy)); |
|
1370 |
if(!policy.Validate()) |
|
1371 |
return KErrArgument; |
|
1372 |
aSecurityPolicy=&policy; |
|
1373 |
} |
|
1374 |
||
1375 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionCreate %S MS:%d Mode %d", &n, aMsgSlots, aMode)); |
|
1376 |
NKern::ThreadEnterCS(); |
|
1377 |
DSession* s=NULL; |
|
1378 |
TInt r = DSession::New(s, aMsgSlots, aMode); |
|
1379 |
if (r==KErrNone) |
|
1380 |
{ |
|
1381 |
servers.Wait(); |
|
1382 |
TFindHandle fh; |
|
1383 |
r = servers.FindByName(fh, n, svrName); |
|
1384 |
if (r==KErrNone) |
|
1385 |
{ |
|
1386 |
svr = (DServer*)servers.At(fh); // can't return NULL since we just found the server |
|
1387 |
NKern::LockSystem(); |
|
1388 |
r = s->Add(svr, aSecurityPolicy); |
|
1389 |
} |
|
1390 |
servers.Signal(); |
|
1391 |
} |
|
1392 |
if (r==KErrNone) |
|
1393 |
r = s->MakeHandle(); |
|
1394 |
if (r<KErrNone && s) |
|
1395 |
s->Close(NULL); |
|
1396 |
NKern::ThreadLeaveCS(); |
|
1397 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionCreate returns %d",r)); |
|
1398 |
if (r==KErrAccessDenied) |
|
1399 |
K::PanicKernExec(EUnsharableSession); |
|
1400 |
return r; |
|
1401 |
} |
|
1402 |
||
1403 |
TInt ExecHandler::SessionCreateFromHandle(TInt aSvrHandle, TInt aMsgSlots, const TSecurityPolicy* aSecurityPolicy, TInt aMode) |
|
1404 |
// |
|
1405 |
// Create a new session to the given server. |
|
1406 |
// Enter and return with system unlocked. |
|
1407 |
// |
|
1408 |
{ |
|
1409 |
TSecurityPolicy policy; |
|
1410 |
if(aSecurityPolicy) |
|
1411 |
{ |
|
1412 |
kumemget32(&policy,aSecurityPolicy,sizeof(policy)); |
|
1413 |
if(!policy.Validate()) |
|
1414 |
return KErrArgument; |
|
1415 |
aSecurityPolicy=&policy; |
|
1416 |
} |
|
1417 |
||
1418 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionCreate %08x MS:%d Mode %d", aSvrHandle, aMsgSlots, aMode)); |
|
1419 |
NKern::ThreadEnterCS(); |
|
1420 |
DSession* s; |
|
1421 |
DThread* t = TheCurrentThread; |
|
1422 |
TInt r = DSession::New(s, aMsgSlots, aMode); |
|
1423 |
if (r==KErrNone) |
|
1424 |
{ |
|
1425 |
NKern::LockSystem(); |
|
1426 |
DServer* svr = (DServer*)t->ObjectFromHandle(aSvrHandle, EServer); |
|
1427 |
if (svr) |
|
1428 |
r = s->Add(svr, aSecurityPolicy); |
|
1429 |
else |
|
1430 |
{ |
|
1431 |
r = KErrBadHandle; |
|
1432 |
NKern::UnlockSystem(); |
|
1433 |
} |
|
1434 |
} |
|
1435 |
if (r==KErrNone) |
|
1436 |
r = s->MakeHandle(); |
|
1437 |
if (r<KErrNone && s) |
|
1438 |
s->Close(NULL); |
|
1439 |
NKern::ThreadLeaveCS(); |
|
1440 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionCreate returns %d",r)); |
|
1441 |
if (r==KErrAccessDenied) |
|
1442 |
K::PanicKernExec(EUnsharableSession); |
|
1443 |
if (r==KErrBadHandle) |
|
1444 |
K::PanicKernExec(EBadHandle); |
|
1445 |
return r; |
|
1446 |
} |
|
1447 |
||
1448 |
// If this session doesn't have its own pool, get a free message from the |
|
1449 |
// global pool. This will attempt to expand the pool if necessary, and may |
|
1450 |
// temporarily release the system lock. |
|
1451 |
// |
|
1452 |
// If this session does have its own message pool, get a free message from |
|
1453 |
// it; if there are none, just return NULL, without attempting to expand it |
|
1454 |
// or releasing the system lock. |
|
1455 |
// |
|
1456 |
// Enter and return with system lock, may be temporarily released though |
|
1457 |
RMessageK* DSession::GetNextFreeMessage() |
|
1458 |
{ |
|
1459 |
CHECK_PRECONDITIONS(MASK_SYSTEM_LOCKED, "DSession::GetNextFreeMessage"); |
|
1460 |
||
1461 |
if (!iHasSessionPool) |
|
1462 |
return RMessageK::GetNextFreeMessage(this); |
|
1463 |
||
1464 |
RMessageK* volatile* p = &iNextFreeMessage; |
|
1465 |
RMessageK* m = *p; |
|
1466 |
if (m) |
|
1467 |
{ |
|
1468 |
*p = (RMessageK*)m->iSessionLink.iNext; |
|
1469 |
m->iSessionLink.iNext = NULL; |
|
1470 |
m->iSession = this; |
|
1471 |
} |
|
1472 |
__KTRACE_OPT(KSERVER,Kern::Printf("DSession::GetNextFreeMessage returns %08x", m)); |
|
1473 |
return m; |
|
1474 |
} |
|
1475 |
||
1476 |
TInt DSession::Send(TInt aHandle, TInt aFunction, const TInt* aPtr, TRequestStatus* aStatus) |
|
1477 |
// |
|
1478 |
// Send a message to a server. Assumes aStatus |
|
1479 |
// has already been set to KRequestPending. |
|
1480 |
// Enter and return with system unlocked. |
|
1481 |
// |
|
1482 |
{ |
|
1483 |
if (aFunction == RMessage2::EDisConnect) |
|
1484 |
return KErrArgument; |
|
1485 |
||
1486 |
RMessageK::TMsgArgs msgArgs; |
|
1487 |
if (aPtr) |
|
1488 |
msgArgs.ReadDesHeaders(aPtr); |
|
1489 |
||
1490 |
NKern::LockSystem(); |
|
1491 |
DSession* session = (DSession*)K::ObjectFromHandle(aHandle, ESession); |
|
1492 |
RMessageK* m = session->GetNextFreeMessage(); |
|
1493 |
if (!m) |
|
1494 |
{ |
|
1495 |
NKern::UnlockSystem(); |
|
1496 |
return KErrServerBusy; |
|
1497 |
} |
|
1498 |
__ASSERT_DEBUG(m->IsFree(), K::Fault(K::EMessageNotFree)); |
|
1499 |
return session->Send(m, aFunction, aPtr ? &msgArgs : NULL, aStatus); |
|
1500 |
} |
|
1501 |
||
1502 |
TInt DSession::SendSync(TInt aHandle, TInt aFunction, const TInt* aPtr, TRequestStatus* aStatus) |
|
1503 |
// |
|
1504 |
// Send a SYNCHRONOUS message to a server using current thread's |
|
1505 |
// dedicated synchronous message. Assumes aStatus has already been set to KRequestPending. |
|
1506 |
// Enter and return with system unlocked. |
|
1507 |
// |
|
1508 |
{ |
|
1509 |
if (aFunction == RMessage2::EDisConnect) |
|
1510 |
return KErrArgument; |
|
1511 |
||
1512 |
RMessageK::TMsgArgs msgArgs; |
|
1513 |
if (aPtr) |
|
1514 |
msgArgs.ReadDesHeaders(aPtr); |
|
1515 |
||
1516 |
NKern::LockSystem(); |
|
1517 |
DSession* session = (DSession*)K::ObjectFromHandle(aHandle, ESession); |
|
1518 |
RMessageK* m = TheCurrentThread->iSyncMsgPtr; |
|
1519 |
__ASSERT_ALWAYS(m->IsFree(), K::PanicCurrentThread(ESyncMsgSentTwice)); |
|
90
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
1520 |
TInt r = session->Send(m, aFunction, aPtr ? &msgArgs : NULL, aStatus); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
1521 |
NKern::YieldTimeslice(); |
947f0dc9f7a8
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
36
diff
changeset
|
1522 |
return r; |
0 | 1523 |
} |
1524 |
||
1525 |
TInt DSession::Send(RMessageK* aMsg, TInt aFunction, const RMessageK::TMsgArgs* aArgs, TRequestStatus* aStatus) |
|
1526 |
// |
|
1527 |
// Send a message to a server. |
|
1528 |
// Enter with system locked, return with system unlocked. |
|
1529 |
// |
|
1530 |
{ |
|
1531 |
TInt panicReason = KErrNone; |
|
1532 |
TInt r = KErrNone; |
|
1533 |
||
1534 |
__NK_ASSERT_DEBUG(aMsg->IsFree()); |
|
1535 |
||
1536 |
aMsg->OpenRef(); |
|
1537 |
aMsg->SetInitialising(); |
|
1538 |
aMsg->iSession = this; |
|
1539 |
aMsg->iClient = TheCurrentThread; |
|
1540 |
TheCurrentThread->iIpcCount += 1; |
|
1541 |
iMsgQ.Add(&aMsg->iSessionLink); |
|
1542 |
if (++iMsgCount > iMsgLimit) |
|
1543 |
{ |
|
1544 |
r = KErrOverflow; |
|
1545 |
goto error; |
|
1546 |
} |
|
1547 |
||
1548 |
// Check server is still alive ... |
|
1549 |
if (!iServer || iServer->IsClosing()) |
|
1550 |
{ |
|
1551 |
r = KErrServerTerminated; |
|
1552 |
goto error; |
|
1553 |
} |
|
1554 |
||
1555 |
if (aFunction == RMessage2::EConnect) |
|
1556 |
{ |
|
1557 |
// Only one connect message is allowed at once |
|
1558 |
if (iConnectMsgPtr) |
|
1559 |
{ |
|
1560 |
panicReason = ERequestAlreadyPending; |
|
1561 |
goto error; |
|
1562 |
} |
|
1563 |
||
1564 |
// Further connect messages not allowed after successful session creation |
|
1565 |
if (iSessionCookie) |
|
1566 |
{ |
|
1567 |
panicReason = ESessionAlreadyConnected; |
|
1568 |
goto error; |
|
1569 |
} |
|
1570 |
||
1571 |
// Keep track of connect message |
|
1572 |
iConnectMsgPtr = aMsg; |
|
1573 |
} |
|
1574 |
||
1575 |
// Set message contents |
|
1576 |
aMsg->iFunction = aFunction; |
|
1577 |
if (aArgs == NULL) |
|
1578 |
{ |
|
1579 |
aMsg->iMsgArgs.iArgFlags = 0; |
|
1580 |
aMsg->iMsgArgs.iArgs[0] = 0; |
|
1581 |
aMsg->iMsgArgs.iArgs[1] = 0; |
|
1582 |
aMsg->iMsgArgs.iArgs[2] = 0; |
|
1583 |
aMsg->iMsgArgs.iArgs[3] = 0; |
|
1584 |
} |
|
1585 |
else |
|
1586 |
{ |
|
1587 |
aMsg->iMsgArgs = *aArgs; |
|
1588 |
||
1589 |
// Attempt to pin the descriptors if required. This may release the system lock temporarily |
|
1590 |
r = aMsg->PinDescriptors(this, iServer->iPinClientDescriptors); |
|
1591 |
if (r != KErrNone) |
|
1592 |
goto error; |
|
1593 |
||
1594 |
if (!iServer || iServer->IsClosing()) |
|
1595 |
{ |
|
1596 |
// Server went away while we weren't holding the system lock |
|
1597 |
r = KErrServerTerminated; |
|
1598 |
goto error; |
|
1599 |
} |
|
1600 |
} |
|
1601 |
||
1602 |
// don't release system lock again after here until message is deliver/accepted |
|
1603 |
// else session may have become detached |
|
1604 |
||
1605 |
#ifdef BTRACE_CLIENT_SERVER |
|
1606 |
BTraceContext12(BTrace::EClientServer,BTrace::EMessageSend,aMsg,aFunction,this); |
|
1607 |
#endif |
|
1608 |
||
1609 |
// NB: aStatus is NULL for blind messages |
|
1610 |
r = aMsg->SetStatus(aStatus); |
|
1611 |
__ASSERT_DEBUG(r == KErrNone, K::Fault(K::EMessageInUse)); |
|
1612 |
iServer->Deliver(aMsg); |
|
1613 |
NKern::UnlockSystem(); |
|
1614 |
return r; |
|
1615 |
||
1616 |
error: |
|
1617 |
aMsg->CloseRef(); |
|
1618 |
if (panicReason) |
|
1619 |
K::PanicCurrentThread(panicReason); // doesn't return! |
|
1620 |
NKern::UnlockSystem(); |
|
1621 |
return r; |
|
1622 |
} |
|
1623 |
||
31
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1624 |
void DSession::BTracePrime(TInt aCategory) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1625 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1626 |
#ifdef BTRACE_CLIENT_SERVER |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1627 |
if (aCategory == BTrace::EClientServer || aCategory == -1) |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1628 |
{ |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1629 |
BTrace12(BTrace::EClientServer, BTrace::ESessionAttach, this, iServer, iOwner); |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1630 |
} |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1631 |
#endif |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1632 |
} |
56f325a607ea
Revision: 200951
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1633 |
|
0 | 1634 |
|
1635 |
void ExecHandler::SetSessionPtr(RMessageK* aMsg, const TAny* aSessionCookie) |
|
1636 |
// |
|
1637 |
// Enter with system locked, return with system unlocked |
|
1638 |
// |
|
1639 |
{ |
|
1640 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SetSessionPtr")); |
|
1641 |
||
1642 |
// Session cookie must not be set to NULL |
|
1643 |
if (!aSessionCookie) |
|
1644 |
K::PanicCurrentThread(ESessionNullCookie); |
|
1645 |
||
1646 |
// Session cookie must be set from the connect message |
|
1647 |
if (aMsg->iFunction != RMessage2::EConnect) |
|
1648 |
K::PanicCurrentThread(ESessionInvalidCookieMsg); |
|
1649 |
||
1650 |
DSession* pS = aMsg->iSession; |
|
1651 |
||
1652 |
// Must not attempt to set the cookie more than once |
|
1653 |
if (pS->iSessionCookie) |
|
1654 |
K::PanicCurrentThread(ESessionCookieAlreadySet); |
|
1655 |
||
1656 |
pS->iSessionCookie = aSessionCookie; |
|
1657 |
||
1658 |
if (pS->IsClosing() && pS->iDisconnectMsgPtr->IsFree()) |
|
1659 |
{ |
|
1660 |
// NB: pS->iServer is set to NULL during CloseFromDisconnect(), called |
|
1661 |
// when a disconnect message has been completed (and hence is free again), |
|
1662 |
// so this cannot send the disconnect message again. |
|
1663 |
__SendDiscMsg(pS); |
|
1664 |
} |
|
1665 |
NKern::UnlockSystem(); |
|
1666 |
} |
|
1667 |
||
1668 |
void ExecHandler::TransferSession(RMessageK* aMsg, TInt aHandle) |
|
1669 |
// |
|
1670 |
// Enter and return with system locked |
|
1671 |
// |
|
1672 |
{ |
|
1673 |
__KTRACE_OPT(KEXEC, Kern::Printf("Exec::TransferSession")); |
|
1674 |
||
1675 |
// Session transfer must done using the Connect message |
|
1676 |
if (aMsg->iFunction != RMessage2::EConnect) |
|
1677 |
K::PanicCurrentThread(ESessionInvalidCookieMsg); |
|
1678 |
||
1679 |
// Session cookie must already have been set |
|
1680 |
DSession* pSession = aMsg->iSession; |
|
1681 |
if (pSession->iSessionCookie == NULL) |
|
1682 |
K::PanicCurrentThread(ESessionNullCookie); |
|
1683 |
||
1684 |
// Find the new server and transfer the session to it |
|
1685 |
DServer* pServer = (DServer*)K::ObjectFromHandle(aHandle, EServer); |
|
1686 |
pSession->Transfer(pServer, aMsg); |
|
1687 |
||
1688 |
// This call also completes the original message with no error |
|
1689 |
ExecHandler::MessageComplete(aMsg, KErrNone); |
|
1690 |
} |
|
1691 |
||
1692 |
void ExecHandler::ServerReceive(DServer* aServer, TRequestStatus& aStatus, TAny* aMsg) |
|
1693 |
// |
|
1694 |
// Enter and leave with system locked |
|
1695 |
// |
|
1696 |
{ |
|
1697 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ServerReceive")); |
|
1698 |
aServer->Receive(aStatus, aMsg); |
|
1699 |
} |
|
1700 |
||
1701 |
void ExecHandler::ServerCancel(DServer* aServer) |
|
1702 |
// |
|
1703 |
// Enter and leave with system locked |
|
1704 |
// |
|
1705 |
{ |
|
1706 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ServerCancel")); |
|
1707 |
aServer->Cancel(); |
|
1708 |
} |
|
1709 |
||
1710 |
TInt ExecHandler::SessionSend(TInt aHandle, TInt aFunction, TAny* aPtr, TRequestStatus* aStatus) |
|
1711 |
// |
|
1712 |
// Enter with system locked, return with system unlocked. |
|
1713 |
// |
|
1714 |
{ |
|
1715 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionSend")); |
|
1716 |
return DSession::Send(aHandle, aFunction, (const TInt*)aPtr, aStatus); |
|
1717 |
} |
|
1718 |
||
1719 |
TInt ExecHandler::SessionSendSync(TInt aHandle, TInt aFunction, TAny* aPtr, TRequestStatus* aStatus) |
|
1720 |
// |
|
1721 |
// Enter with system locked, return with system unlocked. |
|
1722 |
// |
|
1723 |
{ |
|
1724 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionSendSync")); |
|
1725 |
return DSession::SendSync(aHandle, aFunction, (const TInt*)aPtr, aStatus); |
|
1726 |
} |
|
1727 |
||
1728 |
#ifndef __MESSAGE_MACHINE_CODED__ |
|
1729 |
void ExecHandler::MessageComplete(RMessageK* aMsg, TInt aReason) |
|
1730 |
// |
|
1731 |
// Enter and leave with system locked. |
|
1732 |
// |
|
1733 |
{ |
|
1734 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageComplete")); |
|
1735 |
RMessageK& m = *aMsg; |
|
1736 |
||
1737 |
#ifdef BTRACE_CLIENT_SERVER |
|
1738 |
BTraceContext8(BTrace::EClientServer,BTrace::EMessageComplete,aMsg,aReason); |
|
1739 |
#endif |
|
1740 |
||
1741 |
DSession* s = m.iSession; |
|
1742 |
||
1743 |
// First check for disconnect message |
|
1744 |
if (m.iFunction == RMessage2::EDisConnect) |
|
1745 |
{ |
|
1746 |
s->CloseFromDisconnect(); |
|
1747 |
return; |
|
1748 |
} |
|
1749 |
||
1750 |
// Note whether session connect message has completed |
|
1751 |
if (m.iFunction == RMessage2::EConnect) |
|
1752 |
s->iConnectMsgPtr = NULL; |
|
1753 |
||
1754 |
__KTRACE_OPT(KIPC,Kern::Printf("MsgCo: M:%d r:%d %O->%O", m.iFunction, aReason, TheCurrentThread, m.iClient)); |
|
102
ef2a444a7410
Revision: 201018
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
90
diff
changeset
|
1755 |
if (!s->IsClosing() && m.iClient->iMState != DThread::EDead) |
0 | 1756 |
{ |
1757 |
m.SetCompleting(); |
|
1758 |
Kern::QueueRequestComplete(m.iClient, &m, aReason); |
|
1759 |
} |
|
1760 |
else |
|
1761 |
{ |
|
1762 |
// Pending disconnect, is it a connect message? |
|
1763 |
if(m.iFunction == RMessage2::EConnect) |
|
1764 |
{ |
|
1765 |
// If a session has been created, then a disconnect message should already have been sent |
|
1766 |
__ASSERT_DEBUG(!s->iSessionCookie || !s->iServer || !s->iDisconnectMsgPtr->IsFree(), |
|
1767 |
K::Fault(K::EMsgCompleteDiscNotSent)); |
|
1768 |
||
1769 |
// if no server-side session object to clean up, send disconnect message |
|
1770 |
// anyway to prevent message lifetime issues for the server for any other |
|
1771 |
// messages on this unconnected session the server may have accepted |
|
1772 |
if (!s->iSessionCookie) |
|
1773 |
__SendDiscMsg(s); |
|
1774 |
} |
|
1775 |
m.Reset(); |
|
1776 |
m.CloseRef(); // Return message to appropriate pool |
|
1777 |
} |
|
1778 |
} |
|
1779 |
#else |
|
1780 |
#ifdef _DEBUG |
|
1781 |
extern "C" void __FaultBadMsgPool() |
|
1782 |
{ |
|
1783 |
K::Fault(K::EMsgFreeBadPool); |
|
1784 |
} |
|
1785 |
||
1786 |
extern "C" void __FaultMsgNotFree() |
|
1787 |
{ |
|
1788 |
K::Fault(K::EMessageNotFree); |
|
1789 |
} |
|
1790 |
||
1791 |
extern "C" void __FaultMsgInUse() |
|
1792 |
{ |
|
1793 |
K::Fault(K::EMessageInUse); |
|
1794 |
} |
|
1795 |
||
1796 |
extern "C" void __FaultMsgCompleteDiscNotSent() |
|
1797 |
{ |
|
1798 |
K::Fault(K::EMsgCompleteDiscNotSent); |
|
1799 |
} |
|
1800 |
#endif |
|
1801 |
||
1802 |
extern "C" void __PanicSyncMsgSentTwice() |
|
1803 |
{ |
|
1804 |
K::PanicCurrentThread(ESyncMsgSentTwice); |
|
1805 |
} |
|
1806 |
||
1807 |
extern "C" void __PanicMesAlreadyPending() |
|
1808 |
{ |
|
1809 |
K::PanicCurrentThread(EMesAlreadyPending); |
|
1810 |
} |
|
1811 |
||
1812 |
#endif |
|
1813 |
||
1814 |
// |
|
1815 |
// Enter and leave with system locked |
|
1816 |
// |
|
1817 |
extern "C" void __SendDiscMsg(DSession* aSession) |
|
1818 |
{ |
|
1819 |
DServer* server = aSession->iServer; |
|
1820 |
if (server && !server->IsClosing()) |
|
1821 |
{ |
|
1822 |
// Send the preallocated disconnect message, as the session is still attached |
|
1823 |
// and the server isn't closing so it won't detach the session itself. |
|
1824 |
RMessageK* disc = aSession->iDisconnectMsgPtr; |
|
1825 |
__ASSERT_DEBUG(disc->iSession == aSession && disc->IsFree(), |
|
1826 |
K::Fault(K::EMsgCompleteDiscNotSent)); |
|
1827 |
||
1828 |
#ifdef BTRACE_CLIENT_SERVER |
|
1829 |
BTraceContext12(BTrace::EClientServer,BTrace::EMessageSend,disc,disc->iFunction,aSession); |
|
1830 |
#endif |
|
1831 |
disc->iSession = aSession; |
|
1832 |
server->Deliver(disc); |
|
1833 |
} |
|
1834 |
} |
|
1835 |
||
1836 |
TInt ExecHandler::SessionShare(TInt& aHandle, TInt aMode) |
|
1837 |
// |
|
1838 |
// Mutate a single-threaded session into a multi-threaded session |
|
1839 |
// |
|
1840 |
{ |
|
1841 |
if ( (TUint)aMode > (TUint)EIpcSession_GlobalSharable || aMode == EIpcSession_Unsharable) |
|
1842 |
return KErrArgument; |
|
1843 |
TInt oldHandle; |
|
1844 |
TInt newHandle = 0; |
|
1845 |
kumemget32(&oldHandle, &aHandle, sizeof(TInt)); |
|
1846 |
NKern::LockSystem(); |
|
1847 |
DSession* s = (DSession*)K::ObjectFromHandle(oldHandle, ESession); |
|
1848 |
s->CheckedOpen(); |
|
1849 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionShare %O server %O mode %08x", s, s->iServer, aMode)); |
|
1850 |
TInt r = KErrNone; |
|
1851 |
TUint ct = s->iSessionType; |
|
1852 |
if (s->iSvrSessionType == EIpcSession_Unsharable) |
|
1853 |
{ |
|
1854 |
r = EUnsharableSession; |
|
1855 |
goto end; |
|
1856 |
} |
|
1857 |
if ((TUint)s->iSvrSessionType < (TUint)aMode) |
|
1858 |
{ |
|
1859 |
r = KErrPermissionDenied; |
|
1860 |
goto end; |
|
1861 |
} |
|
1862 |
if (aMode == EIpcSession_GlobalSharable) |
|
1863 |
s->SetProtection(DObject::EProtected); |
|
1864 |
if (ct >= (TUint)aMode) |
|
1865 |
goto end; // nothing to do |
|
1866 |
s->iSessionType = (TUint8)aMode; |
|
1867 |
if (ct >= EIpcSession_Sharable) |
|
1868 |
goto end; // nothing more to do |
|
1869 |
K::ThreadEnterCS(); |
|
1870 |
r = K::MakeHandle(EOwnerProcess, s); |
|
1871 |
if (r>=0) |
|
1872 |
{ |
|
1873 |
newHandle = r; |
|
1874 |
r = KErrNone; |
|
1875 |
K::HandleClose(oldHandle); |
|
1876 |
if (Kern::KUSafeWrite(&aHandle, &newHandle, sizeof(newHandle))) // don't let thread die before updating handle |
|
1877 |
r = ECausedException; |
|
1878 |
} |
|
1879 |
if (r==KErrNone) |
|
1880 |
{ |
|
1881 |
NKern::ThreadLeaveCS(); |
|
1882 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionShare returns %d",r)); |
|
1883 |
return r; |
|
1884 |
} |
|
1885 |
goto end2; |
|
1886 |
end: |
|
1887 |
K::ThreadEnterCS(); |
|
1888 |
end2: |
|
1889 |
s->Close(NULL); |
|
1890 |
NKern::ThreadLeaveCS(); |
|
1891 |
if (r>0) |
|
1892 |
K::PanicKernExec(r); |
|
1893 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SessionShare returns %d",r)); |
|
1894 |
return r; |
|
1895 |
} |
|
1896 |
||
1897 |
#ifndef __MESSAGE_MACHINE_CODED_2__ |
|
1898 |
void ExecHandler::MessageConstructFromPtr(RMessageK* aMsgK, TAny* aMsgU) |
|
1899 |
{ |
|
1900 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageConstructFromPtr")); |
|
1901 |
RMessageU2 umsg(*aMsgK); |
|
1902 |
NKern::UnlockSystem(); |
|
1903 |
kumemput32(aMsgU, &umsg, sizeof(umsg)); |
|
1904 |
} |
|
1905 |
#endif |
|
1906 |
||
1907 |
TInt ExecHandler::MessageGetDesLength(RMessageK* aMsg, TInt aParam) |
|
1908 |
{ |
|
1909 |
__KTRACE_OPT(KEXEC, Kern::Printf("Exec::MessageGetDesLength")); |
|
1910 |
if (TUint(aParam) >= TUint(KMaxMessageArguments)) |
|
1911 |
return KErrArgument; |
|
1912 |
if (!aMsg->IsDescriptor(aParam)) |
|
1913 |
return KErrBadDescriptor; |
|
1914 |
return aMsg->DesLength(aParam); |
|
1915 |
} |
|
1916 |
||
1917 |
TInt ExecHandler::MessageGetDesMaxLength(RMessageK* aMsg, TInt aParam) |
|
1918 |
{ |
|
1919 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageGetDesMaxLength")); |
|
1920 |
if (TUint(aParam) >= TUint(KMaxMessageArguments)) |
|
1921 |
return KErrArgument; |
|
1922 |
if (!aMsg->IsDescriptor(aParam)) |
|
1923 |
return KErrBadDescriptor; |
|
1924 |
return aMsg->DesMaxLength(aParam); |
|
1925 |
} |
|
1926 |
||
1927 |
const TRequestStatus* ExecHandler::MessageClientStatus(RMessageK* aMsg) |
|
1928 |
{ |
|
1929 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageClientStatus")); |
|
1930 |
if (aMsg->iMsgType == RMessageK::ESync) |
|
1931 |
return NULL; |
|
1932 |
return aMsg->StatusPtr(); |
|
1933 |
} |
|
1934 |
||
1935 |
#ifndef __REMOVE_PLATSEC_DIAGNOSTICS__ |
|
1936 |
#define __PSIF(msg, diag) PlatSec::ProcessIsolationIPCFail(msg, diag) |
|
1937 |
#else //__REMOVE_PLATSEC_DIAGNOSTICS__ |
|
1938 |
#define __PSIF(msg, diag) PlatSec::EmitDiagnostic() |
|
1939 |
#endif // !__REMOVE_PLATSEC_DIAGNOSTICS__ |
|
1940 |
||
1941 |
TInt ExecHandler::MessageIpcCopy(RMessageK* aMsg, TInt aParam, SIpcCopyInfo& aInfo, TInt aOffset) |
|
1942 |
{ |
|
1943 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageIpcCopy flags=%08x",aInfo.iFlags)); |
|
1944 |
if (TUint(aParam) >= TUint(KMaxMessageArguments)) |
|
1945 |
return KErrArgument; |
|
1946 |
if (!aMsg->IsDescriptor(aParam) || !aMsg->Descriptor(aParam).IsSet()) |
|
1947 |
{ |
|
1948 |
__PSIF(aMsg, __PLATSEC_DIAGNOSTIC_STRING("Server attempted to use RMessagePtr2::Read/Write on a non descriptor message argument")); |
|
1949 |
return KErrBadDescriptor; |
|
1950 |
} |
|
1951 |
||
1952 |
TInt argType = aMsg->ArgType(aParam); |
|
1953 |
if ((aInfo.iFlags & KIpcDirWrite) && (argType & TIpcArgs::EFlagConst)) |
|
1954 |
if (__PSIF(aMsg, __PLATSEC_DIAGNOSTIC_STRING("Server attempted to use RMessagePtr2::Write on a const client descriptor argument"))) |
|
1955 |
return KErrBadDescriptor; |
|
1956 |
if ((aInfo.iFlags & KChunkShiftBy1) && !(argType & TIpcArgs::EFlag16Bit)) |
|
1957 |
if (__PSIF(aMsg, __PLATSEC_DIAGNOSTIC_STRING("Server attempted to use 16bit RMessagePtr2::Read/Write on a 8 bit client descriptor argument"))) |
|
1958 |
return KErrBadDescriptor; |
|
1959 |
if ((argType & TIpcArgs::EFlag16Bit) && !(aInfo.iFlags & KChunkShiftBy1)) |
|
1960 |
if (__PSIF(aMsg, __PLATSEC_DIAGNOSTIC_STRING("Server attempted to use 8bit RMessagePtr2::Read/Write on a 16 bit client descriptor argument"))) |
|
1961 |
return KErrBadDescriptor; |
|
1962 |
||
1963 |
DThread& t = *TheCurrentThread; |
|
1964 |
t.iTempMsg = aMsg; |
|
1965 |
aMsg->OpenRef(); |
|
1966 |
||
1967 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
1968 |
NKern::FlashSystem(); |
|
1969 |
#else |
|
1970 |
NKern::UnlockSystem(); |
|
1971 |
#endif |
|
1972 |
||
1973 |
TInt r = KErrNone; |
|
1974 |
DThread* pT = aMsg->iClient; |
|
1975 |
TInt mode = (aInfo.iFlags & KChunkShiftBy1) | KCheckLocalAddress | KDoNotUpdateDesLength; // assume called from user mode |
|
1976 |
||
1977 |
if ((aInfo.iFlags & KIpcDirWrite) == 0) |
|
1978 |
{ |
|
1979 |
r = pT->DoDesRead(aMsg->Descriptor(aParam), aInfo.iLocalPtr, aInfo.iLocalLen, aOffset, mode); |
|
1980 |
} |
|
1981 |
else |
|
1982 |
{ |
|
1983 |
TAny* ptr = aMsg->Ptr(aParam); |
|
1984 |
r = pT->DoDesWrite(ptr, aMsg->Descriptor(aParam), aInfo.iLocalPtr, aInfo.iLocalLen, aOffset, mode, NULL); |
|
1985 |
if (r >= 0) |
|
1986 |
{ |
|
1987 |
// Update cached flags + length word for all matching descriptors and set written bit |
|
1988 |
TInt descFlags = aMsg->iMsgArgs.AllDescriptorFlags(); |
|
1989 |
for (TInt i = 0; descFlags != 0; ++i, descFlags >>= TIpcArgs::KBitsPerType) |
|
1990 |
if ((descFlags & TIpcArgs::EFlagDes) && aMsg->Ptr(i) == ptr) |
|
1991 |
{ |
|
1992 |
aMsg->Descriptor(i).SetTypeAndLength(r); |
|
1993 |
aMsg->iMsgArgs.SetDesWritten(i); |
|
1994 |
} |
|
1995 |
r = KErrNone; |
|
1996 |
} |
|
1997 |
} |
|
1998 |
||
1999 |
#ifndef __MEMMODEL_FLEXIBLE__ |
|
2000 |
#else |
|
2001 |
NKern::LockSystem(); |
|
2002 |
#endif |
|
2003 |
||
2004 |
__NK_ASSERT_DEBUG(aMsg->IsAccepted() || aMsg->IsCompleting()); |
|
2005 |
t.iTempMsg = NULL; |
|
2006 |
aMsg->CloseRef(); |
|
2007 |
return r; |
|
2008 |
} |
|
2009 |
||
2010 |
TInt ExecHandler::MessageClient(DThread* aClient, TOwnerType aType) |
|
2011 |
{ |
|
2012 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageClient")); |
|
2013 |
NKern::ThreadEnterCS(); |
|
2014 |
TInt r=aClient->Open(); |
|
2015 |
NKern::UnlockSystem(); |
|
2016 |
if (r==KErrNone) |
|
2017 |
{ |
|
2018 |
r=K::MakeHandle(aType,aClient); |
|
2019 |
if (r<KErrNone) |
|
2020 |
aClient->Close(NULL); |
|
2021 |
} |
|
2022 |
NKern::ThreadLeaveCS(); |
|
2023 |
return r; |
|
2024 |
} |
|
2025 |
||
2026 |
TInt ExecHandler::MessageSetProcessPriority(DThread* aClient, TProcessPriority aPriority) |
|
2027 |
{ |
|
2028 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageSetProcessPriority")); |
|
2029 |
DProcess* pP = aClient->iOwningProcess; |
|
2030 |
if (pP->iFlags & KProcessFlagPriorityControl) |
|
2031 |
if (aPriority==EPriorityBackground || aPriority==EPriorityForeground) |
|
2032 |
{ |
|
2033 |
ExecHandler::ProcessSetPriority(pP,aPriority); |
|
2034 |
return KErrNone; |
|
2035 |
} |
|
2036 |
NKern::UnlockSystem(); |
|
2037 |
return KErrPermissionDenied; |
|
2038 |
} |
|
2039 |
||
2040 |
void GetCategory(TDes& aDest, const TDesC& aSrc); // in server.cpp |
|
2041 |
||
2042 |
void ExecHandler::MessageKill(TInt aHandle, TExitType aType, TInt aReason, const TDesC* aCategory) |
|
2043 |
{ |
|
2044 |
TBuf<KMaxExitCategoryName> cat; |
|
2045 |
if (aType==EExitPanic && aCategory) |
|
2046 |
GetCategory(cat,*aCategory); |
|
2047 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageKill %d,%d,%lS",aType,aReason,&cat)); |
|
2048 |
K::CheckKernelUnlocked(); |
|
2049 |
NKern::LockSystem(); |
|
2050 |
RMessageK* pM = RMessageK::MessageK(aHandle); |
|
2051 |
DThread* pT = pM->iClient; |
|
2052 |
pT->Die(aType,aReason,cat); // releases system lock |
|
2053 |
} |
|
2054 |
||
2055 |
TInt ExecHandler::MessageOpenObject(RMessageK* aMsg, TObjectType aObjType, TInt aParam, TOwnerType aOwnerType) |
|
2056 |
{ |
|
2057 |
__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MessageOpenObject")); |
|
2058 |
NKern::ThreadEnterCS(); |
|
2059 |
DObject* pO; |
|
2060 |
pO=0; // To shut up the compiler |
|
2061 |
TInt r; |
|
2062 |
if ( TUint(aParam)>=TUint(KMaxMessageArguments) ) |
|
2063 |
{ |
|
2064 |
r = KErrArgument; |
|
2065 |
goto done; |
|
2066 |
} |
|
2067 |
r = KErrBadHandle; |
|
2068 |
if ( aMsg->ArgType(aParam)!=TIpcArgs::EHandle ) |
|
2069 |
goto done; |
|
2070 |
pO=aMsg->iClient->ObjectFromHandle(aMsg->Arg(aParam),aObjType); |
|
2071 |
if (pO) |
|
2072 |
if(pO->Protection()!=DObject::ELocal) |
|
2073 |
r=pO->Open(); |
|
2074 |
done: |
|
2075 |
NKern::UnlockSystem(); |
|
2076 |
if (r==KErrNone) |
|
2077 |
{ |
|
2078 |
r=K::MakeHandle(aOwnerType,pO); // this will add to process if necessary |
|
2079 |
if (r<KErrNone) |
|
2080 |
pO->Close(NULL); // can't have been added to process so NULL |
|
2081 |
} |
|
2082 |
NKern::ThreadLeaveCS(); |
|
2083 |
return r; |
|
2084 |
} |
|
2085 |
||
2086 |
void ExecHandler::MessageCompleteWithHandle(RMessageK* aMsg, TInt aHandle) |
|
2087 |
// |
|
2088 |
// Enter and leave with system locked. |
|
2089 |
// |
|
2090 |
{ |
|
2091 |
DObject* pO = K::ObjectFromHandle(aHandle); |
|
2092 |
pO->CheckedOpen(); |
|
2093 |
DThread* pT=aMsg->iClient; |
|
2094 |
TInt r = pT->Open(); |
|
2095 |
K::ThreadEnterCS(); |
|
2096 |
TInt h; |
|
2097 |
if(r==KErrNone) |
|
2098 |
{ |
|
2099 |
// always EOwnerThreads so it doesn't leak if client thread dies |
|
2100 |
r = pT->MakeHandleAndOpen(EOwnerThread,pO,h); |
|
2101 |
if (r==KErrNone) |
|
2102 |
r = h; |
|
2103 |
pT->Close(NULL); |
|
2104 |
} |
|
2105 |
pO->Close(NULL); |
|
2106 |
K::ThreadLeaveCS(); |
|
2107 |
RMessageK::MessageK((TInt)aMsg); |
|
2108 |
ExecHandler::MessageComplete(aMsg,r); |
|
2109 |
} |