vm_interpreted.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #pragma once
  16. #include <new>
  17. #include <vector>
  18. #include "common.hpp"
  19. #include "virtual_machine.hpp"
  20. #include "intrin_portable.h"
  21. #include "allocator.hpp"
  22. namespace randomx {
  23. struct InstructionByteCode {
  24. union {
  25. int_reg_t* idst;
  26. __m128d* fdst;
  27. };
  28. union {
  29. int_reg_t* isrc;
  30. __m128d* fsrc;
  31. };
  32. union {
  33. uint64_t imm;
  34. int64_t simm;
  35. };
  36. uint16_t type;
  37. union {
  38. int16_t target;
  39. uint16_t shift;
  40. };
  41. uint32_t memMask;
  42. };
  43. template<class Allocator, bool softAes>
  44. class InterpretedVm : public VmBase<Allocator, softAes> {
  45. public:
  46. using VmBase<Allocator, softAes>::mem;
  47. using VmBase<Allocator, softAes>::scratchpad;
  48. using VmBase<Allocator, softAes>::program;
  49. using VmBase<Allocator, softAes>::config;
  50. using VmBase<Allocator, softAes>::reg;
  51. using VmBase<Allocator, softAes>::datasetPtr;
  52. using VmBase<Allocator, softAes>::datasetOffset;
  53. void* operator new(size_t size) {
  54. void* ptr = AlignedAllocator<CacheLineSize>::allocMemory(size);
  55. if (ptr == nullptr)
  56. throw std::bad_alloc();
  57. return ptr;
  58. }
  59. void operator delete(void* ptr) {
  60. AlignedAllocator<CacheLineSize>::freeMemory(ptr, sizeof(InterpretedVm));
  61. }
  62. void run(void* seed) override;
  63. void setDataset(randomx_dataset* dataset) override;
  64. protected:
  65. virtual void datasetRead(uint32_t blockNumber, int_reg_t(&r)[RegistersCount]);
  66. private:
  67. void execute();
  68. void precompileProgram(int_reg_t(&r)[RegistersCount], __m128d (&f)[RegisterCountFlt], __m128d (&e)[RegisterCountFlt], __m128d (&a)[RegisterCountFlt]);
  69. void executeBytecode(int_reg_t(&r)[RegistersCount], __m128d (&f)[RegisterCountFlt], __m128d (&e)[RegisterCountFlt], __m128d (&a)[RegisterCountFlt]);
  70. void executeBytecode(int& i, int_reg_t(&r)[RegistersCount], __m128d (&f)[RegisterCountFlt], __m128d (&e)[RegisterCountFlt], __m128d (&a)[RegisterCountFlt]);
  71. void* getScratchpadAddress(InstructionByteCode& ibc);
  72. __m128d maskRegisterExponentMantissa(__m128d);
  73. InstructionByteCode byteCode[RANDOMX_PROGRAM_SIZE];
  74. };
  75. using InterpretedVmDefault = InterpretedVm<AlignedAllocator<CacheLineSize>, true>;
  76. using InterpretedVmHardAes = InterpretedVm<AlignedAllocator<CacheLineSize>, false>;
  77. using InterpretedVmLargePage = InterpretedVm<LargePageAllocator, true>;
  78. using InterpretedVmLargePageHardAes = InterpretedVm<LargePageAllocator, false>;
  79. }