From 79816bbe94e50b37126955225ffa1d346ebf06a7 Mon Sep 17 00:00:00 2001 From: Brooks Davis Date: Thu, 15 Sep 2016 17:25:52 +0000 Subject: [PATCH] The TLS offset is a property of the process ABI. Move to a per-proc TLS offset rather than incorrectly keying off the presense of freebsd32 compability in the kernel. Reviewed by: adrian, sbruno Obtained from: CheriBSD Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D7843 --- sys/mips/include/proc.h | 1 + sys/mips/mips/cpu.c | 28 +++++++++++++++++----------- sys/mips/mips/freebsd32_machdep.c | 3 +++ sys/mips/mips/genassym.c | 7 +------ sys/mips/mips/pm_machdep.c | 3 +++ sys/mips/mips/swtch.S | 3 ++- sys/mips/mips/sys_machdep.c | 7 +------ sys/mips/mips/trap.c | 7 +------ sys/mips/mips/vm_machdep.c | 16 +++++++++------- 9 files changed, 38 insertions(+), 37 deletions(-) diff --git a/sys/mips/include/proc.h b/sys/mips/include/proc.h index efd5e795af82..320eeddc09e2 100644 --- a/sys/mips/include/proc.h +++ b/sys/mips/include/proc.h @@ -62,6 +62,7 @@ struct mdthread { int md_pc_count; /* performance counter */ int md_pc_spill; /* performance counter spill */ void *md_tls; + size_t md_tls_tcb_offset; /* TCB offset */ #ifdef CPU_CNMIPS struct octeon_cop2_state *md_cop2; /* kernel context */ struct octeon_cop2_state *md_ucop2; /* userland context */ diff --git a/sys/mips/mips/cpu.c b/sys/mips/mips/cpu.c index 05f546467e3d..e8f48dcb8f88 100644 --- a/sys/mips/mips/cpu.c +++ b/sys/mips/mips/cpu.c @@ -71,13 +71,12 @@ struct mips_cpuinfo cpuinfo; # define _LOAD_T0_MDTLS_A1 \ _ENCODE_INSN(OP_LD, A1, T0, 0, offsetof(struct thread, td_md.md_tls)) -# if defined(COMPAT_FREEBSD32) -# define _ADDIU_V0_T0_TLS_OFFSET \ - _ENCODE_INSN(OP_DADDIU, T0, V0, 0, (TLS_TP_OFFSET + TLS_TCB_SIZE32)) -# else -# define _ADDIU_V0_T0_TLS_OFFSET \ - _ENCODE_INSN(OP_DADDIU, T0, V0, 0, (TLS_TP_OFFSET + TLS_TCB_SIZE)) -# endif /* ! COMPAT_FREEBSD32 */ +# define _LOAD_T0_MDTLS_TCV_OFFSET_A1 \ + _ENCODE_INSN(OP_LD, A1, T1, 0, \ + offsetof(struct thread, td_md.md_tls_tcb_offset)) + +# define _ADDU_V0_T0_T1 \ + _ENCODE_INSN(0, T0, T1, V0, OP_DADDU) # define _MTC0_V0_USERLOCAL \ _ENCODE_INSN(OP_COP0, OP_DMT, V0, 4, 2) @@ -86,8 +85,14 @@ struct mips_cpuinfo cpuinfo; # define _LOAD_T0_MDTLS_A1 \ _ENCODE_INSN(OP_LW, A1, T0, 0, offsetof(struct thread, td_md.md_tls)) -# define _ADDIU_V0_T0_TLS_OFFSET \ - _ENCODE_INSN(OP_ADDIU, T0, V0, 0, (TLS_TP_OFFSET + TLS_TCB_SIZE)) + +# define _LOAD_T0_MDTLS_TCV_OFFSET_A1 \ + _ENCODE_INSN(OP_LW, A1, T1, 0, \ + offsetof(struct thread, td_md.md_tls_tcb_offset)) + +# define _ADDU_V0_T0_T1 \ + _ENCODE_INSN(0, T0, T1, V0, OP_ADDU) + # define _MTC0_V0_USERLOCAL \ _ENCODE_INSN(OP_COP0, OP_MT, V0, 4, 2) @@ -111,8 +116,9 @@ remove_userlocal_code(uint32_t *cpu_switch_code) if (instructp[0] == _JR_RA) panic("%s: Unable to patch cpu_switch().", __func__); if (instructp[0] == _LOAD_T0_MDTLS_A1 && - instructp[1] == _ADDIU_V0_T0_TLS_OFFSET && - instructp[2] == _MTC0_V0_USERLOCAL) { + instructp[1] == _LOAD_T0_MDTLS_TCV_OFFSET_A1 && + instructp[2] == _ADDU_V0_T0_T1 && + instructp[3] == _MTC0_V0_USERLOCAL) { instructp[0] = _JR_RA; instructp[1] = _NOP; break; diff --git a/sys/mips/mips/freebsd32_machdep.c b/sys/mips/mips/freebsd32_machdep.c index 4ed58e219a04..8ce15b460be8 100644 --- a/sys/mips/mips/freebsd32_machdep.c +++ b/sys/mips/mips/freebsd32_machdep.c @@ -61,6 +61,7 @@ #include #include #include +#include #include #include @@ -138,6 +139,8 @@ freebsd32_exec_setregs(struct thread *td, struct image_params *imgp, u_long stac * Clear extended address space bit for userland. */ td->td_frame->sr &= ~MIPS_SR_UX; + + td->td_md.md_tls_tcb_offset = TLS_TP_OFFSET + TLS_TCB_SIZE32; } int diff --git a/sys/mips/mips/genassym.c b/sys/mips/mips/genassym.c index 0396a591b559..eb289eeacdbf 100644 --- a/sys/mips/mips/genassym.c +++ b/sys/mips/mips/genassym.c @@ -74,12 +74,7 @@ ASSYM(TD_FLAGS, offsetof(struct thread, td_flags)); ASSYM(TD_LOCK, offsetof(struct thread, td_lock)); ASSYM(TD_MDFLAGS, offsetof(struct thread, td_md.md_flags)); ASSYM(TD_MDTLS, offsetof(struct thread, td_md.md_tls)); - -#if defined(__mips_n64) && defined(COMPAT_FREEBSD32) -ASSYM(TLS_TCB_OFFSET, (TLS_TP_OFFSET + TLS_TCB_SIZE32)); -#else -ASSYM(TLS_TCB_OFFSET, (TLS_TP_OFFSET + TLS_TCB_SIZE)); -#endif +ASSYM(TD_MDTLS_TCB_OFFSET, offsetof(struct thread, td_md.md_tls_tcb_offset)); ASSYM(U_PCB_REGS, offsetof(struct pcb, pcb_regs.zero)); ASSYM(U_PCB_CONTEXT, offsetof(struct pcb, pcb_context)); diff --git a/sys/mips/mips/pm_machdep.c b/sys/mips/mips/pm_machdep.c index 2b02ba9571a4..02f915eb512c 100644 --- a/sys/mips/mips/pm_machdep.c +++ b/sys/mips/mips/pm_machdep.c @@ -63,6 +63,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -466,6 +467,8 @@ exec_setregs(struct thread *td, struct image_params *imgp, u_long stack) if (PCPU_GET(fpcurthread) == td) PCPU_SET(fpcurthread, (struct thread *)0); td->td_md.md_ss_addr = 0; + + td->td_md.md_tls_tcb_offset = TLS_TP_OFFSET + TLS_TCB_SIZE; } int diff --git a/sys/mips/mips/swtch.S b/sys/mips/mips/swtch.S index b2e6d9a6bac1..aff18acb36cc 100644 --- a/sys/mips/mips/swtch.S +++ b/sys/mips/mips/swtch.S @@ -385,7 +385,8 @@ sw2: * remove_userlocal_code() in cpu.c. */ PTR_L t0, TD_MDTLS(a1) # Get TLS pointer - PTR_ADDIU v0, t0, TLS_TCB_OFFSET # Add TLS/TCB offset + PTR_L t1, TD_MDTLS_TCB_OFFSET(a1) # Get TLS/TCB offset + PTR_ADDU v0, t0, t1 MTC0 v0, MIPS_COP_0_USERLOCAL, 2 # write it to ULR for rdhwr j ra diff --git a/sys/mips/mips/sys_machdep.c b/sys/mips/mips/sys_machdep.c index 976a7f198abf..551b30504adf 100644 --- a/sys/mips/mips/sys_machdep.c +++ b/sys/mips/mips/sys_machdep.c @@ -69,13 +69,8 @@ sysarch(struct thread *td, struct sysarch_args *uap) * rdhwr trap() instruction handler. */ if (cpuinfo.userlocal_reg == true) { -#if defined(__mips_n64) && defined(COMPAT_FREEBSD32) mips_wr_userlocal((unsigned long)(uap->parms + - TLS_TP_OFFSET + TLS_TCB_SIZE32)); -#else - mips_wr_userlocal((unsigned long)(uap->parms + - TLS_TP_OFFSET + TLS_TCB_SIZE)); -#endif + td->td_md.md_tls_tcb_offset)); } return (0); case MIPS_GET_TLS: diff --git a/sys/mips/mips/trap.c b/sys/mips/mips/trap.c index aafe4f84e403..e071e34bef6f 100644 --- a/sys/mips/mips/trap.c +++ b/sys/mips/mips/trap.c @@ -909,12 +909,7 @@ trap(struct trapframe *trapframe) if (inst.RType.rd == 29) { frame_regs = &(trapframe->zero); frame_regs[inst.RType.rt] = (register_t)(intptr_t)td->td_md.md_tls; -#if defined(__mips_n64) && defined(COMPAT_FREEBSD32) - if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) - frame_regs[inst.RType.rt] += TLS_TP_OFFSET + TLS_TCB_SIZE32; - else -#endif - frame_regs[inst.RType.rt] += TLS_TP_OFFSET + TLS_TCB_SIZE; + frame_regs[inst.RType.rt] += td->td_md.md_tls_tcb_offset; trapframe->pc += sizeof(int); goto out; } diff --git a/sys/mips/mips/vm_machdep.c b/sys/mips/mips/vm_machdep.c index d71ac2e3ec96..c364e3483da5 100644 --- a/sys/mips/mips/vm_machdep.c +++ b/sys/mips/mips/vm_machdep.c @@ -154,6 +154,7 @@ cpu_fork(register struct thread *td1,register struct proc *p2, */ td2->td_md.md_tls = td1->td_md.md_tls; + td2->td_md.md_tls_tcb_offset = td1->td_md.md_tls_tcb_offset; td2->td_md.md_saved_intr = MIPS_SR_INT_IE; td2->td_md.md_spinlock_count = 1; #ifdef CPU_CNMIPS @@ -494,15 +495,16 @@ int cpu_set_user_tls(struct thread *td, void *tls_base) { +#if defined(__mips_n64) && defined(COMPAT_FREEBSD32) + if (td->td_proc && SV_PROC_FLAG(td->td_proc, SV_ILP32)) + td->td_md.md_tls_tcb_offset = TLS_TP_OFFSET + TLS_TCB_SIZE32; + else +#endif + td->td_md.md_tls_tcb_offset = TLS_TP_OFFSET + TLS_TCB_SIZE; td->td_md.md_tls = (char*)tls_base; if (td == curthread && cpuinfo.userlocal_reg == true) { -#if defined(__mips_n64) && defined(COMPAT_FREEBSD32) - mips_wr_userlocal((unsigned long)tls_base + TLS_TP_OFFSET + - TLS_TCB_SIZE32); -#else - mips_wr_userlocal((unsigned long)tls_base + TLS_TP_OFFSET + - TLS_TCB_SIZE); -#endif + mips_wr_userlocal((unsigned long)tls_base + + td->td_md.md_tls_tcb_offset); } return (0);