hv1_hypercall/
x86.rs

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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! X86-64 hypercall support.

use super::HypercallIo;
use crate::support::AsHandler;

/// An implementation of [`HypercallIo`] on X64 register state.
pub struct X64RegisterIo<T> {
    inner: T,
    is_64bit: bool,
}

impl<T: X64RegisterState> X64RegisterIo<T> {
    /// Returns a register accessor backed by `t`.
    ///
    /// Uses the 64-bit calling convention if `is_64bit`, otherwise the 32-bit
    /// one.
    pub fn new(t: T, is_64bit: bool) -> Self {
        Self { inner: t, is_64bit }
    }

    fn gp_pair(&mut self, high: X64HypercallRegister, low: X64HypercallRegister) -> u64 {
        (self.inner.gp(high) << 32) | (self.inner.gp(low) & 0xffff_ffff)
    }

    fn mask(&self, value: u64) -> u64 {
        value
            & if self.is_64bit {
                u64::MAX
            } else {
                u32::MAX as u64
            }
    }

    fn set_control(&mut self, control: u64) {
        if self.is_64bit {
            self.inner.set_gp(X64HypercallRegister::Rcx, control);
        } else {
            self.inner.set_gp(X64HypercallRegister::Rdx, control >> 32);
            self.inner
                .set_gp(X64HypercallRegister::Rax, control & u32::MAX as u64);
        }
    }
}

impl<T> AsHandler<T> for X64RegisterIo<T> {
    fn as_handler(&mut self) -> &mut T {
        &mut self.inner
    }
}

impl<T> AsHandler<T> for X64RegisterIo<&mut T> {
    fn as_handler(&mut self) -> &mut T {
        &mut *self.inner
    }
}

impl<T: X64RegisterState> HypercallIo for X64RegisterIo<T> {
    fn advance_ip(&mut self) {
        let rip = self.inner.rip().wrapping_add(3);
        self.inner.set_rip(self.mask(rip));
    }

    fn retry(&mut self, control: u64) {
        // Update the input control.
        self.set_control(control)

        // rip is still at the vmcall/vmmcall instruction, nothing to do.
    }

    fn control(&mut self) -> u64 {
        if self.is_64bit {
            self.inner.gp(X64HypercallRegister::Rcx)
        } else {
            self.gp_pair(X64HypercallRegister::Rdx, X64HypercallRegister::Rax)
        }
    }

    fn vtl_input(&mut self) -> u64 {
        let name = if self.is_64bit {
            X64HypercallRegister::Rax
        } else {
            X64HypercallRegister::Rcx
        };

        let value = self.inner.gp(name);
        self.mask(value)
    }

    fn set_result(&mut self, n: u64) {
        if self.is_64bit {
            self.inner.set_gp(X64HypercallRegister::Rax, n);
        } else {
            self.inner.set_gp(X64HypercallRegister::Rdx, n >> 32);
            self.inner
                .set_gp(X64HypercallRegister::Rax, n & u32::MAX as u64);
        }
    }

    fn input_gpa(&mut self) -> u64 {
        if self.is_64bit {
            self.inner.gp(X64HypercallRegister::Rdx)
        } else {
            self.gp_pair(X64HypercallRegister::Rbx, X64HypercallRegister::Rcx)
        }
    }

    fn output_gpa(&mut self) -> u64 {
        if self.is_64bit {
            self.inner.gp(X64HypercallRegister::R8)
        } else {
            self.gp_pair(X64HypercallRegister::Rdi, X64HypercallRegister::Rsi)
        }
    }

    fn fast_register_pair_count(&mut self) -> usize {
        if self.is_64bit { 7 } else { 1 }
    }

    fn extended_fast_hypercalls_ok(&mut self) -> bool {
        self.is_64bit
    }

    fn fast_input(&mut self, buf: &mut [[u64; 2]], _output_register_pairs: usize) -> usize {
        self.fast_regs(0, buf);
        buf.len()
    }

    fn fast_output(&mut self, starting_pair_index: usize, buf: &[[u64; 2]]) {
        // Continue after the input registers.
        for (i, &[low, high]) in buf.iter().enumerate() {
            let index = i + starting_pair_index;
            if index == 0 {
                self.inner.set_gp(X64HypercallRegister::Rdx, low);
                self.inner.set_gp(X64HypercallRegister::R8, high);
            } else {
                let x = low as u128 | ((high as u128) << 64);
                self.inner.set_xmm(index - 1, x)
            }
        }
    }

    fn fast_regs(&mut self, starting_pair_index: usize, buf: &mut [[u64; 2]]) {
        if self.is_64bit {
            for (i, [low, high]) in buf.iter_mut().enumerate() {
                let index = i + starting_pair_index;
                if index == 0 {
                    *low = self.inner.gp(X64HypercallRegister::Rdx);
                    *high = self.inner.gp(X64HypercallRegister::R8);
                } else {
                    let value = self.inner.xmm(index - 1);
                    *low = value as u64;
                    *high = (value >> 64) as u64;
                }
            }
        } else if let [[low, high], ..] = buf {
            *low = self.gp_pair(X64HypercallRegister::Rbx, X64HypercallRegister::Rcx);
            *high = self.gp_pair(X64HypercallRegister::Rdi, X64HypercallRegister::Rsi);
        }
    }
}

/// Register state access for x86/x64.
pub trait X64RegisterState {
    /// RIP register.
    fn rip(&mut self) -> u64;

    /// Sets the RIP register.
    fn set_rip(&mut self, rip: u64);

