2 ; This is the code that goes from real-mode to protected mode.
3 ; It consumes the reset vector, configures the stack.
5 ; Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
6 ; SPDX-License-Identifier: BSD-2-Clause-Patent
10 ; Define assembler characteristics
16 EXTRN TempRamInitApi:NEAR
20 ; Contrary to the name, this file contains 16 bit code as well.
22 _TEXT_REALMODE SEGMENT PARA PUBLIC USE16 'CODE'
23 ASSUME CS:_TEXT_REALMODE, DS:_TEXT_REALMODE
25 ;----------------------------------------------------------------------------
27 ; Procedure: _ModuleEntryPoint
33 ; Destroys: Assume all registers
37 ; Transition to non-paged flat-model protected mode from a
38 ; hard-coded GDT that provides exactly two descriptors.
39 ; This is a bare bones transition to protected mode only
40 ; used for a while in PEI and possibly DXE.
42 ; After enabling protected mode, a far jump is executed to
43 ; transfer to PEI using the newly loaded GDT.
47 ;----------------------------------------------------------------------------
49 _ModuleEntryPoint PROC C PUBLIC
51 ; Load the GDT table in GdtDesc
53 mov esi, OFFSET GdtDesc
55 lgdt fword ptr cs:[si]
58 ; Transition to 16 bit protected mode
60 mov eax, cr0 ; Get control register 0
61 or eax, 00000003h ; Set PE bit (bit #0) & MP bit (bit #1)
62 mov cr0, eax ; Activate protected mode
65 ; Now we're in 16 bit protected mode
66 ; Set up the selectors for 32 bit protected mode entry
76 ; Transition to Flat 32 bit protected mode
77 ; The jump to a far pointer causes the transition to 32 bit mode
79 mov esi, offset ProtectedModeEntryLinearAddress
82 _ModuleEntryPoint ENDP
88 ; Protected mode portion initializes stack, configures cache, and calls C entry point
91 ;----------------------------------------------------------------------------
93 ; Procedure: ProtectedModeEntryPoint
95 ; Input: Executing in 32 Bit Protected (flat) mode
103 ; Output: This function never returns
112 ; Perform any essential early platform initilaisation
115 ;----------------------------------------------------------------------------
117 ProtectedModeEntryPoint PROC NEAR C PUBLIC
119 ; Dummy function. Consume 2 API to make sure they can be linked.
121 mov eax, TempRamInitApi
124 ; Should never return
127 ProtectedModeEntryPoint ENDP
130 ; ROM-based Global-Descriptor Table for the PEI Phase
136 ; GDT[0]: 0x00: Null entry, never used.
138 NULL_SEL equ $ - GDT_BASE ; Selector [0]
143 ; Linear code segment descriptor
145 LINEAR_CODE_SEL equ $ - GDT_BASE ; Selector [0x8]
146 DW 0FFFFh ; limit 0xFFFF
149 DB 09Bh ; present, ring 0, data, expand-up, not-writable
150 DB 0CFh ; page-granular, 32-bit
153 ; System data segment descriptor
155 SYS_DATA_SEL equ $ - GDT_BASE ; Selector [0x10]
156 DW 0FFFFh ; limit 0xFFFF
159 DB 093h ; present, ring 0, data, expand-up, not-writable
160 DB 0CFh ; page-granular, 32-bit
163 GDT_SIZE EQU $ - BootGDTtable ; Size, in bytes
168 GdtDesc: ; GDT descriptor
169 DW GDT_SIZE - 1 ; GDT limit
170 DD OFFSET BootGdtTable ; GDT base address
172 ProtectedModeEntryLinearAddress LABEL FWORD
173 ProtectedModeEntryLinearOffset LABEL DWORD
174 DD OFFSET ProtectedModeEntryPoint ; Offset of our 32 bit code