physmap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009 Peter Stuge <peter@stuge.se>
  5. * Copyright (C) 2009 coresystems GmbH
  6. * Copyright (C) 2010 Carl-Daniel Hailfinger
  7. * Copyright (C) 2010 Rudolf Marek <r.marek@assembler.cz>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <unistd.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "flash.h"
  27. /* Do we need any file access or ioctl for physmap or MSR? */
  28. #if !defined(__DJGPP__) && !defined(__LIBPAYLOAD__)
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <errno.h>
  32. #endif
  33. #ifdef __DJGPP__
  34. #include <dpmi.h>
  35. #include <sys/nearptr.h>
  36. #define MEM_DEV "dpmi"
  37. static void *realmem_map;
  38. static void *map_first_meg(unsigned long phys_addr, size_t len)
  39. {
  40. if (realmem_map)
  41. return realmem_map + phys_addr;
  42. realmem_map = valloc(1024 * 1024);
  43. if (!realmem_map)
  44. return ERROR_PTR;
  45. if (__djgpp_map_physical_memory(realmem_map, (1024 * 1024), 0)) {
  46. free(realmem_map);
  47. realmem_map = NULL;
  48. return ERROR_PTR;
  49. }
  50. return realmem_map + phys_addr;
  51. }
  52. static void *sys_physmap(unsigned long phys_addr, size_t len)
  53. {
  54. int ret;
  55. __dpmi_meminfo mi;
  56. /* Enable 4GB limit on DS descriptor. */
  57. if (!__djgpp_nearptr_enable())
  58. return ERROR_PTR;
  59. if ((phys_addr + len - 1) < (1024 * 1024)) {
  60. /* We need to use another method to map first 1MB. */
  61. return map_first_meg(phys_addr, len);
  62. }
  63. mi.address = phys_addr;
  64. mi.size = len;
  65. ret = __dpmi_physical_address_mapping(&mi);
  66. if (ret != 0)
  67. return ERROR_PTR;
  68. return (void *) mi.address + __djgpp_conventional_base;
  69. }
  70. #define sys_physmap_rw_uncached sys_physmap
  71. #define sys_physmap_ro_cached sys_physmap
  72. void physunmap(void *virt_addr, size_t len)
  73. {
  74. __dpmi_meminfo mi;
  75. /* There is no known way to unmap the first 1 MB. The DPMI server will
  76. * do this for us on exit.
  77. */
  78. if ((virt_addr >= realmem_map) &&
  79. ((virt_addr + len) <= (realmem_map + (1024 * 1024)))) {
  80. return;
  81. }
  82. mi.address = (unsigned long) virt_addr;
  83. __dpmi_free_physical_address_mapping(&mi);
  84. }
  85. #elif defined(__LIBPAYLOAD__)
  86. #include <arch/virtual.h>
  87. #define MEM_DEV ""
  88. void *sys_physmap(unsigned long phys_addr, size_t len)
  89. {
  90. return (void *)phys_to_virt(phys_addr);
  91. }
  92. #define sys_physmap_rw_uncached sys_physmap
  93. #define sys_physmap_ro_cached sys_physmap
  94. void physunmap(void *virt_addr, size_t len)
  95. {
  96. }
  97. int setup_cpu_msr(int cpu)
  98. {
  99. return 0;
  100. }
  101. void cleanup_cpu_msr(void)
  102. {
  103. }
  104. #elif defined(__DARWIN__)
  105. #define MEM_DEV "DirectHW"
  106. static void *sys_physmap(unsigned long phys_addr, size_t len)
  107. {
  108. /* The short form of ?: is a GNU extension.
  109. * FIXME: map_physical returns NULL both for errors and for success
  110. * if the region is mapped at virtual address zero. If in doubt, report
  111. * an error until a better interface exists.
  112. */
  113. return map_physical(phys_addr, len) ? : ERROR_PTR;
  114. }
  115. /* The OS X driver does not differentiate between mapping types. */
  116. #define sys_physmap_rw_uncached sys_physmap
  117. #define sys_physmap_ro_cached sys_physmap
  118. void physunmap(void *virt_addr, size_t len)
  119. {
  120. unmap_physical(virt_addr, len);
  121. }
  122. #else
  123. #include <sys/mman.h>
  124. #if defined (__sun) && (defined(__i386) || defined(__amd64))
  125. # define MEM_DEV "/dev/xsvc"
  126. #else
  127. # define MEM_DEV "/dev/mem"
  128. #endif
  129. static int fd_mem = -1;
  130. static int fd_mem_cached = -1;
  131. /* For MMIO access. Must be uncached, doesn't make sense to restrict to ro. */
  132. static void *sys_physmap_rw_uncached(unsigned long phys_addr, size_t len)
  133. {
  134. void *virt_addr;
  135. if (-1 == fd_mem) {
  136. /* Open the memory device UNCACHED. Important for MMIO. */
  137. if (-1 == (fd_mem = open(MEM_DEV, O_RDWR | O_SYNC))) {
  138. perror("Critical error: open(" MEM_DEV ")");
  139. exit(2);
  140. }
  141. }
  142. virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED,
  143. fd_mem, (off_t)phys_addr);
  144. return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
  145. }
  146. /* For reading DMI/coreboot/whatever tables. We should never write, and we
  147. * do not care about caching.
  148. */
  149. static void *sys_physmap_ro_cached(unsigned long phys_addr, size_t len)
  150. {
  151. void *virt_addr;
  152. if (-1 == fd_mem_cached) {
  153. /* Open the memory device CACHED. */
  154. if (-1 == (fd_mem_cached = open(MEM_DEV, O_RDWR))) {
  155. msg_perr("Critical error: open(" MEM_DEV "): %s",
  156. strerror(errno));
  157. exit(2);
  158. }
  159. }
  160. virt_addr = mmap(NULL, len, PROT_READ, MAP_SHARED,
  161. fd_mem_cached, (off_t)phys_addr);
  162. return MAP_FAILED == virt_addr ? ERROR_PTR : virt_addr;
  163. }
  164. void physunmap(void *virt_addr, size_t len)
  165. {
  166. if (len == 0) {
  167. msg_pspew("Not unmapping zero size at %p\n", virt_addr);
  168. return;
  169. }
  170. munmap(virt_addr, len);
  171. }
  172. #endif
  173. #define PHYSMAP_NOFAIL 0
  174. #define PHYSMAP_MAYFAIL 1
  175. #define PHYSMAP_RW 0
  176. #define PHYSMAP_RO 1
  177. static void *physmap_common(const char *descr, unsigned long phys_addr,
  178. size_t len, int mayfail, int readonly)
  179. {
  180. void *virt_addr;
  181. if (len == 0) {
  182. msg_pspew("Not mapping %s, zero size at 0x%08lx.\n",
  183. descr, phys_addr);
  184. return ERROR_PTR;
  185. }
  186. if ((getpagesize() - 1) & len) {
  187. msg_perr("Mapping %s at 0x%08lx, unaligned size 0x%lx.\n",
  188. descr, phys_addr, (unsigned long)len);
  189. }
  190. if ((getpagesize() - 1) & phys_addr) {
  191. msg_perr("Mapping %s, 0x%lx bytes at unaligned 0x%08lx.\n",
  192. descr, (unsigned long)len, phys_addr);
  193. }
  194. if (readonly)
  195. virt_addr = sys_physmap_ro_cached(phys_addr, len);
  196. else
  197. virt_addr = sys_physmap_rw_uncached(phys_addr, len);
  198. if (ERROR_PTR == virt_addr) {
  199. if (NULL == descr)
  200. descr = "memory";
  201. msg_perr("Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr,
  202. (unsigned long)len, phys_addr);
  203. perror(MEM_DEV " mmap failed");
  204. #ifdef __linux__
  205. if (EINVAL == errno) {
  206. msg_perr("In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
  207. msg_perr("CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
  208. msg_perr("Please check if either is enabled in your kernel before reporting a failure.\n");
  209. msg_perr("You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
  210. msg_perr("disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
  211. }
  212. #elif defined (__OpenBSD__)
  213. msg_perr("Please set securelevel=-1 in /etc/rc.securelevel "
  214. "and reboot, or reboot into\n"
  215. "single user mode.\n");
  216. #endif
  217. if (!mayfail)
  218. exit(3);
  219. }
  220. return virt_addr;
  221. }
  222. void *physmap(const char *descr, unsigned long phys_addr, size_t len)
  223. {
  224. return physmap_common(descr, phys_addr, len, PHYSMAP_NOFAIL,
  225. PHYSMAP_RW);
  226. }
  227. void *physmap_try_ro(const char *descr, unsigned long phys_addr, size_t len)
  228. {
  229. return physmap_common(descr, phys_addr, len, PHYSMAP_MAYFAIL,
  230. PHYSMAP_RO);
  231. }
  232. #if defined(__i386__) || defined(__x86_64__)
  233. #ifdef __linux__
  234. /*
  235. * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
  236. * which are ring0 privileged instructions so only the kernel can do the
  237. * read/write. This function, therefore, requires that the msr kernel module
  238. * be loaded to access these instructions from user space using device
  239. * /dev/cpu/0/msr.
  240. */
  241. static int fd_msr = -1;
  242. msr_t rdmsr(int addr)
  243. {
  244. uint32_t buf[2];
  245. msr_t msr = { 0xffffffff, 0xffffffff };
  246. if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
  247. perror("Could not lseek() to MSR");
  248. close(fd_msr);
  249. exit(1);
  250. }
  251. if (read(fd_msr, buf, 8) == 8) {
  252. msr.lo = buf[0];
  253. msr.hi = buf[1];
  254. return msr;
  255. }
  256. if (errno != EIO) {
  257. // A severe error.
  258. perror("Could not read() MSR");
  259. close(fd_msr);
  260. exit(1);
  261. }
  262. return msr;
  263. }
  264. int wrmsr(int addr, msr_t msr)
  265. {
  266. uint32_t buf[2];
  267. buf[0] = msr.lo;
  268. buf[1] = msr.hi;
  269. if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
  270. perror("Could not lseek() to MSR");
  271. close(fd_msr);
  272. exit(1);
  273. }
  274. if (write(fd_msr, buf, 8) != 8 && errno != EIO) {
  275. perror("Could not write() MSR");
  276. close(fd_msr);
  277. exit(1);
  278. }
  279. /* Some MSRs must not be written. */
  280. if (errno == EIO)
  281. return -1;
  282. return 0;
  283. }
  284. int setup_cpu_msr(int cpu)
  285. {
  286. char msrfilename[64];
  287. memset(msrfilename, 0, sizeof(msrfilename));
  288. snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu);
  289. if (fd_msr != -1) {
  290. msg_pinfo("MSR was already initialized\n");
  291. return -1;
  292. }
  293. fd_msr = open(msrfilename, O_RDWR);
  294. if (fd_msr < 0) {
  295. perror("Error while opening /dev/cpu/0/msr");
  296. msg_pinfo("Did you run 'modprobe msr'?\n");
  297. return -1;
  298. }
  299. return 0;
  300. }
  301. void cleanup_cpu_msr(void)
  302. {
  303. if (fd_msr == -1) {
  304. msg_pinfo("No MSR initialized.\n");
  305. return;
  306. }
  307. close(fd_msr);
  308. /* Clear MSR file descriptor. */
  309. fd_msr = -1;
  310. }
  311. #else
  312. #if defined(__FreeBSD__) || defined(__DragonFly__)
  313. #include <sys/ioctl.h>
  314. typedef struct {
  315. int msr;
  316. uint64_t data;
  317. } cpu_msr_args_t;
  318. #define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
  319. #define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
  320. static int fd_msr = -1;
  321. msr_t rdmsr(int addr)
  322. {
  323. cpu_msr_args_t args;
  324. msr_t msr = { 0xffffffff, 0xffffffff };
  325. args.msr = addr;
  326. if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
  327. perror("CPU_RDMSR");
  328. close(fd_msr);
  329. exit(1);
  330. }
  331. msr.lo = args.data & 0xffffffff;
  332. msr.hi = args.data >> 32;
  333. return msr;
  334. }
  335. int wrmsr(int addr, msr_t msr)
  336. {
  337. cpu_msr_args_t args;
  338. args.msr = addr;
  339. args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
  340. if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
  341. perror("CPU_WRMSR");
  342. close(fd_msr);
  343. exit(1);
  344. }
  345. return 0;
  346. }
  347. int setup_cpu_msr(int cpu)
  348. {
  349. char msrfilename[64];
  350. memset(msrfilename, 0, sizeof(msrfilename));
  351. snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu);
  352. if (fd_msr != -1) {
  353. msg_pinfo("MSR was already initialized\n");
  354. return -1;
  355. }
  356. fd_msr = open(msrfilename, O_RDWR);
  357. if (fd_msr < 0) {
  358. perror("Error while opening /dev/cpu0");
  359. msg_pinfo("Did you install ports/sysutils/devcpu?\n");
  360. return -1;
  361. }
  362. return 0;
  363. }
  364. void cleanup_cpu_msr(void)
  365. {
  366. if (fd_msr == -1) {
  367. msg_pinfo("No MSR initialized.\n");
  368. return;
  369. }
  370. close(fd_msr);
  371. /* Clear MSR file descriptor. */
  372. fd_msr = -1;
  373. }
  374. #else
  375. #ifdef __DARWIN__
  376. int setup_cpu_msr(int cpu)
  377. {
  378. // Always succeed for now
  379. return 0;
  380. }
  381. void cleanup_cpu_msr(void)
  382. {
  383. // Nothing, yet.
  384. }
  385. #elif defined(__LIBPAYLOAD__)
  386. msr_t libpayload_rdmsr(int addr)
  387. {
  388. msr_t msr;
  389. unsigned long long val = _rdmsr(addr);
  390. msr.lo = val & 0xffffffff;
  391. msr.hi = val >> 32;
  392. return msr;
  393. }
  394. int libpayload_wrmsr(int addr, msr_t msr)
  395. {
  396. _wrmsr(addr, msr.lo | ((unsigned long long)msr.hi << 32));
  397. return 0;
  398. }
  399. #else
  400. msr_t rdmsr(int addr)
  401. {
  402. msr_t ret = { 0xffffffff, 0xffffffff };
  403. return ret;
  404. }
  405. int wrmsr(int addr, msr_t msr)
  406. {
  407. return -1;
  408. }
  409. int setup_cpu_msr(int cpu)
  410. {
  411. msg_pinfo("No MSR support for your OS yet.\n");
  412. return -1;
  413. }
  414. void cleanup_cpu_msr(void)
  415. {
  416. // Nothing, yet.
  417. }
  418. #endif
  419. #endif
  420. #endif
  421. #else
  422. /* Does MSR exist on non-x86 architectures? */
  423. #endif