Skip to content

Commit

Permalink
rdmsr and wrmsr _safe with exception tables
Browse files Browse the repository at this point in the history
Add new rdmsr_safe and wrmsr_safe. These two functions
use exception tables to try performing their operations
and returning safely if the operation could not be performed.

Signed-off-by: Daniele Ahmed <[email protected]>
  • Loading branch information
82marbag committed Sep 6, 2021
1 parent 90e4e23 commit 93ac138
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion include/lib.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -27,6 +27,7 @@

#include <asm-macros.h>
#include <console.h>
#include <extables.h>
#include <ktf.h>
#include <processor.h>
#include <segment.h>
Expand Down Expand Up @@ -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"); }
Expand Down

0 comments on commit 93ac138

Please sign in to comment.