blob: 9bf1091ab32109695b650b1aa920ee347bcf4b62 [file] [log] [blame]
Oleksandr Andrushchenkoc8506742020-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 {
Anastasiia Lukianenko3337b292020-08-21 12:10:04 +0300103 flags = nflags;
104 if ((flags) & (GTF_reading | GTF_writing)) {
Oleksandr Andrushchenkoc8506742020-08-06 12:42:54 +0300105 printf("WARNING: g.e. still in use! (%x)\n", flags);
106 return 0;
107 }
108 } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
109 flags);
110
111 put_free_entry(ref);
112 return 1;
113}
114
115grant_ref_t gnttab_alloc_and_grant(void **map)
116{
117 unsigned long mfn;
118 grant_ref_t gref;
119
120 *map = (void *)memalign(PAGE_SIZE, PAGE_SIZE);
121 mfn = virt_to_mfn(*map);
122 gref = gnttab_grant_access(0, mfn, 0);
123 return gref;
124}
125
126static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
127
128const char *gnttabop_error(int16_t status)
129{
130 status = -status;
131 if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
132 return "bad status";
133 else
134 return gnttabop_error_msgs[status];
135}
136
137/* Get Xen's suggested physical page assignments for the grant table. */
138void get_gnttab_base(phys_addr_t *gnttab_base, phys_size_t *gnttab_sz)
139{
140 const void *blob = gd->fdt_blob;
141 struct fdt_resource res;
142 int mem;
143
144 mem = fdt_node_offset_by_compatible(blob, -1, "xen,xen");
145 if (mem < 0) {
146 printf("No xen,xen compatible found\n");
147 BUG();
148 }
149
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");
153 BUG();
154 }
155
156 *gnttab_base = (phys_addr_t)res.start;
157 if (gnttab_sz)
158 *gnttab_sz = (phys_size_t)(res.end - res.start + 1);
159
160 debug("FDT suggests grant table base at %llx\n",
161 *gnttab_base);
162}
163
164void init_gnttab(void)
165{
166 struct xen_add_to_physmap xatp;
167 struct gnttab_setup_table setup;
168 xen_pfn_t frames[NR_GRANT_FRAMES];
169 int i, rc;
170
171 debug("%s\n", __func__);
172
173 for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
174 put_free_entry(i);
175
176 get_gnttab_base((phys_addr_t *)&gnttab_table, NULL);
177
178 for (i = 0; i < NR_GRANT_FRAMES; i++) {
179 xatp.domid = DOMID_SELF;
180 xatp.size = 0;
181 xatp.space = XENMAPSPACE_grant_table;
182 xatp.idx = i;
183 xatp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
184 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
185 if (rc)
186 printf("XENMEM_add_to_physmap failed; status = %d\n",
187 rc);
188 BUG_ON(rc != 0);
189 }
190
191 setup.dom = DOMID_SELF;
192 setup.nr_frames = NR_GRANT_FRAMES;
193 set_xen_guest_handle(setup.frame_list, frames);
194}
195
196void fini_gnttab(void)
197{
198 struct xen_remove_from_physmap xrtp;
199 struct gnttab_setup_table setup;
200 int i, rc;
201
202 debug("%s\n", __func__);
203
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);
208 if (rc)
209 printf("XENMEM_remove_from_physmap failed; status = %d\n",
210 rc);
211 BUG_ON(rc != 0);
212 }
213
214 setup.dom = DOMID_SELF;
215 setup.nr_frames = 0;
216}
217