From nobody Sun Jun 22 06:15:19 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4bQ1Cq67Czz5tLT1; Sun, 22 Jun 2025 06:15:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bQ1Cq1XFbz3kH5; Sun, 22 Jun 2025 06:15:19 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1750572919; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=M7PJ9zXpw4osA2ic3Ryxv+HhkB7x5OAyuATWpYw8S3A=; b=lxKsbxT/guewj3JG8uzcKWnrejuPBCHYiIUjzQ8nshIgDlZ2c/CRltvtvL2tc0BHvihmYO /fJ2X4kopYHgbG4i2D8JuOD4Xf+Y/dqvEWCjpIBtfOztreELD8eiT0LPIGQ5vDe6V5DHQS opOKbk3Ew6pEjTgxyvdzU5cVRNLvCHCu5UQb5dnzo/NAp3tkKzVF/tFQ0wz6XAlyPcsVVu MyM1/t1Ek5do7fShPGMKekJ9Z4sIbifZiqrE7DEOtZ9gksrVVGBsYuoUjmMwjg0VgiyS/T BtqQaEiPDx2S5wVWntFhSd25vwh6sQRVTj4Bog/apV8SSP2PaJOH8x3UwjaSNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1750572919; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=M7PJ9zXpw4osA2ic3Ryxv+HhkB7x5OAyuATWpYw8S3A=; b=N4+xHtqKPRCn/19adJj2SyUl25dXUpkdqOJcAlwHFHAo/StlcRFb+ZtKmHxo9Ivx5/2uko KRj0HY8JkDVFGfA+KeywYe7Pauiri/ikSpUQun/iZwxFRTcaMAp9vWnjmb/SQT9xxV2RxT Uu+3Qr4IwLy/6rrJ+oGkXGWN+o47idO7YE4y3uWk4t64fpFxlHHmv0xJNPPSAv1oYYPvgo Bt7NmHFrStTuuV109ZIfqhmdyGLudtNfh2kDmQxMQOSZBY4s512PjjgAJbNNEPDlZiSaYX BsB/bzHgDdfcG8Idg1VPRWqr77StK0wBSuW2grhscdRnazUq9cwsfuI0wKBKkg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1750572919; a=rsa-sha256; cv=none; b=yCMylfmKE7frhbabZBCJqxydSrtB84d5o3HSO2yvleW/1Zc3etyl5ZgG0P1VRDwjHGi3vr EpIMtu4HeCUi7OFD9fwjWRxF3AbOmv8YA1vV7WUuCbtCaIQVZg4f9QWqCodS71EEISVZ+T SG/n7wq2C9/lJzSgNnOALulIsPwQG7rbzLaFHmiLzN/r7VifjRD6J/ZkG7XJeBhLA5RaUx 9qjolP8+J3jrdRFkzGYflhPmJmZsT9Soqblk6ABpOaXuKLRF/kWTehHnMr21ncqUV/+jqq 795OnaIEeuYSTDGv43kbUW5AXoH0VfVpqd6SoXBAOyXb34ci8vc3qBQ9o6H7QA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4bQ1Cq10mYz14ws; Sun, 22 Jun 2025 06:15:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 55M6FJwR002963; Sun, 22 Jun 2025 06:15:19 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 55M6FJYl002960; Sun, 22 Jun 2025 06:15:19 GMT (envelope-from git) Date: Sun, 22 Jun 2025 06:15:19 GMT Message-Id: <202506220615.55M6FJYl002960@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 6b96e7a57317 - main - libthr/amd64: do not set THR_C_RUNTIME for thr_new() if the main thread did used AMD64_SET_TLSBASE List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6b96e7a5731795e76fe33df5a23edfb136f2e508 Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=6b96e7a5731795e76fe33df5a23edfb136f2e508 commit 6b96e7a5731795e76fe33df5a23edfb136f2e508 Author: Konstantin Belousov AuthorDate: 2025-06-01 07:00:18 +0000 Commit: Konstantin Belousov CommitDate: 2025-06-22 06:14:07 +0000 libthr/amd64: do not set THR_C_RUNTIME for thr_new() if the main thread did used AMD64_SET_TLSBASE It is up to the code that organizes the runtime to properly set the signal handler, and to set %fsbase if libthr signal handler is to be called. The change should leave the CPU state on the signal handler entry identical to what it was before introduction of TLSBASE, for code that provides its own startup and thread pointer, but still calls into libthr as a hack. Sponsored by: The FreeBSD Foundation MFC after: 1 week --- lib/libthr/arch/aarch64/include/pthread_md.h | 2 ++ lib/libthr/arch/amd64/Makefile.inc | 2 ++ lib/libthr/arch/amd64/amd64/thr_machdep.c | 48 ++++++++++++++++++++++++++++ lib/libthr/arch/amd64/include/pthread_md.h | 2 ++ lib/libthr/arch/arm/include/pthread_md.h | 2 ++ lib/libthr/arch/i386/include/pthread_md.h | 2 ++ lib/libthr/arch/powerpc/include/pthread_md.h | 2 ++ lib/libthr/arch/riscv/include/pthread_md.h | 2 ++ lib/libthr/thread/thr_create.c | 4 ++- lib/libthr/thread/thr_init.c | 2 +- lib/libthr/thread/thr_private.h | 2 ++ 11 files changed, 68 insertions(+), 2 deletions(-) diff --git a/lib/libthr/arch/aarch64/include/pthread_md.h b/lib/libthr/arch/aarch64/include/pthread_md.h index 305abed55d3c..4316955f1d3d 100644 --- a/lib/libthr/arch/aarch64/include/pthread_md.h +++ b/lib/libthr/arch/aarch64/include/pthread_md.h @@ -54,4 +54,6 @@ _thr_resolve_machdep(void) { } +#define __thr_setup_tsd(thread) _tcb_set((thread)->tcb) + #endif /* _PTHREAD_MD_H_ */ diff --git a/lib/libthr/arch/amd64/Makefile.inc b/lib/libthr/arch/amd64/Makefile.inc index f8013ea914ed..fe80e1a73cc9 100644 --- a/lib/libthr/arch/amd64/Makefile.inc +++ b/lib/libthr/arch/amd64/Makefile.inc @@ -3,3 +3,5 @@ # the extra context switch cost. This can measurably impact # performance when the application also does not use enough SSE. CFLAGS+=${CFLAGS_NO_SIMD} + +SRCS+= thr_machdep.c diff --git a/lib/libthr/arch/amd64/amd64/thr_machdep.c b/lib/libthr/arch/amd64/amd64/thr_machdep.c new file mode 100644 index 000000000000..d23e1689779c --- /dev/null +++ b/lib/libthr/arch/amd64/amd64/thr_machdep.c @@ -0,0 +1,48 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2025 The FreeBSD Foundation + * All rights reserved. + * + * This software was developed by Konstantin Belousov + * under sponsorship from the FreeBSD Foundation. + * + */ + +#define _WANT_P_OSREL +#include +#include +#include + +#include "libc_private.h" +#include "thr_private.h" + +void +__thr_setup_tsd(struct pthread *thread) +{ + void *base; + int error; + + if (__getosreldate() < P_OSREL_TLSBASE) { + amd64_set_tlsbase(thread->tcb); + return; + } + + /* + * Make tlsbase handling more compatible with code, like Go + * runtime, which wants to manage fsbase itself, and which do + * not need assistance in setting fsbase for signal handlers. + * + * If the main thread did not used amd64_set_tlsbase(), which + * means that rtld/libc was not utilized, do not use + * amd64_set_tlsbase() either. Also do not mark new threads + * as using C runtime with the THR_C_RUNTIME flag. + */ + error = sysarch(AMD64_GET_TLSBASE, &base); + if (error != 0 && errno == ESRCH) { + __thr_new_flags &= ~THR_C_RUNTIME; + amd64_set_fsbase(thread->tcb); + } else { + amd64_set_tlsbase(thread->tcb); + } +} diff --git a/lib/libthr/arch/amd64/include/pthread_md.h b/lib/libthr/arch/amd64/include/pthread_md.h index 85517c1aee70..02b73d90f006 100644 --- a/lib/libthr/arch/amd64/include/pthread_md.h +++ b/lib/libthr/arch/amd64/include/pthread_md.h @@ -57,4 +57,6 @@ _thr_resolve_machdep(void) { } +void __thr_setup_tsd(struct pthread *thread); + #endif diff --git a/lib/libthr/arch/arm/include/pthread_md.h b/lib/libthr/arch/arm/include/pthread_md.h index d616868bdee4..b90568e249ee 100644 --- a/lib/libthr/arch/arm/include/pthread_md.h +++ b/lib/libthr/arch/arm/include/pthread_md.h @@ -48,4 +48,6 @@ _get_curthread(void) return (NULL); } +#define __thr_setup_tsd(thread) _tcb_set((thread)->tcb) + #endif /* _PTHREAD_MD_H_ */ diff --git a/lib/libthr/arch/i386/include/pthread_md.h b/lib/libthr/arch/i386/include/pthread_md.h index 7e2c8d7330fc..43f84c3d0393 100644 --- a/lib/libthr/arch/i386/include/pthread_md.h +++ b/lib/libthr/arch/i386/include/pthread_md.h @@ -57,4 +57,6 @@ _thr_resolve_machdep(void) { } +#define __thr_setup_tsd(thread) _tcb_set((thread)->tcb) + #endif diff --git a/lib/libthr/arch/powerpc/include/pthread_md.h b/lib/libthr/arch/powerpc/include/pthread_md.h index a5bc0265ed3a..291f2d9350d9 100644 --- a/lib/libthr/arch/powerpc/include/pthread_md.h +++ b/lib/libthr/arch/powerpc/include/pthread_md.h @@ -54,4 +54,6 @@ _thr_resolve_machdep(void) { } +#define __thr_setup_tsd(thread) _tcb_set((thread)->tcb) + #endif /* _PTHREAD_MD_H_ */ diff --git a/lib/libthr/arch/riscv/include/pthread_md.h b/lib/libthr/arch/riscv/include/pthread_md.h index baddfe3ecb22..01dcc9c02b8c 100644 --- a/lib/libthr/arch/riscv/include/pthread_md.h +++ b/lib/libthr/arch/riscv/include/pthread_md.h @@ -61,4 +61,6 @@ _thr_resolve_machdep(void) { } +#define __thr_setup_tsd(thread) _tcb_set((thread)->tcb) + #endif /* _PTHREAD_MD_H_ */ diff --git a/lib/libthr/thread/thr_create.c b/lib/libthr/thread/thr_create.c index c66a5bb3bd21..e56dbcfe30e1 100644 --- a/lib/libthr/thread/thr_create.c +++ b/lib/libthr/thread/thr_create.c @@ -50,6 +50,8 @@ int __getosreldate(void); static int create_stack(struct pthread_attr *pattr); static void thread_start(struct pthread *curthread); +int __thr_new_flags = THR_C_RUNTIME; + __weak_reference(_pthread_create, pthread_create); int @@ -164,7 +166,7 @@ _pthread_create(pthread_t * __restrict thread, param.tls_size = sizeof(struct tcb); param.child_tid = &new_thread->tid; param.parent_tid = &new_thread->tid; - param.flags = THR_C_RUNTIME; + param.flags = __thr_new_flags; if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) param.flags |= THR_SYSTEM_SCOPE; if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c index bd1474a7c6e8..64cf7d004070 100644 --- a/lib/libthr/thread/thr_init.c +++ b/lib/libthr/thread/thr_init.c @@ -354,7 +354,7 @@ _libpthread_init(struct pthread *curthread) _thread_active_threads = 1; /* Setup the thread specific data */ - _tcb_set(curthread->tcb); + __thr_setup_tsd(curthread); if (first) { _thr_initial = curthread; diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h index 11afd74eea16..bca890829057 100644 --- a/lib/libthr/thread/thr_private.h +++ b/lib/libthr/thread/thr_private.h @@ -779,6 +779,8 @@ extern struct pthread *_single_thread __hidden; extern bool _thr_after_fork __hidden; +extern int __thr_new_flags; + /* * Function prototype definitions. */