blob: a21fab24ad8300ad14205605e907327a4e295fb0 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Stefan Reinauer3b314022009-10-26 17:04:28 +000018 */
19
20#include <types.h>
21#include <string.h>
22#include <cbmem.h>
23#include <console/console.h>
Kyösti Mälkkibae775a2014-12-18 10:36:33 +020024#include <cpu/x86/gdt.h>
Stefan Reinauer3b314022009-10-26 17:04:28 +000025
26/* i386 lgdt argument */
27struct gdtarg {
28 u16 limit;
Stefan Reinauer96938852015-06-18 01:23:48 -070029#ifdef __x86_64__
30 u64 base;
31#else
Stefan Reinauer3b314022009-10-26 17:04:28 +000032 u32 base;
Stefan Reinauer96938852015-06-18 01:23:48 -070033#endif
Stefan Reinauer3b314022009-10-26 17:04:28 +000034} __attribute__((packed));
35
Kyösti Mälkkibae775a2014-12-18 10:36:33 +020036/* Copy GDT to new location and reload it.
37 * FIXME: We only do this for BSP CPU.
38 */
Aaron Durbin41607a42015-06-09 13:54:10 -050039static void move_gdt(int is_recovery)
Stefan Reinauer3b314022009-10-26 17:04:28 +000040{
41 void *newgdt;
Stefan Reinauer96938852015-06-18 01:23:48 -070042 u16 num_gdt_bytes = (uintptr_t)&gdt_end - (uintptr_t)&gdt;
Stefan Reinauer3b314022009-10-26 17:04:28 +000043 struct gdtarg gdtarg;
44
45 newgdt = cbmem_find(CBMEM_ID_GDT);
46 if (!newgdt) {
47 newgdt = cbmem_add(CBMEM_ID_GDT, ALIGN(num_gdt_bytes, 512));
48 if (!newgdt) {
49 printk(BIOS_ERR, "Error: Could not relocate GDT.\n");
50 return;
51 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000052 printk(BIOS_DEBUG, "Moving GDT to %p...", newgdt);
Stefan Reinauer3b314022009-10-26 17:04:28 +000053 memcpy((void*)newgdt, &gdt, num_gdt_bytes);
54 }
55
Stefan Reinauer96938852015-06-18 01:23:48 -070056 gdtarg.base = (uintptr_t)newgdt;
Stefan Reinauer3b314022009-10-26 17:04:28 +000057 gdtarg.limit = num_gdt_bytes - 1;
58
59 __asm__ __volatile__ ("lgdt %0\n\t" : : "m" (gdtarg));
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000060 printk(BIOS_DEBUG, "ok\n");
Stefan Reinauer3b314022009-10-26 17:04:28 +000061}
Kyösti Mälkki4fbac462015-01-07 04:48:43 +020062RAMSTAGE_CBMEM_INIT_HOOK(move_gdt)