From ff88a57a989b50604a9c9b7306676f2f8a58040b Mon Sep 17 00:00:00 2001
From: tevador <tevador@gmail.com>
Date: Fri, 26 Apr 2019 23:32:10 +0200
Subject: [PATCH] Fixed reading from the red zone

---
 doc/program.asm                | 5 +++--
 src/assembly_generator_x86.cpp | 5 +++--
 src/jit_compiler_x86.cpp       | 2 +-
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/doc/program.asm b/doc/program.asm
index a1909c4..854744d 100644
--- a/doc/program.asm
+++ b/doc/program.asm
@@ -605,8 +605,9 @@ randomx_isn_155:
 	rol rax, 31
 	and eax, 24576
 	or eax, 40896
-	mov dword ptr [rsp-8], eax
-	ldmxcsr dword ptr [rsp-8]
+	push rax
+	ldmxcsr dword ptr [rsp]
+	pop rax
 randomx_isn_156:
 	; FSUB_R f3, a2
 	subpd xmm3, xmm10
diff --git a/src/assembly_generator_x86.cpp b/src/assembly_generator_x86.cpp
index 0d93752..6c2780c 100644
--- a/src/assembly_generator_x86.cpp
+++ b/src/assembly_generator_x86.cpp
@@ -535,8 +535,9 @@ namespace randomx {
 			asmCode << "\trol rax, " << rotate << std::endl;
 		asmCode << "\tand eax, 24576" << std::endl;
 		asmCode << "\tor eax, 40896" << std::endl;
-		asmCode << "\tmov dword ptr [rsp-8], eax" << std::endl;
-		asmCode << "\tldmxcsr dword ptr [rsp-8]" << std::endl;
+		asmCode << "\tpush rax" << std::endl;
+		asmCode << "\tldmxcsr dword ptr [rsp]" << std::endl;
+		asmCode << "\tpop rax" << std::endl;
 		tracenop(instr);
 	}
 
diff --git a/src/jit_compiler_x86.cpp b/src/jit_compiler_x86.cpp
index 112aaf6..f64a3c3 100644
--- a/src/jit_compiler_x86.cpp
+++ b/src/jit_compiler_x86.cpp
@@ -194,7 +194,7 @@ namespace randomx {
 	static const uint8_t REX_MAXPD[] = { 0x66, 0x41, 0x0f, 0x5f };
 	static const uint8_t REX_DIVPD[] = { 0x66, 0x41, 0x0f, 0x5e };
 	static const uint8_t SQRTPD[] = { 0x66, 0x0f, 0x51 };
-	static const uint8_t AND_OR_MOV_LDMXCSR[] = { 0x25, 0x00, 0x60, 0x00, 0x00, 0x0D, 0xC0, 0x9F, 0x00, 0x00, 0x89, 0x44, 0x24, 0xF8, 0x0F, 0xAE, 0x54, 0x24, 0xF8 };
+	static const uint8_t AND_OR_MOV_LDMXCSR[] = { 0x25, 0x00, 0x60, 0x00, 0x00, 0x0D, 0xC0, 0x9F, 0x00, 0x00, 0x50, 0x0F, 0xAE, 0x14, 0x24, 0x58 };
 	static const uint8_t ROL_RAX[] = { 0x48, 0xc1, 0xc0 };
 	static const uint8_t XOR_ECX_ECX[] = { 0x33, 0xC9 };
 	static const uint8_t REX_CMP_R32I[] = { 0x41, 0x81 };