Skip to content
This repository was archived by the owner on Jan 28, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ struct config_t {
};

#define HAX_MAX_VCPUS 16

#ifdef HAX_PLATFORM_NETBSD
// TODO: Handle 64 VMs
#define HAX_MAX_VMS 8
#else
// Matches the number of bits in vm_mid_bits (see vm.c)
#define HAX_MAX_VMS 64
#endif

#endif // HAX_CORE_CONFIG_H_
3 changes: 3 additions & 0 deletions include/hax.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ int hax_em64t_enabled(void);
#ifdef HAX_PLATFORM_LINUX
#include "linux/hax_linux.h"
#endif
#ifdef HAX_PLATFORM_NETBSD
#include "netbsd/hax_netbsd.h"
#endif
#ifdef HAX_PLATFORM_WINDOWS
#include "windows/hax_windows.h"
#endif
Expand Down
3 changes: 3 additions & 0 deletions include/hax_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#ifdef HAX_PLATFORM_LINUX
#include "linux/hax_interface_linux.h"
#endif
#ifdef HAX_PLATFORM_NETBSD
#include "netbsd/hax_interface_netbsd.h"
#endif
#ifdef HAX_PLATFORM_WINDOWS
#include "windows/hax_interface_windows.h"
#endif
Expand Down
4 changes: 4 additions & 0 deletions include/hax_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@
#elif defined(__linux__)
#define HAX_PLATFORM_LINUX
#include "linux/hax_types_linux.h"
// NetBSD
#elif defined(__NetBSD__)
#define HAX_PLATFORM_NETBSD
#include "netbsd/hax_types_netbsd.h"
// Windows
#elif defined(_WIN32)
#define HAX_PLATFORM_WINDOWS
Expand Down
118 changes: 118 additions & 0 deletions include/netbsd/hax_interface_netbsd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2011 Intel Corporation
* Copyright (c) 2018 Kamil Rytarowski
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef HAX_NETBSD_HAX_INTERFACE_NETBSD_H_
#define HAX_NETBSD_HAX_INTERFACE_NETBSD_H_

#include <sys/param.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/ioccom.h>

/* The mac specific interface to qemu because of mac's
* special handling like hax tunnel allocation etc */
/* HAX model level ioctl */
#define HAX_IOCTL_VERSION _IOWR(0, 0x20, struct hax_module_version)
#define HAX_IOCTL_CREATE_VM _IOWR(0, 0x21, uint32_t)
#define HAX_IOCTL_DESTROY_VM _IOW(0, 0x22, uint32_t)
#define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo)
#define HAX_IOCTL_SET_MEMLIMIT _IOWR(0, 0x24, struct hax_set_memlimit)

// Only for backward compatibility with old Qemu.
#define HAX_VM_IOCTL_VCPU_CREATE_ORIG _IOR(0, 0x80, int)

#define HAX_VM_IOCTL_VCPU_CREATE _IOWR(0, 0x80, uint32_t)
#define HAX_VM_IOCTL_ALLOC_RAM _IOWR(0, 0x81, struct hax_alloc_ram_info)
#define HAX_VM_IOCTL_SET_RAM _IOWR(0, 0x82, struct hax_set_ram_info)
#define HAX_VM_IOCTL_VCPU_DESTROY _IOR(0, 0x83, uint32_t)
#define HAX_VM_IOCTL_ADD_RAMBLOCK _IOW(0, 0x85, struct hax_ramblock_info)
#define HAX_VM_IOCTL_SET_RAM2 _IOWR(0, 0x86, struct hax_set_ram_info2)
#define HAX_VM_IOCTL_PROTECT_RAM _IOWR(0, 0x87, struct hax_protect_ram_info)

#define HAX_VCPU_IOCTL_RUN _IO(0, 0xc0)
#define HAX_VCPU_IOCTL_SET_MSRS _IOWR(0, 0xc1, struct hax_msr_data)
#define HAX_VCPU_IOCTL_GET_MSRS _IOWR(0, 0xc2, struct hax_msr_data)

#define HAX_VCPU_IOCTL_SET_FPU _IOW(0, 0xc3, struct fx_layout)
#define HAX_VCPU_IOCTL_GET_FPU _IOR(0, 0xc4, struct fx_layout)

#define HAX_VCPU_IOCTL_SETUP_TUNNEL _IOWR(0, 0xc5, struct hax_tunnel_info)
#define HAX_VCPU_IOCTL_INTERRUPT _IOWR(0, 0xc6, uint32_t)
#define HAX_VCPU_SET_REGS _IOWR(0, 0xc7, struct vcpu_state_t)
#define HAX_VCPU_GET_REGS _IOWR(0, 0xc8, struct vcpu_state_t)

