]> Git Repo - qemu.git/blob - target-i386/mpx_helper.c
Merge remote-tracking branch 'remotes/rth/tags/pull-i386-20160215' into staging
[qemu.git] / target-i386 / mpx_helper.c
1 /*
2  *  x86 MPX helpers
3  *
4  *  Copyright (c) 2015 Red Hat, Inc.
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 "exec/helper-proto.h"
22 #include "exec/cpu_ldst.h"
23
24
25 void cpu_sync_bndcs_hflags(CPUX86State *env)
26 {
27     uint32_t hflags = env->hflags;
28     uint32_t hflags2 = env->hflags2;
29     uint32_t bndcsr;
30
31     if ((hflags & HF_CPL_MASK) == 3) {
32         bndcsr = env->bndcs_regs.cfgu;
33     } else {
34         bndcsr = env->msr_bndcfgs;
35     }
36
37     if ((env->cr[4] & CR4_OSXSAVE_MASK)
38         && (env->xcr0 & XSTATE_BNDCSR)
39         && (bndcsr & BNDCFG_ENABLE)) {
40         hflags |= HF_MPX_EN_MASK;
41     } else {
42         hflags &= ~HF_MPX_EN_MASK;
43     }
44
45     if (bndcsr & BNDCFG_BNDPRESERVE) {
46         hflags2 |= HF2_MPX_PR_MASK;
47     } else {
48         hflags2 &= ~HF2_MPX_PR_MASK;
49     }
50
51     env->hflags = hflags;
52     env->hflags2 = hflags2;
53 }
54
55 void helper_bndck(CPUX86State *env, uint32_t fail)
56 {
57     if (unlikely(fail)) {
58         env->bndcs_regs.sts = 1;
59         raise_exception_ra(env, EXCP05_BOUND, GETPC());
60     }
61 }
62
63 static uint64_t lookup_bte64(CPUX86State *env, uint64_t base, uintptr_t ra)
64 {
65     uint64_t bndcsr, bde, bt;
66
67     if ((env->hflags & HF_CPL_MASK) == 3) {
68         bndcsr = env->bndcs_regs.cfgu;
69     } else {
70         bndcsr = env->msr_bndcfgs;
71     }
72
73     bde = (extract64(base, 20, 28) << 3) + (extract64(bndcsr, 20, 44) << 12);
74     bt = cpu_ldq_data_ra(env, bde, ra);
75     if ((bt & 1) == 0) {
76         env->bndcs_regs.sts = bde | 2;
77         raise_exception_ra(env, EXCP05_BOUND, ra);
78     }
79
80     return (extract64(base, 3, 17) << 5) + (bt & ~7);
81 }
82
83 static uint32_t lookup_bte32(CPUX86State *env, uint32_t base, uintptr_t ra)
84 {
85     uint32_t bndcsr, bde, bt;
86
87     if ((env->hflags & HF_CPL_MASK) == 3) {
88         bndcsr = env->bndcs_regs.cfgu;
89     } else {
90         bndcsr = env->msr_bndcfgs;
91     }
92
93     bde = (extract32(base, 12, 20) << 2) + (bndcsr & TARGET_PAGE_MASK);
94     bt = cpu_ldl_data_ra(env, bde, ra);
95     if ((bt & 1) == 0) {
96         env->bndcs_regs.sts = bde | 2;
97         raise_exception_ra(env, EXCP05_BOUND, ra);
98     }
99
100     return (extract32(base, 2, 10) << 4) + (bt & ~3);
101 }
102
103 uint64_t helper_bndldx64(CPUX86State *env, target_ulong base, target_ulong ptr)
104 {
105     uintptr_t ra = GETPC();
106     uint64_t bte, lb, ub, pt;
107
108     bte = lookup_bte64(env, base, ra);
109     lb = cpu_ldq_data_ra(env, bte, ra);
110     ub = cpu_ldq_data_ra(env, bte + 8, ra);
111     pt = cpu_ldq_data_ra(env, bte + 16, ra);
112
113     if (pt != ptr) {
114         lb = ub = 0;
115     }
116     env->mmx_t0.MMX_Q(0) = ub;
117     return lb;
118 }
119
120 uint64_t helper_bndldx32(CPUX86State *env, target_ulong base, target_ulong ptr)
121 {
122     uintptr_t ra = GETPC();
123     uint32_t bte, lb, ub, pt;
124
125     bte = lookup_bte32(env, base, ra);
126     lb = cpu_ldl_data_ra(env, bte, ra);
127     ub = cpu_ldl_data_ra(env, bte + 4, ra);
128     pt = cpu_ldl_data_ra(env, bte + 8, ra);
129
130     if (pt != ptr) {
131         lb = ub = 0;
132     }
133     return ((uint64_t)ub << 32) | lb;
134 }
135
136 void helper_bndstx64(CPUX86State *env, target_ulong base, target_ulong ptr,
137                      uint64_t lb, uint64_t ub)
138 {
139     uintptr_t ra = GETPC();
140     uint64_t bte;
141
142     bte = lookup_bte64(env, base, ra);
143     cpu_stq_data_ra(env, bte, lb, ra);
144     cpu_stq_data_ra(env, bte + 8, ub, ra);
145     cpu_stq_data_ra(env, bte + 16, ptr, ra);
146 }
147
148 void helper_bndstx32(CPUX86State *env, target_ulong base, target_ulong ptr,
149                      uint64_t lb, uint64_t ub)
150 {
151     uintptr_t ra = GETPC();
152     uint32_t bte;
153
154     bte = lookup_bte32(env, base, ra);
155     cpu_stl_data_ra(env, bte, lb, ra);
156     cpu_stl_data_ra(env, bte + 4, ub, ra);
157     cpu_stl_data_ra(env, bte + 8, ptr, ra);
158 }
159
160 void helper_bnd_jmp(CPUX86State *env)
161 {
162     if (!(env->hflags2 & HF2_MPX_PR_MASK)) {
163         memset(env->bnd_regs, 0, sizeof(env->bnd_regs));
164         env->hflags &= ~HF_MPX_IU_MASK;
165     }
166 }
This page took 0.032681 seconds and 4 git commands to generate.