Statistics
| Branch: | Revision:

root / target-i386 / helper.c @ 8145122b

History | View | Annotate | Download (70.2 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
    printf("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
#ifdef reg_EAX
113
    env->regs[R_EAX] = EAX;
114
#endif
115
#ifdef reg_ECX
116
    env->regs[R_ECX] = ECX;
117
#endif
118
#ifdef reg_EDX
119
    env->regs[R_EDX] = EDX;
120
#endif
121
#ifdef reg_EBX
122
    env->regs[R_EBX] = EBX;
123
#endif
124
#ifdef reg_ESP
125
    env->regs[R_ESP] = ESP;
126
#endif
127
#ifdef reg_EBP
128
    env->regs[R_EBP] = EBP;
129
#endif
130
#ifdef reg_ESI
131
    env->regs[R_ESI] = ESI;
132
#endif
133
#ifdef reg_EDI
134
    env->regs[R_EDI] = EDI;
135
#endif
136
    longjmp(env->jmp_env, 1);
137
}
138

    
139
/* return non zero if error */
140
static inline int load_segment(uint32_t *e1_ptr, uint32_t *e2_ptr,
141
                               int selector)
142
{
143
    SegmentCache *dt;
144
    int index;
145
    uint8_t *ptr;
146

    
147
    if (selector & 0x4)
148
        dt = &env->ldt;
149
    else
150
        dt = &env->gdt;
151
    index = selector & ~7;
152
    if ((index + 7) > dt->limit)
153
        return -1;
154
    ptr = dt->base + index;
155
    *e1_ptr = ldl_kernel(ptr);
156
    *e2_ptr = ldl_kernel(ptr + 4);
157
    return 0;
158
}
159
                                     
160
static inline unsigned int get_seg_limit(uint32_t e1, uint32_t e2)
161
{
162
    unsigned int limit;
163
    limit = (e1 & 0xffff) | (e2 & 0x000f0000);
164
    if (e2 & DESC_G_MASK)
165
        limit = (limit << 12) | 0xfff;
166
    return limit;
167
}
168

    
169
static inline uint8_t *get_seg_base(uint32_t e1, uint32_t e2)
170
{
171
    return (uint8_t *)((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
172
}
173

    
174
static inline void load_seg_cache_raw_dt(SegmentCache *sc, uint32_t e1, uint32_t e2)
175
{
176
    sc->base = get_seg_base(e1, e2);
177
    sc->limit = get_seg_limit(e1, e2);
178
    sc->flags = e2;
179
}
180

    
181
/* init the segment cache in vm86 mode. */
182
static inline void load_seg_vm(int seg, int selector)
183
{
184
    selector &= 0xffff;
185
    cpu_x86_load_seg_cache(env, seg, selector, 
186
                           (uint8_t *)(selector << 4), 0xffff, 0);
187
}
188

    
189
static inline void get_ss_esp_from_tss(uint32_t *ss_ptr, 
190
                                       uint32_t *esp_ptr, int dpl)
191
{
192
    int type, index, shift;
193
    
194
#if 0
195
    {
196
        int i;
197
        printf("TR: base=%p limit=%x\n", env->tr.base, env->tr.limit);
198
        for(i=0;i<env->tr.limit;i++) {
199
            printf("%02x ", env->tr.base[i]);
200
            if ((i & 7) == 7) printf("\n");
201
        }
202
        printf("\n");
203
    }
204
#endif
205

    
206
    if (!(env->tr.flags & DESC_P_MASK))
207
        cpu_abort(env, "invalid tss");
208
    type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
209
    if ((type & 7) != 1)
210
        cpu_abort(env, "invalid tss type");
211
    shift = type >> 3;
212
    index = (dpl * 4 + 2) << shift;
213
    if (index + (4 << shift) - 1 > env->tr.limit)
214
        raise_exception_err(EXCP0A_TSS, env->tr.selector & 0xfffc);
215
    if (shift == 0) {
216
        *esp_ptr = lduw_kernel(env->tr.base + index);
217
        *ss_ptr = lduw_kernel(env->tr.base + index + 2);
218
    } else {
219
        *esp_ptr = ldl_kernel(env->tr.base + index);
220
        *ss_ptr = lduw_kernel(env->tr.base + index + 4);
221
    }
222
}
223

    
224
/* XXX: merge with load_seg() */
225
static void tss_load_seg(int seg_reg, int selector)
226
{
227
    uint32_t e1, e2;
228
    int rpl, dpl, cpl;
229

    
230
    if ((selector & 0xfffc) != 0) {
231
        if (load_segment(&e1, &e2, selector) != 0)
232
            raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
233
        if (!(e2 & DESC_S_MASK))
234
            raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
235
        rpl = selector & 3;
236
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
237
        cpl = env->hflags & HF_CPL_MASK;
238
        if (seg_reg == R_CS) {
239
            if (!(e2 & DESC_CS_MASK))
240
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
241
            if (dpl != rpl)
242
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
243
            if ((e2 & DESC_C_MASK) && dpl > rpl)
244
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
245
                
246
        } else if (seg_reg == R_SS) {
247
            /* SS must be writable data */
248
            if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
249
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
250
            if (dpl != cpl || dpl != rpl)
251
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
252
        } else {
253
            /* not readable code */
254
            if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK))
255
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
256
            /* if data or non conforming code, checks the rights */
257
            if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) {
258
                if (dpl < cpl || dpl < rpl)
259
                    raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
260
            }
261
        }
262
        if (!(e2 & DESC_P_MASK))
263
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
264
        cpu_x86_load_seg_cache(env, seg_reg, selector, 
265
                       get_seg_base(e1, e2),
266
                       get_seg_limit(e1, e2),
267
                       e2);
268
    } else {
269
        if (seg_reg == R_SS || seg_reg == R_CS) 
270
            raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
271
    }
272
}
273

    
274
#define SWITCH_TSS_JMP  0
275
#define SWITCH_TSS_IRET 1
276
#define SWITCH_TSS_CALL 2
277

    
278
/* XXX: restore CPU state in registers (PowerPC case) */
279
static void switch_tss(int tss_selector, 
280
                       uint32_t e1, uint32_t e2, int source)
281
{
282
    int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, v1, v2, i;
283
    uint8_t *tss_base;
284
    uint32_t new_regs[8], new_segs[6];
285
    uint32_t new_eflags, new_eip, new_cr3, new_ldt, new_trap;
286
    uint32_t old_eflags, eflags_mask;
287
    SegmentCache *dt;
288
    int index;
289
    uint8_t *ptr;
290

    
291
    type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
292
#ifdef DEBUG_PCALL
293
    if (loglevel)
294
        fprintf(logfile, "switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector, type, source);
295
#endif
296

    
297
    /* if task gate, we read the TSS segment and we load it */
298
    if (type == 5) {
299
        if (!(e2 & DESC_P_MASK))
300
            raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
301
        tss_selector = e1 >> 16;
302
        if (tss_selector & 4)
303
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
304
        if (load_segment(&e1, &e2, tss_selector) != 0)
305
            raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
306
        if (e2 & DESC_S_MASK)
307
            raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
308
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
309
        if ((type & 7) != 1)
310
            raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
311
    }
312

    
313
    if (!(e2 & DESC_P_MASK))
314
        raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
315

    
316
    if (type & 8)
317
        tss_limit_max = 103;
318
    else
319
        tss_limit_max = 43;
320
    tss_limit = get_seg_limit(e1, e2);
321
    tss_base = get_seg_base(e1, e2);
322
    if ((tss_selector & 4) != 0 || 
323
        tss_limit < tss_limit_max)
324
        raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
325
    old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
326
    if (old_type & 8)
327
        old_tss_limit_max = 103;
328
    else
329
        old_tss_limit_max = 43;
330

    
331
    /* read all the registers from the new TSS */
332
    if (type & 8) {
333
        /* 32 bit */
334
        new_cr3 = ldl_kernel(tss_base + 0x1c);
335
        new_eip = ldl_kernel(tss_base + 0x20);
336
        new_eflags = ldl_kernel(tss_base + 0x24);
337
        for(i = 0; i < 8; i++)
338
            new_regs[i] = ldl_kernel(tss_base + (0x28 + i * 4));
339
        for(i = 0; i < 6; i++)
340
            new_segs[i] = lduw_kernel(tss_base + (0x48 + i * 4));
341
        new_ldt = lduw_kernel(tss_base + 0x60);
342
        new_trap = ldl_kernel(tss_base + 0x64);
343
    } else {
344
        /* 16 bit */
345
        new_cr3 = 0;
346
        new_eip = lduw_kernel(tss_base + 0x0e);
347
        new_eflags = lduw_kernel(tss_base + 0x10);
348
        for(i = 0; i < 8; i++)
349
            new_regs[i] = lduw_kernel(tss_base + (0x12 + i * 2)) | 0xffff0000;
350
        for(i = 0; i < 4; i++)
351
            new_segs[i] = lduw_kernel(tss_base + (0x22 + i * 4));
352
        new_ldt = lduw_kernel(tss_base + 0x2a);
353
        new_segs[R_FS] = 0;
354
        new_segs[R_GS] = 0;
355
        new_trap = 0;
356
    }
357
    
358
    /* NOTE: we must avoid memory exceptions during the task switch,
359
       so we make dummy accesses before */
360
    /* XXX: it can still fail in some cases, so a bigger hack is
361
       necessary to valid the TLB after having done the accesses */
362

    
363
    v1 = ldub_kernel(env->tr.base);
364
    v2 = ldub(env->tr.base + old_tss_limit_max);
365
    stb_kernel(env->tr.base, v1);
366
    stb_kernel(env->tr.base + old_tss_limit_max, v2);
367
    
368
    /* clear busy bit (it is restartable) */
369
    if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_IRET) {
370
        uint8_t *ptr;
371
        uint32_t e2;
372
        ptr = env->gdt.base + (env->tr.selector << 3);
373
        e2 = ldl_kernel(ptr + 4);
374
        e2 &= ~DESC_TSS_BUSY_MASK;
375
        stl_kernel(ptr + 4, e2);
376
    }
377
    old_eflags = compute_eflags();
378
    if (source == SWITCH_TSS_IRET)
379
        old_eflags &= ~NT_MASK;
380
    
381
    /* save the current state in the old TSS */
382
    if (type & 8) {
383
        /* 32 bit */
384
        stl_kernel(env->tr.base + 0x20, env->eip);
385
        stl_kernel(env->tr.base + 0x24, old_eflags);
386
        for(i = 0; i < 8; i++)
387
            stl_kernel(env->tr.base + (0x28 + i * 4), env->regs[i]);
388
        for(i = 0; i < 6; i++)
389
            stw_kernel(env->tr.base + (0x48 + i * 4), env->segs[i].selector);
390
    } else {
391
        /* 16 bit */
392
        stw_kernel(env->tr.base + 0x0e, new_eip);
393
        stw_kernel(env->tr.base + 0x10, old_eflags);
394
        for(i = 0; i < 8; i++)
395
            stw_kernel(env->tr.base + (0x12 + i * 2), env->regs[i]);
396
        for(i = 0; i < 4; i++)
397
            stw_kernel(env->tr.base + (0x22 + i * 4), env->segs[i].selector);
398
    }
399
    
400
    /* now if an exception occurs, it will occurs in the next task
401
       context */
402

    
403
    if (source == SWITCH_TSS_CALL) {
404
        stw_kernel(tss_base, env->tr.selector);
405
        new_eflags |= NT_MASK;
406
    }
407

    
408
    /* set busy bit */
409
    if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_CALL) {
410
        uint8_t *ptr;
411
        uint32_t e2;
412
        ptr = env->gdt.base + (tss_selector << 3);
413
        e2 = ldl_kernel(ptr + 4);
414
        e2 |= DESC_TSS_BUSY_MASK;
415
        stl_kernel(ptr + 4, e2);
416
    }
417

    
418
    /* set the new CPU state */
419
    /* from this point, any exception which occurs can give problems */
420
    env->cr[0] |= CR0_TS_MASK;
421
    env->tr.selector = tss_selector;
422
    env->tr.base = tss_base;
423
    env->tr.limit = tss_limit;
424
    env->tr.flags = e2 & ~DESC_TSS_BUSY_MASK;
425
    
426
    if ((type & 8) && (env->cr[0] & CR0_PG_MASK)) {
427
        env->cr[3] = new_cr3;
428
        cpu_x86_update_cr3(env);
429
    }
430
    
431
    /* load all registers without an exception, then reload them with
432
       possible exception */
433
    env->eip = new_eip;
434
    eflags_mask = TF_MASK | AC_MASK | ID_MASK | 
435
        IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK;
436
    if (!(type & 8))
437
        eflags_mask &= 0xffff;
438
    load_eflags(new_eflags, eflags_mask);
439
    for(i = 0; i < 8; i++)
440
        env->regs[i] = new_regs[i];
441
    if (new_eflags & VM_MASK) {
442
        for(i = 0; i < 6; i++) 
443
            load_seg_vm(i, new_segs[i]);
444
        /* in vm86, CPL is always 3 */
445
        cpu_x86_set_cpl(env, 3);
446
    } else {
447
        /* CPL is set the RPL of CS */
448
        cpu_x86_set_cpl(env, new_segs[R_CS] & 3);
449
        /* first just selectors as the rest may trigger exceptions */
450
        for(i = 0; i < 6; i++)
451
            cpu_x86_load_seg_cache(env, i, new_segs[i], NULL, 0, 0);
452
    }
453
    
454
    env->ldt.selector = new_ldt & ~4;
455
    env->ldt.base = NULL;
456
    env->ldt.limit = 0;
457
    env->ldt.flags = 0;
458

    
459
    /* load the LDT */
460
    if (new_ldt & 4)
461
        raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
462

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

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

    
519
void check_iob_T0(void)
520
{
521
    check_io(T0, 1);
522
}
523

    
524
void check_iow_T0(void)
525
{
526
    check_io(T0, 2);
527
}
528

    
529
void check_iol_T0(void)
530
{
531
    check_io(T0, 4);
532
}
533

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

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

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

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

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

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

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

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

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

    
593
    has_error_code = 0;
594
    if (!is_int && !is_hw) {
595
        switch(intno) {
596
        case 8:
597
        case 10:
598
        case 11:
599
        case 12:
600
        case 13:
601
        case 14:
602
        case 17:
603
            has_error_code = 1;
604
            break;
605
        }
606
    }
607

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

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

    
708
    shift = type >> 3;
709

    
710
#if 0
711
    /* XXX: check that enough room is available */
712
    push_size = 6 + (new_stack << 2) + (has_error_code << 1);
713
    if (env->eflags & VM_MASK)
714
        push_size += 8;
715
    push_size <<= shift;
716
#endif
717
    if (is_int)
718
        old_eip = next_eip;
719
    else
720
        old_eip = env->eip;
721
    if (shift == 1) {
722
        if (new_stack) {
723
            if (env->eflags & VM_MASK) {
724
                PUSHL(ssp, esp, sp_mask, env->segs[R_GS].selector);
725
                PUSHL(ssp, esp, sp_mask, env->segs[R_FS].selector);
726
                PUSHL(ssp, esp, sp_mask, env->segs[R_DS].selector);
727
                PUSHL(ssp, esp, sp_mask, env->segs[R_ES].selector);
728
            }
729
            PUSHL(ssp, esp, sp_mask, env->segs[R_SS].selector);
730
            PUSHL(ssp, esp, sp_mask, ESP);
731
        }
732
        PUSHL(ssp, esp, sp_mask, compute_eflags());
733
        PUSHL(ssp, esp, sp_mask, env->segs[R_CS].selector);
734
        PUSHL(ssp, esp, sp_mask, old_eip);
735
        if (has_error_code) {
736
            PUSHL(ssp, esp, sp_mask, error_code);
737
        }
738
    } else {
739
        if (new_stack) {
740
            if (env->eflags & VM_MASK) {
741
                PUSHW(ssp, esp, sp_mask, env->segs[R_GS].selector);
742
                PUSHW(ssp, esp, sp_mask, env->segs[R_FS].selector);
743
                PUSHW(ssp, esp, sp_mask, env->segs[R_DS].selector);
744
                PUSHW(ssp, esp, sp_mask, env->segs[R_ES].selector);
745
            }
746
            PUSHW(ssp, esp, sp_mask, env->segs[R_SS].selector);
747
            PUSHW(ssp, esp, sp_mask, ESP);
748
        }
749
        PUSHW(ssp, esp, sp_mask, compute_eflags());
750
        PUSHW(ssp, esp, sp_mask, env->segs[R_CS].selector);
751
        PUSHW(ssp, esp, sp_mask, old_eip);
752
        if (has_error_code) {
753
            PUSHW(ssp, esp, sp_mask, error_code);
754
        }
755
    }
756
    
757
    if (new_stack) {
758
        if (env->eflags & VM_MASK) {
759
            /* XXX: explain me why W2K hangs if the whole segment cache is
760
               reset ? */
761
#if 1
762
            env->segs[R_ES].selector = 0;
763
            env->segs[R_ES].flags = 0;
764
            env->segs[R_DS].selector = 0;
765
            env->segs[R_DS].flags = 0;
766
            env->segs[R_FS].selector = 0;
767
            env->segs[R_FS].flags = 0;
768
            env->segs[R_GS].selector = 0;
769
            env->segs[R_GS].flags = 0;
770
#else
771
            cpu_x86_load_seg_cache(env, R_ES, 0, NULL, 0, 0);
772
            cpu_x86_load_seg_cache(env, R_DS, 0, NULL, 0, 0);
773
            cpu_x86_load_seg_cache(env, R_FS, 0, NULL, 0, 0);
774
            cpu_x86_load_seg_cache(env, R_GS, 0, NULL, 0, 0);
775
#endif
776
        }
777
        ss = (ss & ~3) | dpl;
778
        cpu_x86_load_seg_cache(env, R_SS, ss, 
779
                               ssp, get_seg_limit(ss_e1, ss_e2), ss_e2);
780
    }
781
    ESP = (ESP & ~sp_mask) | (esp & sp_mask);
782

    
783
    selector = (selector & ~3) | dpl;
784
    cpu_x86_load_seg_cache(env, R_CS, selector, 
785
                   get_seg_base(e1, e2),
786
                   get_seg_limit(e1, e2),
787
                   e2);
788
    cpu_x86_set_cpl(env, dpl);
789
    env->eip = offset;
790

    
791
    /* interrupt gate clear IF mask */
792
    if ((type & 1) == 0) {
793
        env->eflags &= ~IF_MASK;
794
    }
795
    env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
796
}
797

    
798
/* real mode interrupt */
799
static void do_interrupt_real(int intno, int is_int, int error_code,
800
                              unsigned int next_eip)
801
{
802
    SegmentCache *dt;
803
    uint8_t *ptr, *ssp;
804
    int selector;
805
    uint32_t offset, esp;
806
    uint32_t old_cs, old_eip;
807

    
808
    /* real mode (simpler !) */
809
    dt = &env->idt;
810
    if (intno * 4 + 3 > dt->limit)
811
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
812
    ptr = dt->base + intno * 4;
813
    offset = lduw_kernel(ptr);
814
    selector = lduw_kernel(ptr + 2);
815
    esp = ESP;
816
    ssp = env->segs[R_SS].base;
817
    if (is_int)
818
        old_eip = next_eip;
819
    else
820
        old_eip = env->eip;
821
    old_cs = env->segs[R_CS].selector;
822
    /* XXX: use SS segment size ? */
823
    PUSHW(ssp, esp, 0xffff, compute_eflags());
824
    PUSHW(ssp, esp, 0xffff, old_cs);
825
    PUSHW(ssp, esp, 0xffff, old_eip);
826
    
827
    /* update processor state */
828
    ESP = (ESP & ~0xffff) | (esp & 0xffff);
829
    env->eip = offset;
830
    env->segs[R_CS].selector = selector;
831
    env->segs[R_CS].base = (uint8_t *)(selector << 4);
832
    env->eflags &= ~(IF_MASK | TF_MASK | AC_MASK | RF_MASK);
833
}
834

    
835
/* fake user mode interrupt */
836
void do_interrupt_user(int intno, int is_int, int error_code, 
837
                       unsigned int next_eip)
