From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4B8EFEE4996 for ; Sun, 20 Aug 2023 04:45:31 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qXaJS-00012t-Bo; Sun, 20 Aug 2023 00:45:18 -0400 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 1qXaJM-00011g-Ef for qemu-devel@nongnu.org; Sun, 20 Aug 2023 00:45:12 -0400 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qXaJG-0002qn-Nl for qemu-devel@nongnu.org; Sun, 20 Aug 2023 00:45:10 -0400 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-99c1c66876aso285493566b.2 for ; Sat, 19 Aug 2023 21:45:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20221208.gappssmtp.com; s=20221208; t=1692506705; x=1693111505; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=LVZ1v2U9CWTWSY+BqDVMo+oZl8OhrckMW31gfwziNT4=; b=vGNxlgOiLER9JjEd3l0XPMDFIMtGbNxrR7pLGtWqbW7HxNQsMgPknf4QhCNglyGfQE p7tz4DAYTsLAGsS56MUI4QmgCSDbI8VWZzi1JM46A0HkJlzBaJFPd0WABfY3uRmTp2me bw0QMuJ+KIg8cPEY/4/C6qGdUbPy9gCJ0HjuTpKOa+IKn4w/tCXkax21EllONuMw3Lrm /oClSk+8x86SunvVWGN8ykiG1b2kFGjWgH3YtCQ+Hu035bKZn54bV0JRGt6hT9XqkwUZ q+5ZLjfgtVuWOPoenHSCj/rhtkg69bh5sv2SKPkfs3L+qV+iioCb/6ZpG9+nR5KcbCBP 7O+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692506705; x=1693111505; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LVZ1v2U9CWTWSY+BqDVMo+oZl8OhrckMW31gfwziNT4=; b=CKSYbqKw4TLxex+W6BFdGHcIReCDj/qrICOpXNrZ01rvalGFKZ+niUWGJkFysDtwlc jq6/SES3Dy0lozoxYinOBgiBP8DNHDc9lZLlhuLldtDlly1QUOZlYZC88U0NP86K3O0M gBj6GhUGrMPvtJNo+wcv77pYHXIXzUruypekGNzxGeUXOdBzLJBWJB/xiCuWipakDa8f NGFYmmTudfVjHx0zT/nmTDpUz2GJ98priHErI6bgGBX4kGr0mGDDTEhn9S+Xf8BFgJx5 t+RoSl9jiGWTCl3Ou/vfqY8W+3XgQ35rR4cz2M5th3hynAgYAqYKCm+q39IjyMiEhmRg QW3w== X-Gm-Message-State: AOJu0YwHpckiudd5j/wwPNTbuQdAmr0VHPIAns6EKFCRCot1Pn9WSs4p m32DltMbc6HdQlTtg7tRhQQyCQ4SsT1exkDUXPl6kA== X-Google-Smtp-Source: AGHT+IE7ocb52l/CymoH90UPnqp8VPMaKWFQDdusVw64qCahHEXVihOkW9papwjqOz7j4qkTPYYOqawK3Zen4b1sXrg= X-Received: by 2002:a17:907:7818:b0:99b:d178:f051 with SMTP id la24-20020a170907781800b0099bd178f051mr2552508ejc.64.1692506705083; Sat, 19 Aug 2023 21:45:05 -0700 (PDT) MIME-Version: 1.0 References: <20230819094806.14965-1-kariem.taha2.7@gmail.com> <20230819094806.14965-22-kariem.taha2.7@gmail.com> In-Reply-To: <20230819094806.14965-22-kariem.taha2.7@gmail.com> From: Warner Losh Date: Sat, 19 Aug 2023 22:44:57 -0600 Message-ID: Subject: Re: [PATCH 21/22] Implement shmat(2) and shmdt(2) To: Karim Taha , Kyle Evans Cc: qemu-devel@nongnu.org, Stacey Son Content-Type: multipart/alternative; boundary="000000000000efcbea0603536cee" Received-SPF: none client-ip=2a00:1450:4864:20::630; envelope-from=wlosh@bsdimp.com; helo=mail-ej1-x630.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org --000000000000efcbea0603536cee Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Aug 19, 2023 at 3:49=E2=80=AFAM Karim Taha wrote: > From: Stacey Son > > Signed-off-by: Stacey Son > Signed-off-by: Karim Taha > --- > bsd-user/bsd-mem.h | 72 +++++++++++++++++++++++++++++++++++ > bsd-user/freebsd/os-syscall.c | 8 ++++ > 2 files changed, 80 insertions(+) > > diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h > index 221ad76d8c..f737b94885 100644 > --- a/bsd-user/bsd-mem.h > +++ b/bsd-user/bsd-mem.h > @@ -335,4 +335,76 @@ static inline abi_long do_bsd_shmctl(abi_long shmid, > abi_long cmd, > return ret; > } > > +/* shmat(2) */ > +static inline abi_long do_bsd_shmat(int shmid, abi_ulong shmaddr, int > shmflg) > +{ > + abi_ulong raddr; > + abi_long ret; > + void *host_raddr; > + struct shmid_ds shm_info; > + int i; > + > + /* 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 the length */ > + return ret; > + } > + > + mmap_lock(); > + > + if (shmaddr) { > + host_raddr =3D shmat(shmid, (void *)g2h_untagged(shmaddr), shmfl= g); > + } else { > + abi_ulong mmap_start; > + > + mmap_start =3D mmap_find_vma(0, shm_info.shm_segsz); > + > + if (mmap_start =3D=3D -1) { > + errno =3D ENOMEM; > + host_raddr =3D (void *)-1; > + } else { > + host_raddr =3D shmat(shmid, g2h_untagged(mmap_start), > + shmflg); /* | SHM_REMAP XXX WHY? */ > I was all set to hit reviewed by on this, but this has me curious. Kyle (or anybody else) do you know the back story here. git blame is less than helpful. Warner > + } > + } > + > + if (host_raddr =3D=3D (void *)-1) { > + mmap_unlock(); > + return get_errno((long)host_raddr); > + } > + raddr =3D h2g((unsigned long)host_raddr); > + > + page_set_flags(raddr, raddr + shm_info.shm_segsz, > + PAGE_VALID | PAGE_READ | ((shmflg & SHM_RDONLY) ? 0 : > PAGE_WRITE)); > + > + for (i =3D 0; i < N_BSD_SHM_REGIONS; i++) { > + if (bsd_shm_regions[i].start =3D=3D 0) { > + bsd_shm_regions[i].start =3D raddr; > + bsd_shm_regions[i].size =3D shm_info.shm_segsz; > + break; > + } > + } > + > + mmap_unlock(); > + return raddr; > +} > + > +/* shmdt(2) */ > +static inline abi_long do_bsd_shmdt(abi_ulong shmaddr) > +{ > + int i; > + > + for (i =3D 0; i < N_BSD_SHM_REGIONS; ++i) { > + if (bsd_shm_regions[i].start =3D=3D shmaddr) { > + bsd_shm_regions[i].start =3D 0; > + page_set_flags(shmaddr, > + shmaddr + bsd_shm_regions[i].size, 0); > + break; > + } > + } > + > + return get_errno(shmdt(g2h_untagged(shmaddr))); > +} > + > #endif /* BSD_USER_BSD_MEM_H */ > diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.= c > index 9681c65ce9..f76bc1eb38 100644 > --- a/bsd-user/freebsd/os-syscall.c > +++ b/bsd-user/freebsd/os-syscall.c > @@ -559,6 +559,14 @@ static abi_long freebsd_syscall(void *cpu_env, int > num, abi_long arg1, > ret =3D do_bsd_shmctl(arg1, arg2, arg3); > break; > > + case TARGET_FREEBSD_NR_shmat: /* shmat(2) */ > + ret =3D do_bsd_shmat(arg1, arg2, arg3); > + break; > + > + case TARGET_FREEBSD_NR_shmdt: /* shmdt(2) */ > + ret =3D do_bsd_shmdt(arg1); > + break; > + > /* > * Misc > */ > -- > 2.40.0 > > --000000000000efcbea0603536cee Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Sat, Aug 19, 2023 at 3:49=E2=80=AF= AM Karim Taha <kariem.taha2.= 7@gmail.com> wrote:
From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
=C2=A0bsd-user/bsd-mem.h=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | 72 ++++= +++++++++++++++++++++++++++++++
=C2=A0bsd-user/freebsd/os-syscall.c |=C2=A0 8 ++++
=C2=A02 files changed, 80 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 221ad76d8c..f737b94885 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -335,4 +335,76 @@ static inline abi_long do_bsd_shmctl(abi_long shmid, a= bi_long cmd,
=C2=A0 =C2=A0 =C2=A0return ret;
=C2=A0}

