blob: fe14155dd6870e25440f495add1248ceea30bbcb [file] [log] [blame]
Stefan Reinauer3b314022009-10-26 17:04:28 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Stefan Reinauer3b314022009-10-26 17:04:28 +000014 */
15
16#include <types.h>
17#include <string.h>
18#include <cbmem.h>
19#include <console/console.h>
Kyösti Mälkkibae775a2014-12-18 10:36:33 +020020#include <cpu/x86/gdt.h>
Stefan Reinauer3b314022009-10-26 17:04:28 +000021
22/* i386 lgdt argument */
23struct gdtarg {
24 u16 limit;
Stefan Reinauer96938852015-06-18 01:23:48 -070025#ifdef __x86_64__
26 u64 base;
27#else
Stefan Reinauer3b314022009-10-26 17:04:28 +000028 u32 base;
Stefan Reinauer96938852015-06-18 01:23:48 -070029#endif
Stefan Reinauer3b314022009-10-26 17:04:28 +000030} __attribute__((packed));
31
Kyösti Mälkkibae775a2014-12-18 10:36:33 +020032/* Copy GDT to new location and reload it.
33 * FIXME: We only do this for BSP CPU.
34 */
Aaron Durbin41607a42015-06-09 13:54:10 -050035static void move_gdt(int is_recovery)
Stefan Reinauer3b314022009-10-26 17:04:28 +000036{
37 void *newgdt;
Stefan Reinauer96938852015-06-18 01:23:48 -070038 u16 num_gdt_bytes = (uintptr_t)&gdt_end - (uintptr_t)&gdt;
Stefan Reinauer3b314022009-10-26 17:04:28 +000039 struct gdtarg gdtarg;
40
41 newgdt = cbmem_find(CBMEM_ID_GDT);
42 if (!newgdt) {
43 newgdt = cbmem_add(CBMEM_ID_GDT, ALIGN(num_gdt_bytes, 512));
44 if (!newgdt) {
45 printk(BIOS_ERR, "Error: Could not relocate GDT.\n");
46 return;
47 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000048 printk(BIOS_DEBUG, "Moving GDT to %p...", newgdt);
Stefan Reinauer3b314022009-10-26 17:04:28 +000049 memcpy((void*)newgdt, &gdt, num_gdt_bytes);
50 }
51
Stefan Reinauer96938852015-06-18 01:23:48 -070052 gdtarg.base = (uintptr_t)newgdt;
Stefan Reinauer3b314022009-10-26 17:04:28 +000053 gdtarg.limit = num_gdt_bytes - 1;
54
55 __asm__ __volatile__ ("lgdt %0\n\t" : : "m" (gdtarg));
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000056 printk(BIOS_DEBUG, "ok\n");
Stefan Reinauer3b314022009-10-26 17:04:28 +000057}
Kyösti Mälkki4fbac462015-01-07 04:48:43 +020058RAMSTAGE_CBMEM_INIT_HOOK(move_gdt)