838
{
839
    SegmentCache *dt;
840
    uint8_t *ptr;
841
    int dpl, cpl;
842
    uint32_t e2;
843

    
844
    dt = &env->idt;
845
    ptr = dt->base + (intno * 8);
846
    e2 = ldl_kernel(ptr + 4);
847
    
848
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
849
    cpl = env->hflags & HF_CPL_MASK;
850
    /* check privledge if software int */
851
    if (is_int && dpl < cpl)
852
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
853

    
854
    /* Since we emulate only user space, we cannot do more than
855
       exiting the emulation with the suitable exception and error
856
       code */
857
    if (is_int)
858
        EIP = next_eip;
859
}
860

    
861
/*
862
 * Begin excution of an interruption. is_int is TRUE if coming from
863
 * the int instruction. next_eip is the EIP value AFTER the interrupt
864
 * instruction. It is only relevant if is_int is TRUE.  
865
 */
866
void do_interrupt(int intno, int is_int, int error_code, 
867
                  unsigned int next_eip, int is_hw)
868
{
869
#if 0
870
    {
871
        extern FILE *stdout;
872
        static int count;
873
        if (env->cr[0] & CR0_PE_MASK) {
874
            fprintf(stdout, "%d: v=%02x e=%04x i=%d CPL=%d CS:EIP=%04x:%08x SS:ESP=%04x:%08x",
875
                    count, intno, error_code, is_int,
876
                    env->hflags & HF_CPL_MASK,
877
                    env->segs[R_CS].selector, EIP,
878
                    env->segs[R_SS].selector, ESP);
879
            if (intno == 0x0e) {
880
                fprintf(stdout, " CR2=%08x", env->cr[2]);
881
            } else {
882
                fprintf(stdout, " EAX=%08x", env->regs[R_EAX]);
883
            }
884
            fprintf(stdout, "\n");
885

886
            if (0) {
887
                cpu_x86_dump_state(env, stdout, X86_DUMP_CCOP);
888
#if 0
889
                {
890
                    int i;
891
                    uint8_t *ptr;
892
                    fprintf(stdout, "       code=");
893
                    ptr = env->segs[R_CS].base + env->eip;
894
                    for(i = 0; i < 16; i++) {
895
                        fprintf(stdout, " %02x", ldub(ptr + i));
896
                    }
897
                    fprintf(stdout, "\n");
898
                }
899
#endif
900
            }
901
            count++;
902
        }
903
    }
904
#endif
905
#ifdef DEBUG_PCALL
906
    if (loglevel) {
907
        static int count;
908
        fprintf(logfile, "%d: interrupt: vector=%02x error_code=%04x int=%d\n",
909
                count, intno, error_code, is_int);
910
        cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP);
911
#if 0
912
        {
913
            int i;
914
            uint8_t *ptr;
915
            fprintf(logfile, "       code=");
916
            ptr = env->segs[R_CS].base + env->eip;
917
            for(i = 0; i < 16; i++) {
918
                fprintf(logfile, " %02x", ldub(ptr + i));
919
            }
920
            fprintf(logfile, "\n");
921
        }
922
#endif
923
        count++;
924
    }
925
#endif
926
    if (env->cr[0] & CR0_PE_MASK) {
927
        do_interrupt_protected(intno, is_int, error_code, next_eip, is_hw);
928
    } else {
929
        do_interrupt_real(intno, is_int, error_code, next_eip);
930
    }
931
}
932

    
933
/*
934
 * Signal an interruption. It is executed in the main CPU loop.
935
 * is_int is TRUE if coming from the int instruction. next_eip is the
936
 * EIP value AFTER the interrupt instruction. It is only relevant if
937
 * is_int is TRUE.  
938
 */
939
void raise_interrupt(int intno, int is_int, int error_code, 
940
                     unsigned int next_eip)
941
{
942
    env->exception_index = intno;
943
    env->error_code = error_code;
944
    env->exception_is_int = is_int;
945
    env->exception_next_eip = next_eip;
946
    cpu_loop_exit();
947
}
948

    
949
/* shortcuts to generate exceptions */
950

    
951
void (raise_exception_err)(int exception_index, int error_code)
952
{
953
    raise_interrupt(exception_index, 0, error_code, 0);
954
}
955

    
956
void raise_exception(int exception_index)
957
{
958
    raise_interrupt(exception_index, 0, 0, 0);
959
}
960

    
961
#ifdef BUGGY_GCC_DIV64
962
/* gcc 2.95.4 on PowerPC does not seem to like using __udivdi3, so we
963
   call it from another function */
964
uint32_t div64(uint32_t *q_ptr, uint64_t num, uint32_t den)
965
{
966
    *q_ptr = num / den;
967
    return num % den;
968
}
969

    
970
int32_t idiv64(int32_t *q_ptr, int64_t num, int32_t den)
971
{
972
    *q_ptr = num / den;
973
    return num % den;
974
}
975
#endif
976

    
977
void helper_divl_EAX_T0(uint32_t eip)
978
{
979
    unsigned int den, q, r;
980
    uint64_t num;
981
    
982
    num = EAX | ((uint64_t)EDX << 32);
983
    den = T0;
984
    if (den == 0) {
985
        EIP = eip;
986
        raise_exception(EXCP00_DIVZ);
987
    }
988
#ifdef BUGGY_GCC_DIV64
989
    r = div64(&q, num, den);
990
#else
991
    q = (num / den);
992
    r = (num % den);
993
#endif
994
    EAX = q;
995
    EDX = r;
996
}
997

    
998
void helper_idivl_EAX_T0(uint32_t eip)
999
{
1000
    int den, q, r;
1001
    int64_t num;
1002
    
1003
    num = EAX | ((uint64_t)EDX << 32);
1004
    den = T0;
1005
    if (den == 0) {
1006
        EIP = eip;
1007
        raise_exception(EXCP00_DIVZ);
1008
    }
1009
#ifdef BUGGY_GCC_DIV64
1010
    r = idiv64(&q, num, den);
1011
#else
1012
    q = (num / den);
1013
    r = (num % den);
1014
#endif
1015
    EAX = q;
1016
    EDX = r;
1017
}
1018

    
1019
void helper_cmpxchg8b(void)
1020
{
1021
    uint64_t d;
1022
    int eflags;
1023

    
1024
    eflags = cc_table[CC_OP].compute_all();
1025
    d = ldq((uint8_t *)A0);
1026
    if (d == (((uint64_t)EDX << 32) | EAX)) {
1027
        stq((uint8_t *)A0, ((uint64_t)ECX << 32) | EBX);
1028
        eflags |= CC_Z;
1029
    } else {
1030
        EDX = d >> 32;
1031
        EAX = d;
1032
        eflags &= ~CC_Z;
1033
    }
1034
    CC_SRC = eflags;
1035
}
1036

    
1037
#define CPUID_FP87 (1 << 0)
1038
#define CPUID_VME  (1 << 1)
1039
#define CPUID_DE   (1 << 2)
1040
#define CPUID_PSE  (1 << 3)
1041
#define CPUID_TSC  (1 << 4)
1042
#define CPUID_MSR  (1 << 5)
1043
#define CPUID_PAE  (1 << 6)
1044
#define CPUID_MCE  (1 << 7)
1045
#define CPUID_CX8  (1 << 8)
1046
#define CPUID_APIC (1 << 9)
1047
#define CPUID_SEP  (1 << 11) /* sysenter/sysexit */
1048
#define CPUID_MTRR (1 << 12)
1049
#define CPUID_PGE  (1 << 13)
1050
#define CPUID_MCA  (1 << 14)
1051
#define CPUID_CMOV (1 << 15)
1052
/* ... */
1053
#define CPUID_MMX  (1 << 23)
1054
#define CPUID_FXSR (1 << 24)
1055
#define CPUID_SSE  (1 << 25)
1056
#define CPUID_SSE2 (1 << 26)
1057

    
1058
void helper_cpuid(void)
1059
{
1060
    switch(EAX) {
1061
    case 0:
1062
        EAX = 2; /* max EAX index supported */
1063
        EBX = 0x756e6547;
1064
        ECX = 0x6c65746e;
1065
        EDX = 0x49656e69;
1066
        break;
1067
    case 1:
1068
        {
1069
            int family, model, stepping;
1070
            /* EAX = 1 info */
1071
#if 0
1072
            /* pentium 75-200 */
1073
            family = 5;
1074
            model = 2;
1075
            stepping = 11;
1076
#else
1077
            /* pentium pro */
1078
            family = 6;
1079
            model = 1;
1080
            stepping = 3;
1081
#endif
1082
            EAX = (family << 8) | (model << 4) | stepping;
1083
            EBX = 0;
1084
            ECX = 0;
1085
            EDX = CPUID_FP87 | CPUID_DE | CPUID_PSE |
1086
                CPUID_TSC | CPUID_MSR | CPUID_MCE |
1087
                CPUID_CX8 | CPUID_PGE | CPUID_CMOV;
1088
        }
1089
        break;
1090
    default:
1091
        /* cache info: needed for Pentium Pro compatibility */
1092
        EAX = 0x410601;
1093
        EBX = 0;
1094
        ECX = 0;
1095
        EDX = 0;
1096
        break;
1097
    }
1098
}
1099

    
1100
void helper_lldt_T0(void)
1101
{
1102
    int selector;
1103
    SegmentCache *dt;
1104
    uint32_t e1, e2;
1105
    int index;
1106
    uint8_t *ptr;
1107
    
1108
    selector = T0 & 0xffff;
1109
    if ((selector & 0xfffc) == 0) {
1110
        /* XXX: NULL selector case: invalid LDT */
1111
        env->ldt.base = NULL;
1112
        env->ldt.limit = 0;
1113
    } else {
1114
        if (selector & 0x4)
1115
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1116
        dt = &env->gdt;
1117
        index = selector & ~7;
1118
        if ((index + 7) > dt->limit)
1119
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1120
        ptr = dt->base + index;
1121
        e1 = ldl_kernel(ptr);
1122
        e2 = ldl_kernel(ptr + 4);
1123
        if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
1124
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1125
        if (!(e2 & DESC_P_MASK))
1126
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1127
        load_seg_cache_raw_dt(&env->ldt, e1, e2);
1128
    }
1129
    env->ldt.selector = selector;
1130
}
1131

    
1132
void helper_ltr_T0(void)
1133
{
1134
    int selector;
1135
    SegmentCache *dt;
1136
    uint32_t e1, e2;
1137
    int index, type;
1138
    uint8_t *ptr;
1139
    
1140
    selector = T0 & 0xffff;
1141
    if ((selector & 0xfffc) == 0) {
1142
        /* NULL selector case: invalid LDT */
1143
        env->tr.base = NULL;
1144
        env->tr.limit = 0;
1145
        env->tr.flags = 0;
1146
    } else {
1147
        if (selector & 0x4)
1148
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1149
        dt = &env->gdt;
1150
        index = selector & ~7;
1151
        if ((index + 7) > dt->limit)
1152
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1153
        ptr = dt->base + index;
1154
        e1 = ldl_kernel(ptr);
1155
        e2 = ldl_kernel(ptr + 4);
1156
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1157
        if ((e2 & DESC_S_MASK) || 
1158
            (type != 1 && type != 9))
1159
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1160
        if (!(e2 & DESC_P_MASK))
1161
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1162
        load_seg_cache_raw_dt(&env->tr, e1, e2);
1163
        e2 |= DESC_TSS_BUSY_MASK;
1164
        stl_kernel(ptr + 4, e2);
1165
    }
1166
    env->tr.selector = selector;
1167
}
1168

    
1169
/* only works if protected mode and not VM86. seg_reg must be != R_CS */
1170
void load_seg(int seg_reg, int selector)
1171
{
1172
    uint32_t e1, e2;
1173
    int cpl, dpl, rpl;
1174
    SegmentCache *dt;
1175
    int index;
1176
    uint8_t *ptr;
1177

    
1178
    selector &= 0xffff;
1179
    if ((selector & 0xfffc) == 0) {
1180
        /* null selector case */
1181
        if (seg_reg == R_SS)
1182
            raise_exception_err(EXCP0D_GPF, 0);
1183
        cpu_x86_load_seg_cache(env, seg_reg, selector, NULL, 0, 0);
1184
    } else {
1185
        
1186
        if (selector & 0x4)
1187
            dt = &env->ldt;
1188
        else
1189
            dt = &env->gdt;
1190
        index = selector & ~7;
1191
        if ((index + 7) > dt->limit)
1192
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1193
        ptr = dt->base + index;
1194
        e1 = ldl_kernel(ptr);
1195
        e2 = ldl_kernel(ptr + 4);
1196

    
1197
        if (!(e2 & DESC_S_MASK))
1198
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1199
        rpl = selector & 3;
1200
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1201
        cpl = env->hflags & HF_CPL_MASK;
1202
        if (seg_reg == R_SS) {
1203
            /* must be writable segment */
1204
            if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
1205
                raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1206
            if (rpl != cpl || dpl != cpl)
1207
                raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1208
        } else {
1209
            /* must be readable segment */
1210
            if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK)
1211
                raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1212
            
1213
            if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1214
                /* if not conforming code, test rights */
1215
                if (dpl < cpl || dpl < rpl)
1216
                    raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1217
            }
1218
        }
