1 // SPDX-License-Identifier: GPL-2.0
3 * (C) 2006 - Cambridge University
4 * (C) 2020 - EPAM Systems Inc.
7 * Author: Steven Smith (sos22@cam.ac.uk)
8 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
12 * Description: Simple grant tables implementation. About as stupid as it's
13 * possible to be and still work.
15 * [1] - http://xenbits.xen.org/gitweb/?p=mini-os.git;a=summary
18 #include <linux/compiler.h>
22 #include <asm/armv8/mmu.h>
24 #include <asm/xen/system.h>
26 #include <linux/bug.h>
28 #include <xen/gnttab.h>
31 #include <xen/interface/memory.h>
33 DECLARE_GLOBAL_DATA_PTR;
35 #define NR_RESERVED_ENTRIES 8
37 /* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
38 #define NR_GRANT_FRAMES 1
39 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(struct grant_entry_v1))
41 static struct grant_entry_v1 *gnttab_table;
42 static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
44 static void put_free_entry(grant_ref_t ref)
48 local_irq_save(flags);
49 gnttab_list[ref] = gnttab_list[0];
51 local_irq_restore(flags);
54 static grant_ref_t get_free_entry(void)
59 local_irq_save(flags);
61 BUG_ON(ref < NR_RESERVED_ENTRIES || ref >= NR_GRANT_ENTRIES);
62 gnttab_list[0] = gnttab_list[ref];
63 local_irq_restore(flags);
68 * gnttab_grant_access() - Allow access to the given frame.
69 * The function creates an entry in the grant table according
70 * to the specified parameters.
71 * @domid: the id of the domain for which access is allowed
72 * @frame: the number of the shared frame
73 * @readonly: determines whether the frame is shared read-only or read-write
75 * Return: relevant grant reference
77 grant_ref_t gnttab_grant_access(domid_t domid, unsigned long frame, int readonly)
81 ref = get_free_entry();
82 gnttab_table[ref].frame = frame;
83 gnttab_table[ref].domid = domid;
85 readonly *= GTF_readonly;
86 gnttab_table[ref].flags = GTF_permit_access | readonly;
92 * gnttab_end_access() - End of memory sharing. The function invalidates
93 * the entry in the grant table.
95 int gnttab_end_access(grant_ref_t ref)
99 BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
101 nflags = gnttab_table[ref].flags;
103 if ((flags = nflags) & (GTF_reading | GTF_writing)) {
104 printf("WARNING: g.e. still in use! (%x)\n", flags);
107 } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
114 grant_ref_t gnttab_alloc_and_grant(void **map)
119 *map = (void *)memalign(PAGE_SIZE, PAGE_SIZE);
120 mfn = virt_to_mfn(*map);
121 gref = gnttab_grant_access(0, mfn, 0);
125 static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
127 const char *gnttabop_error(int16_t status)
130 if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
133 return gnttabop_error_msgs[status];
136 /* Get Xen's suggested physical page assignments for the grant table. */
137 void get_gnttab_base(phys_addr_t *gnttab_base, phys_size_t *gnttab_sz)
139 const void *blob = gd->fdt_blob;
140 struct fdt_resource res;
143 mem = fdt_node_offset_by_compatible(blob, -1, "xen,xen");
145 printf("No xen,xen compatible found\n");
149 mem = fdt_get_resource(blob, mem, "reg", 0, &res);
150 if (mem == -FDT_ERR_NOTFOUND) {
151 printf("No grant table base in the device tree\n");
155 *gnttab_base = (phys_addr_t)res.start;
157 *gnttab_sz = (phys_size_t)(res.end - res.start + 1);
159 debug("FDT suggests grant table base at %llx\n",
163 void init_gnttab(void)
165 struct xen_add_to_physmap xatp;
166 struct gnttab_setup_table setup;
167 xen_pfn_t frames[NR_GRANT_FRAMES];
170 debug("%s\n", __func__);
172 for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
175 get_gnttab_base((phys_addr_t *)&gnttab_table, NULL);
177 for (i = 0; i < NR_GRANT_FRAMES; i++) {
178 xatp.domid = DOMID_SELF;
180 xatp.space = XENMAPSPACE_grant_table;
182 xatp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
183 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
185 printf("XENMEM_add_to_physmap failed; status = %d\n",
190 setup.dom = DOMID_SELF;
191 setup.nr_frames = NR_GRANT_FRAMES;
192 set_xen_guest_handle(setup.frame_list, frames);
195 void fini_gnttab(void)
197 struct xen_remove_from_physmap xrtp;
198 struct gnttab_setup_table setup;
201 debug("%s\n", __func__);
203 for (i = 0; i < NR_GRANT_FRAMES; i++) {
204 xrtp.domid = DOMID_SELF;
205 xrtp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
206 rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrtp);
208 printf("XENMEM_remove_from_physmap failed; status = %d\n",
213 setup.dom = DOMID_SELF;