2018-03-18 17:29:44 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-03-18 17:29:44 +11:00
|
|
|
*
|
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpuid_x86.hpp"
|
|
|
|
|
|
|
|
#include "endian.hpp"
|
|
|
|
#include "view.hpp"
|
|
|
|
#include "format.hpp"
|
|
|
|
#include "bitwise.hpp"
|
|
|
|
|
|
|
|
#include <cstdint>
|
2018-03-22 16:10:06 +11:00
|
|
|
#include <ostream>
|
2018-03-18 17:29:44 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::cpu::x86;
|
2018-03-18 17:29:44 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
auto cpuid (int query, int param = 0)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
uint32_t a, b, c, d;
|
|
|
|
} res;
|
|
|
|
|
|
|
|
asm (
|
|
|
|
"cpuid"
|
|
|
|
: "=a" (res.a), "=b" (res.b), "=c" (res.c), "=d" (res.d)
|
|
|
|
: "a" (query), "c" (param)
|
|
|
|
);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
x86::x86 ()
|
|
|
|
{
|
|
|
|
const auto &[largest_function, vendor0, vendor2, vendor1] = cpuid (0);
|
|
|
|
(void)largest_function;
|
|
|
|
|
|
|
|
memcpy (vendor_name.data () + 0, &vendor0, sizeof (vendor0));
|
|
|
|
memcpy (vendor_name.data () + 4, &vendor1, sizeof (vendor1));
|
|
|
|
memcpy (vendor_name.data () + 8, &vendor2, sizeof (vendor2));
|
|
|
|
|
|
|
|
const bool is_amd = vendor0 == 0x68747541 && vendor1 == 0x69746e65 && vendor2 == 0x444d4163;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto features = cpuid (1);
|
|
|
|
|
|
|
|
simd.sse3 = features.c & (1u << 0);
|
|
|
|
simd.ssse3 = features.c & (1u << 9);
|
|
|
|
simd.sse41 = features.c & (1u << 19);
|
|
|
|
simd.sse42 = features.c & (1u << 20);
|
|
|
|
simd.avx = features.c & (1u << 28);
|
|
|
|
simd.sse = features.d & (1u << 25);
|
|
|
|
simd.sse2 = features.d & (1u << 26);
|
|
|
|
|
|
|
|
cores.hyper_threading = features.d & (1u << 28);
|
|
|
|
cores.logical = from_bits (features.b, 23, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto product0 = cpuid (0x8000'0002);
|
|
|
|
const auto product1 = cpuid (0x8000'0003);
|
|
|
|
const auto product2 = cpuid (0x8000'0004);
|
|
|
|
|
|
|
|
memcpy (&product_name[0x00], &product0, sizeof (product0));
|
|
|
|
memcpy (&product_name[0x10], &product1, sizeof (product1));
|
|
|
|
memcpy (&product_name[0x20], &product2, sizeof (product2));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function 4 isn't implemented by AMD. Intel uses it for cache
|
|
|
|
// descriptors. It has some useful information (like procesor counts) but
|
|
|
|
// it's ugly as sin; try to avoid using it.
|
|
|
|
// { (void)cpuid (0x0b, 4); }
|
|
|
|
|
|
|
|
if (is_amd) {
|
|
|
|
auto long_mode = cpuid (0x8000'0008);
|
|
|
|
auto apic_id_size = from_bits (long_mode.c, 15, 12);
|
|
|
|
(void)apic_id_size;
|
|
|
|
cores.physical = 0; //from_bits (long_mode.c, 7, 0) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
std::ostream&
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::cpu::operator<< (std::ostream &os, const x86 &val)
|
2018-03-18 17:29:44 +11:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
return os << cruft::format::printf (
|
2018-03-18 17:29:44 +11:00
|
|
|
"{ name: { vendor: '%!', product: '%!' }"
|
|
|
|
", cores: { logical: %!, physical: %!, hyper_threading: %! }"
|
|
|
|
", simd { sse: %!, sse2: %!, sse3: %!, ssse3: %!, sse41: %!, sse42: %! }"
|
|
|
|
" }",
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::view {val.vendor_name},
|
|
|
|
cruft::view {val.product_name},
|
2018-03-18 17:29:44 +11:00
|
|
|
val.cores.logical,
|
|
|
|
val.cores.physical,
|
|
|
|
val.cores.hyper_threading,
|
|
|
|
val.simd.sse,
|
|
|
|
val.simd.sse2,
|
|
|
|
val.simd.sse3,
|
|
|
|
val.simd.ssse3,
|
|
|
|
val.simd.sse41,
|
|
|
|
val.simd.sse42
|
|
|
|
);
|
|
|
|
}
|