-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rust wrappers required by external dependencies
The libcrt implements the libc interface required to build SVSM external dependencies, but part of it is backed by SVSM core functions implemented in Rust. This patch adds wrappers for malloc(), calloc(), realloc(), free(), abort() and serial_out(). The latter is not part of the libc standard, but the libcrt uses it as a backend for printf. The "-Wl,-u,malloc" is used in the Makefile to prevent the linker from removing the wrapper.rs symbols. Co-developed-by: Vikram Narayanan <vikram186@gmail.com> Signed-off-by: Vikram Narayanan <vikram186@gmail.com> Signed-off-by: Claudio Carvalho <cclaudio@linux.ibm.com>
- Loading branch information
Showing
7 changed files
with
152 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* SPDX-License-Identifier: MIT */ | ||
/* | ||
* Copyright (C) 2023 IBM | ||
* | ||
* Authors: | ||
* Claudio Carvalho <cclaudio@ibm.com> | ||
* Vikram Narayanan <vikram186@gmail.com> | ||
*/ | ||
|
||
#![allow(non_camel_case_types)] | ||
|
||
use crate::cpu::vc_terminate_svsm_general; | ||
use crate::mem::{mem_allocate, mem_callocate, mem_free, mem_reallocate}; | ||
use crate::prints; | ||
|
||
use core::{ptr, slice, str}; | ||
use x86_64::VirtAddr; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn malloc(size: cty::c_ulong) -> *mut cty::c_void { | ||
if let Ok(va) = mem_allocate(size as usize) { | ||
return va.as_mut_ptr(); | ||
}; | ||
ptr::null_mut() | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn calloc(items: cty::c_ulong, size: cty::c_ulong) -> *mut cty::c_void { | ||
if let Some(num_bytes) = items.checked_mul(size as u64) { | ||
if let Ok(va) = mem_callocate(num_bytes as usize) { | ||
return va.as_mut_ptr(); | ||
} | ||
} | ||
ptr::null_mut() | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn realloc(p: *mut cty::c_void, size: cty::c_ulong) -> *mut cty::c_void { | ||
if let Ok(va) = mem_reallocate(VirtAddr::new(p as u64), size as usize) { | ||
return va.as_mut_ptr(); | ||
} | ||
ptr::null_mut() | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn free(p: *mut cty::c_void) { | ||
if p.is_null() { | ||
return; | ||
} | ||
mem_free(VirtAddr::new(p as u64)); | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn serial_out(s: *const cty::c_char, size: cty::c_int) { | ||
let str_slice: &[u8] = unsafe { slice::from_raw_parts(s as *const u8, size as usize) }; | ||
if let Ok(rust_str) = str::from_utf8(str_slice) { | ||
prints!("{}", rust_str); | ||
} else { | ||
prints!("ERR: BUG: serial_out arg1 is not a valid utf8 string\n"); | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn abort() -> ! { | ||
vc_terminate_svsm_general(); | ||
} |