From nobody Sat Feb 7 06:55:15 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505825146831348.4073113264204; Tue, 19 Sep 2017 05:45:46 -0700 (PDT) Received: from localhost ([::1]:42636 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1duHuM-0008WL-0u for importer@patchew.org; Tue, 19 Sep 2017 08:45:46 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60038) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1duHfQ-0004E1-R4 for qemu-devel@nongnu.org; Tue, 19 Sep 2017 08:30:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1duHfL-0005U4-1w for qemu-devel@nongnu.org; Tue, 19 Sep 2017 08:30:20 -0400 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:38518) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1duHfK-0005TY-Rz for qemu-devel@nongnu.org; Tue, 19 Sep 2017 08:30:14 -0400 Received: by mail-wm0-x242.google.com with SMTP id x17so3722376wmd.5 for ; Tue, 19 Sep 2017 05:30:14 -0700 (PDT) Received: from 640k.lan (dynamic-adsl-78-12-246-117.clienti.tiscali.it. [78.12.246.117]) by smtp.gmail.com with ESMTPSA id k18sm1292878wmd.22.2017.09.19.05.30.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Sep 2017 05:30:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=xGhaycVMI3riVhRe8I1mihu2WOoS8ixcNKxFbRPQod0=; b=MWPp7s3F9ZEK0LG/HSP+RT+KOpOFigQo766uMdo5H/TBTu9GRc3QWvt0xHd3LeEd+1 9Y1XLS4tyqaxmJsLdDT7sgSbZhLol38n+L//b/W18X+rgfqJjYja0t69SKoRvXVCcPx0 d2jvvXf/QmiNCaVNgxEMsC8tE/IDJQNp3hNwUV1C2f/7QDun8/vElirdzCEGlvvXMgOy Q9aVrEQeP8pYTh+OQzR2HAJXMQvQPLXThrivsZsAtlMJb5mNzfH3VcYWeDMMtqcY1SLS ImGZCQdVoCcm1BECZG+P3pHruNnpKN1y5RXnLJhxlNsGWefpLCK+fhMLz3WhGvm1smC+ P1AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=xGhaycVMI3riVhRe8I1mihu2WOoS8ixcNKxFbRPQod0=; b=gznJzCMNfYWDbgRH706YdLGqknqsSUChYEEhZETJ/AbLIES+lzxobc7Xnxvr7AyWfx DeCfLApWSILzVHHo+TUfIhPpidY+JY8k6rAiTC6JQNzkJX2mCZDOCtK/lOP6VGkCNyZ3 4PDgpIyV24z8XHF2tYd/Bte2zp1RzXAbNmN1WTfBbpPdxruaEXd+z5wiRFb8BFTaRO1X QRNl8afNdYipNYPUe0oh2sCp/KiYccNp+eG/dgbhTVa2NW8GGZXC4nripkRFkJwuGKJg yamUTfJgCiL6Uzb+LxGYZ5EwVolrhLiEXiP5Rdl28T4NhtOIVzzsKv6VQ8zuxySv3Sw/ CGWg== X-Gm-Message-State: AHPjjUjosxNgiQTxsgR7RV25GsuQdOYx9zp4BRft8YhzOvtmI0ffRJBO F1AINwT1BPR+MythSbRl4IEQeuNN X-Google-Smtp-Source: AOwi7QDTSFUaOOXmUl0yURDU0aBJfOhQgQuaMSVWXcZ8n7CYK9QYecDklAWW47cMTnR5SoJBS92Iog== X-Received: by 10.28.216.75 with SMTP id p72mr1017000wmg.128.1505824213564; Tue, 19 Sep 2017 05:30:13 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 19 Sep 2017 14:29:16 +0200 Message-Id: <1505824179-21541-28-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1505824179-21541-1-git-send-email-pbonzini@redhat.com> References: <1505824179-21541-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::242 Subject: [Qemu-devel] [PULL 27/50] kvm: factor out alignment of memory section X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: David Hildenbrand Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: David Hildenbrand Factor it out, so we can reuse it later. Signed-off-by: David Hildenbrand Message-Id: <20170911174933.20789-3-david@redhat.com> Signed-off-by: Paolo Bonzini --- accel/kvm/kvm-all.c | 59 +++++++++++++++++++++++++++++++++++--------------= ---- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 985b179..e0d100b 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -190,6 +190,36 @@ static KVMSlot *kvm_lookup_matching_slot(KVMMemoryList= ener *kml, } =20 /* + * Calculate and align the start address and the size of the section. + * Return the size. If the size is 0, the aligned section is empty. + */ +static hwaddr kvm_align_section(MemoryRegionSection *section, + hwaddr *start) +{ + hwaddr size =3D int128_get64(section->size); + hwaddr delta; + + *start =3D section->offset_within_address_space; + + /* kvm works in page size chunks, but the function may be called + with sub-page size and unaligned start address. Pad the start + address to next and truncate size to previous page boundary. */ + delta =3D qemu_real_host_page_size - (*start & ~qemu_real_host_page_ma= sk); + delta &=3D ~qemu_real_host_page_mask; + *start +=3D delta; + if (delta > size) { + return 0; + } + size -=3D delta; + size &=3D qemu_real_host_page_mask; + if (*start & ~qemu_real_host_page_mask) { + return 0; + } + + return size; +} + +/* * Find overlapping slot with lowest start address */ static KVMSlot *kvm_lookup_overlapping_slot(KVMMemoryListener *kml, @@ -700,25 +730,8 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, int err; MemoryRegion *mr =3D section->mr; bool writeable =3D !mr->readonly && !mr->rom_device; - hwaddr start_addr =3D section->offset_within_address_space; - ram_addr_t size =3D int128_get64(section->size); - void *ram =3D NULL; - unsigned delta; - - /* kvm works in page size chunks, but the function may be called - with sub-page size and unaligned start address. Pad the start - address to next and truncate size to previous page boundary. */ - delta =3D qemu_real_host_page_size - (start_addr & ~qemu_real_host_pag= e_mask); - delta &=3D ~qemu_real_host_page_mask; - if (delta > size) { - return; - } - start_addr +=3D delta; - size -=3D delta; - size &=3D qemu_real_host_page_mask; - if (!size || (start_addr & ~qemu_real_host_page_mask)) { - return; - } + hwaddr start_addr, size; + void *ram; =20 if (!memory_region_is_ram(mr)) { if (writeable || !kvm_readonly_mem_allowed) { @@ -730,7 +743,13 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, } } =20 - ram =3D memory_region_get_ram_ptr(mr) + section->offset_within_region = + delta; + size =3D kvm_align_section(section, &start_addr); + if (!size) { + return; + } + + ram =3D memory_region_get_ram_ptr(mr) + section->offset_within_region + + (section->offset_within_address_space - start_addr); =20 while (1) { mem =3D kvm_lookup_overlapping_slot(kml, start_addr, start_addr + = size); --=20 1.8.3.1