-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxtables.c
More file actions
218 lines (165 loc) · 4.56 KB
/
Copy pathxtables.c
File metadata and controls
218 lines (165 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Copyright (C) 2016 Raphaël Poggi <poggi.raph@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <linux/types.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "mmu.h"
#define MAX_ENTRIES 512
#define TLB_TABLE_SIZE 0x00004000
#define TEST_START_RANGE (uint64_t)0x400000000
#define TEST_VIRT_START_RANGE (uint64_t)0xC00000000
#define TEST_END_RANGE (uint64_t)0x600444000
#define TEST_ATTR MEMORY_ATTRIBUTES(MT_NORMAL)
#define TEST_SIZE (uint64_t)(TEST_END_RANGE - TEST_START_RANGE)
#define IS_ALIGNED(x,a) (((x) & ((__typeof__(x))(a)-1UL)) == 0)
static uint64_t *pgd;
static int free_idx;
static int level2shift(int level)
{
/* Page is 12 bits wide, every level translates 9 bits */
return (12 + 9 * (3 - level));
}
static uint64_t level2mask(int level)
{
uint64_t mask = -EINVAL;
if (level == 1)
mask = L1_ADDR_MASK;
else if (level == 2)
mask = L2_ADDR_MASK;
else if (level == 3)
mask = L3_ADDR_MASK;
return mask;
}
static int pte_type(uint64_t *pte)
{
return *pte & PMD_TYPE_MASK;
}
static void xtables_set_table(uint64_t *pt, uint64_t *table_addr)
{
uint64_t val;
val = PMD_TYPE_TABLE | (uint64_t)table_addr;
*pt = val;
}
static uint64_t *xtables_create_table(void)
{
uint64_t *new_table = pgd + free_idx * GRANULE_SIZE;
/* Mark all entries as invalid */
memset(new_table, 0, GRANULE_SIZE);
free_idx++;
return new_table;
}
static uint64_t *xtables_get_level_table(uint64_t *pte)
{
uint64_t *table = (uint64_t *)(*pte & XLAT_ADDR_MASK);
if (pte_type(pte) != PMD_TYPE_TABLE) {
table = xtables_create_table();
xtables_set_table(pte, table);
}
return table;
}
static uint64_t *xtables_find_entry(uint64_t addr)
{
uint64_t *pte;
uint64_t block_shift;
uint64_t idx;
int i;
pte = pgd;
for (i = 1; i < 4; i++) {
block_shift = level2shift(i);
idx = (addr & level2mask(i)) >> block_shift;
pte += idx;
if ((pte_type(pte) != PMD_TYPE_TABLE) || (block_shift <= GRANULE_SIZE_SHIFT))
break;
else
pte = (uint64_t *)(*pte & XLAT_ADDR_MASK);
}
printf("for %lx got pte at %p [%lx] at level %d with idx %lx\n", addr, pte, *pte, i, idx);
return pte;
}
static void xtables_map_region(uint64_t virt, uint64_t phys, uint64_t size, uint64_t attr)
{
uint64_t block_size;
uint64_t block_shift;
uint64_t *pte;
uint64_t idx;
uint64_t addr;
uint64_t *table;
int level;
addr = virt;
attr &= ~(PMD_TYPE_SECT);
while (size) {
table = pgd;
for (level = 1; level < 4; level++) {
block_shift = level2shift(level);
idx = (addr & level2mask(level)) >> block_shift;
block_size = (1 << block_shift);
pte = table + idx;
if (level == 3)
attr |= PMD_TYPE_PAGE;
else
attr |= PMD_TYPE_SECT;
if (size >= block_size && IS_ALIGNED(addr, block_size)) {
*pte = phys | attr;
printf("map virt [%lx] at phys [%lx] at pte %p [%lx] at level %d\n", addr, phys, pte, *pte, level);
addr += block_size;
phys += block_size;
size -= block_size;
break;
}
table = xtables_get_level_table(pte);
}
}
}
static uint64_t xtables_virt_to_phys(uint64_t *pgd, uint64_t virt)
{
uint64_t phys = virt & 0xFFF;
uint64_t *entry = (uint64_t *)xtables_find_entry(virt);
*entry &= 0x7FFFFFF000;
phys |= *entry;
return phys;
}
static int xtables_init(void)
{
uint64_t val;
pgd = (uint64_t *)malloc(TLB_TABLE_SIZE);
if (!pgd) {
printf("cannot allocate pgd\n");
return -ENOMEM;
}
printf("allocate pgd at %p\n", pgd);
memset(pgd, 0, GRANULE_SIZE);
free_idx = 1;
xtables_map_region(TEST_VIRT_START_RANGE, TEST_START_RANGE, TEST_SIZE, PMD_TYPE_SECT | PMD_SECT_AF);
val = xtables_virt_to_phys(pgd, TEST_VIRT_START_RANGE);
printf("for virt %lx got phys %lx\n", TEST_VIRT_START_RANGE, val);
free(pgd);
return 0;
}
int main(int argc, char **argv)
{
int ret = 0;
ret = xtables_init();
if (ret < 0)
return ret;
return ret;
}