blob: 7f14c152dd23b3ca339906fb7c6bdbafdf44f76b [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 Russella91d74a2009-07-30 16:03:45 -060032 /*
33 * This "rcu_read_lock()" helps track when someone is still looking at
34 * the (RCU-using) eventfds array. It's not actually a lock at all;
35 * indeed it's a noop in many configurations. (You didn't expect me to
36 * explain all the RCU secrets here, did you?)
37 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060038 rcu_read_lock();
Rusty Russella91d74a2009-07-30 16:03:45 -060039 /*
40 * rcu_dereference is the counter-side of rcu_assign_pointer(); it
41 * makes sure we don't access the memory pointed to by
42 * cpu->lg->eventfds before cpu->lg->eventfds is set. Sounds crazy,
43 * but Alpha allows this! Paul McKenney points out that a really
44 * aggressive compiler could have the same effect:
45 * http://lists.ozlabs.org/pipermail/lguest/2009-July/001560.html
46 *
47 * So play safe, use rcu_dereference to get the rcu-protected pointer:
48 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060049 map = rcu_dereference(cpu->lg->eventfds);
Rusty Russella91d74a2009-07-30 16:03:45 -060050 /*
51 * Simple array search: even if they add an eventfd while we do this,
52 * we'll continue to use the old array and just won't see the new one.
53 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060054 for (i = 0; i < map->num; i++) {
55 if (map->map[i].addr == cpu->pending_notify) {
56 eventfd_signal(map->map[i].event, 1);
57 cpu->pending_notify = 0;
58 break;
59 }
60 }
Rusty Russella91d74a2009-07-30 16:03:45 -060061 /* We're done with the rcu-protected variable cpu->lg->eventfds. */
Rusty Russelldf60aee2009-06-12 22:27:09 -060062 rcu_read_unlock();
Rusty Russella91d74a2009-07-30 16:03:45 -060063
64 /* If we cleared the notification, it's because we found a match. */
Rusty Russelldf60aee2009-06-12 22:27:09 -060065 return cpu->pending_notify == 0;
66}
67
Rusty Russella91d74a2009-07-30 16:03:45 -060068/*L:055
69 * One of the more tricksy tricks in the Linux Kernel is a technique called
70 * Read Copy Update. Since one point of lguest is to teach lguest journeyers
71 * about kernel coding, I use it here. (In case you're curious, other purposes
72 * include learning about virtualization and instilling a deep appreciation for
73 * simplicity and puppies).
74 *
75 * We keep a simple array which maps LHCALL_NOTIFY values to eventfds, but we
76 * add new eventfds without ever blocking readers from accessing the array.
77 * The current Launcher only does this during boot, so that never happens. But
78 * Read Copy Update is cool, and adding a lock risks damaging even more puppies
79 * than this code does.
80 *
81 * We allocate a brand new one-larger array, copy the old one and add our new
82 * element. Then we make the lg eventfd pointer point to the new array.
83 * That's the easy part: now we need to free the old one, but we need to make
84 * sure no slow CPU somewhere is still looking at it. That's what
85 * synchronize_rcu does for us: waits until every CPU has indicated that it has
86 * moved on to know it's no longer using the old one.
87 *
88 * If that's unclear, see http://en.wikipedia.org/wiki/Read-copy-update.
89 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060090static int add_eventfd(struct lguest *lg, unsigned long addr, int fd)
91{
92 struct lg_eventfd_map *new, *old = lg->eventfds;
93
Rusty Russella91d74a2009-07-30 16:03:45 -060094 /*
95 * We don't allow notifications on value 0 anyway (pending_notify of
96 * 0 means "nothing pending").
97 */
Rusty Russelldf60aee2009-06-12 22:27:09 -060098 if (!addr)
99 return -EINVAL;
100
Rusty Russell2e04ef72009-07-30 16:03:45 -0600101 /*
102 * Replace the old array with the new one, carefully: others can
103 * be accessing it at the same time.
104 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600105 new = kmalloc(sizeof(*new) + sizeof(new->map[0]) * (old->num + 1),
106 GFP_KERNEL);
107 if (!new)
108 return -ENOMEM;
109
110 /* First make identical copy. */
111 memcpy(new->map, old->map, sizeof(old->map[0]) * old->num);
112 new->num = old->num;
113
114 /* Now append new entry. */
115 new->map[new->num].addr = addr;
Davide Libenzi13389012009-06-30 11:41:11 -0700116 new->map[new->num].event = eventfd_ctx_fdget(fd);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600117 if (IS_ERR(new->map[new->num].event)) {
Dan Carpenterf2945262009-07-19 14:46:09 +0300118 int err = PTR_ERR(new->map[new->num].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600119 kfree(new);
Dan Carpenterf2945262009-07-19 14:46:09 +0300120 return err;
Rusty Russelldf60aee2009-06-12 22:27:09 -0600121 }
122 new->num++;
123
Rusty Russella91d74a2009-07-30 16:03:45 -0600124 /*
125 * Now put new one in place: rcu_assign_pointer() is a fancy way of
126 * doing "lg->eventfds = new", but it uses memory barriers to make
127 * absolutely sure that the contents of "new" written above is nailed
128 * down before we actually do the assignment.
129 *
130 * We have to think about these kinds of things when we're operating on
131 * live data without locks.
132 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600133 rcu_assign_pointer(lg->eventfds, new);
134
Rusty Russell2e04ef72009-07-30 16:03:45 -0600135 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300136 * We're not in a big hurry. Wait until no one's looking at old
Rusty Russella91d74a2009-07-30 16:03:45 -0600137 * version, then free it.
Rusty Russell2e04ef72009-07-30 16:03:45 -0600138 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600139 synchronize_rcu();
140 kfree(old);
141
142 return 0;
143}
144
Rusty Russella91d74a2009-07-30 16:03:45 -0600145/*L:052
146 * Receiving notifications from the Guest is usually done by attaching a
147 * particular LHCALL_NOTIFY value to an event filedescriptor. The eventfd will
148 * become readable when the Guest does an LHCALL_NOTIFY with that value.
149 *
150 * This is really convenient for processing each virtqueue in a separate
151 * thread.
152 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600153static int attach_eventfd(struct lguest *lg, const unsigned long __user *input)
154{
155 unsigned long addr, fd;
156 int err;
157
158 if (get_user(addr, input) != 0)
159 return -EFAULT;
160 input++;
161 if (get_user(fd, input) != 0)
162 return -EFAULT;
163
Rusty Russella91d74a2009-07-30 16:03:45 -0600164 /*
165 * Just make sure two callers don't add eventfds at once. We really
166 * only need to lock against callers adding to the same Guest, so using
167 * the Big Lguest Lock is overkill. But this is setup, not a fast path.
168 */
Rusty Russelldf60aee2009-06-12 22:27:09 -0600169 mutex_lock(&lguest_lock);
170 err = add_eventfd(lg, addr, fd);
171 mutex_unlock(&lguest_lock);
172
Dan Carpenterf2945262009-07-19 14:46:09 +0300173 return err;
Rusty Russelldf60aee2009-06-12 22:27:09 -0600174}
175
Rusty Russell18c13732015-02-11 15:15:09 +1030176/* The Launcher can get the registers, and also set some of them. */
177static int getreg_setup(struct lg_cpu *cpu, const unsigned long __user *input)
178{
179 unsigned long which;
180
181 /* We re-use the ptrace structure to specify which register to read. */
182 if (get_user(which, input) != 0)
183 return -EFAULT;
184
185 /*
186 * We set up the cpu register pointer, and their next read will
187 * actually get the value (instead of running the guest).
188 *
189 * The last argument 'true' says we can access any register.
190 */
191 cpu->reg_read = lguest_arch_regptr(cpu, which, true);
192 if (!cpu->reg_read)
193 return -ENOENT;
194
195 /* And because this is a write() call, we return the length used. */
196 return sizeof(unsigned long) * 2;
197}
198
199static int setreg(struct lg_cpu *cpu, const unsigned long __user *input)
200{
201 unsigned long which, value, *reg;
202
203 /* We re-use the ptrace structure to specify which register to read. */
204 if (get_user(which, input) != 0)
205 return -EFAULT;
206 input++;
207 if (get_user(value, input) != 0)
208 return -EFAULT;
209
210 /* The last argument 'false' means we can't access all registers. */
211 reg = lguest_arch_regptr(cpu, which, false);
212 if (!reg)
213 return -ENOENT;
214
215 *reg = value;
216
217 /* And because this is a write() call, we return the length used. */
218 return sizeof(unsigned long) * 3;
219}
220
Rusty Russell2e04ef72009-07-30 16:03:45 -0600221/*L:050
222 * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
223 * number to /dev/lguest.
224 */
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200225static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700226{
Jes Sorensen511801d2007-10-22 11:03:31 +1000227 unsigned long irq;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700228
229 if (get_user(irq, input) != 0)
230 return -EFAULT;
231 if (irq >= LGUEST_IRQS)
232 return -EINVAL;
Rusty Russell9f155a92009-06-12 22:27:08 -0600233
Rusty Russella91d74a2009-07-30 16:03:45 -0600234 /*
235 * Next time the Guest runs, the core code will see if it can deliver
236 * this interrupt.
237 */
Rusty Russell9f155a92009-06-12 22:27:08 -0600238 set_interrupt(cpu, irq);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700239 return 0;
240}
241
Rusty Russell2e04ef72009-07-30 16:03:45 -0600242/*L:040
243 * Once our Guest is initialized, the Launcher makes it run by reading
244 * from /dev/lguest.
245 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700246static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
247{
248 struct lguest *lg = file->private_data;
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200249 struct lg_cpu *cpu;
250 unsigned int cpu_id = *o;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700251
Rusty Russelldde79782007-07-26 10:41:03 -0700252 /* You must write LHREQ_INITIALIZE first! */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700253 if (!lg)
254 return -EINVAL;
255
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200256 /* Watch out for arbitrary vcpu indexes! */
257 if (cpu_id >= lg->nr_cpus)
258 return -EINVAL;
259
260 cpu = &lg->cpus[cpu_id];
261
Rusty Russelle1e72962007-10-25 15:02:50 +1000262 /* If you're not the task which owns the Guest, go away. */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200263 if (current != cpu->tsk)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700264 return -EPERM;
265
Rusty Russella6bd8e12008-03-28 11:05:53 -0500266 /* If the Guest is already dead, we indicate why */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700267 if (lg->dead) {
268 size_t len;
269
Rusty Russelldde79782007-07-26 10:41:03 -0700270 /* lg->dead either contains an error code, or a string. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700271 if (IS_ERR(lg->dead))
272 return PTR_ERR(lg->dead);
273
Rusty Russelldde79782007-07-26 10:41:03 -0700274 /* We can only return as much as the buffer they read with. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700275 len = min(size, strlen(lg->dead)+1);
276 if (copy_to_user(user, lg->dead, len) != 0)
277 return -EFAULT;
278 return len;
279 }
280
Rusty Russell2e04ef72009-07-30 16:03:45 -0600281 /*
282 * If we returned from read() last time because the Guest sent I/O,
283 * clear the flag.
284 */
Glauber de Oliveira Costa5e232f42008-01-07 11:05:36 -0200285 if (cpu->pending_notify)
286 cpu->pending_notify = 0;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700287
Rusty Russelldde79782007-07-26 10:41:03 -0700288 /* Run the Guest until something interesting happens. */
Glauber de Oliveira Costad0953d42008-01-07 11:05:25 -0200289 return run_guest(cpu, (unsigned long __user *)user);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700290}
291
Rusty Russell2e04ef72009-07-30 16:03:45 -0600292/*L:025
293 * This actually initializes a CPU. For the moment, a Guest is only
294 * uniprocessor, so "id" is always 0.
295 */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200296static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
297{
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930298 /* We have a limited number of CPUs in the lguest struct. */
Rusty Russell24adf122008-05-02 21:50:51 -0500299 if (id >= ARRAY_SIZE(cpu->lg->cpus))
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200300 return -EINVAL;
301
Rusty Russella6bd8e12008-03-28 11:05:53 -0500302 /* Set up this CPU's id, and pointer back to the lguest struct. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200303 cpu->id = id;
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930304 cpu->lg = container_of(cpu, struct lguest, cpus[id]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200305 cpu->lg->nr_cpus++;
Rusty Russella6bd8e12008-03-28 11:05:53 -0500306
307 /* Each CPU has a timer it can set. */
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200308 init_clockdev(cpu);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200309
Rusty Russell2e04ef72009-07-30 16:03:45 -0600310 /*
311 * We need a complete page for the Guest registers: they are accessible
312 * to the Guest and we can only grant it access to whole pages.
313 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200314 cpu->regs_page = get_zeroed_page(GFP_KERNEL);
315 if (!cpu->regs_page)
316 return -ENOMEM;
317
Cosmin Paraschivc2ecd512013-04-30 09:23:09 +0930318 /* We actually put the registers at the end of the page. */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200319 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
320
Rusty Russell2e04ef72009-07-30 16:03:45 -0600321 /*
322 * Now we initialize the Guest's registers, handing it the start
323 * address.
324 */
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200325 lguest_arch_setup_regs(cpu, start_ip);
326
Rusty Russell2e04ef72009-07-30 16:03:45 -0600327 /*
328 * We keep a pointer to the Launcher task (ie. current task) for when
329 * other Guests want to wake this one (eg. console input).
330 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200331 cpu->tsk = current;
332
Rusty Russell2e04ef72009-07-30 16:03:45 -0600333 /*
334 * We need to keep a pointer to the Launcher's memory map, because if
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200335 * the Launcher dies we need to clean it up. If we don't keep a
Rusty Russell2e04ef72009-07-30 16:03:45 -0600336 * reference, it is destroyed before close() is called.
337 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200338 cpu->mm = get_task_mm(cpu->tsk);
339
Rusty Russell2e04ef72009-07-30 16:03:45 -0600340 /*
341 * We remember which CPU's pages this Guest used last, for optimization
342 * when the same Guest runs on the same CPU twice.
343 */
Glauber de Oliveira Costaf34f8c52008-01-17 19:13:26 -0200344 cpu->last_pages = NULL;
345
Rusty Russella6bd8e12008-03-28 11:05:53 -0500346 /* No error == success. */
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200347 return 0;
348}
349
Rusty Russell2e04ef72009-07-30 16:03:45 -0600350/*L:020
351 * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in
352 * addition to the LHREQ_INITIALIZE value). These are:
Rusty Russelldde79782007-07-26 10:41:03 -0700353 *
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000354 * base: The start of the Guest-physical memory inside the Launcher memory.
355 *
Rusty Russelldde79782007-07-26 10:41:03 -0700356 * pfnlimit: The highest (Guest-physical) page number the Guest should be
Rusty Russelle1e72962007-10-25 15:02:50 +1000357 * allowed to access. The Guest memory lives inside the Launcher, so it sets
358 * this to ensure the Guest can only reach its own memory.
Rusty Russelldde79782007-07-26 10:41:03 -0700359 *
Rusty Russelldde79782007-07-26 10:41:03 -0700360 * start: The first instruction to execute ("eip" in x86-speak).
Rusty Russelldde79782007-07-26 10:41:03 -0700361 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000362static int initialize(struct file *file, const unsigned long __user *input)
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700363{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600364 /* "struct lguest" contains all we (the Host) know about a Guest. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700365 struct lguest *lg;
Rusty Russell48245cc2007-10-22 11:03:27 +1000366 int err;
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300367 unsigned long args[3];
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700368
Rusty Russell2e04ef72009-07-30 16:03:45 -0600369 /*
370 * We grab the Big Lguest lock, which protects against multiple
371 * simultaneous initializations.
372 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700373 mutex_lock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700374 /* You can't initialize twice! Close the device and start again... */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700375 if (file->private_data) {
376 err = -EBUSY;
377 goto unlock;
378 }
379
380 if (copy_from_user(args, input, sizeof(args)) != 0) {
381 err = -EFAULT;
382 goto unlock;
383 }
384
Rusty Russell48245cc2007-10-22 11:03:27 +1000385 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
386 if (!lg) {
387 err = -ENOMEM;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700388 goto unlock;
389 }
Rusty Russelldde79782007-07-26 10:41:03 -0700390
Rusty Russelldf60aee2009-06-12 22:27:09 -0600391 lg->eventfds = kmalloc(sizeof(*lg->eventfds), GFP_KERNEL);
392 if (!lg->eventfds) {
393 err = -ENOMEM;
394 goto free_lg;
395 }
396 lg->eventfds->num = 0;
397
Rusty Russelldde79782007-07-26 10:41:03 -0700398 /* Populate the easy fields of our "struct lguest" */
Al Viro74dbf712008-03-29 03:08:28 +0000399 lg->mem_base = (void __user *)args[0];
Rusty Russell3c6b5bf2007-10-22 11:03:26 +1000400 lg->pfn_limit = args[1];
Rusty Russelldde79782007-07-26 10:41:03 -0700401
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300402 /* This is the first cpu (cpu 0) and it will start booting at args[2] */
403 err = lg_cpu_start(&lg->cpus[0], 0, args[2]);
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200404 if (err)
Rusty Russelldf60aee2009-06-12 22:27:09 -0600405 goto free_eventfds;
Glauber de Oliveira Costa4dcc53d2008-01-07 11:05:24 -0200406
Rusty Russell2e04ef72009-07-30 16:03:45 -0600407 /*
Rusty Russell9f542882011-07-22 14:39:50 +0930408 * Initialize the Guest's shadow page tables. This allocates
409 * memory, so can fail.
Rusty Russell2e04ef72009-07-30 16:03:45 -0600410 */
Matias Zabaljauregui58a24562008-09-29 01:40:07 -0300411 err = init_guest_pagetable(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700412 if (err)
413 goto free_regs;
414
Rusty Russelldde79782007-07-26 10:41:03 -0700415 /* We keep our "struct lguest" in the file's private_data. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700416 file->private_data = lg;
417
418 mutex_unlock(&lguest_lock);
419
Rusty Russelldde79782007-07-26 10:41:03 -0700420 /* And because this is a write() call, we return the length used. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700421 return sizeof(args);
422
423free_regs:
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200424 /* FIXME: This should be in free_vcpu */
425 free_page(lg->cpus[0].regs_page);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600426free_eventfds:
427 kfree(lg->eventfds);
428free_lg:
Adrian Bunk43054412007-11-14 16:59:00 -0800429 kfree(lg);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700430unlock:
431 mutex_unlock(&lguest_lock);
432 return err;
433}
434
Rusty Russell2e04ef72009-07-30 16:03:45 -0600435/*L:010
436 * The first operation the Launcher does must be a write. All writes
Rusty Russelle1e72962007-10-25 15:02:50 +1000437 * start with an unsigned long number: for the first write this must be
Rusty Russelldde79782007-07-26 10:41:03 -0700438 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
Rusty Russella91d74a2009-07-30 16:03:45 -0600439 * writes of other values to send interrupts or set up receipt of notifications.
Rusty Russella6bd8e12008-03-28 11:05:53 -0500440 *
441 * Note that we overload the "offset" in the /dev/lguest file to indicate what
Rusty Russella91d74a2009-07-30 16:03:45 -0600442 * CPU number we're dealing with. Currently this is always 0 since we only
Rusty Russella6bd8e12008-03-28 11:05:53 -0500443 * support uniprocessor Guests, but you can see the beginnings of SMP support
Rusty Russell2e04ef72009-07-30 16:03:45 -0600444 * here.
445 */
Jes Sorensen511801d2007-10-22 11:03:31 +1000446static ssize_t write(struct file *file, const char __user *in,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700447 size_t size, loff_t *off)
448{
Rusty Russell2e04ef72009-07-30 16:03:45 -0600449 /*
450 * Once the Guest is initialized, we hold the "struct lguest" in the
451 * file private data.
452 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700453 struct lguest *lg = file->private_data;
Jes Sorensen511801d2007-10-22 11:03:31 +1000454 const unsigned long __user *input = (const unsigned long __user *)in;
455 unsigned long req;
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200456 struct lg_cpu *uninitialized_var(cpu);
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200457 unsigned int cpu_id = *off;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700458
Rusty Russella6bd8e12008-03-28 11:05:53 -0500459 /* The first value tells us what this request is. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700460 if (get_user(req, input) != 0)
461 return -EFAULT;
Jes Sorensen511801d2007-10-22 11:03:31 +1000462 input++;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700463
Rusty Russelldde79782007-07-26 10:41:03 -0700464 /* If you haven't initialized, you must do that first. */
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200465 if (req != LHREQ_INITIALIZE) {
466 if (!lg || (cpu_id >= lg->nr_cpus))
467 return -EINVAL;
468 cpu = &lg->cpus[cpu_id];
Eugene Teof73d1e62008-02-09 23:53:17 +0800469
470 /* Once the Guest is dead, you can only read() why it died. */
471 if (lg->dead)
472 return -ENOENT;
Glauber de Oliveira Costa7ea07a12008-01-07 11:05:26 -0200473 }
Rusty Russelldde79782007-07-26 10:41:03 -0700474
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700475 switch (req) {
476 case LHREQ_INITIALIZE:
Jes Sorensen511801d2007-10-22 11:03:31 +1000477 return initialize(file, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700478 case LHREQ_IRQ:
Glauber de Oliveira Costa177e4492008-01-07 11:05:29 -0200479 return user_send_irq(cpu, input);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600480 case LHREQ_EVENTFD:
481 return attach_eventfd(lg, input);
Rusty Russell18c13732015-02-11 15:15:09 +1030482 case LHREQ_GETREG:
483 return getreg_setup(cpu, input);
484 case LHREQ_SETREG:
485 return setreg(cpu, input);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700486 default:
487 return -EINVAL;
488 }
489}
490
Rusty Russell2e04ef72009-07-30 16:03:45 -0600491/*L:060
492 * The final piece of interface code is the close() routine. It reverses
Rusty Russelldde79782007-07-26 10:41:03 -0700493 * everything done in initialize(). This is usually called because the
494 * Launcher exited.
495 *
496 * Note that the close routine returns 0 or a negative error number: it can't
497 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
Rusty Russell2e04ef72009-07-30 16:03:45 -0600498 * letting them do it.
499:*/
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700500static int close(struct inode *inode, struct file *file)
501{
502 struct lguest *lg = file->private_data;
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200503 unsigned int i;
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700504
Rusty Russelldde79782007-07-26 10:41:03 -0700505 /* If we never successfully initialized, there's nothing to clean up */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700506 if (!lg)
507 return 0;
508
Rusty Russell2e04ef72009-07-30 16:03:45 -0600509 /*
510 * We need the big lock, to protect from inter-guest I/O and other
511 * Launchers initializing guests.
512 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700513 mutex_lock(&lguest_lock);
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200514
515 /* Free up the shadow page tables for the Guest. */
516 free_guest_pagetable(lg);
517
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200518 for (i = 0; i < lg->nr_cpus; i++) {
Glauber de Oliveira Costaad8d8f32008-01-07 11:05:28 -0200519 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
520 hrtimer_cancel(&lg->cpus[i].hrt);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200521 /* We can free up the register page we allocated. */
522 free_page(lg->cpus[i].regs_page);
Rusty Russell2e04ef72009-07-30 16:03:45 -0600523 /*
524 * Now all the memory cleanups are done, it's safe to release
525 * the Launcher's memory management structure.
526 */
Glauber de Oliveira Costa66686c22008-01-07 11:05:34 -0200527 mmput(lg->cpus[i].mm);
Glauber de Oliveira Costaa53a35a2008-01-07 11:05:32 -0200528 }
Rusty Russelldf60aee2009-06-12 22:27:09 -0600529
530 /* Release any eventfds they registered. */
531 for (i = 0; i < lg->eventfds->num; i++)
Davide Libenzi13389012009-06-30 11:41:11 -0700532 eventfd_ctx_put(lg->eventfds->map[i].event);
Rusty Russelldf60aee2009-06-12 22:27:09 -0600533 kfree(lg->eventfds);
534
Rusty Russell2e04ef72009-07-30 16:03:45 -0600535 /*
536 * If lg->dead doesn't contain an error code it will be NULL or a
537 * kmalloc()ed string, either of which is ok to hand to kfree().
538 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700539 if (!IS_ERR(lg->dead))
540 kfree(lg->dead);
Mark Wallis05dfdbb2009-01-26 17:32:35 +1100541 /* Free the memory allocated to the lguest_struct */
542 kfree(lg);
Rusty Russelldde79782007-07-26 10:41:03 -0700543 /* Release lock and exit. */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700544 mutex_unlock(&lguest_lock);
Rusty Russelldde79782007-07-26 10:41:03 -0700545
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700546 return 0;
547}
548
Rusty Russelldde79782007-07-26 10:41:03 -0700549/*L:000
550 * Welcome to our journey through the Launcher!
551 *
552 * The Launcher is the Host userspace program which sets up, runs and services
553 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
554 * doing things are inaccurate: the Launcher does all the device handling for
Rusty Russelle1e72962007-10-25 15:02:50 +1000555 * the Guest, but the Guest can't know that.
Rusty Russelldde79782007-07-26 10:41:03 -0700556 *
557 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
558 * shall see more of that later.
559 *
560 * We begin our understanding with the Host kernel interface which the Launcher
561 * uses: reading and writing a character device called /dev/lguest. All the
Rusty Russell2e04ef72009-07-30 16:03:45 -0600562 * work happens in the read(), write() and close() routines:
563 */
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700564static const struct file_operations lguest_fops = {
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700565 .owner = THIS_MODULE,
566 .release = close,
567 .write = write,
568 .read = read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200569 .llseek = default_llseek,
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700570};
Rusty Russell9f542882011-07-22 14:39:50 +0930571/*:*/
Rusty Russelldde79782007-07-26 10:41:03 -0700572
Rusty Russell2e04ef72009-07-30 16:03:45 -0600573/*
574 * This is a textbook example of a "misc" character device. Populate a "struct
575 * miscdevice" and register it with misc_register().
576 */
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700577static struct miscdevice lguest_dev = {
578 .minor = MISC_DYNAMIC_MINOR,
579 .name = "lguest",
580 .fops = &lguest_fops,
581};
582
583int __init lguest_device_init(void)
584{
585 return misc_register(&lguest_dev);
586}
587
588void __exit lguest_device_remove(void)
589{
590 misc_deregister(&lguest_dev);
591}