Statistics
| Branch: | Revision:

root / target-i386 / helper.c @ ca954f6d

History | View | Annotate | Download (91.6 kB)

1
/*
2
 *  i386 helpers
3
 * 
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
#include "exec.h"
21

    
22
//#define DEBUG_PCALL
23

    
24
#if 0
25
#define raise_exception_err(a, b)\
26
do {\
27
    fprintf(logfile, "raise_exception line=%d\n", __LINE__);\
28
    (raise_exception_err)(a, b);\
29
} while (0)
30
#endif
31

    
32
const uint8_t parity_table[256] = {
33
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
34
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
35
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
36
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
37
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
38
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
39
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
40
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
41
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
42
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
43
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
44
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
45
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
46
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
47
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
48
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
49
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
50
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
51
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
52
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
53
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
54
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
55
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
56
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
57
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
58
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
59
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
60
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
61
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
62
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
63
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
64
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
65
};
66

    
67
/* modulo 17 table */
68
const uint8_t rclw_table[32] = {
69
    0, 1, 2, 3, 4, 5, 6, 7, 
70
    8, 9,10,11,12,13,14,15,
71
   16, 0, 1, 2, 3, 4, 5, 6,
72
    7, 8, 9,10,11,12,13,14,
73
};
74

    
75
/* modulo 9 table */
76
const uint8_t rclb_table[32] = {
77
    0, 1, 2, 3, 4, 5, 6, 7, 
78
    8, 0, 1, 2, 3, 4, 5, 6,
79
    7, 8, 0, 1, 2, 3, 4, 5, 
80
    6, 7, 8, 0, 1, 2, 3, 4,
81
};
82

    
83
const CPU86_LDouble f15rk[7] =
84
{
85
    0.00000000000000000000L,
86
    1.00000000000000000000L,
87
    3.14159265358979323851L,  /*pi*/
88
    0.30102999566398119523L,  /*lg2*/
89
    0.69314718055994530943L,  /*ln2*/
90
    1.44269504088896340739L,  /*l2e*/
91
    3.32192809488736234781L,  /*l2t*/
92
};
93
    
94
/* thread support */
95

    
96
spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
97

    
98
void cpu_lock(void)
99
{
100
    spin_lock(&global_cpu_lock);
101
}
102

    
103
void cpu_unlock(void)
104
{
105
    spin_unlock(&global_cpu_lock);
106
}
107

    
108
void cpu_loop_exit(void)
109
{
110
    /* NOTE: the register at this point must be saved by hand because
111
       longjmp restore them */
112
    regs_to_env();
113
    longjmp(env->jmp_env, 1);
114
}
115

    
116
/* return non zero if error */
117
static inline int load_segment(uint32_t *e1_ptr, uint32_t *e2_ptr,
118
                               int selector)
119
{
120
    SegmentCache *dt;
121
    int index;
122
    target_ulong ptr;
123

    
124
    if (selector & 0x4)
125
        dt = &env->ldt;
126
    else
127
        dt = &env->gdt;
128
    index = selector & ~7;
129
    if ((index + 7) > dt->limit)
130
        return -1;
131
    ptr = dt->base + index;
132
    *e1_ptr = ldl_kernel(ptr);
133
    *e2_ptr = ldl_kernel(ptr + 4);
134
    return 0;
135
}
136
                                     
137
static inline unsigned int get_seg_limit(uint32_t e1, uint32_t e2)
138
{
139
    unsigned int limit;
140
    limit = (e1 & 0xffff) | (e2 & 0x000f0000);
141
    if (e2 & DESC_G_MASK)
142
        limit = (limit << 12) | 0xfff;
143
    return limit;
144
}
145

    
146
static inline uint32_t get_seg_base(uint32_t e1, uint32_t e2)
147
{
148
    return ((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
149
}
150

    
151
static inline void load_seg_cache_raw_dt(SegmentCache *sc, uint32_t e1, uint32_t e2)
152
{
153
    sc->base = get_seg_base(e1, e2);
154
    sc->limit = get_seg_limit(e1, e2);
155
    sc->flags = e2;
156
}
157

    
158
/* init the segment cache in vm86 mode. */
159
static inline void load_seg_vm(int seg, int selector)
160
{
161
    selector &= 0xffff;
162
    cpu_x86_load_seg_cache(env, seg, selector, 
163
                           (selector << 4), 0xffff, 0);
164
}
165

    
166
static inline void get_ss_esp_from_tss(uint32_t *ss_ptr, 
167
                                       uint32_t *esp_ptr, int dpl)
168
{
169
    int type, index, shift;
170
    
171
#if 0
172
    {
173
        int i;
174
        printf("TR: base=%p limit=%x\n", env->tr.base, env->tr.limit);
175
        for(i=0;i<env->tr.limit;i++) {
176
            printf("%02x ", env->tr.base[i]);
177
            if ((i & 7) == 7) printf("\n");
178
        }
179
        printf("\n");
180
    }
181
#endif
182

    
183
    if (!(env->tr.flags & DESC_P_MASK))
184
        cpu_abort(env, "invalid tss");
185
    type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
186
    if ((type & 7) != 1)
187
        cpu_abort(env, "invalid tss type");
188
    shift = type >> 3;
189
    index = (dpl * 4 + 2) << shift;
190
    if (index + (4 << shift) - 1 > env->tr.limit)
191
        raise_exception_err(EXCP0A_TSS, env->tr.selector & 0xfffc);
192
    if (shift == 0) {
193
        *esp_ptr = lduw_kernel(env->tr.base + index);
194
        *ss_ptr = lduw_kernel(env->tr.base + index + 2);
195
    } else {
196
        *esp_ptr = ldl_kernel(env->tr.base + index);
197
        *ss_ptr = lduw_kernel(env->tr.base + index + 4);
198
    }
199
}
200

    
201
/* XXX: merge with load_seg() */
202
static void tss_load_seg(int seg_reg, int selector)
203
{
204
    uint32_t e1, e2;
205
    int rpl, dpl, cpl;
206

    
207
    if ((selector & 0xfffc) != 0) {
208
        if (load_segment(&e1, &e2, selector) != 0)
209
            raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
210
        if (!(e2 & DESC_S_MASK))
211
            raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
212
        rpl = selector & 3;
213
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
214
        cpl = env->hflags & HF_CPL_MASK;
215
        if (seg_reg == R_CS) {
216
            if (!(e2 & DESC_CS_MASK))
217
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
218
            if (dpl != rpl)
219
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
220
            if ((e2 & DESC_C_MASK) && dpl > rpl)
221
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
222
                
223
        } else if (seg_reg == R_SS) {
224
            /* SS must be writable data */
225
            if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
226
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
227
            if (dpl != cpl || dpl != rpl)
228
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
229
        } else {
230
            /* not readable code */
231
            if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK))
232
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
233
            /* if data or non conforming code, checks the rights */
234
            if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) {
235
                if (dpl < cpl || dpl < rpl)
236
                    raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
237
            }
238
        }
239
        if (!(e2 & DESC_P_MASK))
240
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
241
        cpu_x86_load_seg_cache(env, seg_reg, selector, 
242
                       get_seg_base(e1, e2),
243
                       get_seg_limit(e1, e2),
244
                       e2);
245
    } else {
246
        if (seg_reg == R_SS || seg_reg == R_CS) 
247
            raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
248
    }
249
}
250

    
251
#define SWITCH_TSS_JMP  0
252
#define SWITCH_TSS_IRET 1
253
#define SWITCH_TSS_CALL 2
254

    
255
/* XXX: restore CPU state in registers (PowerPC case) */
256
static void switch_tss(int tss_selector, 
257
                       uint32_t e1, uint32_t e2, int source,
258
                       uint32_t next_eip)
259
{
260
    int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, v1, v2, i;
261
    target_ulong tss_base;
262
    uint32_t new_regs[8], new_segs[6];
263
    uint32_t new_eflags, new_eip, new_cr3, new_ldt, new_trap;
264
    uint32_t old_eflags, eflags_mask;
265
    SegmentCache *dt;
266
    int index;
267
    target_ulong ptr;
268

    
269
    type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
270
#ifdef DEBUG_PCALL
271
    if (loglevel & CPU_LOG_PCALL)
272
        fprintf(logfile, "switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector, type, source);
273
#endif
274

    
275
    /* if task gate, we read the TSS segment and we load it */
276
    if (type == 5) {
277
        if (!(e2 & DESC_P_MASK))
278
            raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
279
        tss_selector = e1 >> 16;
280
        if (tss_selector & 4)
281
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
282
        if (load_segment(&e1, &e2, tss_selector) != 0)
283
            raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
284
        if (e2 & DESC_S_MASK)
285
            raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
286
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
287
        if ((type & 7) != 1)
288
            raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
289
    }
290

    
291
    if (!(e2 & DESC_P_MASK))
292
        raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
293

    
294
    if (type & 8)
295
        tss_limit_max = 103;
296
    else
297
        tss_limit_max = 43;
298
    tss_limit = get_seg_limit(e1, e2);
299
    tss_base = get_seg_base(e1, e2);
300
    if ((tss_selector & 4) != 0 || 
301
        tss_limit < tss_limit_max)
302
        raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
303
    old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
304
    if (old_type & 8)
305
        old_tss_limit_max = 103;
306
    else
307
        old_tss_limit_max = 43;
308

    
309
    /* read all the registers from the new TSS */
310
    if (type & 8) {
311
        /* 32 bit */
312
        new_cr3 = ldl_kernel(tss_base + 0x1c);
313
        new_eip = ldl_kernel(tss_base + 0x20);
314
        new_eflags = ldl_kernel(tss_base + 0x24);
315
        for(i = 0; i < 8; i++)
316
            new_regs[i] = ldl_kernel(tss_base + (0x28 + i * 4));
317
        for(i = 0; i < 6; i++)
318
            new_segs[i] = lduw_kernel(tss_base + (0x48 + i * 4));
319
        new_ldt = lduw_kernel(tss_base + 0x60);
320
        new_trap = ldl_kernel(tss_base + 0x64);
321
    } else {
322
        /* 16 bit */
323
        new_cr3 = 0;
324
        new_eip = lduw_kernel(tss_base + 0x0e);
325
        new_eflags = lduw_kernel(tss_base + 0x10);
326
        for(i = 0; i < 8; i++)
327
            new_regs[i] = lduw_kernel(tss_base + (0x12 + i * 2)) | 0xffff0000;
328
        for(i = 0; i < 4; i++)
329
            new_segs[i] = lduw_kernel(tss_base + (0x22 + i * 4));
330
        new_ldt = lduw_kernel(tss_base + 0x2a);
331
        new_segs[R_FS] = 0;
332
        new_segs[R_GS] = 0;
333
        new_trap = 0;
334
    }
335
    
336
    /* NOTE: we must avoid memory exceptions during the task switch,
337
       so we make dummy accesses before */
338
    /* XXX: it can still fail in some cases, so a bigger hack is
339
       necessary to valid the TLB after having done the accesses */
340

    
341
    v1 = ldub_kernel(env->tr.base);
342
    v2 = ldub(env->tr.base + old_tss_limit_max);
343
    stb_kernel(env->tr.base, v1);
344
    stb_kernel(env->tr.base + old_tss_limit_max, v2);
345
    
346
    /* clear busy bit (it is restartable) */
347
    if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_IRET) {
348
        target_ulong ptr;
349
        uint32_t e2;
350
        ptr = env->gdt.base + (env->tr.selector & ~7);
351
        e2 = ldl_kernel(ptr + 4);
352
        e2 &= ~DESC_TSS_BUSY_MASK;
353
        stl_kernel(ptr + 4, e2);
354
    }
355
    old_eflags = compute_eflags();
356
    if (source == SWITCH_TSS_IRET)
357
        old_eflags &= ~NT_MASK;
358
    
359
    /* save the current state in the old TSS */
360
    if (type & 8) {
361
        /* 32 bit */
362
        stl_kernel(env->tr.base + 0x20, next_eip);
363
        stl_kernel(env->tr.base + 0x24, old_eflags);
364
        stl_kernel(env->tr.base + (0x28 + 0 * 4), EAX);
365
        stl_kernel(env->tr.base + (0x28 + 1 * 4), ECX);
366
        stl_kernel(env->tr.base + (0x28 + 2 * 4), EDX);
367
        stl_kernel(env->tr.base + (0x28 + 3 * 4), EBX);
368
        stl_kernel(env->tr.base + (0x28 + 4 * 4), ESP);
369
        stl_kernel(env->tr.base + (0x28 + 5 * 4), EBP);
370
        stl_kernel(env->tr.base + (0x28 + 6 * 4), ESI);
371
        stl_kernel(env->tr.base + (0x28 + 7 * 4), EDI);
372
        for(i = 0; i < 6; i++)
373
            stw_kernel(env->tr.base + (0x48 + i * 4), env->segs[i].selector);
374
    } else {
375
        /* 16 bit */
376
        stw_kernel(env->tr.base + 0x0e, next_eip);
377
        stw_kernel(env->tr.base + 0x10, old_eflags);
378
        stw_kernel(env->tr.base + (0x12 + 0 * 2), EAX);
379
        stw_kernel(env->tr.base + (0x12 + 1 * 2), ECX);
380
        stw_kernel(env->tr.base + (0x12 + 2 * 2), EDX);
381
        stw_kernel(env->tr.base + (0x12 + 3 * 2), EBX);
382
        stw_kernel(env->tr.base + (0x12 + 4 * 2), ESP);
383
        stw_kernel(env->tr.base + (0x12 + 5 * 2), EBP);
384
        stw_kernel(env->tr.base + (0x12 + 6 * 2), ESI);
385
        stw_kernel(env->tr.base + (0x12 + 7 * 2), EDI);
386
        for(i = 0; i < 4; i++)
387
            stw_kernel(env->tr.base + (0x22 + i * 4), env->segs[i].selector);
388
    }
389
    
390
    /* now if an exception occurs, it will occurs in the next task
391
       context */
392

    
393
    if (source == SWITCH_TSS_CALL) {
394
        stw_kernel(tss_base, env->tr.selector);
395
        new_eflags |= NT_MASK;
396
    }
397

    
398
    /* set busy bit */
399
    if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_CALL) {
400
        target_ulong ptr;
401
        uint32_t e2;
402
        ptr = env->gdt.base + (tss_selector & ~7);
403
        e2 = ldl_kernel(ptr + 4);
404
        e2 |= DESC_TSS_BUSY_MASK;
405
        stl_kernel(ptr + 4, e2);
406
    }
407

    
408
    /* set the new CPU state */
409
    /* from this point, any exception which occurs can give problems */
410
    env->cr[0] |= CR0_TS_MASK;
411
    env->hflags |= HF_TS_MASK;
412
    env->tr.selector = tss_selector;
413
    env->tr.base = tss_base;
414
    env->tr.limit = tss_limit;
