Add qemu 2.4.0
[kvmfornfv.git] / qemu / target-i386 / mem_helper.c
1 /*
2  *  x86 memory access helpers
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "cpu.h"
21 #include "exec/helper-proto.h"
22 #include "exec/cpu_ldst.h"
23
24 /* broken thread support */
25
26 static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
27
28 void helper_lock(void)
29 {
30     spin_lock(&global_cpu_lock);
31 }
32
33 void helper_unlock(void)
34 {
35     spin_unlock(&global_cpu_lock);
36 }
37
38 void helper_cmpxchg8b(CPUX86State *env, target_ulong a0)
39 {
40     uint64_t d;
41     int eflags;
42
43     eflags = cpu_cc_compute_all(env, CC_OP);
44     d = cpu_ldq_data(env, a0);
45     if (d == (((uint64_t)env->regs[R_EDX] << 32) | (uint32_t)env->regs[R_EAX])) {
46         cpu_stq_data(env, a0, ((uint64_t)env->regs[R_ECX] << 32) | (uint32_t)env->regs[R_EBX]);
47         eflags |= CC_Z;
48     } else {
49         /* always do the store */
50         cpu_stq_data(env, a0, d);
51         env->regs[R_EDX] = (uint32_t)(d >> 32);
52         env->regs[R_EAX] = (uint32_t)d;
53         eflags &= ~CC_Z;
54     }
55     CC_SRC = eflags;
56 }
57
58 #ifdef TARGET_X86_64
59 void helper_cmpxchg16b(CPUX86State *env, target_ulong a0)
60 {
61     uint64_t d0, d1;
62     int eflags;
63
64     if ((a0 & 0xf) != 0) {
65         raise_exception(env, EXCP0D_GPF);
66     }
67     eflags = cpu_cc_compute_all(env, CC_OP);
68     d0 = cpu_ldq_data(env, a0);
69     d1 = cpu_ldq_data(env, a0 + 8);
70     if (d0 == env->regs[R_EAX] && d1 == env->regs[R_EDX]) {
71         cpu_stq_data(env, a0, env->regs[R_EBX]);
72         cpu_stq_data(env, a0 + 8, env->regs[R_ECX]);
73         eflags |= CC_Z;
74     } else {
75         /* always do the store */
76         cpu_stq_data(env, a0, d0);
77         cpu_stq_data(env, a0 + 8, d1);
78         env->regs[R_EDX] = d1;
79         env->regs[R_EAX] = d0;
80         eflags &= ~CC_Z;
81     }
82     CC_SRC = eflags;
83 }
84 #endif
85
86 void helper_boundw(CPUX86State *env, target_ulong a0, int v)
87 {
88     int low, high;
89
90     low = cpu_ldsw_data(env, a0);
91     high = cpu_ldsw_data(env, a0 + 2);
92     v = (int16_t)v;
93     if (v < low || v > high) {
94         raise_exception(env, EXCP05_BOUND);
95     }
96 }
97
98 void helper_boundl(CPUX86State *env, target_ulong a0, int v)
99 {
100     int low, high;
101
102     low = cpu_ldl_data(env, a0);
103     high = cpu_ldl_data(env, a0 + 4);
104     if (v < low || v > high) {
105         raise_exception(env, EXCP05_BOUND);
106     }
107 }
108
109 #if !defined(CONFIG_USER_ONLY)
110 /* try to fill the TLB and return an exception if error. If retaddr is
111  * NULL, it means that the function was called in C code (i.e. not
112  * from generated code or from helper.c)
113  */
114 /* XXX: fix it to restore all registers */
115 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
116               uintptr_t retaddr)
117 {
118     int ret;
119
120     ret = x86_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
121     if (ret) {
122         X86CPU *cpu = X86_CPU(cs);
123         CPUX86State *env = &cpu->env;
124
125         if (retaddr) {
126             /* now we have a real cpu fault */
127             cpu_restore_state(cs, retaddr);
128         }
129         raise_exception_err(env, cs->exception_index, env->error_code);
130     }
131 }
132 #endif