Patrick Rudolph | b1ef725 | 2019-09-28 17:44:01 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of pgtblgen. |
| 3 | * |
| 4 | * Copyright (c) 2019 Patrick Rudolph <siro@das-labor.org> |
| 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 version 2 as |
| 8 | * published by the Free Software Foundation. |
| 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 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | #include <string.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <errno.h> |
| 24 | #include <inttypes.h> |
| 25 | |
| 26 | static void usage(char *argv[]) |
| 27 | { |
| 28 | printf("usage: %s -b <addr> -a <arch> -o <file>\n", argv[0]); |
Patrick Georgi | 220c209 | 2020-01-30 12:58:08 +0100 | [diff] [blame] | 29 | printf(" -a\t architecture. Supported: x86_64\n"); |
Patrick Rudolph | b1ef725 | 2019-09-28 17:44:01 +0200 | [diff] [blame] | 30 | printf(" -b\t base address\n"); |
Patrick Rudolph | bc2204e | 2019-11-21 17:33:58 +0100 | [diff] [blame] | 31 | printf(" -o\t the file to write to\n"); |
Patrick Rudolph | b1ef725 | 2019-09-28 17:44:01 +0200 | [diff] [blame] | 32 | printf(" -h\t show this help text\n"); |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * For reference see "AMD64 ArchitectureProgrammer's Manual Volume 2", |
| 37 | * Document 24593-Rev. 3.31-July 2019 Chapter 5.3.4 |
| 38 | * |
| 39 | * Page table attributes: WB, User+Supervisor, Present, Writeable |
| 40 | */ |
Patrick Rudolph | e69798b | 2019-11-12 16:56:43 +0100 | [diff] [blame] | 41 | #define _PRES (1ULL << 0) |
| 42 | #define _RW (1ULL << 1) |
| 43 | #define _US (1ULL << 2) |
| 44 | #define _A (1ULL << 5) |
| 45 | #define _D (1ULL << 6) |
| 46 | #define _PS (1ULL << 7) |
| 47 | #define _GEN_DIR(a) (_PRES | _RW | _US | _A | (a)) |
| 48 | #define _GEN_PAGE(a) (_PRES | _RW | _US | _PS | _A | _D | (a)) |
Patrick Rudolph | b1ef725 | 2019-09-28 17:44:01 +0200 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Generate x86_64 page tables. |
| 52 | * The page tables needs to be placed at @base_address, and identity map |
| 53 | * the first @size_gib GiB of physical memory. |
| 54 | */ |
| 55 | static int gen_pgtbl_x86_64(const uint64_t base_address, |
| 56 | const size_t size_gib, |
| 57 | void **out_buf, |
| 58 | size_t *out_size) |
| 59 | { |
| 60 | uint64_t *entry; |
| 61 | |
| 62 | if (!out_size || !out_buf) |
| 63 | return 1; |
| 64 | |
| 65 | *out_size = (size_gib + 2) * 4096; |
| 66 | *out_buf = malloc(*out_size); |
| 67 | if (!*out_buf) |
| 68 | return 1; |
| 69 | |
| 70 | memset(*out_buf, 0, *out_size); |
| 71 | entry = (uint64_t *)*out_buf; |
| 72 | |
| 73 | /* Generate one PM4LE entry - point to PDPE */ |
| 74 | entry[0] = _GEN_DIR(base_address + 4096); |
| 75 | entry += 512; |
| 76 | |
| 77 | /* PDPE table - point to PDE */ |
| 78 | for (size_t i = 0; i < size_gib; i++) |
| 79 | entry[i] = _GEN_DIR(base_address + 4096 * (i + 2)); |
| 80 | entry += 512; |
| 81 | |
| 82 | /* PDE tables - identity map 2MiB pages */ |
| 83 | for (size_t g = 0; g < size_gib; g++) { |
| 84 | for (size_t i = 0; i < 512; i++) { |
| 85 | uint64_t addr = ((1ULL << (12 + 9)) * i) | ((1ULL << (12 + 9 + 9)) * g); |
| 86 | entry[i] = _GEN_PAGE(addr); |
| 87 | } |
| 88 | entry += 512; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int main(int argc, char *argv[]) |
| 95 | { |
| 96 | int ret = 1; |
| 97 | uint64_t base_address = 0; |
| 98 | char *filename = NULL; |
| 99 | char *arch = NULL; |
| 100 | void *buf = NULL; |
| 101 | size_t buf_size = 0; |
| 102 | int c; |
| 103 | |
| 104 | while ((c = getopt(argc, argv, "ho:a:b:")) != -1) |
| 105 | switch (c) { |
| 106 | case '?': /* falltrough */ |
| 107 | case 'h': |
| 108 | usage(argv); |
| 109 | return 0; |
| 110 | case 'o': |
| 111 | filename = optarg; |
| 112 | break; |
| 113 | case 'a': |
| 114 | arch = optarg; |
| 115 | break; |
| 116 | case 'b': |
| 117 | base_address = strtoull(optarg, NULL, 0); |
| 118 | break; |
| 119 | default: |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | if (!filename) { |
| 124 | fprintf(stderr, "E: Missing filename.\n"); |
| 125 | goto done; |
| 126 | } |
| 127 | if (!arch) { |
| 128 | fprintf(stderr, "E: Missing architecture.\n"); |
| 129 | goto done; |
| 130 | } else if (strcmp(arch, "x86_64") != 0) { |
| 131 | fprintf(stderr, "E: Unsupported architecture.\n"); |
| 132 | goto done; |
| 133 | } |
| 134 | if (base_address & 4095) { |
| 135 | fprintf(stderr, "E: Base address not 4 KiB aligned\n"); |
| 136 | goto done; |
| 137 | } |
| 138 | |
| 139 | /* FIXME: Identity map 4GiB for now, increase if necessary */ |
| 140 | if (strcmp(arch, "x86_64") == 0) |
| 141 | ret = gen_pgtbl_x86_64(base_address, 4, &buf, &buf_size); |
| 142 | |
| 143 | if (ret) { |
| 144 | fprintf(stderr, "Failed to generate page tables\n"); |
| 145 | goto done; |
| 146 | } |
| 147 | |
| 148 | // write the table |
| 149 | FILE *fd = fopen(filename, "wb"); |
| 150 | if (!fd) { |
| 151 | fprintf(stderr, "%s open failed: %s\n", filename, strerror(errno)); |
| 152 | goto done; |
| 153 | } |
| 154 | |
| 155 | if (fwrite(buf, 1, buf_size, fd) != buf_size) { |
| 156 | fprintf(stderr, "%s write failed: %s\n", filename, strerror(errno)); |
| 157 | fclose(fd); |
| 158 | goto done; |
| 159 | } |
| 160 | |
| 161 | if (fclose(fd)) { |
| 162 | fprintf(stderr, "%s close failed: %s\n", filename, strerror(errno)); |
| 163 | goto done; |
| 164 | } |
| 165 | |
| 166 | ret = 0; |
| 167 | done: |
| 168 | free(buf); |
| 169 | return ret; |
| 170 | } |