Re: [RFC PATCH v2 4/5] softmmu/physmem: Introduce flatview_access_allowed() to check bus perms

2021-08-24 Thread Stefan Hajnoczi
On Mon, Aug 23, 2021 at 06:41:56PM +0200, Philippe Mathieu-Daudé wrote:
> Introduce flatview_access_allowed() to check bus permission
> before running any bus transaction. For now this is a simple
> stub.
> 
> Signed-off-by: Philippe Mathieu-Daudé 
> ---
>  softmmu/physmem.c | 37 +++--
>  1 file changed, 35 insertions(+), 2 deletions(-)
> 
> diff --git a/softmmu/physmem.c b/softmmu/physmem.c
> index e534dc69918..0d31a2f4199 100644
> --- a/softmmu/physmem.c
> +++ b/softmmu/physmem.c
> @@ -2754,6 +2754,27 @@ static bool prepare_mmio_access(MemoryRegion *mr)
>  return release_lock;
>  }
>  
> +/**
> + * flatview_access_allowed
> + * @mr: #MemoryRegion to be accessed
> + * @attrs: memory transaction attributes
> + * @addr: address within that memory region
> + * @len: the number of bytes to access
> + * @result: optional pointer to a MemTxResult or NULL
> + *
> + * Check if a memory transaction is allowed. If an error occurs this
> + * method will return false to indicate denial, as well as setting
> + * @result to contain corresponding #MemTxResult.
> + *
> + * Returns: true if transaction is allowed, false if denied.
> + */
> +static inline bool flatview_access_allowed(MemoryRegion *mr, MemTxAttrs 
> attrs,
> +   hwaddr addr, hwaddr len,
> +   MemTxResult *result)
> +{
> +return true;
> +}

The callers below don't benefit from the result pointer argument. The
code would be clearer if they did:

  if (!flatview_access_allowed(...)) {
  return MEMTX_BUS_ERROR;
  }

