summaryrefslogtreecommitdiffstats
path: root/physmap.c
diff options
context:
space:
mode:
authorStefan Reinauer <stepan@coresystems.de>2009-08-12 09:27:45 +0000
committerStefan Reinauer <stefan.reinauer@coreboot.org>2009-08-12 09:27:45 +0000
commit8fa64816ae22ff9e5fdcc00b153010459edb45f4 (patch)
tree171cc242408ff1e317447f3a8f948e0720e4a767 /physmap.c
parent630c79d494668e7c4d41a2eee1a6df578f2b2ecd (diff)
downloadast2050-flashrom-8fa64816ae22ff9e5fdcc00b153010459edb45f4.zip
ast2050-flashrom-8fa64816ae22ff9e5fdcc00b153010459edb45f4.tar.gz
Fix up MSR handling to support more OSes than Linux.
Corresponding to flashrom svn r677. Signed-off-by: Stefan Reinauer <stepan@coresystems.de> Acked-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Diffstat (limited to 'physmap.c')
-rw-r--r--physmap.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/physmap.c b/physmap.c
index 2246dfa..e03a23d 100644
--- a/physmap.c
+++ b/physmap.c
@@ -2,6 +2,7 @@
* This file is part of the flashrom project.
*
* Copyright (C) 2009 Peter Stuge <peter@stuge.se>
+ * Copyright (C) 2009 coresystems GmbH
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -21,6 +22,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
+#include <string.h>
#include <errno.h>
#include "flash.h"
@@ -117,3 +119,136 @@ void *physmap(const char *descr, unsigned long phys_addr, size_t len)
return virt_addr;
}
+
+#ifdef __linux__
+/*
+ * Reading and writing to MSRs, however requires instructions rdmsr/wrmsr,
+ * which are ring0 privileged instructions so only the kernel can do the
+ * read/write. This function, therefore, requires that the msr kernel module
+ * be loaded to access these instructions from user space using device
+ * /dev/cpu/0/msr.
+ */
+
+static int fd_msr = -1;
+
+msr_t rdmsr(int addr)
+{
+ uint8_t buf[8];
+ msr_t msr = { 0xffffffff, 0xffffffff };
+
+ if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
+ perror("Could not lseek() to MSR");
+ close(fd_msr);
+ exit(1);
+ }
+
+ if (read(fd_msr, buf, 8) == 8) {
+ msr.lo = *(uint32_t *)buf;
+ msr.hi = *(uint32_t *)(buf + 4);
+
+ return msr;
+ }
+
+ if (errno != EIO) {
+ // A severe error.
+ perror("Could not read() MSR");
+ close(fd_msr);
+ exit(1);
+ }
+
+ return msr;
+}
+
+int wrmsr(int addr, msr_t msr)
+{
+ if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
+ perror("Could not lseek() to MSR");
+ close(fd_msr);
+ exit(1);
+ }
+
+ if (write(fd_msr, &msr, 8) != 8 && errno != EIO) {
+ perror("Could not write() MSR");
+ close(fd_msr);
+ exit(1);
+ }
+
+ /* some MSRs must not be written */
+ if (errno == EIO)
+ return -1;
+
+ return 0;
+}
+
+int setup_cpu_msr(int cpu)
+{
+ char msrfilename[64];
+ memset(msrfilename, 0, 64);
+ sprintf(msrfilename, "/dev/cpu/%d/msr", cpu);
+
+ if (fd_msr != -1) {
+ printf("MSR was already initialized\n");
+ return -1;
+ }
+
+ fd_msr = open(msrfilename, O_RDWR);
+
+ if (fd_msr < 0) {
+ perror("Error while opening /dev/cpu/0/msr");
+ printf("Did you run 'modprobe msr'?\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+void cleanup_cpu_msr(void)
+{
+ if (fd_msr == -1) {
+ printf("No MSR initialized.\n");
+ return;
+ }
+
+ close(fd_msr);
+
+ /* Clear MSR file descriptor */
+ fd_msr = -1;
+}
+#else
+#ifdef __DARWIN__
+int setup_cpu_msr(int cpu)
+{
+ // Always succeed for now
+ return 0;
+}
+
+void cleanup_cpu_msr(void)
+{
+ // Nothing, yet.
+}
+#else
+msr_t rdmsr(int addr)
+{
+ msr_t ret = { 0xffffffff, 0xffffffff };
+
+ return ret;
+}
+
+int wrmsr(int addr, msr_t msr)
+{
+ return -1;
+}
+
+int setup_cpu_msr(int cpu)
+{
+ printf("No MSR support for your OS yet.\n");
+ return -1;
+}
+
+void cleanup_cpu_msr(void)
+{
+ // Nothing, yet.
+}
+#endif
+#endif
+
OpenPOWER on IntegriCloud