415
    env->tr.flags = e2 & ~DESC_TSS_BUSY_MASK;
416
    
417
    if ((type & 8) && (env->cr[0] & CR0_PG_MASK)) {
418
        cpu_x86_update_cr3(env, new_cr3);
419
    }
420
    
421
    /* load all registers without an exception, then reload them with
422
       possible exception */
423
    env->eip = new_eip;
424
    eflags_mask = TF_MASK | AC_MASK | ID_MASK | 
425
        IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK;
426
    if (!(type & 8))
427
        eflags_mask &= 0xffff;
428
    load_eflags(new_eflags, eflags_mask);
429
    /* XXX: what to do in 16 bit case ? */
430
    EAX = new_regs[0];
431
    ECX = new_regs[1];
432
    EDX = new_regs[2];
433
    EBX = new_regs[3];
434
    ESP = new_regs[4];
435
    EBP = new_regs[5];
436
    ESI = new_regs[6];
437
    EDI = new_regs[7];
438
    if (new_eflags & VM_MASK) {
439
        for(i = 0; i < 6; i++) 
440
            load_seg_vm(i, new_segs[i]);
441
        /* in vm86, CPL is always 3 */
442
        cpu_x86_set_cpl(env, 3);
443
    } else {
444
        /* CPL is set the RPL of CS */
445
        cpu_x86_set_cpl(env, new_segs[R_CS] & 3);
446
        /* first just selectors as the rest may trigger exceptions */
447
        for(i = 0; i < 6; i++)
448
            cpu_x86_load_seg_cache(env, i, new_segs[i], 0, 0, 0);
449
    }
450
    
451
    env->ldt.selector = new_ldt & ~4;
452
    env->ldt.base = 0;
453
    env->ldt.limit = 0;
454
    env->ldt.flags = 0;
455

    
456
    /* load the LDT */
457
    if (new_ldt & 4)
458
        raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
459

    
460
    if ((new_ldt & 0xfffc) != 0) {
461
        dt = &env->gdt;
462
        index = new_ldt & ~7;
463
        if ((index + 7) > dt->limit)
464
            raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
465
        ptr = dt->base + index;
466
        e1 = ldl_kernel(ptr);
467
        e2 = ldl_kernel(ptr + 4);
468
        if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
469
            raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
470
        if (!(e2 & DESC_P_MASK))
471
            raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
472
        load_seg_cache_raw_dt(&env->ldt, e1, e2);
473
    }
474
    
475
    /* load the segments */
476
    if (!(new_eflags & VM_MASK)) {
477
        tss_load_seg(R_CS, new_segs[R_CS]);
478
        tss_load_seg(R_SS, new_segs[R_SS]);
479
        tss_load_seg(R_ES, new_segs[R_ES]);
480
        tss_load_seg(R_DS, new_segs[R_DS]);
481
        tss_load_seg(R_FS, new_segs[R_FS]);
482
        tss_load_seg(R_GS, new_segs[R_GS]);
483
    }
484
    
485
    /* check that EIP is in the CS segment limits */
486
    if (new_eip > env->segs[R_CS].limit) {
487
        /* XXX: different exception if CALL ? */
488
        raise_exception_err(EXCP0D_GPF, 0);
489
    }
490
}
491

    
492
/* check if Port I/O is allowed in TSS */
493
static inline void check_io(int addr, int size)
494
{
495
    int io_offset, val, mask;
496
    
497
    /* TSS must be a valid 32 bit one */
498
    if (!(env->tr.flags & DESC_P_MASK) ||
499
        ((env->tr.flags >> DESC_TYPE_SHIFT) & 0xf) != 9 ||
500
        env->tr.limit < 103)
501
        goto fail;
502
    io_offset = lduw_kernel(env->tr.base + 0x66);
503
    io_offset += (addr >> 3);
504
    /* Note: the check needs two bytes */
505
    if ((io_offset + 1) > env->tr.limit)
506
        goto fail;
507
    val = lduw_kernel(env->tr.base + io_offset);
508
    val >>= (addr & 7);
509
    mask = (1 << size) - 1;
510
    /* all bits must be zero to allow the I/O */
511
    if ((val & mask) != 0) {
512
    fail:
513
        raise_exception_err(EXCP0D_GPF, 0);
514
    }
515
}
516

    
517
void check_iob_T0(void)
518
{
519
    check_io(T0, 1);
520
}
521

    
522
void check_iow_T0(void)
523
{
524
    check_io(T0, 2);
525
}
526

    
527
void check_iol_T0(void)
528
{
529
    check_io(T0, 4);
530
}
531

    
532
void check_iob_DX(void)
533
{
534
    check_io(EDX & 0xffff, 1);
535
}
536

    
537
void check_iow_DX(void)
538
{
539
    check_io(EDX & 0xffff, 2);
540
}
541

    
542
void check_iol_DX(void)
543
{
544
    check_io(EDX & 0xffff, 4);
545
}
546

    
547
static inline unsigned int get_sp_mask(unsigned int e2)
548
{
549
    if (e2 & DESC_B_MASK)
550
        return 0xffffffff;
551
    else
552
        return 0xffff;
553
}
554

    
555
/* XXX: add a is_user flag to have proper security support */
556
#define PUSHW(ssp, sp, sp_mask, val)\
557
{\
558
    sp -= 2;\
559
    stw_kernel((ssp) + (sp & (sp_mask)), (val));\
560
}
561

    
562
#define PUSHL(ssp, sp, sp_mask, val)\
563
{\
564
    sp -= 4;\
565
    stl_kernel((ssp) + (sp & (sp_mask)), (val));\
566
}
567

    
568
#define POPW(ssp, sp, sp_mask, val)\
569
{\
570
    val = lduw_kernel((ssp) + (sp & (sp_mask)));\
571
    sp += 2;\
572
}
573

    
574
#define POPL(ssp, sp, sp_mask, val)\
575
{\
576
    val = (uint32_t)ldl_kernel((ssp) + (sp & (sp_mask)));\
577
    sp += 4;\
578
}
579

    
580
/* protected mode interrupt */
581
static void do_interrupt_protected(int intno, int is_int, int error_code,
582
                                   unsigned int next_eip, int is_hw)
583
{
584
    SegmentCache *dt;
585
    target_ulong ptr, ssp;
586
    int type, dpl, selector, ss_dpl, cpl, sp_mask;
587
    int has_error_code, new_stack, shift;
588
    uint32_t e1, e2, offset, ss, esp, ss_e1, ss_e2;
589
    uint32_t old_eip;
590

    
591
    has_error_code = 0;
592
    if (!is_int && !is_hw) {
593
        switch(intno) {
594
        case 8:
595
        case 10:
596
        case 11:
597
        case 12:
598
        case 13:
599
        case 14:
600
        case 17:
601
            has_error_code = 1;
602
            break;
603
        }
604
    }
605
    if (is_int)
606
        old_eip = next_eip;
607
    else
608
        old_eip = env->eip;
609

    
610
    dt = &env->idt;
611
    if (intno * 8 + 7 > dt->limit)
612
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
613
    ptr = dt->base + intno * 8;
614
    e1 = ldl_kernel(ptr);
615
    e2 = ldl_kernel(ptr + 4);
616
    /* check gate type */
617
    type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
618
    switch(type) {
619
    case 5: /* task gate */
620
        /* must do that check here to return the correct error code */
621
        if (!(e2 & DESC_P_MASK))
622
            raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
623
        switch_tss(intno * 8, e1, e2, SWITCH_TSS_CALL, old_eip);
624
        if (has_error_code) {
625
            int mask;
626
            /* push the error code */
627
            shift = (env->segs[R_CS].flags >> DESC_B_SHIFT) & 1;
628
            if (env->segs[R_SS].flags & DESC_B_MASK)
629
                mask = 0xffffffff;
630
            else
631
                mask = 0xffff;
632
            esp = (ESP - (2 << shift)) & mask;
633
            ssp = env->segs[R_SS].base + esp;
634
            if (shift)
635
                stl_kernel(ssp, error_code);
636
            else
637
                stw_kernel(ssp, error_code);
638
            ESP = (esp & mask) | (ESP & ~mask);
639
        }
640
        return;
641
    case 6: /* 286 interrupt gate */
642
    case 7: /* 286 trap gate */
643
    case 14: /* 386 interrupt gate */
644
    case 15: /* 386 trap gate */
645
        break;
646
    default:
647
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
648
        break;
649
    }
650
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
651
    cpl = env->hflags & HF_CPL_MASK;
652
    /* check privledge if software int */
653
    if (is_int && dpl < cpl)
654
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
655
    /* check valid bit */
656
    if (!(e2 & DESC_P_MASK))
657
        raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
658
    selector = e1 >> 16;
659
    offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
660
    if ((selector & 0xfffc) == 0)
661
        raise_exception_err(EXCP0D_GPF, 0);
662

    
663
    if (load_segment(&e1, &e2, selector) != 0)
664
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
665
    if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
666
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
667
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
668
    if (dpl > cpl)
669
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
670
    if (!(e2 & DESC_P_MASK))
671
        raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
672
    if (!(e2 & DESC_C_MASK) && dpl < cpl) {
673
        /* to inner priviledge */
674
        get_ss_esp_from_tss(&ss, &esp, dpl);
675
        if ((ss & 0xfffc) == 0)
676
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
677
        if ((ss & 3) != dpl)
678
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
679
        if (load_segment(&ss_e1, &ss_e2, ss) != 0)
680
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
681
        ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
682
        if (ss_dpl != dpl)
683
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
684
        if (!(ss_e2 & DESC_S_MASK) ||
685
            (ss_e2 & DESC_CS_MASK) ||
686
            !(ss_e2 & DESC_W_MASK))
687
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
688
        if (!(ss_e2 & DESC_P_MASK))
689
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
690
        new_stack = 1;
691
        sp_mask = get_sp_mask(ss_e2);
692
        ssp = get_seg_base(ss_e1, ss_e2);
693
    } else if ((e2 & DESC_C_MASK) || dpl == cpl) {
694
        /* to same priviledge */
695
        if (env->eflags & VM_MASK)
696
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
697
        new_stack = 0;
698
        sp_mask = get_sp_mask(env->segs[R_SS].flags);
699
        ssp = env->segs[R_SS].base;
700
        esp = ESP;
701
        dpl = cpl;
702
    } else {
703
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
704
        new_stack = 0; /* avoid warning */
705
        sp_mask = 0; /* avoid warning */
706
        ssp = 0; /* avoid warning */
707
        esp = 0; /* avoid warning */
708
    }
709

    
710
    shift = type >> 3;
711

    
712
#if 0
713
    /* XXX: check that enough room is available */
714
    push_size = 6 + (new_stack << 2) + (has_error_code << 1);
715
    if (env->eflags & VM_MASK)
716
        push_size += 8;
717
    push_size <<= shift;
718
#endif
719
    if (shift == 1) {
720
        if (new_stack) {
721
            if (env->eflags & VM_MASK) {
722
                PUSHL(ssp, esp, sp_mask, env->segs[R_GS].selector);
723
                PUSHL(ssp, esp, sp_mask, env->segs[R_FS].selector);
724
                PUSHL(ssp, esp, sp_mask, env->segs[R_DS].selector);
725
                PUSHL(ssp, esp, sp_mask, env->segs[R_ES].selector);
726
            }
727
            PUSHL(ssp, esp, sp_mask, env->segs[R_SS].selector);
728
            PUSHL(ssp, esp, sp_mask, ESP);
729
        }
730
        PUSHL(ssp, esp, sp_mask, compute_eflags());
731
        PUSHL(ssp, esp, sp_mask, env->segs[R_CS].selector);
732
        PUSHL(ssp, esp, sp_mask, old_eip);
733
        if (has_error_code) {
734
            PUSHL(ssp, esp, sp_mask, error_code);
735
        }
736
    } else {
737
        if (new_stack) {
738
            if (env->eflags & VM_MASK) {
739
                PUSHW(ssp, esp, sp_mask, env->segs[R_GS].selector);
740
                PUSHW(ssp, esp, sp_mask, env->segs[R_FS].selector);
741
                PUSHW(ssp, esp, sp_mask, env->segs[R_DS].selector);
742
                PUSHW(ssp, esp, sp_mask, env->segs[R_ES].selector);
743
            }
744
            PUSHW(ssp, esp, sp_mask, env->segs[R_SS].selector);
745
            PUSHW(ssp, esp, sp_mask, ESP);
746
        }
747
        PUSHW(ssp, esp, sp_mask, compute_eflags());
748
        PUSHW(ssp, esp, sp_mask, env->segs[R_CS].selector);
749
        PUSHW(ssp, esp, sp_mask, old_eip);
750
        if (has_error_code) {
751
            PUSHW(ssp, esp, sp_mask, error_code);
752
        }
753
    }
754
    
755
    if (new_stack) {
756
        if (env->eflags & VM_MASK) {
757
            cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0, 0);
758
            cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0, 0);
759
            cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0, 0);
760
            cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0, 0);
761
        }
762
        ss = (ss & ~3) | dpl;
763
        cpu_x86_load_seg_cache(env, R_SS, ss, 
764
                               ssp, get_seg_limit(ss_e1, ss_e2), ss_e2);
765
    }
766
    ESP = (ESP & ~sp_mask) | (esp & sp_mask);
767

    
768
    selector = (selector & ~3) | dpl;
769
    cpu_x86_load_seg_cache(env, R_CS, selector, 
770
                   get_seg_base(e1, e2),
771
                   get_seg_limit(e1, e2),
772
                   e2);
773
    cpu_x86_set_cpl(env, dpl);
774
    env->eip = offset;
775

    
776
    /* interrupt gate clear IF mask */
777
    if ((type & 1) == 0) {
778
        env->eflags &= ~IF_MASK;
779
    }
780
    env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
781
}
782

    
783
#ifdef TARGET_X86_64
784

    
785
#define PUSHQ(sp, val)\
786
{\
787
    sp -= 8;\
788
    stq_kernel(sp, (val));\
789
}
790

    
791
#define POPQ(sp, val)\
792
{\
793
    val = ldq_kernel(sp);\
794
    sp += 8;\
795
}
796

    
797
static inline target_ulong get_rsp_from_tss(int level)
798
{
799
    int index;
800
    
801
#if 0
802
    printf("TR: base=" TARGET_FMT_lx " limit=%x\n", 
803
           env->tr.base, env->tr.limit);
804
#endif
805

    
806
    if (!(env->tr.flags & DESC_P_MASK))
807
        cpu_abort(env, "invalid tss");
808
    index = 8 * level + 4;
809
    if ((index + 7) > env->tr.limit)
810
        raise_exception_err(EXCP0A_TSS, env->tr.selector & 0xfffc);
811
    return ldq_kernel(env->tr.base + index);
812
}
813

    
814
/* 64 bit interrupt */
815
static void do_interrupt64(int intno, int is_int, int error_code,
816
                           target_ulong next_eip, int is_hw)
