1 /** @file
2 Initialize GDT for Linux.
3
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "LoadLinuxLib.h"
16
17
18 //
19 // Local structure definitions
20 //
21
22 #pragma pack (1)
23
24 //
25 // Global Descriptor Entry structures
26 //
27
28 typedef struct _GDT_ENTRY {
29 UINT16 Limit15_0;
30 UINT16 Base15_0;
31 UINT8 Base23_16;
32 UINT8 Type;
33 UINT8 Limit19_16_and_flags;
34 UINT8 Base31_24;
35 } GDT_ENTRY;
36
37 typedef
38 struct _GDT_ENTRIES {
39 GDT_ENTRY Null;
40 GDT_ENTRY Null2;
41 GDT_ENTRY Linear;
42 GDT_ENTRY LinearCode;
43 GDT_ENTRY TaskSegment;
44 GDT_ENTRY Spare4;
45 GDT_ENTRY Spare5;
46 } GDT_ENTRIES;
47
48 #pragma pack ()
49
50 STATIC GDT_ENTRIES *mGdt = NULL;
51
52 //
53 // Global descriptor table (GDT) Template
54 //
55 STATIC GDT_ENTRIES GdtTemplate = {
56 //
57 // Null
58 //
59 {
60 0x0, // limit 15:0
61 0x0, // base 15:0
62 0x0, // base 23:16
63 0x0, // type
64 0x0, // limit 19:16, flags
65 0x0, // base 31:24
66 },
67 //
68 // Null2
69 //
70 {
71 0x0, // limit 15:0
72 0x0, // base 15:0
73 0x0, // base 23:16
74 0x0, // type
75 0x0, // limit 19:16, flags
76 0x0, // base 31:24
77 },
78 //
79 // Linear
80 //
81 {
82 0x0FFFF, // limit 0xFFFFF
83 0x0, // base 0
84 0x0,
85 0x09A, // present, ring 0, data, expand-up, writable
86 0x0CF, // page-granular, 32-bit
87 0x0,
88 },
89 //
90 // LinearCode
91 //
92 {
93 0x0FFFF, // limit 0xFFFFF
94 0x0, // base 0
95 0x0,
96 0x092, // present, ring 0, data, expand-up, writable
97 0x0CF, // page-granular, 32-bit
98 0x0,
99 },
100 //
101 // TaskSegment
102 //
103 {
104 0x0, // limit 0
105 0x0, // base 0
106 0x0,
107 0x089, // ?
108 0x080, // ?
109 0x0,
110 },
111 //
112 // Spare4
113 //
114 {
115 0x0, // limit 0
116 0x0, // base 0
117 0x0,
118 0x0, // present, ring 0, data, expand-up, writable
119 0x0, // page-granular, 32-bit
120 0x0,
121 },
122 //
123 // Spare5
124 //
125 {
126 0x0, // limit 0
127 0x0, // base 0
128 0x0,
129 0x0, // present, ring 0, data, expand-up, writable
130 0x0, // page-granular, 32-bit
131 0x0,
132 },
133 };
134
135 /**
136 Initialize Global Descriptor Table.
137
138 **/
139 VOID
InitLinuxDescriptorTables(VOID)140 InitLinuxDescriptorTables (
141 VOID
142 )
143 {
144 //
145 // Allocate Runtime Data for the GDT
146 //
147 mGdt = AllocateRuntimePool (sizeof (GdtTemplate) + 8);
148 ASSERT (mGdt != NULL);
149 mGdt = ALIGN_POINTER (mGdt, 8);
150
151 //
152 // Initialize all GDT entries
153 //
154 CopyMem (mGdt, &GdtTemplate, sizeof (GdtTemplate));
155
156 }
157
158 /**
159 Initialize Global Descriptor Table.
160
161 **/
162 VOID
SetLinuxDescriptorTables(VOID)163 SetLinuxDescriptorTables (
164 VOID
165 )
166 {
167 IA32_DESCRIPTOR GdtPtr;
168 IA32_DESCRIPTOR IdtPtr;
169
170 //
171 // Write GDT register
172 //
173 GdtPtr.Base = (UINT32)(UINTN)(VOID*) mGdt;
174 GdtPtr.Limit = (UINT16) (sizeof (GdtTemplate) - 1);
175 AsmWriteGdtr (&GdtPtr);
176
177 IdtPtr.Base = (UINT32) 0;
178 IdtPtr.Limit = (UINT16) 0;
179 AsmWriteIdtr (&IdtPtr);
180 }
181
182