From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 3933C385483A for ; Wed, 17 May 2023 19:14:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3933C385483A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-4f387d97dddso1425416e87.3 for ; Wed, 17 May 2023 12:14:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684350888; x=1686942888; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=jjtjP6SNJz4/32jzlkayjhTbYUKbetfqAZX5bBd03kU=; b=j/BahIO74bw4Yrr7AiC1pBxbRBhv6jMeVgxfeaAAwHKr9OLOXhNxjlvasCWjPOaXWO IfViitMNRCUC1pTAPlF9/9gx5J1SYhivyWsi9gVvw0e3hSebkj74CpF4jOsE52bwjPRH F10q6eWkOrVmOzloLnWQuokSziGV+eac/Ld5nQVx2mk3awi1pV+oD75YDSzdELmg42N2 sV0ul5Oe2NvTnGu86b3OgVBhUxm+KOXomJjhbG2wDQEb1875y4mF+eBXbISbMR4x7T2U PSoiVK2bPniWhHsmR1E/BrwwhMkDtTPfQPTw1tAhqjbiEV/3S9Q5gz2ZtOmgrAa2+beJ zylg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684350888; x=1686942888; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jjtjP6SNJz4/32jzlkayjhTbYUKbetfqAZX5bBd03kU=; b=Kf4ByLTTkdwML+CPu6HB8VztxXXsvI+VGK1gNfYoURQpNYozaTBZHRR6TXXk8w5xX5 bGkbvrJlJy0m6bDBcy2Hfpz4tVpUcnfKbSve65AM4sAD03XaZT7P6n2JnvLJjzzowp7s R1+tOFqG9okFRTldEcO5yB4hLjzWYJP6SCPv9IkXCfgh+ido2IASVA/OyXn/1V2coNIx gunSjXvdnweRDMrxV/2yI3BplCN99n8TwqZFDk0Op312+o3rU4XzpSwgLSHJLnrp7PsC eBJjzWpJu1YLYkqwOId9ygXgpu3YGDQ99ct8NCJIx6xsV/JMOdq5tbtscMFqU00EN5A8 4frg== X-Gm-Message-State: AC+VfDzVCJTOhk3vAMuvMpYUKSyC9zVXSDLAozzJrVMymd0qfgQwphFk YjljBkWIdUrQl8CwjL8kfFtl+pJga4o= X-Google-Smtp-Source: ACHHUZ6jRYsBPTkPhdU5zuHxNu4LjbQAQM+890Efsf0dzuMd0l/cZOl/uK+OEtHGGoVNBV4K9aOvTw== X-Received: by 2002:ac2:5934:0:b0:4ef:f4ef:a1cc with SMTP id v20-20020ac25934000000b004eff4efa1ccmr467330lfi.14.1684350888378; Wed, 17 May 2023 12:14:48 -0700 (PDT) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id o18-20020ac24352000000b004eed8de597csm3415744lfl.32.2023.05.17.12.14.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 12:14:47 -0700 (PDT) From: Sergey Bugaev To: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: [PATCH 07/10] hurd: Fix x86_64 _hurd_tls_fork Date: Wed, 17 May 2023 22:14:33 +0300 Message-Id: <20230517191436.73636-8-bugaevc@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230517191436.73636-1-bugaevc@gmail.com> References: <20230517191436.73636-1-bugaevc@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: It is illegal to call thread_get_state () on mach_thread_self (), so this codepath cannot be used as-is to fork the calling thread's TLS. Fortunately we can use THREAD_SELF (aka %fs:0x0) to find out the value of our fs_base without calling into the kernel. Fixes: f6cf701efc61c9ad910372bda14b9a235db310a8 "hurd: Implement TLS for x86_64" Checked on x86_64-gnu: fork () now works! Signed-off-by: Sergey Bugaev --- It would be nice if GNU Mach allowed thread_get_state (mach_thread_self (), i386_FSGS_BASE_STATE) like it already allows thread_set_state for this case. But even if gnumach adds suppport for this, doing it in userspace without an extra RPC is still better. sysdeps/mach/hurd/x86_64/tls.h | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/sysdeps/mach/hurd/x86_64/tls.h b/sysdeps/mach/hurd/x86_64/tls.h index 35dcef44..6487ed35 100644 --- a/sysdeps/mach/hurd/x86_64/tls.h +++ b/sysdeps/mach/hurd/x86_64/tls.h @@ -140,12 +140,25 @@ _hurd_tls_fork (thread_t child, thread_t orig, error_t err; struct i386_fsgs_base_state state; mach_msg_type_number_t state_count = i386_FSGS_BASE_STATE_COUNT; - err = __thread_get_state (orig, i386_FSGS_BASE_STATE, - (thread_state_t) &state, - &state_count); - if (err) - return err; - assert (state_count == i386_FSGS_BASE_STATE_COUNT); + + extern thread_t hurd_thread_self (void); + if (orig != hurd_thread_self ()) + { + err = __thread_get_state (orig, i386_FSGS_BASE_STATE, + (thread_state_t) &state, + &state_count); + if (err) + return err; + assert (state_count == i386_FSGS_BASE_STATE_COUNT); + } + else + { + /* It is illegal to call thread_get_state () on mach_thread_self (). + But we're only interested in the value of fs_base, and since we're + this thread, we know it points to our TCB. */ + state.fs_base = (unsigned long) THREAD_SELF; + state.gs_base = 0; + } return __thread_set_state (child, i386_FSGS_BASE_STATE, (thread_state_t) &state, -- 2.40.1