    /// Gets a general purpose register.
    fn gp(&mut self, n: X64HypercallRegister) -> u64;

    /// Sets a general purpose register.
    fn set_gp(&mut self, n: X64HypercallRegister, value: u64);

    /// Gets an XMM register, `n` in `0..5`.
    fn xmm(&mut self, n: usize) -> u128;

    /// Sets an XMM register, `n` in `0..5`.
    fn set_xmm(&mut self, n: usize, value: u128);
}

impl<T: X64RegisterState> X64RegisterState for &'_ mut T {
    fn rip(&mut self) -> u64 {
        (**self).rip()
    }

    fn set_rip(&mut self, rip: u64) {
        (**self).set_rip(rip)
    }

    fn gp(&mut self, n: X64HypercallRegister) -> u64 {
        (**self).gp(n)
    }

    fn set_gp(&mut self, n: X64HypercallRegister, value: u64) {
        (**self).set_gp(n, value)
    }

    fn xmm(&mut self, n: usize) -> u128 {
        (**self).xmm(n)
    }

    fn set_xmm(&mut self, n: usize, value: u128) {
        (**self).set_xmm(n, value)
    }
}

/// An x64 GP register. This just contains the subset used in the hypercall ABI.
pub enum X64HypercallRegister {
    /// RAX
    Rax,
    /// RCX
    Rcx,
    /// RDX
    Rdx,
    /// RBX
    Rbx,
    /// RSI
    Rsi,
    /// RDI
    Rdi,
    /// R8
    R8,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tests::TestHypercallIo;
    use crate::tests::TestRegisterState;

    /// Test hypercall IO for x86.
    impl<T: X64RegisterState + TestRegisterState> TestHypercallIo for X64RegisterIo<T> {
        fn get_result(&mut self) -> u64 {
            if self.is_64bit {
                self.inner.gp(X64HypercallRegister::Rax)
            } else {
                self.gp_pair(X64HypercallRegister::Rdx, X64HypercallRegister::Rax)
            }
        }

        fn set_control(&mut self, control: u64) {
            X64RegisterIo::set_control(self, control);
        }

        fn set_input_gpa(&mut self, gpa: u64) {
            if self.is_64bit {
                self.inner.set_gp(X64HypercallRegister::Rdx, gpa);
            } else {
                self.inner.set_gp(X64HypercallRegister::Rbx, gpa >> 32);
                self.inner
                    .set_gp(X64HypercallRegister::Rcx, gpa & u32::MAX as u64);
            }
        }

        fn set_output_gpa(&mut self, gpa: u64) {
            if self.is_64bit {
                self.inner.set_gp(X64HypercallRegister::R8, gpa);
            } else {
                self.inner.set_gp(X64HypercallRegister::Rdi, gpa >> 32);
                self.inner
                    .set_gp(X64HypercallRegister::Rsi, gpa & u32::MAX as u64);
            }
        }

        fn set_fast_input(&mut self, buf: &[[u64; 2]]) {
            if self.is_64bit {
                let (gp, xmm) = buf.split_at(1);
                let rdx = gp[0][0];
                let r8 = gp[0][1];
                self.inner.set_gp(X64HypercallRegister::Rdx, rdx);
                self.inner.set_gp(X64HypercallRegister::R8, r8);
                for (i, [low, high]) in xmm.iter().enumerate() {
                    let value = *low as u128 | ((*high as u128) << 64);
                    self.inner.set_xmm(i, value);
                }
            } else {
                let [low, high] = buf[0];
                self.inner.set_gp(X64HypercallRegister::Rbx, low >> 32);
                self.inner
                    .set_gp(X64HypercallRegister::Rcx, low & u32::MAX as u64);
                self.inner.set_gp(X64HypercallRegister::Rdi, high >> 32);
                self.inner
                    .set_gp(X64HypercallRegister::Rsi, high & u32::MAX as u64);
            }
        }

        fn get_fast_output(&mut self, input_register_pairs: usize, buf: &mut [[u64; 2]]) {
            // Continue after the input registers.
            for (i, [low, high]) in buf.iter_mut().enumerate() {
                if i + input_register_pairs == 0 {
                    *low = self.inner.gp(X64HypercallRegister::Rdx);
                    *high = self.inner.gp(X64HypercallRegister::R8);
                } else {
                    let x = self.inner.xmm(i + input_register_pairs - 1);
                    *low = x as u64;
                    *high = (x >> 64) as u64;
                }
            }
        }

        fn get_modified_mask(&self) -> u64 {
            self.inner.get_modified_mask()
        }

        fn clear_modified_mask(&mut self) {
            self.inner.clear_modified_mask()
        }

        fn get_io_register_mask(&self) -> u64 {
            if self.is_64bit {
                1u64 << X64HypercallRegister::Rcx as usize
                    | 1u64 << X64HypercallRegister::Rax as usize
            } else {
                1u64 << X64HypercallRegister::Rdx as usize
                    | 1u64 << X64HypercallRegister::Rax as usize
            }
        }

        fn get_name(&self) -> String {
            format!("x86_{}", if self.is_64bit { "64" } else { "32" })
        }

        fn set_vtl_input(&mut self, vtl_input: u64) {
            if self.is_64bit {
                self.inner.set_gp(X64HypercallRegister::Rax, vtl_input);
            } else {
                self.inner
                    .set_gp(X64HypercallRegister::Rcx, vtl_input >> 32);
            }
        }

        fn auto_advance_ip(&mut self) {}
    }
}