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