aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lufa/Demos/Host/LowLevel/KeyboardHost/KeyboardHost.c
blob: df733833921ebf0be160fb3a5667c79082373b58 (plain)
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
/*
             LUFA Library
     Copyright (C) Dean Camera, 2017.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaims all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  Main source file for the KeyboardHost demo. This file contains the main tasks of
 *  the demo and is responsible for the initial application hardware configuration.
 */

#include "KeyboardHost.h"

/** Main program entry point. This routine configures the hardware required by the application, then
 *  enters a loop to run the application tasks in sequence.
 */
int main(void)
{
	SetupHardware();

	puts_P(PSTR(ESC_FG_CYAN "Keyboard HID Host Demo running.\r\n" ESC_FG_WHITE));

	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
	GlobalInterruptEnable();

	for (;;)
	{
		KeyboardHost_Task();

		USB_USBTask();
	}
}

/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware(void)
{
#if (ARCH == ARCH_AVR8)
	/* Disable watchdog if enabled by bootloader/fuses */
	MCUSR &= ~(1 << WDRF);
	wdt_disable();

	/* Disable clock division */
	clock_prescale_set(clock_div_1);
#endif

	/* Hardware Initialization */
	Serial_Init(9600, false);
	LEDs_Init();
	USB_Init();

	/* Create a stdio stream for the serial port for stdin and stdout */
	Serial_CreateStream(NULL);
}

/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
 *  starts the library USB task to begin the enumeration and USB management process.
 */
void EVENT_USB_Host_DeviceAttached(void)
{
	puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
}

/** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
 *  stops the library USB task management process.
 */
void EVENT_USB_Host_DeviceUnattached(void)
{
	puts_P(PSTR(ESC_FG_GREEN "Device Unattached.\r\n" ESC_FG_WHITE));
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
}

/** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
 *  enumerated by the host and is now ready to be used by the application.
 */
void EVENT_USB_Host_DeviceEnumerationComplete(void)
{
	puts_P(PSTR("Getting Config Data.\r\n"));

	uint8_t ErrorCode;

	/* Get and process the configuration descriptor data */
	if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
	{
		if (ErrorCode == ControlError)
		  puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
		else
		  puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));

		printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);

		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		return;
	}

	/* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
	if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
	{
		printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"
		                         " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);

		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		return;
	}

	/* HID class request to set the keyboard protocol to the Boot Protocol */
	USB_ControlRequest = (USB_Request_Header_t)
		{
			.bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
			.bRequest      = HID_REQ_SetProtocol,
			.wValue        = 0,
			.wIndex        = 0,
			.wLength       = 0,
		};

	/* Select the control pipe for the request transfer */
	Pipe_SelectPipe(PIPE_CONTROLPIPE);

	/* Send the request, display error and wait for device detach if request fails */
	if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
	{
		printf_P(PSTR(ESC_FG_RED "Control Error (Set Protocol).\r\n"
		                         " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);

		LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
		USB_Host_SetDeviceConfiguration(0);
		return;
	}

	puts_P(PSTR("Keyboard Enumerated.\r\n"));
	LEDs_SetAllLEDs(LEDMASK_USB_READY);
}

/** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
{
	USB_Disable();

	printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
	                         " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);

	LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
	for(;;);
}

/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
 *  enumerating an attached USB device.
 */
void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
                                            const uint8_t SubErrorCode)
{
	printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
	                         " -- Error Code %d\r\n"
	                         " -- Sub Error Code %d\r\n"
	                         " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);

	LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
}

/** Task to read in and processes the next report from the attached device, displaying the report
 *  contents on the board LEDs and via the serial port.
 */
void KeyboardHost_Task(void)
{
	if (USB_HostState != HOST_STATE_Configured)
	  return;

	/* Select keyboard data pipe */
	Pipe_SelectPipe(KEYBOARD_DATA_IN_PIPE);

	/* Unfreeze keyboard data pipe */
	Pipe_Unfreeze();

	/* Check to see if a packet has been received */
	if (!(Pipe_IsINReceived()))
	{
		/* Refreeze HID data IN pipe */
		Pipe_Freeze();

		return;
	}

	/* Ensure pipe contains data before trying to read from it */
	if (Pipe_IsReadWriteAllowed())
	{
		USB_KeyboardReport_Data_t KeyboardReport;

		/* Read in keyboard report data */
		Pipe_Read_Stream_LE(&KeyboardReport, sizeof(KeyboardReport), NULL);

		/* Indicate if the modifier byte is non-zero (special key such as shift is being pressed) */
		LEDs_ChangeLEDs(LEDS_LED1, (KeyboardReport.Modifier) ? LEDS_LED1 : 0);

		uint8_t KeyCode = KeyboardReport.KeyCode[0];

		/* Check if a key has been pressed */
		if (KeyCode)
		{
			/* Toggle status LED to indicate keypress */
			LEDs_ToggleLEDs(LEDS_LED2);

			char PressedKey = 0;

			/* Retrieve pressed key character if alphanumeric */
			if ((KeyCode >= HID_KEYBOARD_SC_A) && (KeyCode <= HID_KEYBOARD_SC_Z))
			{
				PressedKey = (KeyCode - HID_KEYBOARD_SC_A) + 'A';
			}
			else if ((KeyCode >= HID_KEYBOARD_SC_1_AND_EXCLAMATION) &
					 (KeyCode  < HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS))
			{
				PressedKey = (KeyCode - HID_KEYBOARD_SC_1_AND_EXCLAMATION) + '1';
			}
			else if (KeyCode == HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS)
			{
				PressedKey = '0';
			}
			else if (KeyCode == HID_KEYBOARD_SC_SPACE)
			{
				PressedKey = ' ';
			}
			else if (KeyCode == HID_KEYBOARD_SC_ENTER)
			{
				PressedKey = '\n';
			}

			/* Print the pressed key character out through the serial port if valid */
			if (PressedKey)
			  putchar(PressedKey);
		}
	}

	/* Clear the IN endpoint, ready for next data packet */
	Pipe_ClearIN();

	/* Refreeze keyboard data pipe */
	Pipe_Freeze();
}
118'>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
/*
 *  i386 on i386 translation
 * 
 *  Copyright (c) 2003 Fabrice Bellard
 *
 * 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 "config.h"

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>

#include "cpu.h"
#include "exec-all.h"
#include "disas.h"

#ifdef USE_CODE_COPY

#include <signal.h>
#include <sys/mman.h>
#include <sys/ucontext.h>

extern char exec_loop;

/* operand size */
enum {
    OT_BYTE = 0,
    OT_WORD,
    OT_LONG, 
    OT_QUAD,
};

