daily pastebin goal
44%
SHARE
TWEET

Untitled

a guest Oct 14th, 2015 122 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. From 7d1a5c59c93c2b57935c814955c4d749a4740fa9 Mon Sep 17 00:00:00 2001
  2. From: Benjamin ROBIN <dev@benjarobin.fr>
  3. Date: Wed, 14 Oct 2015 13:22:31 +0200
  4. Subject: [PATCH] Add the cpuid_get_vendor() function to the interface.
  5.  
  6. Signed-off-by: Benjamin ROBIN <dev@benjarobin.fr>
  7. ---
  8.  libcpuid/cpuid_main.c | 49 ++++++++++++++++++++++++++++++++++++++-----------
  9.  libcpuid/libcpuid.h   |  8 ++++++++
  10.  2 files changed, 46 insertions(+), 11 deletions(-)
  11.  
  12. diff --git a/libcpuid/cpuid_main.c b/libcpuid/cpuid_main.c
  13. index 0d7aed7..70b4707 100644
  14. --- a/libcpuid/cpuid_main.c
  15. +++ b/libcpuid/cpuid_main.c
  16. @@ -239,10 +239,8 @@ static void load_features_common(struct cpu_raw_data_t* raw, struct cpu_id_t* da
  17.         }
  18.  }
  19.  
  20. -static int cpuid_basic_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data)
  21. +static cpu_vendor_t cpuid_vendor_identify(const uint32_t *raw_vendor, char *vendor_str)
  22.  {
  23. -       int i, j, basic, xmodel, xfamily, ext;
  24. -       char brandstr[64] = {0};
  25.         const struct { cpu_vendor_t vendor; char match[16]; }
  26.         matchtable[NUM_CPU_VENDORS] = {
  27.                 /* source: http://www.sandpile.org/ia32/cpuid.htm */
  28. @@ -257,18 +255,30 @@ static int cpuid_basic_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* dat
  29.                 { VENDOR_SIS            , "SiS SiS SiS " },
  30.                 { VENDOR_NSC            , "Geode by NSC" },
  31.         };
  32. -      
  33. -       memcpy(data->vendor_str + 0, &raw->basic_cpuid[0][1], 4);
  34. -       memcpy(data->vendor_str + 4, &raw->basic_cpuid[0][3], 4);
  35. -       memcpy(data->vendor_str + 8, &raw->basic_cpuid[0][2], 4);
  36. -       data->vendor_str[12] = 0;
  37. +
  38. +       cpu_vendor_t vendor = VENDOR_UNKNOWN;
  39. +
  40. +       memcpy(vendor_str + 0, &raw_vendor[1], 4);
  41. +       memcpy(vendor_str + 4, &raw_vendor[3], 4);
  42. +       memcpy(vendor_str + 8, &raw_vendor[2], 4);
  43. +       vendor_str[12] = 0;
  44. +
  45.         /* Determine vendor: */
  46. -       data->vendor = VENDOR_UNKNOWN;
  47.         for (i = 0; i < NUM_CPU_VENDORS; i++)
  48. -               if (!strcmp(data->vendor_str, matchtable[i].match)) {
  49. -                       data->vendor = matchtable[i].vendor;
  50. +               if (!strcmp(vendor_str, matchtable[i].match)) {
  51. +                       vendor = matchtable[i].vendor;
  52.                         break;
  53.                 }
  54. +
  55. +       return vendor;
  56. +}
  57. +
  58. +static int cpuid_basic_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data)
  59. +{
  60. +       int i, j, basic, xmodel, xfamily, ext;
  61. +       char brandstr[64] = {0};
  62. +
  63. +       data->vendor = cpuid_vendor_identify(raw->basic_cpuid[0], data->vendor_str);
  64.         if (data->vendor == VENDOR_UNKNOWN)
  65.                 return set_error(ERR_CPU_UNKN);
  66.         basic = raw->basic_cpuid[0][0];
  67. @@ -348,6 +358,23 @@ void cpu_exec_cpuid_ext(uint32_t* regs)
  68.         exec_cpuid(regs);
  69.  }
  70.  
  71. +cpu_vendor_t cpuid_get_vendor(void)
  72. +{
  73. +       static cpu_vendor_t vendor = VENDOR_UNKNOWN;
  74. +       uint32_t raw_vendor[4];
  75. +       char vendor_str[VENDOR_STR_MAX];
  76. +
  77. +       if (vendor == VENDOR_UNKNOWN) {
  78. +               if (!cpuid_present())
  79. +                       set_error(ERR_NO_CPUID);
  80. +               else {
  81. +                       cpu_exec_cpuid(0, raw_vendor);
  82. +                       vendor = cpuid_vendor_identify(raw_vendor, vendor_str);
  83. +               }
  84. +       }
  85. +       return vendor;
  86. +}
  87. +
  88.  int cpuid_get_raw_data(struct cpu_raw_data_t* data)
  89.  {
  90.         unsigned i;
  91. diff --git a/libcpuid/libcpuid.h b/libcpuid/libcpuid.h
  92. index 2e317b4..72d370c 100644
  93. --- a/libcpuid/libcpuid.h
  94. +++ b/libcpuid/libcpuid.h
  95. @@ -436,6 +436,14 @@ void cpu_exec_cpuid(uint32_t eax, uint32_t* regs);
  96.  void cpu_exec_cpuid_ext(uint32_t* regs);
  97.  
  98.  /**
  99. + * @brief Obtains the cpu vendor from CPUID from the current CPU
  100. + * @note The result is cached.
  101. + * @returns VENDOR_UNKNOWN if failed, otherwise the cpu vendor type.
  102. + *          @see cpu_vendor_t
  103. + */
  104. +cpu_vendor_t cpuid_get_vendor(void);
  105. +
  106. +/**
  107.   * @brief Obtains the raw CPUID data from the current CPU
  108.   * @param data - a pointer to cpu_raw_data_t structure
  109.   * @returns zero if successful, and some negative number on error.
  110. --
  111. 2.6.1.windows.1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top