/* API 2.0 */
#define HAX_VM_IOCTL_NOTIFY_QEMU_VERSION _IOW(0, 0x84, struct hax_qemu_version)

#define HAX_IOCTL_VCPU_DEBUG _IOW(0, 0xc9, struct hax_debug_t)

#ifdef _KERNEL
#define HAX_KERNEL64_CS 0x80
#define HAX_KERNEL32_CS 0x08

#define is_compatible() 0

struct hax_vm_netbsd_t;

struct hax_vm_softc {
device_t sc_dev;
struct hax_vm_netbsd_t *vm;
};

struct hax_vcpu_netbsd_t;

struct hax_vcpu_softc {
device_t sc_dev;
struct hax_vcpu_netbsd_t *vcpu;
};

struct vm_t;

typedef struct hax_vm_netbsd_t {
struct vm_t *cvm;
int id;
} hax_vm_netbsd_t;

struct vcpu_t;

typedef struct hax_vcpu_netbsd_t {
struct vcpu_t *cvcpu;
struct hax_vm_netbsd_t *vm;
int id;
} hax_vcpu_netbsd_t;

#define unit2vmmid(u) (__SHIFTOUT(u, __BITS(4,6)))
#define unit2vcpuid(u) (__SHIFTOUT(u, __BITS(0,3)))

#define vmvcpu2unit(vm,vcpu) (__SHIFTIN(vm, __BITS(4,6)) | vcpu)
#endif

#endif // HAX_NETBSD_HAX_INTERFACE_NETBSD_H_
124 changes: 124 additions & 0 deletions include/netbsd/hax_netbsd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2011 Intel Corporation
* Copyright (c) 2018 Kamil Rytarowski
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef HAX_NETBSD_HAX_NETBSD_H_
#define HAX_NETBSD_HAX_NETBSD_H_

#define HAX_RAM_ENTRY_SIZE 0x4000000

hax_spinlock *hax_spinlock_alloc_init(void);
void hax_spinlock_free(hax_spinlock *lock);
void hax_spin_lock(hax_spinlock *lock);
void hax_spin_unlock(hax_spinlock *lock);

hax_mutex hax_mutex_alloc_init(void);
void hax_mutex_lock(hax_mutex lock);
void hax_mutex_unlock(hax_mutex lock);
void hax_mutex_free(hax_mutex lock);

/* Return true if the bit is set already */
int hax_test_and_set_bit(int bit, uint64_t *memory);

/* Return true if the bit is cleared already */
int hax_test_and_clear_bit(int bit, uint64_t *memory);

/* Don't care for the big endian situation */
static inline bool hax_test_bit(int bit, uint64_t *memory)
{
int byte = bit / 8;
unsigned char *p;
int offset = bit % 8;

p = (unsigned char *)memory + byte;
return !!(*p & (1 << offset));
}

// memcpy_s() is part of the optional Bounds Checking Interfaces specified in
// Annex K of the C11 standard:
// http://en.cppreference.com/w/c/string/byte/memcpy
// However, it is not implemented by Clang:
// https://stackoverflow.com/questions/40829032/how-to-install-c11-compiler-on-mac-os-with-optional-string-functions-included
// Provide a simplified implementation here so memcpy_s() can be used instead of
// memcpy() everywhere else, which helps reduce the number of Klocwork warnings.
static inline int memcpy_s(void *dest, size_t destsz, const void *src,
size_t count)
{
char *dest_start = (char *)dest;
char *dest_end = (char *)dest + destsz;
char *src_start = (char *)src;
char *src_end = (char *)src + count;
bool overlap;

if (count == 0)
return 0;

if (!dest || destsz == 0)
return -EINVAL;

overlap = src_start < dest_start
? dest_start < src_end : src_start < dest_end;
if (!src || count > destsz || overlap) {
memset(dest, 0, destsz);
return -EINVAL;
}

memcpy(dest, src, count);
return 0;
}

bool hax_cmpxchg32(uint32_t old_val, uint32_t new_val, volatile uint32_t *addr);
bool hax_cmpxchg64(uint64_t old_val, uint64_t new_val, volatile uint64_t *addr);

static inline bool cpu_is_online(int cpu)
{
if (cpu < 0 || cpu >= max_cpus)
return 0;
return !!(((mword)1 << cpu) & cpu_online_map);
}

int hax_notify_host_event(enum hax_notify_event event, uint32_t *param,
uint32_t size);

extern int default_hax_log_level;

void hax_error(char *fmt, ...);
void hax_warning(char *fmt, ...);
void hax_info(char *fmt, ...);
void hax_debug(char *fmt, ...);
void hax_log(char *fmt, ...);

#define hax_log hax_info

#define hax_panic panic

#define hax_assert(condition) KASSERT(condition)

#endif // HAX_NETBSD_HAX_NETBSD_H_
Loading