From 09b43b77db383322d9dbcd0a47a0d4fedab7c6ca Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Sat, 26 Mar 2016 07:14:15 -0400 Subject: runtime: use set_thread_area instead of modify_ldt on linux/386 This is a cherry-pick of https://go-review.googlesource.com/21190 to release-branch-go1.4. Diff prepared by Ian Lance Taylor. linux/386 depends on modify_ldt system call, but recent Linux kernels can disable this system call. Any Go programs built as linux/386 crash with the message 'Trace/breakpoint trap'. The kernel config CONFIG_MODIFY_LDT_SYSCALL, which control enable/disable modify_ldt, is disabled on Amazon Linux 2016.03. This fixes this problem by using set_thread_area instead of modify_ldt on linux/386. Fixes #14795. Change-Id: I22a67d6119e5d24afaa01e2c2b8174991a8a9bf4 Reviewed-on: https://go-review.googlesource.com/31753 Reviewed-by: Brad Fitzpatrick --- src/runtime/sys_linux_386.s | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/runtime/sys_linux_386.s b/src/runtime/sys_linux_386.s index 0f6d4bbb5e..f9dec7597c 100644 --- a/src/runtime/sys_linux_386.s +++ b/src/runtime/sys_linux_386.s @@ -379,9 +379,18 @@ TEXT runtime·sigaltstack(SB),NOSPLIT,$-8 #define SEG_NOT_PRESENT 0x20 #define USEABLE 0x40 +// `-1` means the kernel will pick a TLS entry on the first setldt call, +// which happens during runtime init, and that we'll store back the saved +// entry and reuse that on subsequent calls when creating new threads. +DATA runtime·tls_entry_number+0(SB)/4, $-1 +GLOBL runtime·tls_entry_number(SB), NOPTR, $4 + // setldt(int entry, int address, int limit) +// We use set_thread_area, which mucks with the GDT, instead of modify_ldt, +// which would modify the LDT, but is disabled on some kernels. +// The name, setldt, is a misnomer, although we leave this name as it is for +// the compatibility with other platforms. TEXT runtime·setldt(SB),NOSPLIT,$32 - MOVL entry+0(FP), BX // entry MOVL address+4(FP), CX // base address /* @@ -400,18 +409,19 @@ TEXT runtime·setldt(SB),NOSPLIT,$32 ADDL $0x8, CX // address MOVL CX, 0(CX) + // get entry number + MOVL runtime·tls_entry_number(SB), DX + // set up user_desc LEAL 16(SP), AX // struct user_desc - MOVL BX, 0(AX) - MOVL CX, 4(AX) - MOVL $0xfffff, 8(AX) + MOVL DX, 0(AX) // unsigned int entry_number + MOVL CX, 4(AX) // unsigned long base_addr + MOVL $0xfffff, 8(AX) // unsigned int limit MOVL $(SEG_32BIT|LIMIT_IN_PAGES|USEABLE|CONTENTS_DATA), 12(AX) // flag bits - // call modify_ldt - MOVL $1, BX // func = 1 (write) - MOVL AX, CX // user_desc - MOVL $16, DX // sizeof(user_desc) - MOVL $123, AX // syscall - modify_ldt + // call set_thread_area + MOVL AX, BX // user_desc + MOVL $243, AX // syscall - set_thread_area CALL *runtime·_vdso(SB) // breakpoint on error @@ -419,10 +429,18 @@ TEXT runtime·setldt(SB),NOSPLIT,$32 JLS 2(PC) INT $3 - // compute segment selector - (entry*8+7) - MOVL entry+0(FP), AX + // read allocated entry number back out of user_desc + LEAL 16(SP), AX // get our user_desc back + MOVL 0(AX), AX + + // store entry number if the kernel allocated it + CMPL DX, $-1 + JNE 2(PC) + MOVL AX, runtime·tls_entry_number(SB) + + // compute segment selector - (entry*8+3) SHLL $3, AX - ADDL $7, AX + ADDL $3, AX MOVW AX, GS RET -- cgit v1.2.3-54-g00ecf