Statistics
| Branch: | Revision:

root / target-i386 / seg_helper.c @ ff287bbd

History | View | Annotate | Download (79.1 kB)

1 eaa728ee bellard
/*
2 10774999 Blue Swirl
 *  x86 segmentation related helpers:
3 10774999 Blue Swirl
 *  TSS, interrupts, system calls, jumps and call/task gates, descriptors
4 eaa728ee bellard
 *
5 eaa728ee bellard
 *  Copyright (c) 2003 Fabrice Bellard
6 eaa728ee bellard
 *
7 eaa728ee bellard
 * This library is free software; you can redistribute it and/or
8 eaa728ee bellard
 * modify it under the terms of the GNU Lesser General Public
9 eaa728ee bellard
 * License as published by the Free Software Foundation; either
10 eaa728ee bellard
 * version 2 of the License, or (at your option) any later version.
11 eaa728ee bellard
 *
12 eaa728ee bellard
 * This library is distributed in the hope that it will be useful,
13 eaa728ee bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 eaa728ee bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 eaa728ee bellard
 * Lesser General Public License for more details.
16 eaa728ee bellard
 *
17 eaa728ee bellard
 * You should have received a copy of the GNU Lesser General Public
18 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 eaa728ee bellard
 */
20 83dae095 Paolo Bonzini
21 3e457172 Blue Swirl
#include "cpu.h"
22 3e457172 Blue Swirl
#include "qemu-log.h"
23 3e457172 Blue Swirl
#include "helper.h"
24 eaa728ee bellard
25 3e457172 Blue Swirl
//#define DEBUG_PCALL
26 d12d51d5 aliguori
27 92fc4b58 Blue Swirl
#if !defined(CONFIG_USER_ONLY)
28 92fc4b58 Blue Swirl
#include "softmmu_exec.h"
29 92fc4b58 Blue Swirl
#endif /* !defined(CONFIG_USER_ONLY) */
30 92fc4b58 Blue Swirl
31 d12d51d5 aliguori
#ifdef DEBUG_PCALL
32 20054ef0 Blue Swirl
# define LOG_PCALL(...) qemu_log_mask(CPU_LOG_PCALL, ## __VA_ARGS__)
33 20054ef0 Blue Swirl
# define LOG_PCALL_STATE(env)                                  \
34 6fd2a026 Peter Maydell
    log_cpu_state_mask(CPU_LOG_PCALL, (env), CPU_DUMP_CCOP)
35 d12d51d5 aliguori
#else
36 20054ef0 Blue Swirl
# define LOG_PCALL(...) do { } while (0)
37 20054ef0 Blue Swirl
# define LOG_PCALL_STATE(env) do { } while (0)
38 d12d51d5 aliguori
#endif
39 d12d51d5 aliguori
40 eaa728ee bellard
/* return non zero if error */
41 2999a0b2 Blue Swirl
static inline int load_segment(CPUX86State *env, uint32_t *e1_ptr,
42 2999a0b2 Blue Swirl
                               uint32_t *e2_ptr, int selector)
43 eaa728ee bellard
{
44 eaa728ee bellard
    SegmentCache *dt;
45 eaa728ee bellard
    int index;
46 eaa728ee bellard
    target_ulong ptr;
47 eaa728ee bellard
48 20054ef0 Blue Swirl
    if (selector & 0x4) {
49 eaa728ee bellard
        dt = &env->ldt;
50 20054ef0 Blue Swirl
    } else {
51 eaa728ee bellard
        dt = &env->gdt;
52 20054ef0 Blue Swirl
    }
53 eaa728ee bellard
    index = selector & ~7;
54 20054ef0 Blue Swirl
    if ((index + 7) > dt->limit) {
55 eaa728ee bellard
        return -1;
56 20054ef0 Blue Swirl
    }
57 eaa728ee bellard
    ptr = dt->base + index;
58 329e607d Blue Swirl
    *e1_ptr = cpu_ldl_kernel(env, ptr);
59 329e607d Blue Swirl
    *e2_ptr = cpu_ldl_kernel(env, ptr + 4);
60 eaa728ee bellard
    return 0;
61 eaa728ee bellard
}
62 eaa728ee bellard
63 eaa728ee bellard
static inline unsigned int get_seg_limit(uint32_t e1, uint32_t e2)
64 eaa728ee bellard
{
65 eaa728ee bellard
    unsigned int limit;
66 20054ef0 Blue Swirl
67 eaa728ee bellard
    limit = (e1 & 0xffff) | (e2 & 0x000f0000);
68 20054ef0 Blue Swirl
    if (e2 & DESC_G_MASK) {
69 eaa728ee bellard
        limit = (limit << 12) | 0xfff;
70 20054ef0 Blue Swirl
    }
71 eaa728ee bellard
    return limit;
72 eaa728ee bellard
}
73 eaa728ee bellard
74 eaa728ee bellard
static inline uint32_t get_seg_base(uint32_t e1, uint32_t e2)
75 eaa728ee bellard
{
76 20054ef0 Blue Swirl
    return (e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000);
77 eaa728ee bellard
}
78 eaa728ee bellard
79 20054ef0 Blue Swirl
static inline void load_seg_cache_raw_dt(SegmentCache *sc, uint32_t e1,
80 20054ef0 Blue Swirl
                                         uint32_t e2)
81 eaa728ee bellard
{
82 eaa728ee bellard
    sc->base = get_seg_base(e1, e2);
83 eaa728ee bellard
    sc->limit = get_seg_limit(e1, e2);
84 eaa728ee bellard
    sc->flags = e2;
85 eaa728ee bellard
}
86 eaa728ee bellard
87 eaa728ee bellard
/* init the segment cache in vm86 mode. */
88 2999a0b2 Blue Swirl
static inline void load_seg_vm(CPUX86State *env, int seg, int selector)
89 eaa728ee bellard
{
90 eaa728ee bellard
    selector &= 0xffff;
91 eaa728ee bellard
    cpu_x86_load_seg_cache(env, seg, selector,
92 eaa728ee bellard
                           (selector << 4), 0xffff, 0);
93 eaa728ee bellard
}
94 eaa728ee bellard
95 2999a0b2 Blue Swirl
static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr,
96 eaa728ee bellard
                                       uint32_t *esp_ptr, int dpl)
97 eaa728ee bellard
{
98 eaa728ee bellard
    int type, index, shift;
99 eaa728ee bellard
100 eaa728ee bellard
#if 0
101 eaa728ee bellard
    {
102 eaa728ee bellard
        int i;
103 eaa728ee bellard
        printf("TR: base=%p limit=%x\n", env->tr.base, env->tr.limit);
104 20054ef0 Blue Swirl
        for (i = 0; i < env->tr.limit; i++) {
105 eaa728ee bellard
            printf("%02x ", env->tr.base[i]);
106 20054ef0 Blue Swirl
            if ((i & 7) == 7) {
107 20054ef0 Blue Swirl
                printf("\n");
108 20054ef0 Blue Swirl
            }
109 eaa728ee bellard
        }
110 eaa728ee bellard
        printf("\n");
111 eaa728ee bellard
    }
112 eaa728ee bellard
#endif
113 eaa728ee bellard
114 20054ef0 Blue Swirl
    if (!(env->tr.flags & DESC_P_MASK)) {
115 eaa728ee bellard
        cpu_abort(env, "invalid tss");
116 20054ef0 Blue Swirl
    }
117 eaa728ee bellard
    type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
118 20054ef0 Blue Swirl
    if ((type & 7) != 1) {
119 eaa728ee bellard
        cpu_abort(env, "invalid tss type");
120 20054ef0 Blue Swirl
    }
121 eaa728ee bellard
    shift = type >> 3;
122 eaa728ee bellard
    index = (dpl * 4 + 2) << shift;
123 20054ef0 Blue Swirl
    if (index + (4 << shift) - 1 > env->tr.limit) {
124 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0A_TSS, env->tr.selector & 0xfffc);
125 20054ef0 Blue Swirl
    }
126 eaa728ee bellard
    if (shift == 0) {
127 329e607d Blue Swirl
        *esp_ptr = cpu_lduw_kernel(env, env->tr.base + index);
128 329e607d Blue Swirl
        *ss_ptr = cpu_lduw_kernel(env, env->tr.base + index + 2);
129 eaa728ee bellard
    } else {
130 329e607d Blue Swirl
        *esp_ptr = cpu_ldl_kernel(env, env->tr.base + index);
131 329e607d Blue Swirl
        *ss_ptr = cpu_lduw_kernel(env, env->tr.base + index + 4);
132 eaa728ee bellard
    }
133 eaa728ee bellard
}
134 eaa728ee bellard
135 eaa728ee bellard
/* XXX: merge with load_seg() */
136 2999a0b2 Blue Swirl
static void tss_load_seg(CPUX86State *env, int seg_reg, int selector)
137 eaa728ee bellard
{
138 eaa728ee bellard
    uint32_t e1, e2;
139 eaa728ee bellard
    int rpl, dpl, cpl;
140 eaa728ee bellard
141 eaa728ee bellard
    if ((selector & 0xfffc) != 0) {
142 2999a0b2 Blue Swirl
        if (load_segment(env, &e1, &e2, selector) != 0) {
143 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
144 20054ef0 Blue Swirl
        }
145 20054ef0 Blue Swirl
        if (!(e2 & DESC_S_MASK)) {
146 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
147 20054ef0 Blue Swirl
        }
148 eaa728ee bellard
        rpl = selector & 3;
149 eaa728ee bellard
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
150 eaa728ee bellard
        cpl = env->hflags & HF_CPL_MASK;
151 eaa728ee bellard
        if (seg_reg == R_CS) {
152 20054ef0 Blue Swirl
            if (!(e2 & DESC_CS_MASK)) {
153 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
154 20054ef0 Blue Swirl
            }
155 20054ef0 Blue Swirl
            /* XXX: is it correct? */
156 20054ef0 Blue Swirl
            if (dpl != rpl) {
157 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
158 20054ef0 Blue Swirl
            }
159 20054ef0 Blue Swirl
            if ((e2 & DESC_C_MASK) && dpl > rpl) {
160 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
161 20054ef0 Blue Swirl
            }
162 eaa728ee bellard
        } else if (seg_reg == R_SS) {
163 eaa728ee bellard
            /* SS must be writable data */
164 20054ef0 Blue Swirl
            if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK)) {
165 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
166 20054ef0 Blue Swirl
            }
167 20054ef0 Blue Swirl
            if (dpl != cpl || dpl != rpl) {
168 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
169 20054ef0 Blue Swirl
            }
170 eaa728ee bellard
        } else {
171 eaa728ee bellard
            /* not readable code */
172 20054ef0 Blue Swirl
            if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK)) {
173 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
174 20054ef0 Blue Swirl
            }
175 eaa728ee bellard
            /* if data or non conforming code, checks the rights */
176 eaa728ee bellard
            if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) {
177 20054ef0 Blue Swirl
                if (dpl < cpl || dpl < rpl) {
178 77b2bc2c Blue Swirl
                    raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
179 20054ef0 Blue Swirl
                }
180 eaa728ee bellard
            }
181 eaa728ee bellard
        }
182 20054ef0 Blue Swirl
        if (!(e2 & DESC_P_MASK)) {
183 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
184 20054ef0 Blue Swirl
        }
185 eaa728ee bellard
        cpu_x86_load_seg_cache(env, seg_reg, selector,
186 20054ef0 Blue Swirl
                               get_seg_base(e1, e2),
187 20054ef0 Blue Swirl
                               get_seg_limit(e1, e2),
188 20054ef0 Blue Swirl
                               e2);
189 eaa728ee bellard
    } else {
190 20054ef0 Blue Swirl
        if (seg_reg == R_SS || seg_reg == R_CS) {
191 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc);
192 20054ef0 Blue Swirl
        }
193 eaa728ee bellard
    }
194 eaa728ee bellard
}
195 eaa728ee bellard
196 eaa728ee bellard
#define SWITCH_TSS_JMP  0
197 eaa728ee bellard
#define SWITCH_TSS_IRET 1
198 eaa728ee bellard
#define SWITCH_TSS_CALL 2
199 eaa728ee bellard
200 eaa728ee bellard
/* XXX: restore CPU state in registers (PowerPC case) */
201 2999a0b2 Blue Swirl
static void switch_tss(CPUX86State *env, int tss_selector,
202 eaa728ee bellard
                       uint32_t e1, uint32_t e2, int source,
203 eaa728ee bellard
                       uint32_t next_eip)
204 eaa728ee bellard
{
205 eaa728ee bellard
    int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, v1, v2, i;
206 eaa728ee bellard
    target_ulong tss_base;
207 eaa728ee bellard
    uint32_t new_regs[8], new_segs[6];
208 eaa728ee bellard
    uint32_t new_eflags, new_eip, new_cr3, new_ldt, new_trap;
209 eaa728ee bellard
    uint32_t old_eflags, eflags_mask;
210 eaa728ee bellard
    SegmentCache *dt;
211 eaa728ee bellard
    int index;
212 eaa728ee bellard
    target_ulong ptr;
213 eaa728ee bellard
214 eaa728ee bellard
    type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
215 20054ef0 Blue Swirl
    LOG_PCALL("switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector, type,
216 20054ef0 Blue Swirl
              source);
217 eaa728ee bellard
218 eaa728ee bellard
    /* if task gate, we read the TSS segment and we load it */
219 eaa728ee bellard
    if (type == 5) {
220 20054ef0 Blue Swirl
        if (!(e2 & DESC_P_MASK)) {
221 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0B_NOSEG, tss_selector & 0xfffc);
222 20054ef0 Blue Swirl
        }
223 eaa728ee bellard
        tss_selector = e1 >> 16;
224 20054ef0 Blue Swirl
        if (tss_selector & 4) {
225 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc);
226 20054ef0 Blue Swirl
        }
227 2999a0b2 Blue Swirl
        if (load_segment(env, &e1, &e2, tss_selector) != 0) {
228 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, tss_selector & 0xfffc);
229 20054ef0 Blue Swirl
        }
230 20054ef0 Blue Swirl
        if (e2 & DESC_S_MASK) {
231 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, tss_selector & 0xfffc);
232 20054ef0 Blue Swirl
        }
233 eaa728ee bellard
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
234 20054ef0 Blue Swirl
        if ((type & 7) != 1) {
235 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, tss_selector & 0xfffc);
236 20054ef0 Blue Swirl
        }
237 eaa728ee bellard
    }
238 eaa728ee bellard
239 20054ef0 Blue Swirl
    if (!(e2 & DESC_P_MASK)) {
240 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0B_NOSEG, tss_selector & 0xfffc);
241 20054ef0 Blue Swirl
    }
242 eaa728ee bellard
243 20054ef0 Blue Swirl
    if (type & 8) {
244 eaa728ee bellard
        tss_limit_max = 103;
245 20054ef0 Blue Swirl
    } else {
246 eaa728ee bellard
        tss_limit_max = 43;
247 20054ef0 Blue Swirl
    }
248 eaa728ee bellard
    tss_limit = get_seg_limit(e1, e2);
249 eaa728ee bellard
    tss_base = get_seg_base(e1, e2);
250 eaa728ee bellard
    if ((tss_selector & 4) != 0 ||
251 20054ef0 Blue Swirl
        tss_limit < tss_limit_max) {
252 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc);
253 20054ef0 Blue Swirl
    }
254 eaa728ee bellard
    old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
255 20054ef0 Blue Swirl
    if (old_type & 8) {
256 eaa728ee bellard
        old_tss_limit_max = 103;
257 20054ef0 Blue Swirl
    } else {
258 eaa728ee bellard
        old_tss_limit_max = 43;
259 20054ef0 Blue Swirl
    }
260 eaa728ee bellard
261 eaa728ee bellard
    /* read all the registers from the new TSS */
262 eaa728ee bellard
    if (type & 8) {
263 eaa728ee bellard
        /* 32 bit */
264 329e607d Blue Swirl
        new_cr3 = cpu_ldl_kernel(env, tss_base + 0x1c);
265 329e607d Blue Swirl
        new_eip = cpu_ldl_kernel(env, tss_base + 0x20);
266 329e607d Blue Swirl
        new_eflags = cpu_ldl_kernel(env, tss_base + 0x24);
267 20054ef0 Blue Swirl
        for (i = 0; i < 8; i++) {
268 329e607d Blue Swirl
            new_regs[i] = cpu_ldl_kernel(env, tss_base + (0x28 + i * 4));
269 20054ef0 Blue Swirl
        }
270 20054ef0 Blue Swirl
        for (i = 0; i < 6; i++) {
271 329e607d Blue Swirl
            new_segs[i] = cpu_lduw_kernel(env, tss_base + (0x48 + i * 4));
272 20054ef0 Blue Swirl
        }
273 329e607d Blue Swirl
        new_ldt = cpu_lduw_kernel(env, tss_base + 0x60);
274 329e607d Blue Swirl
        new_trap = cpu_ldl_kernel(env, tss_base + 0x64);
275 eaa728ee bellard
    } else {
276 eaa728ee bellard
        /* 16 bit */
277 eaa728ee bellard
        new_cr3 = 0;
278 329e607d Blue Swirl
        new_eip = cpu_lduw_kernel(env, tss_base + 0x0e);
279 329e607d Blue Swirl
        new_eflags = cpu_lduw_kernel(env, tss_base + 0x10);
280 20054ef0 Blue Swirl
        for (i = 0; i < 8; i++) {
281 329e607d Blue Swirl
            new_regs[i] = cpu_lduw_kernel(env, tss_base + (0x12 + i * 2)) |
282 329e607d Blue Swirl
                0xffff0000;
283 20054ef0 Blue Swirl
        }
284 20054ef0 Blue Swirl
        for (i = 0; i < 4; i++) {
285 329e607d Blue Swirl
            new_segs[i] = cpu_lduw_kernel(env, tss_base + (0x22 + i * 4));
286 20054ef0 Blue Swirl
        }
287 329e607d Blue Swirl
        new_ldt = cpu_lduw_kernel(env, tss_base + 0x2a);
288 eaa728ee bellard
        new_segs[R_FS] = 0;
289 eaa728ee bellard
        new_segs[R_GS] = 0;
290 eaa728ee bellard
        new_trap = 0;
291 eaa728ee bellard
    }
292 4581cbcd Blue Swirl
    /* XXX: avoid a compiler warning, see
293 4581cbcd Blue Swirl
     http://support.amd.com/us/Processor_TechDocs/24593.pdf
294 4581cbcd Blue Swirl
     chapters 12.2.5 and 13.2.4 on how to implement TSS Trap bit */
295 4581cbcd Blue Swirl
    (void)new_trap;
296 eaa728ee bellard
297 eaa728ee bellard
    /* NOTE: we must avoid memory exceptions during the task switch,
298 eaa728ee bellard
       so we make dummy accesses before */
299 eaa728ee bellard
    /* XXX: it can still fail in some cases, so a bigger hack is
300 eaa728ee bellard
       necessary to valid the TLB after having done the accesses */
301 eaa728ee bellard
302 329e607d Blue Swirl
    v1 = cpu_ldub_kernel(env, env->tr.base);
303 329e607d Blue Swirl
    v2 = cpu_ldub_kernel(env, env->tr.base + old_tss_limit_max);
304 329e607d Blue Swirl
    cpu_stb_kernel(env, env->tr.base, v1);
305 329e607d Blue Swirl
    cpu_stb_kernel(env, env->tr.base + old_tss_limit_max, v2);
