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