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

Commit c276671

Browse files
committed
Add experimental NetBSD host support
This module can already boot FREEDOS on NetBSD/amd64.
1 parent eda0250 commit c276671

18 files changed

+2640
-0
lines changed

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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
#define HAX_KERNEL64_CS 0x80
78+
#define HAX_KERNEL32_CS 0x08
79+
80+
#define is_compatible() 0
81+
82+
struct hax_vm_netbsd_t;
83+
84+
struct hax_vm_softc {
85+
device_t sc_dev;
86+
struct hax_vm_netbsd_t *vm;
87+
};
88+
89+
struct hax_vcpu_netbsd_t;
90+
91+
struct hax_vcpu_softc {
92+
device_t sc_dev;
93+
struct hax_vcpu_netbsd_t *vcpu;
94+
};
95+
96+
#define HAX_MAX_VMS 8
97+
98+
struct vm_t;
99+
100+
typedef struct hax_vm_netbsd_t {
101+
struct vm_t *cvm;
102+
int id;
103+
} hax_vm_netbsd_t;
104+
105+
struct vcpu_t;
106+
107+
typedef struct hax_vcpu_netbsd_t {
108+
struct vcpu_t *cvcpu;
109+
struct hax_vm_netbsd_t *vm;
110+
int id;
111+
} hax_vcpu_netbsd_t;
112+
113+
#define unit2vmmid(u) (__SHIFTOUT(u, __BITS(4,6)))
114+
#define unit2vcpuid(u) (__SHIFTOUT(u, __BITS(0,3)))
115+
116+
#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_

include/netbsd/hax_types_netbsd.h

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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_TYPES_NETBSD_H_
33+
#define HAX_NETBSD_HAX_TYPES_NETBSD_H_
34+
35+
#include <sys/param.h>
36+
#include <sys/types.h>
37+
38+
// Signed Types
39+
typedef int8_t int8;
40+
typedef int16_t int16;
41+
typedef int32_t int32;
42+
typedef int64_t int64;
43+
44+
// Unsigned Types
45+
typedef uint8_t uint8;
46+
typedef uint16_t uint16;
47+
typedef uint32_t uint32;
48+
typedef uint64_t uint64;
49+
50+
typedef unsigned int uint;
51+
typedef unsigned long ulong;
52+
typedef unsigned long ulong_t;
53+
54+
#if defined(__i386__)
55+
typedef uint32_t mword;
56+
#endif
57+
#if defined (__x86_64__)
58+
typedef uint64_t mword;
59+
#endif
60+
typedef mword HAX_VADDR_T;
61+
62+
#include "../hax_list.h"
63+
struct hax_page {
64+
void *kva;
65+
struct vm_page *page;
66+
struct pglist *pglist;
67+
uint64_t pa;
68+
uint32_t order;
69+
uint32_t flags;
70+
struct hax_link_list list;
71+
size_t size;
72+
};
73+
74+
typedef struct hax_memdesc_user {
75+
vaddr_t uva;
76+
vsize_t size;
77+
} hax_memdesc_user;
78+
79+
typedef struct hax_kmap_user {
80+
vaddr_t kva;
81+
vsize_t size;
82+
} hax_kmap_user;
83+
84+
typedef struct hax_memdesc_phys {
85+
struct vm_page *page;
86+
} hax_memdesc_phys;
87+
88+
typedef struct hax_kmap_phys {
89+
vaddr_t kva;
90+
} hax_kmap_phys;
91+
92+
typedef struct hax_spinlock hax_spinlock;
93+
94+
typedef int hax_cpumap_t;
95+
96+
static inline hax_cpumap_t cpu2cpumap(int cpu)
97+
{
98+
return (0x1 << cpu);
99+
}
100+
101+
/* Remove this later */
102+
#define is_leaf(x) 1
103+
104+
typedef mword preempt_flag;
105+
typedef kmutex_t *hax_mutex;
106+
typedef uint32_t hax_atomic_t;
107+
108+
/* Return the value before add */
109+
hax_atomic_t hax_atomic_add(volatile hax_atomic_t *atom, uint32_t value);
110+
111+
/* Return the value before the increment */
112+
hax_atomic_t hax_atomic_inc(volatile hax_atomic_t *atom);
113+
114+
/* Return the value before the decrement */
115+
hax_atomic_t hax_atomic_dec(volatile hax_atomic_t *atom);
116+
117+
void hax_smp_mb(void);
118+
119+
#endif // HAX_NETBSD_HAX_TYPES_NETBSD_H_

0 commit comments

Comments
 (0)