From patchwork Fri Jan 3 15:41:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Khrustalev X-Patchwork-Id: 103963 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 8C8A03858D33 for ; Fri, 3 Jan 2025 15:50:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C8A03858D33 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 903413858424 for ; Fri, 3 Jan 2025 15:43:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 903413858424 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 903413858424 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1735919029; cv=none; b=HOs7WcU5W2Zzjvd9TwIb0VgXTzBUqjzHZJ7Ztt9QsPwmw32VMF+MkPiM/4plX70y1w01xntnboVWM6RpNzTc1HTh4ZZdMIPp+mHWpCJcAKYBPI01a0OOSLs4Ly/7NmubiB1plNbNKBEhHKtoWQwxDHsgHMPbjbYNwiu6zT+Ahe4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1735919029; c=relaxed/simple; bh=WLwnqY4EUueRcBwL6vQObltzDOxC+LHVY2FCAUsn3TE=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=eOpJ317Po1VPkR7owQgOPHA9mn6Epnl3ggL90M7JkQyCzglyKiNAGcthBq7aGzoJ1kJQ4xTZdgRkcVsSzUZkExIFd9egnrxrUtQilNPpnCQ97KTsW72l2tk9rIeTf/zpr8t80YsvlCfAXhnceYifU0ciknjZS7Ksfti7VZ+kzMI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 903413858424 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 7EAF11480; Fri, 3 Jan 2025 07:44:17 -0800 (PST) Received: from udebian.localdomain (unknown [10.57.3.206]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3E8373F6A8; Fri, 3 Jan 2025 07:43:48 -0800 (PST) From: Yury Khrustalev To: libc-alpha@sourceware.org Cc: fweimer@redhat.com, adhemerval.zanella@linaro.org, codonell@redhat.com, nsz@gcc.gnu.org, schwab@suse.de, wilco.dijkstra@arm.com Subject: [PATCH v7 21/23] aarch64: Add GCS user-space allocation logic Date: Fri, 3 Jan 2025 15:41:39 +0000 Message-Id: <20250103154141.47731-22-yury.khrustalev@arm.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250103154141.47731-1-yury.khrustalev@arm.com> References: <20250103154141.47731-1-yury.khrustalev@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_SHORT, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org From: Szabolcs Nagy Allocate GCS based on the stack size, this can be used for coroutines (makecontext) and thread creation (if the kernel allows user allocated GCS). Reviewed-by: Wilco Dijkstra --- sysdeps/aarch64/Makefile | 3 +- sysdeps/aarch64/__alloc_gcs.c | 66 +++++++++++++++++++++++++++++++++++ sysdeps/aarch64/aarch64-gcs.h | 28 +++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 sysdeps/aarch64/__alloc_gcs.c create mode 100644 sysdeps/aarch64/aarch64-gcs.h diff --git a/sysdeps/aarch64/Makefile b/sysdeps/aarch64/Makefile index 74479604f2..4b7f8a5c07 100644 --- a/sysdeps/aarch64/Makefile +++ b/sysdeps/aarch64/Makefile @@ -71,7 +71,8 @@ sysdep_headers += sys/ifunc.h sysdep_routines += \ __mtag_tag_zero_region \ __mtag_tag_region \ - __arm_za_disable + __arm_za_disable \ + __alloc_gcs tests += \ tst-sme-jmp diff --git a/sysdeps/aarch64/__alloc_gcs.c b/sysdeps/aarch64/__alloc_gcs.c new file mode 100644 index 0000000000..8268c78abd --- /dev/null +++ b/sysdeps/aarch64/__alloc_gcs.c @@ -0,0 +1,66 @@ +/* AArch64 GCS allocation. + Copyright (C) 2024-2025 Free Software Foundation, Inc. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +#ifndef __NR_map_shadow_stack +# define __NR_map_shadow_stack 453 +#endif +#ifndef SHADOW_STACK_SET_TOKEN +# define SHADOW_STACK_SET_TOKEN (1UL << 0) +# define SHADOW_STACK_SET_MARKER (1UL << 1) +#endif + +static void * +map_shadow_stack (void *addr, size_t size, unsigned long flags) +{ + return (void *) INLINE_SYSCALL_CALL (map_shadow_stack, addr, size, flags); +} + +#define GCS_MAX_SIZE (1UL << 31) +#define GCS_ALTSTACK_RESERVE 160 + +void * +__alloc_gcs (size_t stack_size, void **ss_base, size_t *ss_size) +{ + size_t size = (stack_size / 2 + GCS_ALTSTACK_RESERVE) & -8UL; + if (size > GCS_MAX_SIZE) + size = GCS_MAX_SIZE; + + unsigned long flags = SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN; + void *base = map_shadow_stack (NULL, size, flags); + if (base == (void *) -1) + return NULL; + + *ss_base = base; + *ss_size = size; + + uint64_t *gcsp = (uint64_t *) ((char *) base + size); + /* Skip end of GCS token. */ + gcsp--; + /* Verify GCS cap token. */ + gcsp--; + if (((uint64_t)gcsp & 0xfffffffffffff000) + 1 != *gcsp) + { + __munmap (base, size); + return NULL; + } + /* Return the target GCS pointer for context switch. */ + return gcsp + 1; +} diff --git a/sysdeps/aarch64/aarch64-gcs.h b/sysdeps/aarch64/aarch64-gcs.h new file mode 100644 index 0000000000..162ef18726 --- /dev/null +++ b/sysdeps/aarch64/aarch64-gcs.h @@ -0,0 +1,28 @@ +/* AArch64 GCS (Guarded Control Stack) declarations. + This file is part of the GNU C Library. + Copyright (C) 2024-2025 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _AARCH64_GCS_H +#define _AARCH64_GCS_H + +#include +#include + +void *__alloc_gcs (size_t, void **, size_t *) attribute_hidden; + +#endif