CPU Mining Support
Overview
CPU mining support is implemented by the cpu device driver in driver-cpu.c, with build-time algorithm declarations in driver-cpu.h and user-facing caveats/options in README.CPU. The driver is registered as cpu_drv and integrates with the miner core through the struct device_drv callbacks at the end of driver-cpu.c: detection (drv_detect), per-thread preparation and initialization (thread_prepare, thread_init), work limiting (can_limit_work), minimum nonce difficulty (drv_min_nonce_diff), and the hashing loop (scanhash).
Discovery is deliberately explicit rather than automatic user-facing CPU mining. README.CPU states that CPU mining is disabled by default, must be compiled in, and must be enabled with -S cpu:auto. At runtime, cpu_detect() in driver-cpu.c calls noserial_detect_manual(&cpu_drv, cpu_autodetect), so CPU devices are exposed through the miner's manual/no-serial device path rather than physical device probing. cpu_autodetect() determines num_processors from platform APIs, defaults opt_n_threads to the processor count unless --cpu-threads was forced, allocates one struct cgpu_info per requested CPU thread, marks each enabled, assigns one miner thread per CPU cgpu, sets the displayed kernel name to the selected SHA256 implementation when SHA256D is compiled, and registers each cgpu with add_cgpu().
Thread lifecycle is split between cpu_thread_prepare() and cpu_thread_init() in driver-cpu.c. Preparation initializes the shared CPU algorithm mutex once and calls thread_reportin(thr). Initialization is where CPU miner threads become low-priority background work: it selects the SHA256 implementation if --algo auto or --algo fastauto is in effect, updates cgpu->kname, sets process priority to nice 19, attempts SCHED_IDLE/SCHED_BATCH on Linux via drop_policy(), and binds threads to CPUs with sched_setaffinity() when the configured thread count is an even multiple of detected processors.
Work consumption happens in cpu_scanhash() in driver-cpu.c. The miner core passes a struct work and a maximum nonce; the CPU driver starts from work->blk.nonce, selects an algorithm-specific scan function, runs it until a share candidate, restart, or nonce limit, and returns the number of hashes attempted. If the scan function reports a candidate, cpu_scanhash() submits it through submit_nonce(thr, work, le32toh(*(uint32_t*)&work->data[76])), advances work->blk.nonce, and loops to continue searching the same work range for additional candidates. If no progress was made it returns 0; otherwise it advances work->blk.nonce past the last tested nonce.
Hash helper selection depends on the work's mining algorithm and the configured CPU algorithm. driver-cpu.h defines the enum sha256_algos values behind build flags such as USE_SHA256D, USE_SCRYPT, HAVE_SSE2, HAVE_YASM, architecture macros, and Altivec support. driver-cpu.c maps those enum values to algo_names[] for CLI/config display and, for SHA256D, to sha256_funcs[] entries such as scanhash_c, ScanHash_4WaySSE2, scanhash_via, scanhash_cryptopp, scanhash_sse2_64, scanhash_sse4_64, and ScanHash_altivec_4way. For scrypt work, cpu_scanhash() selects scanhash_scrypt when USE_SCRYPT is enabled. For other mining algorithms or SHA256D work below nonce difficulty 1, it falls back to scanhash_generic(), which uses work_mining_algorithm(work)->hash_data_f to hash work->data, writes the nonce at byte offset 0x4c, compares the last 32 bits of work->hash to the target, and stops on match, max_nonce, or thr->work_restart.
Algorithm auto-selection is SHA256D-specific. set_algo() and show_algo() in driver-cpu.c back the --algo option described in README.CPU. opt_algo defaults to ALGO_FASTAUTO, and the first CPU thread to initialize under ALGO_AUTO or ALGO_FASTAUTO calls pick_fastest_algo() while holding cpualgo_lock. The benchmark path runs each compiled SHA256D implementation against benchmark work from get_benchmark_work(). On Unix, bench_algo_stage2() forks a child and exchanges the measured rate over a nonblocking pipe so crash-prone optimized hashers do not take down the parent process; on Windows it launches a debug child and uses shared memory; on other platforms it runs the benchmark directly. fastauto uses a much smaller nonce range than auto, so it checks viability quickly but may choose with less measurement precision.
CPU configuration is intentionally small. README.CPU documents --algo <arg> and --cpu-threads <arg>. force_nthreads_int() in driver-cpu.c marks the thread count as forced before validating the integer with the shared set_int_range() helper, allowing cpu_autodetect() to distinguish an explicit CPU-thread setting from the default -1. cpu_can_limit_work() returns 0xffff, allowing the miner core to limit nonce chunks for CPU work, while cpu_min_nonce_diff() returns the global minimum_pdiff for CPU devices.
Coupling
- If a CPU SHA256 implementation is added or removed, update both the enum/build guards in driver-cpu.h and the corresponding
algo_names[],sha256_funcs[], andpick_fastest_algo()entries in driver-cpu.c;README.CPUshould also be kept in sync because it is the user-visible list for--algo. - If the miner core changes the layout of
struct worknonce fields, verifycpu_scanhash()andscanhash_generic()in driver-cpu.c: they use bothwork->blk.nonceand raw nonce offsets inwork->data(0x4c/byte 76) when scanning and submitting shares. - If mining algorithm dispatch changes, verify the CPU fallback path in driver-cpu.c;
cpu_scanhash()decides betweenscanhash_scrypt, SHA256D optimized functions, andscanhash_generic()usingwork_mining_algorithm(work)->algo, whilescanhash_generic()depends onhash_data_fand target/hash buffers supplied by the core work object. - If CPU thread-count option parsing changes outside this driver, preserve the
forced_n_threadsbehavior inforce_nthreads_int()andcpu_autodetect(); otherwise an explicit--cpu-threadsvalue can be silently overwritten by processor autodetection.
Risks
- driver-cpu.c directly reads and writes nonce bytes in
work->dataat fixed offsets inscanhash_generic()andcpu_scanhash(). If the work buffer layout or endian convention changes, CPU mining can submit the wrong nonce or fail to find valid shares even while the thread appears to hash normally. - driver-cpu.c mutates the global
opt_algofromALGO_AUTO/ALGO_FASTAUTOto the chosen concrete algorithm duringcpu_thread_init(). The mutex prevents simultaneous selection, but all CPU devices share the same chosen implementation; mixed CPU capabilities within one process are not represented. - driver-cpu.c's Unix auto-benchmark path intentionally executes potentially crashing hash implementations in a forked child. Platforms that fall through to the direct
bench_algo_stage3()path do not get that isolation, so a bad optimized implementation selected by--algo autocan terminate the miner process.
Coverage
- driver-cpu.c
- driver-cpu.h
README.CPU