ReactOS  0.4.13-dev-73-gcfe54aa
arch.c File Reference
#include "bl.h"
Include dependency graph for arch.c:

Go to the source code of this file.

Functions

VOID DECLSPEC_NORETURN ArchTrapNoProcess (VOID)
 
VOID ArchSwitchContext (_In_ PBL_ARCH_CONTEXT NewContext, _In_ PBL_ARCH_CONTEXT OldContext)
 
NTSTATUS ArchInitializeContext (_In_ PBL_ARCH_CONTEXT Context)
 
NTSTATUS ArchInitializeContexts (VOID)
 
VOID BlpArchSwitchContext (_In_ BL_ARCH_MODE NewMode)
 
VOID BlpArchEnableTranslation (VOID)
 
NTSTATUS BlpArchInitialize (_In_ ULONG Phase)
 

Variables

BL_ARCH_CONTEXT FirmwareExecutionContext
 
BL_ARCH_CONTEXT ApplicationExecutionContext
 
PBL_ARCH_CONTEXT CurrentExecutionContext
 

Function Documentation

◆ ArchInitializeContext()

NTSTATUS ArchInitializeContext ( _In_ PBL_ARCH_CONTEXT  Context)

Definition at line 85 of file arch.c.

88 {
90 
91  /* Are we initializing real mode? */
92  if (Context->Mode == BlRealMode)
93  {
94  /* Disable paging, enable interrupts */
95  Context->ContextFlags &= ~BL_CONTEXT_PAGING_ON;
96  Context->ContextFlags |= BL_CONTEXT_INTERRUPTS_ON;
97  }
100  {
101  /* Read the current translation type */
102  Context->TranslationType = BlpLibraryParameters.TranslationType;
103 
104  /* Disable paging (it's already on), enable interrupts */
105  Context->ContextFlags &= ~BL_CONTEXT_PAGING_ON;
106  Context->ContextFlags |= BL_CONTEXT_INTERRUPTS_ON;
107 
108  /* Enable FXSR support in the FPU */
110  }
111  else
112  {
113  /* Invalid context */
115  }
116 
117  /* Return context status */
118  return Status;
119 }
#define BL_APPLICATION_FLAG_CONVERTED_FROM_EFI
Definition: bl.h:52
__INTRIN_INLINE void __writecr4(unsigned int Data)
Definition: intrin_x86.h:1680
BL_LIBRARY_PARAMETERS BlpLibraryParameters
Definition: bootlib.c:15
LONG NTSTATUS
Definition: precomp.h:26
#define CR4_FXSR
Definition: ketypes.h:92
#define BL_CONTEXT_PAGING_ON
Definition: bl.h:84
Definition: bl.h:240
Definition: bl.h:231
__INTRIN_INLINE unsigned long __readcr4(void)
Definition: intrin_x86.h:1713
ULONG BlpApplicationFlags
Definition: bootlib.c:21
Status
Definition: gdiplustypes.h:24
ULONG TranslationType
Definition: bl.h:759
#define BL_CONTEXT_INTERRUPTS_ON
Definition: bl.h:85
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:409
return STATUS_SUCCESS
Definition: btrfs.c:2725

Referenced by ArchInitializeContexts().

◆ ArchInitializeContexts()

NTSTATUS ArchInitializeContexts ( VOID  )

Definition at line 122 of file arch.c.

125 {
127  NTSTATUS EfiStatus, AppStatus;
128 
129  /* No current context */
131 
132  /* Setup the EFI and Application modes respectively */
135 
136  /* Initialize application mode */
138  if (NT_SUCCESS(AppStatus))
139  {
140  /* Set it as current if it worked */
143  }
144 
145  /* Initialize EFI mode */
147  if (NT_SUCCESS(EfiStatus))
148  {
149  /* Set it as current if application context failed */
150  if (!NT_SUCCESS(AppStatus))
151  {
154  }
155 
156  /* Switch to application mode, or EFI if that one failed */
158  EfiStatus = STATUS_SUCCESS;
159  }
160 
161  /* Return initialization state */
162  return EfiStatus;
163 }
BL_ARCH_CONTEXT FirmwareExecutionContext
Definition: arch.c:15
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS ArchInitializeContext(_In_ PBL_ARCH_CONTEXT Context)
Definition: arch.c:85
smooth NULL
Definition: ftsmooth.c:416
Definition: bl.h:240
VOID ArchSwitchContext(_In_ PBL_ARCH_CONTEXT NewContext, _In_ PBL_ARCH_CONTEXT OldContext)
Definition: arch.c:39
BL_ARCH_CONTEXT ApplicationExecutionContext
Definition: arch.c:16
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
PBL_ARCH_CONTEXT CurrentExecutionContext
Definition: arch.c:17
BL_ARCH_MODE Mode
Definition: bl.h:1000
return STATUS_SUCCESS
Definition: btrfs.c:2725

