libcpuid  0.2.2
libcpuid.h
1 /*
2  * Copyright 2008 Veselin Georgiev,
3  * anrieffNOSPAM @ mgail_DOT.com (convert to gmail)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef __LIBCPUID_H__
27 #define __LIBCPUID_H__
28 
81 /* Include some integer type specifications: */
82 #include "libcpuid_types.h"
83 
84 /* Some limits and other constants */
85 #include "libcpuid_constants.h"
86 
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90 
94 typedef enum {
107  VENDOR_UNKNOWN = -1,
108 } cpu_vendor_t;
109 #define NUM_CPU_VENDORS NUM_CPU_VENDORS
110 
120  uint32_t basic_cpuid[MAX_CPUID_LEVEL][4];
121 
123  uint32_t ext_cpuid[MAX_EXT_CPUID_LEVEL][4];
124 
128  uint32_t intel_fn4[MAX_INTELFN4_LEVEL][4];
129 
133  uint32_t intel_fn11[MAX_INTELFN11_LEVEL][4];
134 };
135 
139 struct cpu_id_t {
141  char vendor_str[VENDOR_STR_MAX];
142 
144  char brand_str[BRAND_STR_MAX];
145 
148 
154  uint8_t flags[CPU_FLAGS_MAX];
155 
157  int32_t family;
158 
160  int32_t model;
161 
163  int32_t stepping;
164 
166  int32_t ext_family;
167 
169  int32_t ext_model;
170 
172  int32_t num_cores;
173 
180 
190 
195  int32_t l1_data_cache;
196 
204 
209  int32_t l2_cache;
210 
212  int32_t l3_cache;
213 
215  int32_t l1_assoc;
216 
218  int32_t l2_assoc;
219 
221  int32_t l3_assoc;
222 
224  int32_t l1_cacheline;
225 
227  int32_t l2_cacheline;
228 
230  int32_t l3_cacheline;
231 
246  char cpu_codename[64];
247 
249  int32_t sse_size;
250 
256  uint8_t detection_hints[CPU_HINTS_MAX];
257 };
258 
279 typedef enum {
387  /* termination: */
388  NUM_CPU_FEATURES,
389 } cpu_feature_t;
390 
396 typedef enum {
398  /* termination */
399  NUM_CPU_HINTS,
400 } cpu_hint_t;
401 
405 typedef enum {
406  ERR_OK = 0,
409  ERR_NO_MEM = -3,
410  ERR_OPEN = -4,
411  ERR_BADFMT = -5,
412  ERR_NOT_IMP = -6,
416  ERR_NO_PERMS = -10,
417  ERR_EXTRACT = -11,
418  ERR_HANDLE = -12,
419  ERR_INVMSR = -13,
420  ERR_INVCNB = -14,
421  ERR_HANDLE_R = -15,
422  ERR_INVRANGE = -16,
423 } cpu_error_t;
424 
429 struct cpu_mark_t {
430  uint64_t tsc;
431  uint64_t sys_clock;
432 };
433 
438 int cpuid_get_total_cpus(void);
439 
445 int cpuid_present(void);
446 
454 void cpu_exec_cpuid(uint32_t eax, uint32_t* regs);
455 
464 void cpu_exec_cpuid_ext(uint32_t* regs);
465 
473 int cpuid_get_raw_data(struct cpu_raw_data_t* data);
474 
492 int cpuid_serialize_raw_data(struct cpu_raw_data_t* data, const char* filename);
493 
506 int cpuid_deserialize_raw_data(struct cpu_raw_data_t* data, const char* filename);
507 
531 int cpu_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data);
532 
540 const char* cpu_feature_str(cpu_feature_t feature);
541 
550 const char* cpuid_error(void);
551 
576 void cpu_rdtsc(uint64_t* result);
577 
590 void cpu_tsc_mark(struct cpu_mark_t* mark);
591 
615 void cpu_tsc_unmark(struct cpu_mark_t* mark);
616 
633 int cpu_clock_by_mark(struct cpu_mark_t* mark);
634 
649 int cpu_clock_by_os(void);
650 
693 int cpu_clock_measure(int millis, int quad_check);
694 
734 int cpu_clock_by_ic(int millis, int runs);
735 
746 int cpu_clock(void);
747 
753 const char* cpuid_lib_version(void);
754 
755 typedef void (*libcpuid_warn_fn_t) (const char *msg);
769 libcpuid_warn_fn_t cpuid_set_warn_function(libcpuid_warn_fn_t warn_fun);
770 
782 void cpuid_set_verbosiness_level(int level);
783 
784 
792 
796 struct cpu_list_t {
800  char **names;
801 };
802 
819 void cpuid_get_cpu_list(cpu_vendor_t vendor, struct cpu_list_t* list);
820 
829 void cpuid_free_cpu_list(struct cpu_list_t* list);
830 
831 struct msr_driver_t;
843 struct msr_driver_t* cpu_msr_driver_open(void);
844 
858 struct msr_driver_t* cpu_msr_driver_open_core(unsigned core_num);
859 
884 int cpu_rdmsr(struct msr_driver_t* handle, uint32_t msr_index, uint64_t* result);
885 
886 
887 typedef enum {
907 
922 int cpu_rdmsr_range(struct msr_driver_t* handle, uint32_t msr_index, uint8_t highbit,
923  uint8_t lowbit, uint64_t* result);
924 
936 int cpu_msrinfo(struct msr_driver_t* handle, cpu_msrinfo_request_t which);
937 #define CPU_INVALID_VALUE 0x3fffffff
938 
952 int cpu_msr_driver_close(struct msr_driver_t* handle);
953 
954 #ifdef __cplusplus
955 }; /* extern "C" */
956 #endif
957 
958 
961 #endif /* __LIBCPUID_H__ */
Definition: libcpuid.h:288
Definition: libcpuid.h:331
Definition: libcpuid.h:285
cpu_msrinfo_request_t
Definition: libcpuid.h:887
cpu_error_t
Describes common library error codes.
Definition: libcpuid.h:405
void cpu_tsc_mark(struct cpu_mark_t *mark)
Store TSC and timing info.
Definition: libcpuid.h:411
Definition: libcpuid.h:301
int cpu_clock_by_mark(struct cpu_mark_t *mark)
Calculates the CPU clock.
Definition: libcpuid.h:417
int cpu_identify(struct cpu_raw_data_t *raw, struct cpu_id_t *data)
Identifies the CPU.
Definition: libcpuid.h:304
Definition: libcpuid.h:408
void cpuid_get_cpu_list(cpu_vendor_t vendor, struct cpu_list_t *list)
Gets a list of all known CPU names from a specific vendor.
Definition: libcpuid.h:373
char ** names
Definition: libcpuid.h:800
cpu_vendor_t vendor
Definition: libcpuid.h:147
Definition: libcpuid.h:98
int32_t family
Definition: libcpuid.h:157
void cpu_exec_cpuid_ext(uint32_t *regs)
Executes the CPUID instruction with the given input registers.
Definition: libcpuid.h:295
Definition: libcpuid.h:312
void cpu_rdtsc(uint64_t *result)
Executes RDTSC.
int32_t l1_data_cache
Definition: libcpuid.h:195
Definition: libcpuid.h:375
int cpu_msr_driver_close(struct msr_driver_t *handle)
Closes an open MSR driver.
Definition: libcpuid.h:370
Definition: libcpuid.h:346
char cpu_codename[64]
Definition: libcpuid.h:246
Internal structure, used in cpu_tsc_mark, cpu_tsc_unmark and cpu_clock_by_mark.
Definition: libcpuid.h:429
int cpuid_get_raw_data(struct cpu_raw_data_t *data)
Obtains the raw CPUID data from the current CPU.
Definition: libcpuid.h:280
int32_t num_cores
Definition: libcpuid.h:172
Definition: libcpuid.h:297
void cpu_tsc_unmark(struct cpu_mark_t *mark)
Calculate TSC and timing difference.
Definition: libcpuid.h:347
Definition: libcpuid.h:315
Definition: libcpuid.h:362
Definition: libcpuid.h:413
Definition: libcpuid.h:298
Definition: libcpuid.h:383
Definition: libcpuid.h:329
Definition: libcpuid.h:397
int cpu_clock_measure(int millis, int quad_check)
Measure the CPU clock frequency.
Definition: libcpuid.h:338
int cpuid_serialize_raw_data(struct cpu_raw_data_t *data, const char *filename)
Writes the raw CPUID data to a text file.
Definition: libcpuid.h:353
Definition: libcpuid.h:310
Definition: libcpuid.h:891
Definition: libcpuid.h:103
Definition: libcpuid.h:382
Definition: libcpuid.h:376
void cpuid_set_verbosiness_level(int level)
Sets the verbosiness level.
char vendor_str[VENDOR_STR_MAX]
Definition: libcpuid.h:141
Definition: libcpuid.h:904
cpu_hint_t
CPU detection hints identifiers.
Definition: libcpuid.h:396
Definition: libcpuid.h:349
Definition: libcpuid.h:337
Definition: libcpuid.h:379
int32_t total_logical_cpus
Definition: libcpuid.h:189
Definition: libcpuid.h:281
Definition: libcpuid.h:414
Definition: libcpuid.h:300
Definition: libcpuid.h:419
int cpu_rdmsr(struct msr_driver_t *handle, uint32_t msr_index, uint64_t *result)
Reads a Model-Specific Register (MSR)
int cpuid_deserialize_raw_data(struct cpu_raw_data_t *data, const char *filename)
Reads raw CPUID data from file.
const char * cpuid_lib_version(void)
Returns the libcpuid version.
Definition: libcpuid.h:364
Definition: libcpuid.h:380
Definition: libcpuid.h:366
Definition: libcpuid.h:341
Definition: libcpuid.h:309
Definition: libcpuid.h:319
int32_t l1_cacheline
Definition: libcpuid.h:224
a structure that holds a list of processor names
Definition: libcpuid.h:796
Definition: libcpuid.h:321
Definition: libcpuid.h:320
Definition: libcpuid.h:355
int32_t l3_cacheline
Definition: libcpuid.h:230
Definition: libcpuid.h:372
Definition: libcpuid.h:336
Definition: libcpuid.h:313
Definition: libcpuid.h:104
This contains the recognized CPU features/info.
Definition: libcpuid.h:139
Definition: libcpuid.h:358
Definition: libcpuid.h:416
int cpu_clock(void)
Get the CPU clock frequency (all-in-one method)
Definition: libcpuid.h:328
Definition: libcpuid.h:283
Definition: libcpuid.h:344
Definition: libcpuid.h:322
Definition: libcpuid.h:340
Definition: libcpuid.h:326
uint32_t intel_fn11[MAX_INTELFN11_LEVEL][4]
Definition: libcpuid.h:133
const char * cpu_feature_str(cpu_feature_t feature)
Returns the short textual representation of a CPU flag.
uint32_t intel_fn4[MAX_INTELFN4_LEVEL][4]
Definition: libcpuid.h:128
int32_t l2_cacheline
Definition: libcpuid.h:227
int32_t l2_assoc
Definition: libcpuid.h:218
int32_t num_logical_cpus
Definition: libcpuid.h:179
uint64_t sys_clock
Definition: libcpuid.h:431
Definition: libcpuid.h:335
struct msr_driver_t * cpu_msr_driver_open(void)
Starts/opens a driver, needed to read MSRs (Model Specific Registers)
Definition: libcpuid.h:363
int num_entries
Definition: libcpuid.h:798
int cpu_clock_by_ic(int millis, int runs)
Measure the CPU clock frequency using instruction-counting.
Definition: libcpuid.h:292
Definition: libcpuid.h:334
Definition: libcpuid.h:339
Definition: libcpuid.h:316
int cpuid_present(void)
Checks if the CPUID instruction is supported.
int32_t l1_assoc
Definition: libcpuid.h:215
int cpuid_get_total_cpus(void)
Returns the total number of CPUs even if CPUID is not present.
Definition: libcpuid.h:378
Definition: libcpuid.h:365
Definition: libcpuid.h:299
Definition: libcpuid.h:888
int32_t ext_model
Definition: libcpuid.h:169
Definition: libcpuid.h:345
Definition: libcpuid.h:101
Definition: libcpuid.h:286
Definition: libcpuid.h:343
int32_t l3_cache
Definition: libcpuid.h:212
Definition: libcpuid.h:303
Definition: libcpuid.h:284
Definition: libcpuid.h:308
Definition: libcpuid.h:290
Definition: libcpuid.h:305
Definition: libcpuid.h:371
Definition: libcpuid.h:422
Definition: libcpuid.h:342
Definition: libcpuid.h:314
Definition: libcpuid.h:410
Definition: libcpuid.h:100
Definition: libcpuid.h:384
Definition: libcpuid.h:95
int32_t l3_assoc
Definition: libcpuid.h:221
Definition: libcpuid.h:102
Definition: libcpuid.h:361
Definition: libcpuid.h:332
Definition: libcpuid.h:323
Definition: libcpuid.h:291
uint64_t tsc
Definition: libcpuid.h:430
Definition: libcpuid.h:377
Definition: libcpuid.h:406
Definition: libcpuid.h:306
Definition: libcpuid.h:409
Definition: libcpuid.h:352
int cpu_rdmsr_range(struct msr_driver_t *handle, uint32_t msr_index, uint8_t highbit, uint8_t lowbit, uint64_t *result)
Similar to cpu_rdmsr, but extract a range of bits.
Definition: libcpuid.h:421
Definition: libcpuid.h:324
Definition: libcpuid.h:368
Definition: libcpuid.h:294
Definition: libcpuid.h:296
cpu_vendor_t
CPU vendor, as guessed from the Vendor String.
Definition: libcpuid.h:94
Definition: libcpuid.h:350
Definition: libcpuid.h:302
uint8_t detection_hints[CPU_HINTS_MAX]
Definition: libcpuid.h:256
int cpu_msrinfo(struct msr_driver_t *handle, cpu_msrinfo_request_t which)
Reads extended CPU information from Model-Specific Registers.
Definition: libcpuid.h:902
Definition: libcpuid.h:97
uint32_t basic_cpuid[MAX_CPUID_LEVEL][4]
Definition: libcpuid.h:120
Definition: libcpuid.h:357
int32_t sse_size
Definition: libcpuid.h:249
Definition: libcpuid.h:359
Definition: libcpuid.h:385
cpu_vendor_t cpuid_get_vendor(void)
Obtains the CPU vendor from CPUID from the current CPU.
Definition: libcpuid.h:99
Definition: libcpuid.h:311
Definition: libcpuid.h:289
Definition: libcpuid.h:325
Definition: libcpuid.h:330
Definition: libcpuid.h:367
Definition: libcpuid.h:318
Definition: libcpuid.h:348
Definition: libcpuid.h:287
int32_t l1_instruction_cache
Definition: libcpuid.h:203
Definition: libcpuid.h:354
libcpuid_warn_fn_t cpuid_set_warn_function(libcpuid_warn_fn_t warn_fun)
Sets the warning print function.
void cpuid_free_cpu_list(struct cpu_list_t *list)
Frees a CPU list.
Definition: libcpuid.h:897
uint8_t flags[CPU_FLAGS_MAX]
Definition: libcpuid.h:154
uint32_t ext_cpuid[MAX_EXT_CPUID_LEVEL][4]
Definition: libcpuid.h:123
char brand_str[BRAND_STR_MAX]
Definition: libcpuid.h:144
int32_t stepping
Definition: libcpuid.h:163
const char * cpuid_error(void)
Returns textual description of the last error.
Definition: libcpuid.h:418
Definition: libcpuid.h:420
Definition: libcpuid.h:899
Definition: libcpuid.h:386
Definition: libcpuid.h:317
Definition: libcpuid.h:356
int32_t model
Definition: libcpuid.h:160
Definition: libcpuid.h:106
Definition: libcpuid.h:407
Definition: libcpuid.h:369
Definition: libcpuid.h:307
Definition: libcpuid.h:894
Definition: libcpuid.h:360
struct msr_driver_t * cpu_msr_driver_open_core(unsigned core_num)
Similar to cpu_msr_driver_open, but accept one parameter.
Definition: libcpuid.h:415
Definition: libcpuid.h:900
void cpu_exec_cpuid(uint32_t eax, uint32_t *regs)
Executes the CPUID instruction.
int32_t l2_cache
Definition: libcpuid.h:209
Definition: libcpuid.h:381
Definition: libcpuid.h:333
int cpu_clock_by_os(void)
Returns the CPU clock, as reported by the OS.
Definition: libcpuid.h:412
Definition: libcpuid.h:374
Definition: libcpuid.h:282
Definition: libcpuid.h:96
cpu_feature_t
CPU feature identifiers.
Definition: libcpuid.h:279
int32_t ext_family
Definition: libcpuid.h:166
Definition: libcpuid.h:293
Definition: libcpuid.h:327
Contains just the raw CPUID data.
Definition: libcpuid.h:118
Definition: libcpuid.h:351