Merge #17527: Fix CPUID subleaf iteration

f93fc61c65 Put bounds on the number of CPUID leaves explored (Pieter Wuille)
ba2c5fe147 Fix CPUID subleaf iteration (Pieter Wuille)

Pull request description:

  This fixes #17523.

  The code to determine which CPUID subleaves to explore was incorrect in #17270. The new code here is based on Intel's reference documentation for CPUID (a document called "Intel® Processor Identification and the CPUID Instruction - Application Note 485", which I cannot actually find on their own website).

ACKs for top commit:
  laanwj:
    ACK f93fc61c65
  jonatack:
    ACK f93fc61c65 code review, tested rebased on current master bb862d7 with Debian 4.19 x86_64
  mzumsande:
    ACK f93fc61, reviewed code and compared with the intel doc, tested on an AMD and an Intel processor.

Tree-SHA512: 2790b326fa397b736c0f39f25807bea57de2752fdd58bf6693d044b8cb26df36c11cce165a334b471f8e33724f10e3b76edab5cc4e0e7776601aabda13277245
This commit is contained in:
Wladimir J. van der Laan 2019-11-24 11:19:27 +01:00
commit 2eeacdfe44
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D

View File

@ -197,19 +197,30 @@ void AddAllCPUID(CSHA512& hasher)
// Iterate over all standard leaves
AddCPUID(hasher, 0, 0, ax, bx, cx, dx); // Returns max leaf in ax
uint32_t max = ax;
for (uint32_t leaf = 1; leaf <= max; ++leaf) {
for (uint32_t subleaf = 0;; ++subleaf) {
for (uint32_t leaf = 1; leaf <= max && leaf <= 0xFF; ++leaf) {
uint32_t maxsub = 0;
for (uint32_t subleaf = 0; subleaf <= 0xFF; ++subleaf) {
AddCPUID(hasher, leaf, subleaf, ax, bx, cx, dx);
// Iterate over subleaves for leaf 4, 11, 13
if (leaf != 4 && leaf != 11 && leaf != 13) break;
if ((leaf == 4 || leaf == 13) && ax == 0) break;
if (leaf == 11 && (cx & 0xFF00) == 0) break;
// Iterate subleafs for leaf values 4, 7, 11, 13
if (leaf == 4) {
if ((ax & 0x1f) == 0) break;
} else if (leaf == 7) {
if (subleaf == 0) maxsub = ax;
if (subleaf == maxsub) break;
} else if (leaf == 11) {
if ((cx & 0xff00) == 0) break;
} else if (leaf == 13) {
if (ax == 0 && bx == 0 && cx == 0 && dx == 0) break;
} else {
// For any other leaf, stop after subleaf 0.
break;
}
}
}
// Iterate over all extended leaves
AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
uint32_t ext_max = ax;
for (uint32_t leaf = 0x80000001; leaf <= ext_max; ++leaf) {
for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
}
}