aboutsummaryrefslogtreecommitdiffstats
path: root/linux-2.6.11-xen-sparse/arch/xen/kernel/gnttab.c
blob: 0106a6fea04b1f7e075d89cd98fc95e15aab9b5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/******************************************************************************
 * gnttab.c
 * 
 * Two sets of functionality:
 * 1. Granting foreign access to our memory reservation.
 * 2. Accessing others' memory reservations via grant references.
 * (i.e., mechanisms for both sender and recipient of grant references)
 * 
 * Copyright (c) 2005, Christopher Clark
 * Copyright (c) 2004, K A Fraser
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <asm/pgtable.h>
#include <asm/fixmap.h>
#include <asm/uaccess.h>
#include <asm-xen/xen_proc.h>
#include <asm-xen/linux-public/privcmd.h>
#include <asm-xen/gnttab.h>
#include <asm/synch_bitops.h>

#if 1
#define ASSERT(_p) \
    if ( !(_p) ) { printk(KERN_ALERT"Assertion '%s': line %d, file %s\n", \
    #_p , __LINE__, __FILE__); *(int*)0=0; }
#else
#define ASSERT(_p) ((void)0)
#endif

#define WPRINTK(fmt, args...) \
    printk(KERN_WARNING "xen_grant: " fmt, ##args)


EXPORT_SYMBOL(gnttab_grant_foreign_access);
EXPORT_SYMBOL(gnttab_end_foreign_access);
EXPORT_SYMBOL(gnttab_query_foreign_access);
EXPORT_SYMBOL(gnttab_grant_foreign_transfer);
EXPORT_SYMBOL(gnttab_end_foreign_transfer);
EXPORT_SYMBOL(gnttab_alloc_grant_references);
EXPORT_SYMBOL(gnttab_free_grant_references);
EXPORT_SYMBOL(gnttab_claim_grant_reference);
EXPORT_SYMBOL(gnttab_release_grant_reference);
EXPORT_SYMBOL(gnttab_grant_foreign_access_ref);
EXPORT_SYMBOL(gnttab_grant_foreign_transfer_ref);

static grant_ref_t gnttab_free_list[NR_GRANT_ENTRIES];
static grant_ref_t gnttab_free_head;

static grant_entry_t *shared;

/*
 * Lock-free grant-entry allocator
 */

static inline int
get_free_entry(
    void)
{
    grant_ref_t fh, nfh = gnttab_free_head;
    do { if ( unlikely((fh = nfh) == NR_GRANT_ENTRIES) ) return -1; }
    while ( unlikely((nfh = cmpxchg(&gnttab_free_head, fh,
                                    gnttab_free_list[fh])) != fh) );
    return fh;
}

static inline void
put_free_entry(
    grant_ref_t ref)
{
    grant_ref_t fh, nfh = gnttab_free_head;
    do { gnttab_free_list[ref] = fh = nfh; wmb(); }
    while ( unlikely((nfh = cmpxchg(&gnttab_free_head, fh, ref)) != fh) );
}

/*
 * Public grant-issuing interface functions
 */

int
gnttab_grant_foreign_access(
    domid_t domid, unsigned long frame, int readonly)
{
    int ref;
    
    if ( unlikely((ref = get_free_entry()) == -1) )
        return -ENOSPC;

    shared[ref].frame = frame;
    shared[ref].domid = domid;
    wmb();
    shared[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0);

    return ref;
}

void
gnttab_grant_foreign_access_ref(
    grant_ref_t ref, domid_t domid, unsigned long frame, int readonly)
{
    shared[ref].frame = frame;
    shared[ref].domid = domid;
    wmb();
    shared[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0);
}


int
gnttab_query_foreign_access( grant_ref_t ref )
{
    u16 nflags;

    nflags = shared[ref].flags;

    return ( nflags & (GTF_reading|GTF_writing) );
}

void
gnttab_end_foreign_access( grant_ref_t ref, int readonly )
{
    u16 flags, nflags;

    nflags = shared[ref].flags;
    do {
        if ( (flags = nflags) & (GTF_reading|GTF_writing) )
            printk(KERN_ALERT "WARNING: g.e. still in use!\n");
    }
    while ( (nflags = synch_cmpxchg(&shared[ref].flags, flags, 0)) != flags );

    put_free_entry(ref);
}

int
gnttab_grant_foreign_transfer(
    domid_t domid, unsigned long pfn )
{
    int ref;

    if ( unlikely((ref = get_free_entry()) == -1) )
        return -ENOSPC;

    shared[ref].frame = pfn;
    shared[ref].domid = domid;
    wmb();
    shared[ref].flags = GTF_accept_transfer;

    return ref;
}

void
gnttab_grant_foreign_transfer_ref(
    grant_ref_t ref, domid_t domid, unsigned long pfn )
{
    shared[ref].frame = pfn;
    shared[ref].domid = domid;
    wmb();
    shared[ref].flags = GTF_accept_transfer;
}

unsigned long
gnttab_end_foreign_transfer(
    grant_ref_t ref)
{
    unsigned long frame = 0;
    u16           flags;

    flags = shared[ref].flags;
    ASSERT(flags == (GTF_accept_transfer | GTF_transfer_committed));

    /*
     * If a transfer is committed then wait for the frame address to appear.
     * Otherwise invalidate the grant entry against future use.
     */
    if ( likely(flags != GTF_accept_transfer) ||
         (synch_cmpxchg(&shared[ref].flags, flags, 0) != GTF_accept_transfer) )
        while ( unlikely((frame = shared[ref].frame) == 0) )
            cpu_relax();

    put_free_entry(ref);

    return frame;
}

