2009-05-27 23:09:47 +02:00
|
|
|
/** @file
|
2020-02-07 02:08:29 +01:00
|
|
|
C based implementation of IA32 interrupt handling only
|
2009-05-27 23:09:47 +02:00
|
|
|
requiring a minimal assembly interrupt entry point.
|
|
|
|
|
2021-03-16 04:17:57 +01:00
|
|
|
Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
|
2019-04-04 01:07:22 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2009-05-27 23:09:47 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "CpuDxe.h"
|
2014-11-13 19:25:10 +01:00
|
|
|
#include "CpuGdt.h"
|
2009-05-27 23:09:47 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Global descriptor table (GDT) Template
|
|
|
|
//
|
2021-03-16 04:17:57 +01:00
|
|
|
STATIC GDT_ENTRIES mGdtTemplate = {
|
2009-05-27 23:09:47 +02:00
|
|
|
//
|
|
|
|
// NULL_SEL
|
|
|
|
//
|
|
|
|
{
|
|
|
|
0x0, // limit 15:0
|
|
|
|
0x0, // base 15:0
|
|
|
|
0x0, // base 23:16
|
|
|
|
0x0, // type
|
|
|
|
0x0, // limit 19:16, flags
|
|
|
|
0x0, // base 31:24
|
|
|
|
},
|
|
|
|
//
|
|
|
|
// LINEAR_SEL
|
|
|
|
//
|
|
|
|
{
|
2015-10-30 18:32:27 +01:00
|
|
|
0x0FFFF, // limit 15:0
|
|
|
|
0x0, // base 15:0
|
|
|
|
0x0, // base 23:16
|
|
|
|
0x092, // present, ring 0, data, read/write
|
2009-05-27 23:09:47 +02:00
|
|
|
0x0CF, // page-granular, 32-bit
|
|
|
|
0x0,
|
|
|
|
},
|
|
|
|
//
|
|
|
|
// LINEAR_CODE_SEL
|
|
|
|
//
|
|
|
|
{
|
2015-10-30 18:32:27 +01:00
|
|
|
0x0FFFF, // limit 15:0
|
|
|
|
0x0, // base 15:0
|
|
|
|
0x0, // base 23:16
|
|
|
|
0x09F, // present, ring 0, code, execute/read, conforming, accessed
|
2009-05-27 23:09:47 +02:00
|
|
|
0x0CF, // page-granular, 32-bit
|
|
|
|
0x0,
|
|
|
|
},
|
|
|
|
//
|
|
|
|
// SYS_DATA_SEL
|
|
|
|
//
|
|
|
|
{
|
2015-10-30 18:32:27 +01:00
|
|
|
0x0FFFF, // limit 15:0
|
|
|
|
0x0, // base 15:0
|
|
|
|
0x0, // base 23:16
|
|
|
|
0x093, // present, ring 0, data, read/write, accessed
|
2009-05-27 23:09:47 +02:00
|
|
|
0x0CF, // page-granular, 32-bit
|
|
|
|
0x0,
|
|
|
|
},
|
|
|
|
//
|
|
|
|
// SYS_CODE_SEL
|
|
|
|
//
|
|
|
|
{
|
2015-10-30 18:32:27 +01:00
|
|
|
0x0FFFF, // limit 15:0
|
|
|
|
0x0, // base 15:0
|
|
|
|
0x0, // base 23:16
|
|
|
|
0x09A, // present, ring 0, code, execute/read
|
2009-05-27 23:09:47 +02:00
|
|
|
0x0CF, // page-granular, 32-bit
|
|
|
|
0x0,
|
|
|
|
},
|
|
|
|
//
|
2020-08-12 22:21:42 +02:00
|
|
|
// SYS_CODE16_SEL
|
2009-05-27 23:09:47 +02:00
|
|
|
//
|
|
|
|
{
|
2020-08-12 22:21:42 +02:00
|
|
|
0x0FFFF, // limit 15:0
|
2015-10-30 18:32:27 +01:00
|
|
|
0x0, // base 15:0
|
|
|
|
0x0, // base 23:16
|
2020-08-12 22:21:42 +02:00
|
|
|
0x09A, // present, ring 0, code, execute/read
|
|
|
|
0x08F, // page-granular, 16-bit
|
2015-10-30 18:32:27 +01:00
|
|
|
0x0, // base 31:24
|
2009-05-27 23:09:47 +02:00
|
|
|
},
|
|
|
|
//
|
2015-10-30 18:32:27 +01:00
|
|
|
// LINEAR_DATA64_SEL
|
2009-05-27 23:09:47 +02:00
|
|
|
//
|
|
|
|
{
|
2015-10-30 18:32:27 +01:00
|
|
|
0x0FFFF, // limit 15:0
|
|
|
|
0x0, // base 15:0
|
|
|
|
0x0, // base 23:16
|
|
|
|
0x092, // present, ring 0, data, read/write
|
|
|
|
0x0CF, // page-granular, 32-bit
|
2009-05-27 23:09:47 +02:00
|
|
|
0x0,
|
|
|
|
},
|
|
|
|
//
|
2015-10-30 18:32:27 +01:00
|
|
|
// LINEAR_CODE64_SEL
|
|
|
|
//
|
|
|
|
{
|
|
|
|
0x0FFFF, // limit 15:0
|
|
|
|
0x0, // base 15:0
|
|
|
|
0x0, // base 23:16
|
|
|
|
0x09A, // present, ring 0, code, execute/read
|
|
|
|
0x0AF, // page-granular, 64-bit code
|
|
|
|
0x0, // base (high)
|
|
|
|
},
|
|
|
|
//
|
2009-05-27 23:09:47 +02:00
|
|
|
// SPARE5_SEL
|
|
|
|
//
|
|
|
|
{
|
2015-10-30 18:32:27 +01:00
|
|
|
0x0, // limit 15:0
|
|
|
|
0x0, // base 15:0
|
|
|
|
0x0, // base 23:16
|
|
|
|
0x0, // type
|
|
|
|
0x0, // limit 19:16, flags
|
|
|
|
0x0, // base 31:24
|
2009-05-27 23:09:47 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-07-13 05:08:54 +02:00
|
|
|
Initialize Global Descriptor Table.
|
2009-05-27 23:09:47 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
InitGlobalDescriptorTable (
|
2010-07-13 05:08:54 +02:00
|
|
|
VOID
|
2009-05-27 23:09:47 +02:00
|
|
|
)
|
|
|
|
{
|
2021-03-16 04:32:33 +01:00
|
|
|
EFI_STATUS Status;
|
2021-03-16 04:17:57 +01:00
|
|
|
GDT_ENTRIES *Gdt;
|
|
|
|
IA32_DESCRIPTOR Gdtr;
|
2021-03-16 04:32:33 +01:00
|
|
|
EFI_PHYSICAL_ADDRESS Memory;
|
2009-05-27 23:09:47 +02:00
|
|
|
|
|
|
|
//
|
2021-03-16 04:32:33 +01:00
|
|
|
// Allocate Runtime Data below 4GB for the GDT
|
|
|
|
// AP uses the same GDT when it's waken up from real mode so
|
|
|
|
// the GDT needs to be below 4GB.
|
|
|
|
//
|
|
|
|
Memory = SIZE_4GB - 1;
|
|
|
|
Status = gBS->AllocatePages (
|
|
|
|
AllocateMaxAddress,
|
|
|
|
EfiRuntimeServicesData,
|
|
|
|
EFI_SIZE_TO_PAGES (sizeof (mGdtTemplate)),
|
|
|
|
&Memory
|
|
|
|
);
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
ASSERT ((Memory != 0) && (Memory < SIZE_4GB));
|
|
|
|
Gdt = (GDT_ENTRIES *) (UINTN) Memory;
|
2009-05-27 23:09:47 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Initialize all GDT entries
|
|
|
|
//
|
2021-03-16 04:17:57 +01:00
|
|
|
CopyMem (Gdt, &mGdtTemplate, sizeof (mGdtTemplate));
|
2009-05-27 23:09:47 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Write GDT register
|
|
|
|
//
|
2021-03-16 04:17:57 +01:00
|
|
|
Gdtr.Base = (UINT32) (UINTN) Gdt;
|
|
|
|
Gdtr.Limit = (UINT16) (sizeof (mGdtTemplate) - 1);
|
|
|
|
AsmWriteGdtr (&Gdtr);
|
2009-05-27 23:09:47 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Update selector (segment) registers base on new GDT
|
|
|
|
//
|
|
|
|
SetCodeSelector ((UINT16)CPU_CODE_SEL);
|
|
|
|
SetDataSelectors ((UINT16)CPU_DATA_SEL);
|
|
|
|
}
|