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 #define _POSIX_C_SOURCE 200809L
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <platform.h>
29 #include <sys/mman.h>
30 #include <constants.h>
31 
34 
35 #define PAGE_ROUND_UP(x) ( (((uintptr_t)(x)) + MAX_PAGE_SIZE-1) & (~(MAX_PAGE_SIZE-1)) )
36 
37 uint64_t g_malloc_fails = 0;
38 uint64_t g_set_afinity_fails = 0;
39 uint64_t g_vmcall = 0;
40 
41 int
43 {
44  printf("alloc_count_rw: %d\n", alloc_count_rw);
45  printf("alloc_count_rwe: %d\n", alloc_count_rwe);
46 
47  return (alloc_count_rw == 0) && (alloc_count_rwe == 0);
48 }
49 
50 void *
51 platform_alloc_rw(uint64_t len)
52 {
53  if (g_malloc_fails == len)
54  return 0;
55 
57  return malloc(len);
58 }
59 
60 #include <errno.h>
61 
62 void *
63 platform_alloc_rwe(uint64_t len)
64 {
65  void *addr = 0;
66 
67  if (g_malloc_fails == len)
68  return 0;
69 
70  len = PAGE_ROUND_UP(len);
71 
72  if (posix_memalign(&addr, MAX_PAGE_SIZE, len) != 0)
73  return 0;
74 
75  if (mprotect(addr, len, PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
76  {
77  platform_free_rw(addr, len);
78  return 0;
79  }
80 
82 
83  return addr;
84 }
85 
86 void
87 platform_free_rw(void *addr, uint64_t len)
88 {
89  (void)len;
90 
92  free(addr);
93 }
94 
95 void
96 platform_free_rwe(void *addr, uint64_t len)
97 {
98  (void) len;
99 
100  alloc_count_rwe--;
101  free(addr);
102 }
103 
104 void *
106 {
107  return virt;
108 }
109 
110 void
111 platform_memset(void *ptr, char value, uint64_t num)
112 {
113  if (!ptr)
114  return;
115 
116  memset(ptr, value, num);
117 }
118 
119 void
120 platform_memcpy(void *dst, const void *src, uint64_t num)
121 {
122  if (!dst || !src)
123  return;
124 
125  memcpy(dst, src, num);
126 }
127 
128 void
130 {
131 }
132 
133 void
135 {
136 }
137 
138 int64_t
140 {
141  return 1;
142 }
143 
144 int64_t
145 platform_set_affinity(int64_t affinity)
146 {
147  (void) affinity;
148 
149  if (g_set_afinity_fails != 0)
150  return -1;
151 
152  return 0;
153 }
154 
155 void
156 platform_restore_affinity(int64_t affinity)
157 {
158  (void) affinity;
159 }
160 
161 void
163 {
164  regs->r01 = g_vmcall;
165 }
166 
167 void
169 {
170  regs->r01 = g_vmcall;
171 }
uint64_t g_vmcall
Definition: platform.c:39
int posix_memalign(void **memptr, size_t alignment, size_t size)
Definition: syscall.cpp:305
uint64_t g_set_afinity_fails
Definition: platform.c:38
void free(void *ptr)
Definition: syscall.cpp:370
void * memset(void *block, int c, size_t size)
int alloc_count_rw
Definition: platform.c:32
void platform_start(void)
Definition: platform.c:136
void platform_free_rwe(void *addr, uint64_t len)
Definition: platform.c:95
uint64_t g_malloc_fails
Definition: platform.c:37
void * malloc(size_t size)
Definition: syscall.cpp:364
void platform_memset(void *ptr, char value, uint64_t num)
Definition: platform.c:118
void platform_stop(void)
Definition: platform.c:144
int alloc_count_rwe
Definition: platform.c:33
void * platform_alloc_rw(uint64_t len)
Definition: platform.c:43
int64_t platform_num_cpus(void)
Definition: platform.c:152
#define MAX_PAGE_SIZE
Definition: constants.h:116
constexpr const auto addr
Definition: cpuid_x64.h:80
void platform_vmcall(struct vmcall_registers_t *regs)
Definition: platform.c:162
int64_t platform_set_affinity(int64_t affinity)
Definition: platform.c:163
void platform_restore_affinity(int64_t affinity)
Definition: platform.c:182
void * platform_alloc_rwe(uint64_t len)
Definition: platform.c:62
constexpr page_table_x64::integer_pointer virt
int verify_no_mem_leaks(void)
Definition: platform.c:42
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
#define PAGE_ROUND_UP(x)
Definition: platform.c:35
void * platform_virt_to_phys(void *virt)
Definition: platform.c:109
void platform_vmcall_event(struct vmcall_registers_t *regs)
Definition: platform.c:168