1219

    
1220
        if (!(e2 & DESC_P_MASK)) {
1221
            if (seg_reg == R_SS)
1222
                raise_exception_err(EXCP0C_STACK, selector & 0xfffc);
1223
            else
1224
                raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1225
        }
1226

    
1227
        /* set the access bit if not already set */
1228
        if (!(e2 & DESC_A_MASK)) {
1229
            e2 |= DESC_A_MASK;
1230
            stl_kernel(ptr + 4, e2);
1231
        }
1232

    
1233
        cpu_x86_load_seg_cache(env, seg_reg, selector, 
1234
                       get_seg_base(e1, e2),
1235
                       get_seg_limit(e1, e2),
1236
                       e2);
1237
#if 0
1238
        fprintf(logfile, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n", 
1239
                selector, (unsigned long)sc->base, sc->limit, sc->flags);
1240
#endif
1241
    }
1242
}
1243

    
1244
/* protected mode jump */
1245
void helper_ljmp_protected_T0_T1(void)
1246
{
1247
    int new_cs, new_eip, gate_cs, type;
1248
    uint32_t e1, e2, cpl, dpl, rpl, limit;
1249

    
1250
    new_cs = T0;
1251
    new_eip = T1;
1252
    if ((new_cs & 0xfffc) == 0)
1253
        raise_exception_err(EXCP0D_GPF, 0);
1254
    if (load_segment(&e1, &e2, new_cs) != 0)
1255
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1256
    cpl = env->hflags & HF_CPL_MASK;
1257
    if (e2 & DESC_S_MASK) {
1258
        if (!(e2 & DESC_CS_MASK))
1259
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1260
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1261
        if (e2 & DESC_C_MASK) {
1262
            /* conforming code segment */
1263
            if (dpl > cpl)
1264
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1265
        } else {
1266
            /* non conforming code segment */
1267
            rpl = new_cs & 3;
1268
            if (rpl > cpl)
1269
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1270
            if (dpl != cpl)
1271
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1272
        }
1273
        if (!(e2 & DESC_P_MASK))
1274
            raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1275
        limit = get_seg_limit(e1, e2);
1276
        if (new_eip > limit)
1277
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1278
        cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1279
                       get_seg_base(e1, e2), limit, e2);
1280
        EIP = new_eip;
1281
    } else {
1282
        /* jump to call or task gate */
1283
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1284
        rpl = new_cs & 3;
1285
        cpl = env->hflags & HF_CPL_MASK;
1286
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1287
        switch(type) {
1288
        case 1: /* 286 TSS */
1289
        case 9: /* 386 TSS */
1290
        case 5: /* task gate */
1291
            if (dpl < cpl || dpl < rpl)
1292
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1293
            switch_tss(new_cs, e1, e2, SWITCH_TSS_JMP);
1294
            break;
1295
        case 4: /* 286 call gate */
1296
        case 12: /* 386 call gate */
1297
            if ((dpl < cpl) || (dpl < rpl))
1298
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1299
            if (!(e2 & DESC_P_MASK))
1300
                raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1301
            gate_cs = e1 >> 16;
1302
            if (load_segment(&e1, &e2, gate_cs) != 0)
1303
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1304
            dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1305
            /* must be code segment */
1306
            if (((e2 & (DESC_S_MASK | DESC_CS_MASK)) != 
1307
                 (DESC_S_MASK | DESC_CS_MASK)))
1308
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1309
            if (((e2 & DESC_C_MASK) && (dpl > cpl)) ||
1310
                (!(e2 & DESC_C_MASK) && (dpl != cpl)))
1311
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1312
            if (!(e2 & DESC_P_MASK))
1313
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
1314
            new_eip = (e1 & 0xffff);
1315
            if (type == 12)
1316
                new_eip |= (e2 & 0xffff0000);
1317
            limit = get_seg_limit(e1, e2);
1318
            if (new_eip > limit)
1319
                raise_exception_err(EXCP0D_GPF, 0);
1320
            cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl,
1321
                                   get_seg_base(e1, e2), limit, e2);
1322
            EIP = new_eip;
1323
            break;
1324
        default:
1325
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1326
            break;
1327
        }
1328
    }
1329
}
1330

    
1331
/* real mode call */
1332
void helper_lcall_real_T0_T1(int shift, int next_eip)
1333
{
1334
    int new_cs, new_eip;
1335
    uint32_t esp, esp_mask;
1336
    uint8_t *ssp;
1337

    
1338
    new_cs = T0;
1339
    new_eip = T1;
1340
    esp = ESP;
1341
    esp_mask = get_sp_mask(env->segs[R_SS].flags);
1342
    ssp = env->segs[R_SS].base;
1343
    if (shift) {
1344
        PUSHL(ssp, esp, esp_mask, env->segs[R_CS].selector);
1345
        PUSHL(ssp, esp, esp_mask, next_eip);
1346
    } else {
1347
        PUSHW(ssp, esp, esp_mask, env->segs[R_CS].selector);
1348
        PUSHW(ssp, esp, esp_mask, next_eip);
1349
    }
1350

    
1351
    ESP = (ESP & ~esp_mask) | (esp & esp_mask);
1352
    env->eip = new_eip;
1353
    env->segs[R_CS].selector = new_cs;
1354
    env->segs[R_CS].base = (uint8_t *)(new_cs << 4);
1355
}
1356

    
1357
/* protected mode call */
1358
void helper_lcall_protected_T0_T1(int shift, int next_eip)
1359
{
1360
    int new_cs, new_eip, new_stack, i;
1361
    uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count;
1362
    uint32_t ss, ss_e1, ss_e2, sp, type, ss_dpl, sp_mask;
1363
    uint32_t val, limit, old_sp_mask;
1364
    uint8_t *ssp, *old_ssp;
1365
    
1366
    new_cs = T0;
1367
    new_eip = T1;
1368
#ifdef DEBUG_PCALL
1369
    if (loglevel) {
1370
        fprintf(logfile, "lcall %04x:%08x\n",
1371
                new_cs, new_eip);
1372
        cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP);
1373
    }
1374
#endif
1375
    if ((new_cs & 0xfffc) == 0)
1376
        raise_exception_err(EXCP0D_GPF, 0);
1377
    if (load_segment(&e1, &e2, new_cs) != 0)
1378
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1379
    cpl = env->hflags & HF_CPL_MASK;
