1*fae6e9adSlinfeng use crate::{ebpf::STACK_SIZE, vec, Vec}; 2*fae6e9adSlinfeng 3*fae6e9adSlinfeng pub struct StackFrame { 4*fae6e9adSlinfeng return_address: u16, 5*fae6e9adSlinfeng saved_registers: [u64; 4], 6*fae6e9adSlinfeng sp: u16, 7*fae6e9adSlinfeng frame: Vec<u8>, 8*fae6e9adSlinfeng } 9*fae6e9adSlinfeng 10*fae6e9adSlinfeng impl StackFrame { 11*fae6e9adSlinfeng /// Create a new stack frame 12*fae6e9adSlinfeng /// 13*fae6e9adSlinfeng /// The stack frame is created with a capacity of `STACK_SIZE` == 512 bytes new() -> Self14*fae6e9adSlinfeng pub fn new() -> Self { 15*fae6e9adSlinfeng Self { 16*fae6e9adSlinfeng sp: 0, 17*fae6e9adSlinfeng return_address: 0, 18*fae6e9adSlinfeng saved_registers: [0; 4], 19*fae6e9adSlinfeng frame: vec![0; STACK_SIZE], 20*fae6e9adSlinfeng } 21*fae6e9adSlinfeng } 22*fae6e9adSlinfeng 23*fae6e9adSlinfeng /// Create a new stack frame with a given capacity 24*fae6e9adSlinfeng #[allow(unused)] with_capacity(capacity: usize) -> Self25*fae6e9adSlinfeng pub fn with_capacity(capacity: usize) -> Self { 26*fae6e9adSlinfeng Self { 27*fae6e9adSlinfeng sp: 0, 28*fae6e9adSlinfeng return_address: 0, 29*fae6e9adSlinfeng saved_registers: [0; 4], 30*fae6e9adSlinfeng frame: vec![0; capacity], 31*fae6e9adSlinfeng } 32*fae6e9adSlinfeng } 33*fae6e9adSlinfeng 34*fae6e9adSlinfeng /// The capacity of the stack frame len(&self) -> usize35*fae6e9adSlinfeng pub fn len(&self) -> usize { 36*fae6e9adSlinfeng self.frame.len() 37*fae6e9adSlinfeng } 38*fae6e9adSlinfeng as_ptr(&self) -> *const u839*fae6e9adSlinfeng pub fn as_ptr(&self) -> *const u8 { 40*fae6e9adSlinfeng self.frame.as_ptr() 41*fae6e9adSlinfeng } 42*fae6e9adSlinfeng as_slice(&self) -> &[u8]43*fae6e9adSlinfeng pub fn as_slice(&self) -> &[u8] { 44*fae6e9adSlinfeng self.frame.as_slice() 45*fae6e9adSlinfeng } 46*fae6e9adSlinfeng /// Save the callee-saved registers save_registers(&mut self, regs: &[u64])47*fae6e9adSlinfeng pub fn save_registers(&mut self, regs: &[u64]) { 48*fae6e9adSlinfeng self.saved_registers.copy_from_slice(regs); 49*fae6e9adSlinfeng } 50*fae6e9adSlinfeng 51*fae6e9adSlinfeng /// Get the callee-saved registers get_registers(&self) -> [u64; 4]52*fae6e9adSlinfeng pub fn get_registers(&self) -> [u64; 4] { 53*fae6e9adSlinfeng self.saved_registers 54*fae6e9adSlinfeng } 55*fae6e9adSlinfeng 56*fae6e9adSlinfeng /// Save the return address save_return_address(&mut self, address: u16)57*fae6e9adSlinfeng pub fn save_return_address(&mut self, address: u16) { 58*fae6e9adSlinfeng self.return_address = address; 59*fae6e9adSlinfeng } 60*fae6e9adSlinfeng 61*fae6e9adSlinfeng /// Get the return address get_return_address(&self) -> u1662*fae6e9adSlinfeng pub fn get_return_address(&self) -> u16 { 63*fae6e9adSlinfeng self.return_address 64*fae6e9adSlinfeng } 65*fae6e9adSlinfeng 66*fae6e9adSlinfeng /// Save the stack pointer save_sp(&mut self, sp: u16)67*fae6e9adSlinfeng pub fn save_sp(&mut self, sp: u16) { 68*fae6e9adSlinfeng self.sp = sp; 69*fae6e9adSlinfeng } 70*fae6e9adSlinfeng 71*fae6e9adSlinfeng /// Get the stack pointer get_sp(&self) -> u1672*fae6e9adSlinfeng pub fn get_sp(&self) -> u16 { 73*fae6e9adSlinfeng self.sp 74*fae6e9adSlinfeng } 75*fae6e9adSlinfeng } 76