Re: [PATCH 11/17] xen: Port Xen grant table driver from mini-os

2020-07-03 Thread Anastasiia Lukianenko
Hi Julien,

On Wed, 2020-07-01 at 17:59 +0100, Julien Grall wrote:
> 
> On 01/07/2020 17:29, Anastasiia Lukianenko wrote:
> > From: Oleksandr Andrushchenko 
> > 
> > Make required updates to run on u-boot.
> > 
> > Signed-off-by: Oleksandr Andrushchenko <
> > oleksandr_andrushche...@epam.com>
> > Signed-off-by: Anastasiia Lukianenko <
> > anastasiia_lukiane...@epam.com>
> > ---
> >   board/xen/xenguest_arm64/xenguest_arm64.c |  13 ++
> >   drivers/xen/Makefile  |   1 +
> >   drivers/xen/gnttab.c  | 258
> > ++
> >   drivers/xen/hypervisor.c  |   2 +
> >   include/xen/gnttab.h  |  25 +++
> >   5 files changed, 299 insertions(+)
> >   create mode 100644 drivers/xen/gnttab.c
> >   create mode 100644 include/xen/gnttab.h
> > 
> > diff --git a/board/xen/xenguest_arm64/xenguest_arm64.c
> > b/board/xen/xenguest_arm64/xenguest_arm64.c
> > index e8621f7174..b4e1650f99 100644
> > --- a/board/xen/xenguest_arm64/xenguest_arm64.c
> > +++ b/board/xen/xenguest_arm64/xenguest_arm64.c
> > @@ -22,6 +22,7 @@
> >   
> >   #include 
> >   
> > +#include 
> >   #include 
> >   
> >   DECLARE_GLOBAL_DATA_PTR;
> > @@ -64,6 +65,8 @@ static int setup_mem_map(void)
> > struct fdt_resource res;
> > const void *blob = gd->fdt_blob;
> > u64 gfn;
> > +   phys_addr_t gnttab_base;
> > +   phys_size_t gnttab_sz;
> >   
> > /*
> >  * Add "magic" region which is used by Xen to provide some
> > essentials
> > @@ -97,6 +100,16 @@ static int setup_mem_map(void)
> > PTE_BLOCK_INNER_SHARE);
> > i++;
> >   
> > +   /* Get Xen's suggested physical page assignments for the grant
> > table. */
> > +   get_gnttab_base(_base, _sz);
> > +
> > +   xen_mem_map[i].virt = gnttab_base;
> > +   xen_mem_map[i].phys = gnttab_base;
> > +   xen_mem_map[i].size = gnttab_sz;
> > +   xen_mem_map[i].attrs = (PTE_BLOCK_MEMTYPE(MT_NORMAL) |
> > +   PTE_BLOCK_INNER_SHARE);
> > +   i++;
> > +
> > mem = get_next_memory_node(blob, -1);
> > if (mem < 0) {
> > printf("%s: Missing /memory node\n", __func__);
> > diff --git a/drivers/xen/Makefile b/drivers/xen/Makefile
> > index 9d0f604aaa..243b13277a 100644
> > --- a/drivers/xen/Makefile
> > +++ b/drivers/xen/Makefile
> > @@ -5,3 +5,4 @@
> >   obj-y += hypervisor.o
> >   obj-y += events.o
> >   obj-y += xenbus.o
> > +obj-y += gnttab.o
> > diff --git a/drivers/xen/gnttab.c b/drivers/xen/gnttab.c
> > new file mode 100644
> > index 00..b18102e329
> > --- /dev/null
> > +++ b/drivers/xen/gnttab.c
> > @@ -0,0 +1,258 @@
> > +/*
> > +
> > ***
> > *
> > + * (C) 2006 - Cambridge University
> > + * (C) 2020 - EPAM Systems Inc.
> > +
> > ***
> > *
> > + *
> > + * File: gnttab.c
> > + *   Author: Steven Smith (so...@cam.ac.uk)
> > + *  Changes: Grzegorz Milos (gm...@cam.ac.uk)
> > + *
> > + * Date: July 2006
> > + *
> > + * Environment: Xen Minimal OS
> > + * Description: Simple grant tables implementation. About as
> > stupid as it's
> > + *  possible to be and still work.
> > + *
> > +
> > ***
> > *
> > + */
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +#include 
> > +#include 
> > +#include 
> > +
> > +#include 
> > +
> > +#include 
> > +#include 
> > +
> > +#include 
> > +
> > +DECLARE_GLOBAL_DATA_PTR;
> > +
> > +#define NR_RESERVED_ENTRIES 8
> > +
> > +/* NR_GRANT_FRAMES must be less than or equal to that configured
> > in Xen */
> > +#define NR_GRANT_FRAMES 1
> > +#define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE /
> > sizeof(struct grant_entry_v1))
> > +
> > +static struct grant_entry_v1 *gnttab_table;
> > +static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
> > +
> > +static void put_free_entry(grant_ref_t ref)
> > +{
> > +   unsigned long flags;
> > +
> > +   local_irq_save(flags);
> > +   gnttab_list[ref] = gnttab_list[0];
> > +   gnttab_list[0]  = ref;
> > +   local_irq_restore(flags);
> > +}
> > +
> > +static grant_ref_t get_free_entry(void)
> > +{
> > +   unsigned int ref;
> > +   unsigned long flags;
> > +
> > +   local_irq_save(flags);
> > +   ref = gnttab_list[0];
> > +   BUG_ON(ref < NR_RESERVED_ENTRIES || ref >= NR_GRANT_ENTRIES);
> > +   gnttab_list[0] = gnttab_list[ref];
> > +   local_irq_restore(flags);
> > +   return ref;
> > +}
> > +
> > +grant_ref_t gnttab_grant_access(domid_t domid, unsigned long
> > frame, int readonly)
> > +{
> > +   grant_ref_t ref;
> > +
> > +   ref = get_free_entry();
> > +   gnttab_table[ref].frame = frame;
> > +   gnttab_table[ref].domid = domid;
> > +   wmb();
> > +   readonly *= GTF_readonly;
> > +   gnttab_table[ref].flags = GTF_permit_access | readonly;
> > +
> > +   return ref;
> > +}
> > +
> > +grant_ref_t 

