From nobody Tue Nov 26 10:49:17 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=1708657440; cv=none; d=zohomail.com; s=zohoarc; b=Wb/RZcYQXbiJYzK7XcToZ67sOVPTZON0mtpN5IVsQfJC7psbQOtY8y2BHYXJqObFrJjvU5Qjd+Ev+Zl8wLDgga2BDBAh3cbNwV6XDDmQbKgRA34+sjebqKOW4vCoOqFuL/tKa2LE5tqnaexR5HcJSm3k4kBXmFL2LkLuzV6NWtU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1708657440; 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=/sktbWKa1binb241q0r6ct6YkNzdPk613k2Pdqrs3XY=; b=PVQmSsKpbvx0HF2mBmEwvSQghZ5KX7jGkV1dG906rx0n3kYQ42OkSAy6lS8ZfByEDwCyZ64oZKVZWS3WxUr97hXwujZWbL7CEI5uZHkaLpvo/N96N2bDuU+GPnFjX0O/bOs3P2J1KYu/6hNWndJ5MhRpwq60yiXlTqtIzg8k8VA= 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 1708657440052391.7681536153542; Thu, 22 Feb 2024 19:04:00 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rdLqN-0002d7-Tm; Thu, 22 Feb 2024 22:03:23 -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 1rdLqK-0002cR-QM for qemu-devel@nongnu.org; Thu, 22 Feb 2024 22:03:21 -0500 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rdLqI-0000cq-Vp for qemu-devel@nongnu.org; Thu, 22 Feb 2024 22:03:20 -0500 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-6e08dd0fa0bso411345b3a.1 for ; Thu, 22 Feb 2024 19:03:18 -0800 (PST) Received: from stoup.. (173-197-098-125.biz.spectrum.com. [173.197.98.125]) by smtp.gmail.com with ESMTPSA id n38-20020a056a000d6600b006e31f615af6sm11594159pfv.17.2024.02.22.19.03.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 19:03:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708657397; x=1709262197; 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=/sktbWKa1binb241q0r6ct6YkNzdPk613k2Pdqrs3XY=; b=WPC2MgZmL++cV5nYo1BOWU+lOWnIuzL+7gYBaqtMQqRBwe1Ycn5FUITv76VUaAUN7J R9BGClzmoYQTTsDCg9OMjP0rq0nKD9jCOZXCkmdRSPwGD0RflZ43ABWpR2Y9Q3XMgjsh 4Gck01YhqbHHsiYYyCLo4S/coJfvScaFCAUc0C0XlRaPz1nkHWX8XVmTu807l2+I/0Eu urhoorMtdRzplBRcbPDpI1v/MDTDGLy9JL8rG81ndJWhxIowx5LLMOwBfiL0vB8RchP6 Qj7AND2zlHlN0/V8ap6/H2JAnMu0gZOG5KEpkyBeMkk5BXLkbzxPgAbRJmXquSDShHoB pTpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708657397; x=1709262197; 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=/sktbWKa1binb241q0r6ct6YkNzdPk613k2Pdqrs3XY=; b=IsQjTQqAGenV7mLVPm6oGk9pblLgOSwbBcAlviZzI5SguOFj+dghxUQ3SCN/1uP2a4 pCq2+nmYIS+yVgfQiRBRqLq80otUvck0rkzBoFxBenlSd9AwWvdAnG3CFBobgT+LdpuW IsQm51VAfg50jDXOak7mtVVwxHF/QA8zrC4Axtm9QCy3qAJQ2RndvyntEBG+cLINmgO3 x1CiBwCRgT2VOnBt0KQhLA7lk806Mi+21nA3YGVb25LRkaDpODUewsJBQDkHh7izqvDl MtP8nfUbWaPF/hmCgZET84OpssVQAxllis+qGJYwLW/kI1Ovichl7iUgR7jPnVpppSpu iu7w== X-Gm-Message-State: AOJu0YyesQWAM/kI2HdG4I1c87LDVaY1AEywPv9H7MEXlKevXgj0mo21 9f97h+2fco4pnPm5pifypdnIthDvxSWOt9WZYdO3/FZjnkN5YjSDRQbOPbGYMpNtjXcQHbBo7+h m X-Google-Smtp-Source: AGHT+IHTflDvkUzGMyqQfgieheaBVY+CPzniQDOQ9PGNgSEtei/QtAqc469kvKQWZsSiZZX5HPD/NA== X-Received: by 2002:a05:6a00:1807:b0:6e4:8b07:1060 with SMTP id y7-20020a056a00180700b006e48b071060mr6500541pfa.14.1708657397435; Thu, 22 Feb 2024 19:03:17 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, iii@linux.ibm.com, richard.purdie@linuxfoundation.org, mjt@tls.msk.ru Subject: [PATCH 3/3] linux-user: Rewrite target_shmat Date: Thu, 22 Feb 2024 17:03:09 -1000 Message-Id: <20240223030309.458451-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240223030309.458451-1-richard.henderson@linaro.org> References: <20240223030309.458451-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::432; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x432.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: 1708657440362100001 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 Reviewed-by: Ilya Leoshkevich --- linux-user/mmap.c | 146 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 110 insertions(+), 36 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 82f4026283..29421cfab0 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -1236,69 +1236,143 @@ 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; + void *host_raddr, *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); + } else if (!(shmflg & SHM_REMAP)) { + if (!page_check_range_empty(shmaddr, shmaddr + m_len - 1)) { + return -TARGET_EINVAL; + } + } else if (t_len < h_len) { + /* + * ??? If the host page size is larger than host page size, + * then we might be mapping more pages than the guest expects. + * TODO: Could be fixed with softmmu. + */ + if (!page_check_range_empty(shmaddr + t_len, shmaddr + h_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 now manually completed. */ + host_raddr =3D (void *)g2h_untagged(shmaddr); + test =3D shmat(shmid, host_raddr, shmflg | SHM_REMAP); =20 - page_set_flags(raddr, last, + if (test !=3D MAP_FAILED && t_len > h_len) { + /* + * The target page size is larger than the host page size. + * Fill in the final target page with anonymous memory. + */ + void *t2 =3D mmap(host_raddr + h_len, t_len - h_len, + PROT_READ | (shmflg & SHM_RDONLY ? 0 : PROT_WRI= TE), + MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0); + if (t2 =3D=3D MAP_FAILED) { + test =3D MAP_FAILED; + } + } + + if (test =3D=3D MAP_FAILED) { + ret =3D get_errno(-1); + if (!(shmflg & SHM_REMAP)) { + do_munmap(host_raddr, m_len); + } + return ret; + } + + assert(test =3D=3D host_raddr); + 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 +1386,7 @@ abi_ulong target_shmat(CPUArchState *cpu_env, int shm= id, tb_flush(cpu); } =20 - return raddr; + return shmaddr; } =20 abi_long target_shmdt(abi_ulong shmaddr) --=20 2.34.1