0
|
1 |
// Copyright (c) 2007-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\x86\cexec.cia
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
#include <x86_mem.h>
|
|
19 |
#include <u32std.h>
|
|
20 |
#include <kernel/cache.h>
|
|
21 |
|
|
22 |
GLREF_C TInt CalcKernelHeapUsed();
|
|
23 |
void GetLatencyValues(TInt aMode, TInt& aCount, TInt* aDest);
|
|
24 |
void KernMsgTest();
|
|
25 |
void InvalidExecHandler();
|
|
26 |
void InvalidFastExec();
|
|
27 |
void PreprocessHandler();
|
|
28 |
|
|
29 |
void InvalidExecHandler();
|
|
30 |
|
|
31 |
#define __GEN_KERNEL_EXEC_CODE__
|
|
32 |
|
|
33 |
#include "execs.h"
|
|
34 |
|
|
35 |
// These must be defined here because the inline assembler syntax can't
|
|
36 |
// cope with symbols or expressions involving scope resolution (::).
|
|
37 |
// Therefore, each such value should be turned into a simple constant ...
|
|
38 |
|
|
39 |
const TLinAddr K_PanicCurrentThread = (TLinAddr)&K::PanicCurrentThread;
|
|
40 |
const TLinAddr K_MsgInfo = (TLinAddr)&K::MsgInfo;
|
|
41 |
const TLinAddr NKern_CurrentThread = (TLinAddr)&NKern::CurrentThread;
|
|
42 |
|
|
43 |
#ifdef __X86GCC__
|
|
44 |
|
|
45 |
const TUint32 RMessage2_EDisConnect = RMessage2::EDisConnect;
|
|
46 |
const TUint32 RMessageK_AlignMask = (RMessageK::KMessageSize-1);
|
|
47 |
const TUint32 offset_DThread_iNThread = _FOFF(DThread,iNThread);
|
|
48 |
const TUint32 offset_RMessageK_iNext = _FOFF(RMessageK,iServerLink.iNext);
|
|
49 |
const TUint32 offset_RMessageK_iPrev = _FOFF(RMessageK,iServerLink.iPrev);
|
|
50 |
const TUint32 offset_SMsgInfo_iBase = _FOFF(K::SMsgInfo,iBase);
|
|
51 |
const TUint32 offset_SMsgInfo_iMaxSize = _FOFF(K::SMsgInfo,iMaxSize);
|
|
52 |
const TUint32 sizeof_RMessageK = sizeof(RMessageK);
|
|
53 |
|
|
54 |
#else
|
|
55 |
|
|
56 |
// With non-GCC for immediate values, including offsets, we must use the
|
|
57 |
// following enum trick, as otherwise the compiler allocates storage for
|
|
58 |
// the consts and the code emitted uses the address of this storage rather
|
|
59 |
// than the value of the constant. Grrr.
|
|
60 |
|
|
61 |
enum
|
|
62 |
{
|
|
63 |
RMessage2_EDisConnect = RMessage2::EDisConnect,
|
|
64 |
RMessageK_AlignMask = (RMessageK::KMessageSize-1),
|
|
65 |
offset_DThread_iNThread = _FOFF(DThread,iNThread),
|
|
66 |
offset_RMessageK_iNext = _FOFF(RMessageK,iServerLink.iNext),
|
|
67 |
offset_RMessageK_iPrev = _FOFF(RMessageK,iServerLink.iPrev),
|
|
68 |
offset_SMsgInfo_iBase = _FOFF(K::SMsgInfo,iBase),
|
|
69 |
offset_SMsgInfo_iMaxSize = _FOFF(K::SMsgInfo,iMaxSize),
|
|
70 |
sizeof_RMessageK = sizeof(RMessageK),
|
|
71 |
};
|
|
72 |
|
|
73 |
#endif
|
|
74 |
|
|
75 |
|
|
76 |
/***********************************************************************************
|
|
77 |
* User-side executive handlers
|
|
78 |
***********************************************************************************/
|
|
79 |
|
|
80 |
__NAKED__ void InvalidFastExec()
|
|
81 |
{
|
|
82 |
asm("sti");
|
|
83 |
asm("call %a0": :"i" (&InvalidExecHandler));
|
|
84 |
}
|
|
85 |
|
|
86 |
|
|
87 |
/***************************************************************************
|
|
88 |
* Look up a handle in the current thread or process handles array
|
|
89 |
* If handle invalid, panic the current thread
|
|
90 |
* A handle is:
|
|
91 |
* Bits 0-14 index
|
|
92 |
* Bit 15 no-close flag (ignored here)
|
|
93 |
* Bits 16-29 instance value
|
|
94 |
* Bit 30 thread-local flag
|
|
95 |
* Bit 31 special handle flag
|
|
96 |
* On entry:
|
|
97 |
* EBX = attribute flags for this call
|
|
98 |
* (bits 0-4 indicate the type of object referenced)
|
|
99 |
* XXX means that only 0x1F valid, but RObjectIx::At uses 0x3F ???
|
|
100 |
* EDI points to current NThread
|
|
101 |
* Executive call arguments in the usual place on the stack
|
|
102 |
* i.e. [ESP+4] onwards - handle is at [ESP+4]
|
|
103 |
* Return:
|
|
104 |
* EAX, ECX, EDX, EDI modified, other registers preserved
|
|
105 |
* [ESP+4] replaced with object pointer
|
|
106 |
* K::PanicCurrentThread(EBadHandle) if not a valid handle
|
|
107 |
*
|
|
108 |
* System lock is held on entry and on return.
|
|
109 |
***************************************************************************/
|
|
110 |
__NAKED__ void PreprocessHandler()
|
|
111 |
{
|
|
112 |
asm("mov eax, [esp+4]"); // get the handle
|
|
113 |
asm("mov ecx, %0": :"i" (offset_DThread_iNThread));
|
|
114 |
asm("sub edi, ecx"); // EDI points to DThread
|
|
115 |
// HARDCODED(TObjectType) - all (and only) IPC types are assumed to have this bit set!
|
|
116 |
asm("test bl, 0x20"); // IPC handle?
|
|
117 |
asm("jnz lookup_message_handle");
|
|
118 |
asm("test eax, eax"); // check for special handle
|
|
119 |
asm("js lookup_special"); // branch if special
|
|
120 |
|
|
121 |
asm("test eax, 0x40000000"); // check for thread-relative handle
|
|
122 |
asm("jnz handle_local"); // branch if it is
|
|
123 |
asm("mov ecx, [edi+%0]": : "i"_FOFF(DThread,iOwningProcess));
|
|
124 |
asm("lea ecx, [ecx+%0]": : "i"_FOFF(DProcess,iHandles));
|
|
125 |
asm("jmp got_ix_ptr");
|
|
126 |
|
|
127 |
asm("handle_local:");
|
|
128 |
asm("lea ecx, [edi+%0]": : "i"_FOFF(DThread,iHandles));
|
|
129 |
|
|
130 |
asm("got_ix_ptr:");
|
|
131 |
#ifdef __HANDLES_USE_RW_SPIN_LOCK__
|
|
132 |
XXX TODO need to acquire the read lock
|
|
133 |
// (__HANDLES_USE_RW_SPIN_LOCK__ is not supported or even finished)
|
|
134 |
#else
|
|
135 |
// System lock held on entry, so nothing to do here
|
|
136 |
#endif
|
|
137 |
asm("mov edx, eax");
|
|
138 |
asm("and edx, 0x00007fff"); // edx=handle index
|
|
139 |
asm("cmp edx, [ecx+%0]": : "i"_FOFF(RObjectIx,iCount)); // check against count
|
|
140 |
asm("jae lookup_handle_bad"); // if >= count, invalid
|
|
141 |
|
|
142 |
asm("shl edx, 3"); // sizeof iSlots[0] == 8
|
|
143 |
asm("add edx, [ecx+%0]": : "i"_FOFF(RObjectIx,iSlots)); // edx points to record for this handle
|
|
144 |
asm("shr eax, 16"); // eax=instance value with thread-local and special bits
|
|
145 |
asm("mov ecx, [edx]"); // ecx=attrib:type:instance
|
|
146 |
asm("mov edx, [edx+4]"); // edx points to DObject specified by handle
|
|
147 |
|
|
148 |
// check the instance value in the incoming handle against the value in the slot
|
|
149 |
asm("xor ax, cx");
|
|
150 |
asm("and eax, 0x3fff");
|
|
151 |
asm("jnz lookup_handle_bad");
|
|
152 |
|
|
153 |
// check that we have the right type of object
|
|
154 |
asm("test bl, 0x1f"); // if zero, any type of object accepted
|
|
155 |
// should be the same constant as in "and al" line below
|
|
156 |
asm("jz handle_ok");
|
|
157 |
|
|
158 |
asm("shr ecx, 14");
|
|
159 |
asm("mov al, bl");
|
|
160 |
asm("xor al, cl");
|
|
161 |
asm("and al, 0x1f"); // this should be 0x3f, but function header says
|
|
162 |
// it's only lower 5 bits of ebx
|
|
163 |
asm("jnz lookup_handle_bad");
|
|
164 |
|
|
165 |
asm("handle_ok:");
|
|
166 |
asm("and edx, 0xFFFFFFFC"); // clear bottom two bits in pointer [EObjRObjMask]
|
|
167 |
asm("jz lookup_handle_bad"); // => value was < 4, i.e. a reserved slot, not a valid DObject pointer
|
|
168 |
#ifdef __HANDLES_USE_RW_SPIN_LOCK__
|
|
169 |
XXX TODO must take a reference to the object (call edx->Open(), must == KErrNone)
|
|
170 |
if handle lookup only protected by RW spin lock
|
|
171 |
#endif
|
|
172 |
asm("mov [esp+4], edx"); // handle OK so replace handle with DObject pointer
|
|
173 |
asm("ret");
|
|
174 |
|
|
175 |
asm("lookup_special:");
|
|
176 |
asm("xor ecx, ecx");
|
|
177 |
asm("mov cl, bl");
|
|
178 |
asm("and cl, 0x1f"); // ecx=required UID or zero if none specified
|
|
179 |
asm("cmp cl, 2");
|
|
180 |
asm("ja lookup_handle_bad_no_unlock"); // special handle can only refer to thread or process
|
|
181 |
#ifdef __OBSOLETE_V1_IPC_SUPPORT__
|
|
182 |
asm("cmp eax, 0x88000000");
|
|
183 |
asm("jb lookup_thread_pseudo"); // jump if IPC client thread pseudo handle
|
|
184 |
#endif
|
|
185 |
asm("add eax, 0x8000");
|
|
186 |
asm("jnc lookup_handle_bad_no_unlock"); // must be >= FFFF8000
|
|
187 |
asm("je lookup_special_1"); // branch if must be process
|
|
188 |
asm("dec eax"); // test for current thread handle
|
|
189 |
asm("je current_thread_handle");
|
|
190 |
asm("test cl, cl");
|
|
191 |
asm("jnz lookup_handle_bad_no_unlock");
|
|
192 |
asm("inc eax");
|
|
193 |
asm("lookup_special_1:");
|
|
194 |
asm("test eax, eax");
|
|
195 |
asm("jnz lookup_handle_bad_no_unlock");
|
|
196 |
asm("mov edi, [edi+%0]": : "i" _FOFF(DThread,iOwningProcess));
|
|
197 |
asm("current_thread_handle:");
|
|
198 |
asm("mov [esp+4], edi"); // handle OK so replace handle with DObject pointer
|
|
199 |
asm("ret");
|
|
200 |
|
|
201 |
#ifdef __OBSOLETE_V1_IPC_SUPPORT__
|
|
202 |
asm("lookup_thread_pseudo:");
|
|
203 |
asm("cmp cl, 1"); // must be thread or unspecified
|
|
204 |
asm("ja lookup_handle_bad_no_unlock");
|
|
205 |
asm("mov ecx, %0" : : "i" (K_MsgInfo)); // ecx->msg chunk info
|
|
206 |
asm("add ax, ax"); // shift off no-close bit
|
|
207 |
asm("add eax, eax"); // shift off top bit, eax = alleged message offset
|
|
208 |
asm("test eax, %0" : : "i" (RMessageK_AlignMask)); // check alignment
|
|
209 |
asm("jne bad_message_handle"); // reject if misaligned
|
|
210 |
asm("lea edx, [eax+%0]" : : "i" (sizeof_RMessageK)); // edx = alleged message offset + sizeof RMessageK
|
|
211 |
asm("cmp edx, [ecx+%0]" : : "i" (offset_SMsgInfo_iMaxSize));
|
|
212 |
asm("ja lookup_handle_bad_no_unlock"); // reject if offset+size > msg chunk max size
|
|
213 |
asm("add eax, [ecx+%0]" : : "i" (offset_SMsgInfo_iBase)); // eax = message address
|
|
214 |
asm("jmp lookup_message_handle2");
|
|
215 |
#endif
|
|
216 |
|
|
217 |
asm("lookup_message_handle:");
|
|
218 |
asm("mov ecx, %0" : : "i" (K_MsgInfo)); // ecx->msg chunk info
|
|
219 |
asm("mov edx, eax"); // edx = handle
|
|
220 |
asm("sub edx, [ecx+%0]" : : "i" (offset_SMsgInfo_iBase)); // edx = handle - kernel msg chunk base
|
|
221 |
asm("jb bad_message_handle"); // reject if offset is negative
|
|
222 |
asm("test edx, %0" : : "i" (RMessageK_AlignMask)); // check alignment
|
|
223 |
asm("jne bad_message_handle"); // reject if misaligned
|
|
224 |
asm("add edx, %0": : "i" (sizeof_RMessageK));
|
|
225 |
asm("cmp edx, [ecx+%0]" : : "i" (offset_SMsgInfo_iMaxSize));
|
|
226 |
asm("ja bad_message_handle"); // reject if offset+size > msg chunk max size
|
|
227 |
#ifdef __OBSOLETE_V1_IPC_SUPPORT__
|
|
228 |
asm("lookup_message_handle2:");
|
|
229 |
#endif
|
|
230 |
asm("lea edx, bad_message_handle"); // if exception occurs, handle is bad
|
|
231 |
asm("mov [edi+%0], edx": : "i" _FOFF(DX86PlatThread,iMagicExcHandler));
|
|
232 |
asm("mov edx, [eax+%0]": : "i" (offset_RMessageK_iNext)); // edx should be complement of message address
|
|
233 |
asm("mov ecx, [eax+%0]": : "i" (offset_RMessageK_iPrev)); // ecx should be complement of server process address
|
|
234 |
asm("xor edx, eax"); // should be 0xffffffff
|
|
235 |
asm("xor ecx, [edi+%0]": : "i" _FOFF(DThread,iOwningProcess)); // should be 0xffffffff
|
|
236 |
asm("and ecx, edx"); // should be 0xffffffff
|
|
237 |
asm("mov dword ptr [edi+%0], 0": : "i" _FOFF(DX86PlatThread,iMagicExcHandler));
|
|
238 |
asm("inc ecx"); // this should be zero
|
|
239 |
asm("jne bad_message_handle"); // if not, bad
|
|
240 |
asm("cmp bl, %0": : "i" (EIpcMessageD)); // allow disconnect?
|
|
241 |
asm("je lookup_msg_ok");
|
|
242 |
asm("mov ecx, [eax+%0]": : "i" _FOFF(RMessageK,iFunction));
|
|
243 |
asm("cmp ecx, %0": : "i" (RMessage2_EDisConnect)); // check function
|
|
244 |
asm("je bad_message_handle"); // disconnect is not allowed
|
|
245 |
asm("cmp bl, %0": : "i" (EIpcMessage)); // message or client?
|
|
246 |
asm("je lookup_msg_ok"); // if message, finished
|
|
247 |
asm("mov edx, [eax+%0]": : "i" _FOFF(RMessageK,iClient));
|
|
248 |
asm("mov [esp+4], edx"); // if client, replace first arg with client ptr
|
|
249 |
asm("lookup_msg_ok:");
|
|
250 |
asm("ret");
|
|
251 |
|
|
252 |
// XXX we only take the handle-reading spin lock if we're not dealing with a
|
|
253 |
// special handle (same as in the ARM implementation). In that case, we
|
|
254 |
// shouldn't release the spinlock when they decide that the handle they've
|
|
255 |
// been given is bad.
|
|
256 |
asm("lookup_handle_bad:");
|
|
257 |
#ifdef __HANDLES_USE_RW_SPIN_LOCK__
|
|
258 |
XXX TODO release the read lock
|
|
259 |
#else
|
|
260 |
// System lock held on entry and exit, so don't release here
|
|
261 |
#endif
|
|
262 |
asm("lookup_handle_bad_no_unlock:");
|
|
263 |
// K::PanicCurrentThread(EBadHandle) - x86gcc can't handle "mov dword ptr [esp], %a0" : : "i" (EBadHandle)
|
|
264 |
// EBadHandle is 0
|
|
265 |
asm("mov dword ptr [esp], %0": :"i" (EBadHandle));
|
|
266 |
asm("call %a0": :"i"(K_PanicCurrentThread));
|
|
267 |
|
|
268 |
asm("bad_message_handle:");
|
|
269 |
|
|
270 |
// ((DX86PlatThread*)TheCurrentThread)->iMagicExcHandler = 0;
|
|
271 |
asm("call %a0" : : "i" (NKern_CurrentThread));
|
|
272 |
asm("sub eax, %0": :"i" (offset_DThread_iNThread));
|
|
273 |
asm("mov dword ptr [eax+%0], 0": :"i" _FOFF(DX86PlatThread,iMagicExcHandler));
|
|
274 |
|
|
275 |
// K::PanicCurrentThread(EBadMessageHandle);
|
|
276 |
asm("mov dword ptr [esp], %0": :"i" (EBadMessageHandle));
|
|
277 |
asm("call %a0": :"i" (K_PanicCurrentThread));
|
|
278 |
}
|
|
279 |
|
|
280 |
static __NAKED__ TInt Kern__HalFunction()
|
|
281 |
{
|
|
282 |
asm("mov eax, %0": : "i" (EExecHalFunction));
|
|
283 |
asm("push ebx");
|
|
284 |
asm("push esi");
|
|
285 |
asm("mov ecx, [esp+12]");
|
|
286 |
asm("mov edx, [esp+16]");
|
|
287 |
asm("mov ebx, [esp+20]");
|
|
288 |
asm("mov esi, [esp+24]");
|
|
289 |
asm("int 0x21");
|
|
290 |
asm("pop esi");
|
|
291 |
asm("pop ebx");
|
|
292 |
asm("ret");
|
|
293 |
}
|
|
294 |
|
|
295 |
EXPORT_C __NAKED__ TInt Kern::HalFunction(TInt /*aGroup*/, TInt /*aFunction*/, TAny* /*a1*/, TAny* /*a2*/, TInt aDeviceNumber)
|
|
296 |
//
|
|
297 |
// Execute a HAL function
|
|
298 |
// This must be done as an exec to get the correct permissions when calling from supervisor mode.
|
|
299 |
//
|
|
300 |
{
|
|
301 |
asm("mov eax, [esp+20]");
|
|
302 |
asm("shl eax, 16");
|
|
303 |
asm("or [esp+4], eax");
|
|
304 |
asm("jmp %a0" : : "i" (&Kern__HalFunction));
|
|
305 |
}
|
|
306 |
|
|
307 |
EXPORT_C __NAKED__ TInt Kern::HalFunction(TInt /*aGroup*/, TInt /*aFunction*/, TAny* /*a1*/, TAny* /*a2*/)
|
|
308 |
//
|
|
309 |
// Execute a HAL function
|
|
310 |
// This must be done as an exec to get the correct permissions when calling from supervisor mode.
|
|
311 |
//
|
|
312 |
{
|
|
313 |
asm("jmp %a0": :"i" (&Kern__HalFunction));
|
|
314 |
}
|
|
315 |
|