Skip to content

Instantly share code, notes, and snippets.

@fuzzy
Created October 15, 2014 22:33
Show Gist options
  • Save fuzzy/1e5fc00d897e0012dd9b to your computer and use it in GitHub Desktop.
Save fuzzy/1e5fc00d897e0012dd9b to your computer and use it in GitHub Desktop.
modification of code found at: http://www.linux-kvm.org/page/Enable_VT-X_on_Mac_Pro_%28Early_2008%29 to support FreeBSD
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/cpuctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
/* Returns 1 if processor found, 0 otherwise */
int setupCpu(int cpuid)
{
int addr = 0x3a; // VTX enable
char devfile[255];
char kernmod[255];
#ifdef __FreeBSD__
sprintf(devfile, "/dev/cpuctl%d", cpuid);
sprintf(kernmod, "cpuctl");
#elif __linux__
sprintf(devfile, "/dev/cpu/%d/msr", cpuid);
sprintf(kernmod, "msr");
#endif
int fd = open(devfile, O_RDONLY);
if (fd==-1)
{
printf("\nDevice %s does not exist.\nEither we have run out of processors, or the %s kernel module is not loaded\n", devfile, kernmod);
return 0;
}
char buf[8];
printf("Checking cpu %d - %s ... ",cpuid, devfile);
lseek(fd, addr, SEEK_SET);
read(fd, buf, 8);
char bLo = buf[0]; // Low byte
if (bLo & 1) // locked
{
if (bLo & 4)
printf("locked ON\n");
else
printf("locked OFF\n");
}
else
{ // Unlocked - we can try and turn vtx on
fd = open(devfile, O_WRONLY);
if (fd == -1)
{
printf("Failed to open device for writing\n");
}
else
{
buf[0] = buf[0] | 5; // On + lock
lseek(fd, addr, SEEK_SET);
if (write(fd, buf, 8) < 0)
{
printf("Failed to write to device\n");
}
else
{
printf("Switched VT-X on\n");
}
}
}
return 1;
}
int main(int argc, char *argv[])
{
printf("MSR Magic 1.0 (C) 2009 George Styles www.georgestyles.co.uk\n");
printf("FreeBSD support by Mike 'Fuzzy' Partin <[email protected]>\n\n");
int iCpu = 0;
while (setupCpu(iCpu) != 0)
{
iCpu++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment