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