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 <asm/global_data.h>
19 #include <linux/compiler.h>
23 #include <asm/armv8/mmu.h>
25 #include <asm/xen/system.h>
27 #include <linux/bug.h>
29 #include <xen/gnttab.h>
32 #include <xen/interface/memory.h>
34 DECLARE_GLOBAL_DATA_PTR;
36 #define NR_RESERVED_ENTRIES 8
38 /* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
39 #define NR_GRANT_FRAMES 1
40 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(struct grant_entry_v1))
42 static struct grant_entry_v1 *gnttab_table;
43 static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
45 static void put_free_entry(grant_ref_t ref)
49 local_irq_save(flags);
50 gnttab_list[ref] = gnttab_list[0];
52 local_irq_restore(flags);
55 static grant_ref_t get_free_entry(void)
60 local_irq_save(flags);
62 BUG_ON(ref < NR_RESERVED_ENTRIES || ref >= NR_GRANT_ENTRIES);
63 gnttab_list[0] = gnttab_list[ref];
64 local_irq_restore(flags);
69 * gnttab_grant_access() - Allow access to the given frame.
70 * The function creates an entry in the grant table according
71 * to the specified parameters.
72 * @domid: the id of the domain for which access is allowed
73 * @frame: the number of the shared frame
74 * @readonly: determines whether the frame is shared read-only or read-write
76 * Return: relevant grant reference
78 grant_ref_t gnttab_grant_access(domid_t domid, unsigned long frame, int readonly)
82 ref = get_free_entry();
83 gnttab_table[ref].frame = frame;
84 gnttab_table[ref].domid = domid;
86 readonly *= GTF_readonly;
87 gnttab_table[ref].flags = GTF_permit_access | readonly;
93 * gnttab_end_access() - End of memory sharing. The function invalidates
94 * the entry in the grant table.
96 int gnttab_end_access(grant_ref_t ref)
100 BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
102 nflags = gnttab_table[ref].flags;
105 if ((flags) & (GTF_reading | GTF_writing)) {
106 printf("WARNING: g.e. still in use! (%x)\n", flags);
109 } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
116 grant_ref_t gnttab_alloc_and_grant(void **map)
121 *map = (void *)memalign(PAGE_SIZE, PAGE_SIZE);
122 mfn = virt_to_mfn(*map);
123 gref = gnttab_grant_access(0, mfn, 0);
127 static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
129 const char *gnttabop_error(int16_t status)
132 if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
135 return gnttabop_error_msgs[status];
138 /* Get Xen's suggested physical page assignments for the grant table. */
139 void get_gnttab_base(phys_addr_t *gnttab_base, phys_size_t *gnttab_sz)
141 const void *blob = gd->fdt_blob;
142 struct fdt_resource res;
145 mem = fdt_node_offset_by_compatible(blob, -1, "xen,xen");
147 printf("No xen,xen compatible found\n");
151 mem = fdt_get_resource(blob, mem, "reg", 0, &res);
152 if (mem == -FDT_ERR_NOTFOUND) {
153 printf("No grant table base in the device tree\n");
157 *gnttab_base = (phys_addr_t)res.start;
159 *gnttab_sz = (phys_size_t)(res.end - res.start + 1);
161 debug("FDT suggests grant table base at %llx\n",
165 void init_gnttab(void)
167 struct xen_add_to_physmap xatp;
168 struct gnttab_setup_table setup;
169 xen_pfn_t frames[NR_GRANT_FRAMES];
172 debug("%s\n", __func__);
174 for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
177 get_gnttab_base((phys_addr_t *)&gnttab_table, NULL);
179 for (i = 0; i < NR_GRANT_FRAMES; i++) {
180 xatp.domid = DOMID_SELF;
182 xatp.space = XENMAPSPACE_grant_table;
184 xatp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
185 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
187 printf("XENMEM_add_to_physmap failed; status = %d\n",
192 setup.dom = DOMID_SELF;
193 setup.nr_frames = NR_GRANT_FRAMES;
194 set_xen_guest_handle(setup.frame_list, frames);
197 void fini_gnttab(void)
199 struct xen_remove_from_physmap xrtp;
200 struct gnttab_setup_table setup;
203 debug("%s\n", __func__);
205 for (i = 0; i < NR_GRANT_FRAMES; i++) {
206 xrtp.domid = DOMID_SELF;
207 xrtp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
208 rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrtp);
210 printf("XENMEM_remove_from_physmap failed; status = %d\n",
215 setup.dom = DOMID_SELF;