xref: /openbmc/qemu/contrib/elf2dmp/addrspace.c (revision f14eced5)
1 /*
2  * Copyright (c) 2018 Virtuozzo International GmbH
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2 or later.
5  *
6  */
7 
8 #include "qemu/osdep.h"
9 #include "addrspace.h"
10 
11 static struct pa_block *pa_space_find_block(struct pa_space *ps, uint64_t pa)
12 {
13     size_t i;
14 
15     for (i = 0; i < ps->block_nr; i++) {
16         if (ps->block[i].paddr <= pa &&
17                 pa < ps->block[i].paddr + ps->block[i].size) {
18             return ps->block + i;
19         }
20     }
21 
22     return NULL;
23 }
24 
25 static uint8_t *pa_space_resolve(struct pa_space *ps, uint64_t pa)
26 {
27     struct pa_block *block = pa_space_find_block(ps, pa);
28 
29     if (!block) {
30         return NULL;
31     }
32 
33     return block->addr + (pa - block->paddr);
34 }
35 
36 static void pa_block_align(struct pa_block *b)
37 {
38     uint64_t low_align = ((b->paddr - 1) | ELF2DMP_PAGE_MASK) + 1 - b->paddr;
39     uint64_t high_align = (b->paddr + b->size) & ELF2DMP_PAGE_MASK;
40 
41     if (low_align == 0 && high_align == 0) {
42         return;
43     }
44 
45     if (low_align + high_align < b->size) {
46         printf("Block 0x%"PRIx64"+:0x%"PRIx64" will be aligned to "
47                 "0x%"PRIx64"+:0x%"PRIx64"\n", b->paddr, b->size,
48                 b->paddr + low_align, b->size - low_align - high_align);
49         b->size -= low_align + high_align;
50     } else {
51         printf("Block 0x%"PRIx64"+:0x%"PRIx64" is too small to align\n",
52                 b->paddr, b->size);
53         b->size = 0;
54     }
55 
56     b->addr += low_align;
57     b->paddr += low_align;
58 }
59 
60 int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
61 {
62     Elf64_Half phdr_nr = elf_getphdrnum(qemu_elf->map);
63     Elf64_Phdr *phdr = elf64_getphdr(qemu_elf->map);
64     size_t block_i = 0;
65     size_t i;
66 
67     ps->block_nr = 0;
68 
69     for (i = 0; i < phdr_nr; i++) {
70         if (phdr[i].p_type == PT_LOAD) {
71             ps->block_nr++;
72         }
73     }
74 
75     ps->block = malloc(sizeof(*ps->block) * ps->block_nr);
76     if (!ps->block) {
77         return 1;
78     }
79 
80     for (i = 0; i < phdr_nr; i++) {
81         if (phdr[i].p_type == PT_LOAD) {
82             ps->block[block_i] = (struct pa_block) {
83                 .addr = (uint8_t *)qemu_elf->map + phdr[i].p_offset,
84                 .paddr = phdr[i].p_paddr,
85                 .size = phdr[i].p_filesz,
86             };
87             pa_block_align(&ps->block[block_i]);
88             block_i = ps->block[block_i].size ? (block_i + 1) : block_i;
89         }
90     }
91 
92     ps->block_nr = block_i;
93 
94     return 0;
95 }
96 
97 void pa_space_destroy(struct pa_space *ps)
98 {
99     ps->block_nr = 0;
100     free(ps->block);
101 }
102 
103 void va_space_set_dtb(struct va_space *vs, uint64_t dtb)
104 {
105     vs->dtb = dtb & 0x00ffffffffff000;
106 }
107 
108 void va_space_create(struct va_space *vs, struct pa_space *ps, uint64_t dtb)
109 {
110     vs->ps = ps;
111     va_space_set_dtb(vs, dtb);
112 }
113 
114 static uint64_t get_pml4e(struct va_space *vs, uint64_t va)
115 {
116     uint64_t pa = (vs->dtb & 0xffffffffff000) | ((va & 0xff8000000000) >> 36);
117 
118     return *(uint64_t *)pa_space_resolve(vs->ps, pa);
119 }
120 
121 static uint64_t get_pdpi(struct va_space *vs, uint64_t va, uint64_t pml4e)
122 {
123     uint64_t pdpte_paddr = (pml4e & 0xffffffffff000) |
124         ((va & 0x7FC0000000) >> 27);
125 
126     return *(uint64_t *)pa_space_resolve(vs->ps, pdpte_paddr);
127 }
128 
129 static uint64_t pde_index(uint64_t va)
130 {
131     return (va >> 21) & 0x1FF;
132 }
133 
134 static uint64_t pdba_base(uint64_t pdpe)
135 {
136     return pdpe & 0xFFFFFFFFFF000;
137 }
138 
139 static uint64_t get_pgd(struct va_space *vs, uint64_t va, uint64_t pdpe)
140 {
141     uint64_t pgd_entry = pdba_base(pdpe) + pde_index(va) * 8;
142 
143     return *(uint64_t *)pa_space_resolve(vs->ps, pgd_entry);
144 }
145 
146 static uint64_t pte_index(uint64_t va)
147 {
148     return (va >> 12) & 0x1FF;
149 }
150 
151 static uint64_t ptba_base(uint64_t pde)
152 {
153     return pde & 0xFFFFFFFFFF000;
154 }
155 
156 static uint64_t get_pte(struct va_space *vs, uint64_t va, uint64_t pgd)
157 {
158     uint64_t pgd_val = ptba_base(pgd) + pte_index(va) * 8;
159 
160     return *(uint64_t *)pa_space_resolve(vs->ps, pgd_val);
161 }
162 
163 static uint64_t get_paddr(uint64_t va, uint64_t pte)
164 {
165     return (pte & 0xFFFFFFFFFF000) | (va & 0xFFF);
166 }
167 
168 static bool is_present(uint64_t entry)
169 {
170     return entry & 0x1;
171 }
172 
173 static bool page_size_flag(uint64_t entry)
174 {
175     return entry & (1 << 7);
176 }
177 
178 static uint64_t get_1GB_paddr(uint64_t va, uint64_t pdpte)
179 {
180     return (pdpte & 0xfffffc0000000) | (va & 0x3fffffff);
181 }
182 
183 static uint64_t get_2MB_paddr(uint64_t va, uint64_t pgd_entry)
184 {
185     return (pgd_entry & 0xfffffffe00000) | (va & 0x00000001fffff);
186 }
187 
188 static uint64_t va_space_va2pa(struct va_space *vs, uint64_t va)
189 {
190     uint64_t pml4e, pdpe, pgd, pte;
191 
192     pml4e = get_pml4e(vs, va);
193     if (!is_present(pml4e)) {
194         return INVALID_PA;
195     }
196 
197     pdpe = get_pdpi(vs, va, pml4e);
198     if (!is_present(pdpe)) {
199         return INVALID_PA;
200     }
201 
202     if (page_size_flag(pdpe)) {
203         return get_1GB_paddr(va, pdpe);
204     }
205 
206     pgd = get_pgd(vs, va, pdpe);
207     if (!is_present(pgd)) {
208         return INVALID_PA;
209     }
210 
211     if (page_size_flag(pgd)) {
212         return get_2MB_paddr(va, pgd);
213     }
214 
215     pte = get_pte(vs, va, pgd);
216     if (!is_present(pte)) {
217         return INVALID_PA;
218     }
219 
220     return get_paddr(va, pte);
221 }
222 
223 void *va_space_resolve(struct va_space *vs, uint64_t va)
224 {
225     uint64_t pa = va_space_va2pa(vs, va);
226 
227     if (pa == INVALID_PA) {
228         return NULL;
229     }
230 
231     return pa_space_resolve(vs->ps, pa);
232 }
233 
234 int va_space_rw(struct va_space *vs, uint64_t addr,
235         void *buf, size_t size, int is_write)
236 {
237     while (size) {
238         uint64_t page = addr & ELF2DMP_PFN_MASK;
239         size_t s = (page + ELF2DMP_PAGE_SIZE) - addr;
240         void *ptr;
241 
242         s = (s > size) ? size : s;
243 
244         ptr = va_space_resolve(vs, addr);
245         if (!ptr) {
246             return 1;
247         }
248 
249         if (is_write) {
250             memcpy(ptr, buf, s);
251         } else {
252             memcpy(buf, ptr, s);
253         }
254 
255         size -= s;
256         buf = (uint8_t *)buf + s;
257         addr += s;
258     }
259 
260     return 0;
261 }
262