]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/CpuDxe/CpuGdt.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuGdt.c
1 /** @file
2 C based implementation of IA32 interrupt handling only
3 requiring a minimal assembly interrupt entry point.
4
5 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "CpuDxe.h"
11 #include "CpuGdt.h"
12
13 //
14 // Global descriptor table (GDT) Template
15 //
16 STATIC GDT_ENTRIES GdtTemplate = {
17 //
18 // NULL_SEL
19 //
20 {
21 0x0, // limit 15:0
22 0x0, // base 15:0
23 0x0, // base 23:16
24 0x0, // type
25 0x0, // limit 19:16, flags
26 0x0, // base 31:24
27 },
28 //
29 // LINEAR_SEL
30 //
31 {
32 0x0FFFF, // limit 15:0
33 0x0, // base 15:0
34 0x0, // base 23:16
35 0x092, // present, ring 0, data, read/write
36 0x0CF, // page-granular, 32-bit
37 0x0,
38 },
39 //
40 // LINEAR_CODE_SEL
41 //
42 {
43 0x0FFFF, // limit 15:0
44 0x0, // base 15:0
45 0x0, // base 23:16
46 0x09F, // present, ring 0, code, execute/read, conforming, accessed
47 0x0CF, // page-granular, 32-bit
48 0x0,
49 },
50 //
51 // SYS_DATA_SEL
52 //
53 {
54 0x0FFFF, // limit 15:0
55 0x0, // base 15:0
56 0x0, // base 23:16
57 0x093, // present, ring 0, data, read/write, accessed
58 0x0CF, // page-granular, 32-bit
59 0x0,
60 },
61 //
62 // SYS_CODE_SEL
63 //
64 {
65 0x0FFFF, // limit 15:0
66 0x0, // base 15:0
67 0x0, // base 23:16
68 0x09A, // present, ring 0, code, execute/read
69 0x0CF, // page-granular, 32-bit
70 0x0,
71 },
72 //
73 // SPARE4_SEL
74 //
75 {
76 0x0, // limit 15:0
77 0x0, // base 15:0
78 0x0, // base 23:16
79 0x0, // type
80 0x0, // limit 19:16, flags
81 0x0, // base 31:24
82 },
83 //
84 // LINEAR_DATA64_SEL
85 //
86 {
87 0x0FFFF, // limit 15:0
88 0x0, // base 15:0
89 0x0, // base 23:16
90 0x092, // present, ring 0, data, read/write
91 0x0CF, // page-granular, 32-bit
92 0x0,
93 },
94 //
95 // LINEAR_CODE64_SEL
96 //
97 {
98 0x0FFFF, // limit 15:0
99 0x0, // base 15:0
100 0x0, // base 23:16
101 0x09A, // present, ring 0, code, execute/read
102 0x0AF, // page-granular, 64-bit code
103 0x0, // base (high)
104 },
105 //
106 // SPARE5_SEL
107 //
108 {
109 0x0, // limit 15:0
110 0x0, // base 15:0
111 0x0, // base 23:16
112 0x0, // type
113 0x0, // limit 19:16, flags
114 0x0, // base 31:24
115 },
116 };
117
118 /**
119 Initialize Global Descriptor Table.
120
121 **/
122 VOID
123 InitGlobalDescriptorTable (
124 VOID
125 )
126 {
127 GDT_ENTRIES *gdt;
128 IA32_DESCRIPTOR gdtPtr;
129
130 //
131 // Allocate Runtime Data for the GDT
132 //
133 gdt = AllocateRuntimePool (sizeof (GdtTemplate) + 8);
134 ASSERT (gdt != NULL);
135 gdt = ALIGN_POINTER (gdt, 8);
136
137 //
138 // Initialize all GDT entries
139 //
140 CopyMem (gdt, &GdtTemplate, sizeof (GdtTemplate));
141
142 //
143 // Write GDT register
144 //
145 gdtPtr.Base = (UINT32)(UINTN)(VOID*) gdt;
146 gdtPtr.Limit = (UINT16) (sizeof (GdtTemplate) - 1);
147 AsmWriteGdtr (&gdtPtr);
148
149 //
150 // Update selector (segment) registers base on new GDT
151 //
152 SetCodeSelector ((UINT16)CPU_CODE_SEL);
153 SetDataSelectors ((UINT16)CPU_DATA_SEL);
154 }
155