Referenced by BlpArchInitialize().

◆ ArchSwitchContext()

VOID ArchSwitchContext ( _In_ PBL_ARCH_CONTEXT  NewContext,
_In_ PBL_ARCH_CONTEXT  OldContext 
)

Definition at line 39 of file arch.c.

43 {
44  /* Are we switching to real mode? */
45  if (NewContext->Mode == BlRealMode)
46  {
47  /* Disable paging */
49 
50  /* Are we coming from PAE mode? */
51  if ((OldContext != NULL) && (OldContext->TranslationType == BlPae))
52  {
53  /* Turn off PAE */
55  }
56 
57  /* Enable interrupts */
58  _enable();
59  }
60  else
61  {
62  /* Switching to protected mode -- disable interrupts if needed */
63  if (!(NewContext->ContextFlags & BL_CONTEXT_INTERRUPTS_ON))
64  {
65  _disable();
66  }
67 
68  /* Are we enabling paging for the first time? */
69  if (NewContext->ContextFlags & BL_CONTEXT_PAGING_ON)
70  {
71  /* In PAE mode? */
72  if (NewContext->TranslationType == BlPae)
73  {
74  /* Turn on PAE */
76  }
77 
78  /* Turn on paging */
80  }
81  }
82 }
__INTRIN_INLINE void __writecr4(unsigned int Data)
Definition: intrin_x86.h:1680
void __cdecl _enable(void)
Definition: intrin_arm.h:373
#define BL_CONTEXT_PAGING_ON
Definition: bl.h:84
smooth NULL
Definition: ftsmooth.c:416
__INTRIN_INLINE void __writecr0(unsigned int Data)
Definition: intrin_x86.h:1670
Definition: bl.h:240
_In_ FLT_SET_CONTEXT_OPERATION _In_ PFLT_CONTEXT _Outptr_opt_result_maybenull_ PFLT_CONTEXT * OldContext
Definition: fltkernel.h:1468
__INTRIN_INLINE unsigned long __readcr0(void)
Definition: intrin_x86.h:1692
#define CR0_PG
Definition: asm.h:255
__INTRIN_INLINE unsigned long __readcr4(void)
Definition: intrin_x86.h:1713
#define BL_CONTEXT_INTERRUPTS_ON
Definition: bl.h:85
#define CR4_PAE
Definition: ketypes.h:89
_In_ FLT_SET_CONTEXT_OPERATION _In_ PFLT_CONTEXT NewContext
Definition: fltkernel.h:1468
void __cdecl _disable(void)
Definition: intrin_arm.h:365
Definition: bl.h:233

Referenced by ArchInitializeContexts(), and BlpArchSwitchContext().

◆ ArchTrapNoProcess()

VOID DECLSPEC_NORETURN ArchTrapNoProcess ( VOID  )

Definition at line 23 of file arch.c.

26 {
27  /* Do nothing, this is an unsupported debugging interrupt */
28 #if defined(__GNUC__)
29  __asm__ __volatile__ ("iret");
30 #elif defined (_MSC_VER)
31  _asm { iret };
32 #else
33 #error wtf are you using
34 #endif
35  __assume(0);
36 }
__asm__("\t.globl GetPhys\n" "GetPhys:\t\n" "mflr 0\n\t" "stwu 0,-16(1)\n\t" "mfmsr 5\n\t" "andi. 6,5,0xffef\n\t" "mtmsr 6\n\t" "isync\n\t" "sync\n\t" "lwz 3,0(3)\n\t" "mtmsr 5\n\t" "isync\n\t" "sync\n\t" "lwz 0,0(1)\n\t" "addi 1,1,16\n\t" "mtlr 0\n\t" "blr")