817
{
818
    SegmentCache *dt;
819
    target_ulong ptr;
820
    int type, dpl, selector, cpl, ist;
821
    int has_error_code, new_stack;
822
    uint32_t e1, e2, e3, ss;
823
    target_ulong old_eip, esp, offset;
824

    
825
    has_error_code = 0;
826
    if (!is_int && !is_hw) {
827
        switch(intno) {
828
        case 8:
829
        case 10:
830
        case 11:
831
        case 12:
832
        case 13:
833
        case 14:
834
        case 17:
835
            has_error_code = 1;
836
            break;
837
        }
838
    }
839
    if (is_int)
840
        old_eip = next_eip;
841
    else
842
        old_eip = env->eip;
843

    
844
    dt = &env->idt;
845
    if (intno * 16 + 15 > dt->limit)
846
        raise_exception_err(EXCP0D_GPF, intno * 16 + 2);
847
    ptr = dt->base + intno * 16;
848
    e1 = ldl_kernel(ptr);
849
    e2 = ldl_kernel(ptr + 4);
850
    e3 = ldl_kernel(ptr + 8);
851
    /* check gate type */
852
    type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
853
    switch(type) {
854
    case 14: /* 386 interrupt gate */
855
    case 15: /* 386 trap gate */
856
        break;
857
    default:
858
        raise_exception_err(EXCP0D_GPF, intno * 16 + 2);
859
        break;
860
    }
861
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
862
    cpl = env->hflags & HF_CPL_MASK;
863
    /* check privledge if software int */
864
    if (is_int && dpl < cpl)
865
        raise_exception_err(EXCP0D_GPF, intno * 16 + 2);
866
    /* check valid bit */
867
    if (!(e2 & DESC_P_MASK))
868
        raise_exception_err(EXCP0B_NOSEG, intno * 16 + 2);
869
    selector = e1 >> 16;
870
    offset = ((target_ulong)e3 << 32) | (e2 & 0xffff0000) | (e1 & 0x0000ffff);
871
    ist = e2 & 7;
872
    if ((selector & 0xfffc) == 0)
873
        raise_exception_err(EXCP0D_GPF, 0);
874

    
875
    if (load_segment(&e1, &e2, selector) != 0)
876
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
877
    if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
878
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
879
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
880
    if (dpl > cpl)
881
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
882
    if (!(e2 & DESC_P_MASK))
883
        raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
884
    if (!(e2 & DESC_L_MASK) || (e2 & DESC_B_MASK))
885
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
886
    if ((!(e2 & DESC_C_MASK) && dpl < cpl) || ist != 0) {
887
        /* to inner priviledge */
888
        if (ist != 0)
889
            esp = get_rsp_from_tss(ist + 3);
890
        else
891
            esp = get_rsp_from_tss(dpl);
892
        ss = 0;
893
        new_stack = 1;
894
    } else if ((e2 & DESC_C_MASK) || dpl == cpl) {
895
        /* to same priviledge */
896
        if (env->eflags & VM_MASK)
897
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
898
        new_stack = 0;
899
        esp = ESP & ~0xf; /* align stack */
900
        dpl = cpl;
901
    } else {
902
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
903
        new_stack = 0; /* avoid warning */
904
        esp = 0; /* avoid warning */
905
    }
906

    
907
    PUSHQ(esp, env->segs[R_SS].selector);
908
    PUSHQ(esp, ESP);
909
    PUSHQ(esp, compute_eflags());
910
    PUSHQ(esp, env->segs[R_CS].selector);
911
    PUSHQ(esp, old_eip);
912
    if (has_error_code) {
913
        PUSHQ(esp, error_code);
914
    }
915
    
916
    if (new_stack) {
917
        ss = 0 | dpl;
918
        cpu_x86_load_seg_cache(env, R_SS, ss, 0, 0, 0);
919
    }
920
    ESP = esp;
921

    
922
    selector = (selector & ~3) | dpl;
923
    cpu_x86_load_seg_cache(env, R_CS, selector, 
924
                   get_seg_base(e1, e2),
925
                   get_seg_limit(e1, e2),
926
                   e2);
927
    cpu_x86_set_cpl(env, dpl);
928
    env->eip = offset;
929

    
930
    /* interrupt gate clear IF mask */
931
    if ((type & 1) == 0) {
932
        env->eflags &= ~IF_MASK;
933
    }
934
    env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
935
}
936

    
937
void helper_syscall(int next_eip_addend)
938
{
939
    int selector;
940

    
941
    if (!(env->efer & MSR_EFER_SCE)) {
942
        raise_exception_err(EXCP06_ILLOP, 0);
943
    }
944
    selector = (env->star >> 32) & 0xffff;
945
    if (env->hflags & HF_LMA_MASK) {
946
        ECX = env->eip + next_eip_addend;
947
        env->regs[11] = compute_eflags();
948

    
949
        cpu_x86_set_cpl(env, 0);
950
        cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc, 
951
                           0, 0xffffffff, 
952
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
953
                               DESC_S_MASK |
954
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK | DESC_L_MASK);
955
        cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc, 
956
                               0, 0xffffffff,
957
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
958
                               DESC_S_MASK |
959
                               DESC_W_MASK | DESC_A_MASK);
960
        env->eflags &= ~env->fmask;
961
        if (env->hflags & HF_CS64_MASK)
962
            env->eip = env->lstar;
963
        else
964
            env->eip = env->cstar;
965
    } else {
966
        ECX = (uint32_t)(env->eip + next_eip_addend);
967
        
968
        cpu_x86_set_cpl(env, 0);
969
        cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc, 
970
                           0, 0xffffffff, 
971
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
972
                               DESC_S_MASK |
973
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
974
        cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc, 
975
                               0, 0xffffffff,
976
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
977
                               DESC_S_MASK |
978
                               DESC_W_MASK | DESC_A_MASK);
979
        env->eflags &= ~(IF_MASK | RF_MASK | VM_MASK);
980
        env->eip = (uint32_t)env->star;
981
    }
