platform.c
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 
23 #include <ntddk.h>
24 
25 #include <platform.h>
26 #include <bfelf_loader.h>
27 
28 #define BF_TAG 'BFLK'
29 #define BF_NX_TAG 'BFNX'
30 
31 void *
32 platform_alloc_rw(uint64_t len)
33 {
34  void *addr = NULL;
35 
36  if (len == 0)
37  {
38  ALERT("platform_alloc: invalid length\n");
39  return addr;
40  }
41 
42  addr = ExAllocatePoolWithTag(NonPagedPool, len, BF_TAG);
43 
44  if (addr == NULL)
45  ALERT("platform_alloc_rw: failed to ExAllocatePoolWithTag mem: %lld\n", len);
46 
47  return addr;
48 }
49 
50 void *
51 platform_alloc_rwe(uint64_t len)
52 {
53  void *addr = NULL;
54 
55  if (len == 0)
56  {
57  ALERT("platform_alloc: invalid length\n");
58  return addr;
59  }
60 
61  addr = ExAllocatePoolWithTag(NonPagedPoolExecute, len, BF_TAG);
62 
63  if (addr == NULL)
64  ALERT("platform_alloc_rw: failed to ExAllocatePoolWithTag mem: %lld\n", len);
65 
66  return addr;
67 }
68 
69 void *
71 {
72  PHYSICAL_ADDRESS addr = MmGetPhysicalAddress(virt);
73 
74  return (void *)addr.QuadPart;
75 }
76 
77 void
78 platform_free_rw(void *addr, uint64_t len)
79 {
80  (void) len;
81 
82  if (addr == NULL)
83  {
84  ALERT("platform_free_rw: invalid address %p\n", addr);
85  return;
86  }
87 
88  ExFreePoolWithTag(addr, BF_TAG);
89 }
90 
91 void
92 platform_free_rwe(void *addr, uint64_t len)
93 {
94  (void) len;
95 
96  if (addr == NULL)
97  {
98  ALERT("platform_free_rw: invalid address %p\n", addr);
99  return;
100  }
101 
102  ExFreePoolWithTag(addr, BF_TAG);
103 }
104 
105 void
106 platform_memset(void *ptr, char value, uint64_t num)
107 {
108  if (!ptr)
109  return;
110 
111  RtlFillMemory(ptr, num, value);
112 }
113 
114 void
115 platform_memcpy(void *dst, const void *src, uint64_t num)
116 {
117  if (!dst || !src)
118  return;
119 
120  RtlCopyMemory(dst, src, num);
121 }
122 
123 void
125 {
126 
127 }
128 
129 void
131 {
132 
133 }
134 
135 int64_t
137 {
138  KAFFINITY k_affin;
139  return (int64_t)KeQueryActiveProcessorCount(&k_affin);
140 }
141 
142 int64_t
143 platform_set_affinity(int64_t affinity)
144 {
145  KAFFINITY new_affinity = (1ULL << affinity);
146  return (int64_t)KeSetSystemAffinityThreadEx(new_affinity);
147 }
148 
149 void
150 platform_restore_affinity(int64_t affinity)
151 {
152  KeRevertToUserAffinityThreadEx((KAFFINITY)(affinity));
153 }
void platform_start(void)
Definition: platform.c:136
void platform_free_rwe(void *addr, uint64_t len)
Definition: platform.c:95
void platform_memset(void *ptr, char value, uint64_t num)
Definition: platform.c:118
void platform_stop(void)
Definition: platform.c:144
#define BF_TAG
Definition: platform.c:28
void * platform_alloc_rw(uint64_t len)
Definition: platform.c:43
int64_t platform_num_cpus(void)
Definition: platform.c:152
constexpr const auto addr
Definition: cpuid_x64.h:80
int64_t platform_set_affinity(int64_t affinity)
Definition: platform.c:163
void platform_restore_affinity(int64_t affinity)
Definition: platform.c:182
#define ALERT(...)
Definition: debug.h:156
void * platform_alloc_rwe(uint64_t len)
Definition: platform.c:62
constexpr page_table_x64::integer_pointer virt
void platform_memcpy(void *dst, const void *src, uint64_t num)
Definition: platform.c:127
void platform_free_rw(void *addr, uint64_t len)
Definition: platform.c:81
void * platform_virt_to_phys(void *virt)
Definition: platform.c:109