File: stack16.cc

package info (click to toggle)
bochs 3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,244 kB
  • sloc: cpp: 270,331; ansic: 25,334; sh: 8,371; makefile: 5,512; yacc: 1,485; asm: 395; perl: 359; lex: 318; csh: 3
file content (261 lines) | stat: -rw-r--r-- 6,959 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/////////////////////////////////////////////////////////////////////////
// $Id$
/////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2001-2018  The Bochs Project
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
/////////////////////////////////////////////////////////////////////////

#define NEED_CPU_REG_SHORTCUTS 1
#include "bochs.h"
#include "cpu.h"
#define LOG_THIS BX_CPU_THIS_PTR

void BX_CPP_AttrRegparmN(1) BX_CPU_C::PUSH_EwR(bxInstruction_c *i)
{
  push_16(BX_READ_16BIT_REG(i->dst()));

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::PUSH_EwM(bxInstruction_c *i)
{
  bx_address eaddr = BX_CPU_RESOLVE_ADDR(i);
  Bit16u op1_16 = read_virtual_word(i->seg(), eaddr);

  push_16(op1_16);

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::PUSH16_Sw(bxInstruction_c *i)
{
  push_16(BX_CPU_THIS_PTR sregs[i->src()].selector.value);

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::POP16_Sw(bxInstruction_c *i)
{
  RSP_SPECULATIVE;

  Bit16u selector = pop_16();
  load_seg_reg(&BX_CPU_THIS_PTR sregs[i->dst()], selector);

  RSP_COMMIT;

  if (i->dst() == BX_SEG_REG_SS) {
    // POP SS inhibits interrupts, debug exceptions and single-step
    // trap exceptions until the execution boundary following the
    // next instruction is reached.
    // Same code as MOV_SwEw()
    inhibit_interrupts(BX_INHIBIT_INTERRUPTS_BY_MOVSS);
  }

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::POP_EwR(bxInstruction_c *i)
{
  BX_WRITE_16BIT_REG(i->dst(), pop_16());

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::POP_EwM(bxInstruction_c *i)
{
  RSP_SPECULATIVE;

  Bit16u val16 = pop_16();

  // Note: there is one little weirdism here.  It is possible to use
  // SP in the modrm addressing. If used, the value of SP after the
  // pop is used to calculate the address.
  bx_address eaddr = BX_CPU_RESOLVE_ADDR(i);

  write_virtual_word(i->seg(), eaddr, val16);

  RSP_COMMIT;

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::PUSH_Iw(bxInstruction_c *i)
{
  push_16(i->Iw());

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::PUSHA16(bxInstruction_c *i)
{
  Bit32u temp_ESP = ESP;
  Bit16u temp_SP  = SP;

  if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b)
  {
    stack_write_word((Bit32u)(temp_ESP -  2), AX);
    stack_write_word((Bit32u)(temp_ESP -  4), CX);
    stack_write_word((Bit32u)(temp_ESP -  6), DX);
    stack_write_word((Bit32u)(temp_ESP -  8), BX);
    stack_write_word((Bit32u)(temp_ESP - 10), temp_SP);
    stack_write_word((Bit32u)(temp_ESP - 12), BP);
    stack_write_word((Bit32u)(temp_ESP - 14), SI);
    stack_write_word((Bit32u)(temp_ESP - 16), DI);
    ESP -= 16;
  }
  else
  {
    stack_write_word((Bit16u)(temp_SP -  2), AX);
    stack_write_word((Bit16u)(temp_SP -  4), CX);
    stack_write_word((Bit16u)(temp_SP -  6), DX);
    stack_write_word((Bit16u)(temp_SP -  8), BX);
    stack_write_word((Bit16u)(temp_SP - 10), temp_SP);
    stack_write_word((Bit16u)(temp_SP - 12), BP);
    stack_write_word((Bit16u)(temp_SP - 14), SI);
    stack_write_word((Bit16u)(temp_SP - 16), DI);
    SP -= 16;
  }

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::POPA16(bxInstruction_c *i)
{
  Bit16u di, si, bp, bx, dx, cx, ax;

  if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b)
  {
    Bit32u temp_ESP = ESP;
    di = stack_read_word((Bit32u)(temp_ESP +  0));
    si = stack_read_word((Bit32u)(temp_ESP +  2));
    bp = stack_read_word((Bit32u)(temp_ESP +  4));
         stack_read_word((Bit32u)(temp_ESP +  6));
    bx = stack_read_word((Bit32u)(temp_ESP +  8));
    dx = stack_read_word((Bit32u)(temp_ESP + 10));
    cx = stack_read_word((Bit32u)(temp_ESP + 12));
    ax = stack_read_word((Bit32u)(temp_ESP + 14));
    ESP += 16;
  }
  else
  {
    Bit16u temp_SP = SP;
    di = stack_read_word((Bit16u)(temp_SP +  0));
    si = stack_read_word((Bit16u)(temp_SP +  2));
    bp = stack_read_word((Bit16u)(temp_SP +  4));
         stack_read_word((Bit16u)(temp_SP +  6));
    bx = stack_read_word((Bit16u)(temp_SP +  8));
    dx = stack_read_word((Bit16u)(temp_SP + 10));
    cx = stack_read_word((Bit16u)(temp_SP + 12));
    ax = stack_read_word((Bit16u)(temp_SP + 14));
    SP += 16;
  }

  DI = di;
  SI = si;
  BP = bp;
  BX = bx;
  DX = dx;
  CX = cx;
  AX = ax;

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::ENTER16_IwIb(bxInstruction_c *i)
{
  Bit16u imm16 = i->Iw();
  Bit8u level = i->Ib2();
  level &= 0x1F;

  RSP_SPECULATIVE;

  push_16(BP);
  Bit16u frame_ptr16 = SP;

  if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b) {
    Bit32u ebp = EBP; // Use temp copy for case of exception.

    if (level > 0) {
      /* do level-1 times */
      while (--level) {
        ebp -= 2;
        Bit16u temp16 = stack_read_word(ebp);
        push_16(temp16);
      }

      /* push(frame pointer) */
      push_16(frame_ptr16);
    }

    ESP -= imm16;

    // ENTER finishes with memory write check on the final stack pointer
    // the memory is touched but no write actually occurs
    // emulate it by doing RMW read access from SS:ESP
    read_RMW_virtual_word_32(BX_SEG_REG_SS, ESP); // no lock, should be touch only

    BP = frame_ptr16;
  }
  else {
    Bit16u bp = BP;

    if (level > 0) {
      /* do level-1 times */
      while (--level) {
        bp -= 2;
        Bit16u temp16 = stack_read_word(bp);
        push_16(temp16);
      }

      /* push(frame pointer) */
      push_16(frame_ptr16);
    }

    SP -= imm16;

    // ENTER finishes with memory write check on the final stack pointer
    // the memory is touched but no write actually occurs
    // emulate it by doing RMW read access from SS:SP
    read_RMW_virtual_word_32(BX_SEG_REG_SS, SP); // no lock, should be touch only
  }

  BP = frame_ptr16;

  RSP_COMMIT;

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::LEAVE16(bxInstruction_c *i)
{
  BX_ASSERT(BX_CPU_THIS_PTR cpu_mode != BX_MODE_LONG_64);

  Bit16u value16;

  if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b) {
    value16 = stack_read_word(EBP);
    ESP = EBP + 2;
  }
  else {
    value16 = stack_read_word(BP);
    SP = BP + 2;
  }

  BP = value16;

  BX_NEXT_INSTR(i);
}