982
}
983

    
984
void helper_sysret(int dflag)
985
{
986
    int cpl, selector;
987

    
988
    cpl = env->hflags & HF_CPL_MASK;
989
    if (!(env->cr[0] & CR0_PE_MASK) || cpl != 0) {
990
        raise_exception_err(EXCP0D_GPF, 0);
991
    }
992
    selector = (env->star >> 48) & 0xffff;
993
    if (env->hflags & HF_LMA_MASK) {
994
        if (dflag == 2) {
995
            cpu_x86_load_seg_cache(env, R_CS, (selector + 16) | 3, 
996
                                   0, 0xffffffff, 
997
                                   DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
998
                                   DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
999
                                   DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK | 
1000
                                   DESC_L_MASK);
1001
            env->eip = ECX;
1002
        } else {
1003
            cpu_x86_load_seg_cache(env, R_CS, selector | 3, 
1004
                                   0, 0xffffffff, 
1005
                                   DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1006
                                   DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1007
                                   DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
1008
            env->eip = (uint32_t)ECX;
1009
        }
1010
        cpu_x86_load_seg_cache(env, R_SS, selector + 8, 
1011
                               0, 0xffffffff,
1012
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1013
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1014
                               DESC_W_MASK | DESC_A_MASK);
1015
        load_eflags((uint32_t)(env->regs[11]), 0xffffffff);
1016
        cpu_x86_set_cpl(env, 3);
1017
    } else {
1018
        cpu_x86_load_seg_cache(env, R_CS, selector | 3, 
1019
                               0, 0xffffffff, 
1020
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1021
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1022
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
1023
        env->eip = (uint32_t)ECX;
1024
        cpu_x86_load_seg_cache(env, R_SS, selector + 8, 
1025
                               0, 0xffffffff,
1026
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1027
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1028
                               DESC_W_MASK | DESC_A_MASK);
1029
        env->eflags |= IF_MASK;
1030
        cpu_x86_set_cpl(env, 3);
1031
    }
1032
}
1033
#endif
1034

    
1035
/* real mode interrupt */
1036
static void do_interrupt_real(int intno, int is_int, int error_code,
1037
                              unsigned int next_eip)
1038
{
1039
    SegmentCache *dt;
1040
    target_ulong ptr, ssp;
1041
    int selector;
1042
    uint32_t offset, esp;
1043
    uint32_t old_cs, old_eip;
1044

    
1045
    /* real mode (simpler !) */
1046
    dt = &env->idt;
1047
    if (intno * 4 + 3 > dt->limit)
1048
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
1049
    ptr = dt->base + intno * 4;
1050
    offset = lduw_kernel(ptr);
1051
    selector = lduw_kernel(ptr + 2);
1052
    esp = ESP;
1053
    ssp = env->segs[R_SS].base;
1054
    if (is_int)
1055
        old_eip = next_eip;
1056
    else
1057
        old_eip = env->eip;
1058
    old_cs = env->segs[R_CS].selector;
1059
    /* XXX: use SS segment size ? */
1060
    PUSHW(ssp, esp, 0xffff, compute_eflags());
1061
    PUSHW(ssp, esp, 0xffff, old_cs);
1062
    PUSHW(ssp, esp, 0xffff, old_eip);
1063
    
1064
    /* update processor state */
1065
    ESP = (ESP & ~0xffff) | (esp & 0xffff);
1066
    env->eip = offset;
1067
    env->segs[R_CS].selector = selector;
1068
    env->segs[R_CS].base = (selector << 4);
1069
    env->eflags &= ~(IF_MASK | TF_MASK | AC_MASK | RF_MASK);
1070
}
1071

    
1072
/* fake user mode interrupt */
1073
void do_interrupt_user(int intno, int is_int, int error_code, 
1074
                       target_ulong next_eip)
1075
{
1076
    SegmentCache *dt;
1077
    target_ulong ptr;
1078
    int dpl, cpl;
1079
    uint32_t e2;
1080

    
1081
    dt = &env->idt;
1082
    ptr = dt->base + (intno * 8);
1083
    e2 = ldl_kernel(ptr + 4);
1084
    
1085
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1086
    cpl = env->hflags & HF_CPL_MASK;
1087
    /* check privledge if software int */
1088
    if (is_int && dpl < cpl)
1089
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
1090

    
1091
    /* Since we emulate only user space, we cannot do more than
1092
       exiting the emulation with the suitable exception and error
1093
       code */
1094
    if (is_int)
1095
        EIP = next_eip;
1096
}
1097

    
1098
/*
1099
 * Begin execution of an interruption. is_int is TRUE if coming from
1100
 * the int instruction. next_eip is the EIP value AFTER the interrupt
1101
 * instruction. It is only relevant if is_int is TRUE.  
1102
 */
1103
void do_interrupt(int intno, int is_int, int error_code, 
1104
                  target_ulong next_eip, int is_hw)
1105
{
1106
#ifdef DEBUG_PCALL
1107
    if (loglevel & (CPU_LOG_PCALL | CPU_LOG_INT)) {
1108
        if ((env->cr[0] & CR0_PE_MASK)) {
1109
            static int count;
1110
            fprintf(logfile, "%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx " pc=" TARGET_FMT_lx " SP=%04x:" TARGET_FMT_lx,
1111
                    count, intno, error_code, is_int,
1112
                    env->hflags & HF_CPL_MASK,
1113
                    env->segs[R_CS].selector, EIP,
1114
                    (int)env->segs[R_CS].base + EIP,
1115
                    env->segs[R_SS].selector, ESP);
1116
            if (intno == 0x0e) {
1117
                fprintf(logfile, " CR2=" TARGET_FMT_lx, env->cr[2]);
1118
            } else {
1119
                fprintf(logfile, " EAX=" TARGET_FMT_lx, EAX);
1120
            }
1121
            fprintf(logfile, "\n");
1122
#if 0
1123
            cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
1124
            {
1125
                int i;
1126
                uint8_t *ptr;
1127
                fprintf(logfile, "       code=");
1128
                ptr = env->segs[R_CS].base + env->eip;
1129
                for(i = 0; i < 16; i++) {
1130
                    fprintf(logfile, " %02x", ldub(ptr + i));
1131
                }
1132
                fprintf(logfile, "\n");
1133
            }
1134
#endif
1135
            count++;
1136
        }
1137
    }
1138
#endif
1139
    if (env->cr[0] & CR0_PE_MASK) {
1140
#if TARGET_X86_64
1141
        if (env->hflags & HF_LMA_MASK) {
1142
            do_interrupt64(intno, is_int, error_code, next_eip, is_hw);
1143
        } else
1144
#endif
1145
        {
1146
            do_interrupt_protected(intno, is_int, error_code, next_eip, is_hw);
1147
        }
1148
    } else {
1149
        do_interrupt_real(intno, is_int, error_code, next_eip);
1150
    }
1151
}
1152

    
1153
/*
1154
 * Signal an interruption. It is executed in the main CPU loop.
1155
 * is_int is TRUE if coming from the int instruction. next_eip is the
1156
 * EIP value AFTER the interrupt instruction. It is only relevant if
1157
 * is_int is TRUE.  
1158
 */
1159
void raise_interrupt(int intno, int is_int, int error_code, 
1160
                     int next_eip_addend)
1161
{
1162
    env->exception_index = intno;
1163
    env->error_code = error_code;
1164
    env->exception_is_int = is_int;
1165
    env->exception_next_eip = env->eip + next_eip_addend;
1166
    cpu_loop_exit();
1167
}
1168

    
1169
/* same as raise_exception_err, but do not restore global registers */
1170
static void raise_exception_err_norestore(int exception_index, int error_code)
1171
{
1172
    env->exception_index = exception_index;
1173
    env->error_code = error_code;
1174
    env->exception_is_int = 0;
1175
    env->exception_next_eip = 0;
1176
    longjmp(env->jmp_env, 1);
1177
}
1178

    
1179
/* shortcuts to generate exceptions */
1180

    
1181
void (raise_exception_err)(int exception_index, int error_code)
1182
{
1183
    raise_interrupt(exception_index, 0, error_code, 0);
1184
}
1185

    
1186
void raise_exception(int exception_index)
1187
{
1188
    raise_interrupt(exception_index, 0, 0, 0);
1189
}
1190

    
1191
#ifdef BUGGY_GCC_DIV64
1192
/* gcc 2.95.4 on PowerPC does not seem to like using __udivdi3, so we
1193
   call it from another function */
1194
uint32_t div32(uint32_t *q_ptr, uint64_t num, uint32_t den)
1195
{
1196
    *q_ptr = num / den;
1197
    return num % den;
1198
}
1199

    
1200
int32_t idiv32(int32_t *q_ptr, int64_t num, int32_t den)
1201
{
1202
    *q_ptr = num / den;
1203
    return num % den;
1204
}
1205
#endif
1206

    
1207
void helper_divl_EAX_T0(void)
1208
{
1209
    unsigned int den, q, r;
1210
    uint64_t num;
1211
    
1212
    num = EAX | ((uint64_t)EDX << 32);
1213
    den = T0;
1214
    if (den == 0) {
1215
        raise_exception(EXCP00_DIVZ);
1216
    }
1217
#ifdef BUGGY_GCC_DIV64
1218
    r = div32(&q, num, den);
1219
#else
1220
    q = (num / den);
1221
    r = (num % den);
1222
#endif
1223
    EAX = (uint32_t)q;
1224
    EDX = (uint32_t)r;
1225
}
1226

    
1227
void helper_idivl_EAX_T0(void)
1228
{
1229
    int den, q, r;
1230
    int64_t num;
1231
    
1232
    num = EAX | ((uint64_t)EDX << 32);
1233
    den = T0;
1234
    if (den == 0) {
1235
        raise_exception(EXCP00_DIVZ);
1236
    }
1237
#ifdef BUGGY_GCC_DIV64
1238
    r = idiv32(&q, num, den);
1239
#else
1240
    q = (num / den);
1241
    r = (num % den);
1242
#endif
1243
    EAX = (uint32_t)q;
1244
    EDX = (uint32_t)r;
1245
}
1246

    
1247
void helper_cmpxchg8b(void)
1248
{
1249
    uint64_t d;
1250
    int eflags;
1251

    
1252
    eflags = cc_table[CC_OP].compute_all();
1253
    d = ldq(A0);
1254
    if (d == (((uint64_t)EDX << 32) | EAX)) {
1255
        stq(A0, ((uint64_t)ECX << 32) | EBX);
1256
        eflags |= CC_Z;
1257
    } else {
1258
        EDX = d >> 32;
1259
        EAX = d;
1260
        eflags &= ~CC_Z;
1261
    }
1262
    CC_SRC = eflags;
1263
}
1264

    
1265
void helper_cpuid(void)
1266
{
1267
    switch((uint32_t)EAX) {
1268
    case 0:
1269
        EAX = 2; /* max EAX index supported */
1270
        EBX = env->cpuid_vendor1;
1271
        EDX = env->cpuid_vendor2;
1272
        ECX = env->cpuid_vendor3;
1273
        break;
1274
    case 1:
1275
        EAX = env->cpuid_version;
1276
        EBX = 0;
1277
        ECX = 0;
1278
        EDX = env->cpuid_features;
1279
        break;
1280
    default:
1281
        /* cache info: needed for Pentium Pro compatibility */
1282
        EAX = 0x410601;
1283
        EBX = 0;
1284
        ECX = 0;
1285
        EDX = 0;
1286
        break;
1287
#ifdef TARGET_X86_64
1288
    case 0x80000000:
1289
        EAX = 0x80000008;
1290
        EBX = env->cpuid_vendor1;
1291
        EDX = env->cpuid_vendor2;
1292
        ECX = env->cpuid_vendor3;
1293
        break;
1294
    case 0x80000001:
1295
        EAX = env->cpuid_features;
1296
        EBX = 0;
1297
        ECX = 0;
1298
        /* long mode + syscall/sysret features */
1299
        EDX = (env->cpuid_features & 0x0183F3FF) | (1 << 29) | (1 << 11);
1300
        break;
1301
    case 0x80000008:
1302
        /* virtual & phys address size in low 2 bytes. */
1303
        EAX = 0x00003028;
1304
        EBX = 0;
1305
        ECX = 0;
1306
        EDX = 0;
1307
        break;
1308
#endif
1309
    }
1310
}
1311

    
1312
void helper_enter_level(int level, int data32)
1313
{
1314
    target_ulong ssp;
1315
    uint32_t esp_mask, esp, ebp;
1316

    
1317
    esp_mask = get_sp_mask(env->segs[R_SS].flags);
1318
    ssp = env->segs[R_SS].base;
1319
    ebp = EBP;
1320
    esp = ESP;
1321
    if (data32) {
1322
        /* 32 bit */
1323
        esp -= 4;
1324
        while (--level) {
1325
            esp -= 4;
1326
            ebp -= 4;
1327
            stl(ssp + (esp & esp_mask), ldl(ssp + (ebp & esp_mask)));
1328
        }
1329
        esp -= 4;
1330
        stl(ssp + (esp & esp_mask), T1);
1331
    } else {
1332
        /* 16 bit */
1333
        esp -= 2;
1334
        while (--level) {
1335
            esp -= 2;
1336
            ebp -= 2;
1337
            stw(ssp + (esp & esp_mask), lduw(ssp + (ebp & esp_mask)));
1338
        }
1339
        esp -= 2;
1340
        stw(ssp + (esp & esp_mask), T1);
1341
    }
1342
}
1343

    
1344
void helper_lldt_T0(void)
1345
{
1346
    int selector;
1347
    SegmentCache *dt;
1348
    uint32_t e1, e2;
1349
    int index, entry_limit;
1350
    target_ulong ptr;
1351
    
1352
    selector = T0 & 0xffff;
1353
    if ((selector & 0xfffc) == 0) {
1354
        /* XXX: NULL selector case: invalid LDT */
1355
        env->ldt.base = 0;
1356
        env->ldt.limit = 0;
1357
    } else {
1358
        if (selector & 0x4)
1359
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1360
        dt = &env->gdt;
1361
        index = selector & ~7;
1362
#ifdef TARGET_X86_64
1363
        if (env->hflags & HF_LMA_MASK)
1364
            entry_limit = 15;
1365
        else
1366
#endif            
1367
            entry_limit = 7;
1368
        if ((index + entry_limit) > dt->limit)
1369
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1370
        ptr = dt->base + index;
1371
        e1 = ldl_kernel(ptr);
1372
        e2 = ldl_kernel(ptr + 4);
1373
        if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
1374
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1375
        if (!(e2 & DESC_P_MASK))
1376
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1377
#ifdef TARGET_X86_64
1378
        if (env->hflags & HF_LMA_MASK) {
1379
            uint32_t e3;
1380
            e3 = ldl_kernel(ptr + 8);
1381
            load_seg_cache_raw_dt(&env->ldt, e1, e2);
1382
            env->ldt.base |= (target_ulong)e3 << 32;
1383
        } else
1384
#endif
1385
        {
1386
            load_seg_cache_raw_dt(&env->ldt, e1, e2);
1387
        }
1388
    }
1389
    env->ldt.selector = selector;
1390
}
1391

    
1392
void helper_ltr_T0(void)
1393
{
1394
    int selector;
1395
    SegmentCache *dt;
1396
    uint32_t e1, e2;
1397
    int index, type, entry_limit;
1398
    target_ulong ptr;
1399
    
1400
    selector = T0 & 0xffff;
1401
    if ((selector & 0xfffc) == 0) {
1402
        /* NULL selector case: invalid TR */
1403
        env->tr.base = 0;
1404
        env->tr.limit = 0;
1405
        env->tr.flags = 0;
1406
    } else {
1407
        if (selector & 0x4)
1408
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1409
        dt = &env->gdt;
1410
        index = selector & ~7;
1411
#ifdef TARGET_X86_64
1412
        if (env->hflags & HF_LMA_MASK)
1413
            entry_limit = 15;
1414
        else
1415
#endif            
1416
            entry_limit = 7;
1417
        if ((index + entry_limit) > dt->limit)
1418
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1419
        ptr = dt->base + index;
1420
        e1 = ldl_kernel(ptr);
1421
        e2 = ldl_kernel(ptr + 4);
1422
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1423
        if ((e2 & DESC_S_MASK) || 
1424
            (type != 1 && type != 9))
1425
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1426
        if (!(e2 & DESC_P_MASK))
1427
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1428
#ifdef TARGET_X86_64
1429
        if (env->hflags & HF_LMA_MASK) {
1430
            uint32_t e3;
1431
            e3 = ldl_kernel(ptr + 8);
1432
            load_seg_cache_raw_dt(&env->tr, e1, e2);
1433
            env->tr.base |= (target_ulong)e3 << 32;
1434
        } else 
1435
#endif
1436
        {
1437
            load_seg_cache_raw_dt(&env->tr, e1, e2);
1438
        }
1439
        e2 |= DESC_TSS_BUSY_MASK;
1440
        stl_kernel(ptr + 4, e2);
1441
    }
1442
    env->tr.selector = selector;
1443
}
1444

    
1445
/* only works if protected mode and not VM86. seg_reg must be != R_CS */
1446
void load_seg(int seg_reg, int selector)
1447
{
1448
    uint32_t e1, e2;
1449
    int cpl, dpl, rpl;
1450
    SegmentCache *dt;
1451
    int index;
1452
    target_ulong ptr;
1453

    
1454
    selector &= 0xffff;
1455
    if ((selector & 0xfffc) == 0) {
1456
        /* null selector case */
1457
        if (seg_reg == R_SS)
1458
            raise_exception_err(EXCP0D_GPF, 0);
1459
        cpu_x86_load_seg_cache(env, seg_reg, selector, 0, 0, 0);
1460
    } else {
1461
        
1462
        if (selector & 0x4)
1463
            dt = &env->ldt;
1464
        else
1465
            dt = &env->gdt;
1466
        index = selector & ~7;
1467
        if ((index + 7) > dt->limit)
1468
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1469
        ptr = dt->base + index;
1470
        e1 = ldl_kernel(ptr);
1471
        e2 = ldl_kernel(ptr + 4);
1472
        
1473
        if (!(e2 & DESC_S_MASK))
1474
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1475
        rpl = selector & 3;
1476
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1477
        cpl = env->hflags & HF_CPL_MASK;
1478
        if (seg_reg == R_SS) {
1479
            /* must be writable segment */
1480
            if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
1481
                raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1482
            if (rpl != cpl || dpl != cpl)
1483
                raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1484
        } else {
1485
            /* must be readable segment */
1486
            if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK)
1487
                raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1488
            
1489
            if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1490
                /* if not conforming code, test rights */
1491
                if (dpl < cpl || dpl < rpl)
1492
                    raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1493
            }
1494
        }
1495

    
1496
        if (!(e2 & DESC_P_MASK)) {
1497
            if (seg_reg == R_SS)
1498
                raise_exception_err(EXCP0C_STACK, selector & 0xfffc);
1499
            else
1500
                raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1501
        }
1502

    
1503
        /* set the access bit if not already set */
1504
        if (!(e2 & DESC_A_MASK)) {
1505
            e2 |= DESC_A_MASK;
1506
            stl_kernel(ptr + 4, e2);
1507
        }
1508

    
1509
        cpu_x86_load_seg_cache(env, seg_reg, selector, 
1510
                       get_seg_base(e1, e2),
1511
                       get_seg_limit(e1, e2),
1512
                       e2);
1513
#if 0
1514
        fprintf(logfile, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n", 
1515
                selector, (unsigned long)sc->base, sc->limit, sc->flags);
1516
#endif
1517
    }
1518
}
1519

    
1520
/* protected mode jump */
1521
void helper_ljmp_protected_T0_T1(int next_eip)
1522
{
1523
    int new_cs, gate_cs, type;
1524
    uint32_t e1, e2, cpl, dpl, rpl, limit;
1525
    target_ulong new_eip;
1526
    
1527
    new_cs = T0;
1528
    new_eip = T1;
1529
    if ((new_cs & 0xfffc) == 0)
1530
        raise_exception_err(EXCP0D_GPF, 0);
1531
    if (load_segment(&e1, &e2, new_cs) != 0)
1532
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1533
    cpl = env->hflags & HF_CPL_MASK;
1534
    if (e2 & DESC_S_MASK) {
1535
        if (!(e2 & DESC_CS_MASK))
1536
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1537
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1538
        if (e2 & DESC_C_MASK) {
1539
            /* conforming code segment */
1540
            if (dpl > cpl)
1541
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1542
        } else {
1543
            /* non conforming code segment */
1544
            rpl = new_cs & 3;
1545
            if (rpl > cpl)
1546
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1547
            if (dpl != cpl)
1548
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1549
        }
1550
        if (!(e2 & DESC_P_MASK))
1551
            raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1552
        limit = get_seg_limit(e1, e2);
1553
        if (new_eip > limit && 
1554
            !(env->hflags & HF_LMA_MASK) && !(e2 & DESC_L_MASK))
1555
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1556
        cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1557
                       get_seg_base(e1, e2), limit, e2);
1558
        EIP = new_eip;
1559
    } else {
1560
        /* jump to call or task gate */
1561
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1562
        rpl = new_cs & 3;
1563
        cpl = env->hflags & HF_CPL_MASK;
1564
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1565
        switch(type) {
1566
        case 1: /* 286 TSS */
1567
        case 9: /* 386 TSS */
1568
        case 5: /* task gate */
1569
            if (dpl < cpl || dpl < rpl)
1570
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1571
            switch_tss(new_cs, e1, e2, SWITCH_TSS_JMP, next_eip);
1572
            break;
1573
        case 4: /* 286 call gate */
1574
        case 12: /* 386 call gate */
1575
            if ((dpl < cpl) || (dpl < rpl))
1576
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1577
            if (!(e2 & DESC_P_MASK))
1578
                raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1579
            gate_cs = e1 >> 16;
1580
            new_eip = (e1 & 0xffff);
1581
            if (type == 12)
1582
                new_eip |= (e2 & 0xffff0000);
1583
            if (load_segment(&e1, &e2, gate_cs) != 0)
1584
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1585
            dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1586
            /* must be code segment */
1587
            if (((e2 & (DESC_S_MASK | DESC_CS_MASK)) != 
1588
                 (DESC_S_MASK | DESC_CS_MASK)))
1589
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1590
            if (((e2 & DESC_C_MASK) && (dpl > cpl)) || 
1591
                (!(e2 & DESC_C_MASK) && (dpl != cpl)))
1592
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1593
            if (!(e2 & DESC_P_MASK))
1594
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1595
            limit = get_seg_limit(e1, e2);
1596
            if (new_eip > limit)
1597
                raise_exception_err(EXCP0D_GPF, 0);
1598
            cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl,
1599
                                   get_seg_base(e1, e2), limit, e2);
1600
            EIP = new_eip;
1601
            break;
1602
        default:
1603
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1604
            break;
1605
        }
1606
    }
