- NtUserWaitForInputIdle: Call EngGetTickCount, removing duplicated code
[reactos.git] / reactos / ntoskrnl / ke / i386 / usercall_asm.S
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/ke/i386/usercall_asm.S
5 * PURPOSE: User-Mode callbacks and return.
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include <asm.h>
12 #include <internal/i386/asmmacro.S>
13 .intel_syntax noprefix
14
15 /* FUNCTIONS ****************************************************************/
16
17 .globl _KiGetUserModeStackAddress@0
18 .func KiGetUserModeStackAddress@0
19 _KiGetUserModeStackAddress@0:
20
21 /* Get the current thread's trapframe and return the esp */
22 mov eax, fs:[KPCR_CURRENT_THREAD]
23 mov eax, [eax+KTHREAD_TRAP_FRAME]
24 lea eax, [eax+KTRAP_FRAME_ESP]
25 ret
26
27 .endfunc
28
29 /*++
30 * @name KiCallUserMode
31 *
32 * The KiSwitchToUserMode routine sets up a Trap Frame and a Callback stack
33 * for the purpose of switching to user mode. The actual final jump is done
34 * by KiServiceExit which will treat this as a syscall return.
35 *
36 * @param OutputBuffer
37 * Pointer to a caller-allocated buffer where to receive the return data
38 * from the user-mode function
39 *
40 * @param OutputLength
41 * Size of the Output Buffer described above.
42 *
43 * @return None. Jumps into KiServiceExit.
44 *
45 * @remark If there is not enough Kernel Stack space, the routine will increase the
46 * Kernel Stack.
47 *
48 * User mode execution resumes at ntdll!KiUserCallbackDispatcher.
49 *
50 * This call MUST be paired by interrupt 0x2B or NtCallbackReturn.
51 *
52 *--*/
53 .globl _KiCallUserMode@8
54 .func KiCallUserMode@8
55 _KiCallUserMode@8:
56
57 /* Save volatile registers */
58 push ebp
59 push ebx
60 push esi
61 push edi
62
63 /* Get the current thread */
64 mov ebx, fs:[KPCR_CURRENT_THREAD]
65
66 /* Make sure we're at passive */
67 #if DBG
68 call _KeGetCurrentIrql@0
69 or al, al
70 jz AtPassive
71
72 /* We're not, bugcheck! */
73 push 0
74 push 0
75 push eax
76 push 0
77 push IRQL_GT_ZERO_AT_SYSTEM_SERVICE
78 call _KeBugCheckEx@20
79
80 AtPassive:
81
82 /* Make sure that we are not attached and that APCs are not disabled */
83 movzx eax, byte ptr [ebx+KTHREAD_APC_STATE_INDEX]
84 mov edx, [ebx+KTHREAD_COMBINED_APC_DISABLE]
85 or eax, eax
86 jnz InvalidIndex
87 or edx, edx
88 jz ApcsEnabled
89
90 InvalidIndex:
91
92 push 0
93 push edx
94 push eax
95 push 0
96 push APC_INDEX_MISMATCH
97 call _KeBugCheckEx@20
98 ApcsEnabled:
99 #endif
100
101 /* Get the lowest stack limit and check if we can handle it */
102 lea eax, [esp-0x3000]
103 cmp eax, [ebx+KTHREAD_STACK_LIMIT]
104 jnb StackOk
105
106 /* We can't, we'll have to grow our stack */
107 push esp
108 call _MmGrowKernelStack@4
109
110 /* Quit if we failed */
111 or eax, eax
112 jnz GrowFailed
113
114 /* Save the current callback stack */
115 StackOk:
116 push [ebx+KTHREAD_CALLBACK_STACK]
117
118 /* Get and save the trap frame */
119 mov edx, [ebx+KTHREAD_TRAP_FRAME]
120 push edx
121
122 /* Get and save the initial stack */
123 mov esi, [ebx+KTHREAD_INITIAL_STACK]
124 push esi
125
126 /* Set the new callback stack */
127 mov [ebx+KTHREAD_CALLBACK_STACK], esp
128
129 /* Align stack on 16-byte boundary */
130 and esp, ~15
131 mov edi, esp
132
133 /* Set destination and origin NPX Areas */
134 sub esp, NPX_FRAME_LENGTH
135 sub esi, NPX_FRAME_LENGTH
136
137 /* Disable interrupts so we can fill the NPX State */
138 cli
139
140 /* Now copy the NPX State */
141 mov ecx, [esi+FP_CONTROL_WORD]
142 mov [esp+FP_CONTROL_WORD], ecx
143 mov ecx, [esi+FP_STATUS_WORD]
144 mov [esp+FP_STATUS_WORD], ecx
145 mov ecx, [esi+FP_TAG_WORD]
146 mov [esp+FP_TAG_WORD], ecx
147 mov ecx, [esi+FP_DATA_SELECTOR]
148 mov [esp+FP_DATA_SELECTOR], ecx
149 mov ecx, [esi+FN_CR0_NPX_STATE]
150 mov [esp+FN_CR0_NPX_STATE], ecx
151
152 /* Get TSS */
153 mov esi, fs:[KPCR_TSS]
154
155 /* Set the stack address */
156 mov [ebx+KTHREAD_INITIAL_STACK], edi
157
158 /* Bias the stack for V86 mode */
159 mov ecx, esp
160 sub esp, 16
161 test dword ptr [edx+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
162 jnz DontBias
163 mov ecx, esp
164
165 DontBias:
166 /* Set new stack address in TSS */
167 mov [esi+KTSS_ESP0], ecx
168
169 /* Allocate the trap frame and set it */
170 sub esp, KTRAP_FRAME_V86_ES
171 mov ebp, esp
172
173 /* Set copy iterator and dest/origin parameters and do the copy */
174 mov ecx, (KTRAP_FRAME_V86_ES - KTRAP_FRAME_FS) / 4
175 lea edi, [esp+KTRAP_FRAME_FS]
176 lea esi, [edx+KTRAP_FRAME_FS]
177 rep movsd
178
179 /* Copy DR7 */
180 mov edi, [edx+KTRAP_FRAME_DR7]
181 test edi, ~DR7_RESERVED_MASK
182 mov [esp+KTRAP_FRAME_DR7], edi
183
184 /* Check if we need to save debug registers */
185 jnz SaveDebug
186
187 /* Get user-mode dispatcher address and set it as EIP */
188 SetEip:
189 mov eax, _KeUserCallbackDispatcher
190 mov [esp+KTRAP_FRAME_EIP], eax
191
192 /* Set the exception list */
193 mov eax, fs:[KPCR_EXCEPTION_LIST]
194 mov [esp+KTRAP_FRAME_EXCEPTION_LIST], eax
195
196 /* Set the previous mode */
197 mov eax, [edx+KTRAP_FRAME_PREVIOUS_MODE]
198 mov [esp+KTRAP_FRAME_PREVIOUS_MODE], eax
199
200 /* Bring interrupts back */
201 sti
202
203 /* Write the debug data */
204 SET_TF_DEBUG_HEADER
205
206 /* Exit to user-mode */
207 jmp _KiServiceExit
208
209 SaveDebug:
210
211 /* Copy all 5 DRs */
212 mov ecx, 5
213 lea edi, [esp+KTRAP_FRAME_DR0]
214 lea esi, [edx+KTRAP_FRAME_DR0]
215 rep movsd
216 jmp SetEip
217
218 GrowFailed:
219 /* Restore registers */
220 pop edi
221 pop esi
222 pop ebx
223 pop ebp
224
225 /* Return */
226 ret 8
227
228 .endfunc
229
230 /*++
231 * @name NtCallbackReturn
232 *
233 * The NtCallbackReturn routine returns to kernel mode after a user-mode
234 * callback was done through KeUserModeCallback. It uses the callback frame
235 * which was setup in order to return the information, restores the stack,
236 * and resumes execution where it was left off.
237 *
238 * @param Result
239 * Pointer to a caller-allocated buffer where the return data
240 * from the user-mode function is located.
241 *
242 * @param ResultLength
243 * Size of the Output Buffer described above.
244 *
245 * @param CallbackStatus
246 * Status code of the callback operation.
247 *
248 * @return Status code of the callback operation.
249 *
250 * @remark This call MUST be paired with KeUserModeCallback.
251 *
252 *--*/
253 .globl _NtCallbackReturn@12
254 .func NtCallbackReturn@12
255 _NtCallbackReturn@12:
256
257 /* Get the current thread and make sure we have a callback stack */
258 mov eax, fs:[KPCR_CURRENT_THREAD]
259 mov ecx, [eax+KTHREAD_CALLBACK_STACK]
260 test ecx, ecx
261 jz NoStack
262
263 /* Get the trap frame */
264 mov ebx, [eax+KTHREAD_TRAP_FRAME]
265
266 /* Restore the exception list */
267 mov edx, [ebx+KTRAP_FRAME_EXCEPTION_LIST]
268 mov fs:[KPCR_EXCEPTION_LIST], edx
269
270 /* Get the result, the result length and the status */
271 mov edi, [esp+4]
272 mov esi, [esp+8]
273 mov ebp, [esp+12]
274
275 /* Store the results in the callback stack */
276 mov ebx, [ecx+CBSTACK_RESULT]
277 mov [ebx], edi
278 mov ebx, [ecx+CBSTACK_RESULT_LENGTH]
279 mov [ebx], esi
280
281 /* Get the previous stack */
282 mov ebx, [ecx]
283
284 /* Disable interrupts for NPX save and stack switch */
285 cli
286
287 /* Get the initial stack and restore it */
288 mov esi, [eax+KTHREAD_INITIAL_STACK]
289 mov [eax+KTHREAD_INITIAL_STACK], ebx
290
291 /* Set desination and origin NPX Frames */
292 sub esi, NPX_FRAME_LENGTH
293 sub ebx, NPX_FRAME_LENGTH
294
295 /* Copy NPX Data */
296 mov edx, [esi+FP_CONTROL_WORD]
297 mov [ebx+FP_CONTROL_WORD], edx
298 mov edx, [esi+FP_STATUS_WORD]
299 mov [ebx+FP_STATUS_WORD], edx
300 mov edx, [esi+FP_TAG_WORD]
301 mov [ebx+FP_TAG_WORD], edx
302 mov edx, [esi+FP_DATA_SELECTOR]
303 mov [ebx+FP_DATA_SELECTOR], edx
304 mov edx, [esi+FN_CR0_NPX_STATE]
305 mov [ebx+FN_CR0_NPX_STATE], edx
306
307 /* Check if we failed in user mode */
308 cmp ebp, STATUS_CALLBACK_POP_STACK
309 mov edi, [ecx+CBSTACK_TRAP_FRAME]
310 jz UserFault
311
312 CheckDebug:
313
314 /* Clear DR7 */
315 and dword ptr [edi+KTRAP_FRAME_DR7], 0
316
317 /* Check if debugging was active */
318 test byte ptr [eax+KTHREAD_DEBUG_ACTIVE], 0xFF
319 jnz RestoreDebug
320
321 RestoreStack:
322
323 /* Get TSS */
324 mov edx, fs:[KPCR_TSS]
325
326 /* Restore stack pointer */
327 lea esp, [ecx+CBSTACK_CALLBACK_STACK]
328
329 /* Check if we were in V86 mode */
330 test dword ptr [edi+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
331 jnz V86Ret
332 sub ebx, 16
333
334 V86Ret:
335 /* Restore the ESP in TSS */
336 mov [edx+KTSS_ESP0], ebx
337
338 /* Restore the trap frame */
339 mov [eax+KTHREAD_TRAP_FRAME], edi
340
341 /* Bring interrupts back */
342 sti
343
344 /* Restore the callback stack*/
345 pop [eax+KTHREAD_CALLBACK_STACK]
346
347 /* Set status and return */
348 mov eax, ebp
349 pop edi
350 pop esi
351 pop ebx
352 pop ebp
353 pop edx
354
355 /* Clean stack and jump back */
356 add esp, 8
357 jmp edx
358
359 UserFault:
360 /* Set size to copy */
361 mov ecx, (KTRAP_FRAME_V86_ES - KTRAP_FRAME_FS) / 4
362
363 /* Check if this was V86 mode */
364 mov esi, [eax+KTHREAD_TRAP_FRAME]
365 test dword ptr [esi+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
366
367 /* Save EDI and load destination */
368 mov edx, edi
369 lea edi, [edi+KTRAP_FRAME_FS]
370 jz NotV86
371 add ecx, 16 / 4
372
373 NotV86:
374 /* Set source and copy */
375 lea esi, [esi+KTRAP_FRAME_FS]
376 rep movsd
377
378 /* Restore ECX and ECX */
379 mov ecx, [eax+KTHREAD_CALLBACK_STACK]
380 mov edi, edx
381 jmp CheckDebug
382
383 RestoreDebug:
384 /* Get a pointer to thread's trap frame */
385 mov esi, [eax+KTHREAD_TRAP_FRAME]
386
387 /* Copy debug registers data from it */
388 mov edx, [esi+KTRAP_FRAME_DR0]
389 mov [edi+KTRAP_FRAME_DR0], edx
390 mov edx, [esi+KTRAP_FRAME_DR1]
391 mov [edi+KTRAP_FRAME_DR1], edx
392 mov edx, [esi+KTRAP_FRAME_DR2]
393 mov [edi+KTRAP_FRAME_DR2], edx
394 mov edx, [esi+KTRAP_FRAME_DR3]
395 mov [edi+KTRAP_FRAME_DR3], edx
396 mov edx, [esi+KTRAP_FRAME_DR6]
397 mov [edi+KTRAP_FRAME_DR6], edx
398 mov edx, [esi+KTRAP_FRAME_DR7]
399 mov [edi+KTRAP_FRAME_DR7], edx
400
401 /* Jump back */
402 jmp RestoreStack
403
404 NoStack:
405
406 /* Return failure */
407 mov eax, STATUS_NO_CALLBACK_ACTIVE
408 ret 12
409 .endfunc
410
411 /*++
412 * @name KeSwitchKernelStack
413 *
414 * The KeSwitchKernelStack routine switches from the current thread's stack
415 * to the new specified base and limit.
416 *
417 * @param StackBase
418 * Pointer to the new Stack Base of the thread.
419 *
420 * @param StackLimit
421 * Pointer to the new Stack Limit of the thread.
422 *
423 * @return The previous Stack Base of the thread.
424 *
425 * @remark This routine should typically only be used when converting from a
426 * non-GUI to a GUI Thread. The caller is responsible for freeing the
427 * previous stack. The new stack values MUST be valid before calling
428 * this routine.
429 *
430 *--*/
431 .globl _KeSwitchKernelStack@8
432 .func KeSwitchKernelStack@8
433 _KeSwitchKernelStack@8:
434
435 /* Save volatiles */
436 push esi
437 push edi
438
439 /* Get current thread */
440 mov edx, fs:[KPCR_CURRENT_THREAD]
441
442 /* Get new and current base */
443 mov edi, [esp+12]
444 mov ecx, [edx+KTHREAD_STACK_BASE]
445
446 /* Fixup the frame pointer */
447 sub ebp, ecx
448 add ebp, edi
449
450 /* Fixup the trap frame */
451 mov eax, [edx+KTHREAD_TRAP_FRAME]
452 sub eax, ecx
453 add eax, edi
454 mov [edx+KTHREAD_TRAP_FRAME], eax
455
456 /* Calculate stack size */
457 sub ecx, esp
458
459 /* Get desination and origin */
460 sub edi, ecx
461 mov esi, esp
462
463 /* Save stack pointer */
464 push edi
465
466 /* Copy stack */
467 rep movsb
468
469 /* Restore stack pointer */
470 pop edi
471
472 /* Save old stack base and get new limit/base */
473 mov eax, [edx+KTHREAD_STACK_BASE]
474 mov ecx, [esp+12]
475 mov esi, [esp+16]
476
477 /* Disable interrupts for stack switch */
478 cli
479
480 /* Set new base/limit */
481 mov [edx+KTHREAD_STACK_BASE], ecx
482 mov [edx+KTHREAD_STACK_LIMIT], esi
483
484 /* Set LargeStack */
485 mov byte ptr [edx+KTHREAD_LARGE_STACK], 1
486
487 /* Set new initial stack */
488 mov [edx+KTHREAD_INITIAL_STACK], ecx
489
490 /* Get trap frame */
491 mov esi, [edx+KTHREAD_TRAP_FRAME]
492
493 /* Get TSS */
494 mov edx, fs:[KPCR_TSS]
495
496 /* Check if we came from V86 mode */
497 test dword ptr [esi+KTRAP_FRAME_EFLAGS], EFLAGS_V86_MASK
498
499 /* Bias for NPX Area */
500 lea ecx, [ecx-NPX_FRAME_LENGTH]
501 jnz V86Switch
502 sub ecx, 16
503
504 V86Switch:
505
506 /* Update ESP in TSS */
507 mov [edx+KTSS_ESP0], ecx
508
509 /* Update stack pointer */
510 mov esp, edi
511
512 /* Bring back interrupts and return */
513 sti
514 pop edi
515 pop esi
516 ret 8
517 .endfunc