blob: c2f4769d4b9ee235daebc35e4ab6e0f1bb2f62ec [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
Peter Maydellb6a0aa02016-01-26 18:17:03 +000020#include "qemu/osdep.h"
Blue Swirl10774992012-04-29 16:39:13 +000021#include "cpu.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070022#include "exec/helper-proto.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010023#include "exec/exec-all.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010024#include "exec/cpu_ldst.h"
Blue Swirl10774992012-04-29 16:39:13 +000025
26/* broken thread support */
27
KONRAD Frederic677ef622015-08-10 17:27:02 +020028#if defined(CONFIG_USER_ONLY)
29QemuMutex global_cpu_lock;
Blue Swirl10774992012-04-29 16:39:13 +000030
31void helper_lock(void)
32{
KONRAD Frederic677ef622015-08-10 17:27:02 +020033 qemu_mutex_lock(&global_cpu_lock);
Blue Swirl10774992012-04-29 16:39:13 +000034}
35
36void helper_unlock(void)
37{
KONRAD Frederic677ef622015-08-10 17:27:02 +020038 qemu_mutex_unlock(&global_cpu_lock);
Blue Swirl10774992012-04-29 16:39:13 +000039}
40
KONRAD Frederic677ef622015-08-10 17:27:02 +020041void helper_lock_init(void)
42{
43 qemu_mutex_init(&global_cpu_lock);
44}
45#else
46void helper_lock(void)
47{
48}
49
50void helper_unlock(void)
51{
52}
53
54void helper_lock_init(void)
55{
56}
57#endif
58
Blue Swirl92fc4b52012-04-29 20:35:48 +000059void helper_cmpxchg8b(CPUX86State *env, target_ulong a0)
Blue Swirl10774992012-04-29 16:39:13 +000060{
61 uint64_t d;
62 int eflags;
63
Blue Swirlf0967a12012-04-29 12:45:34 +000064 eflags = cpu_cc_compute_all(env, CC_OP);
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +030065 d = cpu_ldq_data_ra(env, a0, GETPC());
liguang00f5e6f2013-05-28 16:21:02 +080066 if (d == (((uint64_t)env->regs[R_EDX] << 32) | (uint32_t)env->regs[R_EAX])) {
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +030067 cpu_stq_data_ra(env, a0, ((uint64_t)env->regs[R_ECX] << 32)
68 | (uint32_t)env->regs[R_EBX], GETPC());
Blue Swirl10774992012-04-29 16:39:13 +000069 eflags |= CC_Z;
70 } else {
71 /* always do the store */
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +030072 cpu_stq_data_ra(env, a0, d, GETPC());
liguang00f5e6f2013-05-28 16:21:02 +080073 env->regs[R_EDX] = (uint32_t)(d >> 32);
liguang4b34e3a2013-05-28 16:20:59 +080074 env->regs[R_EAX] = (uint32_t)d;
Blue Swirl10774992012-04-29 16:39:13 +000075 eflags &= ~CC_Z;
76 }
77 CC_SRC = eflags;
78}
79
80#ifdef TARGET_X86_64
Blue Swirl92fc4b52012-04-29 20:35:48 +000081void helper_cmpxchg16b(CPUX86State *env, target_ulong a0)
Blue Swirl10774992012-04-29 16:39:13 +000082{
83 uint64_t d0, d1;
84 int eflags;
85
86 if ((a0 & 0xf) != 0) {
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +030087 raise_exception_ra(env, EXCP0D_GPF, GETPC());
Blue Swirl10774992012-04-29 16:39:13 +000088 }
Blue Swirlf0967a12012-04-29 12:45:34 +000089 eflags = cpu_cc_compute_all(env, CC_OP);
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +030090 d0 = cpu_ldq_data_ra(env, a0, GETPC());
91 d1 = cpu_ldq_data_ra(env, a0 + 8, GETPC());
liguang00f5e6f2013-05-28 16:21:02 +080092 if (d0 == env->regs[R_EAX] && d1 == env->regs[R_EDX]) {
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +030093 cpu_stq_data_ra(env, a0, env->regs[R_EBX], GETPC());
94 cpu_stq_data_ra(env, a0 + 8, env->regs[R_ECX], GETPC());
Blue Swirl10774992012-04-29 16:39:13 +000095 eflags |= CC_Z;
96 } else {
97 /* always do the store */
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +030098 cpu_stq_data_ra(env, a0, d0, GETPC());
99 cpu_stq_data_ra(env, a0 + 8, d1, GETPC());
liguang00f5e6f2013-05-28 16:21:02 +0800100 env->regs[R_EDX] = d1;
liguang4b34e3a2013-05-28 16:20:59 +0800101 env->regs[R_EAX] = d0;
Blue Swirl10774992012-04-29 16:39:13 +0000102 eflags &= ~CC_Z;
103 }
104 CC_SRC = eflags;
105}
106#endif
107
Blue Swirl92fc4b52012-04-29 20:35:48 +0000108void helper_boundw(CPUX86State *env, target_ulong a0, int v)
Blue Swirl10774992012-04-29 16:39:13 +0000109{
110 int low, high;
111
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +0300112 low = cpu_ldsw_data_ra(env, a0, GETPC());
113 high = cpu_ldsw_data_ra(env, a0 + 2, GETPC());
Blue Swirl10774992012-04-29 16:39:13 +0000114 v = (int16_t)v;
115 if (v < low || v > high) {
Richard Henderson75d14ed2015-07-06 19:37:40 +0100116 if (env->hflags & HF_MPX_EN_MASK) {
117 env->bndcs_regs.sts = 0;
118 }
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +0300119 raise_exception_ra(env, EXCP05_BOUND, GETPC());
Blue Swirl10774992012-04-29 16:39:13 +0000120 }
121}
122
Blue Swirl92fc4b52012-04-29 20:35:48 +0000123void helper_boundl(CPUX86State *env, target_ulong a0, int v)
Blue Swirl10774992012-04-29 16:39:13 +0000124{
125 int low, high;
126
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +0300127 low = cpu_ldl_data_ra(env, a0, GETPC());
128 high = cpu_ldl_data_ra(env, a0 + 4, GETPC());
Blue Swirl10774992012-04-29 16:39:13 +0000129 if (v < low || v > high) {
Richard Henderson75d14ed2015-07-06 19:37:40 +0100130 if (env->hflags & HF_MPX_EN_MASK) {
131 env->bndcs_regs.sts = 0;
132 }
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +0300133 raise_exception_ra(env, EXCP05_BOUND, GETPC());
Blue Swirl10774992012-04-29 16:39:13 +0000134 }
135}
136
137#if !defined(CONFIG_USER_ONLY)
Blue Swirl10774992012-04-29 16:39:13 +0000138/* try to fill the TLB and return an exception if error. If retaddr is
Andreas Färberd5a11fe2013-08-27 00:28:06 +0200139 * NULL, it means that the function was called in C code (i.e. not
140 * from generated code or from helper.c)
141 */
Blue Swirl10774992012-04-29 16:39:13 +0000142/* XXX: fix it to restore all registers */
Andreas Färberd5a11fe2013-08-27 00:28:06 +0200143void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
Blue Swirl10774992012-04-29 16:39:13 +0000144 uintptr_t retaddr)
145{
Blue Swirl10774992012-04-29 16:39:13 +0000146 int ret;
Blue Swirl10774992012-04-29 16:39:13 +0000147
Andreas Färber27103422013-08-26 08:31:06 +0200148 ret = x86_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
Blue Swirl10774992012-04-29 16:39:13 +0000149 if (ret) {
Andreas Färberd5a11fe2013-08-27 00:28:06 +0200150 X86CPU *cpu = X86_CPU(cs);
151 CPUX86State *env = &cpu->env;
152
Pavel Dovgalyuk2afbdf82015-07-10 12:57:30 +0300153 raise_exception_err_ra(env, cs->exception_index, env->error_code, retaddr);
Blue Swirl10774992012-04-29 16:39:13 +0000154 }
Blue Swirl10774992012-04-29 16:39:13 +0000155}
156#endif