From nobody Sat Apr 20 16:27:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.ibm.com ARC-Seal: i=1; a=rsa-sha256; t=1659720942; cv=none; d=zohomail.com; s=zohoarc; b=Xeq8XbF4JRQMUPwHXR5upqrLAGUn7jQI+vfW07CPb1g4Uc9H8TMfFviO27mAPirv6ngkunLqdmWD1e2hMeDpuZiUxwFt/RrfeCcdz/8ZeK0hkgUf8ZpONUNdIAiT9gt58YS5icS7OsWfJ4mm+6aVMCkmXdrIYGBwRqwlq+FJbbI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1659720942; 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; bh=73qnw5nIm3iv+coHjF7quQ8YT4tdxDWXNrMdnbdBb30=; b=ftkyrGx+XhsMwY7YmTGvsTirlVMhBa6MC9Y8dSsiow4jRJAcLLV/3owKR5e5g7hg6buVl8C66UQ7iMdj5j8ZjRq0A6Ub7YUwXoCU026NBpaNPyC4a5og9S8fgtEE2aDDXcDYLUp4N8fCbc0XZVaM6vi4BoxNgy+05RXpoYJGSlU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1659720941954569.4713953312613; Fri, 5 Aug 2022 10:35:41 -0700 (PDT) Received: from localhost ([::1]:36062 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oK1EZ-0001dX-VS for importer@patchew.org; Fri, 05 Aug 2022 13:35:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55280) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oK1Cz-0007py-PR; Fri, 05 Aug 2022 13:34:02 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:42050) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oK1Cx-0004jd-JP; Fri, 05 Aug 2022 13:34:01 -0400 Received: from pps.filterd (m0187473.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 275HFH5h025974; Fri, 5 Aug 2022 17:33:48 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3hs7eeremw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 17:33:47 +0000 Received: from m0187473.ppops.net (m0187473.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 275HJ4nX037316; Fri, 5 Aug 2022 17:33:47 GMT Received: from ppma06fra.de.ibm.com (48.49.7a9f.ip4.static.sl-reverse.com [159.122.73.72]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3hs7eerekm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 17:33:47 +0000 Received: from pps.filterd (ppma06fra.de.ibm.com [127.0.0.1]) by ppma06fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 275HK1vq026302; Fri, 5 Aug 2022 17:33:44 GMT Received: from b06cxnps4075.portsmouth.uk.ibm.com ([9.149.109.197]) by ppma06fra.de.ibm.com with ESMTP id 3hmuwhtpn8-124 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 17:33:44 +0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 275G9HUY8913360 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 5 Aug 2022 16:09:17 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 85450A405B; Fri, 5 Aug 2022 16:09:17 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E4C66A4054; Fri, 5 Aug 2022 16:09:16 +0000 (GMT) Received: from heavy.lan (unknown [9.171.89.138]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 5 Aug 2022 16:09:16 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=73qnw5nIm3iv+coHjF7quQ8YT4tdxDWXNrMdnbdBb30=; b=NwTxsN1SjriiQzfQGErMgShqNcR9yMgY5aZXASEHRC/8GVlWYjvOHyJTZ0onpr64VNLR DCE9xt+KVN67Hyxg2cucBKNSZ2W9ASsv/zNk/bkuw9NzzVzK/yuCi8gTChAMQaEcFKOo t8CrJeAkeQEWuDxS1XXv3Bw7J/p1ATZqQecCMpNgdLMmCm5lVa14xNCZRVkvNCIwknfQ sHuPXNJuYtSWFGiRPQAgZuRJzcgt44m8p/kooC5A3/APmwXG1ZQpfxfQc+ZuEPT0jdSC oQtrKBvHZInS+LHgA6+0vd3sizKz5QHLp8AwuIbXJJXh2Es9Z44O2q71Uqysu2I8QbSM 6g== From: Ilya Leoshkevich To: Laurent Vivier , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Yanan Wang , Richard Henderson , Paolo Bonzini , David Hildenbrand , Peter Maydell Cc: qemu-devel@nongnu.org, qemu-s390x@nongnu.org, Christian Borntraeger , Ilya Leoshkevich Subject: [PATCH v2 1/4] accel/tcg: Invalidate translations when clearing PAGE_READ Date: Fri, 5 Aug 2022 18:09:11 +0200 Message-Id: <20220805160914.1106091-2-iii@linux.ibm.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220805160914.1106091-1-iii@linux.ibm.com> References: <20220805160914.1106091-1-iii@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: _HBbJuXyE-9JJhkTdKj1C6t3rHZnhT2l X-Proofpoint-GUID: b_ppoRJ3aw37N2LJYf4OXJ85fu5-dRrr X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-05_09,2022-08-05_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 impostorscore=0 adultscore=0 priorityscore=1501 suspectscore=0 clxscore=1015 bulkscore=0 malwarescore=0 mlxscore=0 spamscore=0 mlxlogscore=941 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2208050082 Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=148.163.156.1; envelope-from=iii@linux.ibm.com; helo=mx0a-001b2d01.pphosted.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @ibm.com) X-ZM-MESSAGEID: 1659720942523100001 Content-Type: text/plain; charset="utf-8" After mprotect(addr, PROT_NONE), addr can still be executed if there are cached translations. Drop them. Signed-off-by: Ilya Leoshkevich --- accel/tcg/translate-all.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index ef62a199c7..9318ada6b9 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -2295,12 +2295,19 @@ void page_set_flags(target_ulong start, target_ulon= g end, int flags) len !=3D 0; len -=3D TARGET_PAGE_SIZE, addr +=3D TARGET_PAGE_SIZE) { PageDesc *p =3D page_find_alloc(addr >> TARGET_PAGE_BITS, 1); + bool write_set, read_cleared; =20 - /* If the write protection bit is set, then we invalidate - the code inside. */ - if (!(p->flags & PAGE_WRITE) && - (flags & PAGE_WRITE) && - p->first_tb) { + /* + * If the write protection bit is set, then we invalidate the code + * inside. + */ + write_set =3D !(p->flags & PAGE_WRITE) && (flags & PAGE_WRITE); + /* + * If PAGE_READ is cleared, we also need to invalidate the code in + * order to force a fault when trying to run it. + */ + read_cleared =3D (p->flags & PAGE_READ) && !(flags & PAGE_READ); + if ((write_set || read_cleared) && p->first_tb) { tb_invalidate_phys_page(addr, 0); } if (reset_target_data) { --=20 2.35.3 From nobody Sat Apr 20 16:27:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.ibm.com ARC-Seal: i=1; a=rsa-sha256; t=1659724803; cv=none; d=zohomail.com; s=zohoarc; b=UMY0UJOWTMshVgIjmnCabfsZiIG+ZYJ91l8wQDHgUKl64Kq2jsfVzwVRdE02FWe4X+ymv14FH4aPn9gFX766enWa9wOPYXE/hDNmATaMhFXuRLH4BRUlaDKDeZ7R02Posz/VToq30duH5s20qtTjxHYivAOJaj/Q8VDI6EeaCiM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1659724803; 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; bh=esMkIBD33LWquukdIa5yKTRCkR7YnLBqqMwgGkA7wuU=; b=hZih3nH4f8rGg361r2EUx2O5ch7LIFdhScd/6Pt3wvxX5dvLgim/lPOzO2RfJUrfDFyWBZyZ4lxm6PoY5SV9aigUna67v6TiHoQ73+EpGhs7y9Rvx0iAC0K+6CyOxRv37cHtQ5XXTEgxVsfrtTpow7joiREbvqYh5sUa4Fr/qWI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1659724803702836.1633243545908; Fri, 5 Aug 2022 11:40:03 -0700 (PDT) Received: from localhost ([::1]:47376 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oK2Er-0007gg-PP for importer@patchew.org; Fri, 05 Aug 2022 14:40:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49614) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oK27C-0002BG-1W; Fri, 05 Aug 2022 14:32:06 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:50620) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oK279-0000Ud-Da; Fri, 05 Aug 2022 14:32:05 -0400 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 275HnMqA007265; Fri, 5 Aug 2022 18:31:47 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3hs7xp94gw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 18:31:46 +0000 Received: from m0098404.ppops.net (m0098404.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 275IOWxE012726; Fri, 5 Aug 2022 18:31:46 GMT Received: from ppma06ams.nl.ibm.com (66.31.33a9.ip4.static.sl-reverse.com [169.51.49.102]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3hs7xp94fr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 18:31:45 +0000 Received: from pps.filterd (ppma06ams.nl.ibm.com [127.0.0.1]) by ppma06ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 275I7G9i009969; Fri, 5 Aug 2022 18:31:43 GMT Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by ppma06ams.nl.ibm.com with ESMTP id 3hmuwhyr9b-69 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 18:31:43 +0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 275G9IxX31064458 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 5 Aug 2022 16:09:18 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 47529A405F; Fri, 5 Aug 2022 16:09:18 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9E05CA405C; Fri, 5 Aug 2022 16:09:17 +0000 (GMT) Received: from heavy.lan (unknown [9.171.89.138]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 5 Aug 2022 16:09:17 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=esMkIBD33LWquukdIa5yKTRCkR7YnLBqqMwgGkA7wuU=; b=iOW2vMYZxO5bBCe/I+euTf1OPxFdiW/oDhKIv4b1aODlU0eqzKmgDKUxgnHAIOXiR4Rt 79mO79m4JvuPaeJxjVVySy4BEEuqPoONkSh7tdmVqTo/Vt6sZ+5N5v6sJ9W/+10xWdaA /jti9UzqUuaPoGBiWhe5Krnc1RBKtf/LQlCLFkmWQGYzs5ctTrT+Wp9T2KBQ31WclttO F3S5H5rQpd44bQQLlVAJ3NF0rmphnOzrOHtdDzfMy2CZE5ffta+GyE14uE/7msCpm5Eh h9RUlGteKuV7LIo8dgMZYcE7R4tOQaruVPOULh8v4ts0gjRUC9hUGQpUhXflB22qm/QV SQ== From: Ilya Leoshkevich To: Laurent Vivier , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Yanan Wang , Richard Henderson , Paolo Bonzini , David Hildenbrand , Peter Maydell Cc: qemu-devel@nongnu.org, qemu-s390x@nongnu.org, Christian Borntraeger , Ilya Leoshkevich Subject: [PATCH v2 2/4] target/s390x: Make translator stop before the end of a page Date: Fri, 5 Aug 2022 18:09:12 +0200 Message-Id: <20220805160914.1106091-3-iii@linux.ibm.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220805160914.1106091-1-iii@linux.ibm.com> References: <20220805160914.1106091-1-iii@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: y0mzTaRgMuLBnqDTrpsnGsh5PP1ovFQm X-Proofpoint-GUID: CTDEcYTwaac2JXC6XXxPDkVixbXU_P_V X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-05_09,2022-08-05_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 bulkscore=0 adultscore=0 spamscore=0 malwarescore=0 mlxscore=0 suspectscore=0 clxscore=1015 impostorscore=0 phishscore=0 priorityscore=1501 mlxlogscore=894 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2208050086 Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=148.163.156.1; envelope-from=iii@linux.ibm.com; helo=mx0a-001b2d01.pphosted.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @ibm.com) X-ZM-MESSAGEID: 1659724805405100001 Content-Type: text/plain; charset="utf-8" Right now translator stops right *after* the end of a page, which breaks reporting of fault locations when the last instruction of a multi-insn translation block crosses a page boundary. Signed-off-by: Ilya Leoshkevich --- include/exec/translator.h | 10 ++++++++++ target/s390x/tcg/translate.c | 35 ++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/include/exec/translator.h b/include/exec/translator.h index 7db6845535..d27f8c33b6 100644 --- a/include/exec/translator.h +++ b/include/exec/translator.h @@ -187,4 +187,14 @@ FOR_EACH_TRANSLATOR_LD(GEN_TRANSLATOR_LD) =20 #undef GEN_TRANSLATOR_LD =20 +/* + * Return whether addr is on the same page as where disassembly started. + * Translators can use this to enforce the rule that only single-insn + * translation blocks are allowed to cross page boundaries. + */ +static inline bool is_same_page(DisasContextBase *db, target_ulong addr) +{ + return ((addr ^ db->pc_first) & TARGET_PAGE_MASK) =3D=3D 0; +} + #endif /* EXEC__TRANSLATOR_H */ diff --git a/target/s390x/tcg/translate.c b/target/s390x/tcg/translate.c index e2ee005671..0cd0c932fb 100644 --- a/target/s390x/tcg/translate.c +++ b/target/s390x/tcg/translate.c @@ -6305,14 +6305,13 @@ static void extract_field(DisasFields *o, const Dis= asField *f, uint64_t insn) o->c[f->indexC] =3D r; } =20 -/* Lookup the insn at the current PC, extracting the operands into O and - returning the info struct for the insn. Returns NULL for invalid insn.= */ +/* Lookup the insn at the current PC, filling the info struct. */ =20 -static const DisasInsn *extract_insn(CPUS390XState *env, DisasContext *s) +static DisasJumpType extract_insn(CPUS390XState *env, DisasContext *s, + const DisasInsn **info) { uint64_t insn, pc =3D s->base.pc_next; int op, op2, ilen; - const DisasInsn *info; =20 if (unlikely(s->ex_value)) { /* Drop the EX data now, so that it's clear on exception paths. */ @@ -6325,9 +6324,13 @@ static const DisasInsn *extract_insn(CPUS390XState *= env, DisasContext *s) ilen =3D s->ex_value & 0xf; op =3D insn >> 56; } else { + assert(s->base.num_insns =3D=3D 1 || is_same_page(&s->base, pc)); insn =3D ld_code2(env, s, pc); op =3D (insn >> 8) & 0xff; ilen =3D get_ilen(op); + if (s->base.num_insns > 1 && !is_same_page(&s->base, pc + ilen - 1= )) { + return DISAS_TOO_MANY; + } switch (ilen) { case 2: insn =3D insn << 48; @@ -6394,19 +6397,19 @@ static const DisasInsn *extract_insn(CPUS390XState = *env, DisasContext *s) s->fields.op2 =3D op2; =20 /* Lookup the instruction. */ - info =3D lookup_opc(op << 8 | op2); - s->insn =3D info; + *info =3D lookup_opc(op << 8 | op2); + s->insn =3D *info; =20 /* If we found it, extract the operands. */ - if (info !=3D NULL) { - DisasFormat fmt =3D info->fmt; + if (*info !=3D NULL) { + DisasFormat fmt =3D (*info)->fmt; int i; =20 for (i =3D 0; i < NUM_C_FIELD; ++i) { extract_field(&s->fields, &format_info[fmt].op[i], insn); } } - return info; + return DISAS_NEXT; } =20 static bool is_afp_reg(int reg) @@ -6423,12 +6426,17 @@ static bool is_fp_pair(int reg) static DisasJumpType translate_one(CPUS390XState *env, DisasContext *s) { const DisasInsn *insn; - DisasJumpType ret =3D DISAS_NEXT; + DisasJumpType ret; DisasOps o =3D {}; bool icount =3D false; =20 /* Search for the insn in the table. */ - insn =3D extract_insn(env, s); + ret =3D extract_insn(env, s, &insn); + + /* This is a subsequent insn that crosses a page boundary. */ + if (ret =3D=3D DISAS_TOO_MANY) { + goto out; + } =20 /* Update insn_start now that we know the ILEN. */ tcg_set_insn_start_param(s->insn_start, 2, s->ilen); @@ -6616,10 +6624,7 @@ static void s390x_tr_translate_insn(DisasContextBase= *dcbase, CPUState *cs) =20 dc->base.is_jmp =3D translate_one(env, dc); if (dc->base.is_jmp =3D=3D DISAS_NEXT) { - uint64_t page_start; - - page_start =3D dc->base.pc_first & TARGET_PAGE_MASK; - if (dc->base.pc_next - page_start >=3D TARGET_PAGE_SIZE || dc->ex_= value) { + if (!is_same_page(dcbase, dc->base.pc_next) || dc->ex_value) { dc->base.is_jmp =3D DISAS_TOO_MANY; } } --=20 2.35.3 From nobody Sat Apr 20 16:27:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.ibm.com ARC-Seal: i=1; a=rsa-sha256; t=1659724658; cv=none; d=zohomail.com; s=zohoarc; b=Z0irPmR+HDOIDl123w3in7ImyR/8PgLRR+zHV7vQXkVeQ6ynA7631RjDkpasWv8JYO180imHc/C+gVQhHGHi9emuJo+u1bDeLPxGlpmE1wh4BlZTsCG4GkfMreBOXeWyZLmsZl0jkHlzItCooHgIMOYY9Sx/FBl1JCip4k2HPkg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1659724658; 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; bh=6IimpppYGHxcM35bA+ZMSvfGTOn9+lV7szanabv+6oQ=; b=eoldCfZkdW60W4nrSf3pC+q21CwnDXh7qXaOM2IjvO+izhUaforWk+rHQTrppy2+W7OKwiW+h0f13jrkVdTaE7fT+j4cTRK3oCcfC7yKxGIsnUKDw8j6JoY/jRdaDPHuQm3G3qQNkAKSmrrViDHWCqzcXuX3Hwv8pl9OitYT9DI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1659724658456458.7693738511273; Fri, 5 Aug 2022 11:37:38 -0700 (PDT) Received: from localhost ([::1]:42458 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oK2CW-0004G7-TP for importer@patchew.org; Fri, 05 Aug 2022 14:37:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49274) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oK26G-0001rL-P7; Fri, 05 Aug 2022 14:31:09 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:46190 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oK269-0000Ir-Pb; Fri, 05 Aug 2022 14:31:08 -0400 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 275HoIs9006510; Fri, 5 Aug 2022 18:30:43 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3hs7y616nc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 18:30:42 +0000 Received: from m0098420.ppops.net (m0098420.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 275I6W41015051; Fri, 5 Aug 2022 18:30:42 GMT Received: from ppma03fra.de.ibm.com (6b.4a.5195.ip4.static.sl-reverse.com [149.81.74.107]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3hs7y616m6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 18:30:42 +0000 Received: from pps.filterd (ppma03fra.de.ibm.com [127.0.0.1]) by ppma03fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 275I7XR7006412; Fri, 5 Aug 2022 18:30:40 GMT Received: from b06avi18878370.portsmouth.uk.ibm.com (b06avi18878370.portsmouth.uk.ibm.com [9.149.26.194]) by ppma03fra.de.ibm.com with ESMTP id 3hmv98wrrr-49 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 18:30:40 +0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06avi18878370.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 275G9ZxK15335752 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 5 Aug 2022 16:09:35 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id ED2E4A405C; Fri, 5 Aug 2022 16:09:18 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5D325A4054; Fri, 5 Aug 2022 16:09:18 +0000 (GMT) Received: from heavy.lan (unknown [9.171.89.138]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 5 Aug 2022 16:09:18 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=6IimpppYGHxcM35bA+ZMSvfGTOn9+lV7szanabv+6oQ=; b=T3lIfRQlr1QuITPuAw9gE2yxowRgoCuWK0fqPaxPjbUQhvPc8t5LjAMRM8AVChMY4SD3 l5JVLd2q/NNRTod3Ljf1VS3OkxzGRhDi2kpbpLvyN3jlqi1EUx20IxSUwMeY/7SVe5l8 cP7hOowkq1z6Y9QjymKshaeWDovl3+Yb27XOH2sXFVB178/VmC7OAWHGQoKIvBqG9Jxh rF4gZR/S83gPr1sdMApq8bAhaMlEu0c4ApZylXVE1VhGLISRFieFfgUZ3f0WiD1taLq+ gYkBDX7dzijX7bqeW0sZCFdDA97tnx39b51yAYImmqWbUu92Cf6QOFtaQU1daMptF/pn Bw== From: Ilya Leoshkevich To: Laurent Vivier , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Yanan Wang , Richard Henderson , Paolo Bonzini , David Hildenbrand , Peter Maydell Cc: qemu-devel@nongnu.org, qemu-s390x@nongnu.org, Christian Borntraeger , Ilya Leoshkevich Subject: [PATCH v2 3/4] target/i386: Make translator stop before the end of a page Date: Fri, 5 Aug 2022 18:09:13 +0200 Message-Id: <20220805160914.1106091-4-iii@linux.ibm.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220805160914.1106091-1-iii@linux.ibm.com> References: <20220805160914.1106091-1-iii@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: CTtqWn5o_qa_idoq4PouG0LgFmV0nrvb X-Proofpoint-GUID: gen_A-ngqawF-IyBHD7kVgf1S2cQXIHx X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-05_09,2022-08-05_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 mlxscore=0 lowpriorityscore=0 clxscore=1015 adultscore=0 priorityscore=1501 spamscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 impostorscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2208050085 Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=148.163.158.5; envelope-from=iii@linux.ibm.com; helo=mx0a-001b2d01.pphosted.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_HELO_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @ibm.com) X-ZM-MESSAGEID: 1659724661029100001 Content-Type: text/plain; charset="utf-8" Right now translator stops right *after* the end of a page, which breaks reporting of fault locations when the last instruction of a multi-insn translation block crosses a page boundary. We may find out that we crossed page boundary after some ops were emitted and cc_op was updated. In theory it might be possible to rearrange the code to disassemble first, but this is too error-prone. Simply snapshot and restore the disassembly state instead. Signed-off-by: Ilya Leoshkevich --- target/i386/tcg/translate.c | 42 ++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c index b7972f0ff5..ea749b0a04 100644 --- a/target/i386/tcg/translate.c +++ b/target/i386/tcg/translate.c @@ -2008,6 +2008,12 @@ static uint64_t advance_pc(CPUX86State *env, DisasCo= ntext *s, int num_bytes) { uint64_t pc =3D s->pc; =20 + /* This is a subsequent insn that crosses a page boundary. */ + if (s->base.num_insns > 1 && + !is_same_page(&s->base, s->pc + num_bytes - 1)) { + siglongjmp(s->jmpbuf, 2); + } + s->pc +=3D num_bytes; if (unlikely(s->pc - s->pc_start > X86_MAX_INSN_LENGTH)) { /* If the instruction's 16th byte is on a different page than the = 1st, a @@ -4545,6 +4551,29 @@ static void gen_sse(CPUX86State *env, DisasContext *= s, int b, } } =20 +/* Disassembly state that may affect the next instruction. */ +typedef struct { + TCGOp *last_op; + bool cc_op_dirty; + CCOp cc_op; +} DisasSnapshot; + +/* Save disassembly state. */ +static void disas_save(DisasSnapshot *snapshot, const DisasContext *s) +{ + snapshot->last_op =3D tcg_last_op(); + snapshot->cc_op_dirty =3D s->cc_op_dirty; + snapshot->cc_op =3D s->cc_op; +} + +/* Restore disassembly state. */ +static void disas_restore(const DisasSnapshot *snapshot, DisasContext *s) +{ + tcg_remove_ops_after(snapshot->last_op); + s->cc_op_dirty =3D snapshot->cc_op_dirty; + s->cc_op =3D snapshot->cc_op; +} + /* convert one instruction. s->base.is_jmp is set if the translation must be stopped. Return the next pc value */ static target_ulong disas_insn(DisasContext *s, CPUState *cpu) @@ -4556,6 +4585,7 @@ static target_ulong disas_insn(DisasContext *s, CPUSt= ate *cpu) int modrm, reg, rm, mod, op, opreg, val; target_ulong next_eip, tval; target_ulong pc_start =3D s->base.pc_next; + DisasSnapshot snapshot; =20 s->pc_start =3D s->pc =3D pc_start; s->override =3D -1; @@ -4568,9 +4598,19 @@ static target_ulong disas_insn(DisasContext *s, CPUS= tate *cpu) s->rip_offset =3D 0; /* for relative ip address */ s->vex_l =3D 0; s->vex_v =3D 0; - if (sigsetjmp(s->jmpbuf, 0) !=3D 0) { + disas_save(&snapshot, s); + switch (sigsetjmp(s->jmpbuf, 0)) { + case 0: + break; + case 1: gen_exception_gpf(s); return s->pc; + case 2: + disas_restore(&snapshot, s); + s->base.is_jmp =3D DISAS_TOO_MANY; + return pc_start; + default: + g_assert_not_reached(); } =20 prefixes =3D 0; --=20 2.35.3 From nobody Sat Apr 20 16:27:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.ibm.com ARC-Seal: i=1; a=rsa-sha256; t=1659717280; cv=none; d=zohomail.com; s=zohoarc; b=Ebkp9vmd2pcmCAeUh0yItc4Awl0RpIBdQbC+HaL4SCm9AipfGETNf4ppackOCr+Zn7O2S+uMtkQM1yMnRukEFlyBXOGcDMbxYa4cbNBRS+cVRs6q8nDhtEuN83NTZktR4LrDq3maFbIp9qcpAQkLXMGxpNG61hmgE5T6+hdUzNQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1659717280; 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; bh=mx7WE3EFJlt9psuq5N3BY5Dfy8FVWWpe1R1MZOF6Jm8=; b=kF2jRvgDYR0kO0LDDcQRAqmNsfyfFJzKC1a94agRR5EW+5QEINmPOFDmLJdxlipnxhRa+PleXhpcsttfEwjQXRffG/QfBweqVS/6pFtnMsDKoGMLOlZtBiOweSKVJDbu4VTRW/wDkmog55vow8b3joTtCfn2MYLZZwwBAKS8kAg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1659717280385966.092948219936; Fri, 5 Aug 2022 09:34:40 -0700 (PDT) Received: from localhost ([::1]:49168 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oK0HW-0002Qn-Sk for importer@patchew.org; Fri, 05 Aug 2022 12:34:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43844) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oK0EE-0000qx-NP; Fri, 05 Aug 2022 12:31:22 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:6046 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oK0E9-0003AJ-Q5; Fri, 05 Aug 2022 12:31:11 -0400 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 275GLVWe028537; Fri, 5 Aug 2022 16:30:54 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3hs6ngr6vd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 16:30:53 +0000 Received: from m0098419.ppops.net (m0098419.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 275GQ6Ov011307; Fri, 5 Aug 2022 16:30:53 GMT Received: from ppma06ams.nl.ibm.com (66.31.33a9.ip4.static.sl-reverse.com [169.51.49.102]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3hs6ngr5wg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 16:30:53 +0000 Received: from pps.filterd (ppma06ams.nl.ibm.com [127.0.0.1]) by ppma06ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 275GNSHA017288; Fri, 5 Aug 2022 16:29:58 GMT Received: from b06avi18878370.portsmouth.uk.ibm.com ([9.149.26.194]) by ppma06ams.nl.ibm.com with ESMTP id 3hmuwhym65-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 05 Aug 2022 16:29:58 +0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06avi18878370.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 275G9ZDS15335754 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 5 Aug 2022 16:09:35 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A7215A405C; Fri, 5 Aug 2022 16:09:19 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 107EFA4054; Fri, 5 Aug 2022 16:09:19 +0000 (GMT) Received: from heavy.lan (unknown [9.171.89.138]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 5 Aug 2022 16:09:18 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=mx7WE3EFJlt9psuq5N3BY5Dfy8FVWWpe1R1MZOF6Jm8=; b=IE+EN1HsZd1Mg75Ct8PXtwbeSorSAWYrvL23bHkfpwsIHVa0+tO3XH0GRBBxkI1cup/F e0s7EH1xDA6BurHWOU1Hg6Egs1s9UP4FqHmR56dd3VAC/HYCoASYVhbWyE4RBjW496+M WlB8W6Vh/tUYZtPOXOztHDJDfg60vBFmqL21ruAMLnNp63fYEkBnm3VfaQnzYtt3r7Vr QjncFjSIXRkWNz43iU3lBC9BszB6T6PGfC/Lsrk/txS2l4IZ1EVphFwNnyEYVmUACOTQ KXL8FXtx4TJpN2wF5D4+eDYCSesNBu0MwmYIrJ6mC7R4PzrmvvqIFC8Mrl7DA+AjKoA9 VA== From: Ilya Leoshkevich To: Laurent Vivier , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Yanan Wang , Richard Henderson , Paolo Bonzini , David Hildenbrand , Peter Maydell Cc: qemu-devel@nongnu.org, qemu-s390x@nongnu.org, Christian Borntraeger , Ilya Leoshkevich Subject: [PATCH v2 4/4] tests/tcg: Test siginfo_t contents when jumping to non-readable pages Date: Fri, 5 Aug 2022 18:09:14 +0200 Message-Id: <20220805160914.1106091-5-iii@linux.ibm.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220805160914.1106091-1-iii@linux.ibm.com> References: <20220805160914.1106091-1-iii@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: 80Tt6CUfbyBm_AeNWRr1g0q5kke-BA9I X-Proofpoint-GUID: ZTrXXbB_t7iK7KVZGaxeW4MzCIKkGxbF X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-05_08,2022-08-05_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 spamscore=0 impostorscore=0 suspectscore=0 malwarescore=0 mlxlogscore=999 bulkscore=0 priorityscore=1501 phishscore=0 lowpriorityscore=0 clxscore=1015 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2208050078 Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=148.163.158.5; envelope-from=iii@linux.ibm.com; helo=mx0a-001b2d01.pphosted.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @ibm.com) X-ZM-MESSAGEID: 1659717282073100001 Content-Type: text/plain; charset="utf-8" Add x86_64 and s390x tests to prevent regressions. Signed-off-by: Ilya Leoshkevich --- tests/tcg/multiarch/noexec.h | 114 ++++++++++++++++++++++++ tests/tcg/s390x/Makefile.target | 1 + tests/tcg/s390x/noexec.c | 145 +++++++++++++++++++++++++++++++ tests/tcg/x86_64/Makefile.target | 3 +- tests/tcg/x86_64/noexec.c | 116 +++++++++++++++++++++++++ 5 files changed, 378 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/multiarch/noexec.h create mode 100644 tests/tcg/s390x/noexec.c create mode 100644 tests/tcg/x86_64/noexec.c diff --git a/tests/tcg/multiarch/noexec.h b/tests/tcg/multiarch/noexec.h new file mode 100644 index 0000000000..a76e0aa9ea --- /dev/null +++ b/tests/tcg/multiarch/noexec.h @@ -0,0 +1,114 @@ +/* + * Common code for arch-specific MMU_INST_FETCH fault testing. + * + * Declare struct arch_noexec_test before including this file and define + * arch_check_mcontext() after that. + */ + +#include +#include +#include +#include +#include +#include +#include + +/* Forward declarations. */ + +static void arch_check_mcontext(const struct arch_noexec_test *test, + const mcontext_t *ctx); + +/* Utility functions. */ + +static void safe_print(const char *s) +{ + write(0, s, strlen(s)); +} + +static void safe_puts(const char *s) +{ + safe_print(s); + safe_print("\n"); +} + +#define PAGE_ALIGN(p) (void *)((unsigned long)(p) & ~0xfffUL) + +/* Testing infrastructure. */ + +struct noexec_test { + const char *name; + void (*func)(int); + void *page; + void *expected_si_addr; + struct arch_noexec_test arch; +}; + +static const struct noexec_test *current_noexec_test; + +static void handle_segv(int sig, siginfo_t *info, void *ucontext) +{ + int err; + + if (current_noexec_test =3D=3D NULL) { + safe_puts("[ FAILED ] unexpected SEGV"); + _exit(1); + } + + if (info->si_addr !=3D current_noexec_test->expected_si_addr) { + safe_puts("[ FAILED ] wrong si_addr"); + _exit(1); + } + + arch_check_mcontext(¤t_noexec_test->arch, + &((ucontext_t *)ucontext)->uc_mcontext); + + err =3D mprotect(current_noexec_test->page, 0x1000, PROT_READ | PROT_E= XEC); + if (err !=3D 0) { + safe_puts("[ FAILED ] mprotect() failed"); + _exit(1); + } + + current_noexec_test =3D NULL; +} + +static void test_noexec_1(const struct noexec_test *test) +{ + int ret; + + /* Trigger TB creation in order to test invalidation. */ + test->func(0); + + ret =3D mprotect(test->page, 0x1000, PROT_NONE); + assert(ret =3D=3D 0); + + /* Trigger SEGV and check that handle_segv() ran. */ + current_noexec_test =3D test; + test->func(0); + assert(current_noexec_test =3D=3D NULL); +} + +static int test_noexec(struct noexec_test *tests, size_t n_tests) +{ + struct sigaction act; + size_t i; + int err; + + memset(&act, 0, sizeof(act)); + act.sa_sigaction =3D handle_segv; + act.sa_flags =3D SA_SIGINFO; + err =3D sigaction(SIGSEGV, &act, NULL); + assert(err =3D=3D 0); + + for (i =3D 0; i < n_tests; i++) { + struct noexec_test *test =3D &tests[i]; + + safe_print("[ RUN ] "); + safe_puts(test->name); + test_noexec_1(test); + safe_puts("[ OK ]"); + } + + safe_puts("[ PASSED ]"); + + return EXIT_SUCCESS; +} diff --git a/tests/tcg/s390x/Makefile.target b/tests/tcg/s390x/Makefile.tar= get index 1a7a4a2f59..5e13a41c3f 100644 --- a/tests/tcg/s390x/Makefile.target +++ b/tests/tcg/s390x/Makefile.target @@ -16,6 +16,7 @@ TESTS+=3Dshift TESTS+=3Dtrap TESTS+=3Dsignals-s390x TESTS+=3Dbranch-relative-long +TESTS+=3Dnoexec =20 Z14_TESTS=3Dvfminmax vfminmax: LDFLAGS+=3D-lm diff --git a/tests/tcg/s390x/noexec.c b/tests/tcg/s390x/noexec.c new file mode 100644 index 0000000000..2dfc9ee817 --- /dev/null +++ b/tests/tcg/s390x/noexec.c @@ -0,0 +1,145 @@ +#define _GNU_SOURCE + +struct arch_noexec_test { + void *expected_pswa; + unsigned long expected_r2; +}; + +#include "../multiarch/noexec.h" + +static void arch_check_mcontext(const struct arch_noexec_test *test, + const mcontext_t *ctx) { + if (ctx->psw.addr !=3D (unsigned long)test->expected_pswa) { + safe_puts("[ FAILED ] wrong psw.addr"); + _exit(1); + } + + if (ctx->gregs[2] !=3D test->expected_r2) { + safe_puts("[ FAILED ] wrong r2"); + _exit(1); + } +} + +#define DEFINE_NX(name, offset) \ + void name ## _1(int); \ + void name ## _2(int); \ + void name ## _exrl(int); \ + extern const short name ## _end[]; \ + asm(/* Go to the specified page offset. */ \ + ".align 0x1000\n" \ + ".org .+" #offset "\n" \ + /* %r2 is 0 on entry, overwrite it with 1. */ \ + ".globl " #name "_1\n" \ + #name "_1:\n" \ + ".cfi_startproc\n" \ + "lgfi %r2,1\n" \ + /* Overwrite %2 with 2. */ \ + ".globl " #name "_2\n" \ + #name "_2:\n" \ + "lgfi %r2,2\n" \ + "br %r14\n" \ + /* End of code. */ \ + ".globl " #name "_end\n" \ + #name "_end:\n" \ + ".cfi_endproc\n" \ + /* Go to the next page. */ \ + ".align 0x1000\n" \ + /* Break alignment. */ \ + "nopr %r7\n" \ + ".globl " #name "_exrl\n" \ + #name "_exrl:\n" \ + ".cfi_startproc\n" \ + "exrl %r0," #name "_2\n" \ + "br %r14\n" \ + ".cfi_endproc"); + +/* noexec_1 is executable, noexec_2 is non-executable. */ +DEFINE_NX(noexec, 0xffa); + +/* + * noexec_cross_1 is executable, noexec_cross_2 crosses non-executable page + * boundary. + */ +DEFINE_NX(noexec_cross, 0xff8); + +/* noexec_full_1 and noexec_full_2 are non-executable. */ +DEFINE_NX(noexec_full, 0x322); + +int main(void) +{ + struct noexec_test noexec_tests[] =3D { + { + .name =3D "Fallthrough", + .func =3D noexec_1, + .page =3D noexec_2, + .expected_si_addr =3D noexec_2, + .arch =3D { + .expected_pswa =3D noexec_2, + .expected_r2 =3D 1, + }, + }, + { + .name =3D "Jump", + .func =3D noexec_2, + .page =3D noexec_2, + .expected_si_addr =3D noexec_2, + .arch =3D { + .expected_pswa =3D noexec_2, + .expected_r2 =3D 0, + }, + }, + { + .name =3D "EXRL", + .func =3D noexec_exrl, + .page =3D noexec_2, + .expected_si_addr =3D PAGE_ALIGN(noexec_end), + .arch =3D { + .expected_pswa =3D noexec_exrl, + .expected_r2 =3D 0, + }, + }, + { + .name =3D "Fallthrough [cross]", + .func =3D noexec_cross_1, + .page =3D PAGE_ALIGN(noexec_cross_end), + .expected_si_addr =3D PAGE_ALIGN(noexec_cross_end), + .arch =3D { + .expected_pswa =3D noexec_cross_2, + .expected_r2 =3D 1, + }, + }, + { + .name =3D "Jump [cross]", + .func =3D noexec_cross_2, + .page =3D PAGE_ALIGN(noexec_cross_end), + .expected_si_addr =3D PAGE_ALIGN(noexec_cross_end), + .arch =3D { + .expected_pswa =3D noexec_cross_2, + .expected_r2 =3D 0, + }, + }, + { + .name =3D "EXRL [cross]", + .func =3D noexec_cross_exrl, + .page =3D PAGE_ALIGN(noexec_cross_end), + .expected_si_addr =3D PAGE_ALIGN(noexec_cross_end), + .arch =3D { + .expected_pswa =3D noexec_cross_exrl, + .expected_r2 =3D 0, + }, + }, + { + .name =3D "Jump [full]", + .func =3D noexec_full_1, + .page =3D PAGE_ALIGN(noexec_full_1), + .expected_si_addr =3D PAGE_ALIGN(noexec_full_1), + .arch =3D { + .expected_pswa =3D noexec_full_1, + .expected_r2 =3D 0, + }, + }, + }; + + return test_noexec(noexec_tests, + sizeof(noexec_tests) / sizeof(noexec_tests[0])); +} diff --git a/tests/tcg/x86_64/Makefile.target b/tests/tcg/x86_64/Makefile.t= arget index b71a6bcd5e..c0e7e5b005 100644 --- a/tests/tcg/x86_64/Makefile.target +++ b/tests/tcg/x86_64/Makefile.target @@ -10,6 +10,7 @@ include $(SRC_PATH)/tests/tcg/i386/Makefile.target =20 ifeq ($(filter %-linux-user, $(TARGET)),$(TARGET)) X86_64_TESTS +=3D vsyscall +X86_64_TESTS +=3D noexec TESTS=3D$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64 else TESTS=3D$(MULTIARCH_TESTS) @@ -20,5 +21,5 @@ test-x86_64: LDFLAGS+=3D-lm -lc test-x86_64: test-i386.c test-i386.h test-i386-shift.h test-i386-muldiv.h $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) =20 -vsyscall: $(SRC_PATH)/tests/tcg/x86_64/vsyscall.c +%: $(SRC_PATH)/tests/tcg/x86_64/%.c $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) diff --git a/tests/tcg/x86_64/noexec.c b/tests/tcg/x86_64/noexec.c new file mode 100644 index 0000000000..ec07c9f0ba --- /dev/null +++ b/tests/tcg/x86_64/noexec.c @@ -0,0 +1,116 @@ +#define _GNU_SOURCE + +struct arch_noexec_test { + void *expected_rip; + unsigned long expected_rdi; +}; + +#include "../multiarch/noexec.h" + +static void arch_check_mcontext(const struct arch_noexec_test *test, + const mcontext_t *ctx) { + if (ctx->gregs[REG_RIP] !=3D (unsigned long)test->expected_rip) { + safe_puts("[ FAILED ] wrong rip"); + _exit(1); + } + + if (ctx->gregs[REG_RDI] !=3D test->expected_rdi) { + safe_puts("[ FAILED ] wrong rdi"); + _exit(1); + } +} + +#define DEFINE_NX(name, offset) \ + void name ## _1(int); \ + void name ## _2(int); \ + extern const short name ## _end[]; \ + asm(/* Go to the specified page offset. */ \ + ".align 0x1000\n" \ + ".org .+" #offset "\n" \ + /* %rdi is 0 on entry, overwrite it with 1. */ \ + ".globl " #name "_1\n" \ + #name "_1:\n" \ + ".cfi_startproc\n" \ + "movq $1,%rdi\n" \ + /* Overwrite %rdi with 2. */ \ + ".globl " #name "_2\n" \ + #name "_2:\n" \ + "movq $2,%rdi\n" \ + "ret\n" \ + /* End of code. */ \ + ".globl " #name "_end\n" \ + #name "_end:\n" \ + ".cfi_endproc\n" \ + /* Go to the next page. */ \ + ".align 0x1000"); + +/* noexec_1 is executable, noexec_2 is non-executable. */ +DEFINE_NX(noexec, 0xff9); + +/* + * noexec_cross_1 is executable, noexec_cross_2 crosses non-executable page + * boundary. + */ +DEFINE_NX(noexec_cross, 0xff8); + +/* noexec_full_1 and noexec_full_2 are non-executable. */ +DEFINE_NX(noexec_full, 0x321); + +int main(void) +{ + struct noexec_test noexec_tests[] =3D { + { + .name =3D "Fallthrough", + .func =3D noexec_1, + .page =3D noexec_2, + .expected_si_addr =3D noexec_2, + .arch =3D { + .expected_rip =3D noexec_2, + .expected_rdi =3D 1, + }, + }, + { + .name =3D "Jump", + .func =3D noexec_2, + .page =3D noexec_2, + .expected_si_addr =3D noexec_2, + .arch =3D { + .expected_rip =3D noexec_2, + .expected_rdi =3D 0, + }, + }, + { + .name =3D "Fallthrough [cross]", + .func =3D noexec_cross_1, + .page =3D PAGE_ALIGN(noexec_cross_end), + .expected_si_addr =3D PAGE_ALIGN(noexec_cross_end), + .arch =3D { + .expected_rip =3D noexec_cross_2, + .expected_rdi =3D 1, + }, + }, + { + .name =3D "Jump [cross]", + .func =3D noexec_cross_2, + .page =3D PAGE_ALIGN(noexec_cross_end), + .expected_si_addr =3D PAGE_ALIGN(noexec_cross_end), + .arch =3D { + .expected_rip =3D noexec_cross_2, + .expected_rdi =3D 0, + }, + }, + { + .name =3D "Jump [full]", + .func =3D noexec_full_1, + .page =3D PAGE_ALIGN(noexec_full_1), + .expected_si_addr =3D noexec_full_1, + .arch =3D { + .expected_rip =3D noexec_full_1, + .expected_rdi =3D 0, + }, + }, + }; + + return test_noexec(noexec_tests, + sizeof(noexec_tests) / sizeof(noexec_tests[0])); +} --=20 2.35.3