19f54288def3f92b7805eb6d4b1ddcd73ecf6e889Rusty Russell/*P:200 This contains all the /dev/lguest code, whereby the userspace 29f54288def3f92b7805eb6d4b1ddcd73ecf6e889Rusty Russell * launcher controls and communicates with the Guest. For example, 39f54288def3f92b7805eb6d4b1ddcd73ecf6e889Rusty Russell * the first write will tell us the Guest's memory layout and entry 49f54288def3f92b7805eb6d4b1ddcd73ecf6e889Rusty Russell * point. A read will run the Guest until something happens, such as 59f54288def3f92b7805eb6d4b1ddcd73ecf6e889Rusty Russell * a signal or the Guest doing a NOTIFY out to the Launcher. There is 69f54288def3f92b7805eb6d4b1ddcd73ecf6e889Rusty Russell * also a way for the Launcher to attach eventfds to particular NOTIFY 79f54288def3f92b7805eb6d4b1ddcd73ecf6e889Rusty Russell * values instead of returning from the read() call. 82e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell:*/ 9d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include <linux/uaccess.h> 10d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include <linux/miscdevice.h> 11d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include <linux/fs.h> 12ca94f2bdd1be626361fcfbd474d6b8823ed39f74Glauber de Oliveira Costa#include <linux/sched.h> 13df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell#include <linux/eventfd.h> 14df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell#include <linux/file.h> 155a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h> 1639a0e33da0189c99ed3cea6945cda1bc9f4b7b83Paul Gortmaker#include <linux/export.h> 17d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell#include "lg.h" 18d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 19a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell/*L:056 20a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Before we move on, let's jump ahead and look at what the kernel does when 21a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * it needs to look up the eventfds. That will complete our picture of how we 22a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * use RCU. 23a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 24a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * The notification value is in cpu->pending_notify: we return true if it went 25a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * to an eventfd. 26a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 27df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russellbool send_notify_to_eventfd(struct lg_cpu *cpu) 28df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell{ 29df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell unsigned int i; 30df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell struct lg_eventfd_map *map; 31df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 32a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 33a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * This "rcu_read_lock()" helps track when someone is still looking at 34a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * the (RCU-using) eventfds array. It's not actually a lock at all; 35a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * indeed it's a noop in many configurations. (You didn't expect me to 36a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * explain all the RCU secrets here, did you?) 37a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 38df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell rcu_read_lock(); 39a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 40a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * rcu_dereference is the counter-side of rcu_assign_pointer(); it 41a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * makes sure we don't access the memory pointed to by 42a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * cpu->lg->eventfds before cpu->lg->eventfds is set. Sounds crazy, 43a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * but Alpha allows this! Paul McKenney points out that a really 44a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * aggressive compiler could have the same effect: 45a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * http://lists.ozlabs.org/pipermail/lguest/2009-July/001560.html 46a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 47a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * So play safe, use rcu_dereference to get the rcu-protected pointer: 48a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 49df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell map = rcu_dereference(cpu->lg->eventfds); 50a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 51a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Simple array search: even if they add an eventfd while we do this, 52a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * we'll continue to use the old array and just won't see the new one. 53a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 54df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell for (i = 0; i < map->num; i++) { 55df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (map->map[i].addr == cpu->pending_notify) { 56df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell eventfd_signal(map->map[i].event, 1); 57df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell cpu->pending_notify = 0; 58df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell break; 59df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell } 60df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell } 61a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* We're done with the rcu-protected variable cpu->lg->eventfds. */ 62df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell rcu_read_unlock(); 63a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell 64a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* If we cleared the notification, it's because we found a match. */ 65df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return cpu->pending_notify == 0; 66df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell} 67df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 68a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell/*L:055 69a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * One of the more tricksy tricks in the Linux Kernel is a technique called 70a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Read Copy Update. Since one point of lguest is to teach lguest journeyers 71a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * about kernel coding, I use it here. (In case you're curious, other purposes 72a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * include learning about virtualization and instilling a deep appreciation for 73a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * simplicity and puppies). 74a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 75a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * We keep a simple array which maps LHCALL_NOTIFY values to eventfds, but we 76a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * add new eventfds without ever blocking readers from accessing the array. 77a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * The current Launcher only does this during boot, so that never happens. But 78a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Read Copy Update is cool, and adding a lock risks damaging even more puppies 79a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * than this code does. 80a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 81a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * We allocate a brand new one-larger array, copy the old one and add our new 82a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * element. Then we make the lg eventfd pointer point to the new array. 83a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * That's the easy part: now we need to free the old one, but we need to make 84a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * sure no slow CPU somewhere is still looking at it. That's what 85a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * synchronize_rcu does for us: waits until every CPU has indicated that it has 86a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * moved on to know it's no longer using the old one. 87a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 88a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * If that's unclear, see http://en.wikipedia.org/wiki/Read-copy-update. 89a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 90df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russellstatic int add_eventfd(struct lguest *lg, unsigned long addr, int fd) 91df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell{ 92df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell struct lg_eventfd_map *new, *old = lg->eventfds; 93df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 94a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 95a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * We don't allow notifications on value 0 anyway (pending_notify of 96a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 0 means "nothing pending"). 97a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 98df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (!addr) 99df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return -EINVAL; 100df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 1012e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 1022e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Replace the old array with the new one, carefully: others can 1032e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * be accessing it at the same time. 1042e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 105df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), 106df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell GFP_KERNEL); 107df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (!new) 108df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return -ENOMEM; 109df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 110df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell /* First make identical copy. */ 111df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell memcpy(new->map, old->map, sizeof(old->map[0]) * old->num); 112df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell new->num = old->num; 113df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 114df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell /* Now append new entry. */ 115df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell new->map[new->num].addr = addr; 116133890103b9de08904f909995973e4b5c08a780eDavide Libenzi new->map[new->num].event = eventfd_ctx_fdget(fd); 117df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (IS_ERR(new->map[new->num].event)) { 118f294526279cda8934b0313ebd02184a16ba888c9Dan Carpenter int err = PTR_ERR(new->map[new->num].event); 119df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell kfree(new); 120f294526279cda8934b0313ebd02184a16ba888c9Dan Carpenter return err; 121df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell } 122df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell new->num++; 123df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 124a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 125a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Now put new one in place: rcu_assign_pointer() is a fancy way of 126a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * doing "lg->eventfds = new", but it uses memory barriers to make 127a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * absolutely sure that the contents of "new" written above is nailed 128a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * down before we actually do the assignment. 129a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 130a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * We have to think about these kinds of things when we're operating on 131a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * live data without locks. 132a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 133df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell rcu_assign_pointer(lg->eventfds, new); 134df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 1352e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 13625985edcedea6396277003854657b5f3cb31a628Lucas De Marchi * We're not in a big hurry. Wait until no one's looking at old 137a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * version, then free it. 1382e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 139df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell synchronize_rcu(); 140df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell kfree(old); 141df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 142df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return 0; 143df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell} 144df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 145a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell/*L:052 146a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Receiving notifications from the Guest is usually done by attaching a 147a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * particular LHCALL_NOTIFY value to an event filedescriptor. The eventfd will 148a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * become readable when the Guest does an LHCALL_NOTIFY with that value. 149a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * 150a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * This is really convenient for processing each virtqueue in a separate 151a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * thread. 152a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 153df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russellstatic int attach_eventfd(struct lguest *lg, const unsigned long __user *input) 154df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell{ 155df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell unsigned long addr, fd; 156df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell int err; 157df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 158df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (get_user(addr, input) != 0) 159df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return -EFAULT; 160df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell input++; 161df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (get_user(fd, input) != 0) 162df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return -EFAULT; 163df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 164a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 165a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Just make sure two callers don't add eventfds at once. We really 166a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * only need to lock against callers adding to the same Guest, so using 167a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * the Big Lguest Lock is overkill. But this is setup, not a fast path. 168a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 169df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell mutex_lock(&lguest_lock); 170df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell err = add_eventfd(lg, addr, fd); 171df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell mutex_unlock(&lguest_lock); 172df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 173f294526279cda8934b0313ebd02184a16ba888c9Dan Carpenter return err; 174df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell} 175df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 1762e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:050 1772e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt 1782e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * number to /dev/lguest. 1792e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 180177e449dc5bd4cf8dc48d66abee61ddf34b126b9Glauber de Oliveira Costastatic int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) 181d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 182511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensen unsigned long irq; 183d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 184d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (get_user(irq, input) != 0) 185d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 186d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (irq >= LGUEST_IRQS) 187d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EINVAL; 1889f155a9b3d5a5444bcc5e049ec2547bb5107150eRusty Russell 189a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell /* 190a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * Next time the Guest runs, the core code will see if it can deliver 191a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * this interrupt. 192a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell */ 1939f155a9b3d5a5444bcc5e049ec2547bb5107150eRusty Russell set_interrupt(cpu, irq); 194d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return 0; 195d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 196d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 1972e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:040 1982e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Once our Guest is initialized, the Launcher makes it run by reading 1992e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * from /dev/lguest. 2002e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 201d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) 202d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 203d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg = file->private_data; 204d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa struct lg_cpu *cpu; 205d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa unsigned int cpu_id = *o; 206d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 207dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* You must write LHREQ_INITIALIZE first! */ 208d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!lg) 209d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EINVAL; 210d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 211d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa /* Watch out for arbitrary vcpu indexes! */ 212d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa if (cpu_id >= lg->nr_cpus) 213d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa return -EINVAL; 214d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa 215d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa cpu = &lg->cpus[cpu_id]; 216d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa 217e1e72965ec2c02db99b415cd06c17ea90767e3a4Rusty Russell /* If you're not the task which owns the Guest, go away. */ 21866686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa if (current != cpu->tsk) 219d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EPERM; 220d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 221a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* If the Guest is already dead, we indicate why */ 222d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (lg->dead) { 223d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell size_t len; 224d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 225dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* lg->dead either contains an error code, or a string. */ 226d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (IS_ERR(lg->dead)) 227d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return PTR_ERR(lg->dead); 228d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 229dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We can only return as much as the buffer they read with. */ 230d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell len = min(size, strlen(lg->dead)+1); 231d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (copy_to_user(user, lg->dead, len) != 0) 232d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 233d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return len; 234d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 235d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 2362e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2372e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * If we returned from read() last time because the Guest sent I/O, 2382e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * clear the flag. 2392e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 2405e232f4f428c4266ba5cdae9f23ba19a0913dcf9Glauber de Oliveira Costa if (cpu->pending_notify) 2415e232f4f428c4266ba5cdae9f23ba19a0913dcf9Glauber de Oliveira Costa cpu->pending_notify = 0; 242d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 243dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Run the Guest until something interesting happens. */ 244d0953d42c3445a120299fac9ad70e672d77898e9Glauber de Oliveira Costa return run_guest(cpu, (unsigned long __user *)user); 245d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 246d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 2472e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:025 2482e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * This actually initializes a CPU. For the moment, a Guest is only 2492e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * uniprocessor, so "id" is always 0. 2502e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 2514dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costastatic int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) 2524dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa{ 253c2ecd51531c881c8d47d77ea30395f7f03d42da3Cosmin Paraschiv /* We have a limited number of CPUs in the lguest struct. */ 25424adf12722b4f2800e5b5f0955d57033f0d0c9e5Rusty Russell if (id >= ARRAY_SIZE(cpu->lg->cpus)) 2554dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa return -EINVAL; 2564dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa 257a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* Set up this CPU's id, and pointer back to the lguest struct. */ 2584dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa cpu->id = id; 259c2ecd51531c881c8d47d77ea30395f7f03d42da3Cosmin Paraschiv cpu->lg = container_of(cpu, struct lguest, cpus[id]); 2604dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa cpu->lg->nr_cpus++; 261a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell 262a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* Each CPU has a timer it can set. */ 263ad8d8f3bc61ec712dd141e1029ae68c47fadc4a7Glauber de Oliveira Costa init_clockdev(cpu); 2644dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa 2652e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2662e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We need a complete page for the Guest registers: they are accessible 2672e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * to the Guest and we can only grant it access to whole pages. 2682e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 269a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa cpu->regs_page = get_zeroed_page(GFP_KERNEL); 270a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa if (!cpu->regs_page) 271a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa return -ENOMEM; 272a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa 273c2ecd51531c881c8d47d77ea30395f7f03d42da3Cosmin Paraschiv /* We actually put the registers at the end of the page. */ 274a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); 275a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa 2762e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2772e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Now we initialize the Guest's registers, handing it the start 2782e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * address. 2792e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 280a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa lguest_arch_setup_regs(cpu, start_ip); 281a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa 2822e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2832e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We keep a pointer to the Launcher task (ie. current task) for when 2842e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * other Guests want to wake this one (eg. console input). 2852e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 28666686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa cpu->tsk = current; 28766686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa 2882e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2892e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We need to keep a pointer to the Launcher's memory map, because if 29066686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa * the Launcher dies we need to clean it up. If we don't keep a 2912e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * reference, it is destroyed before close() is called. 2922e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 29366686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa cpu->mm = get_task_mm(cpu->tsk); 29466686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa 2952e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 2962e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We remember which CPU's pages this Guest used last, for optimization 2972e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * when the same Guest runs on the same CPU twice. 2982e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 299f34f8c5fea079065671163c37d98328cff31980bGlauber de Oliveira Costa cpu->last_pages = NULL; 300f34f8c5fea079065671163c37d98328cff31980bGlauber de Oliveira Costa 301a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* No error == success. */ 3024dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa return 0; 3034dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa} 3044dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa 3052e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:020 3062e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in 3072e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * addition to the LHREQ_INITIALIZE value). These are: 308dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 3093c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell * base: The start of the Guest-physical memory inside the Launcher memory. 3103c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell * 311dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * pfnlimit: The highest (Guest-physical) page number the Guest should be 312e1e72965ec2c02db99b415cd06c17ea90767e3a4Rusty Russell * allowed to access. The Guest memory lives inside the Launcher, so it sets 313e1e72965ec2c02db99b415cd06c17ea90767e3a4Rusty Russell * this to ensure the Guest can only reach its own memory. 314dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 315dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * start: The first instruction to execute ("eip" in x86-speak). 316dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell */ 317511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensenstatic int initialize(struct file *file, const unsigned long __user *input) 318d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 3192e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* "struct lguest" contains all we (the Host) know about a Guest. */ 320d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg; 32148245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell int err; 32258a24566449892dda409b9ad92c2e56c76c5670cMatias Zabaljauregui unsigned long args[3]; 323d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 3242e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 3252e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We grab the Big Lguest lock, which protects against multiple 3262e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * simultaneous initializations. 3272e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 328d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_lock(&lguest_lock); 329dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* You can't initialize twice! Close the device and start again... */ 330d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (file->private_data) { 331d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell err = -EBUSY; 332d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto unlock; 333d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 334d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 335d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (copy_from_user(args, input, sizeof(args)) != 0) { 336d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell err = -EFAULT; 337d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto unlock; 338d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 339d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 34048245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell lg = kzalloc(sizeof(*lg), GFP_KERNEL); 34148245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell if (!lg) { 34248245cc0708d49d1d0566b9fa617ad6c5f4c6934Rusty Russell err = -ENOMEM; 343d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto unlock; 344d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 345dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 346df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL); 347df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell if (!lg->eventfds) { 348df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell err = -ENOMEM; 349df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell goto free_lg; 350df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell } 351df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell lg->eventfds->num = 0; 352df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 353dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Populate the easy fields of our "struct lguest" */ 35474dbf719ed3c49687dab507967ebab9189e91ab0Al Viro lg->mem_base = (void __user *)args[0]; 3553c6b5bfa3cf3b4057788e08482a468cc3bc00780Rusty Russell lg->pfn_limit = args[1]; 356dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 35758a24566449892dda409b9ad92c2e56c76c5670cMatias Zabaljauregui /* This is the first cpu (cpu 0) and it will start booting at args[2] */ 35858a24566449892dda409b9ad92c2e56c76c5670cMatias Zabaljauregui err = lg_cpu_start(&lg->cpus[0], 0, args[2]); 3594dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa if (err) 360df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell goto free_eventfds; 3614dcc53da49c2387078fe8ceb7a420d125e027fc6Glauber de Oliveira Costa 3622e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 3639f54288def3f92b7805eb6d4b1ddcd73ecf6e889Rusty Russell * Initialize the Guest's shadow page tables. This allocates 3649f54288def3f92b7805eb6d4b1ddcd73ecf6e889Rusty Russell * memory, so can fail. 3652e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 36658a24566449892dda409b9ad92c2e56c76c5670cMatias Zabaljauregui err = init_guest_pagetable(lg); 367d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (err) 368d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell goto free_regs; 369d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 370dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* We keep our "struct lguest" in the file's private_data. */ 371d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell file->private_data = lg; 372d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 373d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_unlock(&lguest_lock); 374d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 375dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* And because this is a write() call, we return the length used. */ 376d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return sizeof(args); 377d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 378d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellfree_regs: 379a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa /* FIXME: This should be in free_vcpu */ 380a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa free_page(lg->cpus[0].regs_page); 381df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russellfree_eventfds: 382df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell kfree(lg->eventfds); 383df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russellfree_lg: 38443054412db5e5b3eda1eff6c2245ff4257560340Adrian Bunk kfree(lg); 385d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellunlock: 386d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_unlock(&lguest_lock); 387d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return err; 388d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 389d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 3902e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:010 3912e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * The first operation the Launcher does must be a write. All writes 392e1e72965ec2c02db99b415cd06c17ea90767e3a4Rusty Russell * start with an unsigned long number: for the first write this must be 393dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use 394a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * writes of other values to send interrupts or set up receipt of notifications. 395a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell * 396a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell * Note that we overload the "offset" in the /dev/lguest file to indicate what 397a91d74a3c4de8115295ee87350c13a329164aaafRusty Russell * CPU number we're dealing with. Currently this is always 0 since we only 398a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell * support uniprocessor Guests, but you can see the beginnings of SMP support 3992e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * here. 4002e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 401511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensenstatic ssize_t write(struct file *file, const char __user *in, 402d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell size_t size, loff_t *off) 403d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 4042e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 4052e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Once the Guest is initialized, we hold the "struct lguest" in the 4062e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * file private data. 4072e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 408d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg = file->private_data; 409511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensen const unsigned long __user *input = (const unsigned long __user *)in; 410511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensen unsigned long req; 411177e449dc5bd4cf8dc48d66abee61ddf34b126b9Glauber de Oliveira Costa struct lg_cpu *uninitialized_var(cpu); 4127ea07a1500f05e06ebf0136763c781244f77a2a1Glauber de Oliveira Costa unsigned int cpu_id = *off; 413d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 414a6bd8e13034dd7d60b6f14217096efa192d0adc1Rusty Russell /* The first value tells us what this request is. */ 415d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (get_user(req, input) != 0) 416d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EFAULT; 417511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensen input++; 418d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 419dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* If you haven't initialized, you must do that first. */ 4207ea07a1500f05e06ebf0136763c781244f77a2a1Glauber de Oliveira Costa if (req != LHREQ_INITIALIZE) { 4217ea07a1500f05e06ebf0136763c781244f77a2a1Glauber de Oliveira Costa if (!lg || (cpu_id >= lg->nr_cpus)) 4227ea07a1500f05e06ebf0136763c781244f77a2a1Glauber de Oliveira Costa return -EINVAL; 4237ea07a1500f05e06ebf0136763c781244f77a2a1Glauber de Oliveira Costa cpu = &lg->cpus[cpu_id]; 424dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 425f73d1e6ca6985b43a1871467463cba632fbc624dEugene Teo /* Once the Guest is dead, you can only read() why it died. */ 426f73d1e6ca6985b43a1871467463cba632fbc624dEugene Teo if (lg->dead) 427f73d1e6ca6985b43a1871467463cba632fbc624dEugene Teo return -ENOENT; 428f73d1e6ca6985b43a1871467463cba632fbc624dEugene Teo } 429d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 430d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell switch (req) { 431d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell case LHREQ_INITIALIZE: 432511801dc31c095b2bfe3bf5c6a370dbe9b042a70Jes Sorensen return initialize(file, input); 433d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell case LHREQ_IRQ: 434177e449dc5bd4cf8dc48d66abee61ddf34b126b9Glauber de Oliveira Costa return user_send_irq(cpu, input); 435df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell case LHREQ_EVENTFD: 436df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell return attach_eventfd(lg, input); 437d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell default: 438d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return -EINVAL; 439d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell } 440d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 441d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 4422e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/*L:060 4432e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * The final piece of interface code is the close() routine. It reverses 444dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * everything done in initialize(). This is usually called because the 445dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Launcher exited. 446dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 447dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Note that the close routine returns 0 or a negative error number: it can't 448dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * really fail, but it can whine. I blame Sun for this wart, and K&R C for 4492e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * letting them do it. 4502e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell:*/ 451d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic int close(struct inode *inode, struct file *file) 452d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 453d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell struct lguest *lg = file->private_data; 454ad8d8f3bc61ec712dd141e1029ae68c47fadc4a7Glauber de Oliveira Costa unsigned int i; 455d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 456dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* If we never successfully initialized, there's nothing to clean up */ 457d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!lg) 458d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return 0; 459d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 4602e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 4612e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * We need the big lock, to protect from inter-guest I/O and other 4622e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Launchers initializing guests. 4632e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 464d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_lock(&lguest_lock); 46566686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa 46666686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa /* Free up the shadow page tables for the Guest. */ 46766686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa free_guest_pagetable(lg); 46866686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa 469a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa for (i = 0; i < lg->nr_cpus; i++) { 470ad8d8f3bc61ec712dd141e1029ae68c47fadc4a7Glauber de Oliveira Costa /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ 471ad8d8f3bc61ec712dd141e1029ae68c47fadc4a7Glauber de Oliveira Costa hrtimer_cancel(&lg->cpus[i].hrt); 472a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa /* We can free up the register page we allocated. */ 473a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa free_page(lg->cpus[i].regs_page); 4742e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 4752e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * Now all the memory cleanups are done, it's safe to release 4762e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * the Launcher's memory management structure. 4772e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 47866686c2ab08feb721ca4d98285fba64acdf6017fGlauber de Oliveira Costa mmput(lg->cpus[i].mm); 479a53a35a8b485b9c16b73e5177bddaa4321971199Glauber de Oliveira Costa } 480df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 481df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell /* Release any eventfds they registered. */ 482df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell for (i = 0; i < lg->eventfds->num; i++) 483133890103b9de08904f909995973e4b5c08a780eDavide Libenzi eventfd_ctx_put(lg->eventfds->map[i].event); 484df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell kfree(lg->eventfds); 485df60aeef4f4fe0645d9a195a7689005520422de5Rusty Russell 4862e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell /* 4872e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * If lg->dead doesn't contain an error code it will be NULL or a 4882e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * kmalloc()ed string, either of which is ok to hand to kfree(). 4892e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 490d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell if (!IS_ERR(lg->dead)) 491d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell kfree(lg->dead); 49205dfdbbd678ea2b642db73f48b75667a23d15484Mark Wallis /* Free the memory allocated to the lguest_struct */ 49305dfdbbd678ea2b642db73f48b75667a23d15484Mark Wallis kfree(lg); 494dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell /* Release lock and exit. */ 495d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell mutex_unlock(&lguest_lock); 496dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 497d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return 0; 498d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 499d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 500dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell/*L:000 501dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Welcome to our journey through the Launcher! 502dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 503dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * The Launcher is the Host userspace program which sets up, runs and services 504dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * the Guest. In fact, many comments in the Drivers which refer to "the Host" 505dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * doing things are inaccurate: the Launcher does all the device handling for 506e1e72965ec2c02db99b415cd06c17ea90767e3a4Rusty Russell * the Guest, but the Guest can't know that. 507dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 508dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we 509dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * shall see more of that later. 510dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * 511dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * We begin our understanding with the Host kernel interface which the Launcher 512dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell * uses: reading and writing a character device called /dev/lguest. All the 5132e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * work happens in the read(), write() and close() routines: 5142e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 515828c09509b9695271bcbdc53e9fc9a6a737148d2Alexey Dobriyanstatic const struct file_operations lguest_fops = { 516d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .owner = THIS_MODULE, 517d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .release = close, 518d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .write = write, 519d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .read = read, 5206038f373a3dc1f1c26496e60b6c40b164716f07eArnd Bergmann .llseek = default_llseek, 521d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell}; 5229f54288def3f92b7805eb6d4b1ddcd73ecf6e889Rusty Russell/*:*/ 523dde797899ac17ebb812b7566044124d785e98dc7Rusty Russell 5242e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell/* 5252e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * This is a textbook example of a "misc" character device. Populate a "struct 5262e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell * miscdevice" and register it with misc_register(). 5272e04ef76916d1e29a077ea9d0f2003c8fd86724dRusty Russell */ 528d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellstatic struct miscdevice lguest_dev = { 529d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .minor = MISC_DYNAMIC_MINOR, 530d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .name = "lguest", 531d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell .fops = &lguest_fops, 532d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell}; 533d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 534d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellint __init lguest_device_init(void) 535d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 536d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell return misc_register(&lguest_dev); 537d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 538d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell 539d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russellvoid __exit lguest_device_remove(void) 540d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell{ 541d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell misc_deregister(&lguest_dev); 542d7e28ffe6c74416b54345d6004fd0964c115b12cRusty Russell} 543