hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/tools/perf/arch/x86/util/header.c
....@@ -5,8 +5,10 @@
55 #include <stdio.h>
66 #include <stdlib.h>
77 #include <string.h>
8
+#include <regex.h>
89
9
-#include "../../util/header.h"
10
+#include "../../../util/debug.h"
11
+#include "../../../util/header.h"
1012
1113 static inline void
1214 cpuid(unsigned int op, unsigned int *a, unsigned int *b, unsigned int *c,
....@@ -71,9 +73,72 @@
7173 {
7274 char *buf = malloc(128);
7375
74
- if (buf && __get_cpuid(buf, 128, "%s-%u-%X$") < 0) {
76
+ if (buf && __get_cpuid(buf, 128, "%s-%u-%X-%X$") < 0) {
7577 free(buf);
7678 return NULL;
7779 }
7880 return buf;
7981 }
82
+
83
+/* Full CPUID format for x86 is vendor-family-model-stepping */
84
+static bool is_full_cpuid(const char *id)
85
+{
86
+ const char *tmp = id;
87
+ int count = 0;
88
+
89
+ while ((tmp = strchr(tmp, '-')) != NULL) {
90
+ count++;
91
+ tmp++;
92
+ }
93
+
94
+ if (count == 3)
95
+ return true;
96
+
97
+ return false;
98
+}
99
+
100
+int strcmp_cpuid_str(const char *mapcpuid, const char *id)
101
+{
102
+ regex_t re;
103
+ regmatch_t pmatch[1];
104
+ int match;
105
+ bool full_mapcpuid = is_full_cpuid(mapcpuid);
106
+ bool full_cpuid = is_full_cpuid(id);
107
+
108
+ /*
109
+ * Full CPUID format is required to identify a platform.
110
+ * Error out if the cpuid string is incomplete.
111
+ */
112
+ if (full_mapcpuid && !full_cpuid) {
113
+ pr_info("Invalid CPUID %s. Full CPUID is required, "
114
+ "vendor-family-model-stepping\n", id);
115
+ return 1;
116
+ }
117
+
118
+ if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
119
+ /* Warn unable to generate match particular string. */
120
+ pr_info("Invalid regular expression %s\n", mapcpuid);
121
+ return 1;
122
+ }
123
+
124
+ match = !regexec(&re, id, 1, pmatch, 0);
125
+ regfree(&re);
126
+ if (match) {
127
+ size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
128
+ size_t cpuid_len;
129
+
130
+ /* If the full CPUID format isn't required,
131
+ * ignoring the stepping.
132
+ */
133
+ if (!full_mapcpuid && full_cpuid)
134
+ cpuid_len = strrchr(id, '-') - id;
135
+ else
136
+ cpuid_len = strlen(id);
137
+
138
+ /* Verify the entire string matched. */
139
+ if (match_len == cpuid_len)
140
+ return 0;
141
+ }
142
+
143
+ return 1;
144
+}