306 eaa728ee bellard
307 eaa728ee bellard
    /* clear busy bit (it is restartable) */
308 eaa728ee bellard
    if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_IRET) {
309 eaa728ee bellard
        target_ulong ptr;
310 eaa728ee bellard
        uint32_t e2;
311 20054ef0 Blue Swirl
312 eaa728ee bellard
        ptr = env->gdt.base + (env->tr.selector & ~7);
313 329e607d Blue Swirl
        e2 = cpu_ldl_kernel(env, ptr + 4);
314 eaa728ee bellard
        e2 &= ~DESC_TSS_BUSY_MASK;
315 329e607d Blue Swirl
        cpu_stl_kernel(env, ptr + 4, e2);
316 eaa728ee bellard
    }
317 997ff0d9 Blue Swirl
    old_eflags = cpu_compute_eflags(env);
318 20054ef0 Blue Swirl
    if (source == SWITCH_TSS_IRET) {
319 eaa728ee bellard
        old_eflags &= ~NT_MASK;
320 20054ef0 Blue Swirl
    }
321 eaa728ee bellard
322 eaa728ee bellard
    /* save the current state in the old TSS */
323 eaa728ee bellard
    if (type & 8) {
324 eaa728ee bellard
        /* 32 bit */
325 329e607d Blue Swirl
        cpu_stl_kernel(env, env->tr.base + 0x20, next_eip);
326 329e607d Blue Swirl
        cpu_stl_kernel(env, env->tr.base + 0x24, old_eflags);
327 329e607d Blue Swirl
        cpu_stl_kernel(env, env->tr.base + (0x28 + 0 * 4), EAX);
328 329e607d Blue Swirl
        cpu_stl_kernel(env, env->tr.base + (0x28 + 1 * 4), ECX);
329 329e607d Blue Swirl
        cpu_stl_kernel(env, env->tr.base + (0x28 + 2 * 4), EDX);
330 329e607d Blue Swirl
        cpu_stl_kernel(env, env->tr.base + (0x28 + 3 * 4), EBX);
331 329e607d Blue Swirl
        cpu_stl_kernel(env, env->tr.base + (0x28 + 4 * 4), ESP);
332 329e607d Blue Swirl
        cpu_stl_kernel(env, env->tr.base + (0x28 + 5 * 4), EBP);
333 329e607d Blue Swirl
        cpu_stl_kernel(env, env->tr.base + (0x28 + 6 * 4), ESI);
334 329e607d Blue Swirl
        cpu_stl_kernel(env, env->tr.base + (0x28 + 7 * 4), EDI);
335 20054ef0 Blue Swirl
        for (i = 0; i < 6; i++) {
336 329e607d Blue Swirl
            cpu_stw_kernel(env, env->tr.base + (0x48 + i * 4),
337 329e607d Blue Swirl
                           env->segs[i].selector);
338 20054ef0 Blue Swirl
        }
339 eaa728ee bellard
    } else {
340 eaa728ee bellard
        /* 16 bit */
341 329e607d Blue Swirl
        cpu_stw_kernel(env, env->tr.base + 0x0e, next_eip);
342 329e607d Blue Swirl
        cpu_stw_kernel(env, env->tr.base + 0x10, old_eflags);
343 329e607d Blue Swirl
        cpu_stw_kernel(env, env->tr.base + (0x12 + 0 * 2), EAX);
344 329e607d Blue Swirl
        cpu_stw_kernel(env, env->tr.base + (0x12 + 1 * 2), ECX);
345 329e607d Blue Swirl
        cpu_stw_kernel(env, env->tr.base + (0x12 + 2 * 2), EDX);
346 329e607d Blue Swirl
        cpu_stw_kernel(env, env->tr.base + (0x12 + 3 * 2), EBX);
347 329e607d Blue Swirl
        cpu_stw_kernel(env, env->tr.base + (0x12 + 4 * 2), ESP);
348 329e607d Blue Swirl
        cpu_stw_kernel(env, env->tr.base + (0x12 + 5 * 2), EBP);
349 329e607d Blue Swirl
        cpu_stw_kernel(env, env->tr.base + (0x12 + 6 * 2), ESI);
350 329e607d Blue Swirl
        cpu_stw_kernel(env, env->tr.base + (0x12 + 7 * 2), EDI);
351 20054ef0 Blue Swirl
        for (i = 0; i < 4; i++) {
352 329e607d Blue Swirl
            cpu_stw_kernel(env, env->tr.base + (0x22 + i * 4),
353 329e607d Blue Swirl
                           env->segs[i].selector);
354 20054ef0 Blue Swirl
        }
355 eaa728ee bellard
    }
356 eaa728ee bellard
357 eaa728ee bellard
    /* now if an exception occurs, it will occurs in the next task
358 eaa728ee bellard
       context */
359 eaa728ee bellard
360 eaa728ee bellard
    if (source == SWITCH_TSS_CALL) {
361 329e607d Blue Swirl
        cpu_stw_kernel(env, tss_base, env->tr.selector);
362 eaa728ee bellard
        new_eflags |= NT_MASK;
363 eaa728ee bellard
    }
364 eaa728ee bellard
365 eaa728ee bellard
    /* set busy bit */
366 eaa728ee bellard
    if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_CALL) {
367 eaa728ee bellard
        target_ulong ptr;
368 eaa728ee bellard
        uint32_t e2;
369 20054ef0 Blue Swirl
370 eaa728ee bellard
        ptr = env->gdt.base + (tss_selector & ~7);
371 329e607d Blue Swirl
        e2 = cpu_ldl_kernel(env, ptr + 4);
372 eaa728ee bellard
        e2 |= DESC_TSS_BUSY_MASK;
373 329e607d Blue Swirl
        cpu_stl_kernel(env, ptr + 4, e2);
374 eaa728ee bellard
    }
375 eaa728ee bellard
376 eaa728ee bellard
    /* set the new CPU state */
377 eaa728ee bellard
    /* from this point, any exception which occurs can give problems */
378 eaa728ee bellard
    env->cr[0] |= CR0_TS_MASK;
379 eaa728ee bellard
    env->hflags |= HF_TS_MASK;
380 eaa728ee bellard
    env->tr.selector = tss_selector;
381 eaa728ee bellard
    env->tr.base = tss_base;
382 eaa728ee bellard
    env->tr.limit = tss_limit;
383 eaa728ee bellard
    env->tr.flags = e2 & ~DESC_TSS_BUSY_MASK;
384 eaa728ee bellard
385 eaa728ee bellard
    if ((type & 8) && (env->cr[0] & CR0_PG_MASK)) {
386 eaa728ee bellard
        cpu_x86_update_cr3(env, new_cr3);
387 eaa728ee bellard
    }
388 eaa728ee bellard
389 eaa728ee bellard
    /* load all registers without an exception, then reload them with
390 eaa728ee bellard
       possible exception */
391 eaa728ee bellard
    env->eip = new_eip;
392 eaa728ee bellard
    eflags_mask = TF_MASK | AC_MASK | ID_MASK |
393 eaa728ee bellard
        IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK;
394 20054ef0 Blue Swirl
    if (!(type & 8)) {
395 eaa728ee bellard
        eflags_mask &= 0xffff;
396 20054ef0 Blue Swirl
    }
397 997ff0d9 Blue Swirl
    cpu_load_eflags(env, new_eflags, eflags_mask);
398 20054ef0 Blue Swirl
    /* XXX: what to do in 16 bit case? */
399 eaa728ee bellard
    EAX = new_regs[0];
400 eaa728ee bellard
    ECX = new_regs[1];
401 eaa728ee bellard
    EDX = new_regs[2];
402 eaa728ee bellard
    EBX = new_regs[3];
403 eaa728ee bellard
    ESP = new_regs[4];
404 eaa728ee bellard
    EBP = new_regs[5];
405 eaa728ee bellard
    ESI = new_regs[6];
406 eaa728ee bellard
    EDI = new_regs[7];
407 eaa728ee bellard
    if (new_eflags & VM_MASK) {
408 20054ef0 Blue Swirl
        for (i = 0; i < 6; i++) {
409 2999a0b2 Blue Swirl
            load_seg_vm(env, i, new_segs[i]);
410 20054ef0 Blue Swirl
        }
411 eaa728ee bellard
        /* in vm86, CPL is always 3 */
412 eaa728ee bellard
        cpu_x86_set_cpl(env, 3);
413 eaa728ee bellard
    } else {
414 eaa728ee bellard
        /* CPL is set the RPL of CS */
415 eaa728ee bellard
        cpu_x86_set_cpl(env, new_segs[R_CS] & 3);
416 eaa728ee bellard
        /* first just selectors as the rest may trigger exceptions */
417 20054ef0 Blue Swirl
        for (i = 0; i < 6; i++) {
418 eaa728ee bellard
            cpu_x86_load_seg_cache(env, i, new_segs[i], 0, 0, 0);
419 20054ef0 Blue Swirl
        }
420 eaa728ee bellard
    }
421 eaa728ee bellard
422 eaa728ee bellard
    env->ldt.selector = new_ldt & ~4;
423 eaa728ee bellard
    env->ldt.base = 0;
424 eaa728ee bellard
    env->ldt.limit = 0;
425 eaa728ee bellard
    env->ldt.flags = 0;
426 eaa728ee bellard
427 eaa728ee bellard
    /* load the LDT */
428 20054ef0 Blue Swirl
    if (new_ldt & 4) {
429 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc);
430 20054ef0 Blue Swirl
    }
431 eaa728ee bellard
432 eaa728ee bellard
    if ((new_ldt & 0xfffc) != 0) {
433 eaa728ee bellard
        dt = &env->gdt;
434 eaa728ee bellard
        index = new_ldt & ~7;
435 20054ef0 Blue Swirl
        if ((index + 7) > dt->limit) {
436 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc);
437 20054ef0 Blue Swirl
        }
438 eaa728ee bellard
        ptr = dt->base + index;
439 329e607d Blue Swirl
        e1 = cpu_ldl_kernel(env, ptr);
440 329e607d Blue Swirl
        e2 = cpu_ldl_kernel(env, ptr + 4);
441 20054ef0 Blue Swirl
        if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2) {
442 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc);
443 20054ef0 Blue Swirl
        }
444 20054ef0 Blue Swirl
        if (!(e2 & DESC_P_MASK)) {
445 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc);
446 20054ef0 Blue Swirl
        }
447 eaa728ee bellard
        load_seg_cache_raw_dt(&env->ldt, e1, e2);
448 eaa728ee bellard
    }
449 eaa728ee bellard
450 eaa728ee bellard
    /* load the segments */
451 eaa728ee bellard
    if (!(new_eflags & VM_MASK)) {
452 2999a0b2 Blue Swirl
        tss_load_seg(env, R_CS, new_segs[R_CS]);
453 2999a0b2 Blue Swirl
        tss_load_seg(env, R_SS, new_segs[R_SS]);
454 2999a0b2 Blue Swirl
        tss_load_seg(env, R_ES, new_segs[R_ES]);
455 2999a0b2 Blue Swirl
        tss_load_seg(env, R_DS, new_segs[R_DS]);
456 2999a0b2 Blue Swirl
        tss_load_seg(env, R_FS, new_segs[R_FS]);
457 2999a0b2 Blue Swirl
        tss_load_seg(env, R_GS, new_segs[R_GS]);
458 eaa728ee bellard
    }
459 eaa728ee bellard
460 eaa728ee bellard
    /* check that EIP is in the CS segment limits */
461 eaa728ee bellard
    if (new_eip > env->segs[R_CS].limit) {
462 20054ef0 Blue Swirl
        /* XXX: different exception if CALL? */
463 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, 0);
464 eaa728ee bellard
    }
465 01df040b aliguori
466 01df040b aliguori
#ifndef CONFIG_USER_ONLY
467 01df040b aliguori
    /* reset local breakpoints */
468 01df040b aliguori
    if (env->dr[7] & 0x55) {
469 01df040b aliguori
        for (i = 0; i < 4; i++) {
470 20054ef0 Blue Swirl
            if (hw_breakpoint_enabled(env->dr[7], i) == 0x1) {
471 01df040b aliguori
                hw_breakpoint_remove(env, i);
472 20054ef0 Blue Swirl
            }
473 01df040b aliguori
        }
474 01df040b aliguori
        env->dr[7] &= ~0x55;
475 01df040b aliguori
    }
476 01df040b aliguori
#endif
477 eaa728ee bellard
}
478 eaa728ee bellard
479 eaa728ee bellard
static inline unsigned int get_sp_mask(unsigned int e2)
480 eaa728ee bellard
{
481 20054ef0 Blue Swirl
    if (e2 & DESC_B_MASK) {
482 eaa728ee bellard
        return 0xffffffff;
483 20054ef0 Blue Swirl
    } else {
484 eaa728ee bellard
        return 0xffff;
485 20054ef0 Blue Swirl
    }
486 eaa728ee bellard
}
487 eaa728ee bellard
488 20054ef0 Blue Swirl
static int exception_has_error_code(int intno)
489 2ed51f5b aliguori
{
490 20054ef0 Blue Swirl
    switch (intno) {
491 20054ef0 Blue Swirl
    case 8:
492 20054ef0 Blue Swirl
    case 10:
493 20054ef0 Blue Swirl
    case 11:
494 20054ef0 Blue Swirl
    case 12:
495 20054ef0 Blue Swirl
    case 13:
496 20054ef0 Blue Swirl
    case 14:
497 20054ef0 Blue Swirl
    case 17:
498 20054ef0 Blue Swirl
        return 1;
499 20054ef0 Blue Swirl
    }
500 20054ef0 Blue Swirl
    return 0;
501 2ed51f5b aliguori
}
502 2ed51f5b aliguori
503 eaa728ee bellard
#ifdef TARGET_X86_64
504 20054ef0 Blue Swirl
#define SET_ESP(val, sp_mask)                           \
505 20054ef0 Blue Swirl
    do {                                                \
506 20054ef0 Blue Swirl
        if ((sp_mask) == 0xffff) {                      \
507 20054ef0 Blue Swirl
            ESP = (ESP & ~0xffff) | ((val) & 0xffff);   \
508 20054ef0 Blue Swirl
        } else if ((sp_mask) == 0xffffffffLL) {         \
509 20054ef0 Blue Swirl
            ESP = (uint32_t)(val);                      \
510 20054ef0 Blue Swirl
        } else {                                        \
511 20054ef0 Blue Swirl
            ESP = (val);                                \
512 20054ef0 Blue Swirl
        }                                               \
513 20054ef0 Blue Swirl
    } while (0)
514 eaa728ee bellard
#else
515 20054ef0 Blue Swirl
#define SET_ESP(val, sp_mask)                           \
516 20054ef0 Blue Swirl
    do {                                                \
517 20054ef0 Blue Swirl
        ESP = (ESP & ~(sp_mask)) | ((val) & (sp_mask)); \
518 20054ef0 Blue Swirl
    } while (0)
519 eaa728ee bellard
#endif
520 eaa728ee bellard
521 c0a04f0e aliguori
/* in 64-bit machines, this can overflow. So this segment addition macro
522 c0a04f0e aliguori
 * can be used to trim the value to 32-bit whenever needed */
523 c0a04f0e aliguori
#define SEG_ADDL(ssp, sp, sp_mask) ((uint32_t)((ssp) + (sp & (sp_mask))))
524 c0a04f0e aliguori
525 eaa728ee bellard
/* XXX: add a is_user flag to have proper security support */
526 329e607d Blue Swirl
#define PUSHW(ssp, sp, sp_mask, val)                             \
527 329e607d Blue Swirl
    {                                                            \
528 329e607d Blue Swirl
        sp -= 2;                                                 \
529 329e607d Blue Swirl
        cpu_stw_kernel(env, (ssp) + (sp & (sp_mask)), (val));    \
530 20054ef0 Blue Swirl
    }
531 eaa728ee bellard
532 20054ef0 Blue Swirl
#define PUSHL(ssp, sp, sp_mask, val)                                    \
533 20054ef0 Blue Swirl
    {                                                                   \
534 20054ef0 Blue Swirl
        sp -= 4;                                                        \
535 329e607d Blue Swirl
        cpu_stl_kernel(env, SEG_ADDL(ssp, sp, sp_mask), (uint32_t)(val)); \
536 20054ef0 Blue Swirl
    }
537 eaa728ee bellard
538 329e607d Blue Swirl
#define POPW(ssp, sp, sp_mask, val)                              \
539 329e607d Blue Swirl
    {                                                            \
540 329e607d Blue Swirl
        val = cpu_lduw_kernel(env, (ssp) + (sp & (sp_mask)));    \
541 329e607d Blue Swirl
        sp += 2;                                                 \
542 20054ef0 Blue Swirl
    }
543 eaa728ee bellard
544 329e607d Blue Swirl
#define POPL(ssp, sp, sp_mask, val)                                     \
545 329e607d Blue Swirl
    {                                                                   \
546 329e607d Blue Swirl
        val = (uint32_t)cpu_ldl_kernel(env, SEG_ADDL(ssp, sp, sp_mask)); \
547 329e607d Blue Swirl
        sp += 4;                                                        \
548 20054ef0 Blue Swirl
    }
549 eaa728ee bellard
550 eaa728ee bellard
/* protected mode interrupt */
551 2999a0b2 Blue Swirl
static void do_interrupt_protected(CPUX86State *env, int intno, int is_int,
552 2999a0b2 Blue Swirl
                                   int error_code, unsigned int next_eip,
553 2999a0b2 Blue Swirl
                                   int is_hw)
