mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-27 04:52:05 +08:00
8ee22052f6
For GNU/Linux on x86-64, if the target is using the xsave format for passing the floating-point information from the inferior then there currently exists a bug relating to the x87 control registers, and the mxcsr register. The xsave format allows different floating-point features to be lazily enabled, a bit in the xsave format tells GDB which floating-point features have been enabled, and which have not. Currently in GDB, when reading the floating point state, we check the xsave bit flags, if the feature is enabled then we read the feature from the xsave buffer, and if the feature is not enabled, then we supply the default value from within GDB. Within GDB, when writing the floating point state, we first fetch the xsave state from the target and then, for any feature that is not yet enabled, we write the default values into the xsave buffer. Next we compare the regcache value with the value in the xsave buffer, and, if the value has changed we update the value in the xsave buffer, and mark the feature enabled in the xsave bit flags. The problem then, is that the x87 control registers were not following this pattern. We assumed that these registers were always written out by the kernel, and we always wrote them out to the xsave buffer (but didn't enabled the feature). The result of this is that if the kernel had not yet enabled the x87 feature then within GDB we would see random values for the x87 floating point control registers, and if the user tried to modify one of these register, that modification would be lost. Finally, the mxcsr register was also broken in the same way as the x87 control registers. The added complexity with this case is that the mxcsr register is part of both the avx and sse floating point feature set. When reading or writing this register we need to check that at least one of these features is enabled. This bug was present in native GDB, and within gdbserver. Both are fixed with this commit. gdb/ChangeLog: * common/x86-xstate.h (I387_FCTRL_INIT_VAL): New constant. (I387_MXCSR_INIT_VAL): New constant. * amd64-tdep.c (amd64_supply_xsave): Only read state from xsave buffer if it was supplied by the inferior. * i387-tdep.c (i387_supply_fsave): Use I387_MXCSR_INIT_VAL. (i387_xsave_get_clear_bv): New function. (i387_supply_xsave): Only read x87 control registers from the xsave buffer if the feature is enabled, and the state will have been written, otherwise, provide a suitable default. (i387_collect_xsave): Pre-clear all registers in xsave buffer, including x87 control registers. Update control registers if they have changed from the default value, and mark features as enabled as required. * i387-tdep.h (i387_xsave_get_clear_bv): Declare. gdb/gdbserver/ChangeLog: * i387-fp.c (i387_cache_to_xsave): Only write x87 control registers to the cache if their values have changed. (i387_xsave_to_cache): Provide default values for x87 control registers when these features are available, but disabled. * regcache.c (supply_register_by_name_zeroed): New function. * regcache.h (supply_register_by_name_zeroed): Declare new function. gdb/testsuite/ChangeLog: * gdb.arch/amd64-init-x87-values.S: New file. * gdb.arch/amd64-init-x87-values.exp: New file.
176 lines
5.6 KiB
Plaintext
176 lines
5.6 KiB
Plaintext
# Copyright 2018 Free Software Foundation, Inc.
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# This file is part of the gdb testsuite.
|
|
|
|
# Test initial values of x87 control registers, before any x87
|
|
# instructions have been executed in the inferior.
|
|
|
|
if ![is_amd64_regs_target] then {
|
|
return
|
|
}
|
|
|
|
standard_testfile .S
|
|
|
|
set options [list debug \
|
|
additional_flags=-static \
|
|
additional_flags=-nostartfiles]
|
|
if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} $options] } {
|
|
return -1
|
|
}
|
|
|
|
# Start the test file, and check the x87 control registers (and
|
|
# mxcsr), we expect the default values in all registers.
|
|
#
|
|
# Step forward until the x87 unit is enabled, recheck the register
|
|
# values; they should still have the default values.
|
|
#
|
|
# Finally, step forward until the x87 state has changed, and then
|
|
# recheck the register state to view the changes.
|
|
proc_with_prefix check_x87_regs_around_init {} {
|
|
global binfile
|
|
|
|
clean_restart ${binfile}
|
|
|
|
# Get things started.
|
|
if ![runto_main] then {
|
|
fail "can't run to main"
|
|
return 0
|
|
}
|
|
|
|
# Check initial values of x87 control registers. The MXCSR isn't part
|
|
# of the x87 set, it belongs with SSE/AVX, however we test it here
|
|
# too, it should have its default value in both cases.
|
|
foreach {regname regvalue} { "fctrl" "0x37f" \
|
|
"fstat" "0x0" \
|
|
"ftag" "0xffff" \
|
|
"fiseg" "0x0" \
|
|
"fioff" "0x0" \
|
|
"foseg" "0x0" \
|
|
"fooff" "0x0" \
|
|
"fop" "0x0" \
|
|
"mxcsr" "0x1f80"} {
|
|
gdb_test "p/x \$${regname}" " = ${regvalue}" "check initial value of \$${regname}"
|
|
}
|
|
|
|
# No x87 instructions have been executed yet. Step up to FWAIT
|
|
# instruction. Executing this instruction will enable the x87 unit,
|
|
# causing the kernel to place the default values into all registers.
|
|
# After this GDB will no longer supply the default values itself but
|
|
# will instread read the values out of the xsave buffer.
|
|
gdb_test "stepi" "fwait" "step to FWAIT instruction"
|
|
gdb_test "stepi" "nop" "step past FWAIT instruction"
|
|
|
|
# The x87 unit is now enabled, but the register values should be
|
|
# unchanged.
|
|
foreach {regname regvalue} { "fctrl" "0x37f" \
|
|
"fstat" "0x0" \
|
|
"ftag" "0xffff" \
|
|
"fiseg" "0x0" \
|
|
"fioff" "0x0" \
|
|
"foseg" "0x0" \
|
|
"fooff" "0x0" \
|
|
"fop" "0x0" \
|
|
"mxcsr" "0x1f80"} {
|
|
gdb_test "p/x \$${regname}" " = ${regvalue}" "check post FWAIT value of \$${regname}"
|
|
}
|
|
|
|
# Now step to an x87 instruction that modifies some state.
|
|
gdb_test "stepi" "fld1" "step to FLD1 instruction"
|
|
|
|
# Grab the address of this instruction, it will appear in later
|
|
# results.
|
|
set addr [get_hexadecimal_valueof "\$pc" "0"]
|
|
|
|
# Step past the FLD1 instruction.
|
|
gdb_test "stepi" "nop" "step past FLD1 instruction"
|
|
|
|
# Check new values of x87 control registers (and MXCSR).
|
|
foreach {regname regvalue} [list "fctrl" "0x37f" \
|
|
"fstat" "0x3800" \
|
|
"ftag" "0x3fff" \
|
|
"fiseg" "0x0" \
|
|
"fioff" $addr \
|
|
"foseg" "0x0" \
|
|
"fooff" "0x0" \
|
|
"fop" "0x0" \
|
|
"mxcsr" "0x1f80" ] {
|
|
gdb_test "p/x \$${regname}" " = ${regvalue}" "check post FLD1 value of \$${regname}"
|
|
}
|
|
}
|
|
|
|
# Start the test file, all FP features will be disabled. Set a new
|
|
# value into the MXCSR register, then step forward one instruction (a
|
|
# nop that does not enable any FP features). Finally check that the
|
|
# mxcsr register still has the value we set.
|
|
proc_with_prefix check_setting_mxcsr_before_enable {} {
|
|
global binfile
|
|
|
|
clean_restart ${binfile}
|
|
|
|
if ![runto_main] then {
|
|
fail "can't run to main"
|
|
return 0
|
|
}
|
|
|
|
gdb_test_no_output "set \$mxcsr=0x9f80" "set a new value for MXCSR"
|
|
gdb_test "stepi" "fwait" "step forward one instruction for mxcsr test"
|
|
gdb_test "p/x \$mxcsr" " = 0x9f80" "check new value of MXCSR is still in place"
|
|
}
|
|
|
|
# Start the test file, all FP features will be disabled. Set new
|
|
# values into the x87 control-registers, then step forward one
|
|
# instruction (a nop that does not enable any FP features). Finally
|
|
# check that all the x87 control-registers still have the values we
|
|
# set.
|
|
proc_with_prefix check_setting_x87_regs_before_enable {} {
|
|
global binfile
|
|
|
|
clean_restart ${binfile}
|
|
|
|
if ![runto_main] then {
|
|
fail "can't run to main"
|
|
return 0
|
|
}
|
|
|
|
foreach {regname regvalue} [list "fctrl" "0x37f" \
|
|
"fstat" "0x3800" \
|
|
"ftag" "0x7777" \
|
|
"fiseg" "0x12" \
|
|
"fioff" "0x2418" \
|
|
"foseg" "0x24" \
|
|
"fooff" "0x36" \
|
|
"fop" "0x100" ] {
|
|
gdb_test_no_output "set \$$regname=$regvalue" "set a new value for $regname"
|
|
}
|
|
|
|
gdb_test "stepi" "fwait" "step forward one instruction for x87 test"
|
|
|
|
foreach {regname regvalue} [list "fctrl" "0x37f" \
|
|
"fstat" "0x3800" \
|
|
"ftag" "0x7777" \
|
|
"fiseg" "0x12" \
|
|
"fioff" "0x2418" \
|
|
"foseg" "0x24" \
|
|
"fooff" "0x36" \
|
|
"fop" "0x100" ] {
|
|
gdb_test "p/x \$$regname" "= $regvalue" "check new value of $regname"
|
|
}
|
|
}
|
|
|
|
check_x87_regs_around_init
|
|
check_setting_mxcsr_before_enable
|
|
check_setting_x87_regs_before_enable
|