~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to extras/mini-os/arch/ia64/xencomm.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2006 Hollis Blanchard <hollisb@us.ibm.com>, IBM Corporation
 
3
 * Tristan Gingold <tristan.gingold@bull.net>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 
18
 */
 
19
 
 
20
/*
 
21
 * This code is mostly taken from ia64-xen files xcom_mini.c and xencomm.c.
 
22
 * Changes: Dietmar Hahn <dietmar.hahn@fujitsu-siemens.com
 
23
 */
 
24
 
 
25
 
 
26
#include <mini-os/os.h>
 
27
#include <mini-os/errno.h>
 
28
#include <mini-os/lib.h>
 
29
#include <mini-os/hypervisor.h>
 
30
#include <xen/xencomm.h>
 
31
#include <xen/grant_table.h>
 
32
 
 
33
 
 
34
#define XENCOMM_MINI_ADDRS 3
 
35
struct xencomm_mini
 
36
{
 
37
        struct xencomm_desc _desc;
 
38
        uint64_t address[XENCOMM_MINI_ADDRS];
 
39
};
 
40
 
 
41
#define xen_guest_handle(hnd)  ((hnd).p)
 
42
 
 
43
struct xencomm_handle;
 
44
 
 
45
/* Translate virtual address to physical address.  */
 
46
uint64_t
 
47
xencomm_vaddr_to_paddr(uint64_t vaddr)
 
48
{
 
49
        if (IA64_RR_EXTR(vaddr) == 5)
 
50
                return KERN_VIRT_2_PHYS(vaddr);
 
51
 
 
52
        if (IA64_RR_EXTR(vaddr) == 7)
 
53
                return __pa(vaddr);
 
54
 
 
55
        return 0;
 
56
}
 
57
 
 
58
/* Inline version.  To be used only on linear space (kernel space).  */
 
59
static struct xencomm_handle *
 
60
xencomm_create_inline(void *buffer)
 
61
{
 
62
        unsigned long paddr;
 
63
 
 
64
        paddr = xencomm_vaddr_to_paddr((unsigned long)buffer);
 
65
        return (struct xencomm_handle *)(paddr | XENCOMM_INLINE_FLAG);
 
66
}
 
67
 
 
68
#define min(a,b) (((a) < (b)) ? (a) : (b))
 
69
static int
 
70
xencomm_init_desc(struct xencomm_desc *desc, void *buffer, unsigned long bytes)
 
71
{
 
72
        unsigned long recorded = 0;
 
73
        int i = 0;
 
74
 
 
75
        if ((buffer == NULL) && (bytes > 0))
 
76
                BUG();
 
77
 
 
78
        /* record the physical pages used */
 
79
        if (buffer == NULL)
 
80
                desc->nr_addrs = 0;
 
81
 
 
82
        while ((recorded < bytes) && (i < desc->nr_addrs)) {
 
83
                unsigned long vaddr = (unsigned long)buffer + recorded;
 
84
                unsigned long paddr;
 
85
                int offset;
 
86
                int chunksz;
 
87
 
 
88
                offset = vaddr % PAGE_SIZE; /* handle partial pages */
 
89
                chunksz = min(PAGE_SIZE - offset, bytes - recorded);
 
90
 
 
91
                paddr = xencomm_vaddr_to_paddr(vaddr);
 
92
                if (paddr == ~0UL) {
 
93
                        printk("%s: couldn't translate vaddr %lx\n",
 
94
                               __func__, vaddr);
 
95
                        return -EINVAL;
 
96
                }
 
97
 
 
98
                desc->address[i++] = paddr;
 
99
                recorded += chunksz;
 
100
        }
 
101
        if (recorded < bytes) {
 
102
                printk("%s: could only translate %ld of %ld bytes\n",
 
103
                       __func__, recorded, bytes);
 
104
                return -ENOSPC;
 
105
        }
 
106
 
 
107
        /* mark remaining addresses invalid (just for safety) */
 
108
        while (i < desc->nr_addrs)
 
109
                desc->address[i++] = XENCOMM_INVALID;
 
110
        desc->magic = XENCOMM_MAGIC;
 
111
        return 0;
 
112
}
 
113
 
 
114
static void *
 
115
xencomm_alloc_mini(struct xencomm_mini *area, int *nbr_area)
 
116
{
 
117
        unsigned long base;
 
118
        unsigned int pageoffset;
 
119
 
 
120
        while (*nbr_area >= 0) {
 
121
                /* Allocate an area.  */
 
122
                (*nbr_area)--;
 
123
 
 
124
                base = (unsigned long)(area + *nbr_area);
 
125
                pageoffset = base % PAGE_SIZE; 
 
126
 
 
127
                /* If the area does not cross a page, use it.  */
 
128
                if ((PAGE_SIZE - pageoffset) >= sizeof(struct xencomm_mini))
 
129
                        return &area[*nbr_area];
 
130
        }
 
131
        /* No more area.  */
 
132
        return NULL;
 
133
}
 
134
 
 
135
int
 
