]>
Commit | Line | Data |
---|---|---|
eaa728ee | 1 | /* |
10774999 BS |
2 | * x86 segmentation related helpers: |
3 | * TSS, interrupts, system calls, jumps and call/task gates, descriptors | |
eaa728ee FB |
4 | * |
5 | * Copyright (c) 2003 Fabrice Bellard | |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
8167ee88 | 18 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
eaa728ee | 19 | */ |
83dae095 | 20 | |
3e457172 | 21 | #include "cpu.h" |
1de7afc9 | 22 | #include "qemu/log.h" |
3e457172 | 23 | #include "helper.h" |
eaa728ee | 24 | |
3e457172 | 25 | //#define DEBUG_PCALL |
d12d51d5 | 26 | |
92fc4b58 | 27 | #if !defined(CONFIG_USER_ONLY) |
022c62cb | 28 | #include "exec/softmmu_exec.h" |
92fc4b58 BS |
29 | #endif /* !defined(CONFIG_USER_ONLY) */ |
30 | ||
d12d51d5 | 31 | #ifdef DEBUG_PCALL |
20054ef0 BS |
32 | # define LOG_PCALL(...) qemu_log_mask(CPU_LOG_PCALL, ## __VA_ARGS__) |
33 | # define LOG_PCALL_STATE(env) \ | |
6fd2a026 | 34 | log_cpu_state_mask(CPU_LOG_PCALL, (env), CPU_DUMP_CCOP) |
d12d51d5 | 35 | #else |
20054ef0 BS |
36 | # define LOG_PCALL(...) do { } while (0) |
37 | # define LOG_PCALL_STATE(env) do { } while (0) | |
d12d51d5 AL |
38 | #endif |
39 | ||
eaa728ee | 40 | /* return non zero if error */ |
2999a0b2 BS |
41 | static inline int load_segment(CPUX86State *env, uint32_t *e1_ptr, |
42 | uint32_t *e2_ptr, int selector) | |
eaa728ee FB |
43 | { |
44 | SegmentCache *dt; | |
45 | int index; | |
46 | target_ulong ptr; | |
47 | ||
20054ef0 | 48 | if (selector & 0x4) { |
eaa728ee | 49 | dt = &env->ldt; |
20054ef0 | 50 | } else { |
eaa728ee | 51 | dt = &env->gdt; |
20054ef0 | 52 | } |
eaa728ee | 53 | index = selector & ~7; |
20054ef0 | 54 | if ((index + 7) > dt->limit) { |
eaa728ee | 55 | return -1; |
20054ef0 | 56 | } |
eaa728ee | 57 | ptr = dt->base + index; |
329e607d BS |
58 | *e1_ptr = cpu_ldl_kernel(env, ptr); |
59 | *e2_ptr = cpu_ldl_kernel(env, ptr + 4); | |
eaa728ee FB |
60 | return 0; |
61 | } | |
62 | ||
63 | static inline unsigned int get_seg_limit(uint32_t e1, uint32_t e2) | |
64 | { | |
65 | unsigned int limit; | |
20054ef0 | 66 | |
eaa728ee | 67 | limit = (e1 & 0xffff) | (e2 & 0x000f0000); |
20054ef0 | 68 | if (e2 & DESC_G_MASK) { |
eaa728ee | 69 | limit = (limit << 12) | 0xfff; |
20054ef0 | 70 | } |
eaa728ee FB |
71 | return limit; |
72 | } | |
73 | ||
74 | static inline uint32_t get_seg_base(uint32_t e1, uint32_t e2) | |
75 | { | |
20054ef0 | 76 | return (e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000); |
eaa728ee FB |
77 | } |
78 | ||
20054ef0 BS |
79 | static inline void load_seg_cache_raw_dt(SegmentCache *sc, uint32_t e1, |
80 | uint32_t e2) | |
eaa728ee FB |
81 | { |
82 | sc->base = get_seg_base(e1, e2); | |
83 | sc->limit = get_seg_limit(e1, e2); | |
84 | sc->flags = e2; | |
85 | } | |
86 | ||
87 | /* init the segment cache in vm86 mode. */ | |
2999a0b2 | 88 | static inline void load_seg_vm(CPUX86State *env, int seg, int selector) |
eaa728ee FB |
89 | { |
90 | selector &= 0xffff; | |
91 | cpu_x86_load_seg_cache(env, seg, selector, | |
92 | (selector << 4), 0xffff, 0); | |
93 | } | |
94 | ||
2999a0b2 | 95 | static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr, |
eaa728ee FB |
96 | uint32_t *esp_ptr, int dpl) |
97 | { | |
98 | int type, index, shift; | |
99 | ||
100 | #if 0 | |
101 | { | |
102 | int i; | |
103 | printf("TR: base=%p limit=%x\n", env->tr.base, env->tr.limit); | |
20054ef0 | 104 | for (i = 0; i < env->tr.limit; i++) { |
eaa728ee | 105 | printf("%02x ", env->tr.base[i]); |
20054ef0 BS |
106 | if ((i & 7) == 7) { |
107 | printf("\n"); | |
108 | } | |
eaa728ee FB |
109 | } |
110 | printf("\n"); | |
111 | } | |
112 | #endif | |
113 | ||
20054ef0 | 114 | if (!(env->tr.flags & DESC_P_MASK)) { |
eaa728ee | 115 | cpu_abort(env, "invalid tss"); |
20054ef0 | 116 | } |
eaa728ee | 117 | type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf; |
20054ef0 | 118 | if ((type & 7) != 1) { |
eaa728ee | 119 | cpu_abort(env, "invalid tss type"); |
20054ef0 | 120 | } |
eaa728ee FB |
121 | shift = type >> 3; |
122 | index = (dpl * 4 + 2) << shift; | |
20054ef0 | 123 | if (index + (4 << shift) - 1 > env->tr.limit) { |
77b2bc2c | 124 | raise_exception_err(env, EXCP0A_TSS, env->tr.selector & 0xfffc); |
20054ef0 | 125 | } |
eaa728ee | 126 | if (shift == 0) { |
329e607d BS |
127 | *esp_ptr = cpu_lduw_kernel(env, env->tr.base + index); |
128 | *ss_ptr = cpu_lduw_kernel(env, env->tr.base + index + 2); | |
eaa728ee | 129 | } else { |
329e607d BS |
130 | *esp_ptr = cpu_ldl_kernel(env, env->tr.base + index); |
131 | *ss_ptr = cpu_lduw_kernel(env, env->tr.base + index + 4); | |
eaa728ee FB |
132 | } |
133 | } | |
134 | ||
135 | /* XXX: merge with load_seg() */ | |
2999a0b2 | 136 | static void tss_load_seg(CPUX86State *env, int seg_reg, int selector) |
eaa728ee FB |
137 | { |
138 | uint32_t e1, e2; | |
139 | int rpl, dpl, cpl; | |
140 | ||
141 | if ((selector & 0xfffc) != 0) { | |
2999a0b2 | 142 | if (load_segment(env, &e1, &e2, selector) != 0) { |
77b2bc2c | 143 | raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc); |
20054ef0 BS |
144 | } |
145 | if (!(e2 & DESC_S_MASK)) { | |
77b2bc2c | 146 | raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc); |
20054ef0 | 147 | } |
eaa728ee FB |
148 | rpl = selector & 3; |
149 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
150 | cpl = env->hflags & HF_CPL_MASK; | |
151 | if (seg_reg == R_CS) { | |
20054ef0 | 152 | if (!(e2 & DESC_CS_MASK)) { |
77b2bc2c | 153 | raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc); |
20054ef0 BS |
154 | } |
155 | /* XXX: is it correct? */ | |
156 | if (dpl != rpl) { | |
77b2bc2c | 157 | raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc); |
20054ef0 BS |
158 | } |
159 | if ((e2 & DESC_C_MASK) && dpl > rpl) { | |
77b2bc2c | 160 | raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc); |
20054ef0 | 161 | } |
eaa728ee FB |
162 | } else if (seg_reg == R_SS) { |
163 | /* SS must be writable data */ | |
20054ef0 | 164 | if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK)) { |
77b2bc2c | 165 | raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc); |
20054ef0 BS |
166 | } |
167 | if (dpl != cpl || dpl != rpl) { | |
77b2bc2c | 168 | raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc); |
20054ef0 | 169 | } |
eaa728ee FB |
170 | } else { |
171 | /* not readable code */ | |
20054ef0 | 172 | if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK)) { |
77b2bc2c | 173 | raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc); |
20054ef0 | 174 | } |
eaa728ee FB |
175 | /* if data or non conforming code, checks the rights */ |
176 | if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) { | |
20054ef0 | 177 | if (dpl < cpl || dpl < rpl) { |
77b2bc2c | 178 | raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc); |
20054ef0 | 179 | } |
eaa728ee FB |
180 | } |
181 | } | |
20054ef0 | 182 | if (!(e2 & DESC_P_MASK)) { |
77b2bc2c | 183 | raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc); |
20054ef0 | 184 | } |
eaa728ee | 185 | cpu_x86_load_seg_cache(env, seg_reg, selector, |
20054ef0 BS |
186 | get_seg_base(e1, e2), |
187 | get_seg_limit(e1, e2), | |
188 | e2); | |
eaa728ee | 189 | } else { |
20054ef0 | 190 | if (seg_reg == R_SS || seg_reg == R_CS) { |
77b2bc2c | 191 | raise_exception_err(env, EXCP0A_TSS, selector & 0xfffc); |
20054ef0 | 192 | } |
eaa728ee FB |
193 | } |
194 | } | |
195 | ||
196 | #define SWITCH_TSS_JMP 0 | |
197 | #define SWITCH_TSS_IRET 1 | |
198 | #define SWITCH_TSS_CALL 2 | |
199 | ||
200 | /* XXX: restore CPU state in registers (PowerPC case) */ | |
2999a0b2 | 201 | static void switch_tss(CPUX86State *env, int tss_selector, |
eaa728ee FB |
202 | uint32_t e1, uint32_t e2, int source, |
203 | uint32_t next_eip) | |
204 | { | |
205 | int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, v1, v2, i; | |
206 | target_ulong tss_base; | |
207 | uint32_t new_regs[8], new_segs[6]; | |
208 | uint32_t new_eflags, new_eip, new_cr3, new_ldt, new_trap; | |
209 | uint32_t old_eflags, eflags_mask; | |
210 | SegmentCache *dt; | |
211 | int index; | |
212 | target_ulong ptr; | |
213 | ||
214 | type = (e2 >> DESC_TYPE_SHIFT) & 0xf; | |
20054ef0 BS |
215 | LOG_PCALL("switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector, type, |
216 | source); | |
eaa728ee FB |
217 | |
218 | /* if task gate, we read the TSS segment and we load it */ | |
219 | if (type == 5) { | |
20054ef0 | 220 | if (!(e2 & DESC_P_MASK)) { |
77b2bc2c | 221 | raise_exception_err(env, EXCP0B_NOSEG, tss_selector & 0xfffc); |
20054ef0 | 222 | } |
eaa728ee | 223 | tss_selector = e1 >> 16; |
20054ef0 | 224 | if (tss_selector & 4) { |
77b2bc2c | 225 | raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc); |
20054ef0 | 226 | } |
2999a0b2 | 227 | if (load_segment(env, &e1, &e2, tss_selector) != 0) { |
77b2bc2c | 228 | raise_exception_err(env, EXCP0D_GPF, tss_selector & 0xfffc); |
20054ef0 BS |
229 | } |
230 | if (e2 & DESC_S_MASK) { | |
77b2bc2c | 231 | raise_exception_err(env, EXCP0D_GPF, tss_selector & 0xfffc); |
20054ef0 | 232 | } |
eaa728ee | 233 | type = (e2 >> DESC_TYPE_SHIFT) & 0xf; |
20054ef0 | 234 | if ((type & 7) != 1) { |
77b2bc2c | 235 | raise_exception_err(env, EXCP0D_GPF, tss_selector & 0xfffc); |
20054ef0 | 236 | } |
eaa728ee FB |
237 | } |
238 | ||
20054ef0 | 239 | if (!(e2 & DESC_P_MASK)) { |
77b2bc2c | 240 | raise_exception_err(env, EXCP0B_NOSEG, tss_selector & 0xfffc); |
20054ef0 | 241 | } |
eaa728ee | 242 | |
20054ef0 | 243 | if (type & 8) { |
eaa728ee | 244 | tss_limit_max = 103; |
20054ef0 | 245 | } else { |
eaa728ee | 246 | tss_limit_max = 43; |
20054ef0 | 247 | } |
eaa728ee FB |
248 | tss_limit = get_seg_limit(e1, e2); |
249 | tss_base = get_seg_base(e1, e2); | |
250 | if ((tss_selector & 4) != 0 || | |
20054ef0 | 251 | tss_limit < tss_limit_max) { |
77b2bc2c | 252 | raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc); |
20054ef0 | 253 | } |
eaa728ee | 254 | old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf; |
20054ef0 | 255 | if (old_type & 8) { |
eaa728ee | 256 | old_tss_limit_max = 103; |
20054ef0 | 257 | } else { |
eaa728ee | 258 | old_tss_limit_max = 43; |
20054ef0 | 259 | } |
eaa728ee FB |
260 | |
261 | /* read all the registers from the new TSS */ | |
262 | if (type & 8) { | |
263 | /* 32 bit */ | |
329e607d BS |
264 | new_cr3 = cpu_ldl_kernel(env, tss_base + 0x1c); |
265 | new_eip = cpu_ldl_kernel(env, tss_base + 0x20); | |
266 | new_eflags = cpu_ldl_kernel(env, tss_base + 0x24); | |
20054ef0 | 267 | for (i = 0; i < 8; i++) { |
329e607d | 268 | new_regs[i] = cpu_ldl_kernel(env, tss_base + (0x28 + i * 4)); |
20054ef0 BS |
269 | } |
270 | for (i = 0; i < 6; i++) { | |
329e607d | 271 | new_segs[i] = cpu_lduw_kernel(env, tss_base + (0x48 + i * 4)); |
20054ef0 | 272 | } |
329e607d BS |
273 | new_ldt = cpu_lduw_kernel(env, tss_base + 0x60); |
274 | new_trap = cpu_ldl_kernel(env, tss_base + 0x64); | |
eaa728ee FB |
275 | } else { |
276 | /* 16 bit */ | |
277 | new_cr3 = 0; | |
329e607d BS |
278 | new_eip = cpu_lduw_kernel(env, tss_base + 0x0e); |
279 | new_eflags = cpu_lduw_kernel(env, tss_base + 0x10); | |
20054ef0 | 280 | for (i = 0; i < 8; i++) { |
329e607d BS |
281 | new_regs[i] = cpu_lduw_kernel(env, tss_base + (0x12 + i * 2)) | |
282 | 0xffff0000; | |
20054ef0 BS |
283 | } |
284 | for (i = 0; i < 4; i++) { | |
329e607d | 285 | new_segs[i] = cpu_lduw_kernel(env, tss_base + (0x22 + i * 4)); |
20054ef0 | 286 | } |
329e607d | 287 | new_ldt = cpu_lduw_kernel(env, tss_base + 0x2a); |
eaa728ee FB |
288 | new_segs[R_FS] = 0; |
289 | new_segs[R_GS] = 0; | |
290 | new_trap = 0; | |
291 | } | |
4581cbcd BS |
292 | /* XXX: avoid a compiler warning, see |
293 | http://support.amd.com/us/Processor_TechDocs/24593.pdf | |
294 | chapters 12.2.5 and 13.2.4 on how to implement TSS Trap bit */ | |
295 | (void)new_trap; | |
eaa728ee FB |
296 | |
297 | /* NOTE: we must avoid memory exceptions during the task switch, | |
298 | so we make dummy accesses before */ | |
299 | /* XXX: it can still fail in some cases, so a bigger hack is | |
300 | necessary to valid the TLB after having done the accesses */ | |
301 | ||
329e607d BS |
302 | v1 = cpu_ldub_kernel(env, env->tr.base); |
303 | v2 = cpu_ldub_kernel(env, env->tr.base + old_tss_limit_max); | |
304 | cpu_stb_kernel(env, env->tr.base, v1); | |
305 | cpu_stb_kernel(env, env->tr.base + old_tss_limit_max, v2); | |
eaa728ee FB |
306 | |
307 | /* clear busy bit (it is restartable) */ | |
308 | if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_IRET) { | |
309 | target_ulong ptr; | |
310 | uint32_t e2; | |
20054ef0 | 311 | |
eaa728ee | 312 | ptr = env->gdt.base + (env->tr.selector & ~7); |
329e607d | 313 | e2 = cpu_ldl_kernel(env, ptr + 4); |
eaa728ee | 314 | e2 &= ~DESC_TSS_BUSY_MASK; |
329e607d | 315 | cpu_stl_kernel(env, ptr + 4, e2); |
eaa728ee | 316 | } |
997ff0d9 | 317 | old_eflags = cpu_compute_eflags(env); |
20054ef0 | 318 | if (source == SWITCH_TSS_IRET) { |
eaa728ee | 319 | old_eflags &= ~NT_MASK; |
20054ef0 | 320 | } |
eaa728ee FB |
321 | |
322 | /* save the current state in the old TSS */ | |
323 | if (type & 8) { | |
324 | /* 32 bit */ | |
329e607d BS |
325 | cpu_stl_kernel(env, env->tr.base + 0x20, next_eip); |
326 | cpu_stl_kernel(env, env->tr.base + 0x24, old_eflags); | |
327 | cpu_stl_kernel(env, env->tr.base + (0x28 + 0 * 4), EAX); | |
328 | cpu_stl_kernel(env, env->tr.base + (0x28 + 1 * 4), ECX); | |
329 | cpu_stl_kernel(env, env->tr.base + (0x28 + 2 * 4), EDX); | |
330 | cpu_stl_kernel(env, env->tr.base + (0x28 + 3 * 4), EBX); | |
331 | cpu_stl_kernel(env, env->tr.base + (0x28 + 4 * 4), ESP); | |
332 | cpu_stl_kernel(env, env->tr.base + (0x28 + 5 * 4), EBP); | |
333 | cpu_stl_kernel(env, env->tr.base + (0x28 + 6 * 4), ESI); | |
334 | cpu_stl_kernel(env, env->tr.base + (0x28 + 7 * 4), EDI); | |
20054ef0 | 335 | for (i = 0; i < 6; i++) { |
329e607d BS |
336 | cpu_stw_kernel(env, env->tr.base + (0x48 + i * 4), |
337 | env->segs[i].selector); | |
20054ef0 | 338 | } |
eaa728ee FB |
339 | } else { |
340 | /* 16 bit */ | |
329e607d BS |
341 | cpu_stw_kernel(env, env->tr.base + 0x0e, next_eip); |
342 | cpu_stw_kernel(env, env->tr.base + 0x10, old_eflags); | |
343 | cpu_stw_kernel(env, env->tr.base + (0x12 + 0 * 2), EAX); | |
344 | cpu_stw_kernel(env, env->tr.base + (0x12 + 1 * 2), ECX); | |
345 | cpu_stw_kernel(env, env->tr.base + (0x12 + 2 * 2), EDX); | |
346 | cpu_stw_kernel(env, env->tr.base + (0x12 + 3 * 2), EBX); | |
347 | cpu_stw_kernel(env, env->tr.base + (0x12 + 4 * 2), ESP); | |
348 | cpu_stw_kernel(env, env->tr.base + (0x12 + 5 * 2), EBP); | |
349 | cpu_stw_kernel(env, env->tr.base + (0x12 + 6 * 2), ESI); | |
350 | cpu_stw_kernel(env, env->tr.base + (0x12 + 7 * 2), EDI); | |
20054ef0 | 351 | for (i = 0; i < 4; i++) { |
329e607d BS |
352 | cpu_stw_kernel(env, env->tr.base + (0x22 + i * 4), |
353 | env->segs[i].selector); | |
20054ef0 | 354 | } |
eaa728ee FB |
355 | } |
356 | ||
357 | /* now if an exception occurs, it will occurs in the next task | |
358 | context */ | |
359 | ||
360 | if (source == SWITCH_TSS_CALL) { | |
329e607d | 361 | cpu_stw_kernel(env, tss_base, env->tr.selector); |
eaa728ee FB |
362 | new_eflags |= NT_MASK; |
363 | } | |
364 | ||
365 | /* set busy bit */ | |
366 | if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_CALL) { | |
367 | target_ulong ptr; | |
368 | uint32_t e2; | |
20054ef0 | 369 | |
eaa728ee | 370 | ptr = env->gdt.base + (tss_selector & ~7); |
329e607d | 371 | e2 = cpu_ldl_kernel(env, ptr + 4); |
eaa728ee | 372 | e2 |= DESC_TSS_BUSY_MASK; |
329e607d | 373 | cpu_stl_kernel(env, ptr + 4, e2); |
eaa728ee FB |
374 | } |
375 | ||
376 | /* set the new CPU state */ | |
377 | /* from this point, any exception which occurs can give problems */ | |
378 | env->cr[0] |= CR0_TS_MASK; | |
379 | env->hflags |= HF_TS_MASK; | |
380 | env->tr.selector = tss_selector; | |
381 | env->tr.base = tss_base; | |
382 | env->tr.limit = tss_limit; | |
383 | env->tr.flags = e2 & ~DESC_TSS_BUSY_MASK; | |
384 | ||
385 | if ((type & 8) && (env->cr[0] & CR0_PG_MASK)) { | |
386 | cpu_x86_update_cr3(env, new_cr3); | |
387 | } | |
388 | ||
389 | /* load all registers without an exception, then reload them with | |
390 | possible exception */ | |
391 | env->eip = new_eip; | |
392 | eflags_mask = TF_MASK | AC_MASK | ID_MASK | | |
393 | IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK; | |
20054ef0 | 394 | if (!(type & 8)) { |
eaa728ee | 395 | eflags_mask &= 0xffff; |
20054ef0 | 396 | } |
997ff0d9 | 397 | cpu_load_eflags(env, new_eflags, eflags_mask); |
20054ef0 | 398 | /* XXX: what to do in 16 bit case? */ |
eaa728ee FB |
399 | EAX = new_regs[0]; |
400 | ECX = new_regs[1]; | |
401 | EDX = new_regs[2]; | |
402 | EBX = new_regs[3]; | |
403 | ESP = new_regs[4]; | |
404 | EBP = new_regs[5]; | |
405 | ESI = new_regs[6]; | |
406 | EDI = new_regs[7]; | |
407 | if (new_eflags & VM_MASK) { | |
20054ef0 | 408 | for (i = 0; i < 6; i++) { |
2999a0b2 | 409 | load_seg_vm(env, i, new_segs[i]); |
20054ef0 | 410 | } |
eaa728ee FB |
411 | /* in vm86, CPL is always 3 */ |
412 | cpu_x86_set_cpl(env, 3); | |
413 | } else { | |
414 | /* CPL is set the RPL of CS */ | |
415 | cpu_x86_set_cpl(env, new_segs[R_CS] & 3); | |
416 | /* first just selectors as the rest may trigger exceptions */ | |
20054ef0 | 417 | for (i = 0; i < 6; i++) { |
eaa728ee | 418 | cpu_x86_load_seg_cache(env, i, new_segs[i], 0, 0, 0); |
20054ef0 | 419 | } |
eaa728ee FB |
420 | } |
421 | ||
422 | env->ldt.selector = new_ldt & ~4; | |
423 | env->ldt.base = 0; | |
424 | env->ldt.limit = 0; | |
425 | env->ldt.flags = 0; | |
426 | ||
427 | /* load the LDT */ | |
20054ef0 | 428 | if (new_ldt & 4) { |
77b2bc2c | 429 | raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc); |
20054ef0 | 430 | } |
eaa728ee FB |
431 | |
432 | if ((new_ldt & 0xfffc) != 0) { | |
433 | dt = &env->gdt; | |
434 | index = new_ldt & ~7; | |
20054ef0 | 435 | if ((index + 7) > dt->limit) { |
77b2bc2c | 436 | raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc); |
20054ef0 | 437 | } |
eaa728ee | 438 | ptr = dt->base + index; |
329e607d BS |
439 | e1 = cpu_ldl_kernel(env, ptr); |
440 | e2 = cpu_ldl_kernel(env, ptr + 4); | |
20054ef0 | 441 | if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2) { |
77b2bc2c | 442 | raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc); |
20054ef0 BS |
443 | } |
444 | if (!(e2 & DESC_P_MASK)) { | |
77b2bc2c | 445 | raise_exception_err(env, EXCP0A_TSS, new_ldt & 0xfffc); |
20054ef0 | 446 | } |
eaa728ee FB |
447 | load_seg_cache_raw_dt(&env->ldt, e1, e2); |
448 | } | |
449 | ||
450 | /* load the segments */ | |
451 | if (!(new_eflags & VM_MASK)) { | |
2999a0b2 BS |
452 | tss_load_seg(env, R_CS, new_segs[R_CS]); |
453 | tss_load_seg(env, R_SS, new_segs[R_SS]); | |
454 | tss_load_seg(env, R_ES, new_segs[R_ES]); | |
455 | tss_load_seg(env, R_DS, new_segs[R_DS]); | |
456 | tss_load_seg(env, R_FS, new_segs[R_FS]); | |
457 | tss_load_seg(env, R_GS, new_segs[R_GS]); | |
eaa728ee FB |
458 | } |
459 | ||
460 | /* check that EIP is in the CS segment limits */ | |
461 | if (new_eip > env->segs[R_CS].limit) { | |
20054ef0 | 462 | /* XXX: different exception if CALL? */ |
77b2bc2c | 463 | raise_exception_err(env, EXCP0D_GPF, 0); |
eaa728ee | 464 | } |
01df040b AL |
465 | |
466 | #ifndef CONFIG_USER_ONLY | |
467 | /* reset local breakpoints */ | |
428065ce LG |
468 | if (env->dr[7] & DR7_LOCAL_BP_MASK) { |
469 | for (i = 0; i < DR7_MAX_BP; i++) { | |
5902564a LG |
470 | if (hw_local_breakpoint_enabled(env->dr[7], i) && |
471 | !hw_global_breakpoint_enabled(env->dr[7], i)) { | |
01df040b | 472 | hw_breakpoint_remove(env, i); |
20054ef0 | 473 | } |
01df040b | 474 | } |
428065ce | 475 | env->dr[7] &= ~DR7_LOCAL_BP_MASK; |
01df040b AL |
476 | } |
477 | #endif | |
eaa728ee FB |
478 | } |
479 | ||
eaa728ee FB |
480 | static inline unsigned int get_sp_mask(unsigned int e2) |
481 | { | |
20054ef0 | 482 | if (e2 & DESC_B_MASK) { |
eaa728ee | 483 | return 0xffffffff; |
20054ef0 | 484 | } else { |
eaa728ee | 485 | return 0xffff; |
20054ef0 | 486 | } |
eaa728ee FB |
487 | } |
488 | ||
20054ef0 | 489 | static int exception_has_error_code(int intno) |
2ed51f5b | 490 | { |
20054ef0 BS |
491 | switch (intno) { |
492 | case 8: | |
493 | case 10: | |
494 | case 11: | |
495 | case 12: | |
496 | case 13: | |
497 | case 14: | |
498 | case 17: | |
499 | return 1; | |
500 | } | |
501 | return 0; | |
2ed51f5b AL |
502 | } |
503 | ||
eaa728ee | 504 | #ifdef TARGET_X86_64 |
20054ef0 BS |
505 | #define SET_ESP(val, sp_mask) \ |
506 | do { \ | |
507 | if ((sp_mask) == 0xffff) { \ | |
508 | ESP = (ESP & ~0xffff) | ((val) & 0xffff); \ | |
509 | } else if ((sp_mask) == 0xffffffffLL) { \ | |
510 | ESP = (uint32_t)(val); \ | |
511 | } else { \ | |
512 | ESP = (val); \ | |
513 | } \ | |
514 | } while (0) | |
eaa728ee | 515 | #else |
20054ef0 BS |
516 | #define SET_ESP(val, sp_mask) \ |
517 | do { \ | |
518 | ESP = (ESP & ~(sp_mask)) | ((val) & (sp_mask)); \ | |
519 | } while (0) | |
eaa728ee FB |
520 | #endif |
521 | ||
c0a04f0e AL |
522 | /* in 64-bit machines, this can overflow. So this segment addition macro |
523 | * can be used to trim the value to 32-bit whenever needed */ | |
524 | #define SEG_ADDL(ssp, sp, sp_mask) ((uint32_t)((ssp) + (sp & (sp_mask)))) | |
525 | ||
eaa728ee | 526 | /* XXX: add a is_user flag to have proper security support */ |
329e607d BS |
527 | #define PUSHW(ssp, sp, sp_mask, val) \ |
528 | { \ | |
529 | sp -= 2; \ | |
530 | cpu_stw_kernel(env, (ssp) + (sp & (sp_mask)), (val)); \ | |
20054ef0 | 531 | } |
eaa728ee | 532 | |
20054ef0 BS |
533 | #define PUSHL(ssp, sp, sp_mask, val) \ |
534 | { \ | |
535 | sp -= 4; \ | |
329e607d | 536 | cpu_stl_kernel(env, SEG_ADDL(ssp, sp, sp_mask), (uint32_t)(val)); \ |
20054ef0 | 537 | } |
eaa728ee | 538 | |
329e607d BS |
539 | #define POPW(ssp, sp, sp_mask, val) \ |
540 | { \ | |
541 | val = cpu_lduw_kernel(env, (ssp) + (sp & (sp_mask))); \ | |
542 | sp += 2; \ | |
20054ef0 | 543 | } |
eaa728ee | 544 | |
329e607d BS |
545 | #define POPL(ssp, sp, sp_mask, val) \ |
546 | { \ | |
547 | val = (uint32_t)cpu_ldl_kernel(env, SEG_ADDL(ssp, sp, sp_mask)); \ | |
548 | sp += 4; \ | |
20054ef0 | 549 | } |
eaa728ee FB |
550 | |
551 | /* protected mode interrupt */ | |
2999a0b2 BS |
552 | static void do_interrupt_protected(CPUX86State *env, int intno, int is_int, |
553 | int error_code, unsigned int next_eip, | |
554 | int is_hw) | |
eaa728ee FB |
555 | { |
556 | SegmentCache *dt; | |
557 | target_ulong ptr, ssp; | |
558 | int type, dpl, selector, ss_dpl, cpl; | |
559 | int has_error_code, new_stack, shift; | |
1c918eba | 560 | uint32_t e1, e2, offset, ss = 0, esp, ss_e1 = 0, ss_e2 = 0; |
eaa728ee | 561 | uint32_t old_eip, sp_mask; |
eaa728ee | 562 | |
eaa728ee | 563 | has_error_code = 0; |
20054ef0 BS |
564 | if (!is_int && !is_hw) { |
565 | has_error_code = exception_has_error_code(intno); | |
566 | } | |
567 | if (is_int) { | |
eaa728ee | 568 | old_eip = next_eip; |
20054ef0 | 569 | } else { |
eaa728ee | 570 | old_eip = env->eip; |
20054ef0 | 571 | } |
eaa728ee FB |
572 | |
573 | dt = &env->idt; | |
20054ef0 | 574 | if (intno * 8 + 7 > dt->limit) { |
77b2bc2c | 575 | raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); |
20054ef0 | 576 | } |
eaa728ee | 577 | ptr = dt->base + intno * 8; |
329e607d BS |
578 | e1 = cpu_ldl_kernel(env, ptr); |
579 | e2 = cpu_ldl_kernel(env, ptr + 4); | |
eaa728ee FB |
580 | /* check gate type */ |
581 | type = (e2 >> DESC_TYPE_SHIFT) & 0x1f; | |
20054ef0 | 582 | switch (type) { |
eaa728ee FB |
583 | case 5: /* task gate */ |
584 | /* must do that check here to return the correct error code */ | |
20054ef0 | 585 | if (!(e2 & DESC_P_MASK)) { |
77b2bc2c | 586 | raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2); |
20054ef0 | 587 | } |
2999a0b2 | 588 | switch_tss(env, intno * 8, e1, e2, SWITCH_TSS_CALL, old_eip); |
eaa728ee FB |
589 | if (has_error_code) { |
590 | int type; | |
591 | uint32_t mask; | |
20054ef0 | 592 | |
eaa728ee FB |
593 | /* push the error code */ |
594 | type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf; | |
595 | shift = type >> 3; | |
20054ef0 | 596 | if (env->segs[R_SS].flags & DESC_B_MASK) { |
eaa728ee | 597 | mask = 0xffffffff; |
20054ef0 | 598 | } else { |
eaa728ee | 599 | mask = 0xffff; |
20054ef0 | 600 | } |
eaa728ee FB |
601 | esp = (ESP - (2 << shift)) & mask; |
602 | ssp = env->segs[R_SS].base + esp; | |
20054ef0 | 603 | if (shift) { |
329e607d | 604 | cpu_stl_kernel(env, ssp, error_code); |
20054ef0 | 605 | } else { |
329e607d | 606 | cpu_stw_kernel(env, ssp, error_code); |
20054ef0 | 607 | } |
eaa728ee FB |
608 | SET_ESP(esp, mask); |
609 | } | |
610 | return; | |
611 | case 6: /* 286 interrupt gate */ | |
612 | case 7: /* 286 trap gate */ | |
613 | case 14: /* 386 interrupt gate */ | |
614 | case 15: /* 386 trap gate */ | |
615 | break; | |
616 | default: | |
77b2bc2c | 617 | raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); |
eaa728ee FB |
618 | break; |
619 | } | |
620 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
621 | cpl = env->hflags & HF_CPL_MASK; | |
1235fc06 | 622 | /* check privilege if software int */ |
20054ef0 | 623 | if (is_int && dpl < cpl) { |
77b2bc2c | 624 | raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); |
20054ef0 | 625 | } |
eaa728ee | 626 | /* check valid bit */ |
20054ef0 | 627 | if (!(e2 & DESC_P_MASK)) { |
77b2bc2c | 628 | raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2); |
20054ef0 | 629 | } |
eaa728ee FB |
630 | selector = e1 >> 16; |
631 | offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff); | |
20054ef0 | 632 | if ((selector & 0xfffc) == 0) { |
77b2bc2c | 633 | raise_exception_err(env, EXCP0D_GPF, 0); |
20054ef0 | 634 | } |
2999a0b2 | 635 | if (load_segment(env, &e1, &e2, selector) != 0) { |
77b2bc2c | 636 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 BS |
637 | } |
638 | if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) { | |
77b2bc2c | 639 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 640 | } |
eaa728ee | 641 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; |
20054ef0 | 642 | if (dpl > cpl) { |
77b2bc2c | 643 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 BS |
644 | } |
645 | if (!(e2 & DESC_P_MASK)) { | |
77b2bc2c | 646 | raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc); |
20054ef0 | 647 | } |
eaa728ee FB |
648 | if (!(e2 & DESC_C_MASK) && dpl < cpl) { |
649 | /* to inner privilege */ | |
2999a0b2 | 650 | get_ss_esp_from_tss(env, &ss, &esp, dpl); |
20054ef0 | 651 | if ((ss & 0xfffc) == 0) { |
77b2bc2c | 652 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 BS |
653 | } |
654 | if ((ss & 3) != dpl) { | |
77b2bc2c | 655 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 | 656 | } |
2999a0b2 | 657 | if (load_segment(env, &ss_e1, &ss_e2, ss) != 0) { |
77b2bc2c | 658 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 | 659 | } |
eaa728ee | 660 | ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3; |
20054ef0 | 661 | if (ss_dpl != dpl) { |
77b2bc2c | 662 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 | 663 | } |
eaa728ee FB |
664 | if (!(ss_e2 & DESC_S_MASK) || |
665 | (ss_e2 & DESC_CS_MASK) || | |
20054ef0 | 666 | !(ss_e2 & DESC_W_MASK)) { |
77b2bc2c | 667 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 BS |
668 | } |
669 | if (!(ss_e2 & DESC_P_MASK)) { | |
77b2bc2c | 670 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 | 671 | } |
eaa728ee FB |
672 | new_stack = 1; |
673 | sp_mask = get_sp_mask(ss_e2); | |
674 | ssp = get_seg_base(ss_e1, ss_e2); | |
675 | } else if ((e2 & DESC_C_MASK) || dpl == cpl) { | |
676 | /* to same privilege */ | |
20054ef0 | 677 | if (env->eflags & VM_MASK) { |
77b2bc2c | 678 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 679 | } |
eaa728ee FB |
680 | new_stack = 0; |
681 | sp_mask = get_sp_mask(env->segs[R_SS].flags); | |
682 | ssp = env->segs[R_SS].base; | |
683 | esp = ESP; | |
684 | dpl = cpl; | |
685 | } else { | |
77b2bc2c | 686 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
eaa728ee FB |
687 | new_stack = 0; /* avoid warning */ |
688 | sp_mask = 0; /* avoid warning */ | |
689 | ssp = 0; /* avoid warning */ | |
690 | esp = 0; /* avoid warning */ | |
691 | } | |
692 | ||
693 | shift = type >> 3; | |
694 | ||
695 | #if 0 | |
696 | /* XXX: check that enough room is available */ | |
697 | push_size = 6 + (new_stack << 2) + (has_error_code << 1); | |
20054ef0 | 698 | if (env->eflags & VM_MASK) { |
eaa728ee | 699 | push_size += 8; |
20054ef0 | 700 | } |
eaa728ee FB |
701 | push_size <<= shift; |
702 | #endif | |
703 | if (shift == 1) { | |
704 | if (new_stack) { | |
705 | if (env->eflags & VM_MASK) { | |
706 | PUSHL(ssp, esp, sp_mask, env->segs[R_GS].selector); | |
707 | PUSHL(ssp, esp, sp_mask, env->segs[R_FS].selector); | |
708 | PUSHL(ssp, esp, sp_mask, env->segs[R_DS].selector); | |
709 | PUSHL(ssp, esp, sp_mask, env->segs[R_ES].selector); | |
710 | } | |
711 | PUSHL(ssp, esp, sp_mask, env->segs[R_SS].selector); | |
712 | PUSHL(ssp, esp, sp_mask, ESP); | |
713 | } | |
997ff0d9 | 714 | PUSHL(ssp, esp, sp_mask, cpu_compute_eflags(env)); |
eaa728ee FB |
715 | PUSHL(ssp, esp, sp_mask, env->segs[R_CS].selector); |
716 | PUSHL(ssp, esp, sp_mask, old_eip); | |
717 | if (has_error_code) { | |
718 | PUSHL(ssp, esp, sp_mask, error_code); | |
719 | } | |
720 | } else { | |
721 | if (new_stack) { | |
722 | if (env->eflags & VM_MASK) { | |
723 | PUSHW(ssp, esp, sp_mask, env->segs[R_GS].selector); | |
724 | PUSHW(ssp, esp, sp_mask, env->segs[R_FS].selector); | |
725 | PUSHW(ssp, esp, sp_mask, env->segs[R_DS].selector); | |
726 | PUSHW(ssp, esp, sp_mask, env->segs[R_ES].selector); | |
727 | } | |
728 | PUSHW(ssp, esp, sp_mask, env->segs[R_SS].selector); | |
729 | PUSHW(ssp, esp, sp_mask, ESP); | |
730 | } | |
997ff0d9 | 731 | PUSHW(ssp, esp, sp_mask, cpu_compute_eflags(env)); |
eaa728ee FB |
732 | PUSHW(ssp, esp, sp_mask, env->segs[R_CS].selector); |
733 | PUSHW(ssp, esp, sp_mask, old_eip); | |
734 | if (has_error_code) { | |
735 | PUSHW(ssp, esp, sp_mask, error_code); | |
736 | } | |
737 | } | |
738 | ||
739 | if (new_stack) { | |
740 | if (env->eflags & VM_MASK) { | |
741 | cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0, 0); | |
742 | cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0, 0); | |
743 | cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0, 0); | |
744 | cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0, 0); | |
745 | } | |
746 | ss = (ss & ~3) | dpl; | |
747 | cpu_x86_load_seg_cache(env, R_SS, ss, | |
748 | ssp, get_seg_limit(ss_e1, ss_e2), ss_e2); | |
749 | } | |
750 | SET_ESP(esp, sp_mask); | |
751 | ||
752 | selector = (selector & ~3) | dpl; | |
753 | cpu_x86_load_seg_cache(env, R_CS, selector, | |
754 | get_seg_base(e1, e2), | |
755 | get_seg_limit(e1, e2), | |
756 | e2); | |
757 | cpu_x86_set_cpl(env, dpl); | |
758 | env->eip = offset; | |
759 | ||
760 | /* interrupt gate clear IF mask */ | |
761 | if ((type & 1) == 0) { | |
762 | env->eflags &= ~IF_MASK; | |
763 | } | |
764 | env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK); | |
765 | } | |
766 | ||
767 | #ifdef TARGET_X86_64 | |
768 | ||
20054ef0 BS |
769 | #define PUSHQ(sp, val) \ |
770 | { \ | |
771 | sp -= 8; \ | |
329e607d | 772 | cpu_stq_kernel(env, sp, (val)); \ |
20054ef0 | 773 | } |
eaa728ee | 774 | |
20054ef0 BS |
775 | #define POPQ(sp, val) \ |
776 | { \ | |
329e607d | 777 | val = cpu_ldq_kernel(env, sp); \ |
20054ef0 BS |
778 | sp += 8; \ |
779 | } | |
eaa728ee | 780 | |
2999a0b2 | 781 | static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level) |
eaa728ee FB |
782 | { |
783 | int index; | |
784 | ||
785 | #if 0 | |
786 | printf("TR: base=" TARGET_FMT_lx " limit=%x\n", | |
787 | env->tr.base, env->tr.limit); | |
788 | #endif | |
789 | ||
20054ef0 | 790 | if (!(env->tr.flags & DESC_P_MASK)) { |
eaa728ee | 791 | cpu_abort(env, "invalid tss"); |
20054ef0 | 792 | } |
eaa728ee | 793 | index = 8 * level + 4; |
20054ef0 | 794 | if ((index + 7) > env->tr.limit) { |
77b2bc2c | 795 | raise_exception_err(env, EXCP0A_TSS, env->tr.selector & 0xfffc); |
20054ef0 | 796 | } |
329e607d | 797 | return cpu_ldq_kernel(env, env->tr.base + index); |
eaa728ee FB |
798 | } |
799 | ||
800 | /* 64 bit interrupt */ | |
2999a0b2 BS |
801 | static void do_interrupt64(CPUX86State *env, int intno, int is_int, |
802 | int error_code, target_ulong next_eip, int is_hw) | |
eaa728ee FB |
803 | { |
804 | SegmentCache *dt; | |
805 | target_ulong ptr; | |
806 | int type, dpl, selector, cpl, ist; | |
807 | int has_error_code, new_stack; | |
808 | uint32_t e1, e2, e3, ss; | |
809 | target_ulong old_eip, esp, offset; | |
eaa728ee | 810 | |
eaa728ee | 811 | has_error_code = 0; |
20054ef0 BS |
812 | if (!is_int && !is_hw) { |
813 | has_error_code = exception_has_error_code(intno); | |
814 | } | |
815 | if (is_int) { | |
eaa728ee | 816 | old_eip = next_eip; |
20054ef0 | 817 | } else { |
eaa728ee | 818 | old_eip = env->eip; |
20054ef0 | 819 | } |
eaa728ee FB |
820 | |
821 | dt = &env->idt; | |
20054ef0 | 822 | if (intno * 16 + 15 > dt->limit) { |
77b2bc2c | 823 | raise_exception_err(env, EXCP0D_GPF, intno * 16 + 2); |
20054ef0 | 824 | } |
eaa728ee | 825 | ptr = dt->base + intno * 16; |
329e607d BS |
826 | e1 = cpu_ldl_kernel(env, ptr); |
827 | e2 = cpu_ldl_kernel(env, ptr + 4); | |
828 | e3 = cpu_ldl_kernel(env, ptr + 8); | |
eaa728ee FB |
829 | /* check gate type */ |
830 | type = (e2 >> DESC_TYPE_SHIFT) & 0x1f; | |
20054ef0 | 831 | switch (type) { |
eaa728ee FB |
832 | case 14: /* 386 interrupt gate */ |
833 | case 15: /* 386 trap gate */ | |
834 | break; | |
835 | default: | |
77b2bc2c | 836 | raise_exception_err(env, EXCP0D_GPF, intno * 16 + 2); |
eaa728ee FB |
837 | break; |
838 | } | |
839 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
840 | cpl = env->hflags & HF_CPL_MASK; | |
1235fc06 | 841 | /* check privilege if software int */ |
20054ef0 | 842 | if (is_int && dpl < cpl) { |
77b2bc2c | 843 | raise_exception_err(env, EXCP0D_GPF, intno * 16 + 2); |
20054ef0 | 844 | } |
eaa728ee | 845 | /* check valid bit */ |
20054ef0 | 846 | if (!(e2 & DESC_P_MASK)) { |
77b2bc2c | 847 | raise_exception_err(env, EXCP0B_NOSEG, intno * 16 + 2); |
20054ef0 | 848 | } |
eaa728ee FB |
849 | selector = e1 >> 16; |
850 | offset = ((target_ulong)e3 << 32) | (e2 & 0xffff0000) | (e1 & 0x0000ffff); | |
851 | ist = e2 & 7; | |
20054ef0 | 852 | if ((selector & 0xfffc) == 0) { |
77b2bc2c | 853 | raise_exception_err(env, EXCP0D_GPF, 0); |
20054ef0 | 854 | } |
eaa728ee | 855 | |
2999a0b2 | 856 | if (load_segment(env, &e1, &e2, selector) != 0) { |
77b2bc2c | 857 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 BS |
858 | } |
859 | if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) { | |
77b2bc2c | 860 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 861 | } |
eaa728ee | 862 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; |
20054ef0 | 863 | if (dpl > cpl) { |
77b2bc2c | 864 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 BS |
865 | } |
866 | if (!(e2 & DESC_P_MASK)) { | |
77b2bc2c | 867 | raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc); |
20054ef0 BS |
868 | } |
869 | if (!(e2 & DESC_L_MASK) || (e2 & DESC_B_MASK)) { | |
77b2bc2c | 870 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 871 | } |
eaa728ee FB |
872 | if ((!(e2 & DESC_C_MASK) && dpl < cpl) || ist != 0) { |
873 | /* to inner privilege */ | |
20054ef0 | 874 | if (ist != 0) { |
2999a0b2 | 875 | esp = get_rsp_from_tss(env, ist + 3); |
20054ef0 | 876 | } else { |
2999a0b2 | 877 | esp = get_rsp_from_tss(env, dpl); |
20054ef0 | 878 | } |
eaa728ee FB |
879 | esp &= ~0xfLL; /* align stack */ |
880 | ss = 0; | |
881 | new_stack = 1; | |
882 | } else if ((e2 & DESC_C_MASK) || dpl == cpl) { | |
883 | /* to same privilege */ | |
20054ef0 | 884 | if (env->eflags & VM_MASK) { |
77b2bc2c | 885 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 886 | } |
eaa728ee | 887 | new_stack = 0; |
20054ef0 | 888 | if (ist != 0) { |
2999a0b2 | 889 | esp = get_rsp_from_tss(env, ist + 3); |
20054ef0 | 890 | } else { |
eaa728ee | 891 | esp = ESP; |
20054ef0 | 892 | } |
eaa728ee FB |
893 | esp &= ~0xfLL; /* align stack */ |
894 | dpl = cpl; | |
895 | } else { | |
77b2bc2c | 896 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
eaa728ee FB |
897 | new_stack = 0; /* avoid warning */ |
898 | esp = 0; /* avoid warning */ | |
899 | } | |
900 | ||
901 | PUSHQ(esp, env->segs[R_SS].selector); | |
902 | PUSHQ(esp, ESP); | |
997ff0d9 | 903 | PUSHQ(esp, cpu_compute_eflags(env)); |
eaa728ee FB |
904 | PUSHQ(esp, env->segs[R_CS].selector); |
905 | PUSHQ(esp, old_eip); | |
906 | if (has_error_code) { | |
907 | PUSHQ(esp, error_code); | |
908 | } | |
909 | ||
910 | if (new_stack) { | |
911 | ss = 0 | dpl; | |
912 | cpu_x86_load_seg_cache(env, R_SS, ss, 0, 0, 0); | |
913 | } | |
914 | ESP = esp; | |
915 | ||
916 | selector = (selector & ~3) | dpl; | |
917 | cpu_x86_load_seg_cache(env, R_CS, selector, | |
918 | get_seg_base(e1, e2), | |
919 | get_seg_limit(e1, e2), | |
920 | e2); | |
921 | cpu_x86_set_cpl(env, dpl); | |
922 | env->eip = offset; | |
923 | ||
924 | /* interrupt gate clear IF mask */ | |
925 | if ((type & 1) == 0) { | |
926 | env->eflags &= ~IF_MASK; | |
927 | } | |
928 | env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK); | |
929 | } | |
930 | #endif | |
931 | ||
d9957a8b | 932 | #ifdef TARGET_X86_64 |
eaa728ee | 933 | #if defined(CONFIG_USER_ONLY) |
2999a0b2 | 934 | void helper_syscall(CPUX86State *env, int next_eip_addend) |
eaa728ee FB |
935 | { |
936 | env->exception_index = EXCP_SYSCALL; | |
937 | env->exception_next_eip = env->eip + next_eip_addend; | |
1162c041 | 938 | cpu_loop_exit(env); |
eaa728ee FB |
939 | } |
940 | #else | |
2999a0b2 | 941 | void helper_syscall(CPUX86State *env, int next_eip_addend) |
eaa728ee FB |
942 | { |
943 | int selector; | |
944 | ||
945 | if (!(env->efer & MSR_EFER_SCE)) { | |
77b2bc2c | 946 | raise_exception_err(env, EXCP06_ILLOP, 0); |
eaa728ee FB |
947 | } |
948 | selector = (env->star >> 32) & 0xffff; | |
eaa728ee FB |
949 | if (env->hflags & HF_LMA_MASK) { |
950 | int code64; | |
951 | ||
952 | ECX = env->eip + next_eip_addend; | |
997ff0d9 | 953 | env->regs[11] = cpu_compute_eflags(env); |
eaa728ee FB |
954 | |
955 | code64 = env->hflags & HF_CS64_MASK; | |
956 | ||
957 | cpu_x86_set_cpl(env, 0); | |
958 | cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc, | |
959 | 0, 0xffffffff, | |
960 | DESC_G_MASK | DESC_P_MASK | | |
961 | DESC_S_MASK | | |
20054ef0 BS |
962 | DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK | |
963 | DESC_L_MASK); | |
eaa728ee FB |
964 | cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc, |
965 | 0, 0xffffffff, | |
966 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
967 | DESC_S_MASK | | |
968 | DESC_W_MASK | DESC_A_MASK); | |
969 | env->eflags &= ~env->fmask; | |
997ff0d9 | 970 | cpu_load_eflags(env, env->eflags, 0); |
20054ef0 | 971 | if (code64) { |
eaa728ee | 972 | env->eip = env->lstar; |
20054ef0 | 973 | } else { |
eaa728ee | 974 | env->eip = env->cstar; |
20054ef0 | 975 | } |
d9957a8b | 976 | } else { |
eaa728ee FB |
977 | ECX = (uint32_t)(env->eip + next_eip_addend); |
978 | ||
979 | cpu_x86_set_cpl(env, 0); | |
980 | cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc, | |
981 | 0, 0xffffffff, | |
982 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
983 | DESC_S_MASK | | |
984 | DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK); | |
985 | cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc, | |
986 | 0, 0xffffffff, | |
987 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
988 | DESC_S_MASK | | |
989 | DESC_W_MASK | DESC_A_MASK); | |
990 | env->eflags &= ~(IF_MASK | RF_MASK | VM_MASK); | |
991 | env->eip = (uint32_t)env->star; | |
992 | } | |
993 | } | |
994 | #endif | |
d9957a8b | 995 | #endif |
eaa728ee | 996 | |
d9957a8b | 997 | #ifdef TARGET_X86_64 |
2999a0b2 | 998 | void helper_sysret(CPUX86State *env, int dflag) |
eaa728ee FB |
999 | { |
1000 | int cpl, selector; | |
1001 | ||
1002 | if (!(env->efer & MSR_EFER_SCE)) { | |
77b2bc2c | 1003 | raise_exception_err(env, EXCP06_ILLOP, 0); |
eaa728ee FB |
1004 | } |
1005 | cpl = env->hflags & HF_CPL_MASK; | |
1006 | if (!(env->cr[0] & CR0_PE_MASK) || cpl != 0) { | |
77b2bc2c | 1007 | raise_exception_err(env, EXCP0D_GPF, 0); |
eaa728ee FB |
1008 | } |
1009 | selector = (env->star >> 48) & 0xffff; | |
eaa728ee FB |
1010 | if (env->hflags & HF_LMA_MASK) { |
1011 | if (dflag == 2) { | |
1012 | cpu_x86_load_seg_cache(env, R_CS, (selector + 16) | 3, | |
1013 | 0, 0xffffffff, | |
1014 | DESC_G_MASK | DESC_P_MASK | | |
1015 | DESC_S_MASK | (3 << DESC_DPL_SHIFT) | | |
1016 | DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK | | |
1017 | DESC_L_MASK); | |
1018 | env->eip = ECX; | |
1019 | } else { | |
1020 | cpu_x86_load_seg_cache(env, R_CS, selector | 3, | |
1021 | 0, 0xffffffff, | |
1022 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
1023 | DESC_S_MASK | (3 << DESC_DPL_SHIFT) | | |
1024 | DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK); | |
1025 | env->eip = (uint32_t)ECX; | |
1026 | } | |
1027 | cpu_x86_load_seg_cache(env, R_SS, selector + 8, | |
1028 | 0, 0xffffffff, | |
1029 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
1030 | DESC_S_MASK | (3 << DESC_DPL_SHIFT) | | |
1031 | DESC_W_MASK | DESC_A_MASK); | |
997ff0d9 BS |
1032 | cpu_load_eflags(env, (uint32_t)(env->regs[11]), TF_MASK | AC_MASK |
1033 | | ID_MASK | IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | | |
1034 | NT_MASK); | |
eaa728ee | 1035 | cpu_x86_set_cpl(env, 3); |
d9957a8b | 1036 | } else { |
eaa728ee FB |
1037 | cpu_x86_load_seg_cache(env, R_CS, selector | 3, |
1038 | 0, 0xffffffff, | |
1039 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
1040 | DESC_S_MASK | (3 << DESC_DPL_SHIFT) | | |
1041 | DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK); | |
1042 | env->eip = (uint32_t)ECX; | |
1043 | cpu_x86_load_seg_cache(env, R_SS, selector + 8, | |
1044 | 0, 0xffffffff, | |
1045 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
1046 | DESC_S_MASK | (3 << DESC_DPL_SHIFT) | | |
1047 | DESC_W_MASK | DESC_A_MASK); | |
1048 | env->eflags |= IF_MASK; | |
1049 | cpu_x86_set_cpl(env, 3); | |
1050 | } | |
eaa728ee | 1051 | } |
d9957a8b | 1052 | #endif |
eaa728ee FB |
1053 | |
1054 | /* real mode interrupt */ | |
2999a0b2 BS |
1055 | static void do_interrupt_real(CPUX86State *env, int intno, int is_int, |
1056 | int error_code, unsigned int next_eip) | |
eaa728ee FB |
1057 | { |
1058 | SegmentCache *dt; | |
1059 | target_ulong ptr, ssp; | |
1060 | int selector; | |
1061 | uint32_t offset, esp; | |
1062 | uint32_t old_cs, old_eip; | |
eaa728ee | 1063 | |
20054ef0 | 1064 | /* real mode (simpler!) */ |
eaa728ee | 1065 | dt = &env->idt; |
20054ef0 | 1066 | if (intno * 4 + 3 > dt->limit) { |
77b2bc2c | 1067 | raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); |
20054ef0 | 1068 | } |
eaa728ee | 1069 | ptr = dt->base + intno * 4; |
329e607d BS |
1070 | offset = cpu_lduw_kernel(env, ptr); |
1071 | selector = cpu_lduw_kernel(env, ptr + 2); | |
eaa728ee FB |
1072 | esp = ESP; |
1073 | ssp = env->segs[R_SS].base; | |
20054ef0 | 1074 | if (is_int) { |
eaa728ee | 1075 | old_eip = next_eip; |
20054ef0 | 1076 | } else { |
eaa728ee | 1077 | old_eip = env->eip; |
20054ef0 | 1078 | } |
eaa728ee | 1079 | old_cs = env->segs[R_CS].selector; |
20054ef0 | 1080 | /* XXX: use SS segment size? */ |
997ff0d9 | 1081 | PUSHW(ssp, esp, 0xffff, cpu_compute_eflags(env)); |
eaa728ee FB |
1082 | PUSHW(ssp, esp, 0xffff, old_cs); |
1083 | PUSHW(ssp, esp, 0xffff, old_eip); | |
1084 | ||
1085 | /* update processor state */ | |
1086 | ESP = (ESP & ~0xffff) | (esp & 0xffff); | |
1087 | env->eip = offset; | |
1088 | env->segs[R_CS].selector = selector; | |
1089 | env->segs[R_CS].base = (selector << 4); | |
1090 | env->eflags &= ~(IF_MASK | TF_MASK | AC_MASK | RF_MASK); | |
1091 | } | |
1092 | ||
e694d4e2 | 1093 | #if defined(CONFIG_USER_ONLY) |
eaa728ee | 1094 | /* fake user mode interrupt */ |
2999a0b2 BS |
1095 | static void do_interrupt_user(CPUX86State *env, int intno, int is_int, |
1096 | int error_code, target_ulong next_eip) | |
eaa728ee FB |
1097 | { |
1098 | SegmentCache *dt; | |
1099 | target_ulong ptr; | |
1100 | int dpl, cpl, shift; | |
1101 | uint32_t e2; | |
1102 | ||
1103 | dt = &env->idt; | |
1104 | if (env->hflags & HF_LMA_MASK) { | |
1105 | shift = 4; | |
1106 | } else { | |
1107 | shift = 3; | |
1108 | } | |
1109 | ptr = dt->base + (intno << shift); | |
329e607d | 1110 | e2 = cpu_ldl_kernel(env, ptr + 4); |
eaa728ee FB |
1111 | |
1112 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
1113 | cpl = env->hflags & HF_CPL_MASK; | |
1235fc06 | 1114 | /* check privilege if software int */ |
20054ef0 | 1115 | if (is_int && dpl < cpl) { |
77b2bc2c | 1116 | raise_exception_err(env, EXCP0D_GPF, (intno << shift) + 2); |
20054ef0 | 1117 | } |
eaa728ee FB |
1118 | |
1119 | /* Since we emulate only user space, we cannot do more than | |
1120 | exiting the emulation with the suitable exception and error | |
1121 | code */ | |
20054ef0 | 1122 | if (is_int) { |
eaa728ee | 1123 | EIP = next_eip; |
20054ef0 | 1124 | } |
eaa728ee FB |
1125 | } |
1126 | ||
e694d4e2 BS |
1127 | #else |
1128 | ||
2999a0b2 BS |
1129 | static void handle_even_inj(CPUX86State *env, int intno, int is_int, |
1130 | int error_code, int is_hw, int rm) | |
2ed51f5b | 1131 | { |
20054ef0 BS |
1132 | uint32_t event_inj = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, |
1133 | control.event_inj)); | |
1134 | ||
2ed51f5b | 1135 | if (!(event_inj & SVM_EVTINJ_VALID)) { |
20054ef0 BS |
1136 | int type; |
1137 | ||
1138 | if (is_int) { | |
1139 | type = SVM_EVTINJ_TYPE_SOFT; | |
1140 | } else { | |
1141 | type = SVM_EVTINJ_TYPE_EXEPT; | |
1142 | } | |
1143 | event_inj = intno | type | SVM_EVTINJ_VALID; | |
1144 | if (!rm && exception_has_error_code(intno)) { | |
1145 | event_inj |= SVM_EVTINJ_VALID_ERR; | |
1146 | stl_phys(env->vm_vmcb + offsetof(struct vmcb, | |
1147 | control.event_inj_err), | |
1148 | error_code); | |
1149 | } | |
1150 | stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.event_inj), | |
1151 | event_inj); | |
2ed51f5b AL |
1152 | } |
1153 | } | |
00ea18d1 | 1154 | #endif |
2ed51f5b | 1155 | |
eaa728ee FB |
1156 | /* |
1157 | * Begin execution of an interruption. is_int is TRUE if coming from | |
1158 | * the int instruction. next_eip is the EIP value AFTER the interrupt | |
1159 | * instruction. It is only relevant if is_int is TRUE. | |
1160 | */ | |
2999a0b2 BS |
1161 | static void do_interrupt_all(CPUX86State *env, int intno, int is_int, |
1162 | int error_code, target_ulong next_eip, int is_hw) | |
eaa728ee | 1163 | { |
8fec2b8c | 1164 | if (qemu_loglevel_mask(CPU_LOG_INT)) { |
eaa728ee FB |
1165 | if ((env->cr[0] & CR0_PE_MASK)) { |
1166 | static int count; | |
20054ef0 BS |
1167 | |
1168 | qemu_log("%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx | |
1169 | " pc=" TARGET_FMT_lx " SP=%04x:" TARGET_FMT_lx, | |
1170 | count, intno, error_code, is_int, | |
1171 | env->hflags & HF_CPL_MASK, | |
1172 | env->segs[R_CS].selector, EIP, | |
1173 | (int)env->segs[R_CS].base + EIP, | |
1174 | env->segs[R_SS].selector, ESP); | |
eaa728ee | 1175 | if (intno == 0x0e) { |
93fcfe39 | 1176 | qemu_log(" CR2=" TARGET_FMT_lx, env->cr[2]); |
eaa728ee | 1177 | } else { |
93fcfe39 | 1178 | qemu_log(" EAX=" TARGET_FMT_lx, EAX); |
eaa728ee | 1179 | } |
93fcfe39 | 1180 | qemu_log("\n"); |
6fd2a026 | 1181 | log_cpu_state(env, CPU_DUMP_CCOP); |
eaa728ee FB |
1182 | #if 0 |
1183 | { | |
1184 | int i; | |
9bd5494e | 1185 | target_ulong ptr; |
20054ef0 | 1186 | |
93fcfe39 | 1187 | qemu_log(" code="); |
eaa728ee | 1188 | ptr = env->segs[R_CS].base + env->eip; |
20054ef0 | 1189 | for (i = 0; i < 16; i++) { |
93fcfe39 | 1190 | qemu_log(" %02x", ldub(ptr + i)); |
eaa728ee | 1191 | } |
93fcfe39 | 1192 | qemu_log("\n"); |
eaa728ee FB |
1193 | } |
1194 | #endif | |
1195 | count++; | |
1196 | } | |
1197 | } | |
1198 | if (env->cr[0] & CR0_PE_MASK) { | |
00ea18d1 | 1199 | #if !defined(CONFIG_USER_ONLY) |
20054ef0 | 1200 | if (env->hflags & HF_SVMI_MASK) { |
2999a0b2 | 1201 | handle_even_inj(env, intno, is_int, error_code, is_hw, 0); |
20054ef0 | 1202 | } |
00ea18d1 | 1203 | #endif |
eb38c52c | 1204 | #ifdef TARGET_X86_64 |
eaa728ee | 1205 | if (env->hflags & HF_LMA_MASK) { |
2999a0b2 | 1206 | do_interrupt64(env, intno, is_int, error_code, next_eip, is_hw); |
eaa728ee FB |
1207 | } else |
1208 | #endif | |
1209 | { | |
2999a0b2 BS |
1210 | do_interrupt_protected(env, intno, is_int, error_code, next_eip, |
1211 | is_hw); | |
eaa728ee FB |
1212 | } |
1213 | } else { | |
00ea18d1 | 1214 | #if !defined(CONFIG_USER_ONLY) |
20054ef0 | 1215 | if (env->hflags & HF_SVMI_MASK) { |
2999a0b2 | 1216 | handle_even_inj(env, intno, is_int, error_code, is_hw, 1); |
20054ef0 | 1217 | } |
00ea18d1 | 1218 | #endif |
2999a0b2 | 1219 | do_interrupt_real(env, intno, is_int, error_code, next_eip); |
eaa728ee | 1220 | } |
2ed51f5b | 1221 | |
00ea18d1 | 1222 | #if !defined(CONFIG_USER_ONLY) |
2ed51f5b | 1223 | if (env->hflags & HF_SVMI_MASK) { |
20054ef0 BS |
1224 | uint32_t event_inj = ldl_phys(env->vm_vmcb + |
1225 | offsetof(struct vmcb, | |
1226 | control.event_inj)); | |
1227 | ||
1228 | stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.event_inj), | |
1229 | event_inj & ~SVM_EVTINJ_VALID); | |
2ed51f5b | 1230 | } |
00ea18d1 | 1231 | #endif |
eaa728ee FB |
1232 | } |
1233 | ||
2999a0b2 | 1234 | void do_interrupt(CPUX86State *env) |
e694d4e2 | 1235 | { |
e694d4e2 BS |
1236 | #if defined(CONFIG_USER_ONLY) |
1237 | /* if user mode only, we simulate a fake exception | |
1238 | which will be handled outside the cpu execution | |
1239 | loop */ | |
2999a0b2 | 1240 | do_interrupt_user(env, env->exception_index, |
e694d4e2 BS |
1241 | env->exception_is_int, |
1242 | env->error_code, | |
1243 | env->exception_next_eip); | |
1244 | /* successfully delivered */ | |
1245 | env->old_exception = -1; | |
1246 | #else | |
1247 | /* simulate a real cpu exception. On i386, it can | |
1248 | trigger new exceptions, but we do not handle | |
1249 | double or triple faults yet. */ | |
2999a0b2 | 1250 | do_interrupt_all(env, env->exception_index, |
e694d4e2 BS |
1251 | env->exception_is_int, |
1252 | env->error_code, | |
1253 | env->exception_next_eip, 0); | |
1254 | /* successfully delivered */ | |
1255 | env->old_exception = -1; | |
1256 | #endif | |
e694d4e2 BS |
1257 | } |
1258 | ||
2999a0b2 | 1259 | void do_interrupt_x86_hardirq(CPUX86State *env, int intno, int is_hw) |
e694d4e2 | 1260 | { |
2999a0b2 | 1261 | do_interrupt_all(env, intno, 0, 0, 0, is_hw); |
e694d4e2 BS |
1262 | } |
1263 | ||
2999a0b2 BS |
1264 | void helper_enter_level(CPUX86State *env, int level, int data32, |
1265 | target_ulong t1) | |
eaa728ee FB |
1266 | { |
1267 | target_ulong ssp; | |
1268 | uint32_t esp_mask, esp, ebp; | |
1269 | ||
1270 | esp_mask = get_sp_mask(env->segs[R_SS].flags); | |
1271 | ssp = env->segs[R_SS].base; | |
1272 | ebp = EBP; | |
1273 | esp = ESP; | |
1274 | if (data32) { | |
1275 | /* 32 bit */ | |
1276 | esp -= 4; | |
1277 | while (--level) { | |
1278 | esp -= 4; | |
1279 | ebp -= 4; | |
329e607d BS |
1280 | cpu_stl_data(env, ssp + (esp & esp_mask), |
1281 | cpu_ldl_data(env, ssp + (ebp & esp_mask))); | |
eaa728ee FB |
1282 | } |
1283 | esp -= 4; | |
329e607d | 1284 | cpu_stl_data(env, ssp + (esp & esp_mask), t1); |
eaa728ee FB |
1285 | } else { |
1286 | /* 16 bit */ | |
1287 | esp -= 2; | |
1288 | while (--level) { | |
1289 | esp -= 2; | |
1290 | ebp -= 2; | |
329e607d BS |
1291 | cpu_stw_data(env, ssp + (esp & esp_mask), |
1292 | cpu_lduw_data(env, ssp + (ebp & esp_mask))); | |
eaa728ee FB |
1293 | } |
1294 | esp -= 2; | |
329e607d | 1295 | cpu_stw_data(env, ssp + (esp & esp_mask), t1); |
eaa728ee FB |
1296 | } |
1297 | } | |
1298 | ||
1299 | #ifdef TARGET_X86_64 | |
2999a0b2 BS |
1300 | void helper_enter64_level(CPUX86State *env, int level, int data64, |
1301 | target_ulong t1) | |
eaa728ee FB |
1302 | { |
1303 | target_ulong esp, ebp; | |
20054ef0 | 1304 | |
eaa728ee FB |
1305 | ebp = EBP; |
1306 | esp = ESP; | |
1307 | ||
1308 | if (data64) { | |
1309 | /* 64 bit */ | |
1310 | esp -= 8; | |
1311 | while (--level) { | |
1312 | esp -= 8; | |
1313 | ebp -= 8; | |
329e607d | 1314 | cpu_stq_data(env, esp, cpu_ldq_data(env, ebp)); |
eaa728ee FB |
1315 | } |
1316 | esp -= 8; | |
329e607d | 1317 | cpu_stq_data(env, esp, t1); |
eaa728ee FB |
1318 | } else { |
1319 | /* 16 bit */ | |
1320 | esp -= 2; | |
1321 | while (--level) { | |
1322 | esp -= 2; | |
1323 | ebp -= 2; | |
329e607d | 1324 | cpu_stw_data(env, esp, cpu_lduw_data(env, ebp)); |
eaa728ee FB |
1325 | } |
1326 | esp -= 2; | |
329e607d | 1327 | cpu_stw_data(env, esp, t1); |
eaa728ee FB |
1328 | } |
1329 | } | |
1330 | #endif | |
1331 | ||
2999a0b2 | 1332 | void helper_lldt(CPUX86State *env, int selector) |
eaa728ee FB |
1333 | { |
1334 | SegmentCache *dt; | |
1335 | uint32_t e1, e2; | |
1336 | int index, entry_limit; | |
1337 | target_ulong ptr; | |
1338 | ||
1339 | selector &= 0xffff; | |
1340 | if ((selector & 0xfffc) == 0) { | |
1341 | /* XXX: NULL selector case: invalid LDT */ | |
1342 | env->ldt.base = 0; | |
1343 | env->ldt.limit = 0; | |
1344 | } else { | |
20054ef0 | 1345 | if (selector & 0x4) { |
77b2bc2c | 1346 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1347 | } |
eaa728ee FB |
1348 | dt = &env->gdt; |
1349 | index = selector & ~7; | |
1350 | #ifdef TARGET_X86_64 | |
20054ef0 | 1351 | if (env->hflags & HF_LMA_MASK) { |
eaa728ee | 1352 | entry_limit = 15; |
20054ef0 | 1353 | } else |
eaa728ee | 1354 | #endif |
20054ef0 | 1355 | { |
eaa728ee | 1356 | entry_limit = 7; |
20054ef0 BS |
1357 | } |
1358 | if ((index + entry_limit) > dt->limit) { | |
77b2bc2c | 1359 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1360 | } |
eaa728ee | 1361 | ptr = dt->base + index; |
329e607d BS |
1362 | e1 = cpu_ldl_kernel(env, ptr); |
1363 | e2 = cpu_ldl_kernel(env, ptr + 4); | |
20054ef0 | 1364 | if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2) { |
77b2bc2c | 1365 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 BS |
1366 | } |
1367 | if (!(e2 & DESC_P_MASK)) { | |
77b2bc2c | 1368 | raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc); |
20054ef0 | 1369 | } |
eaa728ee FB |
1370 | #ifdef TARGET_X86_64 |
1371 | if (env->hflags & HF_LMA_MASK) { | |
1372 | uint32_t e3; | |
20054ef0 | 1373 | |
329e607d | 1374 | e3 = cpu_ldl_kernel(env, ptr + 8); |
eaa728ee FB |
1375 | load_seg_cache_raw_dt(&env->ldt, e1, e2); |
1376 | env->ldt.base |= (target_ulong)e3 << 32; | |
1377 | } else | |
1378 | #endif | |
1379 | { | |
1380 | load_seg_cache_raw_dt(&env->ldt, e1, e2); | |
1381 | } | |
1382 | } | |
1383 | env->ldt.selector = selector; | |
1384 | } | |
1385 | ||
2999a0b2 | 1386 | void helper_ltr(CPUX86State *env, int selector) |
eaa728ee FB |
1387 | { |
1388 | SegmentCache *dt; | |
1389 | uint32_t e1, e2; | |
1390 | int index, type, entry_limit; | |
1391 | target_ulong ptr; | |
1392 | ||
1393 | selector &= 0xffff; | |
1394 | if ((selector & 0xfffc) == 0) { | |
1395 | /* NULL selector case: invalid TR */ | |
1396 | env->tr.base = 0; | |
1397 | env->tr.limit = 0; | |
1398 | env->tr.flags = 0; | |
1399 | } else { | |
20054ef0 | 1400 | if (selector & 0x4) { |
77b2bc2c | 1401 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1402 | } |
eaa728ee FB |
1403 | dt = &env->gdt; |
1404 | index = selector & ~7; | |
1405 | #ifdef TARGET_X86_64 | |
20054ef0 | 1406 | if (env->hflags & HF_LMA_MASK) { |
eaa728ee | 1407 | entry_limit = 15; |
20054ef0 | 1408 | } else |
eaa728ee | 1409 | #endif |
20054ef0 | 1410 | { |
eaa728ee | 1411 | entry_limit = 7; |
20054ef0 BS |
1412 | } |
1413 | if ((index + entry_limit) > dt->limit) { | |
77b2bc2c | 1414 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1415 | } |
eaa728ee | 1416 | ptr = dt->base + index; |
329e607d BS |
1417 | e1 = cpu_ldl_kernel(env, ptr); |
1418 | e2 = cpu_ldl_kernel(env, ptr + 4); | |
eaa728ee FB |
1419 | type = (e2 >> DESC_TYPE_SHIFT) & 0xf; |
1420 | if ((e2 & DESC_S_MASK) || | |
20054ef0 | 1421 | (type != 1 && type != 9)) { |
77b2bc2c | 1422 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 BS |
1423 | } |
1424 | if (!(e2 & DESC_P_MASK)) { | |
77b2bc2c | 1425 | raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc); |
20054ef0 | 1426 | } |
eaa728ee FB |
1427 | #ifdef TARGET_X86_64 |
1428 | if (env->hflags & HF_LMA_MASK) { | |
1429 | uint32_t e3, e4; | |
20054ef0 | 1430 | |
329e607d BS |
1431 | e3 = cpu_ldl_kernel(env, ptr + 8); |
1432 | e4 = cpu_ldl_kernel(env, ptr + 12); | |
20054ef0 | 1433 | if ((e4 >> DESC_TYPE_SHIFT) & 0xf) { |
77b2bc2c | 1434 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1435 | } |
eaa728ee FB |
1436 | load_seg_cache_raw_dt(&env->tr, e1, e2); |
1437 | env->tr.base |= (target_ulong)e3 << 32; | |
1438 | } else | |
1439 | #endif | |
1440 | { | |
1441 | load_seg_cache_raw_dt(&env->tr, e1, e2); | |
1442 | } | |
1443 | e2 |= DESC_TSS_BUSY_MASK; | |
329e607d | 1444 | cpu_stl_kernel(env, ptr + 4, e2); |
eaa728ee FB |
1445 | } |
1446 | env->tr.selector = selector; | |
1447 | } | |
1448 | ||
1449 | /* only works if protected mode and not VM86. seg_reg must be != R_CS */ | |
2999a0b2 | 1450 | void helper_load_seg(CPUX86State *env, int seg_reg, int selector) |
eaa728ee FB |
1451 | { |
1452 | uint32_t e1, e2; | |
1453 | int cpl, dpl, rpl; | |
1454 | SegmentCache *dt; | |
1455 | int index; | |
1456 | target_ulong ptr; | |
1457 | ||
1458 | selector &= 0xffff; | |
1459 | cpl = env->hflags & HF_CPL_MASK; | |
1460 | if ((selector & 0xfffc) == 0) { | |
1461 | /* null selector case */ | |
1462 | if (seg_reg == R_SS | |
1463 | #ifdef TARGET_X86_64 | |
1464 | && (!(env->hflags & HF_CS64_MASK) || cpl == 3) | |
1465 | #endif | |
20054ef0 | 1466 | ) { |
77b2bc2c | 1467 | raise_exception_err(env, EXCP0D_GPF, 0); |
20054ef0 | 1468 | } |
eaa728ee FB |
1469 | cpu_x86_load_seg_cache(env, seg_reg, selector, 0, 0, 0); |
1470 | } else { | |
1471 | ||
20054ef0 | 1472 | if (selector & 0x4) { |
eaa728ee | 1473 | dt = &env->ldt; |
20054ef0 | 1474 | } else { |
eaa728ee | 1475 | dt = &env->gdt; |
20054ef0 | 1476 | } |
eaa728ee | 1477 | index = selector & ~7; |
20054ef0 | 1478 | if ((index + 7) > dt->limit) { |
77b2bc2c | 1479 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1480 | } |
eaa728ee | 1481 | ptr = dt->base + index; |
329e607d BS |
1482 | e1 = cpu_ldl_kernel(env, ptr); |
1483 | e2 = cpu_ldl_kernel(env, ptr + 4); | |
eaa728ee | 1484 | |
20054ef0 | 1485 | if (!(e2 & DESC_S_MASK)) { |
77b2bc2c | 1486 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1487 | } |
eaa728ee FB |
1488 | rpl = selector & 3; |
1489 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
1490 | if (seg_reg == R_SS) { | |
1491 | /* must be writable segment */ | |
20054ef0 | 1492 | if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK)) { |
77b2bc2c | 1493 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 BS |
1494 | } |
1495 | if (rpl != cpl || dpl != cpl) { | |
77b2bc2c | 1496 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1497 | } |
eaa728ee FB |
1498 | } else { |
1499 | /* must be readable segment */ | |
20054ef0 | 1500 | if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK) { |
77b2bc2c | 1501 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1502 | } |
eaa728ee FB |
1503 | |
1504 | if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) { | |
1505 | /* if not conforming code, test rights */ | |
20054ef0 | 1506 | if (dpl < cpl || dpl < rpl) { |
77b2bc2c | 1507 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1508 | } |
eaa728ee FB |
1509 | } |
1510 | } | |
1511 | ||
1512 | if (!(e2 & DESC_P_MASK)) { | |
20054ef0 | 1513 | if (seg_reg == R_SS) { |
77b2bc2c | 1514 | raise_exception_err(env, EXCP0C_STACK, selector & 0xfffc); |
20054ef0 | 1515 | } else { |
77b2bc2c | 1516 | raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc); |
20054ef0 | 1517 | } |
eaa728ee FB |
1518 | } |
1519 | ||
1520 | /* set the access bit if not already set */ | |
1521 | if (!(e2 & DESC_A_MASK)) { | |
1522 | e2 |= DESC_A_MASK; | |
329e607d | 1523 | cpu_stl_kernel(env, ptr + 4, e2); |
eaa728ee FB |
1524 | } |
1525 | ||
1526 | cpu_x86_load_seg_cache(env, seg_reg, selector, | |
1527 | get_seg_base(e1, e2), | |
1528 | get_seg_limit(e1, e2), | |
1529 | e2); | |
1530 | #if 0 | |
93fcfe39 | 1531 | qemu_log("load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n", |
eaa728ee FB |
1532 | selector, (unsigned long)sc->base, sc->limit, sc->flags); |
1533 | #endif | |
1534 | } | |
1535 | } | |
1536 | ||
1537 | /* protected mode jump */ | |
2999a0b2 | 1538 | void helper_ljmp_protected(CPUX86State *env, int new_cs, target_ulong new_eip, |
eaa728ee FB |
1539 | int next_eip_addend) |
1540 | { | |
1541 | int gate_cs, type; | |
1542 | uint32_t e1, e2, cpl, dpl, rpl, limit; | |
1543 | target_ulong next_eip; | |
1544 | ||
20054ef0 | 1545 | if ((new_cs & 0xfffc) == 0) { |
77b2bc2c | 1546 | raise_exception_err(env, EXCP0D_GPF, 0); |
20054ef0 | 1547 | } |
2999a0b2 | 1548 | if (load_segment(env, &e1, &e2, new_cs) != 0) { |
77b2bc2c | 1549 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1550 | } |
eaa728ee FB |
1551 | cpl = env->hflags & HF_CPL_MASK; |
1552 | if (e2 & DESC_S_MASK) { | |
20054ef0 | 1553 | if (!(e2 & DESC_CS_MASK)) { |
77b2bc2c | 1554 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1555 | } |
eaa728ee FB |
1556 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; |
1557 | if (e2 & DESC_C_MASK) { | |
1558 | /* conforming code segment */ | |
20054ef0 | 1559 | if (dpl > cpl) { |
77b2bc2c | 1560 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1561 | } |
eaa728ee FB |
1562 | } else { |
1563 | /* non conforming code segment */ | |
1564 | rpl = new_cs & 3; | |
20054ef0 | 1565 | if (rpl > cpl) { |
77b2bc2c | 1566 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 BS |
1567 | } |
1568 | if (dpl != cpl) { | |
77b2bc2c | 1569 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1570 | } |
eaa728ee | 1571 | } |
20054ef0 | 1572 | if (!(e2 & DESC_P_MASK)) { |
77b2bc2c | 1573 | raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc); |
20054ef0 | 1574 | } |
eaa728ee FB |
1575 | limit = get_seg_limit(e1, e2); |
1576 | if (new_eip > limit && | |
20054ef0 | 1577 | !(env->hflags & HF_LMA_MASK) && !(e2 & DESC_L_MASK)) { |
77b2bc2c | 1578 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1579 | } |
eaa728ee FB |
1580 | cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl, |
1581 | get_seg_base(e1, e2), limit, e2); | |
1582 | EIP = new_eip; | |
1583 | } else { | |
1584 | /* jump to call or task gate */ | |
1585 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
1586 | rpl = new_cs & 3; | |
1587 | cpl = env->hflags & HF_CPL_MASK; | |
1588 | type = (e2 >> DESC_TYPE_SHIFT) & 0xf; | |
20054ef0 | 1589 | switch (type) { |
eaa728ee FB |
1590 | case 1: /* 286 TSS */ |
1591 | case 9: /* 386 TSS */ | |
1592 | case 5: /* task gate */ | |
20054ef0 | 1593 | if (dpl < cpl || dpl < rpl) { |
77b2bc2c | 1594 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1595 | } |
eaa728ee | 1596 | next_eip = env->eip + next_eip_addend; |
2999a0b2 | 1597 | switch_tss(env, new_cs, e1, e2, SWITCH_TSS_JMP, next_eip); |
eaa728ee FB |
1598 | CC_OP = CC_OP_EFLAGS; |
1599 | break; | |
1600 | case 4: /* 286 call gate */ | |
1601 | case 12: /* 386 call gate */ | |
20054ef0 | 1602 | if ((dpl < cpl) || (dpl < rpl)) { |
77b2bc2c | 1603 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 BS |
1604 | } |
1605 | if (!(e2 & DESC_P_MASK)) { | |
77b2bc2c | 1606 | raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc); |
20054ef0 | 1607 | } |
eaa728ee FB |
1608 | gate_cs = e1 >> 16; |
1609 | new_eip = (e1 & 0xffff); | |
20054ef0 | 1610 | if (type == 12) { |
eaa728ee | 1611 | new_eip |= (e2 & 0xffff0000); |
20054ef0 | 1612 | } |
2999a0b2 | 1613 | if (load_segment(env, &e1, &e2, gate_cs) != 0) { |
77b2bc2c | 1614 | raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc); |
20054ef0 | 1615 | } |
eaa728ee FB |
1616 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; |
1617 | /* must be code segment */ | |
1618 | if (((e2 & (DESC_S_MASK | DESC_CS_MASK)) != | |
20054ef0 | 1619 | (DESC_S_MASK | DESC_CS_MASK))) { |
77b2bc2c | 1620 | raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc); |
20054ef0 | 1621 | } |
eaa728ee | 1622 | if (((e2 & DESC_C_MASK) && (dpl > cpl)) || |
20054ef0 | 1623 | (!(e2 & DESC_C_MASK) && (dpl != cpl))) { |
77b2bc2c | 1624 | raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc); |
20054ef0 BS |
1625 | } |
1626 | if (!(e2 & DESC_P_MASK)) { | |
77b2bc2c | 1627 | raise_exception_err(env, EXCP0D_GPF, gate_cs & 0xfffc); |
20054ef0 | 1628 | } |
eaa728ee | 1629 | limit = get_seg_limit(e1, e2); |
20054ef0 | 1630 | if (new_eip > limit) { |
77b2bc2c | 1631 | raise_exception_err(env, EXCP0D_GPF, 0); |
20054ef0 | 1632 | } |
eaa728ee FB |
1633 | cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl, |
1634 | get_seg_base(e1, e2), limit, e2); | |
1635 | EIP = new_eip; | |
1636 | break; | |
1637 | default: | |
77b2bc2c | 1638 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
eaa728ee FB |
1639 | break; |
1640 | } | |
1641 | } | |
1642 | } | |
1643 | ||
1644 | /* real mode call */ | |
2999a0b2 | 1645 | void helper_lcall_real(CPUX86State *env, int new_cs, target_ulong new_eip1, |
eaa728ee FB |
1646 | int shift, int next_eip) |
1647 | { | |
1648 | int new_eip; | |
1649 | uint32_t esp, esp_mask; | |
1650 | target_ulong ssp; | |
1651 | ||
1652 | new_eip = new_eip1; | |
1653 | esp = ESP; | |
1654 | esp_mask = get_sp_mask(env->segs[R_SS].flags); | |
1655 | ssp = env->segs[R_SS].base; | |
1656 | if (shift) { | |
1657 | PUSHL(ssp, esp, esp_mask, env->segs[R_CS].selector); | |
1658 | PUSHL(ssp, esp, esp_mask, next_eip); | |
1659 | } else { | |
1660 | PUSHW(ssp, esp, esp_mask, env->segs[R_CS].selector); | |
1661 | PUSHW(ssp, esp, esp_mask, next_eip); | |
1662 | } | |
1663 | ||
1664 | SET_ESP(esp, esp_mask); | |
1665 | env->eip = new_eip; | |
1666 | env->segs[R_CS].selector = new_cs; | |
1667 | env->segs[R_CS].base = (new_cs << 4); | |
1668 | } | |
1669 | ||
1670 | /* protected mode call */ | |
2999a0b2 | 1671 | void helper_lcall_protected(CPUX86State *env, int new_cs, target_ulong new_eip, |
eaa728ee FB |
1672 | int shift, int next_eip_addend) |
1673 | { | |
1674 | int new_stack, i; | |
1675 | uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count; | |
1c918eba | 1676 | uint32_t ss = 0, ss_e1 = 0, ss_e2 = 0, sp, type, ss_dpl, sp_mask; |
eaa728ee FB |
1677 | uint32_t val, limit, old_sp_mask; |
1678 | target_ulong ssp, old_ssp, next_eip; | |
1679 | ||
1680 | next_eip = env->eip + next_eip_addend; | |
d12d51d5 AL |
1681 | LOG_PCALL("lcall %04x:%08x s=%d\n", new_cs, (uint32_t)new_eip, shift); |
1682 | LOG_PCALL_STATE(env); | |
20054ef0 | 1683 | if ((new_cs & 0xfffc) == 0) { |
77b2bc2c | 1684 | raise_exception_err(env, EXCP0D_GPF, 0); |
20054ef0 | 1685 | } |
2999a0b2 | 1686 | if (load_segment(env, &e1, &e2, new_cs) != 0) { |
77b2bc2c | 1687 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1688 | } |
eaa728ee | 1689 | cpl = env->hflags & HF_CPL_MASK; |
d12d51d5 | 1690 | LOG_PCALL("desc=%08x:%08x\n", e1, e2); |
eaa728ee | 1691 | if (e2 & DESC_S_MASK) { |
20054ef0 | 1692 | if (!(e2 & DESC_CS_MASK)) { |
77b2bc2c | 1693 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1694 | } |
eaa728ee FB |
1695 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; |
1696 | if (e2 & DESC_C_MASK) { | |
1697 | /* conforming code segment */ | |
20054ef0 | 1698 | if (dpl > cpl) { |
77b2bc2c | 1699 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1700 | } |
eaa728ee FB |
1701 | } else { |
1702 | /* non conforming code segment */ | |
1703 | rpl = new_cs & 3; | |
20054ef0 | 1704 | if (rpl > cpl) { |
77b2bc2c | 1705 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 BS |
1706 | } |
1707 | if (dpl != cpl) { | |
77b2bc2c | 1708 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1709 | } |
eaa728ee | 1710 | } |
20054ef0 | 1711 | if (!(e2 & DESC_P_MASK)) { |
77b2bc2c | 1712 | raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc); |
20054ef0 | 1713 | } |
eaa728ee FB |
1714 | |
1715 | #ifdef TARGET_X86_64 | |
1716 | /* XXX: check 16/32 bit cases in long mode */ | |
1717 | if (shift == 2) { | |
1718 | target_ulong rsp; | |
20054ef0 | 1719 | |
eaa728ee FB |
1720 | /* 64 bit case */ |
1721 | rsp = ESP; | |
1722 | PUSHQ(rsp, env->segs[R_CS].selector); | |
1723 | PUSHQ(rsp, next_eip); | |
1724 | /* from this point, not restartable */ | |
1725 | ESP = rsp; | |
1726 | cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl, | |
1727 | get_seg_base(e1, e2), | |
1728 | get_seg_limit(e1, e2), e2); | |
1729 | EIP = new_eip; | |
1730 | } else | |
1731 | #endif | |
1732 | { | |
1733 | sp = ESP; | |
1734 | sp_mask = get_sp_mask(env->segs[R_SS].flags); | |
1735 | ssp = env->segs[R_SS].base; | |
1736 | if (shift) { | |
1737 | PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector); | |
1738 | PUSHL(ssp, sp, sp_mask, next_eip); | |
1739 | } else { | |
1740 | PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector); | |
1741 | PUSHW(ssp, sp, sp_mask, next_eip); | |
1742 | } | |
1743 | ||
1744 | limit = get_seg_limit(e1, e2); | |
20054ef0 | 1745 | if (new_eip > limit) { |
77b2bc2c | 1746 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1747 | } |
eaa728ee FB |
1748 | /* from this point, not restartable */ |
1749 | SET_ESP(sp, sp_mask); | |
1750 | cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl, | |
1751 | get_seg_base(e1, e2), limit, e2); | |
1752 | EIP = new_eip; | |
1753 | } | |
1754 | } else { | |
1755 | /* check gate type */ | |
1756 | type = (e2 >> DESC_TYPE_SHIFT) & 0x1f; | |
1757 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
1758 | rpl = new_cs & 3; | |
20054ef0 | 1759 | switch (type) { |
eaa728ee FB |
1760 | case 1: /* available 286 TSS */ |
1761 | case 9: /* available 386 TSS */ | |
1762 | case 5: /* task gate */ | |
20054ef0 | 1763 | if (dpl < cpl || dpl < rpl) { |
77b2bc2c | 1764 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1765 | } |
2999a0b2 | 1766 | switch_tss(env, new_cs, e1, e2, SWITCH_TSS_CALL, next_eip); |
eaa728ee FB |
1767 | CC_OP = CC_OP_EFLAGS; |
1768 | return; | |
1769 | case 4: /* 286 call gate */ | |
1770 | case 12: /* 386 call gate */ | |
1771 | break; | |
1772 | default: | |
77b2bc2c | 1773 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
eaa728ee FB |
1774 | break; |
1775 | } | |
1776 | shift = type >> 3; | |
1777 | ||
20054ef0 | 1778 | if (dpl < cpl || dpl < rpl) { |
77b2bc2c | 1779 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 1780 | } |
eaa728ee | 1781 | /* check valid bit */ |
20054ef0 | 1782 | if (!(e2 & DESC_P_MASK)) { |
77b2bc2c | 1783 | raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc); |
20054ef0 | 1784 | } |
eaa728ee FB |
1785 | selector = e1 >> 16; |
1786 | offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff); | |
1787 | param_count = e2 & 0x1f; | |
20054ef0 | 1788 | if ((selector & 0xfffc) == 0) { |
77b2bc2c | 1789 | raise_exception_err(env, EXCP0D_GPF, 0); |
20054ef0 | 1790 | } |
eaa728ee | 1791 | |
2999a0b2 | 1792 | if (load_segment(env, &e1, &e2, selector) != 0) { |
77b2bc2c | 1793 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 BS |
1794 | } |
1795 | if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) { | |
77b2bc2c | 1796 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 | 1797 | } |
eaa728ee | 1798 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; |
20054ef0 | 1799 | if (dpl > cpl) { |
77b2bc2c | 1800 | raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); |
20054ef0 BS |
1801 | } |
1802 | if (!(e2 & DESC_P_MASK)) { | |
77b2bc2c | 1803 | raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc); |
20054ef0 | 1804 | } |
eaa728ee FB |
1805 | |
1806 | if (!(e2 & DESC_C_MASK) && dpl < cpl) { | |
1807 | /* to inner privilege */ | |
2999a0b2 | 1808 | get_ss_esp_from_tss(env, &ss, &sp, dpl); |
20054ef0 BS |
1809 | LOG_PCALL("new ss:esp=%04x:%08x param_count=%d ESP=" TARGET_FMT_lx |
1810 | "\n", | |
1811 | ss, sp, param_count, ESP); | |
1812 | if ((ss & 0xfffc) == 0) { | |
77b2bc2c | 1813 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 BS |
1814 | } |
1815 | if ((ss & 3) != dpl) { | |
77b2bc2c | 1816 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 | 1817 | } |
2999a0b2 | 1818 | if (load_segment(env, &ss_e1, &ss_e2, ss) != 0) { |
77b2bc2c | 1819 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 | 1820 | } |
eaa728ee | 1821 | ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3; |
20054ef0 | 1822 | if (ss_dpl != dpl) { |
77b2bc2c | 1823 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 | 1824 | } |
eaa728ee FB |
1825 | if (!(ss_e2 & DESC_S_MASK) || |
1826 | (ss_e2 & DESC_CS_MASK) || | |
20054ef0 | 1827 | !(ss_e2 & DESC_W_MASK)) { |
77b2bc2c | 1828 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 BS |
1829 | } |
1830 | if (!(ss_e2 & DESC_P_MASK)) { | |
77b2bc2c | 1831 | raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); |
20054ef0 | 1832 | } |
eaa728ee | 1833 | |
20054ef0 | 1834 | /* push_size = ((param_count * 2) + 8) << shift; */ |
eaa728ee FB |
1835 | |
1836 | old_sp_mask = get_sp_mask(env->segs[R_SS].flags); | |
1837 | old_ssp = env->segs[R_SS].base; | |
1838 | ||
1839 | sp_mask = get_sp_mask(ss_e2); | |
1840 | ssp = get_seg_base(ss_e1, ss_e2); | |
1841 | if (shift) { | |
1842 | PUSHL(ssp, sp, sp_mask, env->segs[R_SS].selector); | |
1843 | PUSHL(ssp, sp, sp_mask, ESP); | |
20054ef0 | 1844 | for (i = param_count - 1; i >= 0; i--) { |
329e607d BS |
1845 | val = cpu_ldl_kernel(env, old_ssp + ((ESP + i * 4) & |
1846 | old_sp_mask)); | |
eaa728ee FB |
1847 | PUSHL(ssp, sp, sp_mask, val); |
1848 | } | |
1849 | } else { | |
1850 | PUSHW(ssp, sp, sp_mask, env->segs[R_SS].selector); | |
1851 | PUSHW(ssp, sp, sp_mask, ESP); | |
20054ef0 | 1852 | for (i = param_count - 1; i >= 0; i--) { |
329e607d BS |
1853 | val = cpu_lduw_kernel(env, old_ssp + ((ESP + i * 2) & |
1854 | old_sp_mask)); | |
eaa728ee FB |
1855 | PUSHW(ssp, sp, sp_mask, val); |
1856 | } | |
1857 | } | |
1858 | new_stack = 1; | |
1859 | } else { | |
1860 | /* to same privilege */ | |
1861 | sp = ESP; | |
1862 | sp_mask = get_sp_mask(env->segs[R_SS].flags); | |
1863 | ssp = env->segs[R_SS].base; | |
20054ef0 | 1864 | /* push_size = (4 << shift); */ |
eaa728ee FB |
1865 | new_stack = 0; |
1866 | } | |
1867 | ||
1868 | if (shift) { | |
1869 | PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector); | |
1870 | PUSHL(ssp, sp, sp_mask, next_eip); | |
1871 | } else { | |
1872 | PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector); | |
1873 | PUSHW(ssp, sp, sp_mask, next_eip); | |
1874 | } | |
1875 | ||
1876 | /* from this point, not restartable */ | |
1877 | ||
1878 | if (new_stack) { | |
1879 | ss = (ss & ~3) | dpl; | |
1880 | cpu_x86_load_seg_cache(env, R_SS, ss, | |
1881 | ssp, | |
1882 | get_seg_limit(ss_e1, ss_e2), | |
1883 | ss_e2); | |
1884 | } | |
1885 | ||
1886 | selector = (selector & ~3) | dpl; | |
1887 | cpu_x86_load_seg_cache(env, R_CS, selector, | |
1888 | get_seg_base(e1, e2), | |
1889 | get_seg_limit(e1, e2), | |
1890 | e2); | |
1891 | cpu_x86_set_cpl(env, dpl); | |
1892 | SET_ESP(sp, sp_mask); | |
1893 | EIP = offset; | |
1894 | } | |
eaa728ee FB |
1895 | } |
1896 | ||
1897 | /* real and vm86 mode iret */ | |
2999a0b2 | 1898 | void helper_iret_real(CPUX86State *env, int shift) |
eaa728ee FB |
1899 | { |
1900 | uint32_t sp, new_cs, new_eip, new_eflags, sp_mask; | |
1901 | target_ulong ssp; | |
1902 | int eflags_mask; | |
1903 | ||
20054ef0 | 1904 | sp_mask = 0xffff; /* XXXX: use SS segment size? */ |
eaa728ee FB |
1905 | sp = ESP; |
1906 | ssp = env->segs[R_SS].base; | |
1907 | if (shift == 1) { | |
1908 | /* 32 bits */ | |
1909 | POPL(ssp, sp, sp_mask, new_eip); | |
1910 | POPL(ssp, sp, sp_mask, new_cs); | |
1911 | new_cs &= 0xffff; | |
1912 | POPL(ssp, sp, sp_mask, new_eflags); | |
1913 | } else { | |
1914 | /* 16 bits */ | |
1915 | POPW(ssp, sp, sp_mask, new_eip); | |
1916 | POPW(ssp, sp, sp_mask, new_cs); | |
1917 | POPW(ssp, sp, sp_mask, new_eflags); | |
1918 | } | |
1919 | ESP = (ESP & ~sp_mask) | (sp & sp_mask); | |
bdadc0b5 | 1920 | env->segs[R_CS].selector = new_cs; |
1921 | env->segs[R_CS].base = (new_cs << 4); | |
eaa728ee | 1922 | env->eip = new_eip; |
20054ef0 BS |
1923 | if (env->eflags & VM_MASK) { |
1924 | eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | RF_MASK | | |
1925 | NT_MASK; | |
1926 | } else { | |
1927 | eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK | | |
1928 | RF_MASK | NT_MASK; | |
1929 | } | |
1930 | if (shift == 0) { | |
eaa728ee | 1931 | eflags_mask &= 0xffff; |
20054ef0 | 1932 | } |
997ff0d9 | 1933 | cpu_load_eflags(env, new_eflags, eflags_mask); |
db620f46 | 1934 | env->hflags2 &= ~HF2_NMI_MASK; |
eaa728ee FB |
1935 | } |
1936 | ||
2999a0b2 | 1937 | static inline void validate_seg(CPUX86State *env, int seg_reg, int cpl) |
eaa728ee FB |
1938 | { |
1939 | int dpl; | |
1940 | uint32_t e2; | |
1941 | ||
1942 | /* XXX: on x86_64, we do not want to nullify FS and GS because | |
1943 | they may still contain a valid base. I would be interested to | |
1944 | know how a real x86_64 CPU behaves */ | |
1945 | if ((seg_reg == R_FS || seg_reg == R_GS) && | |
20054ef0 | 1946 | (env->segs[seg_reg].selector & 0xfffc) == 0) { |
eaa728ee | 1947 | return; |
20054ef0 | 1948 | } |
eaa728ee FB |
1949 | |
1950 | e2 = env->segs[seg_reg].flags; | |
1951 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
1952 | if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) { | |
1953 | /* data or non conforming code segment */ | |
1954 | if (dpl < cpl) { | |
1955 | cpu_x86_load_seg_cache(env, seg_reg, 0, 0, 0, 0); | |
1956 | } | |
1957 | } | |
1958 | } | |
1959 | ||
1960 | /* protected mode iret */ | |
2999a0b2 BS |
1961 | static inline void helper_ret_protected(CPUX86State *env, int shift, |
1962 | int is_iret, int addend) | |
eaa728ee FB |
1963 | { |
1964 | uint32_t new_cs, new_eflags, new_ss; | |
1965 | uint32_t new_es, new_ds, new_fs, new_gs; | |
1966 | uint32_t e1, e2, ss_e1, ss_e2; | |
1967 | int cpl, dpl, rpl, eflags_mask, iopl; | |
1968 | target_ulong ssp, sp, new_eip, new_esp, sp_mask; | |
1969 | ||
1970 | #ifdef TARGET_X86_64 | |
20054ef0 | 1971 | if (shift == 2) { |
eaa728ee | 1972 | sp_mask = -1; |
20054ef0 | 1973 | } else |
eaa728ee | 1974 | #endif |
20054ef0 | 1975 | { |
eaa728ee | 1976 | sp_mask = get_sp_mask(env->segs[R_SS].flags); |
20054ef0 | 1977 | } |
eaa728ee FB |
1978 | sp = ESP; |
1979 | ssp = env->segs[R_SS].base; | |
1980 | new_eflags = 0; /* avoid warning */ | |
1981 | #ifdef TARGET_X86_64 | |
1982 | if (shift == 2) { | |
1983 | POPQ(sp, new_eip); | |
1984 | POPQ(sp, new_cs); | |
1985 | new_cs &= 0xffff; | |
1986 | if (is_iret) { | |
1987 | POPQ(sp, new_eflags); | |
1988 | } | |
1989 | } else | |
1990 | #endif | |
20054ef0 BS |
1991 | { |
1992 | if (shift == 1) { | |
1993 | /* 32 bits */ | |
1994 | POPL(ssp, sp, sp_mask, new_eip); | |
1995 | POPL(ssp, sp, sp_mask, new_cs); | |
1996 | new_cs &= 0xffff; | |
1997 | if (is_iret) { | |
1998 | POPL(ssp, sp, sp_mask, new_eflags); | |
1999 | if (new_eflags & VM_MASK) { | |
2000 | goto return_to_vm86; | |
2001 | } | |
2002 | } | |
2003 | } else { | |
2004 | /* 16 bits */ | |
2005 | POPW(ssp, sp, sp_mask, new_eip); | |
2006 | POPW(ssp, sp, sp_mask, new_cs); | |
2007 | if (is_iret) { | |
2008 | POPW(ssp, sp, sp_mask, new_eflags); | |
2009 | } | |
eaa728ee | 2010 | } |
eaa728ee | 2011 | } |
d12d51d5 AL |
2012 | LOG_PCALL("lret new %04x:" TARGET_FMT_lx " s=%d addend=0x%x\n", |
2013 | new_cs, new_eip, shift, addend); | |
2014 | LOG_PCALL_STATE(env); | |
20054ef0 | 2015 | if ((new_cs & 0xfffc) == 0) { |
77b2bc2c | 2016 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 2017 | } |
2999a0b2 | 2018 | if (load_segment(env, &e1, &e2, new_cs) != 0) { |
77b2bc2c | 2019 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 2020 | } |
eaa728ee | 2021 | if (!(e2 & DESC_S_MASK) || |
20054ef0 | 2022 | !(e2 & DESC_CS_MASK)) { |
77b2bc2c | 2023 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 2024 | } |
eaa728ee FB |
2025 | cpl = env->hflags & HF_CPL_MASK; |
2026 | rpl = new_cs & 3; | |
20054ef0 | 2027 | if (rpl < cpl) { |
77b2bc2c | 2028 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 2029 | } |
eaa728ee FB |
2030 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; |
2031 | if (e2 & DESC_C_MASK) { | |
20054ef0 | 2032 | if (dpl > rpl) { |
77b2bc2c | 2033 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 2034 | } |
eaa728ee | 2035 | } else { |
20054ef0 | 2036 | if (dpl != rpl) { |
77b2bc2c | 2037 | raise_exception_err(env, EXCP0D_GPF, new_cs & 0xfffc); |
20054ef0 | 2038 | } |
eaa728ee | 2039 | } |
20054ef0 | 2040 | if (!(e2 & DESC_P_MASK)) { |
77b2bc2c | 2041 | raise_exception_err(env, EXCP0B_NOSEG, new_cs & 0xfffc); |
20054ef0 | 2042 | } |
eaa728ee FB |
2043 | |
2044 | sp += addend; | |
2045 | if (rpl == cpl && (!(env->hflags & HF_CS64_MASK) || | |
2046 | ((env->hflags & HF_CS64_MASK) && !is_iret))) { | |
1235fc06 | 2047 | /* return to same privilege level */ |
eaa728ee FB |
2048 | cpu_x86_load_seg_cache(env, R_CS, new_cs, |
2049 | get_seg_base(e1, e2), | |
2050 | get_seg_limit(e1, e2), | |
2051 | e2); | |
2052 | } else { | |
2053 | /* return to different privilege level */ | |
2054 | #ifdef TARGET_X86_64 | |
2055 | if (shift == 2) { | |
2056 | POPQ(sp, new_esp); | |
2057 | POPQ(sp, new_ss); | |
2058 | new_ss &= 0xffff; | |
2059 | } else | |
2060 | #endif | |
20054ef0 BS |
2061 | { |
2062 | if (shift == 1) { | |
2063 | /* 32 bits */ | |
2064 | POPL(ssp, sp, sp_mask, new_esp); | |
2065 | POPL(ssp, sp, sp_mask, new_ss); | |
2066 | new_ss &= 0xffff; | |
2067 | } else { | |
2068 | /* 16 bits */ | |
2069 | POPW(ssp, sp, sp_mask, new_esp); | |
2070 | POPW(ssp, sp, sp_mask, new_ss); | |
2071 | } | |
eaa728ee | 2072 | } |
d12d51d5 | 2073 | LOG_PCALL("new ss:esp=%04x:" TARGET_FMT_lx "\n", |
20054ef0 | 2074 | new_ss, new_esp); |
eaa728ee FB |
2075 | if ((new_ss & 0xfffc) == 0) { |
2076 | #ifdef TARGET_X86_64 | |
20054ef0 BS |
2077 | /* NULL ss is allowed in long mode if cpl != 3 */ |
2078 | /* XXX: test CS64? */ | |
eaa728ee FB |
2079 | if ((env->hflags & HF_LMA_MASK) && rpl != 3) { |
2080 | cpu_x86_load_seg_cache(env, R_SS, new_ss, | |
2081 | 0, 0xffffffff, | |
2082 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
2083 | DESC_S_MASK | (rpl << DESC_DPL_SHIFT) | | |
2084 | DESC_W_MASK | DESC_A_MASK); | |
20054ef0 | 2085 | ss_e2 = DESC_B_MASK; /* XXX: should not be needed? */ |
eaa728ee FB |
2086 | } else |
2087 | #endif | |
2088 | { | |
77b2bc2c | 2089 | raise_exception_err(env, EXCP0D_GPF, 0); |
eaa728ee FB |
2090 | } |
2091 | } else { | |
20054ef0 | 2092 | if ((new_ss & 3) != rpl) { |
77b2bc2c | 2093 | raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc); |
20054ef0 | 2094 | } |
2999a0b2 | 2095 | if (load_segment(env, &ss_e1, &ss_e2, new_ss) != 0) { |
77b2bc2c | 2096 | raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc); |
20054ef0 | 2097 | } |
eaa728ee FB |
2098 | if (!(ss_e2 & DESC_S_MASK) || |
2099 | (ss_e2 & DESC_CS_MASK) || | |
20054ef0 | 2100 | !(ss_e2 & DESC_W_MASK)) { |
77b2bc2c | 2101 | raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc); |
20054ef0 | 2102 | } |
eaa728ee | 2103 | dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3; |
20054ef0 | 2104 | if (dpl != rpl) { |
77b2bc2c | 2105 | raise_exception_err(env, EXCP0D_GPF, new_ss & 0xfffc); |
20054ef0 BS |
2106 | } |
2107 | if (!(ss_e2 & DESC_P_MASK)) { | |
77b2bc2c | 2108 | raise_exception_err(env, EXCP0B_NOSEG, new_ss & 0xfffc); |
20054ef0 | 2109 | } |
eaa728ee FB |
2110 | cpu_x86_load_seg_cache(env, R_SS, new_ss, |
2111 | get_seg_base(ss_e1, ss_e2), | |
2112 | get_seg_limit(ss_e1, ss_e2), | |
2113 | ss_e2); | |
2114 | } | |
2115 | ||
2116 | cpu_x86_load_seg_cache(env, R_CS, new_cs, | |
2117 | get_seg_base(e1, e2), | |
2118 | get_seg_limit(e1, e2), | |
2119 | e2); | |
2120 | cpu_x86_set_cpl(env, rpl); | |
2121 | sp = new_esp; | |
2122 | #ifdef TARGET_X86_64 | |
20054ef0 | 2123 | if (env->hflags & HF_CS64_MASK) { |
eaa728ee | 2124 | sp_mask = -1; |
20054ef0 | 2125 | } else |
eaa728ee | 2126 | #endif |
20054ef0 | 2127 | { |
eaa728ee | 2128 | sp_mask = get_sp_mask(ss_e2); |
20054ef0 | 2129 | } |
eaa728ee FB |
2130 | |
2131 | /* validate data segments */ | |
2999a0b2 BS |
2132 | validate_seg(env, R_ES, rpl); |
2133 | validate_seg(env, R_DS, rpl); | |
2134 | validate_seg(env, R_FS, rpl); | |
2135 | validate_seg(env, R_GS, rpl); | |
eaa728ee FB |
2136 | |
2137 | sp += addend; | |
2138 | } | |
2139 | SET_ESP(sp, sp_mask); | |
2140 | env->eip = new_eip; | |
2141 | if (is_iret) { | |
2142 | /* NOTE: 'cpl' is the _old_ CPL */ | |
2143 | eflags_mask = TF_MASK | AC_MASK | ID_MASK | RF_MASK | NT_MASK; | |
20054ef0 | 2144 | if (cpl == 0) { |
eaa728ee | 2145 | eflags_mask |= IOPL_MASK; |
20054ef0 | 2146 | } |
eaa728ee | 2147 | iopl = (env->eflags >> IOPL_SHIFT) & 3; |
20054ef0 | 2148 | if (cpl <= iopl) { |
eaa728ee | 2149 | eflags_mask |= IF_MASK; |
20054ef0 BS |
2150 | } |
2151 | if (shift == 0) { | |
eaa728ee | 2152 | eflags_mask &= 0xffff; |
20054ef0 | 2153 | } |
997ff0d9 | 2154 | cpu_load_eflags(env, new_eflags, eflags_mask); |
eaa728ee FB |
2155 | } |
2156 | return; | |
2157 | ||
2158 | return_to_vm86: | |
2159 | POPL(ssp, sp, sp_mask, new_esp); | |
2160 | POPL(ssp, sp, sp_mask, new_ss); | |
2161 | POPL(ssp, sp, sp_mask, new_es); | |
2162 | POPL(ssp, sp, sp_mask, new_ds); | |
2163 | POPL(ssp, sp, sp_mask, new_fs); | |
2164 | POPL(ssp, sp, sp_mask, new_gs); | |
2165 | ||
2166 | /* modify processor state */ | |
997ff0d9 BS |
2167 | cpu_load_eflags(env, new_eflags, TF_MASK | AC_MASK | ID_MASK | |
2168 | IF_MASK | IOPL_MASK | VM_MASK | NT_MASK | VIF_MASK | | |
2169 | VIP_MASK); | |
2999a0b2 | 2170 | load_seg_vm(env, R_CS, new_cs & 0xffff); |
eaa728ee | 2171 | cpu_x86_set_cpl(env, 3); |
2999a0b2 BS |
2172 | load_seg_vm(env, R_SS, new_ss & 0xffff); |
2173 | load_seg_vm(env, R_ES, new_es & 0xffff); | |
2174 | load_seg_vm(env, R_DS, new_ds & 0xffff); | |
2175 | load_seg_vm(env, R_FS, new_fs & 0xffff); | |
2176 | load_seg_vm(env, R_GS, new_gs & 0xffff); | |
eaa728ee FB |
2177 | |
2178 | env->eip = new_eip & 0xffff; | |
2179 | ESP = new_esp; | |
2180 | } | |
2181 | ||
2999a0b2 | 2182 | void helper_iret_protected(CPUX86State *env, int shift, int next_eip) |
eaa728ee FB |
2183 | { |
2184 | int tss_selector, type; | |
2185 | uint32_t e1, e2; | |
2186 | ||
2187 | /* specific case for TSS */ | |
2188 | if (env->eflags & NT_MASK) { | |
2189 | #ifdef TARGET_X86_64 | |
20054ef0 | 2190 | if (env->hflags & HF_LMA_MASK) { |
77b2bc2c | 2191 | raise_exception_err(env, EXCP0D_GPF, 0); |
20054ef0 | 2192 | } |
eaa728ee | 2193 | #endif |
329e607d | 2194 | tss_selector = cpu_lduw_kernel(env, env->tr.base + 0); |
20054ef0 | 2195 | if (tss_selector & 4) { |
77b2bc2c | 2196 | raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc); |
20054ef0 | 2197 | } |
2999a0b2 | 2198 | if (load_segment(env, &e1, &e2, tss_selector) != 0) { |
77b2bc2c | 2199 | raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc); |
20054ef0 | 2200 | } |
eaa728ee FB |
2201 | type = (e2 >> DESC_TYPE_SHIFT) & 0x17; |
2202 | /* NOTE: we check both segment and busy TSS */ | |
20054ef0 | 2203 | if (type != 3) { |
77b2bc2c | 2204 | raise_exception_err(env, EXCP0A_TSS, tss_selector & 0xfffc); |
20054ef0 | 2205 | } |
2999a0b2 | 2206 | switch_tss(env, tss_selector, e1, e2, SWITCH_TSS_IRET, next_eip); |
eaa728ee | 2207 | } else { |
2999a0b2 | 2208 | helper_ret_protected(env, shift, 1, 0); |
eaa728ee | 2209 | } |
db620f46 | 2210 | env->hflags2 &= ~HF2_NMI_MASK; |
eaa728ee FB |
2211 | } |
2212 | ||
2999a0b2 | 2213 | void helper_lret_protected(CPUX86State *env, int shift, int addend) |
eaa728ee | 2214 | { |
2999a0b2 | 2215 | helper_ret_protected(env, shift, 0, addend); |
eaa728ee FB |
2216 | } |
2217 | ||
2999a0b2 | 2218 | void helper_sysenter(CPUX86State *env) |
eaa728ee FB |
2219 | { |
2220 | if (env->sysenter_cs == 0) { | |
77b2bc2c | 2221 | raise_exception_err(env, EXCP0D_GPF, 0); |
eaa728ee FB |
2222 | } |
2223 | env->eflags &= ~(VM_MASK | IF_MASK | RF_MASK); | |
2224 | cpu_x86_set_cpl(env, 0); | |
2436b61a AZ |
2225 | |
2226 | #ifdef TARGET_X86_64 | |
2227 | if (env->hflags & HF_LMA_MASK) { | |
2228 | cpu_x86_load_seg_cache(env, R_CS, env->sysenter_cs & 0xfffc, | |
2229 | 0, 0xffffffff, | |
2230 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
2231 | DESC_S_MASK | | |
20054ef0 BS |
2232 | DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK | |
2233 | DESC_L_MASK); | |
2436b61a AZ |
2234 | } else |
2235 | #endif | |
2236 | { | |
2237 | cpu_x86_load_seg_cache(env, R_CS, env->sysenter_cs & 0xfffc, | |
2238 | 0, 0xffffffff, | |
2239 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
2240 | DESC_S_MASK | | |
2241 | DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK); | |
2242 | } | |
eaa728ee FB |
2243 | cpu_x86_load_seg_cache(env, R_SS, (env->sysenter_cs + 8) & 0xfffc, |
2244 | 0, 0xffffffff, | |
2245 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | | |
2246 | DESC_S_MASK | | |
2247 | DESC_W_MASK | DESC_A_MASK); | |
2248 | ESP = env->sysenter_esp; | |
2249 | EIP = env->sysenter_eip; | |
2250 | } | |
2251 | ||
2999a0b2 | 2252 | void helper_sysexit(CPUX86State *env, int dflag) |
eaa728ee FB |
2253 | { |
2254 | int cpl; | |
2255 | ||
2256 | cpl = env->hflags & HF_CPL_MASK; | |
2257 | if (env->sysenter_cs == 0 || cpl != 0) { | |
77b2bc2c | 2258 | raise_exception_err(env, EXCP0D_GPF, 0); |
eaa728ee FB |
2259 | } |
2260 | cpu_x86_set_cpl(env, 3); | |
2436b61a AZ |
2261 | #ifdef TARGET_X86_64 |
2262 | if (dflag == 2) { | |
20054ef0 BS |
2263 | cpu_x86_load_seg_cache(env, R_CS, ((env->sysenter_cs + 32) & 0xfffc) | |
2264 | 3, 0, 0xffffffff, | |
2436b61a AZ |
2265 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | |
2266 | DESC_S_MASK | (3 << DESC_DPL_SHIFT) | | |
20054ef0 BS |
2267 | DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK | |
2268 | DESC_L_MASK); | |
2269 | cpu_x86_load_seg_cache(env, R_SS, ((env->sysenter_cs + 40) & 0xfffc) | | |
2270 | 3, 0, 0xffffffff, | |
2436b61a AZ |
2271 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | |
2272 | DESC_S_MASK | (3 << DESC_DPL_SHIFT) | | |
2273 | DESC_W_MASK | DESC_A_MASK); | |
2274 | } else | |
2275 | #endif | |
2276 | { | |
20054ef0 BS |
2277 | cpu_x86_load_seg_cache(env, R_CS, ((env->sysenter_cs + 16) & 0xfffc) | |
2278 | 3, 0, 0xffffffff, | |
2436b61a AZ |
2279 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | |
2280 | DESC_S_MASK | (3 << DESC_DPL_SHIFT) | | |
2281 | DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK); | |
20054ef0 BS |
2282 | cpu_x86_load_seg_cache(env, R_SS, ((env->sysenter_cs + 24) & 0xfffc) | |
2283 | 3, 0, 0xffffffff, | |
2436b61a AZ |
2284 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | |
2285 | DESC_S_MASK | (3 << DESC_DPL_SHIFT) | | |
2286 | DESC_W_MASK | DESC_A_MASK); | |
2287 | } | |
eaa728ee FB |
2288 | ESP = ECX; |
2289 | EIP = EDX; | |
eaa728ee FB |
2290 | } |
2291 | ||
2999a0b2 | 2292 | target_ulong helper_lsl(CPUX86State *env, target_ulong selector1) |
eaa728ee FB |
2293 | { |
2294 | unsigned int limit; | |
2295 | uint32_t e1, e2, eflags, selector; | |
2296 | int rpl, dpl, cpl, type; | |
2297 | ||
2298 | selector = selector1 & 0xffff; | |
f0967a1a | 2299 | eflags = cpu_cc_compute_all(env, CC_OP); |
20054ef0 | 2300 | if ((selector & 0xfffc) == 0) { |
dc1ded53 | 2301 | goto fail; |
20054ef0 | 2302 | } |
2999a0b2 | 2303 | if (load_segment(env, &e1, &e2, selector) != 0) { |
eaa728ee | 2304 | goto fail; |
20054ef0 | 2305 | } |
eaa728ee FB |
2306 | rpl = selector & 3; |
2307 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
2308 | cpl = env->hflags & HF_CPL_MASK; | |
2309 | if (e2 & DESC_S_MASK) { | |
2310 | if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) { | |
2311 | /* conforming */ | |
2312 | } else { | |
20054ef0 | 2313 | if (dpl < cpl || dpl < rpl) { |
eaa728ee | 2314 | goto fail; |
20054ef0 | 2315 | } |
eaa728ee FB |
2316 | } |
2317 | } else { | |
2318 | type = (e2 >> DESC_TYPE_SHIFT) & 0xf; | |
20054ef0 | 2319 | switch (type) { |
eaa728ee FB |
2320 | case 1: |
2321 | case 2: | |
2322 | case 3: | |
2323 | case 9: | |
2324 | case 11: | |
2325 | break; | |
2326 | default: | |
2327 | goto fail; | |
2328 | } | |
2329 | if (dpl < cpl || dpl < rpl) { | |
2330 | fail: | |
2331 | CC_SRC = eflags & ~CC_Z; | |
2332 | return 0; | |
2333 | } | |
2334 | } | |
2335 | limit = get_seg_limit(e1, e2); | |
2336 | CC_SRC = eflags | CC_Z; | |
2337 | return limit; | |
2338 | } | |
2339 | ||
2999a0b2 | 2340 | target_ulong helper_lar(CPUX86State *env, target_ulong selector1) |
eaa728ee FB |
2341 | { |
2342 | uint32_t e1, e2, eflags, selector; | |
2343 | int rpl, dpl, cpl, type; | |
2344 | ||
2345 | selector = selector1 & 0xffff; | |
f0967a1a | 2346 | eflags = cpu_cc_compute_all(env, CC_OP); |
20054ef0 | 2347 | if ((selector & 0xfffc) == 0) { |
eaa728ee | 2348 | goto fail; |
20054ef0 | 2349 | } |
2999a0b2 | 2350 | if (load_segment(env, &e1, &e2, selector) != 0) { |
eaa728ee | 2351 | goto fail; |
20054ef0 | 2352 | } |
eaa728ee FB |
2353 | rpl = selector & 3; |
2354 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
2355 | cpl = env->hflags & HF_CPL_MASK; | |
2356 | if (e2 & DESC_S_MASK) { | |
2357 | if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) { | |
2358 | /* conforming */ | |
2359 | } else { | |
20054ef0 | 2360 | if (dpl < cpl || dpl < rpl) { |
eaa728ee | 2361 | goto fail; |
20054ef0 | 2362 | } |
eaa728ee FB |
2363 | } |
2364 | } else { | |
2365 | type = (e2 >> DESC_TYPE_SHIFT) & 0xf; | |
20054ef0 | 2366 | switch (type) { |
eaa728ee FB |
2367 | case 1: |
2368 | case 2: | |
2369 | case 3: | |
2370 | case 4: | |
2371 | case 5: | |
2372 | case 9: | |
2373 | case 11: | |
2374 | case 12: | |
2375 | break; | |
2376 | default: | |
2377 | goto fail; | |
2378 | } | |
2379 | if (dpl < cpl || dpl < rpl) { | |
2380 | fail: | |
2381 | CC_SRC = eflags & ~CC_Z; | |
2382 | return 0; | |
2383 | } | |
2384 | } | |
2385 | CC_SRC = eflags | CC_Z; | |
2386 | return e2 & 0x00f0ff00; | |
2387 | } | |
2388 | ||
2999a0b2 | 2389 | void helper_verr(CPUX86State *env, target_ulong selector1) |
eaa728ee FB |
2390 | { |
2391 | uint32_t e1, e2, eflags, selector; | |
2392 | int rpl, dpl, cpl; | |
2393 | ||
2394 | selector = selector1 & 0xffff; | |
f0967a1a | 2395 | eflags = cpu_cc_compute_all(env, CC_OP); |
20054ef0 | 2396 | if ((selector & 0xfffc) == 0) { |
eaa728ee | 2397 | goto fail; |
20054ef0 | 2398 | } |
2999a0b2 | 2399 | if (load_segment(env, &e1, &e2, selector) != 0) { |
eaa728ee | 2400 | goto fail; |
20054ef0 BS |
2401 | } |
2402 | if (!(e2 & DESC_S_MASK)) { | |
eaa728ee | 2403 | goto fail; |
20054ef0 | 2404 | } |
eaa728ee FB |
2405 | rpl = selector & 3; |
2406 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
2407 | cpl = env->hflags & HF_CPL_MASK; | |
2408 | if (e2 & DESC_CS_MASK) { | |
20054ef0 | 2409 | if (!(e2 & DESC_R_MASK)) { |
eaa728ee | 2410 | goto fail; |
20054ef0 | 2411 | } |
eaa728ee | 2412 | if (!(e2 & DESC_C_MASK)) { |
20054ef0 | 2413 | if (dpl < cpl || dpl < rpl) { |
eaa728ee | 2414 | goto fail; |
20054ef0 | 2415 | } |
eaa728ee FB |
2416 | } |
2417 | } else { | |
2418 | if (dpl < cpl || dpl < rpl) { | |
2419 | fail: | |
2420 | CC_SRC = eflags & ~CC_Z; | |
2421 | return; | |
2422 | } | |
2423 | } | |
2424 | CC_SRC = eflags | CC_Z; | |
2425 | } | |
2426 | ||
2999a0b2 | 2427 | void helper_verw(CPUX86State *env, target_ulong selector1) |
eaa728ee FB |
2428 | { |
2429 | uint32_t e1, e2, eflags, selector; | |
2430 | int rpl, dpl, cpl; | |
2431 | ||
2432 | selector = selector1 & 0xffff; | |
f0967a1a | 2433 | eflags = cpu_cc_compute_all(env, CC_OP); |
20054ef0 | 2434 | if ((selector & 0xfffc) == 0) { |
eaa728ee | 2435 | goto fail; |
20054ef0 | 2436 | } |
2999a0b2 | 2437 | if (load_segment(env, &e1, &e2, selector) != 0) { |
eaa728ee | 2438 | goto fail; |
20054ef0 BS |
2439 | } |
2440 | if (!(e2 & DESC_S_MASK)) { | |
eaa728ee | 2441 | goto fail; |
20054ef0 | 2442 | } |
eaa728ee FB |
2443 | rpl = selector & 3; |
2444 | dpl = (e2 >> DESC_DPL_SHIFT) & 3; | |
2445 | cpl = env->hflags & HF_CPL_MASK; | |
2446 | if (e2 & DESC_CS_MASK) { | |
2447 | goto fail; | |
2448 | } else { | |
20054ef0 | 2449 | if (dpl < cpl || dpl < rpl) { |
eaa728ee | 2450 | goto fail; |
20054ef0 | 2451 | } |
eaa728ee FB |
2452 | if (!(e2 & DESC_W_MASK)) { |
2453 | fail: | |
2454 | CC_SRC = eflags & ~CC_Z; | |
2455 | return; | |
2456 | } | |
2457 | } | |
2458 | CC_SRC = eflags | CC_Z; | |
2459 | } | |
2460 | ||
f299f437 | 2461 | #if defined(CONFIG_USER_ONLY) |
2999a0b2 | 2462 | void cpu_x86_load_seg(CPUX86State *env, int seg_reg, int selector) |
eaa728ee | 2463 | { |
f299f437 BS |
2464 | if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) { |
2465 | selector &= 0xffff; | |
2466 | cpu_x86_load_seg_cache(env, seg_reg, selector, | |
2467 | (selector << 4), 0xffff, 0); | |
2468 | } else { | |
2999a0b2 | 2469 | helper_load_seg(env, seg_reg, selector); |
13822781 | 2470 | } |
eaa728ee | 2471 | } |
eaa728ee | 2472 | #endif |