From patchwork Mon Mar 11 12:35:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 1054412 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44HyYF5s61z9s47 for ; Mon, 11 Mar 2019 23:47:13 +1100 (AEDT) Received: from localhost ([127.0.0.1]:33061 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3KKk-00030x-6u for incoming@patchwork.ozlabs.org; Mon, 11 Mar 2019 08:47:10 -0400 Received: from eggs.gnu.org ([209.51.188.92]:33374) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3KE3-0006Qx-Pd for qemu-devel@nongnu.org; Mon, 11 Mar 2019 08:40:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3KE2-0001oW-Rn for qemu-devel@nongnu.org; Mon, 11 Mar 2019 08:40:15 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57590) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h3K9A-000600-3j; Mon, 11 Mar 2019 08:35:12 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 61596308425C; Mon, 11 Mar 2019 12:35:11 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-64.ams2.redhat.com [10.36.112.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id 342BC19724; Mon, 11 Mar 2019 12:35:09 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 11 Mar 2019 13:35:05 +0100 Message-Id: <20190311123507.24867-2-pbonzini@redhat.com> In-Reply-To: <20190311123507.24867-1-pbonzini@redhat.com> References: <20190311123507.24867-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Mon, 11 Mar 2019 12:35:11 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/3] qemugdb: fix licensing 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: kwolf@redhat.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" qemu-gdb.py was committed after 2012-01-13, so the notice about GPL v2-only contributions does not apply. Signed-off-by: Paolo Bonzini --- scripts/qemu-gdb.py | 7 ++----- scripts/qemugdb/coroutine.py | 7 ++----- scripts/qemugdb/mtree.py | 7 ++----- scripts/qemugdb/tcg.py | 7 ++----- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/scripts/qemu-gdb.py b/scripts/qemu-gdb.py index 690827e6fc..f2a305c42e 100644 --- a/scripts/qemu-gdb.py +++ b/scripts/qemu-gdb.py @@ -7,11 +7,8 @@ # Authors: # Avi Kivity # -# This work is licensed under the terms of the GNU GPL, version 2. See -# the COPYING file in the top-level directory. -# -# Contributions after 2012-01-13 are licensed under the terms of the -# GNU GPL, version 2 or (at your option) any later version. +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. # Usage: # At the (gdb) prompt, type "source scripts/qemu-gdb.py". diff --git a/scripts/qemugdb/coroutine.py b/scripts/qemugdb/coroutine.py index 81f811ac00..41e079d0e2 100644 --- a/scripts/qemugdb/coroutine.py +++ b/scripts/qemugdb/coroutine.py @@ -7,11 +7,8 @@ # Authors: # Avi Kivity # -# This work is licensed under the terms of the GNU GPL, version 2. See -# the COPYING file in the top-level directory. -# -# Contributions after 2012-01-13 are licensed under the terms of the -# GNU GPL, version 2 or (at your option) any later version. +# This work is licensed under the terms of the GNU GPL, version 2 +# or later. See the COPYING file in the top-level directory. import gdb diff --git a/scripts/qemugdb/mtree.py b/scripts/qemugdb/mtree.py index e6791b7885..3030a60d3f 100644 --- a/scripts/qemugdb/mtree.py +++ b/scripts/qemugdb/mtree.py @@ -7,11 +7,8 @@ # Authors: # Avi Kivity # -# This work is licensed under the terms of the GNU GPL, version 2. See -# the COPYING file in the top-level directory. -# -# Contributions after 2012-01-13 are licensed under the terms of the -# GNU GPL, version 2 or (at your option) any later version. +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. # 'qemu mtree' -- display the memory hierarchy diff --git a/scripts/qemugdb/tcg.py b/scripts/qemugdb/tcg.py index 8c7f1d7454..18880fc9a7 100644 --- a/scripts/qemugdb/tcg.py +++ b/scripts/qemugdb/tcg.py @@ -8,11 +8,8 @@ # Authors: # Alex Bennée # -# This work is licensed under the terms of the GNU GPL, version 2. See -# the COPYING file in the top-level directory. -# -# Contributions after 2012-01-13 are licensed under the terms of the -# GNU GPL, version 2 or (at your option) any later version. +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. # 'qemu tcg-lock-status' -- display the TCG lock status across threads From patchwork Mon Mar 11 12:35:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 1054408 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44HyP13QWXz9s47 for ; Mon, 11 Mar 2019 23:40:05 +1100 (AEDT) Received: from localhost ([127.0.0.1]:32893 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3KDr-0005JN-Bj for incoming@patchwork.ozlabs.org; Mon, 11 Mar 2019 08:40:03 -0400 Received: from eggs.gnu.org ([209.51.188.92]:60311) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3K9S-0002Ls-4c for qemu-devel@nongnu.org; Mon, 11 Mar 2019 08:35:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3K9Q-0006Et-JI for qemu-devel@nongnu.org; Mon, 11 Mar 2019 08:35:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49418) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h3K9B-000611-Np; Mon, 11 Mar 2019 08:35:14 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 016FA3082DDD; Mon, 11 Mar 2019 12:35:13 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-64.ams2.redhat.com [10.36.112.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id C71B119731; Mon, 11 Mar 2019 12:35:11 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 11 Mar 2019 13:35:06 +0100 Message-Id: <20190311123507.24867-3-pbonzini@redhat.com> In-Reply-To: <20190311123507.24867-1-pbonzini@redhat.com> References: <20190311123507.24867-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Mon, 11 Mar 2019 12:35:13 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/3] qemugdb: allow adding support for other coroutine backends 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: kwolf@redhat.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Split the jmpbuf access to a separate module and dispatch based on which CoroutineXYZ type is defined. Signed-off-by: Paolo Bonzini --- scripts/qemugdb/coroutine.py | 106 ++++++++------------------ scripts/qemugdb/coroutine_ucontext.py | 69 +++++++++++++++++ 2 files changed, 100 insertions(+), 75 deletions(-) create mode 100644 scripts/qemugdb/coroutine_ucontext.py diff --git a/scripts/qemugdb/coroutine.py b/scripts/qemugdb/coroutine.py index 41e079d0e2..db2753d949 100644 --- a/scripts/qemugdb/coroutine.py +++ b/scripts/qemugdb/coroutine.py @@ -1,6 +1,6 @@ #!/usr/bin/python -# GDB debugging support +# GDB debugging support, coroutine dispatch # # Copyright 2012 Red Hat, Inc. and/or its affiliates # @@ -10,82 +10,25 @@ # This work is licensed under the terms of the GNU GPL, version 2 # or later. See the COPYING file in the top-level directory. +from . import coroutine_ucontext import gdb VOID_PTR = gdb.lookup_type('void').pointer() +UINTPTR_T = gdb.lookup_type('uintptr_t') -def get_fs_base(): - '''Fetch %fs base value using arch_prctl(ARCH_GET_FS). This is - pthread_self().''' - # %rsp - 120 is scratch space according to the SystemV ABI - old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') - gdb.execute('call (int)arch_prctl(0x1003, $rsp - 120)', False, True) - fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') - gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True) - return fs_base - -def pthread_self(): - '''Fetch pthread_self() from the glibc start_thread function.''' - f = gdb.newest_frame() - while f.name() != 'start_thread': - f = f.older() - if f is None: - return get_fs_base() - - try: - return f.read_var("arg") - except ValueError: - return get_fs_base() - -def get_glibc_pointer_guard(): - '''Fetch glibc pointer guard value''' - fs_base = pthread_self() - return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base) - -def glibc_ptr_demangle(val, pointer_guard): - '''Undo effect of glibc's PTR_MANGLE()''' - return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard)) - -def get_jmpbuf_regs(jmpbuf): - JB_RBX = 0 - JB_RBP = 1 - JB_R12 = 2 - JB_R13 = 3 - JB_R14 = 4 - JB_R15 = 5 - JB_RSP = 6 - JB_PC = 7 - - pointer_guard = get_glibc_pointer_guard() - return {'rbx': jmpbuf[JB_RBX], - 'rbp': glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard), - 'rsp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard), - 'r12': jmpbuf[JB_R12], - 'r13': jmpbuf[JB_R13], - 'r14': jmpbuf[JB_R14], - 'r15': jmpbuf[JB_R15], - 'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) } - -def bt_jmpbuf(jmpbuf): - '''Backtrace a jmpbuf''' - regs = get_jmpbuf_regs(jmpbuf) - old = dict() - - for i in regs: - old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i) - - for i in regs: - gdb.execute('set $%s = %s' % (i, regs[i])) - - gdb.execute('bt') - - for i in regs: - gdb.execute('set $%s = %s' % (i, old[i])) - -def coroutine_to_jmpbuf(co): - coroutine_pointer = co.cast(gdb.lookup_type('CoroutineUContext').pointer()) - return coroutine_pointer['env']['__jmpbuf'] +backends = { + 'CoroutineUContext': coroutine_ucontext +} +def coroutine_backend(): + for k, v in backends.items(): + try: + gdb.lookup_type(k) + except: + continue + return v + + raise Exception('could not find coroutine backend') class CoroutineCommand(gdb.Command): '''Display coroutine backtrace''' @@ -99,18 +42,31 @@ class CoroutineCommand(gdb.Command): gdb.write('usage: qemu coroutine \n') return - bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0]))) + addr = gdb.parse_and_eval(argv[0]) + regs = coroutine_backend().get_coroutine_regs(addr) + old = dict() + + for i in regs: + old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i) + + for i in regs: + gdb.execute('set $%s = %s' % (i, regs[i].cast(UINTPTR_T))) + + gdb.execute('bt') + + for i in regs: + gdb.execute('set $%s = %s' % (i, old[i].cast(UINTPTR_T))) class CoroutineSPFunction(gdb.Function): def __init__(self): gdb.Function.__init__(self, 'qemu_coroutine_sp') def invoke(self, addr): - return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID_PTR) + return coroutine_backend().get_coroutine_regs(addr)['rsp'].cast(VOID_PTR) class CoroutinePCFunction(gdb.Function): def __init__(self): gdb.Function.__init__(self, 'qemu_coroutine_pc') def invoke(self, addr): - return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID_PTR) + return coroutine_backend().get_coroutine_regs(addr)['rip'].cast(VOID_PTR) diff --git a/scripts/qemugdb/coroutine_ucontext.py b/scripts/qemugdb/coroutine_ucontext.py new file mode 100644 index 0000000000..a2f8c1dbbf --- /dev/null +++ b/scripts/qemugdb/coroutine_ucontext.py @@ -0,0 +1,69 @@ +#!/usr/bin/python + +# GDB debugging support +# +# Copyright 2012 Red Hat, Inc. and/or its affiliates +# +# Authors: +# Avi Kivity +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. + +import gdb + +def get_fs_base(): + '''Fetch %fs base value using arch_prctl(ARCH_GET_FS). This is + pthread_self().''' + # %rsp - 120 is scratch space according to the SystemV ABI + old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') + gdb.execute('call (int)arch_prctl(0x1003, $rsp - 120)', False, True) + fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') + gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True) + return fs_base + +def pthread_self(): + '''Fetch pthread_self() from the glibc start_thread function.''' + f = gdb.newest_frame() + while f.name() != 'start_thread': + f = f.older() + if f is None: + return get_fs_base() + + try: + return f.read_var("arg") + except ValueError: + return get_fs_base() + +def get_glibc_pointer_guard(): + '''Fetch glibc pointer guard value''' + fs_base = pthread_self() + return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base) + +def glibc_ptr_demangle(val, pointer_guard): + '''Undo effect of glibc's PTR_MANGLE()''' + return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard)) + +def get_jmpbuf_regs(jmpbuf): + JB_RBX = 0 + JB_RBP = 1 + JB_R12 = 2 + JB_R13 = 3 + JB_R14 = 4 + JB_R15 = 5 + JB_RSP = 6 + JB_PC = 7 + + pointer_guard = get_glibc_pointer_guard() + return {'rbx': jmpbuf[JB_RBX], + 'rbp': glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard), + 'rsp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard), + 'r12': jmpbuf[JB_R12], + 'r13': jmpbuf[JB_R13], + 'r14': jmpbuf[JB_R14], + 'r15': jmpbuf[JB_R15], + 'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) } + +def get_coroutine_regs(addr): + co = addr.cast(gdb.lookup_type('CoroutineUContext').pointer()) + return get_jmpbuf_regs(co['env']['__jmpbuf']) From patchwork Mon Mar 11 12:35:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 1054411 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44HyVr6wpqz9s47 for ; Mon, 11 Mar 2019 23:45:08 +1100 (AEDT) Received: from localhost ([127.0.0.1]:32992 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3KIk-0001QL-TK for incoming@patchwork.ozlabs.org; Mon, 11 Mar 2019 08:45:06 -0400 Received: from eggs.gnu.org ([209.51.188.92]:33411) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3KE9-0006Uc-Ds for qemu-devel@nongnu.org; Mon, 11 Mar 2019 08:40:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3KE7-0001rQ-NF for qemu-devel@nongnu.org; Mon, 11 Mar 2019 08:40:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49064) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h3K9D-00062Y-Dx; Mon, 11 Mar 2019 08:35:15 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9B29236809; Mon, 11 Mar 2019 12:35:14 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-64.ams2.redhat.com [10.36.112.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6806919724; Mon, 11 Mar 2019 12:35:13 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 11 Mar 2019 13:35:07 +0100 Message-Id: <20190311123507.24867-4-pbonzini@redhat.com> In-Reply-To: <20190311123507.24867-1-pbonzini@redhat.com> References: <20190311123507.24867-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 11 Mar 2019 12:35:14 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 3/3] coroutine: add x86 specific coroutine backend 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: kwolf@redhat.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" This backend is faster (100ns vs 150ns per switch on my laptop), but especially it will be possible to add CET support to it in 4.1. In the meanwhile, it is nice to have it as an experimental alternative. Signed-off-by: Paolo Bonzini --- configure | 8 ++ scripts/qemugdb/coroutine.py | 5 +- scripts/qemugdb/coroutine_x86.py | 21 +++ util/coroutine-x86.c | 213 +++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 scripts/qemugdb/coroutine_x86.py create mode 100644 util/coroutine-x86.c diff --git a/configure b/configure index 62a2a490f2..af65edc30a 100755 --- a/configure +++ b/configure @@ -5123,6 +5123,14 @@ else error_exit "only the 'windows' coroutine backend is valid for Windows" fi ;; + x86) + if test "$mingw32" = "yes"; then + error_exit "only the 'windows' coroutine backend is valid for Windows" + fi + if test "$cpu" != "x86_64"; then + error_exit "the 'x86' backend is only valid for x86_64 hosts" + fi + ;; *) error_exit "unknown coroutine backend $coroutine" ;; diff --git a/scripts/qemugdb/coroutine.py b/scripts/qemugdb/coroutine.py index db2753d949..f716db22bb 100644 --- a/scripts/qemugdb/coroutine.py +++ b/scripts/qemugdb/coroutine.py @@ -10,14 +10,15 @@ # This work is licensed under the terms of the GNU GPL, version 2 # or later. See the COPYING file in the top-level directory. -from . import coroutine_ucontext +from . import coroutine_ucontext, coroutine_x86 import gdb VOID_PTR = gdb.lookup_type('void').pointer() UINTPTR_T = gdb.lookup_type('uintptr_t') backends = { - 'CoroutineUContext': coroutine_ucontext + 'CoroutineUContext': coroutine_ucontext, + 'CoroutineX86': coroutine_x86 } def coroutine_backend(): diff --git a/scripts/qemugdb/coroutine_x86.py b/scripts/qemugdb/coroutine_x86.py new file mode 100644 index 0000000000..05f830cdb8 --- /dev/null +++ b/scripts/qemugdb/coroutine_x86.py @@ -0,0 +1,21 @@ +#!/usr/bin/python + +# GDB debugging support +# +# Copyright 2019 Red Hat, Inc. +# +# Authors: +# Paolo Bonzini +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. + +import gdb + +U64_PTR = gdb.lookup_type('uint64_t').pointer() + +def get_coroutine_regs(addr): + addr = addr.cast(gdb.lookup_type('CoroutineX86').pointer()) + rsp = addr['sp'].cast(U64_PTR) + return {'rsp': rsp, + 'rip': rsp.dereference()} diff --git a/util/coroutine-x86.c b/util/coroutine-x86.c new file mode 100644 index 0000000000..7f5e7d7696 --- /dev/null +++ b/util/coroutine-x86.c @@ -0,0 +1,213 @@ +/* + * x86-specific coroutine initialization code + * + * Copyright (C) 2006 Anthony Liguori + * Copyright (C) 2011 Kevin Wolf + * Copyright (C) 2019 Paolo Bonzini + * + * This 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.0 of the License, or (at your option) any later version. + * + * This 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 this library; if not, see . + */ + +/* XXX Is there a nicer way to disable glibc's stack check for longjmp? */ +#ifdef _FORTIFY_SOURCE +#undef _FORTIFY_SOURCE +#endif +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qemu/coroutine_int.h" + +#ifdef CONFIG_VALGRIND_H +#include +#endif + +#if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer) +#ifdef CONFIG_ASAN_IFACE_FIBER +#define CONFIG_ASAN 1 +#include +#endif +#endif + +typedef struct { + Coroutine base; + void *stack; + size_t stack_size; + void *sp; + +#ifdef CONFIG_VALGRIND_H + unsigned int valgrind_stack_id; +#endif +} CoroutineX86; + +/** + * Per-thread coroutine bookkeeping + */ +static __thread CoroutineX86 leader; +static __thread Coroutine *current; + +static void finish_switch_fiber(void *fake_stack_save) +{ +#ifdef CONFIG_ASAN + const void *bottom_old; + size_t size_old; + + __sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old); + + if (!leader.stack) { + leader.stack = (void *)bottom_old; + leader.stack_size = size_old; + } +#endif +} + +static void start_switch_fiber(void **fake_stack_save, + const void *bottom, size_t size) +{ +#ifdef CONFIG_ASAN + __sanitizer_start_switch_fiber(fake_stack_save, bottom, size); +#endif +} + +/* On entry to a coroutine, rax is "value" and rsi is the coroutine itself. */ +#define CO_SWITCH(from, to, action, jump) ({ \ + int ret = action; \ + void *from_ = from; \ + void *to_ = to; \ + asm volatile( \ + ".cfi_remember_state\n" \ + "pushq %%rbp\n" /* save scratch register on source stack */ \ + ".cfi_adjust_cfa_offset 8\n" \ + ".cfi_rel_offset %%rbp, 0\n" \ + "call 1f\n" /* switch continues at label 1 */ \ + ".cfi_adjust_cfa_offset 8\n" \ + "jmp 2f\n" /* switch back continues at label 2 */ \ + "1: movq (%%rsp), %%rbp\n" /* save source IP for debugging */ \ + "movq %%rsp, %c[sp](%[FROM])\n" /* save source SP */ \ + "movq %c[sp](%[TO]), %%rsp\n" /* load destination SP */ \ + jump "\n" /* coroutine switch */ \ + "2:" \ + ".cfi_adjust_cfa_offset -8\n" \ + "popq %%rbp\n" \ + ".cfi_adjust_cfa_offset -8\n" \ + ".cfi_restore_state\n" \ + : "+a" (ret), [FROM] "+b" (from_), [TO] "+D" (to_) \ + : [sp] "i" (offsetof(CoroutineX86, sp)) \ + : "rcx", "rdx", "rsi", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", \ + "memory"); \ + ret; \ +}) + +static void __attribute__((__used__)) coroutine_trampoline(void *arg) +{ + CoroutineX86 *self = arg; + Coroutine *co = &self->base; + + finish_switch_fiber(NULL); + + while (true) { + qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); + co->entry(co->entry_arg); + } +} + +Coroutine *qemu_coroutine_new(void) +{ + CoroutineX86 *co; + void *fake_stack_save = NULL; + + co = g_malloc0(sizeof(*co)); + co->stack_size = COROUTINE_STACK_SIZE; + co->stack = qemu_alloc_stack(&co->stack_size); + co->sp = co->stack + co->stack_size; + +#ifdef CONFIG_VALGRIND_H + co->valgrind_stack_id = + VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size); +#endif + + /* Immediately enter the coroutine once to pass it its address as the argument */ + co->base.caller = qemu_coroutine_self(); + start_switch_fiber(&fake_stack_save, co->stack, co->stack_size); + CO_SWITCH(current, co, 0, "jmp coroutine_trampoline"); + finish_switch_fiber(fake_stack_save); + co->base.caller = NULL; + + return &co->base; +} + +#ifdef CONFIG_VALGRIND_H +#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__) +/* Work around an unused variable in the valgrind.h macro... */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-but-set-variable" +#endif +static inline void valgrind_stack_deregister(CoroutineX86 *co) +{ + VALGRIND_STACK_DEREGISTER(co->valgrind_stack_id); +} +#if defined(CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif +#endif + +void qemu_coroutine_delete(Coroutine *co_) +{ + CoroutineX86 *co = DO_UPCAST(CoroutineX86, base, co_); + +#ifdef CONFIG_VALGRIND_H + valgrind_stack_deregister(co); +#endif + + qemu_free_stack(co->stack, co->stack_size); + g_free(co); +} + +/* + * This function is marked noinline to prevent GCC from inlining it + * into coroutine_trampoline(). If we allow it to do that then it + * hoists the code to get the address of the TLS variable "current" + * out of the while() loop. This is an invalid transformation because + * qemu_coroutine_switch() may be called when running thread A but + * return in thread B, and so we might be in a different thread + * context each time round the loop. + */ +CoroutineAction __attribute__((noinline)) +qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, + CoroutineAction action) +{ + CoroutineX86 *from = DO_UPCAST(CoroutineX86, base, from_); + CoroutineX86 *to = DO_UPCAST(CoroutineX86, base, to_); + void *fake_stack_save = NULL; + + current = to_; + + start_switch_fiber(action == COROUTINE_TERMINATE ? + NULL : &fake_stack_save, to->stack, to->stack_size); + action = CO_SWITCH(from, to, action, "ret"); + finish_switch_fiber(fake_stack_save); + + return action; +} + +Coroutine *qemu_coroutine_self(void) +{ + if (!current) { + current = &leader.base; + } + return current; +} + +bool qemu_in_coroutine(void) +{ + return current && current->caller; +}