lguest_user.c revision a91d74a3c4de8115295ee87350c13a329164aaaf
1a91d74a3c4de8115295ee87350c13a329164aaafRusty 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 3a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * tell us the Guest's memory layout and entry point. A read will run the 4a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Guest until something happens, such as a signal or the Guest doing a NOTIFY 5a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * out to the Launcher. 62e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell:*/ 7d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include <linux/uaccess.h> 8d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include <linux/miscdevice.h> 9d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include <linux/fs.h> 10ca94f2bdd1be626361fcfbd474d6b8823ed39f74Glauber de Oliveira Costa#include <linux/sched.h> 11df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell#include <linux/eventfd.h> 12df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell#include <linux/file.h> 13d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include "lg.h" 14d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 15a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell/*L:056 16a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Before we move on, let's jump ahead and look at what the kernel does when 17a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * it needs to look up the eventfds. That will complete our picture of how we 18a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * use RCU. 19a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 20a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * The notification value is in cpu->pending_notify: we return true if it went 21a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * to an eventfd. 22a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 23df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russellbool send_notify_to_eventfd(struct lg_cpu *cpu) 24df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell{ 25df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell unsigned int i; 26df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell struct lg_eventfd_map *map; 27df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 28a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 29a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * This "rcu_read_lock()" helps track when someone is still looking at 30a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * the (RCU-using) eventfds array. It's not actually a lock at all; 31a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * indeed it's a noop in many configurations. (You didn't expect me to 32a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * explain all the RCU secrets here, did you?) 33a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 34df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell rcu_read_lock(); 35a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 36a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * rcu_dereference is the counter-side of rcu_assign_pointer(); it 37a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * makes sure we don't access the memory pointed to by 38a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * cpu->lg->eventfds before cpu->lg->eventfds is set. Sounds crazy, 39a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * but Alpha allows this! Paul McKenney points out that a really 40a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * aggressive compiler could have the same effect: 41a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * http://lists.ozlabs.org/pipermail/lguest/2009-July/001560.html 42a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 43a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * So play safe, use rcu_dereference to get the rcu-protected pointer: 44a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 45df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell map = rcu_dereference(cpu->lg->eventfds); 46a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 47a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Simple array search: even if they add an eventfd while we do this, 48a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * we'll continue to use the old array and just won't see the new one. 49a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 50df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell for (i = 0; i < map->num; i++) { 51df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (map->map[i].addr == cpu->pending_notify) { 52df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell eventfd_signal(map->map[i].event, 1); 53df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell cpu->pending_notify = 0; 54df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell break; 55df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell } 56df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell } 57a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* We're done with the rcu-protected variable cpu->lg->eventfds. */ 58df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell rcu_read_unlock(); 59a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell 60a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* If we cleared the notification, it's because we found a match. */ 61df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return cpu->pending_notify == 0; 62df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell} 63df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 64a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell/*L:055 65a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * One of the more tricksy tricks in the Linux Kernel is a technique called 66a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Read Copy Update. Since one point of lguest is to teach lguest journeyers 67a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * about kernel coding, I use it here. (In case you're curious, other purposes 68a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * include learning about virtualization and instilling a deep appreciation for 69a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * simplicity and puppies). 70a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 71a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * We keep a simple array which maps LHCALL_NOTIFY values to eventfds, but we 72a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * add new eventfds without ever blocking readers from accessing the array. 73a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * The current Launcher only does this during boot, so that never happens. But 74a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Read Copy Update is cool, and adding a lock risks damaging even more puppies 75a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * than this code does. 76a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 77a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * We allocate a brand new one-larger array, copy the old one and add our new 78a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * element. Then we make the lg eventfd pointer point to the new array. 79a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * That's the easy part: now we need to free the old one, but we need to make 80a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * sure no slow CPU somewhere is still looking at it. That's what 81a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * synchronize_rcu does for us: waits until every CPU has indicated that it has 82a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * moved on to know it's no longer using the old one. 83a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 84a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * If that's unclear, see http://en.wikipedia.org/wiki/Read-copy-update. 85a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 86df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russellstatic int add_eventfd(struct lguest *lg, unsigned long addr, int fd) 87df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell{ 88df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell struct lg_eventfd_map *new, *old = lg->eventfds; 89df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 90a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 91a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * We don't allow notifications on value 0 anyway (pending_notify of 92a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 0 means "nothing pending"). 93a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 94df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (!addr) 95df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return -EINVAL; 96df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 972e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 982e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Replace the old array with the new one, carefully: others can 992e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * be accessing it at the same time. 1002e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 101df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), 102df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell GFP_KERNEL); 103df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (!new) 104df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return -ENOMEM; 105df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 106df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell /* First make identical copy. */ 107df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell memcpy(new->map, old->map, sizeof(old->map[0]) * old->num); 108df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell new->num = old->num; 109df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 110df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell /* Now append new entry. */ 111df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell new->map[new->num].addr = addr; 112133890103b9de08904f909995973e4b5c08a780eDavide Libenzi new->map[new->num].event = eventfd_ctx_fdget(fd); 113df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (IS_ERR(new->map[new->num].event)) { 114f294526279cda8934b0313ebd02184a16ba888c9Dan Carpenter int err = PTR_ERR(new->map[new->num].event); 115df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell kfree(new); 116f294526279cda8934b0313ebd02184a16ba888c9Dan Carpenter return err; 117df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell } 118df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell new->num++; 119df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 120a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 121a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Now put new one in place: rcu_assign_pointer() is a fancy way of 122a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * doing "lg->eventfds = new", but it uses memory barriers to make 123a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * absolutely sure that the contents of "new" written above is nailed 124a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * down before we actually do the assignment. 125a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 126a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * We have to think about these kinds of things when we're operating on 127a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * live data without locks. 128a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 129df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell rcu_assign_pointer(lg->eventfds, new); 130df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 1312e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 1322e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We're not in a big hurry. Wait until noone's looking at old 133a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * version, then free it. 1342e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 135df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell synchronize_rcu(); 136df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell kfree(old); 137df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 138df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return 0; 139df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell} 140df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 141a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell/*L:052 142a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Receiving notifications from the Guest is usually done by attaching a 143a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * particular LHCALL_NOTIFY value to an event filedescriptor. The eventfd will 144a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * become readable when the Guest does an LHCALL_NOTIFY with that value. 145a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 146a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * This is really convenient for processing each virtqueue in a separate 147a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * thread. 148a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 149df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russellstatic int attach_eventfd(struct lguest *lg, const unsigned long __user *input) 150df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell{ 151df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell unsigned long addr, fd; 152df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell int err; 153df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 154df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (get_user(addr, input) != 0) 155df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return -EFAULT; 156df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell input++; 157df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (get_user(fd, input) != 0) 158df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return -EFAULT; 159df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 160a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 161a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Just make sure two callers don't add eventfds at once. We really 162a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * only need to lock against callers adding to the same Guest, so using 163a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * the Big Lguest Lock is overkill. But this is setup, not a fast path. 164a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 165df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell mutex_lock(&lguest_lock); 166df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell err = add_eventfd(lg, addr, fd); 167df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell mutex_unlock(&lguest_lock); 168df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 169f294526279cda8934b0313ebd02184a16ba888c9Dan Carpenter return err; 170df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell} 171df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 1722e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:050 1732e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt 1742e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * number to /dev/lguest. 1752e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 176177e449dc5bd4cf8dc48d66abee61ddf34b126b9Glauber de Oliveira Costastatic int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) 177d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 178511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensen unsigned long irq; 179d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 180d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (get_user(irq, input) != 0) 181d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 182d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (irq >= LGUEST_IRQS) 183d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EINVAL; 1849f155a9b3d5a5444bcc5e049ec2547bb5107150eRusty Russell 185a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 186a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Next time the Guest runs, the core code will see if it can deliver 187a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * this interrupt. 188a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 1899f155a9b3d5a5444bcc5e049ec2547bb5107150eRusty Russell set_interrupt(cpu, irq); 190d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return 0; 191d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 192d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 1932e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:040 1942e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Once our Guest is initialized, the Launcher makes it run by reading 1952e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * from /dev/lguest. 1962e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 197d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) 198d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 199d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg = file->private_data; 200d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa struct lg_cpu *cpu; 201d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa unsigned int cpu_id = *o; 202d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 203dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* You must write LHREQ_INITIALIZE first! */ 204d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!lg) 205d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EINVAL; 206d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 207d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa /* Watch out for arbitrary vcpu indexes! */ 208d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa if (cpu_id >= lg->nr_cpus) 209d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa return -EINVAL; 210d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa 211d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa cpu = &lg->cpus[cpu_id]; 212d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa 213e1e72965ec2c02db99b415cd06c17ea90767e3a4Rusty Russell /* If you're not the task which owns the Guest, go away. */ 21466686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa if (current != cpu->tsk) 215d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EPERM; 216d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 217a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* If the Guest is already dead, we indicate why */ 218d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (lg->dead) { 219d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell size_t len; 220d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 221dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* lg->dead either contains an error code, or a string. */ 222d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (IS_ERR(lg->dead)) 223d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return PTR_ERR(lg->dead); 224d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 225dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We can only return as much as the buffer they read with. */ 226d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell len = min(size, strlen(lg->dead)+1); 227d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (copy_to_user(user, lg->dead, len) != 0) 228d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 229d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return len; 230d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 231d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 2322e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2332e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * If we returned from read() last time because the Guest sent I/O, 2342e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * clear the flag. 2352e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 2365e232f4f428c4266ba5cdae9f23ba19a0913dcf9Glauber de Oliveira Costa if (cpu->pending_notify) 2375e232f4f428c4266ba5cdae9f23ba19a0913dcf9Glauber de Oliveira Costa cpu->pending_notify = 0; 238d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 239dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Run the Guest until something interesting happens. */ 240d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa return run_guest(cpu, (unsigned long __user *)user); 241d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 242d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 2432e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:025 2442e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * This actually initializes a CPU. For the moment, a Guest is only 2452e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * uniprocessor, so "id" is always 0. 2462e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 2474dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costastatic int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) 2484dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa{ 249a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* We have a limited number the number of CPUs in the lguest struct. */ 25024adf12722b4f2800e5b5f0955d57033f0d0c9e5Rusty Russell if (id >= ARRAY_SIZE(cpu->lg->cpus)) 2514dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa return -EINVAL; 2524dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa 253a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* Set up this CPU's id, and pointer back to the lguest struct. */ 2544dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa cpu->id = id; 2554dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa cpu->lg = container_of((cpu - id), struct lguest, cpus[0]); 2564dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa cpu->lg->nr_cpus++; 257a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell 258a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* Each CPU has a timer it can set. */ 259ad8d8f3bc61ec712dd141e1029ae68c47fadc4a7Glauber de Oliveira Costa init_clockdev(cpu); 2604dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa 2612e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2622e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We need a complete page for the Guest registers: they are accessible 2632e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * to the Guest and we can only grant it access to whole pages. 2642e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 265a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa cpu->regs_page = get_zeroed_page(GFP_KERNEL); 266a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa if (!cpu->regs_page) 267a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa return -ENOMEM; 268a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa 269a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa /* We actually put the registers at the bottom of the page. */ 270a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); 271a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa 2722e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2732e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Now we initialize the Guest's registers, handing it the start 2742e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * address. 2752e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 276a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa lguest_arch_setup_regs(cpu, start_ip); 277a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa 2782e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2792e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We keep a pointer to the Launcher task (ie. current task) for when 2802e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * other Guests want to wake this one (eg. console input). 2812e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 28266686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa cpu->tsk = current; 28366686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa 2842e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2852e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We need to keep a pointer to the Launcher's memory map, because if 28666686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa * the Launcher dies we need to clean it up. If we don't keep a 2872e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * reference, it is destroyed before close() is called. 2882e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 28966686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa cpu->mm = get_task_mm(cpu->tsk); 29066686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa 2912e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2922e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We remember which CPU's pages this Guest used last, for optimization 2932e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * when the same Guest runs on the same CPU twice. 2942e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 295f34f8c5fea079065671163c37d98328cff31980bGlauber de Oliveira Costa cpu->last_pages = NULL; 296f34f8c5fea079065671163c37d98328cff31980bGlauber de Oliveira Costa 297a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* No error == success. */ 2984dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa return 0; 2994dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa} 3004dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa 3012e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:020 3022e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in 3032e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * addition to the LHREQ_INITIALIZE value). These are: 304dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 3053c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell * base: The start of the Guest-physical memory inside the Launcher memory. 3063c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell * 307dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * pfnlimit: The highest (Guest-physical) page number the Guest should be 308e1e72965ec2c02db99b415cd06c17ea90767e3a4Rusty Russell * allowed to access. The Guest memory lives inside the Launcher, so it sets 309e1e72965ec2c02db99b415cd06c17ea90767e3a4Rusty Russell * this to ensure the Guest can only reach its own memory. 310dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 311dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * start: The first instruction to execute ("eip" in x86-speak). 312dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell */ 313511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensenstatic int initialize(struct file *file, const unsigned long __user *input) 314d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 3152e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* "struct lguest" contains all we (the Host) know about a Guest. */ 316d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg; 31748245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell int err; 31858a24566449892dda409b9ad92c2e56c76c5670cMatias Zabaljauregui unsigned long args[3]; 319d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 3202e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 3212e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We grab the Big Lguest lock, which protects against multiple 3222e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * simultaneous initializations. 3232e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 324d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_lock(&lguest_lock); 325dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* You can't initialize twice! Close the device and start again... */ 326d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (file->private_data) { 327d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell err = -EBUSY; 328d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto unlock; 329d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 330d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 331d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (copy_from_user(args, input, sizeof(args)) != 0) { 332d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell err = -EFAULT; 333d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto unlock; 334d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 335d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 33648245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell lg = kzalloc(sizeof(*lg), GFP_KERNEL); 33748245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell if (!lg) { 33848245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell err = -ENOMEM; 339d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto unlock; 340d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 341dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 342df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL); 343df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (!lg->eventfds) { 344df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell err = -ENOMEM; 345df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell goto free_lg; 346df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell } 347df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell lg->eventfds->num = 0; 348df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 349dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Populate the easy fields of our "struct lguest" */ 35074dbf719ed3c49687dab507967ebab9189e91ab0Al Viro lg->mem_base = (void __user *)args[0]; 3513c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell lg->pfn_limit = args[1]; 352dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 35358a24566449892dda409b9ad92c2e56c76c5670cMatias Zabaljauregui /* This is the first cpu (cpu 0) and it will start booting at args[2] */ 35458a24566449892dda409b9ad92c2e56c76c5670cMatias Zabaljauregui err = lg_cpu_start(&lg->cpus[0], 0, args[2]); 3554dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa if (err) 356df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell goto free_eventfds; 3574dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa 3582e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 3592e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Initialize the Guest's shadow page tables, using the toplevel 3602e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * address the Launcher gave us. This allocates memory, so can fail. 3612e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 36258a24566449892dda409b9ad92c2e56c76c5670cMatias Zabaljauregui err = init_guest_pagetable(lg); 363d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (err) 364d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto free_regs; 365d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 366dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We keep our "struct lguest" in the file's private_data. */ 367d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell file->private_data = lg; 368d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 369d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_unlock(&lguest_lock); 370d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 371dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* And because this is a write() call, we return the length used. */ 372d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return sizeof(args); 373d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 374d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellfree_regs: 375a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa /* FIXME: This should be in free_vcpu */ 376a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa free_page(lg->cpus[0].regs_page); 377df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russellfree_eventfds: 378df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell kfree(lg->eventfds); 379df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russellfree_lg: 38043054412db5e5b3eda1eff6c2245ff4257560340Adrian Bunk kfree(lg); 381d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellunlock: 382d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_unlock(&lguest_lock); 383d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return err; 384d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 385d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 3862e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:010 3872e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * The first operation the Launcher does must be a write. All writes 388e1e72965ec2c02db99b415cd06c17ea90767e3a4Rusty Russell * start with an unsigned long number: for the first write this must be 389dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use 390a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * writes of other values to send interrupts or set up receipt of notifications. 391a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell * 392a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell * Note that we overload the "offset" in the /dev/lguest file to indicate what 393a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * CPU number we're dealing with. Currently this is always 0 since we only 394a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell * support uniprocessor Guests, but you can see the beginnings of SMP support 3952e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * here. 3962e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 397511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensenstatic ssize_t write(struct file *file, const char __user *in, 398d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell size_t size, loff_t *off) 399d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 4002e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 4012e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Once the Guest is initialized, we hold the "struct lguest" in the 4022e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * file private data. 4032e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 404d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg = file->private_data; 405511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensen const unsigned long __user *input = (const unsigned long __user *)in; 406511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensen unsigned long req; 407177e449dc5bd4cf8dc48d66abee61ddf34b126b9Glauber de Oliveira Costa struct lg_cpu *uninitialized_var(cpu); 4087ea07a1500f05e06ebf0136763c781244f77a2a1Glauber de Oliveira Costa unsigned int cpu_id = *off; 409d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 410a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* The first value tells us what this request is. */ 411d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (get_user(req, input) != 0) 412d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 413511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensen input++; 414d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 415dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* If you haven't initialized, you must do that first. */ 4167ea07a1500f05e06ebf0136763c781244f77a2a1Glauber de Oliveira Costa if (req != LHREQ_INITIALIZE) { 4177ea07a1500f05e06ebf0136763c781244f77a2a1Glauber de Oliveira Costa if (!lg || (cpu_id >= lg->nr_cpus)) 4187ea07a1500f05e06ebf0136763c781244f77a2a1Glauber de Oliveira Costa return -EINVAL; 4197ea07a1500f05e06ebf0136763c781244f77a2a1Glauber de Oliveira Costa cpu = &lg->cpus[cpu_id]; 420dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 421f73d1e6ca6985b43a1871467463cba632fbc624dEugene Teo /* Once the Guest is dead, you can only read() why it died. */ 422f73d1e6ca6985b43a1871467463cba632fbc624dEugene Teo if (lg->dead) 423f73d1e6ca6985b43a1871467463cba632fbc624dEugene Teo return -ENOENT; 424f73d1e6ca6985b43a1871467463cba632fbc624dEugene Teo } 425d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 426d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell switch (req) { 427d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell case LHREQ_INITIALIZE: 428511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensen return initialize(file, input); 429d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell case LHREQ_IRQ: 430177e449dc5bd4cf8dc48d66abee61ddf34b126b9Glauber de Oliveira Costa return user_send_irq(cpu, input); 431df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell case LHREQ_EVENTFD: 432df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return attach_eventfd(lg, input); 433d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell default: 434d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EINVAL; 435d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 436d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 437d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 4382e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:060 4392e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * The final piece of interface code is the close() routine. It reverses 440dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * everything done in initialize(). This is usually called because the 441dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Launcher exited. 442dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 443dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Note that the close routine returns 0 or a negative error number: it can't 444dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * really fail, but it can whine. I blame Sun for this wart, and K&R C for 4452e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * letting them do it. 4462e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell:*/ 447d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic int close(struct inode *inode, struct file *file) 448d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 449d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg = file->private_data; 450ad8d8f3bc61ec712dd141e1029ae68c47fadc4a7Glauber de Oliveira Costa unsigned int i; 451d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 452dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* If we never successfully initialized, there's nothing to clean up */ 453d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!lg) 454d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return 0; 455d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 4562e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 4572e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We need the big lock, to protect from inter-guest I/O and other 4582e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Launchers initializing guests. 4592e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 460d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_lock(&lguest_lock); 46166686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa 46266686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa /* Free up the shadow page tables for the Guest. */ 46366686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa free_guest_pagetable(lg); 46466686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa 465a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa for (i = 0; i < lg->nr_cpus; i++) { 466ad8d8f3bc61ec712dd141e1029ae68c47fadc4a7Glauber de Oliveira Costa /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ 467ad8d8f3bc61ec712dd141e1029ae68c47fadc4a7Glauber de Oliveira Costa hrtimer_cancel(&lg->cpus[i].hrt); 468a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa /* We can free up the register page we allocated. */ 469a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa free_page(lg->cpus[i].regs_page); 4702e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 4712e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Now all the memory cleanups are done, it's safe to release 4722e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * the Launcher's memory management structure. 4732e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 47466686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa mmput(lg->cpus[i].mm); 475a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa } 476df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 477df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell /* Release any eventfds they registered. */ 478df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell for (i = 0; i < lg->eventfds->num; i++) 479133890103b9de08904f909995973e4b5c08a780eDavide Libenzi eventfd_ctx_put(lg->eventfds->map[i].event); 480df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell kfree(lg->eventfds); 481df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 4822e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 4832e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * If lg->dead doesn't contain an error code it will be NULL or a 4842e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * kmalloc()ed string, either of which is ok to hand to kfree(). 4852e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 486d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!IS_ERR(lg->dead)) 487d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell kfree(lg->dead); 48805dfdbbd678ea2b642db73f48b75667a23d15484Mark Wallis /* Free the memory allocated to the lguest_struct */ 48905dfdbbd678ea2b642db73f48b75667a23d15484Mark Wallis kfree(lg); 490dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Release lock and exit. */ 491d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_unlock(&lguest_lock); 492dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 493d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return 0; 494d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 495d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 496dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/*L:000 497dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Welcome to our journey through the Launcher! 498dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 499dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * The Launcher is the Host userspace program which sets up, runs and services 500dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * the Guest. In fact, many comments in the Drivers which refer to "the Host" 501dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * doing things are inaccurate: the Launcher does all the device handling for 502e1e72965ec2c02db99b415cd06c17ea90767e3a4Rusty Russell * the Guest, but the Guest can't know that. 503dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 504dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we 505dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * shall see more of that later. 506dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 507dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * We begin our understanding with the Host kernel interface which the Launcher 508dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * uses: reading and writing a character device called /dev/lguest. All the 5092e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * work happens in the read(), write() and close() routines: 5102e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 511d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic struct file_operations lguest_fops = { 512d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .owner = THIS_MODULE, 513d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .release = close, 514d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .write = write, 515d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .read = read, 516d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell}; 517dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 5182e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/* 5192e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * This is a textbook example of a "misc" character device. Populate a "struct 5202e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * miscdevice" and register it with misc_register(). 5212e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 522d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic struct miscdevice lguest_dev = { 523d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .minor = MISC_DYNAMIC_MINOR, 524d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .name = "lguest", 525d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .fops = &lguest_fops, 526d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell}; 527d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 528d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellint __init lguest_device_init(void) 529d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 530d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return misc_register(&lguest_dev); 531d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 532d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 533d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellvoid __exit lguest_device_remove(void) 534d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 535d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell misc_deregister(&lguest_dev); 536d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 537