Skip to content

Commit

Permalink
Merge bitcoin#17527: Fix CPUID subleaf iteration
Browse files Browse the repository at this point in the history
f93fc61 Put bounds on the number of CPUID leaves explored (Pieter Wuille)
ba2c5fe Fix CPUID subleaf iteration (Pieter Wuille)

Pull request description:

  This fixes bitcoin#17523.

  The code to determine which CPUID subleaves to explore was incorrect in bitcoin#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 f93fc61
  jonatack:
    ACK f93fc61 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
  • Loading branch information
laanwj authored and sidhujag committed Nov 24, 2019
1 parent 97a74a7 commit c14e5d9
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/randomenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit c14e5d9

Please sign in to comment.