From nobody Tue May 7 04:32:27 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1556971673; cv=none; d=zoho.com; s=zohoarc; b=SLaknjt41cbSWwtzTJZb7MKmoS+0uGWxlQzUu0US/SOrgQunt04I5V9DQVW09OkSjhhoRsM2Gcfss6shojBFYDKa/HKTV4efs3mDrttK1vC2bzPqbFIjpAJ8W4xFgN2iZlkQuQSSlbt7EMEwM/8n7cs7aeM0dtcGWX+gHL8peDs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556971673; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=pmZJgq4sgMXP9qHYEfxrgn9xoXzv+P55K0/VCJX3rj4=; b=Yb49NH2lSdavevishZErlAtGGGAb265P6g6a8FKOWfM11z0pnmgtx+REwkuzof3PN305XEilyseiffrQ8IvYGbQcuEekiVSnkKGeuPkONuX9J+C8iXIHu2403fbJxa4Zwv29+OQEdhTMRjy90xC+C/mnCjm8hLNLaw5IP6WqiYY= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=temperror (zoho.com: Error in retrieving data from DNS) 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 1556971673020644.0401786468541; Sat, 4 May 2019 05:07:53 -0700 (PDT) Received: from localhost ([127.0.0.1]:55665 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtS5-0008Ix-AT for importer@patchew.org; Sat, 04 May 2019 08:07:37 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50361) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtQ9-000792-2H for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMtQ6-0001C6-Tj for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:36 -0400 Received: from mail-ot1-x330.google.com ([2607:f8b0:4864:20::330]:41296) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMtQ6-0001BM-HE; Sat, 04 May 2019 08:05:34 -0400 Received: by mail-ot1-x330.google.com with SMTP id g8so7624690otl.8; Sat, 04 May 2019 05:05:32 -0700 (PDT) Received: from localhost.localdomain ([198.59.53.9]) by smtp.gmail.com with ESMTPSA id s26sm1844978otk.24.2019.05.04.05.05.30 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 04 May 2019 05:05:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pmZJgq4sgMXP9qHYEfxrgn9xoXzv+P55K0/VCJX3rj4=; b=eJF5BpXMrgWoO8/CcxRFeGVCJLQXnvspWD8+IbqaS8DA5XMPCRmxHhNeTkegwhr0d0 g0cfu2yfrx1muxQqq5aJbYa8lHeZiz416kzLBRv23fEOvIJTNMQd7B6NdXdPw1YV+ngg n2k9A4BV4bZJ9Ki3gUHQYWQdgzdNjCNX9EYF9g+7H45RDqQLG8ypWq7BSmdOM6ErSJpX uebJHdvZqd5g3dPpQ0uVpPebXFDTMhj1BPWtf9fr/sQdV+YcqHbep5KaAzZ06UlvS2Do RhEqf+C4hOR0ytCtQiL5MzV6Ya17M3KfOq9XpEZA4qBTOyQKBl4eyWMp6G9BB3zotxUO IEQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=pmZJgq4sgMXP9qHYEfxrgn9xoXzv+P55K0/VCJX3rj4=; b=HDepH0WIK3elGuNTPks+CnTvyfwtw85UFSHqmnw1h33smpZJJ9omrHHI9WKD3CqUhj nBYBJ45jq4S9mkDlFuLUQn9T4HocySinF/jjG+mbkQ/kTrOdvuEQrC6ipxnAgTL201W1 ceqNANPpo6iROUzsvmb4QdOTiVO5nrVoFswuKp6ND+9GgNjss7smNdyG+1Zifr5whx/P sKcsoQbc0PaMkK9SgHUHh6kLbrWqb4tMECxZKHwM+a8+5qlP/qlUuiDthl6Nr1vDVABd Cxox+o3Hr4B5iym4uHDzhBO81Xr8YJ76LKINmX1z8hrFcIN9VKilPD4BNkpZns2v4RbV tFvA== X-Gm-Message-State: APjAAAWry7HC1mYxbYnBbnGojneqXKhE403Qs1pC+COsceVP7VcG5fmd NKMcAd5HW1Y6c+TMDa0If8AZIuo3 X-Google-Smtp-Source: APXvYqwGdiOQa1jDHcPHGwZBGeKDoOqm6JKnguYPiYDm2NeNqwqnd9MjsZFphMkhn9LfTQahaqFVTA== X-Received: by 2002:a05:6830:2106:: with SMTP id i6mr9763319otc.146.1556971531677; Sat, 04 May 2019 05:05:31 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 4 May 2019 06:05:19 -0600 Message-Id: <20190504120528.6389-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190504120528.6389-1-pbonzini@redhat.com> References: <20190504120528.6389-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::330 Subject: [Qemu-devel] [PATCH 1/9] 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: peter.maydell@linaro.org, cohuck@redhat.com, richard.henderson@linaro.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Split the jmpbuf access to a separate module and dispatch based on which CoroutineXYZ type is defined. Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- 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 =20 -# 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. =20 +from . import coroutine_ucontext import gdb =20 VOID_PTR =3D gdb.lookup_type('void').pointer() +UINTPTR_T =3D gdb.lookup_type('uintptr_t') =20 -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 =3D gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') - gdb.execute('call (int)arch_prctl(0x1003, $rsp - 120)', False, True) - fs_base =3D gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') - gdb.execute('set *(uint64_t*)($rsp - 120) =3D %s' % old, False, True) - return fs_base - -def pthread_self(): - '''Fetch pthread_self() from the glibc start_thread function.''' - f =3D gdb.newest_frame() - while f.name() !=3D 'start_thread': - f =3D 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 =3D pthread_self() - return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_bas= e) - -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 =3D 0 - JB_RBP =3D 1 - JB_R12 =3D 2 - JB_R13 =3D 3 - JB_R14 =3D 4 - JB_R15 =3D 5 - JB_RSP =3D 6 - JB_PC =3D 7 - - pointer_guard =3D 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 =3D get_jmpbuf_regs(jmpbuf) - old =3D dict() - - for i in regs: - old[i] =3D gdb.parse_and_eval('(uint64_t)$%s' % i) - - for i in regs: - gdb.execute('set $%s =3D %s' % (i, regs[i])) - - gdb.execute('bt') - - for i in regs: - gdb.execute('set $%s =3D %s' % (i, old[i])) - -def coroutine_to_jmpbuf(co): - coroutine_pointer =3D co.cast(gdb.lookup_type('CoroutineUContext').poi= nter()) - return coroutine_pointer['env']['__jmpbuf'] +backends =3D { + 'CoroutineUContext': coroutine_ucontext +} =20 +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') =20 class CoroutineCommand(gdb.Command): '''Display coroutine backtrace''' @@ -99,18 +42,31 @@ class CoroutineCommand(gdb.Command): gdb.write('usage: qemu coroutine \n') return =20 - bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0]))) + addr =3D gdb.parse_and_eval(argv[0]) + regs =3D coroutine_backend().get_coroutine_regs(addr) + old =3D dict() + + for i in regs: + old[i] =3D gdb.parse_and_eval('(uint64_t)$%s' % i) + + for i in regs: + gdb.execute('set $%s =3D %s' % (i, regs[i].cast(UINTPTR_T))) + + gdb.execute('bt') + + for i in regs: + gdb.execute('set $%s =3D %s' % (i, old[i].cast(UINTPTR_T))) =20 class CoroutineSPFunction(gdb.Function): def __init__(self): gdb.Function.__init__(self, 'qemu_coroutine_sp') =20 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(VO= ID_PTR) =20 class CoroutinePCFunction(gdb.Function): def __init__(self): gdb.Function.__init__(self, 'qemu_coroutine_pc') =20 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(VO= ID_PTR) diff --git a/scripts/qemugdb/coroutine_ucontext.py b/scripts/qemugdb/corout= ine_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 =3D gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') + gdb.execute('call (int)arch_prctl(0x1003, $rsp - 120)', False, True) + fs_base =3D gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') + gdb.execute('set *(uint64_t*)($rsp - 120) =3D %s' % old, False, True) + return fs_base + +def pthread_self(): + '''Fetch pthread_self() from the glibc start_thread function.''' + f =3D gdb.newest_frame() + while f.name() !=3D 'start_thread': + f =3D 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 =3D pthread_self() + return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_bas= e) + +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 =3D 0 + JB_RBP =3D 1 + JB_R12 =3D 2 + JB_R13 =3D 3 + JB_R14 =3D 4 + JB_R15 =3D 5 + JB_RSP =3D 6 + JB_PC =3D 7 + + pointer_guard =3D 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 =3D addr.cast(gdb.lookup_type('CoroutineUContext').pointer()) + return get_jmpbuf_regs(co['env']['__jmpbuf']) --=20 2.21.0 From nobody Tue May 7 04:32:27 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1556971830; cv=none; d=zoho.com; s=zohoarc; b=IkwTg4x/H2W0uGHpEFlMFi9TucaZqup214Q9zVJy5tKlzEM//yIMXIGSRp5uWj9xqK0eDA3RUCMIyrqvLFROEZ3zfxlL+dw9MGvGZTHRM91LFAIIUfDBGb4Ouz8MfcKsT5yZmrR4QXfvWU9Agg/WA22b3sDgMWOvPaMuHwIFSuE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556971830; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=SKe5W6gEyQ6vDMvZhmfmuPkjdQXC7onAVuM4AddCdiY=; b=gFM0GEswE6c7rc33KtNRshEYl7p1KCC2HECx34WLyrzbKp7syakECQRssihLfnk2GMAcnpHFClDx64kB0sZZA7JiaepHMkh1JAoyYvsetJotY2lzG4f/194RfonKYbeIUZehtf/q32Fbibp8D40Im+RByCe81517Yj4C884GZVQ= 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 1556971830094387.0419788149536; Sat, 4 May 2019 05:10:30 -0700 (PDT) Received: from localhost ([127.0.0.1]:55694 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtUn-0002EJ-VA for importer@patchew.org; Sat, 04 May 2019 08:10:26 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50354) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtQ8-000790-Kn for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMtQ6-0001CE-V4 for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:36 -0400 Received: from mail-ot1-x334.google.com ([2607:f8b0:4864:20::334]:33741) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMtQ6-0001Ba-HU; Sat, 04 May 2019 08:05:34 -0400 Received: by mail-ot1-x334.google.com with SMTP id s11so7661612otp.0; Sat, 04 May 2019 05:05:33 -0700 (PDT) Received: from localhost.localdomain ([198.59.53.9]) by smtp.gmail.com with ESMTPSA id s26sm1844978otk.24.2019.05.04.05.05.31 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 04 May 2019 05:05:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SKe5W6gEyQ6vDMvZhmfmuPkjdQXC7onAVuM4AddCdiY=; b=m7Snk0mZEgvRNhSoMKv9Mv+4ZfKDiMWO+G/SIiPUmIA6LUoE+uxAvMuz9Y5YxPNBhR AJkwNPhs5VEMNc4coPjRNM0om1beXAyiuIA0n3ZIPNWwPO4HiS7nK/2IBdXf3VxpQhtX hp08nNyjGaHRxWrWybOZteoVTBdBbrnwJx2ZIMKAy+yvn15HNHbHvPtuUj6na1r/6His qyyXUz45KKFXwope4iWZjRQhCb4F6tLsTJn3rF29S7NALHkQQsiCRmE6lc5QUF1wBYA9 YM7MGrbyAmvhI2URK0xmhJ/52Hm0zObC9X1kwlt6Y9MJstxeaFirknFDd7dH+lgZqqsO VPwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=SKe5W6gEyQ6vDMvZhmfmuPkjdQXC7onAVuM4AddCdiY=; b=f1WX89Or5aTHDtu//0HLc26n+NF3TtJGefwu9DH75IjT5OD88JDkbfMR5F5sPsR3lP ARln4BnNFM8loB45BoAJPYVpTjSDFLsSe63dX2G+4OoTH9255rY0s8P471lQ1ai6JNIK lEAnu8krtlkTO7i7E70u/n9H5YuqbuSnEspld9fVFBwMXEhtWoOy1O2FQhw9Z4+39oiT SJTvrhgRXGO+n1TeyDvtT7WyvJ33ridrrANzgZ5ntFtateo3/jaVbnA27y3xRJu0V5oe YuoFwOTu6+6QIzrI9dllmTrsSpTTDR9hdruYQN86b3/kc8kpXK/OK7eu+w5kQ+xJUkEW RltA== X-Gm-Message-State: APjAAAWnbV6GMD4XQNVTIxWNsmp5+Oi/cRIUK/3Cbjoe/b6sLl2YbbwA SpPbhFmLpBGLbr/0arIpHhy8pONv X-Google-Smtp-Source: APXvYqx/9giXRu2PfOMGUFS/d2TCMtOy96P2IRLQRXywWtPBLOF8IJWEuwbzhO/JIhsPJcA1KeFB2A== X-Received: by 2002:a9d:27c8:: with SMTP id c66mr4601174otb.85.1556971532677; Sat, 04 May 2019 05:05:32 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 4 May 2019 06:05:20 -0600 Message-Id: <20190504120528.6389-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190504120528.6389-1-pbonzini@redhat.com> References: <20190504120528.6389-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::334 Subject: [Qemu-devel] [PATCH 2/9] qemugdb: allow adding support for other architectures 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, cohuck@redhat.com, richard.henderson@linaro.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" $sp and $pc are standard register names that are available on most machines, use them instead of $rsp and $rip so that other architectures can use qemu_coroutine_sp and qemu_coroutine_pc. Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- scripts/qemugdb/coroutine.py | 4 ++-- scripts/qemugdb/coroutine_ucontext.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/qemugdb/coroutine.py b/scripts/qemugdb/coroutine.py index db2753d949..076f6808ab 100644 --- a/scripts/qemugdb/coroutine.py +++ b/scripts/qemugdb/coroutine.py @@ -62,11 +62,11 @@ class CoroutineSPFunction(gdb.Function): gdb.Function.__init__(self, 'qemu_coroutine_sp') =20 def invoke(self, addr): - return coroutine_backend().get_coroutine_regs(addr)['rsp'].cast(VO= ID_PTR) + return coroutine_backend().get_coroutine_regs(addr)['sp'].cast(VOI= D_PTR) =20 class CoroutinePCFunction(gdb.Function): def __init__(self): gdb.Function.__init__(self, 'qemu_coroutine_pc') =20 def invoke(self, addr): - return coroutine_backend().get_coroutine_regs(addr)['rip'].cast(VO= ID_PTR) + return coroutine_backend().get_coroutine_regs(addr)['pc'].cast(VOI= D_PTR) diff --git a/scripts/qemugdb/coroutine_ucontext.py b/scripts/qemugdb/corout= ine_ucontext.py index a2f8c1dbbf..eed095be22 100644 --- a/scripts/qemugdb/coroutine_ucontext.py +++ b/scripts/qemugdb/coroutine_ucontext.py @@ -57,12 +57,12 @@ def get_jmpbuf_regs(jmpbuf): pointer_guard =3D 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), + 'sp': 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) } + 'pc': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) } =20 def get_coroutine_regs(addr): co =3D addr.cast(gdb.lookup_type('CoroutineUContext').pointer()) --=20 2.21.0 From nobody Tue May 7 04:32:27 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1556971831; cv=none; d=zoho.com; s=zohoarc; b=gsEU+yXiVO0KddIvsamD4B9/OGH1ZYpAVP6c3i0P4VlFmf9V+NnNPuFlDMpC2xoIG7vZ4Ji72811G88LAgW/LZuAJiSEu2fsMUJSLv7OYgnTnszh130zPl1ioth6BZXOtAn3sqqAF0Uvkbr3J8qXqhaE+Vgq/1B9XmhSN8OpZUo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556971831; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=JLVM6LVe8Ci/wZhURG+abGdUOpJKnFkyRFzRhc1SJWU=; b=XHYhplz+yZJV2B3cK6kASxTa2XhQIPLaFr7RYrOZXzVVaO1MEGNtf/lDcW87kkQoaJG1YWLGJVb7eF36fO8AMieVlzBllnfbCN+L/077mt5tepUqNo8+VeYzqcyBHsZkHfjmL6AimNuvNp3bNyoNJfbHVYEnqtCSd/PcnyqDgy8= 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 1556971831623537.4782755796806; Sat, 4 May 2019 05:10:31 -0700 (PDT) Received: from localhost ([127.0.0.1]:55696 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtUq-0002F7-Dw for importer@patchew.org; Sat, 04 May 2019 08:10:28 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50375) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtQA-000795-Dd for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMtQ7-0001CK-0m for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:37 -0400 Received: from mail-ot1-x344.google.com ([2607:f8b0:4864:20::344]:41695) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMtQ6-0001Bq-Oi; Sat, 04 May 2019 08:05:34 -0400 Received: by mail-ot1-x344.google.com with SMTP id g8so7624747otl.8; Sat, 04 May 2019 05:05:34 -0700 (PDT) Received: from localhost.localdomain ([198.59.53.9]) by smtp.gmail.com with ESMTPSA id s26sm1844978otk.24.2019.05.04.05.05.32 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 04 May 2019 05:05:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JLVM6LVe8Ci/wZhURG+abGdUOpJKnFkyRFzRhc1SJWU=; b=XjGrNLQ7KSyKQyu5UmbwKRrE0WcO8JeUSinNAWStQOurprmn5Vzj9dGaemEKFp7tM4 8NpNqASxAcAOQ1faSaaSrjq3b5QU623zKuwPkGlgrvLHx2PYX3uVc25whrztlJBR9sch jpkwCVBMuBCkNCqP7NA3MPXnnuN8RIjqh0Z1HAdCqCW/3yo3xuYDblazypY7j67Nc/5Z Vm5ItF8sFd+3FJJXPa+7eBpmzRRJPgN2GEsMxKzbudloWE89441BvJo53gUTitQ40I9a /GKVAE4CpfMxQEarVQHoVjee67mjtaghic1GlCfNCnKR3x01lay3cBnnr1k7UlBITfse PV/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=JLVM6LVe8Ci/wZhURG+abGdUOpJKnFkyRFzRhc1SJWU=; b=gysFq4+r4qqfWaDoJcqfyADhBMqmXLPVB/rpIcbu1vuticTvYp/igIPJE68Ob/63ve 9P27Y10kWyB77K3uDxpoo0Agmfkm/HrdvSeXTs2pRgCP0II4d1BW/kgmVadToKjDPs+0 TQHhSSUamv95lxrY1dGBmrPe8s70uz8GuH4FFxqZnmAi8sV9DrDyS9qe33qdJh4i9nwp KhuC+FN3e7Gx+c0xU51xZ6aDALrl5M63dhj04m9/Qk8ZIxtktUC/0HzLPT6XA74lUt26 h7tKZmPhNF/w6lKbVWf2IMESue1vgMWnKeJkUBFlSo9n9HxZw1+b4rc0ELapQzEMTArX YPwA== X-Gm-Message-State: APjAAAX7UHDjBKVq/dItYqJB6CZWdTqL3nkkOaT/fdE8ibLkhVjUNL2z iXVs8r7mnud1ZNe1nWU+uzptT+NC X-Google-Smtp-Source: APXvYqwFgA6xZyLcLIz6WrCS29JaaXR+3+Z4YuzBt62cE9J8ViTsxDmC7YyA6payPMOi+y6zu2wDvQ== X-Received: by 2002:a9d:67:: with SMTP id 94mr10123010ota.57.1556971533699; Sat, 04 May 2019 05:05:33 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 4 May 2019 06:05:21 -0600 Message-Id: <20190504120528.6389-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190504120528.6389-1-pbonzini@redhat.com> References: <20190504120528.6389-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::344 Subject: [Qemu-devel] [PATCH 3/9] coroutine: add host specific coroutine backend for 64-bit x86 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, cohuck@redhat.com, richard.henderson@linaro.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This backend is faster (100ns vs 150ns per switch on my laptop), but especially it will be possible to add CET support to it. Most of the code is actually not architecture specific. Signed-off-by: Paolo Bonzini Reviewed-by: Richard Henderson Reviewed-by: Stefan Hajnoczi --- configure | 10 ++ scripts/qemugdb/coroutine.py | 5 +- scripts/qemugdb/coroutine_asm.py | 20 +++ util/Makefile.objs | 1 + util/coroutine-asm.c | 230 +++++++++++++++++++++++++++++++ 5 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 scripts/qemugdb/coroutine_asm.py create mode 100644 util/coroutine-asm.c diff --git a/configure b/configure index 5b183c2e39..c01f57a3ae 100755 --- a/configure +++ b/configure @@ -5200,6 +5200,8 @@ fi if test "$coroutine" =3D ""; then if test "$mingw32" =3D "yes"; then coroutine=3Dwin32 + elif test "$cpu" =3D "x86_64"; then + coroutine=3Dasm elif test "$ucontext_works" =3D "yes"; then coroutine=3Ducontext else @@ -5225,6 +5227,14 @@ else error_exit "only the 'windows' coroutine backend is valid for Window= s" fi ;; + asm) + if test "$mingw32" =3D "yes"; then + error_exit "only the 'windows' coroutine backend is valid for Window= s" + fi + if test "$cpu" !=3D "x86_64"; then + error_exit "the 'asm' 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 076f6808ab..dc760235e7 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. =20 -from . import coroutine_ucontext +from . import coroutine_ucontext, coroutine_asm import gdb =20 VOID_PTR =3D gdb.lookup_type('void').pointer() UINTPTR_T =3D gdb.lookup_type('uintptr_t') =20 backends =3D { - 'CoroutineUContext': coroutine_ucontext + 'CoroutineUContext': coroutine_ucontext, + 'CoroutineAsm': coroutine_asm } =20 def coroutine_backend(): diff --git a/scripts/qemugdb/coroutine_asm.py b/scripts/qemugdb/coroutine_a= sm.py new file mode 100644 index 0000000000..b4ac1291db --- /dev/null +++ b/scripts/qemugdb/coroutine_asm.py @@ -0,0 +1,20 @@ +#!/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 =3D gdb.lookup_type('uint64_t').pointer() + +def get_coroutine_regs(addr): + addr =3D addr.cast(gdb.lookup_type('CoroutineAsm').pointer()) + rsp =3D addr['sp'].cast(U64_PTR) + return {'sp': rsp, 'pc': rsp.dereference()} diff --git a/util/Makefile.objs b/util/Makefile.objs index 9206878dec..41a10539cf 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -39,6 +39,7 @@ util-obj-$(CONFIG_MEMBARRIER) +=3D sys_membarrier.o util-obj-y +=3D qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o util-obj-y +=3D qemu-coroutine-sleep.o util-obj-y +=3D coroutine-$(CONFIG_COROUTINE_BACKEND).o +coroutine-asm.o-cflags :=3D -mno-red-zone util-obj-y +=3D buffer.o util-obj-y +=3D timed-average.o util-obj-y +=3D base64.o diff --git a/util/coroutine-asm.c b/util/coroutine-asm.c new file mode 100644 index 0000000000..a06ecbcb0a --- /dev/null +++ b/util/coroutine-asm.c @@ -0,0 +1,230 @@ +/* + * Host-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 . + */ + +#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 + +#define COROUTINE_SHADOW_STACK_SIZE 4096 + +typedef struct { + Coroutine base; + void *sp; + + void *stack; + size_t stack_size; + +#ifdef CONFIG_VALGRIND_H + unsigned int valgrind_stack_id; +#endif +} CoroutineAsm; + +/** + * Per-thread coroutine bookkeeping + */ +static __thread CoroutineAsm 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_ol= d); + + if (!leader.stack) { + leader.stack =3D (void *)bottom_old; + leader.stack_size =3D 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 +} + +#ifdef __x86_64__ +/* + * We hardcode all operands to specific registers so that we can write dow= n all the + * others in the clobber list. Note that action also needs to be hardcode= d so that + * it is the same register in all expansions of this macro. Also, we use = %rdi + * for the coroutine because that is the ABI's first argument register; + * coroutine_trampoline can then retrieve the current coroutine from there. + * + * Note that push and call would clobber the red zone. Makefile.objs comp= iles this + * file with -mno-red-zone. The alternative is to subtract/add 128 bytes = from rsp + * around the switch, with slightly lower cache performance. + */ +#define CO_SWITCH(from, to, action, jump) ({ = \ + int action_ =3D action; = \ + void *from_ =3D from; = \ + void *to_ =3D to; = \ + asm volatile( = \ + "pushq %%rbp\n" /* save frame register on sour= ce stack */ \ + ".cfi_adjust_cfa_offset 8\n" = \ + "call 1f\n" /* switch continues at label 1= */ \ + "jmp 2f\n" /* switch back continues at la= bel 2 */ \ + = \ + "1: .cfi_adjust_cfa_offset 8\n" = \ + "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" = \ + : "+a" (action_), [FROM] "+b" (from_), [TO] "+D" (to_) = \ + : [SP] "i" (offsetof(CoroutineAsm, sp)) = \ + : "rcx", "rdx", "rsi", "r8", "r9", "r10", "r11", "r12", "r13", "r1= 4", "r15", \ + "memory"); = \ + action_; = \ +}) +/* Use "call" to ensure the stack is aligned correctly. */ +#define CO_SWITCH_NEW(from, to) CO_SWITCH(from, to, 0, "call coroutine_tra= mpoline") +#define CO_SWITCH_RET(from, to, action) CO_SWITCH(from, to, action, "ret") +#else +#error coroutine-asm.c not ported to this architecture. +#endif + +static void __attribute__((__used__)) coroutine_trampoline(CoroutineAsm *s= elf) +{ + finish_switch_fiber(NULL); + + while (true) { + Coroutine *co =3D &self->base; + qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); + co->entry(co->entry_arg); + } +} + +Coroutine *qemu_coroutine_new(void) +{ + CoroutineAsm *co; + void *fake_stack_save =3D NULL; + + co =3D g_malloc0(sizeof(*co)); + co->stack_size =3D COROUTINE_STACK_SIZE; + co->stack =3D qemu_alloc_stack(&co->stack_size); + co->sp =3D co->stack + co->stack_size; + +#ifdef CONFIG_VALGRIND_H + co->valgrind_stack_id =3D + VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size); +#endif + + /* + * Immediately enter the coroutine once to initialize the stack + * and program counter. We could instead just push the address + * of coroutine_trampoline and let qemu_coroutine_switch return + * to it, but doing it this way confines the non-portable code + * to the CO_SWITCH* macros. + */ + co->base.caller =3D qemu_coroutine_self(); + start_switch_fiber(&fake_stack_save, co->stack, co->stack_size); + CO_SWITCH_NEW(current, co); + finish_switch_fiber(fake_stack_save); + co->base.caller =3D 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(CoroutineAsm *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_) +{ + CoroutineAsm *co =3D DO_UPCAST(CoroutineAsm, 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) +{ + CoroutineAsm *from =3D DO_UPCAST(CoroutineAsm, base, from_); + CoroutineAsm *to =3D DO_UPCAST(CoroutineAsm, base, to_); + void *fake_stack_save =3D NULL; + + current =3D to_; + + start_switch_fiber(action =3D=3D COROUTINE_TERMINATE ? + NULL : &fake_stack_save, to->stack, to->stack_size); + action =3D CO_SWITCH_RET(from, to, action); + finish_switch_fiber(fake_stack_save); + + return action; +} + +Coroutine *qemu_coroutine_self(void) +{ + if (!current) { + current =3D &leader.base; + } + return current; +} + +bool qemu_in_coroutine(void) +{ + return current && current->caller; +} --=20 2.21.0 From nobody Tue May 7 04:32:27 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1556971988; cv=none; d=zoho.com; s=zohoarc; b=lakjfQI3nJGll3u3s8qMJJ9Kz0wZOLDhYFwqCuwhoFIaVIRhzSMyR7PV/2CoFyry1GPdWsuMq6YusIF2aquoRVBYxXt+2eiZKI5m5nmKh+hWdW0OsD5+BJm6NpJbC5cmZQzsfVZ7koSQQbt/GCwNWyvXN1wlhyrnSJUxAeAH44c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556971988; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=ahvxSVh2QRrmKy4BCWONH7xM6dTvjNFat384VGW5p48=; b=Vc+5M7wLWthGjMfVUCuG6NIsS+A/Y2GpMNn/Im4v9v4PgVl2jnTovTyG8/0TaCj3ovih25RrsfGfOK4rSlsaVTbST/LoNPpNKCHO1S0TUX1TH6qXRTGti2QaMlJpkJbTmSM0OX/HmNHwSD8wH5UScYce6j+SIJ192qxuXG2z9/c= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=temperror (zoho.com: Error in retrieving data from DNS) 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 1556971988797847.7245411663723; Sat, 4 May 2019 05:13:08 -0700 (PDT) Received: from localhost ([127.0.0.1]:55750 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtXB-0004W1-Ch for importer@patchew.org; Sat, 04 May 2019 08:12:53 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50406) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtQD-0007BQ-Sl for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMtQA-0001Dd-EB for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:40 -0400 Received: from mail-ot1-x32b.google.com ([2607:f8b0:4864:20::32b]:35930) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMtQ8-0001CW-Ko; Sat, 04 May 2019 08:05:37 -0400 Received: by mail-ot1-x32b.google.com with SMTP id b18so7642080otq.3; Sat, 04 May 2019 05:05:35 -0700 (PDT) Received: from localhost.localdomain ([198.59.53.9]) by smtp.gmail.com with ESMTPSA id s26sm1844978otk.24.2019.05.04.05.05.33 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 04 May 2019 05:05:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ahvxSVh2QRrmKy4BCWONH7xM6dTvjNFat384VGW5p48=; b=DlhuQB22RV1+3Fd5TonZaG+STyJJDViJefRG+GqcluWwvMh3UG8r7XIjHMr4QibUuM QkA0I+WfdBOGDRFfXFeuKbYFL0LgkhKhXw/Nb/7dshKXPTgaKwI7CWcyUh46wFsXOrSV bD2o5tJTReYJWdXZgpjbfJz22wBrqzaN/P5mQSaFYyFc7FaP3/7ozq9sCzULtPjY6hK9 KShiX2Az+87XsIREe6bu30FshS31mOe6P7Z+4IxxBCvQSLscz5qC7W983wwifbUvJCp0 jcS6utiq5+YVGR//dQPLx221dr14hlPG8yD7G7aQweKqpEGeGWFffn3bhi93Ys5NajHm xTUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=ahvxSVh2QRrmKy4BCWONH7xM6dTvjNFat384VGW5p48=; b=l21mOjprKJPDceTuz3tAzaRyRNDsbyq2BDO1ee02wo4wG4AFzISA3ZRVbBEH5cEM49 grgfzkHXewE23MR4HuDB3f9fqisDA2M4Zj3ugUYYZLEnJxTD2S+Lsjo8mtK/6eQnMQZE re6QkxZCIb0ZJjw9PGa257mOkg4XuDIYV4oZcIOiqO8O92DH55h/SwM1MoqMm+EywYH0 8PMVxb/OkwhWcQjrvEcJ+XAds2fFM5yyl8UXJo6JyoioPPBN4dcrZDre7jhGD2dsy/Jy JGothV/Pjrtm5K4wZ3560psKnaWPays0I+HL+Lw0hoUWN0WhxgUcswxAzInufzp1PxJb XXQQ== X-Gm-Message-State: APjAAAX874+jL/7EhvL1p7cijLGCsExTMo9gKY7VmPiHqCQJMHayNB4z TuF5XHVUbd4Z0KblZScQy1YLLjwx X-Google-Smtp-Source: APXvYqzQKTNLeqe5MBi7n5ye0an2mSuXMLB7grlxz4GH47D5Z4jl7b8X8VWuM06BHcDbDOP0V+Zj9A== X-Received: by 2002:a05:6830:16ce:: with SMTP id l14mr9987365otr.108.1556971534966; Sat, 04 May 2019 05:05:34 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 4 May 2019 06:05:22 -0600 Message-Id: <20190504120528.6389-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190504120528.6389-1-pbonzini@redhat.com> References: <20190504120528.6389-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::32b Subject: [Qemu-devel] [PATCH 4/9] coroutine: add host specific coroutine backend for 64-bit ARM 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, cohuck@redhat.com, richard.henderson@linaro.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The speedup is similar to x86, 120 ns vs 180 ns on an APM Mustang. Signed-off-by: Paolo Bonzini Reviewed-by: Richard Henderson Reviewed-by: Stefan Hajnoczi --- configure | 2 +- scripts/qemugdb/coroutine_asm.py | 6 ++++- util/Makefile.objs | 2 ++ util/coroutine-asm.c | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/configure b/configure index c01f57a3ae..26e62a4ab1 100755 --- a/configure +++ b/configure @@ -5200,7 +5200,7 @@ fi if test "$coroutine" =3D ""; then if test "$mingw32" =3D "yes"; then coroutine=3Dwin32 - elif test "$cpu" =3D "x86_64"; then + elif test "$cpu" =3D "x86_64" || test "$cpu" =3D "aarch64"; then coroutine=3Dasm elif test "$ucontext_works" =3D "yes"; then coroutine=3Ducontext diff --git a/scripts/qemugdb/coroutine_asm.py b/scripts/qemugdb/coroutine_a= sm.py index b4ac1291db..181b77287b 100644 --- a/scripts/qemugdb/coroutine_asm.py +++ b/scripts/qemugdb/coroutine_asm.py @@ -17,4 +17,8 @@ U64_PTR =3D gdb.lookup_type('uint64_t').pointer() def get_coroutine_regs(addr): addr =3D addr.cast(gdb.lookup_type('CoroutineAsm').pointer()) rsp =3D addr['sp'].cast(U64_PTR) - return {'sp': rsp, 'pc': rsp.dereference()} + arch =3D gdb.selected_frame().architecture.name().split(':'): + if arch[0] =3D=3D 'i386' and arch[1] =3D=3D 'x86-64': + return {'rsp': rsp, 'pc': rsp.dereference()} + else: + return {'sp': rsp, 'pc': addr['scratch'].cast(U64_PTR) } diff --git a/util/Makefile.objs b/util/Makefile.objs index 41a10539cf..2167ffc862 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -39,7 +39,9 @@ util-obj-$(CONFIG_MEMBARRIER) +=3D sys_membarrier.o util-obj-y +=3D qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o util-obj-y +=3D qemu-coroutine-sleep.o util-obj-y +=3D coroutine-$(CONFIG_COROUTINE_BACKEND).o +ifeq ($(ARCH),x86_64) coroutine-asm.o-cflags :=3D -mno-red-zone +endif util-obj-y +=3D buffer.o util-obj-y +=3D timed-average.o util-obj-y +=3D base64.o diff --git a/util/coroutine-asm.c b/util/coroutine-asm.c index a06ecbcb0a..de68e98622 100644 --- a/util/coroutine-asm.c +++ b/util/coroutine-asm.c @@ -40,6 +40,11 @@ typedef struct { Coroutine base; void *sp; =20 + /* + * aarch64: instruction pointer + */ + void *scratch; + void *stack; size_t stack_size; =20 @@ -116,6 +121,49 @@ static void start_switch_fiber(void **fake_stack_save, /* Use "call" to ensure the stack is aligned correctly. */ #define CO_SWITCH_NEW(from, to) CO_SWITCH(from, to, 0, "call coroutine_tra= mpoline") #define CO_SWITCH_RET(from, to, action) CO_SWITCH(from, to, action, "ret") + +#elif defined __aarch64__ +/* + * GCC does not support clobbering the frame pointer, so we save it oursel= ves. + * Saving the link register as well generates slightly better code because= then + * qemu_coroutine_switch can be treated as a leaf procedure. + */ +#define CO_SWITCH_RET(from, to, action) ({ = \ + register uintptr_t action_ __asm__("x0") =3D action; = \ + register void *from_ __asm__("x16") =3D from; = \ + register void *to_ __asm__("x1") =3D to; = \ + asm volatile( = \ + ".cfi_remember_state\n" = \ + "stp x29, x30, [sp, #-16]!\n" /* GCC does not save it, do it ou= rselves */ \ + ".cfi_adjust_cfa_offset 16\n" = \ + ".cfi_def_cfa_register sp\n" = \ + "adr x30, 2f\n" /* source PC will be after the BR= */ \ + "str x30, [x16, %[SCRATCH]]\n" /* save it */ = \ + "mov x30, sp\n" /* save source SP */ = \ + "str x30, [x16, %[SP]]\n" = \ + "ldr x30, [x1, %[SCRATCH]]\n" /* load destination PC */ = \ + "ldr x1, [x1, %[SP]]\n" /* load destination SP */ = \ + "mov sp, x1\n" = \ + "br x30\n" = \ + "2: \n" = \ + "ldp x29, x30, [sp], #16\n" = \ + ".cfi_restore_state\n" = \ + : "+r" (action_), "+r" (from_), "+r" (to_) = \ + : [SP] "i" (offsetof(CoroutineAsm, sp)), = \ + [SCRATCH] "i" (offsetof(CoroutineAsm, scratch)) = \ + : "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x= 12", \ + "x13", "x14", "x15", "x17", "x18", "x19", "x20", "x21", "x22", "= x23", \ + "x24", "x25", "x26", "x27", "x28", = \ + "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10= ", "v11", \ + "v12", "v13", "v14", "v15", v16", "v17", "v18", "v19", "v20", "v= 21", "v22", \ + "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", "= memory", \ + action_; = \ +}) + +#define CO_SWITCH_NEW(from, to) do { = \ + (to)->scratch =3D (void *) coroutine_trampoline; = \ + (void) CO_SWITCH_RET(from, to, (uintptr_t) to); = \ +} while(0) #else #error coroutine-asm.c not ported to this architecture. #endif --=20 2.21.0 From nobody Tue May 7 04:32:27 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1556971681; cv=none; d=zoho.com; s=zohoarc; b=ZmJ1AlxsD7YWGYxBbNDeeBpf39TCp0Qb6ENsF4pO6EtADWKN82YyRycZrm6QPmb0mscavxT0lK6CV2PVGXS/EHslHe34KloBefDDHiW6kTnBhZhj/xbU3wXLvDRyqZa1B6u7szokKcEw98hB/BlbcuFcq7czP15TUhyRuizYK7I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556971681; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=0TS4qEKmw4BOwv+KpHsCK8QIkMFbVbLGctn5J+3azt8=; b=UL3r8AbzqF+uhBsLqhgv/TcFdFiXQwDC5JNkQXmPyIlPsKqTFYSnnoQHMYPb7J5aRpI7rCWJTr0BcWBjOnEvRHTKd87fv8OSflNhw3tzxsj9SogqUEuII1oRZenvrEGZx+q7asLAImWSuYQoYmvMzQhP98JFBwzLzLskWUKk7dc= 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 1556971681482104.29304454154965; Sat, 4 May 2019 05:08:01 -0700 (PDT) Received: from localhost ([127.0.0.1]:55670 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtSL-0008Um-BC for importer@patchew.org; Sat, 04 May 2019 08:07:53 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50448) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtQI-0007F9-2A for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMtQE-0001FI-BY for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:44 -0400 Received: from mail-ot1-x344.google.com ([2607:f8b0:4864:20::344]:44874) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMtQA-0001Ch-Cj; Sat, 04 May 2019 08:05:40 -0400 Received: by mail-ot1-x344.google.com with SMTP id d10so2529023otp.11; Sat, 04 May 2019 05:05:36 -0700 (PDT) Received: from localhost.localdomain ([198.59.53.9]) by smtp.gmail.com with ESMTPSA id s26sm1844978otk.24.2019.05.04.05.05.35 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 04 May 2019 05:05:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0TS4qEKmw4BOwv+KpHsCK8QIkMFbVbLGctn5J+3azt8=; b=YEXDo7B+eNmhUG2unvRXA9tvDkgK9aP+81UlIucm1mkXG6mRRt4Z9E+sPo592EpCpv RJKTgIdogs4/jwSfZe0hXRJA2ivtLmIJ+NguswMNkh9WGbaRYH+NaAnFNueBFEJPA5Hs ZF5y+PWorFQoOCTHNXFvG+Avj23y4aX7gBK4R4SwzOOrHUFYI8UGd9clKwBlU8iawfaR 7HlRdNteXH+//6GSVE4mG+s4lZ2yGDspaf2EaI4F4TUVBg4GLvF0BDdMvEJSrqP7yw5x YobuQDpqGO51nCTxEKfC+fkEzW6QqIpjZ84Om32S0OaRnLEL5dr3184FCoOYOlA59kVr R2PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=0TS4qEKmw4BOwv+KpHsCK8QIkMFbVbLGctn5J+3azt8=; b=MzHcSAJgcmCDKCAtaQ7tS1rQsdPXIFx0VmFxvXwpOE2Pws0E7GedaIVmkkAmXlLs+d BUUT9UIZBS/TFgqCbkJpHkLbe32YkUCykZL7CBTcrKeKlWVsI0jVL898i9tTuFmDfA+O tBPuCKTOJ0QDzWYFuXdR57pYv3y9ndQJL17qZThEwxCZhFSuYP9SiyEuQ6XaEGUDWGKP 42FSLgtz8bZ3P/xyHszg1u9OiJ837nLMqBlLq1vvbyzgaDfmzX1yJDOB7xlRPROXo6PQ AdKZHMhNu2bgo6jGf48Lhgs0EnxywNOHDgNtkOtN0NDmxds4NU3g06XzwngJBMe23BAa iS2Q== X-Gm-Message-State: APjAAAVWV9K8tytLSPRrdP4vWUDxwUfSP5ZR8mNr5ONMIZr/U7UmEjw1 t5YzbZNIlH3qjo0Ms9aiz1xK7rwj X-Google-Smtp-Source: APXvYqz1+oydh5bQXcmeOB86Dy8JWmKvwXNUea3I/l/tzyVW1o7Hs/ZQKewreZp7q1KP90L0rdXgbg== X-Received: by 2002:a9d:5d03:: with SMTP id b3mr9796065oti.268.1556971535928; Sat, 04 May 2019 05:05:35 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 4 May 2019 06:05:23 -0600 Message-Id: <20190504120528.6389-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190504120528.6389-1-pbonzini@redhat.com> References: <20190504120528.6389-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::344 Subject: [Qemu-devel] [PATCH 5/9] coroutine: add host specific coroutine backend for 64-bit s390 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, cohuck@redhat.com, richard.henderson@linaro.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- util/coroutine-asm.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/util/coroutine-asm.c b/util/coroutine-asm.c index de68e98622..a9a80e9c71 100644 --- a/util/coroutine-asm.c +++ b/util/coroutine-asm.c @@ -41,7 +41,7 @@ typedef struct { void *sp; =20 /* - * aarch64: instruction pointer + * aarch64, s390x: instruction pointer */ void *scratch; =20 @@ -161,6 +161,40 @@ static void start_switch_fiber(void **fake_stack_save, (to)->scratch =3D (void *) coroutine_trampoline; = \ (void) CO_SWITCH_RET(from, to, (uintptr_t) to); = \ } while(0) + +#elif defined __s390x__ +#define CO_SWITCH_RET(from, to, action) ({ = \ + register uintptr_t action_ __asm__("r2") =3D action; = \ + register void *from_ __asm__("r1") =3D from; = \ + register void *to_ __asm__("r3") =3D to; = \ + register void *pc_ __asm__("r4") =3D to->scratch; = \ + void *save_r13; = \ + asm volatile( = \ + "stg %%r13, %[SAVE_R13]\n" = \ + "stg %%r15, %[SP](%%r1)\n" /* save source SP */ = \ + "lg %%r15, %[SP](%%r3)\n" /* load destination SP */ = \ + "bras %%r3, 1f\n" /* source PC will be after the BR= */ \ + "1: aghi %%r3, 12\n" /* 4 */ = \ + "stg %%r3, %[SCRATCH](%%r1)\n" /* 6 save switch-back PC */ = \ + "br %%r4\n" /* 2 jump to destination PC */ = \ + "lg %%r13, %[SAVE_R13]\n" = \ + : "+r" (action_), "+r" (from_), "+r" (to_), "+r" (pc_), = \ + [SAVE_R13] "+m" (r13) = \ + : [SP] "i" (offsetof(CoroutineAsm, sp)), = \ + [SCRATCH] "i" (offsetof(CoroutineAsm, scratch)) = \ + : "r0", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12", "r14", = \ + "a2", "a3", "a4", "a5", "a6", "a7", = \ + "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15", = \ + "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", = \ + "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", "memory");= \ + action_; = \ +}) + +#define CO_SWITCH_NEW(from, to) do { = \ + (to)->scratch =3D (void *) coroutine_trampoline; = \ + (to)->sp -=3D 160; = \ + (void) CO_SWITCH_RET(from, to, (uintptr_t) to); = \ +} while(0) #else #error coroutine-asm.c not ported to this architecture. #endif --=20 2.21.0 From nobody Tue May 7 04:32:27 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1556972000; cv=none; d=zoho.com; s=zohoarc; b=CSX2v/H0BsjZ4Qvv5f1XqOc4S/hP+8EUP+QEvd5uhurZkOgh9TAcYUeVOcw/VIg/2bUTZ/2VDSGhU9xqopgeivAaMs65ncC1IGAHOH+Y6t2p91KoocAue7KiPqr5Y9132Va+7A/R9BMLOhBYriIAnlhBmutM73zrTMW4eLNfbx4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556972000; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=VaIf1nGi8yWSPl2rVwrgq2u8YFPcN84q7VRUDJ67mPk=; b=jzXsolw9/uAhf3JN1EieChkXM3eweLSRnfgn0SKc+6+f+5di90SYBA0XBjQ+TAVKz7rLbuVfm+v8VGfDrmbxk5ZdcNXihMZ0S9bYhqtJiEIHcTJW3w7cG9xfccEUo1F7USOS8ztoXuO4xgfoTL72c8bsKd1mEFDYcEIgGcOfKvU= 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 1556972000810147.90224762618732; Sat, 4 May 2019 05:13:20 -0700 (PDT) Received: from localhost ([127.0.0.1]:55754 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtXQ-0004iG-JV for importer@patchew.org; Sat, 04 May 2019 08:13:08 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50447) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtQI-0007F6-1k for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMtQE-0001FN-CA for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:44 -0400 Received: from mail-oi1-x22a.google.com ([2607:f8b0:4864:20::22a]:38607) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMtQB-0001DH-OW; Sat, 04 May 2019 08:05:40 -0400 Received: by mail-oi1-x22a.google.com with SMTP id t70so6408403oif.5; Sat, 04 May 2019 05:05:37 -0700 (PDT) Received: from localhost.localdomain ([198.59.53.9]) by smtp.gmail.com with ESMTPSA id s26sm1844978otk.24.2019.05.04.05.05.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 04 May 2019 05:05:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VaIf1nGi8yWSPl2rVwrgq2u8YFPcN84q7VRUDJ67mPk=; b=uZUhqVube9FIn/nOqLtisPPWqnKsG0hJL9gKJ+FWYKov5drEyE8qxOXnq8OTRuafhL eF2TCobdu7eed8CQgKTlL2MQIf9pVT9iJpxiSm7Gmo4GlgKTIhkyatotrXPgZvSknzap qRo2NPbITtDnu2uP4Sl2VQD224fAcQygt5t6Hm3yEBd599ftegboqbzx10ttg2jN+9j+ z8Me5s4YuPf2YQI1ez2tCWvaht162CGFmkkehOf5GO9c1GsZ5E6T6wA6x4JUoiSN5HsJ D5/khL+01XVBq3v098Tn2UGpGgzXHdBAmh5b9d9ekUKo5eCHWq/Q+4fFVo+jDt4jgT4m 1Whg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=VaIf1nGi8yWSPl2rVwrgq2u8YFPcN84q7VRUDJ67mPk=; b=fKEecHZfQ1I6Eg/OAXBXO/mXh5E+ag/Kyo1HulOlJv0S83tNH1YcXtx5iI04kIdQfu ida4x1bb3TKVhMO20gHLc+COAz70Cc67outGiHCn784OxPfpJ0Q3LDfZJYpCETzUpRyU 30Ct0O78hxYd+mDxRBGY1hZYILgmNhXYSxZ+LudvhI3De8XlscdR4muabHASjmGsbOHE vUqfQkgu4rEE6Fw5XPZcawfx+OugXAAU+YPDThR9H9d1GYdQpQpnYifjGrkxjKmTvdvG nbb4MxVl1J/9OydCBlQdkR8D5QWbK73T00k2u6OaReTW6xXUk97HNDCYiqLKB18ZA1ol mHmw== X-Gm-Message-State: APjAAAWaqKOt3ozcfeMVeTfNUJXVqraV4B5imDtX71+topg94P78Iq9B j+CfWtxoCwr+usO7Z2oorKjSuF53 X-Google-Smtp-Source: APXvYqziEws3+tuF9GmFg7kuJtEzbHA562UxtgtWzmYTms1kV7h+SzuIzxxqKAsCS/jAseQwM5WRqw== X-Received: by 2002:aca:4482:: with SMTP id r124mr2442181oia.39.1556971536857; Sat, 04 May 2019 05:05:36 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 4 May 2019 06:05:24 -0600 Message-Id: <20190504120528.6389-7-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190504120528.6389-1-pbonzini@redhat.com> References: <20190504120528.6389-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::22a Subject: [Qemu-devel] [PATCH 6/9] configure: add control-flow protection support 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, cohuck@redhat.com, richard.henderson@linaro.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Control-flow protection requires object files to note which features are supported. The linker will merge them to the set of features that are supported by all object files. The compiler creates these notes when the -fcf-protection option is passed, but we have to blacklist some object files that only support a subset of the full control-flow protection feature set. Even without any further host-specific patches, user-mode emulation binaries can already use shadow stacks, because they don't need coroutines and don't include the problematic util/coroutine-*.o object files. Likewise, system-mode emulation binaries will enable indirect branch tracking if built without TCG support. The next patches will improve the situation so that QEMU can be built with full protection on x86 hosts. Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- Makefile.target | 3 +++ configure | 29 +++++++++++++++++++++++++++++ util/Makefile.objs | 5 +++++ 3 files changed, 37 insertions(+) diff --git a/Makefile.target b/Makefile.target index ae02495951..667682779b 100644 --- a/Makefile.target +++ b/Makefile.target @@ -111,6 +111,9 @@ obj-y +=3D exec.o obj-y +=3D accel/ obj-$(CONFIG_TCG) +=3D tcg/tcg.o tcg/tcg-op.o tcg/tcg-op-vec.o tcg/tcg-op-= gvec.o obj-$(CONFIG_TCG) +=3D tcg/tcg-common.o tcg/optimize.o +ifeq ($(CONFIG_CF_PROTECTION),y) +tcg/tcg.o-cflags :=3D -fcf-protection=3Dreturn +endif obj-$(CONFIG_TCG_INTERPRETER) +=3D tcg/tci.o obj-$(CONFIG_TCG_INTERPRETER) +=3D disas/tci.o obj-$(CONFIG_TCG) +=3D fpu/softfloat.o diff --git a/configure b/configure index 26e62a4ab1..946ff7825a 100755 --- a/configure +++ b/configure @@ -449,6 +449,7 @@ win_sdk=3D"no" want_tools=3D"yes" libiscsi=3D"" libnfs=3D"" +cf_protection=3D"no" # leave it disabled until we can test performance coroutine=3D"" coroutine_pool=3D"" debug_stack_usage=3D"no" @@ -1267,6 +1268,10 @@ for opt do ;; --with-pkgversion=3D*) pkgversion=3D"$optarg" ;; + --enable-cf-protection) cf_protection=3D"yes" + ;; + --disable-cf-protection) cf_protection=3D"no" + ;; --with-coroutine=3D*) coroutine=3D"$optarg" ;; --disable-coroutine-pool) coroutine_pool=3D"no" @@ -1796,6 +1801,7 @@ disabled with --disable-FEATURE, default is enabled i= f available: lzfse support of lzfse compression library (for reading lzfse-compressed dmg images) seccomp seccomp support + cf-protection Control-flow protection coroutine-pool coroutine freelist (better performance) glusterfs GlusterFS backend tpm TPM support @@ -5176,6 +5182,25 @@ if have_backend "dtrace"; then fi fi =20 +########################################## +# detect Control-flow protection support in the toolchain + +if test "$cf_protection" !=3D no; then + write_c_skeleton; + if ! compile_prog "-fcf-protection" "" ; then + if test "$cf_protection" =3D yes; then + feature_not_found "cf_protection" 'Control-flow protection is not su= pported by your toolchain' + fi + cf_protection=3Dno + fi +fi +if test "$cf_protection" =3D ""; then + cf_protection=3Dyes +fi +if test "$cf_protection" =3D "yes"; then + QEMU_CFLAGS=3D"-fcf-protection $QEMU_CFLAGS" +fi + ########################################## # check and set a backend for coroutine =20 @@ -6361,6 +6386,7 @@ echo "netmap support $netmap" echo "Linux AIO support $linux_aio" echo "ATTR/XATTR support $attr" echo "Install blobs $blobs" +echo "CF protection $cf_protection" echo "KVM support $kvm" echo "HAX support $hax" echo "HVF support $hvf" @@ -6571,6 +6597,9 @@ fi if test "$profiler" =3D "yes" ; then echo "CONFIG_PROFILER=3Dy" >> $config_host_mak fi +if test "$cf_protection" =3D "yes" ; then + echo "CONFIG_CF_PROTECTION=3Dy" >> $config_host_mak +fi if test "$slirp" !=3D "no"; then echo "CONFIG_SLIRP=3Dy" >> $config_host_mak echo "CONFIG_SMBD_COMMAND=3D\"$smbd\"" >> $config_host_mak diff --git a/util/Makefile.objs b/util/Makefile.objs index 2167ffc862..d7add70b63 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -42,6 +42,11 @@ util-obj-y +=3D coroutine-$(CONFIG_COROUTINE_BACKEND).o ifeq ($(ARCH),x86_64) coroutine-asm.o-cflags :=3D -mno-red-zone endif +ifeq ($(CONFIG_CF_PROTECTION),y) +coroutine-sigaltstack.o-cflags :=3D -fcf-protection=3Dbranch +coroutine-ucontext.o-cflags :=3D -fcf-protection=3Dbranch +coroutine-asm.o-cflags +=3D -fcf-protection=3Dbranch +endif util-obj-y +=3D buffer.o util-obj-y +=3D timed-average.o util-obj-y +=3D base64.o --=20 2.21.0 From nobody Tue May 7 04:32:27 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1556971846; cv=none; d=zoho.com; s=zohoarc; b=BLPPMcDGdBC3wFo4b2ie8cqspPqe+xG/vqTYsPU7UjtJJQ2zBoTJ4vkFeC+jd+dq8w3nRiJQjm5PGFxFPjvsWW3aD9kEQBbBO5mxUa7rpaz9s2HUlgjS/EpTKqemlTU7YdScGSxqPe1dGWRmoxBNU+PYc+LB8qe8PYdgSAuOtn0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556971846; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=GntLoJ5LAJvOCja8qBYrU8/E9MBsCe+uCkJo7D67Aro=; b=AJw1cBiMiZTxpdj2Ng7zpe2HN29L1Mpoio2faIM80giEqx/zZjvnyd1k0u1X5fkmEcoAwnBgSUuzV8mAOwLfZYXik2ZUwu+qPMPVsGFThG2O/TfbPkM1pIjWprZFkazfgz/1FUyyJ4mtPtmMKwf1VSCkLVXZt7BrauaaIHym0PY= 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 1556971846345710.0791303092196; Sat, 4 May 2019 05:10:46 -0700 (PDT) Received: from localhost ([127.0.0.1]:55716 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtV5-0002Sp-1c for importer@patchew.org; Sat, 04 May 2019 08:10:43 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50502) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtQL-0007IS-CZ for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMtQJ-0001H5-Na for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:49 -0400 Received: from mail-oi1-x241.google.com ([2607:f8b0:4864:20::241]:36186) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMtQG-0001DN-5K; Sat, 04 May 2019 08:05:46 -0400 Received: by mail-oi1-x241.google.com with SMTP id l203so6417701oia.3; Sat, 04 May 2019 05:05:38 -0700 (PDT) Received: from localhost.localdomain ([198.59.53.9]) by smtp.gmail.com with ESMTPSA id s26sm1844978otk.24.2019.05.04.05.05.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 04 May 2019 05:05:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GntLoJ5LAJvOCja8qBYrU8/E9MBsCe+uCkJo7D67Aro=; b=ssk1T90TsfNFVKW0T25Xl6zCr1WMuYvbbf6XvbocG+YAzRFZbdfsmVHu31JELrJ9iY kdO7swyg8NAeu+SsnO75jkdS1COJQVir6xab43uDT4qK1QfInuvh37t1Q78+3e+aTU3T aUPgWW24hJJoWN566UgOtF3xsGMjfxI5i69F0BlgJhV1k0HRzARYQZBUeEvDGY1kwqNX i6POMXb9+HF1he5dc4y4XxuYWibhUXMkyCeN/Oa12dbI/nKwBartiGAFetUI0tp+3Q4J 4KZ3IUrLqaef/vdx89l9PFPzDiziep0E18sm7KZuCHivegDxrlvCgRWuf3WtJg2Gqxcl SGCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=GntLoJ5LAJvOCja8qBYrU8/E9MBsCe+uCkJo7D67Aro=; b=M11jGX6Sq0LP4xC5FA35NjOGvy2HwIDz1T8GhoNkm1mgXtWqJbKg8Og84oGUi2Fq/t ptWhWs7JAAncZkW1UR/D0B/QibLfM27B9zf5gzQZnoi4nqTtDf7jcgdbK2eMSHLrMpvG 8rKIIVfXB0iQZ+sEkUYi33EmM+lARO3sDFhSssILy2Ni+JfQ6qugGJBBMMhnNAcLzpjX YQO3j91kwlxi2SQMR0z2/YEIxrTMeqQAWOhUvdGErZFGgzcaBuXoWDEVPv+ukqHt3SyD or64OvuBSF4dANM2lfxbfuYEEcmb0JLp4HqxjsxJLEMQlaZ7FMG4NiIBLeEme9myeKEG TFOg== X-Gm-Message-State: APjAAAXaUEIhv5OjuVLZtWQkjknMNL8u8B/MBQ046wu8li7tiR3EqmpX srdLVx9eZucWEe5cRrBsRY6A/AGs X-Google-Smtp-Source: APXvYqzYkYecVBVitWiGe5sxHagGM/N0Bye1cG4nzDbby4noZj7wT0Bp6cVdn1dAgp1wW2m6VDa6kw== X-Received: by 2002:aca:bb07:: with SMTP id l7mr2385734oif.142.1556971537662; Sat, 04 May 2019 05:05:37 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 4 May 2019 06:05:25 -0600 Message-Id: <20190504120528.6389-8-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190504120528.6389-1-pbonzini@redhat.com> References: <20190504120528.6389-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::241 Subject: [Qemu-devel] [PATCH 7/9] tcg: add tcg_out_start 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, cohuck@redhat.com, richard.henderson@linaro.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This function is called at the beginning of any translation block. We will use it to emit ENDBR32 or ENDBR64 annotations for x86 CET. Reviewed-by: Richard Henderson Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- tcg/aarch64/tcg-target.inc.c | 4 ++++ tcg/arm/tcg-target.inc.c | 4 ++++ tcg/i386/tcg-target.inc.c | 4 ++++ tcg/mips/tcg-target.inc.c | 4 ++++ tcg/ppc/tcg-target.inc.c | 4 ++++ tcg/riscv/tcg-target.inc.c | 4 ++++ tcg/s390/tcg-target.inc.c | 4 ++++ tcg/sparc/tcg-target.inc.c | 4 ++++ tcg/tcg.c | 2 ++ tcg/tci/tcg-target.inc.c | 4 ++++ 10 files changed, 38 insertions(+) diff --git a/tcg/aarch64/tcg-target.inc.c b/tcg/aarch64/tcg-target.inc.c index eefa929948..c66f3cb857 100644 --- a/tcg/aarch64/tcg-target.inc.c +++ b/tcg/aarch64/tcg-target.inc.c @@ -2539,6 +2539,10 @@ QEMU_BUILD_BUG_ON(FRAME_SIZE >=3D (1 << 14)); /* We're expecting to use a single ADDI insn. */ QEMU_BUILD_BUG_ON(FRAME_SIZE - PUSH_SIZE > 0xfff); =20 +static inline void tcg_out_start(TCGContext *s) +{ +} + static void tcg_target_qemu_prologue(TCGContext *s) { TCGReg r; diff --git a/tcg/arm/tcg-target.inc.c b/tcg/arm/tcg-target.inc.c index abf0c444b4..8f919c7641 100644 --- a/tcg/arm/tcg-target.inc.c +++ b/tcg/arm/tcg-target.inc.c @@ -2284,6 +2284,10 @@ static void tcg_out_nop_fill(tcg_insn_unit *p, int c= ount) } } =20 +static inline void tcg_out_start(TCGContext *s) +{ +} + /* Compute frame size via macros, to share between tcg_target_qemu_prologue and tcg_register_jit. */ =20 diff --git a/tcg/i386/tcg-target.inc.c b/tcg/i386/tcg-target.inc.c index d5ed9f1ffd..b210977800 100644 --- a/tcg/i386/tcg-target.inc.c +++ b/tcg/i386/tcg-target.inc.c @@ -3510,6 +3510,10 @@ static const int tcg_target_callee_save_regs[] =3D { + TCG_TARGET_STACK_ALIGN - 1) \ & ~(TCG_TARGET_STACK_ALIGN - 1)) =20 +static inline void tcg_out_start(TCGContext *s) +{ +} + /* Generate global QEMU prologue and epilogue code */ static void tcg_target_qemu_prologue(TCGContext *s) { diff --git a/tcg/mips/tcg-target.inc.c b/tcg/mips/tcg-target.inc.c index 412cacdcb9..2bb976a9a5 100644 --- a/tcg/mips/tcg-target.inc.c +++ b/tcg/mips/tcg-target.inc.c @@ -2471,6 +2471,10 @@ static tcg_insn_unit *align_code_ptr(TCGContext *s) return s->code_ptr; } =20 +static inline void tcg_out_start(TCGContext *s) +{ +} + /* Stack frame parameters. */ #define REG_SIZE (TCG_TARGET_REG_BITS / 8) #define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZ= E) diff --git a/tcg/ppc/tcg-target.inc.c b/tcg/ppc/tcg-target.inc.c index 36b4791707..f4efca8f7b 100644 --- a/tcg/ppc/tcg-target.inc.c +++ b/tcg/ppc/tcg-target.inc.c @@ -1904,6 +1904,10 @@ static void tcg_out_nop_fill(tcg_insn_unit *p, int c= ount) } } =20 +static inline void tcg_out_start(TCGContext *s) +{ +} + /* Parameters for function call generation, used in tcg.c. */ #define TCG_TARGET_STACK_ALIGN 16 #define TCG_TARGET_EXTEND_ARGS 1 diff --git a/tcg/riscv/tcg-target.inc.c b/tcg/riscv/tcg-target.inc.c index 2932505094..5780537b73 100644 --- a/tcg/riscv/tcg-target.inc.c +++ b/tcg/riscv/tcg-target.inc.c @@ -1798,6 +1798,10 @@ static const TCGTargetOpDef *tcg_target_op_def(TCGOp= code op) } } =20 +static inline void tcg_out_start(TCGContext *s) +{ +} + static const int tcg_target_callee_save_regs[] =3D { TCG_REG_S0, /* used for the global env (TCG_AREG0) */ TCG_REG_S1, diff --git a/tcg/s390/tcg-target.inc.c b/tcg/s390/tcg-target.inc.c index 3d6150b10e..924bd01afd 100644 --- a/tcg/s390/tcg-target.inc.c +++ b/tcg/s390/tcg-target.inc.c @@ -2499,6 +2499,10 @@ static void query_s390_facilities(void) } } =20 +static inline void tcg_out_start(TCGContext *s) +{ +} + static void tcg_target_init(TCGContext *s) { query_s390_facilities(); diff --git a/tcg/sparc/tcg-target.inc.c b/tcg/sparc/tcg-target.inc.c index 7a61839dc1..f795e78153 100644 --- a/tcg/sparc/tcg-target.inc.c +++ b/tcg/sparc/tcg-target.inc.c @@ -1004,6 +1004,10 @@ static void build_trampolines(TCGContext *s) } #endif =20 +static inline void tcg_out_start(TCGContext *s) +{ +} + /* Generate global QEMU prologue and epilogue code */ static void tcg_target_qemu_prologue(TCGContext *s) { diff --git a/tcg/tcg.c b/tcg/tcg.c index f7bef51de8..c8832c3ccf 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -101,6 +101,7 @@ static void tcg_register_jit_int(void *buf, size_t size, /* Forward declarations for functions declared and used in tcg-target.inc.= c. */ static const char *target_parse_constraint(TCGArgConstraint *ct, const char *ct_str, TCGType typ= e); +static void tcg_out_start(TCGContext *s); static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg= 1, intptr_t arg2); static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg ar= g); @@ -3925,6 +3926,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb) #endif =20 num_insns =3D -1; + tcg_out_start(s); QTAILQ_FOREACH(op, &s->ops, link) { TCGOpcode opc =3D op->opc; =20 diff --git a/tcg/tci/tcg-target.inc.c b/tcg/tci/tcg-target.inc.c index 0015a98485..cb90012999 100644 --- a/tcg/tci/tcg-target.inc.c +++ b/tcg/tci/tcg-target.inc.c @@ -889,6 +889,10 @@ static void tcg_target_init(TCGContext *s) CPU_TEMP_BUF_NLONGS * sizeof(long)); } =20 +static inline void tcg_out_start(void) +{ +} + /* Generate global QEMU prologue and epilogue code. */ static inline void tcg_target_qemu_prologue(TCGContext *s) { --=20 2.21.0 From nobody Tue May 7 04:32:27 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1556971683; cv=none; d=zoho.com; s=zohoarc; b=Q/vlU5RMAPMud99g/hlyZl48KxDq+afbsRtl8J+aFRbPtNXSjADF6u/z1GlvefBrzAi+g1Z3ry5hJvMpIClg/dABbqEVRgC0teAeISQmzgQ2F3nygUZnUgkn0UXvJLmHwPjAV/Y5XUQJGbYBD4wK36vmBX6xI/a+3TiXsgoqFpk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556971683; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=clxKL3l8fCsJ+v+w3/bwQs1BkGuF1T6/oLLs/QaMqLg=; b=ZLHQwCCHOG9vYF/ogStNIewxKZuTtIQxTeLQQQWOrdNxDqFdPvO6tLlRTZSNi22MwSFjK2lIR1Cb4JgBB1L6D8k3puPsUgQs7iLvb1n0HGkuCZrqsQr/rWMXZJXuUXTOz5qvl7C1PPwEy7EjO2aNbYgcg7Rn/Ecj7odaEOJsIhk= 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 15569716832347.174087375950421; Sat, 4 May 2019 05:08:03 -0700 (PDT) Received: from localhost ([127.0.0.1]:55672 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtSP-00008d-3d for importer@patchew.org; Sat, 04 May 2019 08:07:57 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50505) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtQL-0007IV-DI for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMtQJ-0001HQ-RK for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:49 -0400 Received: from mail-oi1-x232.google.com ([2607:f8b0:4864:20::232]:33238) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMtQI-0001Dy-22; Sat, 04 May 2019 08:05:47 -0400 Received: by mail-oi1-x232.google.com with SMTP id m204so739982oib.0; Sat, 04 May 2019 05:05:39 -0700 (PDT) Received: from localhost.localdomain ([198.59.53.9]) by smtp.gmail.com with ESMTPSA id s26sm1844978otk.24.2019.05.04.05.05.37 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 04 May 2019 05:05:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=clxKL3l8fCsJ+v+w3/bwQs1BkGuF1T6/oLLs/QaMqLg=; b=D1YDJoMk5/Ln1/8MOUmYG5JTgeCYii3zgtbHlGcXnVCMGyPsDxYKOpSJSTu6I/SYTt OnAkpIbIWSRyeVe5a2AYtbew0f5hcepyxsq9auUNQ1eeB8DNuv4U+IAuwhiB3/eFe7hh ApUVNBMr/Xv8NF3yK6/wHH1i5JqFhvI7P+MO7Aw8l6VzhCPMml4BbhV2yWMABsqswZ97 9OsJbIw2kRkwJpuIGHLlRx8pexJphpoYSsjnOFAwAzirE7RAMtvpJVMtPJqNCfQmONnr uD0GVjDma/ubqZguZ7MFLea4Zw0XYobwT/FNmcubjWUF4Drf4bxma4+aYvuOIDnr49IV JwFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=clxKL3l8fCsJ+v+w3/bwQs1BkGuF1T6/oLLs/QaMqLg=; b=PFNqiqtFi/w6ujdaqyUyqm/jlTutHDN/CmDeqqYMY33tp/oXWuVZGf16q20ikYUjx5 Cj404QGwBuBFuQ76/vz+M40SLDw6VH7c8g9HocBp1auFye6/1szsKgIbtSUwiz/boUzo 7Y0pVnZgd03egJ6/qL5QuvCxf83BCfbyVzWQunge8oXKEdmPhWSKTu7umCIhxwSkDAKV bXBnZ+1UfY68bRGLNgQqsXINqcnUvJnOOOiTGJd/vSUFB6+hy/pU1GCcoR/j3+J6xgRd 54ITl05vcGeIajFbot5eVxrpnr5yHVl9T21L/lIbV7rwNhwzsuAO6+AvO5mUR2XCdJjT p8OQ== X-Gm-Message-State: APjAAAUil6cVK3ZMC3eDBHJrJsB3RDf1L7toObCY8OjoaAWbfjDiXZ3l WZVwr1k2hOT96M466CXcCjm1JxRd X-Google-Smtp-Source: APXvYqwqtmIgySlFr8l0sR/oulIvMNV5WLxQrx+USSA3VK5trR21YhqnIK7CdfXDS03mLthedxZUog== X-Received: by 2002:aca:da07:: with SMTP id r7mr2428683oig.5.1556971538769; Sat, 04 May 2019 05:05:38 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 4 May 2019 06:05:26 -0600 Message-Id: <20190504120528.6389-9-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190504120528.6389-1-pbonzini@redhat.com> References: <20190504120528.6389-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::232 Subject: [Qemu-devel] [PATCH 8/9] tcg/i386: add support for IBT 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, cohuck@redhat.com, richard.henderson@linaro.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Add endbr annotations before indirect branch targets. This lets QEMU enable IBT even for TCG-enabled builds. Signed-off-by: Paolo Bonzini Reviewed-by: Richard Henderson Reviewed-by: Stefan Hajnoczi --- Makefile.target | 2 ++ configure | 9 +++++++++ include/qemu/cpuid.h | 5 +++++ tcg/i386/tcg-target.inc.c | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/Makefile.target b/Makefile.target index 667682779b..d168ee7555 100644 --- a/Makefile.target +++ b/Makefile.target @@ -112,8 +112,10 @@ obj-y +=3D accel/ obj-$(CONFIG_TCG) +=3D tcg/tcg.o tcg/tcg-op.o tcg/tcg-op-vec.o tcg/tcg-op-= gvec.o obj-$(CONFIG_TCG) +=3D tcg/tcg-common.o tcg/optimize.o ifeq ($(CONFIG_CF_PROTECTION),y) +ifneq ($(CONFIG_CF_PROTECTION_TCG),y) tcg/tcg.o-cflags :=3D -fcf-protection=3Dreturn endif +endif obj-$(CONFIG_TCG_INTERPRETER) +=3D tcg/tci.o obj-$(CONFIG_TCG_INTERPRETER) +=3D disas/tci.o obj-$(CONFIG_TCG) +=3D fpu/softfloat.o diff --git a/configure b/configure index 946ff7825a..c02a5f4b79 100755 --- a/configure +++ b/configure @@ -5200,6 +5200,11 @@ fi if test "$cf_protection" =3D "yes"; then QEMU_CFLAGS=3D"-fcf-protection $QEMU_CFLAGS" fi +if test "$cpu" =3D "x86_64"; then + cf_protection_tcg=3Dyes +else + cf_protection_tcg=3Dno +fi =20 ########################################## # check and set a backend for coroutine @@ -6395,6 +6400,7 @@ echo "TCG support $tcg" if test "$tcg" =3D "yes" ; then echo "TCG debug enabled $debug_tcg" echo "TCG interpreter $tcg_interpreter" + echo "TCG CF protection $cf_protection_tcg" fi echo "malloc trim support $malloc_trim" echo "RDMA support $rdma" @@ -6600,6 +6606,9 @@ fi if test "$cf_protection" =3D "yes" ; then echo "CONFIG_CF_PROTECTION=3Dy" >> $config_host_mak fi +if test "$cf_protection_tcg" =3D "yes" ; then + echo "CONFIG_CF_PROTECTION_TCG=3Dy" >> $config_host_mak +fi if test "$slirp" !=3D "no"; then echo "CONFIG_SLIRP=3Dy" >> $config_host_mak echo "CONFIG_SMBD_COMMAND=3D\"$smbd\"" >> $config_host_mak diff --git a/include/qemu/cpuid.h b/include/qemu/cpuid.h index 69301700bd..e32fb209f5 100644 --- a/include/qemu/cpuid.h +++ b/include/qemu/cpuid.h @@ -49,6 +49,11 @@ #define bit_BMI2 (1 << 8) #endif =20 +/* Leaf 7, %edx */ +#ifndef bit_IBT +#define bit_IBT (1 << 20) +#endif + /* Leaf 0x80000001, %ecx */ #ifndef bit_LZCNT #define bit_LZCNT (1 << 5) diff --git a/tcg/i386/tcg-target.inc.c b/tcg/i386/tcg-target.inc.c index b210977800..cb3de2f7ac 100644 --- a/tcg/i386/tcg-target.inc.c +++ b/tcg/i386/tcg-target.inc.c @@ -159,10 +159,12 @@ bool have_avx2; static bool have_movbe; static bool have_bmi2; static bool have_lzcnt; +static bool have_ibt; #else # define have_movbe 0 # define have_bmi2 0 # define have_lzcnt 0 +# define have_ibt 1 #endif =20 static tcg_insn_unit *tb_ret_addr; @@ -809,6 +811,19 @@ static inline void tgen_arithr(TCGContext *s, int subo= p, int dest, int src) tcg_out_modrm(s, OPC_ARITH_GvEv + (subop << 3) + ext, dest, src); } =20 +static void tcg_out_endbr(TCGContext *s) +{ + if (have_ibt) { +#if defined __CET__ && (__CET__ & 1) +#ifdef __x86_64__ + tcg_out32(s, 0xfa1e0ff3); +#else + tcg_out32(s, 0xfb1e0ff3); +#endif +#endif + } +} + static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg ar= g) { int rexw =3D 0; @@ -3512,6 +3527,7 @@ static const int tcg_target_callee_save_regs[] =3D { =20 static inline void tcg_out_start(TCGContext *s) { + tcg_out_endbr(s); } =20 /* Generate global QEMU prologue and epilogue code */ @@ -3520,6 +3536,7 @@ static void tcg_target_qemu_prologue(TCGContext *s) int i, stack_addend; =20 /* TB prologue */ + tcg_out_endbr(s); =20 /* Reserve some stack space, also for TCG temps. */ stack_addend =3D FRAME_SIZE - PUSH_SIZE; @@ -3566,6 +3583,7 @@ static void tcg_target_qemu_prologue(TCGContext *s) * and fall through to the rest of the epilogue. */ s->code_gen_epilogue =3D s->code_ptr; + tcg_out_endbr(s); tcg_out_movi(s, TCG_TYPE_REG, TCG_REG_EAX, 0); =20 /* TB epilogue */ @@ -3598,6 +3616,7 @@ static void tcg_target_init(TCGContext *s) __cpuid_count(7, 0, a, b7, c, d); have_bmi1 =3D (b7 & bit_BMI) !=3D 0; have_bmi2 =3D (b7 & bit_BMI2) !=3D 0; + have_ibt =3D (d & bit_IBT) !=3D 0; } =20 if (max >=3D 1) { --=20 2.21.0 From nobody Tue May 7 04:32:27 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1556971856; cv=none; d=zoho.com; s=zohoarc; b=QRRDH45Lzp9LrqBpR1G2uV5f9AHk95GREi192EEj+VKjJHkVFoiAv6fKEDUdgZHBBuQHE9n7DaUsO8EIGnPH0SjLYsHhpbnASUtO0sWbvtcgD0wud2ni8J45URjcYZR3tqNKaUxOmNKfdIhqLiAULkiT5UtgO4mnRdGnhztz4mo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556971856; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=/mCFCVnZxjs86hd/2IkgmcY0FzqDfdRP5Dh6XiOK0Bo=; b=Ynhpv/TZwJRMRCOHa85OqCaxyWCD8zoLOQtZ1xYGbylnx7ISvzVbGjx64z6e/3/2LylD3trzlMB0ke/W97E5eAmipdUk+bE6nMvyIEzgYihJ+WSgTjzr7tGgqYipTk3b0Nj2LE1CBq/PdJ/1KqNaTlZepSTNNL0eefAkz15NSp4= 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 1556971856934364.1167176636899; Sat, 4 May 2019 05:10:56 -0700 (PDT) Received: from localhost ([127.0.0.1]:55730 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtVF-0002iH-Nl for importer@patchew.org; Sat, 04 May 2019 08:10:53 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50544) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtQN-0007Ki-EH for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMtQL-0001IO-Gc for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:51 -0400 Received: from mail-oi1-x244.google.com ([2607:f8b0:4864:20::244]:44869) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMtQL-0001EA-AU; Sat, 04 May 2019 08:05:49 -0400 Received: by mail-oi1-x244.google.com with SMTP id t184so6328714oie.11; Sat, 04 May 2019 05:05:40 -0700 (PDT) Received: from localhost.localdomain ([198.59.53.9]) by smtp.gmail.com with ESMTPSA id s26sm1844978otk.24.2019.05.04.05.05.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 04 May 2019 05:05:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/mCFCVnZxjs86hd/2IkgmcY0FzqDfdRP5Dh6XiOK0Bo=; b=RsekO3nG+gnOS+8ZCJ6wGbx02ghB74NNj4NL+ZMwJY5XIxvc7h2RegOpSfeFUKTcE4 bxaP3bv8qBAIOuy+f3EHxp3Z4A1fIwxLU7eeSO2/NjxyYVMssCgE0doPNnn8ItqOEgqG CU0B9LkQ4FlbxLDn67KtU64uoWFVSEsGTScBcemgEsn4l3z76NHCgV/T4BUvFX3muuiN +ru9m5Pnf7j+6d7r/LQWfG1+cdULQMSMFODN54TF5Tt0zN5PSEELgR8FM0Z3s368AXCk Z/cqfHAOiAqhXYcuMiIOY7y4dDG0HHfKZjMsbnzMcuzHV5PkqaPolYyEuAiQeCrx9l+h qZcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=/mCFCVnZxjs86hd/2IkgmcY0FzqDfdRP5Dh6XiOK0Bo=; b=aofCtycGJla4PEQl8TNXbKbtUbRC374YXnqxZLJSSqBz9vJWYQlIvpR3jSjAQb+Nkf kc1+j3DVpYvEKJvAiNhNCSDBcYBlJs/IQ/aU6Mrat+fI2cc5RGaaScbMSkJmT0pr0pjX tUMoYti/ngQqOFTvpQJEElzWEiJfvnMLjM3gvGOw0e8CexCIfRiALLpeqjFiUv79VjS4 lQQHBg1OXLlJGO5OX/SZCRf9Ws3cMq0Dnq3z2ocVJCUYOj99ZtP1THiHawLM+Uwsfl9Y toGhsPiZQVfR6k1WzJxScIIpMYKOJrxujQ1uM10d8BiO6cteIDB8VDW/9WO26lDXQeo8 l+Jg== X-Gm-Message-State: APjAAAVDwhTx6P5MBePXfCGtsbPIY9TC5Xy2AU2IczX/oReRvBn93x3R yt24fGmYE9pftQ3IKqEvMagC0Tb6 X-Google-Smtp-Source: APXvYqwmVMK6pSlmdJHC8xzc241FVkbp6YNMBKYF9tUWG0hi3ERxdpWJzgvvB+5nl6UfzfQZOtdqig== X-Received: by 2002:aca:4f10:: with SMTP id d16mr2311035oib.17.1556971539772; Sat, 04 May 2019 05:05:39 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 4 May 2019 06:05:27 -0600 Message-Id: <20190504120528.6389-10-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190504120528.6389-1-pbonzini@redhat.com> References: <20190504120528.6389-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::244 Subject: [Qemu-devel] [PATCH 9/9] linux-user: add IBT support to x86 safe-syscall.S 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, cohuck@redhat.com, richard.henderson@linaro.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Because safe-syscall.S does not go through the C compiler, the .note.gnu.property note has to be added manually. Safe syscalls do not involve any indirect branch or stack unwinding, so they are trivially safe for IBT or shadow stacks. Reviewed-by: Richard Henderson Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- linux-user/host/i386/safe-syscall.inc.S | 19 +++++++++++++++++++ linux-user/host/x86_64/safe-syscall.inc.S | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/linux-user/host/i386/safe-syscall.inc.S b/linux-user/host/i386= /safe-syscall.inc.S index 9e58fc6504..6c6d568d62 100644 --- a/linux-user/host/i386/safe-syscall.inc.S +++ b/linux-user/host/i386/safe-syscall.inc.S @@ -98,3 +98,22 @@ safe_syscall_end: .cfi_endproc =20 .size safe_syscall_base, .-safe_syscall_base + + .pushsection ".note.gnu.property", "a" + .p2align 2 + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + .long 5 /* NT_GNU_PROPERTY_TYPE_0. */ +0: + .asciz "GNU" /* vendor name. */ + .p2align 2 +1: + /* GNU_PROPERTY_X86_FEATURE_1_AND. */ + .long 0xc0000002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x3 /* IBT, SHSTK */ +3: + .p2align 2 +4: + .popsection diff --git a/linux-user/host/x86_64/safe-syscall.inc.S b/linux-user/host/x8= 6_64/safe-syscall.inc.S index f36992daa3..e1a57db338 100644 --- a/linux-user/host/x86_64/safe-syscall.inc.S +++ b/linux-user/host/x86_64/safe-syscall.inc.S @@ -89,3 +89,22 @@ safe_syscall_end: .cfi_endproc =20 .size safe_syscall_base, .-safe_syscall_base + + .pushsection ".note.gnu.property", "a" + .p2align 3 + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + .long 5 /* NT_GNU_PROPERTY_TYPE_0. */ +0: + .asciz "GNU" /* vendor name. */ + .p2align 3 +1: + /* GNU_PROPERTY_X86_FEATURE_1_AND. */ + .long 0xc0000002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x3 /* IBT, SHSTK */ +3: + .p2align 3 +4: + .popsection --=20 2.21.0 From nobody Tue May 7 04:32:27 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1556971993; cv=none; d=zoho.com; s=zohoarc; b=MbbAQOdG409dzdtCddxYL52bWHgtwQLLILLanDsZV03AE1XRVMBXOhrnRAiqEtYJIYev8laNNzbIVtir6bCFme4Q2xaDUpdsUfJTlf8y1EJNyGu9t6qou+8E/QFh+i1cXfUOfzvIlhtSMc5Wiz8o2L3kZ1mrFAn3T1N30BLvrug= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1556971993; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=Dst2FGMsCs9IpcjAwFYM6Y8XoVeXqqnJoYTrIPSeVd0=; b=Ktfaio1iCcFLhkBQKaejZolRUA6JiEZge/fnomTas5+9xPAlbpH0C8WlTAE1QuUALzICbbFkzIJfuFAsXg7prpooT+ZHucbM97/IL5Q5r5wVr0ouz617YLnCBcFXEeYZrWTtz0mJEdNSJYetN2bt4pj3Z9sQJDVohM2n5vCRDms= 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 1556971993519857.2783568921313; Sat, 4 May 2019 05:13:13 -0700 (PDT) Received: from localhost ([127.0.0.1]:55752 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtXM-0004eh-CM for importer@patchew.org; Sat, 04 May 2019 08:13:04 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50545) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hMtQN-0007Kj-Ee for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hMtQL-0001Hv-BN for qemu-devel@nongnu.org; Sat, 04 May 2019 08:05:51 -0400 Received: from mail-oi1-x230.google.com ([2607:f8b0:4864:20::230]:44209) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hMtQJ-0001Em-Nx; Sat, 04 May 2019 08:05:47 -0400 Received: by mail-oi1-x230.google.com with SMTP id t184so6328736oie.11; Sat, 04 May 2019 05:05:41 -0700 (PDT) Received: from localhost.localdomain ([198.59.53.9]) by smtp.gmail.com with ESMTPSA id s26sm1844978otk.24.2019.05.04.05.05.39 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 04 May 2019 05:05:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Dst2FGMsCs9IpcjAwFYM6Y8XoVeXqqnJoYTrIPSeVd0=; b=JW3l2DuS8al+vh91Gj35Ygr5a5YHO8l5AVOFvhk/tSTNYpnnqfbYIfnMXv2eaPe0vK uCw+5cMP0owamtPcOdV59P6mIQJp3DTIRrni85DJWNODjYIF0OHKf2ZMC5BmPeyZG2/6 rzJeu7WD2n56VNCpniQ3r2WS2QHPZOv4iIKYdgbc4OwXeL2RMp03/ayjqg1Ysmbnqwg+ 0sYtYpXo9I0SVk6ko/lSYfYrJNTXRPb5Qa1Zst8oOQg+N3uFYFzP+KX9YjAqJRz8Xszs ZdDUK9vZ2pa6KJ2LmRW5j63qryNXSWDXdsem77s39aanwR7C6Xo3uDQXDtRJL4QaRGd8 x/Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=Dst2FGMsCs9IpcjAwFYM6Y8XoVeXqqnJoYTrIPSeVd0=; b=HBEnjUxr4rLgUhU1sc0voac/p3vCWnQthVWPEeOknrABSvXE2SB01LPKUQa3zb0+qn 49vaCNzLcyLNLJMx8fAA/WANJaVjL8EmTobt+bxnWFkFofPogQPt5m8yH0ujtABF2Bmv jkQc/czc1Ycu768eRURUfq52TbmvmRLRqWvocAe1XYzhJ2FGY/RyNeLYUgQlixAn8WdL lwIeBhPP0HV6KrGk/gAyfcMfjc5OTAE3xzAkxLREq1HcNeKrh9bcWs/qwUN6QI39qfev UOah4nGc/Y2OBh8DKA8Y4xkSww+D2YpuVIuzzbXYXrXNiWGZKDng/5hXB3QxaBPP+l9I JTTg== X-Gm-Message-State: APjAAAUjuSI7cbvA1CvbAPv9YN0i3cTCC2FlcT+FjfHjUDjNd47PyKhm lPCP0NELmJs7syhCn1nOV2xEkiI4 X-Google-Smtp-Source: APXvYqwI+5y0uYDXcmd5U57FMj+sd0pER5XnICRZV76BvAjg4kj9QCRY6nJKYKJkFZv7uWxhN4tioA== X-Received: by 2002:aca:d7d5:: with SMTP id o204mr2355825oig.23.1556971540786; Sat, 04 May 2019 05:05:40 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sat, 4 May 2019 06:05:28 -0600 Message-Id: <20190504120528.6389-11-pbonzini@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190504120528.6389-1-pbonzini@redhat.com> References: <20190504120528.6389-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::230 Subject: [Qemu-devel] [PATCH 10/9] coroutine-asm: add x86 CET shadow stack support 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, cohuck@redhat.com, richard.henderson@linaro.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Note that the ABI is not yet part of Linux; this patch is not intended to be committed until that is approved. Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- configure | 14 ++++++++ util/Makefile.objs | 2 ++ util/coroutine-asm.c | 82 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/configure b/configure index c02a5f4b79..8e81d08ef1 100755 --- a/configure +++ b/configure @@ -5192,6 +5192,20 @@ if test "$cf_protection" !=3D no; then feature_not_found "cf_protection" 'Control-flow protection is not su= pported by your toolchain' fi cf_protection=3Dno + else + if test $cpu =3D x86_64; then + # only needed by coroutine-asm.c, however it should be rare to have + # CET support in the compiler but not in binutils + cat > $TMPC << EOF +int main(void) { asm("rdsspq %%rax" : : : "rax"); } +EOF + if ! compile_prog "" "" ; then + if test "$cf_protection" =3D yes; then + feature_not_found "cf_protection" 'CET is not supported by your = toolchain' + fi + cf_protection=3Dno + fi + fi fi fi if test "$cf_protection" =3D ""; then diff --git a/util/Makefile.objs b/util/Makefile.objs index d7add70b63..cf08b4d1c4 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -45,8 +45,10 @@ endif ifeq ($(CONFIG_CF_PROTECTION),y) coroutine-sigaltstack.o-cflags :=3D -fcf-protection=3Dbranch coroutine-ucontext.o-cflags :=3D -fcf-protection=3Dbranch +ifneq ($(ARCH),x86_64) coroutine-asm.o-cflags +=3D -fcf-protection=3Dbranch endif +endif util-obj-y +=3D buffer.o util-obj-y +=3D timed-average.o util-obj-y +=3D base64.o diff --git a/util/coroutine-asm.c b/util/coroutine-asm.c index a9a80e9c71..01875acfc4 100644 --- a/util/coroutine-asm.c +++ b/util/coroutine-asm.c @@ -22,6 +22,13 @@ #include "qemu/osdep.h" #include "qemu-common.h" #include "qemu/coroutine_int.h" +#include "qemu/error-report.h" + +#ifdef CONFIG_CF_PROTECTION +#include +#include +int arch_prctl(int code, unsigned long addr); +#endif =20 #ifdef CONFIG_VALGRIND_H #include @@ -42,12 +49,16 @@ typedef struct { =20 /* * aarch64, s390x: instruction pointer + * x86: shadow stack pointer */ void *scratch; =20 void *stack; size_t stack_size; =20 + /* x86: CET shadow stack */ + void *sstack; + size_t sstack_size; #ifdef CONFIG_VALGRIND_H unsigned int valgrind_stack_id; #endif @@ -82,6 +93,35 @@ static void start_switch_fiber(void **fake_stack_save, #endif } =20 +static bool have_sstack(void) +{ +#if defined CONFIG_CF_PROTECTION && defined __x86_64__ + uint64_t ssp; + asm ("xor %0, %0; rdsspq %0\n" : "=3Dr" (ssp)); + return !!ssp; +#else + return 0; +#endif +} + +static void *alloc_sstack(size_t sz) +{ +#if defined CONFIG_CF_PROTECTION && defined __x86_64__ +#ifndef ARCH_X86_CET_ALLOC_SHSTK +#define ARCH_X86_CET_ALLOC_SHSTK 0x3004 +#endif + + uint64_t arg =3D sz; + if (arch_prctl(ARCH_X86_CET_ALLOC_SHSTK, (unsigned long) &arg) < 0) { + abort(); + } + + return (void *)arg; +#else + abort(); +#endif +} + #ifdef __x86_64__ /* * We hardcode all operands to specific registers so that we can write dow= n all the @@ -93,6 +133,26 @@ static void start_switch_fiber(void **fake_stack_save, * Note that push and call would clobber the red zone. Makefile.objs comp= iles this * file with -mno-red-zone. The alternative is to subtract/add 128 bytes = from rsp * around the switch, with slightly lower cache performance. + * + * The RSTORSSP and SAVEPREVSSP instructions are intricate. In a nutshell= they are: + * + * RSTORSSP(mem): oldSSP =3D SSP + * SSP =3D mem + * *SSP =3D oldSSP + * + * SAVEPREVSSP: oldSSP =3D shadow_stack_pop() + * *(oldSSP - 8) =3D oldSSP # "push" to old s= hadow stack + * + * Therefore, RSTORSSP(mem) followed by SAVEPREVSSP is the same as + * + * shadow_stack_push(SSP) + * SSP =3D mem + * shadow_stack_pop() + * + * From the simplified description you can see that co->ssp, being stored = before + * the RSTORSSP+SAVEPREVSSP sequence, points to the top actual entry of th= e shadow + * stack, not to the restore token. Hence we use an offset of -8 in the o= perand + * of rstorssp. */ #define CO_SWITCH(from, to, action, jump) ({ = \ int action_ =3D action; = \ @@ -105,7 +165,15 @@ static void start_switch_fiber(void **fake_stack_save, "jmp 2f\n" /* switch back continues at la= bel 2 */ \ = \ "1: .cfi_adjust_cfa_offset 8\n" = \ - "movq %%rsp, %c[SP](%[FROM])\n" /* save source SP */ = \ + "xor %%rbp, %%rbp\n" /* use old frame pointer as sc= ratch reg */ \ + "rdsspq %%rbp\n" = \ + "test %%rbp, %%rbp\n" /* if CET is enabled... */ = \ + "jz 9f\n" = \ + "movq %%rbp, %c[SCRATCH](%[FROM])\n" /* ... save source shadow SP,= */ \ + "movq %c[SCRATCH](%[TO]), %%rbp\n" /* restore destination shadow= stack, */ \ + "rstorssp -8(%%rbp)\n" = \ + "saveprevssp\n" /* and save source shadow SP t= oken */ \ + "9: movq %%rsp, %c[SP](%[FROM])\n" /* save source SP */ = \ "movq %c[SP](%[TO]), %%rsp\n" /* load destination SP */ = \ jump "\n" /* coroutine switch */ = \ = \ @@ -113,7 +181,8 @@ static void start_switch_fiber(void **fake_stack_save, "popq %%rbp\n" = \ ".cfi_adjust_cfa_offset -8\n" = \ : "+a" (action_), [FROM] "+b" (from_), [TO] "+D" (to_) = \ - : [SP] "i" (offsetof(CoroutineAsm, sp)) = \ + : [SP] "i" (offsetof(CoroutineAsm, sp)), = \ + [SCRATCH] "i" (offsetof(CoroutineAsm, scratch)) = \ : "rcx", "rdx", "rsi", "r8", "r9", "r10", "r11", "r12", "r13", "r1= 4", "r15", \ "memory"); = \ action_; = \ @@ -220,6 +289,12 @@ Coroutine *qemu_coroutine_new(void) co->stack =3D qemu_alloc_stack(&co->stack_size); co->sp =3D co->stack + co->stack_size; =20 + if (have_sstack()) { + co->sstack_size =3D COROUTINE_SHADOW_STACK_SIZE; + co->sstack =3D alloc_sstack(co->sstack_size); + co->scratch =3D co->sstack + co->sstack_size; + } + #ifdef CONFIG_VALGRIND_H co->valgrind_stack_id =3D VALGRIND_STACK_REGISTER(co->stack, co->stack + co->stack_size); @@ -265,6 +340,9 @@ void qemu_coroutine_delete(Coroutine *co_) #endif =20 qemu_free_stack(co->stack, co->stack_size); + if (co->sstack) { + munmap(co->sstack, co->sstack_size); + } g_free(co); } =20 --=20 2.21.0