From nobody Fri May 3 05:35:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1559768905; cv=none; d=zoho.com; s=zohoarc; b=Z/jWpGIaYxFKA42D5LJv3a15/adBrgjgFYtaupHTT/ln0aQW03BS90kwvouIfjw/G+dT8xhKfB6WAbBFajiFFVYLjqaC3PcWyzX1LHpYlLNk+DffpOpXsOiU9dalfRIkvWqMC20oen9wYOz1J54qoNx1G+CeGcG4r/4eal69ri0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1559768905; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=VGvFhyg+EUZksh+VTROd86GaO53VcnzMT3R6u6jPQAY=; b=EaGDWASsQ+wTQIO7v9ywitJtMvQJi4j80ZV5Z0m3XJu+juXrj02kav80Ms8XFbrbULSABUKj2Y2iqMjZtBJBoixkGsATLS3Z3BAVBYb8hQvyS4R4gURq/atJSTsWCA0O71mInJcfShAlv+0kHVSdSgED1OVWXPpIYDMMj4bQA+I= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1559768905870102.46413619140856; Wed, 5 Jun 2019 14:08:25 -0700 (PDT) Received: from localhost ([127.0.0.1]:50070 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYd8y-0006Ea-Rv for importer@patchew.org; Wed, 05 Jun 2019 17:08:24 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48058) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYcyB-0006A4-Lx for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hYcyA-00087R-9p for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:15 -0400 Received: from mail-oi1-x243.google.com ([2607:f8b0:4864:20::243]:35086) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hYcyA-00085J-5G for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:14 -0400 Received: by mail-oi1-x243.google.com with SMTP id y6so59282oix.2 for ; Wed, 05 Jun 2019 13:57:14 -0700 (PDT) Received: from localhost.localdomain (200-56-192-86-cable.cybercable.net.mx. [200.56.192.86]) by smtp.gmail.com with ESMTPSA id f5sm7414155otl.51.2019.06.05.13.57.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jun 2019 13:57:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=VGvFhyg+EUZksh+VTROd86GaO53VcnzMT3R6u6jPQAY=; b=OoIeqPmZjHB3rL9pqVkgC//djxrxRKMbvTLI/lNj/jUKtpVZU24+DJwHFfk8rkB9Et 7VZQU9GvIwyFNUNQPGcg/9eZOzeoBIBK983oq242StsYqiZ//A15cr/9Afk+f4rxa4XB lYxTPX/3mEyYWtPYY23n1eqvuhL/Gxnl7drwrlPsrThBz2/Cdt+0Gbo7+JcSMohL32uc KmQOxb9+xAszytdDLr+/TbaKY/r27in9EW5QvLNcH6YJrgzYcKWQbNS8BSXrhoLfzJog UvSZaazsPsbp/cpi5i+WyC49uDxok4v9bzoPw685T020BnOWZkmNao9ZCerC8zeR0alH 8yLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=VGvFhyg+EUZksh+VTROd86GaO53VcnzMT3R6u6jPQAY=; b=da7zeKHRXv186AJGtKi31/l6LP6uxkKJAxU+xwy2HthsCANmrYOFySs0/jZE07WCrB OSAValXkCp/nsv3/2MXF/4KjWjsWH7VOfjmJt0YUqEhveuwErIR77dm+xW2dZlH4BSzX Ng+IUlmdASFRE9njkpwpE3loiiPC0k6KgJo30cfyKLpoC5+ICZcMhB43cgI07YAWRE3z bwmr1t+rM6m2ECQ/y+sF21WNucp/pczsg2p2Rd8Svfb2+pjZ/I35VmUss9TFKlM5U0CY oPi1U//eMYWYtTaFko7SlE+KSIeIRxSDbGqDu944ALjJJi0NWgvjcE3cJPW7zUwWIj2J zgvQ== X-Gm-Message-State: APjAAAWKTAXwM7DdoDm1lea6WWNIqOTnYVgf9v8Xb+JtAhBJQ3Vho9r1 lqQBFayInSGtsDcZQTyAlZ7ON43a9yulig== X-Google-Smtp-Source: APXvYqwDZlSJHeOHpiN3BzzPI+pVdj83ZReu7U2U08ZNM1z8tN8XzHbbvjthjb70jMuGvmVjoxaHgA== X-Received: by 2002:aca:ef43:: with SMTP id n64mr10168835oih.79.1559768233178; Wed, 05 Jun 2019 13:57:13 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 5 Jun 2019 15:57:01 -0500 Message-Id: <20190605205706.569-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605205706.569-1-richard.henderson@linaro.org> References: <20190605205706.569-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::243 Subject: [Qemu-devel] [PATCH v6 1/6] linux-user/aarch64: Reset btype for syscalls and signals X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, qemu-arm@nongnu.org, Dave.Martin@arm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The value of btype for syscalls is CONSTRAINED UNPREDICTABLE, so we need to make sure that the value is 0 before clone, fork, or syscall return. The kernel sets btype for the signal handler as if for a call. Signed-off-by: Richard Henderson --- linux-user/aarch64/cpu_loop.c | 7 +++++++ linux-user/aarch64/signal.c | 10 ++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/linux-user/aarch64/cpu_loop.c b/linux-user/aarch64/cpu_loop.c index 2f2f63e3e8..1f68b13168 100644 --- a/linux-user/aarch64/cpu_loop.c +++ b/linux-user/aarch64/cpu_loop.c @@ -86,6 +86,13 @@ void cpu_loop(CPUARMState *env) =20 switch (trapnr) { case EXCP_SWI: + /* + * The state of BTYPE on syscall entry is CONSTRAINED + * UNPREDICTABLE. The real kernel will need to tidy this up + * as well. Do this before syscalls so that the value is + * correct on return from syscall (especially clone & fork). + */ + env->btype =3D 0; ret =3D do_syscall(env, env->xregs[8], env->xregs[0], diff --git a/linux-user/aarch64/signal.c b/linux-user/aarch64/signal.c index f84a9cf28a..5605d404b3 100644 --- a/linux-user/aarch64/signal.c +++ b/linux-user/aarch64/signal.c @@ -506,10 +506,16 @@ static void target_setup_frame(int usig, struct targe= t_sigaction *ka, + offsetof(struct target_rt_frame_record, tramp); } env->xregs[0] =3D usig; - env->xregs[31] =3D frame_addr; env->xregs[29] =3D frame_addr + fr_ofs; - env->pc =3D ka->_sa_handler; env->xregs[30] =3D return_addr; + env->xregs[31] =3D frame_addr; + env->pc =3D ka->_sa_handler; + + /* Invoke the signal handler as if by indirect call. */ + if (cpu_isar_feature(aa64_bti, arm_env_get_cpu(env))) { + env->btype =3D 2; + } + if (info) { tswap_siginfo(&frame->info, info); env->xregs[1] =3D frame_addr + offsetof(struct target_rt_sigframe,= info); --=20 2.17.1 From nobody Fri May 3 05:35:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1559769292; cv=none; d=zoho.com; s=zohoarc; b=TrTN8S/LHMjwrcCfmNqRjqaHMgyqkiNT6G6b4aRUVwwssliMqfsfhUZTbxsJ9rX+JVqXkOqC3xShn5eysIZR6wJLJUFzLdj5jCed4GECXbwNeaKvYtzSzErI4B9V6foc7reSUvTZLwRDkM2vTrDFYJqJfsilSzFkyWxH0EIRsD0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1559769292; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=yjdMaLJKLWTE+OzLW+tTbL3wKIJtV8g2rfeZlQHS85U=; b=mRnSZXB3sZmnAy4y2olptDe8xIy4rxNeCk7pdoDDhNqfw6+AKIzQOSCYpoRZnz3Ua4ge0ogAGvt77P4W+2rC+xpQWBI3Hkd81VQS3ut2mL9OE5rHrtCie9T1GkdXKh0wTnRW+9pnWlxP4ZDySYOQEkPwnCHolaD9aoUFtyLqQsw= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1559769291899212.54105836362817; Wed, 5 Jun 2019 14:14:51 -0700 (PDT) Received: from localhost ([127.0.0.1]:50216 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYdEr-0002My-Te for importer@patchew.org; Wed, 05 Jun 2019 17:14:29 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48135) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYcyF-0006E5-NW for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hYcyC-0008Ep-Lf for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:19 -0400 Received: from mail-oi1-x242.google.com ([2607:f8b0:4864:20::242]:37450) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hYcyC-0008Bd-1m for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:16 -0400 Received: by mail-oi1-x242.google.com with SMTP id t76so50288oih.4 for ; Wed, 05 Jun 2019 13:57:15 -0700 (PDT) Received: from localhost.localdomain (200-56-192-86-cable.cybercable.net.mx. [200.56.192.86]) by smtp.gmail.com with ESMTPSA id f5sm7414155otl.51.2019.06.05.13.57.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jun 2019 13:57:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=yjdMaLJKLWTE+OzLW+tTbL3wKIJtV8g2rfeZlQHS85U=; b=xRhC6ePodcuGp4y5m8t0tuRQsw6cKEbUAcgBS1Vg+9SxsGMFJGSVB7dUYGEe5LAMbY GxgstnIjD4Cg7xKHF3xwkg+UtaFg2r9GYAug+7NGkvuUUIkH3S39nscETEtz0OffBhN+ I56KGWxA2OYQdFjT5JQ4vb8ze7qym8AqMa/DJYeKtuafMSaVR0abndxzfEI52lcdVB73 sAAqwQF3MAoYCtTTakk6nJI0GaZ/3kRlea1MDINwQtetzAhvMQYY1ccgkpu3KrV6fLwl 8ca9zQQ3Bl2qTXiwkgo6pKzr8Xp1kh1l/8amg7aO6RB9z/XH7DOvHVXGh4OV7nGGtc1X Mquw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=yjdMaLJKLWTE+OzLW+tTbL3wKIJtV8g2rfeZlQHS85U=; b=h662yTo5D80k1kZPYKLpLJz64rK32vhfpI6LSgRS1Unq3lsGi/+fQKGqN5w7FVZ/DE Lnn5hyG6eIA+bM1Melb14UzP3u4NamcnpOXayUPpSG6SvB1Q/pDxaOwwquRiuFQ4JtXb csgzj/aeeRi2PzMbDU4xNPJdSMheeXPWBIFk2Q03K4Lk8BanU+yP1B6r64g8EoI+bAeB v4N/SaOFWJUdtbIdUx9qZAfYTpT4jd2ANA0P+mtxge13moUv2/dNMCbkQWEGOBJzD8cg fUcSwVpXrYyS2iBPXEEXs3FaXW8DoXmB9t7OW3w3CCMo+0UNwJ3FqtObiwQ3galQ8T54 lPLg== X-Gm-Message-State: APjAAAUifAuDPzXxCDAi4lmKFilMJKCI4E/WpIzh2reJXhq9j50oDYHk KoRpmSDOiQFYIgD1IpQzFLOtugIsilMK8g== X-Google-Smtp-Source: APXvYqxf91QmTZK2CjntHpoIFRzKUqzbMUt/m1s4fCEWp8aixb1SF9xW5L0ufZwkGRtqyxqtPDQvOg== X-Received: by 2002:aca:318c:: with SMTP id x134mr8589922oix.125.1559768234794; Wed, 05 Jun 2019 13:57:14 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 5 Jun 2019 15:57:02 -0500 Message-Id: <20190605205706.569-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605205706.569-1-richard.henderson@linaro.org> References: <20190605205706.569-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::242 Subject: [Qemu-devel] [PATCH v6 2/6] linux-user: Validate mmap/mprotect prot value X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, qemu-arm@nongnu.org, Dave.Martin@arm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The kernel will return -EINVAL for bits set in the prot argument that are unknown or invalid. Previously we were simply cropping out the bits that we care about. Introduce validate_prot_to_pageflags to perform this check in a single place between the two syscalls. Differentiate between the target and host versions of prot. Compute the qemu internal page_flags value at the same time. Signed-off-by: Richard Henderson --- linux-user/mmap.c | 106 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 31 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index af41339d57..3117f57fd8 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -61,11 +61,38 @@ void mmap_fork_end(int child) pthread_mutex_unlock(&mmap_mutex); } =20 +/* + * Validate target prot bitmask. + * Return the prot bitmask for the host in *HOST_PROT. + * Return 0 if the target prot bitmask is invalid, otherwise + * the internal qemu page_flags (which will include PAGE_VALID). + */ +static int validate_prot_to_pageflags(int *host_prot, int prot) +{ + int valid =3D PROT_READ | PROT_WRITE | PROT_EXEC | TARGET_PROT_SEM; + int page_flags =3D (prot & PAGE_BITS) | PAGE_VALID; + + /* + * For the host, we need not pass anything except read/write/exec. + * While PROT_SEM is allowed by all hosts, it is also ignored, so + * don't bother transforming guest bit to host bit. Any other + * target-specific prot bits will not be understood by the host + * and will need to be encoded into page_flags for qemu emulation. + * + * TODO: We do not actually have to map guest pages as executable, + * since they will not be directly executed by the host. We only + * need to remember exec within page_flags. + */ + *host_prot =3D prot & (PROT_READ | PROT_WRITE | PROT_EXEC); + + return prot & ~valid ? 0 : page_flags; +} + /* NOTE: all the constants are the HOST ones, but addresses are target. */ -int target_mprotect(abi_ulong start, abi_ulong len, int prot) +int target_mprotect(abi_ulong start, abi_ulong len, int target_prot) { abi_ulong end, host_start, host_end, addr; - int prot1, ret; + int prot1, ret, page_flags, host_prot; =20 #ifdef DEBUG_MMAP printf("mprotect: start=3D0x" TARGET_ABI_FMT_lx @@ -75,56 +102,65 @@ int target_mprotect(abi_ulong start, abi_ulong len, in= t prot) prot & PROT_EXEC ? 'x' : '-'); #endif =20 - if ((start & ~TARGET_PAGE_MASK) !=3D 0) + if ((start & ~TARGET_PAGE_MASK) !=3D 0) { return -TARGET_EINVAL; + } + page_flags =3D validate_prot_to_pageflags(&host_prot, target_prot); + if (!page_flags) { + return -TARGET_EINVAL; + } len =3D TARGET_PAGE_ALIGN(len); end =3D start + len; if (!guest_range_valid(start, len)) { return -TARGET_ENOMEM; } - prot &=3D PROT_READ | PROT_WRITE | PROT_EXEC; - if (len =3D=3D 0) + if (len =3D=3D 0) { return 0; + } =20 mmap_lock(); host_start =3D start & qemu_host_page_mask; host_end =3D HOST_PAGE_ALIGN(end); if (start > host_start) { /* handle host page containing start */ - prot1 =3D prot; - for(addr =3D host_start; addr < start; addr +=3D TARGET_PAGE_SIZE)= { + prot1 =3D host_prot; + for (addr =3D host_start; addr < start; addr +=3D TARGET_PAGE_SIZE= ) { prot1 |=3D page_get_flags(addr); } if (host_end =3D=3D host_start + qemu_host_page_size) { - for(addr =3D end; addr < host_end; addr +=3D TARGET_PAGE_SIZE)= { + for (addr =3D end; addr < host_end; addr +=3D TARGET_PAGE_SIZE= ) { prot1 |=3D page_get_flags(addr); } end =3D host_end; } - ret =3D mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAG= E_BITS); - if (ret !=3D 0) + ret =3D mprotect(g2h(host_start), qemu_host_page_size, + prot1 & PAGE_BITS); + if (ret !=3D 0) { goto error; + } host_start +=3D qemu_host_page_size; } if (end < host_end) { - prot1 =3D prot; - for(addr =3D end; addr < host_end; addr +=3D TARGET_PAGE_SIZE) { + prot1 =3D host_prot; + for (addr =3D end; addr < host_end; addr +=3D TARGET_PAGE_SIZE) { prot1 |=3D page_get_flags(addr); } - ret =3D mprotect(g2h(host_end - qemu_host_page_size), qemu_host_pa= ge_size, - prot1 & PAGE_BITS); - if (ret !=3D 0) + ret =3D mprotect(g2h(host_end - qemu_host_page_size), + qemu_host_page_size, prot1 & PAGE_BITS); + if (ret !=3D 0) { goto error; + } host_end -=3D qemu_host_page_size; } =20 /* handle the pages in the middle */ if (host_start < host_end) { - ret =3D mprotect(g2h(host_start), host_end - host_start, prot); - if (ret !=3D 0) + ret =3D mprotect(g2h(host_start), host_end - host_start, host_prot= ); + if (ret !=3D 0) { goto error; + } } - page_set_flags(start, start + len, prot | PAGE_VALID); + page_set_flags(start, start + len, page_flags); mmap_unlock(); return 0; error: @@ -364,10 +400,11 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong si= ze, abi_ulong align) } =20 /* NOTE: all the constants are the HOST ones */ -abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, +abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, int flags, int fd, abi_ulong offset) { abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_l= en; + int page_flags, host_prot; =20 mmap_lock(); #ifdef DEBUG_MMAP @@ -402,6 +439,12 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, i= nt prot, goto fail; } =20 + page_flags =3D validate_prot_to_pageflags(&host_prot, target_prot); + if (!page_flags) { + errno =3D EINVAL; + goto fail; + } + /* Also check for overflows... */ len =3D TARGET_PAGE_ALIGN(len); if (!len) { @@ -467,14 +510,15 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, = int prot, /* Note: we prefer to control the mapping address. It is especially important if qemu_host_page_size > qemu_real_host_page_size */ - p =3D mmap(g2h(start), host_len, prot, + p =3D mmap(g2h(start), host_len, host_prot, flags | MAP_FIXED | MAP_ANONYMOUS, -1, 0); - if (p =3D=3D MAP_FAILED) + if (p =3D=3D MAP_FAILED) { goto fail; + } /* update start so that it points to the file position at 'offset'= */ host_start =3D (unsigned long)p; if (!(flags & MAP_ANONYMOUS)) { - p =3D mmap(g2h(start), len, prot, + p =3D mmap(g2h(start), len, host_prot, flags | MAP_FIXED, fd, host_offset); if (p =3D=3D MAP_FAILED) { munmap(g2h(start), host_len); @@ -508,19 +552,19 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, = int prot, /* msync() won't work here, so we return an error if write is possible while it is a shared mapping */ if ((flags & MAP_TYPE) =3D=3D MAP_SHARED && - (prot & PROT_WRITE)) { + (host_prot & PROT_WRITE)) { errno =3D EINVAL; goto fail; } - retaddr =3D target_mmap(start, len, prot | PROT_WRITE, + retaddr =3D target_mmap(start, len, target_prot | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (retaddr =3D=3D -1) goto fail; if (pread(fd, g2h(start), len, offset) =3D=3D -1) goto fail; - if (!(prot & PROT_WRITE)) { - ret =3D target_mprotect(start, len, prot); + if (!(host_prot & PROT_WRITE)) { + ret =3D target_mprotect(start, len, target_prot); assert(ret =3D=3D 0); } goto the_end; @@ -531,13 +575,13 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, = int prot, if (real_end =3D=3D real_start + qemu_host_page_size) { /* one single host page */ ret =3D mmap_frag(real_start, start, end, - prot, flags, fd, offset); + host_prot, flags, fd, offset); if (ret =3D=3D -1) goto fail; goto the_end1; } ret =3D mmap_frag(real_start, start, real_start + qemu_host_pa= ge_size, - prot, flags, fd, offset); + host_prot, flags, fd, offset); if (ret =3D=3D -1) goto fail; real_start +=3D qemu_host_page_size; @@ -546,7 +590,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, in= t prot, if (end < real_end) { ret =3D mmap_frag(real_end - qemu_host_page_size, real_end - qemu_host_page_size, end, - prot, flags, fd, + host_prot, flags, fd, offset + real_end - qemu_host_page_size - star= t); if (ret =3D=3D -1) goto fail; @@ -562,13 +606,13 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, = int prot, else offset1 =3D offset + real_start - start; p =3D mmap(g2h(real_start), real_end - real_start, - prot, flags, fd, offset1); + host_prot, flags, fd, offset1); if (p =3D=3D MAP_FAILED) goto fail; } } the_end1: - page_set_flags(start, start + len, prot | PAGE_VALID); + page_set_flags(start, start + len, page_flags); the_end: #ifdef DEBUG_MMAP printf("ret=3D0x" TARGET_ABI_FMT_lx "\n", start); --=20 2.17.1 From nobody Fri May 3 05:35:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1559769057; cv=none; d=zoho.com; s=zohoarc; b=N6QO+uyCqCZq6/ijNYiXj2NLVX6nLjNNumPOj4Zff/WaOMeUv3OLHh8l07UQ+txvIoH3NEEkfPx3diPp4SNuBUwLDKKk+YKcZZHojmhUDOgFZrHKU4B2VsaJ/nXUY0o44CEITmjIzHFLSK83Oug8UxnStebmerqm93ClGhwetRU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1559769057; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=ZCHRkKij7pQF0R23SJZo3xfE1Nbsenr9fc05DQb889w=; b=J6r37TFgb2lJzNDCCEE3+qQPwSeJgT+PBMOyiQhbJj/p0oqCPrXAlOHlQ7AeUE2gk/E8PLcyXWO9kkQ1R9BMRYrdS1Ev8+13NOBZcnqAjqBR/9MkE3sFAFfAO+rQe4KRlq+eZCRnQsAzKFeI2hHwy4hoUWF8MOR1qqAu0Ohrx9U= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1559769057768662.6044648079009; Wed, 5 Jun 2019 14:10:57 -0700 (PDT) Received: from localhost ([127.0.0.1]:50117 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYdBL-00080O-Jb for importer@patchew.org; Wed, 05 Jun 2019 17:10:51 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48136) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYcyF-0006E6-Nq for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hYcyD-0008JM-QM for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:19 -0400 Received: from mail-ot1-x342.google.com ([2607:f8b0:4864:20::342]:44391) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hYcyD-0008FC-GF for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:17 -0400 Received: by mail-ot1-x342.google.com with SMTP id b7so2050481otl.11 for ; Wed, 05 Jun 2019 13:57:17 -0700 (PDT) Received: from localhost.localdomain (200-56-192-86-cable.cybercable.net.mx. [200.56.192.86]) by smtp.gmail.com with ESMTPSA id f5sm7414155otl.51.2019.06.05.13.57.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jun 2019 13:57:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZCHRkKij7pQF0R23SJZo3xfE1Nbsenr9fc05DQb889w=; b=EovjxdH3T/hFo28fwmEY4teqysvGF3kpTosbWNfLqUUzpCL/8VbOr3ZGjDF1pTpI8B oPHN+5wO3+zDIAIxJRJsvvbX9gAgMWjpbzFADf9Lz5j9VO0UzuruG3h8PqUiUw1Zrt1f y5vuroM/lNzamL3UIV0WTJCsP1I3I3uy+wp9ctYDAGfNBw9ceHa4QzpkjTbsf5uderb6 97i3uUECZOUwXgKhu0RFjLZc6jrzMTogEwJhpyzDTujT6CbfcZi3HNljgukKMl8k4tZt IC2bgX9MZ3N5Y+mnhjF+D5pROkgOhWcOUBHAmwbcjaOjDCjP/JplwRwQ1zaLjJ7DoTCc ze2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZCHRkKij7pQF0R23SJZo3xfE1Nbsenr9fc05DQb889w=; b=LKP7UWYACCwNELBUfW+g6tZRzCOrijQTCTOoqpoBGLvZkFZN/k3kyVXqfhdSQiwMRM a3p6uwoqyrRcOxZKIBVVvyut/3yFzq4AA/+4QoLbOpp1NxxohV1/NElDbrCOaQczBJe9 rRUPrqa/tXvEvoBvkWuLttsTMjJi5jMoe2Hstv1ArxhaQ3KwouA+r6MdiuS3tfeKbt4O 6qO5keWJsiBDjThYplC8Phu0USGW5/mwxy+ksOvNPBiJ1pqwZVOvgLXMoDu80B776zQb YWIMDlZmpSxuY/r6J37PiYICSN5Nd5C8oPhdl1ElsnX2BaHLAfOF7FvpiI2nyiwI1mrf oZUQ== X-Gm-Message-State: APjAAAUUOEO6w/LBCyTVLkJsBSu5xLmgCveuoipJYHDThs+QhOPBWKQU h/DsR3F7PEAWEfDQVdU+u7Or10kAyZ5Ucg== X-Google-Smtp-Source: APXvYqy18XEoP8fT2E9NlREr0NTN1kEIaLyYkvPqMMbn+29/iX9lk1cEw+LcEtEBCitX6HwiJPOtow== X-Received: by 2002:a9d:7c8b:: with SMTP id q11mr4161202otn.161.1559768236302; Wed, 05 Jun 2019 13:57:16 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 5 Jun 2019 15:57:03 -0500 Message-Id: <20190605205706.569-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605205706.569-1-richard.henderson@linaro.org> References: <20190605205706.569-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::342 Subject: [Qemu-devel] [PATCH v6 3/6] linux-user: Set PAGE_TARGET_1 for TARGET_PROT_BTI X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, qemu-arm@nongnu.org, Dave.Martin@arm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Transform the prot bit to a qemu internal page bit, and save it in the page tables. Signed-off-by: Richard Henderson --- include/exec/cpu-all.h | 2 ++ linux-user/syscall_defs.h | 4 ++++ linux-user/mmap.c | 16 ++++++++++++++++ target/arm/translate-a64.c | 6 +++--- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h index da07ce311f..e65530acae 100644 --- a/include/exec/cpu-all.h +++ b/include/exec/cpu-all.h @@ -249,6 +249,8 @@ extern intptr_t qemu_host_page_mask; /* FIXME: Code that sets/uses this is broken and needs to go away. */ #define PAGE_RESERVED 0x0020 #endif +/* Target-specific bits that will be used via page_get_flags(). */ +#define PAGE_TARGET_1 0x0080 =20 #if defined(CONFIG_USER_ONLY) void page_dump(FILE *f); diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h index 7f141f699c..9a8a14e81e 100644 --- a/linux-user/syscall_defs.h +++ b/linux-user/syscall_defs.h @@ -1097,6 +1097,10 @@ struct target_winsize { #define TARGET_PROT_SEM 0x08 #endif =20 +#ifdef TARGET_AARCH64 +#define TARGET_PROT_BTI 0x10 +#endif + /* Common */ #define TARGET_MAP_SHARED 0x01 /* Share changes */ #define TARGET_MAP_PRIVATE 0x02 /* Changes are private */ diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 3117f57fd8..def64a41d5 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -85,6 +85,22 @@ static int validate_prot_to_pageflags(int *host_prot, in= t prot) */ *host_prot =3D prot & (PROT_READ | PROT_WRITE | PROT_EXEC); =20 +#ifdef TARGET_AARCH64 + /* + * The PROT_BTI bit is only accepted if the cpu supports the feature. + * Since this is the unusual case, don't bother checking unless + * the bit has been requested. If set and valid, record the bit + * within QEMU's page_flags as PAGE_TARGET_1. + */ + if (prot & TARGET_PROT_BTI) { + ARMCPU *cpu =3D ARM_CPU(thread_cpu); + if (cpu_isar_feature(aa64_bti, cpu)) { + valid |=3D TARGET_PROT_BTI; + page_flags |=3D PAGE_TARGET_1; + } + } +#endif + return prot & ~valid ? 0 : page_flags; } =20 diff --git a/target/arm/translate-a64.c b/target/arm/translate-a64.c index 092f0df3c4..5043344eba 100644 --- a/target/arm/translate-a64.c +++ b/target/arm/translate-a64.c @@ -14115,10 +14115,10 @@ static void disas_data_proc_simd_fp(DisasContext = *s, uint32_t insn) */ static bool is_guarded_page(CPUARMState *env, DisasContext *s) { -#ifdef CONFIG_USER_ONLY - return false; /* FIXME */ -#else uint64_t addr =3D s->base.pc_first; +#ifdef CONFIG_USER_ONLY + return page_get_flags(addr) & PAGE_TARGET_1; +#else int mmu_idx =3D arm_to_core_mmu_idx(s->mmu_idx); unsigned int index =3D tlb_index(env, mmu_idx, addr); CPUTLBEntry *entry =3D tlb_entry(env, mmu_idx, addr); --=20 2.17.1 From nobody Fri May 3 05:35:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1559768737; cv=none; d=zoho.com; s=zohoarc; b=miz1rseAZ3tzFdkLVCkvYikeZ2NEVXLtBxE/qTCu0DMI/B9KT6Fe4xAXltFPd3N0JWoLMqzQ7vXaXMoeSXyv191o8fJVoPdfeZz0lvqp74cam3BuOCOljJNDMEuFYbv4qr8zSHNICk6tZKkpNN1mSgdMH3+WjTyu+hFs9MeQbN4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1559768737; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=D6qDvZ2yUvasBaOhuVQfPLH+R2qFXowjyUNz4x5+OYc=; b=Act6dOfSvsh/ucer+/U8EM7bjM5hl/JcZd7YK8QVEN87RlcbYvdfiKvGQ6TRaEe47PJlquyuVaLzKrqrCjCZZiHJXaurc6xVhfv7MM+u6FZUrenLI2qaN0l9v3yTesV3M0kcLEaqux7Yd4V6bQKRKZx6sDs7K/LQKAzxWZDBOo0= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1559768737490809.0167359582263; Wed, 5 Jun 2019 14:05:37 -0700 (PDT) Received: from localhost ([127.0.0.1]:50000 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYd64-0003u2-T7 for importer@patchew.org; Wed, 05 Jun 2019 17:05:24 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48179) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYcyL-0006KN-3J for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hYcyH-00008m-Ik for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:23 -0400 Received: from mail-ot1-x343.google.com ([2607:f8b0:4864:20::343]:39336) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hYcyF-0008O5-Lg for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:19 -0400 Received: by mail-ot1-x343.google.com with SMTP id r21so2063880otq.6 for ; Wed, 05 Jun 2019 13:57:18 -0700 (PDT) Received: from localhost.localdomain (200-56-192-86-cable.cybercable.net.mx. [200.56.192.86]) by smtp.gmail.com with ESMTPSA id f5sm7414155otl.51.2019.06.05.13.57.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jun 2019 13:57:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=D6qDvZ2yUvasBaOhuVQfPLH+R2qFXowjyUNz4x5+OYc=; b=J8pG3bM/LjB0cDoDlSI5UGePw0K1tCSNuNaoyOjXBz8CrSjK5paG3fYBqJdIU9EjZT 6I9JHV/67BUUsAVCVFl98PLGWRfYjn6WDXXPZ/gvFMnj3q2seoK9QlrAIr10B2c/SK5k fhLhKuLSX7URKzTIAtnEw1l4W7p+YNEH6WxnD9ZosvxIcVOHpJVzJFtwPd7NSi98brsJ 29YPxB5Uyuvum2L+DAsJZaKkx6ezoz0kNfPUWZ21HyMFWsAGNc/GJEcqCFxKbxZj5RfE 1BwcKaCDhKf+WCMlHcDcTBe1Z2jGk4lZi5R0JKc6e+xUZYEr9suK8S8QO1yEbeBF28g4 mn7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=D6qDvZ2yUvasBaOhuVQfPLH+R2qFXowjyUNz4x5+OYc=; b=UZsWee8412HLjHPup9LQFrikuR7Tf2L4xIVHnkf8rDDCkDGBjM1rzj8/cX6NSOvKy0 LBmkASCADG5Hw7gsWc9dnSJYqicDQ1MIv3vEXKnwimseSHE1sV4zsENFTDt/lT6lM94j wWQuPTFl2Tmy7ZvdZMt8Q5GyR1NX2IMeqcTuAVl3juEiRWSpChby7cNMRZZiily2KLkc GUznKYjxVH6wQ4VqpsvCkFjKP3jcEOfjw+5AAE9Wtc88o+/LpGVEJ0NSoD6HKZMhbJuw lmCK8EFCQKV0rFDqjHtuzOlShEC+BjCT3+lRVzkNhgAHsYb+JUJ3zRiz9wKnKcgamYgO tH3g== X-Gm-Message-State: APjAAAW2Vrf5kdnWFGbbm6VXXz7JwN/NHpw9vPgma2VR4fJVNEyesO/e 5+U2XAr9EZHWuvdN6dw7eWO+a9NWzWZ4qA== X-Google-Smtp-Source: APXvYqwBcD2Gicrk7oIxpgPdL0Wu4yCSxx5ZXlZ7FdhHXh5LDaRCxw2VCbIX8elea933bf4J1keRPg== X-Received: by 2002:a05:6830:1698:: with SMTP id k24mr6033344otr.323.1559768237896; Wed, 05 Jun 2019 13:57:17 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 5 Jun 2019 15:57:04 -0500 Message-Id: <20190605205706.569-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605205706.569-1-richard.henderson@linaro.org> References: <20190605205706.569-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::343 Subject: [Qemu-devel] [PATCH v6 4/6] include/elf: Add defines related to notes for GNU systems X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, qemu-arm@nongnu.org, Dave.Martin@arm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This is a collection of related defines for notes, copied from glibc's . We're not going to use all of these right away, but it seemed foolish to cherry-pick only the ones we need now. Signed-off-by: Richard Henderson --- include/elf.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/include/elf.h b/include/elf.h index ea7708a4ea..6f3eada36f 100644 --- a/include/elf.h +++ b/include/elf.h @@ -1650,6 +1650,54 @@ typedef struct elf64_shdr { #define NT_ARM_HW_WATCH 0x403 /* ARM hardware watchpoint registe= rs */ #define NT_ARM_SYSTEM_CALL 0x404 /* ARM system call number */ =20 +/* Defined note types for GNU systems. */ + +#define NT_GNU_ABI_TAG 1 /* ABI information */ +#define NT_GNU_HWCAP 2 /* Synthetic hwcap information */ +#define NT_GNU_BUILD_ID 3 /* Build ID */ +#define NT_GNU_GOLD_VERSION 4 /* Version of ld.gold */ +#define NT_GNU_PROPERTY_TYPE_0 5 /* Program property */ + +/* Values used in GNU .note.gnu.property notes (NT_GNU_PROPERTY_TYPE_0). = */ + +#define GNU_PROPERTY_STACK_SIZE 1 +#define GNU_PROPERTY_NO_COPY_ON_PROTECTED 2 + +#define GNU_PROPERTY_LOPROC 0xc0000000 +#define GNU_PROPERTY_HIPROC 0xdfffffff +#define GNU_PROPERTY_LOUSER 0xe0000000 +#define GNU_PROPERTY_HIUSER 0xffffffff + +#define GNU_PROPERTY_X86_ISA_1_USED 0xc0000000 +#define GNU_PROPERTY_X86_ISA_1_NEEDED 0xc0000001 + +#define GNU_PROPERTY_X86_ISA_1_486 (1U << 0) +#define GNU_PROPERTY_X86_ISA_1_586 (1U << 1) +#define GNU_PROPERTY_X86_ISA_1_686 (1U << 2) +#define GNU_PROPERTY_X86_ISA_1_SSE (1U << 3) +#define GNU_PROPERTY_X86_ISA_1_SSE2 (1U << 4) +#define GNU_PROPERTY_X86_ISA_1_SSE3 (1U << 5) +#define GNU_PROPERTY_X86_ISA_1_SSSE3 (1U << 6) +#define GNU_PROPERTY_X86_ISA_1_SSE4_1 (1U << 7) +#define GNU_PROPERTY_X86_ISA_1_SSE4_2 (1U << 8) +#define GNU_PROPERTY_X86_ISA_1_AVX (1U << 9) +#define GNU_PROPERTY_X86_ISA_1_AVX2 (1U << 10) +#define GNU_PROPERTY_X86_ISA_1_AVX512F (1U << 11) +#define GNU_PROPERTY_X86_ISA_1_AVX512CD (1U << 12) +#define GNU_PROPERTY_X86_ISA_1_AVX512ER (1U << 13) +#define GNU_PROPERTY_X86_ISA_1_AVX512PF (1U << 14) +#define GNU_PROPERTY_X86_ISA_1_AVX512VL (1U << 15) +#define GNU_PROPERTY_X86_ISA_1_AVX512DQ (1U << 16) +#define GNU_PROPERTY_X86_ISA_1_AVX512BW (1U << 17) + +#define GNU_PROPERTY_X86_FEATURE_1_AND 0xc0000002 +#define GNU_PROPERTY_X86_FEATURE_1_IBT (1U << 0) +#define GNU_PROPERTY_X86_FEATURE_1_SHSTK (1U << 1) + +#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000 +#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI (1u << 0) +#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC (1u << 1) + /* * Physical entry point into the kernel. * --=20 2.17.1 From nobody Fri May 3 05:35:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1559768894; cv=none; d=zoho.com; s=zohoarc; b=LntLfbOcHPtIgg0wiHLTS/lNdQFzq71yQJoWRcjnFEX/E28aM/EqpKr+/+eKJs7KUwfth9dm9/UzJ9v5tPnogJjGrn7LdZ3DgAJ/r/7w4Kh6247L2bKFpARcKYNjeE4vg77N2V8fnqen3xYzHmGln+8jAQ1F0AL8OlTeNxts6yk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1559768894; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=jLJZRLTAFOL7hrqtZKjJ5DWxzgzN1ppLIoLLfp8TQiM=; b=eHlAUyeKWX7zLrP5cu/8QXLlIWpkZv3UIToNCM9e7CAbuejKMU+C9SrhdV/pOoW58p6mtiiKURlGr3A9G6fJIXVT7+XZUIKRtmdA27TkDB3+qVBAvbMUhUW+cpm52jMKyBB86yv+rt75ZwcD42zRwklexoCTNUwnnRfEJ4FZKjo= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1559768894323562.3703001876071; Wed, 5 Jun 2019 14:08:14 -0700 (PDT) Received: from localhost ([127.0.0.1]:50060 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYd8i-00060D-4p for importer@patchew.org; Wed, 05 Jun 2019 17:08:08 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48206) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYcyP-0006Ot-3e for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hYcyL-0000N2-8q for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:27 -0400 Received: from mail-ot1-x341.google.com ([2607:f8b0:4864:20::341]:41049) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hYcyI-00005E-4K for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:23 -0400 Received: by mail-ot1-x341.google.com with SMTP id 107so2060207otj.8 for ; Wed, 05 Jun 2019 13:57:20 -0700 (PDT) Received: from localhost.localdomain (200-56-192-86-cable.cybercable.net.mx. [200.56.192.86]) by smtp.gmail.com with ESMTPSA id f5sm7414155otl.51.2019.06.05.13.57.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jun 2019 13:57:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=jLJZRLTAFOL7hrqtZKjJ5DWxzgzN1ppLIoLLfp8TQiM=; b=nHI2qAKF8DVhaGVta3M8y934YeYSfHsN6rW2M4NWEqfw/rOXiJHAanGayDqYpgaJjg HxkPybsYI3s+FTHw7lI5YqsKIOnYC+HAO0qhnuc9WtZjwxGoIj9yy1+MB9bjHurIHXIi wk5BNxiNgLTpvrWq9bsKaz1p7A0pvIsU5Bd5+rZGGwhlLKctP7664+bEIK6FEX/PD0ZM 2pL/ek67fogTVm9CDhX4hzbZdZqzy4sXJYUdrNJeBROdGpUtIuNrruGp15DBmJm7UqPf lGqsZIYCkbHOIdoUd0i1+RqxT0O9VgMjOLs2P1I13srZl2VASGbeMgfzG/tY85+zstIK qS+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=jLJZRLTAFOL7hrqtZKjJ5DWxzgzN1ppLIoLLfp8TQiM=; b=sdrm3Tx4oGn5zKBVS5w6/7avDkOX8qdYjGl8/idt+CpDr6QGgfYJ1y2uEQlZWbsEUO haeBl0femjht5Y+1asKJ2P6sIhDC7nIn+XBIjbxZ4YnE4a6nn7lOl2LQoKj9KCegVE1K htLHZJ8q4mFqxfPBGheHojRg0tB3sCGsF5962UR3dxKEpESRgyXihGpJPD8WDfWhXNBJ zVYnz+O7iXFDsR9AkhT/iqEk4LZ1ufT9XIE411suy9XvSL8vaxwwUhTzuIcU+Fjh0IYA e4UDgo9TfmXsXo/wk+bvd6Z+Na8bWKJPgJVz70zsrClaiDInAV1w6wdn+2RsbXNl50Xk Fe7Q== X-Gm-Message-State: APjAAAXoNxbjGt+D6hVT+XlZFLLFSETn33n2GBodlm0f521VhHjswWb0 rJntqoVHTm9Tgj+j1CXAetdPQQI2V99Tyg== X-Google-Smtp-Source: APXvYqzPHMhHLYrh2ztWxc8CLuO9imbOQ8AkvPiVPRaq3o1KkaFWCtFPaYA42jrQX83XDvxpUleMrg== X-Received: by 2002:a9d:2f69:: with SMTP id h96mr11289872otb.366.1559768239896; Wed, 05 Jun 2019 13:57:19 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 5 Jun 2019 15:57:05 -0500 Message-Id: <20190605205706.569-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605205706.569-1-richard.henderson@linaro.org> References: <20190605205706.569-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::341 Subject: [Qemu-devel] [PATCH v6 5/6] linux-user: Parse NT_GNU_PROPERTY_TYPE_0 notes X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, qemu-arm@nongnu.org, Dave.Martin@arm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" For aarch64, this includes the GNU_PROPERTY_AARCH64_FEATURE_1_BTI bit, which indicates that the image should be mapped with guarded pages. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 83 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 8 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index a57b7049dd..1a12c60a33 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2253,7 +2253,7 @@ static void load_elf_image(const char *image_name, in= t image_fd, struct elfhdr *ehdr =3D (struct elfhdr *)bprm_buf; struct elf_phdr *phdr; abi_ulong load_addr, load_bias, loaddr, hiaddr, error; - int i, retval; + int i, retval, prot_exec =3D PROT_EXEC; const char *errmsg; =20 /* First of all, some simple consistency checks */ @@ -2288,17 +2288,78 @@ static void load_elf_image(const char *image_name, = int image_fd, loaddr =3D -1, hiaddr =3D 0; info->alignment =3D 0; for (i =3D 0; i < ehdr->e_phnum; ++i) { - if (phdr[i].p_type =3D=3D PT_LOAD) { - abi_ulong a =3D phdr[i].p_vaddr - phdr[i].p_offset; + struct elf_phdr *eppnt =3D phdr + i; + + if (eppnt->p_type =3D=3D PT_LOAD) { + abi_ulong a =3D eppnt->p_vaddr - eppnt->p_offset; if (a < loaddr) { loaddr =3D a; } - a =3D phdr[i].p_vaddr + phdr[i].p_memsz; + a =3D eppnt->p_vaddr + eppnt->p_memsz; if (a > hiaddr) { hiaddr =3D a; } ++info->nsegs; - info->alignment |=3D phdr[i].p_align; + info->alignment |=3D eppnt->p_align; + } else if (eppnt->p_type =3D=3D PT_NOTE) { +#ifdef TARGET_AARCH64 + /* + * Process NT_GNU_PROPERTY_TYPE_0. + * + * TODO: The only item that is AArch64 specific is the + * GNU_PROPERTY_AARCH64_FEATURE_1_AND processing at the end. + * If we were to ever process GNU_PROPERTY_X86_*, all of the + * code through checking the gnu0 magic number is sharable. + * But for now, since this *is* only used by AArch64, don't + * process the note elsewhere. + */ + const uint32_t gnu0_magic =3D const_le32('G' | 'N' << 8 | 'U' = << 16); + uint32_t note[7]; + + /* + * The note contents are 7 words, but depending on LP64 vs ILP= 32 + * there may be an 8th padding word at the end. Check for and + * read the minimum size. Further checks below will validate + * that the sizes of everything involved are as we expect. + */ + if (eppnt->p_filesz < sizeof(note)) { + continue; + } + if (eppnt->p_offset + eppnt->p_filesz <=3D BPRM_BUF_SIZE) { + memcpy(note, bprm_buf + eppnt->p_offset, sizeof(note)); + } else { + retval =3D pread(image_fd, note, sizeof(note), eppnt->p_of= fset); + if (retval !=3D sizeof(note)) { + goto exit_perror; + } + } +#ifdef BSWAP_NEEDED + for (i =3D 0; i < ARRAY_SIZE(note); ++i) { + bswap32s(note + i); + } +#endif + /* + * Check that this is a NT_GNU_PROPERTY_TYPE_0 note. + * Again, descsz includes padding. Full size validation + * awaits checking the final payload. + */ + if (note[0] !=3D 4 || /* namesz */ + note[1] < 12 || /* descsz */ + note[2] !=3D NT_GNU_PROPERTY_TYPE_0 || /* type */ + note[3] !=3D gnu0_magic) { /* name */ + continue; + } + /* + * Check for the BTI feature. If present, this indicates + * that all the executable pages of the binary should be + * mapped with PROT_BTI, so that branch targets are enforced. + */ + if (note[4] =3D=3D GNU_PROPERTY_AARCH64_FEATURE_1_AND && + note[5] =3D=3D 4 && + (note[6] & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) { + prot_exec |=3D TARGET_PROT_BTI; + } +#endif /* TARGET_AARCH64 */ } } =20 @@ -2358,9 +2419,15 @@ static void load_elf_image(const char *image_name, i= nt image_fd, abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr= _len; int elf_prot =3D 0; =20 - if (eppnt->p_flags & PF_R) elf_prot =3D PROT_READ; - if (eppnt->p_flags & PF_W) elf_prot |=3D PROT_WRITE; - if (eppnt->p_flags & PF_X) elf_prot |=3D PROT_EXEC; + if (eppnt->p_flags & PF_R) { + elf_prot |=3D PROT_READ; + } + if (eppnt->p_flags & PF_W) { + elf_prot |=3D PROT_WRITE; + } + if (eppnt->p_flags & PF_X) { + elf_prot |=3D prot_exec; + } =20 vaddr =3D load_bias + eppnt->p_vaddr; vaddr_po =3D TARGET_ELF_PAGEOFFSET(vaddr); --=20 2.17.1 From nobody Fri May 3 05:35:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1559769053; cv=none; d=zoho.com; s=zohoarc; b=hef+BytfvH1zNQwery+0hEkqKZ1i5Otb8t1bgmBwNTtTbLSomMHwe0YFHhYJOZ2ONQmrAGkyX47VfrR6Dy9XuIVyYCyJcMKaUD/g4pgiGdRTujsbkYpKvJkg1OvyXyGPuSB4gLkLoFmgnbi8Sh0CVmA/HZ8DYFIGsJvwPCo9jR0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1559769053; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=c6s3cz8Azngdshx9nMls9ehZ7rxdcfsNQPzwRD0wABs=; b=KFsHbFQOgf3fPx6lrwrYSaOGFGziO8RKxInhBFdBo/2pzzEYFskWpPyx5tYfeDPmganyBEo18pBQZEIEMYKGcwWonOzGzEbFmuzysw5wY8BJLPytOLLNf7s/N34XzrN9J2KVS1vS3aWaP4VwPEVnb+92lLzF8da02dutMwC1ExU= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1559769053264195.39507960644676; Wed, 5 Jun 2019 14:10:53 -0700 (PDT) Received: from localhost ([127.0.0.1]:50111 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYdBA-0007rR-Pj for importer@patchew.org; Wed, 05 Jun 2019 17:10:40 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48249) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYcyW-0006Ym-Rx for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hYcyT-0000k8-9Z for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:35 -0400 Received: from mail-ot1-x343.google.com ([2607:f8b0:4864:20::343]:36873) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hYcyP-0000CX-6P for qemu-devel@nongnu.org; Wed, 05 Jun 2019 16:57:31 -0400 Received: by mail-ot1-x343.google.com with SMTP id r10so2079085otd.4 for ; Wed, 05 Jun 2019 13:57:22 -0700 (PDT) Received: from localhost.localdomain (200-56-192-86-cable.cybercable.net.mx. [200.56.192.86]) by smtp.gmail.com with ESMTPSA id f5sm7414155otl.51.2019.06.05.13.57.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jun 2019 13:57:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=c6s3cz8Azngdshx9nMls9ehZ7rxdcfsNQPzwRD0wABs=; b=Pi3aTdqrqeBRGYuhTiHfiPC9dJQoKV2XJ37yN/xweKBwjVEvLlHt442LxzAWbr5mDD hHDFdAC+YGK+/sJAOM7ZuQMgz2EyltsxHz2v1//GtT/DI650Hp8gfUKN6NEUHmiGJ8/W W3V7UQRZHps8uhZ/6SrC9f3Hw32ugHwDhAOF/hvEWZMi7oczhrN7tzTCEdLKSKRSPv4t 68am+nU/xhCyOKxRvXzRibHYcDbkVM+6A6GJLdRVU4lKt5iKbLP4AwYbs5UAHLQql/ye i2GHYJbqTrzCfK6cT/8Y4Dh/5re747t4ZJP+lspe6bOachDWMFA7Bs2GvyQfGOGF1EOr ak3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=c6s3cz8Azngdshx9nMls9ehZ7rxdcfsNQPzwRD0wABs=; b=qgr0tgxoJBkztPsjFd70vCT4WYNeFVd0UG2V5cYgQ68HioGCPMpHhFgyI5afbakRXD Jk+QMMgebj2uUxWczdni0xwAF5LuBj83IJA4zurAGiY71N4thw5Ls4fTSAY3aVmkwQgy hZvK64IBoAVzc2qoF4D5u9nqUUimbcf/0T2FAsGZxccyISmFNCeXN1KIUwcu0W5hEg8R voaaGhW22mYMy0KSJGKwd5yTKa80CZNVuF8bS3sFaao4OCCz/k2wx9UgJ3DJliDF3mDc VGj4GHIc85cmsNN1LcGaqT5GQkKGLaABCrp8sXmgMEaNOfCu2VrWqHoWdruKtPpsnOxY R4fw== X-Gm-Message-State: APjAAAUn58NsubxSy9N8ssUSOdQRis8SUo2s7WtPq9VwLBtg+eWuE6nM tyoaS4zf6CqPKX+ZivQTfp35G616BmjTSA== X-Google-Smtp-Source: APXvYqwxE8XvdFWNRcxxFqCwbcQEQzs3ZF9tRjHfCGQDr3QLQYMbh/qZEZuWHorRrsJ1j59BdXhVdQ== X-Received: by 2002:a9d:10c:: with SMTP id 12mr11837547otu.123.1559768241530; Wed, 05 Jun 2019 13:57:21 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 5 Jun 2019 15:57:06 -0500 Message-Id: <20190605205706.569-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190605205706.569-1-richard.henderson@linaro.org> References: <20190605205706.569-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::343 Subject: [Qemu-devel] [PATCH v6 6/6] tests/tcg/aarch64: Add bti smoke test X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, qemu-arm@nongnu.org, Dave.Martin@arm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This will build with older toolchains, without the upstream support for -mbranch-protection. Such a toolchain will produce a warning in such cases, ld: warning: /tmp/ccyZt0kq.o: unsupported GNU_PROPERTY_TYPE (5) \ type: 0xc0000000 but the still places the note at the correct location in the binary for processing by the runtime loader. Signed-off-by: Richard Henderson --- tests/tcg/aarch64/bti-1.c | 77 +++++++++++++++++++++++++++++++ tests/tcg/aarch64/bti-crt.inc.c | 69 +++++++++++++++++++++++++++ tests/tcg/aarch64/Makefile.target | 3 ++ 3 files changed, 149 insertions(+) create mode 100644 tests/tcg/aarch64/bti-1.c create mode 100644 tests/tcg/aarch64/bti-crt.inc.c diff --git a/tests/tcg/aarch64/bti-1.c b/tests/tcg/aarch64/bti-1.c new file mode 100644 index 0000000000..2aee57ea7a --- /dev/null +++ b/tests/tcg/aarch64/bti-1.c @@ -0,0 +1,77 @@ +/* + * Branch target identification, basic notskip cases. + */ + +#include "bti-crt.inc.c" + +/* + * Work around lack of -mbranch-protection=3Dstandard in older toolchains. + * The signal handler is invoked by the kernel with PSTATE.BTYPE=3D2, which + * means that the handler must begin with a marker like BTI_C. + */ +asm("skip2_sigill1:\n\ + hint #34\n\ + b skip2_sigill2\n\ +.type skip2_sigill1,%function\n\ +.size skip2_sigill1,8"); + +extern void skip2_sigill1(int sig, siginfo_t *info, ucontext_t *uc) + __attribute__((visibility("hidden"))); + +static void __attribute__((used)) +skip2_sigill2(int sig, siginfo_t *info, ucontext_t *uc) +{ + uc->uc_mcontext.pc +=3D 8; + uc->uc_mcontext.pstate =3D 1; +} + +#define NOP "nop" +#define BTI_N "hint #32" +#define BTI_C "hint #34" +#define BTI_J "hint #36" +#define BTI_JC "hint #38" + +#define BTYPE_1(DEST) \ + asm("mov %0,#1; adr x16, 1f; br x16; 1: " DEST "; mov %0,#0" \ + : "=3Dr"(skipped) : : "x16") + +#define BTYPE_2(DEST) \ + asm("mov %0,#1; adr x16, 1f; blr x16; 1: " DEST "; mov %0,#0" \ + : "=3Dr"(skipped) : : "x16", "x30") + +#define BTYPE_3(DEST) \ + asm("mov %0,#1; adr x15, 1f; br x15; 1: " DEST "; mov %0,#0" \ + : "=3Dr"(skipped) : : "x15") + +#define TEST(WHICH, DEST, EXPECT) \ + do { WHICH(DEST); fail +=3D skipped ^ EXPECT; } while (0) + + +int main() +{ + int fail =3D 0; + int skipped; + + /* Signal-like with SA_SIGINFO. */ + signal_info(SIGILL, skip2_sigill1); + + TEST(BTYPE_1, NOP, 1); + TEST(BTYPE_1, BTI_N, 1); + TEST(BTYPE_1, BTI_C, 0); + TEST(BTYPE_1, BTI_J, 0); + TEST(BTYPE_1, BTI_JC, 0); + + TEST(BTYPE_2, NOP, 1); + TEST(BTYPE_2, BTI_N, 1); + TEST(BTYPE_2, BTI_C, 0); + TEST(BTYPE_2, BTI_J, 1); + TEST(BTYPE_2, BTI_JC, 0); + + TEST(BTYPE_3, NOP, 1); + TEST(BTYPE_3, BTI_N, 1); + TEST(BTYPE_3, BTI_C, 1); + TEST(BTYPE_3, BTI_J, 0); + TEST(BTYPE_3, BTI_JC, 0); + + return fail; +} diff --git a/tests/tcg/aarch64/bti-crt.inc.c b/tests/tcg/aarch64/bti-crt.in= c.c new file mode 100644 index 0000000000..bb363853de --- /dev/null +++ b/tests/tcg/aarch64/bti-crt.inc.c @@ -0,0 +1,69 @@ +/* + * Minimal user-environment for testing BTI. + * + * Normal libc is not built with BTI support enabled, and so could + * generate a BTI TRAP before ever reaching main. + */ + +#include +#include +#include +#include + +int main(void); + +void _start(void) +{ + exit(main()); +} + +void exit(int ret) +{ + register int x0 __asm__("x0") =3D ret; + register int x8 __asm__("x8") =3D __NR_exit; + + asm volatile("svc #0" : : "r"(x0), "r"(x8)); + __builtin_unreachable(); +} + +/* + * Irritatingly, the user API struct sigaction does not match the + * kernel API struct sigaction. So for simplicity, isolate the + * kernel ABI here, and make this act like signal. + */ +void signal_info(int sig, void (*fn)(int, siginfo_t *, ucontext_t *)) +{ + struct kernel_sigaction { + void (*handler)(int, siginfo_t *, ucontext_t *); + unsigned long flags; + unsigned long restorer; + unsigned long mask; + } sa =3D { fn, SA_SIGINFO, 0, 0 }; + + register int x0 __asm__("x0") =3D sig; + register void *x1 __asm__("x1") =3D &sa; + register void *x2 __asm__("x2") =3D 0; + register int x3 __asm__("x3") =3D sizeof(unsigned long); + register int x8 __asm__("x8") =3D __NR_rt_sigaction; + + asm volatile("svc #0" + : : "r"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x8) : "memory= "); +} + +/* + * Create the PT_NOTE that will enable BTI in the page tables. + * This will be created by the compiler with -mbranch-protection=3Dstandar= d, + * but as of 2019-03-29, this is has not been committed to gcc mainline. + * This will probably be in GCC10. + */ +asm(".section .note.gnu.property,\"a\"\n\ + .align 3\n\ + .long 4\n\ + .long 16\n\ + .long 5\n\ + .string \"GNU\"\n\ + .long 0xc0000000\n\ + .long 4\n\ + .long 1\n\ + .align 3\n\ + .previous"); diff --git a/tests/tcg/aarch64/Makefile.target b/tests/tcg/aarch64/Makefile= .target index 2bb914975b..21da3bc37f 100644 --- a/tests/tcg/aarch64/Makefile.target +++ b/tests/tcg/aarch64/Makefile.target @@ -18,4 +18,7 @@ run-fcvt: fcvt AARCH64_TESTS +=3D pauth-1 run-pauth-%: QEMU +=3D -cpu max =20 +AARCH64_TESTS +=3D bti-1 +bti-1: LDFLAGS +=3D -nostartfiles -nodefaultlibs -nostdlib + TESTS:=3D$(AARCH64_TESTS) --=20 2.17.1