blob: becf7a79fbfc18d394ff1feb8d33ccfd4ecf6122 [file] [log] [blame]
Oleksandr Andrushchenko3cc1dcc2020-08-06 12:42:54 +03001// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) 2006 - Cambridge University
4 * (C) 2020 - EPAM Systems Inc.
5 *
6 * File: gnttab.c [1]
7 * Author: Steven Smith (sos22@cam.ac.uk)
8 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
9 *
10 * Date: July 2006
11 *
12 * Description: Simple grant tables implementation. About as stupid as it's
13 * possible to be and still work.
14 *
15 * [1] - http://xenbits.xen.org/gitweb/?p=mini-os.git;a=summary
16 */
17#include <common.h>
18#include <linux/compiler.h>
19#include <log.h>
20#include <malloc.h>
21
22#include <asm/armv8/mmu.h>
23#include <asm/io.h>
24#include <asm/xen/system.h>
25
26#include <linux/bug.h>
27
28#include <xen/gnttab.h>
29#include <xen/hvm.h>
30
31#include <xen/interface/memory.h>
32
33DECLARE_GLOBAL_DATA_PTR;
34
35#define NR_RESERVED_ENTRIES 8
36
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))
40
41static struct grant_entry_v1 *gnttab_table;
42static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
43
44static void put_free_entry(grant_ref_t ref)
45{
46 unsigned long flags;
47
48 local_irq_save(flags);
49 gnttab_list[ref] = gnttab_list[0];
50 gnttab_list[0] = ref;
51 local_irq_restore(flags);
52}
53
54static grant_ref_t get_free_entry(void)
55{
56 unsigned int ref;
57 unsigned long flags;
58
59 local_irq_save(flags);
60 ref = gnttab_list[0];
61 BUG_ON(ref < NR_RESERVED_ENTRIES || ref >= NR_GRANT_ENTRIES);
62 gnttab_list[0] = gnttab_list[ref];
63 local_irq_restore(flags);
64 return ref;
65}
66
67/**
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
74 *
75 * Return: relevant grant reference
76 */
77grant_ref_t gnttab_grant_access(domid_t domid, unsigned long frame, int readonly)
78{
79 grant_ref_t ref;
80
81 ref = get_free_entry();
82 gnttab_table[ref].frame = frame;
83 gnttab_table[ref].domid = domid;
84 wmb();
85 readonly *= GTF_readonly;
86 gnttab_table[ref].flags = GTF_permit_access | readonly;
87
88 return ref;
89}
90
91/**
92 * gnttab_end_access() - End of memory sharing. The function invalidates
93 * the entry in the grant table.
94 */
95int gnttab_end_access(grant_ref_t ref)
96{
97 u16 flags, nflags;
98
99 BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
100
101 nflags = gnttab_table[ref].flags;
102 do {
103 if ((flags = nflags) & (GTF_reading | GTF_writing)) {
104 printf("WARNING: g.e. still in use! (%x)\n", flags);
105 return 0;
106 }
107 } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
108 flags);
109
110 put_free_entry(ref);
111 return 1;
112}
113
114grant_ref_t gnttab_alloc_and_grant(void **map)
115{
116 unsigned long mfn;
117 grant_ref_t gref;
118
119 *map = (void *)memalign(PAGE_SIZE, PAGE_SIZE);
120 mfn = virt_to_mfn(*map);
121 gref = gnttab_grant_access(0, mfn, 0);
122 return gref;
123}
124
125static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
126
127const char *gnttabop_error(int16_t status)
128{
129 status = -status;
130 if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
131 return "bad status";
132 else
133 return gnttabop_error_msgs[status];
134}
135
136/* Get Xen's suggested physical page assignments for the grant table. */
137void get_gnttab_base(phys_addr_t *gnttab_base, phys_size_t *gnttab_sz)
138{
139 const void *blob = gd->fdt_blob;
140 struct fdt_resource res;
141 int mem;
142
143 mem = fdt_node_offset_by_compatible(blob, -1, "xen,xen");
144 if (mem < 0) {
145 printf("No xen,xen compatible found\n");
146 BUG();
147 }
148
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");
152 BUG();
153 }
154
155 *gnttab_base = (phys_addr_t)res.start;
156 if (gnttab_sz)
157 *gnttab_sz = (phys_size_t)(res.end - res.start + 1);
158
159 debug("FDT suggests grant table base at %llx\n",
160 *gnttab_base);
161}
162
163void init_gnttab(void)
164{
165 struct xen_add_to_physmap xatp;
166 struct gnttab_setup_table setup;
167 xen_pfn_t frames[NR_GRANT_FRAMES];
168 int i, rc;
169
170 debug("%s\n", __func__);
171
172 for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
173 put_free_entry(i);
174
175 get_gnttab_base((phys_addr_t *)&gnttab_table, NULL);
176
177 for (i = 0; i < NR_GRANT_FRAMES; i++) {
178 xatp.domid = DOMID_SELF;
179 xatp.size = 0;
180 xatp.space = XENMAPSPACE_grant_table;
181 xatp.idx = i;
182 xatp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
183 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
184 if (rc)
185 printf("XENMEM_add_to_physmap failed; status = %d\n",
186 rc);
187 BUG_ON(rc != 0);
188 }
189
190 setup.dom = DOMID_SELF;
191 setup.nr_frames = NR_GRANT_FRAMES;
192 set_xen_guest_handle(setup.frame_list, frames);
193}
194
195void fini_gnttab(void)
196{
197 struct xen_remove_from_physmap xrtp;
198 struct gnttab_setup_table setup;
199 int i, rc;
200
201 debug("%s\n", __func__);
202
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);
207 if (rc)
208 printf("XENMEM_remove_from_physmap failed; status = %d\n",
209 rc);
210 BUG_ON(rc != 0);
211 }
212
213 setup.dom = DOMID_SELF;
214 setup.nr_frames = 0;
215}
216