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. |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <types.h> |
| 17 | #include <string.h> |
| 18 | #include <cbmem.h> |
| 19 | #include <console/console.h> |
Kyösti Mälkki | bae775a | 2014-12-18 10:36:33 +0200 | [diff] [blame] | 20 | #include <cpu/x86/gdt.h> |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 21 | |
| 22 | /* i386 lgdt argument */ |
| 23 | struct gdtarg { |
| 24 | u16 limit; |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 25 | #ifdef __x86_64__ |
| 26 | u64 base; |
| 27 | #else |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 28 | u32 base; |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 29 | #endif |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 30 | } __packed; |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 31 | |
Paul Menzel | 95c4aa5 | 2017-10-11 09:58:20 +0200 | [diff] [blame] | 32 | /* |
| 33 | * Copy GDT to new location and reload it. |
Kyösti Mälkki | bae775a | 2014-12-18 10:36:33 +0200 | [diff] [blame] | 34 | * FIXME: We only do this for BSP CPU. |
| 35 | */ |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 36 | static void move_gdt(int is_recovery) |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 37 | { |
| 38 | void *newgdt; |
Paul Menzel | 0187380 | 2017-10-11 15:41:43 +0200 | [diff] [blame] | 39 | u16 num_gdt_bytes; |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 40 | struct gdtarg gdtarg; |
| 41 | |
Aaron Durbin | ebbd12f | 2016-11-19 10:06:00 -0600 | [diff] [blame] | 42 | /* ramstage is already in high memory. No need to use a new gdt. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame^] | 43 | if (CONFIG(RELOCATABLE_RAMSTAGE)) |
Aaron Durbin | ebbd12f | 2016-11-19 10:06:00 -0600 | [diff] [blame] | 44 | return; |
| 45 | |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 46 | newgdt = cbmem_find(CBMEM_ID_GDT); |
Paul Menzel | 0187380 | 2017-10-11 15:41:43 +0200 | [diff] [blame] | 47 | num_gdt_bytes = (uintptr_t)&gdt_end - (uintptr_t)&gdt; |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 48 | if (!newgdt) { |
| 49 | newgdt = cbmem_add(CBMEM_ID_GDT, ALIGN(num_gdt_bytes, 512)); |
| 50 | if (!newgdt) { |
| 51 | printk(BIOS_ERR, "Error: Could not relocate GDT.\n"); |
| 52 | return; |
| 53 | } |
Lee Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 54 | memcpy((void *)newgdt, &gdt, num_gdt_bytes); |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 55 | } |
Kyösti Mälkki | 78c5f0c | 2016-11-19 16:39:10 +0200 | [diff] [blame] | 56 | printk(BIOS_DEBUG, "Moving GDT to %p...", newgdt); |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 57 | |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 58 | gdtarg.base = (uintptr_t)newgdt; |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 59 | gdtarg.limit = num_gdt_bytes - 1; |
| 60 | |
| 61 | __asm__ __volatile__ ("lgdt %0\n\t" : : "m" (gdtarg)); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 62 | printk(BIOS_DEBUG, "ok\n"); |
Stefan Reinauer | 3b31402 | 2009-10-26 17:04:28 +0000 | [diff] [blame] | 63 | } |
Kyösti Mälkki | 4fbac46 | 2015-01-07 04:48:43 +0200 | [diff] [blame] | 64 | RAMSTAGE_CBMEM_INIT_HOOK(move_gdt) |