554 eaa728ee bellard
{
555 eaa728ee bellard
    SegmentCache *dt;
556 eaa728ee bellard
    target_ulong ptr, ssp;
557 eaa728ee bellard
    int type, dpl, selector, ss_dpl, cpl;
558 eaa728ee bellard
    int has_error_code, new_stack, shift;
559 1c918eba blueswir1
    uint32_t e1, e2, offset, ss = 0, esp, ss_e1 = 0, ss_e2 = 0;
560 eaa728ee bellard
    uint32_t old_eip, sp_mask;
561 eaa728ee bellard
562 eaa728ee bellard
    has_error_code = 0;
563 20054ef0 Blue Swirl
    if (!is_int && !is_hw) {
564 20054ef0 Blue Swirl
        has_error_code = exception_has_error_code(intno);
565 20054ef0 Blue Swirl
    }
566 20054ef0 Blue Swirl
    if (is_int) {
567 eaa728ee bellard
        old_eip = next_eip;
568 20054ef0 Blue Swirl
    } else {
569 eaa728ee bellard
        old_eip = env->eip;
570 20054ef0 Blue Swirl
    }
571 eaa728ee bellard
572 eaa728ee bellard
    dt = &env->idt;
573 20054ef0 Blue Swirl
    if (intno * 8 + 7 > dt->limit) {
574 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
575 20054ef0 Blue Swirl
    }
576 eaa728ee bellard
    ptr = dt->base + intno * 8;
577 329e607d Blue Swirl
    e1 = cpu_ldl_kernel(env, ptr);
578 329e607d Blue Swirl
    e2 = cpu_ldl_kernel(env, ptr + 4);
579 eaa728ee bellard
    /* check gate type */
580 eaa728ee bellard
    type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
581 20054ef0 Blue Swirl
    switch (type) {
582 eaa728ee bellard
    case 5: /* task gate */
583 eaa728ee bellard
        /* must do that check here to return the correct error code */
584 20054ef0 Blue Swirl
        if (!(e2 & DESC_P_MASK)) {
585 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2);
586 20054ef0 Blue Swirl
        }
587 2999a0b2 Blue Swirl
        switch_tss(env, intno * 8, e1, e2, SWITCH_TSS_CALL, old_eip);
588 eaa728ee bellard
        if (has_error_code) {
589 eaa728ee bellard
            int type;
590 eaa728ee bellard
            uint32_t mask;
591 20054ef0 Blue Swirl
592 eaa728ee bellard
            /* push the error code */
593 eaa728ee bellard
            type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
594 eaa728ee bellard
            shift = type >> 3;
595 20054ef0 Blue Swirl
            if (env->segs[R_SS].flags & DESC_B_MASK) {
596 eaa728ee bellard
                mask = 0xffffffff;
597 20054ef0 Blue Swirl
            } else {
598 eaa728ee bellard
                mask = 0xffff;
599 20054ef0 Blue Swirl
            }
600 eaa728ee bellard
            esp = (ESP - (2 << shift)) & mask;
601 eaa728ee bellard
            ssp = env->segs[R_SS].base + esp;
602 20054ef0 Blue Swirl
            if (shift) {
603 329e607d Blue Swirl
                cpu_stl_kernel(env, ssp, error_code);
604 20054ef0 Blue Swirl
            } else {
605 329e607d Blue Swirl
                cpu_stw_kernel(env, ssp, error_code);
606 20054ef0 Blue Swirl
            }
607 eaa728ee bellard
            SET_ESP(esp, mask);
608 eaa728ee bellard
        }
609 eaa728ee bellard
        return;
610 eaa728ee bellard
    case 6: /* 286 interrupt gate */
611 eaa728ee bellard
    case 7: /* 286 trap gate */
612 eaa728ee bellard
    case 14: /* 386 interrupt gate */
613 eaa728ee bellard
    case 15: /* 386 trap gate */
614 eaa728ee bellard
        break;
615 eaa728ee bellard
    default:
616 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
617 eaa728ee bellard
        break;
618 eaa728ee bellard
    }
619 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
620 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
621 1235fc06 ths
    /* check privilege if software int */
622 20054ef0 Blue Swirl
    if (is_int && dpl < cpl) {
623 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
624 20054ef0 Blue Swirl
    }
625 eaa728ee bellard
    /* check valid bit */
626 20054ef0 Blue Swirl
    if (!(e2 & DESC_P_MASK)) {
627 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2);
628 20054ef0 Blue Swirl
    }
629 eaa728ee bellard
    selector = e1 >> 16;
630 eaa728ee bellard
    offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
631 20054ef0 Blue Swirl
    if ((selector & 0xfffc) == 0) {
632 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, 0);
633 20054ef0 Blue Swirl
    }
634 2999a0b2 Blue Swirl
    if (load_segment(env, &e1, &e2, selector) != 0) {
635 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
636 20054ef0 Blue Swirl
    }
637 20054ef0 Blue Swirl
    if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) {
638 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
639 20054ef0 Blue Swirl
    }
640 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
641 20054ef0 Blue Swirl
    if (dpl > cpl) {
642 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
643 20054ef0 Blue Swirl
    }
644 20054ef0 Blue Swirl
    if (!(e2 & DESC_P_MASK)) {
645 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
646 20054ef0 Blue Swirl
    }
647 eaa728ee bellard
    if (!(e2 & DESC_C_MASK) && dpl < cpl) {
648 eaa728ee bellard
        /* to inner privilege */
649 2999a0b2 Blue Swirl
        get_ss_esp_from_tss(env, &ss, &esp, dpl);
650 20054ef0 Blue Swirl
        if ((ss & 0xfffc) == 0) {
651 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
652 20054ef0 Blue Swirl
        }
653 20054ef0 Blue Swirl
        if ((ss & 3) != dpl) {
654 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
655 20054ef0 Blue Swirl
        }
656 2999a0b2 Blue Swirl
        if (load_segment(env, &ss_e1, &ss_e2, ss) != 0) {
657 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
658 20054ef0 Blue Swirl
        }
659 eaa728ee bellard
        ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
660 20054ef0 Blue Swirl
        if (ss_dpl != dpl) {
661 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
662 20054ef0 Blue Swirl
        }
663 eaa728ee bellard
        if (!(ss_e2 & DESC_S_MASK) ||
664 eaa728ee bellard
            (ss_e2 & DESC_CS_MASK) ||
665 20054ef0 Blue Swirl
            !(ss_e2 & DESC_W_MASK)) {
666 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
667 20054ef0 Blue Swirl
        }
668 20054ef0 Blue Swirl
        if (!(ss_e2 & DESC_P_MASK)) {
669 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
670 20054ef0 Blue Swirl
        }
671 eaa728ee bellard
        new_stack = 1;
672 eaa728ee bellard
        sp_mask = get_sp_mask(ss_e2);
673 eaa728ee bellard
        ssp = get_seg_base(ss_e1, ss_e2);
674 eaa728ee bellard
    } else if ((e2 & DESC_C_MASK) || dpl == cpl) {
675 eaa728ee bellard
        /* to same privilege */
676 20054ef0 Blue Swirl
        if (env->eflags & VM_MASK) {
677 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
678 20054ef0 Blue Swirl
        }
679 eaa728ee bellard
        new_stack = 0;
680 eaa728ee bellard
        sp_mask = get_sp_mask(env->segs[R_SS].flags);
681 eaa728ee bellard
        ssp = env->segs[R_SS].base;
682 eaa728ee bellard
        esp = ESP;
683 eaa728ee bellard
        dpl = cpl;
684 eaa728ee bellard
    } else {
685 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
686 eaa728ee bellard
        new_stack = 0; /* avoid warning */
687 eaa728ee bellard
        sp_mask = 0; /* avoid warning */
688 eaa728ee bellard
        ssp = 0; /* avoid warning */
689 eaa728ee bellard
        esp = 0; /* avoid warning */
690 eaa728ee bellard
    }
691 eaa728ee bellard
692 eaa728ee bellard
    shift = type >> 3;
693 eaa728ee bellard
694 eaa728ee bellard
#if 0
695 eaa728ee bellard
    /* XXX: check that enough room is available */
696 eaa728ee bellard
    push_size = 6 + (new_stack << 2) + (has_error_code << 1);
697 20054ef0 Blue Swirl
    if (env->eflags & VM_MASK) {
698 eaa728ee bellard
        push_size += 8;
699 20054ef0 Blue Swirl
    }
700 eaa728ee bellard
    push_size <<= shift;
701 eaa728ee bellard
#endif
702 eaa728ee bellard
    if (shift == 1) {
703 eaa728ee bellard
        if (new_stack) {
704 eaa728ee bellard
            if (env->eflags & VM_MASK) {
705 eaa728ee bellard
                PUSHL(ssp, esp, sp_mask, env->segs[R_GS].selector);
706 eaa728ee bellard
                PUSHL(ssp, esp, sp_mask, env->segs[R_FS].selector);
707 eaa728ee bellard
                PUSHL(ssp, esp, sp_mask, env->segs[R_DS].selector);
708 eaa728ee bellard
                PUSHL(ssp, esp, sp_mask, env->segs[R_ES].selector);
709 eaa728ee bellard
            }
710 eaa728ee bellard
            PUSHL(ssp, esp, sp_mask, env->segs[R_SS].selector);
711 eaa728ee bellard
            PUSHL(ssp, esp, sp_mask, ESP);
712 eaa728ee bellard
        }
713 997ff0d9 Blue Swirl
        PUSHL(ssp, esp, sp_mask, cpu_compute_eflags(env));
714 eaa728ee bellard
        PUSHL(ssp, esp, sp_mask, env->segs[R_CS].selector);
715 eaa728ee bellard
        PUSHL(ssp, esp, sp_mask, old_eip);
716 eaa728ee bellard
        if (has_error_code) {
717 eaa728ee bellard
            PUSHL(ssp, esp, sp_mask, error_code);
718 eaa728ee bellard
        }
719 eaa728ee bellard
    } else {
720 eaa728ee bellard
        if (new_stack) {
721 eaa728ee bellard
            if (env->eflags & VM_MASK) {
722 eaa728ee bellard
                PUSHW(ssp, esp, sp_mask, env->segs[R_GS].selector);
723 eaa728ee bellard
                PUSHW(ssp, esp, sp_mask, env->segs[R_FS].selector);
724 eaa728ee bellard
                PUSHW(ssp, esp, sp_mask, env->segs[R_DS].selector);
725 eaa728ee bellard
                PUSHW(ssp, esp, sp_mask, env->segs[R_ES].selector);
726 eaa728ee bellard
            }
727 eaa728ee bellard
            PUSHW(ssp, esp, sp_mask, env->segs[R_SS].selector);
728 eaa728ee bellard
            PUSHW(ssp, esp, sp_mask, ESP);
729 eaa728ee bellard
        }
730 997ff0d9 Blue Swirl
        PUSHW(ssp, esp, sp_mask, cpu_compute_eflags(env));
731 eaa728ee bellard
        PUSHW(ssp, esp, sp_mask, env->segs[R_CS].selector);
732 eaa728ee bellard
        PUSHW(ssp, esp, sp_mask, old_eip);
733 eaa728ee bellard
        if (has_error_code) {
734 eaa728ee bellard
            PUSHW(ssp, esp, sp_mask, error_code);
735 eaa728ee bellard
        }
736 eaa728ee bellard
    }
737 eaa728ee bellard
738 eaa728ee bellard
    if (new_stack) {
739 eaa728ee bellard
        if (env->eflags & VM_MASK) {
740 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0, 0);
741 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0, 0);
742 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0, 0);
743 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0, 0);
744 eaa728ee bellard
        }
745 eaa728ee bellard
        ss = (ss & ~3) | dpl;
746 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_SS, ss,
747 eaa728ee bellard
                               ssp, get_seg_limit(ss_e1, ss_e2), ss_e2);
748 eaa728ee bellard
    }
749 eaa728ee bellard
    SET_ESP(esp, sp_mask);
750 eaa728ee bellard
751 eaa728ee bellard
    selector = (selector & ~3) | dpl;
752 eaa728ee bellard
    cpu_x86_load_seg_cache(env, R_CS, selector,
753 eaa728ee bellard
                   get_seg_base(e1, e2),
754 eaa728ee bellard
                   get_seg_limit(e1, e2),
755 eaa728ee bellard
                   e2);
756 eaa728ee bellard
    cpu_x86_set_cpl(env, dpl);
757 eaa728ee bellard
    env->eip = offset;
758 eaa728ee bellard
759 eaa728ee bellard
    /* interrupt gate clear IF mask */
760 eaa728ee bellard
    if ((type & 1) == 0) {
761 eaa728ee bellard
        env->eflags &= ~IF_MASK;
762 eaa728ee bellard
    }
763 eaa728ee bellard
    env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
764 eaa728ee bellard
}
765 eaa728ee bellard
766 eaa728ee bellard
#ifdef TARGET_X86_64
767 eaa728ee bellard
768 20054ef0 Blue Swirl
#define PUSHQ(sp, val)                          \
769 20054ef0 Blue Swirl
    {                                           \
770 20054ef0 Blue Swirl
        sp -= 8;                                \
771 329e607d Blue Swirl
        cpu_stq_kernel(env, sp, (val));         \
772 20054ef0 Blue Swirl
    }
773 eaa728ee bellard
774 20054ef0 Blue Swirl
#define POPQ(sp, val)                           \
775 20054ef0 Blue Swirl
    {                                           \
776 329e607d Blue Swirl
        val = cpu_ldq_kernel(env, sp);          \
777 20054ef0 Blue Swirl
        sp += 8;                                \
778 20054ef0 Blue Swirl
    }
779 eaa728ee bellard
780 2999a0b2 Blue Swirl
static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level)
781 eaa728ee bellard
{
782 eaa728ee bellard
    int index;
783 eaa728ee bellard
784 eaa728ee bellard
#if 0
785 eaa728ee bellard
    printf("TR: base=" TARGET_FMT_lx " limit=%x\n",
786 eaa728ee bellard
           env->tr.base, env->tr.limit);
787 eaa728ee bellard
#endif
788 eaa728ee bellard
789 20054ef0 Blue Swirl
    if (!(env->tr.flags & DESC_P_MASK)) {
790 eaa728ee bellard
        cpu_abort(env, "invalid tss");
791 20054ef0 Blue Swirl
    }
792 eaa728ee bellard
    index = 8 * level + 4;
793 20054ef0 Blue Swirl
    if ((index + 7) > env->tr.limit) {
794 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0A_TSS, env->tr.selector & 0xfffc);
795 20054ef0 Blue Swirl
    }
796 329e607d Blue Swirl
    return cpu_ldq_kernel(env, env->tr.base + index);
797 eaa728ee bellard
}
798 eaa728ee bellard
799 eaa728ee bellard
/* 64 bit interrupt */
800 2999a0b2 Blue Swirl
static void do_interrupt64(CPUX86State *env, int intno, int is_int,
801 2999a0b2 Blue Swirl
                           int error_code, target_ulong next_eip, int is_hw)
802 eaa728ee bellard
{
803 eaa728ee bellard
    SegmentCache *dt;
804 eaa728ee bellard
    target_ulong ptr;
805 eaa728ee bellard
    int type, dpl, selector, cpl, ist;
806 eaa728ee bellard
    int has_error_code, new_stack;
807 eaa728ee bellard
    uint32_t e1, e2, e3, ss;
808 eaa728ee bellard
    target_ulong old_eip, esp, offset;
809 eaa728ee bellard
810 eaa728ee bellard
    has_error_code = 0;
811 20054ef0 Blue Swirl
    if (!is_int && !is_hw) {
812 20054ef0 Blue Swirl
        has_error_code = exception_has_error_code(intno);
813 20054ef0 Blue Swirl
    }
814 20054ef0 Blue Swirl
    if (is_int) {
815 eaa728ee bellard
        old_eip = next_eip;
816 20054ef0 Blue Swirl
    } else {
817 eaa728ee bellard
        old_eip = env->eip;
818 20054ef0 Blue Swirl
    }
819 eaa728ee bellard
820 eaa728ee bellard
    dt = &env->idt;
821 20054ef0 Blue Swirl
    if (intno * 16 + 15 > dt->limit) {
822 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, intno * 16 + 2);
823 20054ef0 Blue Swirl
    }
824 eaa728ee bellard
    ptr = dt->base + intno * 16;
825 329e607d Blue Swirl
    e1 = cpu_ldl_kernel(env, ptr);
826 329e607d Blue Swirl
    e2 = cpu_ldl_kernel(env, ptr + 4);
827 329e607d Blue Swirl
    e3 = cpu_ldl_kernel(env, ptr + 8);
828 eaa728ee bellard
    /* check gate type */
829 eaa728ee bellard
    type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
830 20054ef0 Blue Swirl
    switch (type) {
831 eaa728ee bellard
    case 14: /* 386 interrupt gate */
832 eaa728ee bellard
    case 15: /* 386 trap gate */
833 eaa728ee bellard
        break;
834 eaa728ee bellard
    default:
835 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, intno * 16 + 2);
836 eaa728ee bellard
        break;
837 eaa728ee bellard
    }
838 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
839 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
840 1235fc06 ths
    /* check privilege if software int */
841 20054ef0 Blue Swirl
    if (is_int && dpl < cpl) {
842 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, intno * 16 + 2);
843 20054ef0 Blue Swirl
    }
844 eaa728ee bellard
    /* check valid bit */
845 20054ef0 Blue Swirl
    if (!(e2 & DESC_P_MASK)) {
846 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0B_NOSEG, intno * 16 + 2);
847 20054ef0 Blue Swirl
    }
848 eaa728ee bellard
    selector = e1 >> 16;
849 eaa728ee bellard
    offset = ((target_ulong)e3 << 32) | (e2 & 0xffff0000) | (e1 & 0x0000ffff);
850 eaa728ee bellard
    ist = e2 & 7;
851 20054ef0 Blue Swirl
    if ((selector & 0xfffc) == 0) {
852 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, 0);
853 20054ef0 Blue Swirl
    }
854 eaa728ee bellard
855 2999a0b2 Blue Swirl
    if (load_segment(env, &e1, &e2, selector) != 0) {
856 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
857 20054ef0 Blue Swirl
    }
858 20054ef0 Blue Swirl
    if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) {
859 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
860 20054ef0 Blue Swirl
    }
861 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
862 20054ef0 Blue Swirl
    if (dpl > cpl) {
863 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
864 20054ef0 Blue Swirl
    }
865 20054ef0 Blue Swirl
    if (!(e2 & DESC_P_MASK)) {
866 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
867 20054ef0 Blue Swirl
    }
868 20054ef0 Blue Swirl
    if (!(e2 & DESC_L_MASK) || (e2 & DESC_B_MASK)) {
869 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
870 20054ef0 Blue Swirl
    }
871 eaa728ee bellard
    if ((!(e2 & DESC_C_MASK) && dpl < cpl) || ist != 0) {
872 eaa728ee bellard
        /* to inner privilege */
873 20054ef0 Blue Swirl
        if (ist != 0) {
874 2999a0b2 Blue Swirl
            esp = get_rsp_from_tss(env, ist + 3);
875 20054ef0 Blue Swirl
        } else {
876 2999a0b2 Blue Swirl
            esp = get_rsp_from_tss(env, dpl);
877 20054ef0 Blue Swirl
        }
878 eaa728ee bellard
        esp &= ~0xfLL; /* align stack */
879 eaa728ee bellard
        ss = 0;
880 eaa728ee bellard
        new_stack = 1;
881 eaa728ee bellard
    } else if ((e2 & DESC_C_MASK) || dpl == cpl) {
882 eaa728ee bellard
        /* to same privilege */
883 20054ef0 Blue Swirl
        if (env->eflags & VM_MASK) {
884 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
885 20054ef0 Blue Swirl
        }
886 eaa728ee bellard
        new_stack = 0;
887 20054ef0 Blue Swirl
        if (ist != 0) {
888 2999a0b2 Blue Swirl
            esp = get_rsp_from_tss(env, ist + 3);
889 20054ef0 Blue Swirl
        } else {
890 eaa728ee bellard
            esp = ESP;
891 20054ef0 Blue Swirl
        }
892 eaa728ee bellard
        esp &= ~0xfLL; /* align stack */
893 eaa728ee bellard
        dpl = cpl;
894 eaa728ee bellard
    } else {
895 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
896 eaa728ee bellard
        new_stack = 0; /* avoid warning */
897 eaa728ee bellard
        esp = 0; /* avoid warning */
898 eaa728ee bellard
    }
899 eaa728ee bellard
900 eaa728ee bellard
    PUSHQ(esp, env->segs[R_SS].selector);
901 eaa728ee bellard
    PUSHQ(esp, ESP);
902 997ff0d9 Blue Swirl
    PUSHQ(esp, cpu_compute_eflags(env));
903 eaa728ee bellard
    PUSHQ(esp, env->segs[R_CS].selector);
904 eaa728ee bellard
    PUSHQ(esp, old_eip);
905 eaa728ee bellard
    if (has_error_code) {
906 eaa728ee bellard
        PUSHQ(esp, error_code);
907 eaa728ee bellard
    }
908 eaa728ee bellard
909 eaa728ee bellard
    if (new_stack) {
910 eaa728ee bellard
        ss = 0 | dpl;
911 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_SS, ss, 0, 0, 0);
912 eaa728ee bellard
    }
