Skip to contentSkip to conversation

linux-user/mips: sync k0 TLS for EF_MIPS_MACH_OCTEON userlands

PATCH In review James Hilliard 3 participants 3 messages Apr 10 – Apr 23

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 linux-user/mips: sync k0 TLS for EF_MIPS_MACH_OCTEON userlands

Cavium Octeon userspace is not following a generic MIPS Linux TLS ABI rule here. Older Octeon glibc uses the k0 register as the fast thread pointer, while newer Octeon2 and Octeon3 glibc variants use the normal rdhwr $29 path.

linux-user already updates CP0_UserLocal for cpu_set_tls() and TARGET_NR_set_thread_area, but it does not keep gpr[26] synchronized. That leaves EF_MIPS_MACH_OCTEON userlands able to complete set_thread_area() and still reach pthread startup or pthread_self() with a stale k0 value.

Use the existing MIPS ELF machine flags from linux-user/elfload.c and mirror CP0_UserLocal into gpr[26] only for EF_MIPS_MACH_OCTEON.

---
Changes v1 -> v2:
- only enable on EF_MIPS_MACH_OCTEON

---
 linux-user/elfload.c         | 3 +++
 linux-user/mips/target_cpu.h | 5 +++++
 linux-user/qemu.h            | 1 +
 linux-user/syscall.c         | 2 +-
 4 files changed, 10 insertions(+), 1 deletion(-)
linux-user/elfload.c +3 -0
index 59b543f740..0e757787d2 100644
@@ -1476,6 +1476,9 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
     /* Usual start for brk is after all sections of the main executable. */
     info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias);
     info->elf_flags = ehdr->e_flags;
+#ifdef TARGET_MIPS
+    info->use_k0_tls = (ehdr->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_OCTEON;
+#endif
 
     prot_exec = PROT_EXEC;
 #ifdef TARGET_AARCH64
linux-user/mips/target_cpu.h +5 -0
index c375616c55..2bbd0a81c5 100644
@@ -35,7 +35,12 @@ static inline void cpu_clone_regs_parent(CPUMIPSState *env, unsigned flags)
 
 static inline void cpu_set_tls(CPUMIPSState *env, target_ulong newtls)
 {
+    TaskState *ts = get_task_state(env_cpu(env));
+
     env->active_tc.CP0_UserLocal = newtls;
+    if (ts->info->use_k0_tls) {
+        env->active_tc.gpr[26] = newtls;
+    }
 }
 
 static inline abi_ulong get_sp_from_cpustate(CPUMIPSState *state)
linux-user/qemu.h +1 -0
index cfe5f45fc4..7f98fb2607 100644
@@ -65,6 +65,7 @@ struct image_info {
         uint32_t        note_flags;
 
 #ifdef TARGET_MIPS
+        bool            use_k0_tls;
         int             fp_abi;
         int             interp_fp_abi;
 #endif
linux-user/syscall.c +1 -1
index f4b74ad350..8e96cc26db 100644
@@ -13216,7 +13216,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
 #ifdef TARGET_NR_set_thread_area
     case TARGET_NR_set_thread_area:
 #if defined(TARGET_MIPS)
-      cpu_env->active_tc.CP0_UserLocal = arg1;
+      cpu_set_tls(cpu_env, arg1);
       return 0;
 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
       return do_set_thread_area(cpu_env, arg1);
Signature · 1 line
2.53.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 30 lines · James Hilliard

Cavium Octeon userspace is not following a generic MIPS Linux TLS ABI rule here. Older Octeon glibc uses the k0 register as the fast thread pointer, while newer Octeon2 and Octeon3 glibc variants use the normal rdhwr $29 path.

linux-user already updates CP0_UserLocal for cpu_set_tls() and TARGET_NR_set_thread_area, but it does not keep gpr[26] synchronized. That leaves EF_MIPS_MACH_OCTEON userlands able to complete set_thread_area() and still reach pthread startup or pthread_self() with a stale k0 value.

Use the existing MIPS ELF machine flags from linux-user/elfload.c and mirror CP0_UserLocal into gpr[26] only for EF_MIPS_MACH_OCTEON.

---
Changes v1 -> v2:
- only enable on EF_MIPS_MACH_OCTEON

---
  linux-user/elfload.c         | 3 +++
  linux-user/mips/target_cpu.h | 5 +++++
  linux-user/qemu.h            | 1 +
  linux-user/syscall.c         | 2 +-
  4 files changed, 10 insertions(+), 1 deletion(-)
linux-user/elfload.c +0 -0
index 59b543f740..0e757787d2 100644

linux-user/elfload.c static void load_elf_image(const char *image_name, const ImageSource *src, · line 1481

Quoted 6 lines
linux-user/elfload.c +3 -0
@@ -1476,6 +1476,9 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
      /* Usual start for brk is after all sections of the main executable. */
      info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias);
      info->elf_flags = ehdr->e_flags;
+#ifdef TARGET_MIPS
+    info->use_k0_tls = (ehdr->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_OCTEON;
+#endif

Note that we've just stored e_flags in info->elf_flags. Better placement for this is in mips/cpu_loop.c, init_main_thread.

Otherwise,

r~

#2 lore raw
Quoted 26 lines · Richard Henderson
Quoted 25 lines · James Hilliard

Cavium Octeon userspace is not following a generic MIPS Linux TLS ABI rule here. Older Octeon glibc uses the k0 register as the fast thread pointer, while newer Octeon2 and Octeon3 glibc variants use the normal rdhwr $29 path.

linux-user already updates CP0_UserLocal for cpu_set_tls() and TARGET_NR_set_thread_area, but it does not keep gpr[26] synchronized. That leaves EF_MIPS_MACH_OCTEON userlands able to complete set_thread_area() and still reach pthread startup or pthread_self() with a stale k0 value.

Use the existing MIPS ELF machine flags from linux-user/elfload.c and mirror CP0_UserLocal into gpr[26] only for EF_MIPS_MACH_OCTEON.

---
Changes v1 -> v2:
   - only enable on EF_MIPS_MACH_OCTEON
---
  linux-user/elfload.c         | 3 +++
  linux-user/mips/target_cpu.h | 5 +++++
  linux-user/qemu.h            | 1 +
  linux-user/syscall.c         | 2 +-

  4 files changed, 10 insertions(+), 1 deletion(-)
Quoted 14 lines

linux-user/elfload.c static void load_elf_image(const char *image_name, const ImageSource *src, · line 1481

Quoted 11 lines
linux-user/elfload.c +3 -0
index 59b543f740..0e757787d2 100644
@@ -1476,6 +1476,9 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
       /* Usual start for brk is after all sections of the main executable. */
       info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias);
       info->elf_flags = ehdr->e_flags;
+#ifdef TARGET_MIPS
+    info->use_k0_tls = (ehdr->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_OCTEON;
+#endif

Note that we've just stored e_flags in info->elf_flags. Better placement for this is in mips/cpu_loop.c, init_main_thread.

James, would you please send an updated patch with the suggested change?

Thanks!
Helge

Quoted 7 lines

Otherwise,

r~