blob: 85b714df8eae8d8ef372f74bedeb4497eab1f8d7 [file] [log] [blame]
Rusty Russella91d74a2009-07-30 16:03:45 -06001/*P:200 This contains all the /dev/lguest code, whereby the userspace launcher
Rusty Russellf938d2c2007-07-26 10:41:02 -07002 * controls and communicates with the Guest. For example, the first write will
Rusty Russella91d74a2009-07-30 16:03:45 -06003 * tell us the Guest's memory layout and entry point. A read will run the
4 * Guest until something happens, such as a signal or the Guest doing a NOTIFY
5 * out to the Launcher.
Rusty Russell2e04ef72009-07-30 16:03:45 -06006:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -07007#include <linux/uaccess.h>
8#include <linux/miscdevice.h>
9#include <linux/fs.h>
Glauber de Oliveira Costaca94f2b2008-01-18 23:59:07 -020010#include <linux/sched.h>
Rusty Russelldf60aee2009-06-12 22:27:09 -060011#include <linux/eventfd.h>
12#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070014#include "lg.h"
15
Rusty Russella91d74a2009-07-30 16:03:45 -060016/*L:056
17 * Before we move on, let's jump ahead and look at what the kernel does when
18 * it needs to look up the eventfds. That will complete our picture of how we
19 * use RCU.
20 *
21 * The notification value is in cpu->pending_notify: we return true if it went
22 * to an eventfd.
23 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060024bool send_notify_to_eventfd(struct lg_cpu *cpu)
25{
26 unsigned int i;
27 struct lg_eventfd_map *map;
28
Rusty Russella91d74a2009-07-30 16:03:45 -060029 /*
30 * This "rcu_read_lock()" helps track when someone is still looking at
31 * the (RCU-using) eventfds array. It's not actually a lock at all;
32 * indeed it's a noop in many configurations. (You didn't expect me to
33 * explain all the RCU secrets here, did you?)
34 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060035 rcu_read_lock();
Rusty Russella91d74a2009-07-30 16:03:45 -060036 /*
37 * rcu_dereference is the counter-side of rcu_assign_pointer(); it
38 * makes sure we don't access the memory pointed to by
39 * cpu->lg->eventfds before cpu->lg->eventfds is set. Sounds crazy,
40 * but Alpha allows this! Paul McKenney points out that a really
41 * aggressive compiler could have the same effect:
42 * http://lists.ozlabs.org/pipermail/lguest/2009-July/001560.html
43 *
44 * So play safe, use rcu_dereference to get the rcu-protected pointer:
45 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060046 map = rcu_dereference(cpu->lg->eventfds);
Rusty Russella91d74a2009-07-30 16:03:45 -060047 /*
48 * Simple array search: even if they add an eventfd while we do this,
49 * we'll continue to use the old array and just won't see the new one.
50 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060051 for (i = 0; i < map->num; i++) {
52 if (map->map[i].addr == cpu->pending_notify) {
53 eventfd_signal(map->map[i].event, 1);
54 cpu->pending_notify = 0;
55 break;
56 }
57 }
Rusty Russella91d74a2009-07-30 16:03:45 -060058 /* We're done with the rcu-protected variable cpu->lg->eventfds. */
Rusty Russelldf60aee2009-06-12 22:27:09 -060059 rcu_read_unlock();
Rusty Russella91d74a2009-07-30 16:03:45 -060060
61 /* If we cleared the notification, it's because we found a match. */
Rusty Russelldf60aee2009-06-12 22:27:09 -060062 return cpu->pending_notify == 0;
63}
64
Rusty Russella91d74a2009-07-30 16:03:45 -060065/*L:055
66 * One of the more tricksy tricks in the Linux Kernel is a technique called
67 * Read Copy Update. Since one point of lguest is to teach lguest journeyers
68 * about kernel coding, I use it here. (In case you're curious, other purposes
69 * include learning about virtualization and instilling a deep appreciation for
70 * simplicity and puppies).
71 *
72 * We keep a simple array which maps LHCALL_NOTIFY values to eventfds, but we
73 * add new eventfds without ever blocking readers from accessing the array.
74 * The current Launcher only does this during boot, so that never happens. But
75 * Read Copy Update is cool, and adding a lock risks damaging even more puppies
76 * than this code does.
77 *
78 * We allocate a brand new one-larger array, copy the old one and add our new
79 * element. Then we make the lg eventfd pointer point to the new array.
80 * That's the easy part: now we need to free the old one, but we need to make
81 * sure no slow CPU somewhere is still looking at it. That's what
82 * synchronize_rcu does for us: waits until every CPU has indicated that it has
83 * moved on to know it's no longer using the old one.
84 *
85 * If that's unclear, see http://en.wikipedia.org/wiki/Read-copy-update.
86 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060087static int add_eventfd(struct lguest *lg, unsigned long addr, int fd)
88{
89 struct lg_eventfd_map *new, *old = lg->eventfds;
90
Rusty Russella91d74a2009-07-30 16:03:45 -060091 /*
92 * We don't allow notifications on value 0 anyway (pending_notify of
93 * 0 means "nothing pending").
94 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060095 if (!addr)
96 return -EINVAL;
97
Rusty Russell2e04ef72009-07-30 16:03:45 -060098 /*
99 * Replace the old array with the new one, carefully: others can
100 * be accessing it at the same time.
101 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600102 new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1),
103 GFP_KERNEL);
104 if (!new)
105 return -ENOMEM;
106
107 /* First make identical copy. */
108 memcpy(new->map, old->map, sizeof(old->map[0]) * old->num);
109 new->num = old->num;
110
111 /* Now append new entry. */
112 new->map[new->num].addr = addr;
Davide Libenzi13389012009-06-30 11:41:11 -0700113 new->map[new->num].event = eventfd_ctx_fdget(fd);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600114 if (IS_ERR(new->map[new->num].event)) {
Dan Carpenterf2945262009-07-19 14:46:09 +0300115 int err = PTR_ERR(new->map[new->num].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600116 kfree(new);
Dan Carpenterf2945262009-07-19 14:46:09 +0300117 return err;
Rusty Russelldf60aee2009-06-12 22:27:09 -0600118 }
119 new->num++;
120
Rusty Russella91d74a2009-07-30 16:03:45 -0600121 /*
122 * Now put new one in place: rcu_assign_pointer() is a fancy way of
123 * doing "lg->eventfds = new", but it uses memory barriers to make
124 * absolutely sure that the contents of "new" written above is nailed
125 * down before we actually do the assignment.
126 *
127 * We have to think about these kinds of things when we're operating on
128 * live data without locks.
129 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600130 rcu_assign_pointer(lg->eventfds, new);
131
Rusty Russell2e04ef72009-07-30 16:03:45 -0600132 /*
133 * We're not in a big hurry. Wait until noone's looking at old
Rusty Russella91d74a2009-07-30 16:03:45 -0600134 * version, then free it.
Rusty Russell2e04ef72009-07-30 16:03:45 -0600135 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600136 synchronize_rcu();
137 kfree(old);
138
139 return 0;
140}
141
Rusty Russella91d74a2009-07-30 16:03:45 -0600142/*L:052
143 * Receiving notifications from the Guest is usually done by attaching a
144 * particular LHCALL_NOTIFY value to an event filedescriptor. The eventfd will
145 * become readable when the Guest does an LHCALL_NOTIFY with that value.
146 *
147 * This is really convenient for processing each virtqueue in a separate
148 * thread.
149 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600150static int attach_eventfd(struct lguest *lg, const unsigned long __user *input)
151{
152 unsigned long addr, fd;
153 int err;
154
155 if (get_user(addr, input) != 0)
156 return -EFAULT;
157 input++;
158 if (get_user(fd, input) != 0)
159 return -EFAULT;
160
Rusty Russella91d74a2009-07-30 16:03:45 -0600161 /*
162 * Just make sure two callers don't add eventfds at once. We really
163 * only need to lock against callers adding to the same Guest, so using
164 * the Big Lguest Lock is overkill. But this is setup, not a fast path.
165 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600166 mutex_lock(&lguest_lock);
167 err = add_eventfd(lg, addr, fd);
168 mutex_unlock(&lguest_lock);
169
Dan Carpenterf2945262009-07-19 14:46:09 +0300170 return err;
Rusty Russelldf60aee2009-06-12 22:27:09 -0600171}
172
Rusty Russell2e04ef72009-07-30 16:03:45 -0600173/*L:050
174 * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
175 * number to /dev/lguest.
176 */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200177static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700178{
Jes Sorensen511801d2007-10-22 11:03:31 +1000179 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700180
181 if (get_user(irq, input) != 0)
182 return -EFAULT;
183 if (irq >= LGUEST_IRQS)
184 return -EINVAL;
Rusty Russell9f155a92009-06-12 22:27:08 -0600185
Rusty Russella91d74a2009-07-30 16:03:45 -0600186 /*
187 * Next time the Guest runs, the core code will see if it can deliver
188 * this interrupt.
189 */
Rusty Russell9f155a92009-06-12 22:27:08 -0600190 set_interrupt(cpu, irq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700191 return 0;
192}
193
Rusty Russell2e04ef72009-07-30 16:03:45 -0600194/*L:040
195 * Once our Guest is initialized, the Launcher makes it run by reading
196 * from /dev/lguest.
197 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700198static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
199{
200 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200201 struct lg_cpu *cpu;
202 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700203
Rusty Russelldde79782007-07-26 10:41:03 -0700204 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700205 if (!lg)
206 return -EINVAL;
207
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200208 /* Watch out for arbitrary vcpu indexes! */
209 if (cpu_id >= lg->nr_cpus)
210 return -EINVAL;
211
212 cpu = &lg->cpus[cpu_id];
213
Rusty Russelle1e72962007-10-25 15:02:50 +1000214 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200215 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700216 return -EPERM;
217
Rusty Russella6bd8e12008-03-28 11:05:53 -0500218 /* If the Guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700219 if (lg->dead) {
220 size_t len;
221
Rusty Russelldde79782007-07-26 10:41:03 -0700222 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700223 if (IS_ERR(lg->dead))
224 return PTR_ERR(lg->dead);
225
Rusty Russelldde79782007-07-26 10:41:03 -0700226 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700227 len = min(size, strlen(lg->dead)+1);
228 if (copy_to_user(user, lg->dead, len) != 0)
229 return -EFAULT;
230 return len;
231 }
232
Rusty Russell2e04ef72009-07-30 16:03:45 -0600233 /*
234 * If we returned from read() last time because the Guest sent I/O,
235 * clear the flag.
236 */
Glauber de Oliveira Costa5e232f42008-01-07 11:05:36 -0200237 if (cpu->pending_notify)
238 cpu->pending_notify = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700239
Rusty Russelldde79782007-07-26 10:41:03 -0700240 /* Run the Guest until something interesting happens. */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200241 return run_guest(cpu, (unsigned long __user *)user);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700242}
243
Rusty Russell2e04ef72009-07-30 16:03:45 -0600244/*L:025
245 * This actually initializes a CPU. For the moment, a Guest is only
246 * uniprocessor, so "id" is always 0.
247 */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200248static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
249{
Rusty Russella6bd8e12008-03-28 11:05:53 -0500250 /* We have a limited number the number of CPUs in the lguest struct. */
Rusty Russell24adf122008-05-02 21:50:51 -0500251 if (id >= ARRAY_SIZE(cpu->lg->cpus))
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200252 return -EINVAL;
253
Rusty Russella6bd8e12008-03-28 11:05:53 -0500254 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200255 cpu->id = id;
256 cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
257 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500258
259 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200260 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200261
Rusty Russell2e04ef72009-07-30 16:03:45 -0600262 /*
263 * We need a complete page for the Guest registers: they are accessible
264 * to the Guest and we can only grant it access to whole pages.
265 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200266 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
267 if (!cpu->regs_page)
268 return -ENOMEM;
269
270 /* We actually put the registers at the bottom of the page. */
271 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
272
Rusty Russell2e04ef72009-07-30 16:03:45 -0600273 /*
274 * Now we initialize the Guest's registers, handing it the start
275 * address.
276 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200277 lguest_arch_setup_regs(cpu, start_ip);
278
Rusty Russell2e04ef72009-07-30 16:03:45 -0600279 /*
280 * We keep a pointer to the Launcher task (ie. current task) for when
281 * other Guests want to wake this one (eg. console input).
282 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200283 cpu->tsk = current;
284
Rusty Russell2e04ef72009-07-30 16:03:45 -0600285 /*
286 * We need to keep a pointer to the Launcher's memory map, because if
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200287 * the Launcher dies we need to clean it up. If we don't keep a
Rusty Russell2e04ef72009-07-30 16:03:45 -0600288 * reference, it is destroyed before close() is called.
289 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200290 cpu->mm = get_task_mm(cpu->tsk);
291
Rusty Russell2e04ef72009-07-30 16:03:45 -0600292 /*
293 * We remember which CPU's pages this Guest used last, for optimization
294 * when the same Guest runs on the same CPU twice.
295 */
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200296 cpu->last_pages = NULL;
297
Rusty Russella6bd8e12008-03-28 11:05:53 -0500298 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200299 return 0;
300}
301
Rusty Russell2e04ef72009-07-30 16:03:45 -0600302/*L:020
303 * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in
304 * addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700305 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000306 * base: The start of the Guest-physical memory inside the Launcher memory.
307 *
Rusty Russelldde79782007-07-26 10:41:03 -0700308 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000309 * allowed to access. The Guest memory lives inside the Launcher, so it sets
310 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700311 *
Rusty Russelldde79782007-07-26 10:41:03 -0700312 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700313 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000314static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700315{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600316 /* "struct lguest" contains all we (the Host) know about a Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700317 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000318 int err;
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300319 unsigned long args[3];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700320
Rusty Russell2e04ef72009-07-30 16:03:45 -0600321 /*
322 * We grab the Big Lguest lock, which protects against multiple
323 * simultaneous initializations.
324 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700325 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700326 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700327 if (file->private_data) {
328 err = -EBUSY;
329 goto unlock;
330 }
331
332 if (copy_from_user(args, input, sizeof(args)) != 0) {
333 err = -EFAULT;
334 goto unlock;
335 }
336
Rusty Russell48245cc2007-10-22 11:03:27 +1000337 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
338 if (!lg) {
339 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700340 goto unlock;
341 }
Rusty Russelldde79782007-07-26 10:41:03 -0700342
Rusty Russelldf60aee2009-06-12 22:27:09 -0600343 lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL);
344 if (!lg->eventfds) {
345 err = -ENOMEM;
346 goto free_lg;
347 }
348 lg->eventfds->num = 0;
349
Rusty Russelldde79782007-07-26 10:41:03 -0700350 /* Populate the easy fields of our "struct lguest" */
Al Viro74dbf712008-03-29 03:08:28 +0000351 lg->mem_base = (void __user *)args[0];
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000352 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700353
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300354 /* This is the first cpu (cpu 0) and it will start booting at args[2] */
355 err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200356 if (err)
Rusty Russelldf60aee2009-06-12 22:27:09 -0600357 goto free_eventfds;
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200358
Rusty Russell2e04ef72009-07-30 16:03:45 -0600359 /*
360 * Initialize the Guest's shadow page tables, using the toplevel
361 * address the Launcher gave us. This allocates memory, so can fail.
362 */
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300363 err = init_guest_pagetable(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700364 if (err)
365 goto free_regs;
366
Rusty Russelldde79782007-07-26 10:41:03 -0700367 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700368 file->private_data = lg;
369
370 mutex_unlock(&lguest_lock);
371
Rusty Russelldde79782007-07-26 10:41:03 -0700372 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700373 return sizeof(args);
374
375free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200376 /* FIXME: This should be in free_vcpu */
377 free_page(lg->cpus[0].regs_page);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600378free_eventfds:
379 kfree(lg->eventfds);
380free_lg:
Adrian Bunk43054412007-11-14 16:59:00 -0800381 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700382unlock:
383 mutex_unlock(&lguest_lock);
384 return err;
385}
386
Rusty Russell2e04ef72009-07-30 16:03:45 -0600387/*L:010
388 * The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000389 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700390 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella91d74a2009-07-30 16:03:45 -0600391 * writes of other values to send interrupts or set up receipt of notifications.
Rusty Russella6bd8e12008-03-28 11:05:53 -0500392 *
393 * Note that we overload the "offset" in the /dev/lguest file to indicate what
Rusty Russella91d74a2009-07-30 16:03:45 -0600394 * CPU number we're dealing with. Currently this is always 0 since we only
Rusty Russella6bd8e12008-03-28 11:05:53 -0500395 * support uniprocessor Guests, but you can see the beginnings of SMP support
Rusty Russell2e04ef72009-07-30 16:03:45 -0600396 * here.
397 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000398static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700399 size_t size, loff_t *off)
400{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600401 /*
402 * Once the Guest is initialized, we hold the "struct lguest" in the
403 * file private data.
404 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700405 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000406 const unsigned long __user *input = (const unsigned long __user *)in;
407 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200408 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200409 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700410
Rusty Russella6bd8e12008-03-28 11:05:53 -0500411 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700412 if (get_user(req, input) != 0)
413 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000414 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700415
Rusty Russelldde79782007-07-26 10:41:03 -0700416 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200417 if (req != LHREQ_INITIALIZE) {
418 if (!lg || (cpu_id >= lg->nr_cpus))
419 return -EINVAL;
420 cpu = &lg->cpus[cpu_id];
Eugene Teof73d1e62008-02-09 23:53:17 +0800421
422 /* Once the Guest is dead, you can only read() why it died. */
423 if (lg->dead)
424 return -ENOENT;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200425 }
Rusty Russelldde79782007-07-26 10:41:03 -0700426
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700427 switch (req) {
428 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000429 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700430 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200431 return user_send_irq(cpu, input);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600432 case LHREQ_EVENTFD:
433 return attach_eventfd(lg, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700434 default:
435 return -EINVAL;
436 }
437}
438
Rusty Russell2e04ef72009-07-30 16:03:45 -0600439/*L:060
440 * The final piece of interface code is the close() routine. It reverses
Rusty Russelldde79782007-07-26 10:41:03 -0700441 * everything done in initialize(). This is usually called because the
442 * Launcher exited.
443 *
444 * Note that the close routine returns 0 or a negative error number: it can't
445 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
Rusty Russell2e04ef72009-07-30 16:03:45 -0600446 * letting them do it.
447:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700448static int close(struct inode *inode, struct file *file)
449{
450 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200451 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700452
Rusty Russelldde79782007-07-26 10:41:03 -0700453 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700454 if (!lg)
455 return 0;
456
Rusty Russell2e04ef72009-07-30 16:03:45 -0600457 /*
458 * We need the big lock, to protect from inter-guest I/O and other
459 * Launchers initializing guests.
460 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700461 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200462
463 /* Free up the shadow page tables for the Guest. */
464 free_guest_pagetable(lg);
465
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200466 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200467 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
468 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200469 /* We can free up the register page we allocated. */
470 free_page(lg->cpus[i].regs_page);
Rusty Russell2e04ef72009-07-30 16:03:45 -0600471 /*
472 * Now all the memory cleanups are done, it's safe to release
473 * the Launcher's memory management structure.
474 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200475 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200476 }
Rusty Russelldf60aee2009-06-12 22:27:09 -0600477
478 /* Release any eventfds they registered. */
479 for (i = 0; i < lg->eventfds->num; i++)
Davide Libenzi13389012009-06-30 11:41:11 -0700480 eventfd_ctx_put(lg->eventfds->map[i].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600481 kfree(lg->eventfds);
482
Rusty Russell2e04ef72009-07-30 16:03:45 -0600483 /*
484 * If lg->dead doesn't contain an error code it will be NULL or a
485 * kmalloc()ed string, either of which is ok to hand to kfree().
486 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700487 if (!IS_ERR(lg->dead))
488 kfree(lg->dead);
Mark Wallis05dfdbb2009-01-26 17:32:35 +1100489 /* Free the memory allocated to the lguest_struct */
490 kfree(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700491 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700492 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700493
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700494 return 0;
495}
496
Rusty Russelldde79782007-07-26 10:41:03 -0700497/*L:000
498 * Welcome to our journey through the Launcher!
499 *
500 * The Launcher is the Host userspace program which sets up, runs and services
501 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
502 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000503 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700504 *
505 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
506 * shall see more of that later.
507 *
508 * We begin our understanding with the Host kernel interface which the Launcher
509 * uses: reading and writing a character device called /dev/lguest. All the
Rusty Russell2e04ef72009-07-30 16:03:45 -0600510 * work happens in the read(), write() and close() routines:
511 */
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700512static const struct file_operations lguest_fops = {
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700513 .owner = THIS_MODULE,
514 .release = close,
515 .write = write,
516 .read = read,
517};
Rusty Russelldde79782007-07-26 10:41:03 -0700518
Rusty Russell2e04ef72009-07-30 16:03:45 -0600519/*
520 * This is a textbook example of a "misc" character device. Populate a "struct
521 * miscdevice" and register it with misc_register().
522 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700523static struct miscdevice lguest_dev = {
524 .minor = MISC_DYNAMIC_MINOR,
525 .name = "lguest",
526 .fops = &lguest_fops,
527};
528
529int __init lguest_device_init(void)
530{
531 return misc_register(&lguest_dev);
532}
533
534void __exit lguest_device_remove(void)
535{
536 misc_deregister(&lguest_dev);
537}