blob: 6cf9ba076e837db60cb529ced659bc7a29330c12 [file] [log] [blame]
Blue Swirl10774992012-04-29 16:39:13 +00001/*
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"
Blue Swirl10774992012-04-29 16:39:13 +000021#include "helper.h"
22
23#if !defined(CONFIG_USER_ONLY)
Paolo Bonzini022c62c2012-12-17 18:19:49 +010024#include "exec/softmmu_exec.h"
Blue Swirl10774992012-04-29 16:39:13 +000025#endif /* !defined(CONFIG_USER_ONLY) */
26
27/* broken thread support */
28
29static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
30
31void helper_lock(void)
32{
33 spin_lock(&global_cpu_lock);
34}
35
36void helper_unlock(void)
37{
38 spin_unlock(&global_cpu_lock);
39}
40
Blue Swirl92fc4b52012-04-29 20:35:48 +000041void helper_cmpxchg8b(CPUX86State *env, target_ulong a0)
Blue Swirl10774992012-04-29 16:39:13 +000042{
43 uint64_t d;
44 int eflags;
45
Blue Swirlf0967a12012-04-29 12:45:34 +000046 eflags = cpu_cc_compute_all(env, CC_OP);
Blue Swirl92fc4b52012-04-29 20:35:48 +000047 d = cpu_ldq_data(env, a0);
Blue Swirl10774992012-04-29 16:39:13 +000048 if (d == (((uint64_t)EDX << 32) | (uint32_t)EAX)) {
Blue Swirl92fc4b52012-04-29 20:35:48 +000049 cpu_stq_data(env, a0, ((uint64_t)ECX << 32) | (uint32_t)EBX);
Blue Swirl10774992012-04-29 16:39:13 +000050 eflags |= CC_Z;
51 } else {
52 /* always do the store */
Blue Swirl92fc4b52012-04-29 20:35:48 +000053 cpu_stq_data(env, a0, d);
Blue Swirl10774992012-04-29 16:39:13 +000054 EDX = (uint32_t)(d >> 32);
55 EAX = (uint32_t)d;
56 eflags &= ~CC_Z;
57 }
58 CC_SRC = eflags;
59}
60
61#ifdef TARGET_X86_64
Blue Swirl92fc4b52012-04-29 20:35:48 +000062void helper_cmpxchg16b(CPUX86State *env, target_ulong a0)
Blue Swirl10774992012-04-29 16:39:13 +000063{
64 uint64_t d0, d1;
65 int eflags;
66
67 if ((a0 & 0xf) != 0) {
68 raise_exception(env, EXCP0D_GPF);
69 }
Blue Swirlf0967a12012-04-29 12:45:34 +000070 eflags = cpu_cc_compute_all(env, CC_OP);
Blue Swirl92fc4b52012-04-29 20:35:48 +000071 d0 = cpu_ldq_data(env, a0);
72 d1 = cpu_ldq_data(env, a0 + 8);
Blue Swirl10774992012-04-29 16:39:13 +000073 if (d0 == EAX && d1 == EDX) {
Blue Swirl92fc4b52012-04-29 20:35:48 +000074 cpu_stq_data(env, a0, EBX);
75 cpu_stq_data(env, a0 + 8, ECX);
Blue Swirl10774992012-04-29 16:39:13 +000076 eflags |= CC_Z;
77 } else {
78 /* always do the store */
Blue Swirl92fc4b52012-04-29 20:35:48 +000079 cpu_stq_data(env, a0, d0);
80 cpu_stq_data(env, a0 + 8, d1);
Blue Swirl10774992012-04-29 16:39:13 +000081 EDX = d1;
82 EAX = d0;
83 eflags &= ~CC_Z;
84 }
85 CC_SRC = eflags;
86}
87#endif
88
Blue Swirl92fc4b52012-04-29 20:35:48 +000089void helper_boundw(CPUX86State *env, target_ulong a0, int v)
Blue Swirl10774992012-04-29 16:39:13 +000090{
91 int low, high;
92
Blue Swirl92fc4b52012-04-29 20:35:48 +000093 low = cpu_ldsw_data(env, a0);
94 high = cpu_ldsw_data(env, a0 + 2);
Blue Swirl10774992012-04-29 16:39:13 +000095 v = (int16_t)v;
96 if (v < low || v > high) {
97 raise_exception(env, EXCP05_BOUND);
98 }
99}
100
Blue Swirl92fc4b52012-04-29 20:35:48 +0000101void helper_boundl(CPUX86State *env, target_ulong a0, int v)
Blue Swirl10774992012-04-29 16:39:13 +0000102{
103 int low, high;
104
Blue Swirl92fc4b52012-04-29 20:35:48 +0000105 low = cpu_ldl_data(env, a0);
106 high = cpu_ldl_data(env, a0 + 4);
Blue Swirl10774992012-04-29 16:39:13 +0000107 if (v < low || v > high) {
108 raise_exception(env, EXCP05_BOUND);
109 }
110}
111
112#if !defined(CONFIG_USER_ONLY)
113
114#define MMUSUFFIX _mmu
115
116#define SHIFT 0
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100117#include "exec/softmmu_template.h"
Blue Swirl10774992012-04-29 16:39:13 +0000118
119#define SHIFT 1
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100120#include "exec/softmmu_template.h"
Blue Swirl10774992012-04-29 16:39:13 +0000121
122#define SHIFT 2
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100123#include "exec/softmmu_template.h"
Blue Swirl10774992012-04-29 16:39:13 +0000124
125#define SHIFT 3
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100126#include "exec/softmmu_template.h"
Blue Swirl10774992012-04-29 16:39:13 +0000127
128#endif
129
130#if !defined(CONFIG_USER_ONLY)
131/* try to fill the TLB and return an exception if error. If retaddr is
132 NULL, it means that the function was called in C code (i.e. not
133 from generated code or from helper.c) */
134/* XXX: fix it to restore all registers */
Blue Swirl92fc4b52012-04-29 20:35:48 +0000135void tlb_fill(CPUX86State *env, target_ulong addr, int is_write, int mmu_idx,
Blue Swirl10774992012-04-29 16:39:13 +0000136 uintptr_t retaddr)
137{
Blue Swirl10774992012-04-29 16:39:13 +0000138 int ret;
Blue Swirl10774992012-04-29 16:39:13 +0000139
140 ret = cpu_x86_handle_mmu_fault(env, addr, is_write, mmu_idx);
141 if (ret) {
142 if (retaddr) {
143 /* now we have a real cpu fault */
Blue Swirla8a826a2012-12-04 20:16:07 +0000144 cpu_restore_state(env, retaddr);
Blue Swirl10774992012-04-29 16:39:13 +0000145 }
146 raise_exception_err(env, env->exception_index, env->error_code);
147 }
Blue Swirl10774992012-04-29 16:39:13 +0000148}
149#endif