Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit 5a43d0a

Browse files
committed
Implement free for vtpm-td
Signed-off-by: Min Xu <min.m.xu@intel.com>
1 parent 77b5f1e commit 5a43d0a

File tree

7 files changed

+83
-79
lines changed

7 files changed

+83
-79
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps/rust-tpm-20-ref/sh_script/build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function clean() {
1717
pushd tpm
1818
make clean
1919
popd
20+
21+
cargo clean
2022
}
2123

2224
function build() {

deps/rust-tpm-20-ref/smallc/src/stdlib/__need_impl_stdlib.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// TBD:
66
extern void *__fw_malloc(size_t n);
7+
extern void __fw_free(void *p);
78
extern uint32_t __fw_rdrand32(void);
89

910
void *malloc(size_t n)
@@ -13,7 +14,7 @@ void *malloc(size_t n)
1314

1415
void free(void *p)
1516
{
16-
return;
17+
__fw_free(p);
1718
}
1819

1920
void *realloc(void *p, size_t n)

deps/rust-tpm-20-ref/src/lib.rs

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,12 @@
1+
// Copyright (c) 2022 - 2023 Intel Corporation
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
15
#![no_std]
26
#![feature(naked_functions)]
37

48
extern crate alloc;
59
use core::arch::asm;
6-
#[no_mangle]
7-
pub extern "C" fn __fw_debug_msg(msg: *const u8, len: usize) {
8-
let msg = unsafe {
9-
let r = core::slice::from_raw_parts(msg, len);
10-
core::str::from_utf8_unchecked(r)
11-
};
12-
log::info!("{}", msg);
13-
}
14-
15-
#[no_mangle]
16-
pub extern "C" fn __fw_debug_buffer(buffer: *const u8, len: usize) {
17-
let buf = unsafe { core::slice::from_raw_parts(buffer, len) };
18-
log::info!("buffer {:x?}\n", buf);
19-
}
20-
21-
#[no_mangle]
22-
pub extern "C" fn __fw_abort() {
23-
panic!("abort called");
24-
}
25-
26-
#[no_mangle]
27-
pub extern "C" fn __fw_rdrand32() -> u32 {
28-
unsafe {
29-
let mut ret: u32 = 0;
30-
for _ in 0..10 {
31-
if core::arch::x86_64::_rdrand32_step(&mut ret) == 1 {
32-
return ret;
33-
}
34-
}
35-
panic!("Failed to obtain random data");
36-
}
37-
}
38-
39-
#[no_mangle]
40-
pub unsafe extern "C" fn __fw_malloc(s: usize) -> *mut u8 {
41-
use alloc::alloc::Layout;
42-
alloc::alloc::alloc(Layout::from_size_align(s, 1).unwrap())
43-
}
44-
4510
#[naked]
4611
#[no_mangle]
4712
pub unsafe extern "C" fn ___chkstk_ms() {

src/tpm/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ edition = "2021"
99
global = { path = "../global" }
1010
log = "0.4.13"
1111
rust-tpm-20-ref = { path = "../../deps/rust-tpm-20-ref" }
12+
spin = "0.9.2"
13+
14+
[dependencies.lazy_static]
15+
version = "1.0"
16+
features = ["spin_no_std"]

src/tpm/src/lib.rs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use self::tpm2_provision::tpm2_provision_ek;
2121
extern crate alloc;
2222

2323
pub mod cty;
24+
pub mod std_lib;
2425
pub mod tpm2_cmd_rsp;
2526
pub mod tpm2_digests;
2627
pub mod tpm2_pcr;
@@ -130,42 +131,3 @@ pub fn terminate_tpm() {
130131
}
131132
GLOBAL_TPM_DATA.lock().set_tpm_active(false);
132133
}
133-
134-
#[no_mangle]
135-
pub extern "C" fn __fw_debug_msg(msg: *const u8, len: usize) {
136-
let msg = unsafe {
137-
let r = core::slice::from_raw_parts(msg, len);
138-
core::str::from_utf8_unchecked(r)
139-
};
140-
log::info!("{}", msg);
141-
}
142-
143-
#[no_mangle]
144-
pub extern "C" fn __fw_debug_buffer(buffer: *const u8, len: usize) {
145-
let buf = unsafe { core::slice::from_raw_parts(buffer, len) };
146-
log::info!("buffer {:x?}\n", buf);
147-
}
148-
149-
#[no_mangle]
150-
pub extern "C" fn __fw_abort() {
151-
panic!("abort called");
152-
}
153-
154-
#[no_mangle]
155-
pub extern "C" fn __fw_rdrand32() -> u32 {
156-
unsafe {
157-
let mut ret: u32 = 0;
158-
for _ in 0..10 {
159-
if core::arch::x86_64::_rdrand32_step(&mut ret) == 1 {
160-
return ret;
161-
}
162-
}
163-
panic!("Failed to obtain random data");
164-
}
165-
}
166-
167-
#[no_mangle]
168-
pub unsafe extern "C" fn __fw_malloc(s: usize) -> *mut u8 {
169-
use alloc::alloc::Layout;
170-
alloc::alloc::alloc(Layout::from_size_align(s, 1).unwrap())
171-
}

src/tpm/src/std_lib.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) 2022 - 2023 Intel Corporation
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#![cfg_attr(not(test), no_std)]
6+
#![cfg_attr(test, allow(unused_imports))]
7+
#![feature(alloc_error_handler)]
8+
#![feature(naked_functions)]
9+
use alloc::collections::BTreeMap;
10+
use core::alloc::Layout;
11+
#[allow(unused, non_snake_case, non_upper_case_globals, non_camel_case_types)]
12+
use core::{ffi::c_void, ptr::null_mut};
13+
use lazy_static::lazy_static;
14+
use spin::Mutex;
15+
16+
extern crate alloc;
17+
18+
#[no_mangle]
19+
pub extern "C" fn __fw_debug_msg(msg: *const u8, len: usize) {
20+
let msg = unsafe {
21+
let r = core::slice::from_raw_parts(msg, len);
22+
core::str::from_utf8_unchecked(r)
23+
};
24+
log::info!("{}", msg);
25+
}
26+
27+
#[no_mangle]
28+
pub extern "C" fn __fw_debug_buffer(buffer: *const u8, len: usize) {
29+
let buf = unsafe { core::slice::from_raw_parts(buffer, len) };
30+
log::info!("buffer {:x?}\n", buf);
31+
}
32+
33+
#[no_mangle]
34+
pub extern "C" fn __fw_abort() {
35+
panic!("abort called");
36+
}
37+
38+
#[no_mangle]
39+
pub extern "C" fn __fw_rdrand32() -> u32 {
40+
unsafe {
41+
let mut ret: u32 = 0;
42+
for _ in 0..10 {
43+
if core::arch::x86_64::_rdrand32_step(&mut ret) == 1 {
44+
return ret;
45+
}
46+
}
47+
panic!("Failed to obtain random data");
48+
}
49+
}
50+
51+
lazy_static! {
52+
static ref MALLOC_TABLE: Mutex<BTreeMap<usize, usize>> = Mutex::new(BTreeMap::new());
53+
}
54+
55+
#[no_mangle]
56+
pub unsafe extern "C" fn __fw_malloc(size: usize) -> *mut c_void {
57+
let addr = alloc::alloc::alloc(Layout::from_size_align_unchecked(size, 1)) as *mut c_void;
58+
MALLOC_TABLE.lock().insert(addr as usize, size);
59+
addr
60+
}
61+
62+
#[no_mangle]
63+
pub unsafe extern "C" fn __fw_free(ptr: *mut c_void) {
64+
if let Some(size) = MALLOC_TABLE.lock().get(&(ptr as usize)) {
65+
alloc::alloc::dealloc(ptr as *mut u8, Layout::from_size_align_unchecked(*size, 1))
66+
}
67+
}

0 commit comments

Comments
 (0)