]> git.proxmox.com Git - qemu.git/blame - target-i386/mem_helper.c
x86: split off memory access helpers
[qemu.git] / target-i386 / mem_helper.c
CommitLineData
10774999
BS
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 "dyngen-exec.h"
22#include "helper.h"
23
24#if !defined(CONFIG_USER_ONLY)
25#include "softmmu_exec.h"
26#endif /* !defined(CONFIG_USER_ONLY) */
27
28/* broken thread support */
29
30static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
31
32void helper_lock(void)
33{
34 spin_lock(&global_cpu_lock);
35}
36
37void helper_unlock(void)
38{
39 spin_unlock(&global_cpu_lock);
40}
41
42void helper_cmpxchg8b(target_ulong a0)
43{
44 uint64_t d;
45 int eflags;
46
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;
58 }
59 CC_SRC = eflags;
60}
61
62#ifdef TARGET_X86_64
63void helper_cmpxchg16b(target_ulong a0)
64{
65 uint64_t d0, d1;
66 int eflags;
67
68 if ((a0 & 0xf) != 0) {
69 raise_exception(env, EXCP0D_GPF);
70 }
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;
85 }
86 CC_SRC = eflags;
87}
88#endif
89
90void helper_boundw(target_ulong a0, int v)
91{
92 int low, high;
93
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);
99 }
100}
101
102void helper_boundl(target_ulong a0, int v)
103{
104 int low, high;
105
106 low = ldl(a0);
107 high = ldl(a0 + 4);
108 if (v < low || v > high) {
109 raise_exception(env, EXCP05_BOUND);
110 }
111}
112
113#if !defined(CONFIG_USER_ONLY)
114
115#define MMUSUFFIX _mmu
116
117#define SHIFT 0
118#include "softmmu_template.h"
119
120#define SHIFT 1
121#include "softmmu_template.h"
122
123#define SHIFT 2
124#include "softmmu_template.h"
125
126#define SHIFT 3
127#include "softmmu_template.h"
128
129#endif
130
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 */
136void tlb_fill(CPUX86State *env1, target_ulong addr, int is_write, int mmu_idx,
137 uintptr_t retaddr)
138{
139 TranslationBlock *tb;
140 int ret;
141 CPUX86State *saved_env;
142
143 saved_env = env;
144 env = env1;
145
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);
155 }
156 }
157 raise_exception_err(env, env->exception_index, env->error_code);
158 }
159 env = saved_env;
160}
161#endif