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;
104 if ((flags) & (GTF_reading | GTF_writing)) {
105 printf("WARNING: g.e. still in use! (%x)\n", flags);
108 } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
115 grant_ref_t gnttab_alloc_and_grant(void **map)
120 *map = (void *)memalign(PAGE_SIZE, PAGE_SIZE);
121 mfn = virt_to_mfn(*map);
122 gref = gnttab_grant_access(0, mfn, 0);
126 static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
128 const char *gnttabop_error(int16_t status)
131 if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
134 return gnttabop_error_msgs[status];
137 /* Get Xen's suggested physical page assignments for the grant table. */
138 void get_gnttab_base(phys_addr_t *gnttab_base, phys_size_t *gnttab_sz)
140 const void *blob = gd->fdt_blob;
141 struct fdt_resource res;
144 mem = fdt_node_offset_by_compatible(blob, -1, "xen,xen");
146 printf("No xen,xen compatible found\n");
150 mem = fdt_get_resource(blob, mem, "reg", 0, &res);
151 if (mem == -FDT_ERR_NOTFOUND) {
152 printf("No grant table base in the device tree\n");
156 *gnttab_base = (phys_addr_t)res.start;
158 *gnttab_sz = (phys_size_t)(res.end - res.start + 1);
160 debug("FDT suggests grant table base at %llx\n",
164 void init_gnttab(void)
166 struct xen_add_to_physmap xatp;
167 struct gnttab_setup_table setup;
168 xen_pfn_t frames[NR_GRANT_FRAMES];
171 debug("%s\n", __func__);
173 for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
176 get_gnttab_base((phys_addr_t *)&gnttab_table, NULL);
178 for (i = 0; i < NR_GRANT_FRAMES; i++) {
179 xatp.domid = DOMID_SELF;
181 xatp.space = XENMAPSPACE_grant_table;
183 xatp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
184 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
186 printf("XENMEM_add_to_physmap failed; status = %d\n",
191 setup.dom = DOMID_SELF;
192 setup.nr_frames = NR_GRANT_FRAMES;
193 set_xen_guest_handle(setup.frame_list, frames);
196 void fini_gnttab(void)
198 struct xen_remove_from_physmap xrtp;
199 struct gnttab_setup_table setup;
202 debug("%s\n", __func__);
204 for (i = 0; i < NR_GRANT_FRAMES; i++) {
205 xrtp.domid = DOMID_SELF;
206 xrtp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
207 rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrtp);
209 printf("XENMEM_remove_from_physmap failed; status = %d\n",
214 setup.dom = DOMID_SELF;