2019-05-05 20:56:23 +08:00
|
|
|
pub trait ReadableRegister<T> {
|
|
|
|
fn get(&self) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! def_reg_get {
|
|
|
|
($name:ty, $type:ty, $asm_instr:tt) => {
|
|
|
|
impl ReadableRegister<$type> for $name {
|
|
|
|
#[inline(always)]
|
|
|
|
fn get(&self) -> $type {
|
|
|
|
let mut value;
|
|
|
|
unsafe { asm!($asm_instr : "=r" (value) ::: "volatile") }
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait WritableRegister<T> {
|
|
|
|
fn set(&self, value: T);
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! def_reg_set {
|
|
|
|
($name:ty, $type:ty, $asm_instr:tt) => {
|
|
|
|
impl WritableRegister<$type> for $name {
|
|
|
|
#[inline(always)]
|
|
|
|
fn set(&self, value: $type) {
|
|
|
|
unsafe { asm!($asm_instr :: "r" (value) :: "volatile") }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 07:44:24 +08:00
|
|
|
/// Stack Pointer
|
2019-05-05 20:56:23 +08:00
|
|
|
pub struct SP;
|
|
|
|
def_reg_get!(SP, u32, "mov $0, sp");
|
|
|
|
def_reg_set!(SP, u32, "mov sp, $0");
|
|
|
|
|
2019-05-27 07:44:24 +08:00
|
|
|
/// Link register (function call return address)
|
|
|
|
pub struct LR;
|
|
|
|
def_reg_get!(LR, u32, "mov $0, lr");
|
|
|
|
def_reg_set!(LR, u32, "mov lr, $0");
|
|
|
|
|
2019-05-05 20:56:23 +08:00
|
|
|
pub struct MPIDR;
|
|
|
|
def_reg_get!(MPIDR, u32, "mrc p15, 0, $0, c0, c0, 5");
|
2019-05-24 01:05:06 +08:00
|
|
|
|
|
|
|
/// Invalidate TLBs
|
|
|
|
pub fn tlbiall() {
|
|
|
|
unsafe {
|
|
|
|
asm!("mcr p15, 0, $0, c8, c7, 0" :: "r" (0) :: "volatile");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invalidate I-Cache
|
|
|
|
pub fn iciallu() {
|
|
|
|
unsafe {
|
|
|
|
asm!("mcr p15, 0, $0, c7, c5, 0" :: "r" (0) :: "volatile");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invalidate Branch Predictor Array
|
|
|
|
pub fn bpiall() {
|
|
|
|
unsafe {
|
|
|
|
asm!("mcr p15, 0, $0, c7, c5, 6" :: "r" (0) :: "volatile");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invalidate D-Cache
|
|
|
|
pub fn dccisw() {
|
|
|
|
// TODO: $0 is r11 at what value?
|
|
|
|
unsafe {
|
|
|
|
asm!("mcr p15, 0, $0, c7, c5, 6" :: "r" (0) :: "volatile");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable I-Cache and D-Cache
|
|
|
|
pub fn sctlr() {
|
|
|
|
unsafe {
|
2019-06-05 05:47:23 +08:00
|
|
|
asm!("mcr p15, 0, $0, c1, c0, 0" :: "r" (0x00401004) :: "volatile");
|
2019-05-24 01:05:06 +08:00
|
|
|
}
|
|
|
|
}
|