1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <trusty/smc.h>
26
27 #ifndef __asmeq
28 #define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t"
29 #endif
30
31 #ifdef NS_ARCH_ARM64
32 #define SMC_ARG0 "x0"
33 #define SMC_ARG1 "x1"
34 #define SMC_ARG2 "x2"
35 #define SMC_ARG3 "x3"
36 #define SMC_ARG4 "x4"
37 #define SMC_ARG5 "x5"
38 #define SMC_ARG6 "x6"
39 #define SMC_ARG7 "x7"
40 #define SMC_ARCH_EXTENSION ""
41 #define SMC_REGISTERS_TRASHED \
42 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17"
43 #else
44 #define SMC_ARG0 "r0"
45 #define SMC_ARG1 "r1"
46 #define SMC_ARG2 "r2"
47 #define SMC_ARG3 "r3"
48 #define SMC_ARG4 "r4"
49 #define SMC_ARG5 "r5"
50 #define SMC_ARG6 "r6"
51 #define SMC_ARG7 "r7"
52 #define SMC_ARCH_EXTENSION ".arch_extension sec\n"
53 #define SMC_REGISTERS_TRASHED "ip"
54 #endif
55
56 /*
57 * Execute SMC call into trusty
58 */
smc8(unsigned long r0,unsigned long r1,unsigned long r2,unsigned long r3,unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7)59 struct smc_ret8 smc8(unsigned long r0,
60 unsigned long r1,
61 unsigned long r2,
62 unsigned long r3,
63 unsigned long r4,
64 unsigned long r5,
65 unsigned long r6,
66 unsigned long r7) {
67 register unsigned long _r0 __asm__(SMC_ARG0) = r0;
68 register unsigned long _r1 __asm__(SMC_ARG1) = r1;
69 register unsigned long _r2 __asm__(SMC_ARG2) = r2;
70 register unsigned long _r3 __asm__(SMC_ARG3) = r3;
71 register unsigned long _r4 __asm__(SMC_ARG4) = r4;
72 register unsigned long _r5 __asm__(SMC_ARG5) = r5;
73 register unsigned long _r6 __asm__(SMC_ARG6) = r6;
74 register unsigned long _r7 __asm__(SMC_ARG7) = r7;
75
76 /* clang-format off */
77 __asm__ volatile(
78 __asmeq("%0", SMC_ARG0)
79 __asmeq("%1", SMC_ARG1)
80 __asmeq("%2", SMC_ARG2)
81 __asmeq("%3", SMC_ARG3)
82 __asmeq("%4", SMC_ARG4)
83 __asmeq("%5", SMC_ARG5)
84 __asmeq("%6", SMC_ARG6)
85 __asmeq("%7", SMC_ARG7)
86 __asmeq("%8", SMC_ARG0)
87 __asmeq("%9", SMC_ARG1)
88 __asmeq("%10", SMC_ARG2)
89 __asmeq("%11", SMC_ARG3)
90 __asmeq("%12", SMC_ARG4)
91 __asmeq("%13", SMC_ARG5)
92 __asmeq("%14", SMC_ARG6)
93 __asmeq("%15", SMC_ARG7)
94 SMC_ARCH_EXTENSION
95 "smc #0" /* switch to secure world */
96 : "=r" (_r0), "=r" (_r1), "=r" (_r2), "=r" (_r3), "=r" (_r4),
97 "=r" (_r5), "=r" (_r6), "=r" (_r7)
98 : "r" (_r0), "r" (_r1), "r" (_r2), "r" (_r3), "r" (_r4), "r" (_r5),
99 "r" (_r6), "r" (_r7)
100 : SMC_REGISTERS_TRASHED);
101 /* clang-format on */
102 {
103 struct smc_ret8 ret = {_r0, _r1, _r2, _r3, _r4, _r5, _r6, _r7};
104 return ret;
105 }
106 }
107