diff --git a/include/lib.h b/include/lib.h index c3a032b7..2ee226d3 100644 --- a/include/lib.h +++ b/include/lib.h @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Amazon.com, Inc. or its affiliates. + * Copyright © 2021 Amazon.com, Inc. or its affiliates. * All Rights Reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -144,11 +145,42 @@ static inline uint64_t rdmsr(uint32_t msr_idx) { return (((uint64_t) high) << 32) | low; } +/* + * read the msr_idx msr; the result is valid iff *read is true + */ +static inline uint64_t rdmsr_safe(uint32_t msr_idx, bool *read) { + volatile bool _read = false; + uint32_t low, high; + + asm volatile("1: rdmsr; movq $1, %[read];" + "2:" ASM_EXTABLE_HANDLER(1b, 2b, 0) + : "=a"(low), "=d"(high), [ read ] "=m"(_read) + : "c"(msr_idx) + : "memory"); + *read = _read; + + return (((uint64_t) high) << 32) | low; +} + static inline void wrmsr(uint32_t msr_idx, uint64_t value) { asm volatile("wrmsr" ::"c"(msr_idx), "a"((uint32_t) value), "d"((uint32_t)(value >> 32))); } +/* + * write to the msr_idx msr; the result is valid iff *read is true + */ +static inline void wrmsr_safe(uint32_t msr_idx, uint64_t value, bool *read) { + volatile bool _read = false; + + asm volatile("1: wrmsr; movq $1, %[read];" + "2:" ASM_EXTABLE_HANDLER(1b, 2b, 0) + : [ read ] "=m"(_read) + : "c"(msr_idx), "a"((uint32_t) value), "d"((uint32_t)(value >> 32)) + : "memory"); + *read = _read; +} + static inline void sti(void) { asm volatile("sti"); } static inline void cli(void) { asm volatile("cli"); }