@samitouri / QOSamiQemu / commits / 4c681ba3b8

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. Signed-off-by: James Hilliard <james.hilliard1@gmail.com> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Helge Deller <deller@gmx.de>

James Hilliard committed Apr 9, 2026 at 20:00 UTC 4c681ba3b82d9a9f00a3f361399a1bb7612f3535
4 files changed +10 -1
linux-user/elfload.c
+3
@@ -1476,6 +1476,9 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
1476 /* Usual start for brk is after all sections of the main executable. */
1477 info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias);
1478 info->elf_flags = ehdr->e_flags;
1479 +#ifdef TARGET_MIPS
1480 + info->use_k0_tls = (ehdr->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_OCTEON;
1481 +#endif
1482
1483 prot_exec = PROT_EXEC;
1484 #ifdef TARGET_AARCH64
linux-user/mips/target_cpu.h
+5
@@ -35,7 +35,12 @@ static inline void cpu_clone_regs_parent(CPUMIPSState *env, unsigned flags)
35
36 static inline void cpu_set_tls(CPUMIPSState *env, target_ulong newtls)
37 {
38 + TaskState *ts = get_task_state(env_cpu(env));
39 +
40 env->active_tc.CP0_UserLocal = newtls;
41 + if (ts->info->use_k0_tls) {
42 + env->active_tc.gpr[26] = newtls;
43 + }
44 }
45
46 static inline abi_ulong get_sp_from_cpustate(CPUMIPSState *state)
linux-user/qemu.h
+1
@@ -65,6 +65,7 @@ struct image_info {
65 uint32_t note_flags;
66
67 #ifdef TARGET_MIPS
68 + bool use_k0_tls;
69 int fp_abi;
70 int interp_fp_abi;
71 #endif
linux-user/syscall.c
+1 -1
@@ -13216,7 +13216,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
13216 #ifdef TARGET_NR_set_thread_area
13217 case TARGET_NR_set_thread_area:
13218 #if defined(TARGET_MIPS)
13219 - cpu_env->active_tc.CP0_UserLocal = arg1;
13219 + cpu_set_tls(cpu_env, arg1);
13220 return 0;
13221 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
13222 return do_set_thread_area(cpu_env, arg1);