aboutsummaryrefslogtreecommitdiff
path: root/src/smccc.rs
blob: 4b1b4a4087c56df07ca187d3ed5397df8c6ea975 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2022 the authors.
// This project is dual-licensed under Apache 2.0 and MIT terms.
// See LICENSE-APACHE and LICENSE-MIT for details.

//! Functions for making SMCCC calls.

#[cfg(any(feature = "hvc", feature = "smc"))]
#[inline(always)]
pub(crate) fn call32(function: u32, args: [u32; 7]) -> [u32; 8] {
    #[cfg(feature = "hvc")]
    {
        hvc32(function, args)
    }
    #[cfg(feature = "smc")]
    {
        smc32(function, args)
    }
}

#[cfg(any(feature = "hvc", feature = "smc"))]
#[inline(always)]
pub(crate) fn call64(function: u32, args: [u64; 17]) -> [u64; 18] {
    #[cfg(feature = "hvc")]
    {
        hvc64(function, args)
    }
    #[cfg(feature = "smc")]
    {
        smc64(function, args)
    }
}

/// Makes an HVC32 call to the hypervisor, following the SMC Calling Convention version 1.3.
#[inline(always)]
pub fn hvc32(function: u32, args: [u32; 7]) -> [u32; 8] {
    #[cfg(target_arch = "aarch64")]
    unsafe {
        let mut ret = [0; 8];

        core::arch::asm!(
            "hvc #0",
            inout("w0") function => ret[0],
            inout("w1") args[0] => ret[1],
            inout("w2") args[1] => ret[2],
            inout("w3") args[2] => ret[3],
            inout("w4") args[3] => ret[4],
            inout("w5") args[4] => ret[5],
            inout("w6") args[5] => ret[6],
            inout("w7") args[6] => ret[7],
            options(nomem, nostack)
        );

        ret
    }

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

/// Makes an SMC32 call to the firmware, following the SMC Calling Convention version 1.3.
#[inline(always)]
pub fn smc32(function: u32, args: [u32; 7]) -> [u32; 8] {
    #[cfg(target_arch = "aarch64")]
    unsafe {
        let mut ret = [0; 8];

        core::arch::asm!(
            "smc #0",
            inout("w0") function => ret[0],
            inout("w1") args[0] => ret[1],
            inout("w2") args[1] => ret[2],
            inout("w3") args[2] => ret[3],
            inout("w4") args[3] => ret[4],
            inout("w5") args[4] => ret[5],
            inout("w6") args[5] => ret[6],
            inout("w7") args[6] => ret[7],
            options(nomem, nostack)
        );

        ret
    }

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

/// Makes an HVC64 call to the hypervisor, following the SMC Calling Convention version 1.3.
#[inline(always)]
pub fn hvc64(function: u32, args: [u64; 17]) -> [u64; 18] {
    #[cfg(target_arch = "aarch64")]
    unsafe {
        let mut ret = [0; 18];

        core::arch::asm!(
            "hvc #0",
            inout("x0") function as u64 => ret[0],
            inout("x1") args[0] => ret[1],
            inout("x2") args[1] => ret[2],
            inout("x3") args[2] => ret[3],
            inout("x4") args[3] => ret[4],
            inout("x5") args[4] => ret[5],
            inout("x6") args[5] => ret[6],
            inout("x7") args[6] => ret[7],
            inout("x8") args[7] => ret[8],
            inout("x9") args[8] => ret[9],
            inout("x10") args[9] => ret[10],
            inout("x11") args[10] => ret[11],
            inout("x12") args[11] => ret[12],
            inout("x13") args[12] => ret[13],
            inout("x14") args[13] => ret[14],
            inout("x15") args[14] => ret[15],
            inout("x16") args[15] => ret[16],
            inout("x17") args[16] => ret[17],
            options(nomem, nostack)
        );

        ret
    }

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

/// Makes an SMC64 call to the firmware, following the SMC Calling Convention version 1.3.
#[inline(always)]
pub fn smc64(function: u32, args: [u64; 17]) -> [u64; 18] {
    #[cfg(target_arch = "aarch64")]
    unsafe {
        let mut ret = [0; 18];

        core::arch::asm!(
            "smc #0",
            inout("x0") function as u64 => ret[0],
            inout("x1") args[0] => ret[1],
            inout("x2") args[1] => ret[2],
            inout("x3") args[2] => ret[3],
            inout("x4") args[3] => ret[4],
            inout("x5") args[4] => ret[5],
            inout("x6") args[5] => ret[6],
            inout("x7") args[6] => ret[7],
            inout("x8") args[7] => ret[8],
            inout("x9") args[8] => ret[9],
            inout("x10") args[9] => ret[10],
            inout("x11") args[10] => ret[11],
            inout("x12") args[11] => ret[12],
            inout("x13") args[12] => ret[13],
            inout("x14") args[13] => ret[14],
            inout("x15") args[14] => ret[15],
            inout("x16") args[15] => ret[16],
            inout("x17") args[16] => ret[17],
            options(nomem, nostack)
        );

        ret
    }

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