File: ctrl_xfer32.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 (464 lines) | stat: -rw-r--r-- 10,301 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 *  plex86: run multiple x86 operating systems concurrently
 *  Copyright (C) 1999-2001 Kevin P. Lawton
 *
 *  ctrl_xfer32.c:  emulation of 32-bit control transfer instructions
 *
 *  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"



  void
CALL32_Ep(vm_t *vm)
{
  Bit16u cs_raw;
  Bit32u op1_32;
 
  invalidate_prefetch_q();

  /* op1_32 is a register or memory reference */
  if (vm->i.mod == 0xc0) {
    monpanic(vm, "CALL_Ep: op1 is a register\n");
    }
 
  /* pointer, segment address pair */
  read_virtual_dword(vm, vm->i.seg, vm->i.rm_addr, &op1_32);
  read_virtual_word(vm, vm->i.seg, vm->i.rm_addr+4, &cs_raw);
 
  if ( ProtectedMode(vm) ) {
    call_protected(vm, cs_raw, op1_32);
    goto done;
    }
 
  cache_selector(vm, SRegCS);
  push32(vm, vm->guest_cpu.selector[SRegCS].raw);
  push32(vm, G_EIP(vm));
 
  load_seg_reg(vm, SRegCS, cs_raw);
  G_EIP(vm) = op1_32;
 
done:
  /* Instrumentation was here */
  return;
}

  void
JMP32_Ep(vm_t *vm)
{
  Bit16u cs_raw;
  Bit32u op1_32;
 
  invalidate_prefetch_q();
  if (vm->i.mod == 0xc0) {
    /* far indirect must specify a memory address */
    monpanic(vm, "JMP_Ep: op1 is a reg.\n");
    }
 
  read_virtual_dword(vm, vm->i.seg, vm->i.rm_addr, &op1_32);
  read_virtual_word(vm, vm->i.seg, vm->i.rm_addr+4, &cs_raw);
 
  if ( ProtectedMode(vm) ) {
    jump_protected(vm, cs_raw, op1_32);
    goto done;
    }
 
  load_seg_reg(vm, SRegCS, cs_raw);
  G_EIP(vm) = op1_32;
 
done:
  /* instrumentation was here */
  return;
}

  void
RETfar32_Iw(vm_t *vm)
{
  Bit32u eip, ecs_raw;
  Bit16s imm16;
 
  imm16 = vm->i.Iw;
 
  invalidate_prefetch_q();
 
  if ( ProtectedMode(vm) ) {
    return_protected(vm, imm16);
    goto done;
    }
 
  pop32(vm, &eip);
  pop32(vm, &ecs_raw);
  load_seg_reg(vm, SRegCS, (Bit16u) ecs_raw);
  G_EIP(vm) = eip;
  cache_sreg(vm, SRegSS);
  if (vm->guest_cpu.desc_cache[SRegSS].desc.d_b)
    G_ESP(vm) += imm16;
  else
    G_SP(vm)  += imm16;
 
done:
  /* Instrumentation code was here */
  return;
}

  void
RETfar32(vm_t *vm)
{
  Bit32u eip, ecs_raw;
 
  invalidate_prefetch_q();
 
  if ( ProtectedMode(vm) ) {
    return_protected(vm, 0);
    goto done;
    }
 
  pop32(vm, &eip);
  pop32(vm, &ecs_raw); /* 32bit pop, MSW discarded */
  load_seg_reg(vm, SRegCS, (Bit16u) ecs_raw);
  G_EIP(vm) = eip;
 
done:
  /* Instrumentation code was here */
  return;
}

  void
CALL_Ed(vm_t *vm)
{
  Bit32u op1;

  if (vm->i.mod == 0xc0) { /* reg op */
    op1 = ReadReg32(vm, vm->i.rm);
    }
  else { /* memory op */
    read_virtual_dword(vm, vm->i.seg, vm->i.rm_addr, &op1);
    }

  if (ProtectedMode(vm)) {
    cache_sreg(vm, SRegCS);
    if (op1 > vm->guest_cpu.desc_cache[SRegCS].limit_scaled) {
      monpanic(vm, "CALL_Ed: EIP out of CS limits!\n");
      exception(vm, ExceptionGP, 0);
      }
    }

  push32(vm, vm->guest.addr.guest_context->eip);
  G_EIP(vm) = op1;
}

  void
