[v4,0/2] x86: Update _dl_tlsdesc_dynamic to preserve caller-saved registers

Message ID 20240213041501.2494232-1-hjl.tools@gmail.com
Headers
Series x86: Update _dl_tlsdesc_dynamic to preserve caller-saved registers |

Message

H.J. Lu Feb. 13, 2024, 4:14 a.m. UTC
  Changes in v4:

1.  Add APX registers to STATE_SAVE_MASK so that APX registers are saved
in ld.so trampoline.
2. Also save x87 FPU stack registers for TLSDESC_CALL and TLS_DESC_CALL.
3. Change i386 _dl_tlsdesc_dynamic to IFUNC.
4. Rename GLRO(dl_x86_64_tlsdesc_dynamic) to GLRO(dl_x86_tlsdesc_dynamic)
for both i386 and x86-64.
5. Update the testcase for i386 with a simple malloc interceptor.

Changes in v3:

1. Don't add GLRO(dl_x86_64_tlsdesc_dynamic) to libc.a.

Changes in v2:

1.  Add GLRO(dl_x86_64_runtime_resolve) to optimize
elf_machine_runtime_setup.

---
Add APX registers to STATE_SAVE_MASK so that APX registers are saved in
ld.so trampoline.  This fixes BZ #31371.

Compiler generates the following instruction sequence for GNU2 dynamic
TLS access:

	leaq	tls_var@TLSDESC(%rip), %rax
	call	*tls_var@TLSCALL(%rax)

or

	leal	tls_var@TLSDESC(%ebx), %eax
	call	*tls_var@TLSCALL(%eax)

CALL instruction is transparent to compiler which assumes all registers,
except for EFLAGS and RAX/EAX, are unchanged after CALL.  When
_dl_tlsdesc_dynamic is called, it calls __tls_get_addr on the slow
path.  __tls_get_addr is a normal function which doesn't preserve any
caller-saved registers.  _dl_tlsdesc_dynamic saved and restored integer
caller-saved registers, but didn't preserve any other caller-saved
registers.  Add _dl_tlsdesc_dynamic IFUNC functions for FNSAVE, FXSAVE,
XSAVE and XSAVEC to save and restore all caller-saved registers.  This
fixes BZ #31372.

Add GLRO(dl_x86_64_runtime_resolve) with GLRO(dl_x86_tlsdesc_dynamic)
to optimize elf_machine_runtime_setup.

H.J. Lu (2):
  x86-64: Save APX registers in ld.so trampoline
  x86: Update _dl_tlsdesc_dynamic to preserve caller-saved registers

 elf/Makefile                                 |  19 ++
 elf/malloc-for-test.c                        |  32 ++++
 elf/malloc-for-test.map                      |   6 +
 elf/tst-gnu2-tls2.c                          |  97 ++++++++++
 elf/tst-gnu2-tls2.h                          |  26 +++
 elf/tst-gnu2-tls2mod0.c                      |  28 +++
 elf/tst-gnu2-tls2mod1.c                      |  28 +++
 elf/tst-gnu2-tls2mod2.c                      |  28 +++
 sysdeps/i386/dl-machine.h                    |   2 +-
 sysdeps/i386/dl-tlsdesc-dynamic.h            | 187 +++++++++++++++++++
 sysdeps/i386/dl-tlsdesc.S                    | 115 +++++-------
 sysdeps/i386/tst-gnu2-tls2.c                 |   5 +
 sysdeps/x86/Makefile                         |   7 +-
 sysdeps/x86/cpu-features.c                   |  56 +++++-
 sysdeps/x86/dl-procinfo.c                    |  16 ++
 sysdeps/{x86_64 => x86}/features-offsets.sym |   2 +
 sysdeps/x86/malloc-for-test.c                |  33 ++++
 sysdeps/x86/sysdep.h                         |  54 +++++-
 sysdeps/x86_64/Makefile                      |   2 +-
 sysdeps/x86_64/dl-machine.h                  |  19 +-
 sysdeps/x86_64/dl-procinfo.c                 |  16 ++
 sysdeps/x86_64/dl-tlsdesc-dynamic.h          | 166 ++++++++++++++++
 sysdeps/x86_64/dl-tlsdesc.S                  | 108 ++++-------
 sysdeps/x86_64/dl-trampoline-save.h          |  34 ++++
 sysdeps/x86_64/dl-trampoline-state.h         |  51 +++++
 sysdeps/x86_64/dl-trampoline.S               |  20 +-
 sysdeps/x86_64/dl-trampoline.h               |  34 +---
 27 files changed, 973 insertions(+), 218 deletions(-)
 create mode 100644 elf/malloc-for-test.c
 create mode 100644 elf/malloc-for-test.map
 create mode 100644 elf/tst-gnu2-tls2.c
 create mode 100644 elf/tst-gnu2-tls2.h
 create mode 100644 elf/tst-gnu2-tls2mod0.c
 create mode 100644 elf/tst-gnu2-tls2mod1.c
 create mode 100644 elf/tst-gnu2-tls2mod2.c
 create mode 100644 sysdeps/i386/dl-tlsdesc-dynamic.h
 create mode 100644 sysdeps/i386/tst-gnu2-tls2.c
 rename sysdeps/{x86_64 => x86}/features-offsets.sym (89%)
 create mode 100644 sysdeps/x86/malloc-for-test.c
 create mode 100644 sysdeps/x86_64/dl-tlsdesc-dynamic.h
 create mode 100644 sysdeps/x86_64/dl-trampoline-save.h
 create mode 100644 sysdeps/x86_64/dl-trampoline-state.h