blob: dcf9efd94cf4094195d98d41a354b9cc474b2f06 [file] [log] [blame]
Rusty Russell9f542882011-07-22 14:39:50 +09301/*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 Russell2e04ef72009-07-30 16:03:45 -06008:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -07009#include <linux/uaccess.h>
10#include <linux/miscdevice.h>
11#include <linux/fs.h>
Glauber de Oliveira Costaca94f2b2008-01-18 23:59:07 -020012#include <linux/sched.h>
Rusty Russelldf60aee2009-06-12 22:27:09 -060013#include <linux/eventfd.h>
14#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Gortmaker39a0e332011-07-21 13:03:20 -040016#include <linux/export.h>
Rusty Russelld7e28ff2007-07-19 01:49:23 -070017#include "lg.h"
18
Rusty Russella91d74a2009-07-30 16:03:45 -060019/*L:056
20 * Before we move on, let's jump ahead and look at what the kernel does when
21 * it needs to look up the eventfds. That will complete our picture of how we
22 * use RCU.
23 *
24 * The notification value is in cpu->pending_notify: we return true if it went
25 * to an eventfd.
26 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060027bool send_notify_to_eventfd(struct lg_cpu *cpu)
28{
29 unsigned int i;
30 struct lg_eventfd_map *map;
31
Rusty Russell69a09dc2015-02-11 15:15:09 +103032 /* We only connect LHCALL_NOTIFY to event fds, not other traps. */
33 if (cpu->pending.trap != LGUEST_TRAP_ENTRY)
34 return false;
35
Rusty Russella91d74a2009-07-30 16:03:45 -060036 /*
37 * This "rcu_read_lock()" helps track when someone is still looking at
38 * the (RCU-using) eventfds array. It's not actually a lock at all;
39 * indeed it's a noop in many configurations. (You didn't expect me to
40 * explain all the RCU secrets here, did you?)
41 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060042 rcu_read_lock();
Rusty Russella91d74a2009-07-30 16:03:45 -060043 /*
44 * rcu_dereference is the counter-side of rcu_assign_pointer(); it
45 * makes sure we don't access the memory pointed to by
46 * cpu->lg->eventfds before cpu->lg->eventfds is set. Sounds crazy,
47 * but Alpha allows this! Paul McKenney points out that a really
48 * aggressive compiler could have the same effect:
49 * http://lists.ozlabs.org/pipermail/lguest/2009-July/001560.html
50 *
51 * So play safe, use rcu_dereference to get the rcu-protected pointer:
52 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060053 map = rcu_dereference(cpu->lg->eventfds);
Rusty Russella91d74a2009-07-30 16:03:45 -060054 /*
55 * Simple array search: even if they add an eventfd while we do this,
56 * we'll continue to use the old array and just won't see the new one.
57 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060058 for (i = 0; i < map->num; i++) {
Rusty Russell69a09dc2015-02-11 15:15:09 +103059 if (map->map[i].addr == cpu->pending.addr) {
Rusty Russelldf60aee2009-06-12 22:27:09 -060060 eventfd_signal(map->map[i].event, 1);
Rusty Russell69a09dc2015-02-11 15:15:09 +103061 cpu->pending.trap = 0;
Rusty Russelldf60aee2009-06-12 22:27:09 -060062 break;
63 }
64 }
Rusty Russella91d74a2009-07-30 16:03:45 -060065 /* We're done with the rcu-protected variable cpu->lg->eventfds. */
Rusty Russelldf60aee2009-06-12 22:27:09 -060066 rcu_read_unlock();
Rusty Russella91d74a2009-07-30 16:03:45 -060067
68 /* If we cleared the notification, it's because we found a match. */
Rusty Russell69a09dc2015-02-11 15:15:09 +103069 return cpu->pending.trap == 0;
Rusty Russelldf60aee2009-06-12 22:27:09 -060070}
71
Rusty Russella91d74a2009-07-30 16:03:45 -060072/*L:055
73 * One of the more tricksy tricks in the Linux Kernel is a technique called
74 * Read Copy Update. Since one point of lguest is to teach lguest journeyers
75 * about kernel coding, I use it here. (In case you're curious, other purposes
76 * include learning about virtualization and instilling a deep appreciation for
77 * simplicity and puppies).
78 *
79 * We keep a simple array which maps LHCALL_NOTIFY values to eventfds, but we
80 * add new eventfds without ever blocking readers from accessing the array.
81 * The current Launcher only does this during boot, so that never happens. But
82 * Read Copy Update is cool, and adding a lock risks damaging even more puppies
83 * than this code does.
84 *
85 * We allocate a brand new one-larger array, copy the old one and add our new
86 * element. Then we make the lg eventfd pointer point to the new array.
87 * That's the easy part: now we need to free the old one, but we need to make
88 * sure no slow CPU somewhere is still looking at it. That's what
89 * synchronize_rcu does for us: waits until every CPU has indicated that it has
90 * moved on to know it's no longer using the old one.
91 *
92 * If that's unclear, see http://en.wikipedia.org/wiki/Read-copy-update.
93 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060094static int add_eventfd(struct lguest *lg, unsigned long addr, int fd)
95{
96 struct lg_eventfd_map *new, *old = lg->eventfds;
97
Rusty Russella91d74a2009-07-30 16:03:45 -060098 /*
99 * We don't allow notifications on value 0 anyway (pending_notify of
100 * 0 means "nothing pending").
101 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600102 if (!addr)
103 return -EINVAL;
104
Rusty Russell2e04ef72009-07-30 16:03:45 -0600105 /*
106 * Replace the old array with the new one, carefully: others can
107 * be accessing it at the same time.
108 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600109 new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1),
110 GFP_KERNEL);
111 if (!new)
112 return -ENOMEM;
113
114 /* First make identical copy. */
115 memcpy(new->map, old->map, sizeof(old->map[0]) * old->num);
116 new->num = old->num;
117
118 /* Now append new entry. */
119 new->map[new->num].addr = addr;
Davide Libenzi13389012009-06-30 11:41:11 -0700120 new->map[new->num].event = eventfd_ctx_fdget(fd);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600121 if (IS_ERR(new->map[new->num].event)) {
Dan Carpenterf2945262009-07-19 14:46:09 +0300122 int err = PTR_ERR(new->map[new->num].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600123 kfree(new);
Dan Carpenterf2945262009-07-19 14:46:09 +0300124 return err;
Rusty Russelldf60aee2009-06-12 22:27:09 -0600125 }
126 new->num++;
127
Rusty Russella91d74a2009-07-30 16:03:45 -0600128 /*
129 * Now put new one in place: rcu_assign_pointer() is a fancy way of
130 * doing "lg->eventfds = new", but it uses memory barriers to make
131 * absolutely sure that the contents of "new" written above is nailed
132 * down before we actually do the assignment.
133 *
134 * We have to think about these kinds of things when we're operating on
135 * live data without locks.
136 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600137 rcu_assign_pointer(lg->eventfds, new);
138
Rusty Russell2e04ef72009-07-30 16:03:45 -0600139 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300140 * We're not in a big hurry. Wait until no one's looking at old
Rusty Russella91d74a2009-07-30 16:03:45 -0600141 * version, then free it.
Rusty Russell2e04ef72009-07-30 16:03:45 -0600142 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600143 synchronize_rcu();
144 kfree(old);
145
146 return 0;
147}
148
Rusty Russella91d74a2009-07-30 16:03:45 -0600149/*L:052
150 * Receiving notifications from the Guest is usually done by attaching a
151 * particular LHCALL_NOTIFY value to an event filedescriptor. The eventfd will
152 * become readable when the Guest does an LHCALL_NOTIFY with that value.
153 *
154 * This is really convenient for processing each virtqueue in a separate
155 * thread.
156 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600157static int attach_eventfd(struct lguest *lg, const unsigned long __user *input)
158{
159 unsigned long addr, fd;
160 int err;
161
162 if (get_user(addr, input) != 0)
163 return -EFAULT;
164 input++;
165 if (get_user(fd, input) != 0)
166 return -EFAULT;
167
Rusty Russella91d74a2009-07-30 16:03:45 -0600168 /*
169 * Just make sure two callers don't add eventfds at once. We really
170 * only need to lock against callers adding to the same Guest, so using
171 * the Big Lguest Lock is overkill. But this is setup, not a fast path.
172 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600173 mutex_lock(&lguest_lock);
174 err = add_eventfd(lg, addr, fd);
175 mutex_unlock(&lguest_lock);
176
Dan Carpenterf2945262009-07-19 14:46:09 +0300177 return err;
Rusty Russelldf60aee2009-06-12 22:27:09 -0600178}
179
Rusty Russell18c13732015-02-11 15:15:09 +1030180/* The Launcher can get the registers, and also set some of them. */
181static int getreg_setup(struct lg_cpu *cpu, const unsigned long __user *input)
182{
183 unsigned long which;
184
185 /* We re-use the ptrace structure to specify which register to read. */
186 if (get_user(which, input) != 0)
187 return -EFAULT;
188
189 /*
190 * We set up the cpu register pointer, and their next read will
191 * actually get the value (instead of running the guest).
192 *
193 * The last argument 'true' says we can access any register.
194 */
195 cpu->reg_read = lguest_arch_regptr(cpu, which, true);
196 if (!cpu->reg_read)
197 return -ENOENT;
198
199 /* And because this is a write() call, we return the length used. */
200 return sizeof(unsigned long) * 2;
201}
202
203static int setreg(struct lg_cpu *cpu, const unsigned long __user *input)
204{
205 unsigned long which, value, *reg;
206
207 /* We re-use the ptrace structure to specify which register to read. */
208 if (get_user(which, input) != 0)
209 return -EFAULT;
210 input++;
211 if (get_user(value, input) != 0)
212 return -EFAULT;
213
214 /* The last argument 'false' means we can't access all registers. */
215 reg = lguest_arch_regptr(cpu, which, false);
216 if (!reg)
217 return -ENOENT;
218
219 *reg = value;
220
221 /* And because this is a write() call, we return the length used. */
222 return sizeof(unsigned long) * 3;
223}
224
Rusty Russell2e04ef72009-07-30 16:03:45 -0600225/*L:050
226 * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
227 * number to /dev/lguest.
228 */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200229static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700230{
Jes Sorensen511801d2007-10-22 11:03:31 +1000231 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700232
233 if (get_user(irq, input) != 0)
234 return -EFAULT;
235 if (irq >= LGUEST_IRQS)
236 return -EINVAL;
Rusty Russell9f155a92009-06-12 22:27:08 -0600237
Rusty Russella91d74a2009-07-30 16:03:45 -0600238 /*
239 * Next time the Guest runs, the core code will see if it can deliver
240 * this interrupt.
241 */
Rusty Russell9f155a92009-06-12 22:27:08 -0600242 set_interrupt(cpu, irq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700243 return 0;
244}
245
Rusty Russell2e04ef72009-07-30 16:03:45 -0600246/*L:040
247 * Once our Guest is initialized, the Launcher makes it run by reading
248 * from /dev/lguest.
249 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700250static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
251{
252 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200253 struct lg_cpu *cpu;
254 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700255
Rusty Russelldde79782007-07-26 10:41:03 -0700256 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700257 if (!lg)
258 return -EINVAL;
259
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200260 /* Watch out for arbitrary vcpu indexes! */
261 if (cpu_id >= lg->nr_cpus)
262 return -EINVAL;
263
264 cpu = &lg->cpus[cpu_id];
265
Rusty Russelle1e72962007-10-25 15:02:50 +1000266 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200267 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700268 return -EPERM;
269
Rusty Russella6bd8e12008-03-28 11:05:53 -0500270 /* If the Guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700271 if (lg->dead) {
272 size_t len;
273
Rusty Russelldde79782007-07-26 10:41:03 -0700274 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700275 if (IS_ERR(lg->dead))
276 return PTR_ERR(lg->dead);
277
Rusty Russelldde79782007-07-26 10:41:03 -0700278 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700279 len = min(size, strlen(lg->dead)+1);
280 if (copy_to_user(user, lg->dead, len) != 0)
281 return -EFAULT;
282 return len;
283 }
284
Rusty Russell2e04ef72009-07-30 16:03:45 -0600285 /*
286 * If we returned from read() last time because the Guest sent I/O,
287 * clear the flag.
288 */
Rusty Russell69a09dc2015-02-11 15:15:09 +1030289 if (cpu->pending.trap)
290 cpu->pending.trap = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700291
Rusty Russelldde79782007-07-26 10:41:03 -0700292 /* Run the Guest until something interesting happens. */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200293 return run_guest(cpu, (unsigned long __user *)user);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700294}
295
Rusty Russell2e04ef72009-07-30 16:03:45 -0600296/*L:025
297 * This actually initializes a CPU. For the moment, a Guest is only
298 * uniprocessor, so "id" is always 0.
299 */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200300static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
301{
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930302 /* We have a limited number of CPUs in the lguest struct. */
Rusty Russell24adf122008-05-02 21:50:51 -0500303 if (id >= ARRAY_SIZE(cpu->lg->cpus))
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200304 return -EINVAL;
305
Rusty Russella6bd8e12008-03-28 11:05:53 -0500306 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200307 cpu->id = id;
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930308 cpu->lg = container_of(cpu, struct lguest, cpus[id]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200309 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500310
311 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200312 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200313
Rusty Russell2e04ef72009-07-30 16:03:45 -0600314 /*
315 * We need a complete page for the Guest registers: they are accessible
316 * to the Guest and we can only grant it access to whole pages.
317 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200318 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
319 if (!cpu->regs_page)
320 return -ENOMEM;
321
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930322 /* We actually put the registers at the end of the page. */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200323 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
324
Rusty Russell2e04ef72009-07-30 16:03:45 -0600325 /*
326 * Now we initialize the Guest's registers, handing it the start
327 * address.
328 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200329 lguest_arch_setup_regs(cpu, start_ip);
330
Rusty Russell2e04ef72009-07-30 16:03:45 -0600331 /*
332 * We keep a pointer to the Launcher task (ie. current task) for when
333 * other Guests want to wake this one (eg. console input).
334 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200335 cpu->tsk = current;
336
Rusty Russell2e04ef72009-07-30 16:03:45 -0600337 /*
338 * We need to keep a pointer to the Launcher's memory map, because if
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200339 * the Launcher dies we need to clean it up. If we don't keep a
Rusty Russell2e04ef72009-07-30 16:03:45 -0600340 * reference, it is destroyed before close() is called.
341 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200342 cpu->mm = get_task_mm(cpu->tsk);
343
Rusty Russell2e04ef72009-07-30 16:03:45 -0600344 /*
345 * We remember which CPU's pages this Guest used last, for optimization
346 * when the same Guest runs on the same CPU twice.
347 */
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200348 cpu->last_pages = NULL;
349
Rusty Russella6bd8e12008-03-28 11:05:53 -0500350 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200351 return 0;
352}
353
Rusty Russell2e04ef72009-07-30 16:03:45 -0600354/*L:020
355 * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in
356 * addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700357 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000358 * base: The start of the Guest-physical memory inside the Launcher memory.
359 *
Rusty Russelldde79782007-07-26 10:41:03 -0700360 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000361 * allowed to access. The Guest memory lives inside the Launcher, so it sets
362 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700363 *
Rusty Russelldde79782007-07-26 10:41:03 -0700364 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700365 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000366static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700367{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600368 /* "struct lguest" contains all we (the Host) know about a Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700369 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000370 int err;
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300371 unsigned long args[3];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700372
Rusty Russell2e04ef72009-07-30 16:03:45 -0600373 /*
374 * We grab the Big Lguest lock, which protects against multiple
375 * simultaneous initializations.
376 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700377 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700378 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700379 if (file->private_data) {
380 err = -EBUSY;
381 goto unlock;
382 }
383
384 if (copy_from_user(args, input, sizeof(args)) != 0) {
385 err = -EFAULT;
386 goto unlock;
387 }
388
Rusty Russell48245cc2007-10-22 11:03:27 +1000389 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
390 if (!lg) {
391 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700392 goto unlock;
393 }
Rusty Russelldde79782007-07-26 10:41:03 -0700394
Rusty Russelldf60aee2009-06-12 22:27:09 -0600395 lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL);
396 if (!lg->eventfds) {
397 err = -ENOMEM;
398 goto free_lg;
399 }
400 lg->eventfds->num = 0;
401
Rusty Russelldde79782007-07-26 10:41:03 -0700402 /* Populate the easy fields of our "struct lguest" */
Al Viro74dbf712008-03-29 03:08:28 +0000403 lg->mem_base = (void __user *)args[0];
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000404 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700405
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300406 /* This is the first cpu (cpu 0) and it will start booting at args[2] */
407 err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200408 if (err)
Rusty Russelldf60aee2009-06-12 22:27:09 -0600409 goto free_eventfds;
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200410
Rusty Russell2e04ef72009-07-30 16:03:45 -0600411 /*
Rusty Russell9f542882011-07-22 14:39:50 +0930412 * Initialize the Guest's shadow page tables. This allocates
413 * memory, so can fail.
Rusty Russell2e04ef72009-07-30 16:03:45 -0600414 */
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300415 err = init_guest_pagetable(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700416 if (err)
417 goto free_regs;
418
Rusty Russelldde79782007-07-26 10:41:03 -0700419 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700420 file->private_data = lg;
421
422 mutex_unlock(&lguest_lock);
423
Rusty Russelldde79782007-07-26 10:41:03 -0700424 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700425 return sizeof(args);
426
427free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200428 /* FIXME: This should be in free_vcpu */
429 free_page(lg->cpus[0].regs_page);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600430free_eventfds:
431 kfree(lg->eventfds);
432free_lg:
Adrian Bunk43054412007-11-14 16:59:00 -0800433 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700434unlock:
435 mutex_unlock(&lguest_lock);
436 return err;
437}
438
Rusty Russell2e04ef72009-07-30 16:03:45 -0600439/*L:010
440 * The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000441 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700442 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella91d74a2009-07-30 16:03:45 -0600443 * writes of other values to send interrupts or set up receipt of notifications.
Rusty Russella6bd8e12008-03-28 11:05:53 -0500444 *
445 * Note that we overload the "offset" in the /dev/lguest file to indicate what
Rusty Russella91d74a2009-07-30 16:03:45 -0600446 * CPU number we're dealing with. Currently this is always 0 since we only
Rusty Russella6bd8e12008-03-28 11:05:53 -0500447 * support uniprocessor Guests, but you can see the beginnings of SMP support
Rusty Russell2e04ef72009-07-30 16:03:45 -0600448 * here.
449 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000450static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700451 size_t size, loff_t *off)
452{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600453 /*
454 * Once the Guest is initialized, we hold the "struct lguest" in the
455 * file private data.
456 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700457 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000458 const unsigned long __user *input = (const unsigned long __user *)in;
459 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200460 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200461 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700462
Rusty Russella6bd8e12008-03-28 11:05:53 -0500463 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700464 if (get_user(req, input) != 0)
465 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000466 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700467
Rusty Russelldde79782007-07-26 10:41:03 -0700468 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200469 if (req != LHREQ_INITIALIZE) {
470 if (!lg || (cpu_id >= lg->nr_cpus))
471 return -EINVAL;
472 cpu = &lg->cpus[cpu_id];
Eugene Teof73d1e62008-02-09 23:53:17 +0800473
474 /* Once the Guest is dead, you can only read() why it died. */
475 if (lg->dead)
476 return -ENOENT;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200477 }
Rusty Russelldde79782007-07-26 10:41:03 -0700478
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700479 switch (req) {
480 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000481 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700482 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200483 return user_send_irq(cpu, input);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600484 case LHREQ_EVENTFD:
485 return attach_eventfd(lg, input);
Rusty Russell18c13732015-02-11 15:15:09 +1030486 case LHREQ_GETREG:
487 return getreg_setup(cpu, input);
488 case LHREQ_SETREG:
489 return setreg(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700490 default:
491 return -EINVAL;
492 }
493}
494
Rusty Russell2e04ef72009-07-30 16:03:45 -0600495/*L:060
496 * The final piece of interface code is the close() routine. It reverses
Rusty Russelldde79782007-07-26 10:41:03 -0700497 * everything done in initialize(). This is usually called because the
498 * Launcher exited.
499 *
500 * Note that the close routine returns 0 or a negative error number: it can't
501 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
Rusty Russell2e04ef72009-07-30 16:03:45 -0600502 * letting them do it.
503:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700504static int close(struct inode *inode, struct file *file)
505{
506 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200507 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700508
Rusty Russelldde79782007-07-26 10:41:03 -0700509 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700510 if (!lg)
511 return 0;
512
Rusty Russell2e04ef72009-07-30 16:03:45 -0600513 /*
514 * We need the big lock, to protect from inter-guest I/O and other
515 * Launchers initializing guests.
516 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700517 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200518
519 /* Free up the shadow page tables for the Guest. */
520 free_guest_pagetable(lg);
521
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200522 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200523 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
524 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200525 /* We can free up the register page we allocated. */
526 free_page(lg->cpus[i].regs_page);
Rusty Russell2e04ef72009-07-30 16:03:45 -0600527 /*
528 * Now all the memory cleanups are done, it's safe to release
529 * the Launcher's memory management structure.
530 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200531 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200532 }
Rusty Russelldf60aee2009-06-12 22:27:09 -0600533
534 /* Release any eventfds they registered. */
535 for (i = 0; i < lg->eventfds->num; i++)
Davide Libenzi13389012009-06-30 11:41:11 -0700536 eventfd_ctx_put(lg->eventfds->map[i].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600537 kfree(lg->eventfds);
538
Rusty Russell2e04ef72009-07-30 16:03:45 -0600539 /*
540 * If lg->dead doesn't contain an error code it will be NULL or a
541 * kmalloc()ed string, either of which is ok to hand to kfree().
542 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700543 if (!IS_ERR(lg->dead))
544 kfree(lg->dead);
Mark Wallis05dfdbb2009-01-26 17:32:35 +1100545 /* Free the memory allocated to the lguest_struct */
546 kfree(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700547 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700548 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700549
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700550 return 0;
551}
552
Rusty Russelldde79782007-07-26 10:41:03 -0700553/*L:000
554 * Welcome to our journey through the Launcher!
555 *
556 * The Launcher is the Host userspace program which sets up, runs and services
557 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
558 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000559 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700560 *
561 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
562 * shall see more of that later.
563 *
564 * We begin our understanding with the Host kernel interface which the Launcher
565 * uses: reading and writing a character device called /dev/lguest. All the
Rusty Russell2e04ef72009-07-30 16:03:45 -0600566 * work happens in the read(), write() and close() routines:
567 */
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700568static const struct file_operations lguest_fops = {
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700569 .owner = THIS_MODULE,
570 .release = close,
571 .write = write,
572 .read = read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200573 .llseek = default_llseek,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700574};
Rusty Russell9f542882011-07-22 14:39:50 +0930575/*:*/
Rusty Russelldde79782007-07-26 10:41:03 -0700576
Rusty Russell2e04ef72009-07-30 16:03:45 -0600577/*
578 * This is a textbook example of a "misc" character device. Populate a "struct
579 * miscdevice" and register it with misc_register().
580 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700581static struct miscdevice lguest_dev = {
582 .minor = MISC_DYNAMIC_MINOR,
583 .name = "lguest",
584 .fops = &lguest_fops,
585};
586
587int __init lguest_device_init(void)
588{
589 return misc_register(&lguest_dev);
590}
591
592void __exit lguest_device_remove(void)
593{
594 misc_deregister(&lguest_dev);
595}