-
Notifications
You must be signed in to change notification settings - Fork 44
/
guestmem.rs
223 lines (192 loc) · 4.95 KB
/
guestmem.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
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright (c) 2022-2023 SUSE LLC
//
// Author: Joerg Roedel <[email protected]>
use crate::address::{Address, VirtAddr};
use crate::error::SvsmError;
use core::arch::asm;
use core::mem::{size_of, MaybeUninit};
#[allow(dead_code)]
#[inline]
pub fn read_u8(v: VirtAddr) -> Result<u8, SvsmError> {
let mut rcx: u64;
let mut val: u64;
unsafe {
asm!("1: movb ({0}), %al",
" xorq %rcx, %rcx",
"2:",
".pushsection \"__exception_table\",\"a\"",
".balign 16",
".quad (1b)",
".quad (2b)",
".popsection",
in(reg) v.bits(),
out("rax") val,
out("rcx") rcx,
options(att_syntax, nostack));
}
let ret: u8 = (val & 0xff) as u8;
if rcx == 0 {
Ok(ret)
} else {
Err(SvsmError::InvalidAddress)
}
}
#[allow(dead_code)]
#[inline]
pub fn write_u8(v: VirtAddr, val: u8) -> Result<(), SvsmError> {
let mut rcx: u64;
unsafe {
asm!("1: movb %al, ({0})",
" xorq %rcx, %rcx",
"2:",
".pushsection \"__exception_table\",\"a\"",
".balign 16",
".quad (1b)",
".quad (2b)",
".popsection",
in(reg) v.bits(),
in("rax") val as u64,
out("rcx") rcx,
options(att_syntax, nostack));
}
if rcx == 0 {
Ok(())
} else {
Err(SvsmError::InvalidAddress)
}
}
#[allow(dead_code)]
#[inline]
unsafe fn read_u16(v: VirtAddr) -> Result<u16, SvsmError> {
let mut rcx: u64;
let mut val: u64;
asm!("1: movw ({0}), {1}",
" xorq %rcx, %rcx",
"2:",
".pushsection \"__exception_table\",\"a\"",
".balign 16",
".quad (1b)",
".quad (2b)",
".popsection",
in(reg) v.bits(),
out(reg) val,
out("rcx") rcx,
options(att_syntax, nostack));
let ret: u16 = (val & 0xffff) as u16;
if rcx == 0 {
Ok(ret)
} else {
Err(SvsmError::InvalidAddress)
}
}
#[allow(dead_code)]
#[inline]
unsafe fn read_u32(v: VirtAddr) -> Result<u32, SvsmError> {
let mut rcx: u64;
let mut val: u64;
asm!("1: movl ({0}), {1}",
" xorq %rcx, %rcx",
"2:",
".pushsection \"__exception_table\",\"a\"",
".balign 16",
".quad (1b)",
".quad (2b)",
".popsection",
in(reg) v.bits(),
out(reg) val,
out("rcx") rcx,
options(att_syntax, nostack));
let ret: u32 = (val & 0xffffffff) as u32;
if rcx == 0 {
Ok(ret)
} else {
Err(SvsmError::InvalidAddress)
}
}
#[allow(dead_code)]
#[inline]
unsafe fn read_u64(v: VirtAddr) -> Result<u64, SvsmError> {
let mut rcx: u64;
let mut val: u64;
asm!("1: movq ({0}), {1}",
" xorq %rcx, %rcx",
"2:",
".pushsection \"__exception_table\",\"a\"",
".balign 16",
".quad (1b)",
".quad (2b)",
".popsection",
in(reg) v.bits(),
out(reg) val,
out("rcx") rcx,
options(att_syntax, nostack));
if rcx == 0 {
Ok(val)
} else {
Err(SvsmError::InvalidAddress)
}
}
#[inline]
unsafe fn do_movsb<T>(src: *const T, dst: *mut T) -> Result<(), SvsmError> {
let size: usize = size_of::<T>();
let mut rcx: u64;
asm!("1:cld
rep movsb
2:
.pushsection \"__exception_table\",\"a\"
.balign 16
.quad (1b)
.quad (2b)
.popsection",
inout("rsi") src => _,
inout("rdi") dst => _,
inout("rcx") size => rcx,
options(att_syntax, nostack));
if rcx == 0 {
Ok(())
} else {
Err(SvsmError::InvalidAddress)
}
}
pub struct GuestPtr<T>
where
T: Sized + Copy,
{
ptr: *mut T,
}
impl<T: Sized + Copy> GuestPtr<T> {
pub fn new(v: VirtAddr) -> Self {
Self {
ptr: v.as_mut_ptr::<T>(),
}
}
pub fn from_ptr(p: *mut T) -> Self {
Self { ptr: p }
}
pub fn read(&self) -> Result<T, SvsmError> {
let mut buf = MaybeUninit::<T>::uninit();
unsafe {
do_movsb(self.ptr, buf.as_mut_ptr())?;
Ok(buf.assume_init())
}
}
pub fn write(&self, buf: T) -> Result<(), SvsmError> {
let src = &buf as *const T;
unsafe { do_movsb(src, self.ptr) }
}
pub fn write_ref(&self, buf: &T) -> Result<(), SvsmError> {
let src = buf as *const T;
unsafe { do_movsb(src, self.ptr) }
}
pub fn cast<N: Sized + Copy>(&self) -> GuestPtr<N>
where
N: Sized + Copy,
{
GuestPtr::<N>::from_ptr(self.ptr.cast::<N>())
}
pub fn offset(&self, count: isize) -> Self {
unsafe { GuestPtr::from_ptr(self.ptr.offset(count)) }
}
}