mirror of
https://github.com/anrieff/libcpuid
synced 2025-01-23 20:06:41 +00:00
Basic Linux support, untested
git-svn-id: https://svn.code.sf.net/p/libcpuid/code/HEAD/libcpuid@72 3b4be424-7ac5-41d7-8526-f4ddcb85d872
This commit is contained in:
parent
e4920d79b0
commit
4729960411
4 changed files with 238 additions and 174 deletions
|
@ -12,7 +12,8 @@ libcpuid_la_SOURCES = \
|
||||||
recog_amd.c \
|
recog_amd.c \
|
||||||
rdtsc.c \
|
rdtsc.c \
|
||||||
asm-bits.c \
|
asm-bits.c \
|
||||||
libcpuid_util.c
|
libcpuid_util.c \
|
||||||
|
rdmsr.c
|
||||||
|
|
||||||
libcpuid_la_DEPENDENCIES = \
|
libcpuid_la_DEPENDENCIES = \
|
||||||
$(srcdir)/libcpuid.sym
|
$(srcdir)/libcpuid.sym
|
||||||
|
|
|
@ -343,11 +343,12 @@ typedef enum {
|
||||||
ERR_BADFMT = -5, /*!< "Bad file format" */
|
ERR_BADFMT = -5, /*!< "Bad file format" */
|
||||||
ERR_NOT_IMP = -6, /*!< "Not implemented" */
|
ERR_NOT_IMP = -6, /*!< "Not implemented" */
|
||||||
ERR_CPU_UNKN = -7, /*!< "Unsupported processor" */
|
ERR_CPU_UNKN = -7, /*!< "Unsupported processor" */
|
||||||
ERR_NO_RDMSR = -8, /*!< "RDMSR instruction is not supported" */
|
ERR_NO_RDMSR = -8, /*!< "RDMSR instruction is not supported" */
|
||||||
ERR_NO_DRIVER= -9, /*!< "RDMSR driver error (generic)" */
|
ERR_NO_DRIVER= -9, /*!< "RDMSR driver error (generic)" */
|
||||||
ERR_NO_PERMS = -10, /*!< "No permissions to install RDMSR driver" */
|
ERR_NO_PERMS = -10, /*!< "No permissions to install RDMSR driver" */
|
||||||
ERR_EXTRACT = -11, /*!< "Cannot extract RDMSR driver (read only media?)" */
|
ERR_EXTRACT = -11, /*!< "Cannot extract RDMSR driver (read only media?)" */
|
||||||
ERR_HANDLE = -12, /*!< "Bad handle" */
|
ERR_HANDLE = -12, /*!< "Bad handle" */
|
||||||
|
ERR_INVMSR = -13, /*!< "Invalid MSR" */
|
||||||
} cpu_error_t;
|
} cpu_error_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -19,3 +19,7 @@ cpuid_set_warn_function
|
||||||
cpuid_set_verbosiness_level
|
cpuid_set_verbosiness_level
|
||||||
cpuid_get_cpu_list
|
cpuid_get_cpu_list
|
||||||
cpuid_free_cpu_list
|
cpuid_free_cpu_list
|
||||||
|
cpu_msr_driver_open
|
||||||
|
cpu_rdmsr
|
||||||
|
cpu_msrinfo
|
||||||
|
cpu_msr_driver_close
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
#define _XOPEN_SOURCE 500
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "libcpuid.h"
|
#include "libcpuid.h"
|
||||||
|
@ -30,25 +31,81 @@
|
||||||
#include "libcpuid_util.h"
|
#include "libcpuid_util.h"
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
/* On Linux and Apple, we still do not support RDMSR, so supply dummy struct
|
# ifdef __APPLE__
|
||||||
|
/* On Darwin, we still do not support RDMSR, so supply dummy struct
|
||||||
and functions */
|
and functions */
|
||||||
struct msr_driver_t { int dummy; }
|
struct msr_driver_t { int dummy; };
|
||||||
struct msr_driver_t* cpu_msr_driver_open(void)
|
struct msr_driver_t* cpu_msr_driver_open(void)
|
||||||
{
|
{
|
||||||
set_error(ERR_NOT_IMPL);
|
set_error(ERR_NOT_IMP);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cpu_rdmsr(struct msr_driver_t* driver, int msr_index, uint64_t* result)
|
int cpu_rdmsr(struct msr_driver_t* driver, int msr_index, uint64_t* result)
|
||||||
{
|
{
|
||||||
return set_error(ERR_NOT_IMPL);
|
return set_error(ERR_NOT_IMP);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cpu_msr_driver_close(struct msr_driver_t* driver)
|
int cpu_msr_driver_close(struct msr_driver_t* driver)
|
||||||
{
|
{
|
||||||
return set_error(ERR_NOT_IMPL);
|
return set_error(ERR_NOT_IMP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int cpu_msrinfo(struct msr_driver_t* driver, cpu_msrinfo_request_t which)
|
||||||
|
{
|
||||||
|
return set_error(ERR_NOT_IMP);
|
||||||
|
}
|
||||||
|
# else /* __APPLE__ */
|
||||||
|
/* Assuming linux with /dev/cpu/x/msr: */
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
struct msr_driver_t { int fd; };
|
||||||
|
static int rdmsr_supported(void);
|
||||||
|
struct msr_driver_t* cpu_msr_driver_open(void)
|
||||||
|
{
|
||||||
|
struct msr_driver_t* handle;
|
||||||
|
if (!rdmsr_supported()) {
|
||||||
|
set_error(ERR_NO_RDMSR);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
int fd = open("/dev/cpu/0/msr", O_RDONLY);
|
||||||
|
if (fd < 0) {
|
||||||
|
if (errno == EIO) {
|
||||||
|
set_error(ERR_NO_RDMSR);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
set_error(ERR_NO_DRIVER);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
handle = (struct msr_driver_t*) malloc(sizeof(struct msr_driver_t));
|
||||||
|
handle->fd = fd;
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
int cpu_rdmsr(struct msr_driver_t* driver, int msr_index, uint64_t* result)
|
||||||
|
{
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
|
if (!driver || driver->fd < 0)
|
||||||
|
return set_error(ERR_HANDLE);
|
||||||
|
ret = pread(driver->fd, result, 8, msr_index * 8);
|
||||||
|
if (ret != 8)
|
||||||
|
return set_error(ERR_INVMSR);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cpu_msr_driver_close(struct msr_driver_t* drv)
|
||||||
|
{
|
||||||
|
if (drv && drv->fd >= 0) {
|
||||||
|
close(drv->fd);
|
||||||
|
free(drv);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
# endif /* __APPLE__ */
|
||||||
#else /* _WIN32 */
|
#else /* _WIN32 */
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
|
@ -103,12 +160,6 @@ struct msr_driver_t* cpu_msr_driver_open(void)
|
||||||
return drv;
|
return drv;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int rdmsr_supported(void)
|
|
||||||
{
|
|
||||||
struct cpu_id_t* id = get_cached_cpuid();
|
|
||||||
return id->flags[CPU_FEATURE_MSR];
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
|
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
|
||||||
static BOOL is_running_x64(void)
|
static BOOL is_running_x64(void)
|
||||||
{
|
{
|
||||||
|
@ -305,6 +356,15 @@ int cpu_msr_driver_close(struct msr_driver_t* drv)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
|
|
||||||
|
static int rdmsr_supported(void)
|
||||||
|
{
|
||||||
|
struct cpu_id_t* id = get_cached_cpuid();
|
||||||
|
return id->flags[CPU_FEATURE_MSR];
|
||||||
|
}
|
||||||
|
|
||||||
int cpu_msrinfo(struct msr_driver_t* handle, cpu_msrinfo_request_t which)
|
int cpu_msrinfo(struct msr_driver_t* handle, cpu_msrinfo_request_t which)
|
||||||
{
|
{
|
||||||
uint64_t r;
|
uint64_t r;
|
||||||
|
@ -339,5 +399,3 @@ int cpu_msrinfo(struct msr_driver_t* handle, cpu_msrinfo_request_t which)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
|
|
Loading…
Reference in a new issue