Flat32.asm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ;; @file
  2. ; This is the code that goes from real-mode to protected mode.
  3. ; It consumes the reset vector, configures the stack.
  4. ;
  5. ; Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
  6. ; SPDX-License-Identifier: BSD-2-Clause-Patent
  7. ;;
  8. ;
  9. ; Define assembler characteristics
  10. ;
  11. .586p
  12. .xmm
  13. .model flat, c
  14. EXTRN TempRamInitApi:NEAR
  15. EXTRN FspInitApi:NEAR
  16. ;
  17. ; Contrary to the name, this file contains 16 bit code as well.
  18. ;
  19. _TEXT_REALMODE SEGMENT PARA PUBLIC USE16 'CODE'
  20. ASSUME CS:_TEXT_REALMODE, DS:_TEXT_REALMODE
  21. ;----------------------------------------------------------------------------
  22. ;
  23. ; Procedure: _ModuleEntryPoint
  24. ;
  25. ; Input: None
  26. ;
  27. ; Output: None
  28. ;
  29. ; Destroys: Assume all registers
  30. ;
  31. ; Description:
  32. ;
  33. ; Transition to non-paged flat-model protected mode from a
  34. ; hard-coded GDT that provides exactly two descriptors.
  35. ; This is a bare bones transition to protected mode only
  36. ; used for a while in PEI and possibly DXE.
  37. ;
  38. ; After enabling protected mode, a far jump is executed to
  39. ; transfer to PEI using the newly loaded GDT.
  40. ;
  41. ; Return: None
  42. ;
  43. ;----------------------------------------------------------------------------
  44. align 16
  45. _ModuleEntryPoint PROC C PUBLIC
  46. ;
  47. ; Load the GDT table in GdtDesc
  48. ;
  49. mov esi, OFFSET GdtDesc
  50. db 66h
  51. lgdt fword ptr cs:[si]
  52. ;
  53. ; Transition to 16 bit protected mode
  54. ;
  55. mov eax, cr0 ; Get control register 0
  56. or eax, 00000003h ; Set PE bit (bit #0) & MP bit (bit #1)
  57. mov cr0, eax ; Activate protected mode
  58. ;
  59. ; Now we're in 16 bit protected mode
  60. ; Set up the selectors for 32 bit protected mode entry
  61. ;
  62. mov ax, SYS_DATA_SEL
  63. mov ds, ax
  64. mov es, ax
  65. mov fs, ax
  66. mov gs, ax
  67. mov ss, ax
  68. ;
  69. ; Transition to Flat 32 bit protected mode
  70. ; The jump to a far pointer causes the transition to 32 bit mode
  71. ;
  72. mov esi, offset ProtectedModeEntryLinearAddress
  73. jmp fword ptr cs:[si]
  74. _ModuleEntryPoint ENDP
  75. _TEXT_REALMODE ENDS
  76. .code
  77. ;
  78. ; Protected mode portion initializes stack, configures cache, and calls C entry point
  79. ;
  80. ;----------------------------------------------------------------------------
  81. ;
  82. ; Procedure: ProtectedModeEntryPoint
  83. ;
  84. ; Input: Executing in 32 Bit Protected (flat) mode
  85. ; cs: 0-4GB
  86. ; ds: 0-4GB
  87. ; es: 0-4GB
  88. ; fs: 0-4GB
  89. ; gs: 0-4GB
  90. ; ss: 0-4GB
  91. ;
  92. ; Output: This function never returns
  93. ;
  94. ; Destroys:
  95. ; ecx
  96. ; edi
  97. ; esi
  98. ; esp
  99. ;
  100. ; Description:
  101. ; Perform any essential early platform initilaisation
  102. ; Setup a stack
  103. ;
  104. ;----------------------------------------------------------------------------
  105. ProtectedModeEntryPoint PROC NEAR C PUBLIC
  106. ;
  107. ; Dummy function. Consume 2 API to make sure they can be linked.
  108. ;
  109. mov eax, TempRamInitApi
  110. mov eax, FspInitApi
  111. ; Should never return
  112. jmp $
  113. ProtectedModeEntryPoint ENDP
  114. ;
  115. ; ROM-based Global-Descriptor Table for the PEI Phase
  116. ;
  117. align 16
  118. PUBLIC BootGdtTable
  119. ;
  120. ; GDT[0]: 0x00: Null entry, never used.
  121. ;
  122. NULL_SEL equ $ - GDT_BASE ; Selector [0]
  123. GDT_BASE:
  124. BootGdtTable DD 0
  125. DD 0
  126. ;
  127. ; Linear code segment descriptor
  128. ;
  129. LINEAR_CODE_SEL equ $ - GDT_BASE ; Selector [0x8]
  130. DW 0FFFFh ; limit 0xFFFF
  131. DW 0 ; base 0
  132. DB 0
  133. DB 09Bh ; present, ring 0, data, expand-up, not-writable
  134. DB 0CFh ; page-granular, 32-bit
  135. DB 0
  136. ;
  137. ; System data segment descriptor
  138. ;
  139. SYS_DATA_SEL equ $ - GDT_BASE ; Selector [0x10]
  140. DW 0FFFFh ; limit 0xFFFF
  141. DW 0 ; base 0
  142. DB 0
  143. DB 093h ; present, ring 0, data, expand-up, not-writable
  144. DB 0CFh ; page-granular, 32-bit
  145. DB 0
  146. GDT_SIZE EQU $ - BootGDTtable ; Size, in bytes
  147. ;
  148. ; GDT Descriptor
  149. ;
  150. GdtDesc: ; GDT descriptor
  151. DW GDT_SIZE - 1 ; GDT limit
  152. DD OFFSET BootGdtTable ; GDT base address
  153. ProtectedModeEntryLinearAddress LABEL FWORD
  154. ProtectedModeEntryLinearOffset LABEL DWORD
  155. DD OFFSET ProtectedModeEntryPoint ; Offset of our 32 bit code
  156. DW LINEAR_CODE_SEL
  157. END