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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! x86_64 hypercall functions

use {
    crate::hypercall::{errno_to_result, Error},
    core::{arch::asm, result::Result},
    cty::c_long,
};

#[repr(C)]
struct HypercallEntry([u8; 32]);

extern "C" {
    static hypercall_page: [HypercallEntry; 128];
}

/// Makes hypercall with 0 arguments
///
/// # Safety
///
/// `offset` must be a valid offset into `hypercall_page`
pub unsafe fn hypercall0(offset: u32) -> Result<u64, Error> {
    let mut res: c_long;
    let entry = hypercall_page.as_ptr().offset(offset as isize);

    asm!("call {}",in(reg) entry,

    // All caller-saved registers must be marked as clobberred
    out("rax") res, out("rcx") _, out("rdx") _, out("rsi") _,
    out("r8") _, out("r9") _, out("r10") _, out("r11") _,
    out("xmm0") _, out("xmm1") _, out("xmm2") _, out("xmm3") _,
    out("xmm4") _, out("xmm5") _, out("xmm6") _, out("xmm7") _,
    out("xmm8") _, out("xmm9") _, out("xmm10") _, out("xmm11") _,
    out("xmm12") _, out("xmm13") _, out("xmm14") _, out("xmm15") _);

    errno_to_result(res)
}

/// Makes hypercall with 1 argument
///
/// # Safety
///
/// `offset` must be a valid offset into `hypercall_page`
pub unsafe fn hypercall1(offset: u32, arg0: u64) -> Result<u64, Error> {
    let mut res: c_long;
    let entry = hypercall_page.as_ptr().offset(offset as isize);

    asm!("call {}",in(reg) entry,

    inout("rdi") arg0 => _,

    // all caller-saved registers must be marked as clobberred
    out("rax") res, out("rcx") _, out("rdx") _, out("rsi") _,
    out("r8") _, out("r9") _, out("r10") _, out("r11") _,
    out("xmm0") _, out("xmm1") _, out("xmm2") _, out("xmm3") _,
    out("xmm4") _, out("xmm5") _, out("xmm6") _, out("xmm7") _,
    out("xmm8") _, out("xmm9") _, out("xmm10") _, out("xmm11") _,
    out("xmm12") _, out("xmm13") _, out("xmm14") _, out("xmm15") _);

    errno_to_result(res)
}

/// Makes hypercall with 2 arguments
///
/// # Safety
///
/// `offset` must be a valid offset into `hypercall_page`
pub unsafe fn hypercall2(offset: u32, arg0: u64, arg1: u64) -> Result<u64, Error> {
    let mut res: c_long;
    let entry = hypercall_page.as_ptr().offset(offset as isize);

    asm!("call {}",in(reg) entry,

    inout("rdi") arg0 => _,
    inout("rsi") arg1 => _,

    // all caller-saved registers must be marked as clobberred
    out("rax") res, out("rcx") _, out("rdx") _,
    out("r8") _, out("r9") _, out("r10") _, out("r11") _,
    out("xmm0") _, out("xmm1") _, out("xmm2") _, out("xmm3") _,
    out("xmm4") _, out("xmm5") _, out("xmm6") _, out("xmm7") _,
    out("xmm8") _, out("xmm9") _, out("xmm10") _, out("xmm11") _,
    out("xmm12") _, out("xmm13") _, out("xmm14") _, out("xmm15") _);

    errno_to_result(res)
}

/// Makes hypercall with 3 arguments
///
/// # Safety
///
/// `offset` must be a valid offset into `hypercall_page`
pub unsafe fn hypercall3(offset: u32, arg0: u64, arg1: u64, arg2: u64) -> Result<u64, Error> {
    let mut res: c_long;
    let entry = hypercall_page.as_ptr().offset(offset as isize);

    asm!("call {}",in(reg) entry,

    inout("rdi") arg0 => _,
    inout("rsi") arg1 => _,
    inout("rdx") arg2 => _,

    // all caller-saved registers must be marked as clobberred
    out("rax") res, out("rcx") _,
    out("r8") _, out("r9") _, out("r10") _, out("r11") _,
    out("xmm0") _, out("xmm1") _, out("xmm2") _, out("xmm3") _,
    out("xmm4") _, out("xmm5") _, out("xmm6") _, out("xmm7") _,
    out("xmm8") _, out("xmm9") _, out("xmm10") _, out("xmm11") _,
    out("xmm12") _, out("xmm13") _, out("xmm14") _, out("xmm15") _);

    errno_to_result(res)
}

/// Makes hypercall with 4 arguments
///
/// # Safety
///
/// `offset` must be a valid offset into `hypercall_page`
pub unsafe fn hypercall4(
    offset: u32,
    arg0: u64,
    arg1: u64,
    arg2: u64,
    arg3: u64,
) -> Result<u64, Error> {
    let mut res: c_long;
    let entry = hypercall_page.as_ptr().offset(offset as isize);

    asm!("call {}",in(reg) entry,

    inout("rdi") arg0 => _,
    inout("rsi") arg1 => _,
    inout("rdx") arg2 => _,
    inout("r10") arg3 => _,

    // all caller-saved registers must be marked as clobberred
    out("rax") res, out("rcx") _,
    out("r8") _, out("r9") _,  out("r11") _,
    out("xmm0") _, out("xmm1") _, out("xmm2") _, out("xmm3") _,
    out("xmm4") _, out("xmm5") _, out("xmm6") _, out("xmm7") _,
    out("xmm8") _, out("xmm9") _, out("xmm10") _, out("xmm11") _,
    out("xmm12") _, out("xmm13") _, out("xmm14") _, out("xmm15") _);

    errno_to_result(res)
}

/// Makes hypercall with 5 arguments
///
/// # Safety
///
/// `offset` must be a valid offset into `hypercall_page`
pub unsafe fn hypercall5(
    offset: u32,
    arg0: u64,
    arg1: u64,
    arg2: u64,
    arg3: u64,
    arg4: u64,
) -> Result<u64, Error> {
    let mut res: c_long;
    let entry = hypercall_page.as_ptr().offset(offset as isize);

    asm!("call {}",in(reg) entry,

    inout("rdi") arg0 => _,
    inout("rsi") arg1 => _,
    inout("rdx") arg2 => _,
    inout("r10") arg3 => _,
    inout("r9") arg4 => _,

    // all caller-saved registers must be marked as clobberred
    out("rax") res, out("rcx") _,
    out("r8") _, out("r11") _,
    out("xmm0") _, out("xmm1") _, out("xmm2") _, out("xmm3") _,
    out("xmm4") _, out("xmm5") _, out("xmm6") _, out("xmm7") _,
    out("xmm8") _, out("xmm9") _, out("xmm10") _, out("xmm11") _,
    out("xmm12") _, out("xmm13") _, out("xmm14") _, out("xmm15") _);

    errno_to_result(res)
}