Referenced by BlpArchInitialize().

◆ BlpArchEnableTranslation()

VOID BlpArchEnableTranslation ( VOID  )

Definition at line 189 of file arch.c.

192 {
194 
195  /* Does the current execution context already have paging enabled? */
197  if (!(Context->ContextFlags & BL_CONTEXT_PAGING_ON))
198  {
199  /* No -- does it have interrupts enabled? */
200  if (Context->ContextFlags & BL_CONTEXT_INTERRUPTS_ON)
201  {
202  /* Disable them */
203  _disable();
204  Context->ContextFlags &= ~BL_CONTEXT_INTERRUPTS_ON;
205  }
206 
207  /* Are we enabling PAE? */
208  if (Context->TranslationType == BlPae)
209  {
210  /* Make sure CR4 reflects this */
212  }
213 
214  /* Enable paging in the CPU */
216 
217  /* Reflect that paging is enabled */
218  Context->ContextFlags |= BL_CONTEXT_PAGING_ON;
219  }
220 }
__INTRIN_INLINE void __writecr4(unsigned int Data)
Definition: intrin_x86.h:1680
#define BL_CONTEXT_PAGING_ON
Definition: bl.h:84
__INTRIN_INLINE void __writecr0(unsigned int Data)
Definition: intrin_x86.h:1670
__INTRIN_INLINE unsigned long __readcr0(void)
Definition: intrin_x86.h:1692
#define CR0_PG
Definition: asm.h:255
__INTRIN_INLINE unsigned long __readcr4(void)
Definition: intrin_x86.h:1713
#define BL_CONTEXT_INTERRUPTS_ON
Definition: bl.h:85
#define CR4_PAE
Definition: ketypes.h:89
PBL_ARCH_CONTEXT CurrentExecutionContext
Definition: arch.c:17
void __cdecl _disable(void)
Definition: intrin_arm.h:365
struct tagContext Context
Definition: acpixf.h:1012
Definition: bl.h:233

Referenced by MmDefInitializeTranslation().

◆ BlpArchInitialize()

NTSTATUS BlpArchInitialize ( _In_ ULONG  Phase)

Definition at line 235 of file arch.c.

