blob: be996d1736151882575a4f500a4dba4fa045775d [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 Russell8ed31302015-02-11 15:15:09 +1030246/*L:053
247 * Deliver a trap: this is used by the Launcher if it can't emulate
248 * an instruction.
249 */
250static int trap(struct lg_cpu *cpu, const unsigned long __user *input)
251{
252 unsigned long trapnum;
253
254 if (get_user(trapnum, input) != 0)
255 return -EFAULT;
256
257 if (!deliver_trap(cpu, trapnum))
258 return -EINVAL;
259
260 return 0;
261}
262
Rusty Russell2e04ef72009-07-30 16:03:45 -0600263/*L:040
264 * Once our Guest is initialized, the Launcher makes it run by reading
265 * from /dev/lguest.
266 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700267static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
268{
269 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200270 struct lg_cpu *cpu;
271 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700272
Rusty Russelldde79782007-07-26 10:41:03 -0700273 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700274 if (!lg)
275 return -EINVAL;
276
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200277 /* Watch out for arbitrary vcpu indexes! */
278 if (cpu_id >= lg->nr_cpus)
279 return -EINVAL;
280
281 cpu = &lg->cpus[cpu_id];
282
Rusty Russelle1e72962007-10-25 15:02:50 +1000283 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200284 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700285 return -EPERM;
286
Rusty Russella6bd8e12008-03-28 11:05:53 -0500287 /* If the Guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700288 if (lg->dead) {
289 size_t len;
290
Rusty Russelldde79782007-07-26 10:41:03 -0700291 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700292 if (IS_ERR(lg->dead))
293 return PTR_ERR(lg->dead);
294
Rusty Russelldde79782007-07-26 10:41:03 -0700295 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700296 len = min(size, strlen(lg->dead)+1);
297 if (copy_to_user(user, lg->dead, len) != 0)
298 return -EFAULT;
299 return len;
300 }
301
Rusty Russell2e04ef72009-07-30 16:03:45 -0600302 /*
303 * If we returned from read() last time because the Guest sent I/O,
304 * clear the flag.
305 */
Rusty Russell69a09dc2015-02-11 15:15:09 +1030306 if (cpu->pending.trap)
307 cpu->pending.trap = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700308
Rusty Russelldde79782007-07-26 10:41:03 -0700309 /* Run the Guest until something interesting happens. */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200310 return run_guest(cpu, (unsigned long __user *)user);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700311}
312
Rusty Russell2e04ef72009-07-30 16:03:45 -0600313/*L:025
314 * This actually initializes a CPU. For the moment, a Guest is only
315 * uniprocessor, so "id" is always 0.
316 */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200317static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
318{
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930319 /* We have a limited number of CPUs in the lguest struct. */
Rusty Russell24adf122008-05-02 21:50:51 -0500320 if (id >= ARRAY_SIZE(cpu->lg->cpus))
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200321 return -EINVAL;
322
Rusty Russella6bd8e12008-03-28 11:05:53 -0500323 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200324 cpu->id = id;
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930325 cpu->lg = container_of(cpu, struct lguest, cpus[id]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200326 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500327
328 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200329 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200330
Rusty Russell2e04ef72009-07-30 16:03:45 -0600331 /*
332 * We need a complete page for the Guest registers: they are accessible
333 * to the Guest and we can only grant it access to whole pages.
334 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200335 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
336 if (!cpu->regs_page)
337 return -ENOMEM;
338
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930339 /* We actually put the registers at the end of the page. */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200340 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
341
Rusty Russell2e04ef72009-07-30 16:03:45 -0600342 /*
343 * Now we initialize the Guest's registers, handing it the start
344 * address.
345 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200346 lguest_arch_setup_regs(cpu, start_ip);
347
Rusty Russell2e04ef72009-07-30 16:03:45 -0600348 /*
349 * We keep a pointer to the Launcher task (ie. current task) for when
350 * other Guests want to wake this one (eg. console input).
351 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200352 cpu->tsk = current;
353
Rusty Russell2e04ef72009-07-30 16:03:45 -0600354 /*
355 * We need to keep a pointer to the Launcher's memory map, because if
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200356 * the Launcher dies we need to clean it up. If we don't keep a
Rusty Russell2e04ef72009-07-30 16:03:45 -0600357 * reference, it is destroyed before close() is called.
358 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200359 cpu->mm = get_task_mm(cpu->tsk);
360
Rusty Russell2e04ef72009-07-30 16:03:45 -0600361 /*
362 * We remember which CPU's pages this Guest used last, for optimization
363 * when the same Guest runs on the same CPU twice.
364 */
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200365 cpu->last_pages = NULL;
366
Rusty Russella6bd8e12008-03-28 11:05:53 -0500367 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200368 return 0;
369}
370
Rusty Russell2e04ef72009-07-30 16:03:45 -0600371/*L:020
372 * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in
373 * addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700374 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000375 * base: The start of the Guest-physical memory inside the Launcher memory.
376 *
Rusty Russelldde79782007-07-26 10:41:03 -0700377 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000378 * allowed to access. The Guest memory lives inside the Launcher, so it sets
379 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700380 *
Rusty Russelldde79782007-07-26 10:41:03 -0700381 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700382 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000383static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700384{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600385 /* "struct lguest" contains all we (the Host) know about a Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700386 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000387 int err;
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300388 unsigned long args[3];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700389
Rusty Russell2e04ef72009-07-30 16:03:45 -0600390 /*
391 * We grab the Big Lguest lock, which protects against multiple
392 * simultaneous initializations.
393 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700394 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700395 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700396 if (file->private_data) {
397 err = -EBUSY;
398 goto unlock;
399 }
400
401 if (copy_from_user(args, input, sizeof(args)) != 0) {
402 err = -EFAULT;
403 goto unlock;
404 }
405
Rusty Russell48245cc2007-10-22 11:03:27 +1000406 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
407 if (!lg) {
408 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700409 goto unlock;
410 }
Rusty Russelldde79782007-07-26 10:41:03 -0700411
Rusty Russelldf60aee2009-06-12 22:27:09 -0600412 lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL);
413 if (!lg->eventfds) {
414 err = -ENOMEM;
415 goto free_lg;
416 }
417 lg->eventfds->num = 0;
418
Rusty Russelldde79782007-07-26 10:41:03 -0700419 /* Populate the easy fields of our "struct lguest" */
Al Viro74dbf712008-03-29 03:08:28 +0000420 lg->mem_base = (void __user *)args[0];
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000421 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700422
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300423 /* This is the first cpu (cpu 0) and it will start booting at args[2] */
424 err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200425 if (err)
Rusty Russelldf60aee2009-06-12 22:27:09 -0600426 goto free_eventfds;
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200427
Rusty Russell2e04ef72009-07-30 16:03:45 -0600428 /*
Rusty Russell9f542882011-07-22 14:39:50 +0930429 * Initialize the Guest's shadow page tables. This allocates
430 * memory, so can fail.
Rusty Russell2e04ef72009-07-30 16:03:45 -0600431 */
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300432 err = init_guest_pagetable(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700433 if (err)
434 goto free_regs;
435
Rusty Russelldde79782007-07-26 10:41:03 -0700436 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700437 file->private_data = lg;
438
439 mutex_unlock(&lguest_lock);
440
Rusty Russelldde79782007-07-26 10:41:03 -0700441 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700442 return sizeof(args);
443
444free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200445 /* FIXME: This should be in free_vcpu */
446 free_page(lg->cpus[0].regs_page);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600447free_eventfds:
448 kfree(lg->eventfds);
449free_lg:
Adrian Bunk43054412007-11-14 16:59:00 -0800450 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700451unlock:
452 mutex_unlock(&lguest_lock);
453 return err;
454}
455
Rusty Russell2e04ef72009-07-30 16:03:45 -0600456/*L:010
457 * The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000458 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700459 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella91d74a2009-07-30 16:03:45 -0600460 * writes of other values to send interrupts or set up receipt of notifications.
Rusty Russella6bd8e12008-03-28 11:05:53 -0500461 *
462 * Note that we overload the "offset" in the /dev/lguest file to indicate what
Rusty Russella91d74a2009-07-30 16:03:45 -0600463 * CPU number we're dealing with. Currently this is always 0 since we only
Rusty Russella6bd8e12008-03-28 11:05:53 -0500464 * support uniprocessor Guests, but you can see the beginnings of SMP support
Rusty Russell2e04ef72009-07-30 16:03:45 -0600465 * here.
466 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000467static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700468 size_t size, loff_t *off)
469{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600470 /*
471 * Once the Guest is initialized, we hold the "struct lguest" in the
472 * file private data.
473 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700474 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000475 const unsigned long __user *input = (const unsigned long __user *)in;
476 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200477 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200478 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700479
Rusty Russella6bd8e12008-03-28 11:05:53 -0500480 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700481 if (get_user(req, input) != 0)
482 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000483 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700484
Rusty Russelldde79782007-07-26 10:41:03 -0700485 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200486 if (req != LHREQ_INITIALIZE) {
487 if (!lg || (cpu_id >= lg->nr_cpus))
488 return -EINVAL;
489 cpu = &lg->cpus[cpu_id];
Eugene Teof73d1e62008-02-09 23:53:17 +0800490
491 /* Once the Guest is dead, you can only read() why it died. */
492 if (lg->dead)
493 return -ENOENT;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200494 }
Rusty Russelldde79782007-07-26 10:41:03 -0700495
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700496 switch (req) {
497 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000498 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700499 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200500 return user_send_irq(cpu, input);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600501 case LHREQ_EVENTFD:
502 return attach_eventfd(lg, input);
Rusty Russell18c13732015-02-11 15:15:09 +1030503 case LHREQ_GETREG:
504 return getreg_setup(cpu, input);
505 case LHREQ_SETREG:
506 return setreg(cpu, input);
Rusty Russell8ed31302015-02-11 15:15:09 +1030507 case LHREQ_TRAP:
508 return trap(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700509 default:
510 return -EINVAL;
511 }
512}
513
Rusty Russell2e04ef72009-07-30 16:03:45 -0600514/*L:060
515 * The final piece of interface code is the close() routine. It reverses
Rusty Russelldde79782007-07-26 10:41:03 -0700516 * everything done in initialize(). This is usually called because the
517 * Launcher exited.
518 *
519 * Note that the close routine returns 0 or a negative error number: it can't
520 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
Rusty Russell2e04ef72009-07-30 16:03:45 -0600521 * letting them do it.
522:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700523static int close(struct inode *inode, struct file *file)
524{
525 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200526 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700527
Rusty Russelldde79782007-07-26 10:41:03 -0700528 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700529 if (!lg)
530 return 0;
531
Rusty Russell2e04ef72009-07-30 16:03:45 -0600532 /*
533 * We need the big lock, to protect from inter-guest I/O and other
534 * Launchers initializing guests.
535 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700536 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200537
538 /* Free up the shadow page tables for the Guest. */
539 free_guest_pagetable(lg);
540
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200541 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200542 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
543 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200544 /* We can free up the register page we allocated. */
545 free_page(lg->cpus[i].regs_page);
Rusty Russell2e04ef72009-07-30 16:03:45 -0600546 /*
547 * Now all the memory cleanups are done, it's safe to release
548 * the Launcher's memory management structure.
549 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200550 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200551 }
Rusty Russelldf60aee2009-06-12 22:27:09 -0600552
553 /* Release any eventfds they registered. */
554 for (i = 0; i < lg->eventfds->num; i++)
Davide Libenzi13389012009-06-30 11:41:11 -0700555 eventfd_ctx_put(lg->eventfds->map[i].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600556 kfree(lg->eventfds);
557
Rusty Russell2e04ef72009-07-30 16:03:45 -0600558 /*
559 * If lg->dead doesn't contain an error code it will be NULL or a
560 * kmalloc()ed string, either of which is ok to hand to kfree().
561 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700562 if (!IS_ERR(lg->dead))
563 kfree(lg->dead);
Mark Wallis05dfdbb2009-01-26 17:32:35 +1100564 /* Free the memory allocated to the lguest_struct */
565 kfree(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700566 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700567 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700568
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700569 return 0;
570}
571
Rusty Russelldde79782007-07-26 10:41:03 -0700572/*L:000
573 * Welcome to our journey through the Launcher!
574 *
575 * The Launcher is the Host userspace program which sets up, runs and services
576 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
577 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000578 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700579 *
580 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
581 * shall see more of that later.
582 *
583 * We begin our understanding with the Host kernel interface which the Launcher
584 * uses: reading and writing a character device called /dev/lguest. All the
Rusty Russell2e04ef72009-07-30 16:03:45 -0600585 * work happens in the read(), write() and close() routines:
586 */
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700587static const struct file_operations lguest_fops = {
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700588 .owner = THIS_MODULE,
589 .release = close,
590 .write = write,
591 .read = read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200592 .llseek = default_llseek,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700593};
Rusty Russell9f542882011-07-22 14:39:50 +0930594/*:*/
Rusty Russelldde79782007-07-26 10:41:03 -0700595
Rusty Russell2e04ef72009-07-30 16:03:45 -0600596/*
597 * This is a textbook example of a "misc" character device. Populate a "struct
598 * miscdevice" and register it with misc_register().
599 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700600static struct miscdevice lguest_dev = {
601 .minor = MISC_DYNAMIC_MINOR,
602 .name = "lguest",
603 .fops = &lguest_fops,
604};
605
606int __init lguest_device_init(void)
607{
608 return misc_register(&lguest_dev);
609}
610
611void __exit lguest_device_remove(void)
612{
613 misc_deregister(&lguest_dev);
614}