xref: /qemu/target/i386/tcg/mpx_helper.c (revision fe1a3ace13a8b53fc20c74fb7e3337f754396e6b)
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.1 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 "qemu/osdep.h"
21  #include "cpu.h"
22  #include "exec/helper-proto.h"
23  #include "accel/tcg/cpu-ldst.h"
24  #include "exec/exec-all.h"
25  #include "exec/target_page.h"
26  #include "helper-tcg.h"
27  
28  
29  void helper_bndck(CPUX86State *env, uint32_t fail)
30  {
31      if (unlikely(fail)) {
32          env->bndcs_regs.sts = 1;
33          raise_exception_ra(env, EXCP05_BOUND, GETPC());
34      }
35  }
36  
37  static uint64_t lookup_bte64(CPUX86State *env, uint64_t base, uintptr_t ra)
38  {
39      uint64_t bndcsr, bde, bt;
40  
41      if ((env->hflags & HF_CPL_MASK) == 3) {
42          bndcsr = env->bndcs_regs.cfgu;
43      } else {
44          bndcsr = env->msr_bndcfgs;
45      }
46  
47      bde = (extract64(base, 20, 28) << 3) + (extract64(bndcsr, 20, 44) << 12);
48      bt = cpu_ldq_data_ra(env, bde, ra);
49      if ((bt & 1) == 0) {
50          env->bndcs_regs.sts = bde | 2;
51          raise_exception_ra(env, EXCP05_BOUND, ra);
52      }
53  
54      return (extract64(base, 3, 17) << 5) + (bt & ~7);
55  }
56  
57  static uint32_t lookup_bte32(CPUX86State *env, uint32_t base, uintptr_t ra)
58  {
59      uint32_t bndcsr, bde, bt;
60  
61      if ((env->hflags & HF_CPL_MASK) == 3) {
62          bndcsr = env->bndcs_regs.cfgu;
63      } else {
64          bndcsr = env->msr_bndcfgs;
65      }
66  
67      bde = (extract32(base, 12, 20) << 2) + (bndcsr & TARGET_PAGE_MASK);
68      bt = cpu_ldl_data_ra(env, bde, ra);
69      if ((bt & 1) == 0) {
70          env->bndcs_regs.sts = bde | 2;
71          raise_exception_ra(env, EXCP05_BOUND, ra);
72      }
73  
74      return (extract32(base, 2, 10) << 4) + (bt & ~3);
75  }
76  
77  uint64_t helper_bndldx64(CPUX86State *env, target_ulong base, target_ulong ptr)
78  {
79      uintptr_t ra = GETPC();
80      uint64_t bte, lb, ub, pt;
81  
82      bte = lookup_bte64(env, base, ra);
83      lb = cpu_ldq_data_ra(env, bte, ra);
84      ub = cpu_ldq_data_ra(env, bte + 8, ra);
85      pt = cpu_ldq_data_ra(env, bte + 16, ra);
86  
87      if (pt != ptr) {
88          lb = ub = 0;
89      }
90      env->mmx_t0.MMX_Q(0) = ub;
91      return lb;
92  }
93  
94  uint64_t helper_bndldx32(CPUX86State *env, target_ulong base, target_ulong ptr)
95  {
96      uintptr_t ra = GETPC();
97      uint32_t bte, lb, ub, pt;
98  
99      bte = lookup_bte32(env, base, ra);
100      lb = cpu_ldl_data_ra(env, bte, ra);
101      ub = cpu_ldl_data_ra(env, bte + 4, ra);
102      pt = cpu_ldl_data_ra(env, bte + 8, ra);
103  
104      if (pt != ptr) {
105          lb = ub = 0;
106      }
107      return ((uint64_t)ub << 32) | lb;
108  }
109  
110  void helper_bndstx64(CPUX86State *env, target_ulong base, target_ulong ptr,
111                       uint64_t lb, uint64_t ub)
112  {
113      uintptr_t ra = GETPC();
114      uint64_t bte;
115  
116      bte = lookup_bte64(env, base, ra);
117      cpu_stq_data_ra(env, bte, lb, ra);
118      cpu_stq_data_ra(env, bte + 8, ub, ra);
119      cpu_stq_data_ra(env, bte + 16, ptr, ra);
120  }
121  
122  void helper_bndstx32(CPUX86State *env, target_ulong base, target_ulong ptr,
123                       uint64_t lb, uint64_t ub)
124  {
125      uintptr_t ra = GETPC();
126      uint32_t bte;
127  
128      bte = lookup_bte32(env, base, ra);
129      cpu_stl_data_ra(env, bte, lb, ra);
130      cpu_stl_data_ra(env, bte + 4, ub, ra);
131      cpu_stl_data_ra(env, bte + 8, ptr, ra);
132  }
133  
134  void helper_bnd_jmp(CPUX86State *env)
135  {
136      if (!(env->hflags2 & HF2_MPX_PR_MASK)) {
137          memset(env->bnd_regs, 0, sizeof(env->bnd_regs));
138          env->hflags &= ~HF_MPX_IU_MASK;
139      }
140  }
141