CALL_Ad(vm_t *vm)
{
  Bit32u new_EIP;
  Bit32s disp32;

  disp32 = vm->i.Id;

  new_EIP = G_EIP(vm) + disp32;

  if ( ProtectedMode(vm) ) {
    cache_sreg(vm, SRegCS);
    if ( new_EIP > vm->guest_cpu.desc_cache[SRegCS].limit_scaled ) {
      monpanic(vm, "call_ad: offset outside of CS limits\n");
      exception(vm, ExceptionGP, 0);
      }
    }

  /* push 32 bit EA of next instruction */
  push32(vm, G_EIP(vm));
  G_EIP(vm) = new_EIP;
}

  void
CALL32_Ap(vm_t *vm)
{
  Bit16u cs_raw;
  Bit32u disp32;
 
  disp32 = vm->i.Id;
  cs_raw = vm->i.Iw2;
  invalidate_prefetch_q();
 
  if (ProtectedMode(vm)) {
    call_protected(vm, cs_raw, disp32);
    goto done;
    }
  cache_selector(vm, SRegCS);
  push32(vm, vm->guest_cpu.selector[SRegCS].raw);
  push32(vm, G_EIP(vm));
  G_EIP(vm) = disp32;
  load_seg_reg(vm, SRegCS, cs_raw);
 
done:
  /* instrumentation was here */
  return;
}

  void
JMP_Ap(vm_t *vm)
{
  Bit32u disp32;
  Bit16u cs_raw;

  if (vm->i.os_32) {
    disp32 = vm->i.Id;
    }
  else {
    disp32 = vm->i.Iw;
    }
  cs_raw = vm->i.Iw2;

  if (ProtectedMode(vm)) {
    jump_protected(vm, cs_raw, disp32);
    goto done;
    }

  load_seg_reg(vm, SRegCS, cs_raw);
  G_EIP(vm) = disp32;

done:
  /* instrumentation call was here */
  return;
}

  void
JMP_Jd(vm_t *vm)
{
  Bit32u new_EIP;
 
  invalidate_prefetch_q();
 
  new_EIP = G_EIP(vm) + (Bit32s) vm->i.Id;
 
  if (ProtectedMode(vm)) {
    cache_sreg(vm, SRegCS);
    if (new_EIP > vm->guest_cpu.desc_cache[SRegCS].limit_scaled) {
      monpanic(vm, "jmp_jd: offset outside of CS limits\n");
      exception(vm, ExceptionGP, 0);
      }
    }
 
  G_EIP(vm) = new_EIP;
}

  void
JCC_Jd(vm_t *vm)
{
  Boolean condition = 0;
 
  switch (vm->i.b1 & 0x0f) {
    case 0x00: /* JO */ condition = G_GetOF(vm); break;
    case 0x01: /* JNO */ condition = !G_GetOF(vm); break;
    case 0x02: /* JB */ condition = G_GetCF(vm); break;
    case 0x03: /* JNB */ condition = !G_GetCF(vm); break;
    case 0x04: /* JZ */ condition = G_GetZF(vm); break;
    case 0x05: /* JNZ */ condition = !G_GetZF(vm); break;
    case 0x06: /* JBE */ condition = G_GetCF(vm) || G_GetZF(vm); break;
    case 0x07: /* JNBE */ condition = !G_GetCF(vm) && !G_GetZF(vm); break;
    case 0x08: /* JS */ condition = G_GetSF(vm); break;
    case 0x09: /* JNS */ condition = !G_GetSF(vm); break;
    case 0x0A: /* JP */ condition = G_GetPF(vm); break;
    case 0x0B: /* JNP */ condition = !G_GetPF(vm); break;
    case 0x0C: /* JL */ condition = G_GetSF(vm) != G_GetOF(vm); break;
    case 0x0D: /* JNL */ condition = G_GetSF(vm) == G_GetOF(vm); break;
    case 0x0E: /* JLE */
      condition = G_GetZF(vm) || (G_GetSF(vm) != G_GetOF(vm));
      break;
    case 0x0F: /* JNLE */
      condition = (G_GetSF(vm) == G_GetOF(vm)) && !G_GetZF(vm);
      break;
    }
 
  if (condition) {
    Bit32u new_EIP;
 
    new_EIP = G_EIP(vm) + (Bit32s) vm->i.Id;
    if (ProtectedMode(vm)) {
      cache_sreg(vm, SRegCS);
      if (new_EIP > vm->guest_cpu.desc_cache[SRegCS].limit_scaled) {
        monpanic(vm, "jo_routine: offset outside of CS limits\n");
        exception(vm, ExceptionGP, 0);
        }
      }
    G_EIP(vm) = new_EIP;
    revalidate_prefetch_q();
    }
}

  void
JMP_Ed(vm_t *vm)
{
  Bit32u new_EIP;
  Bit32u op1_32;
 
  if (vm->i.mod == 0xc0) {
    op1_32 = ReadReg32(vm, vm->i.rm);
    }
  else {
    read_virtual_dword(vm, vm->i.seg, vm->i.rm_addr, &op1_32);
    }
 
  invalidate_prefetch_q();
  new_EIP = op1_32;
 
  if (ProtectedMode(vm)) {
    cache_sreg(vm, SRegCS);
    if (new_EIP > vm->guest_cpu.desc_cache[SRegCS].limit_scaled) {
      monpanic(vm, "jmp_ev: IP out of CS limits!\n");
      exception(vm, ExceptionGP, 0);
      }
    }
 
  G_EIP(vm) = new_EIP;
}

  void
RETnear32(vm_t *vm)
{
  Bit32u temp_ESP;
  Bit32u return_EIP;
  unsigned d_b;

  cache_sreg(vm, SRegCS);
  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, "retnear: can't pop EIP\n");
      /* ??? #SS(0) -or #GP(0) */
      }

    access_linear(vm, vm->guest_cpu.desc_cache[SRegSS].base + temp_ESP,
      4, G_CPL(vm)==3, OP_READ, &return_EIP);

    if ( return_EIP > vm->guest_cpu.desc_cache[SRegCS].limit_scaled ) {
      monpanic(vm, "retnear: EIP > limit\n");
      /*exception(vm, ExceptionGP, 0); */
      }
    G_EIP(vm) = return_EIP;
    if (d_b) /* 32bit stack */
      G_ESP(vm) += 4;
    else
      G_SP(vm)  += 4;
    }
  else {
    /* +++ should do the same as above? */
    pop32(vm, &return_EIP);
    G_EIP(vm) = return_EIP;
    }
}

  void
RETnear32_Iw(vm_t *vm)
{
  Bit16u imm16;
  Bit32u temp_ESP;
  Bit32u return_EIP;
  unsigned d_b;

  cache_sreg(vm, SRegCS);
  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);

  imm16 = vm->i.Iw;

  invalidate_prefetch_q();


  if (ProtectedMode(vm)) {
    if ( !can_pop(vm, 4) ) {
      monpanic(vm, "retnear_iw: can't pop EIP\n");
      /* ??? #SS(0) -or #GP(0) */
      }

    access_linear(vm, vm->guest_cpu.desc_cache[SRegSS].base + temp_ESP,
      4, G_CPL(vm)==3, OP_READ, &return_EIP);

    if ( return_EIP > vm->guest_cpu.desc_cache[SRegCS].limit_scaled ) {
      monpanic(vm, "retnear_iw: EIP > limit\n");
      }

    /* Pentium book says imm16 is number of words ??? */
    if ( !can_pop(vm, 4 + imm16) ) {
      monpanic(vm, "retnear_iw: can't release bytes from stack\n");
      /*exception(vm, ExceptionGP, 0); */
      /* #GP(0) -or #SS(0) ??? */
      }

    G_EIP(vm) = return_EIP;
    if (d_b) /* 32bit stack */
      G_ESP(vm) += 4 + imm16; /* ??? should it be 2*imm16 ? */
    else
      G_SP(vm)  += 4 + imm16;
    }
  else {
    pop32(vm, &return_EIP);
    G_EIP(vm) = return_EIP;
    if (d_b) /* 32bit stack */
      G_ESP(vm) += imm16; /* ??? should it be 2*imm16 ? */
    else
      G_SP(vm)  += imm16;
    }
}

  void
IRET32(vm_t *vm)
{
  Bit32u eip, ecs_raw, eflags, change_mask;

  if (V8086Mode(vm)) {
    /* IOPL check in stack_return_from_v86() */
    monpanic(vm, "IRET32: v8086\n");
/*stack_return_from_v86(i); */
    goto done;
    }

  if (vm->guest_cpu.cr0.fields.pe) {
    iret_protected(vm);
    goto done;
    }


  pop32(vm, &eip);
  pop32(vm, &ecs_raw);
  pop32(vm, &eflags);

  load_seg_reg(vm, SRegCS, (Bit16u) ecs_raw);
  G_EIP(vm) = eip;

  /* modify: ID,AC,RF,NT,IOPL,OF,DF,IF,TF,SF,ZF,AF,PF,CF */
  /* ignore: (VIP,VIF),VM */
  change_mask = 0x00257fd5;
monprint(vm, "iret32: eflags=0x%x\n", eflags);
  write_eflags(vm, eflags, change_mask);

done:
  /* instrumentation call was here */
}