lguest_user.c revision 48245cc0708d49d1d0566b9fa617ad6c5f4c6934
1f938d2c892db0d80d144253d4a7b7083efdbedebRusty Russell/*P:200 This contains all the /dev/lguest code, whereby the userspace launcher 2f938d2c892db0d80d144253d4a7b7083efdbedebRusty Russell * controls and communicates with the Guest. For example, the first write will 33c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell * tell us the Guest's memory layout, pagetable, entry point and kernel address 43c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell * offset. A read will run the Guest until something happens, such as a signal 53c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell * or the Guest doing a DMA out to the Launcher. Writes are also used to get a 63c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell * DMA buffer registered by the Guest and to send the Guest an interrupt. :*/ 7d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include <linux/uaccess.h> 8d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include <linux/miscdevice.h> 9d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include <linux/fs.h> 10d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include "lg.h" 11d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 12dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/*L:030 setup_regs() doesn't really belong in this file, but it gives us an 13dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * early glimpse deeper into the Host so it's worth having here. 14dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 15dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Most of the Guest's registers are left alone: we used get_zeroed_page() to 16dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * allocate the structure, so they will be 0. */ 17d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic void setup_regs(struct lguest_regs *regs, unsigned long start) 18d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 19dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* There are four "segment" registers which the Guest needs to boot: 20dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * The "code segment" register (cs) refers to the kernel code segment 21dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * __KERNEL_CS, and the "data", "extra" and "stack" segment registers 22dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * refer to the kernel data segment __KERNEL_DS. 23dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 24dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * The privilege level is packed into the lower bits. The Guest runs 25dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * at privilege level 1 (GUEST_PL).*/ 26d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL; 27d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell regs->cs = __KERNEL_CS|GUEST_PL; 28dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 29dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* The "eflags" register contains miscellaneous flags. Bit 1 (0x002) 30dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * is supposed to always be "1". Bit 9 (0x200) controls whether 31dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * interrupts are enabled. We always leave interrupts enabled while 32dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * running the Guest. */ 33dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell regs->eflags = 0x202; 34dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 35dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* The "Extended Instruction Pointer" register says where the Guest is 36dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * running. */ 37d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell regs->eip = start; 38dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 39dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* %esi points to our boot information, at physical address 0, so don't 40dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * touch it. */ 41d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 42d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 43dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/*L:310 To send DMA into the Guest, the Launcher needs to be able to ask for a 44dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * DMA buffer. This is done by writing LHREQ_GETDMA and the key to 45dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * /dev/lguest. */ 46d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic long user_get_dma(struct lguest *lg, const u32 __user *input) 47d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 48d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell unsigned long key, udma, irq; 49d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 50dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Fetch the key they wrote to us. */ 51d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (get_user(key, input) != 0) 52d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 53dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Look for a free Guest DMA buffer bound to that key. */ 54d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell udma = get_dma_buffer(lg, key, &irq); 55d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!udma) 56d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -ENOENT; 57d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 58dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We need to tell the Launcher what interrupt the Guest expects after 59dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * the buffer is filled. We stash it in udma->used_len. */ 60d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell lgwrite_u32(lg, udma + offsetof(struct lguest_dma, used_len), irq); 61dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 62dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* The (guest-physical) address of the DMA buffer is returned from 63dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * the write(). */ 64d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return udma; 65d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 66d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 67dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/*L:315 To force the Guest to stop running and return to the Launcher, the 68d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell * Waker sets writes LHREQ_BREAK and the value "1" to /dev/lguest. The 69d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell * Launcher then writes LHREQ_BREAK and "0" to release the Waker. */ 70d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic int break_guest_out(struct lguest *lg, const u32 __user *input) 71d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 72d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell unsigned long on; 73d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 74d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell /* Fetch whether they're turning break on or off.. */ 75d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (get_user(on, input) != 0) 76d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 77d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 78d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (on) { 79d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell lg->break_out = 1; 80d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell /* Pop it out (may be running on different CPU) */ 81d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell wake_up_process(lg->tsk); 82d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell /* Wait for them to reset it */ 83d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return wait_event_interruptible(lg->break_wq, !lg->break_out); 84d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } else { 85d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell lg->break_out = 0; 86d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell wake_up(&lg->break_wq); 87d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return 0; 88d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 89d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 90d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 91dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt 92dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * number to /dev/lguest. */ 93d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic int user_send_irq(struct lguest *lg, const u32 __user *input) 94d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 95d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell u32 irq; 96d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 97d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (get_user(irq, input) != 0) 98d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 99d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (irq >= LGUEST_IRQS) 100d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EINVAL; 101dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Next time the Guest runs, the core code will see if it can deliver 102dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * this interrupt. */ 103d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell set_bit(irq, lg->irqs_pending); 104d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return 0; 105d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 106d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 107dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/*L:040 Once our Guest is initialized, the Launcher makes it run by reading 108dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * from /dev/lguest. */ 109d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) 110d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 111d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg = file->private_data; 112d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 113dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* You must write LHREQ_INITIALIZE first! */ 114d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!lg) 115d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EINVAL; 116d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 117d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell /* If you're not the task which owns the guest, go away. */ 118d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (current != lg->tsk) 119d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EPERM; 120d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 121dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* If the guest is already dead, we indicate why */ 122d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (lg->dead) { 123d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell size_t len; 124d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 125dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* lg->dead either contains an error code, or a string. */ 126d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (IS_ERR(lg->dead)) 127d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return PTR_ERR(lg->dead); 128d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 129dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We can only return as much as the buffer they read with. */ 130d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell len = min(size, strlen(lg->dead)+1); 131d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (copy_to_user(user, lg->dead, len) != 0) 132d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 133d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return len; 134d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 135d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 136dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* If we returned from read() last time because the Guest sent DMA, 137dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * clear the flag. */ 138d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (lg->dma_is_pending) 139d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell lg->dma_is_pending = 0; 140d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 141dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Run the Guest until something interesting happens. */ 142d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return run_guest(lg, (unsigned long __user *)user); 143d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 144d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 1453c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell/*L:020 The initialization write supplies 5 32-bit values (in addition to the 146dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 32-bit LHREQ_INITIALIZE value). These are: 147dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 1483c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell * base: The start of the Guest-physical memory inside the Launcher memory. 1493c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell * 150dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * pfnlimit: The highest (Guest-physical) page number the Guest should be 151dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * allowed to access. The Launcher has to live in Guest memory, so it sets 152dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * this to ensure the Guest can't reach it. 153dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 154dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * pgdir: The (Guest-physical) address of the top of the initial Guest 155dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * pagetables (which are set up by the Launcher). 156dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 157dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * start: The first instruction to execute ("eip" in x86-speak). 158dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 159dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * page_offset: The PAGE_OFFSET constant in the Guest kernel. We should 160dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * probably wean the code off this, but it's a very useful constant! Any 161dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * address above this is within the Guest kernel, and any kernel address can 162dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * quickly converted from physical to virtual by adding PAGE_OFFSET. It's 163dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 0xC0000000 (3G) by default, but it's configurable at kernel build time. 164dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell */ 165d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic int initialize(struct file *file, const u32 __user *input) 166d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 167dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* "struct lguest" contains everything we (the Host) know about a 168dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Guest. */ 169d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg; 17048245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell int err; 1713c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell u32 args[5]; 172d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 17348245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell /* We grab the Big Lguest lock, which protects against multiple 17448245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell * simultaneous initializations. */ 175d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_lock(&lguest_lock); 176dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* You can't initialize twice! Close the device and start again... */ 177d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (file->private_data) { 178d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell err = -EBUSY; 179d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto unlock; 180d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 181d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 182d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (copy_from_user(args, input, sizeof(args)) != 0) { 183d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell err = -EFAULT; 184d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto unlock; 185d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 186d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 18748245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell lg = kzalloc(sizeof(*lg), GFP_KERNEL); 18848245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell if (!lg) { 18948245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell err = -ENOMEM; 190d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto unlock; 191d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 192dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 193dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Populate the easy fields of our "struct lguest" */ 1943c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell lg->mem_base = (void __user *)(long)args[0]; 1953c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell lg->pfn_limit = args[1]; 1963c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell lg->page_offset = args[4]; 197dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 198dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We need a complete page for the Guest registers: they are accessible 199dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * to the Guest and we can only grant it access to whole pages. */ 200d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell lg->regs_page = get_zeroed_page(GFP_KERNEL); 201d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!lg->regs_page) { 202d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell err = -ENOMEM; 203d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto release_guest; 204d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 205dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We actually put the registers at the bottom of the page. */ 206d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs); 207d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 208dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Initialize the Guest's shadow page tables, using the toplevel 209dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * address the Launcher gave us. This allocates memory, so can 210dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * fail. */ 2113c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell err = init_guest_pagetable(lg, args[2]); 212d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (err) 213d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto free_regs; 214d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 215dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Now we initialize the Guest's registers, handing it the start 216dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * address. */ 2173c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell setup_regs(lg->regs, args[3]); 218dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 219dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* There are a couple of GDT entries the Guest expects when first 220dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * booting. */ 221d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell setup_guest_gdt(lg); 222dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 223dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* The timer for lguest's clock needs initialization. */ 224d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell init_clockdev(lg); 225dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 226dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We keep a pointer to the Launcher task (ie. current task) for when 227dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * other Guests want to wake this one (inter-Guest I/O). */ 228d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell lg->tsk = current; 229dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We need to keep a pointer to the Launcher's memory map, because if 230dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * the Launcher dies we need to clean it up. If we don't keep a 231dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * reference, it is destroyed before close() is called. */ 232d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell lg->mm = get_task_mm(lg->tsk); 233dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 234dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Initialize the queue for the waker to wait on */ 235d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell init_waitqueue_head(&lg->break_wq); 236dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 237dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We remember which CPU's pages this Guest used last, for optimization 238dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * when the same Guest runs on the same CPU twice. */ 239d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell lg->last_pages = NULL; 240dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 241dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We keep our "struct lguest" in the file's private_data. */ 242d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell file->private_data = lg; 243d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 244d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_unlock(&lguest_lock); 245d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 246dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* And because this is a write() call, we return the length used. */ 247d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return sizeof(args); 248d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 249d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellfree_regs: 250d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell free_page(lg->regs_page); 251d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellrelease_guest: 252d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell memset(lg, 0, sizeof(*lg)); 253d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellunlock: 254d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_unlock(&lguest_lock); 255d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return err; 256d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 257d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 258dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/*L:010 The first operation the Launcher does must be a write. All writes 259dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * start with a 32 bit number: for the first write this must be 260dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use 261dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * writes of other values to get DMA buffers and send interrupts. */ 262d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic ssize_t write(struct file *file, const char __user *input, 263d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell size_t size, loff_t *off) 264d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 265dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Once the guest is initialized, we hold the "struct lguest" in the 266dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * file private data. */ 267d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg = file->private_data; 268d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell u32 req; 269d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 270d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (get_user(req, input) != 0) 271d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 272d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell input += sizeof(req); 273d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 274dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* If you haven't initialized, you must do that first. */ 275d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (req != LHREQ_INITIALIZE && !lg) 276d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EINVAL; 277dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 278dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Once the Guest is dead, all you can do is read() why it died. */ 279d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (lg && lg->dead) 280d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -ENOENT; 281d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 282d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell /* If you're not the task which owns the Guest, you can only break */ 283d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (lg && current != lg->tsk && req != LHREQ_BREAK) 284d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EPERM; 285d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 286d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell switch (req) { 287d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell case LHREQ_INITIALIZE: 288d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return initialize(file, (const u32 __user *)input); 289d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell case LHREQ_GETDMA: 290d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return user_get_dma(lg, (const u32 __user *)input); 291d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell case LHREQ_IRQ: 292d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return user_send_irq(lg, (const u32 __user *)input); 293d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell case LHREQ_BREAK: 294d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return break_guest_out(lg, (const u32 __user *)input); 295d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell default: 296d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EINVAL; 297d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 298d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 299d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 300dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/*L:060 The final piece of interface code is the close() routine. It reverses 301dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * everything done in initialize(). This is usually called because the 302dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Launcher exited. 303dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 304dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Note that the close routine returns 0 or a negative error number: it can't 305dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * really fail, but it can whine. I blame Sun for this wart, and K&R C for 306dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * letting them do it. :*/ 307d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic int close(struct inode *inode, struct file *file) 308d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 309d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg = file->private_data; 310d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 311dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* If we never successfully initialized, there's nothing to clean up */ 312d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!lg) 313d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return 0; 314d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 315dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We need the big lock, to protect from inter-guest I/O and other 316dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Launchers initializing guests. */ 317d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_lock(&lguest_lock); 318d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ 319d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell hrtimer_cancel(&lg->hrt); 320dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Free any DMA buffers the Guest had bound. */ 321d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell release_all_dma(lg); 322dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Free up the shadow page tables for the Guest. */ 323d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell free_guest_pagetable(lg); 324dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Now all the memory cleanups are done, it's safe to release the 325dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Launcher's memory management structure. */ 326d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mmput(lg->mm); 327dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* If lg->dead doesn't contain an error code it will be NULL or a 328dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * kmalloc()ed string, either of which is ok to hand to kfree(). */ 329d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!IS_ERR(lg->dead)) 330d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell kfree(lg->dead); 331dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We can free up the register page we allocated. */ 332d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell free_page(lg->regs_page); 333dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We clear the entire structure, which also marks it as free for the 334dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * next user. */ 335d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell memset(lg, 0, sizeof(*lg)); 336dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Release lock and exit. */ 337d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_unlock(&lguest_lock); 338dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 339d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return 0; 340d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 341d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 342dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/*L:000 343dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Welcome to our journey through the Launcher! 344dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 345dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * The Launcher is the Host userspace program which sets up, runs and services 346dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * the Guest. In fact, many comments in the Drivers which refer to "the Host" 347dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * doing things are inaccurate: the Launcher does all the device handling for 348dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * the Guest. The Guest can't tell what's done by the the Launcher and what by 349dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * the Host. 350dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 351dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we 352dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * shall see more of that later. 353dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 354dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * We begin our understanding with the Host kernel interface which the Launcher 355dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * uses: reading and writing a character device called /dev/lguest. All the 356dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * work happens in the read(), write() and close() routines: */ 357d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic struct file_operations lguest_fops = { 358d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .owner = THIS_MODULE, 359d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .release = close, 360d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .write = write, 361d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .read = read, 362d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell}; 363dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 364dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/* This is a textbook example of a "misc" character device. Populate a "struct 365dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * miscdevice" and register it with misc_register(). */ 366d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic struct miscdevice lguest_dev = { 367d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .minor = MISC_DYNAMIC_MINOR, 368d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .name = "lguest", 369d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .fops = &lguest_fops, 370d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell}; 371d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 372d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellint __init lguest_device_init(void) 373d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 374d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return misc_register(&lguest_dev); 375d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 376d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 377d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellvoid __exit lguest_device_remove(void) 378d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 379d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell misc_deregister(&lguest_dev); 380d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 381