#define PREFIX_REPZ   0x01
#define PREFIX_REPNZ  0x02
#define PREFIX_LOCK   0x04
#define PREFIX_DATA   0x08
#define PREFIX_ADR    0x10

typedef struct DisasContext {
    /* current insn context */
    int override; /* -1 if no override */
    int prefix;
    int aflag, dflag;
    target_ulong pc; /* pc = eip + cs_base */
    int is_jmp; /* 1 = means jump (stop translation), 2 means CPU
                   static state change (stop translation) */
    /* code output */
    uint8_t *gen_code_ptr;
    uint8_t *gen_code_start;
    
    /* current block context */
    target_ulong cs_base; /* base of CS segment */
    int pe;     /* protected mode */
    int code32; /* 32 bit code segment */
    int f_st;   /* currently unused */
    int vm86;   /* vm86 mode */
    int cpl;
    int iopl;
    int flags;
    struct TranslationBlock *tb;
} DisasContext;

#define CPU_FIELD_OFFSET(field) offsetof(CPUState, field)

#define CPU_SEG 0x64 /* fs override */

static inline void gb(DisasContext *s, uint32_t val)
{
    *s->gen_code_ptr++ = val;
}

static inline void gw(DisasContext *s, uint32_t val)
{
    *s->gen_code_ptr++ = val;
    *s->gen_code_ptr++ = val >> 8;
}

static inline void gl(DisasContext *s, uint32_t val)
{
    *s->gen_code_ptr++ = val;
    *s->gen_code_ptr++ = val >> 8;
    *s->gen_code_ptr++ = val >> 16;
    *s->gen_code_ptr++ = val >> 24;
}

static inline void gjmp(DisasContext *s, long val)
{
    gb(s, 0xe9); /* jmp */
    gl(s, val - (long)(s->gen_code_ptr + 4));
}

static inline void gen_movl_addr_im(DisasContext *s, 
                                    uint32_t addr, uint32_t val)
{
    gb(s, CPU_SEG); /* seg movl im, addr */
    gb(s, 0xc7); 
    gb(s, 0x05);
    gl(s, addr);
    gl(s, val);
}

static inline void gen_movw_addr_im(DisasContext *s, 
                                    uint32_t addr, uint32_t val)
{
    gb(s, CPU_SEG); /* seg movl im, addr */
    gb(s, 0x66); 
    gb(s, 0xc7); 
    gb(s, 0x05);
    gl(s, addr);
    gw(s, val);
}


static void gen_jmp(DisasContext *s, uint32_t target_eip)
{
    TranslationBlock *tb = s->tb;

    gb(s, 0xe9); /* jmp */
    tb->tb_jmp_offset[0] = s->gen_code_ptr - s->gen_code_start;
    gl(s, 0);

    tb->tb_next_offset[0] = s->gen_code_ptr - s->gen_code_start;
    gen_movl_addr_im(s, CPU_FIELD_OFFSET(eip), target_eip);
    gen_movl_addr_im(s, CPU_FIELD_OFFSET(tmp0), (uint32_t)tb);
    gjmp(s, (long)&exec_loop);

    s->is_jmp = 1;
}