136
xencomm_create_mini(struct xencomm_mini *area, int *nbr_area,
 
137
                    void *buffer, unsigned long bytes,
 
138
                    struct xencomm_handle **ret)
 
139
{
 
140
        struct xencomm_desc *desc;
 
141
        int rc;
 
142
        unsigned long res;
 
143
 
 
144
        desc = xencomm_alloc_mini(area, nbr_area);
 
145
        if (!desc)
 
146
                return -ENOMEM;
 
147
        desc->nr_addrs = XENCOMM_MINI_ADDRS;
 
148
 
 
149
        rc = xencomm_init_desc(desc, buffer, bytes);
 
150
        if (rc)
 
151
                return rc;
 
152
 
 
153
        res = xencomm_vaddr_to_paddr((unsigned long)desc);
 
154
        if (res == ~0UL)
 
155
                return -EINVAL;
 
156
 
 
157
        *ret = (struct xencomm_handle*)res;
 
158
        return 0;
 
159
}
 
160
 
 
161
static int
 
162
xencommize_mini_grant_table_op(struct xencomm_mini *xc_area, int *nbr_area,
 
163
                               unsigned int cmd, void *op, unsigned int count,
 
164
                               struct xencomm_handle **desc)
 
165
{
 
166
        struct xencomm_handle *desc1;
 
167
        unsigned int argsize=0;
 
168
        int rc;
 
169
 
 
170
        switch (cmd) {
 
171
        case GNTTABOP_map_grant_ref:
 
172
                argsize = sizeof(struct gnttab_map_grant_ref);
 
173
                break;
 
174
        case GNTTABOP_unmap_grant_ref:
 
175
                argsize = sizeof(struct gnttab_unmap_grant_ref);
 
176
                break;
 
177
        case GNTTABOP_setup_table:
 
178
        {
 
179
                struct gnttab_setup_table *setup = op;
 
180
 
 
181
                argsize = sizeof(*setup);
 
182
 
 
183
                if (count != 1)
 
184
                        return -EINVAL;
 
185
                rc = xencomm_create_mini
 
186
                        (xc_area, nbr_area,
 
187
                         (void*)(uint64_t) xen_guest_handle(setup->frame_list),
 
188
                         setup->nr_frames
 
189
                         * sizeof(*xen_guest_handle(setup->frame_list)),
 
190
                         &desc1);
 
191
                if (rc)
 
192
                        return rc;
 
193
                set_xen_guest_handle(setup->frame_list,
 
194
                                     (void *)(uint64_t)desc1);
 
195
                break;
 
196
        }
 
197
        case GNTTABOP_dump_table:
 
198
                argsize = sizeof(struct gnttab_dump_table);
 
199
                break;
 
200
        case GNTTABOP_transfer:
 
201
                argsize = sizeof(struct gnttab_transfer);
 
202
                break;
 
203
        case GNTTABOP_copy:
 
204
                argsize = sizeof(struct gnttab_copy);
 
205
                break;
 
206
        default:
 
207
                printk("%s: unknown mini grant table op %d\n", __func__, cmd);
 
208
                BUG();
 
209
        }
 
210
 
 
211
        rc = xencomm_create_mini(xc_area, nbr_area, op, count * argsize, desc);
 
212
 
 
213
        return rc;
 
214
}
 
215
 
 
216
static inline int
 
217
xencomm_arch_hypercall_grant_table_op(unsigned int cmd,
 
218
                                      struct xencomm_handle *uop,
 
219
                                      unsigned int count)
 
220
{
 
221
        return _hypercall3(int, grant_table_op, cmd, uop, count);
 
222
}
 
223
 
 
224
int
 
225
xencomm_mini_hypercall_grant_table_op(unsigned int cmd, void *op,
 
226
                                      unsigned int count)
 
227
{
 
228
        int rc;
 
229
        struct xencomm_handle *desc;
 
230
        int nbr_area = 2;
 
231
        struct xencomm_mini xc_area[2];
 
232
 
 
233
        rc = xencommize_mini_grant_table_op(xc_area, &nbr_area,
 
234
                                            cmd, op, count, &desc);
 
235
        if (rc)
 
236
                return rc;
 
237
        return xencomm_arch_hypercall_grant_table_op(cmd, desc, count);
 
238
}
 
239
 
 
240
static void
 
241
gnttab_map_grant_ref_pre(struct gnttab_map_grant_ref *uop)
 
242
{
 
243
        uint32_t flags;
 
244
 
 
245
        flags = uop->flags;
 
246
 
 
247
        if (flags & GNTMAP_host_map) {
 
248
                if (flags & GNTMAP_application_map) {
 
249
                        printk("GNTMAP_application_map is not supported yet: "
 
250
                               "flags 0x%x\n", flags);
 
251
                        BUG();
 
252
                }
 
253
                if (flags & GNTMAP_contains_pte) {
 
254
                        printk("GNTMAP_contains_pte is not supported yet flags "
 
255
                               "0x%x\n", flags);
 
256
                        BUG();
 
257
                }
 
258
        } else if (flags & GNTMAP_device_map) {
 
259
                printk("GNTMAP_device_map is not supported yet 0x%x\n", flags);
 
260
                BUG();//XXX not yet. actually this flag is not used.
 
261
        } else {
 
262
                BUG();
 
263
        }
 
264
}
 
265
 
 
266
int
 
267
HYPERVISOR_grant_table_op(unsigned int cmd, void *uop, unsigned int count)
 
268
{
 
269
        if (cmd == GNTTABOP_map_grant_ref) {
 
270
                unsigned int i;
 
271
                for (i = 0; i < count; i++) {
 
272
                        gnttab_map_grant_ref_pre(
 
273
                                (struct gnttab_map_grant_ref*)uop + i);
 
274
                }
 
275
        }
 
276
        return xencomm_mini_hypercall_grant_table_op(cmd, uop, count);
 
277
}
 
278
 
 
279
        /* In fw.S */
 
280
extern int xencomm_arch_hypercall_suspend(struct xencomm_handle *arg);
 
281
int
 
282
HYPERVISOR_suspend(unsigned long srec)
 
283
{
 
284
        struct sched_shutdown arg;
 
285
 
 
286
        arg.reason = (uint32_t)SHUTDOWN_suspend;
 
287
 
 
288
        return xencomm_arch_hypercall_suspend(xencomm_create_inline(&arg));
 
289
}
 
290
 
 
291
int
 
292
HYPERVISOR_event_channel_op(int cmd, void *arg)
 
293
{
 
294
        int rc;
 
295
        struct xencomm_handle *newArg;
 
296
 
 
297
        newArg = xencomm_create_inline(arg);
 
298
        rc = _hypercall2(int, event_channel_op, cmd, newArg);
 
299
        if (unlikely(rc == -ENOSYS)) {
 
300
                struct evtchn_op op;
 
301
 
 
302
                op.cmd = cmd;
 
303
                memcpy(&op.u, arg, sizeof(op.u));
 
304
                rc = _hypercall1(int, event_channel_op_compat, &op);
 
305
        }
 
306
        return rc;
 
307
}
 
308
 
 
309
static int
 
310
xencomm_arch_xen_version(int cmd, struct xencomm_handle *arg)
 
311
{
 
312
        return _hypercall2(int, xen_version, cmd, arg);
 
313
}
 
314
 
 
315
static int
 
316
xencomm_arch_xen_feature(int cmd, struct xencomm_handle *arg)
 
317
{
 
318
        struct xencomm_handle *newArg;
 
319
 
 
320
        newArg = xencomm_create_inline(arg);
 
321
        return _hypercall2(int, xen_version, cmd, newArg);
 
322
}
 
323
 
 
324
int
 
325
HYPERVISOR_xen_version(int cmd, void *arg)
 
326
{
 
327
        switch(cmd) {
 
328
                case XENVER_version:
 
329
                        return xencomm_arch_xen_version(cmd, 0);
 
330
                case XENVER_get_features:
 
331
                        return xencomm_arch_xen_feature(cmd, arg);
 
332
                default:
 
333
                        return -1;
 
334
        }
 
335
}
 
336
 
 
337
int
 
338
HYPERVISOR_console_io(int cmd, int count, char *str)
 
339
{
 
340
        struct xencomm_handle *newStr;
 
341
 
 
342
        newStr = xencomm_create_inline(str);
 
343
        return _hypercall3(int, console_io, cmd, count, newStr);
 
344
}
 
345
 
 
346
int
 
347
HYPERVISOR_sched_op_compat(int cmd, unsigned long arg)
 
348
{
 
349
        return _hypercall2(int, sched_op_compat, cmd, arg);
 
350
}
 
351
 
 
352
int
 
353
HYPERVISOR_sched_op(int cmd, void *arg)
 
354
{
 
355
        struct xencomm_handle *newArg;
 
356
 
 
357
        newArg = xencomm_create_inline(arg);
 
358
        return _hypercall2(int, sched_op, cmd, newArg);
 
359
}
 
360
 
 
361
int
 
362
HYPERVISOR_callback_op(int cmd, void *arg)
 
363
{
 
364
        struct xencomm_handle *newArg;
 
365
 
 
366
        newArg = xencomm_create_inline(arg);
 
367
        return _hypercall2(int, callback_op, cmd, newArg);
 
368
}
 
369
 
 
370
int
 
371
HYPERVISOR_opt_feature(void *arg)
 
372
{
 
373
        struct xencomm_handle *new_arg;
 
374
 
 
375
        new_arg = xencomm_create_inline(arg);
 
376
 
 
377
        return _hypercall1(int, opt_feature, new_arg);
 
378
}
 
379
 
 
380
int
 
381
HYPERVISOR_shutdown(unsigned int reason)
 
382
{
 
383
        struct sched_shutdown sched_shutdown = {
 
384
                .reason = reason
 
385
        };
 
386
 
 
387
        int rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &sched_shutdown);
 
388
 
 
389
        if (rc == -ENOSYS)
 
390
                rc = HYPERVISOR_sched_op_compat(SCHEDOP_shutdown, reason);
 
391
 
 
392
        return rc;
 
393
}
 
394