1607
}
1608

    
1609
/* real mode call */
1610
void helper_lcall_real_T0_T1(int shift, int next_eip)
1611
{
1612
    int new_cs, new_eip;
1613
    uint32_t esp, esp_mask;
1614
    target_ulong ssp;
1615

    
1616
    new_cs = T0;
1617
    new_eip = T1;
1618
    esp = ESP;
1619
    esp_mask = get_sp_mask(env->segs[R_SS].flags);
1620
    ssp = env->segs[R_SS].base;
1621
    if (shift) {
1622
        PUSHL(ssp, esp, esp_mask, env->segs[R_CS].selector);
1623
        PUSHL(ssp, esp, esp_mask, next_eip);
1624
    } else {
1625
        PUSHW(ssp, esp, esp_mask, env->segs[R_CS].selector);
1626
        PUSHW(ssp, esp, esp_mask, next_eip);
1627
    }
1628

    
1629
    ESP = (ESP & ~esp_mask) | (esp & esp_mask);
1630
    env->eip = new_eip;
1631
    env->segs[R_CS].selector = new_cs;
1632
    env->segs[R_CS].base = (new_cs << 4);
1633
}
1634

    
1635
/* protected mode call */
1636
void helper_lcall_protected_T0_T1(int shift, int next_eip)
1637
{
1638
    int new_cs, new_eip, new_stack, i;
1639
    uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count;
1640
    uint32_t ss, ss_e1, ss_e2, sp, type, ss_dpl, sp_mask;
1641
    uint32_t val, limit, old_sp_mask;
1642
    target_ulong ssp, old_ssp;
1643
    
1644
    new_cs = T0;
1645
    new_eip = T1;
1646
#ifdef DEBUG_PCALL
1647
    if (loglevel & CPU_LOG_PCALL) {
1648
        fprintf(logfile, "lcall %04x:%08x s=%d\n",
1649
                new_cs, new_eip, shift);
1650
        cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
1651
    }
1652
#endif
1653
    if ((new_cs & 0xfffc) == 0)
1654
        raise_exception_err(EXCP0D_GPF, 0);
1655
    if (load_segment(&e1, &e2, new_cs) != 0)
1656
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1657
    cpl = env->hflags & HF_CPL_MASK;
1658
#ifdef DEBUG_PCALL
1659
    if (loglevel & CPU_LOG_PCALL) {
1660
        fprintf(logfile, "desc=%08x:%08x\n", e1, e2);
1661
    }
1662
#endif
1663
    if (e2 & DESC_S_MASK) {
1664
        if (!(e2 & DESC_CS_MASK))
1665
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1666
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1667
        if (e2 & DESC_C_MASK) {
1668
            /* conforming code segment */
1669
            if (dpl > cpl)
1670
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1671
        } else {
1672
            /* non conforming code segment */
1673
            rpl = new_cs & 3;
1674
            if (rpl > cpl)
1675
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1676
            if (dpl != cpl)
1677
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1678
        }
1679
        if (!(e2 & DESC_P_MASK))
1680
            raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1681

    
1682
        sp = ESP;
1683
        sp_mask = get_sp_mask(env->segs[R_SS].flags);
1684
        ssp = env->segs[R_SS].base;
1685
        if (shift) {
1686
            PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1687
            PUSHL(ssp, sp, sp_mask, next_eip);
1688
        } else {
1689
            PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1690
            PUSHW(ssp, sp, sp_mask, next_eip);
1691
        }
1692
        
1693
        limit = get_seg_limit(e1, e2);
1694
        if (new_eip > limit)
1695
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1696
        /* from this point, not restartable */
1697
        ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1698
        cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1699
                       get_seg_base(e1, e2), limit, e2);
1700
        EIP = new_eip;
1701
    } else {
1702
        /* check gate type */
1703
        type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
1704
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1705
        rpl = new_cs & 3;
1706
        switch(type) {
1707
        case 1: /* available 286 TSS */
1708
        case 9: /* available 386 TSS */
1709
        case 5: /* task gate */
1710
            if (dpl < cpl || dpl < rpl)
1711
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1712
            switch_tss(new_cs, e1, e2, SWITCH_TSS_CALL, next_eip);
1713
            return;
1714
        case 4: /* 286 call gate */
1715
        case 12: /* 386 call gate */
1716
            break;
1717
        default:
1718
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1719
            break;
1720
        }
1721
        shift = type >> 3;
1722

    
1723
        if (dpl < cpl || dpl < rpl)
1724
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1725
        /* check valid bit */
1726
        if (!(e2 & DESC_P_MASK))
1727
            raise_exception_err(EXCP0B_NOSEG,  new_cs & 0xfffc);
1728
        selector = e1 >> 16;
1729
        offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
1730
        param_count = e2 & 0x1f;
1731
        if ((selector & 0xfffc) == 0)
1732
            raise_exception_err(EXCP0D_GPF, 0);
1733

    
1734
        if (load_segment(&e1, &e2, selector) != 0)
1735
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1736
        if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
1737
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1738
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1739
        if (dpl > cpl)
1740
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1741
        if (!(e2 & DESC_P_MASK))
1742
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1743

    
1744
        if (!(e2 & DESC_C_MASK) && dpl < cpl) {
1745
            /* to inner priviledge */
1746
            get_ss_esp_from_tss(&ss, &sp, dpl);
1747
#ifdef DEBUG_PCALL
1748
            if (loglevel & CPU_LOG_PCALL)
1749
                fprintf(logfile, "new ss:esp=%04x:%08x param_count=%d ESP=" TARGET_FMT_lx "\n", 
1750
                        ss, sp, param_count, ESP);
1751
#endif
1752
            if ((ss & 0xfffc) == 0)
1753
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1754
            if ((ss & 3) != dpl)
1755
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1756
            if (load_segment(&ss_e1, &ss_e2, ss) != 0)
1757
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1758
            ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
1759
            if (ss_dpl != dpl)
1760
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1761
            if (!(ss_e2 & DESC_S_MASK) ||
1762
                (ss_e2 & DESC_CS_MASK) ||
1763
                !(ss_e2 & DESC_W_MASK))
1764
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1765
            if (!(ss_e2 & DESC_P_MASK))
1766
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1767
            
1768
            //            push_size = ((param_count * 2) + 8) << shift;
1769

    
1770
            old_sp_mask = get_sp_mask(env->segs[R_SS].flags);
1771
            old_ssp = env->segs[R_SS].base;
1772
            
1773
            sp_mask = get_sp_mask(ss_e2);
1774
            ssp = get_seg_base(ss_e1, ss_e2);
1775
            if (shift) {
1776
                PUSHL(ssp, sp, sp_mask, env->segs[R_SS].selector);
1777
                PUSHL(ssp, sp, sp_mask, ESP);
1778
                for(i = param_count - 1; i >= 0; i--) {
1779
                    val = ldl_kernel(old_ssp + ((ESP + i * 4) & old_sp_mask));
1780
                    PUSHL(ssp, sp, sp_mask, val);
1781
                }
1782
            } else {
1783
                PUSHW(ssp, sp, sp_mask, env->segs[R_SS].selector);
1784
                PUSHW(ssp, sp, sp_mask, ESP);
1785
                for(i = param_count - 1; i >= 0; i--) {
1786
                    val = lduw_kernel(old_ssp + ((ESP + i * 2) & old_sp_mask));
1787
                    PUSHW(ssp, sp, sp_mask, val);
1788
                }
1789
            }
1790
            new_stack = 1;
1791
        } else {
1792
            /* to same priviledge */
1793
            sp = ESP;
1794
            sp_mask = get_sp_mask(env->segs[R_SS].flags);
1795
            ssp = env->segs[R_SS].base;
1796
            //            push_size = (4 << shift);
1797
            new_stack = 0;
1798
        }
1799

    
1800
        if (shift) {
1801
            PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1802
            PUSHL(ssp, sp, sp_mask, next_eip);
1803
        } else {
1804
            PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1805
            PUSHW(ssp, sp, sp_mask, next_eip);
1806
        }
1807

    
1808
        /* from this point, not restartable */
1809

    
1810
        if (new_stack) {
1811
            ss = (ss & ~3) | dpl;
1812
            cpu_x86_load_seg_cache(env, R_SS, ss, 
1813
                                   ssp,
1814
                                   get_seg_limit(ss_e1, ss_e2),
1815
                                   ss_e2);
1816
        }
1817

    
1818
        selector = (selector & ~3) | dpl;
1819
        cpu_x86_load_seg_cache(env, R_CS, selector, 
1820
                       get_seg_base(e1, e2),
1821
                       get_seg_limit(e1, e2),
1822
                       e2);
1823
        cpu_x86_set_cpl(env, dpl);
1824
        ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1825
        EIP = offset;
1826
    }
1827
}
1828

    
1829
/* real and vm86 mode iret */
1830
void helper_iret_real(int shift)
1831
{
1832
    uint32_t sp, new_cs, new_eip, new_eflags, sp_mask;
1833
    target_ulong ssp;
1834
    int eflags_mask;
1835

    
1836
    sp_mask = 0xffff; /* XXXX: use SS segment size ? */
1837
    sp = ESP;
1838
    ssp = env->segs[R_SS].base;
1839
    if (shift == 1) {
1840
        /* 32 bits */
1841
        POPL(ssp, sp, sp_mask, new_eip);
1842
        POPL(ssp, sp, sp_mask, new_cs);
1843
        new_cs &= 0xffff;
1844
        POPL(ssp, sp, sp_mask, new_eflags);
1845
    } else {
1846
        /* 16 bits */
1847
        POPW(ssp, sp, sp_mask, new_eip);
1848
        POPW(ssp, sp, sp_mask, new_cs);
1849
        POPW(ssp, sp, sp_mask, new_eflags);
1850
    }
1851
    ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1852
    load_seg_vm(R_CS, new_cs);
1853
    env->eip = new_eip;
1854
    if (env->eflags & VM_MASK)
1855
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | RF_MASK | NT_MASK;
1856
    else
1857
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK | RF_MASK | NT_MASK;
1858
    if (shift == 0)
1859
        eflags_mask &= 0xffff;
1860
    load_eflags(new_eflags, eflags_mask);
1861
}
1862

    
1863
static inline void validate_seg(int seg_reg, int cpl)
1864
{
1865
    int dpl;
1866
    uint32_t e2;
1867
    
1868
    e2 = env->segs[seg_reg].flags;
1869
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1870
    if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1871
        /* data or non conforming code segment */
1872
        if (dpl < cpl) {
1873
            cpu_x86_load_seg_cache(env, seg_reg, 0, 0, 0, 0);
1874
        }
1875
    }
