blob: a261ccd51ba6b44f6b0ec13f21c3d47bf41bbc9a [file] [log] [blame]
Peter Stuge33fefca2009-01-26 01:33:02 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Peter Stuge <peter@stuge.se>
Stefan Reinauer8fa64812009-08-12 09:27:45 +00005 * Copyright (C) 2009 coresystems GmbH
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +00006 * Copyright (C) 2010 Carl-Daniel Hailfinger
Rudolf Marek03ae5c12010-03-16 23:59:19 +00007 * Copyright (C) 2010 Rudolf Marek <r.marek@assembler.cz>
Peter Stuge33fefca2009-01-26 01:33:02 +00008 *
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 Hermann7b2969b2009-04-15 10:52:49 +000022
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000023#include <unistd.h>
Stefan Tauner7fb5aa02013-08-14 15:48:44 +000024#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000025#include <stdio.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000026#include <stdlib.h>
Stefan Reinauer8fa64812009-08-12 09:27:45 +000027#include <string.h>
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +000028#include <errno.h>
Stefan Reinauer0593f212009-01-26 01:10:48 +000029#include "flash.h"
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +000030#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000031#include "hwaccess.h"
Stefan Reinauer0593f212009-01-26 01:10:48 +000032
Patrick Georgia9095a92010-09-30 17:03:32 +000033#if !defined(__DJGPP__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +000034/* No file access needed/possible to get mmap access permissions or access MSR. */
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000035#include <sys/stat.h>
36#include <fcntl.h>
Carl-Daniel Hailfingerdcef67e2010-06-21 23:20:15 +000037#endif
38
Rudolf Marek03ae5c12010-03-16 23:59:19 +000039#ifdef __DJGPP__
40#include <dpmi.h>
Rudolf Marek837d8102010-04-25 22:47:50 +000041#include <sys/nearptr.h>
Rudolf Marek03ae5c12010-03-16 23:59:19 +000042
43#define MEM_DEV "dpmi"
44
Rudolf Marek837d8102010-04-25 22:47:50 +000045static void *realmem_map;
46
Stefan Tauner305e0b92013-07-17 23:46:44 +000047static void *map_first_meg(uintptr_t phys_addr, size_t len)
Rudolf Marek837d8102010-04-25 22:47:50 +000048{
Uwe Hermann91f4afa2011-07-28 08:13:25 +000049 if (realmem_map)
Rudolf Marek837d8102010-04-25 22:47:50 +000050 return realmem_map + phys_addr;
Rudolf Marek837d8102010-04-25 22:47:50 +000051
52 realmem_map = valloc(1024 * 1024);
53
Uwe Hermann91f4afa2011-07-28 08:13:25 +000054 if (!realmem_map)
Patrick Georgied7a9642010-09-25 22:53:44 +000055 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000056
57 if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) {
Carl-Daniel Hailfinger602de982010-10-05 23:21:51 +000058 free(realmem_map);
59 realmem_map = NULL;
Patrick Georgied7a9642010-09-25 22:53:44 +000060 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000061 }
62
63 return realmem_map + phys_addr;
64}
Rudolf Marek03ae5c12010-03-16 23:59:19 +000065
Stefan Tauner305e0b92013-07-17 23:46:44 +000066static void *sys_physmap(uintptr_t phys_addr, size_t len)
Rudolf Marek03ae5c12010-03-16 23:59:19 +000067{
68 int ret;
69 __dpmi_meminfo mi;
70
Uwe Hermann91f4afa2011-07-28 08:13:25 +000071 /* Enable 4GB limit on DS descriptor. */
72 if (!__djgpp_nearptr_enable())
Patrick Georgied7a9642010-09-25 22:53:44 +000073 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000074
75 if ((phys_addr + len - 1) < (1024 * 1024)) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +000076 /* We need to use another method to map first 1MB. */
Rudolf Marek837d8102010-04-25 22:47:50 +000077 return map_first_meg(phys_addr, len);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000078 }
79
80 mi.address = phys_addr;
81 mi.size = len;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +000082 ret = __dpmi_physical_address_mapping(&mi);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000083
Uwe Hermann91f4afa2011-07-28 08:13:25 +000084 if (ret != 0)
Patrick Georgied7a9642010-09-25 22:53:44 +000085 return ERROR_PTR;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000086
Rudolf Marek837d8102010-04-25 22:47:50 +000087 return (void *) mi.address + __djgpp_conventional_base;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000088}
89
90#define sys_physmap_rw_uncached sys_physmap
Rudolf Marek837d8102010-04-25 22:47:50 +000091#define sys_physmap_ro_cached sys_physmap
Rudolf Marek03ae5c12010-03-16 23:59:19 +000092
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +000093void sys_physunmap_unaligned(void *virt_addr, size_t len)
Rudolf Marek03ae5c12010-03-16 23:59:19 +000094{
95 __dpmi_meminfo mi;
96
Carl-Daniel Hailfinger602de982010-10-05 23:21:51 +000097 /* There is no known way to unmap the first 1 MB. The DPMI server will
98 * do this for us on exit.
99 */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000100 if ((virt_addr >= realmem_map) &&
101 ((virt_addr + len) <= (realmem_map + (1024 * 1024)))) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000102 return;
103 }
104
105 mi.address = (unsigned long) virt_addr;
106 __dpmi_free_physical_address_mapping(&mi);
107}
108
Patrick Georgia9095a92010-09-30 17:03:32 +0000109#elif defined(__LIBPAYLOAD__)
110#include <arch/virtual.h>
111
112#define MEM_DEV ""
113
Stefan Tauner305e0b92013-07-17 23:46:44 +0000114void *sys_physmap(uintptr_t phys_addr, size_t len)
Patrick Georgia9095a92010-09-30 17:03:32 +0000115{
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000116 return (void *)phys_to_virt(phys_addr);
Patrick Georgia9095a92010-09-30 17:03:32 +0000117}
118
119#define sys_physmap_rw_uncached sys_physmap
120#define sys_physmap_ro_cached sys_physmap
121
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000122void sys_physunmap_unaligned(void *virt_addr, size_t len)
Patrick Georgia9095a92010-09-30 17:03:32 +0000123{
124}
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000125#elif defined(__MACH__) && defined(__APPLE__)
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000126
Stefan Reinauer83704c52011-03-18 22:00:15 +0000127#define MEM_DEV "DirectHW"
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000128
Stefan Tauner305e0b92013-07-17 23:46:44 +0000129static void *sys_physmap(uintptr_t phys_addr, size_t len)
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000130{
Patrick Georgied7a9642010-09-25 22:53:44 +0000131 /* The short form of ?: is a GNU extension.
132 * FIXME: map_physical returns NULL both for errors and for success
133 * if the region is mapped at virtual address zero. If in doubt, report
134 * an error until a better interface exists.
135 */
136 return map_physical(phys_addr, len) ? : ERROR_PTR;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000137}
138
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000139/* The OS X driver does not differentiate between mapping types. */
140#define sys_physmap_rw_uncached sys_physmap
141#define sys_physmap_ro_cached sys_physmap
142
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000143void sys_physunmap_unaligned(void *virt_addr, size_t len)
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000144{
145 unmap_physical(virt_addr, len);
146}
147
148#else
149#include <sys/mman.h>
150
Stefan Reinauer0593f212009-01-26 01:10:48 +0000151#if defined (__sun) && (defined(__i386) || defined(__amd64))
152# define MEM_DEV "/dev/xsvc"
153#else
154# define MEM_DEV "/dev/mem"
155#endif
156
157static int fd_mem = -1;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000158static int fd_mem_cached = -1;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000159
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000160/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
Stefan Tauner305e0b92013-07-17 23:46:44 +0000161static void *sys_physmap_rw_uncached(uintptr_t phys_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000162{
163 void *virt_addr;
164
165 if (-1 == fd_mem) {
166 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000167 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000168 msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno));
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000169 return ERROR_PTR;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000170 }
171 }
172
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000173 virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fd_mem, (off_t)phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000174 return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000175}
176
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000177/* For reading DMI/coreboot/whatever tables. We should never write, and we
178 * do not care about caching.
179 */
Stefan Tauner305e0b92013-07-17 23:46:44 +0000180static void *sys_physmap_ro_cached(uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000181{
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 Tauner363fd7e2013-04-07 13:08:30 +0000187 msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno));
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000188 return ERROR_PTR;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000189 }
190 }
191
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000192 virt_addr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd_mem_cached, (off_t)phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000193 return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000194}
195
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000196void sys_physunmap_unaligned(void *virt_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000197{
198 munmap(virt_addr, len);
199}
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000200#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +0000201
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000202#define PHYSM_RW 0
203#define PHYSM_RO 1
204#define PHYSM_NOCLEANUP 0
205#define PHYSM_CLEANUP 1
206#define PHYSM_EXACT 0
207#define PHYSM_ROUND 1
208
209/* Round start to nearest page boundary below and set len so that the resulting address range ends at the lowest
210 * possible page boundary where the original address range is still entirely contained. It returns the
211 * difference between the rounded start address and the original start address. */
212static uintptr_t round_to_page_boundaries(uintptr_t *start, size_t *len)
213{
214 uintptr_t page_size = getpagesize();
215 uintptr_t page_mask = ~(page_size-1);
216 uintptr_t end = *start + *len;
217 uintptr_t old_start = *start;
218 msg_gspew("page_size=%" PRIxPTR "\n", page_size);
219 msg_gspew("pre-rounding: start=0x%0*" PRIxPTR ", len=0x%zx, end=0x%0*" PRIxPTR "\n",
220 PRIxPTR_WIDTH, *start, *len, PRIxPTR_WIDTH, end);
221 *start = *start & page_mask;
222 end = (end + page_size - 1) & page_mask;
223 *len = end - *start;
224 msg_gspew("post-rounding: start=0x%0*" PRIxPTR ", len=0x%zx, end=0x%0*" PRIxPTR "\n",
225 PRIxPTR_WIDTH, *start, *len, PRIxPTR_WIDTH, *start + *len);
226 return old_start - *start;
227}
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000228
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000229struct undo_physmap_data {
230 void *virt_addr;
231 size_t len;
232};
233
234static int undo_physmap(void *data)
235{
236 if (data == NULL) {
237 msg_perr("%s: tried to physunmap without valid data!\n", __func__);
238 return 1;
239 }
240 struct undo_physmap_data *d = data;
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000241 physunmap_unaligned(d->virt_addr, d->len);
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000242 free(data);
243 return 0;
244}
245
Niklas Söderlund5d307202013-09-14 09:02:27 +0000246static void *physmap_common(const char *descr, uintptr_t phys_addr, size_t len, bool readonly, bool autocleanup,
247 bool round)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000248{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000249 void *virt_addr;
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000250 uintptr_t offset = 0;
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000251
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000252 if (len == 0) {
Stefan Tauner305e0b92013-07-17 23:46:44 +0000253 msg_pspew("Not mapping %s, zero size at 0x%0*" PRIxPTR ".\n", descr, PRIxPTR_WIDTH, phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000254 return ERROR_PTR;
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000255 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000256
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000257 if (round)
258 offset = round_to_page_boundaries(&phys_addr, &len);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000259
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000260 if (readonly)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000261 virt_addr = sys_physmap_ro_cached(phys_addr, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000262 else
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000263 virt_addr = sys_physmap_rw_uncached(phys_addr, len);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000264
Patrick Georgied7a9642010-09-25 22:53:44 +0000265 if (ERROR_PTR == virt_addr) {
Stefan Reinauer0593f212009-01-26 01:10:48 +0000266 if (NULL == descr)
267 descr = "memory";
Stefan Tauner0554ca52013-07-25 22:54:25 +0000268 msg_perr("Error accessing %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
269 descr, len, PRIxPTR_WIDTH, phys_addr);
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000270 msg_perr(MEM_DEV " mmap failed: %s\n", strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000271#ifdef __linux__
Stefan Reinauer0593f212009-01-26 01:10:48 +0000272 if (EINVAL == errno) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000273 msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
274 msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
275 msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n");
276 msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
277 msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
Stefan Reinauer0593f212009-01-26 01:10:48 +0000278 }
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000279#elif defined (__OpenBSD__)
280 msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000281 "and reboot, or reboot into\n"
282 "single user mode.\n");
Sean Nelson316a29f2010-05-07 20:09:04 +0000283#endif
Niklas Söderlund5d307202013-09-14 09:02:27 +0000284 return ERROR_PTR;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000285 }
286
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000287 if (autocleanup) {
288 struct undo_physmap_data *d = malloc(sizeof(struct undo_physmap_data));
289 if (d == NULL) {
290 msg_perr("%s: Out of memory!\n", __func__);
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000291 physunmap_unaligned(virt_addr, len);
Niklas Söderlund5d307202013-09-14 09:02:27 +0000292 return ERROR_PTR;
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000293 }
294
295 d->virt_addr = virt_addr;
296 d->len = len;
297 if (register_shutdown(undo_physmap, d) != 0) {
298 msg_perr("%s: Could not register shutdown function!\n", __func__);
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000299 physunmap_unaligned(virt_addr, len);
Niklas Söderlund5d307202013-09-14 09:02:27 +0000300 return ERROR_PTR;
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000301 }
302 }
303
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000304 return virt_addr + offset;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000305}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000306
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000307void physunmap_unaligned(void *virt_addr, size_t len)
308{
309 /* No need to check for zero size, such mappings would have yielded ERROR_PTR. */
310 if (virt_addr == ERROR_PTR) {
311 msg_perr("Trying to unmap a nonexisting mapping!\n"
312 "Please report a bug at flashrom@flashrom.org\n");
313 return;
314 }
315
316 sys_physunmap_unaligned(virt_addr, len);
317}
318
319void physunmap(void *virt_addr, size_t len)
320{
321 uintptr_t tmp;
322
323 /* No need to check for zero size, such mappings would have yielded ERROR_PTR. */
324 if (virt_addr == ERROR_PTR) {
325 msg_perr("Trying to unmap a nonexisting mapping!\n"
326 "Please report a bug at flashrom@flashrom.org\n");
327 return;
328 }
329 tmp = (uintptr_t)virt_addr;
330 /* We assume that the virtual address of a page-aligned physical address is page-aligned as well. By
331 * extension, rounding a virtual unaligned address as returned by physmap should yield the same offset
332 * between rounded and original virtual address as between rounded and original physical address.
333 */
334 round_to_page_boundaries(&tmp, &len);
335 virt_addr = (void *)tmp;
336 physunmap_unaligned(virt_addr, len);
337}
338
Stefan Tauner305e0b92013-07-17 23:46:44 +0000339void *physmap(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000340{
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000341 return physmap_common(descr, phys_addr, len, PHYSM_RW, PHYSM_NOCLEANUP, PHYSM_ROUND);
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000342}
343
344void *rphysmap(const char *descr, uintptr_t phys_addr, size_t len)
345{
Niklas Söderlund5d307202013-09-14 09:02:27 +0000346 return physmap_common(descr, phys_addr, len, PHYSM_RW, PHYSM_CLEANUP, PHYSM_ROUND);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000347}
348
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000349void *physmap_ro(const char *descr, uintptr_t phys_addr, size_t len)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000350{
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000351 return physmap_common(descr, phys_addr, len, PHYSM_RO, PHYSM_NOCLEANUP, PHYSM_ROUND);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000352}
353
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000354void *physmap_ro_unaligned(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000355{
Niklas Söderlund5d307202013-09-14 09:02:27 +0000356 return physmap_common(descr, phys_addr, len, PHYSM_RO, PHYSM_NOCLEANUP, PHYSM_EXACT);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000357}
358
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000359/* MSR abstraction implementations for Linux, OpenBSD, FreeBSD/Dragonfly, OSX, libpayload
360 * and a non-working default implemenation on the bottom. See also hwaccess.h for some (re)declarations. */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000361#if defined(__i386__) || defined(__x86_64__)
362
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000363#ifdef __linux__
364/*
365 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
366 * which are ring0 privileged instructions so only the kernel can do the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000367 * read/write. This function, therefore, requires that the msr kernel module
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000368 * be loaded to access these instructions from user space using device
369 * /dev/cpu/0/msr.
370 */
371
372static int fd_msr = -1;
373
374msr_t rdmsr(int addr)
375{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000376 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000377 msr_t msr = { 0xffffffff, 0xffffffff };
378
379 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000380 msg_perr("Could not lseek() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000381 close(fd_msr);
382 exit(1);
383 }
384
385 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000386 msr.lo = buf[0];
387 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000388 return msr;
389 }
390
391 if (errno != EIO) {
392 // A severe error.
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000393 msg_perr("Could not read() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000394 close(fd_msr);
395 exit(1);
396 }
397
398 return msr;
399}
400
401int wrmsr(int addr, msr_t msr)
402{
Anti Sullinbe24d812010-05-17 23:19:22 +0000403 uint32_t buf[2];
404 buf[0] = msr.lo;
405 buf[1] = msr.hi;
406
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000407 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000408 msg_perr("Could not lseek() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000409 close(fd_msr);
410 exit(1);
411 }
412
Anti Sullinbe24d812010-05-17 23:19:22 +0000413 if (write(fd_msr, buf, 8) != 8 && errno != EIO) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000414 msg_perr("Could not write() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000415 close(fd_msr);
416 exit(1);
417 }
418
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000419 /* Some MSRs must not be written. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000420 if (errno == EIO)
421 return -1;
422
423 return 0;
424}
425
426int setup_cpu_msr(int cpu)
427{
428 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000429 memset(msrfilename, 0, sizeof(msrfilename));
430 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu);
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000431
432 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000433 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000434 return -1;
435 }
436
437 fd_msr = open(msrfilename, O_RDWR);
438
439 if (fd_msr < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000440 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000441 msg_pinfo("Did you run 'modprobe msr'?\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000442 return -1;
443 }
444
445 return 0;
446}
447
448void cleanup_cpu_msr(void)
449{
450 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000451 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000452 return;
453 }
454
455 close(fd_msr);
456
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000457 /* Clear MSR file descriptor. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000458 fd_msr = -1;
459}
Stefan Taunere5dcc152016-02-07 03:35:04 +0000460#elif defined(__OpenBSD__) && defined (__i386__) /* This does only work for certain AMD Geode LX systems see amdmsr(4). */
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000461#include <sys/ioctl.h>
462#include <machine/amdmsr.h>
463
464static int fd_msr = -1;
465
466msr_t rdmsr(int addr)
467{
468 struct amdmsr_req args;
469
470 msr_t msr = { 0xffffffff, 0xffffffff };
471
472 args.addr = (uint32_t)addr;
473
474 if (ioctl(fd_msr, RDMSR, &args) < 0) {
475 msg_perr("Error while executing RDMSR ioctl: %s\n", strerror(errno));
476 close(fd_msr);
477 exit(1);
478 }
479
480 msr.lo = args.val & 0xffffffff;
481 msr.hi = args.val >> 32;
482
483 return msr;
484}
485
486int wrmsr(int addr, msr_t msr)
487{
488 struct amdmsr_req args;
489
490 args.addr = addr;
491 args.val = (((uint64_t)msr.hi) << 32) | msr.lo;
492
493 if (ioctl(fd_msr, WRMSR, &args) < 0) {
494 msg_perr("Error while executing WRMSR ioctl: %s\n", strerror(errno));
495 close(fd_msr);
496 exit(1);
497 }
498
499 return 0;
500}
501
502int setup_cpu_msr(int cpu)
503{
504 char msrfilename[64];
505 memset(msrfilename, 0, sizeof(msrfilename));
506 snprintf(msrfilename, sizeof(msrfilename), "/dev/amdmsr");
507
508 if (fd_msr != -1) {
509 msg_pinfo("MSR was already initialized\n");
510 return -1;
511 }
512
513 fd_msr = open(msrfilename, O_RDWR);
514
515 if (fd_msr < 0) {
516 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
517 return -1;
518 }
519
520 return 0;
521}
522
523void cleanup_cpu_msr(void)
524{
525 if (fd_msr == -1) {
526 msg_pinfo("No MSR initialized.\n");
527 return;
528 }
529
530 close(fd_msr);
531
532 /* Clear MSR file descriptor. */
533 fd_msr = -1;
534}
535
536#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000537#include <sys/ioctl.h>
538
539typedef struct {
540 int msr;
541 uint64_t data;
542} cpu_msr_args_t;
543#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
544#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
545
546static int fd_msr = -1;
547
548msr_t rdmsr(int addr)
549{
550 cpu_msr_args_t args;
551
552 msr_t msr = { 0xffffffff, 0xffffffff };
553
554 args.msr = addr;
555
556 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000557 msg_perr("Error while executing CPU_RDMSR ioctl: %s\n", strerror(errno));
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000558 close(fd_msr);
559 exit(1);
560 }
561
562 msr.lo = args.data & 0xffffffff;
563 msr.hi = args.data >> 32;
564
565 return msr;
566}
567
568int wrmsr(int addr, msr_t msr)
569{
570 cpu_msr_args_t args;
571
572 args.msr = addr;
573 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
574
575 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000576 msg_perr("Error while executing CPU_WRMSR ioctl: %s\n", strerror(errno));
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000577 close(fd_msr);
578 exit(1);
579 }
580
581 return 0;
582}
583
584int setup_cpu_msr(int cpu)
585{
586 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000587 memset(msrfilename, 0, sizeof(msrfilename));
588 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu);
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000589
590 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000591 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000592 return -1;
593 }
594
595 fd_msr = open(msrfilename, O_RDWR);
596
597 if (fd_msr < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000598 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000599 msg_pinfo("Did you install ports/sysutils/devcpu?\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000600 return -1;
601 }
602
603 return 0;
604}
605
606void cleanup_cpu_msr(void)
607{
608 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000609 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000610 return;
611 }
612
613 close(fd_msr);
614
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000615 /* Clear MSR file descriptor. */
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000616 fd_msr = -1;
617}
618
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000619#elif defined(__MACH__) && defined(__APPLE__)
620/* rdmsr() and wrmsr() are provided by DirectHW which needs neither setup nor cleanup. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000621int setup_cpu_msr(int cpu)
622{
623 // Always succeed for now
624 return 0;
625}
626
627void cleanup_cpu_msr(void)
628{
629 // Nothing, yet.
630}
Patrick Georgia9095a92010-09-30 17:03:32 +0000631#elif defined(__LIBPAYLOAD__)
632msr_t libpayload_rdmsr(int addr)
633{
634 msr_t msr;
635 unsigned long long val = _rdmsr(addr);
636 msr.lo = val & 0xffffffff;
637 msr.hi = val >> 32;
638 return msr;
639}
640
641int libpayload_wrmsr(int addr, msr_t msr)
642{
643 _wrmsr(addr, msr.lo | ((unsigned long long)msr.hi << 32));
Patrick Georgi97bc95c2011-03-08 07:17:44 +0000644 return 0;
Patrick Georgia9095a92010-09-30 17:03:32 +0000645}
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +0000646
647int setup_cpu_msr(int cpu)
648{
649 return 0;
650}
651
652void cleanup_cpu_msr(void)
653{
654}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000655#else
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000656/* default MSR implementation */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000657msr_t rdmsr(int addr)
658{
659 msr_t ret = { 0xffffffff, 0xffffffff };
660
661 return ret;
662}
663
664int wrmsr(int addr, msr_t msr)
665{
666 return -1;
667}
668
669int setup_cpu_msr(int cpu)
670{
Sean Nelson316a29f2010-05-07 20:09:04 +0000671 msg_pinfo("No MSR support for your OS yet.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000672 return -1;
673}
674
675void cleanup_cpu_msr(void)
676{
677 // Nothing, yet.
678}
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000679#endif // OS switches for MSR code
680#else // x86
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000681/* Does MSR exist on non-x86 architectures? */
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000682#endif // arch switches for MSR code