From nobody Tue Nov 26 17:21:29 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=reject dis=none) header.from=linux.ibm.com ARC-Seal: i=1; a=rsa-sha256; t=1706520951; cv=none; d=zohomail.com; s=zohoarc; b=KKaFcXdAlO21JeL+KNAOTPjKwBCrrNmBkwUNNYedTxZ+8CND+ZTOm/iVBR+N0Tq1zGedOK3h1MoAM5Kw92hVgb/6789KVHffUZzKX/vm3YfPPuksAiP3rzHh/k66k3sC5vhOxfTmwIuix+0fagJJ+IbwU+hZVEnIlUw41KyADao= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1706520951; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=EMJdS9hKoA7kur7DqvESiD5ICfk5HChs5chrzcRw4VA=; b=B3B48DdkcTaPxJnvuowcVS7CDP2QoNfaASgIgD3yT7Roe/gwBYggqVV8UOwFJX1GdBJfM27ShsR9LGUTta3qxylBWNJoUCPOyYBkMr+ZbgjSjcRzxUFr9Our1myAh298hg0mBTB7Ci03yviel+hQRRu2jcEPvpitW+nehAVCQEo= 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=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1706520950997542.7486782504848; Mon, 29 Jan 2024 01:35:50 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rUO2A-000270-Fa; Mon, 29 Jan 2024 04:34:30 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rUO26-00025U-F4; Mon, 29 Jan 2024 04:34:26 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rUO23-0008Sf-TW; Mon, 29 Jan 2024 04:34:25 -0500 Received: from pps.filterd (m0353723.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 40T85RFo023735; Mon, 29 Jan 2024 09:34:21 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3vx86vsvua-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 29 Jan 2024 09:34:21 +0000 Received: from m0353723.ppops.net (m0353723.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 40T8tqGD027692; Mon, 29 Jan 2024 09:34:20 GMT Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3vx86vsvu2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 29 Jan 2024 09:34:20 +0000 Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 40T6jKaV007188; Mon, 29 Jan 2024 09:34:20 GMT Received: from smtprelay03.fra02v.mail.ibm.com ([9.218.2.224]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 3vwev1xvry-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 29 Jan 2024 09:34:20 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay03.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 40T9YIGH24314388 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 29 Jan 2024 09:34:18 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1870D20043; Mon, 29 Jan 2024 09:34:18 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7E2DF20040; Mon, 29 Jan 2024 09:34:17 +0000 (GMT) Received: from heavy.ibm.com (unknown [9.179.7.240]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 29 Jan 2024 09:34: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=EMJdS9hKoA7kur7DqvESiD5ICfk5HChs5chrzcRw4VA=; b=iKiG6O065kT97uCxVSERS2RUqwr84bZ5G8ph6PkYQMh4W525rQfTDuTSn8NhhVTxzV6G fmRoaX7wKpsWuJfYMsresSzCtNSCEwpK2rG5LKsK4S2XSBKglXNM9FTNx1HEy53qB7jx Vd+ToIuBIVppy2//ilGKBIl+AKjc+Nv7MTYDuM+sMS8qcRRsfGepwZRVT5FH0FKq4lek wAFGM5oXFKZ66jjxyABF0qHS8z9tNIRFWRiyiQ/TukPCySwWXD4AOWvX/+jGbC18+grv fx4LhyEN7naOX12OThZz+TO7K9uDRVW2YSzNPC2qY/WQ3XFvQ+61gvuRn8HoKF8yDYZJ ew== From: Ilya Leoshkevich To: Richard Henderson , Peter Maydell , =?UTF-8?q?Alex=20Benn=C3=A9e?= , David Hildenbrand Cc: Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-devel@nongnu.org, qemu-arm@nongnu.org, qemu-s390x@nongnu.org, Ilya Leoshkevich Subject: [PATCH v4 1/3] linux-user: Allow gdbstub to ignore page protection Date: Mon, 29 Jan 2024 10:32:14 +0100 Message-ID: <20240129093410.3151-2-iii@linux.ibm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240129093410.3151-1-iii@linux.ibm.com> References: <20240129093410.3151-1-iii@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: R7YmqST-Dcuiy1Fw9vbI2XzNZPN04mln X-Proofpoint-ORIG-GUID: wgwPKY-5wCbwjbq3D4rqeYil9sWISlbA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-01-29_05,2024-01-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 spamscore=0 mlxlogscore=951 mlxscore=0 phishscore=0 bulkscore=0 suspectscore=0 adultscore=0 clxscore=1015 lowpriorityscore=0 malwarescore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401290068 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=mx0b-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_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, 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-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ibm.com) X-ZM-MESSAGEID: 1706520951869100001 Content-Type: text/plain; charset="utf-8" gdbserver ignores page protection by virtue of using /proc/$pid/mem. Teach qemu gdbstub to do this too. This will not work if /proc is not mounted; accept this limitation. One alternative is to temporarily grant the missing PROT_* bit, but this is inherently racy. Another alternative is self-debugging with ptrace(POKE), which will break if QEMU itself is being debugged - a much more severe limitation. Reviewed-by: Richard Henderson Signed-off-by: Ilya Leoshkevich --- cpu-target.c | 78 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 15 deletions(-) diff --git a/cpu-target.c b/cpu-target.c index f6e07c3deb1..958d63e8823 100644 --- a/cpu-target.c +++ b/cpu-target.c @@ -382,6 +382,9 @@ int cpu_memory_rw_debug(CPUState *cpu, vaddr addr, vaddr l, page; void * p; uint8_t *buf =3D ptr; + ssize_t written; + int ret =3D -1; + int fd =3D -1; =20 while (len > 0) { page =3D addr & TARGET_PAGE_MASK; @@ -389,30 +392,75 @@ int cpu_memory_rw_debug(CPUState *cpu, vaddr addr, if (l > len) l =3D len; flags =3D page_get_flags(page); - if (!(flags & PAGE_VALID)) - return -1; + if (!(flags & PAGE_VALID)) { + goto out_close; + } if (is_write) { - if (!(flags & PAGE_WRITE)) - return -1; - /* XXX: this code should not depend on lock_user */ - if (!(p =3D lock_user(VERIFY_WRITE, addr, l, 0))) - return -1; - memcpy(p, buf, l); - unlock_user(p, addr, l); - } else { - if (!(flags & PAGE_READ)) - return -1; + if (flags & PAGE_WRITE) { + /* XXX: this code should not depend on lock_user */ + p =3D lock_user(VERIFY_WRITE, addr, l, 0); + if (!p) { + goto out_close; + } + memcpy(p, buf, l); + unlock_user(p, addr, l); + } else { + /* Bypass the host page protection using ptrace. */ + if (fd =3D=3D -1) { + fd =3D open("/proc/self/mem", O_WRONLY); + if (fd =3D=3D -1) { + goto out; + } + } + /* + * If there is a TranslationBlock and we weren't bypassing= the + * host page protection, the memcpy() above would SEGV, + * ultimately leading to page_unprotect(). So invalidate t= he + * translations manually. Both invalidation and pwrite() m= ust + * be under mmap_lock() in order to prevent the creation of + * another TranslationBlock in between. + */ + mmap_lock(); + tb_invalidate_phys_range(addr, addr + l - 1); + written =3D pwrite(fd, buf, l, + (off_t)(uintptr_t)g2h_untagged(addr)); + mmap_unlock(); + if (written !=3D l) { + goto out_close; + } + } + } else if (flags & PAGE_READ) { /* XXX: this code should not depend on lock_user */ - if (!(p =3D lock_user(VERIFY_READ, addr, l, 1))) - return -1; + p =3D lock_user(VERIFY_READ, addr, l, 1); + if (!p) { + goto out_close; + } memcpy(buf, p, l); unlock_user(p, addr, 0); + } else { + /* Bypass the host page protection using ptrace. */ + if (fd =3D=3D -1) { + fd =3D open("/proc/self/mem", O_RDONLY); + if (fd =3D=3D -1) { + goto out; + } + } + if (pread(fd, buf, l, + (off_t)(uintptr_t)g2h_untagged(addr)) !=3D l) { + goto out_close; + } } len -=3D l; buf +=3D l; addr +=3D l; } - return 0; + ret =3D 0; +out_close: + if (fd !=3D -1) { + close(fd); + } +out: + return ret; } #endif =20 --=20 2.43.0