]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/paging.c
core: Remove cpu_data parameter from page_map_get_guest_pages
[jailhouse.git] / hypervisor / paging.c
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) Siemens AG, 2013
5  *
6  * Authors:
7  *  Jan Kiszka <jan.kiszka@siemens.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12
13 #include <jailhouse/paging.h>
14 #include <jailhouse/printk.h>
15 #include <jailhouse/string.h>
16 #include <jailhouse/control.h>
17 #include <asm/bitops.h>
18
19 #define BITS_PER_PAGE           (PAGE_SIZE * 8)
20
21 #define INVALID_PAGE_NR         (~0UL)
22
23 #define PAGE_SCRUB_ON_FREE      0x1
24
25 extern u8 __page_pool[];
26
27 unsigned long page_offset;
28
29 struct page_pool mem_pool;
30 struct page_pool remap_pool = {
31         .base_address = (void *)REMAP_BASE,
32         .pages = BITS_PER_PAGE * NUM_REMAP_BITMAP_PAGES,
33 };
34
35 struct paging_structures hv_paging_structs;
36
37 unsigned long page_map_get_phys_invalid(pt_entry_t pte, unsigned long virt)
38 {
39         return INVALID_PHYS_ADDR;
40 }
41
42 static unsigned long find_next_free_page(struct page_pool *pool,
43                                          unsigned long start)
44 {
45         unsigned long bmp_pos, bmp_val, page_nr;
46         unsigned long start_mask = 0;
47
48         if (start >= pool->pages)
49                 return INVALID_PAGE_NR;
50
51         /*
52          * If we don't start on the beginning of a bitmap word, create a mask
53          * to mark the pages before the start page as (virtually) used.
54          */
55         if (start % BITS_PER_LONG > 0)
56                 start_mask = ~0UL >> (BITS_PER_LONG - (start % BITS_PER_LONG));
57
58         for (bmp_pos = start / BITS_PER_LONG;
59              bmp_pos < pool->pages / BITS_PER_LONG; bmp_pos++) {
60                 bmp_val = pool->used_bitmap[bmp_pos] | start_mask;
61                 start_mask = 0;
62                 if (bmp_val != ~0UL) {
63                         page_nr = ffzl(bmp_val) + bmp_pos * BITS_PER_LONG;
64                         if (page_nr >= pool->pages)
65                                 break;
66                         return page_nr;
67                 }
68         }
69
70         return INVALID_PAGE_NR;
71 }
72
73 void *page_alloc(struct page_pool *pool, unsigned int num)
74 {
75         unsigned long start, last, next;
76         unsigned int allocated;
77
78         start = find_next_free_page(pool, 0);
79         if (start == INVALID_PAGE_NR)
80                 return NULL;
81
82 restart:
83         for (allocated = 1, last = start; allocated < num;
84              allocated++, last = next) {
85                 next = find_next_free_page(pool, last + 1);
86                 if (next == INVALID_PAGE_NR)
87                         return NULL;
88                 if (next != last + 1) {
89                         start = next;
90                         goto restart;
91                 }
92         }
93
94         for (allocated = 0; allocated < num; allocated++)
95                 set_bit(start + allocated, pool->used_bitmap);
96
97         pool->used_pages += num;
98
99         return pool->base_address + start * PAGE_SIZE;
100 }
101
102 void page_free(struct page_pool *pool, void *page, unsigned int num)
103 {
104         unsigned long page_nr;
105
106         if (!page)
107                 return;
108
109         while (num-- > 0) {
110                 if (pool->flags & PAGE_SCRUB_ON_FREE)
111                         memset(page, 0, PAGE_SIZE);
112                 page_nr = (page - pool->base_address) / PAGE_SIZE;
113                 clear_bit(page_nr, pool->used_bitmap);
114                 pool->used_pages--;
115                 page += PAGE_SIZE;
116         }
117 }
118
119 unsigned long page_map_virt2phys(const struct paging_structures *pg_structs,
120                                  unsigned long virt)
121 {
122         const struct paging *paging = pg_structs->root_paging;
123         page_table_t pt = pg_structs->root_table;
124         unsigned long phys;
125         pt_entry_t pte;
126
127         while (1) {
128                 pte = paging->get_entry(pt, virt);
129                 if (!paging->entry_valid(pte))
130                         return INVALID_PHYS_ADDR;
131                 phys = paging->get_phys(pte, virt);
132                 if (phys != INVALID_PHYS_ADDR)
133                         return phys;
134                 pt = page_map_phys2hvirt(paging->get_next_pt(pte));
135                 paging++;
136         }
137 }
138
139 static void flush_pt_entry(pt_entry_t pte, enum page_map_coherent coherent)
140 {
141         if (coherent == PAGE_MAP_COHERENT)
142                 flush_cache(pte, sizeof(*pte));
143 }
144
145 static int split_hugepage(const struct paging *paging, pt_entry_t pte,
146                           unsigned long virt, enum page_map_coherent coherent)
147 {
148         unsigned long phys = paging->get_phys(pte, virt);
149         struct paging_structures sub_structs;
150         unsigned long page_mask, flags;
151
152         if (phys == INVALID_PHYS_ADDR)
153                 return 0;
154
155         page_mask = ~(paging->page_size - 1);
156         phys &= page_mask;
157         virt &= page_mask;
158
159         flags = paging->get_flags(pte);
160
161         sub_structs.root_paging = paging + 1;
162         sub_structs.root_table = page_alloc(&mem_pool, 1);
163         if (!sub_structs.root_table)
164                 return -ENOMEM;
165         paging->set_next_pt(pte, page_map_hvirt2phys(sub_structs.root_table));
166         flush_pt_entry(pte, coherent);
167
168         return page_map_create(&sub_structs, phys, paging->page_size, virt,
169                                flags, coherent);
170 }
171
172 int page_map_create(const struct paging_structures *pg_structs,
173                     unsigned long phys, unsigned long size, unsigned long virt,
174                     unsigned long flags, enum page_map_coherent coherent)
175 {
176         phys &= PAGE_MASK;
177         virt &= PAGE_MASK;
178         size = PAGE_ALIGN(size);
179
180         while (size > 0) {
181                 const struct paging *paging = pg_structs->root_paging;
182                 page_table_t pt = pg_structs->root_table;
183                 pt_entry_t pte;
184                 int err;
185
186                 while (1) {
187                         pte = paging->get_entry(pt, virt);
188                         if (paging->page_size > 0 &&
189                             paging->page_size <= size &&
190                             ((phys | virt) & (paging->page_size - 1)) == 0) {
191                                 /*
192                                  * We might be overwriting a more fine-grained
193                                  * mapping, so release it first. This cannot
194                                  * fail as we are working along hugepage
195                                  * boundaries.
196                                  */
197                                 if (paging->page_size > PAGE_SIZE)
198                                         page_map_destroy(pg_structs, virt,
199                                                          paging->page_size,
200                                                          coherent);
201                                 paging->set_terminal(pte, phys, flags);
202                                 flush_pt_entry(pte, coherent);
203                                 break;
204                         }
205                         if (paging->entry_valid(pte)) {
206                                 err = split_hugepage(paging, pte, virt,
207                                                      coherent);
208                                 if (err)
209                                         return err;
210                                 pt = page_map_phys2hvirt(
211                                                 paging->get_next_pt(pte));
212                         } else {
213                                 pt = page_alloc(&mem_pool, 1);
214                                 if (!pt)
215                                         return -ENOMEM;
216                                 paging->set_next_pt(pte,
217                                                     page_map_hvirt2phys(pt));
218                                 flush_pt_entry(pte, coherent);
219                         }
220                         paging++;
221                 }
222                 if (pg_structs == &hv_paging_structs)
223                         arch_tlb_flush_page(virt);
224
225                 phys += paging->page_size;
226                 virt += paging->page_size;
227                 size -= paging->page_size;
228         }
229         return 0;
230 }
231
232 int page_map_destroy(const struct paging_structures *pg_structs,
233                      unsigned long virt, unsigned long size,
234                      enum page_map_coherent coherent)
235 {
236         size = PAGE_ALIGN(size);
237
238         while (size > 0) {
239                 const struct paging *paging = pg_structs->root_paging;
240                 page_table_t pt[MAX_PAGE_DIR_LEVELS];
241                 unsigned long page_size;
242                 pt_entry_t pte;
243                 int n = 0;
244                 int err;
245
246                 /* walk down the page table, saving intermediate tables */
247                 pt[0] = pg_structs->root_table;
248                 while (1) {
249                         pte = paging->get_entry(pt[n], virt);
250                         if (!paging->entry_valid(pte))
251                                 break;
252                         if (paging->get_phys(pte, virt) != INVALID_PHYS_ADDR) {
253                                 if (paging->page_size > size) {
254                                         err = split_hugepage(paging, pte, virt,
255                                                              coherent);
256                                         if (err)
257                                                 return err;
258                                 } else
259                                         break;
260                         }
261                         pt[++n] = page_map_phys2hvirt(
262                                         paging->get_next_pt(pte));
263                         paging++;
264                 }
265                 /* advance by page size of current level paging */
266                 page_size = paging->page_size ? paging->page_size : PAGE_SIZE;
267
268                 /* walk up again, clearing entries, releasing empty tables */
269                 while (1) {
270                         paging->clear_entry(pte);
271                         flush_pt_entry(pte, coherent);
272                         if (n == 0 || !paging->page_table_empty(pt[n]))
273                                 break;
274                         page_free(&mem_pool, pt[n], 1);
275                         paging--;
276                         pte = paging->get_entry(pt[--n], virt);
277                 }
278                 if (pg_structs == &hv_paging_structs)
279                         arch_tlb_flush_page(virt);
280
281                 if (page_size > size)
282                         break;
283                 virt += page_size;
284                 size -= page_size;
285         }
286         return 0;
287 }
288
289 static unsigned long
290 page_map_gvirt2gphys(const struct guest_paging_structures *pg_structs,
291                      unsigned long gvirt, unsigned long tmp_page)
292 {
293         unsigned long page_table_gphys = pg_structs->root_table_gphys;
294         const struct paging *paging = pg_structs->root_paging;
295         unsigned long gphys, phys;
296         pt_entry_t pte;
297         int err;
298
299         while (1) {
300                 /* map guest page table */
301                 phys = arch_page_map_gphys2phys(this_cpu_data(),
302                                                 page_table_gphys);
303                 if (phys == INVALID_PHYS_ADDR)
304                         return INVALID_PHYS_ADDR;
305                 err = page_map_create(&hv_paging_structs, phys,
306                                       PAGE_SIZE, tmp_page,
307                                       PAGE_READONLY_FLAGS,
308                                       PAGE_MAP_NON_COHERENT);
309                 if (err)
310                         return INVALID_PHYS_ADDR;
311
312                 /* evaluate page table entry */
313                 pte = paging->get_entry((page_table_t)tmp_page, gvirt);
314                 if (!paging->entry_valid(pte))
315                         return INVALID_PHYS_ADDR;
316                 gphys = paging->get_phys(pte, gvirt);
317                 if (gphys != INVALID_PHYS_ADDR)
318                         return gphys;
319                 page_table_gphys = paging->get_next_pt(pte);
320                 paging++;
321         }
322 }
323
324 void *
325 page_map_get_guest_pages(const struct guest_paging_structures *pg_structs,
326                          unsigned long gaddr, unsigned int num,
327                          unsigned long flags)
328 {
329         unsigned long page_base = TEMPORARY_MAPPING_BASE +
330                 this_cpu_id() * PAGE_SIZE * NUM_TEMPORARY_PAGES;
331         unsigned long phys, gphys, page_virt = page_base;
332         int err;
333
334         if (num > NUM_TEMPORARY_PAGES)
335                 return NULL;
336         while (num-- > 0) {
337                 if (pg_structs)
338                         gphys = page_map_gvirt2gphys(pg_structs, gaddr,
339                                                      page_virt);
340                 else
341                         gphys = gaddr;
342
343                 phys = arch_page_map_gphys2phys(this_cpu_data(), gphys);
344                 if (phys == INVALID_PHYS_ADDR)
345                         return NULL;
346                 /* map guest page */
347                 err = page_map_create(&hv_paging_structs, phys, PAGE_SIZE,
348                                       page_virt, flags, PAGE_MAP_NON_COHERENT);
349                 if (err)
350                         return NULL;
351                 gaddr += PAGE_SIZE;
352                 page_virt += PAGE_SIZE;
353         }
354         return (void *)page_base;
355 }
356
357 int paging_init(void)
358 {
359         unsigned long per_cpu_pages, config_pages, bitmap_pages;
360         unsigned long n;
361         int err;
362
363         per_cpu_pages = hypervisor_header.possible_cpus *
364                 sizeof(struct per_cpu) / PAGE_SIZE;
365
366         system_config = (struct jailhouse_system *)
367                 (__page_pool + per_cpu_pages * PAGE_SIZE);
368         config_pages = (jailhouse_system_config_size(system_config) +
369                         PAGE_SIZE - 1) / PAGE_SIZE;
370
371         page_offset = JAILHOUSE_BASE -
372                 system_config->hypervisor_memory.phys_start;
373
374         mem_pool.pages = (system_config->hypervisor_memory.size -
375                 (__page_pool - (u8 *)&hypervisor_header)) / PAGE_SIZE;
376         bitmap_pages = (mem_pool.pages + BITS_PER_PAGE - 1) / BITS_PER_PAGE;
377
378         if (mem_pool.pages <= per_cpu_pages + config_pages + bitmap_pages)
379                 goto error_nomem;
380
381         mem_pool.base_address = __page_pool;
382         mem_pool.used_bitmap =
383                 (unsigned long *)(__page_pool + per_cpu_pages * PAGE_SIZE +
384                                   config_pages * PAGE_SIZE);
385         mem_pool.used_pages = per_cpu_pages + config_pages + bitmap_pages;
386         for (n = 0; n < mem_pool.used_pages; n++)
387                 set_bit(n, mem_pool.used_bitmap);
388         mem_pool.flags = PAGE_SCRUB_ON_FREE;
389
390         remap_pool.used_bitmap = page_alloc(&mem_pool, NUM_REMAP_BITMAP_PAGES);
391         remap_pool.used_pages =
392                 hypervisor_header.possible_cpus * NUM_TEMPORARY_PAGES;
393         for (n = 0; n < remap_pool.used_pages; n++)
394                 set_bit(n, remap_pool.used_bitmap);
395
396         arch_paging_init();
397
398         hv_paging_structs.root_paging = hv_paging;
399         hv_paging_structs.root_table = page_alloc(&mem_pool, 1);
400         if (!hv_paging_structs.root_table)
401                 goto error_nomem;
402
403         /* Replicate hypervisor mapping of Linux */
404         err = page_map_create(&hv_paging_structs,
405                               page_map_hvirt2phys(&hypervisor_header),
406                               system_config->hypervisor_memory.size,
407                               (unsigned long)&hypervisor_header,
408                               PAGE_DEFAULT_FLAGS, PAGE_MAP_NON_COHERENT);
409         if (err)
410                 goto error_nomem;
411
412         /* Make sure any remappings to the temporary regions can be performed
413          * without allocations of page table pages. */
414         err = page_map_create(&hv_paging_structs, 0,
415                               remap_pool.used_pages * PAGE_SIZE,
416                               TEMPORARY_MAPPING_BASE, PAGE_NONPRESENT_FLAGS,
417                               PAGE_MAP_NON_COHERENT);
418         if (err)
419                 goto error_nomem;
420
421         return 0;
422
423 error_nomem:
424         printk("FATAL: page pool much too small\n");
425         return -ENOMEM;
426 }
427
428 void page_map_dump_stats(const char *when)
429 {
430         printk("Page pool usage %s: mem %d/%d, remap %d/%d\n", when,
431                mem_pool.used_pages, mem_pool.pages,
432                remap_pool.used_pages, remap_pool.pages);
433 }