1380
#ifdef DEBUG_PCALL
1381
    if (loglevel) {
1382
        fprintf(logfile, "desc=%08x:%08x\n", e1, e2);
1383
    }
1384
#endif
1385
    if (e2 & DESC_S_MASK) {
1386
        if (!(e2 & DESC_CS_MASK))
1387
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1388
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1389
        if (e2 & DESC_C_MASK) {
1390
            /* conforming code segment */
1391
            if (dpl > cpl)
1392
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1393
        } else {
1394
            /* non conforming code segment */
1395
            rpl = new_cs & 3;
1396
            if (rpl > cpl)
1397
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1398
            if (dpl != cpl)
1399
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1400
        }
1401
        if (!(e2 & DESC_P_MASK))
1402
            raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1403

    
1404
        sp = ESP;
1405
        sp_mask = get_sp_mask(env->segs[R_SS].flags);
1406
        ssp = env->segs[R_SS].base;
1407
        if (shift) {
1408
            PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1409
            PUSHL(ssp, sp, sp_mask, next_eip);
1410
        } else {
1411
            PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1412
            PUSHW(ssp, sp, sp_mask, next_eip);
1413
        }
1414
        
1415
        limit = get_seg_limit(e1, e2);
1416
        if (new_eip > limit)
1417
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1418
        /* from this point, not restartable */
1419
        ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1420
        cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1421
                       get_seg_base(e1, e2), limit, e2);
1422
        EIP = new_eip;
1423
    } else {
1424
        /* check gate type */
1425
        type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
1426
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1427
        rpl = new_cs & 3;
1428
        switch(type) {
1429
        case 1: /* available 286 TSS */
1430
        case 9: /* available 386 TSS */
1431
        case 5: /* task gate */
1432
            if (dpl < cpl || dpl < rpl)
1433
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1434
            switch_tss(new_cs, e1, e2, SWITCH_TSS_CALL);
1435
            return;
1436
        case 4: /* 286 call gate */
1437
        case 12: /* 386 call gate */
1438
            break;
1439
        default:
1440
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1441
            break;
1442
        }
1443
        shift = type >> 3;
1444

    
1445
        if (dpl < cpl || dpl < rpl)
1446
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1447
        /* check valid bit */
1448
        if (!(e2 & DESC_P_MASK))
1449
            raise_exception_err(EXCP0B_NOSEG,  new_cs & 0xfffc);
1450
        selector = e1 >> 16;
1451
        offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
1452
        param_count = e2 & 0x1f;
1453
        if ((selector & 0xfffc) == 0)
1454
            raise_exception_err(EXCP0D_GPF, 0);
1455

    
1456
        if (load_segment(&e1, &e2, selector) != 0)
1457
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1458
        if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
1459
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1460
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1461
        if (dpl > cpl)
1462
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
1463
        if (!(e2 & DESC_P_MASK))
1464
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
1465

    
1466
        if (!(e2 & DESC_C_MASK) && dpl < cpl) {
1467
            /* to inner priviledge */
1468
            get_ss_esp_from_tss(&ss, &sp, dpl);
1469
#ifdef DEBUG_PCALL
1470
            if (loglevel)
1471
                fprintf(logfile, "ss=%04x sp=%04x param_count=%d ESP=%x\n", 
1472
                        ss, sp, param_count, ESP);
1473
#endif
1474
            if ((ss & 0xfffc) == 0)
1475
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1476
            if ((ss & 3) != dpl)
1477
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1478
            if (load_segment(&ss_e1, &ss_e2, ss) != 0)
1479
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1480
            ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
1481
            if (ss_dpl != dpl)
1482
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1483
            if (!(ss_e2 & DESC_S_MASK) ||
1484
                (ss_e2 & DESC_CS_MASK) ||
1485
                !(ss_e2 & DESC_W_MASK))
1486
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1487
            if (!(ss_e2 & DESC_P_MASK))
1488
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
1489
            
1490
            //            push_size = ((param_count * 2) + 8) << shift;
1491

    
1492
            old_sp_mask = get_sp_mask(env->segs[R_SS].flags);
1493
            old_ssp = env->segs[R_SS].base;
1494
            
1495
            sp_mask = get_sp_mask(ss_e2);
1496
            ssp = get_seg_base(ss_e1, ss_e2);
1497
            if (shift) {
1498
                PUSHL(ssp, sp, sp_mask, env->segs[R_SS].selector);
1499
                PUSHL(ssp, sp, sp_mask, ESP);
1500
                for(i = param_count - 1; i >= 0; i--) {
1501
                    val = ldl_kernel(old_ssp + ((ESP + i * 4) & old_sp_mask));
1502
                    PUSHL(ssp, sp, sp_mask, val);
1503
                }
1504
            } else {
1505
                PUSHW(ssp, sp, sp_mask, env->segs[R_SS].selector);
1506
                PUSHW(ssp, sp, sp_mask, ESP);
1507
                for(i = param_count - 1; i >= 0; i--) {
1508
                    val = lduw_kernel(old_ssp + ((ESP + i * 2) & old_sp_mask));
1509
                    PUSHW(ssp, sp, sp_mask, val);
1510
                }
1511
            }
1512
            new_stack = 1;
1513
        } else {
1514
            /* to same priviledge */
1515
            sp = ESP;
1516
            sp_mask = get_sp_mask(env->segs[R_SS].flags);
1517
            ssp = env->segs[R_SS].base;
1518
            //            push_size = (4 << shift);
1519
            new_stack = 0;
1520
        }
1521

    
1522
        if (shift) {
1523
            PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1524
            PUSHL(ssp, sp, sp_mask, next_eip);
1525
        } else {
1526
            PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1527
            PUSHW(ssp, sp, sp_mask, next_eip);
1528
        }
1529

    
1530
        /* from this point, not restartable */
1531

    
1532
        if (new_stack) {
1533
            ss = (ss & ~3) | dpl;
1534
            cpu_x86_load_seg_cache(env, R_SS, ss, 
1535
                                   ssp,
1536
                                   get_seg_limit(ss_e1, ss_e2),
1537
                                   ss_e2);
1538
        }
1539

    
1540
        selector = (selector & ~3) | dpl;
1541
        cpu_x86_load_seg_cache(env, R_CS, selector, 
1542
                       get_seg_base(e1, e2),
1543
                       get_seg_limit(e1, e2),
1544
                       e2);
1545
        cpu_x86_set_cpl(env, dpl);
1546
        ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1547
        EIP = offset;
1548
    }
1549
}
1550

    
1551
/* real and vm86 mode iret */
1552
void helper_iret_real(int shift)
1553
{
1554
    uint32_t sp, new_cs, new_eip, new_eflags, sp_mask;
1555
    uint8_t *ssp;
1556
    int eflags_mask;
1557

    
1558
    sp_mask = 0xffff; /* XXXX: use SS segment size ? */
1559
    sp = ESP;
1560
    ssp = env->segs[R_SS].base;
1561
    if (shift == 1) {
1562
        /* 32 bits */
1563
        POPL(ssp, sp, sp_mask, new_eip);
1564
        POPL(ssp, sp, sp_mask, new_cs);
1565
        new_cs &= 0xffff;
1566
        POPL(ssp, sp, sp_mask, new_eflags);
1567
    } else {
1568
        /* 16 bits */
1569
        POPW(ssp, sp, sp_mask, new_eip);
1570
        POPW(ssp, sp, sp_mask, new_cs);
1571
        POPW(ssp, sp, sp_mask, new_eflags);
1572
    }
1573
    ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1574
    load_seg_vm(R_CS, new_cs);
1575
    env->eip = new_eip;
1576
    if (env->eflags & VM_MASK)
1577
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | RF_MASK | NT_MASK;
1578
    else
1579
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK | RF_MASK | NT_MASK;
1580
    if (shift == 0)
1581
        eflags_mask &= 0xffff;
1582
    load_eflags(new_eflags, eflags_mask);
1583
}
1584

    
1585
static inline void validate_seg(int seg_reg, int cpl)
1586
{
1587
    int dpl;
1588
    uint32_t e2;
1589
    
1590
    e2 = env->segs[seg_reg].flags;
1591
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1592
    if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1593
        /* data or non conforming code segment */
1594
        if (dpl < cpl) {
1595
            cpu_x86_load_seg_cache(env, seg_reg, 0, NULL, 0, 0);
1596
        }
1597
    }