913 eaa728ee bellard
    ESP = esp;
914 eaa728ee bellard
915 eaa728ee bellard
    selector = (selector & ~3) | dpl;
916 eaa728ee bellard
    cpu_x86_load_seg_cache(env, R_CS, selector,
917 eaa728ee bellard
                   get_seg_base(e1, e2),
918 eaa728ee bellard
                   get_seg_limit(e1, e2),
919 eaa728ee bellard
                   e2);
920 eaa728ee bellard
    cpu_x86_set_cpl(env, dpl);
921 eaa728ee bellard
    env->eip = offset;
922 eaa728ee bellard
923 eaa728ee bellard
    /* interrupt gate clear IF mask */
924 eaa728ee bellard
    if ((type & 1) == 0) {
925 eaa728ee bellard
        env->eflags &= ~IF_MASK;
926 eaa728ee bellard
    }
927 eaa728ee bellard
    env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
928 eaa728ee bellard
}
929 eaa728ee bellard
#endif
930 eaa728ee bellard
931 d9957a8b blueswir1
#ifdef TARGET_X86_64
932 eaa728ee bellard
#if defined(CONFIG_USER_ONLY)
933 2999a0b2 Blue Swirl
void helper_syscall(CPUX86State *env, int next_eip_addend)
934 eaa728ee bellard
{
935 eaa728ee bellard
    env->exception_index = EXCP_SYSCALL;
936 eaa728ee bellard
    env->exception_next_eip = env->eip + next_eip_addend;
937 1162c041 Blue Swirl
    cpu_loop_exit(env);
938 eaa728ee bellard
}
939 eaa728ee bellard
#else
940 2999a0b2 Blue Swirl
void helper_syscall(CPUX86State *env, int next_eip_addend)
941 eaa728ee bellard
{
942 eaa728ee bellard
    int selector;
943 eaa728ee bellard
944 eaa728ee bellard
    if (!(env->efer & MSR_EFER_SCE)) {
945 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP06_ILLOP, 0);
946 eaa728ee bellard
    }
947 eaa728ee bellard
    selector = (env->star >> 32) & 0xffff;
948 eaa728ee bellard
    if (env->hflags & HF_LMA_MASK) {
949 eaa728ee bellard
        int code64;
950 eaa728ee bellard
951 eaa728ee bellard
        ECX = env->eip + next_eip_addend;
952 997ff0d9 Blue Swirl
        env->regs[11] = cpu_compute_eflags(env);
953 eaa728ee bellard
954 eaa728ee bellard
        code64 = env->hflags & HF_CS64_MASK;
955 eaa728ee bellard
956 eaa728ee bellard
        cpu_x86_set_cpl(env, 0);
957 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc,
958 eaa728ee bellard
                           0, 0xffffffff,
959 eaa728ee bellard
                               DESC_G_MASK | DESC_P_MASK |
960 eaa728ee bellard
                               DESC_S_MASK |
961 20054ef0 Blue Swirl
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK |
962 20054ef0 Blue Swirl
                               DESC_L_MASK);
963 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc,
964 eaa728ee bellard
                               0, 0xffffffff,
965 eaa728ee bellard
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
966 eaa728ee bellard
                               DESC_S_MASK |
967 eaa728ee bellard
                               DESC_W_MASK | DESC_A_MASK);
968 eaa728ee bellard
        env->eflags &= ~env->fmask;
969 997ff0d9 Blue Swirl
        cpu_load_eflags(env, env->eflags, 0);
970 20054ef0 Blue Swirl
        if (code64) {
971 eaa728ee bellard
            env->eip = env->lstar;
972 20054ef0 Blue Swirl
        } else {
973 eaa728ee bellard
            env->eip = env->cstar;
974 20054ef0 Blue Swirl
        }
975 d9957a8b blueswir1
    } else {
976 eaa728ee bellard
        ECX = (uint32_t)(env->eip + next_eip_addend);
977 eaa728ee bellard
978 eaa728ee bellard
        cpu_x86_set_cpl(env, 0);
979 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc,
980 eaa728ee bellard
                           0, 0xffffffff,
981 eaa728ee bellard
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
982 eaa728ee bellard
                               DESC_S_MASK |
983 eaa728ee bellard
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
984 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc,
985 eaa728ee bellard
                               0, 0xffffffff,
986 eaa728ee bellard
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
987 eaa728ee bellard
                               DESC_S_MASK |
988 eaa728ee bellard
                               DESC_W_MASK | DESC_A_MASK);
989 eaa728ee bellard
        env->eflags &= ~(IF_MASK | RF_MASK | VM_MASK);
990 eaa728ee bellard
        env->eip = (uint32_t)env->star;
991 eaa728ee bellard
    }
992 eaa728ee bellard
}
993 eaa728ee bellard
#endif
994 d9957a8b blueswir1
#endif
995 eaa728ee bellard
996 d9957a8b blueswir1
#ifdef TARGET_X86_64
997 2999a0b2 Blue Swirl
void helper_sysret(CPUX86State *env, int dflag)
998 eaa728ee bellard
{
999 eaa728ee bellard
    int cpl, selector;
1000 eaa728ee bellard
1001 eaa728ee bellard
    if (!(env->efer & MSR_EFER_SCE)) {
1002 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP06_ILLOP, 0);
1003 eaa728ee bellard
    }
1004 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
1005 eaa728ee bellard
    if (!(env->cr[0] & CR0_PE_MASK) || cpl != 0) {
1006 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, 0);
1007 eaa728ee bellard
    }
1008 eaa728ee bellard
    selector = (env->star >> 48) & 0xffff;
1009 eaa728ee bellard
    if (env->hflags & HF_LMA_MASK) {
1010 eaa728ee bellard
        if (dflag == 2) {
1011 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_CS, (selector + 16) | 3,
1012 eaa728ee bellard
                                   0, 0xffffffff,
1013 eaa728ee bellard
                                   DESC_G_MASK | DESC_P_MASK |
1014 eaa728ee bellard
                                   DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1015 eaa728ee bellard
                                   DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK |
1016 eaa728ee bellard
                                   DESC_L_MASK);
1017 eaa728ee bellard
            env->eip = ECX;
1018 eaa728ee bellard
        } else {
1019 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_CS, selector | 3,
1020 eaa728ee bellard
                                   0, 0xffffffff,
1021 eaa728ee bellard
                                   DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1022 eaa728ee bellard
                                   DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1023 eaa728ee bellard
                                   DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
1024 eaa728ee bellard
            env->eip = (uint32_t)ECX;
1025 eaa728ee bellard
        }
1026 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_SS, selector + 8,
1027 eaa728ee bellard
                               0, 0xffffffff,
1028 eaa728ee bellard
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1029 eaa728ee bellard
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1030 eaa728ee bellard
                               DESC_W_MASK | DESC_A_MASK);
1031 997ff0d9 Blue Swirl
        cpu_load_eflags(env, (uint32_t)(env->regs[11]), TF_MASK | AC_MASK
1032 997ff0d9 Blue Swirl
                        | ID_MASK | IF_MASK | IOPL_MASK | VM_MASK | RF_MASK |
1033 997ff0d9 Blue Swirl
                        NT_MASK);
1034 eaa728ee bellard
        cpu_x86_set_cpl(env, 3);
1035 d9957a8b blueswir1
    } else {
1036 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_CS, selector | 3,
1037 eaa728ee bellard
                               0, 0xffffffff,
1038 eaa728ee bellard
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1039 eaa728ee bellard
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1040 eaa728ee bellard
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
1041 eaa728ee bellard
        env->eip = (uint32_t)ECX;
1042 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_SS, selector + 8,
1043 eaa728ee bellard
                               0, 0xffffffff,
1044 eaa728ee bellard
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
1045 eaa728ee bellard
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1046 eaa728ee bellard
                               DESC_W_MASK | DESC_A_MASK);
1047 eaa728ee bellard
        env->eflags |= IF_MASK;
1048 eaa728ee bellard
        cpu_x86_set_cpl(env, 3);
1049 eaa728ee bellard
    }
1050 eaa728ee bellard
}
1051 d9957a8b blueswir1
#endif
1052 eaa728ee bellard
1053 eaa728ee bellard
/* real mode interrupt */
1054 2999a0b2 Blue Swirl
static void do_interrupt_real(CPUX86State *env, int intno, int is_int,
1055 2999a0b2 Blue Swirl
                              int error_code, unsigned int next_eip)
1056 eaa728ee bellard
{
1057 eaa728ee bellard
    SegmentCache *dt;
1058 eaa728ee bellard
    target_ulong ptr, ssp;
1059 eaa728ee bellard
    int selector;
1060 eaa728ee bellard
    uint32_t offset, esp;
1061 eaa728ee bellard
    uint32_t old_cs, old_eip;
1062 eaa728ee bellard
1063 20054ef0 Blue Swirl
    /* real mode (simpler!) */
1064 eaa728ee bellard
    dt = &env->idt;
1065 20054ef0 Blue Swirl
    if (intno * 4 + 3 > dt->limit) {
1066 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2);
1067 20054ef0 Blue Swirl
    }
1068 eaa728ee bellard
    ptr = dt->base + intno * 4;
1069 329e607d Blue Swirl
    offset = cpu_lduw_kernel(env, ptr);
1070 329e607d Blue Swirl
    selector = cpu_lduw_kernel(env, ptr + 2);
1071 eaa728ee bellard
    esp = ESP;
1072 eaa728ee bellard
    ssp = env->segs[R_SS].base;
1073 20054ef0 Blue Swirl
    if (is_int) {
1074 eaa728ee bellard
        old_eip = next_eip;
1075 20054ef0 Blue Swirl
    } else {
1076 eaa728ee bellard
        old_eip = env->eip;
1077 20054ef0 Blue Swirl
    }
1078 eaa728ee bellard
    old_cs = env->segs[R_CS].selector;
1079 20054ef0 Blue Swirl
    /* XXX: use SS segment size? */
1080 997ff0d9 Blue Swirl
    PUSHW(ssp, esp, 0xffff, cpu_compute_eflags(env));
1081 eaa728ee bellard
    PUSHW(ssp, esp, 0xffff, old_cs);
1082 eaa728ee bellard
    PUSHW(ssp, esp, 0xffff, old_eip);
1083 eaa728ee bellard
1084 eaa728ee bellard
    /* update processor state */
1085 eaa728ee bellard
    ESP = (ESP & ~0xffff) | (esp & 0xffff);
1086 eaa728ee bellard
    env->eip = offset;
1087 eaa728ee bellard
    env->segs[R_CS].selector = selector;
1088 eaa728ee bellard
    env->segs[R_CS].base = (selector << 4);
1089 eaa728ee bellard
    env->eflags &= ~(IF_MASK | TF_MASK | AC_MASK | RF_MASK);
1090 eaa728ee bellard
}
1091 eaa728ee bellard
1092 e694d4e2 Blue Swirl
#if defined(CONFIG_USER_ONLY)
1093 eaa728ee bellard
/* fake user mode interrupt */
1094 2999a0b2 Blue Swirl
static void do_interrupt_user(CPUX86State *env, int intno, int is_int,
1095 2999a0b2 Blue Swirl
                              int error_code, target_ulong next_eip)
1096 eaa728ee bellard
{
1097 eaa728ee bellard
    SegmentCache *dt;
1098 eaa728ee bellard
    target_ulong ptr;
1099 eaa728ee bellard
    int dpl, cpl, shift;
1100 eaa728ee bellard
    uint32_t e2;
1101 eaa728ee bellard
1102 eaa728ee bellard
    dt = &env->idt;
1103 eaa728ee bellard
    if (env->hflags & HF_LMA_MASK) {
1104 eaa728ee bellard
        shift = 4;
1105 eaa728ee bellard
    } else {
1106 eaa728ee bellard
        shift = 3;
1107 eaa728ee bellard
    }
1108 eaa728ee bellard
    ptr = dt->base + (intno << shift);
1109 329e607d Blue Swirl
    e2 = cpu_ldl_kernel(env, ptr + 4);
1110 eaa728ee bellard
1111 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1112 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
1113 1235fc06 ths
    /* check privilege if software int */
1114 20054ef0 Blue Swirl
    if (is_int && dpl < cpl) {
1115 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, (intno << shift) + 2);
1116 20054ef0 Blue Swirl
    }
1117 eaa728ee bellard
1118 eaa728ee bellard
    /* Since we emulate only user space, we cannot do more than
1119 eaa728ee bellard
       exiting the emulation with the suitable exception and error
1120 eaa728ee bellard
       code */
1121 20054ef0 Blue Swirl
    if (is_int) {
1122 eaa728ee bellard
        EIP = next_eip;
1123 20054ef0 Blue Swirl
    }
1124 eaa728ee bellard
}
1125 eaa728ee bellard
1126 e694d4e2 Blue Swirl
#else
1127 e694d4e2 Blue Swirl
1128 2999a0b2 Blue Swirl
static void handle_even_inj(CPUX86State *env, int intno, int is_int,
1129 2999a0b2 Blue Swirl
                            int error_code, int is_hw, int rm)
1130 2ed51f5b aliguori
{
1131 20054ef0 Blue Swirl
    uint32_t event_inj = ldl_phys(env->vm_vmcb + offsetof(struct vmcb,
1132 20054ef0 Blue Swirl
                                                          control.event_inj));
1133 20054ef0 Blue Swirl
1134 2ed51f5b aliguori
    if (!(event_inj & SVM_EVTINJ_VALID)) {
1135 20054ef0 Blue Swirl
        int type;
1136 20054ef0 Blue Swirl
1137 20054ef0 Blue Swirl
        if (is_int) {
1138 20054ef0 Blue Swirl
            type = SVM_EVTINJ_TYPE_SOFT;
1139 20054ef0 Blue Swirl
        } else {
1140 20054ef0 Blue Swirl
            type = SVM_EVTINJ_TYPE_EXEPT;
1141 20054ef0 Blue Swirl
        }
1142 20054ef0 Blue Swirl
        event_inj = intno | type | SVM_EVTINJ_VALID;
1143 20054ef0 Blue Swirl
        if (!rm && exception_has_error_code(intno)) {
1144 20054ef0 Blue Swirl
            event_inj |= SVM_EVTINJ_VALID_ERR;
1145 20054ef0 Blue Swirl
            stl_phys(env->vm_vmcb + offsetof(struct vmcb,
1146 20054ef0 Blue Swirl
                                             control.event_inj_err),
1147 20054ef0 Blue Swirl
                     error_code);
1148 20054ef0 Blue Swirl
        }
1149 20054ef0 Blue Swirl
        stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.event_inj),
1150 20054ef0 Blue Swirl
                 event_inj);
1151 2ed51f5b aliguori
    }
1152 2ed51f5b aliguori
}
1153 00ea18d1 aliguori
#endif
1154 2ed51f5b aliguori
1155 eaa728ee bellard
/*
1156 eaa728ee bellard
 * Begin execution of an interruption. is_int is TRUE if coming from
1157 eaa728ee bellard
 * the int instruction. next_eip is the EIP value AFTER the interrupt
1158 eaa728ee bellard
 * instruction. It is only relevant if is_int is TRUE.
1159 eaa728ee bellard
 */
1160 2999a0b2 Blue Swirl
static void do_interrupt_all(CPUX86State *env, int intno, int is_int,
1161 2999a0b2 Blue Swirl
                             int error_code, target_ulong next_eip, int is_hw)
1162 eaa728ee bellard
{
1163 8fec2b8c aliguori
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
1164 eaa728ee bellard
        if ((env->cr[0] & CR0_PE_MASK)) {
1165 eaa728ee bellard
            static int count;
1166 20054ef0 Blue Swirl
1167 20054ef0 Blue Swirl
            qemu_log("%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx
1168 20054ef0 Blue Swirl
                     " pc=" TARGET_FMT_lx " SP=%04x:" TARGET_FMT_lx,
1169 20054ef0 Blue Swirl
                     count, intno, error_code, is_int,
1170 20054ef0 Blue Swirl
                     env->hflags & HF_CPL_MASK,
1171 20054ef0 Blue Swirl
                     env->segs[R_CS].selector, EIP,
1172 20054ef0 Blue Swirl
                     (int)env->segs[R_CS].base + EIP,
1173 20054ef0 Blue Swirl
                     env->segs[R_SS].selector, ESP);
1174 eaa728ee bellard
            if (intno == 0x0e) {
1175 93fcfe39 aliguori
                qemu_log(" CR2=" TARGET_FMT_lx, env->cr[2]);
1176 eaa728ee bellard
            } else {
1177 93fcfe39 aliguori
                qemu_log(" EAX=" TARGET_FMT_lx, EAX);
1178 eaa728ee bellard
            }
1179 93fcfe39 aliguori
            qemu_log("\n");
1180 6fd2a026 Peter Maydell
            log_cpu_state(env, CPU_DUMP_CCOP);
1181 eaa728ee bellard
#if 0
1182 eaa728ee bellard
            {
1183 eaa728ee bellard
                int i;
1184 9bd5494e Adam Lackorzynski
                target_ulong ptr;
1185 20054ef0 Blue Swirl

1186 93fcfe39 aliguori
                qemu_log("       code=");
1187 eaa728ee bellard
                ptr = env->segs[R_CS].base + env->eip;
1188 20054ef0 Blue Swirl
                for (i = 0; i < 16; i++) {
1189 93fcfe39 aliguori
                    qemu_log(" %02x", ldub(ptr + i));
1190 eaa728ee bellard
                }
1191 93fcfe39 aliguori
                qemu_log("\n");
1192 eaa728ee bellard
            }
1193 eaa728ee bellard
#endif
1194 eaa728ee bellard
            count++;
1195 eaa728ee bellard
        }
1196 eaa728ee bellard
    }
1197 eaa728ee bellard
    if (env->cr[0] & CR0_PE_MASK) {
1198 00ea18d1 aliguori
#if !defined(CONFIG_USER_ONLY)
1199 20054ef0 Blue Swirl
        if (env->hflags & HF_SVMI_MASK) {
1200 2999a0b2 Blue Swirl
            handle_even_inj(env, intno, is_int, error_code, is_hw, 0);
1201 20054ef0 Blue Swirl
        }
1202 00ea18d1 aliguori
#endif
1203 eb38c52c blueswir1
#ifdef TARGET_X86_64
1204 eaa728ee bellard
        if (env->hflags & HF_LMA_MASK) {
1205 2999a0b2 Blue Swirl
            do_interrupt64(env, intno, is_int, error_code, next_eip, is_hw);
1206 eaa728ee bellard
        } else
1207 eaa728ee bellard
#endif
1208 eaa728ee bellard
        {
1209 2999a0b2 Blue Swirl
            do_interrupt_protected(env, intno, is_int, error_code, next_eip,
1210 2999a0b2 Blue Swirl
                                   is_hw);
1211 eaa728ee bellard
        }
1212 eaa728ee bellard
    } else {
1213 00ea18d1 aliguori
#if !defined(CONFIG_USER_ONLY)
1214 20054ef0 Blue Swirl
        if (env->hflags & HF_SVMI_MASK) {
1215 2999a0b2 Blue Swirl
            handle_even_inj(env, intno, is_int, error_code, is_hw, 1);
1216 20054ef0 Blue Swirl
        }
1217 00ea18d1 aliguori
#endif
1218 2999a0b2 Blue Swirl
        do_interrupt_real(env, intno, is_int, error_code, next_eip);
1219 eaa728ee bellard
    }
