Peter Stuge | 33fefca | 2009-01-26 01:33:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Peter Stuge <peter@stuge.se> |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009 coresystems GmbH |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 6 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 7 | * Copyright (C) 2010 Rudolf Marek <r.marek@assembler.cz> |
Peter Stuge | 33fefca | 2009-01-26 01:33:02 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 22 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 24 | #include <stdbool.h> |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 27 | #include <string.h> |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 28 | #include <errno.h> |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 29 | #include "flash.h" |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 30 | #include "programmer.h" |
Patrick Georgi | 32508eb | 2012-07-20 20:35:14 +0000 | [diff] [blame] | 31 | #include "hwaccess.h" |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 32 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 33 | #if !defined(__DJGPP__) && !defined(__LIBPAYLOAD__) |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 34 | /* No file access needed/possible to get mmap access permissions or access MSR. */ |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 35 | #include <sys/stat.h> |
| 36 | #include <fcntl.h> |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 39 | #ifdef __DJGPP__ |
| 40 | #include <dpmi.h> |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 41 | #include <malloc.h> |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 42 | #include <sys/nearptr.h> |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 43 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 44 | #define ONE_MEGABYTE (1024 * 1024) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 45 | #define MEM_DEV "dpmi" |
| 46 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 47 | static void *realmem_map_aligned; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 48 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 49 | static void *map_first_meg(uintptr_t phys_addr, size_t len) |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 50 | { |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 51 | void *realmem_map; |
| 52 | size_t pagesize; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 53 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 54 | if (realmem_map_aligned) |
| 55 | return realmem_map_aligned + phys_addr; |
| 56 | |
| 57 | /* valloc() from DJGPP 2.05 does not work properly */ |
| 58 | pagesize = getpagesize(); |
| 59 | |
| 60 | realmem_map = malloc(ONE_MEGABYTE + pagesize); |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 61 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 62 | if (!realmem_map) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 63 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 64 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 65 | realmem_map_aligned = (void *)(((size_t) realmem_map + |
| 66 | (pagesize - 1)) & ~(pagesize - 1)); |
| 67 | |
| 68 | if (__djgpp_map_physical_memory(realmem_map_aligned, ONE_MEGABYTE, 0)) { |
Carl-Daniel Hailfinger | 602de98 | 2010-10-05 23:21:51 +0000 | [diff] [blame] | 69 | free(realmem_map); |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 70 | realmem_map_aligned = NULL; |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 71 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 74 | return realmem_map_aligned + phys_addr; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 75 | } |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 76 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 77 | static void *sys_physmap(uintptr_t phys_addr, size_t len) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 78 | { |
| 79 | int ret; |
| 80 | __dpmi_meminfo mi; |
| 81 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 82 | /* Enable 4GB limit on DS descriptor. */ |
| 83 | if (!__djgpp_nearptr_enable()) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 84 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 85 | |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 86 | if ((phys_addr + len - 1) < ONE_MEGABYTE) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 87 | /* We need to use another method to map first 1MB. */ |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 88 | return map_first_meg(phys_addr, len); |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | mi.address = phys_addr; |
| 92 | mi.size = len; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 93 | ret = __dpmi_physical_address_mapping(&mi); |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 94 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 95 | if (ret != 0) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 96 | return ERROR_PTR; |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 97 | |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 98 | return (void *) mi.address + __djgpp_conventional_base; |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | #define sys_physmap_rw_uncached sys_physmap |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 102 | #define sys_physmap_ro_cached sys_physmap |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 103 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 104 | void sys_physunmap_unaligned(void *virt_addr, size_t len) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 105 | { |
| 106 | __dpmi_meminfo mi; |
| 107 | |
Carl-Daniel Hailfinger | 602de98 | 2010-10-05 23:21:51 +0000 | [diff] [blame] | 108 | /* There is no known way to unmap the first 1 MB. The DPMI server will |
| 109 | * do this for us on exit. |
| 110 | */ |
Rudolf Marek | 25fde40 | 2017-12-29 16:30:49 +0100 | [diff] [blame] | 111 | if ((virt_addr >= realmem_map_aligned) && |
| 112 | ((virt_addr + len) <= (realmem_map_aligned + ONE_MEGABYTE))) { |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
| 116 | mi.address = (unsigned long) virt_addr; |
| 117 | __dpmi_free_physical_address_mapping(&mi); |
| 118 | } |
| 119 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 120 | #elif defined(__LIBPAYLOAD__) |
| 121 | #include <arch/virtual.h> |
| 122 | |
| 123 | #define MEM_DEV "" |
| 124 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 125 | void *sys_physmap(uintptr_t phys_addr, size_t len) |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 126 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 127 | return (void *)phys_to_virt(phys_addr); |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | #define sys_physmap_rw_uncached sys_physmap |
| 131 | #define sys_physmap_ro_cached sys_physmap |
| 132 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 133 | void sys_physunmap_unaligned(void *virt_addr, size_t len) |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 134 | { |
| 135 | } |
Carl-Daniel Hailfinger | 60d9bd2 | 2012-08-09 23:34:41 +0000 | [diff] [blame] | 136 | #elif defined(__MACH__) && defined(__APPLE__) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 137 | |
Stefan Reinauer | 83704c5 | 2011-03-18 22:00:15 +0000 | [diff] [blame] | 138 | #define MEM_DEV "DirectHW" |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 139 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 140 | static void *sys_physmap(uintptr_t phys_addr, size_t len) |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 141 | { |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 142 | /* The short form of ?: is a GNU extension. |
| 143 | * FIXME: map_physical returns NULL both for errors and for success |
| 144 | * if the region is mapped at virtual address zero. If in doubt, report |
| 145 | * an error until a better interface exists. |
| 146 | */ |
| 147 | return map_physical(phys_addr, len) ? : ERROR_PTR; |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 150 | /* The OS X driver does not differentiate between mapping types. */ |
| 151 | #define sys_physmap_rw_uncached sys_physmap |
| 152 | #define sys_physmap_ro_cached sys_physmap |
| 153 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 154 | void sys_physunmap_unaligned(void *virt_addr, size_t len) |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 155 | { |
| 156 | unmap_physical(virt_addr, len); |
| 157 | } |
| 158 | |
| 159 | #else |
| 160 | #include <sys/mman.h> |
| 161 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 162 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 163 | # define MEM_DEV "/dev/xsvc" |
| 164 | #else |
| 165 | # define MEM_DEV "/dev/mem" |
| 166 | #endif |
| 167 | |
| 168 | static int fd_mem = -1; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 169 | static int fd_mem_cached = -1; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 170 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 171 | /* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */ |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 172 | static void *sys_physmap_rw_uncached(uintptr_t phys_addr, size_t len) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 173 | { |
| 174 | void *virt_addr; |
| 175 | |
| 176 | if (-1 == fd_mem) { |
| 177 | /* Open the memory device UNCACHED. Important for MMIO. */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 178 | if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 179 | msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno)); |
Niklas Söderlund | 2d8b7ef | 2013-09-13 19:19:25 +0000 | [diff] [blame] | 180 | return ERROR_PTR; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Niklas Söderlund | 2d8b7ef | 2013-09-13 19:19:25 +0000 | [diff] [blame] | 184 | virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fd_mem, (off_t)phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 185 | return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 188 | /* For reading DMI/coreboot/whatever tables. We should never write, and we |
| 189 | * do not care about caching. |
| 190 | */ |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 191 | static void *sys_physmap_ro_cached(uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 192 | { |
| 193 | void *virt_addr; |
| 194 | |
| 195 | if (-1 == fd_mem_cached) { |
| 196 | /* Open the memory device CACHED. */ |
| 197 | if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 198 | msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno)); |
Niklas Söderlund | 2d8b7ef | 2013-09-13 19:19:25 +0000 | [diff] [blame] | 199 | return ERROR_PTR; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Niklas Söderlund | 2d8b7ef | 2013-09-13 19:19:25 +0000 | [diff] [blame] | 203 | virt_addr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd_mem_cached, (off_t)phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 204 | return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 207 | void sys_physunmap_unaligned(void *virt_addr, size_t len) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 208 | { |
| 209 | munmap(virt_addr, len); |
| 210 | } |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 211 | #endif |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 212 | |
Stefan Tauner | 3285d7f | 2013-08-14 16:28:19 +0000 | [diff] [blame] | 213 | #define PHYSM_RW 0 |
| 214 | #define PHYSM_RO 1 |
| 215 | #define PHYSM_NOCLEANUP 0 |
| 216 | #define PHYSM_CLEANUP 1 |
| 217 | #define PHYSM_EXACT 0 |
| 218 | #define PHYSM_ROUND 1 |
| 219 | |
| 220 | /* Round start to nearest page boundary below and set len so that the resulting address range ends at the lowest |
| 221 | * possible page boundary where the original address range is still entirely contained. It returns the |
| 222 | * difference between the rounded start address and the original start address. */ |
| 223 | static uintptr_t round_to_page_boundaries(uintptr_t *start, size_t *len) |
| 224 | { |
| 225 | uintptr_t page_size = getpagesize(); |
| 226 | uintptr_t page_mask = ~(page_size-1); |
| 227 | uintptr_t end = *start + *len; |
| 228 | uintptr_t old_start = *start; |
| 229 | msg_gspew("page_size=%" PRIxPTR "\n", page_size); |
| 230 | msg_gspew("pre-rounding: start=0x%0*" PRIxPTR ", len=0x%zx, end=0x%0*" PRIxPTR "\n", |
| 231 | PRIxPTR_WIDTH, *start, *len, PRIxPTR_WIDTH, end); |
| 232 | *start = *start & page_mask; |
| 233 | end = (end + page_size - 1) & page_mask; |
| 234 | *len = end - *start; |
| 235 | msg_gspew("post-rounding: start=0x%0*" PRIxPTR ", len=0x%zx, end=0x%0*" PRIxPTR "\n", |
| 236 | PRIxPTR_WIDTH, *start, *len, PRIxPTR_WIDTH, *start + *len); |
| 237 | return old_start - *start; |
| 238 | } |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 239 | |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 240 | struct undo_physmap_data { |
| 241 | void *virt_addr; |
| 242 | size_t len; |
| 243 | }; |
| 244 | |
| 245 | static int undo_physmap(void *data) |
| 246 | { |
| 247 | if (data == NULL) { |
| 248 | msg_perr("%s: tried to physunmap without valid data!\n", __func__); |
| 249 | return 1; |
| 250 | } |
| 251 | struct undo_physmap_data *d = data; |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 252 | physunmap_unaligned(d->virt_addr, d->len); |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 253 | free(data); |
| 254 | return 0; |
| 255 | } |
| 256 | |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 257 | static void *physmap_common(const char *descr, uintptr_t phys_addr, size_t len, bool readonly, bool autocleanup, |
| 258 | bool round) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 259 | { |
Stephan Guilloux | 6d08a3e | 2009-06-23 10:44:36 +0000 | [diff] [blame] | 260 | void *virt_addr; |
Stefan Tauner | 3285d7f | 2013-08-14 16:28:19 +0000 | [diff] [blame] | 261 | uintptr_t offset = 0; |
Stephan Guilloux | 6d08a3e | 2009-06-23 10:44:36 +0000 | [diff] [blame] | 262 | |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 263 | if (len == 0) { |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 264 | msg_pspew("Not mapping %s, zero size at 0x%0*" PRIxPTR ".\n", descr, PRIxPTR_WIDTH, phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 265 | return ERROR_PTR; |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 266 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 267 | |
Stefan Tauner | 3285d7f | 2013-08-14 16:28:19 +0000 | [diff] [blame] | 268 | if (round) |
| 269 | offset = round_to_page_boundaries(&phys_addr, &len); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 270 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 271 | if (readonly) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 272 | virt_addr = sys_physmap_ro_cached(phys_addr, len); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 273 | else |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 274 | virt_addr = sys_physmap_rw_uncached(phys_addr, len); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 275 | |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 276 | if (ERROR_PTR == virt_addr) { |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 277 | if (NULL == descr) |
| 278 | descr = "memory"; |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 279 | msg_perr("Error accessing %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n", |
| 280 | descr, len, PRIxPTR_WIDTH, phys_addr); |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 281 | msg_perr(MEM_DEV " mmap failed: %s\n", strerror(errno)); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 282 | #ifdef __linux__ |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 283 | if (EINVAL == errno) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 284 | msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); |
| 285 | msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); |
| 286 | msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n"); |
| 287 | msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); |
| 288 | msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n"); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 289 | } |
Carl-Daniel Hailfinger | b63b067 | 2010-07-02 17:12:50 +0000 | [diff] [blame] | 290 | #elif defined (__OpenBSD__) |
| 291 | msg_perr("Please set securelevel=-1 in /etc/rc.securelevel " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 292 | "and reboot, or reboot into\n" |
| 293 | "single user mode.\n"); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 294 | #endif |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 295 | return ERROR_PTR; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 298 | if (autocleanup) { |
| 299 | struct undo_physmap_data *d = malloc(sizeof(struct undo_physmap_data)); |
| 300 | if (d == NULL) { |
| 301 | msg_perr("%s: Out of memory!\n", __func__); |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 302 | physunmap_unaligned(virt_addr, len); |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 303 | return ERROR_PTR; |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | d->virt_addr = virt_addr; |
| 307 | d->len = len; |
| 308 | if (register_shutdown(undo_physmap, d) != 0) { |
| 309 | msg_perr("%s: Could not register shutdown function!\n", __func__); |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 310 | physunmap_unaligned(virt_addr, len); |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 311 | return ERROR_PTR; |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Stefan Tauner | 3285d7f | 2013-08-14 16:28:19 +0000 | [diff] [blame] | 315 | return virt_addr + offset; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 316 | } |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 317 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 318 | void physunmap_unaligned(void *virt_addr, size_t len) |
| 319 | { |
| 320 | /* No need to check for zero size, such mappings would have yielded ERROR_PTR. */ |
| 321 | if (virt_addr == ERROR_PTR) { |
| 322 | msg_perr("Trying to unmap a nonexisting mapping!\n" |
| 323 | "Please report a bug at flashrom@flashrom.org\n"); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | sys_physunmap_unaligned(virt_addr, len); |
| 328 | } |
| 329 | |
| 330 | void physunmap(void *virt_addr, size_t len) |
| 331 | { |
| 332 | uintptr_t tmp; |
| 333 | |
| 334 | /* No need to check for zero size, such mappings would have yielded ERROR_PTR. */ |
| 335 | if (virt_addr == ERROR_PTR) { |
| 336 | msg_perr("Trying to unmap a nonexisting mapping!\n" |
| 337 | "Please report a bug at flashrom@flashrom.org\n"); |
| 338 | return; |
| 339 | } |
| 340 | tmp = (uintptr_t)virt_addr; |
| 341 | /* We assume that the virtual address of a page-aligned physical address is page-aligned as well. By |
| 342 | * extension, rounding a virtual unaligned address as returned by physmap should yield the same offset |
| 343 | * between rounded and original virtual address as between rounded and original physical address. |
| 344 | */ |
| 345 | round_to_page_boundaries(&tmp, &len); |
| 346 | virt_addr = (void *)tmp; |
| 347 | physunmap_unaligned(virt_addr, len); |
| 348 | } |
| 349 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 350 | void *physmap(const char *descr, uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 351 | { |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 352 | return physmap_common(descr, phys_addr, len, PHYSM_RW, PHYSM_NOCLEANUP, PHYSM_ROUND); |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void *rphysmap(const char *descr, uintptr_t phys_addr, size_t len) |
| 356 | { |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 357 | return physmap_common(descr, phys_addr, len, PHYSM_RW, PHYSM_CLEANUP, PHYSM_ROUND); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 360 | void *physmap_ro(const char *descr, uintptr_t phys_addr, size_t len) |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 361 | { |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 362 | return physmap_common(descr, phys_addr, len, PHYSM_RO, PHYSM_NOCLEANUP, PHYSM_ROUND); |
Sean Nelson | 4c6d3a4 | 2013-09-11 23:35:03 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Carl-Daniel Hailfinger | 43eac03 | 2014-03-05 00:16:16 +0000 | [diff] [blame] | 365 | void *physmap_ro_unaligned(const char *descr, uintptr_t phys_addr, size_t len) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 366 | { |
Niklas Söderlund | 5d30720 | 2013-09-14 09:02:27 +0000 | [diff] [blame] | 367 | return physmap_common(descr, phys_addr, len, PHYSM_RO, PHYSM_NOCLEANUP, PHYSM_EXACT); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 370 | /* MSR abstraction implementations for Linux, OpenBSD, FreeBSD/Dragonfly, OSX, libpayload |
| 371 | * and a non-working default implemenation on the bottom. See also hwaccess.h for some (re)declarations. */ |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 372 | #if defined(__i386__) || defined(__x86_64__) |
| 373 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 374 | #ifdef __linux__ |
| 375 | /* |
| 376 | * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr, |
| 377 | * which are ring0 privileged instructions so only the kernel can do the |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 378 | * read/write. This function, therefore, requires that the msr kernel module |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 379 | * be loaded to access these instructions from user space using device |
| 380 | * /dev/cpu/0/msr. |
| 381 | */ |
| 382 | |
| 383 | static int fd_msr = -1; |
| 384 | |
| 385 | msr_t rdmsr(int addr) |
| 386 | { |
Stefan Reinauer | 95e892b | 2009-09-04 13:57:07 +0000 | [diff] [blame] | 387 | uint32_t buf[2]; |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 388 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 389 | |
| 390 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 391 | msg_perr("Could not lseek() MSR: %s\n", strerror(errno)); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 392 | close(fd_msr); |
| 393 | exit(1); |
| 394 | } |
| 395 | |
| 396 | if (read(fd_msr, buf, 8) == 8) { |
Stefan Reinauer | 95e892b | 2009-09-04 13:57:07 +0000 | [diff] [blame] | 397 | msr.lo = buf[0]; |
| 398 | msr.hi = buf[1]; |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 399 | return msr; |
| 400 | } |
| 401 | |
| 402 | if (errno != EIO) { |
| 403 | // A severe error. |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 404 | msg_perr("Could not read() MSR: %s\n", strerror(errno)); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 405 | close(fd_msr); |
| 406 | exit(1); |
| 407 | } |
| 408 | |
| 409 | return msr; |
| 410 | } |
| 411 | |
| 412 | int wrmsr(int addr, msr_t msr) |
| 413 | { |
Anti Sullin | be24d81 | 2010-05-17 23:19:22 +0000 | [diff] [blame] | 414 | uint32_t buf[2]; |
| 415 | buf[0] = msr.lo; |
| 416 | buf[1] = msr.hi; |
| 417 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 418 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 419 | msg_perr("Could not lseek() MSR: %s\n", strerror(errno)); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 420 | close(fd_msr); |
| 421 | exit(1); |
| 422 | } |
| 423 | |
Anti Sullin | be24d81 | 2010-05-17 23:19:22 +0000 | [diff] [blame] | 424 | if (write(fd_msr, buf, 8) != 8 && errno != EIO) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 425 | msg_perr("Could not write() MSR: %s\n", strerror(errno)); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 426 | close(fd_msr); |
| 427 | exit(1); |
| 428 | } |
| 429 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 430 | /* Some MSRs must not be written. */ |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 431 | if (errno == EIO) |
| 432 | return -1; |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | int setup_cpu_msr(int cpu) |
| 438 | { |
| 439 | char msrfilename[64]; |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 440 | memset(msrfilename, 0, sizeof(msrfilename)); |
| 441 | snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 442 | |
| 443 | if (fd_msr != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 444 | msg_pinfo("MSR was already initialized\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 445 | return -1; |
| 446 | } |
| 447 | |
| 448 | fd_msr = open(msrfilename, O_RDWR); |
| 449 | |
| 450 | if (fd_msr < 0) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 451 | msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno)); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 452 | msg_pinfo("Did you run 'modprobe msr'?\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 453 | return -1; |
| 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | void cleanup_cpu_msr(void) |
| 460 | { |
| 461 | if (fd_msr == -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 462 | msg_pinfo("No MSR initialized.\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
| 466 | close(fd_msr); |
| 467 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 468 | /* Clear MSR file descriptor. */ |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 469 | fd_msr = -1; |
| 470 | } |
Stefan Tauner | e5dcc15 | 2016-02-07 03:35:04 +0000 | [diff] [blame] | 471 | #elif defined(__OpenBSD__) && defined (__i386__) /* This does only work for certain AMD Geode LX systems see amdmsr(4). */ |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 472 | #include <sys/ioctl.h> |
| 473 | #include <machine/amdmsr.h> |
| 474 | |
| 475 | static int fd_msr = -1; |
| 476 | |
| 477 | msr_t rdmsr(int addr) |
| 478 | { |
| 479 | struct amdmsr_req args; |
| 480 | |
| 481 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 482 | |
| 483 | args.addr = (uint32_t)addr; |
| 484 | |
| 485 | if (ioctl(fd_msr, RDMSR, &args) < 0) { |
| 486 | msg_perr("Error while executing RDMSR ioctl: %s\n", strerror(errno)); |
| 487 | close(fd_msr); |
| 488 | exit(1); |
| 489 | } |
| 490 | |
| 491 | msr.lo = args.val & 0xffffffff; |
| 492 | msr.hi = args.val >> 32; |
| 493 | |
| 494 | return msr; |
| 495 | } |
| 496 | |
| 497 | int wrmsr(int addr, msr_t msr) |
| 498 | { |
| 499 | struct amdmsr_req args; |
| 500 | |
| 501 | args.addr = addr; |
| 502 | args.val = (((uint64_t)msr.hi) << 32) | msr.lo; |
| 503 | |
| 504 | if (ioctl(fd_msr, WRMSR, &args) < 0) { |
| 505 | msg_perr("Error while executing WRMSR ioctl: %s\n", strerror(errno)); |
| 506 | close(fd_msr); |
| 507 | exit(1); |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | int setup_cpu_msr(int cpu) |
| 514 | { |
| 515 | char msrfilename[64]; |
| 516 | memset(msrfilename, 0, sizeof(msrfilename)); |
| 517 | snprintf(msrfilename, sizeof(msrfilename), "/dev/amdmsr"); |
| 518 | |
| 519 | if (fd_msr != -1) { |
| 520 | msg_pinfo("MSR was already initialized\n"); |
| 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | fd_msr = open(msrfilename, O_RDWR); |
| 525 | |
| 526 | if (fd_msr < 0) { |
| 527 | msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno)); |
| 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | void cleanup_cpu_msr(void) |
| 535 | { |
| 536 | if (fd_msr == -1) { |
| 537 | msg_pinfo("No MSR initialized.\n"); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | close(fd_msr); |
| 542 | |
| 543 | /* Clear MSR file descriptor. */ |
| 544 | fd_msr = -1; |
| 545 | } |
| 546 | |
| 547 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 548 | #include <sys/ioctl.h> |
| 549 | |
| 550 | typedef struct { |
| 551 | int msr; |
| 552 | uint64_t data; |
| 553 | } cpu_msr_args_t; |
| 554 | #define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t) |
| 555 | #define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t) |
| 556 | |
| 557 | static int fd_msr = -1; |
| 558 | |
| 559 | msr_t rdmsr(int addr) |
| 560 | { |
| 561 | cpu_msr_args_t args; |
| 562 | |
| 563 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 564 | |
| 565 | args.msr = addr; |
| 566 | |
| 567 | if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 568 | msg_perr("Error while executing CPU_RDMSR ioctl: %s\n", strerror(errno)); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 569 | close(fd_msr); |
| 570 | exit(1); |
| 571 | } |
| 572 | |
| 573 | msr.lo = args.data & 0xffffffff; |
| 574 | msr.hi = args.data >> 32; |
| 575 | |
| 576 | return msr; |
| 577 | } |
| 578 | |
| 579 | int wrmsr(int addr, msr_t msr) |
| 580 | { |
| 581 | cpu_msr_args_t args; |
| 582 | |
| 583 | args.msr = addr; |
| 584 | args.data = (((uint64_t)msr.hi) << 32) | msr.lo; |
| 585 | |
| 586 | if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 587 | msg_perr("Error while executing CPU_WRMSR ioctl: %s\n", strerror(errno)); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 588 | close(fd_msr); |
| 589 | exit(1); |
| 590 | } |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | int setup_cpu_msr(int cpu) |
| 596 | { |
| 597 | char msrfilename[64]; |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 598 | memset(msrfilename, 0, sizeof(msrfilename)); |
| 599 | snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 600 | |
| 601 | if (fd_msr != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 602 | msg_pinfo("MSR was already initialized\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 603 | return -1; |
| 604 | } |
| 605 | |
| 606 | fd_msr = open(msrfilename, O_RDWR); |
| 607 | |
| 608 | if (fd_msr < 0) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 609 | msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno)); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 610 | msg_pinfo("Did you install ports/sysutils/devcpu?\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 611 | return -1; |
| 612 | } |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | void cleanup_cpu_msr(void) |
| 618 | { |
| 619 | if (fd_msr == -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 620 | msg_pinfo("No MSR initialized.\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 621 | return; |
| 622 | } |
| 623 | |
| 624 | close(fd_msr); |
| 625 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 626 | /* Clear MSR file descriptor. */ |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 627 | fd_msr = -1; |
| 628 | } |
| 629 | |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 630 | #elif defined(__MACH__) && defined(__APPLE__) |
| 631 | /* rdmsr() and wrmsr() are provided by DirectHW which needs neither setup nor cleanup. */ |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 632 | int setup_cpu_msr(int cpu) |
| 633 | { |
| 634 | // Always succeed for now |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | void cleanup_cpu_msr(void) |
| 639 | { |
| 640 | // Nothing, yet. |
| 641 | } |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 642 | #elif defined(__LIBPAYLOAD__) |
| 643 | msr_t libpayload_rdmsr(int addr) |
| 644 | { |
| 645 | msr_t msr; |
| 646 | unsigned long long val = _rdmsr(addr); |
| 647 | msr.lo = val & 0xffffffff; |
| 648 | msr.hi = val >> 32; |
| 649 | return msr; |
| 650 | } |
| 651 | |
| 652 | int libpayload_wrmsr(int addr, msr_t msr) |
| 653 | { |
| 654 | _wrmsr(addr, msr.lo | ((unsigned long long)msr.hi << 32)); |
Patrick Georgi | 97bc95c | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 655 | return 0; |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 656 | } |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 657 | |
| 658 | int setup_cpu_msr(int cpu) |
| 659 | { |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | void cleanup_cpu_msr(void) |
| 664 | { |
| 665 | } |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 666 | #else |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 667 | /* default MSR implementation */ |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 668 | msr_t rdmsr(int addr) |
| 669 | { |
| 670 | msr_t ret = { 0xffffffff, 0xffffffff }; |
| 671 | |
| 672 | return ret; |
| 673 | } |
| 674 | |
| 675 | int wrmsr(int addr, msr_t msr) |
| 676 | { |
| 677 | return -1; |
| 678 | } |
| 679 | |
| 680 | int setup_cpu_msr(int cpu) |
| 681 | { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 682 | msg_pinfo("No MSR support for your OS yet.\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 683 | return -1; |
| 684 | } |
| 685 | |
| 686 | void cleanup_cpu_msr(void) |
| 687 | { |
| 688 | // Nothing, yet. |
| 689 | } |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 690 | #endif // OS switches for MSR code |
| 691 | #else // x86 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 692 | /* Does MSR exist on non-x86 architectures? */ |
Stefan Tauner | 6bf5fe7 | 2013-07-13 21:18:32 +0000 | [diff] [blame] | 693 | #endif // arch switches for MSR code |