/*P:5001* Just as userspace programs request kernel operations through a system2* call, the Guest requests Host operations through a "hypercall". You might3* notice this nomenclature doesn't really follow any logic, but the name has4* been around for long enough that we're stuck with it. As you'd expect, this5* code is basically a one big switch statement.6:*/78/* Copyright (C) 2006 Rusty Russell IBM Corporation910This program is free software; you can redistribute it and/or modify11it under the terms of the GNU General Public License as published by12the Free Software Foundation; either version 2 of the License, or13(at your option) any later version.1415This program is distributed in the hope that it will be useful,16but WITHOUT ANY WARRANTY; without even the implied warranty of17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the18GNU General Public License for more details.1920You should have received a copy of the GNU General Public License21along with this program; if not, write to the Free Software22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA23*/24#include <linux/uaccess.h>25#include <linux/syscalls.h>26#include <linux/mm.h>27#include <linux/ktime.h>28#include <asm/page.h>29#include <asm/pgtable.h>30#include "lg.h"3132/*H:12033* This is the core hypercall routine: where the Guest gets what it wants.34* Or gets killed. Or, in the case of LHCALL_SHUTDOWN, both.35*/36static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)37{38switch (args->arg0) {39case LHCALL_FLUSH_ASYNC:40/*41* This call does nothing, except by breaking out of the Guest42* it makes us process all the asynchronous hypercalls.43*/44break;45case LHCALL_SEND_INTERRUPTS:46/*47* This call does nothing too, but by breaking out of the Guest48* it makes us process any pending interrupts.49*/50break;51case LHCALL_LGUEST_INIT:52/*53* You can't get here unless you're already initialized. Don't54* do that.55*/56kill_guest(cpu, "already have lguest_data");57break;58case LHCALL_SHUTDOWN: {59char msg[128];60/*61* Shutdown is such a trivial hypercall that we do it in five62* lines right here.63*64* If the lgread fails, it will call kill_guest() itself; the65* kill_guest() with the message will be ignored.66*/67__lgread(cpu, msg, args->arg1, sizeof(msg));68msg[sizeof(msg)-1] = '\0';69kill_guest(cpu, "CRASH: %s", msg);70if (args->arg2 == LGUEST_SHUTDOWN_RESTART)71cpu->lg->dead = ERR_PTR(-ERESTART);72break;73}74case LHCALL_FLUSH_TLB:75/* FLUSH_TLB comes in two flavors, depending on the argument: */76if (args->arg1)77guest_pagetable_clear_all(cpu);78else79guest_pagetable_flush_user(cpu);80break;8182/*83* All these calls simply pass the arguments through to the right84* routines.85*/86case LHCALL_NEW_PGTABLE:87guest_new_pagetable(cpu, args->arg1);88break;89case LHCALL_SET_STACK:90guest_set_stack(cpu, args->arg1, args->arg2, args->arg3);91break;92case LHCALL_SET_PTE:93#ifdef CONFIG_X86_PAE94guest_set_pte(cpu, args->arg1, args->arg2,95__pte(args->arg3 | (u64)args->arg4 << 32));96#else97guest_set_pte(cpu, args->arg1, args->arg2, __pte(args->arg3));98#endif99break;100case LHCALL_SET_PGD:101guest_set_pgd(cpu->lg, args->arg1, args->arg2);102break;103#ifdef CONFIG_X86_PAE104case LHCALL_SET_PMD:105guest_set_pmd(cpu->lg, args->arg1, args->arg2);106break;107#endif108case LHCALL_SET_CLOCKEVENT:109guest_set_clockevent(cpu, args->arg1);110break;111case LHCALL_TS:112/* This sets the TS flag, as we saw used in run_guest(). */113cpu->ts = args->arg1;114break;115case LHCALL_HALT:116/* Similarly, this sets the halted flag for run_guest(). */117cpu->halted = 1;118break;119case LHCALL_NOTIFY:120cpu->pending_notify = args->arg1;121break;122default:123/* It should be an architecture-specific hypercall. */124if (lguest_arch_do_hcall(cpu, args))125kill_guest(cpu, "Bad hypercall %li\n", args->arg0);126}127}128129/*H:124130* Asynchronous hypercalls are easy: we just look in the array in the131* Guest's "struct lguest_data" to see if any new ones are marked "ready".132*133* We are careful to do these in order: obviously we respect the order the134* Guest put them in the ring, but we also promise the Guest that they will135* happen before any normal hypercall (which is why we check this before136* checking for a normal hcall).137*/138static void do_async_hcalls(struct lg_cpu *cpu)139{140unsigned int i;141u8 st[LHCALL_RING_SIZE];142143/* For simplicity, we copy the entire call status array in at once. */144if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st)))145return;146147/* We process "struct lguest_data"s hcalls[] ring once. */148for (i = 0; i < ARRAY_SIZE(st); i++) {149struct hcall_args args;150/*151* We remember where we were up to from last time. This makes152* sure that the hypercalls are done in the order the Guest153* places them in the ring.154*/155unsigned int n = cpu->next_hcall;156157/* 0xFF means there's no call here (yet). */158if (st[n] == 0xFF)159break;160161/*162* OK, we have hypercall. Increment the "next_hcall" cursor,163* and wrap back to 0 if we reach the end.164*/165if (++cpu->next_hcall == LHCALL_RING_SIZE)166cpu->next_hcall = 0;167168/*169* Copy the hypercall arguments into a local copy of the170* hcall_args struct.171*/172if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n],173sizeof(struct hcall_args))) {174kill_guest(cpu, "Fetching async hypercalls");175break;176}177178/* Do the hypercall, same as a normal one. */179do_hcall(cpu, &args);180181/* Mark the hypercall done. */182if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) {183kill_guest(cpu, "Writing result for async hypercall");184break;185}186187/*188* Stop doing hypercalls if they want to notify the Launcher:189* it needs to service this first.190*/191if (cpu->pending_notify)192break;193}194}195196/*197* Last of all, we look at what happens first of all. The very first time the198* Guest makes a hypercall, we end up here to set things up:199*/200static void initialize(struct lg_cpu *cpu)201{202/*203* You can't do anything until you're initialized. The Guest knows the204* rules, so we're unforgiving here.205*/206if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {207kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0);208return;209}210211if (lguest_arch_init_hypercalls(cpu))212kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);213214/*215* The Guest tells us where we're not to deliver interrupts by putting216* the range of addresses into "struct lguest_data".217*/218if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start)219|| get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end))220kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);221222/*223* We write the current time into the Guest's data page once so it can224* set its clock.225*/226write_timestamp(cpu);227228/* page_tables.c will also do some setup. */229page_table_guest_data_init(cpu);230231/*232* This is the one case where the above accesses might have been the233* first write to a Guest page. This may have caused a copy-on-write234* fault, but the old page might be (read-only) in the Guest235* pagetable.236*/237guest_pagetable_clear_all(cpu);238}239/*:*/240241/*M:013242* If a Guest reads from a page (so creates a mapping) that it has never243* written to, and then the Launcher writes to it (ie. the output of a virtual244* device), the Guest will still see the old page. In practice, this never245* happens: why would the Guest read a page which it has never written to? But246* a similar scenario might one day bite us, so it's worth mentioning.247*248* Note that if we used a shared anonymous mapping in the Launcher instead of249* mapping /dev/zero private, we wouldn't worry about cop-on-write. And we250* need that to switch the Launcher to processes (away from threads) anyway.251:*/252253/*H:100254* Hypercalls255*256* Remember from the Guest, hypercalls come in two flavors: normal and257* asynchronous. This file handles both of types.258*/259void do_hypercalls(struct lg_cpu *cpu)260{261/* Not initialized yet? This hypercall must do it. */262if (unlikely(!cpu->lg->lguest_data)) {263/* Set up the "struct lguest_data" */264initialize(cpu);265/* Hcall is done. */266cpu->hcall = NULL;267return;268}269270/*271* The Guest has initialized.272*273* Look in the hypercall ring for the async hypercalls:274*/275do_async_hcalls(cpu);276277/*278* If we stopped reading the hypercall ring because the Guest did a279* NOTIFY to the Launcher, we want to return now. Otherwise we do280* the hypercall.281*/282if (!cpu->pending_notify) {283do_hcall(cpu, cpu->hcall);284/*285* Tricky point: we reset the hcall pointer to mark the286* hypercall as "done". We use the hcall pointer rather than287* the trap number to indicate a hypercall is pending.288* Normally it doesn't matter: the Guest will run again and289* update the trap number before we come back here.290*291* However, if we are signalled or the Guest sends I/O to the292* Launcher, the run_guest() loop will exit without running the293* Guest. When it comes back it would try to re-run the294* hypercall. Finding that bug sucked.295*/296cpu->hcall = NULL;297}298}299300/*301* This routine supplies the Guest with time: it's used for wallclock time at302* initial boot and as a rough time source if the TSC isn't available.303*/304void write_timestamp(struct lg_cpu *cpu)305{306struct timespec now;307ktime_get_real_ts(&now);308if (copy_to_user(&cpu->lg->lguest_data->time,309&now, sizeof(struct timespec)))310kill_guest(cpu, "Writing timestamp");311}312313314