Andriy Gapon | d80e57c | 2009-11-28 05:21:42 +0000 | [diff] [blame] | 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. |
Andriy Gapon | d80e57c | 2009-11-28 05:21:42 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | |
| 24 | #include "msrtool.h" |
| 25 | |
| 26 | static int msr_fd[MAX_CORES] = {-1, -1, -1, -1, -1, -1, -1, -1}; |
| 27 | |
| 28 | int freebsd_probe(const struct sysdef *system) |
| 29 | { |
| 30 | #ifdef __FreeBSD__ |
| 31 | struct stat st; |
| 32 | |
| 33 | return stat("/dev/cpuctl0", &st) == 0; |
| 34 | #else |
| 35 | return 0; |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | int freebsd_open(uint8_t cpu, enum SysModes mode) |
| 40 | { |
| 41 | #ifdef __FreeBSD__ |
| 42 | int flags; |
| 43 | char devname[32]; |
| 44 | |
| 45 | if (cpu >= MAX_CORES) |
| 46 | return 0; |
| 47 | |
| 48 | if (SYS_RDWR == mode) |
| 49 | flags = O_RDWR; |
| 50 | else if (SYS_WRONLY == mode) |
| 51 | flags = O_WRONLY; |
| 52 | else |
| 53 | flags = O_RDONLY; |
| 54 | |
| 55 | snprintf(devname, sizeof(devname), "/dev/cpuctl%u", cpu); |
| 56 | msr_fd[cpu] = open(devname, flags); |
| 57 | if (msr_fd[cpu] < 0) { |
| 58 | perror(devname); |
| 59 | return 0; |
| 60 | } |
| 61 | return 1; |
| 62 | #else |
| 63 | return 0; |
| 64 | #endif |
| 65 | } |
| 66 | |
| 67 | int freebsd_close(uint8_t cpu) |
| 68 | { |
| 69 | if (cpu >= MAX_CORES) |
| 70 | return 0; |
| 71 | |
| 72 | if (msr_fd[cpu] != -1) |
| 73 | close(msr_fd[cpu]); |
| 74 | msr_fd[cpu] = -1; |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | int freebsd_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val) |
| 79 | { |
| 80 | #ifdef __FreeBSD__ |
| 81 | cpuctl_msr_args_t args; |
| 82 | |
| 83 | if (cpu >= MAX_CORES) |
| 84 | return 0; |
| 85 | |
| 86 | if (msr_fd[cpu] < 0) |
| 87 | return 0; |
| 88 | |
| 89 | args.msr = addr; |
| 90 | if (ioctl(msr_fd[cpu], CPUCTL_RDMSR, &args) < 0) { |
| 91 | perror("ioctl(CPUCTL_RDMSR)"); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | val->hi = args.data >> 32; |
| 96 | val->lo = args.data & 0xffffffff; |
| 97 | return 1; |
| 98 | #else |
| 99 | return 0; |
| 100 | #endif |
| 101 | } |