From nobody Tue Nov 26 09:35:54 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=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1709152022; cv=none; d=zohomail.com; s=zohoarc; b=csZEKqxbmucxAVEkmAzikZ1Vj1rpuzY7ZHfw31RQRbDZjSZHicgcrxKb4uLfq382MS7Frfujayolf15JwLBwegAuxRv01vl50+1ETkeZ6JtX/uGs0bhftpKnn38G1rw6tI0TozSbF929NmqxmKWIHysRdJi6XcXNw5jthcMzXz0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1709152022; 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=uPBaP6Nvz5twvhuDlhbUvNtwTF9Ex3S1vqWc/rUTDIY=; b=R/IyFYG+a4kzCSbNIotj8BgQpMkGX+UnNLN3T2geNnPRMWpS7/wQZk+BgN6X1h5wQF6wtROGaq+dTZHHXBePii0fLPRzqpTIXZYn7ZVmCjV4Rm1+AtFzGaocpc02ZaPaPZvrRBsUHC1Pdp+xb9dgOmTA9YDjh59Y5u6KWQkOFVw= 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 1709152022366317.9129623147941; Wed, 28 Feb 2024 12:27:02 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rfQUf-00080E-7W; Wed, 28 Feb 2024 15:25:33 -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 1rfQUd-0007zv-HM for qemu-devel@nongnu.org; Wed, 28 Feb 2024 15:25:31 -0500 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rfQUb-0007Mc-HQ for qemu-devel@nongnu.org; Wed, 28 Feb 2024 15:25:31 -0500 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-29a378040daso63075a91.1 for ; Wed, 28 Feb 2024 12:25:28 -0800 (PST) Received: from stoup.. (098-147-055-211.res.spectrum.com. [98.147.55.211]) by smtp.gmail.com with ESMTPSA id oh5-20020a17090b3a4500b0029ab712f648sm2118499pjb.38.2024.02.28.12.25.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 12:25:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1709151928; x=1709756728; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uPBaP6Nvz5twvhuDlhbUvNtwTF9Ex3S1vqWc/rUTDIY=; b=vIk6xCZVq7QLKagEaxk/6V/AmZVmNtsMMSM63yYfmT9DPqfxQldSxocjOXqiTL5j0K pLmjEwt7qyQLy0biuaeNH5KDnWnrlr4lkrlvCYruwgoZq+MBaNZJKT7TNQUXCAbo1jsO 4BCLmvpYYYi56HVsA7TbTViOQkarhZrgMUvktvYeVTgxOyzkL8vBXLqlmGg05P8tPdMq maHCKjOnTo/u0fqt9LmUD3MGC1xElaMxag5KOkJBs/34hGZjAQcCzDCx3E25kbSH/lBX 2PygybeQgg8tibyXozliMqcfE558F0krZqNIEEHWzM74jcp0OaLz5VNCdK2UmhqY55nz vYDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709151928; x=1709756728; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uPBaP6Nvz5twvhuDlhbUvNtwTF9Ex3S1vqWc/rUTDIY=; b=a5+lCqFEWbS836Cv6Q7XaDDg2rcTyZs1GUzN+b8gj/n2aBJ2SRznWKbKmnvmR3GkDS lvP35BfUc81Ks+r5mN2qrZrEOx4lVC0lfd9CL1D8DW5UkiK1MehbJs91xFUa+vwDn7Dk ufT/hHPY4Bwoyh1fWzfBhRj7RnFw4qZPEL9N7cS0MJljUr0hvmW2F3YDR8D1U9GJQoge kzO8sE7Je+AjjSLaXZy0PcZYfLbu5gP9itKFGB9b0qFLWKf418m+a3uGF+uBKNgs4SAO YKEdqbhAqyPoCXtSzv4cfKCgg2Sgez99MS+QgpHR3r4//5nwIaXy1ISb+VTydJ9UiKh7 90ag== X-Gm-Message-State: AOJu0YzuTlH5PabBGiBRu4D3UmoMCbLzmwM7Xct8mu3Kt2DmpcVNhsxC lrQ+4Ag6I0ScIjKDcdk32Bu/S+ixOzl/Ar8LqRlLkEt2Vv4m+3RqwmQHu5DLyHJW69YQ8weKX4t Z X-Google-Smtp-Source: AGHT+IFvthCH2/jTS1/fq1T6qH3gqIG92ZgYOBDfSTpW9Lt4JOtDM7Ztd3jYNKaOmVpgg125piux8g== X-Received: by 2002:a17:90b:30c6:b0:29a:b67b:fe5e with SMTP id hi6-20020a17090b30c600b0029ab67bfe5emr235652pjb.36.1709151927903; Wed, 28 Feb 2024 12:25:27 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, richard.purdie@linuxfoundation.org, mjt@tls.msk.ru, iii@linux.ibm.com Subject: [PATCH v2 4/5] linux-user: Rewrite target_shmat Date: Wed, 28 Feb 2024 10:25:17 -1000 Message-Id: <20240228202518.33180-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240228202518.33180-1-richard.henderson@linaro.org> References: <20240228202518.33180-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::1031; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1031.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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 @linaro.org) X-ZM-MESSAGEID: 1709152023993100007 Content-Type: text/plain; charset="utf-8" Handle combined host and guest alignment requirements. Handle host and guest page size differences. Handle SHM_EXEC. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/115 Signed-off-by: Richard Henderson --- linux-user/mmap.c | 172 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 136 insertions(+), 36 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 82f4026283..4505fd7376 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -1236,69 +1236,161 @@ static inline abi_ulong target_shmlba(CPUArchState= *cpu_env) } #endif =20 +#if defined(__arm__) || defined(__mips__) || defined(__sparc__) +#define HOST_FORCE_SHMLBA 1 +#else +#define HOST_FORCE_SHMLBA 0 +#endif + abi_ulong target_shmat(CPUArchState *cpu_env, int shmid, abi_ulong shmaddr, int shmflg) { CPUState *cpu =3D env_cpu(cpu_env); - abi_ulong raddr; struct shmid_ds shm_info; int ret; - abi_ulong shmlba; + int h_pagesize; + int t_shmlba, h_shmlba, m_shmlba; + size_t t_len, h_len, m_len; =20 /* shmat pointers are always untagged */ =20 - /* find out the length of the shared memory segment */ + /* + * Because we can't use host shmat() unless the address is sufficiently + * aligned for the host, we'll need to check both. + * TODO: Could be fixed with softmmu. + */ + t_shmlba =3D target_shmlba(cpu_env); + h_pagesize =3D qemu_real_host_page_size(); + h_shmlba =3D (HOST_FORCE_SHMLBA ? SHMLBA : h_pagesize); + m_shmlba =3D MAX(t_shmlba, h_shmlba); + + if (shmaddr) { + if (shmaddr & (m_shmlba - 1)) { + if (shmflg & SHM_RND) { + /* + * The guest is allowing the kernel to round the address. + * Assume that the guest is ok with us rounding to the + * host required alignment too. Anyway if we don't, we'll + * get an error from the kernel. + */ + shmaddr &=3D ~(m_shmlba - 1); + if (shmaddr =3D=3D 0 && (shmflg & SHM_REMAP)) { + return -TARGET_EINVAL; + } + } else { + int require =3D TARGET_PAGE_SIZE; +#ifdef TARGET_FORCE_SHMLBA + require =3D t_shmlba; +#endif + /* + * Include host required alignment, as otherwise we cannot + * use host shmat at all. + */ + require =3D MAX(require, h_shmlba); + if (shmaddr & (require - 1)) { + return -TARGET_EINVAL; + } + } + } + } else { + if (shmflg & SHM_REMAP) { + return -TARGET_EINVAL; + } + } + /* All rounding now manually concluded. */ + shmflg &=3D ~SHM_RND; + + /* Find out the length of the shared memory segment. */ ret =3D get_errno(shmctl(shmid, IPC_STAT, &shm_info)); if (is_error(ret)) { /* can't get length, bail out */ return ret; } + t_len =3D TARGET_PAGE_ALIGN(shm_info.shm_segsz); + h_len =3D ROUND_UP(shm_info.shm_segsz, h_pagesize); + m_len =3D MAX(t_len, h_len); =20 - shmlba =3D target_shmlba(cpu_env); - - if (shmaddr & (shmlba - 1)) { - if (shmflg & SHM_RND) { - shmaddr &=3D ~(shmlba - 1); - } else { - return -TARGET_EINVAL; - } - } - if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) { + if (!guest_range_valid_untagged(shmaddr, m_len)) { return -TARGET_EINVAL; } =20 WITH_MMAP_LOCK_GUARD() { - void *host_raddr; + bool mapped =3D false; + void *want, *test; abi_ulong last; =20 - if (shmaddr) { - host_raddr =3D shmat(shmid, (void *)g2h_untagged(shmaddr), shm= flg); - } else { - abi_ulong mmap_start; - - /* In order to use the host shmat, we need to honor host SHMLB= A. */ - mmap_start =3D mmap_find_vma(0, shm_info.shm_segsz, - MAX(SHMLBA, shmlba)); - - if (mmap_start =3D=3D -1) { + if (!shmaddr) { + shmaddr =3D mmap_find_vma(0, m_len, m_shmlba); + if (shmaddr =3D=3D -1) { return -TARGET_ENOMEM; } - host_raddr =3D shmat(shmid, g2h_untagged(mmap_start), - shmflg | SHM_REMAP); + mapped =3D !reserved_va; + } else if (shmflg & SHM_REMAP) { + /* + * If host page size > target page size, the host shmat may map + * more memory than the guest expects. Reject a mapping that + * would replace memory in the unexpected gap. + * TODO: Could be fixed with softmmu. + */ + if (t_len < h_len && + !page_check_range_empty(shmaddr + t_len, + shmaddr + h_len - 1)) { + return -TARGET_EINVAL; + } + } else { + if (!page_check_range_empty(shmaddr, shmaddr + m_len - 1)) { + return -TARGET_EINVAL; + } } =20 - if (host_raddr =3D=3D (void *)-1) { - return get_errno(-1); - } - raddr =3D h2g(host_raddr); - last =3D raddr + shm_info.shm_segsz - 1; + /* All placement is now complete. */ + want =3D (void *)g2h_untagged(shmaddr); =20 - page_set_flags(raddr, last, + /* + * Map anonymous pages across the entire range, then remap with + * the shared memory. This is required for a number of corner + * cases for which host and guest page sizes differ. + */ + if (h_len !=3D t_len) { + int mmap_p =3D PROT_READ | (shmflg & SHM_RDONLY ? 0 : PROT_WRI= TE); + int mmap_f =3D MAP_PRIVATE | MAP_ANONYMOUS + | (reserved_va || (shmflg & SHM_REMAP) + ? MAP_FIXED : MAP_FIXED_NOREPLACE); + + test =3D mmap(want, m_len, mmap_p, mmap_f, -1, 0); + if (unlikely(test !=3D want)) { + /* shmat returns EINVAL not EEXIST like mmap. */ + ret =3D (test =3D=3D MAP_FAILED && errno !=3D EEXIST + ? get_errno(-1) : -TARGET_EINVAL); + if (mapped) { + do_munmap(want, m_len); + } + return ret; + } + mapped =3D true; + } + + if (reserved_va || mapped) { + shmflg |=3D SHM_REMAP; + } + test =3D shmat(shmid, want, shmflg); + if (test =3D=3D MAP_FAILED) { + ret =3D get_errno(-1); + if (mapped) { + do_munmap(want, m_len); + } + return ret; + } + assert(test =3D=3D want); + + last =3D shmaddr + m_len - 1; + page_set_flags(shmaddr, last, PAGE_VALID | PAGE_RESET | PAGE_READ | - (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE)); + (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE) | + (shmflg & SHM_EXEC ? PAGE_EXEC : 0)); =20 - shm_region_rm_complete(raddr, last); - shm_region_add(raddr, last); + shm_region_rm_complete(shmaddr, last); + shm_region_add(shmaddr, last); } =20 /* @@ -1312,7 +1404,15 @@ abi_ulong target_shmat(CPUArchState *cpu_env, int sh= mid, tb_flush(cpu); } =20 - return raddr; + if (qemu_loglevel_mask(CPU_LOG_PAGE)) { + FILE *f =3D qemu_log_trylock(); + if (f) { + fprintf(f, "page layout changed following shmat\n"); + page_dump(f); + qemu_log_unlock(f); + } + } + return shmaddr; } =20 abi_long target_shmdt(abi_ulong shmaddr) --=20 2.34.1