From patchwork Wed May 17 19:14:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 69544 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5BEF538432F7 for ; Wed, 17 May 2023 19:15:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5BEF538432F7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684350908; bh=85WWdk4rPFjt4WrV3R0b3y8iHoN/8C0zYsFRSicwoHo=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=igpsQXY84nQezkDAgk/EEowSd8s8C9gQwP7CEEn0W4xCP8IG5BKYywOtOH30C5KxC 9GiWehkc7enM5t5QEiJJWjOyWuR+Z9hR/dVaxI2mRoiK4qpTHnNZNqD/Us+apnE6cu O07yhfiD/jfPlbTlPD/CT16PuPeS0R390PN33TKE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by sourceware.org (Postfix) with ESMTPS id 002663858C78 for ; Wed, 17 May 2023 19:14:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 002663858C78 Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-4f22908a082so1407453e87.1 for ; Wed, 17 May 2023 12:14:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684350881; x=1686942881; 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=85WWdk4rPFjt4WrV3R0b3y8iHoN/8C0zYsFRSicwoHo=; b=aYTnmcJy1pY8fCM2A3k9lJIOfiE/9aPi2BBxJNd1WKaNsfYC8MT2CdpKcep/wMPAmy 8AUHLtq3vzP1R/hfSdZbSRi/MupV/QGUHSu9BedufcHimqNhKQ8QquJaiiVGsL6TnXg/ S8zNInAxGXVehpkVGr7dEcHJOiQR2zx0yAKxH/4ZKrc2h4mwhYXBR+AatoHi2U9x2bdB mcEDLWGoUGX8iLXFResku51okSgn5CvCLHgZUL+8TJs+nyzcvkRfZ/tnMdv64Hkgwytl VL4lRjwjpm5AuxO6G9C08rRkvMxICfDzJ3MpnT7oKlPfVz9AjJa99IKndWbZ4w3hsUMO WYTw== X-Gm-Message-State: AC+VfDwkHYaCr2bre9qMT8kbuLe6QR/f371bzM4xrYp+iTPPN1IOwSMC FdXIxTmXZ2HVG3SFKVeMksS2sPOORik= X-Google-Smtp-Source: ACHHUZ7cGrZgU/dfc6RGJAZxJ1/3MWQLT4HwxS5bAd+mmXrUPVY5zMwMVaYr9g4IoclbotkV9TDGkQ== X-Received: by 2002:a05:6512:51a:b0:4ed:bf01:3ff3 with SMTP id o26-20020a056512051a00b004edbf013ff3mr428154lfb.43.1684350880916; Wed, 17 May 2023 12:14:40 -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.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 12:14:40 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: [PATCH 02/10] mach: Define MACHINE_THREAD_STATE_SETUP_CALL Date: Wed, 17 May 2023 22:14:28 +0300 Message-Id: <20230517191436.73636-3-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 X-Spam-Status: No, score=-11.1 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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 */