]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/lguest/lguest_user.c
lguest: per-vcpu lguest task management
[linux-2.6-omap-h63xx.git] / drivers / lguest / lguest_user.c
1 /*P:200 This contains all the /dev/lguest code, whereby the userspace launcher
2  * controls and communicates with the Guest.  For example, the first write will
3  * tell us the Guest's memory layout, pagetable, entry point and kernel address
4  * offset.  A read will run the Guest until something happens, such as a signal
5  * or the Guest doing a NOTIFY out to the Launcher. :*/
6 #include <linux/uaccess.h>
7 #include <linux/miscdevice.h>
8 #include <linux/fs.h>
9 #include "lg.h"
10
11 /*L:055 When something happens, the Waker process needs a way to stop the
12  * kernel running the Guest and return to the Launcher.  So the Waker writes
13  * LHREQ_BREAK and the value "1" to /dev/lguest to do this.  Once the Launcher
14  * has done whatever needs attention, it writes LHREQ_BREAK and "0" to release
15  * the Waker. */
16 static int break_guest_out(struct lg_cpu *cpu, const unsigned long __user*input)
17 {
18         unsigned long on;
19
20         /* Fetch whether they're turning break on or off. */
21         if (get_user(on, input) != 0)
22                 return -EFAULT;
23
24         if (on) {
25                 cpu->break_out = 1;
26                 /* Pop it out of the Guest (may be running on different CPU) */
27                 wake_up_process(cpu->tsk);
28                 /* Wait for them to reset it */
29                 return wait_event_interruptible(cpu->break_wq, !cpu->break_out);
30         } else {
31                 cpu->break_out = 0;
32                 wake_up(&cpu->break_wq);
33                 return 0;
34         }
35 }
36
37 /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
38  * number to /dev/lguest. */
39 static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
40 {
41         unsigned long irq;
42
43         if (get_user(irq, input) != 0)
44                 return -EFAULT;
45         if (irq >= LGUEST_IRQS)
46                 return -EINVAL;
47         /* Next time the Guest runs, the core code will see if it can deliver
48          * this interrupt. */
49         set_bit(irq, cpu->irqs_pending);
50         return 0;
51 }
52
53 /*L:040 Once our Guest is initialized, the Launcher makes it run by reading
54  * from /dev/lguest. */
55 static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
56 {
57         struct lguest *lg = file->private_data;
58         struct lg_cpu *cpu;
59         unsigned int cpu_id = *o;
60
61         /* You must write LHREQ_INITIALIZE first! */
62         if (!lg)
63                 return -EINVAL;
64
65         /* Watch out for arbitrary vcpu indexes! */
66         if (cpu_id >= lg->nr_cpus)
67                 return -EINVAL;
68
69         cpu = &lg->cpus[cpu_id];
70
71         /* If you're not the task which owns the Guest, go away. */
72         if (current != cpu->tsk)
73                 return -EPERM;
74
75         /* If the guest is already dead, we indicate why */
76         if (lg->dead) {
77                 size_t len;
78
79                 /* lg->dead either contains an error code, or a string. */
80                 if (IS_ERR(lg->dead))
81                         return PTR_ERR(lg->dead);
82
83                 /* We can only return as much as the buffer they read with. */
84                 len = min(size, strlen(lg->dead)+1);
85                 if (copy_to_user(user, lg->dead, len) != 0)
86                         return -EFAULT;
87                 return len;
88         }
89
90         /* If we returned from read() last time because the Guest notified,
91          * clear the flag. */
92         if (lg->pending_notify)
93                 lg->pending_notify = 0;
94
95         /* Run the Guest until something interesting happens. */
96         return run_guest(cpu, (unsigned long __user *)user);
97 }
98
99 static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
100 {
101         if (id >= NR_CPUS)
102                 return -EINVAL;
103
104         cpu->id = id;
105         cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
106         cpu->lg->nr_cpus++;
107         init_clockdev(cpu);
108
109         /* We need a complete page for the Guest registers: they are accessible
110          * to the Guest and we can only grant it access to whole pages. */
111         cpu->regs_page = get_zeroed_page(GFP_KERNEL);
112         if (!cpu->regs_page)
113                 return -ENOMEM;
114
115         /* We actually put the registers at the bottom of the page. */
116         cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
117
118         /* Now we initialize the Guest's registers, handing it the start
119          * address. */
120         lguest_arch_setup_regs(cpu, start_ip);
121
122         /* Initialize the queue for the waker to wait on */
123         init_waitqueue_head(&cpu->break_wq);
124
125         /* We keep a pointer to the Launcher task (ie. current task) for when
126          * other Guests want to wake this one (inter-Guest I/O). */
127         cpu->tsk = current;
128
129         /* We need to keep a pointer to the Launcher's memory map, because if
130          * the Launcher dies we need to clean it up.  If we don't keep a
131          * reference, it is destroyed before close() is called. */
132         cpu->mm = get_task_mm(cpu->tsk);
133
134         return 0;
135 }
136
137 /*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
138  * values (in addition to the LHREQ_INITIALIZE value).  These are:
139  *
140  * base: The start of the Guest-physical memory inside the Launcher memory.
141  *
142  * pfnlimit: The highest (Guest-physical) page number the Guest should be
143  * allowed to access.  The Guest memory lives inside the Launcher, so it sets
144  * this to ensure the Guest can only reach its own memory.
145  *
146  * pgdir: The (Guest-physical) address of the top of the initial Guest
147  * pagetables (which are set up by the Launcher).
148  *
149  * start: The first instruction to execute ("eip" in x86-speak).
150  */
151 static int initialize(struct file *file, const unsigned long __user *input)
152 {
153         /* "struct lguest" contains everything we (the Host) know about a
154          * Guest. */
155         struct lguest *lg;
156         int err;
157         unsigned long args[4];
158
159         /* We grab the Big Lguest lock, which protects against multiple
160          * simultaneous initializations. */
161         mutex_lock(&lguest_lock);
162         /* You can't initialize twice!  Close the device and start again... */
163         if (file->private_data) {
164                 err = -EBUSY;
165                 goto unlock;
166         }
167
168         if (copy_from_user(args, input, sizeof(args)) != 0) {
169                 err = -EFAULT;
170                 goto unlock;
171         }
172
173         lg = kzalloc(sizeof(*lg), GFP_KERNEL);
174         if (!lg) {
175                 err = -ENOMEM;
176                 goto unlock;
177         }
178
179         /* Populate the easy fields of our "struct lguest" */
180         lg->mem_base = (void __user *)(long)args[0];
181         lg->pfn_limit = args[1];
182
183         /* This is the first cpu */
184         err = lg_cpu_start(&lg->cpus[0], 0, args[3]);
185         if (err)
186                 goto release_guest;
187
188         /* Initialize the Guest's shadow page tables, using the toplevel
189          * address the Launcher gave us.  This allocates memory, so can
190          * fail. */
191         err = init_guest_pagetable(lg, args[2]);
192         if (err)
193                 goto free_regs;
194
195         /* We remember which CPU's pages this Guest used last, for optimization
196          * when the same Guest runs on the same CPU twice. */
197         lg->last_pages = NULL;
198
199         /* We keep our "struct lguest" in the file's private_data. */
200         file->private_data = lg;
201
202         mutex_unlock(&lguest_lock);
203
204         /* And because this is a write() call, we return the length used. */
205         return sizeof(args);
206
207 free_regs:
208         /* FIXME: This should be in free_vcpu */
209         free_page(lg->cpus[0].regs_page);
210 release_guest:
211         kfree(lg);
212 unlock:
213         mutex_unlock(&lguest_lock);
214         return err;
215 }
216
217 /*L:010 The first operation the Launcher does must be a write.  All writes
218  * start with an unsigned long number: for the first write this must be
219  * LHREQ_INITIALIZE to set up the Guest.  After that the Launcher can use
220  * writes of other values to send interrupts. */
221 static ssize_t write(struct file *file, const char __user *in,
222                      size_t size, loff_t *off)
223 {
224         /* Once the guest is initialized, we hold the "struct lguest" in the
225          * file private data. */
226         struct lguest *lg = file->private_data;
227         const unsigned long __user *input = (const unsigned long __user *)in;
228         unsigned long req;
229         struct lg_cpu *uninitialized_var(cpu);
230         unsigned int cpu_id = *off;
231
232         if (get_user(req, input) != 0)
233                 return -EFAULT;
234         input++;
235
236         /* If you haven't initialized, you must do that first. */
237         if (req != LHREQ_INITIALIZE) {
238                 if (!lg || (cpu_id >= lg->nr_cpus))
239                         return -EINVAL;
240                 cpu = &lg->cpus[cpu_id];
241                 if (!cpu)
242                         return -EINVAL;
243         }
244
245         /* Once the Guest is dead, all you can do is read() why it died. */
246         if (lg && lg->dead)
247                 return -ENOENT;
248
249         /* If you're not the task which owns the Guest, you can only break */
250         if (lg && current != cpu->tsk && req != LHREQ_BREAK)
251                 return -EPERM;
252
253         switch (req) {
254         case LHREQ_INITIALIZE:
255                 return initialize(file, input);
256         case LHREQ_IRQ:
257                 return user_send_irq(cpu, input);
258         case LHREQ_BREAK:
259                 return break_guest_out(cpu, input);
260         default:
261                 return -EINVAL;
262         }
263 }
264
265 /*L:060 The final piece of interface code is the close() routine.  It reverses
266  * everything done in initialize().  This is usually called because the
267  * Launcher exited.
268  *
269  * Note that the close routine returns 0 or a negative error number: it can't
270  * really fail, but it can whine.  I blame Sun for this wart, and K&R C for
271  * letting them do it. :*/
272 static int close(struct inode *inode, struct file *file)
273 {
274         struct lguest *lg = file->private_data;
275         unsigned int i;
276
277         /* If we never successfully initialized, there's nothing to clean up */
278         if (!lg)
279                 return 0;
280
281         /* We need the big lock, to protect from inter-guest I/O and other
282          * Launchers initializing guests. */
283         mutex_lock(&lguest_lock);
284
285         /* Free up the shadow page tables for the Guest. */
286         free_guest_pagetable(lg);
287
288         for (i = 0; i < lg->nr_cpus; i++) {
289                 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
290                 hrtimer_cancel(&lg->cpus[i].hrt);
291                 /* We can free up the register page we allocated. */
292                 free_page(lg->cpus[i].regs_page);
293                 /* Now all the memory cleanups are done, it's safe to release
294                  * the Launcher's memory management structure. */
295                 mmput(lg->cpus[i].mm);
296         }
297         /* If lg->dead doesn't contain an error code it will be NULL or a
298          * kmalloc()ed string, either of which is ok to hand to kfree(). */
299         if (!IS_ERR(lg->dead))
300                 kfree(lg->dead);
301         /* We clear the entire structure, which also marks it as free for the
302          * next user. */
303         memset(lg, 0, sizeof(*lg));
304         /* Release lock and exit. */
305         mutex_unlock(&lguest_lock);
306
307         return 0;
308 }
309
310 /*L:000
311  * Welcome to our journey through the Launcher!
312  *
313  * The Launcher is the Host userspace program which sets up, runs and services
314  * the Guest.  In fact, many comments in the Drivers which refer to "the Host"
315  * doing things are inaccurate: the Launcher does all the device handling for
316  * the Guest, but the Guest can't know that.
317  *
318  * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
319  * shall see more of that later.
320  *
321  * We begin our understanding with the Host kernel interface which the Launcher
322  * uses: reading and writing a character device called /dev/lguest.  All the
323  * work happens in the read(), write() and close() routines: */
324 static struct file_operations lguest_fops = {
325         .owner   = THIS_MODULE,
326         .release = close,
327         .write   = write,
328         .read    = read,
329 };
330
331 /* This is a textbook example of a "misc" character device.  Populate a "struct
332  * miscdevice" and register it with misc_register(). */
333 static struct miscdevice lguest_dev = {
334         .minor  = MISC_DYNAMIC_MINOR,
335         .name   = "lguest",
336         .fops   = &lguest_fops,
337 };
338
339 int __init lguest_device_init(void)
340 {
341         return misc_register(&lguest_dev);
342 }
343
344 void __exit lguest_device_remove(void)
345 {
346         misc_deregister(&lguest_dev);
347 }