static void gen_jcc(DisasContext *s, int op,
                    uint32_t target_eip, uint32_t next_eip)
{
    TranslationBlock *tb = s->tb;

    gb(s, 0x0f); /* jcc */
    gb(s, 0x80 + op);
    tb->tb_jmp_offset[0] = s->gen_code_ptr - s->gen_code_start;
    gl(s, 0);
    gb(s, 0xe9); /* jmp */
    tb->tb_jmp_offset[1] = s->gen_code_ptr - s->gen_code_start;
    gl(s, 0);
    
    tb->tb_next_offset[0] = s->gen_code_ptr - s->gen_code_start;
    gen_movl_addr_im(s, CPU_FIELD_OFFSET(eip), target_eip);
    gen_movl_addr_im(s, CPU_FIELD_OFFSET(tmp0), (uint32_t)tb);
    gjmp(s, (long)&exec_loop);

    tb->tb_next_offset[1] = s->gen_code_ptr - s->gen_code_start;
    gen_movl_addr_im(s, CPU_FIELD_OFFSET(eip), next_eip);
    gen_movl_addr_im(s, CPU_FIELD_OFFSET(tmp0), (uint32_t)tb | 1);
    gjmp(s, (long)&exec_loop);

    s->is_jmp = 1;
}

static void gen_eob(DisasContext *s)
{
    gen_movl_addr_im(s, CPU_FIELD_OFFSET(tmp0), 0);
    gjmp(s, (long)&exec_loop);

    s->is_jmp = 1;
}

static inline void gen_lea_modrm(DisasContext *s, int modrm)
{
    int havesib;
    int base, disp;
    int index;
    int scale;
    int mod, rm, code;

    mod = (modrm >> 6) & 3;
    rm = modrm & 7;

    if (s->aflag) {

        havesib = 0;
        base = rm;
        index = 0;
        scale = 0;
        
        if (base == 4) {
            havesib = 1;
            code = ldub_code(s->pc++);
            scale = (code >> 6) & 3;
            index = (code >> 3) & 7;
            base = code & 7;
        }

        switch (mod) {
        case 0:
            if (base == 5) {
                base = -1;
                disp = ldl_code(s->pc);
                s->pc += 4;
            } else {
                disp = 0;
            }
            break;
        case 1:
            disp = (int8_t)ldub_code(s->pc++);
            break;
        default:
        case 2:
            disp = ldl_code(s->pc);
            s->pc += 4;
            break;
        }
        
    } else {
        switch (mod) {
        case 0:
            if (rm == 6) {
                disp = lduw_code(s->pc);
                s->pc += 2;
            } else {
                disp = 0;
            }
            break;
        case 1:
            disp = (int8_t)ldub_code(s->pc++);
            break;
        default:
        case 2:
            disp = lduw_code(s->pc);
            s->pc += 2;
            break;
        }
    }
}

static inline void parse_modrm(DisasContext *s, int modrm)
{
    if ((modrm & 0xc0) != 0xc0)
        gen_lea_modrm(s, modrm);        
}

static inline uint32_t insn_get(DisasContext *s, int ot)
{
    uint32_t ret;

    switch(ot) {
    case OT_BYTE:
        ret = ldub_code(s->pc);
        s->pc++;
        break;
    case OT_WORD:
        ret = lduw_code(s->pc);
        s->pc += 2;
        break;
    default:
    case OT_LONG:
        ret = ldl_code(s->pc);
        s->pc += 4;
        break;
    }
    return ret;
}

/* convert one instruction. s->is_jmp is set if the translation must
   be stopped.  */
