forked from panda-re/hypernvram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypercall.h
More file actions
134 lines (122 loc) · 3.56 KB
/
hypercall.h
File metadata and controls
134 lines (122 loc) · 3.56 KB
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
#define MAGIC_VAL 0x31838188
int strnlen_n(char* s, int max_len){
int i = 0;
while (i < max_len && s[i] != '\0'){
i++;
}
return i;
}
// #if defined(__x86_64__)
#if defined(__x86_64__)
static inline int hc(char *s) {
uint64_t eax = MAGIC_VAL;
uint64_t ret = MAGIC_VAL;
uint64_t action = (uint32_t)strnlen_n(s, 0x1000);
asm __volatile__(
"movq %1, %%rax \t\n\
movq %2, %%rdi \t\n\
movq %3, %%rsi \t\n\
cpuid \t\n\
mov %%rax, %0 \t\n\
"
: "=m" (ret) /* output operand */
: "g" (eax), "g" (action), "g" (s) /* input operands */
: "rdi", "rsi", "rdx", "eax" /* clobbered registers */
);
return ret;
}
#elif defined(__i386__) && !defined(__x86_64__)
static inline int hc(char *s) {
int eax = MAGIC_VAL;
int ret = MAGIC_VAL;
int action = (uint32_t)strnlen_n(s, 0x1000);
asm __volatile__(
"mov %1, %%eax \t\n\
mov %2, %%ebx \t\n\
mov %3, %%ecx \t\n\
cpuid \t\n\
mov %%eax, %0 \t\n\
"
: "=g" (ret) /* output operand */
: "g" (eax), "g" (action), "g" (s) /* input operands */
: "eax", "ebx", "ecx", "edx" /* clobbered registers */
);
return ret;
}
#elif defined(__arm__)
static inline __attribute__((always_inline)) int hc(char *s) {
unsigned int action = (unsigned int) strnlen_n(s, 0x1000);
unsigned long r0 = MAGIC_VAL;
int ret = MAGIC_VAL;
asm __volatile__("push {%%r0-%%r4} \t\n\
mov %%r7, %1 \t\n\
mov %%r0, %2 \t\n\
mov %%r1, %3 \t\n\
mov %%r2, %4 \t\n\
mcr p7, 0, r0, c0, c0, 0 \t\n\
mov %0, %%r0 \t\n\
pop {%%r0-%%r4} \t\n"
: "=g"(ret) /* no output registers */
: "r" (r0), "r" (action), "r" (s), "r" (0) /* input registers */
: "r0", "r1", "r2", "r3", "r4" /* clobbered registers */
);
return ret;
}
#elif defined(__mips64)
static inline int hc(void *s) {
unsigned int action = (unsigned int) strnlen_n(s, 0x1000);
unsigned long r0 = MAGIC_VAL;
int ret = MAGIC_VAL;
asm __volatile__(
"move $2, %1\t\n"
"move $4, %2\t\n"
"move $5, %3\t\n"
"movz $0, $0, $0\t\n"
"move %0, $2\t\n"
: "=g"(ret) /* output operand */
: "r" (r0), "r" (action), "r" (s) /* input operands */
: "a0", "a1", "a2", "a3" /* clobbered registers */
);
return ret;
}
#elif defined(mips) || defined(__mips__) || defined(__mips)
static inline int hc(void *s) {
unsigned int action = (unsigned int) strnlen_n(s, 0x1000);
unsigned long r0 = MAGIC_VAL;
int ret = MAGIC_VAL;
asm __volatile__(
"move $2, %1\t\n"
"move $4, %2\t\n"
"move $5, %3\t\n"
"movz $0, $0, $0\t\n"
"move %0, $2\t\n"
: "=g"(ret) /* output operand */
: "r" (r0), "r" (action), "r" (s) /* input operands */
: "a0", "a1", "a2", "a3" /* clobbered registers */
);
return ret;
}
#elif defined(__aarch64__)
static inline __attribute__((always_inline)) int hc(char *s) {
unsigned int action = (unsigned int) strnlen_n(s, 0x1000);
unsigned long r0 = MAGIC_VAL;
int ret = MAGIC_VAL;
asm __volatile__("stp x0, x1, [sp, #-16]! \t\n\
stp x2, x3, [sp, #-16]! \t\n\
mov x8, %1 \t\n\
mov x0, %2 \t\n\
mov x1, %3 \t\n\
mov x2, %4 \t\n\
msr S0_0_c5_c0_0, xzr \t\n\
mov %0, x0 \t\n\
ldp x0, x1, [sp], #16 \t\n\
ldp x2, x3, [sp], #16 \t\n"
: "=g"(ret) /* no output registers */
: "r" (r0), "r" (action), "r" (s), "r" (0) /* input registers */
: "x0", "x1", "x2", "x3", "x4" /* clobbered registers */
);
return ret;
}
#else
#error Unsupported platform.
#endif