From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from second.openwall.net (second.openwall.net [193.110.157.125]) by inbox.vuxu.org (Postfix) with SMTP id 6E0B8290FA for ; Sat, 27 Jan 2024 01:12:07 +0100 (CET) Received: (qmail 3411 invoked by uid 550); 27 Jan 2024 00:09:51 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 3376 invoked from network); 27 Jan 2024 00:09:51 -0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BYRYSuJbMlNeWA0CGp9hIVDywSaLwL7UPiamsuZcgweZIlk4pnXQf/iPZh108ew88XmwhgS+EE263THBjXoogurYqKxWW9DfzkQnXyL2lvVyJIUW6pmxyGqBH8GnEUCGdnMj6Ti7nHDIsZMC+y5Xhq8fH+K50J1Bbw7KGAz10BqWPT4b/hTMNF2G7pPFgxMQ8OY9p2qgr/XwzzkAlvrAVfFpnuqbRHa/LUNA+CuihczVLynz141CLsVSzfXp25r17a4evofTsM98B+fIepZkwBEfH5uXJXs8cf19aRICmjSzy/T3yt59SxGfnvPiaEFbJw7lmNuNY4xxzPmZXbWgFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vMEaSJdyMgyALRFdGz6597vpAFKiLVLgbmawNJ1BPeY=; b=nw2/Gf0+cOg2o/MD78mTYGmdQvRxuoxp9v8X4v4J2LWRK/MIijL/6TDLviyCBEH9jT3eyazjdoFEVynyjjYcQ1HEnmRNc7IeEFDmngasRdz3KsdOr0JnJAqyafpPHYLXGre/Fp8t+YVXFv/7HwYNQzBR1IULa7O89qnukc8r2UQaCkum2GHEfVmF3IQpjpXTqGMMnrJgFbjFLgAdBvMfi7V82y710+NJ1qGj0nNh3wBQ00LUvp3ze6/XxVngXSgQJg3cIacnsGn+QuPIpnmeFVlJI88M3faaQqlegGnYB91vXJ5A93MFvU0/ZBURzDIDu4TgiLXeIBaclLmF2blFbQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none X-Gm-Message-State: AOJu0YxFv10iQeIhiZO4ezyRUzsmwT3fMoJ2dJz9TGzyUwAmclvnNL1e QE2D7yM3nZbDVsZBgRlLALgy6mw2ohSoB8+Fz6cL2ePovA+llHjLy9D287wcKZfqJwM5yrdNhev K1VV9Kr4Z0wpVZ+MHV35/4+jrPNk= X-Google-Smtp-Source: AGHT+IHqOQZlj8zd3YwFFBO9e20wz4tibwCGBnu1d81VmKZDI9r6LJMrwLHSeNEYgdGx0cW+lLMvxlYDNKYkvkslLhE= X-Received: by 2002:a05:690c:f96:b0:5ee:a910:107 with SMTP id df22-20020a05690c0f9600b005eea9100107mr793788ywb.21.1706314310220; Fri, 26 Jan 2024 16:11:50 -0800 (PST) References: <20230822173821.GW4163@brightrain.aerifal.cx> <20240121222831.GC4163@brightrain.aerifal.cx> <20240122000348.GD4163@brightrain.aerifal.cx> <20240123132631.GF4163@brightrain.aerifal.cx> In-Reply-To: From: Fangrui Song Date: Fri, 26 Jan 2024 16:11:39 -0800 X-Gmail-Original-Message-ID: Message-ID: To: musl@lists.openwall.com Cc: Tatsuyuki Ishi Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-TMN: [tEYxoKudX4yIdxZRaywm4KR61LV4uhg4] X-ClientProxiedBy: BN0PR02CA0004.namprd02.prod.outlook.com (2603:10b6:408:e4::9) To DS7PR12MB5765.namprd12.prod.outlook.com (2603:10b6:8:74::19) X-Microsoft-Original-Message-ID: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB5765:EE_|LV3PR12MB9144:EE_ X-MS-Office365-Filtering-Correlation-Id: 4596a3d7-84a6-4398-8c49-08dc1ecc8fe3 X-MS-Exchange-SLBlob-MailProps: Z68gl6A5j2/JPE+ICbLav/EN0BnMqOmzc/ZN81/iv9fw1dkYSMZLQcdPGBX0vUAplMQIrl1an3xlrHyST5puKW4rlG04ogFqKNsF7jQA//EDIvCy59r/XRW6gCFGJBJs9JfnrqCNbG/0632T3rPGPik3Sw4QPUQY0WenN13x/AA7RnQNtFpEybxTHJ1s2X6B6c3a9QvtJtXMKOQkZSBqg8/DhFUGWDifUd0FwCJ13mMrDO4ryIPMV1EfQRkqQGNgqm/DEXI75JZkhLQ1ov5aPGwJ3lmqOa0ol6MYKTEdvgihMdy3070Beho/+Vp6VlpD3ZkwE37P6uNHnvmOVx5LHV0oEHUJzVqhBoBYyuPdnjX6JvXqAHFIwiQPvNXgreK/p7SRNLPH9RerRdEDYK4L2mpzOIj1WXzjUnIY4DhbG+eHbeZktNEEaWgZ/kRBvzdiBUk1QDR4H0qGF8N7qST2tQDlpViXs7WB310aImL/AUYq765n0AM6LOmF9LQ8QE01IxFWuK7AhPsFBPfCTy8pYpakrqlDou1tHzkbPw7M8axEwgWlVG+158iJa5O7nT1CsvLDx9GaEsHG73FY8WOqDGk2F/BsFbyFmKDOtc9EIB/6GQRBtXTukMlaBWLqwhWvs3Ut5YjU1SLUI9KJ3h1xuaH87YMLTpt3rckx77q1yOtrga4xfZd5nraApx6Zta8smz0Tj2Z62KXN0xbhklEtIKzrIJ6He1yfpI5pIlWvwoQl9XsOsG9g5KqgeSped8DN/N2MkEOIf8JUksXxVIa0PlI/yoQbbjH13Dgz7fDXp4N+XdH7Babhjc/S8lZpxL9E X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Lop0hikDkBZ/W3r8kpmn2NuQgrkD2DiifaM0baDL1/brdjeFGbjIydNJoPgF8sEA1B1Wo7A1bwMgXgXiljmeWSYZkj4ePmAQRN36QdC5JGBw+iZlThyAttwmFkAUBGHCRIE+A4+rGqnd+uayuyv7Fa6wNm4O1e2uvG1RICC+R5aJVl0oy+AlqEHqp8cbqWpiPdx3HTAeHkCCcXGGx5Ab6E0dyNtOEuHC8S8wYhPFCj3n6nNt11xyv6sfVjy3HmhHbw1rxFqtpx16p0ClWRv0Q/Z5s75zLUTkvAAA/hn5XAdj70k5NJZTMQrNIEr2090Jd2IC1w2v69C01qs97poU2iAB1DSrFtXqQ0NKxcYD+S4QiwMT/EXqe8c4oTOKvaM8iRK/Q61h/6sYR37reDxJYLjfz7c7AdwvtGelrOtozTuKiiysT1brUfAGRe2/IlYujycIsLjme0mVqGfuk67SxhvygCqseipIxvLoK7ZgyuOnqpBpO/uDRcMDp3r7y7DZF+hp4zmmwkLd6KusKHTO7bJ/HQXs8XeuFx5G0lv80Hw95C0+IjAAan65CPxgRO7DZfADHWbENx5dM0+XjDoaMP19umZmAOzoPKul0COZoyPq21tPcTk/A5vA2TBgfM6lD5q4vflkGArIxgDKVyMcPw== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?TlFVamZhbDZIeXEyU2NsRFVOT2I0OVNFR2pxRkxEZHVjcnZqWWxXZGdHT0Vv?= =?utf-8?B?RmFQekdmSTN2WTcyMUxKRmxIYVFEQWEvOXFyOUl3V2Y5V0lYZ1I5NjJDMXlF?= =?utf-8?B?RjdhNFZ3NmhOMHE2djNwazRxTWVtL1RaNEZNN2tBc1NieTdFQ1hMU3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dUFlcXlremJuYzRRNHBJMmUxV3R4cnJ4RXB0?= =?utf-8?B?OVZLdzNQRlBvWVdJSjFzTmJuTTRHVUV3Sm14TTBSbkxaSG5aVXMxSTNadWpp?= =?utf-8?B?NFNzVU5YS2lCRG5obThuMTJ6Qlg0UC9sVFBwVTRyUVhKai9Wb282cVkvOFVz?= =?utf-8?B?d29YdFhGaEVHWDBwZ2MrMUpBUmdZV3R2NUhsZzAvODNvd0EvKzBvRk05MExW?= =?utf-8?B?ZEk4VTMxWXdEVHRabDhkRDBUTmE0Q045Z3N1aVo3UXVjdEFySU1JR1JLSWhU?= =?utf-8?B?Szdpb08ybXZ0ZEd2ZWZ0U3Y4NzNxSTZUM0p1RTAvZ1lSRlBsSnlNMzVvczFU?= =?utf-8?B?VU1JU3k2MDdHY0k0TkwvQkFLYXpvbTFya1BiVjNEbW9lcDBVSk03RG5ETk5Y?= =?utf-8?B?OG1DWjNmTmZrOVh2c1krdU1PbHBqTzFhR0tQU1FyMzlNcEtodmQ0K0xoMC9B?= =?utf-8?B?RUY1cVFFdlNBRHp5VnZuK2ZyTHp4Y3V0bzFjS3JUcDFucnl0b0VjUGxZL3Jp?= =?utf-8?Q?z76b5tFQX+qhE2D6eWE/tz51QMvjaU03OenveclDn/LK9M?= X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-71ea3.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 4596a3d7-84a6-4398-8c49-08dc1ecc8fe3 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB5765.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jan 2024 00:11:51.8586 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV3PR12MB9144 Subject: Re: [musl] Draft riscv64 TLSDESC implementation On Wed, Jan 24, 2024 at 10:48=E2=80=AFPM Fangrui Song wrote: > > On Tue, Jan 23, 2024 at 5:26=E2=80=AFAM Rich Felker wro= te: > > > > On Tue, Jan 23, 2024 at 12:52:01AM -0800, Fangrui Song wrote: > > > On Sun, Jan 21, 2024 at 7:41=E2=80=AFPM Tatsuyuki Ishi wrote: > > > > > > > > > On Jan 22, 2024, at 9:03, Rich Felker wrote: > > > > > > > > > > On Sun, Jan 21, 2024 at 03:48:55PM -0800, Fangrui Song wrote: > > > > >> On Sun, Jan 21, 2024 at 2:28=E2=80=AFPM Rich Felker wrote: > > > > >>> > > > > >>> On Tue, Aug 22, 2023 at 01:38:21PM -0400, Rich Felker wrote: > > > > >>>> The psABI work is not finalized, but based on the current stat= us of > > > > >>>> https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/373,= I think > > > > >>>> the attached is a valid (but untested) implementation of TLSDE= SC for > > > > >>>> riscv64. Actually activating it requires also adding the reloc= ation > > > > >>>> type macro to riscv64/reloc.h. > > > > >>>> > > > > >>>> If any rv folks could look it over and make sure I haven't mad= e any > > > > >>>> stupid asm errors or missed any obvious optimizations, that wo= uld help > > > > >>>> to quickly get this merged when the psABI is finalized. > > > > >>>> > > > > >>>> Rich > > > > >>> > > > > >>>> .text > > > > >>>> .global __tlsdesc_static > > > > >>>> .hidden __tlsdesc_static > > > > >>>> .type __tlsdesc_static,%function > > > > >>>> __tlsdesc_static: > > > > >>>> ld a0,8(a0) > > > > >>>> jr t0 > > > > >>>> > > > > >>>> .global __tlsdesc_dynamic > > > > >>>> .hidden __tlsdesc_dynamic > > > > >>>> .type __tlsdesc_dynamic,%function > > > > >>>> __tlsdesc_dynamic: > > > > >>>> add sp,sp,-8 > > > > >>>> sd t1,(sp) > > > > >>>> sd t2,8(sp) > > > > >>>> > > > > >>>> ld t2,-8(tp) # t2=3Ddtv > > > > >>>> > > > > >>>> ld a0,8(a0) # a0=3D&{modidx,off} > > > > >>>> ld t1,8(a0) # t1=3Doff > > > > >>>> ld a0,(a0) # a0=3Dmodidx > > > > >>>> sll a0,a0,3 # a0=3D8*modidx > > > > >>>> > > > > >>>> add a0,a0,t2 # a0=3Ddtv+8*modidx > > > > >>>> ld a0,(a0) # a0=3Ddtv[modidx] > > > > >>>> add a0,a0,t1 # a0=3Ddtv[modidx]+off > > > > >>>> sub a0,a0,tp # a0=3Ddtv[modidx]+off-tp > > > > >>>> > > > > >>>> ld t1,(sp) > > > > >>>> ld t2,8(sp) > > > > >>>> add sp,sp,8 > > > > >>>> jr t0 > > > > >>> > > > > >>> Any feedback on this? Offhand, it looks like adjusting sp by 8 = is > > > > >>> wrong and that should be 16. Anything else? Does anyone have re= cent > > > > >>> enough tooling to test this? > > > > >> > > > > >> Tatsuyuki, do you have links to the latest version of > > > > >> gcc/binutils/glibc patches? > > > > >> Downloading patches from these mailing lists is probably a large > > > > >> hurdle for many users, so having the relevant repositories onlin= e may > > > > >> help. > > > > >> > > > > >> mold has implemented RISC-V TLSDESC. > > > > >> > > > > >> On the LLVM side, I have reviewed > > > > >> https://github.com/llvm/llvm-project/pull/66915 and am waiting f= or it > > > > >> to land, before I can check the lld status. > > > > > > > > > > To test this, drop it in src/ldso/riscv64/tlsdesc.s, and add to > > > > > arch/riscv64/reloc.h: > > > > > > > > > > #define REL_TLSDESC R_RISCV_TLSDESC > > > > > > > > > > or whatever the reloc name is (I don't think it's in elf.h yet so= you > > > > > probably need to either add it there too or just hard-code the nu= mber > > > > > for testing). > > > > > > > > > > Updated version with the sp bugfix attached. > > > > > > > > The assembly looks fine to me. (It=E2=80=99s nice that musl don=E2= =80=99t need to bother with save/restore at all since DTVs are initialized = eagerly..) > > > > For the patches mentioned in the other thread, most tests were done= with glibc=E2=80=99s portable testsuite (tst-elf-*). > > > > If musl has a similar one, you should be able to run it with my GCC= / binutils fork (with either --with_tls=3Ddesc at configure time or -mtls-= dialect=3Ddesc at compile time). > > > > > > > > Tatsuyuki. > > > > > > > > > Rich > > > > > > > > > > > > > > > I have verified that the patch works using a runtime test under qemu-= user. > > > I use Paul Kirth's pending LLVM codegen/assembly patch and my pending > > > lld patch:) > > > > Thanks for running tests! > > > > > Compile b.c to bb.s. Replace general dynamic code sequences (e.g. > > > `la.tls.gd a0,tls0; call __tls_get_addr@plt`) with TLSDESC, e.g. > > > ``` > > > ..Ltlsdesc_hi0: > > > auipc a0, %tlsdesc_hi(tls0) > > > ld a1, %tlsdesc_load_lo(.Ltlsdesc_hi0)(a0) > > > addi a0, a0, %tlsdesc_add_lo(.Ltlsdesc_hi0) > > > jalr t0, 0(a1), %tlsdesc_call(.Ltlsdesc_hi0) > > > add a0, a0, tp > > > ``` > > > > Is this due to compiler not supporting generation of TLSDESC sequence > > yet? > > LLVM assembler and code gen support has been added > (https://github.com/llvm/llvm-project/pull/66915/files), > but the clang -mtls-dialect=3Ddesc part has not landed yet > (https://github.com/llvm/llvm-project/pull/79256). > > > If so, unlike a full test with compiler support, it does not test > > for the TLSDESC machinery honoring all the non-clobbering requirements > > of the ABI. But I think we can check by hand that that part of the ABI > > is honored. > > > > Rich > > Yes. > > % cat a.c > __thread int x; > void ext(int a, int b, int c, int d, int e, int f); > int foo(int a, int b, int c, int d, int e, int f) { > int ret =3D ++x; > ext(a, b, c, d, e, f); > return ret; > } > % clang --target=3Driscv64 -mtls-dialect=3Ddesc -O1 -S a.c -fpic -o desc.= s > # patched clang with -mtls-dialect=3D > > We can verify that a0~a5 (the resigers holding arguments) are not spilled= :) > > BTW, when you add static relocation types to include/elf.h, be sure > that the `_I` suffix is not included > (typos in the spec, which will be fixed by > https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/420) The aforementioned patches (LLVM, Clang, lld) have all landed in the main branch of llvm-project. I have filed cherry-pick requests for them to the upcoming 18.1 release (https://github.com/llvm/llvm-project/commits/release/18.x/) Rich, you may consider merging https://www.openwall.com/lists/musl/2024/01/26/9 ("elf.h: update RISC-V relocation types") first, so that your RISC-V TLSDESC patch does not need to touch include/elf.h Cheers