Skip to content
This repository was archived by the owner on Jan 28, 2023. It is now read-only.

Commit 5993f56

Browse files
authored
Merge pull request #137 from krytarowski/netbsd-1e
Add experimental NetBSD host support
2 parents f40dfd2 + bbf540c commit 5993f56

19 files changed

+2641
-0
lines changed

core/include/config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ struct config_t {
6565
};
6666

6767
#define HAX_MAX_VCPUS 16
68+
69+
#ifdef HAX_PLATFORM_NETBSD
70+
// TODO: Handle 64 VMs
71+
#define HAX_MAX_VMS 8
72+
#else
6873
// Matches the number of bits in vm_mid_bits (see vm.c)
6974
#define HAX_MAX_VMS 64
75+
#endif
7076

7177
#endif // HAX_CORE_CONFIG_H_

include/hax.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ int hax_em64t_enabled(void);
269269
#ifdef HAX_PLATFORM_LINUX
270270
#include "linux/hax_linux.h"
271271
#endif
272+
#ifdef HAX_PLATFORM_NETBSD
273+
#include "netbsd/hax_netbsd.h"
274+
#endif
272275
#ifdef HAX_PLATFORM_WINDOWS
273276
#include "windows/hax_windows.h"
274277
#endif

include/hax_interface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
#ifdef HAX_PLATFORM_LINUX
4646
#include "linux/hax_interface_linux.h"
4747
#endif
48+
#ifdef HAX_PLATFORM_NETBSD
49+
#include "netbsd/hax_interface_netbsd.h"
50+
#endif
4851
#ifdef HAX_PLATFORM_WINDOWS
4952
#include "windows/hax_interface_windows.h"
5053
#endif

include/hax_types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@
7878
#elif defined(__linux__)
7979
#define HAX_PLATFORM_LINUX
8080
#include "linux/hax_types_linux.h"
81+
// NetBSD
82+
#elif defined(__NetBSD__)
83+
#define HAX_PLATFORM_NETBSD
84+
#include "netbsd/hax_types_netbsd.h"
8185
// Windows
8286
#elif defined(_WIN32)
8387
#define HAX_PLATFORM_WINDOWS
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2011 Intel Corporation
3+
* Copyright (c) 2018 Kamil Rytarowski
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef HAX_NETBSD_HAX_INTERFACE_NETBSD_H_
33+
#define HAX_NETBSD_HAX_INTERFACE_NETBSD_H_
34+
35+
#include <sys/param.h>
36+
#include <sys/types.h>
37+
#include <sys/ioctl.h>
38+
#include <sys/ioccom.h>
39+
40+
/* The mac specific interface to qemu because of mac's
41+
* special handling like hax tunnel allocation etc */
42+
/* HAX model level ioctl */
43+
#define HAX_IOCTL_VERSION _IOWR(0, 0x20, struct hax_module_version)
44+
#define HAX_IOCTL_CREATE_VM _IOWR(0, 0x21, uint32_t)
45+
#define HAX_IOCTL_DESTROY_VM _IOW(0, 0x22, uint32_t)
46+
#define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo)
47+
#define HAX_IOCTL_SET_MEMLIMIT _IOWR(0, 0x24, struct hax_set_memlimit)
48+
49+
// Only for backward compatibility with old Qemu.
50+
#define HAX_VM_IOCTL_VCPU_CREATE_ORIG _IOR(0, 0x80, int)
51+
52+
#define HAX_VM_IOCTL_VCPU_CREATE _IOWR(0, 0x80, uint32_t)
53+
#define HAX_VM_IOCTL_ALLOC_RAM _IOWR(0, 0x81, struct hax_alloc_ram_info)
54+
#define HAX_VM_IOCTL_SET_RAM _IOWR(0, 0x82, struct hax_set_ram_info)
55+
#define HAX_VM_IOCTL_VCPU_DESTROY _IOR(0, 0x83, uint32_t)
56+
#define HAX_VM_IOCTL_ADD_RAMBLOCK _IOW(0, 0x85, struct hax_ramblock_info)
57+
#define HAX_VM_IOCTL_SET_RAM2 _IOWR(0, 0x86, struct hax_set_ram_info2)
58+
#define HAX_VM_IOCTL_PROTECT_RAM _IOWR(0, 0x87, struct hax_protect_ram_info)
59+
60+
#define HAX_VCPU_IOCTL_RUN _IO(0, 0xc0)
61+
#define HAX_VCPU_IOCTL_SET_MSRS _IOWR(0, 0xc1, struct hax_msr_data)
62+
#define HAX_VCPU_IOCTL_GET_MSRS _IOWR(0, 0xc2, struct hax_msr_data)
63+
64+
#define HAX_VCPU_IOCTL_SET_FPU _IOW(0, 0xc3, struct fx_layout)
65+
#define HAX_VCPU_IOCTL_GET_FPU _IOR(0, 0xc4, struct fx_layout)
66+
67+
#define HAX_VCPU_IOCTL_SETUP_TUNNEL _IOWR(0, 0xc5, struct hax_tunnel_info)
68+
#define HAX_VCPU_IOCTL_INTERRUPT _IOWR(0, 0xc6, uint32_t)
69+
#define HAX_VCPU_SET_REGS _IOWR(0, 0xc7, struct vcpu_state_t)
70+
#define HAX_VCPU_GET_REGS _IOWR(0, 0xc8, struct vcpu_state_t)
71+
72+
/* API 2.0 */
73+
#define HAX_VM_IOCTL_NOTIFY_QEMU_VERSION _IOW(0, 0x84, struct hax_qemu_version)
74+
75+
#define HAX_IOCTL_VCPU_DEBUG _IOW(0, 0xc9, struct hax_debug_t)
76+
77+
#ifdef _KERNEL
78+
#define HAX_KERNEL64_CS 0x80
79+
#define HAX_KERNEL32_CS 0x08
80+
81+
#define is_compatible() 0
82+
83+
struct hax_vm_netbsd_t;
84+
85+
struct hax_vm_softc {
86+
device_t sc_dev;
87+
struct hax_vm_netbsd_t *vm;
88+
};
89+
90+
struct hax_vcpu_netbsd_t;
91+
92+
struct hax_vcpu_softc {
93+
device_t sc_dev;
94+
struct hax_vcpu_netbsd_t *vcpu;
95+
};
96+
97+
struct vm_t;
98+
99+
typedef struct hax_vm_netbsd_t {
100+
struct vm_t *cvm;
101+
int id;
102+
} hax_vm_netbsd_t;
103+
104+
struct vcpu_t;
105+
106+
typedef struct hax_vcpu_netbsd_t {
107+
struct vcpu_t *cvcpu;
108+
struct hax_vm_netbsd_t *vm;
109+
int id;
110+
} hax_vcpu_netbsd_t;
111+
112+
#define unit2vmmid(u) (__SHIFTOUT(u, __BITS(4,6)))
113+
#define unit2vcpuid(u) (__SHIFTOUT(u, __BITS(0,3)))
114+
115+
#define vmvcpu2unit(vm,vcpu) (__SHIFTIN(vm, __BITS(4,6)) | vcpu)
116+
#endif
117+
118+
#endif // HAX_NETBSD_HAX_INTERFACE_NETBSD_H_

