2 C based implemention of IA32 interrupt handling only
3 requiring a minimal assembly interrupt entry point.
5 Copyright (c) 2006 - 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
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.
20 // Local structure definitions
26 // Global Descriptor Entry structures
29 typedef struct _GDT_ENTRY
{
34 UINT8 Limit19_16_and_flags
;
45 GDT_ENTRY LinearCode64
;
50 #define NULL_SEL OFFSET_OF (GDT_ENTRIES, Null)
51 #define LINEAR_SEL OFFSET_OF (GDT_ENTRIES, Linear)
52 #define LINEAR_CODE_SEL OFFSET_OF (GDT_ENTRIES, LinearCode)
53 #define SYS_DATA_SEL OFFSET_OF (GDT_ENTRIES, SysData)
54 #define SYS_CODE_SEL OFFSET_OF (GDT_ENTRIES, SysCode)
55 #define LINEAR_CODE64_SEL OFFSET_OF (GDT_ENTRIES, LinearCode64)
56 #define SPARE4_SEL OFFSET_OF (GDT_ENTRIES, Spare4)
57 #define SPARE5_SEL OFFSET_OF (GDT_ENTRIES, Spare5)
59 #if defined (MDE_CPU_IA32)
60 #define CPU_CODE_SEL LINEAR_CODE_SEL
61 #define CPU_DATA_SEL LINEAR_SEL
62 #elif defined (MDE_CPU_X64)
63 #define CPU_CODE_SEL LINEAR_CODE64_SEL
64 #define CPU_DATA_SEL LINEAR_SEL
66 #error CPU type not supported for CPU GDT initialization!
70 // Global descriptor table (GDT) Template
72 STATIC GDT_ENTRIES GdtTemplate
= {
81 0x0, // limit 19:16, flags
88 0x0FFFF, // limit 0xFFFFF
91 0x092, // present, ring 0, data, expand-up, writable
92 0x0CF, // page-granular, 32-bit
99 0x0FFFF, // limit 0xFFFFF
102 0x09A, // present, ring 0, data, expand-up, writable
103 0x0CF, // page-granular, 32-bit
110 0x0FFFF, // limit 0xFFFFF
113 0x092, // present, ring 0, data, expand-up, writable
114 0x0CF, // page-granular, 32-bit
121 0x0FFFF, // limit 0xFFFFF
124 0x09A, // present, ring 0, data, expand-up, writable
125 0x0CF, // page-granular, 32-bit
132 0x0FFFF, // limit 0xFFFFF
135 0x09B, // present, ring 0, code, expand-up, writable
136 0x0AF, // LimitHigh (CS.L=1, CS.D=0)
146 0x0, // present, ring 0, data, expand-up, writable
147 0x0, // page-granular, 32-bit
157 0x0, // present, ring 0, data, expand-up, writable
158 0x0, // page-granular, 32-bit
164 Initialize Global Descriptor Table.
168 InitGlobalDescriptorTable (
173 IA32_DESCRIPTOR gdtPtr
;
176 // Allocate Runtime Data for the GDT
178 gdt
= AllocateRuntimePool (sizeof (GdtTemplate
) + 8);
179 ASSERT (gdt
!= NULL
);
180 gdt
= ALIGN_POINTER (gdt
, 8);
183 // Initialize all GDT entries
185 CopyMem (gdt
, &GdtTemplate
, sizeof (GdtTemplate
));
188 // Write GDT register
190 gdtPtr
.Base
= (UINT32
)(UINTN
)(VOID
*) gdt
;
191 gdtPtr
.Limit
= (UINT16
) (sizeof (GdtTemplate
) - 1);
192 AsmWriteGdtr (&gdtPtr
);
195 // Update selector (segment) registers base on new GDT
197 SetCodeSelector ((UINT16
)CPU_CODE_SEL
);
198 SetDataSelectors ((UINT16
)CPU_DATA_SEL
);