Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3617734pxb; Wed, 13 Oct 2021 09:28:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwj3nLVFP0T5r2a7x7bpiI0tK1+GZJcmcJVF0/lDB4knsxxzlBWC5S0YjbQA6B1O7GIOR6L X-Received: by 2002:a17:902:b188:b029:11b:1549:da31 with SMTP id s8-20020a170902b188b029011b1549da31mr215474plr.7.1634142521608; Wed, 13 Oct 2021 09:28:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634142521; cv=none; d=google.com; s=arc-20160816; b=en7UyS/eVW4tm0jfe+jAJ/4DflMgcbjHFzOXKCaACaPYZatTHljC8nNMTX6+CF4HFd B/Zmg+pEho5ty/SzJ2x1CMC5zbSO8Gfn0DcB6VKwjA4Kr9F7GqKUEdv4MdnyEJY/JYAv MyrgRuvYGBak319gZ7NXsAUCA4C22V6evi/WmHRObRKgiMd38dd6RCJ4eEi4ns30NTkv cmxVkHg0Tle7Kd5Inb6P7Y3bbhtFDnkB4YZ/4JWRCEBGGr/6ooU7X/3Gkk08GyEChzoz 9RBaVK65UYFIRNzVM55cwqT8szwwKxTWZOaUkX11X4SkDdwIwQHoShJffInm56LDdIYb IDjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature:dkim-signature; bh=lx+5wzFLRZqiTb+Uhg/iGQ2TCpoSNAvjoBnIgV2vO3M=; b=E89MIqlKb7S9Aij0wThYApRqaH4lkC9yrj/kpqyY+svC9l6wssDTg9NcNxawEnyS9t IpLPe0/WhKDdgyOo14nY02qnVQZ34Dnsmk6LVv75GSAcxe2YIbyKVcY6YmZvfMTZQCHO SCYfNf7RPMzdHe4wpiOmPrbJal1tLCsdbFGkP8jbeyrYlx6G/QhFwXF4GCshz3ky07bI 4pPv0OrA1GlMUJYqhSK5qYcsnGYBRFqnZfgaU7gtkeqSu32AEsMbpmTI0I13zk9Y6Xnr kV9UJrFChUuFgbFWGSI06ND0d03V/6sDcNG0UA69dhlwBCzvQbMW9M9lE+GCM/t7GtlJ 49/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=GeQRnnuf; dkim=neutral (no key) header.i=@suse.de header.b=78WoZBrH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 12si10068496pjl.32.2021.10.13.09.28.04; Wed, 13 Oct 2021 09:28:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=GeQRnnuf; dkim=neutral (no key) header.i=@suse.de header.b=78WoZBrH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236501AbhJMQ0k (ORCPT + 99 others); Wed, 13 Oct 2021 12:26:40 -0400 Received: from smtp-out1.suse.de ([195.135.220.28]:37596 "EHLO smtp-out1.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230312AbhJMQ0c (ORCPT ); Wed, 13 Oct 2021 12:26:32 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 982A821A23; Wed, 13 Oct 2021 16:24:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1634142268; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=lx+5wzFLRZqiTb+Uhg/iGQ2TCpoSNAvjoBnIgV2vO3M=; b=GeQRnnufRM9AV36kt5B0pHuaYeCHNLzKT1FxhEsHTOGiESfqfFCZM49yZEaylWxZVDRJDy 3jCDlmqaqNcRGtFUt5SYyeEWh4dP7fE0GHBlh7/FopROz7JXenw7liIJDhgMtzGKm+Zo/o aGuSFbxwFezOwCTKQy5ZQTNuTrK7tQ8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1634142268; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=lx+5wzFLRZqiTb+Uhg/iGQ2TCpoSNAvjoBnIgV2vO3M=; b=78WoZBrHk++fou7WWCKvkqM/mM1ROfej2zhFnWy25WOfar+xQg8eMcSF+kpTOZ++OISK2V IROGd27+EU/dbnAw== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 2DBA6A3B85; Wed, 13 Oct 2021 16:24:28 +0000 (UTC) Received: by wotan.suse.de (Postfix, from userid 10510) id 230006443; Wed, 13 Oct 2021 16:24:28 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by wotan.suse.de (Postfix) with ESMTP id 2118F6349; Wed, 13 Oct 2021 16:24:28 +0000 (UTC) Date: Wed, 13 Oct 2021 16:24:28 +0000 (UTC) From: Michael Matz To: Willy Tarreau cc: Borislav Petkov , Ammar Faizi , Paul Walmsley , Palmer Dabbelt , Albert Ou , Linux Kernel Mailing List , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" Subject: Re: [PATCH] tools/nolibc: x86: Remove `r8`, `r9` and `r10` from the clobber list In-Reply-To: <20211013142433.GB8557@1wt.eu> Message-ID: References: <20211012222311.578581-1-ammar.faizi@students.amikom.ac.id> <20211013125142.GD5485@1wt.eu> <20211013140723.GE5485@1wt.eu> <20211013142433.GB8557@1wt.eu> User-Agent: Alpine 2.20 (LSU 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, On Wed, 13 Oct 2021, Willy Tarreau wrote: > On Wed, Oct 13, 2021 at 04:20:55PM +0200, Borislav Petkov wrote: > > On Wed, Oct 13, 2021 at 04:07:23PM +0200, Willy Tarreau wrote: > > > Yes I agree with the "potentially" here. If it can potentially be (i.e. > > > the kernel is allowed by contract to later change the way it's currently > > > done) then we have to save them even if it means lower code efficiency. > > > > > > If, however, the kernel performs such savings on purpose because it is > > > willing to observe a stricter saving than the AMD64 ABI, we can follow > > > it but only once it's written down somewhere that it is by contract and > > > will not change. > > > > Right, and Micha noted that such a change to the document can be done. > > great. > > > And we're basically doing that registers restoring anyway, in POP_REGS. > > That's what I based my analysis on when I wanted to verify Ammar's > finding. I would tend to think that if we're burning cycles popping > plenty of registers it's probably for a reason, maybe at least a good > one, which is that it's the only way to make sure we're not leaking > internal kernel data! This is not a concern for kernel->kernel nor > user->user calls but for user->kernel calls it definitely is one, and > I don't think we could relax that series of pop without causing leaks > anyway. It might also be interesting to know that while the wording of the psABI was indeed intended to imply that all argument registers are potentially clobbered (like with normal calls) glibc's inline assembler to call syscalls relies on most registers to actually be preserved: # define REGISTERS_CLOBBERED_BY_SYSCALL "cc", "r11", "cx" ... #define internal_syscall6(number, arg1, arg2, arg3, arg4, arg5, arg6) \ ({ \ unsigned long int resultvar; \ TYPEFY (arg6, __arg6) = ARGIFY (arg6); \ TYPEFY (arg5, __arg5) = ARGIFY (arg5); \ TYPEFY (arg4, __arg4) = ARGIFY (arg4); \ TYPEFY (arg3, __arg3) = ARGIFY (arg3); \ TYPEFY (arg2, __arg2) = ARGIFY (arg2); \ TYPEFY (arg1, __arg1) = ARGIFY (arg1); \ register TYPEFY (arg6, _a6) asm ("r9") = __arg6; \ register TYPEFY (arg5, _a5) asm ("r8") = __arg5; \ register TYPEFY (arg4, _a4) asm ("r10") = __arg4; \ register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \ register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ asm volatile ( \ "syscall\n\t" \ : "=a" (resultvar) \ : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \ "r" (_a5), "r" (_a6) \ : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \ (long int) resultvar; \ }) Note in particular the missing clobbers or outputs of any of the argument regs. So, even though the psABI (might have) meant something else, as glibc is doing the above we in fact have a de-facto standard that the kernel can't clobber any of the argument regs. The wording and the linux x86-64 syscall implementation (and use in glibc) all come from the same time in 2001, so there never was a time when the kernel was not saving/restoring the arg registers, so it can't stop now. In effect this means the psABI should be clarified to explicitely say the the arg registers aren't clobbered, i.e. that the mentioned list of clobbered regs isn't inclusive but exclusive. I will do that. When I was discussing this with Boris earlier I hadn't yet looked at glibc use but only gave my interpretation from memory and reading. Obviously reality trumps anything like that :-) In short: Ammars initial claim: > Linux x86-64 syscall only clobbers rax, rcx and r11 (and "memory"). > > - rax for the return value. > - rcx to save the return address. > - r11 to save the rflags. > > Other registers are preserved. is accurate and I will clarify the psABI to make that explicit. Ciao, Michael.