1 /* $NetBSD: xengnt.c,v 1.17 2010/01/23 22:32:42 cegger Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Manuel Bouyer. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: xengnt.c,v 1.17 2010/01/23 22:32:42 cegger Exp $"); 30 31 #include <sys/types.h> 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/malloc.h> 35 #include <sys/queue.h> 36 #include <sys/extent.h> 37 #include <sys/kernel.h> 38 #include <uvm/uvm.h> 39 40 #include <xen/hypervisor.h> 41 #include <xen/xen.h> 42 #include <xen/granttables.h> 43 44 /* #define XENDEBUG */ 45 #ifdef XENDEBUG 46 #define DPRINTF(x) printf x 47 #else 48 #define DPRINTF(x) 49 #endif 50 51 #define NR_GRANT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(grant_entry_t)) 52 53 /* Current number of frames making up the grant table */ 54 int gnt_nr_grant_frames; 55 /* Maximum number of frames that can make up the grant table */ 56 int gnt_max_grant_frames; 57 58 /* table of free grant entries */ 59 grant_ref_t *gnt_entries; 60 /* last free entry */ 61 int last_gnt_entry; 62 /* empty entry in the list */ 63 #define XENGNT_NO_ENTRY 0xffffffff 64 65 /* VM address of the grant table */ 66 grant_entry_t *grant_table; 67 68 static grant_ref_t xengnt_get_entry(void); 69 static void xengnt_free_entry(grant_ref_t); 70 static void xengnt_resume(void); 71 static int xengnt_more_entries(void); 72 73 void 74 xengnt_init(void) 75 { 76 struct gnttab_query_size query; 77 int rc; 78 int nr_grant_entries; 79 int i; 80 81 query.dom = DOMID_SELF; 82 rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1); 83 if ((rc < 0) || (query.status != GNTST_okay)) 84 gnt_max_grant_frames = 4; /* Legacy max number of frames */ 85 else 86 gnt_max_grant_frames = query.max_nr_frames; 87 gnt_nr_grant_frames = 0; 88 89 nr_grant_entries = 90 gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE; 91 92 grant_table = (void *)uvm_km_alloc(kernel_map, 93 gnt_max_grant_frames * PAGE_SIZE, 0, UVM_KMF_VAONLY); 94 if (grant_table == NULL) 95 panic("xengnt_init() no VM space"); 96 gnt_entries = malloc((nr_grant_entries + 1) * sizeof(grant_ref_t), 97 M_DEVBUF, M_NOWAIT); 98 if (gnt_entries == NULL) 99 panic("xengnt_init() no space for bitmask"); 100 for (i = 0; i <= nr_grant_entries; i++) 101 gnt_entries[i] = XENGNT_NO_ENTRY; 102 103 last_gnt_entry = 0; 104 xengnt_resume(); 105 106 } 107 108 /* 109 * Resume grant table state 110 */ 111 static void 112 xengnt_resume(void) 113 { 114 int previous_nr_grant_frames = gnt_nr_grant_frames; 115 gnt_nr_grant_frames = 0; 116 while (gnt_nr_grant_frames < previous_nr_grant_frames) { 117 if (xengnt_more_entries() != 0) 118 panic("xengnt_resume: can't restore grant frames"); 119 } 120 } 121 122 /* 123 * Add another page to the grant table 124 * Returns 0 on success, ENOMEM on failure 125 */ 126 static int 127 xengnt_more_entries(void) 128 { 129 gnttab_setup_table_t setup; 130 u_long *pages; 131 int nframes_new = gnt_nr_grant_frames + 1; 132 int i; 133 134 if (gnt_nr_grant_frames == gnt_max_grant_frames) 135 return ENOMEM; 136 137 pages = malloc(nframes_new * sizeof(u_long), M_DEVBUF, M_NOWAIT); 138 if (pages == NULL) 139 return ENOMEM; 140 141 setup.dom = DOMID_SELF; 142 setup.nr_frames = nframes_new; 143 xenguest_handle(setup.frame_list) = pages; 144 145 /* 146 * setup the grant table, made of nframes_new frames 147 * and return the list of their virtual addresses 148 * in 'pages' 149 */ 150 if (HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1) != 0) 151 panic("%s: setup table failed", __func__); 152 if (setup.status != GNTST_okay) { 153 aprint_error("%s: setup table returned %d\n", 154 __func__, setup.status); 155 free(pages, M_DEVBUF); 156 return ENOMEM; 157 } 158 159 DPRINTF(("xengnt_more_entries: map 0x%lx -> %p\n", 160 pages[gnt_nr_grant_frames], 161 (char *)grant_table + gnt_nr_grant_frames * PAGE_SIZE)); 162 163 /* 164 * map between grant_table addresses and the machine addresses of 165 * the grant table frames 166 */ 167 pmap_kenter_ma(((vaddr_t)grant_table) + gnt_nr_grant_frames * PAGE_SIZE, 168 ((paddr_t)pages[gnt_nr_grant_frames]) << PAGE_SHIFT, 169 VM_PROT_WRITE, 0); 170 171 /* 172 * add the grant entries associated to the last grant table frame 173 * and mark them as free 174 */ 175 for (i = gnt_nr_grant_frames * NR_GRANT_ENTRIES_PER_PAGE; 176 i < nframes_new * NR_GRANT_ENTRIES_PER_PAGE; 177 i++) { 178 KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY); 179 gnt_entries[last_gnt_entry] = i; 180 last_gnt_entry++; 181 } 182 gnt_nr_grant_frames = nframes_new; 183 free(pages, M_DEVBUF); 184 return 0; 185 } 186 187 /* 188 * Returns a reference to the first free entry in grant table 189 */ 190 static grant_ref_t 191 xengnt_get_entry(void) 192 { 193 grant_ref_t entry; 194 int s = splvm(); 195 static struct timeval xengnt_nonmemtime; 196 static const struct timeval xengnt_nonmemintvl = {5,0}; 197 198 if (last_gnt_entry == 0) { 199 if (xengnt_more_entries()) { 200 splx(s); 201 if (ratecheck(&xengnt_nonmemtime, &xengnt_nonmemintvl)) 202 printf("xengnt_get_entry: out of grant " 203 "table entries\n"); 204 return XENGNT_NO_ENTRY; 205 } 206 } 207 KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY); 208 last_gnt_entry--; 209 entry = gnt_entries[last_gnt_entry]; 210 gnt_entries[last_gnt_entry] = XENGNT_NO_ENTRY; 211 splx(s); 212 KASSERT(entry != XENGNT_NO_ENTRY); 213 KASSERT(last_gnt_entry >= 0 && last_gnt_entry <= gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE); 214 return entry; 215 } 216 217 /* 218 * Mark the grant table entry as free 219 */ 220 static void 221 xengnt_free_entry(grant_ref_t entry) 222 { 223 int s = splvm(); 224 KASSERT(gnt_entries[last_gnt_entry] == XENGNT_NO_ENTRY); 225 KASSERT(last_gnt_entry >= 0 && last_gnt_entry <= gnt_max_grant_frames * NR_GRANT_ENTRIES_PER_PAGE); 226 gnt_entries[last_gnt_entry] = entry; 227 last_gnt_entry++; 228 splx(s); 229 } 230 231 int 232 xengnt_grant_access(domid_t dom, paddr_t ma, int ro, grant_ref_t *entryp) 233 { 234 *entryp = xengnt_get_entry(); 235 if (__predict_false(*entryp == XENGNT_NO_ENTRY)) 236 return ENOMEM; 237 238 grant_table[*entryp].frame = ma >> PAGE_SHIFT; 239 grant_table[*entryp].domid = dom; 240 /* 241 * ensure that the above values reach global visibility 242 * before permitting frame's access (done when we set flags) 243 */ 244 xen_rmb(); 245 grant_table[*entryp].flags = 246 GTF_permit_access | (ro ? GTF_readonly : 0); 247 return 0; 248 } 249 250 void 251 xengnt_revoke_access(grant_ref_t entry) 252 { 253 uint16_t flags, nflags; 254 255 nflags = grant_table[entry].flags; 256 257 do { 258 if ((flags = nflags) & (GTF_reading|GTF_writing)) 259 panic("xengnt_revoke_access: still in use"); 260 nflags = xen_atomic_cmpxchg16(&grant_table[entry].flags, 261 flags, 0); 262 } while (nflags != flags); 263 xengnt_free_entry(entry); 264 } 265 266 int 267 xengnt_grant_transfer(domid_t dom, grant_ref_t *entryp) 268 { 269 *entryp = xengnt_get_entry(); 270 if (__predict_false(*entryp == XENGNT_NO_ENTRY)) 271 return ENOMEM; 272 273 grant_table[*entryp].frame = 0; 274 grant_table[*entryp].domid = dom; 275 /* 276 * ensure that the above values reach global visibility 277 * before permitting frame's transfer (done when we set flags) 278 */ 279 xen_rmb(); 280 grant_table[*entryp].flags = GTF_accept_transfer; 281 return 0; 282 } 283 284 paddr_t 285 xengnt_revoke_transfer(grant_ref_t entry) 286 { 287 paddr_t page; 288 uint16_t flags; 289 290 /* if the transfer has not started, free the entry and return 0 */ 291 while (!((flags = grant_table[entry].flags) & GTF_transfer_committed)) { 292 if (xen_atomic_cmpxchg16(&grant_table[entry].flags, 293 flags, 0) == flags ) { 294 xengnt_free_entry(entry); 295 return 0; 296 } 297 HYPERVISOR_yield(); 298 } 299 300 /* If transfer in progress, wait for completion */ 301 while (!((flags = grant_table[entry].flags) & GTF_transfer_completed)) 302 HYPERVISOR_yield(); 303 304 /* Read the frame number /after/ reading completion status. */ 305 __insn_barrier(); 306 page = grant_table[entry].frame; 307 if (page == 0) 308 printf("xengnt_revoke_transfer: guest sent pa 0\n"); 309 310 xengnt_free_entry(entry); 311 return page; 312 } 313 314 int 315 xengnt_status(grant_ref_t entry) 316 { 317 return (grant_table[entry].flags & (GTF_reading|GTF_writing)); 318 } 319