From b1ef725f391b3864c5051c7e7290311c475cfcd3 Mon Sep 17 00:00:00 2001 From: Patrick Rudolph Date: Sat, 28 Sep 2019 17:44:01 +0200 Subject: cpu/qemu-x86: Add x86_64 bootblock support Add support for x86_64 bootblock on qemu. Introduce a new approach to long mode support. The previous patch set generated page tables at runtime and placed them in heap. The new approach places the page tables in memory mapped ROM. Introduce a new tool called pgtblgen that creates x86 long mode compatible page tables and writes those to a file. The file is included into the CBFS and placed at a predefined offset. Add assembly code to load the page tables, based on a Kconfig symbol and enter long in bootblock. The code can be easily ported to real hardware bootblock. Tested on qemu q35. Change-Id: Iec92c6cea464c97c18a0811e2e91bc22133ace42 Signed-off-by: Patrick Rudolph Reviewed-on: https://review.coreboot.org/c/coreboot/+/35680 Tested-by: build bot (Jenkins) Reviewed-by: Arthur Heymans --- util/pgtblgen/Makefile.inc | 19 +++++ util/pgtblgen/description.md | 1 + util/pgtblgen/pgtblgen.c | 168 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 util/pgtblgen/Makefile.inc create mode 100644 util/pgtblgen/description.md create mode 100644 util/pgtblgen/pgtblgen.c (limited to 'util') diff --git a/util/pgtblgen/Makefile.inc b/util/pgtblgen/Makefile.inc new file mode 100644 index 0000000000..c4f3ef37b6 --- /dev/null +++ b/util/pgtblgen/Makefile.inc @@ -0,0 +1,19 @@ +ifeq ($(CONFIG_ARCH_BOOTBLOCK_X86_64),y) + +PGTBLGEN:= $(obj)/pgtblgen + +cbfs-files-y += pagetables +pagetables-file := $(obj)/mainboard/$(MAINBOARDDIR)/pagetables +pagetables-type := raw +pagetables-compression := none +pagetables-COREBOOT-position := $(CONFIG_ARCH_X86_64_PGTBL_LOC) + +$(obj)/mainboard/$(MAINBOARDDIR)/pagetables: $(PGTBLGEN) $(obj)/config.h + printf " TOOL Creating page tables\n" + $(PGTBLGEN) -b $(CONFIG_ARCH_X86_64_PGTBL_LOC) -a x86_64 -o $@ + +$(PGTBLGEN): util/pgtblgen/pgtblgen.c + printf " MAKE Creating PGTBLGEN tool\n" + $(HOSTCC) $< -I$(obj) -o $@ + +endif diff --git a/util/pgtblgen/description.md b/util/pgtblgen/description.md new file mode 100644 index 0000000000..f2e4763d75 --- /dev/null +++ b/util/pgtblgen/description.md @@ -0,0 +1 @@ +Generates page tables based on fixed physical address. `C` diff --git a/util/pgtblgen/pgtblgen.c b/util/pgtblgen/pgtblgen.c new file mode 100644 index 0000000000..efbad55204 --- /dev/null +++ b/util/pgtblgen/pgtblgen.c @@ -0,0 +1,168 @@ +/* + * This file is part of pgtblgen. + * + * Copyright (c) 2019 Patrick Rudolph + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void usage(char *argv[]) +{ + printf("usage: %s -b -a -o \n", argv[0]); + printf(" -a\t architecure. Supported: x86_64\n"); + printf(" -b\t base address\n"); + printf(" -b\t the file to write to\n"); + printf(" -h\t show this help text\n"); +} + +/* + * For reference see "AMD64 ArchitectureProgrammer's Manual Volume 2", + * Document 24593-Rev. 3.31-July 2019 Chapter 5.3.4 + * + * Page table attributes: WB, User+Supervisor, Present, Writeable + */ +#define PRES (1ULL << 0) +#define RW (1ULL << 1) +#define US (1ULL << 2) +#define PS (1ULL << 7) +#define _GEN_DIR(a) (PRES | RW | US | (a)) +#define _GEN_PAGE(a) (PRES | RW | US | PS | (a)) + +/* + * Generate x86_64 page tables. + * The page tables needs to be placed at @base_address, and identity map + * the first @size_gib GiB of physical memory. + */ +static int gen_pgtbl_x86_64(const uint64_t base_address, + const size_t size_gib, + void **out_buf, + size_t *out_size) +{ + uint64_t *entry; + + if (!out_size || !out_buf) + return 1; + + *out_size = (size_gib + 2) * 4096; + *out_buf = malloc(*out_size); + if (!*out_buf) + return 1; + + memset(*out_buf, 0, *out_size); + entry = (uint64_t *)*out_buf; + + /* Generate one PM4LE entry - point to PDPE */ + entry[0] = _GEN_DIR(base_address + 4096); + entry += 512; + + /* PDPE table - point to PDE */ + for (size_t i = 0; i < size_gib; i++) + entry[i] = _GEN_DIR(base_address + 4096 * (i + 2)); + entry += 512; + + /* PDE tables - identity map 2MiB pages */ + for (size_t g = 0; g < size_gib; g++) { + for (size_t i = 0; i < 512; i++) { + uint64_t addr = ((1ULL << (12 + 9)) * i) | ((1ULL << (12 + 9 + 9)) * g); + entry[i] = _GEN_PAGE(addr); + } + entry += 512; + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + int ret = 1; + uint64_t base_address = 0; + char *filename = NULL; + char *arch = NULL; + void *buf = NULL; + size_t buf_size = 0; + int c; + + while ((c = getopt(argc, argv, "ho:a:b:")) != -1) + switch (c) { + case '?': /* falltrough */ + case 'h': + usage(argv); + return 0; + case 'o': + filename = optarg; + break; + case 'a': + arch = optarg; + break; + case 'b': + base_address = strtoull(optarg, NULL, 0); + break; + default: + break; + } + + if (!filename) { + fprintf(stderr, "E: Missing filename.\n"); + goto done; + } + if (!arch) { + fprintf(stderr, "E: Missing architecture.\n"); + goto done; + } else if (strcmp(arch, "x86_64") != 0) { + fprintf(stderr, "E: Unsupported architecture.\n"); + goto done; + } + if (base_address & 4095) { + fprintf(stderr, "E: Base address not 4 KiB aligned\n"); + goto done; + } + + /* FIXME: Identity map 4GiB for now, increase if necessary */ + if (strcmp(arch, "x86_64") == 0) + ret = gen_pgtbl_x86_64(base_address, 4, &buf, &buf_size); + + if (ret) { + fprintf(stderr, "Failed to generate page tables\n"); + goto done; + } + + // write the table + FILE *fd = fopen(filename, "wb"); + if (!fd) { + fprintf(stderr, "%s open failed: %s\n", filename, strerror(errno)); + goto done; + } + + if (fwrite(buf, 1, buf_size, fd) != buf_size) { + fprintf(stderr, "%s write failed: %s\n", filename, strerror(errno)); + fclose(fd); + goto done; + } + + if (fclose(fd)) { + fprintf(stderr, "%s close failed: %s\n", filename, strerror(errno)); + goto done; + } + + ret = 0; +done: + free(buf); + return ret; +} -- cgit v1.2.3