SayoriOS  0.3.3
cputemp.c
1 #include "common.h"
2 #include "sys/cpuinfo.h"
3 #include "sys/msr.h"
4 #include "sys/cpuid.h"
5 
6 // X86 CPU Temperature support (partially based on memtest86+ code)
7 
8 size_t cputemp_calibrate_value = 0;
9 
15 bool is_temperature_module_present() {
16  if(cpu_get_id() == INTEL_MAGIC && get_max_cpuid_count() >= 6) { // Only possible on real hardware (not QEMU)
17  uint32_t info[4] = {0};
18 
19  cpuid(0x6,
20  info[0],
21  info[1], // Not needed
22  info[2], // Not needed
23  info[3] // Not needed
24  );
25 
26  return (bool)(info[0] & 1);
27  }
28 
29  return false;
30 }
31 
36 void cputemp_calibrate() {
37  uint32_t a = 0;
38  uint32_t b = 0;
39 
40  if(is_temperature_module_present()) {
41  rdmsr(INTEL_TEMPERATURE_TARGET, a, b);
42  cputemp_calibrate_value = (a >> 16) & 0x7F;
43 
44  // From memtest86+ code
45 
46  if (cputemp_calibrate_value < 50 || cputemp_calibrate_value > 125) {
47  cputemp_calibrate_value = 100;
48  }
49  }
50 }
51 
57 size_t get_cpu_temperature() {
58  if(is_temperature_module_present()) {
59  uint32_t a = 0;
60  uint32_t b = 0;
61 
62  rdmsr(INTEL_THERMAL_STATUS, a, b);
63 
64  uint32_t absolute = (a >> 16) & 0x7F;
65 
66  return cputemp_calibrate_value - absolute;
67  }
68 
69  return 0;
70 }
Основные определения ядра
#define INTEL_MAGIC
Ключ процессора Intel.
Definition: cpuinfo.c:21