File: stack_pro.c

package info (click to toggle)
plex86 0.0.20011018-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,868 kB
  • ctags: 8,721
  • sloc: ansic: 46,915; cpp: 17,817; xml: 1,283; makefile: 1,130; sh: 451; asm: 360; csh: 18
file content (295 lines) | stat: -rw-r--r-- 7,376 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 *  plex86: run multiple x86 operating systems concurrently
 *  Copyright (C) 1999-2001 Kevin P. Lawton
 *
 *  stack_pro.c:  stack oriented framework operations
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */


#include "plex86.h"
#include "monitor.h"



  unsigned
can_push(vm_t *vm, descriptor_cache_t *ss_cache, Bit32u esp, unsigned bytes)
{
  /* No need for cache_sreg(), we're being passed a cache */

  if ( !RealMode(vm) ) { /* CR0.PE==1, EFLAGS.VM==0 */
    unsigned d_b;

    d_b = ss_cache->desc.d_b;

    /* small stack compares against 16-bit SP */
    if (!d_b)
      esp &= 0x0000ffff;

    if (ss_cache->desc.p==0) {
      monpanic(vm, "can_push: not present\n");
      return(0);
      }

    if (ss_cache->desc.type & D_EXDOWN) { /* expand down segment */
      Bit32u expand_down_limit;

      if (d_b)
        expand_down_limit = 0xffffffff;
      else
        expand_down_limit = 0x0000ffff;

      if (esp==0) {
        monpanic(vm, "can_push(): esp=0, wraparound?\n");
        return(0);
        }

      if (esp < bytes) {
        monpanic(vm, "can_push(): expand-down: esp < N\n");
        return(0);
        }
      if ( (esp - bytes) <= ss_cache->limit_scaled ) {
        monpanic(vm, "can_push(): expand-down: esp-N < limit\n");
        return(0);
        }
      if ( esp > expand_down_limit ) {
        monpanic(vm, "can_push(): esp > expand-down-limit\n");
        return(0);
        }
      return(1);
      }
    else { /* normal (expand-up) segment */
      if (ss_cache->limit_scaled==0) {
        monpanic(vm, "can_push(): found limit of 0\n");
        return(0);
        }

      /* Look at case where esp==0.  Possibly, it's an intentional wraparound */
      /* If so, limit must be the maximum for the given stack size */
      if (esp==0) {
        if (d_b && (ss_cache->limit_scaled==0xffffffff))
          return(1);
        if ((d_b==0) && (ss_cache->limit_scaled>=0xffff))
          return(1);
        monpanic(vm, "can_push(): esp=0, normal, wraparound? limit=%08x\n",
          ss_cache->limit_scaled);
        return(0);
        }

      if (esp < bytes) {
        monpanic(vm, "can_push(): expand-up: esp < N\n");
        return(0);
        }
      if ((esp-1) > ss_cache->limit_scaled) {
        monpanic(vm, "can_push(): expand-up: SP > limit\n");
        return(0);
        }
      return(1);
      }
    }
  else {
    monpanic(vm, "can_push: RM\n");
    /* need to fill in for real/virtual mode */
#if 0
/* from stack_pro.cc */
if ((ESP>=1) && (ESP<=3)) {
  monpanic(vm, "push_32: ESP=%08x\n", (unsigned) ESP);
  }
#endif
    }
  return(0);
}

  unsigned
can_pop(vm_t *vm, unsigned bytes)
{
  Bit32u temp_ESP, expand_down_limit;

  /* +++ needed? */
  if (RealMode(vm)) monpanic(vm, "can_pop: called in real mode?\n");

  /* +++ Need this call ? */
  cache_sreg(vm, SRegSS);
  if (vm->guest_cpu.desc_cache[SRegSS].desc.d_b) { /* use ESP */
    temp_ESP = G_ESP(vm);
    expand_down_limit = 0xFFFFFFFF;
    }
  else { /* use SP */
    temp_ESP = G_SP(vm);
    expand_down_limit = 0xFFFF;
    }

  if (vm->guest_cpu.desc_cache[SRegSS].valid==0) {
    monpanic(vm, "can_pop: SS invalidated.\n");
    }

  if (vm->guest_cpu.desc_cache[SRegSS].desc.p==0) {
    monpanic(vm, "can_pop: SS.p = 0\n");
    }


  if (vm->guest_cpu.desc_cache[SRegSS].desc.type & D_EXDOWN) { /* expand down */
    if ( temp_ESP == expand_down_limit ) {
      monpanic(vm, "can_pop: found SP=ffff\n");
      }
    if ( ((expand_down_limit - temp_ESP) + 1) >= bytes )
      return(1);
    return(0);
    }
  else { /* normal (expand-up) segment */
    if (vm->guest_cpu.desc_cache[SRegSS].limit_scaled==0) {
      monpanic(vm, "can_pop: SS.limit = 0\n");
      }
    if ( temp_ESP == expand_down_limit ) {
      monpanic(vm, "can_pop: found SP=ffff\n");
      }
    if ( temp_ESP > vm->guest_cpu.desc_cache[SRegSS].limit_scaled ) {
      monpanic(vm, "can_pop: eSP > SS.limit\n");
      }
    if ( ((vm->guest_cpu.desc_cache[SRegSS].limit_scaled - temp_ESP) + 1) >= bytes )
      return(1);
    return(0);
    }
}

  void