static int disas_insn(DisasContext *s)
{
    target_ulong pc_start, pc_tmp, pc_start_insn;
    int b, prefixes, aflag, dflag, next_eip, val;
    int ot;
    int modrm, mod, op, rm;

    pc_start = s->pc;
    prefixes = 0;
    aflag = s->code32;
    dflag = s->code32;
    s->override = -1;
 next_byte:
    b = ldub_code(s->pc);
    s->pc++;
    /* check prefixes */
    switch (b) {
    case 0xf3:
        prefixes |= PREFIX_REPZ;
        goto next_byte;
    case 0xf2:
        prefixes |= PREFIX_REPNZ;
        goto next_byte;
    case 0xf0:
        prefixes |= PREFIX_LOCK;
        goto next_byte;
    case 0x2e:
        s->override = R_CS;
        goto next_byte;
    case 0x36:
        s->override = R_SS;
        goto next_byte;
    case 0x3e:
        s->override = R_DS;
        goto next_byte;
    case 0x26:
        s->override = R_ES;
        goto next_byte;
    case 0x64:
        s->override = R_FS;
        goto next_byte;
    case 0x65:
        s->override = R_GS;
        goto next_byte;
    case 0x66:
        prefixes |= PREFIX_DATA;
        goto next_byte;
    case 0x67:
        prefixes |= PREFIX_ADR;
        goto next_byte;
    }

    if (prefixes & PREFIX_DATA)
        dflag ^= 1;
    if (prefixes & PREFIX_ADR)
        aflag ^= 1;

    s->prefix = prefixes;
    s->aflag = aflag;
    s->dflag = dflag;

    /* lock generation */
    if (prefixes & PREFIX_LOCK)
        goto unsupported_op;
    if (s->override == R_FS || s->override == R_GS || s->override == R_CS)
        goto unsupported_op;

    pc_start_insn = s->pc - 1;
    /* now check op code */
 reswitch:
    switch(b) {
    case 0x0f:
        /**************************/
        /* extended op code */
        b = ldub_code(s->pc++) | 0x100;
        goto reswitch;
        
        /**************************/
        /* arith & logic */
    case 0x00 ... 0x05:
    case 0x08 ... 0x0d:
    case 0x10 ... 0x15:
    case 0x18 ... 0x1d:
    case 0x20 ... 0x25:
    case 0x28 ... 0x2d:
    case 0x30 ... 0x35:
    case 0x38 ... 0x3d:
        {
            int f;
            f = (b >> 1) & 3;

            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
                ot = dflag ? OT_LONG : OT_WORD;
            
            switch(f) {
            case 0: /* OP Ev, Gv */
                modrm = ldub_code(s->pc++);
                parse_modrm(s, modrm);
                break;
            case 1: /* OP Gv, Ev */
                modrm = ldub_code(s->pc++);
                parse_modrm(s, modrm);
                break;
            case 2: /* OP A, Iv */
                insn_get(s, ot);
                break;
            }
        }
        break;

    case 0x80: /* GRP1 */
    case 0x81:
    case 0x82:
    case 0x83:
        {
            if ((b & 1) == 0)
                ot = OT_BYTE;
            else
                ot = dflag ? OT_LONG : OT_WORD;
            
            modrm = ldub_code(s->pc++);
            parse_modrm(s, modrm);

            switch(b) {
            default:
            case 0x80:
            case 0x81:
            case 0x82:
                insn_get(s, ot);
                break;
            case 0x83:
                insn_get(s, OT_BYTE);
                break;
            }
        }
        break;

        /**************************/
        /* inc, dec, and other misc arith */
    case 0x40 ... 0x47: /* inc Gv */
        break;
    case 0x48 ... 0x4f: /* dec Gv */
        break;
    case 0xf6: /* GRP3 */
    case 0xf7:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;

        modrm = ldub_code(s->pc++);
        op = (modrm >> 3) & 7;
        parse_modrm(s, modrm);

        switch(op) {
        case 0: /* test */
            insn_get(s, ot);
            break;
        case 2: /* not */
            break;
        case 3: /* neg */
            break;
        case 4: /* mul */
            break;
        case 5: /* imul */
            break;
        case 6: /* div */
            break;
        case 7: /* idiv */
            break;
        default:
            goto illegal_op;
        }
        break;

    case 0xfe: /* GRP4 */
    case 0xff: /* GRP5 */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;

        modrm = ldub_code(s->pc++);
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        if (op >= 2 && b == 0xfe) {
            goto illegal_op;
        }
        pc_tmp = s->pc;
        parse_modrm(s, modrm);

        switch(op) {
        case 0: /* inc Ev */
            break;
        case 1: /* dec Ev */
            break;
        case 2: /* call Ev */
            /* XXX: optimize and handle MEM exceptions specifically
               fs movl %eax, regs[0] 
               movl Ev, %eax 
               pushl next_eip
               fs movl %eax, eip
            */
            goto unsupported_op;
        case 3: /* lcall Ev */
            goto unsupported_op;
        case 4: /* jmp Ev */
            /* XXX: optimize and handle MEM exceptions specifically
               fs movl %eax, regs[0] 
               movl Ev, %eax 
               fs movl %eax, eip
            */
            goto unsupported_op;
        case 5: /* ljmp Ev */
            goto unsupported_op;
        case 6: /* push Ev */
            break;
        default:
            goto illegal_op;
        }
        break;
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
        insn_get(s, ot);
        break;
        
    case 0x98: /* CWDE/CBW */
        break;
    case 0x99: /* CDQ/CWD */
        break;
    case 0x1af: /* imul Gv, Ev */
    case 0x69: /* imul Gv, Ev, I */
    case 0x6b:
        ot = dflag ? OT_LONG : OT_WORD;
        modrm = ldub_code(s->pc++);
        parse_modrm(s, modrm);
        if (b == 0x69) {
            insn_get(s, ot);
        } else if (b == 0x6b) {
            insn_get(s, OT_BYTE);
        } else {
        }
        break;

    case 0x84: /* test Ev, Gv */
    case 0x85: 
        
    case 0x1c0:
    case 0x1c1: /* xadd Ev, Gv */

    case 0x1b0:
    case 0x1b1: /* cmpxchg Ev, Gv */

    case 0x8f: /* pop Ev */

    case 0x88:
    case 0x89: /* mov Gv, Ev */

    case 0x8a:
    case 0x8b: /* mov Ev, Gv */

    case 0x1b6: /* movzbS Gv, Eb */
    case 0x1b7: /* movzwS Gv, Eb */
    case 0x1be: /* movsbS Gv, Eb */
    case 0x1bf: /* movswS Gv, Eb */

    case 0x86:
    case 0x87: /* xchg Ev, Gv */

    case 0xd0:
    case 0xd1: /* shift Ev,1 */

    case 0xd2:
    case 0xd3: /* shift Ev,cl */

    case 0x1a5: /* shld cl */
    case 0x1ad: /* shrd cl */

    case 0x190 ... 0x19f: /* setcc Gv */

    /* XXX: emulate cmov if not available ? */
    case 0x140 ... 0x14f: /* cmov Gv, Ev */

    case 0x1a3: /* bt Gv, Ev */
    case 0x1ab: /* bts */
    case 0x1b3: /* btr */
    case 0x1bb: /* btc */

    case 0x1bc: /* bsf */
    case 0x1bd: /* bsr */

        modrm = ldub_code(s->pc++);
        parse_modrm(s, modrm);
        break;

    case 0x1c7: /* cmpxchg8b */
        modrm = ldub_code(s->pc++);
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        parse_modrm(s, modrm);
        break;
        
        /**************************/
        /* push/pop */
    case 0x50 ... 0x57: /* push */
    case 0x58 ... 0x5f: /* pop */
    case 0x60: /* pusha */
    case 0x61: /* popa */
        break;

    case 0x68: /* push Iv */
    case 0x6a:
        ot = dflag ? OT_LONG : OT_WORD;
        if (b == 0x68)
            insn_get(s, ot);
        else
            insn_get(s, OT_BYTE);
        break;
    case 0xc8: /* enter */
        lduw_code(s->pc);
        s->pc += 2;
        ldub_code(s->pc++);
        break;
    case 0xc9: /* leave */
        break;

    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
        /* XXX: optimize:
         push segs[n].selector
        */
        goto unsupported_op;
    case 0x1a0: /* push fs */
    case 0x1a8: /* push gs */
        goto unsupported_op;
    case 0x07: /* pop es */
    case 0x17: /* pop ss */
    case 0x1f: /* pop ds */
        goto unsupported_op;
    case 0x1a1: /* pop fs */
    case 0x1a9: /* pop gs */
        goto unsupported_op;
    case 0x8e: /* mov seg, Gv */
        /* XXX: optimize:
           fs movl r, regs[]
           movl segs[].selector, r
           mov r, Gv
           fs movl regs[], r
        */
        goto unsupported_op;
    case 0x8c: /* mov Gv, seg */
        goto unsupported_op;
    case 0xc4: /* les Gv */
        op = R_ES;
        goto do_lxx;
    case 0xc5: /* lds Gv */
        op = R_DS;
        goto do_lxx;
    case 0x1b2: /* lss Gv */
        op = R_SS;
        goto do_lxx;
    case 0x1b4: /* lfs Gv */
        op = R_FS;
        goto do_lxx;
    case 0x1b5: /* lgs Gv */
        op = R_GS;
    do_lxx:
        goto unsupported_op;
        /************************/
        /* floats */
    case 0xd8 ... 0xdf: 
#if 1
        /* currently not stable enough */
        goto unsupported_op;
#else
        if (s->flags & (HF_EM_MASK | HF_TS_MASK))
            goto unsupported_op;
#endif
#if 0
        /* for testing FPU context switch */
        {
            static int count;
            count = (count + 1) % 3;
            if (count != 0)
                goto unsupported_op;
        }
#endif
        modrm = ldub_code(s->pc++);
        mod = (modrm >> 6) & 3;
        rm = modrm & 7;
        op = ((b & 7) << 3) | ((modrm >> 3) & 7);
        if (mod != 3) {
            /* memory op */
            parse_modrm(s, modrm);
            switch(op) {
            case 0x00 ... 0x07: /* fxxxs */
            case 0x10 ... 0x17: /* fixxxl */
            case 0x20 ... 0x27: /* fxxxl */
            case 0x30 ... 0x37: /* fixxx */
                break;
            case 0x08: /* flds */
            case 0x0a: /* fsts */
            case 0x0b: /* fstps */
            case 0x18: /* fildl */
            case 0x1a: /* fistl */
            case 0x1b: /* fistpl */
            case 0x28: /* fldl */
            case 0x2a: /* fstl */
            case 0x2b: /* fstpl */
            case 0x38: /* filds */
            case 0x3a: /* fists */
            case 0x3b: /* fistps */
            case 0x0c: /* fldenv mem */
            case 0x0d: /* fldcw mem */
            case 0x0e: /* fnstenv mem */
            case 0x0f: /* fnstcw mem */
            case 0x1d: /* fldt mem */
            case 0x1f: /* fstpt mem */
            case 0x2c: /* frstor mem */
            case 0x2e: /* fnsave mem */
            case 0x2f: /* fnstsw mem */
            case 0x3c: /* fbld */
            case 0x3e: /* fbstp */
            case 0x3d: /* fildll */
            case 0x3f: /* fistpll */
                break;
            default:
                goto illegal_op;
            }
        } else {
            /* register float ops */
            switch(op) {
            case 0x08: /* fld sti */
            case 0x09: /* fxchg sti */
                break;
            case 0x0a: /* grp d9/2 */
                switch(rm) {
                case 0: /* fnop */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0c: /* grp d9/4 */
                switch(rm) {
                case 0: /* fchs */
                case 1: /* fabs */
                case 4: /* ftst */
                case 5: /* fxam */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0d: /* grp d9/5 */
                switch(rm) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0e: /* grp d9/6 */
                break;
            case 0x0f: /* grp d9/7 */
                break;
            case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
            case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
            case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
                break;
            case 0x02: /* fcom */
                break;
            case 0x03: /* fcomp */
                break;
            case 0x15: /* da/5 */
                switch(rm) {
                case 1: /* fucompp */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x1c:
                switch(rm) {
                case 0: /* feni (287 only, just do nop here) */
                case 1: /* fdisi (287 only, just do nop here) */
                    goto unsupported_op;
                case 2: /* fclex */
                case 3: /* fninit */
                case 4: /* fsetpm (287 only, just do nop here) */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x1d: /* fucomi */
                break;
            case 0x1e: /* fcomi */
                break;
            case 0x28: /* ffree sti */
                break;
            case 0x2a: /* fst sti */
                break;
            case 0x2b: /* fstp sti */
                break;
            case 0x2c: /* fucom st(i) */
                break;
            case 0x2d: /* fucomp st(i) */
                break;
            case 0x33: /* de/3 */
                switch(rm) {
                case 1: /* fcompp */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x3c: /* df/4 */
                switch(rm) {
                case 0:
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x3d: /* fucomip */
                break;
            case 0x3e: /* fcomip */
                break;
            case 0x10 ... 0x13: /* fcmovxx */
            case 0x18 ... 0x1b:
                break;
            default:
                goto illegal_op;
            }
        }
        s->tb->cflags |= CF_TB_FP_USED;
        break;

        /**************************/
        /* mov */
    case 0xc6:
    case 0xc7: /* mov Ev, Iv */
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
        modrm = ldub_code(s->pc++);
        parse_modrm(s, modrm);
        insn_get(s, ot);
        break;

    case 0x8d: /* lea */
        ot = dflag ? OT_LONG : OT_WORD;
        modrm = ldub_code(s->pc++);
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        parse_modrm(s, modrm);
        break;
        
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        if ((b & 1) == 0)
            ot = OT_BYTE;
        else
            ot = dflag ? OT_LONG : OT_WORD;
        if (s->aflag)
            insn_get(s, OT_LONG);
        else
            insn_get(s, OT_WORD);
        break;
    case 0xd7: /* xlat */
        break;
    case 0xb0 ... 0xb7: /* mov R, Ib */
        insn_get(s, OT_BYTE);
        break;
    case 0xb8 ... 0xbf: /* mov R, Iv */
        ot = dflag ? OT_LONG : OT_WORD;
        insn_get(s, ot);
        break;

    case 0x91 ... 0x97: /* xchg R, EAX */
        break;

        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1: /* shift Ev,imm */

    case 0x1a4: /* shld imm */
    case 0x1ac: /* shrd imm */
        modrm = ldub_code(s->pc++);
        parse_modrm(s, modrm);
        ldub_code(s->pc++);
        break;
        
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
        break;
        
    case 0xaa: /* stosS */
    case 0xab:
        break;

    case 0xac: /* lodsS */
    case 0xad:
        break;

    case 0xae: /* scasS */
    case 0xaf:
        break;

    case 0xa6: /* cmpsS */
    case 0xa7:
        break;

    case 0x6c: /* insS */
    case 0x6d:
        goto unsupported_op;

    case 0x6e: /* outsS */
    case 0x6f:
        goto unsupported_op;

        /************************/
        /* port I/O */
    case 0xe4:
    case 0xe5:
        goto unsupported_op;

    case 0xe6:
    case 0xe7:
        goto unsupported_op;

    case 0xec:
    case 0xed:
        goto unsupported_op;

    case 0xee:
    case 0xef:
        goto unsupported_op;

        /************************/
        /* control */
#if 0
    case 0xc2: /* ret im */
        val = ldsw_code(s->pc);
        s->pc += 2;
        gen_pop_T0(s);
        gen_stack_update(s, val + (2 << s->dflag));
        if (s->dflag == 0)
            gen_op_andl_T0_ffff();
        gen_op_jmp_T0();
        gen_eob(s);
        break;
#endif

    case 0xc3: /* ret */
        gb(s, CPU_SEG);
        if (!s->dflag)  
            gb(s, 0x66); /* d16 */
        gb(s, 0x8f); /* pop addr */
        gb(s, 0x05);
        gl(s, CPU_FIELD_OFFSET(eip));
        if (!s->dflag) {
            /* reset high bits of EIP */
            gen_movw_addr_im(s, CPU_FIELD_OFFSET(eip) + 2, 0);
        }
        gen_eob(s);
        goto no_copy;
    case 0xca: /* lret im */
    case 0xcb: /* lret */
    case 0xcf: /* iret */
    case 0x9a: /* lcall im */
    case 0xea: /* ljmp im */
        goto unsupported_op;

    case 0xe8: /* call im */
        ot = dflag ? OT_LONG : OT_WORD;
        val = insn_get(s, ot);
        next_eip = s->pc - s->cs_base;
        val += next_eip;
        if (s->dflag) {
            gb(s, 0x68); /* pushl imm */
            gl(s, next_eip);
        } else {
            gb(s, 0x66); /* pushw imm */
            gb(s, 0x68);
            gw(s, next_eip);
            val &= 0xffff;
        }
        gen_jmp(s, val);
        goto no_copy;
    case 0xe9: /* jmp */
        ot = dflag ? OT_LONG : OT_WORD;
        val = insn_get(s, ot);
        val += s->pc - s->cs_base;
        if (s->dflag == 0)
            val = val & 0xffff;
        gen_jmp(s, val);
        goto no_copy;
    case 0xeb: /* jmp Jb */
        val = (int8_t)insn_get(s, OT_BYTE);
        val += s->pc - s->cs_base;
        if (s->dflag == 0)
            val = val & 0xffff;
        gen_jmp(s, val);
        goto no_copy;
    case 0x70 ... 0x7f: /* jcc Jb */
        val = (int8_t)insn_get(s, OT_BYTE);
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
        if (dflag) {
            val = insn_get(s, OT_LONG);
        } else {
            val = (int16_t)insn_get(s, OT_WORD); 
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
        val += next_eip;
        if (s->dflag == 0)
            val &= 0xffff;
        gen_jcc(s, b & 0xf, val, next_eip);
        goto no_copy;

        /************************/
        /* flags */
    case 0x9c: /* pushf */
        /* XXX: put specific code ? */
        goto unsupported_op;
    case 0x9d: /* popf */
        goto unsupported_op;

    case 0x9e: /* sahf */
    case 0x9f: /* lahf */
    case 0xf5: /* cmc */
    case 0xf8: /* clc */
    case 0xf9: /* stc */
    case 0xfc: /* cld */
    case 0xfd: /* std */
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
        ot = dflag ? OT_LONG : OT_WORD;
        modrm = ldub_code(s->pc++);
        op = (modrm >> 3) & 7;
        parse_modrm(s, modrm);
        /* load shift */
        ldub_code(s->pc++);
        if (op < 4)
            goto illegal_op;
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
        break;
    case 0x2f: /* das */
        break;
    case 0x37: /* aaa */
        break;
    case 0x3f: /* aas */
        break;
    case 0xd4: /* aam */
        ldub_code(s->pc++);
        break;
    case 0xd5: /* aad */
        ldub_code(s->pc++);
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
        break;
    case 0x9b: /* fwait */
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) == 
            (HF_MP_MASK | HF_TS_MASK)) {
            goto unsupported_op;
        }
        break;
    case 0xcc: /* int3 */
        goto unsupported_op;
    case 0xcd: /* int N */
        goto unsupported_op;
    case 0xce: /* into */
        goto unsupported_op;
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
        goto unsupported_op;
    case 0xfa: /* cli */
        goto unsupported_op;
    case 0xfb: /* sti */
        goto unsupported_op;
    case 0x62: /* bound */
        modrm = ldub_code(s->pc++);
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        parse_modrm(s, modrm);
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
        break;
    case 0xd6: /* salc */
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
        goto unsupported_op;

    case 0x130: /* wrmsr */
    case 0x132: /* rdmsr */
        goto unsupported_op;
    case 0x131: /* rdtsc */
        goto unsupported_op;
    case 0x1a2: /* cpuid */
        goto unsupported_op;
    case 0xf4: /* hlt */
        goto unsupported_op;
    case 0x100:
        goto unsupported_op;
    case 0x101:
        goto unsupported_op;
    case 0x108: /* invd */
    case 0x109: /* wbinvd */
        goto unsupported_op;
    case 0x63: /* arpl */
        goto unsupported_op;
    case 0x102: /* lar */
    case 0x103: /* lsl */
        goto unsupported_op;
    case 0x118:
        goto unsupported_op;
    case 0x120: /* mov reg, crN */
    case 0x122: /* mov crN, reg */
        goto unsupported_op;
    case 0x121: /* mov reg, drN */
    case 0x123: /* mov drN, reg */
        goto unsupported_op;
    case 0x106: /* clts */
        goto unsupported_op;
    default:
        goto illegal_op;
    }

    /* just copy the code */

    /* no override yet */
    if (!s->dflag)
        gb(s, 0x66);
    if (!s->aflag)
        gb(s, 0x67);
    if (prefixes & PREFIX_REPZ)
        gb(s, 0xf3);
    else if (prefixes & PREFIX_REPNZ)
        gb(s, 0xf2);
    {
        int len, i;
        len = s->pc - pc_start_insn;
        for(i = 0; i < len; i++) {
            *s->gen_code_ptr++ = ldub_code(pc_start_insn + i);
        }
    }
 no_copy:
    return 0;
 illegal_op:
 unsupported_op:
    /* fall back to slower code gen necessary */
    s->pc = pc_start;
    return -1;
}

#define GEN_CODE_MAX_SIZE      8192
#define GEN_CODE_MAX_INSN_SIZE 512

static inline int gen_intermediate_code_internal(CPUState *env,
                                                 TranslationBlock *tb, 
                                                 uint8_t *gen_code_ptr,
                                                 int *gen_code_size_ptr,
                                                 int search_pc,
                                                 uint8_t *tc_ptr)
{
    DisasContext dc1, *dc = &dc1;
    target_ulong pc_insn, pc_start, cs_base;
    uint8_t *gen_code_end;
    int flags, ret;

    if (env->nb_breakpoints > 0 ||
        env->singlestep_enabled)
        return -1;
    flags = tb->flags;
    if (flags & (HF_TF_MASK | HF_ADDSEG_MASK | 
                 HF_SOFTMMU_MASK | HF_INHIBIT_IRQ_MASK))
        return -1;
    if (!(flags & HF_SS32_MASK))
        return -1;
    if (tb->cflags & CF_SINGLE_INSN)
        return -1;
    gen_code_end = gen_code_ptr + 
        GEN_CODE_MAX_SIZE - GEN_CODE_MAX_INSN_SIZE;
    dc->gen_code_ptr = gen_code_ptr;
    dc->gen_code_start = gen_code_ptr;

    /* generate intermediate code */
    pc_start = tb->pc;
    cs_base = tb->cs_base;
    dc->pc = pc_start;
    dc->cs_base = cs_base;
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
    dc->code32 = (flags >> HF_CS32_SHIFT) & 1;
    dc->f_st = 0;
    dc->vm86 = (flags >> VM_SHIFT) & 1;
    dc->cpl = (flags >> HF_CPL_SHIFT) & 3;
    dc->iopl = (flags >> IOPL_SHIFT) & 3;
    dc->tb = tb;
    dc->flags = flags;

    dc->is_jmp = 0;

    for(;;) {
        pc_insn = dc->pc;
        ret = disas_insn(dc);
        if (ret < 0) {
            /* unsupported insn */
            if (dc->pc == pc_start) {
                /* if first instruction, signal that no copying was done */
                return -1;
            } else {
                gen_jmp(dc, dc->pc - dc->cs_base);
                dc->is_jmp = 1;
            }
        }
        if (search_pc) {
            /* search pc mode */
            if (tc_ptr < dc->gen_code_ptr) {
                env->eip = pc_insn - cs_base;
                return 0;
            }
        }
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if too long translation, stop generation */
        if (dc->gen_code_ptr >= gen_code_end ||
            (dc->pc - pc_start) >= (TARGET_PAGE_SIZE - 32)) {
            gen_jmp(dc, dc->pc - dc->cs_base);
            break;
        }
    }
    
#ifdef DEBUG_DISAS
    if (loglevel & CPU_LOG_TB_IN_ASM) {
        fprintf(logfile, "----------------\n");
        fprintf(logfile, "IN: COPY: %s fpu=%d\n", 
                lookup_symbol(pc_start),
                tb->cflags & CF_TB_FP_USED ? 1 : 0);
	target_disas(logfile, pc_start, dc->pc - pc_start, !dc->code32);
        fprintf(logfile, "\n");
    }
#endif

    if (!search_pc) {
        *gen_code_size_ptr = dc->gen_code_ptr - dc->gen_code_start;
        tb->size = dc->pc - pc_start;
        tb->cflags |= CF_CODE_COPY;
        return 0;
    } else {
        return -1;
    }
}

/* generate code by just copying data. Return -1 if cannot generate
   any code. Return 0 if code was generated */
int cpu_gen_code_copy(CPUState *env, TranslationBlock *tb,
                      int max_code_size, int *gen_code_size_ptr)
{
    /* generate machine code */
    tb->tb_next_offset[0] = 0xffff;
    tb->tb_next_offset[1] = 0xffff;
#ifdef USE_DIRECT_JUMP
    /* the following two entries are optional (only used for string ops) */
    tb->tb_jmp_offset[2] = 0xffff;
    tb->tb_jmp_offset[3] = 0xffff;
#endif
    return gen_intermediate_code_internal(env, tb, 
                                          tb->tc_ptr, gen_code_size_ptr,
                                          0, NULL);
}

static uint8_t dummy_gen_code_buf[GEN_CODE_MAX_SIZE];

int cpu_restore_state_copy(TranslationBlock *tb, 
                           CPUState *env, unsigned long searched_pc,
                           void *puc)
{
    struct ucontext *uc = puc;
    int ret, eflags;

    /* find opc index corresponding to search_pc */
    if (searched_pc < (unsigned long)tb->tc_ptr)
        return -1;
    searched_pc = searched_pc - (long)tb->tc_ptr + (long)dummy_gen_code_buf;
    ret = gen_intermediate_code_internal(env, tb, 
                                         dummy_gen_code_buf, NULL,
                                         1, (uint8_t *)searched_pc);
    if (ret < 0)
        return ret;
    /* restore all the CPU state from the CPU context from the
       signal. The FPU context stays in the host CPU. */
    
    env->regs[R_EAX] = uc->uc_mcontext.gregs[REG_EAX];
    env->regs[R_ECX] = uc->uc_mcontext.gregs[REG_ECX];
    env->regs[R_EDX] = uc->uc_mcontext.gregs[REG_EDX];
    env->regs[R_EBX] = uc->uc_mcontext.gregs[REG_EBX];
    env->regs[R_ESP] = uc->uc_mcontext.gregs[REG_ESP];
    env->regs[R_EBP] = uc->uc_mcontext.gregs[REG_EBP];
    env->regs[R_ESI] = uc->uc_mcontext.gregs[REG_ESI];
    env->regs[R_EDI] = uc->uc_mcontext.gregs[REG_EDI];
    eflags = uc->uc_mcontext.gregs[REG_EFL];
    env->df = 1 - (2 * ((eflags >> 10) & 1));
    env->cc_src = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
    env->cc_op = CC_OP_EFLAGS;
    return 0;
}

#endif /* USE_CODE_COPY */