| 1 |
#include <assert.h> |
| 2 |
#include <stddef.h> |
| 3 |
#include <stdint.h> |
| 4 |
#include <string.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <unistd.h> |
| 7 |
#if !(defined(_MSC_VER) || defined(__MINGW32__)) |
| 8 |
#include <sys/mman.h> |
| 9 |
#endif |
| 10 |
|
| 11 |
#include "int-util.h" |
| 12 |
#include "hash-ops.h" |
| 13 |
#include "variant4_random_math.h" |
| 14 |
#include "CryptonightR_JIT.h" |
| 15 |
#include "CryptonightR_template.h" |
| 16 |
|
| 17 |
static const uint8_t prologue[] = { |
| 18 |
#if defined __i386 || defined __x86_64__ |
| 19 |
0x4C, 0x8B, 0xD7, |
| 20 |
0x53, |
| 21 |
0x55, |
| 22 |
0x41, 0x57, |
| 23 |
0x4C, 0x8B, 0xDC, |
| 24 |
0x41, 0x8B, 0x1A, |
| 25 |
0x41, 0x8B, 0x72, 0x04, |
| 26 |
0x41, 0x8B, 0x7A, 0x08, |
| 27 |
0x41, 0x8B, 0x6A, 0x0C, |
| 28 |
0x41, 0x8B, 0x62, 0x10, |
| 29 |
0x45, 0x8B, 0x7A, 0x14, |
| 30 |
0x41, 0x8B, 0x42, 0x18, |
| 31 |
0x41, 0x8B, 0x52, 0x1C, |
| 32 |
0x45, 0x8B, 0x4A, 0x20, |
| 33 |
#endif |
| 34 |
}; |
| 35 |
|
| 36 |
static const uint8_t epilogue[] = { |
| 37 |
#if defined __i386 || defined __x86_64__ |
| 38 |
0x49, 0x8B, 0xE3, |
| 39 |
0x41, 0x89, 0x1A, |
| 40 |
0x41, 0x89, 0x72, 0x04, |
| 41 |
0x41, 0x89, 0x7A, 0x08, |
| 42 |
0x41, 0x89, 0x6A, 0x0C, |
| 43 |
0x41, 0x5F, |
| 44 |
0x5D, |
| 45 |
0x5B, |
| 46 |
0xC3, |
| 47 |
#endif |
| 48 |
}; |
| 49 |
|
| 50 |
#define APPEND_CODE(src, size) \ |
| 51 |
do { \ |
| 52 |
if (JIT_code + (size) > JIT_code_end) \ |
| 53 |
return -1; \ |
| 54 |
memcpy(JIT_code, (src), (size)); \ |
| 55 |
JIT_code += (size); \ |
| 56 |
} while (0) |
| 57 |
|
| 58 |
int v4_generate_JIT_code(const struct V4_Instruction* code, v4_random_math_JIT_func buf, const size_t buf_size) |
| 59 |
{ |
| 60 |
#if defined __i386 || defined __x86_64__ |
| 61 |
uint8_t* JIT_code = (uint8_t*) buf; |
| 62 |
const uint8_t* JIT_code_end = JIT_code + buf_size; |
| 63 |
|
| 64 |
#if !(defined(_MSC_VER) || defined(__MINGW32__)) |
| 65 |
if (mprotect((void*)buf, buf_size, PROT_READ | PROT_WRITE)) |
| 66 |
return -1; |
| 67 |
#endif |
| 68 |
|
| 69 |
APPEND_CODE(prologue, sizeof(prologue)); |
| 70 |
|
| 71 |
uint32_t prev_rot_src = 0xFFFFFFFFU; |
| 72 |
|
| 73 |
for (int i = 0;; ++i) |
| 74 |
{ |
| 75 |
const struct V4_Instruction inst = code[i]; |
| 76 |
if (inst.opcode == RET) |
| 77 |
break; |
| 78 |
|
| 79 |
const uint8_t opcode = (inst.opcode == MUL) ? inst.opcode : (inst.opcode + 2); |
| 80 |
|
| 81 |
const uint32_t a = inst.dst_index; |
| 82 |
const uint32_t b = inst.src_index; |
| 83 |
const uint8_t c = opcode | (inst.dst_index << V4_OPCODE_BITS) | (((inst.src_index == 8) ? inst.dst_index : inst.src_index) << (V4_OPCODE_BITS + V4_DST_INDEX_BITS)); |
| 84 |
|
| 85 |
switch (inst.opcode) |
| 86 |
{ |
| 87 |
case ROR: |
| 88 |
case ROL: |
| 89 |
if (b != prev_rot_src) |
| 90 |
{ |
| 91 |
prev_rot_src = b; |
| 92 |
const uint8_t* p1 = (const uint8_t*) instructions_mov[c]; |
| 93 |
const uint8_t* p2 = (const uint8_t*) instructions_mov[c + 1]; |
| 94 |
APPEND_CODE(p1, p2 - p1); |
| 95 |
} |
| 96 |
break; |
| 97 |
} |
| 98 |
|
| 99 |
if (a == prev_rot_src) |
| 100 |
prev_rot_src = 0xFFFFFFFFU; |
| 101 |
|
| 102 |
const uint8_t* p1 = (const uint8_t*) instructions[c]; |
| 103 |
const uint8_t* p2 = (const uint8_t*) instructions[c + 1]; |
| 104 |
APPEND_CODE(p1, p2 - p1); |
| 105 |
|
| 106 |
if (inst.opcode == ADD) |
| 107 |
*(uint32_t*)(JIT_code - 4) = inst.C; |
| 108 |
} |
| 109 |
|
| 110 |
APPEND_CODE(epilogue, sizeof(epilogue)); |
| 111 |
|
| 112 |
#if !(defined(_MSC_VER) || defined(__MINGW32__)) |
| 113 |
if (mprotect((void*)buf, buf_size, PROT_READ | PROT_EXEC)) |
| 114 |
return -1; |
| 115 |
#endif |
| 116 |
|
| 117 |
__builtin___clear_cache((char*)buf, (char*)JIT_code); |
| 118 |
|
| 119 |
return 0; |
| 120 |
#else |
| 121 |
return -1; |
| 122 |
#endif |
| 123 |
} |