1;------------------------------------------------------------------------------
2; @file
3; Transition from 16 bit real mode into 32 bit flat protected mode
4;
5; Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>
6; This program and the accompanying materials
7; are licensed and made available under the terms and conditions of the BSD License
8; which accompanies this distribution.  The full text of the license may be found at
9; http://opensource.org/licenses/bsd-license.php
10;
11; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13;
14;------------------------------------------------------------------------------
15
16%define SEC_DEFAULT_CR0  0x40000023
17%define SEC_DEFAULT_CR4  0x640
18
19BITS    16
20
21;
22; Modified:  EAX, EBX
23;
24TransitionFromReal16To32BitFlat:
25
26    debugShowPostCode POSTCODE_16BIT_MODE
27
28    cli
29
30    mov     bx, 0xf000
31    mov     ds, bx
32
33    mov     bx, ADDR16_OF(gdtr)
34
35o32 lgdt    [cs:bx]
36
37    mov     eax, SEC_DEFAULT_CR0
38    mov     cr0, eax
39
40    jmp     LINEAR_CODE_SEL:dword ADDR_OF(jumpTo32BitAndLandHere)
41BITS    32
42jumpTo32BitAndLandHere:
43
44    mov     eax, SEC_DEFAULT_CR4
45    mov     cr4, eax
46
47    debugShowPostCode POSTCODE_32BIT_MODE
48
49    mov     ax, LINEAR_SEL
50    mov     ds, ax
51    mov     es, ax
52    mov     fs, ax
53    mov     gs, ax
54    mov     ss, ax
55
56    OneTimeCallRet TransitionFromReal16To32BitFlat
57
58ALIGN   2
59
60gdtr:
61    dw      GDT_END - GDT_BASE - 1   ; GDT limit
62    dd      ADDR_OF(GDT_BASE)
63
64ALIGN   16
65
66;
67; Macros for GDT entries
68;
69
70%define  PRESENT_FLAG(p) (p << 7)
71%define  DPL(dpl) (dpl << 5)
72%define  SYSTEM_FLAG(s) (s << 4)
73%define  DESC_TYPE(t) (t)
74
75; Type: data, expand-up, writable, accessed
76%define  DATA32_TYPE 3
77
78; Type: execute, readable, expand-up, accessed
79%define  CODE32_TYPE 0xb
80
81; Type: execute, readable, expand-up, accessed
82%define  CODE64_TYPE 0xb
83
84%define  GRANULARITY_FLAG(g) (g << 7)
85%define  DEFAULT_SIZE32(d) (d << 6)
86%define  CODE64_FLAG(l) (l << 5)
87%define  UPPER_LIMIT(l) (l)
88
89;
90; The Global Descriptor Table (GDT)
91;
92
93GDT_BASE:
94; null descriptor
95NULL_SEL            equ $-GDT_BASE
96    DW      0            ; limit 15:0
97    DW      0            ; base 15:0
98    DB      0            ; base 23:16
99    DB      0            ; sys flag, dpl, type
100    DB      0            ; limit 19:16, flags
101    DB      0            ; base 31:24
102
103; linear data segment descriptor
104LINEAR_SEL          equ $-GDT_BASE
105    DW      0xffff       ; limit 15:0
106    DW      0            ; base 15:0
107    DB      0            ; base 23:16
108    DB      PRESENT_FLAG(1)|DPL(0)|SYSTEM_FLAG(1)|DESC_TYPE(DATA32_TYPE)
109    DB      GRANULARITY_FLAG(1)|DEFAULT_SIZE32(1)|CODE64_FLAG(0)|UPPER_LIMIT(0xf)
110    DB      0            ; base 31:24
111
112; linear code segment descriptor
113LINEAR_CODE_SEL     equ $-GDT_BASE
114    DW      0xffff       ; limit 15:0
115    DW      0            ; base 15:0
116    DB      0            ; base 23:16
117    DB      PRESENT_FLAG(1)|DPL(0)|SYSTEM_FLAG(1)|DESC_TYPE(CODE32_TYPE)
118    DB      GRANULARITY_FLAG(1)|DEFAULT_SIZE32(1)|CODE64_FLAG(0)|UPPER_LIMIT(0xf)
119    DB      0            ; base 31:24
120
121%ifdef ARCH_X64
122; linear code (64-bit) segment descriptor
123LINEAR_CODE64_SEL   equ $-GDT_BASE
124    DW      0xffff       ; limit 15:0
125    DW      0            ; base 15:0
126    DB      0            ; base 23:16
127    DB      PRESENT_FLAG(1)|DPL(0)|SYSTEM_FLAG(1)|DESC_TYPE(CODE64_TYPE)
128    DB      GRANULARITY_FLAG(1)|DEFAULT_SIZE32(0)|CODE64_FLAG(1)|UPPER_LIMIT(0xf)
129    DB      0            ; base 31:24
130%endif
131
132GDT_END:
133
134