1 /*
2  * Copyright (c) 2015 Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * 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 NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "smc.h"
25 
26 #if ARCH_ARM64
27 #define SMC_ARG0 "x0"
28 #define SMC_ARG1 "x1"
29 #define SMC_ARG2 "x2"
30 #define SMC_ARG3 "x3"
31 #define SMC_ARCH_EXTENSION ""
32 #define SMC_REGISTERS_TRASHED                                              \
33     "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", \
34             "x15", "x16", "x17"
35 #else
36 #define SMC_ARG0 "r0"
37 #define SMC_ARG1 "r1"
38 #define SMC_ARG2 "r2"
39 #define SMC_ARG3 "r3"
40 #define SMC_ARCH_EXTENSION ".arch_extension sec\n"
41 #define SMC_REGISTERS_TRASHED "ip"
42 #endif
43 
generic_arm64_smc(ulong r0,ulong r1,ulong r2,ulong r3)44 ulong generic_arm64_smc(ulong r0, ulong r1, ulong r2, ulong r3) {
45     register ulong _r0 __asm__(SMC_ARG0) = r0;
46     register ulong _r1 __asm__(SMC_ARG1) = r1;
47     register ulong _r2 __asm__(SMC_ARG2) = r2;
48     register ulong _r3 __asm__(SMC_ARG3) = r3;
49 
50     __asm__ volatile(SMC_ARCH_EXTENSION "smc	#0" /* switch to secure world */
51                      : "=r"(_r0), "=r"(_r1), "=r"(_r2), "=r"(_r3)
52                      : "r"(_r0), "r"(_r1), "r"(_r2), "r"(_r3)
53                      : SMC_REGISTERS_TRASHED);
54     return _r0;
55 }
56