direct-io.hg

view extras/mini-os/gnttab.c @ 15481:538c3d8aa4b1

Revert 15471:7ac7f147241405af83e7a9d748cf7b01279734fc

Block-device specifiers in ioemu can contain colons, so skipping
always past the first colon is not a good idea. Better solutions are
in the pipeline to solve the blktap issues.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Fri Jul 06 15:01:20 2007 +0100 (2007-07-06)
parents d6ca67c0645e
children
line source
1 /*
2 ****************************************************************************
3 * (C) 2006 - Cambridge University
4 ****************************************************************************
5 *
6 * File: gnttab.c
7 * Author: Steven Smith (sos22@cam.ac.uk)
8 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
9 *
10 * Date: July 2006
11 *
12 * Environment: Xen Minimal OS
13 * Description: Simple grant tables implementation. About as stupid as it's
14 * possible to be and still work.
15 *
16 ****************************************************************************
17 */
18 #include <os.h>
19 #include <mm.h>
20 #include <gnttab.h>
22 #define NR_RESERVED_ENTRIES 8
24 /* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
25 #ifdef __ia64__
26 #define NR_GRANT_FRAMES 1
27 #else
28 #define NR_GRANT_FRAMES 4
29 #endif
30 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(grant_entry_t))
32 static grant_entry_t *gnttab_table;
33 static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
35 static void
36 put_free_entry(grant_ref_t ref)
37 {
38 gnttab_list[ref] = gnttab_list[0];
39 gnttab_list[0] = ref;
41 }
43 static grant_ref_t
44 get_free_entry(void)
45 {
46 unsigned int ref = gnttab_list[0];
47 gnttab_list[0] = gnttab_list[ref];
48 return ref;
49 }
51 grant_ref_t
52 gnttab_grant_access(domid_t domid, unsigned long frame, int readonly)
53 {
54 grant_ref_t ref;
56 ref = get_free_entry();
57 gnttab_table[ref].frame = frame;
58 gnttab_table[ref].domid = domid;
59 wmb();
60 readonly *= GTF_readonly;
61 gnttab_table[ref].flags = GTF_permit_access | readonly;
63 return ref;
64 }
66 grant_ref_t
67 gnttab_grant_transfer(domid_t domid, unsigned long pfn)
68 {
69 grant_ref_t ref;
71 ref = get_free_entry();
72 gnttab_table[ref].frame = pfn;
73 gnttab_table[ref].domid = domid;
74 wmb();
75 gnttab_table[ref].flags = GTF_accept_transfer;
77 return ref;
78 }
80 int
81 gnttab_end_access(grant_ref_t ref)
82 {
83 u16 flags, nflags;
85 nflags = gnttab_table[ref].flags;
86 do {
87 if ((flags = nflags) & (GTF_reading|GTF_writing)) {
88 printk("WARNING: g.e. still in use!\n");
89 return 0;
90 }
91 } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
92 flags);
94 put_free_entry(ref);
95 return 1;
96 }
98 unsigned long
99 gnttab_end_transfer(grant_ref_t ref)
100 {
101 unsigned long frame;
102 u16 flags;
104 while (!((flags = gnttab_table[ref].flags) & GTF_transfer_committed)) {
105 if (synch_cmpxchg(&gnttab_table[ref].flags, flags, 0) == flags) {
106 printk("Release unused transfer grant.\n");
107 put_free_entry(ref);
108 return 0;
109 }
110 }
112 /* If a transfer is in progress then wait until it is completed. */
113 while (!(flags & GTF_transfer_completed)) {
114 flags = gnttab_table[ref].flags;
115 }
117 /* Read the frame number /after/ reading completion status. */
118 rmb();
119 frame = gnttab_table[ref].frame;
121 put_free_entry(ref);
123 return frame;
124 }
126 grant_ref_t
127 gnttab_alloc_and_grant(void **map)
128 {
129 unsigned long mfn;
130 grant_ref_t gref;
132 *map = (void *)alloc_page();
133 mfn = virt_to_mfn(*map);
134 gref = gnttab_grant_access(0, mfn, 0);
135 return gref;
136 }
138 static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
140 const char *
141 gnttabop_error(int16_t status)
142 {
143 status = -status;
144 if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
145 return "bad status";
146 else
147 return gnttabop_error_msgs[status];
148 }
150 void
151 init_gnttab(void)
152 {
153 struct gnttab_setup_table setup;
154 unsigned long frames[NR_GRANT_FRAMES];
155 int i;
157 for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
158 put_free_entry(i);
160 setup.dom = DOMID_SELF;
161 setup.nr_frames = NR_GRANT_FRAMES;
162 set_xen_guest_handle(setup.frame_list, frames);
164 HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
165 gnttab_table = map_frames(frames, NR_GRANT_FRAMES);
166 printk("gnttab_table mapped at %p.\n", gnttab_table);
167 }