Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 1 | /* |
| 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 Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <types.h> |
| 21 | #include <string.h> |
| 22 | #include <cbmem.h> |
| 23 | #include <console/console.h> |
Kyösti Mälkki | bae775a | 2014-12-18 10:36:33 +0200 | [diff] [blame] | 24 | #include <cpu/x86/gdt.h> |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 25 | |
| 26 | /* i386 lgdt argument */ |
| 27 | struct gdtarg { |
| 28 | u16 limit; |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame^] | 29 | #ifdef __x86_64__ |
| 30 | u64 base; |
| 31 | #else |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 32 | u32 base; |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame^] | 33 | #endif |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 34 | } __attribute__((packed)); |
| 35 | |
Kyösti Mälkki | bae775a | 2014-12-18 10:36:33 +0200 | [diff] [blame] | 36 | /* Copy GDT to new location and reload it. |
| 37 | * FIXME: We only do this for BSP CPU. |
| 38 | */ |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 39 | static void move_gdt(int is_recovery) |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 40 | { |
| 41 | void *newgdt; |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame^] | 42 | u16 num_gdt_bytes = (uintptr_t)&gdt_end - (uintptr_t)&gdt; |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 43 | 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 Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 52 | printk(BIOS_DEBUG, "Moving GDT to %p...", newgdt); |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 53 | memcpy((void*)newgdt, &gdt, num_gdt_bytes); |
| 54 | } |
| 55 | |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame^] | 56 | gdtarg.base = (uintptr_t)newgdt; |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 57 | gdtarg.limit = num_gdt_bytes - 1; |
| 58 | |
| 59 | __asm__ __volatile__ ("lgdt %0\n\t" : : "m" (gdtarg)); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 60 | printk(BIOS_DEBUG, "ok\n"); |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 61 | } |
Kyösti Mälkki | 4fbac46 | 2015-01-07 04:48:43 +0200 | [diff] [blame] | 62 | RAMSTAGE_CBMEM_INIT_HOOK(move_gdt) |