blob: e13d3bc4da5e1d91982154e6c15162ba326ee3fd [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;
29 u32 base;
30} __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 */
Kyösti Mälkki4fbac462015-01-07 04:48:43 +020035static void move_gdt(void)
Stefan Reinauer3b314022009-10-26 17:04:28 +000036{
37 void *newgdt;
Kyösti Mälkkibae775a2014-12-18 10:36:33 +020038 u16 num_gdt_bytes = (u32)&gdt_end - (u32)&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
Uwe Hermann312673c2009-10-27 21:49:33 +000052 gdtarg.base = (u32)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)