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
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

//! Allocator functionality used by Simics and which should be used by modules importing Simics
//! functionality to ensure no conflicts

use crate::{
    sys::{mm_free, mm_realloc, mm_zalloc},
    Result,
};
use raw_cstr::raw_cstr;
use std::{alloc::GlobalAlloc, ffi::c_void, mem::transmute};

#[macro_export]
/// Allocate memory with a size, of some type
macro_rules! simics_alloc {
    ($typ:ty, $sz:expr) => {
        $crate::api::alloc($sz, stringify!($typ), file!(), line!() as i32)
    };
}

/// Allocate using the SIMICS zalloc implementation
///
/// # Context
///
/// All Contexts
pub fn alloc<T, S: AsRef<str>>(
    size: usize,
    typename: S,
    filename: S,
    line_number: i32,
) -> Result<*mut T> {
    unsafe {
        let res = mm_zalloc(
            size,
            size,
            raw_cstr(typename)?,
            raw_cstr(filename)?,
            line_number,
        );
        Ok(transmute::<*mut std::ffi::c_void, *mut T>(res))
    }
}

/// Free a pointer that was allocated with [`alloc`]
///
/// # Context
///
/// All Contexts
pub fn free<T>(ptr: *mut T) {
    unsafe { mm_free(ptr as *mut c_void) };
}

/// Global allocator that uses SIMICS' exported memory management functionality
pub struct SimicsAlloc;

// Unfortunately we don't have a good way to access the caller from global alloc, so we
// provide dummy values for these fields
const SIMICS_ALLOC_TYPE: &[u8; 8] = b"unknown\0";
const SIMICS_ALLOC_FILE: &[u8; 8] = b"unknown\0";
const SIMICS_ALLOC_LINE: i32 = 0;

unsafe impl GlobalAlloc for SimicsAlloc {
    /// Allocate using the global SIMICS allocator. Note: this allocation function
    /// may fail in circumstances where very unusual alignment is requried.
    unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {
        let size = layout.size();

        unsafe {
            mm_zalloc(
                size,
                1,
                SIMICS_ALLOC_TYPE.as_ptr() as *const i8,
                SIMICS_ALLOC_FILE.as_ptr() as *const i8,
                SIMICS_ALLOC_LINE,
            ) as *mut u8
        }
    }

    /// All allocations are zeroed, so this method calls through to `alloc`
    unsafe fn alloc_zeroed(&self, layout: std::alloc::Layout) -> *mut u8 {
        self.alloc(layout)
    }

    unsafe fn realloc(
        &self,
        ptr: *mut u8,
        _layout: std::alloc::Layout,
        new_size: usize,
    ) -> *mut u8 {
        unsafe {
            mm_realloc(
                ptr as *mut c_void,
                new_size,
                1,
                SIMICS_ALLOC_TYPE.as_ptr() as *const i8,
                SIMICS_ALLOC_FILE.as_ptr() as *const i8,
                SIMICS_ALLOC_LINE,
            ) as *mut u8
        }
    }

    /// Deallocate using the global SIMICS allocator. Note: this deallocation function
    /// may fail in circumstances where very unusual alignment is required.
    unsafe fn dealloc(&self, ptr: *mut u8, _layout: std::alloc::Layout) {
        unsafe { mm_free(ptr as *mut c_void) };
    }
}