summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/geode_msr.c50
1 files changed, 46 insertions, 4 deletions
diff --git a/src/geode_msr.c b/src/geode_msr.c
index b7c56d9..8f57939 100644
--- a/src/geode_msr.c
+++ b/src/geode_msr.c
@@ -33,18 +33,36 @@
#include <fcntl.h>
#include <sys/types.h>
#include <errno.h>
+
+#ifdef __OpenBSD__
+#include <sys/ioctl.h>
+#include <machine/amdmsr.h>
+#endif
+
#include "os.h"
#include "geode.h"
+#ifdef __OpenBSD__
+#define _PATH_MSRDEV "/dev/amdmsr"
+#define X_PRIVSEP
+#else
+#define _PATH_MSRDEV "/dev/cpu/0/msr"
+#endif
+
static int
_msr_open(void)
{
static int msrfd = 0;
if (msrfd == 0) {
- msrfd = open("/dev/cpu/0/msr", O_RDWR);
+#ifdef X_PRIVSEP
+ msrfd = priv_open_device(_PATH_MSRDEV);
+#else
+ msrfd = open(_PATH_MSRDEV, O_RDWR);
+#endif
if (msrfd == -1)
- ErrorF("Unable to open /dev/cpu/0/msr: %d\n", errno);
+ FatalError("Unable to open %s: %s\n", _PATH_MSRDEV,
+ strerror(errno));
}
return msrfd;
@@ -53,6 +71,19 @@ _msr_open(void)
int
GeodeReadMSR(unsigned long addr, unsigned long *lo, unsigned long *hi)
{
+#ifdef __OpenBSD__
+ struct amdmsr_req req;
+ int fd = _msr_open();
+
+ req.addr = addr;
+
+ if (ioctl(fd, RDMSR, &req) == -1)
+ FatalError("Unable to read MSR at address %0x06x: %s\n", addr,
+ strerror(errno));
+
+ *hi = req.val >> 32;
+ *lo = req.val & 0xffffffff;
+#else
unsigned int data[2];
int fd = _msr_open();
int ret;
@@ -72,13 +103,24 @@ GeodeReadMSR(unsigned long addr, unsigned long *lo, unsigned long *hi)
*hi = data[1];
*lo = data[0];
-
+#endif
return 0;
}
int
GeodeWriteMSR(unsigned long addr, unsigned long lo, unsigned long hi)
{
+#ifdef __OpenBSD__
+ struct amdmsr_req req;
+ int fd = _msr_open();
+
+ req.addr = addr;
+ req.val = (u_int64_t) hi << 32 | (u_int64_t)lo;
+
+ if (ioctl(fd, WRMSR, &req) == -1)
+ FatalError("Unable to write MSR at address 0x%06x: %s\n", addr,
+ strerror(errno));
+#else
unsigned int data[2];
int fd = _msr_open();
@@ -93,6 +135,6 @@ GeodeWriteMSR(unsigned long addr, unsigned long lo, unsigned long hi)
if (write(fd, (void *) data, 8) != 8)
return -1;
-
+#endif
return 0;
}