hv1_hypercall/
aarch64.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! ARM64 hypercall support.

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

/// Provides access to the ARM64 register state needed to parse hypercalls.
pub trait Arm64RegisterState {
    /// Gets the program counter.
    fn pc(&mut self) -> u64;
    /// Sets the program counter.
    fn set_pc(&mut self, pc: u64);
    /// Gets register Xn, `n <= 17`.
    fn x(&mut self, n: u8) -> u64;
    /// Sets register Xn, `n <= 17`.
    fn set_x(&mut self, n: u8, v: u64);
}

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

    fn set_pc(&mut self, pc: u64) {
        (**self).set_pc(pc)
    }

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

    fn set_x(&mut self, n: u8, v: u64) {
        (**self).set_x(n, v)
    }
}

/// An implementation of [`HypercallIo`] on top of [`Arm64RegisterState`].
pub struct Arm64RegisterIo<T> {
    inner: T,
    pre_advanced: bool,
    smccc: bool,
}

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

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

impl<T: Arm64RegisterState> Arm64RegisterIo<T> {
    /// Returns a new instance.
    ///
    /// If `pre_advanced`, the PC has already been advanced (which the ARM
    /// processor does automatically when the HVC instruction is executed).
    ///
    /// If `smccc`, this is the SMCCC calling convention (hvc #0). Otherwise, it
    /// is the Hyper-V calling convention (hvc #1).
    pub fn new(t: T, pre_advanced: bool, smccc: bool) -> Self {
        Self {
            inner: t,
            pre_advanced,
            smccc,
        }
    }

    fn set_control(&mut self, control: u64) {
        // X0 for Hyper-V, X1 for SMCCC.
        self.inner.set_x(self.smccc as u8, control);
    }
}

impl<T: Arm64RegisterState> HypercallIo for Arm64RegisterIo<T> {
    fn advance_ip(&mut self) {
        if !self.pre_advanced {
            let pc = self.inner.pc().wrapping_add(4);
            self.inner.set_pc(pc);
        }
    }

    fn retry(&mut self, control: u64) {
        self.set_control(control);
        if self.pre_advanced {
            let pc = self.inner.pc().wrapping_sub(4);
            self.inner.set_pc(pc);
        }
    }

    fn control(&mut self) -> u64 {
        // X0 for Hyper-V, X1 for SMCCC.
        self.inner.x(self.smccc as u8)
    }

    fn input_gpa(&mut self) -> u64 {
        // X1 for Hyper-V, X2 for SMCCC.
        self.inner.x(1 + self.smccc as u8)
    }

    fn output_gpa(&mut self) -> u64 {
        // X2 for Hyper-V, X3 for SMCCC.
        self.inner.x(2 + self.smccc as u8)
    }

    fn fast_register_pair_count(&mut self) -> usize {
        8
    }

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

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

        if self.smccc {
            // SMCCC: start output after the input registers.
            buf.len()
        } else {
            // Hyper-V: use the last n registers for output.
            self.fast_register_pair_count() - output_register_pairs
        }
    }

    fn fast_output(&mut self, starting_pair_index: usize, buf: &[[u64; 2]]) {
        // X1-X16 for Hyper-V, X2-X17 for SMCCC.
        let start = starting_pair_index * 2 + 1 + self.smccc as usize;

        for (i, &[low, high]) in buf.iter().enumerate() {
            self.inner.set_x((start + i * 2) as u8, low);
            self.inner.set_x((start + i * 2 + 1) as u8, high);
        }
    }

    fn vtl_input(&mut self) -> u64 {
        0
    }

    fn set_result(&mut self, n: u64) {
        // Always X0.
        self.inner.set_x(0, n)
    }

    fn fast_regs(&mut self, starting_pair_index: usize, buf: &mut [[u64; 2]]) {
        // X1-X16 for Hyper-V, X2-X17 for SMCCC.
        let start = starting_pair_index * 2 + 1 + self.smccc as usize;
        for (i, [low, high]) in buf.iter_mut().enumerate() {
            *low = self.inner.x((start + i * 2) as u8);
            *high = self.inner.x((start + i * 2 + 1) as u8);
        }
    }
}

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

    /// Test hypercall IO for ARM64.
    impl<T: Arm64RegisterState + TestRegisterState> TestHypercallIo for Arm64RegisterIo<T> {
        fn get_result(&mut self) -> u64 {
            // Always X0.
            self.inner.x(0)
        }

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

        fn set_input_gpa(&mut self, gpa: u64) {
            // X1 for Hyper-V, X2 for SMCCC.
            self.inner.set_x(1 + self.smccc as u8, gpa)
        }

        fn set_output_gpa(&mut self, gpa: u64) {
            // X2 for Hyper-V, X3 for SMCCC.
            self.inner.set_x(2 + self.smccc as u8, gpa);
        }

        fn set_fast_input(&mut self, buf: &[[u64; 2]]) {
            // X1-X16 for Hyper-V, X2-X17 for SMCCC.
            for (i, [low, high]) in buf.iter().enumerate() {
                self.inner.set_x(i as u8 * 2 + 1 + self.smccc as u8, *low);
                self.inner.set_x(i as u8 * 2 + 2 + self.smccc as u8, *high);
            }
        }

        fn get_fast_output(&mut self, input_register_pairs: usize, buf: &mut [[u64; 2]]) {
            let start = if self.smccc {
                // SMCCC: start after the input registers.
                2 + input_register_pairs * 2
            } else {
                // Hyper-V: use the last n registers, ending with X16.
                17 - buf.len() * 2
            };
            for (i, [low, high]) in buf.iter_mut().enumerate() {
                *low = self.inner.x((start + i * 2) as u8);
                *high = self.inner.x((start + i * 2 + 1) as u8);
            }
        }

        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 {
            // X0 is always output, control is either X0 (for Hyper-V) or X1 (for SMCCC).
            1u64 << (self.smccc as u8) | 1
        }

        fn get_name(&self) -> String {
            format!(
                "Arm64RegisterIo<pre_advanced={}, smccc={}>",
                self.pre_advanced, self.smccc
            )
        }

        fn set_vtl_input(&mut self, vtl_input: u64) {
            // No VTL input for ARM64.
            assert_eq!(vtl_input, 0);
        }

        fn auto_advance_ip(&mut self) {
            if self.pre_advanced {
                let pc = self.inner.pc().wrapping_add(4);
                self.inner.set_pc(pc);
            }
        }
    }
}