238 {
239  KDESCRIPTOR Idtr;
240  USHORT CodeSegment;
242  PKIDTENTRY IdtBase;
243 
244  /* Assume success */
246 
247  /* Is this phase 1? */
248  if (Phase != 0)
249  {
250  /* Get the IDT */
251  __sidt(&Idtr);
252  IdtBase = (PKIDTENTRY)Idtr.Base;
253 
254  /* Get the Code Segment */
255 #if defined(__GNUC__)
256  __asm__ __volatile__ ("mov %%cs,%0\n\t" :"=r" (CodeSegment));
257 #elif defined (_MSC_VER)
258  _asm { mov CodeSegment, cs };
259 #else
260 #error wtf are you using
261 #endif
262 
263  /* Set up INT 3, ASSERT, and SECURITY_ASSERT to be no-op (for Rtl) */
264  IdtBase[3].Offset = (USHORT)(ULONG_PTR)ArchTrapNoProcess;
265  IdtBase[3].Selector = CodeSegment;
266  IdtBase[3].Access = 0x8E00u;
267  IdtBase[3].ExtendedOffset = (ULONG_PTR)ArchTrapNoProcess >> 16;
268  IdtBase[0x2C].Offset = (USHORT)(ULONG_PTR)ArchTrapNoProcess;
269  IdtBase[0x2C].Selector = CodeSegment;
270  IdtBase[0x2C].Access = 0x8E00u;
271  IdtBase[0x2C].ExtendedOffset = (ULONG_PTR)ArchTrapNoProcess >> 16;
272  IdtBase[0x2D].Offset = (USHORT)(ULONG_PTR)ArchTrapNoProcess;
273  IdtBase[0x2D].Selector = CodeSegment;
274  IdtBase[0x2D].Access = 0x8E00u;
275  IdtBase[0x2D].ExtendedOffset = (ULONG_PTR)ArchTrapNoProcess >> 16;
276 
277  /* Write the IDT back */
278  Idtr.Base = (ULONG)IdtBase;
279  __lidt(&Idtr);
280 
281  /* Reset FPU state */
282 #if defined(__GNUC__)
283  __asm__ __volatile__ ("fninit");
284 #elif defined (_MSC_VER)
285  _asm { fninit };
286 #else
287 #error wtf are you using
288 #endif
289  }
290  else
291  {
292  /* Reset TSC if needed */
293  if ((__readmsr(0x10) >> 32) & 0xFFC00000)
294  {
295  __writemsr(0x10, 0);
296  }
297 
298  /* Initialize all the contexts */
300  }
301 
302  /* Return initialization state */
303  return Status;
304 }
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
#define __GNUC__
Definition: _icc.h:38
LONG NTSTATUS
Definition: precomp.h:26
__INTRIN_INLINE void __lidt(void *Source)
Definition: intrin_x86.h:1915
USHORT Access
Definition: ketypes.h:386
PVOID Base
Definition: ketypes.h:486
uint32_t ULONG_PTR
Definition: typedefs.h:63
uint32_t cs
Definition: isohybrid.c:75
__asm__("\t.globl GetPhys\n" "GetPhys:\t\n" "mflr 0\n\t" "stwu 0,-16(1)\n\t" "mfmsr 5\n\t" "andi. 6,5,0xffef\n\t" "mtmsr 6\n\t" "isync\n\t" "sync\n\t" "lwz 3,0(3)\n\t" "mtmsr 5\n\t" "isync\n\t" "sync\n\t" "lwz 0,0(1)\n\t" "addi 1,1,16\n\t" "mtlr 0\n\t" "blr")
__INTRIN_INLINE void __sidt(void *Destination)
Definition: intrin_x86.h:1920
NTSTATUS ArchInitializeContexts(VOID)
Definition: arch.c:122
PPC_QUAL unsigned long long __readmsr()
Definition: intrin_ppc.h:741
USHORT Offset
Definition: ketypes.h:384
USHORT ExtendedOffset
Definition: ketypes.h:387
#define PKIDTENTRY
Definition: ketypes.h:480
PPC_QUAL void __writemsr(const unsigned long Value)
Definition: intrin_ppc.h:748
Status
Definition: gdiplustypes.h:24
unsigned short USHORT
Definition: pedump.c:61
VOID DECLSPEC_NORETURN ArchTrapNoProcess(VOID)
Definition: arch.c:23
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
return STATUS_SUCCESS
Definition: btrfs.c:2725
USHORT Selector
Definition: ketypes.h:385

◆ BlpArchSwitchContext()

VOID BlpArchSwitchContext ( _In_ BL_ARCH_MODE  NewMode)

Definition at line 166 of file arch.c.

169 {
171 
172  /* In real mode, use EFI, otherwise, use the application mode */
174  if (NewMode != BlRealMode)
175  {
177  }
178 
179  /* Are we in a different mode? */
180  if (CurrentExecutionContext->Mode != NewMode)
181  {
182  /* Switch to the new one */
185  }
186 }
BL_ARCH_CONTEXT FirmwareExecutionContext
Definition: arch.c:15
Definition: bl.h:240
VOID ArchSwitchContext(_In_ PBL_ARCH_CONTEXT NewContext, _In_ PBL_ARCH_CONTEXT OldContext)
Definition: arch.c:39
BL_ARCH_CONTEXT ApplicationExecutionContext
Definition: arch.c:16
PBL_ARCH_CONTEXT CurrentExecutionContext
Definition: arch.c:17
struct tagContext Context
Definition: acpixf.h:1012
BL_ARCH_MODE Mode
Definition: bl.h:1000

Variable Documentation

◆ ApplicationExecutionContext

BL_ARCH_CONTEXT ApplicationExecutionContext

Definition at line 16 of file arch.c.

Referenced by ArchInitializeContexts(), and BlpArchSwitchContext().

◆ CurrentExecutionContext

PBL_ARCH_CONTEXT CurrentExecutionContext

Definition at line 17 of file arch.c.

Referenced by ArchInitializeContexts(), BlpArchEnableTranslation(), and BlpArchSwitchContext().

◆ FirmwareExecutionContext

BL_ARCH_CONTEXT FirmwareExecutionContext

Definition at line 15 of file arch.c.

Referenced by ArchInitializeContexts(), and BlpArchSwitchContext().