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