qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@redhat.com>
To: Bin Meng <bmeng.cn@gmail.com>,
	Peter Maydell <peter.maydell@linaro.org>,
	David Hildenbrand <david@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>, Peter Xu <peterx@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [PATCH] docs/devel: memory: Document MemoryRegionOps requirement
Date: Mon, 6 Sep 2021 15:01:54 +0200	[thread overview]
Message-ID: <54817618-59b9-d6e6-f903-f7d6938c17ba@redhat.com> (raw)
In-Reply-To: <20210906122020.5793-1-bmeng.cn@gmail.com>

On 9/6/21 2:20 PM, Bin Meng wrote:
> It's been a requirement that at least one function pointer for read
> and one for write are provided ever since the MemoryRegion APIs were
> introduced in 2012.
> 
> Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
> ---
> 
>  docs/devel/memory.rst | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/docs/devel/memory.rst b/docs/devel/memory.rst
> index 5dc8a12682..7b589b21d2 100644
> --- a/docs/devel/memory.rst
> +++ b/docs/devel/memory.rst
> @@ -344,6 +344,11 @@ based on the attributes used for the memory transaction, or need
>  to be able to respond that the access should provoke a bus error
>  rather than completing successfully; those devices can use the
>  ->read_with_attrs() and ->write_with_attrs() callbacks instead.
> +The requirement for a device's MemoryRegionOps is that at least
> +one callback for read and one for write are provided. If both
> +->read() and ->read_with_attrs() are provided, the plain ->read()
> +version takes precedence over the with_attrs() version. So does
> +the write callback.

What about also adding a runtime check?

-- >8 --
diff --git a/softmmu/memory.c b/softmmu/memory.c
index bfedaf9c4df..8ab602d3379 100644
--- a/softmmu/memory.c
+++ b/softmmu/memory.c
@@ -1516,6 +1516,17 @@ MemTxResult
memory_region_dispatch_write(MemoryRegion *mr,
     }
 }

+static void memory_region_set_ops(MemoryRegion *mr, const
MemoryRegionOps *ops)
+{
+    if (ops) {
+        assert(ops->valid.accepts || (ops->read || ops->read_with_attrs));
+        assert(ops->valid.accepts || (ops->write ||
ops->write_with_attrs));
+        mr->ops = ops;
+    } else {
+        mr->ops = &unassigned_mem_ops;
+    }
+}
+
 void memory_region_init_io(MemoryRegion *mr,
                            Object *owner,
                            const MemoryRegionOps *ops,
@@ -1524,7 +1535,7 @@ void memory_region_init_io(MemoryRegion *mr,
                            uint64_t size)
 {
     memory_region_init(mr, owner, name, size);
-    mr->ops = ops ? ops : &unassigned_mem_ops;
+    memory_region_set_ops(mr, ops);
     mr->opaque = opaque;
     mr->terminates = true;
 }
@@ -1701,7 +1712,7 @@ void
memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
     Error *err = NULL;
     assert(ops);
     memory_region_init(mr, owner, name, size);
-    mr->ops = ops;
+    memory_region_set_ops(mr, ops);
     mr->opaque = opaque;
     mr->terminates = true;
     mr->rom_device = true;
---



  parent reply	other threads:[~2021-09-06 13:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-06 12:20 [PATCH] docs/devel: memory: Document MemoryRegionOps requirement Bin Meng
2021-09-06 12:25 ` David Hildenbrand
2021-09-06 13:01 ` Philippe Mathieu-Daudé [this message]
2021-09-08 18:50   ` Peter Xu
2021-09-08 20:17     ` Philippe Mathieu-Daudé
2021-10-02 14:37       ` Bin Meng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=54817618-59b9-d6e6-f903-f7d6938c17ba@redhat.com \
    --to=philmd@redhat.com \
    --cc=bmeng.cn@gmail.com \
    --cc=david@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).