1 | ;------------------------------------------------------------------------------
|
---|
2 | ; @file
|
---|
3 | ; Transition from 16 bit real mode into 32 bit flat protected mode
|
---|
4 | ;
|
---|
5 | ; Copyright (c) 2020, Rebecca Cran <[email protected]>. All rights reserved.<BR>
|
---|
6 | ; Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>
|
---|
7 | ; SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
8 | ;
|
---|
9 | ;------------------------------------------------------------------------------
|
---|
10 |
|
---|
11 | %define SEC_DEFAULT_CR0 0x00000023
|
---|
12 | %define SEC_DEFAULT_CR4 0x640
|
---|
13 |
|
---|
14 | BITS 16
|
---|
15 |
|
---|
16 | ;
|
---|
17 | ; Modified: EAX, EBX
|
---|
18 | ;
|
---|
19 | ; @param[out] DS Selector allowing flat access to all addresses
|
---|
20 | ; @param[out] ES Selector allowing flat access to all addresses
|
---|
21 | ; @param[out] FS Selector allowing flat access to all addresses
|
---|
22 | ; @param[out] GS Selector allowing flat access to all addresses
|
---|
23 | ; @param[out] SS Selector allowing flat access to all addresses
|
---|
24 | ;
|
---|
25 | TransitionFromReal16To32BitFlat:
|
---|
26 |
|
---|
27 | debugShowPostCode POSTCODE_16BIT_MODE
|
---|
28 |
|
---|
29 | cli
|
---|
30 |
|
---|
31 | mov bx, 0xf000
|
---|
32 | mov ds, bx
|
---|
33 |
|
---|
34 | mov bx, ADDR16_OF(gdtr)
|
---|
35 |
|
---|
36 | o32 lgdt [cs:bx]
|
---|
37 |
|
---|
38 | mov eax, SEC_DEFAULT_CR0
|
---|
39 | mov cr0, eax
|
---|
40 |
|
---|
41 | jmp LINEAR_CODE_SEL:dword ADDR_OF(jumpTo32BitAndLandHere)
|
---|
42 | BITS 32
|
---|
43 | jumpTo32BitAndLandHere:
|
---|
44 |
|
---|
45 | mov eax, SEC_DEFAULT_CR4
|
---|
46 | mov cr4, eax
|
---|
47 |
|
---|
48 | debugShowPostCode POSTCODE_32BIT_MODE
|
---|
49 |
|
---|
50 | mov ax, LINEAR_SEL
|
---|
51 | mov ds, ax
|
---|
52 | mov es, ax
|
---|
53 | mov fs, ax
|
---|
54 | mov gs, ax
|
---|
55 | mov ss, ax
|
---|
56 |
|
---|
57 | OneTimeCallRet TransitionFromReal16To32BitFlat
|
---|
58 |
|
---|
59 | ALIGN 2
|
---|
60 |
|
---|
61 | gdtr:
|
---|
62 | dw GDT_END - GDT_BASE - 1 ; GDT limit
|
---|
63 | dd ADDR_OF(GDT_BASE)
|
---|
64 |
|
---|
65 | ALIGN 16
|
---|
66 |
|
---|
67 | ;
|
---|
68 | ; Macros for GDT entries
|
---|
69 | ;
|
---|
70 |
|
---|
71 | %define PRESENT_FLAG(p) (p << 7)
|
---|
72 | %define DPL(dpl) (dpl << 5)
|
---|
73 | %define SYSTEM_FLAG(s) (s << 4)
|
---|
74 | %define DESC_TYPE(t) (t)
|
---|
75 |
|
---|
76 | ; Type: data, expand-up, writable, accessed
|
---|
77 | %define DATA32_TYPE 3
|
---|
78 |
|
---|
79 | ; Type: execute, readable, expand-up, accessed
|
---|
80 | %define CODE32_TYPE 0xb
|
---|
81 |
|
---|
82 | ; Type: execute, readable, expand-up, accessed
|
---|
83 | %define CODE64_TYPE 0xb
|
---|
84 |
|
---|
85 | %define GRANULARITY_FLAG(g) (g << 7)
|
---|
86 | %define DEFAULT_SIZE32(d) (d << 6)
|
---|
87 | %define CODE64_FLAG(l) (l << 5)
|
---|
88 | %define UPPER_LIMIT(l) (l)
|
---|
89 |
|
---|
90 | ;
|
---|
91 | ; The Global Descriptor Table (GDT)
|
---|
92 | ;
|
---|
93 |
|
---|
94 | GDT_BASE:
|
---|
95 | ; null descriptor
|
---|
96 | NULL_SEL equ $-GDT_BASE
|
---|
97 | DW 0 ; limit 15:0
|
---|
98 | DW 0 ; base 15:0
|
---|
99 | DB 0 ; base 23:16
|
---|
100 | DB 0 ; sys flag, dpl, type
|
---|
101 | DB 0 ; limit 19:16, flags
|
---|
102 | DB 0 ; base 31:24
|
---|
103 |
|
---|
104 | ; linear data segment descriptor
|
---|
105 | LINEAR_SEL equ $-GDT_BASE
|
---|
106 | DW 0xffff ; limit 15:0
|
---|
107 | DW 0 ; base 15:0
|
---|
108 | DB 0 ; base 23:16
|
---|
109 | DB PRESENT_FLAG(1)|DPL(0)|SYSTEM_FLAG(1)|DESC_TYPE(DATA32_TYPE)
|
---|
110 | DB GRANULARITY_FLAG(1)|DEFAULT_SIZE32(1)|CODE64_FLAG(0)|UPPER_LIMIT(0xf)
|
---|
111 | DB 0 ; base 31:24
|
---|
112 |
|
---|
113 | ; linear code segment descriptor
|
---|
114 | LINEAR_CODE_SEL equ $-GDT_BASE
|
---|
115 | DW 0xffff ; limit 15:0
|
---|
116 | DW 0 ; base 15:0
|
---|
117 | DB 0 ; base 23:16
|
---|
118 | DB PRESENT_FLAG(1)|DPL(0)|SYSTEM_FLAG(1)|DESC_TYPE(CODE32_TYPE)
|
---|
119 | DB GRANULARITY_FLAG(1)|DEFAULT_SIZE32(1)|CODE64_FLAG(0)|UPPER_LIMIT(0xf)
|
---|
120 | DB 0 ; base 31:24
|
---|
121 |
|
---|
122 | %ifdef ARCH_X64
|
---|
123 | ; linear code (64-bit) segment descriptor
|
---|
124 | LINEAR_CODE64_SEL equ $-GDT_BASE
|
---|
125 | DW 0xffff ; limit 15:0
|
---|
126 | DW 0 ; base 15:0
|
---|
127 | DB 0 ; base 23:16
|
---|
128 | DB PRESENT_FLAG(1)|DPL(0)|SYSTEM_FLAG(1)|DESC_TYPE(CODE64_TYPE)
|
---|
129 | DB GRANULARITY_FLAG(1)|DEFAULT_SIZE32(0)|CODE64_FLAG(1)|UPPER_LIMIT(0xf)
|
---|
130 | DB 0 ; base 31:24
|
---|
131 | %endif
|
---|
132 |
|
---|
133 | ; linear code segment descriptor
|
---|
134 | LINEAR_CODE16_SEL equ $-GDT_BASE
|
---|
135 | DW 0xffff ; limit 15:0
|
---|
136 | DW 0 ; base 15:0
|
---|
137 | DB 0 ; base 23:16
|
---|
138 | DB PRESENT_FLAG(1)|DPL(0)|SYSTEM_FLAG(1)|DESC_TYPE(CODE32_TYPE)
|
---|
139 | DB GRANULARITY_FLAG(1)|DEFAULT_SIZE32(0)|CODE64_FLAG(0)|UPPER_LIMIT(0xf)
|
---|
140 | DB 0 ; base 31:24
|
---|
141 |
|
---|
142 | GDT_END:
|
---|
143 |
|
---|