1598
}
1599

    
1600
/* protected mode iret */
1601
static inline void helper_ret_protected(int shift, int is_iret, int addend)
1602
{
1603
    uint32_t sp, new_cs, new_eip, new_eflags, new_esp, new_ss, sp_mask;
1604
    uint32_t new_es, new_ds, new_fs, new_gs;
1605
    uint32_t e1, e2, ss_e1, ss_e2;
1606
    int cpl, dpl, rpl, eflags_mask, iopl;
1607
    uint8_t *ssp;
1608
    
1609
    sp_mask = get_sp_mask(env->segs[R_SS].flags);
1610
    sp = ESP;
1611
    ssp = env->segs[R_SS].base;
1612
    if (shift == 1) {
1613
        /* 32 bits */
1614
        POPL(ssp, sp, sp_mask, new_eip);
1615
        POPL(ssp, sp, sp_mask, new_cs);
1616
        new_cs &= 0xffff;
1617
        if (is_iret) {
1618
            POPL(ssp, sp, sp_mask, new_eflags);
1619
            if (new_eflags & VM_MASK)
1620
                goto return_to_vm86;
1621
        }
1622
    } else {
1623
        /* 16 bits */
1624
        POPW(ssp, sp, sp_mask, new_eip);
1625
        POPW(ssp, sp, sp_mask, new_cs);
1626
        if (is_iret)
1627
            POPW(ssp, sp, sp_mask, new_eflags);
1628
    }
1629
#ifdef DEBUG_PCALL
1630
    if (loglevel) {
1631
        fprintf(logfile, "lret new %04x:%08x addend=0x%x\n",
1632
                new_cs, new_eip, addend);
1633
        cpu_x86_dump_state(env, logfile, X86_DUMP_CCOP);
1634
    }
1635
#endif
1636
    if ((new_cs & 0xfffc) == 0)
1637
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1638
    if (load_segment(&e1, &e2, new_cs) != 0)
1639
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1640
    if (!(e2 & DESC_S_MASK) ||
1641
        !(e2 & DESC_CS_MASK))
1642
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1643
    cpl = env->hflags & HF_CPL_MASK;
1644
    rpl = new_cs & 3; 
1645
    if (rpl < cpl)
1646
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1647
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1648
    if (e2 & DESC_C_MASK) {
1649
        if (dpl > rpl)
1650
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1651
    } else {
1652
        if (dpl != rpl)
1653
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
1654
    }
1655
    if (!(e2 & DESC_P_MASK))
1656
        raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
1657
    
1658
    sp += addend;
1659
    if (rpl == cpl) {
1660
        /* return to same priledge level */
1661
        cpu_x86_load_seg_cache(env, R_CS, new_cs, 
1662
                       get_seg_base(e1, e2),
1663
                       get_seg_limit(e1, e2),
1664
                       e2);
1665
    } else {
1666
        /* return to different priviledge level */
1667
        if (shift == 1) {
1668
            /* 32 bits */
1669
            POPL(ssp, sp, sp_mask, new_esp);
1670
            POPL(ssp, sp, sp_mask, new_ss);
1671
            new_ss &= 0xffff;
1672
        } else {
1673
            /* 16 bits */
1674
            POPW(ssp, sp, sp_mask, new_esp);
1675
            POPW(ssp, sp, sp_mask, new_ss);
1676
        }
1677
        
1678
        if ((new_ss & 3) != rpl)
1679
            raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1680
        if (load_segment(&ss_e1, &ss_e2, new_ss) != 0)
1681
            raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1682
        if (!(ss_e2 & DESC_S_MASK) ||
1683
            (ss_e2 & DESC_CS_MASK) ||
1684
            !(ss_e2 & DESC_W_MASK))
1685
            raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1686
        dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
1687
        if (dpl != rpl)
1688
            raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
1689
        if (!(ss_e2 & DESC_P_MASK))
1690
            raise_exception_err(EXCP0B_NOSEG, new_ss & 0xfffc);
1691

    
1692
        cpu_x86_load_seg_cache(env, R_CS, new_cs, 
1693
                       get_seg_base(e1, e2),
1694
                       get_seg_limit(e1, e2),
1695
                       e2);
1696
        cpu_x86_load_seg_cache(env, R_SS, new_ss, 
1697
                       get_seg_base(ss_e1, ss_e2),
1698
                       get_seg_limit(ss_e1, ss_e2),
1699
                       ss_e2);
1700
        cpu_x86_set_cpl(env, rpl);
1701
        sp = new_esp;
1702
        /* XXX: change sp_mask according to old segment ? */
1703

    
1704
        /* validate data segments */
1705
        validate_seg(R_ES, cpl);
1706
        validate_seg(R_DS, cpl);
1707
        validate_seg(R_FS, cpl);
1708
        validate_seg(R_GS, cpl);
1709
    }
1710
    ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1711
    env->eip = new_eip;
1712
    if (is_iret) {
1713
        /* NOTE: 'cpl' is the _old_ CPL */
1714
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | RF_MASK | NT_MASK;
1715
        if (cpl == 0)
1716
            eflags_mask |= IOPL_MASK;
1717
        iopl = (env->eflags >> IOPL_SHIFT) & 3;
1718
        if (cpl <= iopl)
1719
            eflags_mask |= IF_MASK;
1720
        if (shift == 0)
1721
            eflags_mask &= 0xffff;
1722
        load_eflags(new_eflags, eflags_mask);
1723
    }
1724
    return;
1725

    
1726
 return_to_vm86:
1727
    POPL(ssp, sp, sp_mask, new_esp);
1728
    POPL(ssp, sp, sp_mask, new_ss);
1729
    POPL(ssp, sp, sp_mask, new_es);
1730
    POPL(ssp, sp, sp_mask, new_ds);
1731
    POPL(ssp, sp, sp_mask, new_fs);
1732
    POPL(ssp, sp, sp_mask, new_gs);
1733
    
1734
    /* modify processor state */
1735
    load_eflags(new_eflags, TF_MASK | AC_MASK | ID_MASK | 
1736
                IF_MASK | IOPL_MASK | VM_MASK | NT_MASK | VIF_MASK | VIP_MASK);
1737
    load_seg_vm(R_CS, new_cs & 0xffff);
1738
    cpu_x86_set_cpl(env, 3);
1739
    load_seg_vm(R_SS, new_ss & 0xffff);
1740
    load_seg_vm(R_ES, new_es & 0xffff);
1741
    load_seg_vm(R_DS, new_ds & 0xffff);
1742
    load_seg_vm(R_FS, new_fs & 0xffff);
1743
    load_seg_vm(R_GS, new_gs & 0xffff);
1744

    
1745
    env->eip = new_eip;
1746
    ESP = new_esp;
1747
}
1748

    
1749
void helper_iret_protected(int shift)
1750
{
1751
    int tss_selector, type;
1752
    uint32_t e1, e2;
1753
    
1754
    /* specific case for TSS */
1755
    if (env->eflags & NT_MASK) {
1756
        tss_selector = lduw_kernel(env->tr.base + 0);
1757
        if (tss_selector & 4)
1758
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
1759
        if (load_segment(&e1, &e2, tss_selector) != 0)
1760
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
1761
        type = (e2 >> DESC_TYPE_SHIFT) & 0x17;
1762
        /* NOTE: we check both segment and busy TSS */
1763
        if (type != 3)
1764
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
1765
        switch_tss(tss_selector, e1, e2, SWITCH_TSS_IRET);
1766
    } else {
1767
        helper_ret_protected(shift, 1, 0);
1768
    }
1769
}
1770

    
1771
void helper_lret_protected(int shift, int addend)
1772
{
1773
    helper_ret_protected(shift, 0, addend);
1774
}
1775

    
1776
void helper_movl_crN_T0(int reg)
1777
{
1778
    env->cr[reg] = T0;
1779
    switch(reg) {
1780
    case 0:
1781
        cpu_x86_update_cr0(env);
1782
        break;
1783
    case 3:
1784
        cpu_x86_update_cr3(env);
1785
        break;
1786
    }
1787
}
1788

    
1789
/* XXX: do more */
1790
void helper_movl_drN_T0(int reg)
1791
{
1792
    env->dr[reg] = T0;
1793
}
1794

    
1795
void helper_invlpg(unsigned int addr)
1796
{
1797
    cpu_x86_flush_tlb(env, addr);
1798
}
1799

    
1800
/* rdtsc */
1801
#ifndef __i386__
1802
uint64_t emu_time;
1803
#endif
1804

    
1805
void helper_rdtsc(void)
1806
{
1807
    uint64_t val;
1808
#ifdef __i386__
1809
    asm("rdtsc" : "=A" (val));
1810
#else
1811
    /* better than nothing: the time increases */
1812
    val = emu_time++;
1813
#endif
1814
    EAX = val;
1815
    EDX = val >> 32;
1816
}
1817

    
1818
void helper_wrmsr(void)
1819
{
1820
    switch(ECX) {
1821
    case MSR_IA32_SYSENTER_CS:
1822
        env->sysenter_cs = EAX & 0xffff;
1823
        break;
1824
    case MSR_IA32_SYSENTER_ESP:
1825
        env->sysenter_esp = EAX;
1826
        break;
1827
    case MSR_IA32_SYSENTER_EIP:
1828
        env->sysenter_eip = EAX;
1829
        break;
1830
    default:
1831
        /* XXX: exception ? */
1832
        break; 
1833
    }
1834
}
1835

    
1836
void helper_rdmsr(void)
1837
{
1838
    switch(ECX) {
1839
    case MSR_IA32_SYSENTER_CS:
1840
        EAX = env->sysenter_cs;
1841
        EDX = 0;
1842
        break;
1843
    case MSR_IA32_SYSENTER_ESP:
1844
        EAX = env->sysenter_esp;
1845
        EDX = 0;
1846
        break;
1847
    case MSR_IA32_SYSENTER_EIP:
1848
        EAX = env->sysenter_eip;
1849
        EDX = 0;
1850
        break;
1851
    default:
1852
        /* XXX: exception ? */
1853
        break; 
1854
    }
1855
}
1856

    
1857
void helper_lsl(void)
1858
{
1859
    unsigned int selector, limit;
1860
    uint32_t e1, e2;
1861
    int rpl, dpl, cpl, type;
1862

    
1863
    CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1864
    selector = T0 & 0xffff;
1865
    if (load_segment(&e1, &e2, selector) != 0)
1866
        return;
1867
    rpl = selector & 3;
1868
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1869
    cpl = env->hflags & HF_CPL_MASK;
1870
    if (e2 & DESC_S_MASK) {
1871
        if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
1872
            /* conforming */
1873
        } else {
1874
            if (dpl < cpl || dpl < rpl)
1875
                return;
1876
        }
1877
    } else {
1878
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1879
        switch(type) {
1880
        case 1:
1881
        case 2:
1882
        case 3:
1883
        case 9:
1884
        case 11:
1885
            break;
1886
        default:
1887
            return;
1888
        }
1889
        if (dpl < cpl || dpl < rpl)
1890
            return;
1891
    }
1892
    limit = get_seg_limit(e1, e2);
1893
    T1 = limit;
1894
    CC_SRC |= CC_Z;
