| 1 |
/* Adapted from Linux: arch/x86/include/asm/paravirt.h */ |
| 2 |
|
| 3 |
/* { dg-do compile { target x86_64-pc-linux-gnu } } */ |
| 4 |
/* { dg-skip-if "" { *-*-* } { "-fno-fat-lto-objects" } { "" } } */ |
| 5 |
/* { dg-skip-if "" { *-*-* } { "-O0" } { "" } } */ |
| 6 |
|
| 7 |
/* Adapted/reduced from linux kernel (GPL-2.0). */ |
| 8 |
|
| 9 |
#include "../analyzer-decls.h" |
| 10 |
|
| 11 |
typedef unsigned char u8; |
| 12 |
typedef unsigned __INT32_TYPE__ u32; |
| 13 |
typedef unsigned __INT64_TYPE__ u64; |
| 14 |
typedef __SIZE_TYPE__ size_t; |
| 15 |
|
| 16 |
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) |
| 17 |
|
| 18 |
#define __stringify_1(x...) #x |
| 19 |
#define __stringify(x...) __stringify_1(x) |
| 20 |
|
| 21 |
# define __ASM_FORM(x, ...) " " __stringify(x,##__VA_ARGS__) " " |
| 22 |
# define __ASM_FORM_RAW(x, ...) __stringify(x,##__VA_ARGS__) |
| 23 |
|
| 24 |
#ifndef __x86_64__ |
| 25 |
/* 32 bit */ |
| 26 |
# define __ASM_SEL(a,b) __ASM_FORM(a) |
| 27 |
# define __ASM_SEL_RAW(a,b) __ASM_FORM_RAW(a) |
| 28 |
#else |
| 29 |
/* 64 bit */ |
| 30 |
# define __ASM_SEL(a,b) __ASM_FORM(b) |
| 31 |
# define __ASM_SEL_RAW(a,b) __ASM_FORM_RAW(b) |
| 32 |
#endif |
| 33 |
|
| 34 |
#define __ASM_REG(reg) __ASM_SEL_RAW(e##reg, r##reg) |
| 35 |
|
| 36 |
#define _ASM_PTR __ASM_SEL(.long, .quad) |
| 37 |
#define _ASM_ALIGN __ASM_SEL(.balign 4, .balign 8) |
| 38 |
|
| 39 |
#define _ASM_SP __ASM_REG(sp) |
| 40 |
|
| 41 |
|
| 42 |
register unsigned long current_stack_pointer asm(_ASM_SP); |
| 43 |
#define ASM_CALL_CONSTRAINT "+r" (current_stack_pointer) |
| 44 |
|
| 45 |
#define ANNOTATE_RETPOLINE_SAFE \ |
| 46 |
"999:\n\t" \ |
| 47 |
".pushsection .discard.retpoline_safe\n\t" \ |
| 48 |
_ASM_PTR " 999b\n\t" \ |
| 49 |
".popsection\n\t" |
| 50 |
|
| 51 |
/* Adapted from Linux arch/x86/include/asm/paravirt.h */ |
| 52 |
|
| 53 |
|
| 54 |
/* snip */ |
| 55 |
|
| 56 |
/* ./arch/x86/include/asm/paravirt.h I think; was: |
| 57 |
PVOP_VCALL4(cpu.cpuid, eax, ebx, ecx, edx); |
| 58 |
|
| 59 |
*/ |
| 60 |
|
| 61 |
#ifndef __x86_64__ |
| 62 |
#define CLBR_ANY ((1 << 4) - 1) |
| 63 |
#else |
| 64 |
#define CLBR_ANY ((1 << 9) - 1) |
| 65 |
#endif /* X86_64 */ |
| 66 |
|
| 67 |
struct pv_cpu_ops { |
| 68 |
/* snip */ |
| 69 |
u64 (*read_msr_safe)(unsigned int msr, int *err); |
| 70 |
/* snip */ |
| 71 |
}; |
| 72 |
|
| 73 |
struct paravirt_patch_template { |
| 74 |
struct pv_cpu_ops cpu; |
| 75 |
/* snip */ |
| 76 |
}; |
| 77 |
extern struct paravirt_patch_template pv_ops; |
| 78 |
|
| 79 |
#define PARAVIRT_PATCH(x) \ |
| 80 |
(offsetof(struct paravirt_patch_template, x) / sizeof(void *)) |
| 81 |
|
| 82 |
#define paravirt_type(op) \ |
| 83 |
[paravirt_typenum] "i" (PARAVIRT_PATCH(op)), \ |
| 84 |
[paravirt_opptr] "i" (&(pv_ops.op)) |
| 85 |
#define paravirt_clobber(clobber) \ |
| 86 |
[paravirt_clobber] "i" (clobber) |
| 87 |
|
| 88 |
/* |
| 89 |
* Generate some code, and mark it as patchable by the |
| 90 |
* apply_paravirt() alternate instruction patcher. |
| 91 |
*/ |
| 92 |
#define _paravirt_alt(insn_string, type, clobber) \ |
| 93 |
"771:\n\t" insn_string "\n" "772:\n" \ |
| 94 |
".pushsection .parainstructions,\"a\"\n" \ |
| 95 |
_ASM_ALIGN "\n" \ |
| 96 |
_ASM_PTR " 771b\n" \ |
| 97 |
" .byte " type "\n" \ |
| 98 |
" .byte 772b-771b\n" \ |
| 99 |
" .short " clobber "\n" \ |
| 100 |
".popsection\n" |
| 101 |
|
| 102 |
/* Generate patchable code, with the default asm parameters. */ |
| 103 |
#define paravirt_alt(insn_string) \ |
| 104 |
_paravirt_alt(insn_string, "%c[paravirt_typenum]", "%c[paravirt_clobber]") |
| 105 |
|
| 106 |
#define PARAVIRT_CALL \ |
| 107 |
ANNOTATE_RETPOLINE_SAFE \ |
| 108 |
"call *%c[paravirt_opptr];" |
| 109 |
|
| 110 |
#ifndef __x86_64__ |
| 111 |
|
| 112 |
/* 32-bit. */ |
| 113 |
|
| 114 |
#define PVOP_CALL_ARGS \ |
| 115 |
unsigned long __eax = __eax, __edx = __edx, __ecx = __ecx; |
| 116 |
|
| 117 |
#define PVOP_CALL_ARG1(x) "a" ((unsigned long)(x)) |
| 118 |
#define PVOP_CALL_ARG2(x) "d" ((unsigned long)(x)) |
| 119 |
|
| 120 |
#define PVOP_VCALL_CLOBBERS "=a" (__eax), "=d" (__edx), \ |
| 121 |
"=c" (__ecx) |
| 122 |
#define PVOP_CALL_CLOBBERS PVOP_VCALL_CLOBBERS |
| 123 |
|
| 124 |
#define PVOP_VCALLEE_CLOBBERS "=a" (__eax), "=d" (__edx) |
| 125 |
#define PVOP_CALLEE_CLOBBERS PVOP_VCALLEE_CLOBBERS |
| 126 |
|
| 127 |
#define EXTRA_CLOBBERS |
| 128 |
|
| 129 |
#else |
| 130 |
|
| 131 |
/* 64-bit. */ |
| 132 |
|
| 133 |
/* [re]ax isn't an arg, but the return val */ |
| 134 |
#define PVOP_CALL_ARGS \ |
| 135 |
unsigned long __edi = __edi, __esi = __esi, \ |
| 136 |
__edx = __edx, __ecx = __ecx, __eax = __eax; |
| 137 |
|
| 138 |
#define PVOP_CALL_ARG1(x) "D" ((unsigned long)(x)) |
| 139 |
#define PVOP_CALL_ARG2(x) "S" ((unsigned long)(x)) |
| 140 |
|
| 141 |
#define PVOP_VCALL_CLOBBERS "=D" (__edi), \ |
| 142 |
"=S" (__esi), "=d" (__edx), \ |
| 143 |
"=c" (__ecx) |
| 144 |
#define PVOP_CALL_CLOBBERS PVOP_VCALL_CLOBBERS, "=a" (__eax) |
| 145 |
#define PVOP_VCALLEE_CLOBBERS "=a" (__eax) |
| 146 |
#define PVOP_CALLEE_CLOBBERS PVOP_VCALLEE_CLOBBERS |
| 147 |
|
| 148 |
#define EXTRA_CLOBBERS , "r8", "r9", "r10", "r11" |
| 149 |
#endif /* CONFIG_X86_32 */ |
| 150 |
|
| 151 |
#define PVOP_TEST_NULL(op) ((void)pv_ops.op) |
| 152 |
|
| 153 |
#define PVOP_RETVAL(rettype) \ |
| 154 |
({ unsigned long __mask = ~0UL; \ |
| 155 |
switch (sizeof(rettype)) { \ |
| 156 |
case 1: __mask = 0xffUL; break; \ |
| 157 |
case 2: __mask = 0xffffUL; break; \ |
| 158 |
case 4: __mask = 0xffffffffUL; break; \ |
| 159 |
default: break; \ |
| 160 |
} \ |
| 161 |
__mask & __eax; \ |
| 162 |
}) |
| 163 |
|
| 164 |
#define ____PVOP_CALL(ret, op, clbr, call_clbr, extra_clbr, ...) \ |
| 165 |
({ \ |
| 166 |
PVOP_CALL_ARGS; \ |
| 167 |
PVOP_TEST_NULL(op); \ |
| 168 |
asm volatile(paravirt_alt(PARAVIRT_CALL) \ |
| 169 |
: call_clbr, ASM_CALL_CONSTRAINT \ |
| 170 |
: paravirt_type(op), \ |
| 171 |
paravirt_clobber(clbr), \ |
| 172 |
##__VA_ARGS__ \ |
| 173 |
: "memory", "cc" extra_clbr); \ |
| 174 |
ret; \ |
| 175 |
}) |
| 176 |
|
| 177 |
#define __PVOP_CALL(rettype, op, ...) \ |
| 178 |
____PVOP_CALL(PVOP_RETVAL(rettype), op, CLBR_ANY, \ |
| 179 |
PVOP_CALL_CLOBBERS, EXTRA_CLOBBERS, ##__VA_ARGS__) |
| 180 |
|
| 181 |
#define PVOP_CALL2(rettype, op, arg1, arg2) \ |
| 182 |
__PVOP_CALL(rettype, op, PVOP_CALL_ARG1(arg1), PVOP_CALL_ARG2(arg2)) |
| 183 |
|
| 184 |
static inline u64 paravirt_read_msr_safe(unsigned msr, int *err) |
| 185 |
{ |
| 186 |
return PVOP_CALL2(u64, cpu.read_msr_safe, msr, err); |
| 187 |
} |
| 188 |
|
| 189 |
#define rdmsr_safe(msr, a, b) \ |
| 190 |
({ \ |
| 191 |
int _err; \ |
| 192 |
u64 _l = paravirt_read_msr_safe(msr, &_err); \ |
| 193 |
(*a) = (u32)_l; \ |
| 194 |
(*b) = _l >> 32; \ |
| 195 |
_err; \ |
| 196 |
}) |
| 197 |
|
| 198 |
|
| 199 |
void check_init_int(int); |
| 200 |
void check_init_u32(u32); |
| 201 |
|
| 202 |
void test(void) |
| 203 |
{ |
| 204 |
int err; |
| 205 |
u32 eax, edx; |
| 206 |
err = rdmsr_safe(0, &eax, &edx); |
| 207 |
check_init_int(err); |
| 208 |
check_init_u32(eax); |
| 209 |
check_init_u32(edx); |
| 210 |
} |