ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

context.c
Go to the documentation of this file.
00001 /*
00002  * PROJECT:         ReactOS Kernel
00003  * LICENSE:         GPL - See COPYING in the top level directory
00004  * PURPOSE:         CONTEXT related functions
00005  * PROGRAMMERS:     Timo Kreuzer (timo.kreuzer@reactos.org)
00006  */
00007 
00008 /* INCLUDES ******************************************************************/
00009 
00010 #include <ntoskrnl.h>
00011 
00012 #define NDEBUG
00013 #include <debug.h>
00014 
00015 /* FUNCTIONS *****************************************************************/
00016 
00017 VOID
00018 NTAPI
00019 KeContextToTrapFrame(IN PCONTEXT Context,
00020                      IN OUT PKEXCEPTION_FRAME ExceptionFrame,
00021                      IN OUT PKTRAP_FRAME TrapFrame,
00022                      IN ULONG ContextFlags,
00023                      IN KPROCESSOR_MODE PreviousMode)
00024 {
00025     KIRQL OldIrql;
00026 
00027     /* Make sure we have an amd64 context, then remove the flag */
00028     ASSERT(ContextFlags & CONTEXT_AMD64);
00029     ContextFlags &= ~CONTEXT_AMD64;
00030 
00031     /* Do this at APC_LEVEL */
00032     OldIrql = KeGetCurrentIrql();
00033     if (OldIrql < APC_LEVEL) KeRaiseIrql(APC_LEVEL, &OldIrql);
00034 
00035     /* Handle integer registers */
00036     if (ContextFlags & CONTEXT_INTEGER)
00037     {
00038         TrapFrame->Rax = Context->Rax;
00039         TrapFrame->Rbx = Context->Rbx;
00040         TrapFrame->Rcx = Context->Rcx;
00041         TrapFrame->Rdx = Context->Rdx;
00042         TrapFrame->Rsi = Context->Rsi;
00043         TrapFrame->Rdi = Context->Rdi;
00044         TrapFrame->Rbp = Context->Rbp;
00045         TrapFrame->R8 = Context->R8;
00046         TrapFrame->R9 = Context->R9;
00047         TrapFrame->R10 = Context->R10;
00048         TrapFrame->R11 = Context->R11;
00049         if (ExceptionFrame)
00050         {
00051             ExceptionFrame->R12 = Context->R12;
00052             ExceptionFrame->R13 = Context->R13;
00053             ExceptionFrame->R14 = Context->R14;
00054             ExceptionFrame->R15 = Context->R15;
00055         }
00056     }
00057 
00058     /* Handle floating point registers */
00059     if ((ContextFlags & CONTEXT_FLOATING_POINT) &&
00060         (Context->SegCs & MODE_MASK))
00061     {
00062         TrapFrame->Xmm0 = Context->Xmm0;
00063         TrapFrame->Xmm1 = Context->Xmm1;
00064         TrapFrame->Xmm2 = Context->Xmm2;
00065         TrapFrame->Xmm3 = Context->Xmm3;
00066         TrapFrame->Xmm4 = Context->Xmm4;
00067         TrapFrame->Xmm5 = Context->Xmm5;
00068         if (ExceptionFrame)
00069         {
00070             ExceptionFrame->Xmm6 = Context->Xmm6;
00071             ExceptionFrame->Xmm7 = Context->Xmm7;
00072             ExceptionFrame->Xmm8 = Context->Xmm8;
00073             ExceptionFrame->Xmm9 = Context->Xmm9;
00074             ExceptionFrame->Xmm10 = Context->Xmm10;
00075             ExceptionFrame->Xmm11 = Context->Xmm11;
00076             ExceptionFrame->Xmm12 = Context->Xmm12;
00077             ExceptionFrame->Xmm13 = Context->Xmm13;
00078             ExceptionFrame->Xmm14 = Context->Xmm14;
00079             ExceptionFrame->Xmm15 = Context->Xmm15;
00080         }
00081     }
00082 
00083     /* Handle control registers */
00084     if (ContextFlags & CONTEXT_CONTROL)
00085     {
00086         /* Check if this was a Kernel Trap */
00087         if (Context->SegCs == KGDT64_R0_CODE)
00088         {
00089             /* Set valid selectors */
00090             TrapFrame->SegCs = KGDT64_R0_CODE;
00091             TrapFrame->SegSs = KGDT64_R0_DATA;
00092         }
00093         else
00094         {
00095             /* Copy selectors */
00096             TrapFrame->SegCs = Context->SegCs;
00097             TrapFrame->SegSs = Context->SegSs;
00098         }
00099 
00100         /* RIP, RSP, EFLAGS */
00101         TrapFrame->Rip = Context->Rip;
00102         TrapFrame->Rsp = Context->Rsp;
00103         TrapFrame->EFlags = Context->EFlags;
00104     }
00105 
00106     /* Handle segment selectors */
00107     if (ContextFlags & CONTEXT_SEGMENTS)
00108     {
00109         /* Check if this was a Kernel Trap */
00110         if (Context->SegCs == KGDT64_R0_CODE)
00111         {
00112             /* Set valid selectors */
00113             TrapFrame->SegDs = KGDT64_R3_DATA | RPL_MASK;
00114             TrapFrame->SegEs = KGDT64_R3_DATA | RPL_MASK;
00115             TrapFrame->SegFs = KGDT64_R3_CMTEB | RPL_MASK;
00116             TrapFrame->SegGs = KGDT64_R3_DATA | RPL_MASK;
00117         }
00118         else
00119         {
00120             /* Copy selectors */
00121             TrapFrame->SegDs = Context->SegDs;
00122             TrapFrame->SegEs = Context->SegEs;
00123             TrapFrame->SegFs = Context->SegFs;
00124             TrapFrame->SegGs = Context->SegGs;
00125         }
00126     }
00127 
00128     /* Handle debug registers */
00129     if (ContextFlags & CONTEXT_DEBUG_REGISTERS)
00130     {
00131         /* Copy the debug registers */
00132         TrapFrame->Dr0 = Context->Dr0;
00133         TrapFrame->Dr1 = Context->Dr1;
00134         TrapFrame->Dr2 = Context->Dr2;
00135         TrapFrame->Dr3 = Context->Dr3;
00136         TrapFrame->Dr6 = Context->Dr6;
00137         TrapFrame->Dr7 = Context->Dr7;
00138     }
00139 
00140     /* Restore IRQL */
00141     if (OldIrql < APC_LEVEL) KeLowerIrql(OldIrql);
00142 }
00143 
00144 VOID
00145 NTAPI
00146 KeTrapFrameToContext(IN PKTRAP_FRAME TrapFrame,
00147                      IN PKEXCEPTION_FRAME ExceptionFrame,
00148                      IN OUT PCONTEXT Context)
00149 {
00150     KIRQL OldIrql;
00151 
00152     /* Do this at APC_LEVEL */
00153     OldIrql = KeGetCurrentIrql();
00154     if (OldIrql < APC_LEVEL) KeRaiseIrql(APC_LEVEL, &OldIrql);
00155 
00156     /* Handle integer registers */
00157     if ((Context->ContextFlags & CONTEXT_INTEGER) == CONTEXT_INTEGER)
00158     {
00159         Context->Rax = TrapFrame->Rax;
00160         Context->Rbx = TrapFrame->Rbx;
00161         Context->Rcx = TrapFrame->Rcx;
00162         Context->Rdx = TrapFrame->Rdx;
00163         Context->Rsi = TrapFrame->Rsi;
00164         Context->Rdi = TrapFrame->Rdi;
00165         Context->Rbp = TrapFrame->Rbp;
00166         Context->R8 = TrapFrame->R8;
00167         Context->R9 = TrapFrame->R9;
00168         Context->R10 = TrapFrame->R10;
00169         Context->R11 = TrapFrame->R11;
00170 
00171         if (ExceptionFrame)
00172         {
00173             Context->R12 = ExceptionFrame->R12;
00174             Context->R13 = ExceptionFrame->R13;
00175             Context->R14 = ExceptionFrame->R14;
00176             Context->R15 = ExceptionFrame->R15;
00177         }
00178     }
00179 
00180     /* Handle floating point registers */
00181     if (((Context->ContextFlags & CONTEXT_FLOATING_POINT) ==
00182         CONTEXT_FLOATING_POINT) && (TrapFrame->SegCs & MODE_MASK))
00183     {
00184         Context->Xmm0 = TrapFrame->Xmm0;
00185         Context->Xmm1 = TrapFrame->Xmm1;
00186         Context->Xmm2 = TrapFrame->Xmm2;
00187         Context->Xmm3 = TrapFrame->Xmm3;
00188         Context->Xmm4 = TrapFrame->Xmm4;
00189         Context->Xmm5 = TrapFrame->Xmm5;
00190         if (ExceptionFrame)
00191         {
00192             Context->Xmm6 = ExceptionFrame->Xmm6;
00193             Context->Xmm7 = ExceptionFrame->Xmm7;
00194             Context->Xmm8 = ExceptionFrame->Xmm8;
00195             Context->Xmm9 = ExceptionFrame->Xmm9;
00196             Context->Xmm10 = ExceptionFrame->Xmm10;
00197             Context->Xmm11 = ExceptionFrame->Xmm11;
00198             Context->Xmm12 = ExceptionFrame->Xmm12;
00199             Context->Xmm13 = ExceptionFrame->Xmm13;
00200             Context->Xmm14 = ExceptionFrame->Xmm14;
00201             Context->Xmm15 = ExceptionFrame->Xmm15;
00202         }
00203     }
00204 
00205     /* Handle control registers */
00206     if ((Context->ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL)
00207     {
00208         /* Check if this was a Kernel Trap */
00209         if (TrapFrame->SegCs == KGDT64_R0_CODE)
00210         {
00211             /* Set valid selectors */
00212             Context->SegCs = KGDT64_R0_CODE;
00213             Context->SegSs = KGDT64_R0_DATA;
00214         }
00215         else
00216         {
00217             /* Copy selectors */
00218             Context->SegCs = TrapFrame->SegCs;
00219             Context->SegSs = TrapFrame->SegSs;
00220         }
00221 
00222         /* Copy RIP, RSP, EFLAGS */
00223         Context->Rip = TrapFrame->Rip;
00224         Context->Rsp = TrapFrame->Rsp;
00225         Context->EFlags = TrapFrame->EFlags;
00226     }
00227 
00228     /* Handle segment selectors */
00229     if ((Context->ContextFlags & CONTEXT_SEGMENTS) == CONTEXT_SEGMENTS)
00230     {
00231         /* Check if this was a Kernel Trap */
00232         if (TrapFrame->SegCs == KGDT64_R0_CODE)
00233         {
00234             /* Set valid selectors */
00235             Context->SegDs = KGDT64_R3_DATA | RPL_MASK;
00236             Context->SegEs = KGDT64_R3_DATA | RPL_MASK;
00237             Context->SegFs = KGDT64_R3_CMTEB | RPL_MASK;
00238             Context->SegGs = KGDT64_R3_DATA | RPL_MASK;
00239         }
00240         else
00241         {
00242             /* Copy selectors */
00243             Context->SegDs = TrapFrame->SegDs;
00244             Context->SegEs = TrapFrame->SegEs;
00245             Context->SegFs = TrapFrame->SegFs;
00246             Context->SegGs = TrapFrame->SegGs;
00247         }
00248     }
00249 
00250     /* Handle debug registers */
00251     if ((Context->ContextFlags & CONTEXT_DEBUG_REGISTERS) ==
00252         CONTEXT_DEBUG_REGISTERS)
00253     {
00254         /* Copy the debug registers */
00255         Context->Dr0 = TrapFrame->Dr0;
00256         Context->Dr1 = TrapFrame->Dr1;
00257         Context->Dr2 = TrapFrame->Dr2;
00258         Context->Dr3 = TrapFrame->Dr3;
00259         Context->Dr6 = TrapFrame->Dr6;
00260         Context->Dr7 = TrapFrame->Dr7;
00261     }
00262 
00263     /* Restore IRQL */
00264     if (OldIrql < APC_LEVEL) KeLowerIrql(OldIrql);
00265 }
00266 

Generated on Mon May 28 2012 04:19:58 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.