1220 2ed51f5b aliguori
1221 00ea18d1 aliguori
#if !defined(CONFIG_USER_ONLY)
1222 2ed51f5b aliguori
    if (env->hflags & HF_SVMI_MASK) {
1223 20054ef0 Blue Swirl
        uint32_t event_inj = ldl_phys(env->vm_vmcb +
1224 20054ef0 Blue Swirl
                                      offsetof(struct vmcb,
1225 20054ef0 Blue Swirl
                                               control.event_inj));
1226 20054ef0 Blue Swirl
1227 20054ef0 Blue Swirl
        stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.event_inj),
1228 20054ef0 Blue Swirl
                 event_inj & ~SVM_EVTINJ_VALID);
1229 2ed51f5b aliguori
    }
1230 00ea18d1 aliguori
#endif
1231 eaa728ee bellard
}
1232 eaa728ee bellard
1233 2999a0b2 Blue Swirl
void do_interrupt(CPUX86State *env)
1234 e694d4e2 Blue Swirl
{
1235 e694d4e2 Blue Swirl
#if defined(CONFIG_USER_ONLY)
1236 e694d4e2 Blue Swirl
    /* if user mode only, we simulate a fake exception
1237 e694d4e2 Blue Swirl
       which will be handled outside the cpu execution
1238 e694d4e2 Blue Swirl
       loop */
1239 2999a0b2 Blue Swirl
    do_interrupt_user(env, env->exception_index,
1240 e694d4e2 Blue Swirl
                      env->exception_is_int,
1241 e694d4e2 Blue Swirl
                      env->error_code,
1242 e694d4e2 Blue Swirl
                      env->exception_next_eip);
1243 e694d4e2 Blue Swirl
    /* successfully delivered */
1244 e694d4e2 Blue Swirl
    env->old_exception = -1;
1245 e694d4e2 Blue Swirl
#else
1246 e694d4e2 Blue Swirl
    /* simulate a real cpu exception. On i386, it can
1247 e694d4e2 Blue Swirl
       trigger new exceptions, but we do not handle
1248 e694d4e2 Blue Swirl
       double or triple faults yet. */
1249 2999a0b2 Blue Swirl
    do_interrupt_all(env, env->exception_index,
1250 e694d4e2 Blue Swirl
                     env->exception_is_int,
1251 e694d4e2 Blue Swirl
                     env->error_code,
1252 e694d4e2 Blue Swirl
                     env->exception_next_eip, 0);
1253 e694d4e2 Blue Swirl
    /* successfully delivered */
1254 e694d4e2 Blue Swirl
    env->old_exception = -1;
1255 e694d4e2 Blue Swirl
#endif
1256 e694d4e2 Blue Swirl
}
1257 e694d4e2 Blue Swirl
1258 2999a0b2 Blue Swirl
void do_interrupt_x86_hardirq(CPUX86State *env, int intno, int is_hw)
1259 e694d4e2 Blue Swirl
{
1260 2999a0b2 Blue Swirl
    do_interrupt_all(env, intno, 0, 0, 0, is_hw);
1261 e694d4e2 Blue Swirl
}
1262 e694d4e2 Blue Swirl
1263 2999a0b2 Blue Swirl
void helper_enter_level(CPUX86State *env, int level, int data32,
1264 2999a0b2 Blue Swirl
                        target_ulong t1)
1265 eaa728ee bellard
{
1266 eaa728ee bellard
    target_ulong ssp;
1267 eaa728ee bellard
    uint32_t esp_mask, esp, ebp;
1268 eaa728ee bellard
1269 eaa728ee bellard
    esp_mask = get_sp_mask(env->segs[R_SS].flags);
1270 eaa728ee bellard
    ssp = env->segs[R_SS].base;
1271 eaa728ee bellard
    ebp = EBP;
1272 eaa728ee bellard
    esp = ESP;
1273 eaa728ee bellard
    if (data32) {
1274 eaa728ee bellard
        /* 32 bit */
1275 eaa728ee bellard
        esp -= 4;
1276 eaa728ee bellard
        while (--level) {
1277 eaa728ee bellard
            esp -= 4;
1278 eaa728ee bellard
            ebp -= 4;
1279 329e607d Blue Swirl
            cpu_stl_data(env, ssp + (esp & esp_mask),
1280 329e607d Blue Swirl
                         cpu_ldl_data(env, ssp + (ebp & esp_mask)));
1281 eaa728ee bellard
        }
1282 eaa728ee bellard
        esp -= 4;
1283 329e607d Blue Swirl
        cpu_stl_data(env, ssp + (esp & esp_mask), t1);
1284 eaa728ee bellard
    } else {
1285 eaa728ee bellard
        /* 16 bit */
1286 eaa728ee bellard
        esp -= 2;
1287 eaa728ee bellard
        while (--level) {
1288 eaa728ee bellard
            esp -= 2;
1289 eaa728ee bellard
            ebp -= 2;
1290 329e607d Blue Swirl
            cpu_stw_data(env, ssp + (esp & esp_mask),
1291 329e607d Blue Swirl
                         cpu_lduw_data(env, ssp + (ebp & esp_mask)));
1292 eaa728ee bellard
        }
1293 eaa728ee bellard
        esp -= 2;
1294 329e607d Blue Swirl
        cpu_stw_data(env, ssp + (esp & esp_mask), t1);
1295 eaa728ee bellard
    }
1296 eaa728ee bellard
}
1297 eaa728ee bellard
1298 eaa728ee bellard
#ifdef TARGET_X86_64
1299 2999a0b2 Blue Swirl
void helper_enter64_level(CPUX86State *env, int level, int data64,
1300 2999a0b2 Blue Swirl
                          target_ulong t1)
1301 eaa728ee bellard
{
1302 eaa728ee bellard
    target_ulong esp, ebp;
1303 20054ef0 Blue Swirl
1304 eaa728ee bellard
    ebp = EBP;
1305 eaa728ee bellard
    esp = ESP;
1306 eaa728ee bellard
1307 eaa728ee bellard
    if (data64) {
1308 eaa728ee bellard
        /* 64 bit */
1309 eaa728ee bellard
        esp -= 8;
1310 eaa728ee bellard
        while (--level) {
1311 eaa728ee bellard
            esp -= 8;
1312 eaa728ee bellard
            ebp -= 8;
1313 329e607d Blue Swirl
            cpu_stq_data(env, esp, cpu_ldq_data(env, ebp));
1314 eaa728ee bellard
        }
1315 eaa728ee bellard
        esp -= 8;
1316 329e607d Blue Swirl
        cpu_stq_data(env, esp, t1);
1317 eaa728ee bellard
    } else {
1318 eaa728ee bellard
        /* 16 bit */
1319 eaa728ee bellard
        esp -= 2;
1320 eaa728ee bellard
        while (--level) {
1321 eaa728ee bellard
            esp -= 2;
1322 eaa728ee bellard
            ebp -= 2;
1323 329e607d Blue Swirl
            cpu_stw_data(env, esp, cpu_lduw_data(env, ebp));
1324 eaa728ee bellard
        }
1325 eaa728ee bellard
        esp -= 2;
1326 329e607d Blue Swirl
        cpu_stw_data(env, esp, t1);
1327 eaa728ee bellard
    }
1328 eaa728ee bellard
}
1329 eaa728ee bellard
#endif
1330 eaa728ee bellard
1331 2999a0b2 Blue Swirl
void helper_lldt(CPUX86State *env, int selector)
1332 eaa728ee bellard
{
1333 eaa728ee bellard
    SegmentCache *dt;
1334 eaa728ee bellard
    uint32_t e1, e2;
1335 eaa728ee bellard
    int index, entry_limit;
1336 eaa728ee bellard
    target_ulong ptr;
1337 eaa728ee bellard
1338 eaa728ee bellard
    selector &= 0xffff;
1339 eaa728ee bellard
    if ((selector & 0xfffc) == 0) {
1340 eaa728ee bellard
        /* XXX: NULL selector case: invalid LDT */
1341 eaa728ee bellard
        env->ldt.base = 0;
1342 eaa728ee bellard
        env->ldt.limit = 0;
1343 eaa728ee bellard
    } else {
1344 20054ef0 Blue Swirl
        if (selector & 0x4) {
1345 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1346 20054ef0 Blue Swirl
        }
1347 eaa728ee bellard
        dt = &env->gdt;
1348 eaa728ee bellard
        index = selector & ~7;
1349 eaa728ee bellard
#ifdef TARGET_X86_64
1350 20054ef0 Blue Swirl
        if (env->hflags & HF_LMA_MASK) {
1351 eaa728ee bellard
            entry_limit = 15;
1352 20054ef0 Blue Swirl
        } else
1353 eaa728ee bellard
#endif
1354 20054ef0 Blue Swirl
        {
1355 eaa728ee bellard
            entry_limit = 7;
1356 20054ef0 Blue Swirl
        }
1357 20054ef0 Blue Swirl
        if ((index + entry_limit) > dt->limit) {
1358 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1359 20054ef0 Blue Swirl
        }
1360 eaa728ee bellard
        ptr = dt->base + index;
1361 329e607d Blue Swirl
        e1 = cpu_ldl_kernel(env, ptr);
1362 329e607d Blue Swirl
        e2 = cpu_ldl_kernel(env, ptr + 4);
1363 20054ef0 Blue Swirl
        if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2) {
1364 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1365 20054ef0 Blue Swirl
        }
1366 20054ef0 Blue Swirl
        if (!(e2 & DESC_P_MASK)) {
1367 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
1368 20054ef0 Blue Swirl
        }
1369 eaa728ee bellard
#ifdef TARGET_X86_64
1370 eaa728ee bellard
        if (env->hflags & HF_LMA_MASK) {
1371 eaa728ee bellard
            uint32_t e3;
1372 20054ef0 Blue Swirl
1373 329e607d Blue Swirl
            e3 = cpu_ldl_kernel(env, ptr + 8);
1374 eaa728ee bellard
            load_seg_cache_raw_dt(&env->ldt, e1, e2);
1375 eaa728ee bellard
            env->ldt.base |= (target_ulong)e3 << 32;
1376 eaa728ee bellard
        } else
1377 eaa728ee bellard
#endif
1378 eaa728ee bellard
        {
1379 eaa728ee bellard
            load_seg_cache_raw_dt(&env->ldt, e1, e2);
1380 eaa728ee bellard
        }
1381 eaa728ee bellard
    }
1382 eaa728ee bellard
    env->ldt.selector = selector;
1383 eaa728ee bellard
}
1384 eaa728ee bellard
1385 2999a0b2 Blue Swirl
void helper_ltr(CPUX86State *env, int selector)
1386 eaa728ee bellard
{
1387 eaa728ee bellard
    SegmentCache *dt;
1388 eaa728ee bellard
    uint32_t e1, e2;
1389 eaa728ee bellard
    int index, type, entry_limit;
1390 eaa728ee bellard
    target_ulong ptr;
1391 eaa728ee bellard
1392 eaa728ee bellard
    selector &= 0xffff;
1393 eaa728ee bellard
    if ((selector & 0xfffc) == 0) {
1394 eaa728ee bellard
        /* NULL selector case: invalid TR */
1395 eaa728ee bellard
        env->tr.base = 0;
1396 eaa728ee bellard
        env->tr.limit = 0;
1397 eaa728ee bellard
        env->tr.flags = 0;
1398 eaa728ee bellard
    } else {
1399 20054ef0 Blue Swirl
        if (selector & 0x4) {
1400 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1401 20054ef0 Blue Swirl
        }
1402 eaa728ee bellard
        dt = &env->gdt;
1403 eaa728ee bellard
        index = selector & ~7;
1404 eaa728ee bellard
#ifdef TARGET_X86_64
1405 20054ef0 Blue Swirl
        if (env->hflags & HF_LMA_MASK) {
1406 eaa728ee bellard
            entry_limit = 15;
1407 20054ef0 Blue Swirl
        } else
1408 eaa728ee bellard
#endif
1409 20054ef0 Blue Swirl
        {
1410 eaa728ee bellard
            entry_limit = 7;
1411 20054ef0 Blue Swirl
        }
1412 20054ef0 Blue Swirl
        if ((index + entry_limit) > dt->limit) {
1413 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1414 20054ef0 Blue Swirl
        }
1415 eaa728ee bellard
        ptr = dt->base + index;
1416 329e607d Blue Swirl
        e1 = cpu_ldl_kernel(env, ptr);
1417 329e607d Blue Swirl
        e2 = cpu_ldl_kernel(env, ptr + 4);
1418 eaa728ee bellard
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1419 eaa728ee bellard
        if ((e2 & DESC_S_MASK) ||
1420 20054ef0 Blue Swirl
            (type != 1 && type != 9)) {
1421 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1422 20054ef0 Blue Swirl
        }
1423 20054ef0 Blue Swirl
        if (!(e2 & DESC_P_MASK)) {
1424 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
1425 20054ef0 Blue Swirl
        }
1426 eaa728ee bellard
#ifdef TARGET_X86_64
1427 eaa728ee bellard
        if (env->hflags & HF_LMA_MASK) {
1428 eaa728ee bellard
            uint32_t e3, e4;
1429 20054ef0 Blue Swirl
1430 329e607d Blue Swirl
            e3 = cpu_ldl_kernel(env, ptr + 8);
1431 329e607d Blue Swirl
            e4 = cpu_ldl_kernel(env, ptr + 12);
1432 20054ef0 Blue Swirl
            if ((e4 >> DESC_TYPE_SHIFT) & 0xf) {
1433 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1434 20054ef0 Blue Swirl
            }
1435 eaa728ee bellard
            load_seg_cache_raw_dt(&env->tr, e1, e2);
1436 eaa728ee bellard
            env->tr.base |= (target_ulong)e3 << 32;
1437 eaa728ee bellard
        } else
1438 eaa728ee bellard
#endif
1439 eaa728ee bellard
        {
1440 eaa728ee bellard
            load_seg_cache_raw_dt(&env->tr, e1, e2);
1441 eaa728ee bellard
        }
1442 eaa728ee bellard
        e2 |= DESC_TSS_BUSY_MASK;
1443 329e607d Blue Swirl
        cpu_stl_kernel(env, ptr + 4, e2);
1444 eaa728ee bellard
    }
1445 eaa728ee bellard
    env->tr.selector = selector;
1446 eaa728ee bellard
}
1447 eaa728ee bellard
1448 eaa728ee bellard
/* only works if protected mode and not VM86. seg_reg must be != R_CS */
1449 2999a0b2 Blue Swirl
void helper_load_seg(CPUX86State *env, int seg_reg, int selector)
1450 eaa728ee bellard
{
1451 eaa728ee bellard
    uint32_t e1, e2;
1452 eaa728ee bellard
    int cpl, dpl, rpl;
1453 eaa728ee bellard
    SegmentCache *dt;
1454 eaa728ee bellard
    int index;
1455 eaa728ee bellard
    target_ulong ptr;
1456 eaa728ee bellard
1457 eaa728ee bellard
    selector &= 0xffff;
1458 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
1459 eaa728ee bellard
    if ((selector & 0xfffc) == 0) {
1460 eaa728ee bellard
        /* null selector case */
1461 eaa728ee bellard
        if (seg_reg == R_SS
1462 eaa728ee bellard
#ifdef TARGET_X86_64
1463 eaa728ee bellard
            && (!(env->hflags & HF_CS64_MASK) || cpl == 3)
1464 eaa728ee bellard
#endif
1465 20054ef0 Blue Swirl
            ) {
1466 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, 0);
1467 20054ef0 Blue Swirl
        }
1468 eaa728ee bellard
        cpu_x86_load_seg_cache(env, seg_reg, selector, 0, 0, 0);
1469 eaa728ee bellard
    } else {
1470 eaa728ee bellard
1471 20054ef0 Blue Swirl
        if (selector & 0x4) {
1472 eaa728ee bellard
            dt = &env->ldt;
1473 20054ef0 Blue Swirl
        } else {
1474 eaa728ee bellard
            dt = &env->gdt;
1475 20054ef0 Blue Swirl
        }
1476 eaa728ee bellard
        index = selector & ~7;
1477 20054ef0 Blue Swirl
        if ((index + 7) > dt->limit) {
1478 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1479 20054ef0 Blue Swirl
        }
1480 eaa728ee bellard
        ptr = dt->base + index;
1481 329e607d Blue Swirl
        e1 = cpu_ldl_kernel(env, ptr);
1482 329e607d Blue Swirl
        e2 = cpu_ldl_kernel(env, ptr + 4);
1483 eaa728ee bellard
1484 20054ef0 Blue Swirl
        if (!(e2 & DESC_S_MASK)) {
1485 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1486 20054ef0 Blue Swirl
        }
1487 eaa728ee bellard
        rpl = selector & 3;
1488 eaa728ee bellard
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1489 eaa728ee bellard
        if (seg_reg == R_SS) {
1490 eaa728ee bellard
            /* must be writable segment */
1491 20054ef0 Blue Swirl
            if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK)) {
1492 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1493 20054ef0 Blue Swirl
            }
1494 20054ef0 Blue Swirl
            if (rpl != cpl || dpl != cpl) {
1495 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1496 20054ef0 Blue Swirl
            }
1497 eaa728ee bellard
        } else {
1498 eaa728ee bellard
            /* must be readable segment */
1499 20054ef0 Blue Swirl
            if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK) {
1500 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1501 20054ef0 Blue Swirl
            }
1502 eaa728ee bellard
1503 eaa728ee bellard
            if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1504 eaa728ee bellard
                /* if not conforming code, test rights */
1505 20054ef0 Blue Swirl
                if (dpl < cpl || dpl < rpl) {
1506 77b2bc2c Blue Swirl
                    raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1507 20054ef0 Blue Swirl
                }
1508 eaa728ee bellard
            }
1509 eaa728ee bellard
        }
1510 eaa728ee bellard
1511 eaa728ee bellard
        if (!(e2 & DESC_P_MASK)) {
1512 20054ef0 Blue Swirl
            if (seg_reg == R_SS) {
1513 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0C_STACK, selector & 0xfffc);
1514 20054ef0 Blue Swirl
            } else {
1515 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
1516 20054ef0 Blue Swirl
            }
1517 eaa728ee bellard
        }
1518 eaa728ee bellard
1519 eaa728ee bellard
        /* set the access bit if not already set */
1520 eaa728ee bellard
        if (!(e2 & DESC_A_MASK)) {
1521 eaa728ee bellard
            e2 |= DESC_A_MASK;
1522 329e607d Blue Swirl
            cpu_stl_kernel(env, ptr + 4, e2);
1523 eaa728ee bellard
        }
1524 eaa728ee bellard
1525 eaa728ee bellard
        cpu_x86_load_seg_cache(env, seg_reg, selector,
1526 eaa728ee bellard
                       get_seg_base(e1, e2),
1527 eaa728ee bellard
                       get_seg_limit(e1, e2),
1528 eaa728ee bellard
                       e2);
1529 eaa728ee bellard
#if 0
1530 93fcfe39 aliguori
        qemu_log("load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
1531 eaa728ee bellard
                selector, (unsigned long)sc->base, sc->limit, sc->flags);
1532 eaa728ee bellard
#endif
1533 eaa728ee bellard
    }
1534 eaa728ee bellard
}
1535 eaa728ee bellard
1536 eaa728ee bellard
/* protected mode jump */
1537 2999a0b2 Blue Swirl
void helper_ljmp_protected(CPUX86State *env, int new_cs, target_ulong new_eip,
1538 eaa728ee bellard
                           int next_eip_addend)
