debuggers.hg

view extras/mini-os/gnttab.c @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children 5c0bf00e371d
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>
21 #include <semaphore.h>
23 #define NR_RESERVED_ENTRIES 8
25 /* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
26 #ifdef __ia64__
27 #define NR_GRANT_FRAMES 1
28 #else
29 #define NR_GRANT_FRAMES 4
30 #endif
31 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(grant_entry_t))
33 static grant_entry_t *gnttab_table;
34 static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
35 #ifdef GNT_DEBUG
36 static char inuse[NR_GRANT_ENTRIES];
37 #endif
38 static __DECLARE_SEMAPHORE_GENERIC(gnttab_sem, NR_GRANT_ENTRIES);
40 static void
41 put_free_entry(grant_ref_t ref)
42 {
43 unsigned long flags;
44 local_irq_save(flags);
45 #ifdef GNT_DEBUG
46 BUG_ON(!inuse[ref]);
47 inuse[ref] = 0;
48 #endif
49 gnttab_list[ref] = gnttab_list[0];
50 gnttab_list[0] = ref;
51 local_irq_restore(flags);
52 up(&gnttab_sem);
53 }
55 static grant_ref_t
56 get_free_entry(void)
57 {
58 unsigned int ref;
59 unsigned long flags;
60 down(&gnttab_sem);
61 local_irq_save(flags);
62 ref = gnttab_list[0];
63 gnttab_list[0] = gnttab_list[ref];
64 #ifdef GNT_DEBUG
65 BUG_ON(inuse[ref]);
66 inuse[ref] = 1;
67 #endif
68 local_irq_restore(flags);
69 return ref;
70 }
72 grant_ref_t
73 gnttab_grant_access(domid_t domid, unsigned long frame, int readonly)
74 {
75 grant_ref_t ref;
77 ref = get_free_entry();
78 gnttab_table[ref].frame = frame;
79 gnttab_table[ref].domid = domid;
80 wmb();
81 readonly *= GTF_readonly;
82 gnttab_table[ref].flags = GTF_permit_access | readonly;
84 return ref;
85 }
87 grant_ref_t
88 gnttab_grant_transfer(domid_t domid, unsigned long pfn)
89 {
90 grant_ref_t ref;
92 ref = get_free_entry();
93 gnttab_table[ref].frame = pfn;
94 gnttab_table[ref].domid = domid;
95 wmb();
96 gnttab_table[ref].flags = GTF_accept_transfer;
98 return ref;
99 }
101 int
102 gnttab_end_access(grant_ref_t ref)
103 {
104 u16 flags, nflags;
106 BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
108 nflags = gnttab_table[ref].flags;
109 do {
110 if ((flags = nflags) & (GTF_reading|GTF_writing)) {
111 printk("WARNING: g.e. still in use! (%x)\n", flags);
112 return 0;
113 }
114 } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
115 flags);
117 put_free_entry(ref);
118 return 1;
119 }
121 unsigned long
122 gnttab_end_transfer(grant_ref_t ref)
123 {
124 unsigned long frame;
125 u16 flags;
127 BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
129 while (!((flags = gnttab_table[ref].flags) & GTF_transfer_committed)) {
130 if (synch_cmpxchg(&gnttab_table[ref].flags, flags, 0) == flags) {
131 printk("Release unused transfer grant.\n");
132 put_free_entry(ref);
133 return 0;
134 }
135 }
137 /* If a transfer is in progress then wait until it is completed. */
138 while (!(flags & GTF_transfer_completed)) {
139 flags = gnttab_table[ref].flags;
140 }
142 /* Read the frame number /after/ reading completion status. */
143 rmb();
144 frame = gnttab_table[ref].frame;
146 put_free_entry(ref);
148 return frame;
149 }
151 grant_ref_t
152 gnttab_alloc_and_grant(void **map)
153 {
154 unsigned long mfn;
155 grant_ref_t gref;
157 *map = (void *)alloc_page();
158 mfn = virt_to_mfn(*map);
159 gref = gnttab_grant_access(0, mfn, 0);
160 return gref;
161 }
163 static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
165 const char *
166 gnttabop_error(int16_t status)
167 {
168 status = -status;
169 if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
170 return "bad status";
171 else
172 return gnttabop_error_msgs[status];
173 }
175 void
176 init_gnttab(void)
177 {
178 struct gnttab_setup_table setup;
179 unsigned long frames[NR_GRANT_FRAMES];
180 int i;
182 #ifdef GNT_DEBUG
183 memset(inuse, 1, sizeof(inuse));
184 #endif
185 for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
186 put_free_entry(i);
188 setup.dom = DOMID_SELF;
189 setup.nr_frames = NR_GRANT_FRAMES;
190 set_xen_guest_handle(setup.frame_list, frames);
192 HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
193 gnttab_table = map_frames(frames, NR_GRANT_FRAMES);
194 printk("gnttab_table mapped at %p.\n", gnttab_table);
195 }