Rusty Russell | 9f54288 | 2011-07-22 14:39:50 +0930 | [diff] [blame] | 1 | /*P:200 This contains all the /dev/lguest code, whereby the userspace |
| 2 | * launcher controls and communicates with the Guest. For example, |
| 3 | * the first write will tell us the Guest's memory layout and entry |
| 4 | * point. A read will run the Guest until something happens, such as |
| 5 | * a signal or the Guest doing a NOTIFY out to the Launcher. There is |
| 6 | * also a way for the Launcher to attach eventfds to particular NOTIFY |
| 7 | * values instead of returning from the read() call. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 8 | :*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 9 | #include <linux/uaccess.h> |
| 10 | #include <linux/miscdevice.h> |
| 11 | #include <linux/fs.h> |
Glauber de Oliveira Costa | ca94f2b | 2008-01-18 23:59:07 -0200 | [diff] [blame] | 12 | #include <linux/sched.h> |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 13 | #include <linux/eventfd.h> |
| 14 | #include <linux/file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 16 | #include "lg.h" |
| 17 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 18 | /*L:056 |
| 19 | * Before we move on, let's jump ahead and look at what the kernel does when |
| 20 | * it needs to look up the eventfds. That will complete our picture of how we |
| 21 | * use RCU. |
| 22 | * |
| 23 | * The notification value is in cpu->pending_notify: we return true if it went |
| 24 | * to an eventfd. |
| 25 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 26 | bool send_notify_to_eventfd(struct lg_cpu *cpu) |
| 27 | { |
| 28 | unsigned int i; |
| 29 | struct lg_eventfd_map *map; |
| 30 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 31 | /* |
| 32 | * This "rcu_read_lock()" helps track when someone is still looking at |
| 33 | * the (RCU-using) eventfds array. It's not actually a lock at all; |
| 34 | * indeed it's a noop in many configurations. (You didn't expect me to |
| 35 | * explain all the RCU secrets here, did you?) |
| 36 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 37 | rcu_read_lock(); |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 38 | /* |
| 39 | * rcu_dereference is the counter-side of rcu_assign_pointer(); it |
| 40 | * makes sure we don't access the memory pointed to by |
| 41 | * cpu->lg->eventfds before cpu->lg->eventfds is set. Sounds crazy, |
| 42 | * but Alpha allows this! Paul McKenney points out that a really |
| 43 | * aggressive compiler could have the same effect: |
| 44 | * http://lists.ozlabs.org/pipermail/lguest/2009-July/001560.html |
| 45 | * |
| 46 | * So play safe, use rcu_dereference to get the rcu-protected pointer: |
| 47 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 48 | map = rcu_dereference(cpu->lg->eventfds); |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 49 | /* |
| 50 | * Simple array search: even if they add an eventfd while we do this, |
| 51 | * we'll continue to use the old array and just won't see the new one. |
| 52 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 53 | for (i = 0; i < map->num; i++) { |
| 54 | if (map->map[i].addr == cpu->pending_notify) { |
| 55 | eventfd_signal(map->map[i].event, 1); |
| 56 | cpu->pending_notify = 0; |
| 57 | break; |
| 58 | } |
| 59 | } |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 60 | /* We're done with the rcu-protected variable cpu->lg->eventfds. */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 61 | rcu_read_unlock(); |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 62 | |
| 63 | /* If we cleared the notification, it's because we found a match. */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 64 | return cpu->pending_notify == 0; |
| 65 | } |
| 66 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 67 | /*L:055 |
| 68 | * One of the more tricksy tricks in the Linux Kernel is a technique called |
| 69 | * Read Copy Update. Since one point of lguest is to teach lguest journeyers |
| 70 | * about kernel coding, I use it here. (In case you're curious, other purposes |
| 71 | * include learning about virtualization and instilling a deep appreciation for |
| 72 | * simplicity and puppies). |
| 73 | * |
| 74 | * We keep a simple array which maps LHCALL_NOTIFY values to eventfds, but we |
| 75 | * add new eventfds without ever blocking readers from accessing the array. |
| 76 | * The current Launcher only does this during boot, so that never happens. But |
| 77 | * Read Copy Update is cool, and adding a lock risks damaging even more puppies |
| 78 | * than this code does. |
| 79 | * |
| 80 | * We allocate a brand new one-larger array, copy the old one and add our new |
| 81 | * element. Then we make the lg eventfd pointer point to the new array. |
| 82 | * That's the easy part: now we need to free the old one, but we need to make |
| 83 | * sure no slow CPU somewhere is still looking at it. That's what |
| 84 | * synchronize_rcu does for us: waits until every CPU has indicated that it has |
| 85 | * moved on to know it's no longer using the old one. |
| 86 | * |
| 87 | * If that's unclear, see http://en.wikipedia.org/wiki/Read-copy-update. |
| 88 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 89 | static int add_eventfd(struct lguest *lg, unsigned long addr, int fd) |
| 90 | { |
| 91 | struct lg_eventfd_map *new, *old = lg->eventfds; |
| 92 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 93 | /* |
| 94 | * We don't allow notifications on value 0 anyway (pending_notify of |
| 95 | * 0 means "nothing pending"). |
| 96 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 97 | if (!addr) |
| 98 | return -EINVAL; |
| 99 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 100 | /* |
| 101 | * Replace the old array with the new one, carefully: others can |
| 102 | * be accessing it at the same time. |
| 103 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 104 | new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1), |
| 105 | GFP_KERNEL); |
| 106 | if (!new) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | /* First make identical copy. */ |
| 110 | memcpy(new->map, old->map, sizeof(old->map[0]) * old->num); |
| 111 | new->num = old->num; |
| 112 | |
| 113 | /* Now append new entry. */ |
| 114 | new->map[new->num].addr = addr; |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 115 | new->map[new->num].event = eventfd_ctx_fdget(fd); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 116 | if (IS_ERR(new->map[new->num].event)) { |
Dan Carpenter | f294526 | 2009-07-19 14:46:09 +0300 | [diff] [blame] | 117 | int err = PTR_ERR(new->map[new->num].event); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 118 | kfree(new); |
Dan Carpenter | f294526 | 2009-07-19 14:46:09 +0300 | [diff] [blame] | 119 | return err; |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 120 | } |
| 121 | new->num++; |
| 122 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 123 | /* |
| 124 | * Now put new one in place: rcu_assign_pointer() is a fancy way of |
| 125 | * doing "lg->eventfds = new", but it uses memory barriers to make |
| 126 | * absolutely sure that the contents of "new" written above is nailed |
| 127 | * down before we actually do the assignment. |
| 128 | * |
| 129 | * We have to think about these kinds of things when we're operating on |
| 130 | * live data without locks. |
| 131 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 132 | rcu_assign_pointer(lg->eventfds, new); |
| 133 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 134 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 135 | * We're not in a big hurry. Wait until no one's looking at old |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 136 | * version, then free it. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 137 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 138 | synchronize_rcu(); |
| 139 | kfree(old); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 144 | /*L:052 |
| 145 | * Receiving notifications from the Guest is usually done by attaching a |
| 146 | * particular LHCALL_NOTIFY value to an event filedescriptor. The eventfd will |
| 147 | * become readable when the Guest does an LHCALL_NOTIFY with that value. |
| 148 | * |
| 149 | * This is really convenient for processing each virtqueue in a separate |
| 150 | * thread. |
| 151 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 152 | static int attach_eventfd(struct lguest *lg, const unsigned long __user *input) |
| 153 | { |
| 154 | unsigned long addr, fd; |
| 155 | int err; |
| 156 | |
| 157 | if (get_user(addr, input) != 0) |
| 158 | return -EFAULT; |
| 159 | input++; |
| 160 | if (get_user(fd, input) != 0) |
| 161 | return -EFAULT; |
| 162 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 163 | /* |
| 164 | * Just make sure two callers don't add eventfds at once. We really |
| 165 | * only need to lock against callers adding to the same Guest, so using |
| 166 | * the Big Lguest Lock is overkill. But this is setup, not a fast path. |
| 167 | */ |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 168 | mutex_lock(&lguest_lock); |
| 169 | err = add_eventfd(lg, addr, fd); |
| 170 | mutex_unlock(&lguest_lock); |
| 171 | |
Dan Carpenter | f294526 | 2009-07-19 14:46:09 +0300 | [diff] [blame] | 172 | return err; |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 173 | } |
| 174 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 175 | /*L:050 |
| 176 | * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
| 177 | * number to /dev/lguest. |
| 178 | */ |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 179 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 180 | { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 181 | unsigned long irq; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 182 | |
| 183 | if (get_user(irq, input) != 0) |
| 184 | return -EFAULT; |
| 185 | if (irq >= LGUEST_IRQS) |
| 186 | return -EINVAL; |
Rusty Russell | 9f155a9 | 2009-06-12 22:27:08 -0600 | [diff] [blame] | 187 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 188 | /* |
| 189 | * Next time the Guest runs, the core code will see if it can deliver |
| 190 | * this interrupt. |
| 191 | */ |
Rusty Russell | 9f155a9 | 2009-06-12 22:27:08 -0600 | [diff] [blame] | 192 | set_interrupt(cpu, irq); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 196 | /*L:040 |
| 197 | * Once our Guest is initialized, the Launcher makes it run by reading |
| 198 | * from /dev/lguest. |
| 199 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 200 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
| 201 | { |
| 202 | struct lguest *lg = file->private_data; |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 203 | struct lg_cpu *cpu; |
| 204 | unsigned int cpu_id = *o; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 205 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 206 | /* You must write LHREQ_INITIALIZE first! */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 207 | if (!lg) |
| 208 | return -EINVAL; |
| 209 | |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 210 | /* Watch out for arbitrary vcpu indexes! */ |
| 211 | if (cpu_id >= lg->nr_cpus) |
| 212 | return -EINVAL; |
| 213 | |
| 214 | cpu = &lg->cpus[cpu_id]; |
| 215 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 216 | /* If you're not the task which owns the Guest, go away. */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 217 | if (current != cpu->tsk) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 218 | return -EPERM; |
| 219 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 220 | /* If the Guest is already dead, we indicate why */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 221 | if (lg->dead) { |
| 222 | size_t len; |
| 223 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 224 | /* lg->dead either contains an error code, or a string. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 225 | if (IS_ERR(lg->dead)) |
| 226 | return PTR_ERR(lg->dead); |
| 227 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 228 | /* We can only return as much as the buffer they read with. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 229 | len = min(size, strlen(lg->dead)+1); |
| 230 | if (copy_to_user(user, lg->dead, len) != 0) |
| 231 | return -EFAULT; |
| 232 | return len; |
| 233 | } |
| 234 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 235 | /* |
| 236 | * If we returned from read() last time because the Guest sent I/O, |
| 237 | * clear the flag. |
| 238 | */ |
Glauber de Oliveira Costa | 5e232f4 | 2008-01-07 11:05:36 -0200 | [diff] [blame] | 239 | if (cpu->pending_notify) |
| 240 | cpu->pending_notify = 0; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 241 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 242 | /* Run the Guest until something interesting happens. */ |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 243 | return run_guest(cpu, (unsigned long __user *)user); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 246 | /*L:025 |
| 247 | * This actually initializes a CPU. For the moment, a Guest is only |
| 248 | * uniprocessor, so "id" is always 0. |
| 249 | */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 250 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
| 251 | { |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 252 | /* We have a limited number the number of CPUs in the lguest struct. */ |
Rusty Russell | 24adf12 | 2008-05-02 21:50:51 -0500 | [diff] [blame] | 253 | if (id >= ARRAY_SIZE(cpu->lg->cpus)) |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 254 | return -EINVAL; |
| 255 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 256 | /* Set up this CPU's id, and pointer back to the lguest struct. */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 257 | cpu->id = id; |
| 258 | cpu->lg = container_of((cpu - id), struct lguest, cpus[0]); |
| 259 | cpu->lg->nr_cpus++; |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 260 | |
| 261 | /* Each CPU has a timer it can set. */ |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 262 | init_clockdev(cpu); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 263 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 264 | /* |
| 265 | * We need a complete page for the Guest registers: they are accessible |
| 266 | * to the Guest and we can only grant it access to whole pages. |
| 267 | */ |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 268 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); |
| 269 | if (!cpu->regs_page) |
| 270 | return -ENOMEM; |
| 271 | |
| 272 | /* We actually put the registers at the bottom of the page. */ |
| 273 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); |
| 274 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 275 | /* |
| 276 | * Now we initialize the Guest's registers, handing it the start |
| 277 | * address. |
| 278 | */ |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 279 | lguest_arch_setup_regs(cpu, start_ip); |
| 280 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 281 | /* |
| 282 | * We keep a pointer to the Launcher task (ie. current task) for when |
| 283 | * other Guests want to wake this one (eg. console input). |
| 284 | */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 285 | cpu->tsk = current; |
| 286 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 287 | /* |
| 288 | * We need to keep a pointer to the Launcher's memory map, because if |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 289 | * the Launcher dies we need to clean it up. If we don't keep a |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 290 | * reference, it is destroyed before close() is called. |
| 291 | */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 292 | cpu->mm = get_task_mm(cpu->tsk); |
| 293 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 294 | /* |
| 295 | * We remember which CPU's pages this Guest used last, for optimization |
| 296 | * when the same Guest runs on the same CPU twice. |
| 297 | */ |
Glauber de Oliveira Costa | f34f8c5 | 2008-01-17 19:13:26 -0200 | [diff] [blame] | 298 | cpu->last_pages = NULL; |
| 299 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 300 | /* No error == success. */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 304 | /*L:020 |
| 305 | * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in |
| 306 | * addition to the LHREQ_INITIALIZE value). These are: |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 307 | * |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 308 | * base: The start of the Guest-physical memory inside the Launcher memory. |
| 309 | * |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 310 | * pfnlimit: The highest (Guest-physical) page number the Guest should be |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 311 | * allowed to access. The Guest memory lives inside the Launcher, so it sets |
| 312 | * this to ensure the Guest can only reach its own memory. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 313 | * |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 314 | * start: The first instruction to execute ("eip" in x86-speak). |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 315 | */ |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 316 | static int initialize(struct file *file, const unsigned long __user *input) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 317 | { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 318 | /* "struct lguest" contains all we (the Host) know about a Guest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 319 | struct lguest *lg; |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 320 | int err; |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 321 | unsigned long args[3]; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 322 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 323 | /* |
| 324 | * We grab the Big Lguest lock, which protects against multiple |
| 325 | * simultaneous initializations. |
| 326 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 327 | mutex_lock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 328 | /* You can't initialize twice! Close the device and start again... */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 329 | if (file->private_data) { |
| 330 | err = -EBUSY; |
| 331 | goto unlock; |
| 332 | } |
| 333 | |
| 334 | if (copy_from_user(args, input, sizeof(args)) != 0) { |
| 335 | err = -EFAULT; |
| 336 | goto unlock; |
| 337 | } |
| 338 | |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 339 | lg = kzalloc(sizeof(*lg), GFP_KERNEL); |
| 340 | if (!lg) { |
| 341 | err = -ENOMEM; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 342 | goto unlock; |
| 343 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 344 | |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 345 | lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL); |
| 346 | if (!lg->eventfds) { |
| 347 | err = -ENOMEM; |
| 348 | goto free_lg; |
| 349 | } |
| 350 | lg->eventfds->num = 0; |
| 351 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 352 | /* Populate the easy fields of our "struct lguest" */ |
Al Viro | 74dbf71 | 2008-03-29 03:08:28 +0000 | [diff] [blame] | 353 | lg->mem_base = (void __user *)args[0]; |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 354 | lg->pfn_limit = args[1]; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 355 | |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 356 | /* This is the first cpu (cpu 0) and it will start booting at args[2] */ |
| 357 | err = lg_cpu_start(&lg->cpus[0], 0, args[2]); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 358 | if (err) |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 359 | goto free_eventfds; |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 360 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 361 | /* |
Rusty Russell | 9f54288 | 2011-07-22 14:39:50 +0930 | [diff] [blame] | 362 | * Initialize the Guest's shadow page tables. This allocates |
| 363 | * memory, so can fail. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 364 | */ |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 365 | err = init_guest_pagetable(lg); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 366 | if (err) |
| 367 | goto free_regs; |
| 368 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 369 | /* We keep our "struct lguest" in the file's private_data. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 370 | file->private_data = lg; |
| 371 | |
| 372 | mutex_unlock(&lguest_lock); |
| 373 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 374 | /* And because this is a write() call, we return the length used. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 375 | return sizeof(args); |
| 376 | |
| 377 | free_regs: |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 378 | /* FIXME: This should be in free_vcpu */ |
| 379 | free_page(lg->cpus[0].regs_page); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 380 | free_eventfds: |
| 381 | kfree(lg->eventfds); |
| 382 | free_lg: |
Adrian Bunk | 4305441 | 2007-11-14 16:59:00 -0800 | [diff] [blame] | 383 | kfree(lg); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 384 | unlock: |
| 385 | mutex_unlock(&lguest_lock); |
| 386 | return err; |
| 387 | } |
| 388 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 389 | /*L:010 |
| 390 | * The first operation the Launcher does must be a write. All writes |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 391 | * start with an unsigned long number: for the first write this must be |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 392 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 393 | * writes of other values to send interrupts or set up receipt of notifications. |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 394 | * |
| 395 | * Note that we overload the "offset" in the /dev/lguest file to indicate what |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 396 | * CPU number we're dealing with. Currently this is always 0 since we only |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 397 | * support uniprocessor Guests, but you can see the beginnings of SMP support |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 398 | * here. |
| 399 | */ |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 400 | static ssize_t write(struct file *file, const char __user *in, |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 401 | size_t size, loff_t *off) |
| 402 | { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 403 | /* |
| 404 | * Once the Guest is initialized, we hold the "struct lguest" in the |
| 405 | * file private data. |
| 406 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 407 | struct lguest *lg = file->private_data; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 408 | const unsigned long __user *input = (const unsigned long __user *)in; |
| 409 | unsigned long req; |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 410 | struct lg_cpu *uninitialized_var(cpu); |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 411 | unsigned int cpu_id = *off; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 412 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 413 | /* The first value tells us what this request is. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 414 | if (get_user(req, input) != 0) |
| 415 | return -EFAULT; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 416 | input++; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 417 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 418 | /* If you haven't initialized, you must do that first. */ |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 419 | if (req != LHREQ_INITIALIZE) { |
| 420 | if (!lg || (cpu_id >= lg->nr_cpus)) |
| 421 | return -EINVAL; |
| 422 | cpu = &lg->cpus[cpu_id]; |
Eugene Teo | f73d1e6 | 2008-02-09 23:53:17 +0800 | [diff] [blame] | 423 | |
| 424 | /* Once the Guest is dead, you can only read() why it died. */ |
| 425 | if (lg->dead) |
| 426 | return -ENOENT; |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 427 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 428 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 429 | switch (req) { |
| 430 | case LHREQ_INITIALIZE: |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 431 | return initialize(file, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 432 | case LHREQ_IRQ: |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 433 | return user_send_irq(cpu, input); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 434 | case LHREQ_EVENTFD: |
| 435 | return attach_eventfd(lg, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 436 | default: |
| 437 | return -EINVAL; |
| 438 | } |
| 439 | } |
| 440 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 441 | /*L:060 |
| 442 | * The final piece of interface code is the close() routine. It reverses |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 443 | * everything done in initialize(). This is usually called because the |
| 444 | * Launcher exited. |
| 445 | * |
| 446 | * Note that the close routine returns 0 or a negative error number: it can't |
| 447 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 448 | * letting them do it. |
| 449 | :*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 450 | static int close(struct inode *inode, struct file *file) |
| 451 | { |
| 452 | struct lguest *lg = file->private_data; |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 453 | unsigned int i; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 454 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 455 | /* If we never successfully initialized, there's nothing to clean up */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 456 | if (!lg) |
| 457 | return 0; |
| 458 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 459 | /* |
| 460 | * We need the big lock, to protect from inter-guest I/O and other |
| 461 | * Launchers initializing guests. |
| 462 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 463 | mutex_lock(&lguest_lock); |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 464 | |
| 465 | /* Free up the shadow page tables for the Guest. */ |
| 466 | free_guest_pagetable(lg); |
| 467 | |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 468 | for (i = 0; i < lg->nr_cpus; i++) { |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 469 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ |
| 470 | hrtimer_cancel(&lg->cpus[i].hrt); |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 471 | /* We can free up the register page we allocated. */ |
| 472 | free_page(lg->cpus[i].regs_page); |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 473 | /* |
| 474 | * Now all the memory cleanups are done, it's safe to release |
| 475 | * the Launcher's memory management structure. |
| 476 | */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 477 | mmput(lg->cpus[i].mm); |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 478 | } |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 479 | |
| 480 | /* Release any eventfds they registered. */ |
| 481 | for (i = 0; i < lg->eventfds->num; i++) |
Davide Libenzi | 1338901 | 2009-06-30 11:41:11 -0700 | [diff] [blame] | 482 | eventfd_ctx_put(lg->eventfds->map[i].event); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 483 | kfree(lg->eventfds); |
| 484 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 485 | /* |
| 486 | * If lg->dead doesn't contain an error code it will be NULL or a |
| 487 | * kmalloc()ed string, either of which is ok to hand to kfree(). |
| 488 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 489 | if (!IS_ERR(lg->dead)) |
| 490 | kfree(lg->dead); |
Mark Wallis | 05dfdbb | 2009-01-26 17:32:35 +1100 | [diff] [blame] | 491 | /* Free the memory allocated to the lguest_struct */ |
| 492 | kfree(lg); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 493 | /* Release lock and exit. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 494 | mutex_unlock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 495 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 499 | /*L:000 |
| 500 | * Welcome to our journey through the Launcher! |
| 501 | * |
| 502 | * The Launcher is the Host userspace program which sets up, runs and services |
| 503 | * the Guest. In fact, many comments in the Drivers which refer to "the Host" |
| 504 | * doing things are inaccurate: the Launcher does all the device handling for |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 505 | * the Guest, but the Guest can't know that. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 506 | * |
| 507 | * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we |
| 508 | * shall see more of that later. |
| 509 | * |
| 510 | * We begin our understanding with the Host kernel interface which the Launcher |
| 511 | * uses: reading and writing a character device called /dev/lguest. All the |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 512 | * work happens in the read(), write() and close() routines: |
| 513 | */ |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 514 | static const struct file_operations lguest_fops = { |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 515 | .owner = THIS_MODULE, |
| 516 | .release = close, |
| 517 | .write = write, |
| 518 | .read = read, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 519 | .llseek = default_llseek, |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 520 | }; |
Rusty Russell | 9f54288 | 2011-07-22 14:39:50 +0930 | [diff] [blame] | 521 | /*:*/ |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 522 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 523 | /* |
| 524 | * This is a textbook example of a "misc" character device. Populate a "struct |
| 525 | * miscdevice" and register it with misc_register(). |
| 526 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 527 | static struct miscdevice lguest_dev = { |
| 528 | .minor = MISC_DYNAMIC_MINOR, |
| 529 | .name = "lguest", |
| 530 | .fops = &lguest_fops, |
| 531 | }; |
| 532 | |
| 533 | int __init lguest_device_init(void) |
| 534 | { |
| 535 | return misc_register(&lguest_dev); |
| 536 | } |
| 537 | |
| 538 | void __exit lguest_device_remove(void) |
| 539 | { |
| 540 | misc_deregister(&lguest_dev); |
| 541 | } |