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
/*
 * Copyright (c) 2018 by the author(s)
 *
 * =============================================================================
 *
 * Licensed under either of
 *   - Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 *   - MIT License (http://opensource.org/licenses/MIT)
 * at your option.
 *
 * =============================================================================
 *
 * Author(s):
 *   - Andre Richter <andre.o.richter@gmail.com>
 */

macro_rules! __read_raw {
    ($width:ty, $asm_instr:tt, $asm_reg_name:tt) => {
        /// Reads the raw bits of the CPU register.
        #[inline]
        fn get(&self) -> $width {
            match () {
                #[cfg(target_arch = "aarch64")]
                () => {
                    let reg;
                    unsafe {
                        asm!(concat!($asm_instr, " $0, ", $asm_reg_name) : "=r"(reg) ::: "volatile");
                    }
                    reg
                }

                #[cfg(not(target_arch = "aarch64"))]
                () => unimplemented!(),
            }
        }
    };
}

macro_rules! __write_raw {
    ($width:ty, $asm_instr:tt, $asm_reg_name:tt) => {
        /// Writes raw bits to the CPU register.
        #[cfg_attr(not(target_arch = "aarch64"), allow(unused_variables))]
        #[inline]
        fn set(&self, value: $width) {
            match () {
                #[cfg(target_arch = "aarch64")]
                () => {
                    unsafe {
                        asm!(concat!($asm_instr, " ", $asm_reg_name, ", $0") :: "r"(value) :: "volatile")
                    }
                }

                #[cfg(not(target_arch = "aarch64"))]
                () => unimplemented!(),
            }
        }
    };
}

/// Raw read from system coprocessor registers
macro_rules! sys_coproc_read_raw {
    ($width:ty, $asm_reg_name:tt) => {
        __read_raw!($width, "mrs", $asm_reg_name);
    };
}

/// Raw write to system coprocessor registers
macro_rules! sys_coproc_write_raw {
    ($width:ty, $asm_reg_name:tt) => {
        __write_raw!($width, "msr", $asm_reg_name);
    };
}

/// Raw read from (ordinary) registers
macro_rules! read_raw {
    ($width:ty, $asm_reg_name:tt) => {
        __read_raw!($width, "mov", $asm_reg_name);
    };
}
/// Raw write to (ordinary) registers
macro_rules! write_raw {
    ($width:ty, $asm_reg_name:tt) => {
        __write_raw!($width, "mov", $asm_reg_name);
    };
}