Re: [PATCH 11/17] xen: Port Xen grant table driver from mini-os

2020-07-02 Thread Julien Grall




On 01/07/2020 17:29, Anastasiia Lukianenko wrote:

From: Oleksandr Andrushchenko 

Make required updates to run on u-boot.

Signed-off-by: Oleksandr Andrushchenko 
Signed-off-by: Anastasiia Lukianenko 
---
  board/xen/xenguest_arm64/xenguest_arm64.c |  13 ++
  drivers/xen/Makefile  |   1 +
  drivers/xen/gnttab.c  | 258 ++
  drivers/xen/hypervisor.c  |   2 +
  include/xen/gnttab.h  |  25 +++
  5 files changed, 299 insertions(+)
  create mode 100644 drivers/xen/gnttab.c
  create mode 100644 include/xen/gnttab.h

diff --git a/board/xen/xenguest_arm64/xenguest_arm64.c 
b/board/xen/xenguest_arm64/xenguest_arm64.c
index e8621f7174..b4e1650f99 100644
--- a/board/xen/xenguest_arm64/xenguest_arm64.c
+++ b/board/xen/xenguest_arm64/xenguest_arm64.c
@@ -22,6 +22,7 @@
  
  #include 
  
+#include 

  #include 
  
  DECLARE_GLOBAL_DATA_PTR;

@@ -64,6 +65,8 @@ static int setup_mem_map(void)
struct fdt_resource res;
const void *blob = gd->fdt_blob;
u64 gfn;
+   phys_addr_t gnttab_base;
+   phys_size_t gnttab_sz;
  
  	/*

 * Add "magic" region which is used by Xen to provide some essentials
@@ -97,6 +100,16 @@ static int setup_mem_map(void)
PTE_BLOCK_INNER_SHARE);
i++;
  
+	/* Get Xen's suggested physical page assignments for the grant table. */

