miner.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #include <curl/curl.h>
  10. #ifdef HAVE_OPENCL
  11. #ifdef __APPLE_CC__
  12. #include <OpenCL/opencl.h>
  13. #else
  14. #include <CL/cl.h>
  15. #endif
  16. #endif /* HAVE_OPENCL */
  17. #ifdef STDC_HEADERS
  18. # include <stdlib.h>
  19. # include <stddef.h>
  20. #else
  21. # ifdef HAVE_STDLIB_H
  22. # include <stdlib.h>
  23. # endif
  24. #endif
  25. #ifdef HAVE_ALLOCA_H
  26. # include <alloca.h>
  27. #elif defined __GNUC__
  28. # define alloca __builtin_alloca
  29. #elif defined _AIX
  30. # define alloca __alloca
  31. #elif defined _MSC_VER
  32. # include <malloc.h>
  33. # define alloca _alloca
  34. #else
  35. # ifndef HAVE_ALLOCA
  36. # ifdef __cplusplus
  37. extern "C"
  38. # endif
  39. void *alloca (size_t);
  40. # endif
  41. #endif
  42. #ifdef __SSE2__
  43. #define WANT_SSE2_4WAY 1
  44. #endif
  45. #if defined(__i386__) || defined(__x86_64__)
  46. #define WANT_VIA_PADLOCK 1
  47. #endif
  48. #if defined(__x86_64__) && defined(__SSE2__) && defined(HAS_YASM)
  49. #define WANT_X8664_SSE2 1
  50. #endif
  51. #if defined(__x86_64__) && defined(__SSE4_1__) && defined(HAS_YASM)
  52. #define WANT_X8664_SSE4 1
  53. #endif
  54. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  55. #define WANT_BUILTIN_BSWAP
  56. #else
  57. #if HAVE_BYTESWAP_H
  58. #include <byteswap.h>
  59. #elif defined(USE_SYS_ENDIAN_H)
  60. #include <sys/endian.h>
  61. #elif defined(__APPLE__)
  62. #include <libkern/OSByteOrder.h>
  63. #define bswap_16 OSSwapInt16
  64. #define bswap_32 OSSwapInt32
  65. #define bswap_64 OSSwapInt64
  66. #else
  67. #define bswap_16(value) \
  68. ((((value) & 0xff) << 8) | ((value) >> 8))
  69. #define bswap_32(value) \
  70. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  71. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  72. #define bswap_64(value) \
  73. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  74. << 32) | \
  75. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  76. #endif
  77. #endif /* !defined(__GLXBYTEORDER_H__) */
  78. /* This assumes htobe32 is a macro in endian.h */
  79. #ifndef htobe32
  80. # if __BYTE_ORDER == __LITTLE_ENDIAN
  81. # define be32toh(x) bswap_32(x)
  82. # define htobe32(x) bswap_32(x)
  83. # elif __BYTE_ORDER == __BIG_ENDIAN
  84. # define be32toh(x) (x)
  85. # define htobe32(x) (x)
  86. #else
  87. #error UNKNOWN BYTE ORDER
  88. #endif
  89. #endif
  90. #ifdef HAVE_SYSLOG_H
  91. #include <syslog.h>
  92. #else
  93. enum {
  94. LOG_ERR,
  95. LOG_WARNING,
  96. LOG_INFO,
  97. LOG_DEBUG,
  98. };
  99. #endif
  100. #undef unlikely
  101. #undef likely
  102. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  103. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  104. #define likely(expr) (__builtin_expect(!!(expr), 1))
  105. #else
  106. #define unlikely(expr) (expr)
  107. #define likely(expr) (expr)
  108. #endif
  109. #if defined(__i386__)
  110. #define WANT_CRYPTOPP_ASM32
  111. #endif
  112. #ifndef ARRAY_SIZE
  113. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  114. #endif
  115. struct cgpu_info {
  116. int is_gpu;
  117. int cpu_gpu;
  118. int accepted;
  119. int rejected;
  120. int hw_errors;
  121. double local_mhashes;
  122. double total_mhashes;
  123. unsigned int getworks;
  124. double efficiency;
  125. double utility;
  126. };
  127. struct thr_info {
  128. int id;
  129. pthread_t pth;
  130. struct thread_q *q;
  131. struct cgpu_info *cgpu;
  132. struct timeval last;
  133. bool getwork;
  134. };
  135. static inline uint32_t swab32(uint32_t v)
  136. {
  137. #ifdef WANT_BUILTIN_BSWAP
  138. return __builtin_bswap32(v);
  139. #else
  140. return bswap_32(v);
  141. #endif
  142. }
  143. static inline void swap256(void *dest_p, const void *src_p)
  144. {
  145. uint32_t *dest = dest_p;
  146. const uint32_t *src = src_p;
  147. dest[0] = src[7];
  148. dest[1] = src[6];
  149. dest[2] = src[5];
  150. dest[3] = src[4];
  151. dest[4] = src[3];
  152. dest[5] = src[2];
  153. dest[6] = src[1];
  154. dest[7] = src[0];
  155. }
  156. struct pool;
  157. extern bool opt_debug;
  158. extern bool opt_protocol;
  159. extern bool opt_log_output;
  160. extern const uint32_t sha256_init_state[];
  161. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  162. const char *rpc_req, bool, bool, struct pool *pool);
  163. extern char *bin2hex(const unsigned char *p, size_t len);
  164. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  165. extern unsigned int ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  166. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  167. const unsigned char *ptarget,
  168. uint32_t max_nonce, unsigned long *nHashesDone, uint32_t nonce);
  169. extern unsigned int scanhash_sse2_amd64(int, const unsigned char *pmidstate,
  170. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  171. const unsigned char *ptarget,
  172. uint32_t max_nonce, unsigned long *nHashesDone);
  173. extern bool scanhash_via(int, unsigned char *data_inout,
  174. const unsigned char *target,
  175. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  176. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  177. unsigned char *hash1, unsigned char *hash,
  178. const unsigned char *target,
  179. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  180. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  181. unsigned char *hash1, unsigned char *hash,
  182. const unsigned char *target,
  183. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  184. extern bool scanhash_asm32(int, const unsigned char *midstate,unsigned char *data,
  185. unsigned char *hash1, unsigned char *hash,
  186. const unsigned char *target,
  187. uint32_t max_nonce, unsigned long *hashes_done, uint32_t nonce);
  188. extern int scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  189. unsigned char *phash1, unsigned char *phash,
  190. const unsigned char *ptarget,
  191. uint32_t max_nonce, unsigned long *nHashesDone,
  192. uint32_t nonce);
  193. extern int scanhash_sse4_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  194. unsigned char *phash1, unsigned char *phash,
  195. const unsigned char *ptarget,
  196. uint32_t max_nonce, unsigned long *nHashesDone,
  197. uint32_t nonce);
  198. extern int
  199. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  200. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  201. extern int opt_scantime;
  202. extern bool want_longpoll;
  203. extern bool have_longpoll;
  204. struct thread_q;
  205. struct work_restart {
  206. volatile unsigned long restart;
  207. char padding[128 - sizeof(unsigned long)];
  208. };
  209. extern int hw_errors;
  210. extern bool use_syslog;
  211. extern struct thr_info *thr_info;
  212. extern int longpoll_thr_id;
  213. extern struct work_restart *work_restart;
  214. extern pthread_mutex_t control_lock;
  215. #ifdef HAVE_OPENCL
  216. typedef struct {
  217. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  218. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  219. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  220. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  221. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  222. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  223. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  224. cl_uint W16; cl_uint W17; cl_uint W2;
  225. cl_uint PreVal4; cl_uint T1;
  226. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  227. cl_uint PreVal4addT1; cl_uint T1substate0;
  228. } dev_blk_ctx;
  229. #else
  230. typedef struct {
  231. uint32_t nonce;
  232. } dev_blk_ctx;
  233. #endif
  234. struct pool {
  235. int pool_no;
  236. int prio;
  237. int accepted, rejected;
  238. bool submit_fail;
  239. bool idle;
  240. bool has_rolltime;
  241. bool probed;
  242. bool enabled;
  243. unsigned int getwork_requested;
  244. unsigned int stale_shares;
  245. unsigned int discarded_work;
  246. unsigned int localgen_occasions;
  247. unsigned int remotefail_occasions;
  248. struct timeval tv_idle;
  249. char *rpc_url;
  250. char *rpc_userpass;
  251. char *rpc_user, *rpc_pass;
  252. pthread_mutex_t pool_lock;
  253. };
  254. struct work {
  255. unsigned char data[128];
  256. unsigned char hash1[64];
  257. unsigned char midstate[32];
  258. unsigned char target[32];
  259. unsigned char hash[32];
  260. int rolls;
  261. uint32_t output[1];
  262. uint32_t res_nonce;
  263. uint32_t valid;
  264. dev_blk_ctx blk;
  265. int thr_id;
  266. struct pool *pool;
  267. };
  268. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  269. extern void kill_work(void);
  270. extern void log_curses(const char *f, va_list ap);
  271. extern void vapplog(int prio, const char *fmt, va_list ap);
  272. extern void applog(int prio, const char *fmt, ...);
  273. extern struct thread_q *tq_new(void);
  274. extern void tq_free(struct thread_q *tq);
  275. extern bool tq_push(struct thread_q *tq, void *data);
  276. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  277. extern void tq_freeze(struct thread_q *tq);
  278. extern void tq_thaw(struct thread_q *tq);
  279. extern bool test_and_set(bool *var);
  280. extern bool test_and_clear(bool *var);
  281. extern bool successful_connect;
  282. #endif /* __MINER_H__ */