From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id 8FF833858D1E for ; Wed, 17 May 2023 20:52:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8FF833858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gnu.org Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzO7t-0002Vp-U0; Wed, 17 May 2023 16:52:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=In-Reply-To:MIME-Version:References:Subject:To:From: Date; bh=ipoMOvarO/Cb/KT4Jddsw68UAdDfOTE0X4yMR6hu1b8=; b=RouAv2ExfeUCB946kTdX paHqD7EPluPGORX54uhpk7VFYTY92/oVRc7WSHQP0uFvzXh/6XqVQCjNoZv7G//mGURIvKNFzdZ+d LnNGPU+cYZ4qjIZqLXafcSY/fzLcTLWcCZ2ztUviKGs9526hiiC1CBqV56QQYVlyWQH8DRSGXVcy/ Z/GQ1hRBFxOTcHxMikaKBkrcRsILsLuIqmNj6lFxXzl+FCQqlojmw2mlv+yA6ON4pBTgXyQT0XYmZ aCXWfF8wsKNRvAi2nF8HqEoiWU20kxaKzBMlx9NyK2/jO5E6pXoRt/u4hhyXqlKqgm3JVtgeCP+Mw MYHmz/22S/ihYw==; Received: from 2a01cb008c251f00de41a9fffe47ec49.ipv6.abo.wanadoo.fr ([2a01:cb00:8c25:1f00:de41:a9ff:fe47:ec49] helo=begin.home) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzO7t-0008GN-N0; Wed, 17 May 2023 16:52:01 -0400 Received: from samy by begin.home with local (Exim 4.96) (envelope-from ) id 1pzO7s-006Ksj-27; Wed, 17 May 2023 22:52:00 +0200 Date: Wed, 17 May 2023 22:52:00 +0200 From: Samuel Thibault To: Sergey Bugaev Cc: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: Re: [PATCH 02/10] mach: Define MACHINE_THREAD_STATE_SETUP_CALL Message-ID: <20230517205200.hbrro26pddj6hkb3@begin> Mail-Followup-To: Sergey Bugaev , libc-alpha@sourceware.org, bug-hurd@gnu.org References: <20230517191436.73636-1-bugaevc@gmail.com> <20230517191436.73636-3-bugaevc@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230517191436.73636-3-bugaevc@gmail.com> Organization: I am not organized User-Agent: NeoMutt/20170609 (1.8.3) X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_PASS,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: Applied, thanks! Sergey Bugaev via Libc-alpha, le mer. 17 mai 2023 22:14:28 +0300, a ecrit: > The existing two macros, MACHINE_THREAD_STATE_SET_PC and > MACHINE_THREAD_STATE_SET_SP, can be used to set program counter and the > stack pointer registers in a machine-specific thread state structure. > > Useful as it is, this may not be enough to set up the thread to make a > function call, because the machine-specific ABI may impose additional > requirements. In particular, x86_64 ABI requires that upon function > entry, the stack pointer is 8 less than 16-byte aligned (sp & 15 == 8). > > To deal with this, introduce a new macro, > MACHINE_THREAD_STATE_SETUP_CALL (), which sets both stack and > instruction pointers, and also applies any machine-specific requirements > to make a valid function call. The default implementation simply > forwards to MACHINE_THREAD_STATE_SET_PC and MACHINE_THREAD_STATE_SET_SP, > but on x86_64 we additionally align the stack pointer. > > Signed-off-by: Sergey Bugaev > --- > Any ideas for a better name than PTR_ALIGN_DOWN_8_16? > > sysdeps/mach/thread_state.h | 9 +++++++++ > sysdeps/mach/x86/thread_state.h | 13 +++++++++++++ > 2 files changed, 22 insertions(+) > > diff --git a/sysdeps/mach/thread_state.h b/sysdeps/mach/thread_state.h > index 9fa3d4e1..431aaf82 100644 > --- a/sysdeps/mach/thread_state.h > +++ b/sysdeps/mach/thread_state.h > @@ -38,6 +38,15 @@ > #endif > #endif > > +/* Set up the thread state to call the given function on the given state. > + Dependning on architecture, this may imply more than just setting PC > + and SP. */ > +#ifndef MACHINE_THREAD_STATE_SETUP_CALL > +#define MACHINE_THREAD_STATE_SETUP_CALL(ts, stack, size, func) \ > + (MACHINE_THREAD_STATE_SET_PC (ts, func), \ > + MACHINE_THREAD_STATE_SET_SP (ts, stack, size)) > +#endif > + > /* This copies architecture-specific bits from the current thread to the new > thread state. */ > #ifndef MACHINE_THREAD_STATE_FIX_NEW > diff --git a/sysdeps/mach/x86/thread_state.h b/sysdeps/mach/x86/thread_state.h > index 5be0bec1..8c419515 100644 > --- a/sysdeps/mach/x86/thread_state.h > +++ b/sysdeps/mach/x86/thread_state.h > @@ -20,6 +20,7 @@ > #define _MACH_X86_THREAD_STATE_H 1 > > #include > +#include > > /* This lets the kernel define segments for a new thread. */ > #define MACHINE_NEW_THREAD_STATE_FLAVOR i386_THREAD_STATE > @@ -54,6 +55,18 @@ struct machine_thread_all_state > struct i386_float_state fpu; > }; > > +#ifdef __x86_64__ > +/* We're setting up the stack to perform a function call. On function entry, > + the stack pointer must be 8 bytes less than 16-aligned. */ > +#define PTR_ALIGN_DOWN_8_16(ptr) \ > + ({ uintptr_t __ptr = PTR_ALIGN_DOWN (ptr, 8); \ > + PTR_IS_ALIGNED (__ptr, 16) ? (__ptr - 8) : __ptr; }) > + > +#define MACHINE_THREAD_STATE_SETUP_CALL(ts, stack, size, func) \ > + ((ts)->SP = PTR_ALIGN_DOWN_8_16 ((uintptr_t) (stack) + (size)), \ > + (ts)->PC = (uintptr_t) func) > +#endif > + > #include > > #endif /* mach/x86/thread_state.h */ > -- > 2.40.1 > -- Samuel --- Pour une évaluation indépendante, transparente et rigoureuse ! Je soutiens la Commission d'Évaluation de l'Inria.