correctly mark code segments as code in SELF
[coreboot.git] / util / msrtool / freebsd.c
1 /*
2  * This file is part of msrtool.
3  *
4  * Copyright (c) 2009 Andriy Gapon <avg@icyb.net.ua>
5  * Copyright (c) 2009 Peter Stuge <peter@stuge.se>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "msrtool.h"
29
30 static int msr_fd[MAX_CORES] = {-1, -1, -1, -1, -1, -1, -1, -1};
31
32 int freebsd_probe(const struct sysdef *system)
33 {
34 #ifdef __FreeBSD__
35         struct stat st;
36
37         return stat("/dev/cpuctl0", &st) == 0;
38 #else
39         return 0;
40 #endif
41 }
42
43 int freebsd_open(uint8_t cpu, enum SysModes mode)
44 {
45 #ifdef __FreeBSD__
46         int flags;
47         char devname[32];
48
49         if (cpu >= MAX_CORES)
50                 return 0;
51
52         if (SYS_RDWR == mode)
53                 flags = O_RDWR;
54         else if (SYS_WRONLY == mode)
55                 flags = O_WRONLY;
56         else
57                 flags = O_RDONLY;
58
59         snprintf(devname, sizeof(devname), "/dev/cpuctl%u", cpu);
60         msr_fd[cpu] = open(devname, flags);
61         if (msr_fd[cpu] < 0) {
62                 perror(devname);
63                 return 0;
64         }
65         return 1;
66 #else
67         return 0;
68 #endif
69 }
70
71 int freebsd_close(uint8_t cpu)
72 {
73         if (cpu >= MAX_CORES)
74                 return 0;
75
76         if (msr_fd[cpu] != -1)
77                 close(msr_fd[cpu]);
78         msr_fd[cpu] = -1;
79         return 1;
80 }
81
82 int freebsd_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val)
83 {
84 #ifdef __FreeBSD__
85         cpuctl_msr_args_t args;
86
87         if (cpu >= MAX_CORES)
88                 return 0;
89
90         if (msr_fd[cpu] < 0)
91                 return 0;
92
93         args.msr = addr;
94         if (ioctl(msr_fd[cpu], CPUCTL_RDMSR, &args) < 0) {
95                 perror("ioctl(CPUCTL_RDMSR)");
96                 return 0;
97         }
98
99         val->hi = args.data >> 32;
100         val->lo = args.data & 0xffffffff;
101         return 1;
102 #else
103         return 0;
104 #endif
105 }