aboutsummaryrefslogtreecommitdiffstats
path: root/xen/arch/x86/hvm/hvm.c
blob: f4dee16ecc52c3d24a4aeed95dd58409993c391f (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * hvm.c: Common hardware virtual machine abstractions.
 *
 * Copyright (c) 2004, Intel Corporation.
 * Copyright (c) 2005, International Business Machines Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 */

#include <xen/config.h>
#include <xen/init.h>
#include <xen/lib.h>
#include <xen/trace.h>
#include <xen/sched.h>
#include <xen/irq.h>
#include <xen/softirq.h>
#include <xen/domain.h>
#include <xen/domain_page.h>
#include <xen/hypercall.h>
#include <asm/current.h>
#include <asm/io.h>
#include <asm/shadow.h>
#include <asm/regs.h>
#include <asm/cpufeature.h>
#include <asm/processor.h>
#include <asm/types.h>
#include <asm/msr.h>
#include <asm/spinlock.h>
#include <asm/hvm/hvm.h>
#include <asm/hvm/support.h>
#include <asm/shadow.h>
#if CONFIG_PAGING_LEVELS >= 3
#include <asm/shadow_64.h>
#endif
#include <public/sched.h>
#include <public/hvm/ioreq.h>
#include <public/hvm/hvm_info_table.h>

int hvm_enabled = 0;

unsigned int opt_hvm_debug_level = 0;
integer_param("hvm_debug", opt_hvm_debug_level);

struct hvm_function_table hvm_funcs;

static void hvm_zap_mmio_range(
    struct domain *d, unsigned long pfn, unsigned long nr_pfn)
{
    unsigned long i, val = INVALID_MFN;

    for ( i = 0; i < nr_pfn; i++ )
    {
        if ( pfn + i >= 0xfffff )
            break;

        __copy_to_user(&phys_to_machine_mapping[pfn + i], &val, sizeof (val));
    }
}

static void hvm_map_io_shared_page(struct domain *d)
{
    int i;
    unsigned char e820_map_nr;
    struct e820entry *e820entry;
    unsigned char *p;
    unsigned long mfn;
    unsigned long gpfn = 0;

    local_flush_tlb_pge();

    mfn = get_mfn_from_gpfn(E820_MAP_PAGE >> PAGE_SHIFT);
    if (mfn == INVALID_MFN) {
        printk("Can not find E820 memory map page for HVM domain.\n");
        domain_crash_synchronous();
    }

    p = map_domain_page(mfn);
    if (p == NULL) {
        printk("Can not map E820 memory map page for HVM domain.\n");
        domain_crash_synchronous();
    }

    e820_map_nr = *(p + E820_MAP_NR_OFFSET);
    e820entry = (struct e820entry *)(p + E820_MAP_OFFSET);

    for ( i = 0; i < e820_map_nr; i++ )
    {
        if ( e820entry[i].type == E820_SHARED_PAGE )
            gpfn = (e820entry[i].addr >> PAGE_SHIFT);
        if ( e820entry[i].type == E820_IO )
            hvm_zap_mmio_range(
                d, 
                e820entry[i].addr >> PAGE_SHIFT,
                e820entry[i].size >> PAGE_SHIFT);
    }

    if ( gpfn == 0 ) {
        printk("Can not get io request shared page"
               " from E820 memory map for HVM domain.\n");
        unmap_domain_page(p);
        domain_crash_synchronous();
    }
    unmap_domain_page(p);

    /* Initialise shared page */
    mfn = get_mfn_from_gpfn(gpfn);
    if (mfn == INVALID_MFN) {
        printk("Can not find io request shared page for HVM domain.\n");
        domain_crash_synchronous();
    }

    p = map_domain_page_global(mfn);
    if (p == NULL) {
        printk("Can not map io request shared page for HVM domain.\n");
        domain_crash_synchronous();
    }
    d->arch.hvm_domain.shared_page_va = (unsigned long)p;
}

static int validate_hvm_info(struct hvm_info_table *t)
{
    char signature[] = "HVM INFO";
    uint8_t *ptr = (uint8_t *)t;
    uint8_t sum = 0;
    int i;

    /* strncmp(t->signature, "HVM INFO", 8) */
    for ( i = 0; i < 8; i++ ) {
        if ( signature[i] != t->signature[i] ) {
            printk("Bad hvm info signature\n");
            return 0;
        }
    }

    for ( i = 0; i < t->length; i++ )
        sum += ptr[i];

    return (sum == 0);
}

static void hvm_get_info(struct domain *d)
{
    unsigned char *p;
    unsigned long mfn;
    struct hvm_info_table *t;

    mfn = get_mfn_from_gpfn(HVM_INFO_PFN);
    if ( mfn == INVALID_MFN ) {
        printk("Can not get info page mfn for HVM domain.\n");
        domain_crash_synchronous();
    }

    p = map_domain_page(mfn);
    if ( p == NULL ) {
        printk("Can not map info page for HVM domain.\n");
        domain_crash_synchronous();
    }

    t = (struct hvm_info_table *)(p + HVM_INFO_OFFSET);

    if ( validate_hvm_info(t) ) {
        d->arch.hvm_domain.nr_vcpus = t->nr_vcpus;
        d->arch.hvm_domain.apic_enabled = t->apic_enabled;
        d->arch.hvm_domain.pae_enabled = t->pae_enabled;
    } else {
        printk("Bad hvm info table\n");
        d->arch.hvm_domain.nr_vcpus = 1;
        d->arch.hvm_domain.apic_enabled = 0;
        d->arch.hvm_domain.pae_enabled = 0;
    }

    unmap_domain_page(p);
}

void hvm_setup_platform(struct domain* d)
{
    struct hvm_domain *platform;
    struct vcpu *v=current;

    if ( !hvm_guest(v) || (v->vcpu_id != 0) )
        return;

    if ( shadow_direct_map_init(d) == 0 )
    {
        printk("Can not allocate shadow direct map for HVM domain.\n");
        domain_crash_synchronous();
    }

    hvm_map_io_shared_page(d);
    hvm_get_info(d);

    platform = &d->arch.hvm_domain;
    pic_init(&platform->vpic, pic_irq_request, &platform->interrupt_request);
    register_pic_io_hook();

    if ( hvm_apic_support(d) )
    {
        spin_lock_init(&d->arch.hvm_domain.round_robin_lock);
        hvm_vioapic_init(d);
    }

    init_timer(&platform->pl_time.periodic_tm.timer, pt_timer_fn, v, v->processor);
    pit_init(v, cpu_khz);
}

void pic_irq_request(void *data, int level)
{
    int *interrupt_request = data;
    *interrupt_request = level;
}

void hvm_pic_assist(struct vcpu *v)
{
    global_iodata_t *spg;
    u16   *virq_line, irqs;
    struct hvm_virpic *pic = &v->domain->arch.hvm_domain.vpic;

    spg = &get_sp(v->domain)->sp_global;
    virq_line  = &spg->pic_clear_irr;
    if ( *virq_line ) {
        do {
            irqs = *(volatile u16*)virq_line;
        } while ( (u16)cmpxchg(virq_line,irqs, 0) != irqs );
        do_pic_irqs_clear(pic, irqs);
    }
    virq_line  = &spg->pic_irr;
    if ( *virq_line ) {
        do {
            irqs = *(volatile u16*)virq_line;
        } while ( (u16)cmpxchg(virq_line,irqs, 0) != irqs );
        do_pic_irqs(pic, irqs);
    }
}

u64 hvm_get_guest_time(struct vcpu *v)
{
    u64    host_tsc;
    
    rdtscll(host_tsc);
    return host_tsc + v->arch.hvm_vcpu.cache_tsc_offset;
}

int cpu_get_interrupt(struct vcpu *v, int *type)
{
    int intno;
    struct hvm_virpic *s = &v->domain->arch.hvm_domain.vpic;
    unsigned long flags;

    if ( (intno = cpu_get_apic_interrupt(v, type)) != -1 ) {
        /* set irq request if a PIC irq is still pending */
        /* XXX: improve that */
        spin_lock_irqsave(&s->lock, flags);
        pic_update_irq(s);
        spin_unlock_irqrestore(&s->lock, flags);
        return intno;
    }
    /* read the irq from the PIC */
    if ( v->vcpu_id == 0 && (intno = cpu_get_pic_interrupt(v, type)) != -1 )
        return intno;

    return -1;
}

/*
 * Copy from/to guest virtual.
 */
int
hvm_copy(void *buf, unsigned long vaddr, int size, int dir)
{
    unsigned long gpa, mfn;
    char *addr;
    int count;

    while (size > 0) {
        count = PAGE_SIZE - (vaddr & ~PAGE_MASK);
        if (count > size)
            count = size;

        if (hvm_paging_enabled(current)) {
            gpa = gva_to_gpa(vaddr);
            mfn = get_mfn_from_gpfn(gpa >> PAGE_SHIFT);
        } else
            mfn = get_mfn_from_gpfn(vaddr >> PAGE_SHIFT);
        if (mfn == INVALID_MFN)
            return 0;

        addr = (char *)map_domain_page(mfn) + (vaddr & ~PAGE_MASK);

        if (dir == HVM_COPY_IN)
            memcpy(buf, addr, count);
        else
            memcpy(addr, buf, count);

        unmap_domain_page(addr);

        vaddr += count;
        buf += count;
        size -= count;
    }

    return 1;
}

/*
 * HVM specific printbuf. Mostly used for hvmloader chit-chat.
 */
void hvm_print_line(struct vcpu *v, const char c)
{
    int *index = &v->domain->arch.hvm_domain.pbuf_index;
    char *pbuf = v->domain->arch.hvm_domain.pbuf;

    if (*index == HVM_PBUF_SIZE-2 || c == '\n') {
        if (*index == HVM_PBUF_SIZE-2)
	    pbuf[(*index)++] = c;
        pbuf[*index] = '\0';
        printk("(GUEST: %u) %s\n", v->domain->domain_id, pbuf);
	*index = 0;
    } else
	pbuf[(*index)++] = c;
}

#if defined(__i386__)

typedef unsigned long hvm_hypercall_t(
    unsigned long, unsigned long, unsigned long, unsigned long, unsigned long);
#define HYPERCALL(x) [ __HYPERVISOR_ ## x ] = (hvm_hypercall_t *) do_ ## x
static hvm_hypercall_handler *hvm_hypercall_table[] = {
    HYPERCALL(mmu_update),
    HYPERCALL(memory_op),
    HYPERCALL(multicall),
    HYPERCALL(update_va_mapping),
    HYPERCALL(event_channel_op_compat),
    HYPERCALL(xen_version),
    HYPERCALL(grant_table_op),
    HYPERCALL(event_channel_op),
    HYPERCALL(hvm_op)
};
#undef HYPERCALL

void hvm_do_hypercall(struct cpu_user_regs *pregs)
{
    if ( ring_3(pregs) )
    {
        pregs->eax = -EPERM;
        return;
    }

    if ( pregs->eax > ARRAY_SIZE(hvm_hypercall_table) ||
         !hvm_hypercall_table[pregs->eax] )
    {
        DPRINTK("HVM vcpu %d:%d did a bad hypercall %d.\n",
                current->domain->domain_id, current->vcpu_id,
                pregs->eax);
        pregs->eax = -ENOSYS;
    }
    else
    {
        pregs->eax = hvm_hypercall_table[pregs->eax](
            pregs->ebx, pregs->ecx, pregs->edx, pregs->esi, pregs->edi);
    }
}

#else /* __x86_64__ */

void hvm_do_hypercall(struct cpu_user_regs *pregs)
{
    printk("not supported yet!\n");
}

#endif

/* Initialise a hypercall transfer page for a VMX domain using
   paravirtualised drivers. */
void hvm_hypercall_page_initialise(struct domain *d,
                                   void *hypercall_page)
{
    hvm_funcs.init_hypercall_page(d, hypercall_page);
}


/*
 * only called in HVM domain BSP context
 * when booting, vcpuid is always equal to apic_id
 */
int hvm_bringup_ap(int vcpuid, int trampoline_vector)
{
    struct vcpu *bsp = current, *v;
    struct domain *d = bsp->domain;
    struct vcpu_guest_context *ctxt;
    int rc = 0;

    /* current must be HVM domain BSP */
    if ( !(hvm_guest(bsp) && bsp->vcpu_id == 0) ) {
        printk("Not calling hvm_bringup_ap from BSP context.\n");
        domain_crash_synchronous();
    }

    if ( (v = d->vcpu[vcpuid]) == NULL )
        return -ENOENT;

    if ( (ctxt = xmalloc(struct vcpu_guest_context)) == NULL ) {
        printk("Failed to allocate memory in hvm_bringup_ap.\n");
        return -ENOMEM;
    }

    hvm_init_ap_context(ctxt, vcpuid, trampoline_vector);

    LOCK_BIGLOCK(d);
    rc = -EEXIST;
    if ( !test_bit(_VCPUF_initialised, &v->vcpu_flags) )
        rc = boot_vcpu(d, vcpuid, ctxt);
    UNLOCK_BIGLOCK(d);

    if ( rc != 0 )
        printk("AP %d bringup failed in boot_vcpu %x.\n", vcpuid, rc);
    else {
        if ( test_and_clear_bit(_VCPUF_down, &d->vcpu[vcpuid]->vcpu_flags) )
            vcpu_wake(d->vcpu[vcpuid]);
        printk("AP %d bringup suceeded.\n", vcpuid);
    }

    xfree(ctxt);

    return rc;
}

/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */