blob: 28029be47fbb839f248826b517a9e295f4389395 [file] [log] [blame]
Kees Cook3a947072016-05-06 15:01:35 -07001/*
2 * This code is used on x86_64 to create page table identity mappings on
3 * demand by building up a new set of page tables (or appending to the
4 * existing ones), and then switching over to them when ready.
Kees Cook11fdf972016-05-25 15:45:31 -07005 *
6 * Copyright (C) 2015-2016 Yinghai Lu
7 * Copyright (C) 2016 Kees Cook
Kees Cook3a947072016-05-06 15:01:35 -07008 */
9
10/*
11 * Since we're dealing with identity mappings, physical and virtual
12 * addresses are the same, so override these defines which are ultimately
13 * used by the headers in misc.h.
14 */
15#define __pa(x) ((unsigned long)(x))
16#define __va(x) ((void *)((unsigned long)(x)))
17
18#include "misc.h"
19
20/* These actually do the work of building the kernel identity maps. */
21#include <asm/init.h>
22#include <asm/pgtable.h>
Thomas Garnier021182e2016-06-21 17:47:03 -070023/* Use the static base for this part of the boot process */
24#undef __PAGE_OFFSET
25#define __PAGE_OFFSET __PAGE_OFFSET_BASE
Kees Cook3a947072016-05-06 15:01:35 -070026#include "../../mm/ident_map.c"
27
28/* Used by pgtable.h asm code to force instruction serialization. */
29unsigned long __force_order;
30
31/* Used to track our page table allocation area. */
32struct alloc_pgt_data {
33 unsigned char *pgt_buf;
34 unsigned long pgt_buf_size;
35 unsigned long pgt_buf_offset;
36};
37
38/*
39 * Allocates space for a page table entry, using struct alloc_pgt_data
40 * above. Besides the local callers, this is used as the allocation
41 * callback in mapping_info below.
42 */
43static void *alloc_pgt_page(void *context)
44{
45 struct alloc_pgt_data *pages = (struct alloc_pgt_data *)context;
46 unsigned char *entry;
47
48 /* Validate there is space available for a new page. */
49 if (pages->pgt_buf_offset >= pages->pgt_buf_size) {
50 debug_putstr("out of pgt_buf in " __FILE__ "!?\n");
51 debug_putaddr(pages->pgt_buf_offset);
52 debug_putaddr(pages->pgt_buf_size);
53 return NULL;
54 }
55
56 entry = pages->pgt_buf + pages->pgt_buf_offset;
57 pages->pgt_buf_offset += PAGE_SIZE;
58
59 return entry;
60}
61
62/* Used to track our allocated page tables. */
63static struct alloc_pgt_data pgt_data;
64
65/* The top level page table entry pointer. */
Kirill A. Shutemova24261d2017-06-28 15:17:30 +030066static unsigned long top_level_pgt;
Kees Cook3a947072016-05-06 15:01:35 -070067
Kees Cook11fdf972016-05-25 15:45:31 -070068/*
69 * Mapping information structure passed to kernel_ident_mapping_init().
70 * Due to relocation, pointers must be assigned at run time not build time.
71 */
72static struct x86_mapping_info mapping_info = {
Xunlei Pang66aad4f2017-05-04 09:42:50 +080073 .page_flag = __PAGE_KERNEL_LARGE_EXEC,
Kees Cook11fdf972016-05-25 15:45:31 -070074};
75
Kees Cook3a947072016-05-06 15:01:35 -070076/* Locates and clears a region for a new top level page table. */
Kees Cook11fdf972016-05-25 15:45:31 -070077void initialize_identity_maps(void)
Kees Cook3a947072016-05-06 15:01:35 -070078{
Kees Cook11fdf972016-05-25 15:45:31 -070079 /* Init mapping_info with run-time function/buffer pointers. */
80 mapping_info.alloc_pgt_page = alloc_pgt_page;
81 mapping_info.context = &pgt_data;
82
Kees Cook3a947072016-05-06 15:01:35 -070083 /*
84 * It should be impossible for this not to already be true,
85 * but since calling this a second time would rewind the other
86 * counters, let's just make sure this is reset too.
87 */
88 pgt_data.pgt_buf_offset = 0;
89
90 /*
91 * If we came here via startup_32(), cr3 will be _pgtable already
92 * and we must append to the existing area instead of entirely
93 * overwriting it.
Kirill A. Shutemova24261d2017-06-28 15:17:30 +030094 *
95 * With 5-level paging, we use '_pgtable' to allocate the p4d page table,
96 * the top-level page table is allocated separately.
97 *
98 * p4d_offset(top_level_pgt, 0) would cover both the 4- and 5-level
99 * cases. On 4-level paging it's equal to 'top_level_pgt'.
Kees Cook3a947072016-05-06 15:01:35 -0700100 */
Kirill A. Shutemova24261d2017-06-28 15:17:30 +0300101 top_level_pgt = read_cr3_pa();
102 if (p4d_offset((pgd_t *)top_level_pgt, 0) == (p4d_t *)_pgtable) {
Kees Cook3a947072016-05-06 15:01:35 -0700103 debug_putstr("booted via startup_32()\n");
104 pgt_data.pgt_buf = _pgtable + BOOT_INIT_PGT_SIZE;
105 pgt_data.pgt_buf_size = BOOT_PGT_SIZE - BOOT_INIT_PGT_SIZE;
106 memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
107 } else {
108 debug_putstr("booted via startup_64()\n");
109 pgt_data.pgt_buf = _pgtable;
110 pgt_data.pgt_buf_size = BOOT_PGT_SIZE;
111 memset(pgt_data.pgt_buf, 0, pgt_data.pgt_buf_size);
Kirill A. Shutemova24261d2017-06-28 15:17:30 +0300112 top_level_pgt = (unsigned long)alloc_pgt_page(&pgt_data);
Kees Cook3a947072016-05-06 15:01:35 -0700113 }
114}
115
116/*
Kees Cook3a947072016-05-06 15:01:35 -0700117 * Adds the specified range to what will become the new identity mappings.
118 * Once all ranges have been added, the new mapping is activated by calling
119 * finalize_identity_maps() below.
120 */
121void add_identity_map(unsigned long start, unsigned long size)
122{
Kees Cook3a947072016-05-06 15:01:35 -0700123 unsigned long end = start + size;
124
Kees Cook3a947072016-05-06 15:01:35 -0700125 /* Align boundary to 2M. */
126 start = round_down(start, PMD_SIZE);
127 end = round_up(end, PMD_SIZE);
128 if (start >= end)
129 return;
130
131 /* Build the mapping. */
Kirill A. Shutemova24261d2017-06-28 15:17:30 +0300132 kernel_ident_mapping_init(&mapping_info, (pgd_t *)top_level_pgt,
Kees Cook3a947072016-05-06 15:01:35 -0700133 start, end);
134}
135
136/*
137 * This switches the page tables to the new level4 that has been built
138 * via calls to add_identity_map() above. If booted via startup_32(),
139 * this is effectively a no-op.
140 */
141void finalize_identity_maps(void)
142{
Kirill A. Shutemova24261d2017-06-28 15:17:30 +0300143 write_cr3(top_level_pgt);
Kees Cook3a947072016-05-06 15:01:35 -0700144}