2 * Copyright (c) 2013-2018, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
38 static const char * const cpu_vendors[] = {
44 pt_cpuid_vendor_size = 12
48 /* The raw data returned from cpuid. */
55 /* The resulting vendor string. */
56 char vendor_string[pt_cpuid_vendor_size];
59 static enum pt_cpu_vendor cpu_vendor(void)
61 union cpu_vendor vendor;
65 memset(&vendor, 0, sizeof(vendor));
68 pt_cpuid(0u, &eax, &vendor.cpuid.ebx, &vendor.cpuid.ecx,
71 for (i = 0; i < sizeof(cpu_vendors)/sizeof(*cpu_vendors); i++)
72 if (strncmp(vendor.vendor_string,
73 cpu_vendors[i], pt_cpuid_vendor_size) == 0)
74 return (enum pt_cpu_vendor) i;
79 static uint32_t cpu_info(void)
81 uint32_t eax, ebx, ecx, edx;
87 pt_cpuid(1u, &eax, &ebx, &ecx, &edx);
92 int pt_cpu_parse(struct pt_cpu *cpu, const char *s)
96 long family, model, stepping;
101 family = strtol(s, &endptr, 0);
102 if (s == endptr || *endptr == '\0' || *endptr != sep)
105 if (family < 0 || family > USHRT_MAX)
111 model = strtol(s, &endptr, 0);
112 if (s == endptr || (*endptr != '\0' && *endptr != sep))
115 if (model < 0 || model > UCHAR_MAX)
119 /* stepping was omitted, it defaults to 0 */
125 stepping = strtol(s, &endptr, 0);
129 if (stepping < 0 || stepping > UCHAR_MAX)
133 cpu->vendor = pcv_intel;
134 cpu->family = (uint16_t) family;
135 cpu->model = (uint8_t) model;
136 cpu->stepping = (uint8_t) stepping;
141 int pt_cpu_read(struct pt_cpu *cpu)
149 cpu->vendor = cpu_vendor();
153 cpu->family = family = (info>>8) & 0xf;
155 cpu->family += (info>>20) & 0xf;
157 cpu->model = (info>>4) & 0xf;
158 if (family == 0x6 || family == 0xf)
159 cpu->model += (info>>12) & 0xf0;
161 cpu->stepping = (info>>0) & 0xf;