b1dd1e5b1cded1c87e2f29306b961c168cd591fe
[reactos.git] / reactos / ntoskrnl / ps / i386 / psctx.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ps/i386/pxctx.c
5 * PURPOSE: Process Manager: Set/Get Context for i386
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 /* FUNCTIONS ******************************************************************/
16
17 VOID
18 NTAPI
19 PspGetContext(IN PKTRAP_FRAME TrapFrame,
20 IN PVOID NonVolatileContext,
21 IN OUT PCONTEXT Context)
22 {
23 PAGED_CODE();
24
25 /* Convert the trap frame to a context */
26 KeTrapFrameToContext(TrapFrame, NULL, Context);
27 }
28
29 VOID
30 NTAPI
31 PspSetContext(OUT PKTRAP_FRAME TrapFrame,
32 OUT PVOID NonVolatileContext,
33 IN PCONTEXT Context,
34 IN KPROCESSOR_MODE Mode)
35 {
36 PAGED_CODE();
37
38 /* Convert the context to a trap frame structure */
39 KeContextToTrapFrame(Context, NULL, TrapFrame, Context->ContextFlags, Mode);
40 }
41
42 VOID
43 NTAPI
44 PspGetOrSetContextKernelRoutine(IN PKAPC Apc,
45 IN OUT PKNORMAL_ROUTINE* NormalRoutine,
46 IN OUT PVOID* NormalContext,
47 IN OUT PVOID* SystemArgument1,
48 IN OUT PVOID* SystemArgument2)
49 {
50 PGET_SET_CTX_CONTEXT GetSetContext;
51 PKEVENT Event;
52 PCONTEXT Context;
53 PKTHREAD Thread;
54 KPROCESSOR_MODE Mode;
55 PKTRAP_FRAME TrapFrame = NULL;
56 PAGED_CODE();
57
58 /* Get the Context Structure */
59 GetSetContext = CONTAINING_RECORD(Apc, GET_SET_CTX_CONTEXT, Apc);
60 Context = &GetSetContext->Context;
61 Event = &GetSetContext->Event;
62 Mode = GetSetContext->Mode;
63 Thread = Apc->SystemArgument2;
64
65 /* If this is a kernel-mode request, grab the saved trap frame */
66 if (Mode == KernelMode) TrapFrame = Thread->TrapFrame;
67
68 /* If we don't have one, grab it from the stack */
69 if (!TrapFrame)
70 {
71 /* Trap frame is right under our initial stack */
72 TrapFrame = (PKTRAP_FRAME)((ULONG_PTR)Thread->InitialStack -
73 ROUND_UP(sizeof(KTRAP_FRAME), KTRAP_FRAME_ALIGN) -
74 sizeof(FX_SAVE_AREA));
75 }
76
77 /* Check if it's a set or get */
78 if (Apc->SystemArgument1)
79 {
80 /* Get the Context */
81 PspSetContext(TrapFrame, NULL, Context, Mode);
82 }
83 else
84 {
85 /* Set the Context */
86 PspGetContext(TrapFrame, NULL, Context);
87 }
88
89 /* Notify the Native API that we are done */
90 KeSetEvent(Event, IO_NO_INCREMENT, FALSE);
91 }
92
93 /* EOF */