+/* shmat(2) */
+static inline abi_long do_bsd_shmat(int shmid, abi_ulong shmaddr, int shmf= lg)
+{
+=C2=A0 =C2=A0 abi_ulong raddr;
+=C2=A0 =C2=A0 abi_long ret;
+=C2=A0 =C2=A0 void *host_raddr;
+=C2=A0 =C2=A0 struct shmid_ds shm_info;
+=C2=A0 =C2=A0 int i;
+
+=C2=A0 =C2=A0 /* Find out the length of the shared memory segment. */
+=C2=A0 =C2=A0 ret =3D get_errno(shmctl(shmid, IPC_STAT, &shm_info)); +=C2=A0 =C2=A0 if (is_error(ret)) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 /* Can't get the length */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 return ret;
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 mmap_lock();
+
+=C2=A0 =C2=A0 if (shmaddr) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 host_raddr =3D shmat(shmid, (void *)g2h_untagg= ed(shmaddr), shmflg);
+=C2=A0 =C2=A0 } else {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 abi_ulong mmap_start;
+
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 mmap_start =3D mmap_find_vma(0, shm_info.shm_s= egsz);
+
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (mmap_start =3D=3D -1) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 errno =3D ENOMEM;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 host_raddr =3D (void *)-1;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 } else {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 host_raddr =3D shmat(shmid, g2h_= untagged(mmap_start),
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 shmflg); /* | SHM_= REMAP XXX WHY? */

I was all set to hit = reviewed by on this, but this has me curious. Kyle (or anybody else) do
you know the back story here. git blame is less than helpful.
<= div>
Warner
=C2=A0
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 if (host_raddr =3D=3D (void *)-1) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 mmap_unlock();
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 return get_errno((long)host_raddr);
+=C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 raddr =3D h2g((unsigned long)host_raddr);
+
+=C2=A0 =C2=A0 page_set_flags(raddr, raddr + shm_info.shm_segsz,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 PAGE_VALID | PAGE_READ | ((shmflg & SHM_RD= ONLY) ? 0 : PAGE_WRITE));
+
+=C2=A0 =C2=A0 for (i =3D 0; i < N_BSD_SHM_REGIONS; i++) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (bsd_shm_regions[i].start =3D=3D 0) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 bsd_shm_regions[i].start =3D rad= dr;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 bsd_shm_regions[i].size =3D shm_= info.shm_segsz;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 mmap_unlock();
+=C2=A0 =C2=A0 return raddr;
+}
+
+/* shmdt(2) */
+static inline abi_long do_bsd_shmdt(abi_ulong shmaddr)
+{
+=C2=A0 =C2=A0 int i;
+
+=C2=A0 =C2=A0 for (i =3D 0; i < N_BSD_SHM_REGIONS; ++i) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (bsd_shm_regions[i].start =3D=3D shmaddr) {=
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 bsd_shm_regions[i].start =3D 0;<= br> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 page_set_flags(shmaddr,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 shmaddr + bsd_shm_= regions[i].size, 0);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 return get_errno(shmdt(g2h_untagged(shmaddr)));
+}
+
=C2=A0#endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c<= br> index 9681c65ce9..f76bc1eb38 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -559,6 +559,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num= , abi_long arg1,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ret =3D do_bsd_shmctl(arg1, arg2, arg3);<= br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0break;

+=C2=A0 =C2=A0 case TARGET_FREEBSD_NR_shmat: /* shmat(2) */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 ret =3D do_bsd_shmat(arg1, arg2, arg3);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+
+=C2=A0 =C2=A0 case TARGET_FREEBSD_NR_shmdt: /* shmdt(2) */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 ret =3D do_bsd_shmdt(arg1);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/*
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * Misc
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 */
--
2.40.0

--000000000000efcbea0603536cee--