1
0
Fork 0
mirror of https://github.com/anrieff/libcpuid synced 2024-11-10 22:59:13 +00:00

Add load_driver() support for GNU/Linux & FreeBSD in cpu_msr_driver_open_core()

This commit is contained in:
Xorg 2016-06-05 14:10:19 +02:00
parent 023f0307f0
commit ec445b0a54

View file

@ -42,6 +42,21 @@
#include <errno.h> #include <errno.h>
struct msr_driver_t { int fd; }; struct msr_driver_t { int fd; };
static int rdmsr_supported(void); static int rdmsr_supported(void);
static int load_driver(char *msr_path)
{
const int file_exists = !access(msr_path, F_OK);
const int file_readable = !access(msr_path, R_OK);
if (file_exists && file_readable)
return 1;
else if (file_exists && !file_readable)
return 0;
else if (getuid() != 0)
return 0;
else
return !system("modprobe msr 2> /dev/null");
}
struct msr_driver_t* cpu_msr_driver_open(void) struct msr_driver_t* cpu_msr_driver_open(void)
{ {
return cpu_msr_driver_open_core(0); return cpu_msr_driver_open_core(0);
@ -51,7 +66,7 @@ struct msr_driver_t* cpu_msr_driver_open_core(unsigned core_num)
{ {
char msr[32]; char msr[32];
struct msr_driver_t* handle; struct msr_driver_t* handle;
if (core_num >= cpuid_get_total_cpus()) { if (core_num >= cpuid_get_total_cpus()) {
set_error(ERR_INVCNB); set_error(ERR_INVCNB);
return NULL; return NULL;
} }
@ -60,6 +75,10 @@ struct msr_driver_t* cpu_msr_driver_open_core(unsigned core_num)
return NULL; return NULL;
} }
sprintf(msr, "/dev/cpu/%u/msr", core_num); sprintf(msr, "/dev/cpu/%u/msr", core_num);
if(!load_driver(msr)) {
set_error(ERR_NO_DRIVER);
return NULL;
}
int fd = open(msr, O_RDONLY); int fd = open(msr, O_RDONLY);
if (fd < 0) { if (fd < 0) {
if (errno == EIO) { if (errno == EIO) {
@ -107,6 +126,21 @@ int cpu_msr_driver_close(struct msr_driver_t* drv)
struct msr_driver_t { int fd; }; struct msr_driver_t { int fd; };
static int rdmsr_supported(void); static int rdmsr_supported(void);
static int load_driver(char *msr_path)
{
const int file_exists = !access(msr_path, F_OK);
const int file_readable = !access(msr_path, R_OK);
if (file_exists && file_readable)
return 1;
else if (file_exists && !file_readable)
return 0;
else if (getuid() != 0)
return 0;
else
return !system("kldload -n cpuctl 2> /dev/null");
}
struct msr_driver_t* cpu_msr_driver_open(void) struct msr_driver_t* cpu_msr_driver_open(void)
{ {
return cpu_msr_driver_open_core(0); return cpu_msr_driver_open_core(0);
@ -125,6 +159,10 @@ struct msr_driver_t* cpu_msr_driver_open_core(unsigned core_num)
return NULL; return NULL;
} }
sprintf(msr, "/dev/cpuctl%u", core_num); sprintf(msr, "/dev/cpuctl%u", core_num);
if(!load_driver(msr)) {
set_error(ERR_NO_DRIVER);
return NULL;
}
int fd = open(msr, O_RDONLY); int fd = open(msr, O_RDONLY);
if (fd < 0) { if (fd < 0) {
if (errno == EIO) { if (errno == EIO) {