2 C based implemention of IA32 interrupt handling only
3 requiring a minimal assembly interrupt entry point.
5 Copyright (c) 2006 - 2009, Intel Corporation
6 All rights reserved. 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
35 UINT8 limit19_16_and_flags
;
46 GDT_ENTRY LinearCode64
;
51 #define NULL_SEL OFFSET_OF (GDT_ENTRIES, Null)
52 #define LINEAR_SEL OFFSET_OF (GDT_ENTRIES, Linear)
53 #define LINEAR_CODE_SEL OFFSET_OF (GDT_ENTRIES, LinearCode)
54 #define SYS_DATA_SEL OFFSET_OF (GDT_ENTRIES, SysData)
55 #define SYS_CODE_SEL OFFSET_OF (GDT_ENTRIES, SysCode)
56 #define LINEAR_CODE64_SEL OFFSET_OF (GDT_ENTRIES, LinearCode64)
57 #define SPARE4_SEL OFFSET_OF (GDT_ENTRIES, Spare4)
58 #define SPARE5_SEL OFFSET_OF (GDT_ENTRIES, Spare5)
60 #if defined (MDE_CPU_IA32)
61 #define CPU_CODE_SEL LINEAR_CODE_SEL
62 #define CPU_DATA_SEL LINEAR_SEL
63 #elif defined (MDE_CPU_X64)
64 #define CPU_CODE_SEL LINEAR_CODE64_SEL
65 #define CPU_DATA_SEL LINEAR_SEL
67 #error CPU type not supported for CPU GDT initialization!
71 // Global descriptor table (GDT) Template
73 STATIC GDT_ENTRIES GdtTemplate
= {
82 0x0, // limit 19:16, flags
89 0x0FFFF, // limit 0xFFFFF
92 0x092, // present, ring 0, data, expand-up, writable
93 0x0CF, // page-granular, 32-bit
100 0x0FFFF, // limit 0xFFFFF
103 0x09A, // present, ring 0, data, expand-up, writable
104 0x0CF, // page-granular, 32-bit
111 0x0FFFF, // limit 0xFFFFF
114 0x092, // present, ring 0, data, expand-up, writable
115 0x0CF, // page-granular, 32-bit
122 0x0FFFF, // limit 0xFFFFF
125 0x09A, // present, ring 0, data, expand-up, writable
126 0x0CF, // page-granular, 32-bit
133 0x0FFFF, // limit 0xFFFFF
136 0x09B, // present, ring 0, code, expand-up, writable
137 0x0AF, // LimitHigh (CS.L=1, CS.D=0)
147 0x0, // present, ring 0, data, expand-up, writable
148 0x0, // page-granular, 32-bit
158 0x0, // present, ring 0, data, expand-up, writable
159 0x0, // page-granular, 32-bit
165 Initialize Global Descriptor Table
169 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
= 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
);