-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathlib.rs
More file actions
561 lines (488 loc) · 19 KB
/
lib.rs
File metadata and controls
561 lines (488 loc) · 19 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//! A [LiteBox platform](../litebox/platform/index.html) for running LiteBox in kernel mode
#![cfg(target_arch = "x86_64")]
#![no_std]
use core::sync::atomic::AtomicU64;
use core::{arch::asm, sync::atomic::AtomicU32};
use litebox::mm::linux::PageRange;
use litebox::platform::RawPointerProvider;
use litebox::platform::page_mgmt::FixedAddressBehavior;
use litebox::platform::{
DebugLogProvider, IPInterfaceProvider, ImmediatelyWokenUp, PageManagementProvider, Provider,
Punchthrough, PunchthroughProvider, PunchthroughToken, RawMutexProvider, TimeProvider,
UnblockedOrTimedOut,
};
use litebox_common_linux::PunchthroughSyscall;
use litebox_common_linux::errno::Errno;
extern crate alloc;
pub mod arch;
pub mod host;
pub mod mm;
static CPU_MHZ: AtomicU64 = AtomicU64::new(0);
pub fn update_cpu_mhz(freq: u64) {
CPU_MHZ.store(freq, core::sync::atomic::Ordering::Relaxed);
}
/// This is the platform for running LiteBox in kernel mode.
/// It requires a host that implements the [`HostInterface`] trait.
pub struct LinuxKernel<Host: HostInterface> {
// Invariant in `Host`: <https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns>
host_and_task: core::marker::PhantomData<fn(Host) -> Host>,
page_table: mm::PageTable<4096>,
/// The system time captured at boot, used together with [`boot_instant`](Self::boot_instant)
/// to derive the current system time from the monotonic clock.
boot_system_time: core::time::Duration,
/// The monotonic instant captured at boot.
boot_instant: Instant,
}
impl<Host: HostInterface> core::fmt::Debug for LinuxKernel<Host> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct(&alloc::format!(
"LinuxKernel<{}>",
core::any::type_name::<Host>()
))
.finish_non_exhaustive()
}
}
pub struct LinuxPunchthroughToken<'a, Host: HostInterface> {
punchthrough: PunchthroughSyscall<'a, LinuxKernel<Host>>,
host: core::marker::PhantomData<Host>,
}
impl<'a, Host: HostInterface> PunchthroughToken for LinuxPunchthroughToken<'a, Host> {
type Punchthrough = PunchthroughSyscall<'a, LinuxKernel<Host>>;
fn execute(
self,
) -> Result<
<Self::Punchthrough as Punchthrough>::ReturnSuccess,
litebox::platform::PunchthroughError<<Self::Punchthrough as Punchthrough>::ReturnFailure>,
> {
let r = match self.punchthrough {
PunchthroughSyscall::SetFsBase { addr } => {
unsafe { litebox_common_linux::wrfsbase(addr) };
Ok(0)
}
PunchthroughSyscall::GetFsBase => Ok(unsafe { litebox_common_linux::rdfsbase() }),
};
match r {
Ok(v) => Ok(v),
Err(e) => Err(litebox::platform::PunchthroughError::Failure(e)),
}
}
}
impl<Host: HostInterface> Provider for LinuxKernel<Host> {}
// TODO: implement pointer validation to ensure the pointers are in user space.
type UserConstPtr<T> = litebox::platform::common_providers::userspace_pointers::UserConstPtr<
litebox::platform::common_providers::userspace_pointers::NoValidation,
T,
>;
type UserMutPtr<T> = litebox::platform::common_providers::userspace_pointers::UserMutPtr<
litebox::platform::common_providers::userspace_pointers::NoValidation,
T,
>;
impl<Host: HostInterface> RawPointerProvider for LinuxKernel<Host> {
type RawConstPointer<T: zerocopy::FromBytes> = UserConstPtr<T>;
type RawMutPointer<T: zerocopy::FromBytes + zerocopy::IntoBytes> = UserMutPtr<T>;
}
impl<Host: HostInterface> PunchthroughProvider for LinuxKernel<Host> {
type PunchthroughToken<'a> = LinuxPunchthroughToken<'a, Host>;
fn get_punchthrough_token_for<'a>(
&self,
punchthrough: <Self::PunchthroughToken<'a> as PunchthroughToken>::Punchthrough,
) -> Option<Self::PunchthroughToken<'a>> {
Some(LinuxPunchthroughToken {
punchthrough,
host: core::marker::PhantomData,
})
}
}
impl<Host: HostInterface> LinuxKernel<Host> {
pub fn new(init_page_table_addr: x86_64::PhysAddr) -> &'static Self {
// Capture the initial system time and monotonic instant so that
// subsequent `current_time` calls can be derived from the monotonic
// clock without additional host calls.
let boot_system_time = Host::current_system_time();
let boot_instant = Instant::now();
// There is only one long-running platform ever expected, thus this leak is perfectly ok in
// order to simplify usage of the platform.
alloc::boxed::Box::leak(alloc::boxed::Box::new(Self {
host_and_task: core::marker::PhantomData,
// TODO: Update the init physaddr
page_table: unsafe { mm::PageTable::new(init_page_table_addr) },
boot_system_time,
boot_instant,
}))
}
pub fn terminate(&self, reason_set: u64, reason_code: u64) -> ! {
Host::terminate(reason_set, reason_code)
}
}
impl<Host: HostInterface> RawMutexProvider for LinuxKernel<Host> {
type RawMutex = RawMutex<Host>;
}
/// An implementation of [`litebox::platform::RawMutex`]
pub struct RawMutex<Host: HostInterface> {
inner: AtomicU32,
host: core::marker::PhantomData<fn(Host) -> Host>,
}
unsafe impl<Host: HostInterface> Send for RawMutex<Host> {}
unsafe impl<Host: HostInterface> Sync for RawMutex<Host> {}
impl<Host: HostInterface> litebox::platform::RawMutex for RawMutex<Host> {
const INIT: Self = Self::new();
fn underlying_atomic(&self) -> &core::sync::atomic::AtomicU32 {
&self.inner
}
fn wake_many(&self, n: usize) -> usize {
Host::wake_many(&self.inner, n).unwrap()
}
fn block(&self, val: u32) -> Result<(), ImmediatelyWokenUp> {
match self.block_or_maybe_timeout(val, None) {
Ok(UnblockedOrTimedOut::Unblocked) => Ok(()),
Ok(UnblockedOrTimedOut::TimedOut) => unreachable!(),
Err(ImmediatelyWokenUp) => Err(ImmediatelyWokenUp),
}
}
fn block_or_timeout(
&self,
val: u32,
time: core::time::Duration,
) -> Result<litebox::platform::UnblockedOrTimedOut, ImmediatelyWokenUp> {
self.block_or_maybe_timeout(val, Some(time))
}
}
impl<Host: HostInterface> RawMutex<Host> {
const fn new() -> Self {
Self {
inner: AtomicU32::new(0),
host: core::marker::PhantomData,
}
}
fn block_or_maybe_timeout(
&self,
val: u32,
timeout: Option<core::time::Duration>,
) -> Result<UnblockedOrTimedOut, ImmediatelyWokenUp> {
match Host::block_or_maybe_timeout(&self.inner, val, timeout) {
Ok(()) | Err(Errno::EINTR) => Ok(UnblockedOrTimedOut::Unblocked),
Err(Errno::EAGAIN) => {
// If the futex value does not match val, then the call fails
// immediately with the error EAGAIN.
Err(ImmediatelyWokenUp)
}
Err(Errno::ETIMEDOUT) => Ok(UnblockedOrTimedOut::TimedOut),
Err(e) => {
todo!("Error: {:?}", e);
}
}
}
}
impl<Host: HostInterface> DebugLogProvider for LinuxKernel<Host> {
fn debug_log_print(&self, msg: &str) {
Host::log(msg);
}
}
/// An implementation of [`litebox::platform::Instant`]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Instant(u64);
/// An implementation of [`litebox::platform::SystemTime`]
pub struct SystemTime {
inner: core::time::Duration,
}
impl<Host: HostInterface> TimeProvider for LinuxKernel<Host> {
type Instant = Instant;
type SystemTime = SystemTime;
fn now(&self) -> Self::Instant {
Instant::now()
}
fn current_time(&self) -> Self::SystemTime {
use litebox::platform::Instant as _;
// Derive the current system time from the monotonic clock elapsed
// since boot, avoiding repeated host calls.
//
// NOTE: Because the system time is only sampled once at boot and
// subsequent values are computed from the monotonic clock, the returned
// time will drift from the real system time if the host's clock
// is adjusted after boot (e.g. NTP step, manual set, leap-second).
let elapsed = Instant::now()
.checked_duration_since(&self.boot_instant)
.unwrap_or(core::time::Duration::ZERO);
SystemTime {
inner: self.boot_system_time + elapsed,
}
}
}
impl litebox::platform::Instant for Instant {
fn checked_duration_since(&self, earlier: &Self) -> Option<core::time::Duration> {
self.0.checked_sub(earlier.0).map(|v| {
core::time::Duration::from_micros(
v / CPU_MHZ.load(core::sync::atomic::Ordering::Relaxed),
)
})
}
fn checked_add(&self, duration: core::time::Duration) -> Option<Self> {
let duration_micros: u64 = duration.as_micros().try_into().ok()?;
Some(Instant(self.0.checked_add(
duration_micros.checked_mul(CPU_MHZ.load(core::sync::atomic::Ordering::Relaxed))?,
)?))
}
}
impl Instant {
fn rdtsc() -> u64 {
let lo: u32;
let hi: u32;
unsafe {
asm!(
"rdtsc",
out("eax") lo,
out("edx") hi,
);
}
(u64::from(hi) << 32) | u64::from(lo)
}
fn now() -> Self {
Instant(Self::rdtsc())
}
}
impl litebox::platform::SystemTime for SystemTime {
const UNIX_EPOCH: Self = SystemTime {
inner: core::time::Duration::ZERO,
};
fn duration_since(&self, earlier: &Self) -> Result<core::time::Duration, core::time::Duration> {
self.inner
.checked_sub(earlier.inner)
.ok_or_else(|| earlier.inner.checked_sub(self.inner).unwrap())
}
}
impl<Host: HostInterface> IPInterfaceProvider for LinuxKernel<Host> {
fn send_ip_packet(&self, packet: &[u8]) -> Result<(), litebox::platform::SendError> {
match Host::send_ip_packet(packet) {
Ok(n) => {
if n != packet.len() {
unimplemented!()
}
Ok(())
}
Err(e) => {
// Avoid allocation for error message
crate::print_str_and_int!(
"Error sending IP packet: ",
u64::from(e.as_neg().unsigned_abs()),
16
);
unimplemented!()
}
}
}
fn receive_ip_packet(
&self,
packet: &mut [u8],
) -> Result<usize, litebox::platform::ReceiveError> {
match Host::receive_ip_packet(packet) {
Ok(n) => Ok(n),
Err(Errno::EAGAIN) => Err(litebox::platform::ReceiveError::WouldBlock),
Err(e) => {
// Avoid allocation for error message
crate::print_str_and_int!(
"Error receiving IP packet: ",
u64::from(e.as_neg().unsigned_abs()),
16
);
unimplemented!()
}
}
}
}
impl<Host: HostInterface> litebox::platform::StdioProvider for LinuxKernel<Host> {
fn read_from_stdin(&self, buf: &mut [u8]) -> Result<usize, litebox::platform::StdioReadError> {
Host::read_from_stdin(buf).map_err(|err| match err {
Errno::EPIPE => litebox::platform::StdioReadError::Closed,
_ => panic!("unhandled error {err}"),
})
}
fn write_to(
&self,
stream: litebox::platform::StdioOutStream,
buf: &[u8],
) -> Result<usize, litebox::platform::StdioWriteError> {
Host::write_to(stream, buf).map_err(|err| match err {
Errno::EPIPE => litebox::platform::StdioWriteError::Closed,
_ => panic!("unhandled error {err}"),
})
}
fn is_a_tty(&self, _stream: litebox::platform::StdioStream) -> bool {
false
}
}
/// Platform-Host Interface
pub trait HostInterface: 'static {
/// Page allocation from host.
///
/// It can return more than requested size. On success, it returns the start address
/// and the size of the allocated memory.
fn alloc(layout: &core::alloc::Layout) -> Option<(usize, usize)>;
/// Returns the memory back to host.
///
/// Note host should know the size of allocated memory and needs to check the validity
/// of the given address.
///
/// # Safety
///
/// The caller must ensure that the `addr` is valid and was allocated by this [`Self::alloc`].
unsafe fn free(addr: usize);
/// Switch back to host
fn return_to_host() -> !;
/// Terminate LiteBox
fn terminate(reason_set: u64, reason_code: u64) -> !;
fn wake_many(mutex: &AtomicU32, n: usize) -> Result<usize, Errno>;
fn block_or_maybe_timeout(
mutex: &AtomicU32,
val: u32,
timeout: Option<core::time::Duration>,
) -> Result<(), Errno>;
/// Terminate the current process.
fn terminate_process(code: i32) -> !;
/// For Network
fn send_ip_packet(packet: &[u8]) -> Result<usize, Errno>;
fn receive_ip_packet(packet: &mut [u8]) -> Result<usize, Errno>;
// For Stdio
fn read_from_stdin(buf: &mut [u8]) -> Result<usize, Errno>;
fn write_to(stream: litebox::platform::StdioOutStream, buf: &[u8]) -> Result<usize, Errno>;
/// Returns the current system time as a [`Duration`](core::time::Duration) since the
/// UNIX epoch.
fn current_system_time() -> core::time::Duration;
/// For Debugging
fn log(msg: &str);
}
impl<Host: HostInterface, const ALIGN: usize> PageManagementProvider<ALIGN> for LinuxKernel<Host> {
const TASK_ADDR_MIN: usize = 0x1_0000; // default linux config
const TASK_ADDR_MAX: usize = 0x7FFF_FFFF_F000; // (1 << 47) - PAGE_SIZE;
fn allocate_pages(
&self,
suggested_range: core::ops::Range<usize>,
initial_permissions: litebox::platform::page_mgmt::MemoryRegionPermissions,
can_grow_down: bool,
populate_pages_immediately: bool,
_noreserve: bool,
fixed_address_behavior: FixedAddressBehavior,
) -> Result<Self::RawMutPointer<u8>, litebox::platform::page_mgmt::AllocationError> {
let range = PageRange::new(suggested_range.start, suggested_range.end)
.ok_or(litebox::platform::page_mgmt::AllocationError::Unaligned)?;
match fixed_address_behavior {
FixedAddressBehavior::Hint | FixedAddressBehavior::NoReplace => {}
FixedAddressBehavior::Replace => {
// Clear the existing mappings first.
unsafe { self.page_table.unmap_pages(range, true).unwrap() };
}
}
let flags = u32::from(initial_permissions.bits())
| if can_grow_down {
litebox::mm::linux::VmFlags::VM_GROWSDOWN.bits()
} else {
0
};
let flags = litebox::mm::linux::VmFlags::from_bits(flags).unwrap();
Ok(self
.page_table
.map_pages(range, flags, populate_pages_immediately))
}
unsafe fn deallocate_pages(
&self,
range: core::ops::Range<usize>,
) -> Result<(), litebox::platform::page_mgmt::DeallocationError> {
let range = PageRange::new(range.start, range.end)
.ok_or(litebox::platform::page_mgmt::DeallocationError::Unaligned)?;
unsafe { self.page_table.unmap_pages(range, true) }
}
unsafe fn remap_pages(
&self,
old_range: core::ops::Range<usize>,
new_range: core::ops::Range<usize>,
_permissions: litebox::platform::page_mgmt::MemoryRegionPermissions,
) -> Result<UserMutPtr<u8>, litebox::platform::page_mgmt::RemapError> {
let old_range = PageRange::new(old_range.start, old_range.end)
.ok_or(litebox::platform::page_mgmt::RemapError::Unaligned)?;
let new_range = PageRange::new(new_range.start, new_range.end)
.ok_or(litebox::platform::page_mgmt::RemapError::Unaligned)?;
if old_range.start.max(new_range.start) <= old_range.end.min(new_range.end) {
return Err(litebox::platform::page_mgmt::RemapError::Overlapping);
}
unsafe { self.page_table.remap_pages(old_range, new_range) }
}
unsafe fn update_permissions(
&self,
range: core::ops::Range<usize>,
new_permissions: litebox::platform::page_mgmt::MemoryRegionPermissions,
) -> Result<(), litebox::platform::page_mgmt::PermissionUpdateError> {
let range = PageRange::new(range.start, range.end)
.ok_or(litebox::platform::page_mgmt::PermissionUpdateError::Unaligned)?;
let new_flags =
litebox::mm::linux::VmFlags::from_bits(new_permissions.bits().into()).unwrap();
unsafe { self.page_table.mprotect_pages(range, new_flags) }
}
fn reserved_pages(&self) -> impl Iterator<Item = &core::ops::Range<usize>> {
core::iter::empty()
}
}
impl<Host: HostInterface> litebox::mm::linux::VmemPageFaultHandler for LinuxKernel<Host> {
unsafe fn handle_page_fault(
&self,
fault_addr: usize,
flags: litebox::mm::linux::VmFlags,
error_code: u64,
) -> Result<(), litebox::mm::linux::PageFaultError> {
unsafe {
self.page_table
.handle_page_fault(fault_addr, flags, error_code)
}
}
fn access_error(error_code: u64, flags: litebox::mm::linux::VmFlags) -> bool {
mm::PageTable::<4096>::access_error(error_code, flags)
}
}
impl<Host: HostInterface> litebox::platform::SystemInfoProvider for LinuxKernel<Host> {
fn get_syscall_entry_point(&self) -> usize {
// Currently this is only used in ELF loader to fix trampoline code.
// When running in kernel mode, we don't need a syscall trampoline.
0
}
fn get_vdso_address(&self) -> Option<usize> {
None
}
}
const RIP_OFFSET: usize = core::mem::offset_of!(litebox_common_linux::PtRegs, rip);
const EFLAGS_OFFSET: usize = core::mem::offset_of!(litebox_common_linux::PtRegs, eflags);
/// Switches to the guest context using sysretq.
///
/// # Safety
///
/// The context must be valid guest context.
unsafe fn switch_to_guest(ctx: &litebox_common_linux::PtRegs) -> ! {
unsafe {
core::arch::asm!(
"mov rsp, {0}",
"mov rcx, [rsp + {rip_off}]",
"mov r11, [rsp + {eflags_off}]",
"pop r15",
"pop r14",
"pop r13",
"pop r12",
"pop rbp",
"pop rbx",
"pop rsi", /* skip r11 */
"pop r10",
"pop r9",
"pop r8",
"pop rax",
"pop rsi", /* skip rcx */
"pop rdx",
"pop rsi",
"pop rdi",
"mov rsp, [rsp + 0x20]", /* original rsp */
"swapgs",
"sysretq",
in(reg) ctx,
rip_off = const RIP_OFFSET,
eflags_off = const EFLAGS_OFFSET,
options(noreturn),
);
}
}