Advertisement
Guest User

Untitled

a guest
Jan 8th, 2018
99
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5.  
  6. #define BUFSIZE (64)
  7.  
  8. int get_msr_value(uint64_t* reg_value) {
  9.   const char* cmd = "rdmsr -u 0x1FC";
  10.   char cmd_buf[BUFSIZE];
  11.  
  12.   FILE* fp;
  13.  
  14.   if ((fp = popen(cmd, "r")) == NULL) {
  15.     printf("Error opening pipe!\n");
  16.     return -1;
  17.   }
  18.  
  19.   cmd_buf[strcspn(fgets(cmd_buf, BUFSIZE, fp), "\n")] = 0;
  20.   *reg_value = atoi(cmd_buf);
  21.  
  22.   if (pclose(fp)) {
  23.     printf("Command not found or exited with error status\n");
  24.     return -1;
  25.   }
  26.  
  27.   return 0;
  28. }
  29.  
  30. int main(void) {
  31.   const char* cmd = "wrmsr -a 0x1FC";
  32.   char* concat_cmd;
  33.   int ret;
  34.   uint64_t* reg_value = &(uint64_t){0};
  35.  
  36.   if ((ret = get_msr_value(reg_value))) {
  37.     return ret;
  38.   }
  39.  
  40.   printf("Old register value: %lu\n", *reg_value);
  41.  
  42.   *reg_value = *reg_value & 0xFFFFFFFE;  // clear bit 0
  43.  
  44.   printf("New register value: %lu\n", *reg_value);
  45.  
  46.   asprintf(&concat_cmd, "%s %i", cmd, *reg_value);
  47.   printf("Executing: %s\n", concat_cmd);
  48.  
  49.   system(concat_cmd);
  50.   free(concat_cmd);
  51.  
  52.   return 0;
  53. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement