File: emulation.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 (1907 lines) | stat: -rw-r--r-- 45,670 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
/*
 *  plex86: run multiple x86 operating systems concurrently
 *  Copyright (C) 1999-2001 Kevin P. Lawton
 *
 *  emulation.c:  This file contains functions to emulate IA32 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"





  unsigned
cpuEmulate(vm_t *vm, guest_context_t *context,
           unsigned *instructionsRequested)
{
  unsigned ret;
  Bit32u port;
  Boolean seg32;
  unsigned max_len, room_in_page;
  Bit8u *ptr;
  unsigned group;
  unsigned len;
  Bit32u data, laddr0, guest_ppi, perror;
  unsigned us, rw;
  // unsigned instructionsExpired = 0; // xxx Conditionally compile
  unsigned stopReason = 0;


  if (SetJmp(vm->jmp_buf_env)) {
    /* nonzero return, means returning from longjmp() */
    }

cpuLoop:

/* xxx Humm, not sure about placement of loop label and following code. */

  /* Store EIP,ESP in case we need to rewind them due to an */
  /* exception caused by the instruction. */
  vm->guest_cpu.prev_eip = vm->guest.addr.guest_context->eip;
  vm->guest_cpu.prev_esp = vm->guest.addr.guest_context->esp;

  vm->guest_cpu.EXT = 0;
  vm->guest_cpu.errorno = 0;

  if (vm->guest_cpu.async_event)
    goto handleAsyncEvent;

asyncEventsProcessed:
  /* Now we can handle things which are synchronous to instruction */
  /* execution. */
  if (G_GetRF(vm)) {
    G_SetRF(vm, 0);
    }
#if 0
#if BX_X86_DEBUGGER /* +++ */
  else {
    /* only bother comparing if any breakpoints enabled */
    if ( BX_CPU_THIS_PTR dr7 & 0x000000ff ) {
      Bit32u iaddr =
        BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base +
        BX_CPU_THIS_PTR prev_eip;
      Bit32u dr6_bits;
      if ( (dr6_bits = hwdebug_compare(iaddr, 1, BX_HWDebugInstruction,
                                       BX_HWDebugInstruction)) ) {
        /* Add to the list of debug events thus far. */
        BX_CPU_THIS_PTR debug_trap |= dr6_bits;
        BX_CPU_THIS_PTR async_event = 1;
        /* If debug events are not inhibited on this boundary, */
        /* fire off a debug fault.  Otherwise handle it on the next */
        /* boundary. (becomes a trap) */
        if ( !(BX_CPU_THIS_PTR inhibit_mask & BX_INHIBIT_DEBUG) ) {
          /* Commit debug events to DR6 */
          BX_CPU_THIS_PTR dr6 = BX_CPU_THIS_PTR debug_trap;
          exception(BX_DB_EXCEPTION, 0, 0); /* no error, not interrupt */
          }
        }
      }
    }
#endif  /* BX_X86_DEBUGGER */
#endif  /* 0 */

  /* Events in the mask may have been inhibited during processing */
  /* of the events above.  Now that they have been properly ignored, */
  /* we can clear the inhibit mask. */
  vm->guest_cpu.inhibit_mask = 0; /* clear inhibits */


  if (G_GetTF(vm)) {
    /* TF is set before execution of this instruction.  Schedule */
    /* a debug trap (#DB) after execution.  After completion of */
    /* the instruction, handle_async_event will invoke the trap. */
    vm->guest_cpu.debug_trap |= 0x00004000; /* BS flag in DR6 */
    vm->guest_cpu.async_event = 1;
    }

  cache_sreg(vm, SRegCS);
  seg32 = vm->guest_cpu.desc_cache[SRegCS].desc.d_b;
  laddr0 = vm->guest_cpu.desc_cache[SRegCS].base + context->eip;
  max_len = MaxIA32OpcodeLen;
  room_in_page = 4096 - (laddr0 & 0xfff);
  if ( room_in_page < MaxIA32OpcodeLen)
    max_len = room_in_page;

/* xxx Check for final CS.base + eip + i.ilen-1. */

  if ( (laddr0 & 0xffc00000) == vm->mon_pde_mask )
    goto emulateFetch;
  if ( !__fetchCheckNative(context->eip) ) {
    /* Error was generated.  Have to do this the hard way. */
    goto emulateFetch;
    }
  ptr = (Bit8u*) Guest2Monitor(vm, laddr0);
  ret = fetchDecode(ptr, &vm->i, max_len, seg32);

  /* Possibly, we ran up against a page boundary.  Check out access to
   * next page and retry.
   */
  if ( ret==0 ) {
    Bit32u laddr1;
    /* Instruction runs over into next page.  See if it is OK to */
    /* read natively in the next page.  If so, then no problem. */
    laddr1 = (laddr0 | 0x00000fff) + 1;
    if ( (laddr1 & 0xffc00000) == vm->mon_pde_mask )
      goto emulateFetch;
    if ( !__fetchCheckNative(context->eip+max_len) ) {
      /* Error was generated.  Have to do this the hard way. */
      goto emulateFetch;
      }
    ret = fetchDecode(ptr, &vm->i, MaxIA32OpcodeLen, seg32);
    if ( ret==0 )
      monpanic(vm, "fetchDecode returned 0 (multipage)\n");
    }


fetchComplete:
  if ( vm->i.attr & InvalidOpcode ) {
    monprint(vm, "fetchDecode InvalidOpcode b1=0x%x nnn=%u\n",
      vm->i.b1, vm->i.nnn);
    UndefinedOpcode(vm);
    }

  /* If instruction requires resolution of mod-rm address, */
  /* then call resolve function with current CPU context. */
  if (vm->i.ResolveModrm)
    vm->i.ResolveModrm(&vm->i, context);

  if (vm->i.rep_used && (vm->i.attr & Repeatable)) {
    if (vm->i.attr & RepeatableZF) {
      if (vm->i.as_32) {
        if (G_ECX(vm) != 0) {
          goto execute;
          }
        goto advance;
        }
      else {
        if (G_CX(vm) != 0) {
          goto execute;
          }
        goto advance;
        }
      }
    else { /* normal repeat, no concern for ZF */
      if (vm->i.as_32) {
        if (G_ECX(vm) != 0) {
          goto execute;
          }
        goto advance;
        }
      else { /* 16bit addrsize */
        if (G_CX(vm) != 0) {
          goto execute;
          }
        goto advance;
        }
      }
    }
  else {
    /* Advance EIP by length of instruction.  An exception will */
    /* rewind it back to prev_eip. */
    context->eip += vm->i.ilen;
    }

execute:

  //InstrEmulateOpcode(vm);

  /* Execute instruction. */
  if (vm->i.attr & GroupN)
    goto group_n;

/* */
/* Standard 1 or 2-byte opcodes: */
/* */

  switch (vm->i.b1) {
    case 0x00:
#if 0
      if (vm->i.modrm == 0) {
        monpanic(vm, "Potentially running in the weeds\n");
        }
#endif
      ADD_EbGb(vm);
      goto advance;

    case 0x01:
      if (vm->i.os_32)
        ADD_EdGd(vm);
      else
        ADD_EwGw(vm);
      goto advance;

    case 0x02:
      ADD_GbEb(vm);
      goto advance;

    case 0x03:
      if (vm->i.os_32)
        ADD_GdEd(vm);
      else
        ADD_GwEw(vm);
      goto advance;

    case 0x04:
      ADD_ALIb(vm);
      goto advance;

    case 0x05:
      if (vm->i.os_32)
        ADD_EAXId(vm);
      else
        ADD_AXIw(vm);
      goto advance;

    case 0x06:
      PUSH_SREG(vm, SRegES);
      goto advance;

    case 0x07:
      POP_SREG(vm, SRegES);
      goto advance;

    case 0x08:
      OR_EbGb(vm);
      goto advance;

    case 0x09:
      if (vm->i.os_32)
        OR_EdGd(vm);
      else
        OR_EwGw(vm);
      goto advance;

    case 0x0a:
      OR_GbEb(vm);
      goto advance;

    case 0x0b:
      if (vm->i.os_32)
        OR_GdEd(vm);
      else
        OR_GwEw(vm);
      goto advance;

    case 0x0c:
      OR_ALIb(vm);
      goto advance;

    case 0x0d:
      if (vm->i.os_32)
        OR_EAXId(vm);
      else
        OR_AXIw(vm);
      goto advance;

    case 0x0e:
      PUSH_SREG(vm, SRegCS);
      goto advance;

    case 0x10:
      ADC_EbGb(vm);
      goto advance;

    case 0x11:
      if (vm->i.os_32)
        ADC_EdGd(vm);
      else
        ADC_EwGw(vm);
      goto advance;

    case 0x12:
      ADC_GbEb(vm);
      goto advance;

    case 0x13:
      if (vm->i.os_32)
        ADC_GdEd(vm);
      else
        ADC_GwEw(vm);
      goto advance;

    case 0x14:
      ADC_ALIb(vm);
      goto advance;

    case 0x15:
      if (vm->i.os_32)
        ADC_EAXId(vm);
      else
        ADC_AXIw(vm);
      goto advance;

    case 0x16:
      PUSH_SREG(vm, SRegSS);
      goto advance;

    case 0x17:
      POP_SREG(vm, SRegSS);
      goto advance;

    case 0x18:
      SBB_EbGb(vm);
      goto advance;

    case 0x19:
      if (vm->i.os_32)
        SBB_EdGd(vm);
      else
        SBB_EwGw(vm);
      goto advance;

    case 0x1a:
      SBB_GbEb(vm);
      goto advance;

    case 0x1b:
      if (vm->i.os_32)
        SBB_GdEd(vm);
      else
        SBB_GwEw(vm);
      goto advance;

    case 0x1c:
      SBB_ALIb(vm);
      goto advance;

    case 0x1d:
      if (vm->i.os_32)
        SBB_EAXId(vm);
      else
        SBB_AXIw(vm);
      goto advance;

    case 0x1e:
      PUSH_SREG(vm, SRegDS);
      goto advance;

    case 0x1f:
      POP_SREG(vm, SRegDS);
      goto advance;

    case 0x20:
      AND_EbGb(vm);
      goto advance;

    case 0x21:
      if (vm->i.os_32)
        AND_EdGd(vm);
      else
        AND_EwGw(vm);
      goto advance;

    case 0x22:
      AND_GbEb(vm);
      goto advance;

    case 0x23:
      if (vm->i.os_32)
        AND_GdEd(vm);
      else
        AND_GwEw(vm);
      goto advance;

    case 0x24:
      AND_ALIb(vm);
      goto advance;

    case 0x25:
      if (vm->i.os_32)
        AND_EAXId(vm);
      else
        AND_AXIw(vm);
      goto advance;

    case 0x27:
      DAA(vm);
      goto advance;

    case 0x28:
      SUB_EbGb(vm);
      goto advance;

    case 0x29:
      if (vm->i.os_32)
        SUB_EdGd(vm);
      else
        SUB_EwGw(vm);
      goto advance;

    case 0x2a:
      SUB_GbEb(vm);
      goto advance;

    case 0x2b:
      if (vm->i.os_32)
        SUB_GdEd(vm);
      else
        SUB_GwEw(vm);
      goto advance;

    case 0x2c:
      SUB_ALIb(vm);
      goto advance;

    case 0x2d:
      if (vm->i.os_32)
        SUB_EAXId(vm);
      else
        SUB_AXIw(vm);
      goto advance;

    case 0x2f:
      DAS(vm);
      goto advance;

    case 0x30:
      XOR_EbGb(vm);
      goto advance;

    case 0x31:
      if (vm->i.os_32)
        XOR_EdGd(vm);
      else
        XOR_EwGw(vm);
      goto advance;

    case 0x32:
      XOR_GbEb(vm);
      goto advance;

    case 0x33:
      if (vm->i.os_32)
        XOR_GdEd(vm);
      else
        XOR_GwEw(vm);
      goto advance;

    case 0x34:
      XOR_ALIb(vm);
      goto advance;

    case 0x35:
      if (vm->i.os_32)
        XOR_EAXId(vm);
      else
        XOR_AXIw(vm);
      goto advance;

    case 0x37:
      AAA(vm);
      goto advance;

    case 0x38:
      CMP_EbGb(vm);
      goto advance;

    case 0x39:
      if (vm->i.os_32)
        CMP_EdGd(vm);
      else
        CMP_EwGw(vm);
      goto advance;

    case 0x3a:
      CMP_GbEb(vm);
      goto advance;

    case 0x3b:
      if (vm->i.os_32)
        CMP_GdEd(vm);
      else
        CMP_GwEw(vm);
      goto advance;

    case 0x3c:
      CMP_ALIb(vm);
      goto advance;

    case 0x3d:
      if (vm->i.os_32)
        CMP_EAXId(vm);
      else
        CMP_AXIw(vm);
      goto advance;

    case 0x3f:
      AAS(vm);
      goto advance;

    case 0x40:
    case 0x41:
    case 0x42:
    case 0x43:
    case 0x44:
    case 0x45:
    case 0x46:
    case 0x47:
      if (vm->i.os_32)
        INC_ERX(vm);
      else
        INC_RX(vm);
      goto advance;

    case 0x48:
    case 0x49:
    case 0x4a:
    case 0x4b:
    case 0x4c:
    case 0x4d:
    case 0x4e:
    case 0x4f:
      if (vm->i.os_32)
        DEC_ERX(vm);
      else
        DEC_RX(vm);
      goto advance;

    case 0x50: /* PUSH_eAX */
    case 0x51: /* PUSH_eCX */
    case 0x52: /* PUSH_eDX */
    case 0x53: /* PUSH_eBX */
    case 0x54: /* PUSH_eSP */
    case 0x55: /* PUSH_eBP */
    case 0x56: /* PUSH_eSI */
    case 0x57: /* PUSH_eDI */
      if (vm->i.os_32)
        push32(vm, ReadReg32(vm, vm->i.b1 & 0x07));
      else
        push16(vm, ReadReg16(vm, vm->i.b1 & 0x07));
      goto advance;

    case 0x58: /* POP_eAX */
    case 0x59: /* POP_eCX */
    case 0x5a: /* POP_eDX */
    case 0x5b: /* POP_eBX */
    case 0x5c: /* POP_eSP */
    case 0x5d: /* POP_eBP */
    case 0x5e: /* POP_eSI */
    case 0x5f: /* POP_eDI */
      if (vm->i.os_32) {
        Bit32u erx;
        pop32(vm, &erx);
        WriteReg32(vm, vm->i.b1 & 0x07, erx);
        }
      else {
        Bit16u rx;
        pop16(vm, &rx);
        WriteReg16(vm, vm->i.b1 & 0x07, rx);
        }
      goto advance;

    case 0x60:
      if (vm->i.os_32)
        PUSHAD32(vm);
      else
        PUSHAD16(vm);
      goto advance;

    case 0x61:
      if (vm->i.os_32)
        POPAD32(vm);
      else
        POPAD16(vm);
      goto advance;

    case 0x63:
      ARPL_EwGw(vm);
      goto advance;

    case 0x68: /* PUSH_Iv */
      if (vm->i.os_32)
        push32(vm, vm->i.Id);
      else
        push16(vm, vm->i.Iw);
      goto advance;

    case 0x69:
    case 0x6b:
      if (vm->i.os_32)
        IMUL_GdEdId(vm);
      else
        IMUL_GwEwIw(vm);
      goto advance;

    case 0x6a: /* PUSH_Ib (byte extended to word or dword) */
      PUSH_Iv(vm);
      goto advance;

    case 0x6c:
      INSB_YbDX(vm);
      goto advance;

    case 0x6d:
      INSW_YvDX(vm);
      goto advance;

    case 0x6e:
      OUTSB_DXXb(vm);
      goto advance;

    case 0x6f:
      OUTSW_DXXv(vm);
      goto advance;

    case 0x70:
    case 0x71:
    case 0x72:
    case 0x73:
    case 0x74:
    case 0x75:
    case 0x76:
    case 0x77:
    case 0x78:
    case 0x79:
    case 0x7a:
    case 0x7b:
    case 0x7c:
    case 0x7d:
    case 0x7e:
    case 0x7f:
      if (vm->i.os_32)
        JCC_Jd(vm);
      else
        JCC_Jw(vm);
      goto advance;

    case 0x84: TEST_EbGb(vm); goto advance;

    case 0x85:
      if (vm->i.os_32)
        TEST_EdGd(vm);
      else
        TEST_EwGw(vm);
      goto advance;

    case 0x86: XCHG_EbGb(vm); goto advance;

    case 0x87:
      if (vm->i.os_32)
        XCHG_EdGd(vm);
      else
        XCHG_EwGw(vm);
      goto advance;

    case 0x88: MOV_EbGb(vm); goto advance;

    case 0x89:
      if (vm->i.os_32)
        MOV_EdGd(vm);
      else
        MOV_EwGw(vm);
      goto advance;

    case 0x8a:
      MOV_GbEb(vm);
      goto advance;

    case 0x8b:
      if (vm->i.os_32)
        MOV_GdEd(vm);
      else
        MOV_GwEw(vm);
      goto advance;

    case 0x8c:
      MOV_EwSw(vm);
      goto advance;

    case 0x8d:
      if (vm->i.os_32)
        LEA_GdM(vm);
      else
        LEA_GwM(vm);
      goto advance;

    case 0x8e:
      MOV_SwEw(vm);
      goto advance;

    case 0x8f:
      if (vm->i.os_32)
        POP_Ed(vm);
      else
        POP_Ew(vm);
      goto advance;

    case 0x90: /* NOP */
      /* Nothing to do. */
      goto advance;

    case 0x91:
    case 0x92:
    case 0x93:
    case 0x94:
    case 0x95:
    case 0x96:
    case 0x97:
      if (vm->i.os_32)
        XCHG_ERXEAX(vm);
      else
        XCHG_RXAX(vm);
      goto advance;

    case 0x98:
      if (vm->i.os_32)
        CWDE(vm);
      else
        CBW(vm);
      goto advance;

    case 0x99:
      if (vm->i.os_32)
        CDQ(vm);
      else
        CWD(vm);
      goto advance;

    case 0x9a:
      if (vm->i.os_32)
        CALL32_Ap(vm);
      else
        CALL16_Ap(vm);
      goto advance;

    case 0x9b:
      FWAIT(vm);
      goto advance;

    case 0x9c:
      PUSHF_Fv(vm);
      goto advance;

    case 0x9d:
      POPF_Fv(vm);
      goto advance;

    case 0x9e:
      SAHF(vm);
      goto advance;

    case 0x9f:
      LAHF(vm);
      goto advance;

    case 0xa0:
      MOV_ALOb(vm);
      goto advance;

    case 0xa1:
      if (vm->i.os_32)
        MOV_EAXOd(vm);
      else
        MOV_AXOw(vm);
      goto advance;

    case 0xa2:
      MOV_ObAL(vm);
      goto advance;

    case 0xa3:
      if (vm->i.os_32)
        MOV_OdEAX(vm);
      else
        MOV_OwAX(vm);
      goto advance;

    case 0xa4:
      MOVSB_XbYb(vm);
      goto advance;

    case 0xa5:
      MOVSW_XvYv(vm);
      goto advance;

    case 0xa6:
      CMPSB_XbYb(vm);
      goto advance;

    case 0xa7:
      CMPSW_XvYv(vm);
      goto advance;

    case 0xa8:
      TEST_ALIb(vm);
      goto advance;

    case 0xa9:
      if (vm->i.os_32)
        TEST_EAXId(vm);
      else
        TEST_AXIw(vm);
      goto advance;

    case 0xaa:
      STOSB_YbAL(vm);
      goto advance;

    case 0xab:
      STOSW_YveAX(vm);
      goto advance;

    case 0xac:
      LODSB_ALXb(vm);
      goto advance;

    case 0xad:
      LODSW_eAXXv(vm);
      goto advance;

    case 0xae:
      SCASB_ALXb(vm);
      goto advance;

    case 0xaf:
      SCASW_eAXXv(vm);
      goto advance;

    case 0xb0: /* MOV_ALIb */
    case 0xb1:
    case 0xb2:
    case 0xb3:
    case 0xb4:
    case 0xb5:
    case 0xb6:
    case 0xb7:
      WriteReg8(vm, vm->i.b1 & 0x07, vm->i.Ib);
      goto advance;

    case 0xb8:
    case 0xb9:
    case 0xba:
    case 0xbb:
    case 0xbc:
    case 0xbd:
    case 0xbe:
    case 0xbf:
      if (vm->i.os_32)
        WriteReg32(vm, vm->i.b1 & 0x07, vm->i.Id);
      else
        WriteReg16(vm, vm->i.b1 & 0x07, vm->i.Iw);
      goto advance;

    case 0xc2:
      if (vm->i.os_32)
        RETnear32_Iw(vm);
      else
        RETnear16_Iw(vm);
      goto advance;

    case 0xc3:
      if (vm->i.os_32)
        RETnear32(vm);
      else
        RETnear16(vm);
      goto advance;

    case 0xc4:
      LxS_GvMp(vm, SRegES);
      goto advance;

    case 0xc5:
      LxS_GvMp(vm, SRegDS);
      goto advance;

    case 0xc6:
      MOV_EbIb(vm);
      goto advance;

    case 0xc7:
      if (vm->i.os_32)
        MOV_EdId(vm);
      else
        MOV_EwIw(vm);
      goto advance;

    case 0xc8:
      ENTER_IwIb(vm);
      goto advance;

    case 0xc9:
      LEAVE(vm);
      goto advance;

    case 0xca:
      if (vm->i.os_32)
        RETfar32_Iw(vm);
      else
        RETfar16_Iw(vm);
      goto advance;

    case 0xcb:
      if (vm->i.os_32)
        RETfar32(vm);
      else
        RETfar16(vm);
      goto advance;

    case 0xcc:
      INT3(vm);
      goto advance;

    case 0xcd:
      INT_Ib(vm);
      goto advance;

    case 0xcf:
      if (vm->i.os_32)
        IRET32(vm);
      else
        IRET16(vm);
      goto advance;

    case 0xd4:
      AAM(vm);
      goto advance;

    case 0xd5:
      AAD(vm);
      goto advance;

    case 0xd6:
      SALC(vm);
      goto advance;

    case 0xd7:
      XLAT(vm);
      goto advance;

    case 0xd8: ESC0(vm); goto advance;
    case 0xd9: ESC1(vm); goto advance;
    case 0xda: ESC2(vm); goto advance;
    case 0xdb: ESC3(vm); goto advance;
    case 0xdc: ESC4(vm); goto advance;
    case 0xdd: ESC5(vm); goto advance;
    case 0xde: ESC6(vm); goto advance;
    case 0xdf: ESC7(vm); goto advance;

    case 0xe0:
      LOOPNE_Jb(vm);
      goto advance;

    case 0xe1:
      LOOPE_Jb(vm);
      goto advance;

    case 0xe2:
      LOOP_Jb(vm);
      goto advance;

    case 0xe3:
      JCXZ_Jb(vm);
      goto advance;

    case 0xe4:
      IN_ALIb(vm);
      goto advance;

    case 0xe5:
      IN_eAXIb(vm);
      goto advance;

    case 0xe6:
      OUT_IbAL(vm);
      goto advance;

    case 0xe7:
      OUT_IbeAX(vm);
      goto advance;

    case 0xe8:
      if (vm->i.os_32)
        CALL_Ad(vm);
      else
        CALL_Aw(vm);
      goto advance;

    case 0xe9: /* JMP_Jv */
    case 0xeb: /* JMP_Jb */
      if (vm->i.os_32)
        JMP_Jd(vm);
      else
        JMP_Jw(vm);
      goto advance;

    case 0xea:
      JMP_Ap(vm);
      goto advance;

    case 0xec:
      IN_ALDX(vm);
      goto advance;

    case 0xed:
      IN_eAXDX(vm);
      goto advance;

    case 0xee: /* out dx, al */
      port = context->edx & 0xffff;
      IOPermCheck(vm, port, 1);
      sysIOOut(vm, port, 1, context->eax & 0xff);
      goto advance;

    case 0xef: /* out dx, eAX */
      port = context->edx & 0xffff;
      if (vm->i.os_32) {
        data = context->eax;
        len = 4;
        }
      else {
        data = context->eax & 0xffff;
        len = 2;
        }
      IOPermCheck(vm, port, len);
      sysIOOut(vm, port, len, data);
      goto advance;

    case 0xf4:
      HLT(vm);
      goto advance;

    case 0xf5:
      CMC(vm);
      goto advance;

    case 0xf8: /* CLC */
      G_SetCF(vm, 0);
      goto advance;

    case 0xf9: /* STC */
      G_SetCF(vm, 1);
      goto advance;

    case 0xfa:
      CLI_guest(vm);
      goto advance;

    case 0xfb:
      STI_guest(vm);
      goto advance;

    case 0xfc: /* CLD */
      G_SetDF(vm, 0);
      goto advance;

    case 0xfd: /* STD */
      G_SetDF(vm, 1);
      goto advance;

    case 0x102:
      LAR_GvEw(vm);
      goto advance;

    case 0x103:
      LSL_GvEw(vm);
      goto advance;

    case 0x106:
      CLTS(vm);
      goto advance;

    case 0x109:
      WBINVD(vm);
      goto advance;

    case 0x120:
      MOV_RdCd(vm);
      goto advance;

    case 0x121:
      MOV_RdDd(vm);
      goto advance;

    case 0x122:
      MOV_CdRd(vm);
      goto advance;

    case 0x123:
      MOV_DdRd(vm);
      goto advance;

    case 0x131:
      RDTSC(vm);
      goto advance;

    case 0x140:
    case 0x141:
    case 0x142:
    case 0x143:
    case 0x144:
    case 0x145:
    case 0x146:
    case 0x147:
    case 0x148:
    case 0x149:
    case 0x14a:
    case 0x14b:
    case 0x14c:
    case 0x14d:
    case 0x14e:
    case 0x14f:
      if (vm->i.os_32)
        CMOV_GdEd(vm);
      else
        CMOV_GwEw(vm);
      goto advance;

    case 0x180:
    case 0x181:
    case 0x182:
    case 0x183:
    case 0x184:
    case 0x185:
    case 0x186:
    case 0x187:
    case 0x188:
    case 0x189:
    case 0x18a:
    case 0x18b:
    case 0x18c:
    case 0x18d:
    case 0x18e:
    case 0x18f:
      if (vm->i.os_32)
        JCC_Jd(vm);
      else
        JCC_Jw(vm);
      goto advance;

    case 0x190: SETCC_Eb(vm); goto advance;
    case 0x191: SETCC_Eb(vm); goto advance;
    case 0x192: SETCC_Eb(vm); goto advance;
    case 0x193: SETCC_Eb(vm); goto advance;
    case 0x194: SETCC_Eb(vm); goto advance;
    case 0x195: SETCC_Eb(vm); goto advance;
    case 0x196: SETCC_Eb(vm); goto advance;
    case 0x197: SETCC_Eb(vm); goto advance;
    case 0x198: SETCC_Eb(vm); goto advance;
    case 0x199: SETCC_Eb(vm); goto advance;
    case 0x19A: SETCC_Eb(vm); goto advance;
    case 0x19B: SETCC_Eb(vm); goto advance;
    case 0x19C: SETCC_Eb(vm); goto advance;
    case 0x19D: SETCC_Eb(vm); goto advance;
    case 0x19E: SETCC_Eb(vm); goto advance;
    case 0x19F: SETCC_Eb(vm); goto advance;

    case 0x1a0:
      PUSH_SREG(vm, SRegFS);
      goto advance;

    case 0x1a1:
      POP_SREG(vm, SRegFS);
      goto advance;

    case 0x1a2: CPUID(vm); goto advance;

    case 0x1a3: BT_EvGv(vm); goto advance;

    case 0x1a4:
    case 0x1a5:
      if (vm->i.os_32)
        SHLD_EdGd(vm);
      else
        SHLD_EwGw(vm);
      goto advance;

    case 0x1a8:
      PUSH_SREG(vm, SRegGS);
      goto advance;

    case 0x1a9:
      POP_SREG(vm, SRegGS);
      goto advance;

    case 0x1ab: BTS_EvGv(vm); goto advance;

    case 0x1ac:
    case 0x1ad:
      if (vm->i.os_32)
        SHRD_EdGd(vm);
      else
        SHRD_EwGw(vm);
      goto advance;

    case 0x1af:
      if (vm->i.os_32)
        IMUL_GdEd(vm);
      else
        IMUL_GwEw(vm);
      goto advance;

    case 0x1b0:
      CMPXCHG_EbGb(vm);
      goto advance;

    case 0x1b1:
      if (vm->i.os_32)
        CMPXCHG_EdGd(vm);
      else
        CMPXCHG_EwGw(vm);
      goto advance;

    case 0x1b2: LxS_GvMp(vm, SRegSS); goto advance;

    case 0x1b3: BTR_EvGv(vm); goto advance;

    case 0x1b4: LxS_GvMp(vm, SRegFS); goto advance;
    case 0x1b5: LxS_GvMp(vm, SRegGS); goto advance;

    case 0x1b6:
      if (vm->i.os_32)
        MOVZX_GdEb(vm);
      else
        MOVZX_GwEb(vm);
      goto advance;

    case 0x1b7:
      if (vm->i.os_32)
        MOVZX_GdEw(vm);
      else
        MOVZX_GwEw(vm);
      goto advance;

    case 0x1bb: BTC_EvGv(vm); goto advance;
    case 0x1bc: BSF_GvEv(vm); goto advance;
    case 0x1bd: BSR_GvEv(vm); goto advance;

    case 0x1be:
      if (vm->i.os_32)
        MOVSX_GdEb(vm);
      else
        MOVSX_GwEb(vm);
      goto advance;

    case 0x1bf:
      if (vm->i.os_32)
        MOVSX_GdEw(vm);
      else
        MOVSX_GwEw(vm);
      goto advance;

    case 0x1c0:
      XADD_EbGb(vm);
      goto advance;

    case 0x1c1:
      if (vm->i.os_32)
        XADD_EdGd(vm);
      else
        XADD_EwGw(vm);
      goto advance;

    case 0x1C8:
    case 0x1C9:
    case 0x1CA:
    case 0x1CB:
    case 0x1CC:
    case 0x1CD:
    case 0x1CE:
    case 0x1CF:
      BSWAP_ERX(vm);
      goto advance;

    default:
      monpanic(vm, "default case on i.b1 of 0x%x\n",
        vm->i.b1);
    }

/* Special Group opcodes: */
group_n:
  group = GetGroupIndex(vm->i.attr);

  /*ucode_map.groups[group][vm->i.nnn]; */
  switch ((group<<3) | vm->i.nnn) {
    case (G1EbIb<<3) | 0: ADD_EbIb(vm); break;
    case (G1EbIb<<3) | 1: OR_EbIb(vm);  break;
    case (G1EbIb<<3) | 2: ADC_EbIb(vm); break;
    case (G1EbIb<<3) | 3: SBB_EbIb(vm); break;
    case (G1EbIb<<3) | 4: AND_EbIb(vm); break;
    case (G1EbIb<<3) | 5: SUB_EbIb(vm); break;
    case (G1EbIb<<3) | 6: XOR_EbIb(vm); break;
    case (G1EbIb<<3) | 7: CMP_EbIb(vm); break;

    case (G1Ew<<3) | 0: ADD_EwIw(vm); break;
    case (G1Ew<<3) | 1: OR_EwIw(vm); break;
    case (G1Ew<<3) | 2: ADC_EwIw(vm); break;
    case (G1Ew<<3) | 3: SBB_EwIw(vm); break;
    case (G1Ew<<3) | 4: AND_EwIw(vm); break;
    case (G1Ew<<3) | 5: SUB_EwIw(vm); break;
    case (G1Ew<<3) | 6: XOR_EwIw(vm); break;
    case (G1Ew<<3) | 7: CMP_EwIw(vm); break;

    case (G1Ed<<3) | 0: ADD_EdId(vm); break;
    case (G1Ed<<3) | 1:  OR_EdId(vm); break;
    case (G1Ed<<3) | 2: ADC_EdId(vm); break;
    case (G1Ed<<3) | 3: SBB_EdId(vm); break;
    case (G1Ed<<3) | 4: AND_EdId(vm); break;
    case (G1Ed<<3) | 5: SUB_EdId(vm); break;
    case (G1Ed<<3) | 6: XOR_EdId(vm); break;
    case (G1Ed<<3) | 7: CMP_EdId(vm); break;

    case (G2Eb<<3) | 0: ROL_Eb(vm); break;
    case (G2Eb<<3) | 1: ROR_Eb(vm); break;
    case (G2Eb<<3) | 2: RCL_Eb(vm); break;
    case (G2Eb<<3) | 3: RCR_Eb(vm); break;
    case (G2Eb<<3) | 4: SHL_Eb(vm); break;
    case (G2Eb<<3) | 5: SHR_Eb(vm); break;
    case (G2Eb<<3) | 6: SHL_Eb(vm); break;
    case (G2Eb<<3) | 7: SAR_Eb(vm); break;

    case (G2Ew<<3) | 0: ROL_Ew(vm); break;
    case (G2Ew<<3) | 1: ROR_Ew(vm); break;
    case (G2Ew<<3) | 2: RCL_Ew(vm); break;
    case (G2Ew<<3) | 3: RCR_Ew(vm); break;
    case (G2Ew<<3) | 4: SHL_Ew(vm); break;
    case (G2Ew<<3) | 5: SHR_Ew(vm); break;
    case (G2Ew<<3) | 6: SHL_Ew(vm); break;
    case (G2Ew<<3) | 7: SAR_Ew(vm); break;

    case (G2Ed<<3) | 0: ROL_Ed(vm); break;
    case (G2Ed<<3) | 1: ROR_Ed(vm); break;
    case (G2Ed<<3) | 2: RCL_Ed(vm); break;
    case (G2Ed<<3) | 3: RCR_Ed(vm); break;
    case (G2Ed<<3) | 4: SHL_Ed(vm); break;
    case (G2Ed<<3) | 5: SHR_Ed(vm); break;
    case (G2Ed<<3) | 6: SHL_Ed(vm); break;
    case (G2Ed<<3) | 7: SAR_Ed(vm); break;

    case (G3Eb<<3) | 0:
    case (G3Eb<<3) | 1: TEST_EbIb(vm); break;
    case (G3Eb<<3) | 2: NOT_Eb(vm); break;
    case (G3Eb<<3) | 3: NEG_Eb(vm); break;
    case (G3Eb<<3) | 4: MUL_ALEb(vm); break;
    case (G3Eb<<3) | 5: IMUL_ALEb(vm); break;
    case (G3Eb<<3) | 6: DIV_ALEb(vm); break;
    case (G3Eb<<3) | 7: IDIV_ALEb(vm); break;

    case (G3Ew<<3) | 0:
    case (G3Ew<<3) | 1: TEST_EwIw(vm); break;
    case (G3Ew<<3) | 2: NOT_Ew(vm); break;
    case (G3Ew<<3) | 3: NEG_Ew(vm); break;
    case (G3Ew<<3) | 4: MUL_AXEw(vm); break;
    case (G3Ew<<3) | 5: IMUL_AXEw(vm); break;
    case (G3Ew<<3) | 6: DIV_AXEw(vm); break;
    case (G3Ew<<3) | 7: IDIV_AXEw(vm); break;

    case (G3Ed<<3) | 0: TEST_EdId(vm); break;
    case (G3Ed<<3) | 1: TEST_EdId(vm); break;
    case (G3Ed<<3) | 2: NOT_Ed(vm); break;
    case (G3Ed<<3) | 3: NEG_Ed(vm); break;
    case (G3Ed<<3) | 4: MUL_EAXEd(vm); break;
    case (G3Ed<<3) | 5: IMUL_EAXEd(vm); break;
    case (G3Ed<<3) | 6: DIV_EAXEd(vm); break;
    case (G3Ed<<3) | 7: IDIV_EAXEd(vm); break;

    case (G4<<3) | 0: INC_Eb(vm); break;
    case (G4<<3) | 1: DEC_Eb(vm); break;
    case (G4<<3) | 2: monprint(vm, "G4.2 unhandled\n"); goto unhandled;
    case (G4<<3) | 3: monprint(vm, "G4.3 unhandled\n"); goto unhandled;
    case (G4<<3) | 4: monprint(vm, "G4.4 unhandled\n"); goto unhandled;
    case (G4<<3) | 5: monprint(vm, "G4.5 unhandled\n"); goto unhandled;
    case (G4<<3) | 6: monprint(vm, "G4.6 unhandled\n"); goto unhandled;
    case (G4<<3) | 7: monprint(vm, "G4.7 unhandled\n"); goto unhandled;

    case (G5w<<3) | 0: INC_Ew(vm); break;
    case (G5w<<3) | 1: DEC_Ew(vm); break;
    case (G5w<<3) | 2: CALL_Ew(vm); break;
    case (G5w<<3) | 3: CALL16_Ep(vm); break;
    case (G5w<<3) | 4: JMP_Ew(vm); break;
    case (G5w<<3) | 5: JMP16_Ep(vm); break;
    case (G5w<<3) | 6: PUSH_Ew(vm); break;
    case (G5w<<3) | 7: monprint(vm, "G5w7 unhandled\n"); goto unhandled;

    case (G5d<<3) | 0: INC_Ed(vm); break;
    case (G5d<<3) | 1: DEC_Ed(vm); break;
    case (G5d<<3) | 2: CALL_Ed(vm); break;
    case (G5d<<3) | 3: CALL32_Ep(vm); break;
    case (G5d<<3) | 4: JMP_Ed(vm); break;
    case (G5d<<3) | 5: JMP32_Ep(vm); break;
    case (G5d<<3) | 6: PUSH_Ed(vm); break;
    case (G5d<<3) | 7: monprint(vm, "G5d7 unhandled\n"); goto unhandled;

    case (G6<<3) | 0: SLDT_Ew(vm); break;
    case (G6<<3) | 1: STR_Ew(vm); break;
    case (G6<<3) | 2: LLDT_Ew(vm); break;
    case (G6<<3) | 3: LTR_Ew(vm); break;
    case (G6<<3) | 4: VERR_Ew(vm); break;
    case (G6<<3) | 5: VERW_Ew(vm); break;
    case (G6<<3) | 6: monprint(vm, "G6.6 unhandled\n"); goto unhandled;
    case (G6<<3) | 7: monprint(vm, "G6.7 unhandled\n"); goto unhandled;

    case (G7<<3) | 0: SGDT_Ms(vm); break;
    case (G7<<3) | 1: SIDT_Ms(vm); break;
    case (G7<<3) | 2: LGDT_Ms(vm); break;
    case (G7<<3) | 3: LIDT_Ms(vm); break;
    case (G7<<3) | 4: SMSW_Ew(vm); break;
    case (G7<<3) | 5: monprint(vm, "G7.5 unhandled\n"); goto unhandled;
    case (G7<<3) | 6: LMSW_Ew(vm); break;
    case (G7<<3) | 7: INVLPG(vm); break;

    case (G8EvIb<<3) | 0:
    case (G8EvIb<<3) | 1:
    case (G8EvIb<<3) | 2:
    case (G8EvIb<<3) | 3:
      monprint(vm, "G8EvIb unhandled\n");
      goto unhandled;
    case (G8EvIb<<3) | 4: BT_EvIb(vm);  break;
    case (G8EvIb<<3) | 5: BTS_EvIb(vm); break;
    case (G8EvIb<<3) | 6: BTR_EvIb(vm); break;
    case (G8EvIb<<3) | 7: BTC_EvIb(vm); break;

    case (G9<<3) | 0:
    default:
      monprint(vm, "GroupN unhandled\n");
      goto unhandled;
    }

advance:

    if (vm->i.rep_used && (vm->i.attr & Repeatable)) {
      if (vm->i.attr & RepeatableZF) {
        if (vm->i.as_32) {
          if (G_ECX(vm) != 0) {
            G_ECX(vm) -= 1;
            }
          if ((vm->i.rep_used==0xf3) && (G_GetZF(vm)==0)) goto repeat_done;
          if ((vm->i.rep_used==0xf2) && (G_GetZF(vm)!=0)) goto repeat_done;
          if (G_ECX(vm) == 0) goto repeat_done;
          goto repeat_not_done;
          }
        else {
          if (G_CX(vm) != 0) {
            G_CX(vm) -= 1;
            }
          if ((vm->i.rep_used==0xf3) && (G_GetZF(vm)==0)) goto repeat_done;
          if ((vm->i.rep_used==0xf2) && (G_GetZF(vm)!=0)) goto repeat_done;
          if (G_CX(vm) == 0) goto repeat_done;
          goto repeat_not_done;
          }
        }
      else { /* normal repeat, no concern for ZF */
        if (vm->i.as_32) {
          if (G_ECX(vm) != 0) {
            G_ECX(vm) -= 1;
            }
          if (G_ECX(vm) == 0) goto repeat_done;
          goto repeat_not_done;
          }
        else { /* 16bit addrsize */
          if (G_CX(vm) != 0) {
            G_CX(vm) -= 1;
            }
          if (G_CX(vm) == 0) goto repeat_done;
          goto repeat_not_done;
          }
        }
      /* shouldn't get here from above */

repeat_done:
      context->eip += vm->i.ilen;
repeat_not_done:
      }

  /* Execution complete, Commit EIP & ESP.  Actually the following
   * code signals to the debugger, that the values in eip/esp are
   * good and that the instruction completed.
   */
  vm->guest_cpu.prev_eip = -1;
  vm->guest_cpu.prev_esp = -1;

  /* Pretend instruction took N cycles to execute. */
  vm->system.cyclesElapsed += CyclesPerInstruction;
  // instructionsExpired += 1; // xxx Conditionally compile

checkStopReasons:
  if ( vm->system.cyclesElapsed >= vm->system.cyclesRemaining ) {
    stopReason |= EmulateStopReasonCycles;
    }
#if 0
  if ( instructionsExpired >= *instructionsRequested ) {
    *instructionsRequested = instructionsExpired;
    stopReason |= EmulateStopReasonInstructions;
    }
#endif
  if (vm->modeChange)
    stopReason |= EmulateStopReasonModeChange;
  if (stopReason)
    return(stopReason);
  goto cpuLoop;

handleAsyncEvent:
  /* I made up this bitmask to mean HALT state. */
  if (vm->guest_cpu.debug_trap & 0x80000000) {
    vm->guest_cpu.debug_trap = 0; /* clear traps for after resume */
    vm->guest_cpu.inhibit_mask = 0; /* clear inhibits for after resume */
    /* Need to fix this for cosimulation. */

    while (1) {
      if (vm->system.intr && G_GetIF(vm)) {
        break;
        }
      if (vm->system.cyclesElapsed >= vm->system.cyclesRemaining)
        goto checkStopReasons;
      /* Accelerate things by skipping to the next timer event. */
      vm->system.cyclesElapsed = vm->system.cyclesRemaining;
      goto checkStopReasons;
      }
    }

  /* Priority 1: Hardware Reset and Machine Checks */
  /*   RESET */
  /*   Machine Check */
  /* (plex86 doesn't support these) */

  /* Priority 2: Trap on Task Switch */
  /*   T flag in TSS is set */
  if (vm->guest_cpu.debug_trap & 0x00008000) {
    monpanic(vm, "Trap on Task Switch\n");
    vm->guest_cpu.dr6 |= vm->guest_cpu.debug_trap;
    exception(vm, ExceptionDB, 0);
    }

  /* Priority 3: External Hardware Interventions */
  /*   FLUSH */
  /*   STOPCLK */
  /*   SMI */
  /*   INIT  */
  /* (plex86 doesn't support these) */

  /* Priority 4: Traps on Previous Instruction */
  /*   Breakpoints */
  /*   Debug Trap Exceptions (TF flag set or data/IO breakpoint) */
  if ( vm->guest_cpu.debug_trap &&
       !(vm->guest_cpu.inhibit_mask & InhibitDebug) ) {
    /* A trap may be inhibited on this boundary due to an instruction */
    /* which loaded SS.  If so we clear the inhibit_mask below */
    /* and don't execute this code until the next boundary. */
    /* Commit debug events to DR6 */
    monpanic(vm, "Trap on Prev Instr\n");
    vm->guest_cpu.dr6 |= vm->guest_cpu.debug_trap; 
    exception(vm, ExceptionDB, 0);
    }

  /* Priority 5: External Interrupts */
  /*   NMI Interrupts */
  /*   Maskable Hardware Interrupts */
  if (vm->guest_cpu.inhibit_mask & InhibitInterrupts) {
    /* Processing external interrupts is inhibited on this */
    /* boundary because of certain instructions like STI. */
    /* inhibit_mask is cleared below, in which case we will have */
    /* an opportunity to check interrupts on the next instruction */
    /* boundary. */
    }
#define DbgAsyncIntr 1 /* don't ask! */
  else if (vm->system.intr && G_GetIF(vm) && DbgAsyncIntr) {
    Bit8u vector;

/* xxx Need to look into getting rid of this */
/* Store EIP,ESP in case we need to rewind them due to an */
/* exception caused by the instruction. */
vm->guest_cpu.prev_eip = vm->guest.addr.guest_context->eip;
vm->guest_cpu.prev_esp = vm->guest.addr.guest_context->esp;
 
    /* NOTE: similar code in ::take_irq() */
    vector = sysIAC(vm); /* may set INTR with next interrupt */
    vm->guest_cpu.errorno = 0;
    vm->guest_cpu.EXT     = 1; /* external event */
    interrupt(vm, vector, 0, 0, 0);
    }
#define DbgAsyncDma 1 /* don't ask! */
  else if (vm->system.hrq && DbgAsyncDma) {
    /* NOTE: similar code in ::take_dma() */
    /* assert Hold Acknowledge (HLDA) and go into a bus hold state */
    monpanic(vm, "DMA event\n");
    sysRaiseHLDA(vm);
    }

  /* Priority 6: Faults from fetching next instruction */
  /*   Code breakpoint fault */
  /*   Code segment limit violation (priority 7 on 486/Pentium) */
  /*   Code page fault (priority 7 on 486/Pentium) */
  /* (handled in main decode loop) */

  /* Priority 7: Faults from decoding next instruction */
  /*   Instruction length > 15 bytes */
  /*   Illegal opcode */
  /*   Coprocessor not available */
  /* (handled in main decode loop etc) */

  /* Priority 8: Faults on executing an instruction */
  /*   Floating point execution */
  /*   Overflow */
  /*   Bound error */
  /*   Invalid TSS */
  /*   Segment not present */
  /*   Stack fault */
  /*   General protection */
  /*   Data page fault */
  /*   Alignment check */
  /* (handled by rest of the code) */


  if ( !(vm->system.intr ||
         vm->guest_cpu.debug_trap ||
         vm->system.hrq) )
    vm->guest_cpu.async_event = 0;
  goto asyncEventsProcessed;

emulateFetch:
  /* See if we can read(fetch) using the current monitor mapping. */
  /* This would be the case if we are virtualizing an instruction, */
  /* where the page is mapped in to the monitor and accessible from */
  /* the currently executing guest code. */
  us = G_GetCPL(vm)==3;
  rw = 0;
  switch ( (ret = mapGuestLinAddr(vm, laddr0, &guest_ppi,
            us, rw, PageUsageVCode, &perror)) ) {
    case MapLinOK:
    case MapLinAlreadyMapped:
      break;
    case MapLinException:
      vm->guest_cpu.cr2 = laddr0;
      exception(vm, ExceptionPF, perror);
      break;
    case MapLinMonConflict:
    case MapLinPPageOOB:
    default:
      monpanic(vm, "emulate:1 default case of %u\n", ret);
    }
  /* Page is OK for reading (fetching) using natural CPU reads */
  ptr = (Bit8u*) Guest2Monitor(vm, laddr0);
  ret = fetchDecode(ptr, &vm->i, max_len, seg32);
  if ( ret==0 ) {
    Bit32u laddr1;
    unsigned map_ret;
    /* Instruction runs over into next page.  See if it is OK to */
    /* read natively in the next page.  If so, then no problem. */
    laddr1 = (laddr0 | 0x00000fff) + 1;
    map_ret = mapGuestLinAddr(vm, laddr1, &guest_ppi,
                              us, rw, PageUsageVCode, &perror);
    switch (map_ret) {
      case MapLinOK:
      case MapLinAlreadyMapped:
        break;
      case MapLinException:
        vm->guest_cpu.cr2 = laddr1;
        exception(vm, ExceptionPF, perror);
        break;
      case MapLinMonConflict:
      case MapLinPPageOOB:
      default:
        monpanic(vm, "emulate:2 default case of %u\n", map_ret);
      }
    ret = fetchDecode(ptr, &vm->i, MaxIA32OpcodeLen, seg32);
    if ( ret==0 )
      monpanic(vm, "fetchDecode returned 0 (multipage)\n");
    }
  goto fetchComplete;

unhandled:
  monpanic(vm, "i.b1=0x%x, i.nnn=%u, group=%u\n",
    vm->i.b1, vm->i.nnn, group);
}

  void
emulate_interrupt(vm_t *vm, unsigned vector)
{
/*monprint(vm, "EmInt:\n");*/
  /* Store EIP,ESP in case we need to rewind them due to an */
  /* exception caused by the instruction. */
  vm->guest_cpu.prev_eip = vm->guest.addr.guest_context->eip;
  vm->guest_cpu.prev_esp = vm->guest.addr.guest_context->esp;
 
  vm->guest_cpu.errorno = 0;
  vm->guest_cpu.EXT     = 1; /* external event */
  if (SetJmp(vm->jmp_buf_env)) {
    /* nonzero return, means returning from longjmp() */
    return;
    }
  interrupt(vm, vector, 0, 0, 0);
}

  void
emulate_exception(vm_t *vm, unsigned vector, Bit32u gerror)
{
/*monprint(vm, "EmExc:\n");*/
  /* Store EIP,ESP in case we need to rewind them due to an */
  /* exception caused by the instruction. */
  vm->guest_cpu.prev_eip = vm->guest.addr.guest_context->eip;
  vm->guest_cpu.prev_esp = vm->guest.addr.guest_context->esp;
 
  vm->guest_cpu.EXT = 0;
  vm->guest_cpu.errorno = 0;

  if (SetJmp(vm->jmp_buf_env)) {
    /* nonzero return, means returning from longjmp() */
    return;
    }
  exception(vm, vector, gerror);
}


#if 0
// xxx Delete extra emulate_async_checks routine
  void
emulate_async_checks(vm_t *vm)
{
  if (SetJmp(vm->jmp_buf_env)) {
    return;
    }
  async_checks(vm);
}


  void
async_checks(vm_t *vm)
{
  if (vm->guest_cpu.async_event) {
    /* I made up this bitmask to mean HALT state. */
    if (vm->guest_cpu.debug_trap & 0x80000000) {
      vm->guest_cpu.debug_trap = 0; /* clear traps for after resume */
      vm->guest_cpu.inhibit_mask = 0; /* clear inhibits for after resume */
      /* Need to fix this for cosimulation. */
  
      while (1) {
        if (vm->system.intr && G_GetIF(vm)) {
          break;
          }
        vm->system.elapsed += 400;
        guest_cycles_elapsed(vm);
        }
      }
  
    /* Priority 1: Hardware Reset and Machine Checks */
    /*   RESET */
    /*   Machine Check */
    /* (plex86 doesn't support these) */
  
    /* Priority 2: Trap on Task Switch */
    /*   T flag in TSS is set */
    if (vm->guest_cpu.debug_trap & 0x00008000) {
      monpanic(vm, "Trap on Task Switch\n");
      vm->guest_cpu.dr6 |= vm->guest_cpu.debug_trap;
      exception(vm, ExceptionDB, 0);
      }
  
    /* Priority 3: External Hardware Interventions */
    /*   FLUSH */
    /*   STOPCLK */
    /*   SMI */
    /*   INIT  */
    /* (plex86 doesn't support these) */
  
    /* Priority 4: Traps on Previous Instruction */
    /*   Breakpoints */
    /*   Debug Trap Exceptions (TF flag set or data/IO breakpoint) */
    if ( vm->guest_cpu.debug_trap &&
         !(vm->guest_cpu.inhibit_mask & InhibitDebug) ) {
      /* A trap may be inhibited on this boundary due to an instruction */
      /* which loaded SS.  If so we clear the inhibit_mask below */
      /* and don't execute this code until the next boundary. */
      /* Commit debug events to DR6 */
      monpanic(vm, "Trap on Prev Instr\n");
      vm->guest_cpu.dr6 |= vm->guest_cpu.debug_trap; 
      exception(vm, ExceptionDB, 0);
      }
  
    /* Priority 5: External Interrupts */
    /*   NMI Interrupts */
    /*   Maskable Hardware Interrupts */
    if (vm->guest_cpu.inhibit_mask & InhibitInterrupts) {
      /* Processing external interrupts is inhibited on this */
      /* boundary because of certain instructions like STI. */
      /* inhibit_mask is cleared below, in which case we will have */
      /* an opportunity to check interrupts on the next instruction */
      /* boundary. */
      }
  #define DbgAsyncIntr 1 /* don't ask! */
    else if (vm->system.intr && G_GetIF(vm) && DbgAsyncIntr) {
      Bit8u vector;
  
  /* +++ Need to look into getting rid of this */
  /* Store EIP,ESP in case we need to rewind them due to an */
  /* exception caused by the instruction. */
  vm->guest_cpu.prev_eip = vm->guest.addr.guest_context->eip;
  vm->guest_cpu.prev_esp = vm->guest.addr.guest_context->esp;
   
      /* NOTE: similar code in ::take_irq() */
      vector = sysIAC(vm); /* may set INTR with next interrupt */
      vm->guest_cpu.errorno = 0;
      vm->guest_cpu.EXT     = 1; /* external event */
      interrupt(vm, vector, 0, 0, 0);
      }
  #define DbgAsyncDma 1 /* don't ask! */
    else if (vm->system.hrq && DbgAsyncDma) {
      /* NOTE: similar code in ::take_dma() */
      /* assert Hold Acknowledge (HLDA) and go into a bus hold state */
      monpanic(vm, "DMA event\n");
      sysRaiseHLDA(vm);
      }
  
    /* Priority 6: Faults from fetching next instruction */
    /*   Code breakpoint fault */
    /*   Code segment limit violation (priority 7 on 486/Pentium) */
    /*   Code page fault (priority 7 on 486/Pentium) */
    /* (handled in main decode loop) */
  
    /* Priority 7: Faults from decoding next instruction */
    /*   Instruction length > 15 bytes */
    /*   Illegal opcode */
    /*   Coprocessor not available */
    /* (handled in main decode loop etc) */
  
    /* Priority 8: Faults on executing an instruction */
    /*   Floating point execution */
    /*   Overflow */
    /*   Bound error */
    /*   Invalid TSS */
    /*   Segment not present */
    /*   Stack fault */
    /*   General protection */
    /*   Data page fault */
    /*   Alignment check */
    /* (handled by rest of the code) */
  
  
    if ( !(vm->system.intr ||
           vm->guest_cpu.debug_trap ||
           vm->system.hrq) )
      vm->guest_cpu.async_event = 0;
    }

  /* Now we can handle things which are synchronous to instruction */
  /* execution. */
  if (G_GetRF(vm)) {
    G_SetRF(vm, 0);
    }
#if 0
#if BX_X86_DEBUGGER /* +++ */
  else {
    /* only bother comparing if any breakpoints enabled */
    if ( BX_CPU_THIS_PTR dr7 & 0x000000ff ) {
      Bit32u iaddr =
        BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base +
        BX_CPU_THIS_PTR prev_eip;
      Bit32u dr6_bits;
      if ( (dr6_bits = hwdebug_compare(iaddr, 1, BX_HWDebugInstruction,
                                       BX_HWDebugInstruction)) ) {
        /* Add to the list of debug events thus far. */
        BX_CPU_THIS_PTR debug_trap |= dr6_bits;
        BX_CPU_THIS_PTR async_event = 1;
        /* If debug events are not inhibited on this boundary, */
        /* fire off a debug fault.  Otherwise handle it on the next */
        /* boundary. (becomes a trap) */
        if ( !(BX_CPU_THIS_PTR inhibit_mask & BX_INHIBIT_DEBUG) ) {
          /* Commit debug events to DR6 */
          BX_CPU_THIS_PTR dr6 = BX_CPU_THIS_PTR debug_trap;
          exception(BX_DB_EXCEPTION, 0, 0); /* no error, not interrupt */
          }
        }
      }
    }
#endif  /* BX_X86_DEBUGGER */
#endif  /* 0 */

  /* Events in the mask may have been inhibited during processing */
  /* of the events above.  Now that they have been properly ignored, */
  /* we can clear the inhibit mask. */
  vm->guest_cpu.inhibit_mask = 0; /* clear inhibits */
}
#endif