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