X-Git-Url: https://git.karo-electronics.de/?a=blobdiff_plain;f=drivers%2Flguest%2Fhypercalls.c;h=0f2cb4fd7c6980448e01770476e262d6ea708d70;hb=2bf86b7aa8e74bf81a9872f7b610f49b610a4649;hp=ea52ca451f74f78dce4dec85f1e0b39551615fd9;hpb=2fe83b3ad12d43799af5f3156886eca443a88bac;p=mv-sheeva.git diff --git a/drivers/lguest/hypercalls.c b/drivers/lguest/hypercalls.c index ea52ca451f7..0f2cb4fd7c6 100644 --- a/drivers/lguest/hypercalls.c +++ b/drivers/lguest/hypercalls.c @@ -1,5 +1,10 @@ -/* Actual hypercalls, which allow guests to actually do something. - Copyright (C) 2006 Rusty Russell IBM Corporation +/*P:500 Just as userspace programs request kernel operations through a system + * call, the Guest requests Host operations through a "hypercall". You might + * notice this nomenclature doesn't really follow any logic, but the name has + * been around for long enough that we're stuck with it. As you'd expect, this + * code is basically a one big switch statement. :*/ + +/* Copyright (C) 2006 Rusty Russell IBM Corporation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,175 +23,222 @@ #include #include #include +#include #include #include -#include #include "lg.h" -static void do_hcall(struct lguest *lg, struct lguest_regs *regs) +/*H:120 This is the core hypercall routine: where the Guest gets what it wants. + * Or gets killed. Or, in the case of LHCALL_CRASH, both. */ +static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args) { - switch (regs->eax) { + switch (args->arg0) { case LHCALL_FLUSH_ASYNC: + /* This call does nothing, except by breaking out of the Guest + * it makes us process all the asynchronous hypercalls. */ break; case LHCALL_LGUEST_INIT: - kill_guest(lg, "already have lguest_data"); + /* You can't get here unless you're already initialized. Don't + * do that. */ + kill_guest(cpu, "already have lguest_data"); break; - case LHCALL_CRASH: { + case LHCALL_SHUTDOWN: { + /* Shutdown is such a trivial hypercall that we do it in four + * lines right here. */ char msg[128]; - lgread(lg, msg, regs->edx, sizeof(msg)); + /* If the lgread fails, it will call kill_guest() itself; the + * kill_guest() with the message will be ignored. */ + __lgread(cpu, msg, args->arg1, sizeof(msg)); msg[sizeof(msg)-1] = '\0'; - kill_guest(lg, "CRASH: %s", msg); + kill_guest(cpu, "CRASH: %s", msg); + if (args->arg2 == LGUEST_SHUTDOWN_RESTART) + cpu->lg->dead = ERR_PTR(-ERESTART); break; } case LHCALL_FLUSH_TLB: - if (regs->edx) - guest_pagetable_clear_all(lg); + /* FLUSH_TLB comes in two flavors, depending on the + * argument: */ + if (args->arg1) + guest_pagetable_clear_all(cpu); else - guest_pagetable_flush_user(lg); - break; - case LHCALL_GET_WALLCLOCK: { - struct timespec ts; - ktime_get_real_ts(&ts); - regs->eax = ts.tv_sec; - break; - } - case LHCALL_BIND_DMA: - regs->eax = bind_dma(lg, regs->edx, regs->ebx, - regs->ecx >> 8, regs->ecx & 0xFF); - break; - case LHCALL_SEND_DMA: - send_dma(lg, regs->edx, regs->ebx); - break; - case LHCALL_LOAD_GDT: - load_guest_gdt(lg, regs->edx, regs->ebx); - break; - case LHCALL_LOAD_IDT_ENTRY: - load_guest_idt_entry(lg, regs->edx, regs->ebx, regs->ecx); + guest_pagetable_flush_user(cpu); break; + + /* All these calls simply pass the arguments through to the right + * routines. */ case LHCALL_NEW_PGTABLE: - guest_new_pagetable(lg, regs->edx); + guest_new_pagetable(cpu, args->arg1); break; case LHCALL_SET_STACK: - guest_set_stack(lg, regs->edx, regs->ebx, regs->ecx); + guest_set_stack(cpu, args->arg1, args->arg2, args->arg3); break; case LHCALL_SET_PTE: - guest_set_pte(lg, regs->edx, regs->ebx, mkgpte(regs->ecx)); + guest_set_pte(cpu, args->arg1, args->arg2, __pte(args->arg3)); break; case LHCALL_SET_PMD: - guest_set_pmd(lg, regs->edx, regs->ebx); - break; - case LHCALL_LOAD_TLS: - guest_load_tls(lg, regs->edx); + guest_set_pmd(cpu->lg, args->arg1, args->arg2); break; case LHCALL_SET_CLOCKEVENT: - guest_set_clockevent(lg, regs->edx); + guest_set_clockevent(cpu, args->arg1); break; case LHCALL_TS: - lg->ts = regs->edx; + /* This sets the TS flag, as we saw used in run_guest(). */ + cpu->ts = args->arg1; break; case LHCALL_HALT: - lg->halted = 1; + /* Similarly, this sets the halted flag for run_guest(). */ + cpu->halted = 1; + break; + case LHCALL_NOTIFY: + cpu->pending_notify = args->arg1; break; default: - kill_guest(lg, "Bad hypercall %li\n", regs->eax); + /* It should be an architecture-specific hypercall. */ + if (lguest_arch_do_hcall(cpu, args)) + kill_guest(cpu, "Bad hypercall %li\n", args->arg0); } } - -/* We always do queued calls before actual hypercall. */ -static void do_async_hcalls(struct lguest *lg) +/*:*/ + +/*H:124 Asynchronous hypercalls are easy: we just look in the array in the + * Guest's "struct lguest_data" to see if any new ones are marked "ready". + * + * We are careful to do these in order: obviously we respect the order the + * Guest put them in the ring, but we also promise the Guest that they will + * happen before any normal hypercall (which is why we check this before + * checking for a normal hcall). */ +static void do_async_hcalls(struct lg_cpu *cpu) { unsigned int i; u8 st[LHCALL_RING_SIZE]; - if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st))) + /* For simplicity, we copy the entire call status array in at once. */ + if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st))) return; + /* We process "struct lguest_data"s hcalls[] ring once. */ for (i = 0; i < ARRAY_SIZE(st); i++) { - struct lguest_regs regs; - unsigned int n = lg->next_hcall; + struct hcall_args args; + /* We remember where we were up to from last time. This makes + * sure that the hypercalls are done in the order the Guest + * places them in the ring. */ + unsigned int n = cpu->next_hcall; + /* 0xFF means there's no call here (yet). */ if (st[n] == 0xFF) break; - if (++lg->next_hcall == LHCALL_RING_SIZE) - lg->next_hcall = 0; + /* OK, we have hypercall. Increment the "next_hcall" cursor, + * and wrap back to 0 if we reach the end. */ + if (++cpu->next_hcall == LHCALL_RING_SIZE) + cpu->next_hcall = 0; - if (get_user(regs.eax, &lg->lguest_data->hcalls[n].eax) - || get_user(regs.edx, &lg->lguest_data->hcalls[n].edx) - || get_user(regs.ecx, &lg->lguest_data->hcalls[n].ecx) - || get_user(regs.ebx, &lg->lguest_data->hcalls[n].ebx)) { - kill_guest(lg, "Fetching async hypercalls"); + /* Copy the hypercall arguments into a local copy of + * the hcall_args struct. */ + if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n], + sizeof(struct hcall_args))) { + kill_guest(cpu, "Fetching async hypercalls"); break; } - do_hcall(lg, ®s); - if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) { - kill_guest(lg, "Writing result for async hypercall"); + /* Do the hypercall, same as a normal one. */ + do_hcall(cpu, &args); + + /* Mark the hypercall done. */ + if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) { + kill_guest(cpu, "Writing result for async hypercall"); break; } - if (lg->dma_is_pending) + /* Stop doing hypercalls if they want to notify the Launcher: + * it needs to service this first. */ + if (cpu->pending_notify) break; } } -static void initialize(struct lguest *lg) +/* Last of all, we look at what happens first of all. The very first time the + * Guest makes a hypercall, we end up here to set things up: */ +static void initialize(struct lg_cpu *cpu) { - u32 tsc_speed; - - if (lg->regs->eax != LHCALL_LGUEST_INIT) { - kill_guest(lg, "hypercall %li before LGUEST_INIT", - lg->regs->eax); + /* You can't do anything until you're initialized. The Guest knows the + * rules, so we're unforgiving here. */ + if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) { + kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0); return; } - /* We only tell the guest to use the TSC if it's reliable. */ - if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) && !check_tsc_unstable()) - tsc_speed = tsc_khz; - else - tsc_speed = 0; + if (lguest_arch_init_hypercalls(cpu)) + kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); - lg->lguest_data = (struct lguest_data __user *)lg->regs->edx; - /* We check here so we can simply copy_to_user/from_user */ - if (!lguest_address_ok(lg, lg->regs->edx, sizeof(*lg->lguest_data))) { - kill_guest(lg, "bad guest page %p", lg->lguest_data); - return; - } - if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start) - || get_user(lg->noirq_end, &lg->lguest_data->noirq_end) - /* We reserve the top pgd entry. */ - || put_user(4U*1024*1024, &lg->lguest_data->reserve_mem) - || put_user(tsc_speed, &lg->lguest_data->tsc_khz) - || put_user(lg->guestid, &lg->lguest_data->guestid)) - kill_guest(lg, "bad guest page %p", lg->lguest_data); - - /* This is the one case where the above accesses might have - * been the first write to a Guest page. This may have caused - * a copy-on-write fault, but the Guest might be referring to - * the old (read-only) page. */ - guest_pagetable_clear_all(lg); -} + /* The Guest tells us where we're not to deliver interrupts by putting + * the range of addresses into "struct lguest_data". */ + if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start) + || get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end)) + kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data); -/* Even if we go out to userspace and come back, we don't want to do - * the hypercall again. */ -static void clear_hcall(struct lguest *lg) -{ - lg->regs->trapnum = 255; + /* We write the current time into the Guest's data page once so it can + * set its clock. */ + write_timestamp(cpu); + + /* page_tables.c will also do some setup. */ + page_table_guest_data_init(cpu); + + /* This is the one case where the above accesses might have been the + * first write to a Guest page. This may have caused a copy-on-write + * fault, but the old page might be (read-only) in the Guest + * pagetable. */ + guest_pagetable_clear_all(cpu); } -void do_hypercalls(struct lguest *lg) +/*H:100 + * Hypercalls + * + * Remember from the Guest, hypercalls come in two flavors: normal and + * asynchronous. This file handles both of types. + */ +void do_hypercalls(struct lg_cpu *cpu) { - if (unlikely(!lg->lguest_data)) { - if (lg->regs->trapnum == LGUEST_TRAP_ENTRY) { - initialize(lg); - clear_hcall(lg); - } + /* Not initialized yet? This hypercall must do it. */ + if (unlikely(!cpu->lg->lguest_data)) { + /* Set up the "struct lguest_data" */ + initialize(cpu); + /* Hcall is done. */ + cpu->hcall = NULL; return; } - do_async_hcalls(lg); - if (!lg->dma_is_pending && lg->regs->trapnum == LGUEST_TRAP_ENTRY) { - do_hcall(lg, lg->regs); - clear_hcall(lg); + /* The Guest has initialized. + * + * Look in the hypercall ring for the async hypercalls: */ + do_async_hcalls(cpu); + + /* If we stopped reading the hypercall ring because the Guest did a + * NOTIFY to the Launcher, we want to return now. Otherwise we do + * the hypercall. */ + if (!cpu->pending_notify) { + do_hcall(cpu, cpu->hcall); + /* Tricky point: we reset the hcall pointer to mark the + * hypercall as "done". We use the hcall pointer rather than + * the trap number to indicate a hypercall is pending. + * Normally it doesn't matter: the Guest will run again and + * update the trap number before we come back here. + * + * However, if we are signalled or the Guest sends I/O to the + * Launcher, the run_guest() loop will exit without running the + * Guest. When it comes back it would try to re-run the + * hypercall. */ + cpu->hcall = NULL; } } + +/* This routine supplies the Guest with time: it's used for wallclock time at + * initial boot and as a rough time source if the TSC isn't available. */ +void write_timestamp(struct lg_cpu *cpu) +{ + struct timespec now; + ktime_get_real_ts(&now); + if (copy_to_user(&cpu->lg->lguest_data->time, + &now, sizeof(struct timespec))) + kill_guest(cpu, "Writing timestamp"); +}