map_ptr_x64.cpp
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #include <stdint.h>
23 
26 
27 namespace bfn
28 {
29 
30 void
32  uintptr_t vmap,
33  uintptr_t virt,
34  uintptr_t cr3,
35  size_t size,
37 {
38  expects(vmap != 0);
39  expects(lower(vmap) == 0);
40  expects(virt != 0);
41  expects(cr3 != 0);
42  expects(lower(cr3) == 0);
43  expects(size != 0);
44 
45  for (auto offset = 0UL; offset < size; offset += x64::page_size)
46  {
47  uintptr_t from;
48  uintptr_t phys;
49  uintptr_t pati;
50  uintptr_t current_virt = virt + offset;
51 
52  while (true)
53  {
55  auto &&pml4_idx = x64::page_table::index(current_virt, from);
56  auto &&pml4_map = make_unique_map_x64<uintptr_t>(cr3);
57  auto &&pml4_pte = page_table_entry_x64{&pml4_map.get()[pml4_idx]};
58 
59  expects(pml4_pte.present());
60  expects(pml4_pte.phys_addr() != 0);
61 
63  auto &&pdpt_idx = x64::page_table::index(current_virt, from);
64  auto &&pdpt_map = make_unique_map_x64<uintptr_t>(pml4_pte.phys_addr());
65  auto &&pdpt_pte = page_table_entry_x64{&pdpt_map.get()[pdpt_idx]};
66 
67  expects(pdpt_pte.present());
68  expects(pdpt_pte.phys_addr() != 0);
69 
70  if (pdpt_pte.ps())
71  {
72  phys = pdpt_pte.phys_addr();
73  pati = pdpt_pte.pat_index_large();
74  break;
75  }
76 
78  auto &&pd_idx = x64::page_table::index(current_virt, from);
79  auto &&pd_map = make_unique_map_x64<uintptr_t>(pdpt_pte.phys_addr());
80  auto &&pd_pte = page_table_entry_x64{&pd_map.get()[pd_idx]};
81 
82  expects(pd_pte.present());
83  expects(pd_pte.phys_addr() != 0);
84 
85  if (pd_pte.ps())
86  {
87  phys = pd_pte.phys_addr();
88  pati = pd_pte.pat_index_large();
89  break;
90  }
91 
93  auto &&pt_idx = x64::page_table::index(current_virt, from);
94  auto &&pt_map = make_unique_map_x64<uintptr_t>(pd_pte.phys_addr());
95  auto &&pt_pte = page_table_entry_x64{&pt_map.get()[pt_idx]};
96 
97  expects(pt_pte.present());
98  expects(pt_pte.phys_addr() != 0);
99 
100  phys = pt_pte.phys_addr();
101  pati = pt_pte.pat_index_4k();
102  break;
103  }
104 
105  auto &&vadr = vmap + offset;
106  auto &&padr = upper(phys, from) | lower(current_virt, from);
107 
108  auto &&perm = x64::memory_attr::rw;
109  auto &&type = x64::msrs::ia32_pat::pa(pat, pati);
110 
111  g_pt->map_4k(vadr, upper(padr), x64::memory_attr::mem_type_to_attr(perm, type));
112  }
113 }
114 
115 }
void uintptr_t uintptr_t cr3
Definition: map_ptr_x64.cpp:33
#define g_pt
void __attribute__((weak)) map_with_cr3(uintptr_t vmap
constexpr const auto from
auto upper(T ptr) noexcept
Definition: upper_lower.h:55
void uintptr_t uintptr_t size_t x64::msrs::value_type pat
Definition: map_ptr_x64.cpp:37
constexpr const auto rw
Definition: mem_attr_x64.h:39
constexpr const auto from
Definition: cpuid_x64.h:86
constexpr const auto from
auto index(const T virt, const F from)
void uintptr_t uintptr_t size_t size
Definition: map_ptr_x64.cpp:33
uint64_t value_type
Definition: msrs_x64.h:40
void uintptr_t virt
Definition: map_ptr_x64.cpp:33
auto pa(T index)
Definition: msrs_x64.h:218
constexpr const auto page_size
Definition: x64.h:35
auto mem_type_to_attr(P perm, T type)
Definition: mem_attr_x64.h:67
auto lower(T ptr) noexcept
Definition: upper_lower.h:36
constexpr const auto from
#define offset(a, b)
Definition: test.cpp:192
constexpr const auto from
void map_with_cr3(uintptr_t vmap, uintptr_t virt, uintptr_t cr3, size_t size, x64::msrs::value_type pat)
expects(lower(vmap)==0)