Home | History | Annotate | Line # | Download | only in nat
      1 /* x86 XSAVE extended state functions.
      2 
      3    Copyright (C) 2022-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #include "gdbsupport/x86-xstate.h"
     21 #include "nat/x86-cpuid.h"
     22 #include "nat/x86-xstate.h"
     23 
     24 /* Fetch the offset of a specific XSAVE extended region.  */
     25 
     26 static int
     27 xsave_feature_offset (uint64_t xcr0, int feature)
     28 {
     29   uint32_t ebx;
     30 
     31   if ((xcr0 & (1ULL << feature)) == 0)
     32     return 0;
     33 
     34   if (!x86_cpuid_count (0xd, feature, nullptr, &ebx, nullptr, nullptr))
     35     return 0;
     36   return ebx;
     37 }
     38 
     39 /* See x86-xstate.h.  */
     40 
     41 int
     42 x86_xsave_length ()
     43 {
     44   uint32_t ebx;
     45 
     46   if (!x86_cpuid_count (0xd, 0, nullptr, &ebx, nullptr, nullptr))
     47     return 0;
     48   return ebx;
     49 }
     50 
     51 /* See x86-xstate.h.  */
     52 
     53 x86_xsave_layout
     54 x86_fetch_xsave_layout (uint64_t xcr0, int len)
     55 {
     56   x86_xsave_layout layout;
     57   layout.sizeof_xsave = len;
     58   layout.avx_offset = xsave_feature_offset (xcr0, X86_XSTATE_AVX_ID);
     59   layout.bndregs_offset = xsave_feature_offset (xcr0, X86_XSTATE_BNDREGS_ID);
     60   layout.bndcfg_offset = xsave_feature_offset (xcr0, X86_XSTATE_BNDCFG_ID);
     61   layout.k_offset = xsave_feature_offset (xcr0, X86_XSTATE_K_ID);
     62   layout.zmm_h_offset = xsave_feature_offset (xcr0, X86_XSTATE_ZMM_H_ID);
     63   layout.zmm_offset = xsave_feature_offset (xcr0, X86_XSTATE_ZMM_ID);
     64   layout.pkru_offset = xsave_feature_offset (xcr0, X86_XSTATE_PKRU_ID);
     65   return layout;
     66 }
     67