From patchwork Thu Jul 28 01:01:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 14062 Received: (qmail 107806 invoked by alias); 28 Jul 2016 01:01:32 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 107786 invoked by uid 89); 28 Jul 2016 01:01:31 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=zap, Hx-languages-length:2801 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 28 Jul 2016 01:01:30 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0437381228 for ; Thu, 28 Jul 2016 01:01:29 +0000 (UTC) Received: from cascais.lan (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u6S11Qsh027476 for ; Wed, 27 Jul 2016 21:01:28 -0400 From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 2/6] x32 Fast tracepoints: Customize jump pad address Date: Thu, 28 Jul 2016 02:01:21 +0100 Message-Id: <1469667685-10848-3-git-send-email-palves@redhat.com> In-Reply-To: <1469667685-10848-1-git-send-email-palves@redhat.com> References: <1469667685-10848-1-git-send-email-palves@redhat.com> MAP_32BIT is ignored on x32, meaning the jump pad can end up somewhere between 2GB and 4GB, too far away from the executable for 5-byte relative jumps (JMP rel32). So on x32, try explicitly placing the jump pad near the middle of the available address space. gdb/gdbserver/ChangeLog: yyyy-mm-dd Pedro Alves * linux-amd64-ipa.c (alloc_jump_pad_buffer) [__ILP32__]: Try allocating around 0x80000000. --- gdb/gdbserver/linux-amd64-ipa.c | 52 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/gdb/gdbserver/linux-amd64-ipa.c b/gdb/gdbserver/linux-amd64-ipa.c index 15d08ff..0625b85 100644 --- a/gdb/gdbserver/linux-amd64-ipa.c +++ b/gdb/gdbserver/linux-amd64-ipa.c @@ -206,14 +206,57 @@ get_ipa_tdesc (int idx) "unknown ipa tdesc index: %d", idx); } -/* Allocate buffer for the jump pads. Since we're using 32-bit jumps - to reach them, and the executable is at low addresses, MAP_32BIT - works just fine. Shared libraries, being allocated at the top, - are unfortunately out of luck. */ +/* Allocate buffer for the jump pads. The branch instruction has a + reach of +/- 31-bit, and the executable is loaded at low addresses. + + 64-bit: Use MAP_32BIT to allocate in the first 2GB. Shared + libraries, being allocated at the top, are unfortunately out of + luck. + + x32: Since MAP_32BIT is 64-bit only, do the placement manually. + Try allocating at '0x80000000 - SIZE' initially, decreasing until + we hit a free area. This ensures the executable is fully covered, + and is as close as possible to the shared libraries, which are + usually mapped at the top of the first 4GB of the address space. +*/ void * alloc_jump_pad_buffer (size_t size) { +#if __ILP32__ + uintptr_t addr; + int pagesize; + + pagesize = sysconf (_SC_PAGE_SIZE); + if (pagesize == -1) + perror_with_name ("sysconf"); + + addr = 0x80000000 - size; + + /* size should already be page-aligned, but this can't hurt. */ + addr &= ~(pagesize - 1); + + /* Search for a free area. If we hit 0, we're out of luck. */ + for (; addr; addr -= pagesize) + { + void *res; + + /* No MAP_FIXED - we don't want to zap someone's mapping. */ + res = mmap ((void *) addr, size, + PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + /* If we got what we wanted, return. */ + if ((uintptr_t) res == addr) + return res; + + /* If we got a mapping, but at a wrong address, undo it. */ + if (res != MAP_FAILED) + munmap (res, size); + } + + return NULL; +#else void *res = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0); @@ -221,6 +264,7 @@ alloc_jump_pad_buffer (size_t size) return NULL; return res; +#endif } void