1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#define _LARGEFILE64_SOURCE
#include <sys/types.h>
#include <sys/ioctl.h>
#ifdef __OpenBSD__
#include <machine/amdmsr.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#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) {
#ifdef X_PRIVSEP
msrfd = priv_open_device(_PATH_MSRDEV);
#else
msrfd = open(_PATH_MSRDEV, O_RDWR);
#endif
if (msrfd == -1)
FatalError("Unable to open %s: %s\n", _PATH_MSRDEV,
strerror(errno));
}
return msrfd;
}
int
GeodeReadMSR(unsigned long addr, unsigned long *lo, unsigned long *hi)
{
#ifdef __OpenBSD__
struct amdmsr_req req;
#endif
int fd = _msr_open();
#ifdef __OpenBSD__
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;
if (fd == -1)
return -1;
ret = lseek64(fd, (off64_t) addr, SEEK_SET);
if (ret == -1)
return -1;
ret = read(fd, (void *)data, sizeof(data));
if (ret != 8)
return -1;
*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;
#endif
int fd = _msr_open();
#ifdef __OpenBSD__
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();
if (fd == -1)
return -1;
if (lseek64(fd, (off64_t) addr, SEEK_SET) == -1)
return -1;
data[0] = lo;
data[1] = hi;
if (write(fd, (void *)data, 8) != 8)
return -1;
#endif
return 0;
}
|