1539 eaa728ee bellard
{
1540 eaa728ee bellard
    int gate_cs, type;
1541 eaa728ee bellard
    uint32_t e1, e2, cpl, dpl, rpl, limit;
1542 eaa728ee bellard
    target_ulong next_eip;
1543 eaa728ee bellard
1544 20054ef0 Blue Swirl
    if ((new_cs & 0xfffc) == 0) {
1545 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, 0);
1546 20054ef0 Blue Swirl
    }
1547 2999a0b2 Blue Swirl
    if (load_segment(env, &e1, &e2, new_cs) != 0) {
1548 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1549 20054ef0 Blue Swirl
    }
1550 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
1551 eaa728ee bellard
    if (e2 & DESC_S_MASK) {
1552 20054ef0 Blue Swirl
        if (!(e2 & DESC_CS_MASK)) {
1553 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1554 20054ef0 Blue Swirl
        }
1555 eaa728ee bellard
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1556 eaa728ee bellard
        if (e2 & DESC_C_MASK) {
1557 eaa728ee bellard
            /* conforming code segment */
1558 20054ef0 Blue Swirl
            if (dpl > cpl) {
1559 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1560 20054ef0 Blue Swirl
            }
1561 eaa728ee bellard
        } else {
1562 eaa728ee bellard
            /* non conforming code segment */
1563 eaa728ee bellard
            rpl = new_cs & 3;
1564 20054ef0 Blue Swirl
            if (rpl > cpl) {
1565 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1566 20054ef0 Blue Swirl
            }
1567 20054ef0 Blue Swirl
            if (dpl != cpl) {
1568 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1569 20054ef0 Blue Swirl
            }
1570 eaa728ee bellard
        }
1571 20054ef0 Blue Swirl
        if (!(e2 & DESC_P_MASK)) {
1572 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc);
1573 20054ef0 Blue Swirl
        }
1574 eaa728ee bellard
        limit = get_seg_limit(e1, e2);
1575 eaa728ee bellard
        if (new_eip > limit &&
1576 20054ef0 Blue Swirl
            !(env->hflags & HF_LMA_MASK) && !(e2 & DESC_L_MASK)) {
1577 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1578 20054ef0 Blue Swirl
        }
1579 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1580 eaa728ee bellard
                       get_seg_base(e1, e2), limit, e2);
1581 eaa728ee bellard
        EIP = new_eip;
1582 eaa728ee bellard
    } else {
1583 eaa728ee bellard
        /* jump to call or task gate */
1584 eaa728ee bellard
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1585 eaa728ee bellard
        rpl = new_cs & 3;
1586 eaa728ee bellard
        cpl = env->hflags & HF_CPL_MASK;
1587 eaa728ee bellard
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
1588 20054ef0 Blue Swirl
        switch (type) {
1589 eaa728ee bellard
        case 1: /* 286 TSS */
1590 eaa728ee bellard
        case 9: /* 386 TSS */
1591 eaa728ee bellard
        case 5: /* task gate */
1592 20054ef0 Blue Swirl
            if (dpl < cpl || dpl < rpl) {
1593 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1594 20054ef0 Blue Swirl
            }
1595 eaa728ee bellard
            next_eip = env->eip + next_eip_addend;
1596 2999a0b2 Blue Swirl
            switch_tss(env, new_cs, e1, e2, SWITCH_TSS_JMP, next_eip);
1597 eaa728ee bellard
            CC_OP = CC_OP_EFLAGS;
1598 eaa728ee bellard
            break;
1599 eaa728ee bellard
        case 4: /* 286 call gate */
1600 eaa728ee bellard
        case 12: /* 386 call gate */
1601 20054ef0 Blue Swirl
            if ((dpl < cpl) || (dpl < rpl)) {
1602 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1603 20054ef0 Blue Swirl
            }
1604 20054ef0 Blue Swirl
            if (!(e2 & DESC_P_MASK)) {
1605 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc);
1606 20054ef0 Blue Swirl
            }
1607 eaa728ee bellard
            gate_cs = e1 >> 16;
1608 eaa728ee bellard
            new_eip = (e1 & 0xffff);
1609 20054ef0 Blue Swirl
            if (type == 12) {
1610 eaa728ee bellard
                new_eip |= (e2 & 0xffff0000);
1611 20054ef0 Blue Swirl
            }
1612 2999a0b2 Blue Swirl
            if (load_segment(env, &e1, &e2, gate_cs) != 0) {
1613 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc);
1614 20054ef0 Blue Swirl
            }
1615 eaa728ee bellard
            dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1616 eaa728ee bellard
            /* must be code segment */
1617 eaa728ee bellard
            if (((e2 & (DESC_S_MASK | DESC_CS_MASK)) !=
1618 20054ef0 Blue Swirl
                 (DESC_S_MASK | DESC_CS_MASK))) {
1619 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc);
1620 20054ef0 Blue Swirl
            }
1621 eaa728ee bellard
            if (((e2 & DESC_C_MASK) && (dpl > cpl)) ||
1622 20054ef0 Blue Swirl
                (!(e2 & DESC_C_MASK) && (dpl != cpl))) {
1623 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc);
1624 20054ef0 Blue Swirl
            }
1625 20054ef0 Blue Swirl
            if (!(e2 & DESC_P_MASK)) {
1626 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc);
1627 20054ef0 Blue Swirl
            }
1628 eaa728ee bellard
            limit = get_seg_limit(e1, e2);
1629 20054ef0 Blue Swirl
            if (new_eip > limit) {
1630 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, 0);
1631 20054ef0 Blue Swirl
            }
1632 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl,
1633 eaa728ee bellard
                                   get_seg_base(e1, e2), limit, e2);
1634 eaa728ee bellard
            EIP = new_eip;
1635 eaa728ee bellard
            break;
1636 eaa728ee bellard
        default:
1637 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1638 eaa728ee bellard
            break;
1639 eaa728ee bellard
        }
1640 eaa728ee bellard
    }
1641 eaa728ee bellard
}
1642 eaa728ee bellard
1643 eaa728ee bellard
/* real mode call */
1644 2999a0b2 Blue Swirl
void helper_lcall_real(CPUX86State *env, int new_cs, target_ulong new_eip1,
1645 eaa728ee bellard
                       int shift, int next_eip)
1646 eaa728ee bellard
{
1647 eaa728ee bellard
    int new_eip;
1648 eaa728ee bellard
    uint32_t esp, esp_mask;
1649 eaa728ee bellard
    target_ulong ssp;
1650 eaa728ee bellard
1651 eaa728ee bellard
    new_eip = new_eip1;
1652 eaa728ee bellard
    esp = ESP;
1653 eaa728ee bellard
    esp_mask = get_sp_mask(env->segs[R_SS].flags);
1654 eaa728ee bellard
    ssp = env->segs[R_SS].base;
1655 eaa728ee bellard
    if (shift) {
1656 eaa728ee bellard
        PUSHL(ssp, esp, esp_mask, env->segs[R_CS].selector);
1657 eaa728ee bellard
        PUSHL(ssp, esp, esp_mask, next_eip);
1658 eaa728ee bellard
    } else {
1659 eaa728ee bellard
        PUSHW(ssp, esp, esp_mask, env->segs[R_CS].selector);
1660 eaa728ee bellard
        PUSHW(ssp, esp, esp_mask, next_eip);
1661 eaa728ee bellard
    }
1662 eaa728ee bellard
1663 eaa728ee bellard
    SET_ESP(esp, esp_mask);
1664 eaa728ee bellard
    env->eip = new_eip;
1665 eaa728ee bellard
    env->segs[R_CS].selector = new_cs;
1666 eaa728ee bellard
    env->segs[R_CS].base = (new_cs << 4);
1667 eaa728ee bellard
}
1668 eaa728ee bellard
1669 eaa728ee bellard
/* protected mode call */
1670 2999a0b2 Blue Swirl
void helper_lcall_protected(CPUX86State *env, int new_cs, target_ulong new_eip,
1671 eaa728ee bellard
                            int shift, int next_eip_addend)
1672 eaa728ee bellard
{
1673 eaa728ee bellard
    int new_stack, i;
1674 eaa728ee bellard
    uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count;
1675 1c918eba blueswir1
    uint32_t ss = 0, ss_e1 = 0, ss_e2 = 0, sp, type, ss_dpl, sp_mask;
1676 eaa728ee bellard
    uint32_t val, limit, old_sp_mask;
1677 eaa728ee bellard
    target_ulong ssp, old_ssp, next_eip;
1678 eaa728ee bellard
1679 eaa728ee bellard
    next_eip = env->eip + next_eip_addend;
1680 d12d51d5 aliguori
    LOG_PCALL("lcall %04x:%08x s=%d\n", new_cs, (uint32_t)new_eip, shift);
1681 d12d51d5 aliguori
    LOG_PCALL_STATE(env);
1682 20054ef0 Blue Swirl
    if ((new_cs & 0xfffc) == 0) {
1683 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, 0);
1684 20054ef0 Blue Swirl
    }
1685 2999a0b2 Blue Swirl
    if (load_segment(env, &e1, &e2, new_cs) != 0) {
1686 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1687 20054ef0 Blue Swirl
    }
1688 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
1689 d12d51d5 aliguori
    LOG_PCALL("desc=%08x:%08x\n", e1, e2);
1690 eaa728ee bellard
    if (e2 & DESC_S_MASK) {
1691 20054ef0 Blue Swirl
        if (!(e2 & DESC_CS_MASK)) {
1692 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1693 20054ef0 Blue Swirl
        }
1694 eaa728ee bellard
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1695 eaa728ee bellard
        if (e2 & DESC_C_MASK) {
1696 eaa728ee bellard
            /* conforming code segment */
1697 20054ef0 Blue Swirl
            if (dpl > cpl) {
1698 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1699 20054ef0 Blue Swirl
            }
1700 eaa728ee bellard
        } else {
1701 eaa728ee bellard
            /* non conforming code segment */
1702 eaa728ee bellard
            rpl = new_cs & 3;
1703 20054ef0 Blue Swirl
            if (rpl > cpl) {
1704 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1705 20054ef0 Blue Swirl
            }
1706 20054ef0 Blue Swirl
            if (dpl != cpl) {
1707 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1708 20054ef0 Blue Swirl
            }
1709 eaa728ee bellard
        }
1710 20054ef0 Blue Swirl
        if (!(e2 & DESC_P_MASK)) {
1711 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc);
1712 20054ef0 Blue Swirl
        }
1713 eaa728ee bellard
1714 eaa728ee bellard
#ifdef TARGET_X86_64
1715 eaa728ee bellard
        /* XXX: check 16/32 bit cases in long mode */
1716 eaa728ee bellard
        if (shift == 2) {
1717 eaa728ee bellard
            target_ulong rsp;
1718 20054ef0 Blue Swirl
1719 eaa728ee bellard
            /* 64 bit case */
1720 eaa728ee bellard
            rsp = ESP;
1721 eaa728ee bellard
            PUSHQ(rsp, env->segs[R_CS].selector);
1722 eaa728ee bellard
            PUSHQ(rsp, next_eip);
1723 eaa728ee bellard
            /* from this point, not restartable */
1724 eaa728ee bellard
            ESP = rsp;
1725 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1726 eaa728ee bellard
                                   get_seg_base(e1, e2),
1727 eaa728ee bellard
                                   get_seg_limit(e1, e2), e2);
1728 eaa728ee bellard
            EIP = new_eip;
1729 eaa728ee bellard
        } else
1730 eaa728ee bellard
#endif
1731 eaa728ee bellard
        {
1732 eaa728ee bellard
            sp = ESP;
1733 eaa728ee bellard
            sp_mask = get_sp_mask(env->segs[R_SS].flags);
1734 eaa728ee bellard
            ssp = env->segs[R_SS].base;
1735 eaa728ee bellard
            if (shift) {
1736 eaa728ee bellard
                PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1737 eaa728ee bellard
                PUSHL(ssp, sp, sp_mask, next_eip);
1738 eaa728ee bellard
            } else {
1739 eaa728ee bellard
                PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1740 eaa728ee bellard
                PUSHW(ssp, sp, sp_mask, next_eip);
1741 eaa728ee bellard
            }
1742 eaa728ee bellard
1743 eaa728ee bellard
            limit = get_seg_limit(e1, e2);
1744 20054ef0 Blue Swirl
            if (new_eip > limit) {
1745 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1746 20054ef0 Blue Swirl
            }
1747 eaa728ee bellard
            /* from this point, not restartable */
1748 eaa728ee bellard
            SET_ESP(sp, sp_mask);
1749 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
1750 eaa728ee bellard
                                   get_seg_base(e1, e2), limit, e2);
1751 eaa728ee bellard
            EIP = new_eip;
1752 eaa728ee bellard
        }
1753 eaa728ee bellard
    } else {
1754 eaa728ee bellard
        /* check gate type */
1755 eaa728ee bellard
        type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
1756 eaa728ee bellard
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1757 eaa728ee bellard
        rpl = new_cs & 3;
1758 20054ef0 Blue Swirl
        switch (type) {
1759 eaa728ee bellard
        case 1: /* available 286 TSS */
1760 eaa728ee bellard
        case 9: /* available 386 TSS */
1761 eaa728ee bellard
        case 5: /* task gate */
1762 20054ef0 Blue Swirl
            if (dpl < cpl || dpl < rpl) {
1763 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1764 20054ef0 Blue Swirl
            }
1765 2999a0b2 Blue Swirl
            switch_tss(env, new_cs, e1, e2, SWITCH_TSS_CALL, next_eip);
1766 eaa728ee bellard
            CC_OP = CC_OP_EFLAGS;
1767 eaa728ee bellard
            return;
1768 eaa728ee bellard
        case 4: /* 286 call gate */
1769 eaa728ee bellard
        case 12: /* 386 call gate */
1770 eaa728ee bellard
            break;
1771 eaa728ee bellard
        default:
1772 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1773 eaa728ee bellard
            break;
1774 eaa728ee bellard
        }
1775 eaa728ee bellard
        shift = type >> 3;
1776 eaa728ee bellard
1777 20054ef0 Blue Swirl
        if (dpl < cpl || dpl < rpl) {
1778 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
1779 20054ef0 Blue Swirl
        }
1780 eaa728ee bellard
        /* check valid bit */
1781 20054ef0 Blue Swirl
        if (!(e2 & DESC_P_MASK)) {
1782 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0B_NOSEG,  new_cs & 0xfffc);
1783 20054ef0 Blue Swirl
        }
1784 eaa728ee bellard
        selector = e1 >> 16;
1785 eaa728ee bellard
        offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
1786 eaa728ee bellard
        param_count = e2 & 0x1f;
1787 20054ef0 Blue Swirl
        if ((selector & 0xfffc) == 0) {
1788 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, 0);
1789 20054ef0 Blue Swirl
        }
1790 eaa728ee bellard
1791 2999a0b2 Blue Swirl
        if (load_segment(env, &e1, &e2, selector) != 0) {
1792 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1793 20054ef0 Blue Swirl
        }
1794 20054ef0 Blue Swirl
        if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) {
1795 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1796 20054ef0 Blue Swirl
        }
1797 eaa728ee bellard
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1798 20054ef0 Blue Swirl
        if (dpl > cpl) {
1799 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc);
1800 20054ef0 Blue Swirl
        }
1801 20054ef0 Blue Swirl
        if (!(e2 & DESC_P_MASK)) {
1802 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc);
1803 20054ef0 Blue Swirl
        }
1804 eaa728ee bellard
1805 eaa728ee bellard
        if (!(e2 & DESC_C_MASK) && dpl < cpl) {
1806 eaa728ee bellard
            /* to inner privilege */
1807 2999a0b2 Blue Swirl
            get_ss_esp_from_tss(env, &ss, &sp, dpl);
1808 20054ef0 Blue Swirl
            LOG_PCALL("new ss:esp=%04x:%08x param_count=%d ESP=" TARGET_FMT_lx
1809 20054ef0 Blue Swirl
                      "\n",
1810 20054ef0 Blue Swirl
                      ss, sp, param_count, ESP);
1811 20054ef0 Blue Swirl
            if ((ss & 0xfffc) == 0) {
1812 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
1813 20054ef0 Blue Swirl
            }
1814 20054ef0 Blue Swirl
            if ((ss & 3) != dpl) {
1815 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
1816 20054ef0 Blue Swirl
            }
1817 2999a0b2 Blue Swirl
            if (load_segment(env, &ss_e1, &ss_e2, ss) != 0) {
1818 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
1819 20054ef0 Blue Swirl
            }
1820 eaa728ee bellard
            ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
1821 20054ef0 Blue Swirl
            if (ss_dpl != dpl) {
1822 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
1823 20054ef0 Blue Swirl
            }
1824 eaa728ee bellard
            if (!(ss_e2 & DESC_S_MASK) ||
1825 eaa728ee bellard
                (ss_e2 & DESC_CS_MASK) ||
1826 20054ef0 Blue Swirl
                !(ss_e2 & DESC_W_MASK)) {
1827 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
1828 20054ef0 Blue Swirl
            }
1829 20054ef0 Blue Swirl
            if (!(ss_e2 & DESC_P_MASK)) {
1830 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc);
1831 20054ef0 Blue Swirl
            }
1832 eaa728ee bellard
1833 20054ef0 Blue Swirl
            /* push_size = ((param_count * 2) + 8) << shift; */
1834 eaa728ee bellard
1835 eaa728ee bellard
            old_sp_mask = get_sp_mask(env->segs[R_SS].flags);
1836 eaa728ee bellard
            old_ssp = env->segs[R_SS].base;
1837 eaa728ee bellard
1838 eaa728ee bellard
            sp_mask = get_sp_mask(ss_e2);
1839 eaa728ee bellard
            ssp = get_seg_base(ss_e1, ss_e2);
1840 eaa728ee bellard
            if (shift) {
1841 eaa728ee bellard
                PUSHL(ssp, sp, sp_mask, env->segs[R_SS].selector);
1842 eaa728ee bellard
                PUSHL(ssp, sp, sp_mask, ESP);
1843 20054ef0 Blue Swirl
                for (i = param_count - 1; i >= 0; i--) {
1844 329e607d Blue Swirl
                    val = cpu_ldl_kernel(env, old_ssp + ((ESP + i * 4) &
1845 329e607d Blue Swirl
                                                         old_sp_mask));
1846 eaa728ee bellard
                    PUSHL(ssp, sp, sp_mask, val);
1847 eaa728ee bellard
                }
1848 eaa728ee bellard
            } else {
1849 eaa728ee bellard
                PUSHW(ssp, sp, sp_mask, env->segs[R_SS].selector);
1850 eaa728ee bellard
                PUSHW(ssp, sp, sp_mask, ESP);
1851 20054ef0 Blue Swirl
                for (i = param_count - 1; i >= 0; i--) {
1852 329e607d Blue Swirl
                    val = cpu_lduw_kernel(env, old_ssp + ((ESP + i * 2) &
1853 329e607d Blue Swirl
                                                          old_sp_mask));
1854 eaa728ee bellard
                    PUSHW(ssp, sp, sp_mask, val);
1855 eaa728ee bellard
                }
1856 eaa728ee bellard
            }
1857 eaa728ee bellard
            new_stack = 1;
