scsi-disk: support toggling the write cache
[qemu/opensuse.git] / target-i386 / mem_helper.c
blob91353c07887dd6da9d9072706baaadac2ba9a7da
1 /*
2 * x86 memory access helpers
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
20 #include "cpu.h"
21 #include "dyngen-exec.h"
22 #include "helper.h"
24 #if !defined(CONFIG_USER_ONLY)
25 #include "softmmu_exec.h"
26 #endif /* !defined(CONFIG_USER_ONLY) */
28 /* broken thread support */
30 static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
32 void helper_lock(void)
34 spin_lock(&global_cpu_lock);
37 void helper_unlock(void)
39 spin_unlock(&global_cpu_lock);
42 void helper_cmpxchg8b(target_ulong a0)
44 uint64_t d;
45 int eflags;
47 eflags = helper_cc_compute_all(CC_OP);
48 d = ldq(a0);
49 if (d == (((uint64_t)EDX << 32) | (uint32_t)EAX)) {
50 stq(a0, ((uint64_t)ECX << 32) | (uint32_t)EBX);
51 eflags |= CC_Z;
52 } else {
53 /* always do the store */
54 stq(a0, d);
55 EDX = (uint32_t)(d >> 32);
56 EAX = (uint32_t)d;
57 eflags &= ~CC_Z;
59 CC_SRC = eflags;
62 #ifdef TARGET_X86_64
63 void helper_cmpxchg16b(target_ulong a0)
65 uint64_t d0, d1;
66 int eflags;
68 if ((a0 & 0xf) != 0) {
69 raise_exception(env, EXCP0D_GPF);
71 eflags = helper_cc_compute_all(CC_OP);
72 d0 = ldq(a0);
73 d1 = ldq(a0 + 8);
74 if (d0 == EAX && d1 == EDX) {
75 stq(a0, EBX);
76 stq(a0 + 8, ECX);
77 eflags |= CC_Z;
78 } else {
79 /* always do the store */
80 stq(a0, d0);
81 stq(a0 + 8, d1);
82 EDX = d1;
83 EAX = d0;
84 eflags &= ~CC_Z;
86 CC_SRC = eflags;
88 #endif
90 void helper_boundw(target_ulong a0, int v)
92 int low, high;
94 low = ldsw(a0);
95 high = ldsw(a0 + 2);
96 v = (int16_t)v;
97 if (v < low || v > high) {
98 raise_exception(env, EXCP05_BOUND);
102 void helper_boundl(target_ulong a0, int v)
104 int low, high;
106 low = ldl(a0);
107 high = ldl(a0 + 4);
108 if (v < low || v > high) {
109 raise_exception(env, EXCP05_BOUND);
113 #if !defined(CONFIG_USER_ONLY)
115 #define MMUSUFFIX _mmu
117 #define SHIFT 0
118 #include "softmmu_template.h"
120 #define SHIFT 1
121 #include "softmmu_template.h"
123 #define SHIFT 2
124 #include "softmmu_template.h"
126 #define SHIFT 3
127 #include "softmmu_template.h"
129 #endif
131 #if !defined(CONFIG_USER_ONLY)
132 /* try to fill the TLB and return an exception if error. If retaddr is
133 NULL, it means that the function was called in C code (i.e. not
134 from generated code or from helper.c) */
135 /* XXX: fix it to restore all registers */
136 void tlb_fill(CPUX86State *env1, target_ulong addr, int is_write, int mmu_idx,
137 uintptr_t retaddr)
139 TranslationBlock *tb;
140 int ret;
141 CPUX86State *saved_env;
143 saved_env = env;
144 env = env1;
146 ret = cpu_x86_handle_mmu_fault(env, addr, is_write, mmu_idx);
147 if (ret) {
148 if (retaddr) {
149 /* now we have a real cpu fault */
150 tb = tb_find_pc(retaddr);
151 if (tb) {
152 /* the PC is inside the translated code. It means that we have
153 a virtual CPU fault */
154 cpu_restore_state(tb, env, retaddr);
157 raise_exception_err(env, env->exception_index, env->error_code);
159 env = saved_env;
161 #endif