1 /////////////////////////////////////////////////////////////////////////
2 // $Id: stack64.cc 13466 2018-02-16 07:57:32Z sshwarts $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 //  Copyright (C) 2001-2018  The Bochs Project
6 //
7 //  This library is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU Lesser General Public
9 //  License as published by the Free Software Foundation; either
10 //  version 2 of the License, or (at your option) any later version.
11 //
12 //  This library is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 //  Lesser General Public License for more details.
16 //
17 //  You should have received a copy of the GNU Lesser General Public
18 //  License along with this library; if not, write to the Free Software
19 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
20 /////////////////////////////////////////////////////////////////////////
21 
22 #define NEED_CPU_REG_SHORTCUTS 1
23 #include "bochs.h"
24 #include "cpu.h"
25 #define LOG_THIS BX_CPU_THIS_PTR
26 
27 #if BX_SUPPORT_X86_64
28 
POP_EqM(bxInstruction_c * i)29 void BX_CPP_AttrRegparmN(1) BX_CPU_C::POP_EqM(bxInstruction_c *i)
30 {
31   RSP_SPECULATIVE;
32 
33   Bit64u val64 = pop_64();
34 
35   // Note: there is one little weirdism here.  It is possible to use
36   // RSP in the modrm addressing. If used, the value of RSP after the
37   // pop is used to calculate the address.
38   bx_address eaddr = BX_CPU_RESOLVE_ADDR_64(i);
39 
40   write_linear_qword(i->seg(), get_laddr64(i->seg(), eaddr), val64);
41 
42   RSP_COMMIT;
43 
44   BX_NEXT_INSTR(i);
45 }
46 
PUSH_EqR(bxInstruction_c * i)47 void BX_CPP_AttrRegparmN(1) BX_CPU_C::PUSH_EqR(bxInstruction_c *i)
48 {
49   push_64(BX_READ_64BIT_REG(i->dst()));
50 
51   BX_NEXT_INSTR(i);
52 }
53 
POP_EqR(bxInstruction_c * i)54 void BX_CPP_AttrRegparmN(1) BX_CPU_C::POP_EqR(bxInstruction_c *i)
55 {
56   BX_WRITE_64BIT_REG(i->dst(), pop_64());
57 
58   BX_NEXT_INSTR(i);
59 }
60 
PUSH64_Sw(bxInstruction_c * i)61 void BX_CPP_AttrRegparmN(1) BX_CPU_C::PUSH64_Sw(bxInstruction_c *i)
62 {
63   push_64(BX_CPU_THIS_PTR sregs[i->src()].selector.value);
64 
65   BX_NEXT_INSTR(i);
66 }
67 
POP64_Sw(bxInstruction_c * i)68 void BX_CPP_AttrRegparmN(1) BX_CPU_C::POP64_Sw(bxInstruction_c *i)
69 {
70   Bit16u selector = stack_read_word(RSP);
71   load_seg_reg(&BX_CPU_THIS_PTR sregs[i->dst()], selector);
72   RSP += 8;
73 
74   BX_NEXT_INSTR(i);
75 }
76 
PUSH64_Id(bxInstruction_c * i)77 void BX_CPP_AttrRegparmN(1) BX_CPU_C::PUSH64_Id(bxInstruction_c *i)
78 {
79   Bit64u imm64 = (Bit32s) i->Id();
80   push_64(imm64);
81 
82   BX_NEXT_INSTR(i);
83 }
84 
PUSH_EqM(bxInstruction_c * i)85 void BX_CPP_AttrRegparmN(1) BX_CPU_C::PUSH_EqM(bxInstruction_c *i)
86 {
87   bx_address eaddr = BX_CPU_RESOLVE_ADDR_64(i);
88 
89   Bit64u op1_64 = read_linear_qword(i->seg(), get_laddr64(i->seg(), eaddr));
90 
91   push_64(op1_64);
92 
93   BX_NEXT_INSTR(i);
94 }
95 
ENTER64_IwIb(bxInstruction_c * i)96 void BX_CPP_AttrRegparmN(1) BX_CPU_C::ENTER64_IwIb(bxInstruction_c *i)
97 {
98   Bit8u level = i->Ib2();
99   level &= 0x1F;
100 
101   Bit64u temp_RSP = RSP, temp_RBP = RBP;
102 
103   temp_RSP -= 8;
104   stack_write_qword(temp_RSP, temp_RBP);
105 
106   Bit64u frame_ptr64 = temp_RSP;
107 
108   if (level > 0) {
109     /* do level-1 times */
110     while (--level) {
111       temp_RBP -= 8;
112       Bit64u temp64 = stack_read_qword(temp_RBP);
113       temp_RSP -= 8;
114       stack_write_qword(temp_RSP, temp64);
115     } /* while (--level) */
116 
117     /* push(frame pointer) */
118     temp_RSP -= 8;
119     stack_write_qword(temp_RSP, frame_ptr64);
120   } /* if (level > 0) ... */
121 
122   temp_RSP -= i->Iw();
123 
124   // ENTER finishes with memory write check on the final stack pointer
125   // the memory is touched but no write actually occurs
126   // emulate it by doing RMW read access from SS:RSP
127   read_RMW_linear_qword(BX_SEG_REG_SS, temp_RSP);
128 
129   RBP = frame_ptr64;
130   RSP = temp_RSP;
131 
132   BX_NEXT_INSTR(i);
133 }
134 
LEAVE64(bxInstruction_c * i)135 void BX_CPP_AttrRegparmN(1) BX_CPU_C::LEAVE64(bxInstruction_c *i)
136 {
137   // restore frame pointer
138   Bit64u temp64 = stack_read_qword(RBP);
139   RSP = RBP + 8;
140   RBP = temp64;
141 
142   BX_NEXT_INSTR(i);
143 }
144 
145 #endif /* if BX_SUPPORT_X86_64 */
146