blob: 3338b6209a24d40eafee40c2f966d1fa1c55c1d7 [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 Marek25fde402017-12-29 16:30:49 +010041#include <malloc.h>
Rudolf Marek837d8102010-04-25 22:47:50 +000042#include <sys/nearptr.h>
Rudolf Marek03ae5c12010-03-16 23:59:19 +000043
Rudolf Marek25fde402017-12-29 16:30:49 +010044#define ONE_MEGABYTE (1024 * 1024)
Rudolf Marek03ae5c12010-03-16 23:59:19 +000045#define MEM_DEV "dpmi"
46
Rudolf Marek25fde402017-12-29 16:30:49 +010047static void *realmem_map_aligned;
Rudolf Marek837d8102010-04-25 22:47:50 +000048
Stefan Tauner305e0b92013-07-17 23:46:44 +000049static void *map_first_meg(uintptr_t phys_addr, size_t len)
Rudolf Marek837d8102010-04-25 22:47:50 +000050{
Rudolf Marek25fde402017-12-29 16:30:49 +010051 void *realmem_map;
52 size_t pagesize;
Rudolf Marek837d8102010-04-25 22:47:50 +000053
Rudolf Marek25fde402017-12-29 16:30:49 +010054 if (realmem_map_aligned)
55 return realmem_map_aligned + phys_addr;
56
57 /* valloc() from DJGPP 2.05 does not work properly */
58 pagesize = getpagesize();
59
60 realmem_map = malloc(ONE_MEGABYTE + pagesize);
Rudolf Marek837d8102010-04-25 22:47:50 +000061
Uwe Hermann91f4afa2011-07-28 08:13:25 +000062 if (!realmem_map)
Patrick Georgied7a9642010-09-25 22:53:44 +000063 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000064
Rudolf Marek25fde402017-12-29 16:30:49 +010065 realmem_map_aligned = (void *)(((size_t) realmem_map +
66 (pagesize - 1)) & ~(pagesize - 1));
67
68 if (__djgpp_map_physical_memory(realmem_map_aligned, ONE_MEGABYTE, 0)) {
Carl-Daniel Hailfinger602de982010-10-05 23:21:51 +000069 free(realmem_map);
Rudolf Marek25fde402017-12-29 16:30:49 +010070 realmem_map_aligned = NULL;
Patrick Georgied7a9642010-09-25 22:53:44 +000071 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000072 }
73
Rudolf Marek25fde402017-12-29 16:30:49 +010074 return realmem_map_aligned + phys_addr;
Rudolf Marek837d8102010-04-25 22:47:50 +000075}
Rudolf Marek03ae5c12010-03-16 23:59:19 +000076
Stefan Tauner305e0b92013-07-17 23:46:44 +000077static void *sys_physmap(uintptr_t phys_addr, size_t len)
Rudolf Marek03ae5c12010-03-16 23:59:19 +000078{
79 int ret;
80 __dpmi_meminfo mi;
81
Uwe Hermann91f4afa2011-07-28 08:13:25 +000082 /* Enable 4GB limit on DS descriptor. */
83 if (!__djgpp_nearptr_enable())
Patrick Georgied7a9642010-09-25 22:53:44 +000084 return ERROR_PTR;
Rudolf Marek837d8102010-04-25 22:47:50 +000085
Rudolf Marek25fde402017-12-29 16:30:49 +010086 if ((phys_addr + len - 1) < ONE_MEGABYTE) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +000087 /* We need to use another method to map first 1MB. */
Rudolf Marek837d8102010-04-25 22:47:50 +000088 return map_first_meg(phys_addr, len);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000089 }
90
91 mi.address = phys_addr;
92 mi.size = len;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +000093 ret = __dpmi_physical_address_mapping(&mi);
Rudolf Marek03ae5c12010-03-16 23:59:19 +000094
Uwe Hermann91f4afa2011-07-28 08:13:25 +000095 if (ret != 0)
Patrick Georgied7a9642010-09-25 22:53:44 +000096 return ERROR_PTR;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000097
Rudolf Marek837d8102010-04-25 22:47:50 +000098 return (void *) mi.address + __djgpp_conventional_base;
Rudolf Marek03ae5c12010-03-16 23:59:19 +000099}
100
101#define sys_physmap_rw_uncached sys_physmap
Rudolf Marek837d8102010-04-25 22:47:50 +0000102#define sys_physmap_ro_cached sys_physmap
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000103
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000104void sys_physunmap_unaligned(void *virt_addr, size_t len)
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000105{
106 __dpmi_meminfo mi;
107
Carl-Daniel Hailfinger602de982010-10-05 23:21:51 +0000108 /* There is no known way to unmap the first 1 MB. The DPMI server will
109 * do this for us on exit.
110 */
Rudolf Marek25fde402017-12-29 16:30:49 +0100111 if ((virt_addr >= realmem_map_aligned) &&
112 ((virt_addr + len) <= (realmem_map_aligned + ONE_MEGABYTE))) {
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000113 return;
114 }
115
116 mi.address = (unsigned long) virt_addr;
117 __dpmi_free_physical_address_mapping(&mi);
118}
119
Patrick Georgia9095a92010-09-30 17:03:32 +0000120#elif defined(__LIBPAYLOAD__)
121#include <arch/virtual.h>
122
123#define MEM_DEV ""
124
Stefan Tauner305e0b92013-07-17 23:46:44 +0000125void *sys_physmap(uintptr_t phys_addr, size_t len)
Patrick Georgia9095a92010-09-30 17:03:32 +0000126{
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000127 return (void *)phys_to_virt(phys_addr);
Patrick Georgia9095a92010-09-30 17:03:32 +0000128}
129
130#define sys_physmap_rw_uncached sys_physmap
131#define sys_physmap_ro_cached sys_physmap
132
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000133void sys_physunmap_unaligned(void *virt_addr, size_t len)
Patrick Georgia9095a92010-09-30 17:03:32 +0000134{
135}
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000136#elif defined(__MACH__) && defined(__APPLE__)
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000137
Stefan Reinauer83704c52011-03-18 22:00:15 +0000138#define MEM_DEV "DirectHW"
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000139
Stefan Tauner305e0b92013-07-17 23:46:44 +0000140static void *sys_physmap(uintptr_t phys_addr, size_t len)
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000141{
Patrick Georgied7a9642010-09-25 22:53:44 +0000142 /* The short form of ?: is a GNU extension.
143 * FIXME: map_physical returns NULL both for errors and for success
144 * if the region is mapped at virtual address zero. If in doubt, report
145 * an error until a better interface exists.
146 */
147 return map_physical(phys_addr, len) ? : ERROR_PTR;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000148}
149
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000150/* The OS X driver does not differentiate between mapping types. */
151#define sys_physmap_rw_uncached sys_physmap
152#define sys_physmap_ro_cached sys_physmap
153
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000154void sys_physunmap_unaligned(void *virt_addr, size_t len)
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000155{
156 unmap_physical(virt_addr, len);
157}
158
159#else
160#include <sys/mman.h>
161
Stefan Reinauer0593f212009-01-26 01:10:48 +0000162#if defined (__sun) && (defined(__i386) || defined(__amd64))
163# define MEM_DEV "/dev/xsvc"
164#else
165# define MEM_DEV "/dev/mem"
166#endif
167
168static int fd_mem = -1;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000169static int fd_mem_cached = -1;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000170
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000171/* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
Stefan Tauner305e0b92013-07-17 23:46:44 +0000172static void *sys_physmap_rw_uncached(uintptr_t phys_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000173{
174 void *virt_addr;
175
176 if (-1 == fd_mem) {
177 /* Open the memory device UNCACHED. Important for MMIO. */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000178 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000179 msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno));
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000180 return ERROR_PTR;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000181 }
182 }
183
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000184 virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fd_mem, (off_t)phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000185 return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000186}
187
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000188/* For reading DMI/coreboot/whatever tables. We should never write, and we
189 * do not care about caching.
190 */
Stefan Tauner305e0b92013-07-17 23:46:44 +0000191static void *sys_physmap_ro_cached(uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000192{
193 void *virt_addr;
194
195 if (-1 == fd_mem_cached) {
196 /* Open the memory device CACHED. */
197 if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000198 msg_perr("Critical error: open(" MEM_DEV "): %s\n", strerror(errno));
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000199 return ERROR_PTR;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000200 }
201 }
202
Niklas Söderlund2d8b7ef2013-09-13 19:19:25 +0000203 virt_addr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd_mem_cached, (off_t)phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000204 return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000205}
206
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000207void sys_physunmap_unaligned(void *virt_addr, size_t len)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000208{
209 munmap(virt_addr, len);
210}
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000211#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +0000212
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000213#define PHYSM_RW 0
214#define PHYSM_RO 1
215#define PHYSM_NOCLEANUP 0
216#define PHYSM_CLEANUP 1
217#define PHYSM_EXACT 0
218#define PHYSM_ROUND 1
219
220/* Round start to nearest page boundary below and set len so that the resulting address range ends at the lowest
221 * possible page boundary where the original address range is still entirely contained. It returns the
222 * difference between the rounded start address and the original start address. */
223static uintptr_t round_to_page_boundaries(uintptr_t *start, size_t *len)
224{
225 uintptr_t page_size = getpagesize();
226 uintptr_t page_mask = ~(page_size-1);
227 uintptr_t end = *start + *len;
228 uintptr_t old_start = *start;
229 msg_gspew("page_size=%" PRIxPTR "\n", page_size);
230 msg_gspew("pre-rounding: start=0x%0*" PRIxPTR ", len=0x%zx, end=0x%0*" PRIxPTR "\n",
231 PRIxPTR_WIDTH, *start, *len, PRIxPTR_WIDTH, end);
232 *start = *start & page_mask;
233 end = (end + page_size - 1) & page_mask;
234 *len = end - *start;
235 msg_gspew("post-rounding: start=0x%0*" PRIxPTR ", len=0x%zx, end=0x%0*" PRIxPTR "\n",
236 PRIxPTR_WIDTH, *start, *len, PRIxPTR_WIDTH, *start + *len);
237 return old_start - *start;
238}
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000239
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000240struct undo_physmap_data {
241 void *virt_addr;
242 size_t len;
243};
244
245static int undo_physmap(void *data)
246{
247 if (data == NULL) {
248 msg_perr("%s: tried to physunmap without valid data!\n", __func__);
249 return 1;
250 }
251 struct undo_physmap_data *d = data;
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000252 physunmap_unaligned(d->virt_addr, d->len);
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000253 free(data);
254 return 0;
255}
256
Niklas Söderlund5d307202013-09-14 09:02:27 +0000257static void *physmap_common(const char *descr, uintptr_t phys_addr, size_t len, bool readonly, bool autocleanup,
258 bool round)
Stefan Reinauer0593f212009-01-26 01:10:48 +0000259{
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000260 void *virt_addr;
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000261 uintptr_t offset = 0;
Stephan Guilloux6d08a3e2009-06-23 10:44:36 +0000262
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000263 if (len == 0) {
Stefan Tauner305e0b92013-07-17 23:46:44 +0000264 msg_pspew("Not mapping %s, zero size at 0x%0*" PRIxPTR ".\n", descr, PRIxPTR_WIDTH, phys_addr);
Patrick Georgied7a9642010-09-25 22:53:44 +0000265 return ERROR_PTR;
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000266 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000267
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000268 if (round)
269 offset = round_to_page_boundaries(&phys_addr, &len);
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000270
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000271 if (readonly)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000272 virt_addr = sys_physmap_ro_cached(phys_addr, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000273 else
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000274 virt_addr = sys_physmap_rw_uncached(phys_addr, len);
Stefan Reinauer0593f212009-01-26 01:10:48 +0000275
Patrick Georgied7a9642010-09-25 22:53:44 +0000276 if (ERROR_PTR == virt_addr) {
Stefan Reinauer0593f212009-01-26 01:10:48 +0000277 if (NULL == descr)
278 descr = "memory";
Stefan Tauner0554ca52013-07-25 22:54:25 +0000279 msg_perr("Error accessing %s, 0x%zx bytes at 0x%0*" PRIxPTR "\n",
280 descr, len, PRIxPTR_WIDTH, phys_addr);
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000281 msg_perr(MEM_DEV " mmap failed: %s\n", strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000282#ifdef __linux__
Stefan Reinauer0593f212009-01-26 01:10:48 +0000283 if (EINVAL == errno) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000284 msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
285 msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
286 msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n");
287 msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
288 msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
Stefan Reinauer0593f212009-01-26 01:10:48 +0000289 }
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000290#elif defined (__OpenBSD__)
291 msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000292 "and reboot, or reboot into\n"
293 "single user mode.\n");
Sean Nelson316a29f2010-05-07 20:09:04 +0000294#endif
Niklas Söderlund5d307202013-09-14 09:02:27 +0000295 return ERROR_PTR;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000296 }
297
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000298 if (autocleanup) {
299 struct undo_physmap_data *d = malloc(sizeof(struct undo_physmap_data));
300 if (d == NULL) {
301 msg_perr("%s: Out of memory!\n", __func__);
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000302 physunmap_unaligned(virt_addr, len);
Niklas Söderlund5d307202013-09-14 09:02:27 +0000303 return ERROR_PTR;
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000304 }
305
306 d->virt_addr = virt_addr;
307 d->len = len;
308 if (register_shutdown(undo_physmap, d) != 0) {
309 msg_perr("%s: Could not register shutdown function!\n", __func__);
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000310 physunmap_unaligned(virt_addr, len);
Niklas Söderlund5d307202013-09-14 09:02:27 +0000311 return ERROR_PTR;
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000312 }
313 }
314
Stefan Tauner3285d7f2013-08-14 16:28:19 +0000315 return virt_addr + offset;
Stefan Reinauer0593f212009-01-26 01:10:48 +0000316}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000317
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000318void physunmap_unaligned(void *virt_addr, size_t len)
319{
320 /* No need to check for zero size, such mappings would have yielded ERROR_PTR. */
321 if (virt_addr == ERROR_PTR) {
322 msg_perr("Trying to unmap a nonexisting mapping!\n"
323 "Please report a bug at flashrom@flashrom.org\n");
324 return;
325 }
326
327 sys_physunmap_unaligned(virt_addr, len);
328}
329
330void physunmap(void *virt_addr, size_t len)
331{
332 uintptr_t tmp;
333
334 /* No need to check for zero size, such mappings would have yielded ERROR_PTR. */
335 if (virt_addr == ERROR_PTR) {
336 msg_perr("Trying to unmap a nonexisting mapping!\n"
337 "Please report a bug at flashrom@flashrom.org\n");
338 return;
339 }
340 tmp = (uintptr_t)virt_addr;
341 /* We assume that the virtual address of a page-aligned physical address is page-aligned as well. By
342 * extension, rounding a virtual unaligned address as returned by physmap should yield the same offset
343 * between rounded and original virtual address as between rounded and original physical address.
344 */
345 round_to_page_boundaries(&tmp, &len);
346 virt_addr = (void *)tmp;
347 physunmap_unaligned(virt_addr, len);
348}
349
Stefan Tauner305e0b92013-07-17 23:46:44 +0000350void *physmap(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000351{
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000352 return physmap_common(descr, phys_addr, len, PHYSM_RW, PHYSM_NOCLEANUP, PHYSM_ROUND);
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000353}
354
355void *rphysmap(const char *descr, uintptr_t phys_addr, size_t len)
356{
Niklas Söderlund5d307202013-09-14 09:02:27 +0000357 return physmap_common(descr, phys_addr, len, PHYSM_RW, PHYSM_CLEANUP, PHYSM_ROUND);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000358}
359
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000360void *physmap_ro(const char *descr, uintptr_t phys_addr, size_t len)
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000361{
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000362 return physmap_common(descr, phys_addr, len, PHYSM_RO, PHYSM_NOCLEANUP, PHYSM_ROUND);
Sean Nelson4c6d3a42013-09-11 23:35:03 +0000363}
364
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000365void *physmap_ro_unaligned(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000366{
Niklas Söderlund5d307202013-09-14 09:02:27 +0000367 return physmap_common(descr, phys_addr, len, PHYSM_RO, PHYSM_NOCLEANUP, PHYSM_EXACT);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000368}
369
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000370/* MSR abstraction implementations for Linux, OpenBSD, FreeBSD/Dragonfly, OSX, libpayload
371 * and a non-working default implemenation on the bottom. See also hwaccess.h for some (re)declarations. */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000372#if defined(__i386__) || defined(__x86_64__)
373
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000374#ifdef __linux__
375/*
376 * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
377 * which are ring0 privileged instructions so only the kernel can do the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000378 * read/write. This function, therefore, requires that the msr kernel module
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000379 * be loaded to access these instructions from user space using device
380 * /dev/cpu/0/msr.
381 */
382
383static int fd_msr = -1;
384
385msr_t rdmsr(int addr)
386{
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000387 uint32_t buf[2];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000388 msr_t msr = { 0xffffffff, 0xffffffff };
389
390 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000391 msg_perr("Could not lseek() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000392 close(fd_msr);
393 exit(1);
394 }
395
396 if (read(fd_msr, buf, 8) == 8) {
Stefan Reinauer95e892b2009-09-04 13:57:07 +0000397 msr.lo = buf[0];
398 msr.hi = buf[1];
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000399 return msr;
400 }
401
402 if (errno != EIO) {
403 // A severe error.
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000404 msg_perr("Could not read() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000405 close(fd_msr);
406 exit(1);
407 }
408
409 return msr;
410}
411
412int wrmsr(int addr, msr_t msr)
413{
Anti Sullinbe24d812010-05-17 23:19:22 +0000414 uint32_t buf[2];
415 buf[0] = msr.lo;
416 buf[1] = msr.hi;
417
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000418 if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000419 msg_perr("Could not lseek() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000420 close(fd_msr);
421 exit(1);
422 }
423
Anti Sullinbe24d812010-05-17 23:19:22 +0000424 if (write(fd_msr, buf, 8) != 8 && errno != EIO) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000425 msg_perr("Could not write() MSR: %s\n", strerror(errno));
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000426 close(fd_msr);
427 exit(1);
428 }
429
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000430 /* Some MSRs must not be written. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000431 if (errno == EIO)
432 return -1;
433
434 return 0;
435}
436
437int setup_cpu_msr(int cpu)
438{
439 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000440 memset(msrfilename, 0, sizeof(msrfilename));
441 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu);
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000442
443 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000444 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000445 return -1;
446 }
447
448 fd_msr = open(msrfilename, O_RDWR);
449
450 if (fd_msr < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000451 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000452 msg_pinfo("Did you run 'modprobe msr'?\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000453 return -1;
454 }
455
456 return 0;
457}
458
459void cleanup_cpu_msr(void)
460{
461 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000462 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000463 return;
464 }
465
466 close(fd_msr);
467
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000468 /* Clear MSR file descriptor. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000469 fd_msr = -1;
470}
Stefan Taunere5dcc152016-02-07 03:35:04 +0000471#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 +0000472#include <sys/ioctl.h>
473#include <machine/amdmsr.h>
474
475static int fd_msr = -1;
476
477msr_t rdmsr(int addr)
478{
479 struct amdmsr_req args;
480
481 msr_t msr = { 0xffffffff, 0xffffffff };
482
483 args.addr = (uint32_t)addr;
484
485 if (ioctl(fd_msr, RDMSR, &args) < 0) {
486 msg_perr("Error while executing RDMSR ioctl: %s\n", strerror(errno));
487 close(fd_msr);
488 exit(1);
489 }
490
491 msr.lo = args.val & 0xffffffff;
492 msr.hi = args.val >> 32;
493
494 return msr;
495}
496
497int wrmsr(int addr, msr_t msr)
498{
499 struct amdmsr_req args;
500
501 args.addr = addr;
502 args.val = (((uint64_t)msr.hi) << 32) | msr.lo;
503
504 if (ioctl(fd_msr, WRMSR, &args) < 0) {
505 msg_perr("Error while executing WRMSR ioctl: %s\n", strerror(errno));
506 close(fd_msr);
507 exit(1);
508 }
509
510 return 0;
511}
512
513int setup_cpu_msr(int cpu)
514{
515 char msrfilename[64];
516 memset(msrfilename, 0, sizeof(msrfilename));
517 snprintf(msrfilename, sizeof(msrfilename), "/dev/amdmsr");
518
519 if (fd_msr != -1) {
520 msg_pinfo("MSR was already initialized\n");
521 return -1;
522 }
523
524 fd_msr = open(msrfilename, O_RDWR);
525
526 if (fd_msr < 0) {
527 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
528 return -1;
529 }
530
531 return 0;
532}
533
534void cleanup_cpu_msr(void)
535{
536 if (fd_msr == -1) {
537 msg_pinfo("No MSR initialized.\n");
538 return;
539 }
540
541 close(fd_msr);
542
543 /* Clear MSR file descriptor. */
544 fd_msr = -1;
545}
546
547#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000548#include <sys/ioctl.h>
549
550typedef struct {
551 int msr;
552 uint64_t data;
553} cpu_msr_args_t;
554#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
555#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
556
557static int fd_msr = -1;
558
559msr_t rdmsr(int addr)
560{
561 cpu_msr_args_t args;
562
563 msr_t msr = { 0xffffffff, 0xffffffff };
564
565 args.msr = addr;
566
567 if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000568 msg_perr("Error while executing CPU_RDMSR ioctl: %s\n", strerror(errno));
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000569 close(fd_msr);
570 exit(1);
571 }
572
573 msr.lo = args.data & 0xffffffff;
574 msr.hi = args.data >> 32;
575
576 return msr;
577}
578
579int wrmsr(int addr, msr_t msr)
580{
581 cpu_msr_args_t args;
582
583 args.msr = addr;
584 args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
585
586 if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000587 msg_perr("Error while executing CPU_WRMSR ioctl: %s\n", strerror(errno));
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000588 close(fd_msr);
589 exit(1);
590 }
591
592 return 0;
593}
594
595int setup_cpu_msr(int cpu)
596{
597 char msrfilename[64];
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000598 memset(msrfilename, 0, sizeof(msrfilename));
599 snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu);
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000600
601 if (fd_msr != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000602 msg_pinfo("MSR was already initialized\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000603 return -1;
604 }
605
606 fd_msr = open(msrfilename, O_RDWR);
607
608 if (fd_msr < 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000609 msg_perr("Error while opening %s: %s\n", msrfilename, strerror(errno));
Sean Nelson316a29f2010-05-07 20:09:04 +0000610 msg_pinfo("Did you install ports/sysutils/devcpu?\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000611 return -1;
612 }
613
614 return 0;
615}
616
617void cleanup_cpu_msr(void)
618{
619 if (fd_msr == -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000620 msg_pinfo("No MSR initialized.\n");
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000621 return;
622 }
623
624 close(fd_msr);
625
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000626 /* Clear MSR file descriptor. */
Stefan Reinauer173e3ea2009-08-19 10:46:23 +0000627 fd_msr = -1;
628}
629
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000630#elif defined(__MACH__) && defined(__APPLE__)
631/* rdmsr() and wrmsr() are provided by DirectHW which needs neither setup nor cleanup. */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000632int setup_cpu_msr(int cpu)
633{
634 // Always succeed for now
635 return 0;
636}
637
638void cleanup_cpu_msr(void)
639{
640 // Nothing, yet.
641}
Patrick Georgia9095a92010-09-30 17:03:32 +0000642#elif defined(__LIBPAYLOAD__)
643msr_t libpayload_rdmsr(int addr)
644{
645 msr_t msr;
646 unsigned long long val = _rdmsr(addr);
647 msr.lo = val & 0xffffffff;
648 msr.hi = val >> 32;
649 return msr;
650}
651
652int libpayload_wrmsr(int addr, msr_t msr)
653{
654 _wrmsr(addr, msr.lo | ((unsigned long long)msr.hi << 32));
Patrick Georgi97bc95c2011-03-08 07:17:44 +0000655 return 0;
Patrick Georgia9095a92010-09-30 17:03:32 +0000656}
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +0000657
658int setup_cpu_msr(int cpu)
659{
660 return 0;
661}
662
663void cleanup_cpu_msr(void)
664{
665}
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000666#else
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000667/* default MSR implementation */
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000668msr_t rdmsr(int addr)
669{
670 msr_t ret = { 0xffffffff, 0xffffffff };
671
672 return ret;
673}
674
675int wrmsr(int addr, msr_t msr)
676{
677 return -1;
678}
679
680int setup_cpu_msr(int cpu)
681{
Sean Nelson316a29f2010-05-07 20:09:04 +0000682 msg_pinfo("No MSR support for your OS yet.\n");
Stefan Reinauer8fa64812009-08-12 09:27:45 +0000683 return -1;
684}
685
686void cleanup_cpu_msr(void)
687{
688 // Nothing, yet.
689}
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000690#endif // OS switches for MSR code
691#else // x86
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000692/* Does MSR exist on non-x86 architectures? */
Stefan Tauner6bf5fe72013-07-13 21:18:32 +0000693#endif // arch switches for MSR code