1895
}
1896

    
1897
void helper_lar(void)
1898
{
1899
    unsigned int selector;
1900
    uint32_t e1, e2;
1901
    int rpl, dpl, cpl, type;
1902

    
1903
    CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1904
    selector = T0 & 0xffff;
1905
    if ((selector & 0xfffc) == 0)
1906
        return;
1907
    if (load_segment(&e1, &e2, selector) != 0)
1908
        return;
1909
    rpl = selector & 3;
1910
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1911
    cpl = env->hflags & HF_CPL_MASK;
1912
    if (e2 & DESC_S_MASK) {
1913
        if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
1914
            /* conforming */
1915
        } else {
1916
            if (dpl < cpl || dpl < rpl)
1917
                return;
1918
        }
1919
    } else {
1920
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1921
        switch(type) {
1922
        case 1:
1923
        case 2:
1924
        case 3:
1925
        case 4:
1926
        case 5:
1927
        case 9:
1928
        case 11:
1929
        case 12:
1930
            break;
1931
        default:
1932
            return;
1933
        }
1934
        if (dpl < cpl || dpl < rpl)
1935
            return;
1936
    }
1937
    T1 = e2 & 0x00f0ff00;
1938
    CC_SRC |= CC_Z;
1939
}
1940

    
1941
void helper_verr(void)
1942
{
1943
    unsigned int selector;
1944
    uint32_t e1, e2;
1945
    int rpl, dpl, cpl;
1946

    
1947
    CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1948
    selector = T0 & 0xffff;
1949
    if ((selector & 0xfffc) == 0)
1950
        return;
1951
    if (load_segment(&e1, &e2, selector) != 0)
1952
        return;
1953
    if (!(e2 & DESC_S_MASK))
1954
        return;
1955
    rpl = selector & 3;
1956
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1957
    cpl = env->hflags & HF_CPL_MASK;
1958
    if (e2 & DESC_CS_MASK) {
1959
        if (!(e2 & DESC_R_MASK))
1960
            return;
1961
        if (!(e2 & DESC_C_MASK)) {
1962
            if (dpl < cpl || dpl < rpl)
1963
                return;
1964
        }
1965
    } else {
1966
        if (dpl < cpl || dpl < rpl)
1967
            return;
1968
    }
1969
    CC_SRC |= CC_Z;
1970
}
1971

    
1972
void helper_verw(void)
1973
{
1974
    unsigned int selector;
1975
    uint32_t e1, e2;
1976
    int rpl, dpl, cpl;
1977

    
1978
    CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1979
    selector = T0 & 0xffff;
1980
    if ((selector & 0xfffc) == 0)
1981
        return;
1982
    if (load_segment(&e1, &e2, selector) != 0)
1983
        return;
1984
    if (!(e2 & DESC_S_MASK))
1985
        return;
1986
    rpl = selector & 3;
1987
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1988
    cpl = env->hflags & HF_CPL_MASK;
1989
    if (e2 & DESC_CS_MASK) {
1990
        return;
1991
    } else {
1992
        if (dpl < cpl || dpl < rpl)
1993
            return;
1994
        if (!(e2 & DESC_W_MASK))
1995
            return;
1996
    }
1997
    CC_SRC |= CC_Z;
1998
}
1999

    
2000
/* FPU helpers */
2001

    
2002
void helper_fldt_ST0_A0(void)
2003
{
2004
    int new_fpstt;
2005
    new_fpstt = (env->fpstt - 1) & 7;
2006
    env->fpregs[new_fpstt] = helper_fldt((uint8_t *)A0);
2007
    env->fpstt = new_fpstt;
2008
    env->fptags[new_fpstt] = 0; /* validate stack entry */
2009
}
2010

    
2011
void helper_fstt_ST0_A0(void)
2012
{
2013
    helper_fstt(ST0, (uint8_t *)A0);
2014
}
2015

    
2016
/* BCD ops */
2017

    
2018
#define MUL10(iv) ( iv + iv + (iv << 3) )
2019

    
2020
void helper_fbld_ST0_A0(void)
2021
{
2022
    CPU86_LDouble tmp;
2023
    uint64_t val;
2024
    unsigned int v;
2025
    int i;
2026

    
2027
    val = 0;
2028
    for(i = 8; i >= 0; i--) {
2029
        v = ldub((uint8_t *)A0 + i);
2030
        val = (val * 100) + ((v >> 4) * 10) + (v & 0xf);
2031
    }
2032
    tmp = val;
2033
    if (ldub((uint8_t *)A0 + 9) & 0x80)
2034
        tmp = -tmp;
2035
    fpush();
2036
    ST0 = tmp;
2037
}
2038

    
2039
void helper_fbst_ST0_A0(void)
2040
{
2041
    CPU86_LDouble tmp;
2042
    int v;
2043
    uint8_t *mem_ref, *mem_end;
2044
    int64_t val;
2045

    
2046
    tmp = rint(ST0);
2047
    val = (int64_t)tmp;
2048
    mem_ref = (uint8_t *)A0;
2049
    mem_end = mem_ref + 9;
2050
    if (val < 0) {
2051
        stb(mem_end, 0x80);
2052
        val = -val;
2053
    } else {
2054
        stb(mem_end, 0x00);
2055
    }
2056
    while (mem_ref < mem_end) {
2057
        if (val == 0)
2058
            break;
2059
        v = val % 100;
2060
        val = val / 100;
2061
        v = ((v / 10) << 4) | (v % 10);
2062
        stb(mem_ref++, v);
2063
    }
2064
    while (mem_ref < mem_end) {
2065
        stb(mem_ref++, 0);
2066
    }
2067
}
2068

    
2069
void helper_f2xm1(void)
2070
{
2071
    ST0 = pow(2.0,ST0) - 1.0;
2072
}
2073

    
2074
void helper_fyl2x(void)
2075
{
2076
    CPU86_LDouble fptemp;
2077
    
2078
    fptemp = ST0;
2079
    if (fptemp>0.0){
2080
        fptemp = log(fptemp)/log(2.0);         /* log2(ST) */
2081
        ST1 *= fptemp;
2082
        fpop();
2083
    } else { 
2084
        env->fpus &= (~0x4700);
2085
        env->fpus |= 0x400;
2086
    }
2087
}
2088

    
2089
void helper_fptan(void)
2090
{
2091
    CPU86_LDouble fptemp;
2092

    
2093
    fptemp = ST0;
2094
    if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2095
        env->fpus |= 0x400;
2096
    } else {
2097
        ST0 = tan(fptemp);
2098
        fpush();
2099
        ST0 = 1.0;
2100
        env->fpus &= (~0x400);  /* C2 <-- 0 */
2101
        /* the above code is for  |arg| < 2**52 only */
2102
    }
2103
}
2104

    
2105
void helper_fpatan(void)
2106
{
2107
    CPU86_LDouble fptemp, fpsrcop;
2108

    
2109
    fpsrcop = ST1;
2110
    fptemp = ST0;
2111
    ST1 = atan2(fpsrcop,fptemp);
2112
    fpop();
2113
}
2114

    
2115
void helper_fxtract(void)
2116
{
2117
    CPU86_LDoubleU temp;
2118
    unsigned int expdif;
2119

    
2120
    temp.d = ST0;
2121
    expdif = EXPD(temp) - EXPBIAS;
2122
    /*DP exponent bias*/
2123
    ST0 = expdif;
2124
    fpush();
2125
    BIASEXPONENT(temp);
2126
    ST0 = temp.d;
2127
}
2128

    
2129
void helper_fprem1(void)
2130
{
2131
    CPU86_LDouble dblq, fpsrcop, fptemp;
2132
    CPU86_LDoubleU fpsrcop1, fptemp1;
2133
    int expdif;
2134
    int q;
2135

    
2136
    fpsrcop = ST0;
2137
    fptemp = ST1;
2138
    fpsrcop1.d = fpsrcop;
2139
    fptemp1.d = fptemp;
2140
    expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2141
    if (expdif < 53) {
2142
        dblq = fpsrcop / fptemp;
2143
        dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2144
        ST0 = fpsrcop - fptemp*dblq;
2145
        q = (int)dblq; /* cutting off top bits is assumed here */
2146
        env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2147
                                /* (C0,C1,C3) <-- (q2,q1,q0) */
2148
        env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2149
        env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2150
        env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2151
    } else {
2152
        env->fpus |= 0x400;  /* C2 <-- 1 */
2153
        fptemp = pow(2.0, expdif-50);
2154
        fpsrcop = (ST0 / ST1) / fptemp;
2155
        /* fpsrcop = integer obtained by rounding to the nearest */
2156
        fpsrcop = (fpsrcop-floor(fpsrcop) < ceil(fpsrcop)-fpsrcop)?
2157
            floor(fpsrcop): ceil(fpsrcop);
2158
        ST0 -= (ST1 * fpsrcop * fptemp);
2159
    }
2160
}
2161

    
2162
void helper_fprem(void)
2163
{
2164
    CPU86_LDouble dblq, fpsrcop, fptemp;
2165
    CPU86_LDoubleU fpsrcop1, fptemp1;
2166
    int expdif;
2167
    int q;
2168
    
2169
    fpsrcop = ST0;
2170
    fptemp = ST1;
2171
    fpsrcop1.d = fpsrcop;
2172
    fptemp1.d = fptemp;
2173
    expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2174
    if ( expdif < 53 ) {
2175
        dblq = fpsrcop / fptemp;
2176
        dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2177
        ST0 = fpsrcop - fptemp*dblq;
2178
        q = (int)dblq; /* cutting off top bits is assumed here */
2179
        env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2180
                                /* (C0,C1,C3) <-- (q2,q1,q0) */
2181
        env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2182
        env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2183
        env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2184
    } else {
2185
        env->fpus |= 0x400;  /* C2 <-- 1 */
2186
        fptemp = pow(2.0, expdif-50);
2187
        fpsrcop = (ST0 / ST1) / fptemp;
2188
        /* fpsrcop = integer obtained by chopping */
2189
        fpsrcop = (fpsrcop < 0.0)?
2190
            -(floor(fabs(fpsrcop))): floor(fpsrcop);
2191
        ST0 -= (ST1 * fpsrcop * fptemp);
2192
    }
2193
}
2194

    
2195
void helper_fyl2xp1(void)
2196
{
2197
    CPU86_LDouble fptemp;
2198

    
2199
    fptemp = ST0;
2200
    if ((fptemp+1.0)>0.0) {
2201
        fptemp = log(fptemp+1.0) / log(2.0); /* log2(ST+1.0) */
2202
        ST1 *= fptemp;
2203
        fpop();
2204
    } else { 
2205
        env->fpus &= (~0x4700);
2206
        env->fpus |= 0x400;
2207
    }
2208
}
2209

    
2210
void helper_fsqrt(void)
2211
{
2212
    CPU86_LDouble fptemp;
2213

    
2214
    fptemp = ST0;
2215
    if (fptemp<0.0) { 
2216
        env->fpus &= (~0x4700);  /* (C3,C2,C1,C0) <-- 0000 */
2217
        env->fpus |= 0x400;
2218
    }
2219
    ST0 = sqrt(fptemp);
2220
}
2221

    
2222
void helper_fsincos(void)
2223
{
2224
    CPU86_LDouble fptemp;
2225

    
2226
    fptemp = ST0;
2227
    if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2228
        env->fpus |= 0x400;
2229
    } else {
2230
        ST0 = sin(fptemp);
2231
        fpush();
2232
        ST0 = cos(fptemp);
2233
        env->fpus &= (~0x400);  /* C2 <-- 0 */
2234
        /* the above code is for  |arg| < 2**63 only */
2235
    }