void
gnttab_free_grant_references( u16 count, grant_ref_t head )
{
    /* TODO: O(N)...? */
    grant_ref_t to_die = 0, next = head;
    int i;

    for ( i = 0; i < count; i++ )
    {
        to_die = next;
        next = gnttab_free_list[next];
        put_free_entry( to_die );
    }
}

int
gnttab_alloc_grant_references( u16 count,
                               grant_ref_t *head,
                               grant_ref_t *terminal )
{
    int i;
    grant_ref_t h = gnttab_free_head;

    for ( i = 0; i < count; i++ )
        if ( unlikely(get_free_entry() == -1) )
            goto not_enough_refs;

    *head = h;
    *terminal = gnttab_free_head;

    return 0;

not_enough_refs:
    gnttab_free_head = h;
    return -ENOSPC;
}

int
gnttab_claim_grant_reference( grant_ref_t *private_head,
                              grant_ref_t  terminal )
{
    grant_ref_t g;
    if ( unlikely((g = *private_head) == terminal) )
        return -ENOSPC;
    *private_head = gnttab_free_list[g];
    return g;
}

void
gnttab_release_grant_reference( grant_ref_t *private_head,
                                grant_ref_t  release )
{
    gnttab_free_list[release] = *private_head;
    *private_head = release;
}

/*
 * ProcFS operations
 */

#ifdef CONFIG_PROC_FS

static struct proc_dir_entry *grant_pde;

static int grant_ioctl(struct inode *inode, struct file *file,
                       unsigned int cmd, unsigned long data)
{
    int                     ret;
    privcmd_hypercall_t     hypercall;

    /* XXX Need safety checks here if using for anything other
     *     than debugging */
    return -ENOSYS;

    if ( cmd != IOCTL_PRIVCMD_HYPERCALL )
        return -ENOSYS;

    if ( copy_from_user(&hypercall, (void *)data, sizeof(hypercall)) )
        return -EFAULT;

    if ( hypercall.op != __HYPERVISOR_grant_table_op )
        return -ENOSYS;

    /* hypercall-invoking asm taken from privcmd.c */
    __asm__ __volatile__ (
        "pushl %%ebx; pushl %%ecx; pushl %%edx; pushl %%esi; pushl %%edi; "
        "movl  4(%%eax),%%ebx ;"
        "movl  8(%%eax),%%ecx ;"
        "movl 12(%%eax),%%edx ;"
        "movl 16(%%eax),%%esi ;"
        "movl 20(%%eax),%%edi ;"
        "movl   (%%eax),%%eax ;"
        TRAP_INSTR "; "
        "popl %%edi; popl %%esi; popl %%edx; popl %%ecx; popl %%ebx"
        : "=a" (ret) : "0" (&hypercall) : "memory" );

    return ret;
}

static struct file_operations grant_file_ops = {
    ioctl:  grant_ioctl,
};

static int grant_read(char *page, char **start, off_t off,
                      int count, int *eof, void *data)
{
    int             len;
    unsigned int    i;
    grant_entry_t  *gt;

    gt = (grant_entry_t *)shared;
    len = 0;

    for ( i = 0; i < NR_GRANT_ENTRIES; i++ )
        /* TODO: safety catch here until this can handle >PAGE_SIZE output */
        if (len > (PAGE_SIZE - 200))
        {
            len += sprintf( page + len, "Truncated.\n");
            break;
        }

        if ( gt[i].flags )
            len += sprintf( page + len,
                    "Grant: ref (0x%x) flags (0x%hx) dom (0x%hx) frame (0x%x)\n", 
                    i,
                    gt[i].flags,
                    gt[i].domid,
                    gt[i].frame );

    *eof = 1;
    return len;
}

static int grant_write(struct file *file, const char __user *buffer,
                       unsigned long count, void *data)
{
    /* TODO: implement this */
    return -ENOSYS;
}

#endif /* CONFIG_PROC_FS */

int gnttab_resume(void)
{
    gnttab_setup_table_t setup;
    unsigned long        frames[NR_GRANT_FRAMES];
    int                  i;

    setup.dom        = DOMID_SELF;
    setup.nr_frames  = NR_GRANT_FRAMES;
    setup.frame_list = frames;

    BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1) != 0);
    BUG_ON(setup.status != 0);

    for ( i = 0; i < NR_GRANT_FRAMES; i++ )
        set_fixmap(FIX_GNTTAB_END - i, frames[i] << PAGE_SHIFT);

    return 0;
}

int gnttab_suspend(void)
{
    int i;

    for ( i = 0; i < NR_GRANT_FRAMES; i++ )
	clear_fixmap(FIX_GNTTAB_END - i);

    return 0;
}

static int __init gnttab_init(void)
{
    int i;

    BUG_ON(gnttab_resume());

    shared = (grant_entry_t *)fix_to_virt(FIX_GNTTAB_END);

    for ( i = 0; i < NR_GRANT_ENTRIES; i++ )
        gnttab_free_list[i] = i + 1;
    
#ifdef CONFIG_PROC_FS
    /*
     *  /proc/xen/grant : used by libxc to access grant tables
     */
    if ( (grant_pde = create_xen_proc_entry("grant", 0600)) == NULL )
    {
        WPRINTK("Unable to create grant xen proc entry\n");
        return -1;
    }

    grant_file_ops.read   = grant_pde->proc_fops->read;
    grant_file_ops.write  = grant_pde->proc_fops->write;

    grant_pde->proc_fops  = &grant_file_ops;

    grant_pde->read_proc  = &grant_read;
    grant_pde->write_proc = &grant_write;
#endif

    printk("Grant table initialized\n");
    return 0;
}

__initcall(gnttab_init);