miner.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  52. #define WANT_BUILTIN_BSWAP
  53. #else
  54. #if HAVE_BYTESWAP_H
  55. #include <byteswap.h>
  56. #elif defined(USE_SYS_ENDIAN_H)
  57. #include <sys/endian.h>
  58. #elif defined(__APPLE__)
  59. #include <libkern/OSByteOrder.h>
  60. #define bswap_16 OSSwapInt16
  61. #define bswap_32 OSSwapInt32
  62. #define bswap_64 OSSwapInt64
  63. #else
  64. #define bswap_16(value) \
  65. ((((value) & 0xff) << 8) | ((value) >> 8))
  66. #define bswap_32(value) \
  67. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  68. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  69. #define bswap_64(value) \
  70. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  71. << 32) | \
  72. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  73. #endif
  74. #endif /* !defined(__GLXBYTEORDER_H__) */
  75. /* This assumes htobe32 is a macro in endian.h */
  76. #ifndef htobe32
  77. # if __BYTE_ORDER == __LITTLE_ENDIAN
  78. # define be32toh(x) bswap_32(x)
  79. # define htobe32(x) bswap_32(x)
  80. # elif __BYTE_ORDER == __BIG_ENDIAN
  81. # define be32toh(x) (x)
  82. # define htobe32(x) (x)
  83. #else
  84. #error UNKNOWN BYTE ORDER
  85. #endif
  86. #endif
  87. #ifdef HAVE_SYSLOG_H
  88. #include <syslog.h>
  89. #else
  90. enum {
  91. LOG_ERR,
  92. LOG_WARNING,
  93. LOG_INFO,
  94. LOG_DEBUG,
  95. };
  96. #endif
  97. #undef unlikely
  98. #undef likely
  99. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  100. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  101. #define likely(expr) (__builtin_expect(!!(expr), 1))
  102. #else
  103. #define unlikely(expr) (expr)
  104. #define likely(expr) (expr)
  105. #endif
  106. #if defined(__i386__)
  107. #define WANT_CRYPTOPP_ASM32
  108. #endif
  109. #ifndef ARRAY_SIZE
  110. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  111. #endif
  112. struct cgpu_info {
  113. int is_gpu;
  114. int cpu_gpu;
  115. int accepted;
  116. int rejected;
  117. int hw_errors;
  118. double local_mhashes;
  119. double total_mhashes;
  120. unsigned int getworks;
  121. double efficiency;
  122. double utility;
  123. };
  124. struct thr_info {
  125. int id;
  126. pthread_t pth;
  127. struct thread_q *q;
  128. struct cgpu_info *cgpu;
  129. struct timeval last;
  130. };
  131. static inline uint32_t swab32(uint32_t v)
  132. {
  133. #ifdef WANT_BUILTIN_BSWAP
  134. return __builtin_bswap32(v);
  135. #else
  136. return bswap_32(v);
  137. #endif
  138. }
  139. static inline void swap256(void *dest_p, const void *src_p)
  140. {
  141. uint32_t *dest = dest_p;
  142. const uint32_t *src = src_p;
  143. dest[0] = src[7];
  144. dest[1] = src[6];
  145. dest[2] = src[5];
  146. dest[3] = src[4];
  147. dest[4] = src[3];
  148. dest[5] = src[2];
  149. dest[6] = src[1];
  150. dest[7] = src[0];
  151. }
  152. extern bool opt_debug;
  153. extern bool opt_protocol;
  154. extern bool opt_log_output;
  155. extern const uint32_t sha256_init_state[];
  156. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  157. const char *rpc_req, bool, bool);
  158. extern char *bin2hex(const unsigned char *p, size_t len);
  159. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  160. extern unsigned int ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  161. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  162. const unsigned char *ptarget,
  163. uint32_t max_nonce, unsigned long *nHashesDone, uint32_t nonce);
  164. extern unsigned int scanhash_sse2_amd64(int, const unsigned char *pmidstate,
  165. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  166. const unsigned char *ptarget,
  167. uint32_t max_nonce, unsigned long *nHashesDone);
  168. extern bool scanhash_via(int, unsigned char *data_inout,
  169. const unsigned char *target,
  170. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  171. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  172. unsigned char *hash1, unsigned char *hash,
  173. const unsigned char *target,
  174. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  175. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  176. unsigned char *hash1, unsigned char *hash,
  177. const unsigned char *target,
  178. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  179. extern bool scanhash_asm32(int, const unsigned char *midstate,unsigned char *data,
  180. unsigned char *hash1, unsigned char *hash,
  181. const unsigned char *target,
  182. uint32_t max_nonce, unsigned long *hashes_done, uint32_t nonce);
  183. extern int scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  184. unsigned char *phash1, unsigned char *phash,
  185. const unsigned char *ptarget,
  186. uint32_t max_nonce, unsigned long *nHashesDone,
  187. uint32_t nonce);
  188. extern int
  189. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  190. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  191. extern int opt_scantime;
  192. extern bool want_longpoll;
  193. extern bool have_longpoll;
  194. struct thread_q;
  195. struct work_restart {
  196. volatile unsigned long restart;
  197. char padding[128 - sizeof(unsigned long)];
  198. };
  199. extern int hw_errors;
  200. extern bool use_syslog;
  201. extern struct thr_info *thr_info;
  202. extern int longpoll_thr_id;
  203. extern struct work_restart *work_restart;
  204. #ifdef HAVE_OPENCL
  205. typedef struct {
  206. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  207. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  208. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  209. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  210. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  211. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  212. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  213. cl_uint W16; cl_uint W17; cl_uint W2;
  214. cl_uint PreVal4; cl_uint T1;
  215. } dev_blk_ctx;
  216. #else
  217. typedef struct {
  218. uint32_t nonce;
  219. } dev_blk_ctx;
  220. #endif
  221. struct work {
  222. unsigned char data[128];
  223. unsigned char hash1[64];
  224. unsigned char midstate[32];
  225. unsigned char target[32];
  226. unsigned char hash[32];
  227. uint32_t output[1];
  228. uint32_t res_nonce;
  229. uint32_t valid;
  230. dev_blk_ctx blk;
  231. int thr_id;
  232. };
  233. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  234. extern void kill_work(void);
  235. extern void log_curses(const char *f, va_list ap);
  236. extern void vapplog(int prio, const char *fmt, va_list ap);
  237. extern void applog(int prio, const char *fmt, ...);
  238. extern struct thread_q *tq_new(void);
  239. extern void tq_free(struct thread_q *tq);
  240. extern bool tq_push(struct thread_q *tq, void *data);
  241. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  242. extern void tq_freeze(struct thread_q *tq);
  243. extern void tq_thaw(struct thread_q *tq);
  244. extern bool successful_connect;
  245. #endif /* __MINER_H__ */