From nobody Thu Nov 28 09:45:18 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1694234079; cv=none; d=zohomail.com; s=zohoarc; b=JPgB2TNJtZ4HjhVhpW8eNUpC7kYHJvHPOEczTjuS2MJBQQCe2a6G112PG+y8z24CkIrhqdh57AZ7OYX30mbo5a8YXSd5wp0z5CVBHdCwAzKu5LllQo3x6l+scUD1YstuXMIkUFr+a5Q+WFcKmKHnemSwhtbcaNFbzC6l0bvx+ug= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1694234079; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=wVHZ2gTKJUTTUFCrTx1jb9XTLg6fK+kTZHzI2+VVFKw=; b=IPl8R+TeLgqdL6BxEUVLOk5xaD/TJsAXimrPvMBDbBJTf7mqne+tMP9w9VMaq4QNCLqy4pOZWoqXwgwqlOH0ykBwcF3BhSOuT9E9/27bgBbHQ5JiBZ7AGGER7mmePzTYSLj3di0Tae0AXP2jyCVGcW6u5rT3Vn2W6ZKq50/8+cQ= 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 1694234079767147.72732079477953; Fri, 8 Sep 2023 21:34:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qepfe-00017R-3b; Sat, 09 Sep 2023 00:34:11 -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 1qepfR-00012Z-Kz for qemu-devel@nongnu.org; Sat, 09 Sep 2023 00:33:57 -0400 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qepfP-0002xl-7n for qemu-devel@nongnu.org; Sat, 09 Sep 2023 00:33:57 -0400 Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-31c5c06e8bbso2653375f8f.1 for ; Fri, 08 Sep 2023 21:33:54 -0700 (PDT) Received: from karim.my.domain ([197.39.120.250]) by smtp.gmail.com with ESMTPSA id d16-20020adff2d0000000b0031981c500aasm3615102wrp.25.2023.09.08.21.33.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Sep 2023 21:33:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694234033; x=1694838833; 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=wVHZ2gTKJUTTUFCrTx1jb9XTLg6fK+kTZHzI2+VVFKw=; b=iCqwmeO0EnxnJjr2YY3P6eXuTl9zk2spHceR/WJln6TdviAXPvnHG08qOO8pVnKJH/ PldNsJWuYwNf9C/JVPnRyqv9asTOtSK6OUFbyoUz/golMYIg0DM0GA+P+uRg1hZKUhY9 nJEZt+GpXgkHx/haGaraoQLvZpU8I8LYKLWGpWjy4U40s7pb7seqV7QmdvYg5QizwJFr uCtzZL2TPQ3+rIiZ6VgqDI503TNROcjEWwN6ujffhHOf/WXtR+DRTH+8Ng8XuzyY2KAK e6MybsR3JMx0rN7V4AXYgM/GfoHjLT9RKJm5mlNfdEbWSg8nCxp+qn3FTrjxYUk4Dm2+ Aw/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694234033; x=1694838833; 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=wVHZ2gTKJUTTUFCrTx1jb9XTLg6fK+kTZHzI2+VVFKw=; b=Qag73A6NMZD7MLX7656fk3N1g/HB7pJFBM4DGWGdeMer32gJWR39a7Fv5TB2Et2Nqr Max7k3BhderQMEh+z6mcrd/E5VfY4dMvIy3Gh6qOY4RfYIkCj0Ix+HeQATtgJYlFWM1b +Rc09W/cXm9NJJQO7c6wmvkcVw94ijD5SsgBF+icW2QQrAqTFxvjLbdJiKZhPZg3SUFv NOmSjfepeFkWWWWafXLqfI6zBWRDruNgUjKGyA5c9b4ygKd7cQQI7UxBV2Fpz42CRca4 JIlZM+R4rDE52YzTAz+ANfksNR/MfQx3QulHnVJBY43A7nuN2PHVWRf+uh5Po7ebP8K8 wRpw== X-Gm-Message-State: AOJu0YybIvvQVTIjHbcffJrBDjF8xDqqLspqZGqTDdudBUEQARqaI/I1 uxcqRHynasIxTF0tgOnwhBXVBrcd8Q4= X-Google-Smtp-Source: AGHT+IGnNS0XDqLqyth/1uRrGH+ZcKBRDO4ToiPvlmTFgUKUfnDkLs3at7+S4H03/2d/T90c0vF/xQ== X-Received: by 2002:a5d:5709:0:b0:318:f7a:e3cb with SMTP id a9-20020a5d5709000000b003180f7ae3cbmr3389656wrv.31.1694234033506; Fri, 08 Sep 2023 21:33:53 -0700 (PDT) From: Karim Taha To: qemu-devel@nongnu.org Cc: imp@bsdimp.com, Karim Taha Subject: [PATCH v2 16/23] bsd-user: Implment madvise(2) to match the linux-user implementation. Date: Thu, 7 Sep 2023 09:42:55 +0200 Message-ID: <20230907074302.79234-17-kariem.taha2.7@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230907074302.79234-1-kariem.taha2.7@gmail.com> References: <20230907074302.79234-1-kariem.taha2.7@gmail.com> 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=2a00:1450:4864:20::42c; envelope-from=kariem.taha2.7@gmail.com; helo=mail-wr1-x42c.google.com X-Spam_score_int: -4 X-Spam_score: -0.5 X-Spam_bar: / X-Spam_report: (-0.5 / 5.0 requ) BAYES_00=-1.9, DATE_IN_PAST_24_48=1.34, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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 @gmail.com) X-ZM-MESSAGEID: 1694234081938100018 Content-Type: text/plain; charset="utf-8" Signed-off-by: Signed-off-by: Karim Taha --- bsd-user/bsd-mem.h | 53 +++++++++++++++++++++++++++++++++++ bsd-user/freebsd/os-syscall.c | 4 +++ bsd-user/syscall_defs.h | 2 ++ 3 files changed, 59 insertions(+) diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h index 16c22593bf..0e16051418 100644 --- a/bsd-user/bsd-mem.h +++ b/bsd-user/bsd-mem.h @@ -129,6 +129,59 @@ static inline abi_long do_bsd_munlockall(void) return get_errno(munlockall()); } =20 +/* madvise(2) */ +static inline abi_long do_bsd_madvise(abi_long arg1, abi_long arg2, + abi_long arg3) +{ + abi_ulong len; + int ret =3D 0; + abi_long start =3D arg1; + abi_long len_in =3D arg2; + abi_long advice =3D arg3; + + if (start & ~TARGET_PAGE_MASK) { + return -TARGET_EINVAL; + } + if (len_in =3D=3D 0) { + return 0; + } + len =3D TARGET_PAGE_ALIGN(len_in); + if (len =3D=3D 0 || !guest_range_valid_untagged(start, len)) { + return -TARGET_EINVAL; + } + + /* + * Most advice values are hints, so ignoring and returning success is = ok. + * + * However, some advice values such as MADV_DONTNEED, are not hints and + * need to be emulated. + * + * A straight passthrough for those may not be safe because qemu somet= imes + * turns private file-backed mappings into anonymous mappings. + * If all guest pages have PAGE_PASSTHROUGH set, mappings have the + * same semantics for the host as for the guest. + * + * MADV_DONTNEED is passed through, if possible. + * If passthrough isn't possible, we nevertheless (wrongly!) return + * success, which is broken but some userspace programs fail to work + * otherwise. Completely implementing such emulation is quite complica= ted + * though. + */ + mmap_lock(); + switch (advice) { + case MADV_DONTNEED: + if (page_check_range(start, len, PAGE_PASSTHROUGH)) { + ret =3D get_errno(madvise(g2h_untagged(start), len, advice)); + if ((advice =3D=3D MADV_DONTNEED) && (ret =3D=3D 0)) { + page_reset_target_data(start, start + len - 1); + } + } + } + mmap_unlock(); + + return ret; +} + /* minherit(2) */ static inline abi_long do_bsd_minherit(abi_long addr, abi_long len, abi_long inherit) diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index 6eaa705cd3..f5d60cf902 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -519,6 +519,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num= , abi_long arg1, ret =3D do_bsd_munlockall(); break; =20 + case TARGET_FREEBSD_NR_madvise: /* madvise(2) */ + ret =3D do_bsd_madvise(arg1, arg2, arg3); + break; + case TARGET_FREEBSD_NR_minherit: /* minherit(2) */ ret =3D do_bsd_minherit(arg1, arg2, arg3); break; diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h index 074df7bdd6..76f4856009 100644 --- a/bsd-user/syscall_defs.h +++ b/bsd-user/syscall_defs.h @@ -95,6 +95,8 @@ struct bsd_shm_regions { /* * sys/mman.h */ +#define TARGET_MADV_DONTNEED 4 /* dont need these pages */ + #define TARGET_FREEBSD_MAP_RESERVED0080 0x0080 /* previously misimplement= ed */ /* MAP_INHERIT */ #define TARGET_FREEBSD_MAP_RESERVED0100 0x0100 /* previously unimplemente= d */ --=20 2.42.0