pat_x64.h
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 // Author: Connor Davis <davisc@ainfosec.com>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 
23 #ifndef PAT_X64_H
24 #define PAT_X64_H
25 
27 
28 // *INDENT-OFF*
29 
30 namespace x64
31 {
32 
33 // The PAT has been constructed in such a way that you can take the
34 // x86_64 memory types defined in the Memory Cache Control section
35 // of Volume 3, and use them as an index into the PAT, and get the
36 // correct memory type as a result.
37 //
38 // Sadly, this doesn't work for page directories, as they do not have a
39 // PAT bit to set, and thus can only index into the first half of
40 // the PAT. Thankfully, memory types 2 and 3 are reserved, and thus
41 // would have blank entires in the PAT using this scheme. Therefore,
42 // we have filled in the blank entires with WB and WT, allowing
43 // page directories to use WB as needed.
44 //
45 // Using this scheme, when we map guest memory, we take take the
46 // memory type that the guest has, and use it as an index into the
47 // pat without conversions.
48 
49 namespace pat
50 {
51  constexpr const auto pat_value = 0x0706050406040100UL;
52 
53  constexpr const auto uncacheable_index = 0x00000000UL;
54  constexpr const auto write_combining_index = 0x00000001UL;
55  constexpr const auto write_through_index = 0x00000002UL;
56  constexpr const auto write_protected_index = 0x00000005UL;
57  constexpr const auto write_back_index = 0x00000003UL;
58  constexpr const auto uncacheable_minus_index = 0x00000007UL;
59 
60  template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
61  auto mem_attr_to_pat_index(T attr)
62  {
63  // The attr parameter could either be an x64::memory_type,
64  // or it could be a x64::memory_attr with the big difference
65  // being memory attributes have permissions encoded in them.
66  // This function can filter both and return the PAT index
67 
68  switch (attr & 0xF)
69  {
76 
77  default:
78  throw std::runtime_error("mem_attr_to_pat_index failed: invalid attr");
79  };
80  }
81 }
82 
83 }
84 
85 // *INDENT-ON*
86 
87 #endif
constexpr const auto uncacheable_minus_index
Definition: pat_x64.h:58
constexpr const auto write_through_index
Definition: pat_x64.h:55
auto mem_attr_to_pat_index(T attr)
Definition: pat_x64.h:61
void uintptr_t uintptr_t size_t x64::msrs::value_type pat
Definition: map_ptr_x64.cpp:37
constexpr const auto write_protected_index
Definition: pat_x64.h:56
constexpr const auto pat_value
Definition: pat_x64.h:51
constexpr const auto write_back
Definition: x64.h:46
constexpr const auto uncacheable_index
Definition: pat_x64.h:53
constexpr const auto uncacheable_minus
Definition: x64.h:47
constexpr const auto write_protected
Definition: x64.h:45
constexpr const auto uncacheable
Definition: x64.h:42
constexpr const auto write_back_index
Definition: pat_x64.h:57
constexpr const auto write_combining_index
Definition: pat_x64.h:54
Definition: cache_x64.h:31
constexpr const auto write_through
Definition: x64.h:44
constexpr const auto write_combining
Definition: x64.h:43