1876
}
1877

    
1878
/* protected mode iret */
1879
static inline void helper_ret_protected(int shift, int is_iret, int addend)
1880
{
1881
    uint32_t new_cs, new_eflags, new_ss;
1882
    uint32_t new_es, new_ds, new_fs, new_gs;
1883
    uint32_t e1, e2, ss_e1, ss_e2;
1884
    int cpl, dpl, rpl, eflags_mask, iopl;
1885
    target_ulong ssp, sp, new_eip, new_esp, sp_mask;
1886
    
1887
#ifdef TARGET_X86_64
1888
    if (shift == 2)
1889
        sp_mask = -1;
1890
    else
1891
#endif
1892
        sp_mask = get_sp_mask(env->segs[R_SS].flags);
1893
    sp = ESP;
1894
    ssp = env->segs[R_SS].base;
1895
    new_eflags = 0; /* avoid warning */
1896
#ifdef TARGET_X86_64
1897
    if (shift == 2) {
1898
        POPQ(sp, new_eip);
1899
        POPQ(sp, new_cs);
1900
        new_cs &= 0xffff;
1901
        if (is_iret) {
1902
            POPQ(sp, new_eflags);
1903
        }
1904
    } else
1905
#endif
1906
    if (shift == 1) {
1907
        /* 32 bits */
1908
        POPL(ssp, sp, sp_mask, new_eip);
1909
        POPL(ssp, sp, sp_mask, new_cs);
1910
        new_cs &= 0xffff;
1911
        if (is_iret) {
1912
            POPL(ssp, sp, sp_mask, new_eflags);
1913
            if (new_eflags & VM_MASK)
1914
                goto return_to_vm86;
1915
        }
1916
    } else {
1917
        /* 16 bits */
1918
        POPW(ssp, sp, sp_mask, new_eip);
1919
        POPW(ssp, sp, sp_mask, new_cs);
1920
        if (is_iret)
1921
            POPW(ssp, sp, sp_mask, new_eflags);
1922
    }
1923
#ifdef DEBUG_PCALL
1924
    if (loglevel & CPU_LOG_PCALL) {
1925
        fprintf(logfile, "lret new %04x:" TARGET_FMT_lx " s=%d addend=0x%x\n",
1926
                new_cs, new_eip, shift, addend);
1927
        cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
1928
    }
1929
#endif
1930
    if ((new_cs & 0xfffc) == 0)
1931
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1932
    if (load_segment(&e1, &e2, new_cs) != 0)
1933
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1934
    if (!(e2 & DESC_S_MASK) ||
1935
        !(e2 & DESC_CS_MASK))
1936
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1937
    cpl = env->hflags & HF_CPL_MASK;
1938
    rpl = new_cs & 3; 
1939
    if (rpl < cpl)
1940
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1941
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1942
    if (e2 & DESC_C_MASK) {
1943
        if (dpl > rpl)
1944
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1945
    } else {
1946
        if (dpl != rpl)
1947
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1948
    }
1949
    if (!(e2 & DESC_P_MASK))
1950
        raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1951
    
1952
    sp += addend;
1953
    if (rpl == cpl && (!(env->hflags & HF_CS64_MASK) || 
1954
                       ((env->hflags & HF_CS64_MASK) && !is_iret))) {
1955
        /* return to same priledge level */
1956
        cpu_x86_load_seg_cache(env, R_CS, new_cs, 
1957
                       get_seg_base(e1, e2),
1958
                       get_seg_limit(e1, e2),
1959
                       e2);
1960
    } else {
1961
        /* return to different priviledge level */
1962
#ifdef TARGET_X86_64
1963
        if (shift == 2) {
1964
            POPQ(sp, new_esp);
1965
            POPQ(sp, new_ss);
1966
            new_ss &= 0xffff;
1967
        } else
1968
#endif
1969
        if (shift == 1) {
1970
            /* 32 bits */
1971
            POPL(ssp, sp, sp_mask, new_esp);
1972
            POPL(ssp, sp, sp_mask, new_ss);
1973
            new_ss &= 0xffff;
1974
        } else {
1975
            /* 16 bits */
1976
            POPW(ssp, sp, sp_mask, new_esp);
1977
            POPW(ssp, sp, sp_mask, new_ss);
1978
        }
1979
#ifdef DEBUG_PCALL
1980
        if (loglevel & CPU_LOG_PCALL) {
1981
            fprintf(logfile, "new ss:esp=%04x:" TARGET_FMT_lx "\n",
1982
                    new_ss, new_esp);
1983
        }
1984
#endif
1985
        if ((env->hflags & HF_LMA_MASK) && (new_ss & 0xfffc) == 0) {
1986
            /* NULL ss is allowed in long mode */
1987
            cpu_x86_load_seg_cache(env, R_SS, new_ss, 
1988
                                   0, 0xffffffff,
1989
                                   DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1990
                                   DESC_S_MASK | (rpl << DESC_DPL_SHIFT) |
1991
                                   DESC_W_MASK | DESC_A_MASK);
1992
        } else {
1993
            if ((new_ss & 3) != rpl)
1994
                raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1995
            if (load_segment(&ss_e1, &ss_e2, new_ss) != 0)
1996
                raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1997
            if (!(ss_e2 & DESC_S_MASK) ||
1998
                (ss_e2 & DESC_CS_MASK) ||
1999
                !(ss_e2 & DESC_W_MASK))
2000
                raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
2001
            dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
2002
            if (dpl != rpl)
2003
                raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
2004
            if (!(ss_e2 & DESC_P_MASK))
2005
                raise_exception_err(EXCP0B_NOSEG, new_ss & 0xfffc);
2006
            cpu_x86_load_seg_cache(env, R_SS, new_ss, 
2007
                                   get_seg_base(ss_e1, ss_e2),
2008
                                   get_seg_limit(ss_e1, ss_e2),
2009
                                   ss_e2);
2010
        }
2011

    
2012
        cpu_x86_load_seg_cache(env, R_CS, new_cs, 
2013
                       get_seg_base(e1, e2),
2014
                       get_seg_limit(e1, e2),
2015
                       e2);
2016
        cpu_x86_set_cpl(env, rpl);
2017
        sp = new_esp;
2018
#ifdef TARGET_X86_64
2019
        if (shift == 2)
2020
            sp_mask = -1;
2021
        else
2022
#endif
2023
            sp_mask = get_sp_mask(ss_e2);
2024

    
2025
        /* validate data segments */
2026
        validate_seg(R_ES, cpl);
2027
        validate_seg(R_DS, cpl);
2028
        validate_seg(R_FS, cpl);
2029
        validate_seg(R_GS, cpl);
2030

    
2031
        sp += addend;
2032
    }
2033
    ESP = (ESP & ~sp_mask) | (sp & sp_mask);
2034
    env->eip = new_eip;
2035
    if (is_iret) {
2036
        /* NOTE: 'cpl' is the _old_ CPL */
2037
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | RF_MASK | NT_MASK;
2038
        if (cpl == 0)
2039
            eflags_mask |= IOPL_MASK;
2040
        iopl = (env->eflags >> IOPL_SHIFT) & 3;
2041
        if (cpl <= iopl)
2042
            eflags_mask |= IF_MASK;
2043
        if (shift == 0)
2044
            eflags_mask &= 0xffff;
2045
        load_eflags(new_eflags, eflags_mask);
2046
    }
2047
    return;
2048

    
2049
 return_to_vm86:
2050
    POPL(ssp, sp, sp_mask, new_esp);
2051
    POPL(ssp, sp, sp_mask, new_ss);
2052
    POPL(ssp, sp, sp_mask, new_es);
2053
    POPL(ssp, sp, sp_mask, new_ds);
2054
    POPL(ssp, sp, sp_mask, new_fs);
2055
    POPL(ssp, sp, sp_mask, new_gs);
2056
    
2057
    /* modify processor state */
2058
    load_eflags(new_eflags, TF_MASK | AC_MASK | ID_MASK | 
2059
                IF_MASK | IOPL_MASK | VM_MASK | NT_MASK | VIF_MASK | VIP_MASK);
2060
    load_seg_vm(R_CS, new_cs & 0xffff);
2061
    cpu_x86_set_cpl(env, 3);
2062
    load_seg_vm(R_SS, new_ss & 0xffff);
2063
    load_seg_vm(R_ES, new_es & 0xffff);
2064
    load_seg_vm(R_DS, new_ds & 0xffff);
2065
    load_seg_vm(R_FS, new_fs & 0xffff);
2066
    load_seg_vm(R_GS, new_gs & 0xffff);
2067

    
2068
    env->eip = new_eip & 0xffff;
2069
    ESP = new_esp;
2070
}
2071

    
2072
void helper_iret_protected(int shift, int next_eip)
2073
{
2074
    int tss_selector, type;
2075
    uint32_t e1, e2;
2076
    
2077
    /* specific case for TSS */
2078
    if (env->eflags & NT_MASK) {
2079
#ifdef TARGET_X86_64
2080
        if (env->hflags & HF_LMA_MASK)
2081
            raise_exception_err(EXCP0D_GPF, 0);
2082
#endif
2083
        tss_selector = lduw_kernel(env->tr.base + 0);
2084
        if (tss_selector & 4)
2085
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
2086
        if (load_segment(&e1, &e2, tss_selector) != 0)
2087
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
2088
        type = (e2 >> DESC_TYPE_SHIFT) & 0x17;
2089
        /* NOTE: we check both segment and busy TSS */
2090
        if (type != 3)
2091
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
2092
        switch_tss(tss_selector, e1, e2, SWITCH_TSS_IRET, next_eip);
2093
    } else {
2094
        helper_ret_protected(shift, 1, 0);
2095
    }
2096
}
2097

    
2098
void helper_lret_protected(int shift, int addend)
2099
{
2100
    helper_ret_protected(shift, 0, addend);
2101
}
2102

    
2103
void helper_sysenter(void)
2104
{
2105
    if (env->sysenter_cs == 0) {
2106
        raise_exception_err(EXCP0D_GPF, 0);
2107
    }
2108
    env->eflags &= ~(VM_MASK | IF_MASK | RF_MASK);
2109
    cpu_x86_set_cpl(env, 0);
2110
    cpu_x86_load_seg_cache(env, R_CS, env->sysenter_cs & 0xfffc, 
2111
                           0, 0xffffffff, 
2112
                           DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2113
                           DESC_S_MASK |
2114
                           DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
2115
    cpu_x86_load_seg_cache(env, R_SS, (env->sysenter_cs + 8) & 0xfffc, 
2116
                           0, 0xffffffff,
2117
                           DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2118
                           DESC_S_MASK |
2119
                           DESC_W_MASK | DESC_A_MASK);
2120
    ESP = env->sysenter_esp;
2121
    EIP = env->sysenter_eip;
2122
}
2123

    
2124
void helper_sysexit(void)
2125
{
2126
    int cpl;
2127

    
2128
    cpl = env->hflags & HF_CPL_MASK;
2129
    if (env->sysenter_cs == 0 || cpl != 0) {
2130
        raise_exception_err(EXCP0D_GPF, 0);
2131
    }
2132
    cpu_x86_set_cpl(env, 3);
2133
    cpu_x86_load_seg_cache(env, R_CS, ((env->sysenter_cs + 16) & 0xfffc) | 3, 
2134
                           0, 0xffffffff, 
2135
                           DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2136
                           DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
2137
                           DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
2138
    cpu_x86_load_seg_cache(env, R_SS, ((env->sysenter_cs + 24) & 0xfffc) | 3, 
2139
                           0, 0xffffffff,
2140
                           DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2141
                           DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
2142
                           DESC_W_MASK | DESC_A_MASK);
2143
    ESP = ECX;
2144
    EIP = EDX;
2145
}
2146

    
2147
void helper_movl_crN_T0(int reg)
2148
{
2149
    switch(reg) {
2150
    case 0:
2151
        cpu_x86_update_cr0(env, T0);
2152
        break;
2153
    case 3:
2154
        cpu_x86_update_cr3(env, T0);
2155
        break;
2156
    case 4:
2157
        cpu_x86_update_cr4(env, T0);
2158
        break;
2159
    default:
2160
        env->cr[reg] = T0;
2161
        break;
2162
    }
2163
}
2164

    
2165
/* XXX: do more */
2166
void helper_movl_drN_T0(int reg)
2167
{
2168
    env->dr[reg] = T0;
2169
}
2170

    
2171
void helper_invlpg(unsigned int addr)
2172
{
2173
    cpu_x86_flush_tlb(env, addr);
2174
}
2175

    
2176
void helper_rdtsc(void)
2177
{
2178
    uint64_t val;
2179
    
2180
    val = cpu_get_tsc(env);
2181
    EAX = (uint32_t)(val);
2182
    EDX = (uint32_t)(val >> 32);
2183
}
2184

    
2185
#if defined(CONFIG_USER_ONLY) 
2186
void helper_wrmsr(void)
2187
{
2188
}
2189

    
2190
void helper_rdmsr(void)
2191
{
2192
}
2193
#else
2194
void helper_wrmsr(void)
2195
{
2196
    uint64_t val;
2197

    
2198
    val = ((uint32_t)EAX) | ((uint64_t)((uint32_t)EDX) << 32);
2199

    
2200
    switch((uint32_t)ECX) {
2201
    case MSR_IA32_SYSENTER_CS:
2202
        env->sysenter_cs = val & 0xffff;
2203
        break;
2204
    case MSR_IA32_SYSENTER_ESP:
2205
        env->sysenter_esp = val;
2206
        break;
2207
    case MSR_IA32_SYSENTER_EIP:
2208
        env->sysenter_eip = val;
2209
        break;
2210
    case MSR_IA32_APICBASE:
2211
        cpu_set_apic_base(env, val);
2212
        break;
2213
#ifdef TARGET_X86_64
2214
    case MSR_EFER:
2215
#define MSR_EFER_UPDATE_MASK (MSR_EFER_SCE | MSR_EFER_LME | \
2216
                              MSR_EFER_NXE | MSR_EFER_FFXSR)
2217
        env->efer = (env->efer & ~MSR_EFER_UPDATE_MASK) | 
2218
            (val & MSR_EFER_UPDATE_MASK);
2219
        break;
2220
    case MSR_STAR:
2221
        env->star = val;
2222
        break;
2223
    case MSR_LSTAR:
2224
        env->lstar = val;
2225
        break;
2226
    case MSR_CSTAR:
2227
        env->cstar = val;
2228
        break;
2229
    case MSR_FMASK:
2230
        env->fmask = val;
2231
        break;
2232
    case MSR_FSBASE:
2233
        env->segs[R_FS].base = val;
2234
        break;
2235
    case MSR_GSBASE:
2236
        env->segs[R_GS].base = val;
2237
        break;
2238
    case MSR_KERNELGSBASE:
2239
        env->kernelgsbase = val;
2240
        break;
2241
#endif
2242
    default:
2243
        /* XXX: exception ? */
2244
        break; 
2245
    }
2246
}
2247

    
2248
void helper_rdmsr(void)
2249
{
2250
    uint64_t val;
2251
    switch((uint32_t)ECX) {
2252
    case MSR_IA32_SYSENTER_CS:
2253
        val = env->sysenter_cs;
2254
        break;
2255
    case MSR_IA32_SYSENTER_ESP:
2256
        val = env->sysenter_esp;
2257
        break;
2258
    case MSR_IA32_SYSENTER_EIP:
2259
        val = env->sysenter_eip;
2260
        break;
2261
    case MSR_IA32_APICBASE:
2262
        val = cpu_get_apic_base(env);
2263
        break;
2264
#ifdef TARGET_X86_64
2265
    case MSR_EFER:
2266
        val = env->efer;
2267
        break;
2268
    case MSR_STAR:
2269
        val = env->star;
2270
        break;
2271
    case MSR_LSTAR:
2272
        val = env->lstar;
2273
        break;
2274
    case MSR_CSTAR:
2275
        val = env->cstar;
2276
        break;
2277
    case MSR_FMASK:
2278
        val = env->fmask;
2279
        break;
2280
    case MSR_FSBASE:
2281
        val = env->segs[R_FS].base;
2282
        break;
2283
    case MSR_GSBASE:
2284
        val = env->segs[R_GS].base;
2285
        break;
2286
    case MSR_KERNELGSBASE:
2287
        val = env->kernelgsbase;
2288
        break;
2289
#endif
2290
    default:
2291
        /* XXX: exception ? */
2292
        val = 0;
2293
        break; 
2294
    }
2295
    EAX = (uint32_t)(val);
2296
    EDX = (uint32_t)(val >> 32);
2297
}
2298
#endif
2299

    
2300
void helper_lsl(void)
2301
{
2302
    unsigned int selector, limit;
2303
    uint32_t e1, e2;
2304
    int rpl, dpl, cpl, type;
2305

    
2306
    CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2307
    selector = T0 & 0xffff;
2308
    if (load_segment(&e1, &e2, selector) != 0)
2309
        return;
2310
    rpl = selector & 3;
2311
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2312
    cpl = env->hflags & HF_CPL_MASK;
2313
    if (e2 & DESC_S_MASK) {
2314
        if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
2315
            /* conforming */
2316
        } else {
2317
            if (dpl < cpl || dpl < rpl)
2318
                return;
2319
        }
2320
    } else {
2321
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
2322
        switch(type) {
2323
        case 1:
2324
        case 2:
2325
        case 3:
2326
        case 9:
2327
        case 11:
2328
            break;
2329
        default:
2330
            return;
2331
        }
2332
        if (dpl < cpl || dpl < rpl)
2333
            return;
2334
    }
2335
    limit = get_seg_limit(e1, e2);
2336
    T1 = limit;
2337
    CC_SRC |= CC_Z;
2338
}
2339

    
2340
void helper_lar(void)
2341
{
2342
    unsigned int selector;
2343
    uint32_t e1, e2;
2344
    int rpl, dpl, cpl, type;
2345

    
2346
    CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2347
    selector = T0 & 0xffff;
2348
    if ((selector & 0xfffc) == 0)
2349
        return;
2350
    if (load_segment(&e1, &e2, selector) != 0)
2351
        return;
2352
    rpl = selector & 3;
2353
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2354
    cpl = env->hflags & HF_CPL_MASK;
2355
    if (e2 & DESC_S_MASK) {
2356
        if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
2357
            /* conforming */
2358
        } else {
2359
            if (dpl < cpl || dpl < rpl)
2360
                return;
2361
        }
2362
    } else {
2363
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
2364
        switch(type) {
2365
        case 1:
2366
        case 2:
2367
        case 3:
2368
        case 4:
2369
        case 5:
2370
        case 9:
2371
        case 11:
2372
        case 12:
2373
            break;
2374
        default:
2375
            return;
2376
        }
2377
        if (dpl < cpl || dpl < rpl)
2378
            return;
2379
    }
2380
    T1 = e2 & 0x00f0ff00;
2381
    CC_SRC |= CC_Z;
