xref: /openbmc/qemu/contrib/elf2dmp/addrspace.c (revision 259ebed4)
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 = g_new(struct pa_block, ps->block_nr);
76 
77     for (i = 0; i < phdr_nr; i++) {
78         if (phdr[i].p_type == PT_LOAD) {
79             ps->block[block_i] = (struct pa_block) {
80                 .addr = (uint8_t *)qemu_elf->map + phdr[i].p_offset,
81                 .paddr = phdr[i].p_paddr,
82                 .size = phdr[i].p_filesz,
83             };
84             pa_block_align(&ps->block[block_i]);
85             block_i = ps->block[block_i].size ? (block_i + 1) : block_i;
86         }
87     }
88 
89     ps->block_nr = block_i;
90 
91     return 0;
92 }
93 
94 void pa_space_destroy(struct pa_space *ps)
95 {
96     ps->block_nr = 0;
97     g_free(ps->block);
98 }
99 
100 void va_space_set_dtb(struct va_space *vs, uint64_t dtb)
101 {
102     vs->dtb = dtb & 0x00ffffffffff000;
103 }
104 
105 void va_space_create(struct va_space *vs, struct pa_space *ps, uint64_t dtb)
106 {
107     vs->ps = ps;
108     va_space_set_dtb(vs, dtb);
109 }
110 
111 static uint64_t get_pml4e(struct va_space *vs, uint64_t va)
112 {
113     uint64_t pa = (vs->dtb & 0xffffffffff000) | ((va & 0xff8000000000) >> 36);
114 
115     return *(uint64_t *)pa_space_resolve(vs->ps, pa);
116 }
117 
118 static uint64_t get_pdpi(struct va_space *vs, uint64_t va, uint64_t pml4e)
119 {
120     uint64_t pdpte_paddr = (pml4e & 0xffffffffff000) |
121         ((va & 0x7FC0000000) >> 27);
122 
123     return *(uint64_t *)pa_space_resolve(vs->ps, pdpte_paddr);
124 }
125 
126 static uint64_t pde_index(uint64_t va)
127 {
128     return (va >> 21) & 0x1FF;
129 }
130 
131 static uint64_t pdba_base(uint64_t pdpe)
132 {
133     return pdpe & 0xFFFFFFFFFF000;
134 }
135 
136 static uint64_t get_pgd(struct va_space *vs, uint64_t va, uint64_t pdpe)
137 {
138     uint64_t pgd_entry = pdba_base(pdpe) + pde_index(va) * 8;
139 
140     return *(uint64_t *)pa_space_resolve(vs->ps, pgd_entry);
141 }
142 
143 static uint64_t pte_index(uint64_t va)
144 {
145     return (va >> 12) & 0x1FF;
146 }
147 
148 static uint64_t ptba_base(uint64_t pde)
149 {
150     return pde & 0xFFFFFFFFFF000;
151 }
152 
153 static uint64_t get_pte(struct va_space *vs, uint64_t va, uint64_t pgd)
154 {
155     uint64_t pgd_val = ptba_base(pgd) + pte_index(va) * 8;
156 
157     return *(uint64_t *)pa_space_resolve(vs->ps, pgd_val);
158 }
159 
160 static uint64_t get_paddr(uint64_t va, uint64_t pte)
161 {
162     return (pte & 0xFFFFFFFFFF000) | (va & 0xFFF);
163 }
164 
165 static bool is_present(uint64_t entry)
166 {
167     return entry & 0x1;
168 }
169 
170 static bool page_size_flag(uint64_t entry)
171 {
172     return entry & (1 << 7);
173 }
174 
175 static uint64_t get_1GB_paddr(uint64_t va, uint64_t pdpte)
176 {
177     return (pdpte & 0xfffffc0000000) | (va & 0x3fffffff);
178 }
179 
180 static uint64_t get_2MB_paddr(uint64_t va, uint64_t pgd_entry)
181 {
182     return (pgd_entry & 0xfffffffe00000) | (va & 0x00000001fffff);
183 }
184 
185 static uint64_t va_space_va2pa(struct va_space *vs, uint64_t va)
186 {
187     uint64_t pml4e, pdpe, pgd, pte;
188 
189     pml4e = get_pml4e(vs, va);
190     if (!is_present(pml4e)) {
191         return INVALID_PA;
192     }
193 
194     pdpe = get_pdpi(vs, va, pml4e);
195     if (!is_present(pdpe)) {
196         return INVALID_PA;
197     }
198 
199     if (page_size_flag(pdpe)) {
200         return get_1GB_paddr(va, pdpe);
201     }
202 
203     pgd = get_pgd(vs, va, pdpe);
204     if (!is_present(pgd)) {
205         return INVALID_PA;
206     }
207 
208     if (page_size_flag(pgd)) {
209         return get_2MB_paddr(va, pgd);
210     }
211 
212     pte = get_pte(vs, va, pgd);
213     if (!is_present(pte)) {
214         return INVALID_PA;
215     }
216 
217     return get_paddr(va, pte);
218 }
219 
220 void *va_space_resolve(struct va_space *vs, uint64_t va)
221 {
222     uint64_t pa = va_space_va2pa(vs, va);
223 
224     if (pa == INVALID_PA) {
225         return NULL;
226     }
227 
228     return pa_space_resolve(vs->ps, pa);
229 }
230 
231 int va_space_rw(struct va_space *vs, uint64_t addr,
232         void *buf, size_t size, int is_write)
233 {
234     while (size) {
235         uint64_t page = addr & ELF2DMP_PFN_MASK;
236         size_t s = (page + ELF2DMP_PAGE_SIZE) - addr;
237         void *ptr;
238 
239         s = (s > size) ? size : s;
240 
241         ptr = va_space_resolve(vs, addr);
242         if (!ptr) {
243             return 1;
244         }
245 
246         if (is_write) {
247             memcpy(ptr, buf, s);
248         } else {
249             memcpy(buf, ptr, s);
250         }
251 
252         size -= s;
253         buf = (uint8_t *)buf + s;
254         addr += s;
255     }
256 
257     return 0;
258 }
259