1858 eaa728ee bellard
        } else {
1859 eaa728ee bellard
            /* to same privilege */
1860 eaa728ee bellard
            sp = ESP;
1861 eaa728ee bellard
            sp_mask = get_sp_mask(env->segs[R_SS].flags);
1862 eaa728ee bellard
            ssp = env->segs[R_SS].base;
1863 20054ef0 Blue Swirl
            /* push_size = (4 << shift); */
1864 eaa728ee bellard
            new_stack = 0;
1865 eaa728ee bellard
        }
1866 eaa728ee bellard
1867 eaa728ee bellard
        if (shift) {
1868 eaa728ee bellard
            PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
1869 eaa728ee bellard
            PUSHL(ssp, sp, sp_mask, next_eip);
1870 eaa728ee bellard
        } else {
1871 eaa728ee bellard
            PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
1872 eaa728ee bellard
            PUSHW(ssp, sp, sp_mask, next_eip);
1873 eaa728ee bellard
        }
1874 eaa728ee bellard
1875 eaa728ee bellard
        /* from this point, not restartable */
1876 eaa728ee bellard
1877 eaa728ee bellard
        if (new_stack) {
1878 eaa728ee bellard
            ss = (ss & ~3) | dpl;
1879 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_SS, ss,
1880 eaa728ee bellard
                                   ssp,
1881 eaa728ee bellard
                                   get_seg_limit(ss_e1, ss_e2),
1882 eaa728ee bellard
                                   ss_e2);
1883 eaa728ee bellard
        }
1884 eaa728ee bellard
1885 eaa728ee bellard
        selector = (selector & ~3) | dpl;
1886 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_CS, selector,
1887 eaa728ee bellard
                       get_seg_base(e1, e2),
1888 eaa728ee bellard
                       get_seg_limit(e1, e2),
1889 eaa728ee bellard
                       e2);
1890 eaa728ee bellard
        cpu_x86_set_cpl(env, dpl);
1891 eaa728ee bellard
        SET_ESP(sp, sp_mask);
1892 eaa728ee bellard
        EIP = offset;
1893 eaa728ee bellard
    }
1894 eaa728ee bellard
}
1895 eaa728ee bellard
1896 eaa728ee bellard
/* real and vm86 mode iret */
1897 2999a0b2 Blue Swirl
void helper_iret_real(CPUX86State *env, int shift)
1898 eaa728ee bellard
{
1899 eaa728ee bellard
    uint32_t sp, new_cs, new_eip, new_eflags, sp_mask;
1900 eaa728ee bellard
    target_ulong ssp;
1901 eaa728ee bellard
    int eflags_mask;
1902 eaa728ee bellard
1903 20054ef0 Blue Swirl
    sp_mask = 0xffff; /* XXXX: use SS segment size? */
1904 eaa728ee bellard
    sp = ESP;
1905 eaa728ee bellard
    ssp = env->segs[R_SS].base;
1906 eaa728ee bellard
    if (shift == 1) {
1907 eaa728ee bellard
        /* 32 bits */
1908 eaa728ee bellard
        POPL(ssp, sp, sp_mask, new_eip);
1909 eaa728ee bellard
        POPL(ssp, sp, sp_mask, new_cs);
1910 eaa728ee bellard
        new_cs &= 0xffff;
1911 eaa728ee bellard
        POPL(ssp, sp, sp_mask, new_eflags);
1912 eaa728ee bellard
    } else {
1913 eaa728ee bellard
        /* 16 bits */
1914 eaa728ee bellard
        POPW(ssp, sp, sp_mask, new_eip);
1915 eaa728ee bellard
        POPW(ssp, sp, sp_mask, new_cs);
1916 eaa728ee bellard
        POPW(ssp, sp, sp_mask, new_eflags);
1917 eaa728ee bellard
    }
1918 eaa728ee bellard
    ESP = (ESP & ~sp_mask) | (sp & sp_mask);
1919 bdadc0b5 malc
    env->segs[R_CS].selector = new_cs;
1920 bdadc0b5 malc
    env->segs[R_CS].base = (new_cs << 4);
1921 eaa728ee bellard
    env->eip = new_eip;
1922 20054ef0 Blue Swirl
    if (env->eflags & VM_MASK) {
1923 20054ef0 Blue Swirl
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | RF_MASK |
1924 20054ef0 Blue Swirl
            NT_MASK;
1925 20054ef0 Blue Swirl
    } else {
1926 20054ef0 Blue Swirl
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK |
1927 20054ef0 Blue Swirl
            RF_MASK | NT_MASK;
1928 20054ef0 Blue Swirl
    }
1929 20054ef0 Blue Swirl
    if (shift == 0) {
1930 eaa728ee bellard
        eflags_mask &= 0xffff;
1931 20054ef0 Blue Swirl
    }
1932 997ff0d9 Blue Swirl
    cpu_load_eflags(env, new_eflags, eflags_mask);
1933 db620f46 bellard
    env->hflags2 &= ~HF2_NMI_MASK;
1934 eaa728ee bellard
}
1935 eaa728ee bellard
1936 2999a0b2 Blue Swirl
static inline void validate_seg(CPUX86State *env, int seg_reg, int cpl)
1937 eaa728ee bellard
{
1938 eaa728ee bellard
    int dpl;
1939 eaa728ee bellard
    uint32_t e2;
1940 eaa728ee bellard
1941 eaa728ee bellard
    /* XXX: on x86_64, we do not want to nullify FS and GS because
1942 eaa728ee bellard
       they may still contain a valid base. I would be interested to
1943 eaa728ee bellard
       know how a real x86_64 CPU behaves */
1944 eaa728ee bellard
    if ((seg_reg == R_FS || seg_reg == R_GS) &&
1945 20054ef0 Blue Swirl
        (env->segs[seg_reg].selector & 0xfffc) == 0) {
1946 eaa728ee bellard
        return;
1947 20054ef0 Blue Swirl
    }
1948 eaa728ee bellard
1949 eaa728ee bellard
    e2 = env->segs[seg_reg].flags;
1950 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
1951 eaa728ee bellard
    if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
1952 eaa728ee bellard
        /* data or non conforming code segment */
1953 eaa728ee bellard
        if (dpl < cpl) {
1954 eaa728ee bellard
            cpu_x86_load_seg_cache(env, seg_reg, 0, 0, 0, 0);
1955 eaa728ee bellard
        }
1956 eaa728ee bellard
    }
1957 eaa728ee bellard
}
1958 eaa728ee bellard
1959 eaa728ee bellard
/* protected mode iret */
1960 2999a0b2 Blue Swirl
static inline void helper_ret_protected(CPUX86State *env, int shift,
1961 2999a0b2 Blue Swirl
                                        int is_iret, int addend)
1962 eaa728ee bellard
{
1963 eaa728ee bellard
    uint32_t new_cs, new_eflags, new_ss;
1964 eaa728ee bellard
    uint32_t new_es, new_ds, new_fs, new_gs;
1965 eaa728ee bellard
    uint32_t e1, e2, ss_e1, ss_e2;
1966 eaa728ee bellard
    int cpl, dpl, rpl, eflags_mask, iopl;
1967 eaa728ee bellard
    target_ulong ssp, sp, new_eip, new_esp, sp_mask;
1968 eaa728ee bellard
1969 eaa728ee bellard
#ifdef TARGET_X86_64
1970 20054ef0 Blue Swirl
    if (shift == 2) {
1971 eaa728ee bellard
        sp_mask = -1;
1972 20054ef0 Blue Swirl
    } else
1973 eaa728ee bellard
#endif
1974 20054ef0 Blue Swirl
    {
1975 eaa728ee bellard
        sp_mask = get_sp_mask(env->segs[R_SS].flags);
1976 20054ef0 Blue Swirl
    }
1977 eaa728ee bellard
    sp = ESP;
1978 eaa728ee bellard
    ssp = env->segs[R_SS].base;
1979 eaa728ee bellard
    new_eflags = 0; /* avoid warning */
1980 eaa728ee bellard
#ifdef TARGET_X86_64
1981 eaa728ee bellard
    if (shift == 2) {
1982 eaa728ee bellard
        POPQ(sp, new_eip);
1983 eaa728ee bellard
        POPQ(sp, new_cs);
1984 eaa728ee bellard
        new_cs &= 0xffff;
1985 eaa728ee bellard
        if (is_iret) {
1986 eaa728ee bellard
            POPQ(sp, new_eflags);
1987 eaa728ee bellard
        }
1988 eaa728ee bellard
    } else
1989 eaa728ee bellard
#endif
1990 20054ef0 Blue Swirl
    {
1991 20054ef0 Blue Swirl
        if (shift == 1) {
1992 20054ef0 Blue Swirl
            /* 32 bits */
1993 20054ef0 Blue Swirl
            POPL(ssp, sp, sp_mask, new_eip);
1994 20054ef0 Blue Swirl
            POPL(ssp, sp, sp_mask, new_cs);
1995 20054ef0 Blue Swirl
            new_cs &= 0xffff;
1996 20054ef0 Blue Swirl
            if (is_iret) {
1997 20054ef0 Blue Swirl
                POPL(ssp, sp, sp_mask, new_eflags);
1998 20054ef0 Blue Swirl
                if (new_eflags & VM_MASK) {
1999 20054ef0 Blue Swirl
                    goto return_to_vm86;
2000 20054ef0 Blue Swirl
                }
2001 20054ef0 Blue Swirl
            }
2002 20054ef0 Blue Swirl
        } else {
2003 20054ef0 Blue Swirl
            /* 16 bits */
2004 20054ef0 Blue Swirl
            POPW(ssp, sp, sp_mask, new_eip);
2005 20054ef0 Blue Swirl
            POPW(ssp, sp, sp_mask, new_cs);
2006 20054ef0 Blue Swirl
            if (is_iret) {
2007 20054ef0 Blue Swirl
                POPW(ssp, sp, sp_mask, new_eflags);
2008 20054ef0 Blue Swirl
            }
2009 eaa728ee bellard
        }
2010 eaa728ee bellard
    }
2011 d12d51d5 aliguori
    LOG_PCALL("lret new %04x:" TARGET_FMT_lx " s=%d addend=0x%x\n",
2012 d12d51d5 aliguori
              new_cs, new_eip, shift, addend);
2013 d12d51d5 aliguori
    LOG_PCALL_STATE(env);
2014 20054ef0 Blue Swirl
    if ((new_cs & 0xfffc) == 0) {
2015 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
2016 20054ef0 Blue Swirl
    }
2017 2999a0b2 Blue Swirl
    if (load_segment(env, &e1, &e2, new_cs) != 0) {
2018 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
2019 20054ef0 Blue Swirl
    }
2020 eaa728ee bellard
    if (!(e2 & DESC_S_MASK) ||
2021 20054ef0 Blue Swirl
        !(e2 & DESC_CS_MASK)) {
2022 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
2023 20054ef0 Blue Swirl
    }
2024 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
2025 eaa728ee bellard
    rpl = new_cs & 3;
2026 20054ef0 Blue Swirl
    if (rpl < cpl) {
2027 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
2028 20054ef0 Blue Swirl
    }
2029 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2030 eaa728ee bellard
    if (e2 & DESC_C_MASK) {
2031 20054ef0 Blue Swirl
        if (dpl > rpl) {
2032 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
2033 20054ef0 Blue Swirl
        }
2034 eaa728ee bellard
    } else {
2035 20054ef0 Blue Swirl
        if (dpl != rpl) {
2036 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc);
2037 20054ef0 Blue Swirl
        }
2038 eaa728ee bellard
    }
2039 20054ef0 Blue Swirl
    if (!(e2 & DESC_P_MASK)) {
2040 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc);
2041 20054ef0 Blue Swirl
    }
2042 eaa728ee bellard
2043 eaa728ee bellard
    sp += addend;
2044 eaa728ee bellard
    if (rpl == cpl && (!(env->hflags & HF_CS64_MASK) ||
2045 eaa728ee bellard
                       ((env->hflags & HF_CS64_MASK) && !is_iret))) {
2046 1235fc06 ths
        /* return to same privilege level */
2047 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_CS, new_cs,
2048 eaa728ee bellard
                       get_seg_base(e1, e2),
2049 eaa728ee bellard
                       get_seg_limit(e1, e2),
2050 eaa728ee bellard
                       e2);
2051 eaa728ee bellard
    } else {
2052 eaa728ee bellard
        /* return to different privilege level */
2053 eaa728ee bellard
#ifdef TARGET_X86_64
2054 eaa728ee bellard
        if (shift == 2) {
2055 eaa728ee bellard
            POPQ(sp, new_esp);
2056 eaa728ee bellard
            POPQ(sp, new_ss);
2057 eaa728ee bellard
            new_ss &= 0xffff;
2058 eaa728ee bellard
        } else
2059 eaa728ee bellard
#endif
2060 20054ef0 Blue Swirl
        {
2061 20054ef0 Blue Swirl
            if (shift == 1) {
2062 20054ef0 Blue Swirl
                /* 32 bits */
2063 20054ef0 Blue Swirl
                POPL(ssp, sp, sp_mask, new_esp);
2064 20054ef0 Blue Swirl
                POPL(ssp, sp, sp_mask, new_ss);
2065 20054ef0 Blue Swirl
                new_ss &= 0xffff;
2066 20054ef0 Blue Swirl
            } else {
2067 20054ef0 Blue Swirl
                /* 16 bits */
2068 20054ef0 Blue Swirl
                POPW(ssp, sp, sp_mask, new_esp);
2069 20054ef0 Blue Swirl
                POPW(ssp, sp, sp_mask, new_ss);
2070 20054ef0 Blue Swirl
            }
2071 eaa728ee bellard
        }
2072 d12d51d5 aliguori
        LOG_PCALL("new ss:esp=%04x:" TARGET_FMT_lx "\n",
2073 20054ef0 Blue Swirl
                  new_ss, new_esp);
2074 eaa728ee bellard
        if ((new_ss & 0xfffc) == 0) {
2075 eaa728ee bellard
#ifdef TARGET_X86_64
2076 20054ef0 Blue Swirl
            /* NULL ss is allowed in long mode if cpl != 3 */
2077 20054ef0 Blue Swirl
            /* XXX: test CS64? */
2078 eaa728ee bellard
            if ((env->hflags & HF_LMA_MASK) && rpl != 3) {
2079 eaa728ee bellard
                cpu_x86_load_seg_cache(env, R_SS, new_ss,
2080 eaa728ee bellard
                                       0, 0xffffffff,
2081 eaa728ee bellard
                                       DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2082 eaa728ee bellard
                                       DESC_S_MASK | (rpl << DESC_DPL_SHIFT) |
2083 eaa728ee bellard
                                       DESC_W_MASK | DESC_A_MASK);
2084 20054ef0 Blue Swirl
                ss_e2 = DESC_B_MASK; /* XXX: should not be needed? */
2085 eaa728ee bellard
            } else
2086 eaa728ee bellard
#endif
2087 eaa728ee bellard
            {
2088 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, 0);
2089 eaa728ee bellard
            }
2090 eaa728ee bellard
        } else {
2091 20054ef0 Blue Swirl
            if ((new_ss & 3) != rpl) {
2092 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc);
2093 20054ef0 Blue Swirl
            }
2094 2999a0b2 Blue Swirl
            if (load_segment(env, &ss_e1, &ss_e2, new_ss) != 0) {
2095 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc);
2096 20054ef0 Blue Swirl
            }
2097 eaa728ee bellard
            if (!(ss_e2 & DESC_S_MASK) ||
2098 eaa728ee bellard
                (ss_e2 & DESC_CS_MASK) ||
2099 20054ef0 Blue Swirl
                !(ss_e2 & DESC_W_MASK)) {
2100 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc);
2101 20054ef0 Blue Swirl
            }
2102 eaa728ee bellard
            dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
2103 20054ef0 Blue Swirl
            if (dpl != rpl) {
2104 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc);
2105 20054ef0 Blue Swirl
            }
2106 20054ef0 Blue Swirl
            if (!(ss_e2 & DESC_P_MASK)) {
2107 77b2bc2c Blue Swirl
                raise_exception_err(env, EXCP0B_NOSEG, new_ss & 0xfffc);
2108 20054ef0 Blue Swirl
            }
2109 eaa728ee bellard
            cpu_x86_load_seg_cache(env, R_SS, new_ss,
2110 eaa728ee bellard
                                   get_seg_base(ss_e1, ss_e2),
2111 eaa728ee bellard
                                   get_seg_limit(ss_e1, ss_e2),
2112 eaa728ee bellard
                                   ss_e2);
2113 eaa728ee bellard
        }
2114 eaa728ee bellard
2115 eaa728ee bellard
        cpu_x86_load_seg_cache(env, R_CS, new_cs,
2116 eaa728ee bellard
                       get_seg_base(e1, e2),
2117 eaa728ee bellard
                       get_seg_limit(e1, e2),
2118 eaa728ee bellard
                       e2);
2119 eaa728ee bellard
        cpu_x86_set_cpl(env, rpl);
2120 eaa728ee bellard
        sp = new_esp;
2121 eaa728ee bellard
#ifdef TARGET_X86_64
2122 20054ef0 Blue Swirl
        if (env->hflags & HF_CS64_MASK) {
2123 eaa728ee bellard
            sp_mask = -1;
2124 20054ef0 Blue Swirl
        } else
2125 eaa728ee bellard
#endif
2126 20054ef0 Blue Swirl
        {
2127 eaa728ee bellard
            sp_mask = get_sp_mask(ss_e2);
2128 20054ef0 Blue Swirl
        }
2129 eaa728ee bellard
2130 eaa728ee bellard
        /* validate data segments */
2131 2999a0b2 Blue Swirl
        validate_seg(env, R_ES, rpl);
2132 2999a0b2 Blue Swirl
        validate_seg(env, R_DS, rpl);
2133 2999a0b2 Blue Swirl
        validate_seg(env, R_FS, rpl);
2134 2999a0b2 Blue Swirl
        validate_seg(env, R_GS, rpl);
2135 eaa728ee bellard
2136 eaa728ee bellard
        sp += addend;
2137 eaa728ee bellard
    }
2138 eaa728ee bellard
    SET_ESP(sp, sp_mask);
2139 eaa728ee bellard
    env->eip = new_eip;
2140 eaa728ee bellard
    if (is_iret) {
2141 eaa728ee bellard
        /* NOTE: 'cpl' is the _old_ CPL */
2142 eaa728ee bellard
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | RF_MASK | NT_MASK;
2143 20054ef0 Blue Swirl
        if (cpl == 0) {
2144 eaa728ee bellard
            eflags_mask |= IOPL_MASK;
2145 20054ef0 Blue Swirl
        }
2146 eaa728ee bellard
        iopl = (env->eflags >> IOPL_SHIFT) & 3;
2147 20054ef0 Blue Swirl
        if (cpl <= iopl) {
2148 eaa728ee bellard
            eflags_mask |= IF_MASK;
2149 20054ef0 Blue Swirl
        }
2150 20054ef0 Blue Swirl
        if (shift == 0) {
2151 eaa728ee bellard
            eflags_mask &= 0xffff;
2152 20054ef0 Blue Swirl
        }
2153 997ff0d9 Blue Swirl
        cpu_load_eflags(env, new_eflags, eflags_mask);
2154 eaa728ee bellard
    }
2155 eaa728ee bellard
    return;
2156 eaa728ee bellard
2157 eaa728ee bellard
 return_to_vm86:
2158 eaa728ee bellard
    POPL(ssp, sp, sp_mask, new_esp);
2159 eaa728ee bellard
    POPL(ssp, sp, sp_mask, new_ss);
