From nobody Mon Nov 23 04:52:33 2026 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=1791982663; cv=none; d=zohomail.com; s=zohoarc; b=ACs1dV0H/a0QUZL4wYa4qhVlC7ZItIR+ftFn++1SIaBP6DuHzJ6nr5uqOqn5xvrsC+gqCRiVd7L3TX3wAHpiCUFr4wR8PSgdmrVuMIEJJy2v3Fknt07VNVlrw2I6it/u0ZyLSU6seCFHi/AS6+5UtZ8QSeiS3KRgwgSz83WxaPA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791982663; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Lsjn3LV7JBpZW2IxCjuKF63c/Yth+tJQr8znBD2cPwE=; b=f1Ps7/lpUP1bzz23xPYv3g3br/j+v9HbPpsD8AUO3dUN30tQDNIKkbyQqtPDeI1AN0sN/m6qd6SZSGI9Fk6/KkMT+gD1TKesDXrA2FB/IoukEdoD80gQJeTr3M0TJYKE1I4Mb8GUNHz7ul8tEAI7bAmNxGNjEhwiOEwgapoLi84= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1791982663822533.0195557371476; Wed, 14 Oct 2026 05:57:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xGyWf-0007WD-9u; Wed, 14 Oct 2026 08:56:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xGyHA-0004Jn-Fy for qemu-devel@nongnu.org; Wed, 14 Oct 2026 08:40:08 -0400 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xGyH7-0007O2-1r for qemu-devel@nongnu.org; Wed, 14 Oct 2026 08:40:07 -0400 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-48de6a62a4cso1241299f8f.2 for ; Wed, 14 Oct 2026 05:40:04 -0700 (PDT) Received: from sfomberg-mac.redhat.corp ([2a06:c701:7925:8a00:fca2:9f5c:1c03:33ff]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48de0bc6b82sm12029945f8f.51.2026.10.14.05.40.01 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 14 Oct 2026 05:40:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791981603; x=1792586403; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=Lsjn3LV7JBpZW2IxCjuKF63c/Yth+tJQr8znBD2cPwE=; b=iqcbkF7UGZLMA5jUnkRYHXhVPWZNkOWYzKcS0bRWLqw70p2KOkG01gPhfSPz05CtJN /oWynsrp4i69ec8rVm1+a976fplopJmQlmcrm9B0p7TpewHcc1meK7oJ0aOI8jhPO4Cl WbQxdCra0e4rbuo3ogYeehmDnt5Nc8jZr/vWf+muyzqNG4eeZlyFgj9DxPvKvm/K8BOW wp35RQUAjXfL+YFDLBxjbdRi6cZ3mnCPt0k5jnK8tdNUQ1UmK4FOubnse2m4GvEuWxv9 qPz9T2LqhC2z24RyVKN3vV7ofx1qEURsKOqlbmRixwM6k2NSwwFb0rixQJLckJP67juA EfzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791981603; x=1792586403; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Lsjn3LV7JBpZW2IxCjuKF63c/Yth+tJQr8znBD2cPwE=; b=vr7KAPGkJ5ZnvZq0CiTNzdpbgWT+NPAlJk6hWmmlPhftdDWk0SxyMojqe6xcJXOfNy z5eqy0Z6lgioBUESJgG+vh3TU0aId0hFGvzAcLLPVs3kL2yFHl27sxwiNTAuXQIH30D9 scK6jy+Ekiu3DqYZaVdVb2TbxlKyaGCgkivt1SGB5yNAWSZ94HRZ+V05KtcLI7gBYTvb /EWEsuPUONohwGjQdLRGLODaAZJ0zHJSHY6+NEFaDyyqNHsssZNZvJZei25nY47HinUc ljANRQMTMh31GPA4aWzjQVyKvRAxv4uAv0vpUh5AVmGL5/roSrQQyEL8xbufmat6QtCF aD4A== X-Gm-Message-State: AFq9FYLWX75dPnHAXb3LWl4KmdB6qEGfTJuQWIgWhxUko+a7+h+lfYJN GF9Q8NH7VLGzN9xdMYeYLUvKk2NCMRuNqh530GSjNPZqYUTgTABFlosWN81jyXjN X-Gm-Gg: AYBFou2+qgO1MSVTvfFBSVaWjTRNggw5Wlv0VnJ/jAzdiPKwr5a8nGEIp3GyyCUr3b4 d9b5H9q3dSC62IbozPLlJxBroNH5ytPQ/S3emylzkH+by5bkxtek7aY/OnjCYHEkB1A48F833hM oVZL9OpRl/3DyO3dETJ2jvMKm/RJllCIYNaVZ3+h5Xu4qJMhYxHAQZ4J1pqt7oVjSr69LmOR1f3 4vyrJbe+86HjPWVmwErUz+trZUaQvFiczRgJz71Yb/FoLbKI01ZaOyKQOOdlFOH/Rg+ekNS5j0o UQpqO9baGiJBuIWaVeXwhsv87q9viWsaGMW3pJF330n5xGc6GkR0FoscBPAdMyWHF1wOWPEgDLO sq8yzQiA4gA9QOYfxopzyf60tvbaaPlTASUHMxCS0kQZZ72k+rWQANVaICGZS/uG+5BbonQpEbn pVTy5AQABoY27ozkqHdN0yHsYEB4kcowtRKqSu8fkUKrJswS5nj4yOG/7YM7OcZKGRc4fZ58oB1 rAkn0VlkHwnw2VkoMgP66sp8eowvxteYS1daiO5MoVmUg== X-Received: by 2002:a5d:588c:0:b0:48b:5083:96ae with SMTP id ffacd0b85a97d-48ddff64e46mr12217126f8f.15.1791981603132; Wed, 14 Oct 2026 05:40:03 -0700 (PDT) From: Shmuel Fomberg To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, zhao1.liu@intel.com, mtosatti@redhat.com, pierrick.bouvier@oss.qualcomm.com, kvm@vger.kernel.org, Shmuel Fomberg Subject: [PATCH] Adding support for hyper-v enlightments hv-extcalls and hv-memory-hint (hypercall HvExtCallMemoryHeatHint) Date: Wed, 14 Oct 2026 15:39:54 +0300 Message-ID: <20261014123954.95181-1-shmuelfomberg@gmail.com> X-Mailer: git-send-email 2.54.0 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=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::434; envelope-from=shmuelfomberg@gmail.com; helo=mail-wr1-x434.google.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.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, FORGED_GMAIL_RCVD=1, 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-Mailman-Approved-At: Wed, 14 Oct 2026 08:54:54 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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: 1791982664504158503 Content-Type: text/plain; charset="utf-8" Signed-off-by: Shmuel Fomberg --- hw/hyperv/hv-gpa-range.c | 59 ++++ include/hw/hyperv/hv-gpa-range.h | 52 ++++ .../functional/x86_64/test_hyperv_extcalls.py | 101 +++++++ tests/unit/test-hv-gpa-range.c | 271 ++++++++++++++++++ 4 files changed, 483 insertions(+) create mode 100644 hw/hyperv/hv-gpa-range.c create mode 100644 include/hw/hyperv/hv-gpa-range.h create mode 100644 tests/functional/x86_64/test_hyperv_extcalls.py create mode 100644 tests/unit/test-hv-gpa-range.c diff --git a/hw/hyperv/hv-gpa-range.c b/hw/hyperv/hv-gpa-range.c new file mode 100644 index 0000000000..e6d3d3eb42 --- /dev/null +++ b/hw/hyperv/hv-gpa-range.c @@ -0,0 +1,59 @@ +/* + * Hyper-V extended memory heat-hint GPA range helpers. + * + * Copyright (c) 2015-2018 Virtuozzo International GmbH. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "hw/hyperv/hv-gpa-range.h" +#include "hw/hyperv/hyperv-proto.h" + +bool hv_decode_gpa_page_range(uint64_t raw, hwaddr *gpa, hwaddr *len) +{ + uint64_t pages =3D (raw & HV_GPA_PAGE_RANGE_ADDITIONAL_MASK) + 1; + + if (raw & HV_GPA_PAGE_RANGE_LARGEPAGE) { + if (raw & HV_GPA_PAGE_RANGE_PAGE_SIZE_1GB) { + *gpa =3D raw & HV_GPA_PAGE_RANGE_1G_GPA_MASK; + *len =3D pages << 30; + } else { + *gpa =3D raw & HV_GPA_PAGE_RANGE_2M_GPA_MASK; + *len =3D pages << 21; + } + } else { + *gpa =3D raw & HV_GPA_PAGE_RANGE_4K_GPA_MASK; + *len =3D pages << 12; + } + + return *len !=3D 0; +} + +bool hv_madvise_walk_step(hwaddr *gpa, hwaddr *len, + hwaddr section_start, hwaddr section_size, + hwaddr *out_gpa, hwaddr *out_len) +{ + hwaddr skip; + + /* + * A hole with no memory region makes the section start past the + * cursor; a zero-size section means there is nothing left to find. + * Either way, stop -- do not advise or advance past the gap. + */ + if (!section_size || section_start < *gpa) { + return false; + } + + skip =3D section_start - *gpa; + if (skip >=3D *len || section_size > *len - skip) { + return false; + } + + *out_gpa =3D section_start; + *out_len =3D section_size; + + *gpa =3D section_start + section_size; + *len -=3D skip + section_size; + return true; +} diff --git a/include/hw/hyperv/hv-gpa-range.h b/include/hw/hyperv/hv-gpa-ra= nge.h new file mode 100644 index 0000000000..b1cf6664b8 --- /dev/null +++ b/include/hw/hyperv/hv-gpa-range.h @@ -0,0 +1,52 @@ +/* + * Hyper-V extended memory heat-hint GPA range helpers. + * + * These are small, dependency-free helpers factored out of hyperv.c so + * that the bit-decoding and walk-cursor bookkeeping can be exercised + * directly by unit tests (see tests/unit/test-hv-gpa-range.c) without + * requiring a real MemoryRegion/MemoryRegionSection or any KVM/system + * emulation plumbing. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef HW_HYPERV_HV_GPA_RANGE_H +#define HW_HYPERV_HV_GPA_RANGE_H + +#include "exec/hwaddr.h" + +/* + * Decode one HV_GPA_PAGE_RANGE qword (as found in + * struct hyperv_memory_heat_hint.ranges[]) into a GPA and byte length. + * + * Returns false if the decoded length is zero (nothing to do for this + * range); true otherwise, with *gpa and *len filled in. + * + * Visible for testing. + */ +bool hv_decode_gpa_page_range(uint64_t raw, hwaddr *gpa, hwaddr *len); + +/* + * Validate a MemoryRegionSection found by memory_region_find() for the + * current walk cursor (*gpa, *len), and advance the cursor past it. + * + * @section_start/@section_size are the section's + * offset_within_address_space and size, as returned by + * memory_region_find(). + * + * On success, returns true and fills in @out_gpa/@out_len with the GPA + * and length that the caller should actually act upon (e.g. madvise()), + * and advances *gpa and *len past the section. + * + * Returns false, without touching *gpa or *len, if the section does not + * fit within the remaining [*gpa, *gpa + *len) range -- e.g. because + * memory_region_find() skipped over a hole and returned a section that + * starts past the cursor, or a section that runs past the requested + * range. The caller should stop walking in that case. + * + * Visible for testing. + */ +bool hv_madvise_walk_step(hwaddr *gpa, hwaddr *len, + hwaddr section_start, hwaddr section_size, + hwaddr *out_gpa, hwaddr *out_len); + +#endif /* HW_HYPERV_HV_GPA_RANGE_H */ diff --git a/tests/functional/x86_64/test_hyperv_extcalls.py b/tests/functi= onal/x86_64/test_hyperv_extcalls.py new file mode 100644 index 0000000000..c48f59d554 --- /dev/null +++ b/tests/functional/x86_64/test_hyperv_extcalls.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 +# +# Validation of the hv-extcalls / hv-memory-hint CPU properties. +# +# hv-extcalls gates the Hyper-V extended hypercalls (HV_ENABLE_EXTENDED_ +# HYPERCALLS, HV_CPUID_FEATURES.EBX bit 20); hv-memory-hint additionally +# gates whether HvExtCallQueryCapabilities advertises, and +# HvExtCallMemoryHeatHint accepts, the memory heat-hint calls. +# +# SPDX-License-Identifier: GPL-2.0-or-later +# + +from qemu_test import QemuSystemTest + + +class HypervExtCallsProps(QemuSystemTest): + """ + Validation of the hv-extcalls and hv-memory-hint CPU properties. + """ + + def get_cpu_prop(self, prop): + cpu_path =3D self.vm.cmd('query-cpus-fast')[0].get('qom-path') + return self.vm.cmd('qom-get', path=3Dcpu_path, property=3Dprop) + + def launch_with_cpu_props(self, cpu_props): + self.set_machine('pc') + self.vm.add_args('-S') + self.set_vm_arg('-cpu', 'qemu64' + cpu_props) + self.vm.launch() + + def test_defaults_off(self): + """ + Neither property is requested: both must default to off, in + particular hv-extcalls (which was deliberately made default-off + so that enabling the extended hypercalls is an explicit opt-in). + """ + self.launch_with_cpu_props('') + self.assertFalse(self.get_cpu_prop('hv-extcalls'), + 'hv-extcalls must default to off') + self.assertFalse(self.get_cpu_prop('hv-memory-hint'), + 'hv-memory-hint must default to off') + + def test_hv_extcalls_on(self): + """ + hv-extcalls=3Don by itself must not implicitly turn on + hv-memory-hint. + """ + self.launch_with_cpu_props(',hv-extcalls=3Don') + self.assertTrue(self.get_cpu_prop('hv-extcalls'), + 'hv-extcalls=3Don must be reflected on the CPU') + self.assertFalse(self.get_cpu_prop('hv-memory-hint'), + 'hv-extcalls=3Don must not imply hv-memory-hint') + + def test_hv_memory_hint_on(self): + """ + hv-memory-hint=3Don without hv-extcalls must still parse/boot (the + combination is a no-op at runtime -- kvm_hv_handle_exit() gates + *all* extended hypercalls, including the capabilities query, on + hv-extcalls -- but QEMU must not reject it at CPU-realize time). + """ + self.launch_with_cpu_props(',hv-memory-hint=3Don') + self.assertFalse(self.get_cpu_prop('hv-extcalls')) + self.assertTrue(self.get_cpu_prop('hv-memory-hint'), + 'hv-memory-hint=3Don must be reflected on the CPU') + + def test_both_on(self): + """ + The intended "fully enabled" configuration: both properties on. + """ + self.launch_with_cpu_props(',hv-extcalls=3Don,hv-memory-hint=3Don') + self.assertTrue(self.get_cpu_prop('hv-extcalls')) + self.assertTrue(self.get_cpu_prop('hv-memory-hint')) + + +class HypervExtCallsModelExpansion(QemuSystemTest): + """ + Confirm hv-extcalls/hv-memory-hint are discoverable via + query-cpu-model-expansion, the mechanism management layers (e.g. + libvirt) use to probe which CPU properties/features are supported + before adding them to a domain definition. + """ + + def test(self): + self.set_machine('none') + self.vm.add_args('-S') + self.vm.launch() + + model =3D {'name': 'qemu64', + 'props': {'hv-extcalls': True, 'hv-memory-hint': True}} + e =3D self.vm.cmd('query-cpu-model-expansion', model=3Dmodel, + type=3D'full') + props =3D e['model']['props'] + + self.assertIn('hv-extcalls', props) + self.assertIn('hv-memory-hint', props) + self.assertTrue(props['hv-extcalls']) + self.assertTrue(props['hv-memory-hint']) + + +if __name__ =3D=3D '__main__': + QemuSystemTest.main() diff --git a/tests/unit/test-hv-gpa-range.c b/tests/unit/test-hv-gpa-range.c new file mode 100644 index 0000000000..70196f271b --- /dev/null +++ b/tests/unit/test-hv-gpa-range.c @@ -0,0 +1,271 @@ +/* + * Unit tests for the Hyper-V extended memory heat-hint GPA range helpers + * (hw/hyperv/hv-gpa-range.c). + * + * These exercise: + * - hv_decode_gpa_page_range(): decoding of the HV_GPA_PAGE_RANGE qwords + * found in a HvExtCallMemoryHeatHint[Async] input buffer. + * - hv_madvise_walk_step(): the walk-cursor bookkeeping used by + * hv_madvise_gpa_range() to turn a sequence of memory_region_find() + * results into the correct set of (host, len) madvise() targets, in + * particular across holes in the GPA space. This directly covers the + * "madvise walk can log/act on the wrong GPA" regression. + * + * Copyright (c) 2015-2018 Virtuozzo International GmbH. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "hw/hyperv/hyperv-proto.h" +#include "hw/hyperv/hv-gpa-range.h" + +#define KiB (1024ULL) +#define MiB (1024 * KiB) +#define GiB (1024 * MiB) + +/* + * --------------------------------------------------------------------- + * hv_decode_gpa_page_range() + * --------------------------------------------------------------------- + */ + +static void test_decode_4k_single_page(void) +{ + hwaddr gpa, len; + /* pfn =3D 5, additional_pages =3D 0 -> one 4K page at GPA 0x5000 */ + uint64_t raw =3D (5ULL << 12); + + g_assert_true(hv_decode_gpa_page_range(raw, &gpa, &len)); + g_assert_cmphex(gpa, =3D=3D, 5 * 4 * KiB); + g_assert_cmphex(len, =3D=3D, 4 * KiB); +} + +static void test_decode_4k_additional_pages(void) +{ + hwaddr gpa, len; + /* pfn =3D 7, additional_pages =3D 3 -> 4 consecutive 4K pages */ + uint64_t raw =3D (7ULL << 12) | 3; + + g_assert_true(hv_decode_gpa_page_range(raw, &gpa, &len)); + g_assert_cmphex(gpa, =3D=3D, 7 * 4 * KiB); + g_assert_cmphex(len, =3D=3D, 4 * 4 * KiB); +} + +static void test_decode_4k_max_additional_pages(void) +{ + hwaddr gpa, len; + /* additional_pages field is 11 bits: max value 0x7ff -> 2048 pages */ + uint64_t raw =3D (1ULL << 20) | HV_GPA_PAGE_RANGE_ADDITIONAL_MASK; + + g_assert_true(hv_decode_gpa_page_range(raw, &gpa, &len)); + g_assert_cmphex(len, =3D=3D, 2048 * 4 * KiB); +} + +static void test_decode_2m_large_page(void) +{ + hwaddr gpa, len; + /* pfn (2M units) =3D 3, additional_pages =3D 1 -> two 2M pages */ + uint64_t raw =3D (3ULL << 21) | HV_GPA_PAGE_RANGE_LARGEPAGE | 1; + + g_assert_true(hv_decode_gpa_page_range(raw, &gpa, &len)); + g_assert_cmphex(gpa, =3D=3D, 3 * 2 * MiB); + g_assert_cmphex(len, =3D=3D, 2 * 2 * MiB); +} + +static void test_decode_1g_large_page(void) +{ + hwaddr gpa, len; + /* pfn (1G units) =3D 2, additional_pages =3D 0 -> one 1G page */ + uint64_t raw =3D (2ULL << 30) | HV_GPA_PAGE_RANGE_LARGEPAGE | + HV_GPA_PAGE_RANGE_PAGE_SIZE_1GB; + + g_assert_true(hv_decode_gpa_page_range(raw, &gpa, &len)); + g_assert_cmphex(gpa, =3D=3D, 2 * GiB); + g_assert_cmphex(len, =3D=3D, 1 * GiB); +} + +static void test_decode_masks_strip_low_bits(void) +{ + hwaddr gpa, len; + /* + * For a 2M large page, everything below bit 21 other than the + * LARGEPAGE flag (bit 11) and additional_pages (bits 0-10) must not + * leak into the decoded GPA. + */ + uint64_t raw =3D (5ULL << 21) | HV_GPA_PAGE_RANGE_LARGEPAGE | 0x3ff; + + g_assert_true(hv_decode_gpa_page_range(raw, &gpa, &len)); + g_assert_cmphex(gpa, =3D=3D, 5 * 2 * MiB); + g_assert_cmphex(gpa & ((2 * MiB) - 1), =3D=3D, 0); +} + +/* + * --------------------------------------------------------------------- + * hv_madvise_walk_step() + * --------------------------------------------------------------------- + */ + +static void test_walk_full_match(void) +{ + hwaddr gpa =3D 0x1000, len =3D 0x1000; + hwaddr out_gpa, out_len; + + /* memory_region_find() returned exactly the requested range */ + g_assert_true(hv_madvise_walk_step(&gpa, &len, 0x1000, 0x1000, + &out_gpa, &out_len)); + g_assert_cmphex(out_gpa, =3D=3D, 0x1000); + g_assert_cmphex(out_len, =3D=3D, 0x1000); + g_assert_cmphex(gpa, =3D=3D, 0x2000); + g_assert_cmphex(len, =3D=3D, 0); +} + +static void test_walk_partial_section_no_skip(void) +{ + /* Section starts exactly at the cursor but is smaller than @len */ + hwaddr gpa =3D 0x1000, len =3D 0x3000; + hwaddr out_gpa, out_len; + + g_assert_true(hv_madvise_walk_step(&gpa, &len, 0x1000, 0x1000, + &out_gpa, &out_len)); + g_assert_cmphex(out_gpa, =3D=3D, 0x1000); + g_assert_cmphex(out_len, =3D=3D, 0x1000); + /* cursor must advance past exactly the found section, nothing more */ + g_assert_cmphex(gpa, =3D=3D, 0x2000); + g_assert_cmphex(len, =3D=3D, 0x2000); +} + +/* + * Regression test for "the madvise walk can log/act on the wrong GPA": + * memory_region_find() skips over a hole and returns a section that + * starts *past* the walk cursor. The reported/advised GPA must be the + * section's real start, not the stale search cursor, and the hole must + * be consumed without being advised. + */ +static void test_walk_hole_before_section(void) +{ + /* Looking for RAM in [0, 0x3000); nothing mapped until 0x1000 */ + hwaddr gpa =3D 0, len =3D 0x3000; + hwaddr out_gpa, out_len; + + g_assert_true(hv_madvise_walk_step(&gpa, &len, 0x1000, 0x1000, + &out_gpa, &out_len)); + /* Must report the section's actual GPA, not the original cursor (0) */ + g_assert_cmphex(out_gpa, =3D=3D, 0x1000); + g_assert_cmphex(out_len, =3D=3D, 0x1000); + /* Cursor must account for the 0x1000 hole *and* the 0x1000 section */ + g_assert_cmphex(gpa, =3D=3D, 0x2000); + g_assert_cmphex(len, =3D=3D, 0x1000); +} + +/* + * Drive two consecutive steps the way hv_madvise_gpa_range()'s while loop + * does: a hole, then a directly-adjacent section, then end of range. + * Confirms the cursor neither double-processes the boundary nor drifts. + */ +static void test_walk_hole_then_adjacent_section(void) +{ + hwaddr gpa =3D 0, len =3D 0x3000; + hwaddr out_gpa, out_len; + + /* Step 1: hole [0,0x1000), section [0x1000,0x2000) */ + g_assert_true(hv_madvise_walk_step(&gpa, &len, 0x1000, 0x1000, + &out_gpa, &out_len)); + g_assert_cmphex(out_gpa, =3D=3D, 0x1000); + g_assert_cmphex(gpa, =3D=3D, 0x2000); + g_assert_cmphex(len, =3D=3D, 0x1000); + + /* Step 2: section starts exactly at the new cursor, no further skip */ + g_assert_true(hv_madvise_walk_step(&gpa, &len, 0x2000, 0x1000, + &out_gpa, &out_len)); + g_assert_cmphex(out_gpa, =3D=3D, 0x2000); + g_assert_cmphex(out_len, =3D=3D, 0x1000); + g_assert_cmphex(gpa, =3D=3D, 0x3000); + g_assert_cmphex(len, =3D=3D, 0); +} + +static void test_walk_section_past_requested_range(void) +{ + /* Section is larger than what's left to process -> stop, don't act */ + hwaddr gpa =3D 0x1000, len =3D 0x1000; + hwaddr out_gpa =3D 0xdead, out_len =3D 0xdead; + + g_assert_false(hv_madvise_walk_step(&gpa, &len, 0x1000, 0x2000, + &out_gpa, &out_len)); + /* cursor and outputs must be left untouched on failure */ + g_assert_cmphex(gpa, =3D=3D, 0x1000); + g_assert_cmphex(len, =3D=3D, 0x1000); + g_assert_cmphex(out_gpa, =3D=3D, 0xdead); + g_assert_cmphex(out_len, =3D=3D, 0xdead); +} + +static void test_walk_section_before_cursor(void) +{ + /* Defensive case: section starts before the cursor -> stop */ + hwaddr gpa =3D 0x2000, len =3D 0x1000; + hwaddr out_gpa, out_len; + + g_assert_false(hv_madvise_walk_step(&gpa, &len, 0x1000, 0x1000, + &out_gpa, &out_len)); + g_assert_cmphex(gpa, =3D=3D, 0x2000); + g_assert_cmphex(len, =3D=3D, 0x1000); +} + +static void test_walk_zero_size_section(void) +{ + hwaddr gpa =3D 0, len =3D 0x1000; + hwaddr out_gpa, out_len; + + g_assert_false(hv_madvise_walk_step(&gpa, &len, 0, 0, &out_gpa, &out_l= en)); + g_assert_cmphex(gpa, =3D=3D, 0); + g_assert_cmphex(len, =3D=3D, 0x1000); +} + +static void test_walk_skip_equals_len(void) +{ + /* The whole remaining range is a hole; section starts right at the en= d */ + hwaddr gpa =3D 0, len =3D 0x1000; + hwaddr out_gpa, out_len; + + g_assert_false(hv_madvise_walk_step(&gpa, &len, 0x1000, 0x1000, + &out_gpa, &out_len)); + g_assert_cmphex(gpa, =3D=3D, 0); + g_assert_cmphex(len, =3D=3D, 0x1000); +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/hv-gpa-range/decode/4k-single-page", + test_decode_4k_single_page); + g_test_add_func("/hv-gpa-range/decode/4k-additional-pages", + test_decode_4k_additional_pages); + g_test_add_func("/hv-gpa-range/decode/4k-max-additional-pages", + test_decode_4k_max_additional_pages); + g_test_add_func("/hv-gpa-range/decode/2m-large-page", + test_decode_2m_large_page); + g_test_add_func("/hv-gpa-range/decode/1g-large-page", + test_decode_1g_large_page); + g_test_add_func("/hv-gpa-range/decode/masks-strip-low-bits", + test_decode_masks_strip_low_bits); + + g_test_add_func("/hv-gpa-range/walk/full-match", + test_walk_full_match); + g_test_add_func("/hv-gpa-range/walk/partial-section-no-skip", + test_walk_partial_section_no_skip); + g_test_add_func("/hv-gpa-range/walk/hole-before-section", + test_walk_hole_before_section); + g_test_add_func("/hv-gpa-range/walk/hole-then-adjacent-section", + test_walk_hole_then_adjacent_section); + g_test_add_func("/hv-gpa-range/walk/section-past-requested-range", + test_walk_section_past_requested_range); + g_test_add_func("/hv-gpa-range/walk/section-before-cursor", + test_walk_section_before_cursor); + g_test_add_func("/hv-gpa-range/walk/zero-size-section", + test_walk_zero_size_section); + g_test_add_func("/hv-gpa-range/walk/skip-equals-len", + test_walk_skip_equals_len); + + return g_test_run(); +} --=20 2.54.0 (Apple Git-157)