2382
}
2383

    
2384
void helper_verr(void)
2385
{
2386
    unsigned int selector;
2387
    uint32_t e1, e2;
2388
    int rpl, dpl, cpl;
2389

    
2390
    CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2391
    selector = T0 & 0xffff;
2392
    if ((selector & 0xfffc) == 0)
2393
        return;
2394
    if (load_segment(&e1, &e2, selector) != 0)
2395
        return;
2396
    if (!(e2 & DESC_S_MASK))
2397
        return;
2398
    rpl = selector & 3;
2399
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2400
    cpl = env->hflags & HF_CPL_MASK;
2401
    if (e2 & DESC_CS_MASK) {
2402
        if (!(e2 & DESC_R_MASK))
2403
            return;
2404
        if (!(e2 & DESC_C_MASK)) {
2405
            if (dpl < cpl || dpl < rpl)
2406
                return;
2407
        }
2408
    } else {
2409
        if (dpl < cpl || dpl < rpl)
2410
            return;
2411
    }
2412
    CC_SRC |= CC_Z;
2413
}
2414

    
2415
void helper_verw(void)
2416
{
2417
    unsigned int selector;
2418
    uint32_t e1, e2;
2419
    int rpl, dpl, cpl;
2420

    
2421
    CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
2422
    selector = T0 & 0xffff;
2423
    if ((selector & 0xfffc) == 0)
2424
        return;
2425
    if (load_segment(&e1, &e2, selector) != 0)
2426
        return;
2427
    if (!(e2 & DESC_S_MASK))
2428
        return;
2429
    rpl = selector & 3;
2430
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2431
    cpl = env->hflags & HF_CPL_MASK;
2432
    if (e2 & DESC_CS_MASK) {
2433
        return;
2434
    } else {
2435
        if (dpl < cpl || dpl < rpl)
2436
            return;
2437
        if (!(e2 & DESC_W_MASK))
2438
            return;
2439
    }
2440
    CC_SRC |= CC_Z;
2441
}
2442

    
2443
/* FPU helpers */
2444

    
2445
void helper_fldt_ST0_A0(void)
2446
{
2447
    int new_fpstt;
2448
    new_fpstt = (env->fpstt - 1) & 7;
2449
    env->fpregs[new_fpstt].d = helper_fldt(A0);
2450
    env->fpstt = new_fpstt;
2451
    env->fptags[new_fpstt] = 0; /* validate stack entry */
2452
}
2453

    
2454
void helper_fstt_ST0_A0(void)
2455
{
2456
    helper_fstt(ST0, A0);
2457
}
2458

    
2459
void fpu_set_exception(int mask)
2460
{
2461
    env->fpus |= mask;
2462
    if (env->fpus & (~env->fpuc & FPUC_EM))
2463
        env->fpus |= FPUS_SE | FPUS_B;
2464
}
2465

    
2466
CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b)
2467
{
2468
    if (b == 0.0) 
2469
        fpu_set_exception(FPUS_ZE);
2470
    return a / b;
2471
}
2472

    
2473
void fpu_raise_exception(void)
2474
{
2475
    if (env->cr[0] & CR0_NE_MASK) {
2476
        raise_exception(EXCP10_COPR);
2477
    } 
2478
#if !defined(CONFIG_USER_ONLY) 
2479
    else {
2480
        cpu_set_ferr(env);
2481
    }
2482
#endif
2483
}
2484

    
2485
/* BCD ops */
2486

    
2487
void helper_fbld_ST0_A0(void)
2488
{
2489
    CPU86_LDouble tmp;
2490
    uint64_t val;
2491
    unsigned int v;
2492
    int i;
2493

    
2494
    val = 0;
2495
    for(i = 8; i >= 0; i--) {
2496
        v = ldub(A0 + i);
2497
        val = (val * 100) + ((v >> 4) * 10) + (v & 0xf);
2498
    }
2499
    tmp = val;
2500
    if (ldub(A0 + 9) & 0x80)
2501
        tmp = -tmp;
2502
    fpush();
2503
    ST0 = tmp;
2504
}
2505

    
2506
void helper_fbst_ST0_A0(void)
2507
{
2508
    CPU86_LDouble tmp;
2509
    int v;
2510
    target_ulong mem_ref, mem_end;
2511
    int64_t val;
2512

    
2513
    tmp = rint(ST0);
2514
    val = (int64_t)tmp;
2515
    mem_ref = A0;
2516
    mem_end = mem_ref + 9;
2517
    if (val < 0) {
2518
        stb(mem_end, 0x80);
2519
        val = -val;
2520
    } else {
2521
        stb(mem_end, 0x00);
2522
    }
2523
    while (mem_ref < mem_end) {
2524
        if (val == 0)
2525
            break;
2526
        v = val % 100;
2527
        val = val / 100;
2528
        v = ((v / 10) << 4) | (v % 10);
2529
        stb(mem_ref++, v);
2530
    }
2531
    while (mem_ref < mem_end) {
2532
        stb(mem_ref++, 0);
2533
    }
2534
}
2535

    
2536
void helper_f2xm1(void)
2537
{
2538
    ST0 = pow(2.0,ST0) - 1.0;
2539
}
2540

    
2541
void helper_fyl2x(void)
2542
{
2543
    CPU86_LDouble fptemp;
2544
    
2545
    fptemp = ST0;
2546
    if (fptemp>0.0){
2547
        fptemp = log(fptemp)/log(2.0);         /* log2(ST) */
2548
        ST1 *= fptemp;
2549
        fpop();
2550
    } else { 
2551
        env->fpus &= (~0x4700);
2552
        env->fpus |= 0x400;
2553
    }
2554
}
2555

    
2556
void helper_fptan(void)
2557
{
2558
    CPU86_LDouble fptemp;
2559

    
2560
    fptemp = ST0;
2561
    if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2562
        env->fpus |= 0x400;
2563
    } else {
2564
        ST0 = tan(fptemp);
2565
        fpush();
2566
        ST0 = 1.0;
2567
        env->fpus &= (~0x400);  /* C2 <-- 0 */
2568
        /* the above code is for  |arg| < 2**52 only */
2569
    }
2570
}
2571

    
2572
void helper_fpatan(void)
2573
{
2574
    CPU86_LDouble fptemp, fpsrcop;
2575

    
2576
    fpsrcop = ST1;
2577
    fptemp = ST0;
2578
    ST1 = atan2(fpsrcop,fptemp);
2579
    fpop();
2580
}
2581

    
2582
void helper_fxtract(void)
2583
{
2584
    CPU86_LDoubleU temp;
2585
    unsigned int expdif;
2586

    
2587
    temp.d = ST0;
2588
    expdif = EXPD(temp) - EXPBIAS;
2589
    /*DP exponent bias*/
2590
    ST0 = expdif;
2591
    fpush();
2592
    BIASEXPONENT(temp);
2593
    ST0 = temp.d;
2594
}
2595

    
2596
void helper_fprem1(void)
2597
{
2598
    CPU86_LDouble dblq, fpsrcop, fptemp;
2599
    CPU86_LDoubleU fpsrcop1, fptemp1;
2600
    int expdif;
2601
    int q;
2602

    
2603
    fpsrcop = ST0;
2604
    fptemp = ST1;
2605
    fpsrcop1.d = fpsrcop;
2606
    fptemp1.d = fptemp;
2607
    expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2608
    if (expdif < 53) {
2609
        dblq = fpsrcop / fptemp;
2610
        dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2611
        ST0 = fpsrcop - fptemp*dblq;
2612
        q = (int)dblq; /* cutting off top bits is assumed here */
2613
        env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2614
                                /* (C0,C1,C3) <-- (q2,q1,q0) */
2615
        env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2616
        env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2617
        env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2618
    } else {
2619
        env->fpus |= 0x400;  /* C2 <-- 1 */
2620
        fptemp = pow(2.0, expdif-50);
2621
        fpsrcop = (ST0 / ST1) / fptemp;
2622
        /* fpsrcop = integer obtained by rounding to the nearest */
2623
        fpsrcop = (fpsrcop-floor(fpsrcop) < ceil(fpsrcop)-fpsrcop)?
2624
            floor(fpsrcop): ceil(fpsrcop);
2625
        ST0 -= (ST1 * fpsrcop * fptemp);
2626
    }
2627
}
2628

    
2629
void helper_fprem(void)
2630
{
2631
    CPU86_LDouble dblq, fpsrcop, fptemp;
2632
    CPU86_LDoubleU fpsrcop1, fptemp1;
2633
    int expdif;
2634
    int q;
2635
    
2636
    fpsrcop = ST0;
2637
    fptemp = ST1;
2638
    fpsrcop1.d = fpsrcop;
2639
    fptemp1.d = fptemp;
2640
    expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2641
    if ( expdif < 53 ) {
2642
        dblq = fpsrcop / fptemp;
2643
        dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2644
        ST0 = fpsrcop - fptemp*dblq;
2645
        q = (int)dblq; /* cutting off top bits is assumed here */
2646
        env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2647
                                /* (C0,C1,C3) <-- (q2,q1,q0) */
2648
        env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2649
        env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2650
        env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2651
    } else {
2652
        env->fpus |= 0x400;  /* C2 <-- 1 */
2653
        fptemp = pow(2.0, expdif-50);
2654
        fpsrcop = (ST0 / ST1) / fptemp;
2655
        /* fpsrcop = integer obtained by chopping */
2656
        fpsrcop = (fpsrcop < 0.0)?
2657
            -(floor(fabs(fpsrcop))): floor(fpsrcop);
2658
        ST0 -= (ST1 * fpsrcop * fptemp);
2659
    }
2660
}
2661

    
2662
void helper_fyl2xp1(void)
2663
{
2664
    CPU86_LDouble fptemp;
2665

    
2666
    fptemp = ST0;
2667
    if ((fptemp+1.0)>0.0) {
2668
        fptemp = log(fptemp+1.0) / log(2.0); /* log2(ST+1.0) */
2669
        ST1 *= fptemp;
2670
        fpop();
2671
    } else { 
2672
        env->fpus &= (~0x4700);
2673
        env->fpus |= 0x400;
2674
    }
2675
}
2676

    
2677
void helper_fsqrt(void)
2678
{
2679
    CPU86_LDouble fptemp;
2680

    
2681
    fptemp = ST0;
2682
    if (fptemp<0.0) { 
2683
        env->fpus &= (~0x4700);  /* (C3,C2,C1,C0) <-- 0000 */
2684
        env->fpus |= 0x400;
2685
    }
2686
    ST0 = sqrt(fptemp);
2687
}
2688

    
2689
void helper_fsincos(void)
2690
{
2691
    CPU86_LDouble fptemp;
2692

    
2693
    fptemp = ST0;
2694
    if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2695
        env->fpus |= 0x400;
2696
    } else {
2697
        ST0 = sin(fptemp);
2698
        fpush();
2699
        ST0 = cos(fptemp);
2700
        env->fpus &= (~0x400);  /* C2 <-- 0 */
2701
        /* the above code is for  |arg| < 2**63 only */
2702
    }
2703
}
2704

    
2705
void helper_frndint(void)
2706
{
2707
    CPU86_LDouble a;
2708

    
2709
    a = ST0;
2710
#ifdef __arm__
2711
    switch(env->fpuc & RC_MASK) {
2712
    default:
2713
    case RC_NEAR:
2714
        asm("rndd %0, %1" : "=f" (a) : "f"(a));
2715
        break;
2716
    case RC_DOWN:
2717
        asm("rnddm %0, %1" : "=f" (a) : "f"(a));
2718
        break;
2719
    case RC_UP:
2720
        asm("rnddp %0, %1" : "=f" (a) : "f"(a));
2721
        break;
2722
    case RC_CHOP:
2723
        asm("rnddz %0, %1" : "=f" (a) : "f"(a));
2724
        break;
2725
    }
2726
#else
2727
    a = rint(a);
2728
#endif
2729
    ST0 = a;
2730
}
2731

    
2732
void helper_fscale(void)
2733
{
2734
    CPU86_LDouble fpsrcop, fptemp;
2735

    
2736
    fpsrcop = 2.0;
2737
    fptemp = pow(fpsrcop,ST1);
2738
    ST0 *= fptemp;
2739
}
2740

    
2741
void helper_fsin(void)
2742
{
2743
    CPU86_LDouble fptemp;
2744

    
2745
    fptemp = ST0;
2746
    if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2747
        env->fpus |= 0x400;
2748
    } else {
2749
        ST0 = sin(fptemp);
2750
        env->fpus &= (~0x400);  /* C2 <-- 0 */
2751
        /* the above code is for  |arg| < 2**53 only */
2752
    }
2753
}
2754

    
2755
void helper_fcos(void)
2756
{
2757
    CPU86_LDouble fptemp;
2758

    
2759
    fptemp = ST0;
2760
    if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2761
        env->fpus |= 0x400;
2762
    } else {
2763
        ST0 = cos(fptemp);
2764
        env->fpus &= (~0x400);  /* C2 <-- 0 */
2765
        /* the above code is for  |arg5 < 2**63 only */
2766
    }
