Skip to content

Commit

Permalink
runtime: use set_thread_area instead of modify_ldt on linux/386
Browse files Browse the repository at this point in the history
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 golang#14795.

Change-Id: I22a67d6119e5d24afaa01e2c2b8174991a8a9bf4
Reviewed-on: https://go-review.googlesource.com/31753
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
josharian authored and wheatman committed Jun 26, 2018
1 parent 0d6764e commit 7358423
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/runtime/sys_linux_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -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

/*
Expand All @@ -400,29 +409,38 @@ 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
CMPL AX, $0xfffff001
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
Expand Down

0 comments on commit 7358423

Please sign in to comment.