Initial commit
[wrt350n-kernel.git] / drivers / lguest / lguest_user.c
blob85d42d3d01a9a9bbeb739ce422271848cd4be3ff
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 <linux/sched.h>
10 #include "lg.h"
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. */
17 static int break_guest_out(struct lg_cpu *cpu, const unsigned long __user*input)
19 unsigned long on;
21 /* Fetch whether they're turning break on or off. */
22 if (get_user(on, input) != 0)
23 return -EFAULT;
25 if (on) {
26 cpu->break_out = 1;
27 /* Pop it out of the Guest (may be running on different CPU) */
28 wake_up_process(cpu->tsk);
29 /* Wait for them to reset it */
30 return wait_event_interruptible(cpu->break_wq, !cpu->break_out);
31 } else {
32 cpu->break_out = 0;
33 wake_up(&cpu->break_wq);
34 return 0;
38 /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
39 * number to /dev/lguest. */
40 static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input)
42 unsigned long irq;
44 if (get_user(irq, input) != 0)
45 return -EFAULT;
46 if (irq >= LGUEST_IRQS)
47 return -EINVAL;
48 /* Next time the Guest runs, the core code will see if it can deliver
49 * this interrupt. */
50 set_bit(irq, cpu->irqs_pending);
51 return 0;
54 /*L:040 Once our Guest is initialized, the Launcher makes it run by reading
55 * from /dev/lguest. */
56 static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
58 struct lguest *lg = file->private_data;
59 struct lg_cpu *cpu;
60 unsigned int cpu_id = *o;
62 /* You must write LHREQ_INITIALIZE first! */
63 if (!lg)
64 return -EINVAL;
66 /* Watch out for arbitrary vcpu indexes! */
67 if (cpu_id >= lg->nr_cpus)
68 return -EINVAL;
70 cpu = &lg->cpus[cpu_id];
72 /* If you're not the task which owns the Guest, go away. */
73 if (current != cpu->tsk)
74 return -EPERM;
76 /* If the guest is already dead, we indicate why */
77 if (lg->dead) {
78 size_t len;
80 /* lg->dead either contains an error code, or a string. */
81 if (IS_ERR(lg->dead))
82 return PTR_ERR(lg->dead);
84 /* We can only return as much as the buffer they read with. */
85 len = min(size, strlen(lg->dead)+1);
86 if (copy_to_user(user, lg->dead, len) != 0)
87 return -EFAULT;
88 return len;
91 /* If we returned from read() last time because the Guest notified,
92 * clear the flag. */
93 if (cpu->pending_notify)
94 cpu->pending_notify = 0;
96 /* Run the Guest until something interesting happens. */
97 return run_guest(cpu, (unsigned long __user *)user);
100 static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
102 if (id >= NR_CPUS)
103 return -EINVAL;
105 cpu->id = id;
106 cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
107 cpu->lg->nr_cpus++;
108 init_clockdev(cpu);
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;
116 /* We actually put the registers at the bottom of the page. */
117 cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs);
119 /* Now we initialize the Guest's registers, handing it the start
120 * address. */
121 lguest_arch_setup_regs(cpu, start_ip);
123 /* Initialize the queue for the waker to wait on */
124 init_waitqueue_head(&cpu->break_wq);
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;
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);
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;
139 return 0;
142 /*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
143 * values (in addition to the LHREQ_INITIALIZE value). These are:
145 * base: The start of the Guest-physical memory inside the Launcher memory.
147 * pfnlimit: The highest (Guest-physical) page number the Guest should be
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.
151 * pgdir: The (Guest-physical) address of the top of the initial Guest
152 * pagetables (which are set up by the Launcher).
154 * start: The first instruction to execute ("eip" in x86-speak).
156 static int initialize(struct file *file, const unsigned long __user *input)
158 /* "struct lguest" contains everything we (the Host) know about a
159 * Guest. */
160 struct lguest *lg;
161 int err;
162 unsigned long args[4];
164 /* We grab the Big Lguest lock, which protects against multiple
165 * simultaneous initializations. */
166 mutex_lock(&lguest_lock);
167 /* You can't initialize twice! Close the device and start again... */
168 if (file->private_data) {
169 err = -EBUSY;
170 goto unlock;
173 if (copy_from_user(args, input, sizeof(args)) != 0) {
174 err = -EFAULT;
175 goto unlock;
178 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
179 if (!lg) {
180 err = -ENOMEM;
181 goto unlock;
184 /* Populate the easy fields of our "struct lguest" */
185 lg->mem_base = (void __user *)(long)args[0];
186 lg->pfn_limit = args[1];
188 /* This is the first cpu */
189 err = lg_cpu_start(&lg->cpus[0], 0, args[3]);
190 if (err)
191 goto release_guest;
193 /* Initialize the Guest's shadow page tables, using the toplevel
194 * address the Launcher gave us. This allocates memory, so can
195 * fail. */
196 err = init_guest_pagetable(lg, args[2]);
197 if (err)
198 goto free_regs;
200 /* We keep our "struct lguest" in the file's private_data. */
201 file->private_data = lg;
203 mutex_unlock(&lguest_lock);
205 /* And because this is a write() call, we return the length used. */
206 return sizeof(args);
208 free_regs:
209 /* FIXME: This should be in free_vcpu */
210 free_page(lg->cpus[0].regs_page);
211 release_guest:
212 kfree(lg);
213 unlock:
214 mutex_unlock(&lguest_lock);
215 return err;
218 /*L:010 The first operation the Launcher does must be a write. All writes
219 * start with an unsigned long number: for the first write this must be
220 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
221 * writes of other values to send interrupts. */
222 static ssize_t write(struct file *file, const char __user *in,
223 size_t size, loff_t *off)
225 /* Once the guest is initialized, we hold the "struct lguest" in the
226 * file private data. */
227 struct lguest *lg = file->private_data;
228 const unsigned long __user *input = (const unsigned long __user *)in;
229 unsigned long req;
230 struct lg_cpu *uninitialized_var(cpu);
231 unsigned int cpu_id = *off;
233 if (get_user(req, input) != 0)
234 return -EFAULT;
235 input++;
237 /* If you haven't initialized, you must do that first. */
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;
246 /* Once the Guest is dead, all you can do is read() why it died. */
247 if (lg && lg->dead)
248 return -ENOENT;
250 /* If you're not the task which owns the Guest, you can only break */
251 if (lg && current != cpu->tsk && req != LHREQ_BREAK)
252 return -EPERM;
254 switch (req) {
255 case LHREQ_INITIALIZE:
256 return initialize(file, input);
257 case LHREQ_IRQ:
258 return user_send_irq(cpu, input);
259 case LHREQ_BREAK:
260 return break_guest_out(cpu, input);
261 default:
262 return -EINVAL;
266 /*L:060 The final piece of interface code is the close() routine. It reverses
267 * everything done in initialize(). This is usually called because the
268 * Launcher exited.
270 * Note that the close routine returns 0 or a negative error number: it can't
271 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
272 * letting them do it. :*/
273 static int close(struct inode *inode, struct file *file)
275 struct lguest *lg = file->private_data;
276 unsigned int i;
278 /* If we never successfully initialized, there's nothing to clean up */
279 if (!lg)
280 return 0;
282 /* We need the big lock, to protect from inter-guest I/O and other
283 * Launchers initializing guests. */
284 mutex_lock(&lguest_lock);
286 /* Free up the shadow page tables for the Guest. */
287 free_guest_pagetable(lg);
289 for (i = 0; i < lg->nr_cpus; i++) {
290 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
291 hrtimer_cancel(&lg->cpus[i].hrt);
292 /* We can free up the register page we allocated. */
293 free_page(lg->cpus[i].regs_page);
294 /* Now all the memory cleanups are done, it's safe to release
295 * the Launcher's memory management structure. */
296 mmput(lg->cpus[i].mm);
298 /* If lg->dead doesn't contain an error code it will be NULL or a
299 * kmalloc()ed string, either of which is ok to hand to kfree(). */
300 if (!IS_ERR(lg->dead))
301 kfree(lg->dead);
302 /* We clear the entire structure, which also marks it as free for the
303 * next user. */
304 memset(lg, 0, sizeof(*lg));
305 /* Release lock and exit. */
306 mutex_unlock(&lguest_lock);
308 return 0;
311 /*L:000
312 * Welcome to our journey through the Launcher!
314 * The Launcher is the Host userspace program which sets up, runs and services
315 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
316 * doing things are inaccurate: the Launcher does all the device handling for
317 * the Guest, but the Guest can't know that.
319 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
320 * shall see more of that later.
322 * We begin our understanding with the Host kernel interface which the Launcher
323 * uses: reading and writing a character device called /dev/lguest. All the
324 * work happens in the read(), write() and close() routines: */
325 static struct file_operations lguest_fops = {
326 .owner = THIS_MODULE,
327 .release = close,
328 .write = write,
329 .read = read,
332 /* This is a textbook example of a "misc" character device. Populate a "struct
333 * miscdevice" and register it with misc_register(). */
334 static struct miscdevice lguest_dev = {
335 .minor = MISC_DYNAMIC_MINOR,
336 .name = "lguest",
337 .fops = &lguest_fops,
340 int __init lguest_device_init(void)
342 return misc_register(&lguest_dev);
345 void __exit lguest_device_remove(void)
347 misc_deregister(&lguest_dev);