-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmm.c
104 lines (89 loc) · 3.12 KB
/
vmm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#define LOG_PFX VMM
#include "vmm.h"
// vmm_dispatch() is a PowerPC-only system call that allows us to invoke
// functions residing in the Vmm dispatch table. In general, Vmm routines
// are available to user space, but the C library (or another library) does
// not contain stubs to call them. Thus, we must go through vmm_dispatch(),
// using the index of the function to call as the first parameter in GPR3.
//
// Since vmachmon.h contains the kernel prototype of vmm_dispatch(), which
// is not what we want, we will declare our own function pointer and set
// it to the stub available in the C library.
//
vmm_dispatch_func_t vmm_call;
static vmm_version_t vmm_version;
// Convenience data structure for pretty-printing Vmm features
struct VmmFeature {
int32_t mask;
char *name;
} VmmFeatures[] = {
{ kVmmFeature_LittleEndian, "LittleEndian" },
{ kVmmFeature_Stop, "Stop" },
{ kVmmFeature_ExtendedMapping, "ExtendedMapping" },
{ kVmmFeature_ListMapping, "ListMapping" },
{ kVmmFeature_FastAssist, "FastAssist" },
{ kVmmFeature_XA, "XA" },
{ kVmmFeature_SixtyFourBit, "SixtyFourBit" },
{ kVmmFeature_MultAddrSpace, "MultAddrSpace" },
{ kVmmFeature_GuestShadowAssist, "GuestShadowAssist" },
{ kVmmFeature_GlobalMappingAssist, "GlobalMappingAssist" },
{ kVmmFeature_HostShadowAssist, "HostShadowAssist" },
{ kVmmFeature_MultAddrSpaceAssist, "MultAddrSpaceAssist" },
{ -1, NULL },
};
const char *
vmm_return_code_to_string(vmm_return_code_t code)
{
#define _VMM_RETURN_CODE(x) case x: { \
return #x; \
break; \
}
switch(code) {
VMM_RETURN_CODES
default:
return "unknown";
}
#undef _VMM_RETURN_CODE
}
err_t
vmm_init(void)
{
int i;
vmm_features_t features;
vmm_call = (vmm_dispatch_func_t) vmm_dispatch;
vmm_version = vmm_call(kVmmGetVersion);
LOG("Mac OS X virtual machine monitor (version %lu.%lu)",
(vmm_version >> 16), (vmm_version & 0xFFFF));
features = vmm_call(kVmmvGetFeatures);
DEBUG("Vmm features:");
for (i = 0; VmmFeatures[i].mask != -1; i++){
DEBUG(" %-20s = %s", VmmFeatures[i].name,
(features & VmmFeatures[i].mask) ? "Yes" : "No");
}
DEBUG("Page size is %u bytes", vm_page_size);
return ERR_NONE;
}
err_t
vmm_init_vm(vmm_state_page_t **vm_state)
{
kern_return_t kr;
mach_port_t mt;
ha_t vmmUStatePage = 0;
vmm_state_page_t *vmmUState = NULL; // It's a vmm_comm_page_t too
mt = mach_task_self();
// VM user state
kr = vm_allocate(mt, &vmmUStatePage, vm_page_size, VM_FLAGS_ANYWHERE);
ON_MACH_ERROR("vm_allocate", kr, out);
vmmUState = (vmm_state_page_t *)vmmUStatePage;
// Initialize a new virtual machine context
kr = vmm_call(kVmmInitContext, vmm_version, vmmUState);
ON_MACH_ERROR("kVmmInitContext", kr, out);
kr = vmm_call(kVmmActivateXA, vmmUState->thread_index, vmmGSA);
ON_MACH_ERROR("kVmmActivateXA", kr, out);
*vm_state = vmmUState;
out:
if (kr != KERN_SUCCESS) {
return ERR_MACH;
}
return ERR_NONE;
}