include/netbsd/hax_netbsd.h

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2011 Intel Corporation
3+
* Copyright (c) 2018 Kamil Rytarowski
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef HAX_NETBSD_HAX_NETBSD_H_
33+
#define HAX_NETBSD_HAX_NETBSD_H_
34+
35+
#define HAX_RAM_ENTRY_SIZE 0x4000000
36+
37+
hax_spinlock *hax_spinlock_alloc_init(void);
38+
void hax_spinlock_free(hax_spinlock *lock);
39+
void hax_spin_lock(hax_spinlock *lock);
40+
void hax_spin_unlock(hax_spinlock *lock);
41+
42+
hax_mutex hax_mutex_alloc_init(void);
43+
void hax_mutex_lock(hax_mutex lock);
44+
void hax_mutex_unlock(hax_mutex lock);
45+
void hax_mutex_free(hax_mutex lock);
46+
47+
/* Return true if the bit is set already */
48+
int hax_test_and_set_bit(int bit, uint64_t *memory);
49+
50+
/* Return true if the bit is cleared already */
51+
int hax_test_and_clear_bit(int bit, uint64_t *memory);
52+
53+
/* Don't care for the big endian situation */
54+
static inline bool hax_test_bit(int bit, uint64_t *memory)
55+
{
56+
int byte = bit / 8;
57+
unsigned char *p;
58+
int offset = bit % 8;
59+
60+
p = (unsigned char *)memory + byte;
61+
return !!(*p & (1 << offset));
62+
}
63+
64+
// memcpy_s() is part of the optional Bounds Checking Interfaces specified in
65+
// Annex K of the C11 standard:
66+
// http://en.cppreference.com/w/c/string/byte/memcpy
67+
// However, it is not implemented by Clang:
68+
// https://stackoverflow.com/questions/40829032/how-to-install-c11-compiler-on-mac-os-with-optional-string-functions-included
69+
// Provide a simplified implementation here so memcpy_s() can be used instead of
70+
// memcpy() everywhere else, which helps reduce the number of Klocwork warnings.
71+
static inline int memcpy_s(void *dest, size_t destsz, const void *src,
72+
size_t count)
73+
{
74+
char *dest_start = (char *)dest;
75+
char *dest_end = (char *)dest + destsz;
76+
char *src_start = (char *)src;
77+
char *src_end = (char *)src + count;
78+
bool overlap;
79+
80+
if (count == 0)
81+
return 0;
82+
83+
if (!dest || destsz == 0)
84+
return -EINVAL;
85+
86+
overlap = src_start < dest_start
87+
? dest_start < src_end : src_start < dest_end;
88+
if (!src || count > destsz || overlap) {
89+
memset(dest, 0, destsz);
90+
return -EINVAL;
91+
}
92+
93+
memcpy(dest, src, count);
94+
return 0;
95+
}
96+
97+
bool hax_cmpxchg32(uint32_t old_val, uint32_t new_val, volatile uint32_t *addr);
98+
bool hax_cmpxchg64(uint64_t old_val, uint64_t new_val, volatile uint64_t *addr);
99+
100+
static inline bool cpu_is_online(int cpu)
101+
{
102+
if (cpu < 0 || cpu >= max_cpus)
103+
return 0;
104+
return !!(((mword)1 << cpu) & cpu_online_map);
105+
}
106+
107+
int hax_notify_host_event(enum hax_notify_event event, uint32_t *param,
108+
uint32_t size);
109+
110+
extern int default_hax_log_level;
111+
112+
void hax_error(char *fmt, ...);
113+
void hax_warning(char *fmt, ...);
114+
void hax_info(char *fmt, ...);
115+
void hax_debug(char *fmt, ...);
116+
void hax_log(char *fmt, ...);
117+
118+
#define hax_log hax_info
119+
120+
#define hax_panic panic
121+
122+
#define hax_assert(condition) KASSERT(condition)
123+
124+
#endif // HAX_NETBSD_HAX_NETBSD_H_

0 commit comments

Comments
 (0)