blob: 1aec8a5f1914955ce096fde3ae09f4d46d74107d [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"
Richard Henderson2ef61752014-04-07 22:31:41 -070021#include "exec/helper-proto.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010022#include "exec/cpu_ldst.h"
Blue Swirl10774992012-04-29 16:39:13 +000023
24/* broken thread support */
25
26static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
27
28void helper_lock(void)
29{
30 spin_lock(&global_cpu_lock);
31}
32
33void helper_unlock(void)
34{
35 spin_unlock(&global_cpu_lock);
36}
37
Blue Swirl92fc4b52012-04-29 20:35:48 +000038void helper_cmpxchg8b(CPUX86State *env, target_ulong a0)
Blue Swirl10774992012-04-29 16:39:13 +000039{
40 uint64_t d;
41 int eflags;
42
Blue Swirlf0967a12012-04-29 12:45:34 +000043 eflags = cpu_cc_compute_all(env, CC_OP);
Blue Swirl92fc4b52012-04-29 20:35:48 +000044 d = cpu_ldq_data(env, a0);
liguang00f5e6f2013-05-28 16:21:02 +080045 if (d == (((uint64_t)env->regs[R_EDX] << 32) | (uint32_t)env->regs[R_EAX])) {
liguanga4165612013-05-28 16:21:01 +080046 cpu_stq_data(env, a0, ((uint64_t)env->regs[R_ECX] << 32) | (uint32_t)env->regs[R_EBX]);
Blue Swirl10774992012-04-29 16:39:13 +000047 eflags |= CC_Z;
48 } else {
49 /* always do the store */
Blue Swirl92fc4b52012-04-29 20:35:48 +000050 cpu_stq_data(env, a0, d);
liguang00f5e6f2013-05-28 16:21:02 +080051 env->regs[R_EDX] = (uint32_t)(d >> 32);
liguang4b34e3a2013-05-28 16:20:59 +080052 env->regs[R_EAX] = (uint32_t)d;
Blue Swirl10774992012-04-29 16:39:13 +000053 eflags &= ~CC_Z;
54 }
55 CC_SRC = eflags;
56}
57
58#ifdef TARGET_X86_64
Blue Swirl92fc4b52012-04-29 20:35:48 +000059void helper_cmpxchg16b(CPUX86State *env, target_ulong a0)
Blue Swirl10774992012-04-29 16:39:13 +000060{
61 uint64_t d0, d1;
62 int eflags;
63
64 if ((a0 & 0xf) != 0) {
65 raise_exception(env, EXCP0D_GPF);
66 }
Blue Swirlf0967a12012-04-29 12:45:34 +000067 eflags = cpu_cc_compute_all(env, CC_OP);
Blue Swirl92fc4b52012-04-29 20:35:48 +000068 d0 = cpu_ldq_data(env, a0);
69 d1 = cpu_ldq_data(env, a0 + 8);
liguang00f5e6f2013-05-28 16:21:02 +080070 if (d0 == env->regs[R_EAX] && d1 == env->regs[R_EDX]) {
liguang70b51362013-05-28 16:21:00 +080071 cpu_stq_data(env, a0, env->regs[R_EBX]);
liguanga4165612013-05-28 16:21:01 +080072 cpu_stq_data(env, a0 + 8, env->regs[R_ECX]);
Blue Swirl10774992012-04-29 16:39:13 +000073 eflags |= CC_Z;
74 } else {
75 /* always do the store */
Blue Swirl92fc4b52012-04-29 20:35:48 +000076 cpu_stq_data(env, a0, d0);
77 cpu_stq_data(env, a0 + 8, d1);
liguang00f5e6f2013-05-28 16:21:02 +080078 env->regs[R_EDX] = d1;
liguang4b34e3a2013-05-28 16:20:59 +080079 env->regs[R_EAX] = d0;
Blue Swirl10774992012-04-29 16:39:13 +000080 eflags &= ~CC_Z;
81 }
82 CC_SRC = eflags;
83}
84#endif
85
Blue Swirl92fc4b52012-04-29 20:35:48 +000086void helper_boundw(CPUX86State *env, target_ulong a0, int v)
Blue Swirl10774992012-04-29 16:39:13 +000087{
88 int low, high;
89
Blue Swirl92fc4b52012-04-29 20:35:48 +000090 low = cpu_ldsw_data(env, a0);
91 high = cpu_ldsw_data(env, a0 + 2);
Blue Swirl10774992012-04-29 16:39:13 +000092 v = (int16_t)v;
93 if (v < low || v > high) {
94 raise_exception(env, EXCP05_BOUND);
95 }
96}
97
Blue Swirl92fc4b52012-04-29 20:35:48 +000098void helper_boundl(CPUX86State *env, target_ulong a0, int v)
Blue Swirl10774992012-04-29 16:39:13 +000099{
100 int low, high;
101
Blue Swirl92fc4b52012-04-29 20:35:48 +0000102 low = cpu_ldl_data(env, a0);
103 high = cpu_ldl_data(env, a0 + 4);
Blue Swirl10774992012-04-29 16:39:13 +0000104 if (v < low || v > high) {
105 raise_exception(env, EXCP05_BOUND);
106 }
107}
108
109#if !defined(CONFIG_USER_ONLY)
Blue Swirl10774992012-04-29 16:39:13 +0000110/* try to fill the TLB and return an exception if error. If retaddr is
Andreas Färberd5a11fe2013-08-27 00:28:06 +0200111 * NULL, it means that the function was called in C code (i.e. not
112 * from generated code or from helper.c)
113 */
Blue Swirl10774992012-04-29 16:39:13 +0000114/* XXX: fix it to restore all registers */
Andreas Färberd5a11fe2013-08-27 00:28:06 +0200115void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
Blue Swirl10774992012-04-29 16:39:13 +0000116 uintptr_t retaddr)
117{
Blue Swirl10774992012-04-29 16:39:13 +0000118 int ret;
Blue Swirl10774992012-04-29 16:39:13 +0000119
Andreas Färber27103422013-08-26 08:31:06 +0200120 ret = x86_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
Blue Swirl10774992012-04-29 16:39:13 +0000121 if (ret) {
Andreas Färberd5a11fe2013-08-27 00:28:06 +0200122 X86CPU *cpu = X86_CPU(cs);
123 CPUX86State *env = &cpu->env;
124
Blue Swirl10774992012-04-29 16:39:13 +0000125 if (retaddr) {
126 /* now we have a real cpu fault */
Andreas Färber3f38f302013-09-01 16:51:34 +0200127 cpu_restore_state(cs, retaddr);
Blue Swirl10774992012-04-29 16:39:13 +0000128 }
Andreas Färber27103422013-08-26 08:31:06 +0200129 raise_exception_err(env, cs->exception_index, env->error_code);
Blue Swirl10774992012-04-29 16:39:13 +0000130 }
Blue Swirl10774992012-04-29 16:39:13 +0000131}
132#endif