microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
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)] |
| 7 | mod 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 | extern "C" { |
| 23 | fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8; |
| 24 | } |
| 25 | |
| 26 | /// Implementation cribbed from compiler_builtins. |
| 27 | #[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 | #[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 | |