Commit d145c725 authored by Linus Torvalds's avatar Linus Torvalds

Merge git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus

* git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus: (27 commits)
  lguest: use __PAGE_KERNEL instead of _PAGE_KERNEL
  lguest: Use explicit includes rateher than indirect
  lguest: get rid of lg variable assignments
  lguest: change gpte_addr header
  lguest: move changed bitmap to lg_cpu
  lguest: move last_pages to lg_cpu
  lguest: change last_guest to last_cpu
  lguest: change spte_addr header
  lguest: per-vcpu lguest pgdir management
  lguest: make pending notifications per-vcpu
  lguest: makes special fields be per-vcpu
  lguest: per-vcpu lguest task management
  lguest: replace lguest_arch with lg_cpu_arch.
  lguest: make registers per-vcpu
  lguest: make emulate_insn receive a vcpu struct.
  lguest: map_switcher_in_guest() per-vcpu
  lguest: per-vcpu interrupt processing.
  lguest: per-vcpu lguest timers
  lguest: make hypercalls use the vcpu struct
  lguest: make write() operation smp aware
  ...

Manual conflict resolved (maybe even correctly, who knows) in
drivers/lguest/x86/core.c
parents 44c3b591 84f12e39
...@@ -79,6 +79,9 @@ static void *guest_base; ...@@ -79,6 +79,9 @@ static void *guest_base;
/* The maximum guest physical address allowed, and maximum possible. */ /* The maximum guest physical address allowed, and maximum possible. */
static unsigned long guest_limit, guest_max; static unsigned long guest_limit, guest_max;
/* a per-cpu variable indicating whose vcpu is currently running */
static unsigned int __thread cpu_id;
/* This is our list of devices. */ /* This is our list of devices. */
struct device_list struct device_list
{ {
...@@ -153,6 +156,9 @@ struct virtqueue ...@@ -153,6 +156,9 @@ struct virtqueue
void (*handle_output)(int fd, struct virtqueue *me); void (*handle_output)(int fd, struct virtqueue *me);
}; };
/* Remember the arguments to the program so we can "reboot" */
static char **main_args;
/* Since guest is UP and we don't run at the same time, we don't need barriers. /* Since guest is UP and we don't run at the same time, we don't need barriers.
* But I include them in the code in case others copy it. */ * But I include them in the code in case others copy it. */
#define wmb() #define wmb()
...@@ -554,7 +560,7 @@ static void wake_parent(int pipefd, int lguest_fd) ...@@ -554,7 +560,7 @@ static void wake_parent(int pipefd, int lguest_fd)
else else
FD_CLR(-fd - 1, &devices.infds); FD_CLR(-fd - 1, &devices.infds);
} else /* Send LHREQ_BREAK command. */ } else /* Send LHREQ_BREAK command. */
write(lguest_fd, args, sizeof(args)); pwrite(lguest_fd, args, sizeof(args), cpu_id);
} }
} }
...@@ -1489,7 +1495,9 @@ static void setup_block_file(const char *filename) ...@@ -1489,7 +1495,9 @@ static void setup_block_file(const char *filename)
/* Create stack for thread and run it */ /* Create stack for thread and run it */
stack = malloc(32768); stack = malloc(32768);
if (clone(io_thread, stack + 32768, CLONE_VM, dev) == -1) /* SIGCHLD - We dont "wait" for our cloned thread, so prevent it from
* becoming a zombie. */
if (clone(io_thread, stack + 32768, CLONE_VM | SIGCHLD, dev) == -1)
err(1, "Creating clone"); err(1, "Creating clone");
/* We don't need to keep the I/O thread's end of the pipes open. */ /* We don't need to keep the I/O thread's end of the pipes open. */
...@@ -1499,7 +1507,21 @@ static void setup_block_file(const char *filename) ...@@ -1499,7 +1507,21 @@ static void setup_block_file(const char *filename)
verbose("device %u: virtblock %llu sectors\n", verbose("device %u: virtblock %llu sectors\n",
devices.device_num, cap); devices.device_num, cap);
} }
/* That's the end of device setup. */ /* That's the end of device setup. :*/
/* Reboot */
static void __attribute__((noreturn)) restart_guest(void)
{
unsigned int i;
/* Closing pipes causes the waker thread and io_threads to die, and
* closing /dev/lguest cleans up the Guest. Since we don't track all
* open fds, we simply close everything beyond stderr. */
for (i = 3; i < FD_SETSIZE; i++)
close(i);
execv(main_args[0], main_args);
err(1, "Could not exec %s", main_args[0]);
}
/*L:220 Finally we reach the core of the Launcher, which runs the Guest, serves /*L:220 Finally we reach the core of the Launcher, which runs the Guest, serves
* its input and output, and finally, lays it to rest. */ * its input and output, and finally, lays it to rest. */
...@@ -1511,7 +1533,8 @@ static void __attribute__((noreturn)) run_guest(int lguest_fd) ...@@ -1511,7 +1533,8 @@ static void __attribute__((noreturn)) run_guest(int lguest_fd)
int readval; int readval;
/* We read from the /dev/lguest device to run the Guest. */ /* We read from the /dev/lguest device to run the Guest. */
readval = read(lguest_fd, &notify_addr, sizeof(notify_addr)); readval = pread(lguest_fd, &notify_addr,
sizeof(notify_addr), cpu_id);
/* One unsigned long means the Guest did HCALL_NOTIFY */ /* One unsigned long means the Guest did HCALL_NOTIFY */
if (readval == sizeof(notify_addr)) { if (readval == sizeof(notify_addr)) {
...@@ -1521,16 +1544,23 @@ static void __attribute__((noreturn)) run_guest(int lguest_fd) ...@@ -1521,16 +1544,23 @@ static void __attribute__((noreturn)) run_guest(int lguest_fd)
/* ENOENT means the Guest died. Reading tells us why. */ /* ENOENT means the Guest died. Reading tells us why. */
} else if (errno == ENOENT) { } else if (errno == ENOENT) {
char reason[1024] = { 0 }; char reason[1024] = { 0 };
read(lguest_fd, reason, sizeof(reason)-1); pread(lguest_fd, reason, sizeof(reason)-1, cpu_id);
errx(1, "%s", reason); errx(1, "%s", reason);
/* ERESTART means that we need to reboot the guest */
} else if (errno == ERESTART) {
restart_guest();
/* EAGAIN means the Waker wanted us to look at some input. /* EAGAIN means the Waker wanted us to look at some input.
* Anything else means a bug or incompatible change. */ * Anything else means a bug or incompatible change. */
} else if (errno != EAGAIN) } else if (errno != EAGAIN)
err(1, "Running guest failed"); err(1, "Running guest failed");
/* Only service input on thread for CPU 0. */
if (cpu_id != 0)
continue;
/* Service input, then unset the BREAK to release the Waker. */ /* Service input, then unset the BREAK to release the Waker. */
handle_input(lguest_fd); handle_input(lguest_fd);
if (write(lguest_fd, args, sizeof(args)) < 0) if (pwrite(lguest_fd, args, sizeof(args), cpu_id) < 0)
err(1, "Resetting break"); err(1, "Resetting break");
} }
} }
...@@ -1571,6 +1601,12 @@ int main(int argc, char *argv[]) ...@@ -1571,6 +1601,12 @@ int main(int argc, char *argv[])
/* If they specify an initrd file to load. */ /* If they specify an initrd file to load. */
const char *initrd_name = NULL; const char *initrd_name = NULL;
/* Save the args: we "reboot" by execing ourselves again. */
main_args = argv;
/* We don't "wait" for the children, so prevent them from becoming
* zombies. */
signal(SIGCHLD, SIG_IGN);
/* First we initialize the device list. Since console and network /* First we initialize the device list. Since console and network
* device receive input from a file descriptor, we keep an fdset * device receive input from a file descriptor, we keep an fdset
* (infds) and the maximum fd number (max_infd) with the head of the * (infds) and the maximum fd number (max_infd) with the head of the
...@@ -1582,6 +1618,7 @@ int main(int argc, char *argv[]) ...@@ -1582,6 +1618,7 @@ int main(int argc, char *argv[])
devices.lastdev = &devices.dev; devices.lastdev = &devices.dev;
devices.next_irq = 1; devices.next_irq = 1;
cpu_id = 0;
/* We need to know how much memory so we can set up the device /* We need to know how much memory so we can set up the device
* descriptor and memory pages for the devices as we parse the command * descriptor and memory pages for the devices as we parse the command
* line. So we quickly look through the arguments to find the amount * line. So we quickly look through the arguments to find the amount
......
...@@ -67,6 +67,7 @@ ...@@ -67,6 +67,7 @@
#include <asm/mce.h> #include <asm/mce.h>
#include <asm/io.h> #include <asm/io.h>
#include <asm/i387.h> #include <asm/i387.h>
#include <asm/reboot.h> /* for struct machine_ops */
/*G:010 Welcome to the Guest! /*G:010 Welcome to the Guest!
* *
...@@ -813,7 +814,7 @@ static void lguest_safe_halt(void) ...@@ -813,7 +814,7 @@ static void lguest_safe_halt(void)
* rather than virtual addresses, so we use __pa() here. */ * rather than virtual addresses, so we use __pa() here. */
static void lguest_power_off(void) static void lguest_power_off(void)
{ {
hcall(LHCALL_CRASH, __pa("Power down"), 0, 0); hcall(LHCALL_SHUTDOWN, __pa("Power down"), LGUEST_SHUTDOWN_POWEROFF, 0);
} }
/* /*
...@@ -823,7 +824,7 @@ static void lguest_power_off(void) ...@@ -823,7 +824,7 @@ static void lguest_power_off(void)
*/ */
static int lguest_panic(struct notifier_block *nb, unsigned long l, void *p) static int lguest_panic(struct notifier_block *nb, unsigned long l, void *p)
{ {
hcall(LHCALL_CRASH, __pa(p), 0, 0); hcall(LHCALL_SHUTDOWN, __pa(p), LGUEST_SHUTDOWN_POWEROFF, 0);
/* The hcall won't return, but to keep gcc happy, we're "done". */ /* The hcall won't return, but to keep gcc happy, we're "done". */
return NOTIFY_DONE; return NOTIFY_DONE;
} }
...@@ -927,6 +928,11 @@ static unsigned lguest_patch(u8 type, u16 clobber, void *ibuf, ...@@ -927,6 +928,11 @@ static unsigned lguest_patch(u8 type, u16 clobber, void *ibuf,
return insn_len; return insn_len;
} }
static void lguest_restart(char *reason)
{
hcall(LHCALL_SHUTDOWN, __pa(reason), LGUEST_SHUTDOWN_RESTART, 0);
}
/*G:030 Once we get to lguest_init(), we know we're a Guest. The pv_ops /*G:030 Once we get to lguest_init(), we know we're a Guest. The pv_ops
* structures in the kernel provide points for (almost) every routine we have * structures in the kernel provide points for (almost) every routine we have
* to override to avoid privileged instructions. */ * to override to avoid privileged instructions. */
...@@ -1060,6 +1066,7 @@ __init void lguest_init(void) ...@@ -1060,6 +1066,7 @@ __init void lguest_init(void)
* the Guest routine to power off. */ * the Guest routine to power off. */
pm_power_off = lguest_power_off; pm_power_off = lguest_power_off;
machine_ops.restart = lguest_restart;
/* Now we're set up, call start_kernel() in init/main.c and we proceed /* Now we're set up, call start_kernel() in init/main.c and we proceed
* to boot as normal. It never returns. */ * to boot as normal. It never returns. */
start_kernel(); start_kernel();
......
...@@ -72,7 +72,7 @@ obj-$(CONFIG_ISDN) += isdn/ ...@@ -72,7 +72,7 @@ obj-$(CONFIG_ISDN) += isdn/
obj-$(CONFIG_EDAC) += edac/ obj-$(CONFIG_EDAC) += edac/
obj-$(CONFIG_MCA) += mca/ obj-$(CONFIG_MCA) += mca/
obj-$(CONFIG_EISA) += eisa/ obj-$(CONFIG_EISA) += eisa/
obj-$(CONFIG_LGUEST_GUEST) += lguest/ obj-y += lguest/
obj-$(CONFIG_CPU_FREQ) += cpufreq/ obj-$(CONFIG_CPU_FREQ) += cpufreq/
obj-$(CONFIG_CPU_IDLE) += cpuidle/ obj-$(CONFIG_CPU_IDLE) += cpuidle/
obj-$(CONFIG_MMC) += mmc/ obj-$(CONFIG_MMC) += mmc/
......
...@@ -151,43 +151,43 @@ int lguest_address_ok(const struct lguest *lg, ...@@ -151,43 +151,43 @@ int lguest_address_ok(const struct lguest *lg,
/* This routine copies memory from the Guest. Here we can see how useful the /* This routine copies memory from the Guest. Here we can see how useful the
* kill_lguest() routine we met in the Launcher can be: we return a random * kill_lguest() routine we met in the Launcher can be: we return a random
* value (all zeroes) instead of needing to return an error. */ * value (all zeroes) instead of needing to return an error. */
void __lgread(struct lguest *lg, void *b, unsigned long addr, unsigned bytes) void __lgread(struct lg_cpu *cpu, void *b, unsigned long addr, unsigned bytes)
{ {
if (!lguest_address_ok(lg, addr, bytes) if (!lguest_address_ok(cpu->lg, addr, bytes)
|| copy_from_user(b, lg->mem_base + addr, bytes) != 0) { || copy_from_user(b, cpu->lg->mem_base + addr, bytes) != 0) {
/* copy_from_user should do this, but as we rely on it... */ /* copy_from_user should do this, but as we rely on it... */
memset(b, 0, bytes); memset(b, 0, bytes);
kill_guest(lg, "bad read address %#lx len %u", addr, bytes); kill_guest(cpu, "bad read address %#lx len %u", addr, bytes);
} }
} }
/* This is the write (copy into guest) version. */ /* This is the write (copy into guest) version. */
void __lgwrite(struct lguest *lg, unsigned long addr, const void *b, void __lgwrite(struct lg_cpu *cpu, unsigned long addr, const void *b,
unsigned bytes) unsigned bytes)
{ {
if (!lguest_address_ok(lg, addr, bytes) if (!lguest_address_ok(cpu->lg, addr, bytes)
|| copy_to_user(lg->mem_base + addr, b, bytes) != 0) || copy_to_user(cpu->lg->mem_base + addr, b, bytes) != 0)
kill_guest(lg, "bad write address %#lx len %u", addr, bytes); kill_guest(cpu, "bad write address %#lx len %u", addr, bytes);
} }
/*:*/ /*:*/
/*H:030 Let's jump straight to the the main loop which runs the Guest. /*H:030 Let's jump straight to the the main loop which runs the Guest.
* Remember, this is called by the Launcher reading /dev/lguest, and we keep * Remember, this is called by the Launcher reading /dev/lguest, and we keep
* going around and around until something interesting happens. */ * going around and around until something interesting happens. */
int run_guest(struct lguest *lg, unsigned long __user *user) int run_guest(struct lg_cpu *cpu, unsigned long __user *user)
{ {
/* We stop running once the Guest is dead. */ /* We stop running once the Guest is dead. */
while (!lg->dead) { while (!cpu->lg->dead) {
/* First we run any hypercalls the Guest wants done. */ /* First we run any hypercalls the Guest wants done. */
if (lg->hcall) if (cpu->hcall)
do_hypercalls(lg); do_hypercalls(cpu);
/* It's possible the Guest did a NOTIFY hypercall to the /* It's possible the Guest did a NOTIFY hypercall to the
* Launcher, in which case we return from the read() now. */ * Launcher, in which case we return from the read() now. */
if (lg->pending_notify) { if (cpu->pending_notify) {
if (put_user(lg->pending_notify, user)) if (put_user(cpu->pending_notify, user))
return -EFAULT; return -EFAULT;
return sizeof(lg->pending_notify); return sizeof(cpu->pending_notify);
} }
/* Check for signals */ /* Check for signals */
...@@ -195,13 +195,13 @@ int run_guest(struct lguest *lg, unsigned long __user *user) ...@@ -195,13 +195,13 @@ int run_guest(struct lguest *lg, unsigned long __user *user)
return -ERESTARTSYS; return -ERESTARTSYS;
/* If Waker set break_out, return to Launcher. */ /* If Waker set break_out, return to Launcher. */
if (lg->break_out) if (cpu->break_out)
return -EAGAIN; return -EAGAIN;
/* Check if there are any interrupts which can be delivered /* Check if there are any interrupts which can be delivered
* now: if so, this sets up the hander to be executed when we * now: if so, this sets up the hander to be executed when we
* next run the Guest. */ * next run the Guest. */
maybe_do_interrupt(lg); maybe_do_interrupt(cpu);
/* All long-lived kernel loops need to check with this horrible /* All long-lived kernel loops need to check with this horrible
* thing called the freezer. If the Host is trying to suspend, * thing called the freezer. If the Host is trying to suspend,
...@@ -210,12 +210,12 @@ int run_guest(struct lguest *lg, unsigned long __user *user) ...@@ -210,12 +210,12 @@ int run_guest(struct lguest *lg, unsigned long __user *user)
/* Just make absolutely sure the Guest is still alive. One of /* Just make absolutely sure the Guest is still alive. One of
* those hypercalls could have been fatal, for example. */ * those hypercalls could have been fatal, for example. */
if (lg->dead) if (cpu->lg->dead)
break; break;
/* If the Guest asked to be stopped, we sleep. The Guest's /* If the Guest asked to be stopped, we sleep. The Guest's
* clock timer or LHCALL_BREAK from the Waker will wake us. */ * clock timer or LHCALL_BREAK from the Waker will wake us. */
if (lg->halted) { if (cpu->halted) {
set_current_state(TASK_INTERRUPTIBLE); set_current_state(TASK_INTERRUPTIBLE);
schedule(); schedule();
continue; continue;
...@@ -226,15 +226,17 @@ int run_guest(struct lguest *lg, unsigned long __user *user) ...@@ -226,15 +226,17 @@ int run_guest(struct lguest *lg, unsigned long __user *user)
local_irq_disable(); local_irq_disable();
/* Actually run the Guest until something happens. */ /* Actually run the Guest until something happens. */
lguest_arch_run_guest(lg); lguest_arch_run_guest(cpu);
/* Now we're ready to be interrupted or moved to other CPUs */ /* Now we're ready to be interrupted or moved to other CPUs */
local_irq_enable(); local_irq_enable();
/* Now we deal with whatever happened to the Guest. */ /* Now we deal with whatever happened to the Guest. */
lguest_arch_handle_trap(lg); lguest_arch_handle_trap(cpu);
} }
if (cpu->lg->dead == ERR_PTR(-ERESTART))
return -ERESTART;
/* The Guest is dead => "No such file or directory" */ /* The Guest is dead => "No such file or directory" */
return -ENOENT; return -ENOENT;
} }
...@@ -253,7 +255,7 @@ static int __init init(void) ...@@ -253,7 +255,7 @@ static int __init init(void)
/* Lguest can't run under Xen, VMI or itself. It does Tricky Stuff. */ /* Lguest can't run under Xen, VMI or itself. It does Tricky Stuff. */
if (paravirt_enabled()) { if (paravirt_enabled()) {
printk("lguest is afraid of %s\n", pv_info.name); printk("lguest is afraid of being a guest\n");
return -EPERM; return -EPERM;
} }
......
...@@ -23,13 +23,14 @@ ...@@ -23,13 +23,14 @@
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/syscalls.h> #include <linux/syscalls.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/ktime.h>
#include <asm/page.h> #include <asm/page.h>
#include <asm/pgtable.h> #include <asm/pgtable.h>
#include "lg.h" #include "lg.h"
/*H:120 This is the core hypercall routine: where the Guest gets what it wants. /*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. */ * Or gets killed. Or, in the case of LHCALL_CRASH, both. */
static void do_hcall(struct lguest *lg, struct hcall_args *args) static void do_hcall(struct lg_cpu *cpu, struct hcall_args *args)
{ {
switch (args->arg0) { switch (args->arg0) {
case LHCALL_FLUSH_ASYNC: case LHCALL_FLUSH_ASYNC:
...@@ -39,60 +40,62 @@ static void do_hcall(struct lguest *lg, struct hcall_args *args) ...@@ -39,60 +40,62 @@ static void do_hcall(struct lguest *lg, struct hcall_args *args)
case LHCALL_LGUEST_INIT: case LHCALL_LGUEST_INIT:
/* You can't get here unless you're already initialized. Don't /* You can't get here unless you're already initialized. Don't
* do that. */ * do that. */
kill_guest(lg, "already have lguest_data"); kill_guest(cpu, "already have lguest_data");
break; break;
case LHCALL_CRASH: { case LHCALL_SHUTDOWN: {
/* Crash is such a trivial hypercall that we do it in four /* Shutdown is such a trivial hypercall that we do it in four
* lines right here. */ * lines right here. */
char msg[128]; char msg[128];
/* If the lgread fails, it will call kill_guest() itself; the /* If the lgread fails, it will call kill_guest() itself; the
* kill_guest() with the message will be ignored. */ * kill_guest() with the message will be ignored. */
__lgread(lg, msg, args->arg1, sizeof(msg)); __lgread(cpu, msg, args->arg1, sizeof(msg));
msg[sizeof(msg)-1] = '\0'; 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; break;
} }
case LHCALL_FLUSH_TLB: case LHCALL_FLUSH_TLB:
/* FLUSH_TLB comes in two flavors, depending on the /* FLUSH_TLB comes in two flavors, depending on the
* argument: */ * argument: */
if (args->arg1) if (args->arg1)
guest_pagetable_clear_all(lg); guest_pagetable_clear_all(cpu);
else else
guest_pagetable_flush_user(lg); guest_pagetable_flush_user(cpu);
break; break;
/* All these calls simply pass the arguments through to the right /* All these calls simply pass the arguments through to the right
* routines. */ * routines. */
case LHCALL_NEW_PGTABLE: case LHCALL_NEW_PGTABLE:
guest_new_pagetable(lg, args->arg1); guest_new_pagetable(cpu, args->arg1);
break; break;
case LHCALL_SET_STACK: case LHCALL_SET_STACK:
guest_set_stack(lg, args->arg1, args->arg2, args->arg3); guest_set_stack(cpu, args->arg1, args->arg2, args->arg3);
break; break;
case LHCALL_SET_PTE: case LHCALL_SET_PTE:
guest_set_pte(lg, args->arg1, args->arg2, __pte(args->arg3)); guest_set_pte(cpu, args->arg1, args->arg2, __pte(args->arg3));
break; break;
case LHCALL_SET_PMD: case LHCALL_SET_PMD:
guest_set_pmd(lg, args->arg1, args->arg2); guest_set_pmd(cpu->lg, args->arg1, args->arg2);
break; break;
case LHCALL_SET_CLOCKEVENT: case LHCALL_SET_CLOCKEVENT:
guest_set_clockevent(lg, args->arg1); guest_set_clockevent(cpu, args->arg1);
break; break;
case LHCALL_TS: case LHCALL_TS:
/* This sets the TS flag, as we saw used in run_guest(). */ /* This sets the TS flag, as we saw used in run_guest(). */
lg->ts = args->arg1; cpu->ts = args->arg1;
break; break;
case LHCALL_HALT: case LHCALL_HALT:
/* Similarly, this sets the halted flag for run_guest(). */ /* Similarly, this sets the halted flag for run_guest(). */
lg->halted = 1; cpu->halted = 1;
break; break;
case LHCALL_NOTIFY: case LHCALL_NOTIFY:
lg->pending_notify = args->arg1; cpu->pending_notify = args->arg1;
break; break;
default: default:
/* It should be an architecture-specific hypercall. */ /* It should be an architecture-specific hypercall. */
if (lguest_arch_do_hcall(lg, args)) if (lguest_arch_do_hcall(cpu, args))
kill_guest(lg, "Bad hypercall %li\n", args->arg0); kill_guest(cpu, "Bad hypercall %li\n", args->arg0);
} }
} }
/*:*/ /*:*/
...@@ -104,13 +107,13 @@ static void do_hcall(struct lguest *lg, struct hcall_args *args) ...@@ -104,13 +107,13 @@ static void do_hcall(struct lguest *lg, struct hcall_args *args)
* Guest put them in the ring, but we also promise the Guest that they will * 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 * happen before any normal hypercall (which is why we check this before
* checking for a normal hcall). */ * checking for a normal hcall). */
static void do_async_hcalls(struct lguest *lg) static void do_async_hcalls(struct lg_cpu *cpu)
{ {
unsigned int i; unsigned int i;
u8 st[LHCALL_RING_SIZE]; u8 st[LHCALL_RING_SIZE];
/* For simplicity, we copy the entire call status array in at once. */ /* For simplicity, we copy the entire call status array in at once. */
if (copy_from_user(&st, &lg->lguest_data->hcall_status, sizeof(st))) if (copy_from_user(&st, &cpu->lg->lguest_data->hcall_status, sizeof(st)))
return; return;
/* We process "struct lguest_data"s hcalls[] ring once. */ /* We process "struct lguest_data"s hcalls[] ring once. */
...@@ -119,7 +122,7 @@ static void do_async_hcalls(struct lguest *lg) ...@@ -119,7 +122,7 @@ static void do_async_hcalls(struct lguest *lg)
/* We remember where we were up to from last time. This makes /* We remember where we were up to from last time. This makes
* sure that the hypercalls are done in the order the Guest * sure that the hypercalls are done in the order the Guest
* places them in the ring. */ * places them in the ring. */
unsigned int n = lg->next_hcall; unsigned int n = cpu->next_hcall;
/* 0xFF means there's no call here (yet). */ /* 0xFF means there's no call here (yet). */
if (st[n] == 0xFF) if (st[n] == 0xFF)
...@@ -127,65 +130,65 @@ static void do_async_hcalls(struct lguest *lg) ...@@ -127,65 +130,65 @@ static void do_async_hcalls(struct lguest *lg)
/* OK, we have hypercall. Increment the "next_hcall" cursor, /* OK, we have hypercall. Increment the "next_hcall" cursor,
* and wrap back to 0 if we reach the end. */ * and wrap back to 0 if we reach the end. */
if (++lg->next_hcall == LHCALL_RING_SIZE) if (++cpu->next_hcall == LHCALL_RING_SIZE)
lg->next_hcall = 0; cpu->next_hcall = 0;
/* Copy the hypercall arguments into a local copy of /* Copy the hypercall arguments into a local copy of
* the hcall_args struct. */ * the hcall_args struct. */
if (copy_from_user(&args, &lg->lguest_data->hcalls[n], if (copy_from_user(&args, &cpu->lg->lguest_data->hcalls[n],
sizeof(struct hcall_args))) { sizeof(struct hcall_args))) {
kill_guest(lg, "Fetching async hypercalls"); kill_guest(cpu, "Fetching async hypercalls");
break; break;
} }
/* Do the hypercall, same as a normal one. */ /* Do the hypercall, same as a normal one. */
do_hcall(lg, &args); do_hcall(cpu, &args);
/* Mark the hypercall done. */ /* Mark the hypercall done. */
if (put_user(0xFF, &lg->lguest_data->hcall_status[n])) { if (put_user(0xFF, &cpu->lg->lguest_data->hcall_status[n])) {
kill_guest(lg, "Writing result for async hypercall"); kill_guest(cpu, "Writing result for async hypercall");
break; break;
} }
/* Stop doing hypercalls if they want to notify the Launcher: /* Stop doing hypercalls if they want to notify the Launcher:
* it needs to service this first. */ * it needs to service this first. */
if (lg->pending_notify) if (cpu->pending_notify)
break; break;
} }
} }
/* Last of all, we look at what happens first of all. The very first time the /* 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: */ * Guest makes a hypercall, we end up here to set things up: */
static void initialize(struct lguest *lg) static void initialize(struct lg_cpu *cpu)
{ {
/* You can't do anything until you're initialized. The Guest knows the /* You can't do anything until you're initialized. The Guest knows the
* rules, so we're unforgiving here. */ * rules, so we're unforgiving here. */
if (lg->hcall->arg0 != LHCALL_LGUEST_INIT) { if (cpu->hcall->arg0 != LHCALL_LGUEST_INIT) {
kill_guest(lg, "hypercall %li before INIT", lg->hcall->arg0); kill_guest(cpu, "hypercall %li before INIT", cpu->hcall->arg0);
return; return;
} }
if (lguest_arch_init_hypercalls(lg)) if (lguest_arch_init_hypercalls(cpu))
kill_guest(lg, "bad guest page %p", lg->lguest_data); kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
/* The Guest tells us where we're not to deliver interrupts by putting /* The Guest tells us where we're not to deliver interrupts by putting
* the range of addresses into "struct lguest_data". */ * the range of addresses into "struct lguest_data". */
if (get_user(lg->noirq_start, &lg->lguest_data->noirq_start) if (get_user(cpu->lg->noirq_start, &cpu->lg->lguest_data->noirq_start)
|| get_user(lg->noirq_end, &lg->lguest_data->noirq_end)) || get_user(cpu->lg->noirq_end, &cpu->lg->lguest_data->noirq_end))
kill_guest(lg, "bad guest page %p", lg->lguest_data); kill_guest(cpu, "bad guest page %p", cpu->lg->lguest_data);
/* We write the current time into the Guest's data page once so it can /* We write the current time into the Guest's data page once so it can
* set its clock. */ * set its clock. */
write_timestamp(lg); write_timestamp(cpu);
/* page_tables.c will also do some setup. */ /* page_tables.c will also do some setup. */
page_table_guest_data_init(lg); page_table_guest_data_init(cpu);
/* This is the one case where the above accesses might have been the /* 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 * 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 * fault, but the old page might be (read-only) in the Guest
* pagetable. */ * pagetable. */
guest_pagetable_clear_all(lg); guest_pagetable_clear_all(cpu);
} }
/*H:100 /*H:100
...@@ -194,27 +197,27 @@ static void initialize(struct lguest *lg) ...@@ -194,27 +197,27 @@ static void initialize(struct lguest *lg)
* Remember from the Guest, hypercalls come in two flavors: normal and * Remember from the Guest, hypercalls come in two flavors: normal and
* asynchronous. This file handles both of types. * asynchronous. This file handles both of types.
*/ */
void do_hypercalls(struct lguest *lg) void do_hypercalls(struct lg_cpu *cpu)
{ {
/* Not initialized yet? This hypercall must do it. */ /* Not initialized yet? This hypercall must do it. */
if (unlikely(!lg->lguest_data)) { if (unlikely(!cpu->lg->lguest_data)) {
/* Set up the "struct lguest_data" */ /* Set up the "struct lguest_data" */
initialize(lg); initialize(cpu);
/* Hcall is done. */ /* Hcall is done. */
lg->hcall = NULL; cpu->hcall = NULL;
return; return;
} }
/* The Guest has initialized. /* The Guest has initialized.
* *
* Look in the hypercall ring for the async hypercalls: */ * Look in the hypercall ring for the async hypercalls: */
do_async_hcalls(lg); do_async_hcalls(cpu);
/* If we stopped reading the hypercall ring because the Guest did a /* If we stopped reading the hypercall ring because the Guest did a
* NOTIFY to the Launcher, we want to return now. Otherwise we do * NOTIFY to the Launcher, we want to return now. Otherwise we do
* the hypercall. */ * the hypercall. */
if (!lg->pending_notify) { if (!cpu->pending_notify) {
do_hcall(lg, lg->hcall); do_hcall(cpu, cpu->hcall);
/* Tricky point: we reset the hcall pointer to mark the /* Tricky point: we reset the hcall pointer to mark the
* hypercall as "done". We use the hcall pointer rather than * hypercall as "done". We use the hcall pointer rather than
* the trap number to indicate a hypercall is pending. * the trap number to indicate a hypercall is pending.
...@@ -225,16 +228,17 @@ void do_hypercalls(struct lguest *lg) ...@@ -225,16 +228,17 @@ void do_hypercalls(struct lguest *lg)
* Launcher, the run_guest() loop will exit without running the * Launcher, the run_guest() loop will exit without running the
* Guest. When it comes back it would try to re-run the * Guest. When it comes back it would try to re-run the
* hypercall. */ * hypercall. */
lg->hcall = NULL; cpu->hcall = NULL;
} }
} }
/* This routine supplies the Guest with time: it's used for wallclock time at /* 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. */ * initial boot and as a rough time source if the TSC isn't available. */
void write_timestamp(struct lguest *lg) void write_timestamp(struct lg_cpu *cpu)
{ {
struct timespec now; struct timespec now;
ktime_get_real_ts(&now); ktime_get_real_ts(&now);
if (copy_to_user(&lg->lguest_data->time, &now, sizeof(struct timespec))) if (copy_to_user(&cpu->lg->lguest_data->time,
kill_guest(lg, "Writing timestamp"); &now, sizeof(struct timespec)))
kill_guest(cpu, "Writing timestamp");
} }
This diff is collapsed.
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <linux/lguest.h> #include <linux/lguest.h>
#include <linux/lguest_launcher.h> #include <linux/lguest_launcher.h>
#include <linux/wait.h> #include <linux/wait.h>
#include <linux/hrtimer.h>
#include <linux/err.h> #include <linux/err.h>
#include <asm/semaphore.h> #include <asm/semaphore.h>
...@@ -38,58 +39,72 @@ struct lguest_pages ...@@ -38,58 +39,72 @@ struct lguest_pages
#define CHANGED_GDT_TLS 4 /* Actually a subset of CHANGED_GDT */ #define CHANGED_GDT_TLS 4 /* Actually a subset of CHANGED_GDT */
#define CHANGED_ALL 3 #define CHANGED_ALL 3
/* The private info the thread maintains about the guest. */ struct lguest;
struct lguest
{ struct lg_cpu {
/* At end of a page shared mapped over lguest_pages in guest. */ unsigned int id;
unsigned long regs_page; struct lguest *lg;
struct lguest_regs *regs;
struct lguest_data __user *lguest_data;
struct task_struct *tsk; struct task_struct *tsk;
struct mm_struct *mm; /* == tsk->mm, but that becomes NULL on exit */ struct mm_struct *mm; /* == tsk->mm, but that becomes NULL on exit */
u32 pfn_limit;
/* This provides the offset to the base of guest-physical
* memory in the Launcher. */
void __user *mem_base;
unsigned long kernel_address;
u32 cr2; u32 cr2;
int halted;
int ts; int ts;
u32 next_hcall;
u32 esp1; u32 esp1;
u8 ss1; u8 ss1;
/* Bitmap of what has changed: see CHANGED_* above. */
int changed;
unsigned long pending_notify; /* pfn from LHCALL_NOTIFY */
/* At end of a page shared mapped over lguest_pages in guest. */
unsigned long regs_page;
struct lguest_regs *regs;
struct lguest_pages *last_pages;
int cpu_pgd; /* which pgd this cpu is currently using */
/* If a hypercall was asked for, this points to the arguments. */ /* If a hypercall was asked for, this points to the arguments. */
struct hcall_args *hcall; struct hcall_args *hcall;
u32 next_hcall;
/* Virtual clock device */
struct hrtimer hrt;
/* Do we need to stop what we're doing and return to userspace? */ /* Do we need to stop what we're doing and return to userspace? */
int break_out; int break_out;
wait_queue_head_t break_wq; wait_queue_head_t break_wq;
int halted;
/* Bitmap of what has changed: see CHANGED_* above. */ /* Pending virtual interrupts */
int changed; DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
struct lguest_pages *last_pages;
struct lg_cpu_arch arch;
};
/* The private info the thread maintains about the guest. */
struct lguest
{
struct lguest_data __user *lguest_data;
struct lg_cpu cpus[NR_CPUS];
unsigned int nr_cpus;
u32 pfn_limit;
/* This provides the offset to the base of guest-physical
* memory in the Launcher. */
void __user *mem_base;
unsigned long kernel_address;
/* We keep a small number of these. */
u32 pgdidx;
struct pgdir pgdirs[4]; struct pgdir pgdirs[4];
unsigned long noirq_start, noirq_end; unsigned long noirq_start, noirq_end;
unsigned long pending_notify; /* pfn from LHCALL_NOTIFY */
unsigned int stack_pages; unsigned int stack_pages;
u32 tsc_khz; u32 tsc_khz;
/* Dead? */ /* Dead? */
const char *dead; const char *dead;
struct lguest_arch arch;
/* Virtual clock device */
struct hrtimer hrt;
/* Pending virtual interrupts */
DECLARE_BITMAP(irqs_pending, LGUEST_IRQS);
}; };
extern struct mutex lguest_lock; extern struct mutex lguest_lock;
...@@ -97,26 +112,26 @@ extern struct mutex lguest_lock; ...@@ -97,26 +112,26 @@ extern struct mutex lguest_lock;
/* core.c: */ /* core.c: */
int lguest_address_ok(const struct lguest *lg, int lguest_address_ok(const struct lguest *lg,
unsigned long addr, unsigned long len); unsigned long addr, unsigned long len);
void __lgread(struct lguest *, void *, unsigned long, unsigned); void __lgread(struct lg_cpu *, void *, unsigned long, unsigned);
void __lgwrite(struct lguest *, unsigned long, const void *, unsigned); void __lgwrite(struct lg_cpu *, unsigned long, const void *, unsigned);
/*H:035 Using memory-copy operations like that is usually inconvient, so we /*H:035 Using memory-copy operations like that is usually inconvient, so we
* have the following helper macros which read and write a specific type (often * have the following helper macros which read and write a specific type (often
* an unsigned long). * an unsigned long).
* *
* This reads into a variable of the given type then returns that. */ * This reads into a variable of the given type then returns that. */
#define lgread(lg, addr, type) \ #define lgread(cpu, addr, type) \
({ type _v; __lgread((lg), &_v, (addr), sizeof(_v)); _v; }) ({ type _v; __lgread((cpu), &_v, (addr), sizeof(_v)); _v; })
/* This checks that the variable is of the given type, then writes it out. */ /* This checks that the variable is of the given type, then writes it out. */
#define lgwrite(lg, addr, type, val) \ #define lgwrite(cpu, addr, type, val) \
do { \ do { \
typecheck(type, val); \ typecheck(type, val); \
__lgwrite((lg), (addr), &(val), sizeof(val)); \ __lgwrite((cpu), (addr), &(val), sizeof(val)); \
} while(0) } while(0)
/* (end of memory access helper routines) :*/ /* (end of memory access helper routines) :*/
int run_guest(struct lguest *lg, unsigned long __user *user); int run_guest(struct lg_cpu *cpu, unsigned long __user *user);
/* Helper macros to obtain the first 12 or the last 20 bits, this is only the /* Helper macros to obtain the first 12 or the last 20 bits, this is only the
* first step in the migration to the kernel types. pte_pfn is already defined * first step in the migration to the kernel types. pte_pfn is already defined
...@@ -126,52 +141,53 @@ int run_guest(struct lguest *lg, unsigned long __user *user); ...@@ -126,52 +141,53 @@ int run_guest(struct lguest *lg, unsigned long __user *user);
#define pgd_pfn(x) (pgd_val(x) >> PAGE_SHIFT) #define pgd_pfn(x) (pgd_val(x) >> PAGE_SHIFT)
/* interrupts_and_traps.c: */ /* interrupts_and_traps.c: */
void maybe_do_interrupt(struct lguest *lg); void maybe_do_interrupt(struct lg_cpu *cpu);
int deliver_trap(struct lguest *lg, unsigned int num); int deliver_trap(struct lg_cpu *cpu, unsigned int num);
void load_guest_idt_entry(struct lguest *lg, unsigned int i, u32 low, u32 hi); void load_guest_idt_entry(struct lg_cpu *cpu, unsigned int i,
void guest_set_stack(struct lguest *lg, u32 seg, u32 esp, unsigned int pages); u32 low, u32 hi);
void pin_stack_pages(struct lguest *lg); void guest_set_stack(struct lg_cpu *cpu, u32 seg, u32 esp, unsigned int pages);
void pin_stack_pages(struct lg_cpu *cpu);
void setup_default_idt_entries(struct lguest_ro_state *state, void setup_default_idt_entries(struct lguest_ro_state *state,
const unsigned long *def); const unsigned long *def);
void copy_traps(const struct lguest *lg, struct desc_struct *idt, void copy_traps(const struct lg_cpu *cpu, struct desc_struct *idt,
const unsigned long *def); const unsigned long *def);
void guest_set_clockevent(struct lguest *lg, unsigned long delta); void guest_set_clockevent(struct lg_cpu *cpu, unsigned long delta);
void init_clockdev(struct lguest *lg); void init_clockdev(struct lg_cpu *cpu);
bool check_syscall_vector(struct lguest *lg); bool check_syscall_vector(struct lguest *lg);
int init_interrupts(void); int init_interrupts(void);
void free_interrupts(void); void free_interrupts(void);
/* segments.c: */ /* segments.c: */
void setup_default_gdt_entries(struct lguest_ro_state *state); void setup_default_gdt_entries(struct lguest_ro_state *state);
void setup_guest_gdt(struct lguest *lg); void setup_guest_gdt(struct lg_cpu *cpu);
void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num); void load_guest_gdt(struct lg_cpu *cpu, unsigned long table, u32 num);
void guest_load_tls(struct lguest *lg, unsigned long tls_array); void guest_load_tls(struct lg_cpu *cpu, unsigned long tls_array);
void copy_gdt(const struct lguest *lg, struct desc_struct *gdt); void copy_gdt(const struct lg_cpu *cpu, struct desc_struct *gdt);
void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt); void copy_gdt_tls(const struct lg_cpu *cpu, struct desc_struct *gdt);
/* page_tables.c: */ /* page_tables.c: */
int init_guest_pagetable(struct lguest *lg, unsigned long pgtable); int init_guest_pagetable(struct lguest *lg, unsigned long pgtable);
void free_guest_pagetable(struct lguest *lg); void free_guest_pagetable(struct lguest *lg);
void guest_new_pagetable(struct lguest *lg, unsigned long pgtable); void guest_new_pagetable(struct lg_cpu *cpu, unsigned long pgtable);
void guest_set_pmd(struct lguest *lg, unsigned long gpgdir, u32 i); void guest_set_pmd(struct lguest *lg, unsigned long gpgdir, u32 i);
void guest_pagetable_clear_all(struct lguest *lg); void guest_pagetable_clear_all(struct lg_cpu *cpu);
void guest_pagetable_flush_user(struct lguest *lg); void guest_pagetable_flush_user(struct lg_cpu *cpu);
void guest_set_pte(struct lguest *lg, unsigned long gpgdir, void guest_set_pte(struct lg_cpu *cpu, unsigned long gpgdir,
unsigned long vaddr, pte_t val); unsigned long vaddr, pte_t val);
void map_switcher_in_guest(struct lguest *lg, struct lguest_pages *pages); void map_switcher_in_guest(struct lg_cpu *cpu, struct lguest_pages *pages);
int demand_page(struct lguest *info, unsigned long cr2, int errcode); int demand_page(struct lg_cpu *cpu, unsigned long cr2, int errcode);
void pin_page(struct lguest *lg, unsigned long vaddr); void pin_page(struct lg_cpu *cpu, unsigned long vaddr);
unsigned long guest_pa(struct lguest *lg, unsigned long vaddr); unsigned long guest_pa(struct lg_cpu *cpu, unsigned long vaddr);
void page_table_guest_data_init(struct lguest *lg); void page_table_guest_data_init(struct lg_cpu *cpu);
/* <arch>/core.c: */ /* <arch>/core.c: */
void lguest_arch_host_init(void); void lguest_arch_host_init(void);
void lguest_arch_host_fini(void); void lguest_arch_host_fini(void);
void lguest_arch_run_guest(struct lguest *lg); void lguest_arch_run_guest(struct lg_cpu *cpu);
void lguest_arch_handle_trap(struct lguest *lg); void lguest_arch_handle_trap(struct lg_cpu *cpu);
int lguest_arch_init_hypercalls(struct lguest *lg); int lguest_arch_init_hypercalls(struct lg_cpu *cpu);
int lguest_arch_do_hcall(struct lguest *lg, struct hcall_args *args); int lguest_arch_do_hcall(struct lg_cpu *cpu, struct hcall_args *args);
void lguest_arch_setup_regs(struct lguest *lg, unsigned long start); void lguest_arch_setup_regs(struct lg_cpu *cpu, unsigned long start);
/* <arch>/switcher.S: */ /* <arch>/switcher.S: */
extern char start_switcher_text[], end_switcher_text[], switch_to_guest[]; extern char start_switcher_text[], end_switcher_text[], switch_to_guest[];
...@@ -181,8 +197,8 @@ int lguest_device_init(void); ...@@ -181,8 +197,8 @@ int lguest_device_init(void);
void lguest_device_remove(void); void lguest_device_remove(void);
/* hypercalls.c: */ /* hypercalls.c: */
void do_hypercalls(struct lguest *lg); void do_hypercalls(struct lg_cpu *cpu);
void write_timestamp(struct lguest *lg); void write_timestamp(struct lg_cpu *cpu);
/*L:035 /*L:035
* Let's step aside for the moment, to study one important routine that's used * Let's step aside for the moment, to study one important routine that's used
...@@ -208,12 +224,12 @@ void write_timestamp(struct lguest *lg); ...@@ -208,12 +224,12 @@ void write_timestamp(struct lguest *lg);
* Like any macro which uses an "if", it is safely wrapped in a run-once "do { * Like any macro which uses an "if", it is safely wrapped in a run-once "do {
* } while(0)". * } while(0)".
*/ */
#define kill_guest(lg, fmt...) \ #define kill_guest(cpu, fmt...) \
do { \ do { \
if (!(lg)->dead) { \ if (!(cpu)->lg->dead) { \
(lg)->dead = kasprintf(GFP_ATOMIC, fmt); \ (cpu)->lg->dead = kasprintf(GFP_ATOMIC, fmt); \
if (!(lg)->dead) \ if (!(cpu)->lg->dead) \
(lg)->dead = ERR_PTR(-ENOMEM); \ (cpu)->lg->dead = ERR_PTR(-ENOMEM); \
} \ } \
} while(0) } while(0)
/* (End of aside) :*/ /* (End of aside) :*/
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/miscdevice.h> #include <linux/miscdevice.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/sched.h>
#include "lg.h" #include "lg.h"
/*L:055 When something happens, the Waker process needs a way to stop the /*L:055 When something happens, the Waker process needs a way to stop the
...@@ -13,7 +14,7 @@ ...@@ -13,7 +14,7 @@
* LHREQ_BREAK and the value "1" to /dev/lguest to do this. Once the Launcher * LHREQ_BREAK and the value "1" to /dev/lguest to do this. Once the Launcher
* has done whatever needs attention, it writes LHREQ_BREAK and "0" to release * has done whatever needs attention, it writes LHREQ_BREAK and "0" to release
* the Waker. */ * the Waker. */
static int break_guest_out(struct lguest *lg, const unsigned long __user *input) static int break_guest_out(struct lg_cpu *cpu, const unsigned long __user*input)
{ {
unsigned long on; unsigned long on;
...@@ -22,21 +23,21 @@ static int break_guest_out(struct lguest *lg, const unsigned long __user *input) ...@@ -22,21 +23,21 @@ static int break_guest_out(struct lguest *lg, const unsigned long __user *input)
return -EFAULT; return -EFAULT;
if (on) { if (on) {
lg->break_out = 1; cpu->break_out = 1;
/* Pop it out of the Guest (may be running on different CPU) */ /* Pop it out of the Guest (may be running on different CPU) */
wake_up_process(lg->tsk); wake_up_process(cpu->tsk);
/* Wait for them to reset it */ /* Wait for them to reset it */
return wait_event_interruptible(lg->break_wq, !lg->break_out); return wait_event_interruptible(cpu->break_wq, !cpu->break_out);
} else { } else {
lg->break_out = 0; cpu->break_out = 0;
wake_up(&lg->break_wq); wake_up(&cpu->break_wq);
return 0; return 0;
} }
} }
/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
* number to /dev/lguest. */ * number to /dev/lguest. */
static int user_send_irq(struct lguest *lg, const unsigned long __user *input) static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
{ {
unsigned long irq; unsigned long irq;
...@@ -46,7 +47,7 @@ static int user_send_irq(struct lguest *lg, const unsigned long __user *input) ...@@ -46,7 +47,7 @@ static int user_send_irq(struct lguest *lg, const unsigned long __user *input)
return -EINVAL; return -EINVAL;
/* Next time the Guest runs, the core code will see if it can deliver /* Next time the Guest runs, the core code will see if it can deliver
* this interrupt. */ * this interrupt. */
set_bit(irq, lg->irqs_pending); set_bit(irq, cpu->irqs_pending);
return 0; return 0;
} }
...@@ -55,13 +56,21 @@ static int user_send_irq(struct lguest *lg, const unsigned long __user *input) ...@@ -55,13 +56,21 @@ static int user_send_irq(struct lguest *lg, const unsigned long __user *input)
static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
{ {
struct lguest *lg = file->private_data; struct lguest *lg = file->private_data;
struct lg_cpu *cpu;
unsigned int cpu_id = *o;
/* You must write LHREQ_INITIALIZE first! */ /* You must write LHREQ_INITIALIZE first! */
if (!lg) if (!lg)
return -EINVAL; return -EINVAL;
/* Watch out for arbitrary vcpu indexes! */
if (cpu_id >= lg->nr_cpus)
return -EINVAL;
cpu = &lg->cpus[cpu_id];
/* If you're not the task which owns the Guest, go away. */ /* If you're not the task which owns the Guest, go away. */
if (current != lg->tsk) if (current != cpu->tsk)
return -EPERM; return -EPERM;
/* If the guest is already dead, we indicate why */ /* If the guest is already dead, we indicate why */
...@@ -81,11 +90,53 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) ...@@ -81,11 +90,53 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
/* If we returned from read() last time because the Guest notified, /* If we returned from read() last time because the Guest notified,
* clear the flag. */ * clear the flag. */
if (lg->pending_notify) if (cpu->pending_notify)
lg->pending_notify = 0; cpu->pending_notify = 0;
/* Run the Guest until something interesting happens. */ /* Run the Guest until something interesting happens. */
return run_guest(lg, (unsigned long __user *)user); return run_guest(cpu, (unsigned long __user *)user);
}
static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
{
if (id >= NR_CPUS)
return -EINVAL;
cpu->id = id;
cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
cpu->lg->nr_cpus++;
init_clockdev(cpu);
/* We need a complete page for the Guest registers: they are accessible
* to the Guest and we can only grant it access to whole pages. */
cpu->regs_page = get_zeroed_page(GFP_KERNEL);
if (!cpu->regs_page)
return -ENOMEM;
/* We actually put the registers at the bottom of the page. */
cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
/* Now we initialize the Guest's registers, handing it the start
* address. */
lguest_arch_setup_regs(cpu, start_ip);
/* Initialize the queue for the waker to wait on */
init_waitqueue_head(&cpu->break_wq);
/* We keep a pointer to the Launcher task (ie. current task) for when
* other Guests want to wake this one (inter-Guest I/O). */
cpu->tsk = current;
/* We need to keep a pointer to the Launcher's memory map, because if
* the Launcher dies we need to clean it up. If we don't keep a
* reference, it is destroyed before close() is called. */
cpu->mm = get_task_mm(cpu->tsk);
/* We remember which CPU's pages this Guest used last, for optimization
* when the same Guest runs on the same CPU twice. */
cpu->last_pages = NULL;
return 0;
} }
/*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit) /*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
...@@ -134,15 +185,10 @@ static int initialize(struct file *file, const unsigned long __user *input) ...@@ -134,15 +185,10 @@ static int initialize(struct file *file, const unsigned long __user *input)
lg->mem_base = (void __user *)(long)args[0]; lg->mem_base = (void __user *)(long)args[0];
lg->pfn_limit = args[1]; lg->pfn_limit = args[1];
/* We need a complete page for the Guest registers: they are accessible /* This is the first cpu */
* to the Guest and we can only grant it access to whole pages. */ err = lg_cpu_start(&lg->cpus[0], 0, args[3]);
lg->regs_page = get_zeroed_page(GFP_KERNEL); if (err)
if (!lg->regs_page) {
err = -ENOMEM;
goto release_guest; goto release_guest;
}
/* We actually put the registers at the bottom of the page. */
lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs);
/* Initialize the Guest's shadow page tables, using the toplevel /* Initialize the Guest's shadow page tables, using the toplevel
* address the Launcher gave us. This allocates memory, so can * address the Launcher gave us. This allocates memory, so can
...@@ -151,28 +197,6 @@ static int initialize(struct file *file, const unsigned long __user *input) ...@@ -151,28 +197,6 @@ static int initialize(struct file *file, const unsigned long __user *input)
if (err) if (err)
goto free_regs; goto free_regs;
/* Now we initialize the Guest's registers, handing it the start
* address. */
lguest_arch_setup_regs(lg, args[3]);
/* The timer for lguest's clock needs initialization. */
init_clockdev(lg);
/* We keep a pointer to the Launcher task (ie. current task) for when
* other Guests want to wake this one (inter-Guest I/O). */
lg->tsk = current;
/* We need to keep a pointer to the Launcher's memory map, because if
* the Launcher dies we need to clean it up. If we don't keep a
* reference, it is destroyed before close() is called. */
lg->mm = get_task_mm(lg->tsk);
/* Initialize the queue for the waker to wait on */
init_waitqueue_head(&lg->break_wq);
/* We remember which CPU's pages this Guest used last, for optimization
* when the same Guest runs on the same CPU twice. */
lg->last_pages = NULL;
/* We keep our "struct lguest" in the file's private_data. */ /* We keep our "struct lguest" in the file's private_data. */
file->private_data = lg; file->private_data = lg;
...@@ -182,7 +206,8 @@ static int initialize(struct file *file, const unsigned long __user *input) ...@@ -182,7 +206,8 @@ static int initialize(struct file *file, const unsigned long __user *input)
return sizeof(args); return sizeof(args);
free_regs: free_regs:
free_page(lg->regs_page); /* FIXME: This should be in free_vcpu */
free_page(lg->cpus[0].regs_page);
release_guest: release_guest:
kfree(lg); kfree(lg);
unlock: unlock:
...@@ -202,30 +227,37 @@ static ssize_t write(struct file *file, const char __user *in, ...@@ -202,30 +227,37 @@ static ssize_t write(struct file *file, const char __user *in,
struct lguest *lg = file->private_data; struct lguest *lg = file->private_data;
const unsigned long __user *input = (const unsigned long __user *)in; const unsigned long __user *input = (const unsigned long __user *)in;
unsigned long req; unsigned long req;
struct lg_cpu *uninitialized_var(cpu);
unsigned int cpu_id = *off;
if (get_user(req, input) != 0) if (get_user(req, input) != 0)
return -EFAULT; return -EFAULT;
input++; input++;
/* If you haven't initialized, you must do that first. */ /* If you haven't initialized, you must do that first. */
if (req != LHREQ_INITIALIZE && !lg) if (req != LHREQ_INITIALIZE) {
return -EINVAL; if (!lg || (cpu_id >= lg->nr_cpus))
return -EINVAL;
cpu = &lg->cpus[cpu_id];
if (!cpu)
return -EINVAL;
}
/* Once the Guest is dead, all you can do is read() why it died. */ /* Once the Guest is dead, all you can do is read() why it died. */
if (lg && lg->dead) if (lg && lg->dead)
return -ENOENT; return -ENOENT;
/* If you're not the task which owns the Guest, you can only break */ /* If you're not the task which owns the Guest, you can only break */
if (lg && current != lg->tsk && req != LHREQ_BREAK) if (lg && current != cpu->tsk && req != LHREQ_BREAK)
return -EPERM; return -EPERM;
switch (req) { switch (req) {
case LHREQ_INITIALIZE: case LHREQ_INITIALIZE:
return initialize(file, input); return initialize(file, input);
case LHREQ_IRQ: case LHREQ_IRQ:
return user_send_irq(lg, input); return user_send_irq(cpu, input);
case LHREQ_BREAK: case LHREQ_BREAK:
return break_guest_out(lg, input); return break_guest_out(cpu, input);
default: default:
return -EINVAL; return -EINVAL;
} }
...@@ -241,6 +273,7 @@ static ssize_t write(struct file *file, const char __user *in, ...@@ -241,6 +273,7 @@ static ssize_t write(struct file *file, const char __user *in,
static int close(struct inode *inode, struct file *file) static int close(struct inode *inode, struct file *file)
{ {
struct lguest *lg = file->private_data; struct lguest *lg = file->private_data;
unsigned int i;
/* If we never successfully initialized, there's nothing to clean up */ /* If we never successfully initialized, there's nothing to clean up */
if (!lg) if (!lg)
...@@ -249,19 +282,23 @@ static int close(struct inode *inode, struct file *file) ...@@ -249,19 +282,23 @@ static int close(struct inode *inode, struct file *file)
/* We need the big lock, to protect from inter-guest I/O and other /* We need the big lock, to protect from inter-guest I/O and other
* Launchers initializing guests. */ * Launchers initializing guests. */
mutex_lock(&lguest_lock); mutex_lock(&lguest_lock);
/* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
hrtimer_cancel(&lg->hrt);
/* Free up the shadow page tables for the Guest. */ /* Free up the shadow page tables for the Guest. */
free_guest_pagetable(lg); free_guest_pagetable(lg);
/* Now all the memory cleanups are done, it's safe to release the
* Launcher's memory management structure. */ for (i = 0; i < lg->nr_cpus; i++) {
mmput(lg->mm); /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
hrtimer_cancel(&lg->cpus[i].hrt);
/* We can free up the register page we allocated. */
free_page(lg->cpus[i].regs_page);
/* Now all the memory cleanups are done, it's safe to release
* the Launcher's memory management structure. */
mmput(lg->cpus[i].mm);
}
/* If lg->dead doesn't contain an error code it will be NULL or a /* If lg->dead doesn't contain an error code it will be NULL or a
* kmalloc()ed string, either of which is ok to hand to kfree(). */ * kmalloc()ed string, either of which is ok to hand to kfree(). */
if (!IS_ERR(lg->dead)) if (!IS_ERR(lg->dead))
kfree(lg->dead); kfree(lg->dead);
/* We can free up the register page we allocated. */
free_page(lg->regs_page);
/* We clear the entire structure, which also marks it as free for the /* We clear the entire structure, which also marks it as free for the
* next user. */ * next user. */
memset(lg, 0, sizeof(*lg)); memset(lg, 0, sizeof(*lg));
......
This diff is collapsed.
...@@ -58,7 +58,7 @@ static int ignored_gdt(unsigned int num) ...@@ -58,7 +58,7 @@ static int ignored_gdt(unsigned int num)
* Protection Fault in the Switcher when it restores a Guest segment register * Protection Fault in the Switcher when it restores a Guest segment register
* which tries to use that entry. Then we kill the Guest for causing such a * which tries to use that entry. Then we kill the Guest for causing such a
* mess: the message will be "unhandled trap 256". */ * mess: the message will be "unhandled trap 256". */
static void fixup_gdt_table(struct lguest *lg, unsigned start, unsigned end) static void fixup_gdt_table(struct lg_cpu *cpu, unsigned start, unsigned end)
{ {
unsigned int i; unsigned int i;
...@@ -71,14 +71,14 @@ static void fixup_gdt_table(struct lguest *lg, unsigned start, unsigned end) ...@@ -71,14 +71,14 @@ static void fixup_gdt_table(struct lguest *lg, unsigned start, unsigned end)
/* Segment descriptors contain a privilege level: the Guest is /* Segment descriptors contain a privilege level: the Guest is
* sometimes careless and leaves this as 0, even though it's * sometimes careless and leaves this as 0, even though it's
* running at privilege level 1. If so, we fix it here. */ * running at privilege level 1. If so, we fix it here. */
if ((lg->arch.gdt[i].b & 0x00006000) == 0) if ((cpu->arch.gdt[i].b & 0x00006000) == 0)
lg->arch.gdt[i].b |= (GUEST_PL << 13); cpu->arch.gdt[i].b |= (GUEST_PL << 13);
/* Each descriptor has an "accessed" bit. If we don't set it /* Each descriptor has an "accessed" bit. If we don't set it
* now, the CPU will try to set it when the Guest first loads * now, the CPU will try to set it when the Guest first loads
* that entry into a segment register. But the GDT isn't * that entry into a segment register. But the GDT isn't
* writable by the Guest, so bad things can happen. */ * writable by the Guest, so bad things can happen. */
lg->arch.gdt[i].b |= 0x00000100; cpu->arch.gdt[i].b |= 0x00000100;
} }
} }
...@@ -109,31 +109,31 @@ void setup_default_gdt_entries(struct lguest_ro_state *state) ...@@ -109,31 +109,31 @@ void setup_default_gdt_entries(struct lguest_ro_state *state)
/* This routine sets up the initial Guest GDT for booting. All entries start /* This routine sets up the initial Guest GDT for booting. All entries start
* as 0 (unusable). */ * as 0 (unusable). */
void setup_guest_gdt(struct lguest *lg) void setup_guest_gdt(struct lg_cpu *cpu)
{ {
/* Start with full 0-4G segments... */ /* Start with full 0-4G segments... */
lg->arch.gdt[GDT_ENTRY_KERNEL_CS] = FULL_EXEC_SEGMENT; cpu->arch.gdt[GDT_ENTRY_KERNEL_CS] = FULL_EXEC_SEGMENT;
lg->arch.gdt[GDT_ENTRY_KERNEL_DS] = FULL_SEGMENT; cpu->arch.gdt[GDT_ENTRY_KERNEL_DS] = FULL_SEGMENT;
/* ...except the Guest is allowed to use them, so set the privilege /* ...except the Guest is allowed to use them, so set the privilege
* level appropriately in the flags. */ * level appropriately in the flags. */
lg->arch.gdt[GDT_ENTRY_KERNEL_CS].b |= (GUEST_PL << 13); cpu->arch.gdt[GDT_ENTRY_KERNEL_CS].b |= (GUEST_PL << 13);
lg->arch.gdt[GDT_ENTRY_KERNEL_DS].b |= (GUEST_PL << 13); cpu->arch.gdt[GDT_ENTRY_KERNEL_DS].b |= (GUEST_PL << 13);
} }
/*H:650 An optimization of copy_gdt(), for just the three "thead-local storage" /*H:650 An optimization of copy_gdt(), for just the three "thead-local storage"
* entries. */ * entries. */
void copy_gdt_tls(const struct lguest *lg, struct desc_struct *gdt) void copy_gdt_tls(const struct lg_cpu *cpu, struct desc_struct *gdt)
{ {
unsigned int i; unsigned int i;
for (i = GDT_ENTRY_TLS_MIN; i <= GDT_ENTRY_TLS_MAX; i++) for (i = GDT_ENTRY_TLS_MIN; i <= GDT_ENTRY_TLS_MAX; i++)
gdt[i] = lg->arch.gdt[i]; gdt[i] = cpu->arch.gdt[i];
} }
/*H:640 When the Guest is run on a different CPU, or the GDT entries have /*H:640 When the Guest is run on a different CPU, or the GDT entries have
* changed, copy_gdt() is called to copy the Guest's GDT entries across to this * changed, copy_gdt() is called to copy the Guest's GDT entries across to this
* CPU's GDT. */ * CPU's GDT. */
void copy_gdt(const struct lguest *lg, struct desc_struct *gdt) void copy_gdt(const struct lg_cpu *cpu, struct desc_struct *gdt)
{ {
unsigned int i; unsigned int i;
...@@ -141,38 +141,38 @@ void copy_gdt(const struct lguest *lg, struct desc_struct *gdt) ...@@ -141,38 +141,38 @@ void copy_gdt(const struct lguest *lg, struct desc_struct *gdt)
* replaced. See ignored_gdt() above. */ * replaced. See ignored_gdt() above. */
for (i = 0; i < GDT_ENTRIES; i++) for (i = 0; i < GDT_ENTRIES; i++)
if (!ignored_gdt(i)) if (!ignored_gdt(i))
gdt[i] = lg->arch.gdt[i]; gdt[i] = cpu->arch.gdt[i];
} }
/*H:620 This is where the Guest asks us to load a new GDT (LHCALL_LOAD_GDT). /*H:620 This is where the Guest asks us to load a new GDT (LHCALL_LOAD_GDT).
* We copy it from the Guest and tweak the entries. */ * We copy it from the Guest and tweak the entries. */
void load_guest_gdt(struct lguest *lg, unsigned long table, u32 num) void load_guest_gdt(struct lg_cpu *cpu, unsigned long table, u32 num)
{ {
/* We assume the Guest has the same number of GDT entries as the /* We assume the Guest has the same number of GDT entries as the
* Host, otherwise we'd have to dynamically allocate the Guest GDT. */ * Host, otherwise we'd have to dynamically allocate the Guest GDT. */
if (num > ARRAY_SIZE(lg->arch.gdt)) if (num > ARRAY_SIZE(cpu->arch.gdt))
kill_guest(lg, "too many gdt entries %i", num); kill_guest(cpu, "too many gdt entries %i", num);
/* We read the whole thing in, then fix it up. */ /* We read the whole thing in, then fix it up. */
__lgread(lg, lg->arch.gdt, table, num * sizeof(lg->arch.gdt[0])); __lgread(cpu, cpu->arch.gdt, table, num * sizeof(cpu->arch.gdt[0]));
fixup_gdt_table(lg, 0, ARRAY_SIZE(lg->arch.gdt)); fixup_gdt_table(cpu, 0, ARRAY_SIZE(cpu->arch.gdt));
/* Mark that the GDT changed so the core knows it has to copy it again, /* Mark that the GDT changed so the core knows it has to copy it again,
* even if the Guest is run on the same CPU. */ * even if the Guest is run on the same CPU. */
lg->changed |= CHANGED_GDT; cpu->changed |= CHANGED_GDT;
} }
/* This is the fast-track version for just changing the three TLS entries. /* This is the fast-track version for just changing the three TLS entries.
* Remember that this happens on every context switch, so it's worth * Remember that this happens on every context switch, so it's worth
* optimizing. But wouldn't it be neater to have a single hypercall to cover * optimizing. But wouldn't it be neater to have a single hypercall to cover
* both cases? */ * both cases? */
void guest_load_tls(struct lguest *lg, unsigned long gtls) void guest_load_tls(struct lg_cpu *cpu, unsigned long gtls)
{ {
struct desc_struct *tls = &lg->arch.gdt[GDT_ENTRY_TLS_MIN]; struct desc_struct *tls = &cpu->arch.gdt[GDT_ENTRY_TLS_MIN];
__lgread(lg, tls, gtls, sizeof(*tls)*GDT_ENTRY_TLS_ENTRIES); __lgread(cpu, tls, gtls, sizeof(*tls)*GDT_ENTRY_TLS_ENTRIES);
fixup_gdt_table(lg, GDT_ENTRY_TLS_MIN, GDT_ENTRY_TLS_MAX+1); fixup_gdt_table(cpu, GDT_ENTRY_TLS_MIN, GDT_ENTRY_TLS_MAX+1);
/* Note that just the TLS entries have changed. */ /* Note that just the TLS entries have changed. */
lg->changed |= CHANGED_GDT_TLS; cpu->changed |= CHANGED_GDT_TLS;
} }
/*:*/ /*:*/
......
This diff is collapsed.
...@@ -56,7 +56,7 @@ struct lguest_ro_state ...@@ -56,7 +56,7 @@ struct lguest_ro_state
struct desc_struct guest_gdt[GDT_ENTRIES]; struct desc_struct guest_gdt[GDT_ENTRIES];
}; };
struct lguest_arch struct lg_cpu_arch
{ {
/* The GDT entries copied into lguest_ro_state when running. */ /* The GDT entries copied into lguest_ro_state when running. */
struct desc_struct gdt[GDT_ENTRIES]; struct desc_struct gdt[GDT_ENTRIES];
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#define LHCALL_FLUSH_ASYNC 0 #define LHCALL_FLUSH_ASYNC 0
#define LHCALL_LGUEST_INIT 1 #define LHCALL_LGUEST_INIT 1
#define LHCALL_CRASH 2 #define LHCALL_SHUTDOWN 2
#define LHCALL_LOAD_GDT 3 #define LHCALL_LOAD_GDT 3
#define LHCALL_NEW_PGTABLE 4 #define LHCALL_NEW_PGTABLE 4
#define LHCALL_FLUSH_TLB 5 #define LHCALL_FLUSH_TLB 5
...@@ -20,6 +20,10 @@ ...@@ -20,6 +20,10 @@
#define LGUEST_TRAP_ENTRY 0x1F #define LGUEST_TRAP_ENTRY 0x1F
/* Argument number 3 to LHCALL_LGUEST_SHUTDOWN */
#define LGUEST_SHUTDOWN_POWEROFF 1
#define LGUEST_SHUTDOWN_RESTART 2
#ifndef __ASSEMBLY__ #ifndef __ASSEMBLY__
#include <asm/hw_irq.h> #include <asm/hw_irq.h>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment