From nobody Mon Jan 17 13:04:37 2022 X-Original-To: freebsd-hackers@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 43BBE19539A9 for ; Mon, 17 Jan 2022 13:04:41 +0000 (UTC) (envelope-from gljennjohn@gmail.com) Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JcsZ01wBlz4VjW for ; Mon, 17 Jan 2022 13:04:40 +0000 (UTC) (envelope-from gljennjohn@gmail.com) Received: by mail-ed1-x533.google.com with SMTP id 30so65350594edv.3 for ; Mon, 17 Jan 2022 05:04:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=fYgIPMoQ5FJn9T55sANicdVyo/y1LTkE6QBrnrItpQM=; b=Zkfx2LFnlBfHQfLJDyI2oIctIM0XozW0KeFpfq/sTkIl9qOFB2MDWxYdyvNiubARbw evxhN8FQn4tHMMeTlYvFKc07Hww8IblUWI6h0Eediuh5aphrgNUss/830+bn2i/Jalel sgwDcpsj58t/jH12qlJQogLZ0P5uCP3GfM5krgX06ONiUPMDiLoA01UbVlEGniCAmtfY UhN/QPkRnlDXcUBuO5iG0Jcum4ye6i9vXtKRMmjHo94H+nnoMe9t9mrtXDvcQuNA835I hhGdaOYYnOHwgbjP2ar/qt5/W3rNCblVJIXH445C2Zvh6mL/EsA0jqYA4b1MubRMsMOo XRMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=fYgIPMoQ5FJn9T55sANicdVyo/y1LTkE6QBrnrItpQM=; b=lw5sK55olWneH61q278fjS4ceVcUsYesG0gIljG/Uhh5XsxQQqNFOTeX2wJKX4sG+g aPqxFBOZsMN5OoJJgH74QAbBuTo9/onNnINPdy8vsTV5RTs8A7s+QmpH/KOxste/cDIG Ea2J0KPCtQy2udFNILTDGlaiia6AuLgtt4Fwg8igBLLnLDTmDD9oSGTcWRILs94k4bWu nt8O95xyqE+Ziqtk52ZQCiRerofSCdlMdk3S25iQ0UKOqnFJ+lsVlOdXpD5c6BwyDNuz re2PGMEtE2qOYPWrtTIJfDf6ynsF9a99lTOUcsKiRQNbqEXv1CLqOFoGW3uGQz6BYUNf z6SA== X-Gm-Message-State: AOAM533/wkI/uMfISoE9/1DB1x3z0b6PwWQmDGwwe0PvXKRBKs9A2FBt uFIoT/n7n9nGcGkVPKNmRaw6pU3dcuQ= X-Google-Smtp-Source: ABdhPJxSr9ElWyr9b7yVAZda5W594gUwwWQG86z/0zIxJlRTfg0DOAtu1HRLtXsYl15lIB6UQLbiog== X-Received: by 2002:a50:c04c:: with SMTP id u12mr20228018edd.107.1642424679264; Mon, 17 Jan 2022 05:04:39 -0800 (PST) Received: from ernst.home (p5b3be0d9.dip0.t-ipconnect.de. [91.59.224.217]) by smtp.gmail.com with ESMTPSA id ky3sm4472346ejc.178.2022.01.17.05.04.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jan 2022 05:04:38 -0800 (PST) Date: Mon, 17 Jan 2022 14:04:37 +0100 From: Gary Jennejohn To: Damian Malarczyk Cc: "freebsd-hackers@freebsd.org" Subject: Re: amd64 syscall ABI (vs. Darwin) Message-ID: <20220117140437.13663e70@ernst.home> In-Reply-To: References: Reply-To: gljennjohn@gmail.com X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; amd64-portbld-freebsd14.0) List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4JcsZ01wBlz4VjW X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20210112 header.b=Zkfx2LFn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of gljennjohn@gmail.com designates 2a00:1450:4864:20::533 as permitted sender) smtp.mailfrom=gljennjohn@gmail.com X-Spamd-Result: default: False [-1.98 / 15.00]; HAS_REPLYTO(0.00)[gljennjohn@gmail.com]; TO_DN_EQ_ADDR_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; TO_DN_SOME(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2a00:1450:4000::/36:c]; FREEMAIL_FROM(0.00)[gmail.com]; REPLYTO_ADDR_EQ_FROM(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[gmail.com:+]; RCPT_COUNT_TWO(0.00)[2]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; NEURAL_HAM_SHORT(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-0.96)[-0.958]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20210112]; RECEIVED_SPAMHAUS_PBL(0.00)[91.59.224.217:received]; FROM_HAS_DN(0.00)[]; MIME_GOOD(-0.10)[text/plain]; FREEMAIL_REPLYTO(0.00)[gmail.com]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; TO_MATCH_ENVRCPT_SOME(0.00)[]; NEURAL_SPAM_LONG(0.98)[0.977]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::533:from]; MLMMJ_DEST(0.00)[freebsd-hackers]; RCVD_TLS_ALL(0.00)[] X-ThisMailContainsUnwantedMimeParts: N On Mon, 17 Jan 2022 12:41:59 +0000 Damian Malarczyk wrote: > Hello, > > I'm hacking on a toy project to run Darwin (MachO) binaries on FreeBSD. > Currently I'm at a stage of syscalls support, and I've noticed a difference in the amd64 ABI that I didn't expect. > > FreeBSD is changing values of some registers that aren't used as the syscall output. e.g., r8-r11 are changed, while r12-r15 don't seem to be affected. > That's not the case on Darwin, from what I've seen onlyrax, rdx used as syscall results are changed. > It looks like FreeBSD's syscalls calling convention is more like standard function calling, and r8-r11 should be always caller saved. > > At a first glance Darwin approach seems more optimal, as less registers get clobbered. Is there any specific reason why this isn't also the case on FreeBSD? > I'm also wondering where exactly the register values are changed. When I look at thetrapframe contents in the sv_set_syscall_retvalsystem vector callback the r8 register value is same as on the input, so it must be changed somewhere later. Does anyone know where exactly this happens? > > Thanks in advance for any tips. > This happens in the tree in various assembler files. The primary use of these registers appears to be for holding temporary data. Try running this in /usr/src: find -type f -name "*.S" -print0 | xargs -0 grep -e r8 -e r9 -e r10 -e r11 \ --mmap -l $1 > Here're the programs I used to test this behaviour: > - [FreeBSD](https://gist.github.com/dmcyk/11c29b2d5e5d3e04e5b954e43e12d384) > - [macOS](https://gist.github.com/dmcyk/ed1c6fcced78844c8e2e4a0fb3d18391) > > When you run the macOS version it wil write twice the number of arguments to stdout, FreeBSD will write the number only once followed by a 0, because r8 got overwritten. > > P.S. I'm relatively new to FreeBSD, and first time writing here on the mailing list so hello everyone :). > > - Damian -- Gary Jennejohn