+/* --- @rdrand_works_p@ --- *
+ *
+ *
+ * Arguments: ---
+ *
+ * Returns: Nonzero if the `rdrand' instruction actually works. Assumes
+ * that it's already been verified to be safe to issue.
+ */
+
+enum { OP_RDRAND, OP_RDSEED };
+
+static int rdrand_works_p(unsigned op)
+{
+ unsigned ref, x, i;
+ const char *what;
+
+ switch (op) {
+ case OP_RDRAND: what = "RDRAND"; break;
+ case OP_RDSEED: what = "RDSEED"; break;
+ default: assert(!"unexpected op");
+ }
+
+ /* Check that it doesn't always give the same answer. Try four times: this
+ * will fail with probability %$2^{-128}$% with a truly random generator,
+ * which seems fair enough.
+ */
+ if (dispatch_x86ish_rdrand(op, &ref)) goto fail;
+ for (i = 0; i < 4; i++) {
+ if (dispatch_x86ish_rdrand(op, &x)) goto fail;
+ if (x != ref) goto not_stuck;
+ }
+ dispatch_debug("%s always returns 0x%08x!", what, ref);
+ return (0);
+
+not_stuck:
+ dispatch_debug("%s instruction looks plausible", what);
+ return (1);
+
+fail:
+ dispatch_debug("%s instruction fails too often", what);
+ return (0);
+}
+