From nobody Tue Nov 26 20:13:44 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=1705365450; cv=none; d=zohomail.com; s=zohoarc; b=kOob7eBOzORUa3qr4Ov3bJooDho9zoelc/qO4/sgRu3mVJ8PotErYaUJynxRCQ9w10ovDz5Q04O+kp7r1OVZkucXoNehULZGNzTPU4QQH0lH15/lJnd1GjUk2XmrRqr+JslFY4o5e4nXT15e1EyvE2tg2cY1Hxg6OiMet+6oGVM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705365450; 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=t1SC+zmPpc1Y0OU/FyOVQC5ZJl78RtELoSmL2Pk8z88=; b=dSkTma+Ud2VzQ8ID0KwqfJHJp8ANeDbEAz4nstM2I0N4MOBR0cprwkCe53ZU2ohdwj1/EAgRkSHOCiuYdSHX6r7+A1IkeCqaUElyG4macm8y0F2CbzLHCaq+r6+L/IjO3t+ese0CSVsqBguXG3EsJEEt2uH02k3vHeK5RA8YvkM= 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 1705365450594564.4551203477955; Mon, 15 Jan 2024 16:37:30 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rPXRG-0005h9-Dh; Mon, 15 Jan 2024 19:36:22 -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 1rPXR9-0005ce-EG; Mon, 15 Jan 2024 19:36:15 -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 1rPXR5-00085T-46; Mon, 15 Jan 2024 19:36:13 -0500 Received: from pps.filterd (m0353725.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 40FNTxSX010544; Tue, 16 Jan 2024 00:36:02 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3vncnekb2p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jan 2024 00:36:02 +0000 Received: from m0353725.ppops.net (m0353725.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 40FNsO3H002689; Tue, 16 Jan 2024 00:36:02 GMT Received: from ppma12.dal12v.mail.ibm.com (dc.9e.1632.ip4.static.sl-reverse.com [50.22.158.220]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3vncnekb27-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jan 2024 00:36:02 +0000 Received: from pps.filterd (ppma12.dal12v.mail.ibm.com [127.0.0.1]) by ppma12.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 40FNHk50014890; Tue, 16 Jan 2024 00:36:00 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma12.dal12v.mail.ibm.com (PPS) with ESMTPS id 3vm4uskph7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 16 Jan 2024 00:36:00 +0000 Received: from smtpav01.fra02v.mail.ibm.com (smtpav01.fra02v.mail.ibm.com [10.20.54.100]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 40G0ZwAw4063896 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 16 Jan 2024 00:35:59 GMT Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D886820043; Tue, 16 Jan 2024 00:35:58 +0000 (GMT) Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 366C620040; Tue, 16 Jan 2024 00:35:58 +0000 (GMT) Received: from heavy.boeblingen.de.ibm.com (unknown [9.179.4.64]) by smtpav01.fra02v.mail.ibm.com (Postfix) with ESMTP; Tue, 16 Jan 2024 00:35:58 +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=t1SC+zmPpc1Y0OU/FyOVQC5ZJl78RtELoSmL2Pk8z88=; b=XkdbvgHpx16Bcpshp6weSMupdHX1W/SQwCNSXDoaSWO/jmuHj588U6Xm5iMUS2++IPY7 C+J+l3qjxhPDaI+/CdqGdMyUtkvkWQfkeEMd5tfcrknsouwpAJLJdPtkz+X5a/kAvWzg 6ZoK6DBudP0yQ+xxXTRpXatADrQw7yW2qK2WYcJSXbj6gC2K/z83Sw7PMiT1OCCuCHSQ FEb5nm+w/FpXKMl4FW8kp0UQth6A3EkGoi0Xi5KIs4I/sP7/6Df5zjzaFJnlT8QiDmzl gMWAXyKVlslCOIAs8+0MjaF2aSFu4pMalvGZ7wcKfU/2+ib1AC1LjjTekSISP3mMoR6i 5Q== 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 v3 1/3] linux-user: Allow gdbstub to ignore page protection Date: Tue, 16 Jan 2024 01:31:40 +0100 Message-ID: <20240116003551.75168-2-iii@linux.ibm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240116003551.75168-1-iii@linux.ibm.com> References: <20240116003551.75168-1-iii@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: khQyEJLnj7dh6oK_bGSa1nhVrz2kALtF X-Proofpoint-GUID: cIivWzxTHXKmxZHgbYsOs1hIISBdEjJM X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-01-15_17,2024-01-15_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 clxscore=1015 phishscore=0 spamscore=0 lowpriorityscore=0 suspectscore=0 mlxlogscore=961 impostorscore=0 adultscore=0 malwarescore=0 priorityscore=1501 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401160002 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.001, RCVD_IN_MSPIKE_WL=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-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ibm.com) X-ZM-MESSAGEID: 1705365451450100005 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 | 76 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/cpu-target.c b/cpu-target.c index 5eecd7ea2d7..723f6af5fba 100644 --- a/cpu-target.c +++ b/cpu-target.c @@ -406,6 +406,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; @@ -413,30 +416,73 @@ 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)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)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