2160 eaa728ee bellard
    POPL(ssp, sp, sp_mask, new_es);
2161 eaa728ee bellard
    POPL(ssp, sp, sp_mask, new_ds);
2162 eaa728ee bellard
    POPL(ssp, sp, sp_mask, new_fs);
2163 eaa728ee bellard
    POPL(ssp, sp, sp_mask, new_gs);
2164 eaa728ee bellard
2165 eaa728ee bellard
    /* modify processor state */
2166 997ff0d9 Blue Swirl
    cpu_load_eflags(env, new_eflags, TF_MASK | AC_MASK | ID_MASK |
2167 997ff0d9 Blue Swirl
                    IF_MASK | IOPL_MASK | VM_MASK | NT_MASK | VIF_MASK |
2168 997ff0d9 Blue Swirl
                    VIP_MASK);
2169 2999a0b2 Blue Swirl
    load_seg_vm(env, R_CS, new_cs & 0xffff);
2170 eaa728ee bellard
    cpu_x86_set_cpl(env, 3);
2171 2999a0b2 Blue Swirl
    load_seg_vm(env, R_SS, new_ss & 0xffff);
2172 2999a0b2 Blue Swirl
    load_seg_vm(env, R_ES, new_es & 0xffff);
2173 2999a0b2 Blue Swirl
    load_seg_vm(env, R_DS, new_ds & 0xffff);
2174 2999a0b2 Blue Swirl
    load_seg_vm(env, R_FS, new_fs & 0xffff);
2175 2999a0b2 Blue Swirl
    load_seg_vm(env, R_GS, new_gs & 0xffff);
2176 eaa728ee bellard
2177 eaa728ee bellard
    env->eip = new_eip & 0xffff;
2178 eaa728ee bellard
    ESP = new_esp;
2179 eaa728ee bellard
}
2180 eaa728ee bellard
2181 2999a0b2 Blue Swirl
void helper_iret_protected(CPUX86State *env, int shift, int next_eip)
2182 eaa728ee bellard
{
2183 eaa728ee bellard
    int tss_selector, type;
2184 eaa728ee bellard
    uint32_t e1, e2;
2185 eaa728ee bellard
2186 eaa728ee bellard
    /* specific case for TSS */
2187 eaa728ee bellard
    if (env->eflags & NT_MASK) {
2188 eaa728ee bellard
#ifdef TARGET_X86_64
2189 20054ef0 Blue Swirl
        if (env->hflags & HF_LMA_MASK) {
2190 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0D_GPF, 0);
2191 20054ef0 Blue Swirl
        }
2192 eaa728ee bellard
#endif
2193 329e607d Blue Swirl
        tss_selector = cpu_lduw_kernel(env, env->tr.base + 0);
2194 20054ef0 Blue Swirl
        if (tss_selector & 4) {
2195 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc);
2196 20054ef0 Blue Swirl
        }
2197 2999a0b2 Blue Swirl
        if (load_segment(env, &e1, &e2, tss_selector) != 0) {
2198 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc);
2199 20054ef0 Blue Swirl
        }
2200 eaa728ee bellard
        type = (e2 >> DESC_TYPE_SHIFT) & 0x17;
2201 eaa728ee bellard
        /* NOTE: we check both segment and busy TSS */
2202 20054ef0 Blue Swirl
        if (type != 3) {
2203 77b2bc2c Blue Swirl
            raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc);
2204 20054ef0 Blue Swirl
        }
2205 2999a0b2 Blue Swirl
        switch_tss(env, tss_selector, e1, e2, SWITCH_TSS_IRET, next_eip);
2206 eaa728ee bellard
    } else {
2207 2999a0b2 Blue Swirl
        helper_ret_protected(env, shift, 1, 0);
2208 eaa728ee bellard
    }
2209 db620f46 bellard
    env->hflags2 &= ~HF2_NMI_MASK;
2210 eaa728ee bellard
}
2211 eaa728ee bellard
2212 2999a0b2 Blue Swirl
void helper_lret_protected(CPUX86State *env, int shift, int addend)
2213 eaa728ee bellard
{
2214 2999a0b2 Blue Swirl
    helper_ret_protected(env, shift, 0, addend);
2215 eaa728ee bellard
}
2216 eaa728ee bellard
2217 2999a0b2 Blue Swirl
void helper_sysenter(CPUX86State *env)
2218 eaa728ee bellard
{
2219 eaa728ee bellard
    if (env->sysenter_cs == 0) {
2220 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, 0);
2221 eaa728ee bellard
    }
2222 eaa728ee bellard
    env->eflags &= ~(VM_MASK | IF_MASK | RF_MASK);
2223 eaa728ee bellard
    cpu_x86_set_cpl(env, 0);
2224 2436b61a balrog
2225 2436b61a balrog
#ifdef TARGET_X86_64
2226 2436b61a balrog
    if (env->hflags & HF_LMA_MASK) {
2227 2436b61a balrog
        cpu_x86_load_seg_cache(env, R_CS, env->sysenter_cs & 0xfffc,
2228 2436b61a balrog
                               0, 0xffffffff,
2229 2436b61a balrog
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2230 2436b61a balrog
                               DESC_S_MASK |
2231 20054ef0 Blue Swirl
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK |
2232 20054ef0 Blue Swirl
                               DESC_L_MASK);
2233 2436b61a balrog
    } else
2234 2436b61a balrog
#endif
2235 2436b61a balrog
    {
2236 2436b61a balrog
        cpu_x86_load_seg_cache(env, R_CS, env->sysenter_cs & 0xfffc,
2237 2436b61a balrog
                               0, 0xffffffff,
2238 2436b61a balrog
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2239 2436b61a balrog
                               DESC_S_MASK |
2240 2436b61a balrog
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
2241 2436b61a balrog
    }
2242 eaa728ee bellard
    cpu_x86_load_seg_cache(env, R_SS, (env->sysenter_cs + 8) & 0xfffc,
2243 eaa728ee bellard
                           0, 0xffffffff,
2244 eaa728ee bellard
                           DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2245 eaa728ee bellard
                           DESC_S_MASK |
2246 eaa728ee bellard
                           DESC_W_MASK | DESC_A_MASK);
2247 eaa728ee bellard
    ESP = env->sysenter_esp;
2248 eaa728ee bellard
    EIP = env->sysenter_eip;
2249 eaa728ee bellard
}
2250 eaa728ee bellard
2251 2999a0b2 Blue Swirl
void helper_sysexit(CPUX86State *env, int dflag)
2252 eaa728ee bellard
{
2253 eaa728ee bellard
    int cpl;
2254 eaa728ee bellard
2255 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
2256 eaa728ee bellard
    if (env->sysenter_cs == 0 || cpl != 0) {
2257 77b2bc2c Blue Swirl
        raise_exception_err(env, EXCP0D_GPF, 0);
2258 eaa728ee bellard
    }
2259 eaa728ee bellard
    cpu_x86_set_cpl(env, 3);
2260 2436b61a balrog
#ifdef TARGET_X86_64
2261 2436b61a balrog
    if (dflag == 2) {
2262 20054ef0 Blue Swirl
        cpu_x86_load_seg_cache(env, R_CS, ((env->sysenter_cs + 32) & 0xfffc) |
2263 20054ef0 Blue Swirl
                               3, 0, 0xffffffff,
2264 2436b61a balrog
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2265 2436b61a balrog
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
2266 20054ef0 Blue Swirl
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK |
2267 20054ef0 Blue Swirl
                               DESC_L_MASK);
2268 20054ef0 Blue Swirl
        cpu_x86_load_seg_cache(env, R_SS, ((env->sysenter_cs + 40) & 0xfffc) |
2269 20054ef0 Blue Swirl
                               3, 0, 0xffffffff,
2270 2436b61a balrog
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2271 2436b61a balrog
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
2272 2436b61a balrog
                               DESC_W_MASK | DESC_A_MASK);
2273 2436b61a balrog
    } else
2274 2436b61a balrog
#endif
2275 2436b61a balrog
    {
2276 20054ef0 Blue Swirl
        cpu_x86_load_seg_cache(env, R_CS, ((env->sysenter_cs + 16) & 0xfffc) |
2277 20054ef0 Blue Swirl
                               3, 0, 0xffffffff,
2278 2436b61a balrog
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2279 2436b61a balrog
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
2280 2436b61a balrog
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
2281 20054ef0 Blue Swirl
        cpu_x86_load_seg_cache(env, R_SS, ((env->sysenter_cs + 24) & 0xfffc) |
2282 20054ef0 Blue Swirl
                               3, 0, 0xffffffff,
2283 2436b61a balrog
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
2284 2436b61a balrog
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
2285 2436b61a balrog
                               DESC_W_MASK | DESC_A_MASK);
2286 2436b61a balrog
    }
2287 eaa728ee bellard
    ESP = ECX;
2288 eaa728ee bellard
    EIP = EDX;
2289 eaa728ee bellard
}
2290 eaa728ee bellard
2291 2999a0b2 Blue Swirl
target_ulong helper_lsl(CPUX86State *env, target_ulong selector1)
2292 eaa728ee bellard
{
2293 eaa728ee bellard
    unsigned int limit;
2294 eaa728ee bellard
    uint32_t e1, e2, eflags, selector;
2295 eaa728ee bellard
    int rpl, dpl, cpl, type;
2296 eaa728ee bellard
2297 eaa728ee bellard
    selector = selector1 & 0xffff;
2298 f0967a1a Blue Swirl
    eflags = cpu_cc_compute_all(env, CC_OP);
2299 20054ef0 Blue Swirl
    if ((selector & 0xfffc) == 0) {
2300 dc1ded53 aliguori
        goto fail;
2301 20054ef0 Blue Swirl
    }
2302 2999a0b2 Blue Swirl
    if (load_segment(env, &e1, &e2, selector) != 0) {
2303 eaa728ee bellard
        goto fail;
2304 20054ef0 Blue Swirl
    }
2305 eaa728ee bellard
    rpl = selector & 3;
2306 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2307 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
2308 eaa728ee bellard
    if (e2 & DESC_S_MASK) {
2309 eaa728ee bellard
        if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
2310 eaa728ee bellard
            /* conforming */
2311 eaa728ee bellard
        } else {
2312 20054ef0 Blue Swirl
            if (dpl < cpl || dpl < rpl) {
2313 eaa728ee bellard
                goto fail;
2314 20054ef0 Blue Swirl
            }
2315 eaa728ee bellard
        }
2316 eaa728ee bellard
    } else {
2317 eaa728ee bellard
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
2318 20054ef0 Blue Swirl
        switch (type) {
2319 eaa728ee bellard
        case 1:
2320 eaa728ee bellard
        case 2:
2321 eaa728ee bellard
        case 3:
2322 eaa728ee bellard
        case 9:
2323 eaa728ee bellard
        case 11:
2324 eaa728ee bellard
            break;
2325 eaa728ee bellard
        default:
2326 eaa728ee bellard
            goto fail;
2327 eaa728ee bellard
        }
2328 eaa728ee bellard
        if (dpl < cpl || dpl < rpl) {
2329 eaa728ee bellard
        fail:
2330 eaa728ee bellard
            CC_SRC = eflags & ~CC_Z;
2331 eaa728ee bellard
            return 0;
2332 eaa728ee bellard
        }
2333 eaa728ee bellard
    }
2334 eaa728ee bellard
    limit = get_seg_limit(e1, e2);
2335 eaa728ee bellard
    CC_SRC = eflags | CC_Z;
2336 eaa728ee bellard
    return limit;
2337 eaa728ee bellard
}
2338 eaa728ee bellard
2339 2999a0b2 Blue Swirl
target_ulong helper_lar(CPUX86State *env, target_ulong selector1)
2340 eaa728ee bellard
{
2341 eaa728ee bellard
    uint32_t e1, e2, eflags, selector;
2342 eaa728ee bellard
    int rpl, dpl, cpl, type;
2343 eaa728ee bellard
2344 eaa728ee bellard
    selector = selector1 & 0xffff;
2345 f0967a1a Blue Swirl
    eflags = cpu_cc_compute_all(env, CC_OP);
2346 20054ef0 Blue Swirl
    if ((selector & 0xfffc) == 0) {
2347 eaa728ee bellard
        goto fail;
2348 20054ef0 Blue Swirl
    }
2349 2999a0b2 Blue Swirl
    if (load_segment(env, &e1, &e2, selector) != 0) {
2350 eaa728ee bellard
        goto fail;
2351 20054ef0 Blue Swirl
    }
2352 eaa728ee bellard
    rpl = selector & 3;
2353 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2354 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
2355 eaa728ee bellard
    if (e2 & DESC_S_MASK) {
2356 eaa728ee bellard
        if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
2357 eaa728ee bellard
            /* conforming */
2358 eaa728ee bellard
        } else {
2359 20054ef0 Blue Swirl
            if (dpl < cpl || dpl < rpl) {
2360 eaa728ee bellard
                goto fail;
2361 20054ef0 Blue Swirl
            }
2362 eaa728ee bellard
        }
2363 eaa728ee bellard
    } else {
2364 eaa728ee bellard
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
2365 20054ef0 Blue Swirl
        switch (type) {
2366 eaa728ee bellard
        case 1:
2367 eaa728ee bellard
        case 2:
2368 eaa728ee bellard
        case 3:
2369 eaa728ee bellard
        case 4:
2370 eaa728ee bellard
        case 5:
2371 eaa728ee bellard
        case 9:
2372 eaa728ee bellard
        case 11:
2373 eaa728ee bellard
        case 12:
2374 eaa728ee bellard
            break;
2375 eaa728ee bellard
        default:
2376 eaa728ee bellard
            goto fail;
2377 eaa728ee bellard
        }
2378 eaa728ee bellard
        if (dpl < cpl || dpl < rpl) {
2379 eaa728ee bellard
        fail:
2380 eaa728ee bellard
            CC_SRC = eflags & ~CC_Z;
2381 eaa728ee bellard
            return 0;
2382 eaa728ee bellard
        }
2383 eaa728ee bellard
    }
2384 eaa728ee bellard
    CC_SRC = eflags | CC_Z;
2385 eaa728ee bellard
    return e2 & 0x00f0ff00;
2386 eaa728ee bellard
}
2387 eaa728ee bellard
2388 2999a0b2 Blue Swirl
void helper_verr(CPUX86State *env, target_ulong selector1)
2389 eaa728ee bellard
{
2390 eaa728ee bellard
    uint32_t e1, e2, eflags, selector;
2391 eaa728ee bellard
    int rpl, dpl, cpl;
2392 eaa728ee bellard
2393 eaa728ee bellard
    selector = selector1 & 0xffff;
2394 f0967a1a Blue Swirl
    eflags = cpu_cc_compute_all(env, CC_OP);
2395 20054ef0 Blue Swirl
    if ((selector & 0xfffc) == 0) {
2396 eaa728ee bellard
        goto fail;
2397 20054ef0 Blue Swirl
    }
2398 2999a0b2 Blue Swirl
    if (load_segment(env, &e1, &e2, selector) != 0) {
2399 eaa728ee bellard
        goto fail;
2400 20054ef0 Blue Swirl
    }
2401 20054ef0 Blue Swirl
    if (!(e2 & DESC_S_MASK)) {
2402 eaa728ee bellard
        goto fail;
2403 20054ef0 Blue Swirl
    }
2404 eaa728ee bellard
    rpl = selector & 3;
2405 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2406 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
2407 eaa728ee bellard
    if (e2 & DESC_CS_MASK) {
2408 20054ef0 Blue Swirl
        if (!(e2 & DESC_R_MASK)) {
2409 eaa728ee bellard
            goto fail;
2410 20054ef0 Blue Swirl
        }
2411 eaa728ee bellard
        if (!(e2 & DESC_C_MASK)) {
2412 20054ef0 Blue Swirl
            if (dpl < cpl || dpl < rpl) {
2413 eaa728ee bellard
                goto fail;
2414 20054ef0 Blue Swirl
            }
2415 eaa728ee bellard
        }
2416 eaa728ee bellard
    } else {
2417 eaa728ee bellard
        if (dpl < cpl || dpl < rpl) {
2418 eaa728ee bellard
        fail:
2419 eaa728ee bellard
            CC_SRC = eflags & ~CC_Z;
2420 eaa728ee bellard
            return;
2421 eaa728ee bellard
        }
2422 eaa728ee bellard
    }
2423 eaa728ee bellard
    CC_SRC = eflags | CC_Z;
2424 eaa728ee bellard
}
2425 eaa728ee bellard
2426 2999a0b2 Blue Swirl
void helper_verw(CPUX86State *env, target_ulong selector1)
2427 eaa728ee bellard
{
2428 eaa728ee bellard
    uint32_t e1, e2, eflags, selector;
2429 eaa728ee bellard
    int rpl, dpl, cpl;
2430 eaa728ee bellard
2431 eaa728ee bellard
    selector = selector1 & 0xffff;
2432 f0967a1a Blue Swirl
    eflags = cpu_cc_compute_all(env, CC_OP);
2433 20054ef0 Blue Swirl
    if ((selector & 0xfffc) == 0) {
2434 eaa728ee bellard
        goto fail;
2435 20054ef0 Blue Swirl
    }
2436 2999a0b2 Blue Swirl
    if (load_segment(env, &e1, &e2, selector) != 0) {
2437 eaa728ee bellard
        goto fail;
2438 20054ef0 Blue Swirl
    }
2439 20054ef0 Blue Swirl
    if (!(e2 & DESC_S_MASK)) {
2440 eaa728ee bellard
        goto fail;
2441 20054ef0 Blue Swirl
    }
2442 eaa728ee bellard
    rpl = selector & 3;
2443 eaa728ee bellard
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2444 eaa728ee bellard
    cpl = env->hflags & HF_CPL_MASK;
2445 eaa728ee bellard
    if (e2 & DESC_CS_MASK) {
2446 eaa728ee bellard
        goto fail;
2447 eaa728ee bellard
    } else {
2448 20054ef0 Blue Swirl
        if (dpl < cpl || dpl < rpl) {
2449 eaa728ee bellard
            goto fail;
2450 20054ef0 Blue Swirl
        }
2451 eaa728ee bellard
        if (!(e2 & DESC_W_MASK)) {
2452 eaa728ee bellard
        fail:
2453 eaa728ee bellard
            CC_SRC = eflags & ~CC_Z;
2454 eaa728ee bellard
            return;
2455 eaa728ee bellard
        }
2456 eaa728ee bellard
    }
2457 eaa728ee bellard
    CC_SRC = eflags | CC_Z;
2458 eaa728ee bellard
}
2459 eaa728ee bellard
2460 f299f437 Blue Swirl
#if defined(CONFIG_USER_ONLY)
2461 2999a0b2 Blue Swirl
void cpu_x86_load_seg(CPUX86State *env, int seg_reg, int selector)
2462 eaa728ee bellard
{
2463 f299f437 Blue Swirl
    if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
2464 f299f437 Blue Swirl
        selector &= 0xffff;
2465 f299f437 Blue Swirl
        cpu_x86_load_seg_cache(env, seg_reg, selector,
2466 f299f437 Blue Swirl
                               (selector << 4), 0xffff, 0);
2467 f299f437 Blue Swirl
    } else {
2468 2999a0b2 Blue Swirl
        helper_load_seg(env, seg_reg, selector);
2469 13822781 Aurelien Jarno
    }
2470 eaa728ee bellard
}
2471 eaa728ee bellard
#endif