/* * Copyright (C) 2006, Intel Corporation * * This file is part of the Linux-ready Firmware Developer Kit * * This program file is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation;version 2.1 of the License. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program in a file named COPYING; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include "../biostest.h" static unsigned long long readmsr(int cpu, unsigned long offset) { char buffer[PATH_MAX]; FILE *file; int fd; unsigned long long msr_value=0xffffffff; unsigned char *msr_value_buf; int ret; msr_value_buf = (unsigned char *)&msr_value; sprintf(buffer, "/dev/msr%i", cpu); file = fopen(buffer, "r"); if (!file) { printf("Error: fopen failed \n"); return -1; } fd = fileno(file); ret = pread(fd, msr_value_buf, 8, offset); fclose(file); if (ret<0) { printf("Error: pread failed %d\n, errno=%d", ret, errno); return -2; } return msr_value; } static void writemsr(int cpu, unsigned long offset, unsigned long long value) { char buffer[PATH_MAX]; FILE *file; int fd; unsigned char *msr_value_buf; int ret; sprintf(buffer, "/dev/msr%i", cpu); file = fopen(buffer, "r"); if (!file) return; fd = fileno(file); msr_value_buf = (unsigned char *)&value; ret = pwrite(fd, msr_value_buf, 8, offset); fclose(file); if (ret<0) return; } static void msr_hwpoke_call(void) { int myWin; newtComponent myForm; newtComponent myOk, myRead, myWrite; newtComponent resu; newtComponent myAddress, myNewValue, myValue, myCPU; const char * address_value, * cpu_value, *new_value; char buffer[32]; int W,H; newtGetScreenSize(&W,&H); myWin = newtOpenWindow((W-40)/2,(H-19)/2,40,19, "Read/Write Model Specific Registers"); myForm = newtForm(NULL,NULL,0); myOk = newtButton(27,15, "Back"); newtFormAddComponent(myForm, myOk); newtFormAddComponent(myForm, newtLabel(2,2, "CPU number")); myCPU = newtEntry(20, 2, "0", 15, &cpu_value, 0); newtFormAddComponent(myForm, myCPU); newtFormAddComponent(myForm, newtLabel(2,4, "MSR number")); myAddress = newtEntry(20, 4, "00000000", 15, &address_value, NEWT_FLAG_RETURNEXIT); newtFormAddComponent(myForm, myAddress); newtFormAddComponent(myForm, newtLabel(2,6, "Current value")); myValue = newtTextbox(20,6,16,1, 0); newtFormAddComponent(myForm, myValue); myRead = newtButton(2,8, "Read"); newtFormAddComponent(myForm, myRead); newtFormAddComponent(myForm, newtLabel(2,13, "New value")); myNewValue = newtEntry(20, 13, "00000000", 15, &new_value, NEWT_FLAG_RETURNEXIT); newtFormAddComponent(myForm, myNewValue); myWrite = newtButton(2,15, "Write"); newtFormAddComponent(myForm, myWrite); while (1) { resu = newtRunForm(myForm); if (resu == myRead || resu==myAddress) { unsigned long address, cpu; unsigned long long value; cpu = strtoull(cpu_value, NULL, 10); address = strtoull(address_value, NULL, 16); value = readmsr(cpu, address); sprintf(buffer,"%llx", value); newtTextboxSetText(myValue, buffer); newtEntrySet(myNewValue, buffer, 1); } else if (resu == myWrite) { unsigned long address, cpu; unsigned long long value; cpu = strtoull(cpu_value, NULL, 10); address = strtoull(address_value, NULL, 16); value = strtoull(new_value, NULL, 16); writemsr(cpu, address, value); } else break; } newtPopWindow(); } void run_test(void) { register_hw_poke("MSR Registers", msr_hwpoke_call); }