From nobody Sun Nov 24 14:38:14 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 ARC-Seal: i=1; a=rsa-sha256; t=1722643165; cv=none; d=zohomail.com; s=zohoarc; b=WRPL0scb5V0ZykR2z3DSITHIe5zKX+blXoeNLNo5BPQoIRErVl856Jhaw1aYCYkeLv7+Lwv0oZ76Y+Cg8uTN6k3608DikFQicpI/byOF47XkRrFB5aW5ZJEe7TIOoPo5tzhxCIkfXJouF8lxoSL5dMR0x/KgGONhRcbu/h7WQVM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722643165; 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=38kVVXcpRVzigthSDk6irqa7cPAYSjq2c74UEfJaIlc=; b=E31cDV00JwB+6IDQBXscT1c/mUGY2cWZuanEGym2fh11pi8NiGQ9aB2jOz2vLMiUU/boL2/EcZXxKHpjUIpViokKCRihZ7Odj6xqnVO6gMp73mpyvi9CPi1MzHDqgR9+QlQmMlIUuBDK4zpRL2jdfo3jmSB+5l5JGBknK/N5Qu8= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722643165657861.570959355446; Fri, 2 Aug 2024 16:59:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sa29p-0002NC-Uh; Fri, 02 Aug 2024 19:58:01 -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 1sa29o-0002Hh-I5 for qemu-devel@nongnu.org; Fri, 02 Aug 2024 19:58:00 -0400 Received: from mail-io1-xd2f.google.com ([2607:f8b0:4864:20::d2f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sa29m-0004L2-IV for qemu-devel@nongnu.org; Fri, 02 Aug 2024 19:58:00 -0400 Received: by mail-io1-xd2f.google.com with SMTP id ca18e2360f4ac-81f86fd93acso128172139f.1 for ; Fri, 02 Aug 2024 16:57:58 -0700 (PDT) Received: from dune.bsdimp.com ([50.253.99.174]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4c8d69a8227sm683131173.47.2024.08.02.16.57.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Aug 2024 16:57:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20230601.gappssmtp.com; s=20230601; t=1722643077; x=1723247877; 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=38kVVXcpRVzigthSDk6irqa7cPAYSjq2c74UEfJaIlc=; b=NMvE/plYa7VzJNbc05CY0RaLBgBEz/JUxsswjNBfjsjKZBpuZj1su7ZYmf6M8uqr3V kccUIp7VtWkgFF71RoW/k6Suf0SnKxqZJXqt2QIEn6/FXdzLS+WGqEw40ltvt67VBlWU EodepFdJQ7kTezBLERpRKrauwoNCtYsS49rYSZKVQnGViUHQxc063EET17NQv9wWS1Q6 eyrYLaD1ihRA/I1utSoERbj9dcrep3FgQmMcnXvqsFULNQTFVYnpfGH1zOhLmaIajIO4 J8PHiHD+4/Nv3JhHL2nMVaedyQsc7FRb3F7GR9xl75UXtDDPP4UI6W/Q5/O9w9fjhVQN rE9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722643077; x=1723247877; 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=38kVVXcpRVzigthSDk6irqa7cPAYSjq2c74UEfJaIlc=; b=OX0rFyBNWE7yBrw1JLnwzwPbyGoJJURtmoyPKGqTpaVMK12HI5AkamX0YxIFN2SLjo 3FwAB1UyeMKrVZ7DfO5NjGG18YLPryZ6VPiqsQiMu+52pSKVIurq9bcQvYObdDYxqfu/ lnhD8pWyB5qaN3vT00obXSZdAkfj71TZm87nYniZSSS76CV444IA4ZHeNoF2GeAeR5AD u73Xr05GkX05Asn2ARWhX2aLFGt2JVIPML/ZxJJe579PUu4GNFqTRWwGLdJwQPehWSrH jWkK2Fygat2jgcErwzZQwQq+JPscz18QmN0L1Pm4d8mj32KmC4BwN8hmZCCUTaOmHfT1 d/DQ== X-Gm-Message-State: AOJu0YwyPiSbplOKALAqORjAgC6V9iIRi5E5FfMSUUlpSL61x2Xvf475 u18xFehUT+MsbDPxtbIz7bu/fz/EyvZUoLFT+t/Cv4c+dC527V6HH9A1nhnlub66GbjIy8kxmL7 YmV0= X-Google-Smtp-Source: AGHT+IFvfUBT+yr8mGnwlmG+vqVkKBTVuNRMPnfbSTRxMJG4HDzlJLXnTTGXpe52XCOXffW01XD/wg== X-Received: by 2002:a5d:8e19:0:b0:806:3dac:5081 with SMTP id ca18e2360f4ac-81fd11321aemr415677139f.7.1722643077036; Fri, 02 Aug 2024 16:57:57 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: Warner Losh , Kyle Evans , Jessica Clarke Subject: [PATCH 17/17] bsd-user: copy linux-user target_mprotect impl Date: Fri, 2 Aug 2024 17:56:17 -0600 Message-ID: <20240802235617.7971-18-imp@bsdimp.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240802235617.7971-1-imp@bsdimp.com> References: <20240802235617.7971-1-imp@bsdimp.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: none client-ip=2607:f8b0:4864:20::d2f; envelope-from=imp@bsdimp.com; helo=mail-io1-xd2f.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, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bsdimp-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1722643167102116600 Content-Type: text/plain; charset="utf-8" Now that we're closer to the linux-user target_mprotect code, go ahead and grab the rest of the implementation. This moves from a stard, end impl to a start, last which will allow last page mapping, etc. This also moves to a more general algorithm. We're close enough that this jump isn't so large, and doing it incrementally further has become too much work for too little gain. Signed-off-by: Warner Losh Acked-by: Richard Henderson --- bsd-user/mmap.c | 138 +++++++++++++++++++++++++++++++----------------- 1 file changed, 90 insertions(+), 48 deletions(-) diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c index 3c48a188e88..a4de7674bec 100644 --- a/bsd-user/mmap.c +++ b/bsd-user/mmap.c @@ -60,6 +60,17 @@ void mmap_fork_end(int child) pthread_mutex_unlock(&mmap_mutex); } =20 +/* + * Map target protection mask to host. Identity on FreeBSD. + */ +static abi_ulong target_to_host_prot(abi_ulong prot) +{ + return (prot); +} + +/* Helpful temporary #define to reduce diffs with linux-user mmap.c */ +#define trace_target_mprotect(start, len, target_prot) + /* * Validate target prot bitmask. * Return the prot bitmask for the host in *HOST_PROT. @@ -78,72 +89,103 @@ static int validate_prot_to_pageflags(int prot) int target_mprotect(abi_ulong start, abi_ulong len, int target_prot) { int host_page_size =3D qemu_real_host_page_size(); - abi_ulong end, host_start, host_end, addr; - int prot1, ret, page_flags; - - qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=3D0x" TARGET_ABI_FMT_lx - " len=3D0x" TARGET_ABI_FMT_lx " prot=3D%c%c%c\n", start,= len, - target_prot & PROT_READ ? 'r' : '-', - target_prot & PROT_WRITE ? 'w' : '-', - target_prot & PROT_EXEC ? 'x' : '-'); - if ((start & ~TARGET_PAGE_MASK) !=3D 0) - return -EINVAL; + abi_ulong starts[3]; + abi_ulong lens[3]; + int prots[3]; + abi_ulong host_start, host_last, last; + int prot1, ret, page_flags, nranges; + + trace_target_mprotect(start, len, target_prot); + + if ((start & ~TARGET_PAGE_MASK) !=3D 0) { + return -TARGET_EINVAL; + } page_flags =3D validate_prot_to_pageflags(target_prot); if (!page_flags) { return -TARGET_EINVAL; } - len =3D TARGET_PAGE_ALIGN(len); - if (len =3D=3D 0) + if (len =3D=3D 0) { return 0; + } + len =3D TARGET_PAGE_ALIGN(len); if (!guest_range_valid_untagged(start, len)) { - return -ENOMEM; + return -TARGET_ENOMEM; } - target_prot &=3D PROT_READ | PROT_WRITE | PROT_EXEC; - end =3D start + len; =20 - mmap_lock(); + last =3D start + len - 1; host_start =3D start & -host_page_size; - host_end =3D HOST_PAGE_ALIGN(end); - if (start > host_start) { - /* handle host page containing start */ + host_last =3D ROUND_UP(last, host_page_size) - 1; + nranges =3D 0; + + mmap_lock(); + + if (host_last - host_start < host_page_size) { + /* Single host page contains all guest pages: sum the prot. */ prot1 =3D target_prot; - for (addr =3D host_start; addr < start; addr +=3D TARGET_PAGE_SIZE= ) { - prot1 |=3D page_get_flags(addr); + for (abi_ulong a =3D host_start; a < start; a +=3D TARGET_PAGE_SIZ= E) { + prot1 |=3D page_get_flags(a); + } + for (abi_ulong a =3D last; a < host_last; a +=3D TARGET_PAGE_SIZE)= { + prot1 |=3D page_get_flags(a + 1); } - if (host_end =3D=3D host_start + host_page_size) { - for (addr =3D end; addr < host_end; addr +=3D TARGET_PAGE_SIZE= ) { - prot1 |=3D page_get_flags(addr); + starts[nranges] =3D host_start; + lens[nranges] =3D host_page_size; + prots[nranges] =3D prot1; + nranges++; + } else { + if (host_start < start) { + /* Host page contains more than one guest page: sum the prot. = */ + prot1 =3D target_prot; + for (abi_ulong a =3D host_start; a < start; a +=3D TARGET_PAGE= _SIZE) { + prot1 |=3D page_get_flags(a); + } + /* If the resulting sum differs, create a new range. */ + if (prot1 !=3D target_prot) { + starts[nranges] =3D host_start; + lens[nranges] =3D host_page_size; + prots[nranges] =3D prot1; + nranges++; + host_start +=3D host_page_size; } - end =3D host_end; } - ret =3D mprotect(g2h_untagged(host_start), - host_page_size, prot1 & PAGE_RWX); - if (ret !=3D 0) - goto error; - host_start +=3D host_page_size; - } - if (end < host_end) { - prot1 =3D target_prot; - for (addr =3D end; addr < host_end; addr +=3D TARGET_PAGE_SIZE) { - prot1 |=3D page_get_flags(addr); + + if (last < host_last) { + /* Host page contains more than one guest page: sum the prot. = */ + prot1 =3D target_prot; + for (abi_ulong a =3D last; a < host_last; a +=3D TARGET_PAGE_S= IZE) { + prot1 |=3D page_get_flags(a + 1); + } + /* If the resulting sum differs, create a new range. */ + if (prot1 !=3D target_prot) { + host_last -=3D host_page_size; + starts[nranges] =3D host_last + 1; + lens[nranges] =3D host_page_size; + prots[nranges] =3D prot1; + nranges++; + } + } + + /* Create a range for the middle, if any remains. */ + if (host_start < host_last) { + starts[nranges] =3D host_start; + lens[nranges] =3D host_last - host_start + 1; + prots[nranges] =3D target_prot; + nranges++; } - ret =3D mprotect(g2h_untagged(host_end - host_page_size), - host_page_size, prot1 & PAGE_RWX); - if (ret !=3D 0) - goto error; - host_end -=3D host_page_size; } =20 - /* handle the pages in the middle */ - if (host_start < host_end) { - ret =3D mprotect(g2h_untagged(host_start), host_end - host_start, = target_prot); - if (ret !=3D 0) + for (int i =3D 0; i < nranges; ++i) { + ret =3D mprotect(g2h_untagged(starts[i]), lens[i], + target_to_host_prot(prots[i])); + if (ret !=3D 0) { goto error; + } } - page_set_flags(start, start + len - 1, page_flags); - mmap_unlock(); - return 0; -error: + + page_set_flags(start, last, page_flags); + ret =3D 0; + + error: mmap_unlock(); return ret; } --=20 2.45.1