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

Commit 396bba3

Browse files
authoredMar 27, 2020
Merge pull request #281 from krytarowski/netbsd-8
Fix NetBSD build and add support for HAX_VCPU_IOCTL_SET_CPUID
2 parents 93998a7 + 21a2f78 commit 396bba3

File tree

6 files changed

+76
-27
lines changed

6 files changed

+76
-27
lines changed
 

‎core/cpuid.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
#define CPUID_CACHE_SIZE 6
3636

3737
typedef struct cpuid_cache_t {
38-
uint32_t data[CPUID_CACHE_SIZE]; // Host cached features
39-
cpuid_t host_supported; // Physical CPU supported features
40-
cpuid_t hax_supported; // Hypervisor supported features
41-
bool initialized;
38+
uint32_t data[CPUID_CACHE_SIZE]; // Host cached features
39+
hax_cpuid_t host_supported; // Physical CPU supported features
40+
hax_cpuid_t hax_supported; // Hypervisor supported features
41+
bool initialized;
4242
} cpuid_cache_t;
4343

4444
typedef union cpuid_feature_t {
@@ -59,9 +59,9 @@ static cpuid_cache_t cache = {0};
5959

6060
static hax_cpuid_entry * find_cpuid_entry(hax_cpuid *cpuid_info,
6161
uint32_t function, uint32_t index);
62-
static void cpuid_set_0000_0001(cpuid_t *cpuid, hax_cpuid *cpuid_info);
63-
static void cpuid_set_8000_0001(cpuid_t *cpuid, hax_cpuid *cpuid_info);
64-
static void cpuid_set_fixed_features(cpuid_t *cpuid);
62+
static void cpuid_set_0000_0001(hax_cpuid_t *cpuid, hax_cpuid *cpuid_info);
63+
static void cpuid_set_8000_0001(hax_cpuid_t *cpuid, hax_cpuid *cpuid_info);
64+
static void cpuid_set_fixed_features(hax_cpuid_t *cpuid);
6565

6666
void cpuid_query_leaf(cpuid_args_t *args, uint32_t leaf)
6767
{
@@ -168,7 +168,7 @@ void cpuid_init_supported_features(void)
168168
cache.host_supported.feature_8000_0001_edx);
169169

170170
// Initialize HAXM supported features
171-
cache.hax_supported = (cpuid_t){
171+
cache.hax_supported = (hax_cpuid_t){
172172
.feature_1_ecx =
173173
FEATURE(SSE3) |
174174
FEATURE(SSSE3) |
@@ -221,23 +221,24 @@ void cpuid_init_supported_features(void)
221221
cache.hax_supported.feature_8000_0001_edx);
222222
}
223223

224-
void cpuid_guest_init(cpuid_t *cpuid)
224+
void cpuid_guest_init(hax_cpuid_t *cpuid)
225225
{
226226
*cpuid = cache.hax_supported;
227227
cpuid->features_mask = ~0ULL;
228228
}
229229

230-
void cpuid_get_features_mask(cpuid_t *cpuid, uint64_t *features_mask)
230+
void cpuid_get_features_mask(hax_cpuid_t *cpuid, uint64_t *features_mask)
231231
{
232232
*features_mask = cpuid->features_mask;
233233
}
234234

235-
void cpuid_set_features_mask(cpuid_t *cpuid, uint64_t features_mask)
235+
void cpuid_set_features_mask(hax_cpuid_t *cpuid, uint64_t features_mask)
236236
{
237237
cpuid->features_mask = features_mask;
238238
}
239239

240-
void cpuid_get_guest_features(cpuid_t *cpuid, uint32_t *cpuid_1_features_ecx,
240+
void cpuid_get_guest_features(hax_cpuid_t *cpuid,
241+
uint32_t *cpuid_1_features_ecx,
241242
uint32_t *cpuid_1_features_edx,
242243
uint32_t *cpuid_8000_0001_features_ecx,
243244
uint32_t *cpuid_8000_0001_features_edx)
@@ -248,9 +249,9 @@ void cpuid_get_guest_features(cpuid_t *cpuid, uint32_t *cpuid_1_features_ecx,
248249
*cpuid_8000_0001_features_edx = cpuid->feature_8000_0001_edx;
249250
}
250251

251-
void cpuid_set_guest_features(cpuid_t *cpuid, hax_cpuid *cpuid_info)
252+
void cpuid_set_guest_features(hax_cpuid_t *cpuid, hax_cpuid *cpuid_info)
252253
{
253-
static void (*cpuid_set_guest_feature[])(cpuid_t *, hax_cpuid *) = {
254+
static void (*cpuid_set_guest_feature[])(hax_cpuid_t *, hax_cpuid *) = {
254255
cpuid_set_0000_0001,
255256
cpuid_set_8000_0001
256257
};
@@ -292,7 +293,7 @@ static hax_cpuid_entry * find_cpuid_entry(hax_cpuid *cpuid_info,
292293
return found;
293294
}
294295

295-
static void cpuid_set_0000_0001(cpuid_t *cpuid, hax_cpuid *cpuid_info)
296+
static void cpuid_set_0000_0001(hax_cpuid_t *cpuid, hax_cpuid *cpuid_info)
296297
{
297298
const uint32_t kFunction = 0x01;
298299
hax_cpuid_entry *entry;
@@ -326,7 +327,7 @@ static void cpuid_set_0000_0001(cpuid_t *cpuid, hax_cpuid *cpuid_info)
326327
}
327328
}
328329

329-
static void cpuid_set_8000_0001(cpuid_t *cpuid, hax_cpuid *cpuid_info)
330+
static void cpuid_set_8000_0001(hax_cpuid_t *cpuid, hax_cpuid *cpuid_info)
330331
{
331332
const uint32_t kFunction = 0x80000001;
332333
hax_cpuid_entry *entry;
@@ -353,7 +354,7 @@ static void cpuid_set_8000_0001(cpuid_t *cpuid, hax_cpuid *cpuid_info)
353354
}
354355
}
355356

356-
static void cpuid_set_fixed_features(cpuid_t *cpuid)
357+
static void cpuid_set_fixed_features(hax_cpuid_t *cpuid)
357358
{
358359
const uint32_t kFixedFeatures =
359360
FEATURE(MCE) |

‎core/include/cpuid.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ typedef union cpuid_args_t {
4949
uint32_t regs[4];
5050
} cpuid_args_t;
5151

52-
typedef struct cpuid_t {
52+
typedef struct hax_cpuid_t {
5353
uint64_t features_mask;
5454
uint32_t feature_1_ecx;
5555
uint32_t feature_1_edx;
5656
uint32_t feature_8000_0001_ecx;
5757
uint32_t feature_8000_0001_edx;
58-
} cpuid_t;
58+
} hax_cpuid_t;
5959

6060
/*
6161
* X86 Features
@@ -261,13 +261,14 @@ bool cpuid_host_has_feature(uint32_t feature_key);
261261
bool cpuid_host_has_feature_uncached(uint32_t feature_key);
262262

263263
void cpuid_init_supported_features(void);
264-
void cpuid_guest_init(cpuid_t *cpuid);
265-
void cpuid_get_features_mask(cpuid_t *cpuid, uint64_t *features_mask);
266-
void cpuid_set_features_mask(cpuid_t *cpuid, uint64_t features_mask);
267-
void cpuid_get_guest_features(cpuid_t *cpuid, uint32_t *cpuid_1_features_ecx,
264+
void cpuid_guest_init(hax_cpuid_t *cpuid);
265+
void cpuid_get_features_mask(hax_cpuid_t *cpuid, uint64_t *features_mask);
266+
void cpuid_set_features_mask(hax_cpuid_t *cpuid, uint64_t features_mask);
267+
void cpuid_get_guest_features(hax_cpuid_t *cpuid,
268+
uint32_t *cpuid_1_features_ecx,
268269
uint32_t *cpuid_1_features_edx,
269270
uint32_t *cpuid_8000_0001_features_ecx,
270271
uint32_t *cpuid_8000_0001_features_edx);
271-
void cpuid_set_guest_features(cpuid_t *cpuid, hax_cpuid *cpuid_info);
272+
void cpuid_set_guest_features(hax_cpuid_t *cpuid, hax_cpuid *cpuid_info);
272273

273274
#endif /* HAX_CORE_CPUID_H_ */

‎core/include/vcpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ struct vcpu_t {
240240
// * All vCPUs share the unique memory, which is actually allocated by the
241241
// first vCPU created by VM. If any vCPU sets features in this field, all
242242
// vCPUs will change accordingly.
243-
cpuid_t *guest_cpuid;
243+
hax_cpuid_t *guest_cpuid;
244244
};
245245

246246
#define vmx(v, field) v->vmx.field

‎core/vcpu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4522,7 +4522,7 @@ static int vcpu_alloc_cpuid(struct vcpu_t *vcpu)
45224522
if (vcpu->vcpu_id != 0)
45234523
return 1;
45244524

4525-
vcpu->guest_cpuid = hax_vmalloc(sizeof(cpuid_t), HAX_MEM_NONPAGE);
4525+
vcpu->guest_cpuid = hax_vmalloc(sizeof(hax_cpuid_t), HAX_MEM_NONPAGE);
45264526
if (vcpu->guest_cpuid == NULL)
45274527
return 0;
45284528

@@ -4544,7 +4544,7 @@ static void vcpu_free_cpuid(struct vcpu_t *vcpu)
45444544
return;
45454545
}
45464546

4547-
hax_vfree(vcpu->guest_cpuid, sizeof(cpuid_t));
4547+
hax_vfree(vcpu->guest_cpuid, sizeof(hax_cpuid_t));
45484548
vcpu->guest_cpuid = NULL;
45494549
hax_log(HAX_LOGI, "%s: freed vcpu[%u].guest_cpuid.\n", __func__,
45504550
vcpu->vcpu_id);

‎include/netbsd/hax_interface_netbsd.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
7474

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

77+
// `hax_cpuid *` is specified as the size of data buffer because `hax_cpuid` is
78+
// a variable-length type. When ioctl() is invoked, the argument of user data
79+
// should pass the address of the pointer to `hax_cpuid`.
80+
#define HAX_VCPU_IOCTL_SET_CPUID _IOW(0, 0xca, struct hax_cpuid *)
81+
7782
#ifdef _KERNEL
7883
#define HAX_KERNEL64_CS 0x80
7984
#define HAX_KERNEL32_CS 0x08

‎platforms/netbsd/hax_entry_vcpu.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,40 @@ struct cdevsw hax_vcpu_cdevsw = {
5858
.d_flag = D_OTHER | D_MPSAFE
5959
};
6060

61+
#define load_user_data(dest, src, body_len, body_max, arg_t, body_t) \
62+
void *uaddr = (void *)(*(arg_t **)(src)); \
63+
size_t size; \
64+
arg_t header; \
65+
(dest) = NULL; \
66+
if (copyin(uaddr, &header, sizeof(arg_t))) { \
67+
hax_log(HAX_LOGE, "%s: argument header read error.\n", __func__); \
68+
ret = -EFAULT; \
69+
break; \
70+
} \
71+
if (header.body_len > (body_max)) { \
72+
hax_log(HAX_LOGW, "%s: %d exceeds argument body maximum %d.\n", \
73+
__func__, header.body_len, (body_max)); \
74+
ret = -E2BIG; \
75+
break; \
76+
} \
77+
size = sizeof(arg_t) + header.body_len * sizeof(body_t); \
78+
(dest) = hax_vmalloc(size, HAX_MEM_NONPAGE); \
79+
if ((dest) == NULL) { \
80+
hax_log(HAX_LOGE, "%s: failed to allocate memory.\n", __func__); \
81+
ret = -ENOMEM; \
82+
break; \
83+
} \
84+
if (copyin(uaddr, (dest), size)) { \
85+
hax_log(HAX_LOGE, "%s: argument read error.\n", __func__); \
86+
unload_user_data(dest); \
87+
ret = -EFAULT; \
88+
break; \
89+
}
90+
91+
#define unload_user_data(dest) \
92+
if ((dest) != NULL) \
93+
hax_vfree((dest), size);
94+
6195
/* VCPU operations */
6296

6397
int hax_vcpu_open(dev_t self, int flag __unused, int mode __unused,
@@ -243,6 +277,14 @@ int hax_vcpu_ioctl(dev_t self, u_long cmd, void *data, int flag,
243277
vcpu_debug(cvcpu, hax_debug);
244278
break;
245279
}
280+
case HAX_VCPU_IOCTL_SET_CPUID: {
281+
struct hax_cpuid *cpuid;
282+
load_user_data(cpuid, data, total, HAX_MAX_CPUID_ENTRIES, hax_cpuid,
283+
hax_cpuid_entry);
284+
ret = vcpu_set_cpuid(cvcpu, cpuid);
285+
unload_user_data(cpuid);
286+
break;
287+
}
246288
default:
247289
// TODO: Print information about the process that sent the ioctl.
248290
hax_log(HAX_LOGE, "Unknown VCPU IOCTL %#lx, pid=%d ('%s')\n", cmd,

0 commit comments

Comments
 (0)