xref: /qemu/target/i386/tcg/user/seg_helper.c (revision 7cef6d686309e2792186504ae17cf4f3eb57ef68)
1 /*
2  *  x86 segmentation related helpers (user-mode code):
3  *  TSS, interrupts, system calls, jumps and call/task gates, descriptors
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.1 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
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "exec/helper-proto.h"
24 #include "accel/tcg/cpu-ldst.h"
25 #include "tcg/helper-tcg.h"
26 #include "tcg/seg_helper.h"
27 
helper_syscall(CPUX86State * env,int next_eip_addend)28 void helper_syscall(CPUX86State *env, int next_eip_addend)
29 {
30     CPUState *cs = env_cpu(env);
31 
32     cs->exception_index = EXCP_SYSCALL;
33     env->exception_is_int = 0;
34     env->exception_next_eip = env->eip + next_eip_addend;
35     cpu_loop_exit(cs);
36 }
37 
38 /*
39  * fake user mode interrupt. is_int is TRUE if coming from the int
40  * instruction. next_eip is the env->eip value AFTER the interrupt
41  * instruction. It is only relevant if is_int is TRUE or if intno
42  * is EXCP_SYSCALL.
43  */
do_interrupt_user(CPUX86State * env,int intno,int is_int,int error_code,target_ulong next_eip)44 static void do_interrupt_user(CPUX86State *env, int intno, int is_int,
45                               int error_code, target_ulong next_eip)
46 {
47     if (is_int) {
48         SegmentCache *dt;
49         target_ulong ptr;
50         int dpl, cpl, shift;
51         uint32_t e2;
52 
53         dt = &env->idt;
54         if (env->hflags & HF_LMA_MASK) {
55             shift = 4;
56         } else {
57             shift = 3;
58         }
59         ptr = dt->base + (intno << shift);
60         e2 = cpu_ldl_kernel(env, ptr + 4);
61 
62         dpl = (e2 >> DESC_DPL_SHIFT) & 3;
63         cpl = env->hflags & HF_CPL_MASK;
64         /* check privilege if software int */
65         if (dpl < cpl) {
66             raise_exception_err(env, EXCP0D_GPF, (intno << shift) + 2);
67         }
68     }
69 
70     /* Since we emulate only user space, we cannot do more than
71        exiting the emulation with the suitable exception and error
72        code. So update EIP for INT 0x80 and EXCP_SYSCALL. */
73     if (is_int || intno == EXCP_SYSCALL) {
74         env->eip = next_eip;
75     }
76 }
77 
x86_cpu_do_interrupt(CPUState * cs)78 void x86_cpu_do_interrupt(CPUState *cs)
79 {
80     X86CPU *cpu = X86_CPU(cs);
81     CPUX86State *env = &cpu->env;
82 
83     /* if user mode only, we simulate a fake exception
84        which will be handled outside the cpu execution
85        loop */
86     do_interrupt_user(env, cs->exception_index,
87                       env->exception_is_int,
88                       env->error_code,
89                       env->exception_next_eip);
90     /* successfully delivered */
91     env->old_exception = -1;
92 }
93 
cpu_x86_load_seg(CPUX86State * env,X86Seg seg_reg,int selector)94 void cpu_x86_load_seg(CPUX86State *env, X86Seg seg_reg, int selector)
95 {
96     if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
97         int dpl = (env->eflags & VM_MASK) ? 3 : 0;
98         selector &= 0xffff;
99         cpu_x86_load_seg_cache(env, seg_reg, selector,
100                                (selector << 4), 0xffff,
101                                DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
102                                DESC_A_MASK | (dpl << DESC_DPL_SHIFT));
103     } else {
104         helper_load_seg(env, seg_reg, selector);
105     }
106 }
107