push32(vm_t *vm, Bit32u val32)
{
  Bit32u old_esp, new_esp;
  unsigned d_b;

  cache_sreg(vm, SRegSS);
  d_b = vm->guest_cpu.desc_cache[SRegSS].desc.d_b;
  if (d_b) {
    old_esp = vm->guest.addr.guest_context->esp;
    new_esp = old_esp - 4;
    }
  else {
    old_esp = G_SP(vm);
    new_esp = (old_esp - 4) & 0x0000ffff;
    }
  if (ProtectedMode(vm)) {
    if (!can_push(vm, &vm->guest_cpu.desc_cache[SRegSS], old_esp, 4)) {
      monpanic(vm, "push32: cant push 4\n");
      }
    }
  else {
    if ((old_esp>=1) && (old_esp<=3))
      monpanic(vm, "push32: ESP=%08x\n", (unsigned) old_esp);
    }
  write_virtual_dword(vm, SRegSS, new_esp, &val32);
  if (d_b) {
    /* modify ESP (all 32 bits) */
    G_ESP(vm) = new_esp;
    }
  else {
    /* only modify SP (lower 16 bits) */
    G_SP(vm) = new_esp;
    }
}

  void
push16(vm_t *vm, Bit16u val16)
{
  Bit32u old_esp, new_esp;
  unsigned d_b;

  cache_sreg(vm, SRegSS);
  d_b = vm->guest_cpu.desc_cache[SRegSS].desc.d_b;
  if (d_b) {
    old_esp = vm->guest.addr.guest_context->esp;
    new_esp = old_esp - 2;
    }
  else {
    old_esp = G_SP(vm);
    new_esp = (old_esp - 2) & 0x0000ffff;
    }
  if (ProtectedMode(vm)) {
    if (!can_push(vm, &vm->guest_cpu.desc_cache[SRegSS], old_esp, 2)) {
      monpanic(vm, "push16: cant push 2\n");
      }
    }
  else {
    if (old_esp == 1)
      monpanic(vm, "CPU shutting down due to lack of space, eSP==1\n");
    }
  write_virtual_word(vm, SRegSS, new_esp, &val16);
  if (d_b) {
    /* modify ESP (all 32 bits) */
    G_ESP(vm) = new_esp;
    }
  else {
    /* only modify SP (lower 16 bits) */
    G_SP(vm) = new_esp;
    }
}


  void
pop32(vm_t *vm, Bit32u *val32)
{
  Bit32u temp_ESP;
  unsigned d_b;

  cache_sreg(vm, SRegSS);
  d_b = vm->guest_cpu.desc_cache[SRegSS].desc.d_b;
  if (d_b)
    temp_ESP = G_ESP(vm);
  else
    temp_ESP = G_SP(vm);

  if (ProtectedMode(vm)) {
    if ( !can_pop(vm, 4) ) {
      monpanic(vm, "pop32: can't pop from stack\n");
      exception(vm, ExceptionSS, 0);
      }
    }

  read_virtual_dword(vm, SRegSS, temp_ESP, val32);

  if (d_b)
    G_ESP(vm) += 4;
  else
    G_SP(vm) += 4;
}

  void
pop16(vm_t *vm, Bit16u *val16)
{
  Bit32u temp_ESP;
  unsigned d_b;

  cache_sreg(vm, SRegSS);
  d_b = vm->guest_cpu.desc_cache[SRegSS].desc.d_b;
  if (d_b)
    temp_ESP = G_ESP(vm);
  else
    temp_ESP = G_SP(vm);

  if (ProtectedMode(vm)) {
    if ( !can_pop(vm, 2) ) {
      monpanic(vm, "pop16: can't pop from stack\n");
      exception(vm, ExceptionSS, 0);
      }
    }

  read_virtual_word(vm, SRegSS, temp_ESP, val16);

  if (d_b)
    G_ESP(vm) += 2;
  else
    G_SP(vm) += 2;
}