2767
}
2768

    
2769
void helper_fxam_ST0(void)
2770
{
2771
    CPU86_LDoubleU temp;
2772
    int expdif;
2773

    
2774
    temp.d = ST0;
2775

    
2776
    env->fpus &= (~0x4700);  /* (C3,C2,C1,C0) <-- 0000 */
2777
    if (SIGND(temp))
2778
        env->fpus |= 0x200; /* C1 <-- 1 */
2779

    
2780
    expdif = EXPD(temp);
2781
    if (expdif == MAXEXPD) {
2782
        if (MANTD(temp) == 0)
2783
            env->fpus |=  0x500 /*Infinity*/;
2784
        else
2785
            env->fpus |=  0x100 /*NaN*/;
2786
    } else if (expdif == 0) {
2787
        if (MANTD(temp) == 0)
2788
            env->fpus |=  0x4000 /*Zero*/;
2789
        else
2790
            env->fpus |= 0x4400 /*Denormal*/;
2791
    } else {
2792
        env->fpus |= 0x400;
2793
    }
2794
}
2795

    
2796
void helper_fstenv(target_ulong ptr, int data32)
2797
{
2798
    int fpus, fptag, exp, i;
2799
    uint64_t mant;
2800
    CPU86_LDoubleU tmp;
2801

    
2802
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2803
    fptag = 0;
2804
    for (i=7; i>=0; i--) {
2805
        fptag <<= 2;
2806
        if (env->fptags[i]) {
2807
            fptag |= 3;
2808
        } else {
2809
            tmp.d = env->fpregs[i].d;
2810
            exp = EXPD(tmp);
2811
            mant = MANTD(tmp);
2812
            if (exp == 0 && mant == 0) {
2813
                /* zero */
2814
                fptag |= 1;
2815
            } else if (exp == 0 || exp == MAXEXPD
2816
#ifdef USE_X86LDOUBLE
2817
                       || (mant & (1LL << 63)) == 0
2818
#endif
2819
                       ) {
2820
                /* NaNs, infinity, denormal */
2821
                fptag |= 2;
2822
            }
2823
        }
2824
    }
2825
    if (data32) {
2826
        /* 32 bit */
2827
        stl(ptr, env->fpuc);
2828
        stl(ptr + 4, fpus);
2829
        stl(ptr + 8, fptag);
2830
        stl(ptr + 12, 0); /* fpip */
2831
        stl(ptr + 16, 0); /* fpcs */
2832
        stl(ptr + 20, 0); /* fpoo */
2833
        stl(ptr + 24, 0); /* fpos */
2834
    } else {
2835
        /* 16 bit */
2836
        stw(ptr, env->fpuc);
2837
        stw(ptr + 2, fpus);
2838
        stw(ptr + 4, fptag);
2839
        stw(ptr + 6, 0);
2840
        stw(ptr + 8, 0);
2841
        stw(ptr + 10, 0);
2842
        stw(ptr + 12, 0);
2843
    }
2844
}
2845

    
2846
void helper_fldenv(target_ulong ptr, int data32)
2847
{
2848
    int i, fpus, fptag;
2849

    
2850
    if (data32) {
2851
        env->fpuc = lduw(ptr);
2852
        fpus = lduw(ptr + 4);
2853
        fptag = lduw(ptr + 8);
2854
    }
2855
    else {
2856
        env->fpuc = lduw(ptr);
2857
        fpus = lduw(ptr + 2);
2858
        fptag = lduw(ptr + 4);
2859
    }
2860
    env->fpstt = (fpus >> 11) & 7;
2861
    env->fpus = fpus & ~0x3800;
2862
    for(i = 0;i < 8; i++) {
2863
        env->fptags[i] = ((fptag & 3) == 3);
2864
        fptag >>= 2;
2865
    }
2866
}
2867

    
2868
void helper_fsave(target_ulong ptr, int data32)
2869
{
2870
    CPU86_LDouble tmp;
2871
    int i;
2872

    
2873
    helper_fstenv(ptr, data32);
2874

    
2875
    ptr += (14 << data32);
2876
    for(i = 0;i < 8; i++) {
2877
        tmp = ST(i);
2878
        helper_fstt(tmp, ptr);
2879
        ptr += 10;
2880
    }
2881

    
2882
    /* fninit */
2883
    env->fpus = 0;
2884
    env->fpstt = 0;
2885
    env->fpuc = 0x37f;
2886
    env->fptags[0] = 1;
2887
    env->fptags[1] = 1;
2888
    env->fptags[2] = 1;
2889
    env->fptags[3] = 1;
2890
    env->fptags[4] = 1;
2891
    env->fptags[5] = 1;
2892
    env->fptags[6] = 1;
2893
    env->fptags[7] = 1;
2894
}
2895

    
2896
void helper_frstor(target_ulong ptr, int data32)
2897
{
2898
    CPU86_LDouble tmp;
2899
    int i;
2900

    
2901
    helper_fldenv(ptr, data32);
2902
    ptr += (14 << data32);
2903

    
2904
    for(i = 0;i < 8; i++) {
2905
        tmp = helper_fldt(ptr);
2906
        ST(i) = tmp;
2907
        ptr += 10;
2908
    }
2909
}
2910

    
2911
void helper_fxsave(target_ulong ptr, int data64)
2912
{
2913
    int fpus, fptag, i, nb_xmm_regs;
2914
    CPU86_LDouble tmp;
2915
    target_ulong addr;
2916

    
2917
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2918
    fptag = 0;
2919
    for(i = 0; i < 8; i++) {
2920
        fptag |= (env->fptags[i] << i);
2921
    }
2922
    stw(ptr, env->fpuc);
2923
    stw(ptr + 2, fpus);
2924
    stw(ptr + 4, fptag ^ 0xff);
2925

    
2926
    addr = ptr + 0x20;
2927
    for(i = 0;i < 8; i++) {
2928
        tmp = ST(i);
2929
        helper_fstt(tmp, addr);
2930
        addr += 16;
2931
    }
2932
    
2933
    if (env->cr[4] & CR4_OSFXSR_MASK) {
2934
        /* XXX: finish it */
2935
        stl(ptr + 0x18, env->mxcsr); /* mxcsr */
2936
        stl(ptr + 0x1c, 0x0000ffff); /* mxcsr_mask */
2937
        nb_xmm_regs = 8 << data64;
2938
        addr = ptr + 0xa0;
2939
        for(i = 0; i < nb_xmm_regs; i++) {
2940
            stq(addr, env->xmm_regs[i].XMM_Q(0));
2941
            stq(addr + 8, env->xmm_regs[i].XMM_Q(1));
2942
            addr += 16;
2943
        }
2944
    }
2945
}
2946

    
2947
void helper_fxrstor(target_ulong ptr, int data64)
2948
{
2949
    int i, fpus, fptag, nb_xmm_regs;
2950
    CPU86_LDouble tmp;
2951
    target_ulong addr;
2952

    
2953
    env->fpuc = lduw(ptr);
2954
    fpus = lduw(ptr + 2);
2955
    fptag = lduw(ptr + 4);
2956
    env->fpstt = (fpus >> 11) & 7;
2957
    env->fpus = fpus & ~0x3800;
2958
    fptag ^= 0xff;
2959
    for(i = 0;i < 8; i++) {
2960
        env->fptags[i] = ((fptag >> i) & 1);
2961
    }
2962

    
2963
    addr = ptr + 0x20;
2964
    for(i = 0;i < 8; i++) {
2965
        tmp = helper_fldt(addr);
2966
        ST(i) = tmp;
2967
        addr += 16;
2968
    }
2969

    
2970
    if (env->cr[4] & CR4_OSFXSR_MASK) {
2971
        /* XXX: finish it, endianness */
2972
        env->mxcsr = ldl(ptr + 0x18);
2973
        //ldl(ptr + 0x1c);
2974
        nb_xmm_regs = 8 << data64;
2975
        addr = ptr + 0xa0;
2976
        for(i = 0; i < nb_xmm_regs; i++) {
2977
            env->xmm_regs[i].XMM_Q(0) = ldq(addr);
2978
            env->xmm_regs[i].XMM_Q(1) = ldq(addr + 8);
2979
            addr += 16;
2980
        }
2981
    }
2982
}
2983

    
2984
#ifndef USE_X86LDOUBLE
2985

    
2986
void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f)
2987
{
2988
    CPU86_LDoubleU temp;
2989
    int e;
2990

    
2991
    temp.d = f;
2992
    /* mantissa */
2993
    *pmant = (MANTD(temp) << 11) | (1LL << 63);
2994
    /* exponent + sign */
2995
    e = EXPD(temp) - EXPBIAS + 16383;
2996
    e |= SIGND(temp) >> 16;
2997
    *pexp = e;
2998
}
2999

    
3000
CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper)
3001
{
3002
    CPU86_LDoubleU temp;
3003
    int e;
3004
    uint64_t ll;
3005

    
3006
    /* XXX: handle overflow ? */
3007
    e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
3008
    e |= (upper >> 4) & 0x800; /* sign */
3009
    ll = (mant >> 11) & ((1LL << 52) - 1);
3010
#ifdef __arm__
3011
    temp.l.upper = (e << 20) | (ll >> 32);
3012
    temp.l.lower = ll;
3013
#else
3014
    temp.ll = ll | ((uint64_t)e << 52);
3015
#endif
3016
    return temp.d;
3017
}
3018

    
3019
#else
3020

    
3021
void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f)
3022
{
3023
    CPU86_LDoubleU temp;
3024

    
3025
    temp.d = f;
3026
    *pmant = temp.l.lower;
3027
    *pexp = temp.l.upper;
3028
}
3029

    
3030
CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper)
3031
{
3032
    CPU86_LDoubleU temp;
3033

    
3034
    temp.l.upper = upper;
3035
    temp.l.lower = mant;
3036
    return temp.d;
3037
}
3038
#endif
3039

    
3040
#ifdef TARGET_X86_64
3041

    
3042
//#define DEBUG_MULDIV
3043

    
3044
static void add128(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
3045
{
3046
    *plow += a;
3047
    /* carry test */
3048
    if (*plow < a)
3049
        (*phigh)++;
3050
    *phigh += b;
3051
}
3052

    
3053
static void neg128(uint64_t *plow, uint64_t *phigh)
3054
{
3055
    *plow = ~ *plow;
3056
    *phigh = ~ *phigh;
3057
    add128(plow, phigh, 1, 0);
3058
}
3059

    
3060
static void mul64(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
3061
{
3062
    uint32_t a0, a1, b0, b1;
3063
    uint64_t v;
3064

    
3065
    a0 = a;
3066
    a1 = a >> 32;
3067

    
3068
    b0 = b;
3069
    b1 = b >> 32;
3070
    
3071
    v = (uint64_t)a0 * (uint64_t)b0;
3072
    *plow = v;
3073
    *phigh = 0;
3074

    
3075
    v = (uint64_t)a0 * (uint64_t)b1;
3076
    add128(plow, phigh, v << 32, v >> 32);
3077
    
3078
    v = (uint64_t)a1 * (uint64_t)b0;
3079
    add128(plow, phigh, v << 32, v >> 32);
3080
    
3081
    v = (uint64_t)a1 * (uint64_t)b1;
3082
    *phigh += v;
3083
#ifdef DEBUG_MULDIV
3084
    printf("mul: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
3085
           a, b, *phigh, *plow);
3086
#endif
3087
}
3088

    
3089
static void imul64(uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b)
3090
{
3091
    int sa, sb;
3092
    sa = (a < 0);
3093
    if (sa)
3094
        a = -a;
3095
    sb = (b < 0);
3096
    if (sb)
3097
        b = -b;
3098
    mul64(plow, phigh, a, b);
3099
    if (sa ^ sb) {
3100
        neg128(plow, phigh);
3101
    }
3102
}
3103

    
3104
/* XXX: overflow support */
3105
static void div64(uint64_t *plow, uint64_t *phigh, uint64_t b)
3106
{
3107
    uint64_t q, r, a1, a0;
3108
    int i, qb;
3109

    
3110
    a0 = *plow;
3111
    a1 = *phigh;
3112
    if (a1 == 0) {
3113
        q = a0 / b;
3114
        r = a0 % b;
3115
        *plow = q;
3116
        *phigh = r;
3117
    } else {
3118
        /* XXX: use a better algorithm */
3119
        for(i = 0; i < 64; i++) {
3120
            a1 = (a1 << 1) | (a0 >> 63);
3121
            if (a1 >= b) {
3122
                a1 -= b;
3123
                qb = 1;
3124
            } else {
3125
                qb = 0;
3126
            }
3127
            a0 = (a0 << 1) | qb;
3128
        }
3129
#if defined(DEBUG_MULDIV)
3130
        printf("div: 0x%016llx%016llx / 0x%016llx: q=0x%016llx r=0x%016llx\n",
3131
               *phigh, *plow, b, a0, a1);
3132
#endif
3133
        *plow = a0;
3134
        *phigh = a1;
3135
    }
3136
}
3137

    
3138
static void idiv64(uint64_t *plow, uint64_t *phigh, uint64_t b)
3139
{
3140
    int sa, sb;
3141
    sa = ((int64_t)*phigh < 0);
3142
    if (sa)
3143
        neg128(plow, phigh);
3144
    sb = (b < 0);
3145
    if (sb)
3146
        b = -b;
3147
    div64(plow, phigh, b);
3148
    if (sa ^ sb)
3149
        *plow = - *plow;
3150
    if (sb)
3151
        *phigh = - *phigh;
3152
}
3153

    
3154
void helper_mulq_EAX_T0(void)
3155
{
3156
    uint64_t r0, r1;
3157

    
3158
    mul64(&r0, &r1, EAX, T0);
3159
    EAX = r0;
3160
    EDX = r1;
3161
    CC_DST = r0;
3162
    CC_SRC = r1;
3163
}
3164

    
3165
void helper_imulq_EAX_T0(void)
3166
{
3167
    uint64_t r0, r1;
3168

    
3169
    imul64(&r0, &r1, EAX, T0);
3170
    EAX = r0;
3171
    EDX = r1;
3172
    CC_DST = r0;
3173
    CC_SRC = ((int64_t)r1 != ((int64_t)r0 >> 63));
3174
}
3175

    
3176
void helper_imulq_T0_T1(void)
3177
{
3178
    uint64_t r0, r1;
3179

    
3180
    imul64(&r0, &r1, T0, T1);
3181
    T0 = r0;
3182
    CC_DST = r0;
3183
    CC_SRC = ((int64_t)r1 != ((int64_t)r0 >> 63));
3184
}
3185

    
3186
void helper_divq_EAX_T0(void)
3187
{
3188
    uint64_t r0, r1;
3189
    if (T0 == 0) {
3190
        raise_exception(EXCP00_DIVZ);
3191
    }
3192
    r0 = EAX;
3193
    r1 = EDX;
3194
    div64(&r0, &r1, T0);
3195
    EAX = r0;
3196
    EDX = r1;
3197
}
3198

    
3199
void helper_idivq_EAX_T0(void)
3200
{
3201
    uint64_t r0, r1;
3202
    if (T0 == 0) {
3203
        raise_exception(EXCP00_DIVZ);
3204
    }
3205
    r0 = EAX;
3206
    r1 = EDX;
3207
    idiv64(&r0, &r1, T0);
3208
    EAX = r0;
3209
    EDX = r1;
3210
}
3211

    
3212
#endif
3213

    
3214
/* XXX: do it */
3215
int fpu_isnan(double a)
3216
{
3217
    return 0;
3218
}
3219

    
3220
float approx_rsqrt(float a)
3221
{
3222
    return 1.0 / sqrt(a);
3223
}
3224

    
3225
float approx_rcp(float a)
3226
{
3227
    return 1.0 / a;
3228
}
3229

    
3230

    
3231
#if !defined(CONFIG_USER_ONLY) 
3232

    
3233
#define MMUSUFFIX _mmu
3234
#define GETPC() (__builtin_return_address(0))
3235

    
3236
#define SHIFT 0
3237
#include "softmmu_template.h"
3238

    
3239
#define SHIFT 1
3240
#include "softmmu_template.h"
3241

    
3242
#define SHIFT 2
3243
#include "softmmu_template.h"
3244

    
3245
#define SHIFT 3
3246
#include "softmmu_template.h"
3247

    
3248
#endif
3249

    
3250
/* try to fill the TLB and return an exception if error. If retaddr is
3251
   NULL, it means that the function was called in C code (i.e. not
3252
   from generated code or from helper.c) */
3253
/* XXX: fix it to restore all registers */
3254
void tlb_fill(target_ulong addr, int is_write, int is_user, void *retaddr)
3255
{
3256
    TranslationBlock *tb;
3257
    int ret;
3258
    unsigned long pc;
3259
    CPUX86State *saved_env;
3260

    
3261
    /* XXX: hack to restore env in all cases, even if not called from
3262
       generated code */
3263
    saved_env = env;
3264
    env = cpu_single_env;
3265

    
3266
    ret = cpu_x86_handle_mmu_fault(env, addr, is_write, is_user, 1);
3267
    if (ret) {
3268
        if (retaddr) {
3269
            /* now we have a real cpu fault */
3270
            pc = (unsigned long)retaddr;
3271
            tb = tb_find_pc(pc);
3272
            if (tb) {
3273
                /* the PC is inside the translated code. It means that we have
3274
                   a virtual CPU fault */
3275
                cpu_restore_state(tb, env, pc, NULL);
3276
            }
3277
        }
3278
        if (retaddr)
3279
            raise_exception_err(EXCP0E_PAGE, env->error_code);
3280
        else
3281
            raise_exception_err_norestore(EXCP0E_PAGE, env->error_code);
3282
    }
3283
    env = saved_env;
3284
}