2236
}
2237

    
2238
void helper_frndint(void)
2239
{
2240
    CPU86_LDouble a;
2241

    
2242
    a = ST0;
2243
#ifdef __arm__
2244
    switch(env->fpuc & RC_MASK) {
2245
    default:
2246
    case RC_NEAR:
2247
        asm("rndd %0, %1" : "=f" (a) : "f"(a));
2248
        break;
2249
    case RC_DOWN:
2250
        asm("rnddm %0, %1" : "=f" (a) : "f"(a));
2251
        break;
2252
    case RC_UP:
2253
        asm("rnddp %0, %1" : "=f" (a) : "f"(a));
2254
        break;
2255
    case RC_CHOP:
2256
        asm("rnddz %0, %1" : "=f" (a) : "f"(a));
2257
        break;
2258
    }
2259
#else
2260
    a = rint(a);
2261
#endif
2262
    ST0 = a;
2263
}
2264

    
2265
void helper_fscale(void)
2266
{
2267
    CPU86_LDouble fpsrcop, fptemp;
2268

    
2269
    fpsrcop = 2.0;
2270
    fptemp = pow(fpsrcop,ST1);
2271
    ST0 *= fptemp;
2272
}
2273

    
2274
void helper_fsin(void)
2275
{
2276
    CPU86_LDouble fptemp;
2277

    
2278
    fptemp = ST0;
2279
    if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2280
        env->fpus |= 0x400;
2281
    } else {
2282
        ST0 = sin(fptemp);
2283
        env->fpus &= (~0x400);  /* C2 <-- 0 */
2284
        /* the above code is for  |arg| < 2**53 only */
2285
    }
2286
}
2287

    
2288
void helper_fcos(void)
2289
{
2290
    CPU86_LDouble fptemp;
2291

    
2292
    fptemp = ST0;
2293
    if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2294
        env->fpus |= 0x400;
2295
    } else {
2296
        ST0 = cos(fptemp);
2297
        env->fpus &= (~0x400);  /* C2 <-- 0 */
2298
        /* the above code is for  |arg5 < 2**63 only */
2299
    }
2300
}
2301

    
2302
void helper_fxam_ST0(void)
2303
{
2304
    CPU86_LDoubleU temp;
2305
    int expdif;
2306

    
2307
    temp.d = ST0;
2308

    
2309
    env->fpus &= (~0x4700);  /* (C3,C2,C1,C0) <-- 0000 */
2310
    if (SIGND(temp))
2311
        env->fpus |= 0x200; /* C1 <-- 1 */
2312

    
2313
    expdif = EXPD(temp);
2314
    if (expdif == MAXEXPD) {
2315
        if (MANTD(temp) == 0)
2316
            env->fpus |=  0x500 /*Infinity*/;
2317
        else
2318
            env->fpus |=  0x100 /*NaN*/;
2319
    } else if (expdif == 0) {
2320
        if (MANTD(temp) == 0)
2321
            env->fpus |=  0x4000 /*Zero*/;
2322
        else
2323
            env->fpus |= 0x4400 /*Denormal*/;
2324
    } else {
2325
        env->fpus |= 0x400;
2326
    }
2327
}
2328

    
2329
void helper_fstenv(uint8_t *ptr, int data32)
2330
{
2331
    int fpus, fptag, exp, i;
2332
    uint64_t mant;
2333
    CPU86_LDoubleU tmp;
2334

    
2335
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2336
    fptag = 0;
2337
    for (i=7; i>=0; i--) {
2338
        fptag <<= 2;
2339
        if (env->fptags[i]) {
2340
            fptag |= 3;
2341
        } else {
2342
            tmp.d = env->fpregs[i];
2343
            exp = EXPD(tmp);
2344
            mant = MANTD(tmp);
2345
            if (exp == 0 && mant == 0) {
2346
                /* zero */
2347
                fptag |= 1;
2348
            } else if (exp == 0 || exp == MAXEXPD
2349
#ifdef USE_X86LDOUBLE
2350
                       || (mant & (1LL << 63)) == 0
2351
#endif
2352
                       ) {
2353
                /* NaNs, infinity, denormal */
2354
                fptag |= 2;
2355
            }
2356
        }
2357
    }
2358
    if (data32) {
2359
        /* 32 bit */
2360
        stl(ptr, env->fpuc);
2361
        stl(ptr + 4, fpus);
2362
        stl(ptr + 8, fptag);
2363
        stl(ptr + 12, 0);
2364
        stl(ptr + 16, 0);
2365
        stl(ptr + 20, 0);
2366
        stl(ptr + 24, 0);
2367
    } else {
2368
        /* 16 bit */
2369
        stw(ptr, env->fpuc);
2370
        stw(ptr + 2, fpus);
2371
        stw(ptr + 4, fptag);
2372
        stw(ptr + 6, 0);
2373
        stw(ptr + 8, 0);
2374
        stw(ptr + 10, 0);
2375
        stw(ptr + 12, 0);
2376
    }
2377
}
2378

    
2379
void helper_fldenv(uint8_t *ptr, int data32)
2380
{
2381
    int i, fpus, fptag;
2382

    
2383
    if (data32) {
2384
        env->fpuc = lduw(ptr);
2385
        fpus = lduw(ptr + 4);
2386
        fptag = lduw(ptr + 8);
2387
    }
2388
    else {
2389
        env->fpuc = lduw(ptr);
2390
        fpus = lduw(ptr + 2);
2391
        fptag = lduw(ptr + 4);
2392
    }
2393
    env->fpstt = (fpus >> 11) & 7;
2394
    env->fpus = fpus & ~0x3800;
2395
    for(i = 0;i < 7; i++) {
2396
        env->fptags[i] = ((fptag & 3) == 3);
2397
        fptag >>= 2;
2398
    }
2399
}
2400

    
2401
void helper_fsave(uint8_t *ptr, int data32)
2402
{
2403
    CPU86_LDouble tmp;
2404
    int i;
2405

    
2406
    helper_fstenv(ptr, data32);
2407

    
2408
    ptr += (14 << data32);
2409
    for(i = 0;i < 8; i++) {
2410
        tmp = ST(i);
2411
        helper_fstt(tmp, ptr);
2412
        ptr += 10;
2413
    }
2414

    
2415
    /* fninit */
2416
    env->fpus = 0;
2417
    env->fpstt = 0;
2418
    env->fpuc = 0x37f;
2419
    env->fptags[0] = 1;
2420
    env->fptags[1] = 1;
2421
    env->fptags[2] = 1;
2422
    env->fptags[3] = 1;
2423
    env->fptags[4] = 1;
2424
    env->fptags[5] = 1;
2425
    env->fptags[6] = 1;
2426
    env->fptags[7] = 1;
2427
}
2428

    
2429
void helper_frstor(uint8_t *ptr, int data32)
2430
{
2431
    CPU86_LDouble tmp;
2432
    int i;
2433

    
2434
    helper_fldenv(ptr, data32);
2435
    ptr += (14 << data32);
2436

    
2437
    for(i = 0;i < 8; i++) {
2438
        tmp = helper_fldt(ptr);
2439
        ST(i) = tmp;
2440
        ptr += 10;
2441
    }
2442
}
2443

    
2444
#if !defined(CONFIG_USER_ONLY) 
2445

    
2446
#define MMUSUFFIX _mmu
2447
#define GETPC() (__builtin_return_address(0))
2448

    
2449
#define SHIFT 0
2450
#include "softmmu_template.h"
2451

    
2452
#define SHIFT 1
2453
#include "softmmu_template.h"
2454

    
2455
#define SHIFT 2
2456
#include "softmmu_template.h"
2457

    
2458
#define SHIFT 3
2459
#include "softmmu_template.h"
2460

    
2461
#endif
2462

    
2463
/* try to fill the TLB and return an exception if error. If retaddr is
2464
   NULL, it means that the function was called in C code (i.e. not
2465
   from generated code or from helper.c) */
2466
/* XXX: fix it to restore all registers */
2467
void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
2468
{
2469
    TranslationBlock *tb;
2470
    int ret;
2471
    unsigned long pc;
2472
    CPUX86State *saved_env;
2473

    
2474
    /* XXX: hack to restore env in all cases, even if not called from
2475
       generated code */
2476
    saved_env = env;
2477
    env = cpu_single_env;
2478

    
2479
    ret = cpu_x86_handle_mmu_fault(env, addr, is_write, is_user, 1);
2480
    if (ret) {
2481
        if (retaddr) {
2482
            /* now we have a real cpu fault */
2483
            pc = (unsigned long)retaddr;
2484
            tb = tb_find_pc(pc);
2485
            if (tb) {
2486
                /* the PC is inside the translated code. It means that we have
2487
                   a virtual CPU fault */
2488
                cpu_restore_state(tb, env, pc);
2489
            }
2490
        }
2491
        raise_exception_err(EXCP0E_PAGE, env->error_code);
2492
    }
2493
    env = saved_env;
2494
}