+   get_gnttab_base(_base, _sz);
+
+   xen_mem_map[i].virt = gnttab_base;
+   xen_mem_map[i].phys = gnttab_base;
+   xen_mem_map[i].size = gnttab_sz;
+   xen_mem_map[i].attrs = (PTE_BLOCK_MEMTYPE(MT_NORMAL) |
+   PTE_BLOCK_INNER_SHARE);
+   i++;
+
mem = get_next_memory_node(blob, -1);
if (mem < 0) {
printf("%s: Missing /memory node\n", __func__);
diff --git a/drivers/xen/Makefile b/drivers/xen/Makefile
index 9d0f604aaa..243b13277a 100644
--- a/drivers/xen/Makefile
+++ b/drivers/xen/Makefile
@@ -5,3 +5,4 @@
  obj-y += hypervisor.o
  obj-y += events.o
  obj-y += xenbus.o
+obj-y += gnttab.o
diff --git a/drivers/xen/gnttab.c b/drivers/xen/gnttab.c
new file mode 100644
index 00..b18102e329
--- /dev/null
+++ b/drivers/xen/gnttab.c
@@ -0,0 +1,258 @@
+/*
+ 
+ * (C) 2006 - Cambridge University
+ * (C) 2020 - EPAM Systems Inc.
+ 
+ *
+ * File: gnttab.c
+ *   Author: Steven Smith (so...@cam.ac.uk)
+ *  Changes: Grzegorz Milos (gm...@cam.ac.uk)
+ *
+ * Date: July 2006
+ *
+ * Environment: Xen Minimal OS
+ * Description: Simple grant tables implementation. About as stupid as it's
+ *  possible to be and still work.
+ *
+ 
+ */
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+#include 
+
+#include 
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define NR_RESERVED_ENTRIES 8
+
+/* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
+#define NR_GRANT_FRAMES 1
+#define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(struct 
grant_entry_v1))
+
+static struct grant_entry_v1 *gnttab_table;
+static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
+
+static void put_free_entry(grant_ref_t ref)
+{
+   unsigned long flags;
+
+   local_irq_save(flags);
+   gnttab_list[ref] = gnttab_list[0];
+   gnttab_list[0]  = ref;
+   local_irq_restore(flags);
+}
+
+static grant_ref_t get_free_entry(void)
+{
+   unsigned int ref;
+   unsigned long flags;
+
+   local_irq_save(flags);
+   ref = gnttab_list[0];
+   BUG_ON(ref < NR_RESERVED_ENTRIES || ref >= NR_GRANT_ENTRIES);
+   gnttab_list[0] = gnttab_list[ref];
+   local_irq_restore(flags);
+   return ref;
+}
+
+grant_ref_t gnttab_grant_access(domid_t domid, unsigned long frame, int 
readonly)
+{
+   grant_ref_t ref;
+
+   ref = get_free_entry();
+   gnttab_table[ref].frame = frame;
+   gnttab_table[ref].domid = domid;
+   wmb();
+   readonly *= GTF_readonly;
+   gnttab_table[ref].flags = GTF_permit_access | readonly;
+
+   return ref;
+}
+
+grant_ref_t gnttab_grant_transfer(domid_t domid, unsigned long pfn)


It is not possible to transfer grant on Arm. So I would suggest to 
remove the code related to it.


[...]


+unsigned long gnttab_end_transfer(grant_ref_t ref)


likewise.


+{
+   unsigned long frame;
+   u16 flags;
+
+   BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
+
+   while (!((flags = gnttab_table[ref].flags) & GTF_transfer_committed)) {
+   if (synch_cmpxchg(_table[ref].flags, flags, 0) == flags) 
{
+   printf("Release unused transfer grant.\n");
+   put_free_entry(ref);
+   return 0;
+ 

[PATCH 11/17] xen: Port Xen grant table driver from mini-os

2020-07-02 Thread Anastasiia Lukianenko
From: Oleksandr Andrushchenko 

Make required updates to run on u-boot.

Signed-off-by: Oleksandr Andrushchenko 
Signed-off-by: Anastasiia Lukianenko 
---
 board/xen/xenguest_arm64/xenguest_arm64.c |  13 ++
 drivers/xen/Makefile  |   1 +
 drivers/xen/gnttab.c  | 258 ++
 drivers/xen/hypervisor.c  |   2 +
 include/xen/gnttab.h  |  25 +++
 5 files changed, 299 insertions(+)
 create mode 100644 drivers/xen/gnttab.c
 create mode 100644 include/xen/gnttab.h

diff --git a/board/xen/xenguest_arm64/xenguest_arm64.c 
b/board/xen/xenguest_arm64/xenguest_arm64.c
index e8621f7174..b4e1650f99 100644
--- a/board/xen/xenguest_arm64/xenguest_arm64.c
+++ b/board/xen/xenguest_arm64/xenguest_arm64.c
@@ -22,6 +22,7 @@
 
 #include 
 
+#include 
 #include 
 
 DECLARE_GLOBAL_DATA_PTR;
@@ -64,6 +65,8 @@ static int setup_mem_map(void)
struct fdt_resource res;
const void *blob = gd->fdt_blob;
u64 gfn;
+   phys_addr_t gnttab_base;
+   phys_size_t gnttab_sz;
 
/*
 * Add "magic" region which is used by Xen to provide some essentials
@@ -97,6 +100,16 @@ static int setup_mem_map(void)
PTE_BLOCK_INNER_SHARE);
i++;
 
+   /* Get Xen's suggested physical page assignments for the grant table. */
+   get_gnttab_base(_base, _sz);
+
+   xen_mem_map[i].virt = gnttab_base;
+   xen_mem_map[i].phys = gnttab_base;
+   xen_mem_map[i].size = gnttab_sz;
+   xen_mem_map[i].attrs = (PTE_BLOCK_MEMTYPE(MT_NORMAL) |
+   PTE_BLOCK_INNER_SHARE);
+   i++;
+
mem = get_next_memory_node(blob, -1);
if (mem < 0) {
printf("%s: Missing /memory node\n", __func__);
diff --git a/drivers/xen/Makefile b/drivers/xen/Makefile
index 9d0f604aaa..243b13277a 100644
--- a/drivers/xen/Makefile
+++ b/drivers/xen/Makefile
@@ -5,3 +5,4 @@
 obj-y += hypervisor.o
 obj-y += events.o
 obj-y += xenbus.o
+obj-y += gnttab.o
diff --git a/drivers/xen/gnttab.c b/drivers/xen/gnttab.c
new file mode 100644
index 00..b18102e329
--- /dev/null
+++ b/drivers/xen/gnttab.c
@@ -0,0 +1,258 @@
+/*
+ 
+ * (C) 2006 - Cambridge University
+ * (C) 2020 - EPAM Systems Inc.
+ 
+ *
+ * File: gnttab.c
+ *   Author: Steven Smith (so...@cam.ac.uk)
+ *  Changes: Grzegorz Milos (gm...@cam.ac.uk)
+ *
+ * Date: July 2006
+ *
+ * Environment: Xen Minimal OS
+ * Description: Simple grant tables implementation. About as stupid as it's
+ *  possible to be and still work.
+ *
+ 
+ */
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#include 
+
+#include 
+#include 
+
+#include 
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define NR_RESERVED_ENTRIES 8
+
+/* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
+#define NR_GRANT_FRAMES 1
+#define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(struct 
grant_entry_v1))
+
+static struct grant_entry_v1 *gnttab_table;
+static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
+
+static void put_free_entry(grant_ref_t ref)
+{
+   unsigned long flags;
+
+   local_irq_save(flags);
+   gnttab_list[ref] = gnttab_list[0];
+   gnttab_list[0]  = ref;
+   local_irq_restore(flags);
+}
+
+static grant_ref_t get_free_entry(void)
+{
+   unsigned int ref;
+   unsigned long flags;
+
+   local_irq_save(flags);
+   ref = gnttab_list[0];
+   BUG_ON(ref < NR_RESERVED_ENTRIES || ref >= NR_GRANT_ENTRIES);
+   gnttab_list[0] = gnttab_list[ref];
+   local_irq_restore(flags);
+   return ref;
+}
+
+grant_ref_t gnttab_grant_access(domid_t domid, unsigned long frame, int 
readonly)
+{
+   grant_ref_t ref;
+
+   ref = get_free_entry();
+   gnttab_table[ref].frame = frame;
+   gnttab_table[ref].domid = domid;
+   wmb();
+   readonly *= GTF_readonly;
+   gnttab_table[ref].flags = GTF_permit_access | readonly;
+
+   return ref;
+}
+
+grant_ref_t gnttab_grant_transfer(domid_t domid, unsigned long pfn)
+{
+   grant_ref_t ref;
+
+   ref = get_free_entry();
+   gnttab_table[ref].frame = pfn;
+   gnttab_table[ref].domid = domid;
+   wmb();
+   gnttab_table[ref].flags = GTF_accept_transfer;
+
+   return ref;
+}
+
+int gnttab_end_access(grant_ref_t ref)
+{
+   u16 flags, nflags;
+
+   BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
+
+   nflags = gnttab_table[ref].flags;
+   do {
+   if ((flags = nflags) & (GTF_reading | GTF_writing)) {
+   printf("WARNING: g.e. still in use! (%x)\n", flags);
+   return 0;
+   }
+   } while ((nflags =