> +
>  /* Called within RCU critical section.  */
>  static MemTxResult flatview_write_continue(FlatView *fv, hwaddr addr,
> MemTxAttrs attrs,
> @@ -2768,7 +2789,9 @@ static MemTxResult flatview_write_continue(FlatView 
> *fv, hwaddr addr,
>  const uint8_t *buf = ptr;
>  
>  for (;;) {
> -if (!memory_access_is_direct(mr, true)) {
> +if (!flatview_access_allowed(mr, attrs, addr1, l, )) {
> +/* 'result' contains the error, keep going. */
> +} else if (!memory_access_is_direct(mr, true)) {
>  release_lock |= prepare_mmio_access(mr);
>  l = memory_access_size(mr, l, addr1);
>  /* XXX: could force current_cpu to NULL to avoid
> @@ -2810,9 +2833,13 @@ static MemTxResult flatview_write(FlatView *fv, hwaddr 
> addr, MemTxAttrs attrs,
>  hwaddr l;
>  hwaddr addr1;
>  MemoryRegion *mr;
> +MemTxResult result = MEMTX_OK;
>  
>  l = len;
>  mr = flatview_translate(fv, addr, , , true, attrs);
> +if (!flatview_access_allowed(mr, attrs, addr, len, )) {
> +return result;
> +}
>  return flatview_write_continue(fv, addr, attrs, buf, len,
> addr1, l, mr);
>  }
> @@ -2831,7 +2858,9 @@ MemTxResult flatview_read_continue(FlatView *fv, hwaddr 
> addr,
>  
>  fuzz_dma_read_cb(addr, len, mr);
>  for (;;) {
> -if (!memory_access_is_direct(mr, false)) {
> +if (!flatview_access_allowed(mr, attrs, addr1, l, )) {
> +/* 'result' contains the error, keep going. */
> +} else if (!memory_access_is_direct(mr, false)) {
>  /* I/O case */
>  release_lock |= prepare_mmio_access(mr);
>  l = memory_access_size(mr, l, addr1);
> @@ -2871,9 +2900,13 @@ static MemTxResult flatview_read(FlatView *fv, hwaddr 
> addr,
>  hwaddr l;
>  hwaddr addr1;
>  MemoryRegion *mr;
> +MemTxResult result = MEMTX_OK;
>  
>  l = len;
>  mr = flatview_translate(fv, addr, , , false, attrs);
> +if (!flatview_access_allowed(mr, attrs, addr, len, )) {
> +return result;
> +}
>  return flatview_read_continue(fv, addr, attrs, buf, len,
>addr1, l, mr);
>  }
> -- 
> 2.31.1
> 


signature.asc
Description: PGP signature


Re: [RFC PATCH v2 4/5] softmmu/physmem: Introduce flatview_access_allowed() to check bus perms

2021-08-23 Thread David Hildenbrand

On 23.08.21 20:43, Peter Xu wrote:

On Mon, Aug 23, 2021 at 06:41:56PM +0200, Philippe Mathieu-Daudé wrote:

Introduce flatview_access_allowed() to check bus permission
before running any bus transaction. For now this is a simple
stub.

Signed-off-by: Philippe Mathieu-Daudé 


Shall we squash this patch into the next one?  It helps explain better on why
it's needed.  Thanks,



I'd even go one step further and squash 3-5 into a single one.

--
Thanks,

David / dhildenb




Re: [RFC PATCH v2 4/5] softmmu/physmem: Introduce flatview_access_allowed() to check bus perms

2021-08-23 Thread Peter Xu
On Mon, Aug 23, 2021 at 06:41:56PM +0200, Philippe Mathieu-Daudé wrote:
> Introduce flatview_access_allowed() to check bus permission
> before running any bus transaction. For now this is a simple
> stub.
> 
> Signed-off-by: Philippe Mathieu-Daudé 

Shall we squash this patch into the next one?  It helps explain better on why
it's needed.  Thanks,

-- 
Peter Xu




[RFC PATCH v2 4/5] softmmu/physmem: Introduce flatview_access_allowed() to check bus perms

2021-08-23 Thread Philippe Mathieu-Daudé
Introduce flatview_access_allowed() to check bus permission
before running any bus transaction. For now this is a simple
stub.

Signed-off-by: Philippe Mathieu-Daudé 
---
 softmmu/physmem.c | 37 +++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/softmmu/physmem.c b/softmmu/physmem.c
index e534dc69918..0d31a2f4199 100644
--- a/softmmu/physmem.c
+++ b/softmmu/physmem.c
@@ -2754,6 +2754,27 @@ static bool prepare_mmio_access(MemoryRegion *mr)
 return release_lock;
 }
 
+/**
+ * flatview_access_allowed
+ * @mr: #MemoryRegion to be accessed
+ * @attrs: memory transaction attributes
+ * @addr: address within that memory region
+ * @len: the number of bytes to access
+ * @result: optional pointer to a MemTxResult or NULL
+ *
+ * Check if a memory transaction is allowed. If an error occurs this
+ * method will return false to indicate denial, as well as setting
+ * @result to contain corresponding #MemTxResult.
+ *
+ * Returns: true if transaction is allowed, false if denied.
+ */
+static inline bool flatview_access_allowed(MemoryRegion *mr, MemTxAttrs attrs,
+   hwaddr addr, hwaddr len,
+   MemTxResult *result)
+{
+return true;
+}
+
 /* Called within RCU critical section.  */
 static MemTxResult flatview_write_continue(FlatView *fv, hwaddr addr,
MemTxAttrs attrs,
@@ -2768,7 +2789,9 @@ static MemTxResult flatview_write_continue(FlatView *fv, 
hwaddr addr,
 const uint8_t *buf = ptr;
 
 for (;;) {
-if (!memory_access_is_direct(mr, true)) {
+if (!flatview_access_allowed(mr, attrs, addr1, l, )) {
+/* 'result' contains the error, keep going. */
+} else if (!memory_access_is_direct(mr, true)) {
 release_lock |= prepare_mmio_access(mr);
 l = memory_access_size(mr, l, addr1);
 /* XXX: could force current_cpu to NULL to avoid
@@ -2810,9 +2833,13 @@ static MemTxResult flatview_write(FlatView *fv, hwaddr 
addr, MemTxAttrs attrs,
 hwaddr l;
 hwaddr addr1;
 MemoryRegion *mr;
+MemTxResult result = MEMTX_OK;
 
 l = len;
 mr = flatview_translate(fv, addr, , , true, attrs);
+if (!flatview_access_allowed(mr, attrs, addr, len, )) {
+return result;
+}
 return flatview_write_continue(fv, addr, attrs, buf, len,
addr1, l, mr);
 }
@@ -2831,7 +2858,9 @@ MemTxResult flatview_read_continue(FlatView *fv, hwaddr 
addr,
 
 fuzz_dma_read_cb(addr, len, mr);
 for (;;) {
-if (!memory_access_is_direct(mr, false)) {
+if (!flatview_access_allowed(mr, attrs, addr1, l, )) {
+/* 'result' contains the error, keep going. */
+} else if (!memory_access_is_direct(mr, false)) {
 /* I/O case */
 release_lock |= prepare_mmio_access(mr);
 l = memory_access_size(mr, l, addr1);
@@ -2871,9 +2900,13 @@ static MemTxResult flatview_read(FlatView *fv, hwaddr 
addr,
 hwaddr l;
 hwaddr addr1;
 MemoryRegion *mr;
+MemTxResult result = MEMTX_OK;
 
 l = len;
 mr = flatview_translate(fv, addr, , , false, attrs);
+if (!flatview_access_allowed(mr, attrs, addr, len, )) {
+return result;
+}
 return flatview_read_continue(fv, addr, attrs, buf, len,
   addr1, l, mr);
 }
-- 
2.31.1