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> |
| 24 | #include <stdio.h> |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 26 | #include <string.h> |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 27 | #include "flash.h" |
| 28 | |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 29 | /* Do we need any file access or ioctl for physmap or MSR? */ |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 30 | #if !defined(__DJGPP__) && !defined(__LIBPAYLOAD__) |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <errno.h> |
| 34 | #endif |
| 35 | |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 36 | #ifdef __DJGPP__ |
| 37 | #include <dpmi.h> |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 38 | #include <sys/nearptr.h> |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 39 | |
| 40 | #define MEM_DEV "dpmi" |
| 41 | |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 42 | static void *realmem_map; |
| 43 | |
| 44 | static void *map_first_meg(unsigned long phys_addr, size_t len) |
| 45 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 46 | if (realmem_map) |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 47 | return realmem_map + phys_addr; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 48 | |
| 49 | realmem_map = valloc(1024 * 1024); |
| 50 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 51 | if (!realmem_map) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 52 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 53 | |
| 54 | if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) { |
Carl-Daniel Hailfinger | 602de98 | 2010-10-05 23:21:51 +0000 | [diff] [blame] | 55 | free(realmem_map); |
| 56 | realmem_map = NULL; |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 57 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | return realmem_map + phys_addr; |
| 61 | } |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 62 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 63 | static void *sys_physmap(unsigned long phys_addr, size_t len) |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 64 | { |
| 65 | int ret; |
| 66 | __dpmi_meminfo mi; |
| 67 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 68 | /* Enable 4GB limit on DS descriptor. */ |
| 69 | if (!__djgpp_nearptr_enable()) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 70 | return ERROR_PTR; |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 71 | |
| 72 | if ((phys_addr + len - 1) < (1024 * 1024)) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 73 | /* We need to use another method to map first 1MB. */ |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 74 | return map_first_meg(phys_addr, len); |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | mi.address = phys_addr; |
| 78 | mi.size = len; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 79 | ret = __dpmi_physical_address_mapping(&mi); |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 80 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 81 | if (ret != 0) |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 82 | return ERROR_PTR; |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 83 | |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 84 | return (void *) mi.address + __djgpp_conventional_base; |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | #define sys_physmap_rw_uncached sys_physmap |
Rudolf Marek | 837d810 | 2010-04-25 22:47:50 +0000 | [diff] [blame] | 88 | #define sys_physmap_ro_cached sys_physmap |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 89 | |
| 90 | void physunmap(void *virt_addr, size_t len) |
| 91 | { |
| 92 | __dpmi_meminfo mi; |
| 93 | |
Carl-Daniel Hailfinger | 602de98 | 2010-10-05 23:21:51 +0000 | [diff] [blame] | 94 | /* There is no known way to unmap the first 1 MB. The DPMI server will |
| 95 | * do this for us on exit. |
| 96 | */ |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 97 | if ((virt_addr >= realmem_map) && |
| 98 | ((virt_addr + len) <= (realmem_map + (1024 * 1024)))) { |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
| 102 | mi.address = (unsigned long) virt_addr; |
| 103 | __dpmi_free_physical_address_mapping(&mi); |
| 104 | } |
| 105 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 106 | #elif defined(__LIBPAYLOAD__) |
| 107 | #include <arch/virtual.h> |
| 108 | |
| 109 | #define MEM_DEV "" |
| 110 | |
| 111 | void *sys_physmap(unsigned long phys_addr, size_t len) |
| 112 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 113 | return (void *)phys_to_virt(phys_addr); |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | #define sys_physmap_rw_uncached sys_physmap |
| 117 | #define sys_physmap_ro_cached sys_physmap |
| 118 | |
| 119 | void physunmap(void *virt_addr, size_t len) |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | int setup_cpu_msr(int cpu) |
| 124 | { |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | void cleanup_cpu_msr(void) |
| 129 | { |
| 130 | } |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 131 | #elif defined(__DARWIN__) |
| 132 | |
Stefan Reinauer | 83704c5 | 2011-03-18 22:00:15 +0000 | [diff] [blame] | 133 | #define MEM_DEV "DirectHW" |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 134 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 135 | static void *sys_physmap(unsigned long phys_addr, size_t len) |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 136 | { |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 137 | /* The short form of ?: is a GNU extension. |
| 138 | * FIXME: map_physical returns NULL both for errors and for success |
| 139 | * if the region is mapped at virtual address zero. If in doubt, report |
| 140 | * an error until a better interface exists. |
| 141 | */ |
| 142 | return map_physical(phys_addr, len) ? : ERROR_PTR; |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 145 | /* The OS X driver does not differentiate between mapping types. */ |
| 146 | #define sys_physmap_rw_uncached sys_physmap |
| 147 | #define sys_physmap_ro_cached sys_physmap |
| 148 | |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 149 | void physunmap(void *virt_addr, size_t len) |
| 150 | { |
| 151 | unmap_physical(virt_addr, len); |
| 152 | } |
| 153 | |
| 154 | #else |
| 155 | #include <sys/mman.h> |
| 156 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 157 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 158 | # define MEM_DEV "/dev/xsvc" |
| 159 | #else |
| 160 | # define MEM_DEV "/dev/mem" |
| 161 | #endif |
| 162 | |
| 163 | static int fd_mem = -1; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 164 | static int fd_mem_cached = -1; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 165 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 166 | /* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 167 | static void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 168 | { |
| 169 | void *virt_addr; |
| 170 | |
| 171 | if (-1 == fd_mem) { |
| 172 | /* Open the memory device UNCACHED. Important for MMIO. */ |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 173 | if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) { |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 174 | perror("Critical error: open(" MEM_DEV ")"); |
Peter Stuge | 43438ba | 2009-01-26 02:04:19 +0000 | [diff] [blame] | 175 | exit(2); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 179 | virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 180 | fd_mem, (off_t)phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 181 | return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr; |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 184 | /* For reading DMI/coreboot/whatever tables. We should never write, and we |
| 185 | * do not care about caching. |
| 186 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 187 | static void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 188 | { |
| 189 | void *virt_addr; |
| 190 | |
| 191 | if (-1 == fd_mem_cached) { |
| 192 | /* Open the memory device CACHED. */ |
| 193 | if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 194 | msg_perr("Critical error: open(" MEM_DEV "): %s", |
| 195 | strerror(errno)); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 196 | exit(2); |
| 197 | } |
| 198 | } |
| 199 | |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 200 | virt_addr = mmap(NULL, len, PROT_READ, MAP_SHARED, |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 201 | fd_mem_cached, (off_t)phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 202 | return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr; |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 205 | void physunmap(void *virt_addr, size_t len) |
| 206 | { |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 207 | if (len == 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 208 | msg_pspew("Not unmapping zero size at %p\n", virt_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 212 | munmap(virt_addr, len); |
| 213 | } |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 214 | #endif |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 215 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 216 | #define PHYSMAP_NOFAIL 0 |
| 217 | #define PHYSMAP_MAYFAIL 1 |
| 218 | #define PHYSMAP_RW 0 |
| 219 | #define PHYSMAP_RO 1 |
| 220 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 221 | static void *physmap_common(const char *descr, unsigned long phys_addr, |
| 222 | size_t len, int mayfail, int readonly) |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 223 | { |
Stephan Guilloux | 6d08a3e | 2009-06-23 10:44:36 +0000 | [diff] [blame] | 224 | void *virt_addr; |
| 225 | |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 226 | if (len == 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 227 | msg_pspew("Not mapping %s, zero size at 0x%08lx.\n", |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 228 | descr, phys_addr); |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 229 | return ERROR_PTR; |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 230 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 231 | |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 232 | if ((getpagesize() - 1) & len) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 233 | msg_perr("Mapping %s at 0x%08lx, unaligned size 0x%lx.\n", |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 234 | descr, phys_addr, (unsigned long)len); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if ((getpagesize() - 1) & phys_addr) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 238 | msg_perr("Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n", |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 239 | descr, (unsigned long)len, phys_addr); |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 242 | if (readonly) |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 243 | virt_addr = sys_physmap_ro_cached(phys_addr, len); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 244 | else |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 245 | virt_addr = sys_physmap_rw_uncached(phys_addr, len); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 246 | |
Patrick Georgi | ed7a964 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 247 | if (ERROR_PTR == virt_addr) { |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 248 | if (NULL == descr) |
| 249 | descr = "memory"; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 250 | msg_perr("Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, |
| 251 | (unsigned long)len, phys_addr); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 252 | perror(MEM_DEV " mmap failed"); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 253 | #ifdef __linux__ |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 254 | if (EINVAL == errno) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 255 | msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); |
| 256 | msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); |
| 257 | msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n"); |
| 258 | msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); |
| 259 | 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] | 260 | } |
Carl-Daniel Hailfinger | b63b067 | 2010-07-02 17:12:50 +0000 | [diff] [blame] | 261 | #elif defined (__OpenBSD__) |
| 262 | msg_perr("Please set securelevel=-1 in /etc/rc.securelevel " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 263 | "and reboot, or reboot into\n" |
| 264 | "single user mode.\n"); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 265 | #endif |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 266 | if (!mayfail) |
| 267 | exit(3); |
Stefan Reinauer | 0593f21 | 2009-01-26 01:10:48 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return virt_addr; |
| 271 | } |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 272 | |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 273 | void *physmap(const char *descr, unsigned long phys_addr, size_t len) |
| 274 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 275 | return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL, |
| 276 | PHYSMAP_RW); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len) |
| 280 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 281 | return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL, |
| 282 | PHYSMAP_RO); |
Carl-Daniel Hailfinger | baaffe0 | 2010-02-02 11:09:03 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 285 | #if defined(__i386__) || defined(__x86_64__) |
| 286 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 287 | #ifdef __linux__ |
| 288 | /* |
| 289 | * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr, |
| 290 | * which are ring0 privileged instructions so only the kernel can do the |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 291 | * read/write. This function, therefore, requires that the msr kernel module |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 292 | * be loaded to access these instructions from user space using device |
| 293 | * /dev/cpu/0/msr. |
| 294 | */ |
| 295 | |
| 296 | static int fd_msr = -1; |
| 297 | |
| 298 | msr_t rdmsr(int addr) |
| 299 | { |
Stefan Reinauer | 95e892b | 2009-09-04 13:57:07 +0000 | [diff] [blame] | 300 | uint32_t buf[2]; |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 301 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 302 | |
| 303 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
| 304 | perror("Could not lseek() to MSR"); |
| 305 | close(fd_msr); |
| 306 | exit(1); |
| 307 | } |
| 308 | |
| 309 | if (read(fd_msr, buf, 8) == 8) { |
Stefan Reinauer | 95e892b | 2009-09-04 13:57:07 +0000 | [diff] [blame] | 310 | msr.lo = buf[0]; |
| 311 | msr.hi = buf[1]; |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 312 | return msr; |
| 313 | } |
| 314 | |
| 315 | if (errno != EIO) { |
| 316 | // A severe error. |
| 317 | perror("Could not read() MSR"); |
| 318 | close(fd_msr); |
| 319 | exit(1); |
| 320 | } |
| 321 | |
| 322 | return msr; |
| 323 | } |
| 324 | |
| 325 | int wrmsr(int addr, msr_t msr) |
| 326 | { |
Anti Sullin | be24d81 | 2010-05-17 23:19:22 +0000 | [diff] [blame] | 327 | uint32_t buf[2]; |
| 328 | buf[0] = msr.lo; |
| 329 | buf[1] = msr.hi; |
| 330 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 331 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
| 332 | perror("Could not lseek() to MSR"); |
| 333 | close(fd_msr); |
| 334 | exit(1); |
| 335 | } |
| 336 | |
Anti Sullin | be24d81 | 2010-05-17 23:19:22 +0000 | [diff] [blame] | 337 | if (write(fd_msr, buf, 8) != 8 && errno != EIO) { |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 338 | perror("Could not write() MSR"); |
| 339 | close(fd_msr); |
| 340 | exit(1); |
| 341 | } |
| 342 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 343 | /* Some MSRs must not be written. */ |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 344 | if (errno == EIO) |
| 345 | return -1; |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | int setup_cpu_msr(int cpu) |
| 351 | { |
| 352 | char msrfilename[64]; |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 353 | memset(msrfilename, 0, sizeof(msrfilename)); |
| 354 | snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 355 | |
| 356 | if (fd_msr != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 357 | msg_pinfo("MSR was already initialized\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | fd_msr = open(msrfilename, O_RDWR); |
| 362 | |
| 363 | if (fd_msr < 0) { |
| 364 | perror("Error while opening /dev/cpu/0/msr"); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 365 | msg_pinfo("Did you run 'modprobe msr'?\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 366 | return -1; |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | void cleanup_cpu_msr(void) |
| 373 | { |
| 374 | if (fd_msr == -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 375 | msg_pinfo("No MSR initialized.\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 376 | return; |
| 377 | } |
| 378 | |
| 379 | close(fd_msr); |
| 380 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 381 | /* Clear MSR file descriptor. */ |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 382 | fd_msr = -1; |
| 383 | } |
| 384 | #else |
Carl-Daniel Hailfinger | a5eecda | 2012-02-25 22:50:21 +0000 | [diff] [blame] | 385 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 386 | #include <sys/ioctl.h> |
| 387 | |
| 388 | typedef struct { |
| 389 | int msr; |
| 390 | uint64_t data; |
| 391 | } cpu_msr_args_t; |
| 392 | #define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t) |
| 393 | #define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t) |
| 394 | |
| 395 | static int fd_msr = -1; |
| 396 | |
| 397 | msr_t rdmsr(int addr) |
| 398 | { |
| 399 | cpu_msr_args_t args; |
| 400 | |
| 401 | msr_t msr = { 0xffffffff, 0xffffffff }; |
| 402 | |
| 403 | args.msr = addr; |
| 404 | |
| 405 | if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) { |
| 406 | perror("CPU_RDMSR"); |
| 407 | close(fd_msr); |
| 408 | exit(1); |
| 409 | } |
| 410 | |
| 411 | msr.lo = args.data & 0xffffffff; |
| 412 | msr.hi = args.data >> 32; |
| 413 | |
| 414 | return msr; |
| 415 | } |
| 416 | |
| 417 | int wrmsr(int addr, msr_t msr) |
| 418 | { |
| 419 | cpu_msr_args_t args; |
| 420 | |
| 421 | args.msr = addr; |
| 422 | args.data = (((uint64_t)msr.hi) << 32) | msr.lo; |
| 423 | |
| 424 | if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) { |
| 425 | perror("CPU_WRMSR"); |
| 426 | close(fd_msr); |
| 427 | exit(1); |
| 428 | } |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | int setup_cpu_msr(int cpu) |
| 434 | { |
| 435 | char msrfilename[64]; |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 436 | memset(msrfilename, 0, sizeof(msrfilename)); |
| 437 | snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 438 | |
| 439 | if (fd_msr != -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 440 | msg_pinfo("MSR was already initialized\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 441 | return -1; |
| 442 | } |
| 443 | |
| 444 | fd_msr = open(msrfilename, O_RDWR); |
| 445 | |
| 446 | if (fd_msr < 0) { |
| 447 | perror("Error while opening /dev/cpu0"); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 448 | msg_pinfo("Did you install ports/sysutils/devcpu?\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | void cleanup_cpu_msr(void) |
| 456 | { |
| 457 | if (fd_msr == -1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 458 | msg_pinfo("No MSR initialized.\n"); |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
| 462 | close(fd_msr); |
| 463 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 464 | /* Clear MSR file descriptor. */ |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 465 | fd_msr = -1; |
| 466 | } |
| 467 | |
| 468 | #else |
| 469 | |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 470 | #ifdef __DARWIN__ |
| 471 | int setup_cpu_msr(int cpu) |
| 472 | { |
| 473 | // Always succeed for now |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | void cleanup_cpu_msr(void) |
| 478 | { |
| 479 | // Nothing, yet. |
| 480 | } |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 481 | #elif defined(__LIBPAYLOAD__) |
| 482 | msr_t libpayload_rdmsr(int addr) |
| 483 | { |
| 484 | msr_t msr; |
| 485 | unsigned long long val = _rdmsr(addr); |
| 486 | msr.lo = val & 0xffffffff; |
| 487 | msr.hi = val >> 32; |
| 488 | return msr; |
| 489 | } |
| 490 | |
| 491 | int libpayload_wrmsr(int addr, msr_t msr) |
| 492 | { |
| 493 | _wrmsr(addr, msr.lo | ((unsigned long long)msr.hi << 32)); |
Patrick Georgi | 97bc95c | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 494 | return 0; |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 495 | } |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 496 | #else |
| 497 | msr_t rdmsr(int addr) |
| 498 | { |
| 499 | msr_t ret = { 0xffffffff, 0xffffffff }; |
| 500 | |
| 501 | return ret; |
| 502 | } |
| 503 | |
| 504 | int wrmsr(int addr, msr_t msr) |
| 505 | { |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | int setup_cpu_msr(int cpu) |
| 510 | { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 511 | msg_pinfo("No MSR support for your OS yet.\n"); |
Stefan Reinauer | 8fa6481 | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 512 | return -1; |
| 513 | } |
| 514 | |
| 515 | void cleanup_cpu_msr(void) |
| 516 | { |
| 517 | // Nothing, yet. |
| 518 | } |
| 519 | #endif |
| 520 | #endif |
Stefan Reinauer | 173e3ea | 2009-08-19 10:46:23 +0000 | [diff] [blame] | 521 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 522 | #else |
| 523 | /* Does MSR exist on non-x86 architectures? */ |
| 524 | #endif |