microsoft/openvmm

Public

mirrored fromhttps://github.com/microsoft/openvmmAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e8c4f2e8ffc1914ac7dab5e566370609f1f37cd7

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

openhcl/minimal_rt/src/rt.rs

66lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Architecture-independent runtime support.
5
6#[cfg(minimal_rt)]
7mod instead_of_builtins {
8 /// Implementation cribbed from compiler_builtins.
9 #[inline(always)]
10 unsafe fn copy_backward_bytes(mut dest: *mut u8, mut src: *const u8, n: usize) {
11 // SAFETY: The caller guarantees that the pointers and length are correct.
12 unsafe {
13 let dest_start = dest.sub(n);
14 while dest_start < dest {
15 dest = dest.sub(1);
16 src = src.sub(1);
17 *dest = *src;
18 }
19 }
20 }
21
22 unsafe extern "C" {
23 fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
24 }
25
26 /// Implementation cribbed from compiler_builtins.
27 #[unsafe(no_mangle)]
28 unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
29 let delta = (dest as usize).wrapping_sub(src as usize);
30 if delta >= n {
31 // SAFETY: We can copy forwards because either dest is far enough ahead of src,
32 // or src is ahead of dest (and delta overflowed).
33 unsafe {
34 memcpy(dest, src, n);
35 }
36 } else {
37 // SAFETY: dest and src must be copied backward due to src and dest.
38 unsafe {
39 let dest = dest.add(n);
40 let src = src.add(n);
41 copy_backward_bytes(dest, src, n);
42 }
43 }
44 dest
45 }
46
47 /// This implementation is cribbed from compiler_builtins. It would be nice to
48 /// use those implementation for all the above functions, but those require
49 /// nightly as these are not yet stabilized.
50 #[unsafe(no_mangle)]
51 unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
52 // SAFETY: The caller guarantees that the pointers and length are correct.
53 unsafe {
54 let mut i = 0;
55 while i < n {
56 let a = *s1.add(i);
57 let b = *s2.add(i);
58 if a != b {
59 return a as i32 - b as i32;
60 }
61 i += 1;
62 }
63 0
64 }
65 }
66}
67