2013-09-14 11:10:14 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2013.
|
|
|
|
|
|
|
|
This file is part of the Sortix C Library.
|
|
|
|
|
|
|
|
The Sortix C Library is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
The Sortix C Library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
msr.h
|
|
|
|
High-level access to the rdmsr and wrmsr instructions for accessing and
|
|
|
|
modifying machine specific registers.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#ifndef INCLUDE_MSR_H
|
|
|
|
#define INCLUDE_MSR_H
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
|
2015-05-13 12:11:02 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2013-09-14 11:10:14 -04:00
|
|
|
|
2013-09-14 12:59:15 -04:00
|
|
|
#define MSRID_FSBASE __UINT32_C(0xC0000100)
|
|
|
|
#define MSRID_GSBASE __UINT32_C(0xC0000101)
|
|
|
|
|
2013-09-14 11:10:14 -04:00
|
|
|
__attribute__((unused))
|
2013-09-14 14:05:17 -04:00
|
|
|
static __inline uint64_t rdmsr_instruction(uint32_t msrid)
|
2013-09-14 11:10:14 -04:00
|
|
|
{
|
|
|
|
uint32_t low;
|
|
|
|
uint32_t high;
|
2014-04-28 07:50:11 -04:00
|
|
|
__asm__ __volatile__ ("rdmsr" : "=a"(low), "=d"(high) : "c"(msrid));
|
2013-09-14 11:10:14 -04:00
|
|
|
return (uint64_t) low << 0 | (uint64_t) high << 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((unused))
|
2013-09-14 14:05:17 -04:00
|
|
|
static __inline uint64_t wrmsr_instruction(uint32_t msrid, uint64_t value)
|
2013-09-14 11:10:14 -04:00
|
|
|
{
|
|
|
|
uint32_t low = value >> 0 & 0xFFFFFFFF;
|
2015-03-07 07:33:35 -05:00
|
|
|
uint32_t high = value >> 32 & 0xFFFFFFFF;
|
2014-04-28 07:50:11 -04:00
|
|
|
__asm__ __volatile__ ("wrmsr" : : "a"(low), "d"(high), "c"(msrid) : "memory");
|
2013-09-14 11:10:14 -04:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2013-09-14 14:05:17 -04:00
|
|
|
#if __STDC_HOSTED__
|
|
|
|
|
|
|
|
uint64_t rdmsr(uint32_t msrid);
|
|
|
|
uint64_t wrmsr(uint32_t msrid, uint64_t value);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
__attribute__((unused))
|
|
|
|
static __inline uint64_t rdmsr(uint32_t msrid)
|
|
|
|
{
|
|
|
|
return rdmsr_instruction(msrid);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((unused))
|
|
|
|
static __inline uint64_t wrmsr(uint32_t msrid, uint64_t value)
|
|
|
|
{
|
|
|
|
return wrmsr_instruction(msrid, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-09-14 11:10:14 -04:00
|
|
|
__attribute__((unused))
|
2014-04-28 07:50:11 -04:00
|
|
|
static __inline void rdmsr_split(uint32_t msrid, uint32_t* low, uint32_t* high)
|
2013-09-14 11:10:14 -04:00
|
|
|
{
|
|
|
|
uint64_t result = rdmsr(msrid);
|
|
|
|
*low = result >> 0 & 0xFFFFFFFF;
|
|
|
|
*high = result >> 32 & 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((unused))
|
2014-04-28 07:50:11 -04:00
|
|
|
static __inline void wrmsr_split(uint32_t msrid, uint32_t low, uint32_t high)
|
2013-09-14 11:10:14 -04:00
|
|
|
{
|
|
|
|
wrmsr(msrid, (uint64_t) low << 0 | (uint64_t) high << 32);
|
|
|
|
}
|
|
|
|
|
2015-05-13 12:11:02 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|
2013-09-14 11:10:14 -04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|