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>
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
16 static int break_guest_out(struct lguest *lg, const unsigned long __user *input)
20 /* Fetch whether they're turning break on or off. */
21 if (get_user(on, input) != 0)
26 /* Pop it out of the Guest (may be running on different CPU) */
27 wake_up_process(lg->tsk);
28 /* Wait for them to reset it */
29 return wait_event_interruptible(lg->break_wq, !lg->break_out);
32 wake_up(&lg->break_wq);
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 lguest *lg, const unsigned long __user *input)
43 if (get_user(irq, input) != 0)
45 if (irq >= LGUEST_IRQS)
47 /* Next time the Guest runs, the core code will see if it can deliver
49 set_bit(irq, lg->irqs_pending);
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)
57 struct lguest *lg = file->private_data;
59 /* You must write LHREQ_INITIALIZE first! */
63 /* If you're not the task which owns the Guest, go away. */
64 if (current != lg->tsk)
67 /* If the guest is already dead, we indicate why */
71 /* lg->dead either contains an error code, or a string. */
73 return PTR_ERR(lg->dead);
75 /* We can only return as much as the buffer they read with. */
76 len = min(size, strlen(lg->dead)+1);
77 if (copy_to_user(user, lg->dead, len) != 0)
82 /* If we returned from read() last time because the Guest notified,
84 if (lg->pending_notify)
85 lg->pending_notify = 0;
87 /* Run the Guest until something interesting happens. */
88 return run_guest(lg, (unsigned long __user *)user);
91 static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip)
97 cpu->lg = container_of((cpu - id), struct lguest, cpus[0]);
103 /*L:020 The initialization write supplies 4 pointer sized (32 or 64 bit)
104 * values (in addition to the LHREQ_INITIALIZE value). These are:
106 * base: The start of the Guest-physical memory inside the Launcher memory.
108 * pfnlimit: The highest (Guest-physical) page number the Guest should be
109 * allowed to access. The Guest memory lives inside the Launcher, so it sets
110 * this to ensure the Guest can only reach its own memory.
112 * pgdir: The (Guest-physical) address of the top of the initial Guest
113 * pagetables (which are set up by the Launcher).
115 * start: The first instruction to execute ("eip" in x86-speak).
117 static int initialize(struct file *file, const unsigned long __user *input)
119 /* "struct lguest" contains everything we (the Host) know about a
123 unsigned long args[4];
125 /* We grab the Big Lguest lock, which protects against multiple
126 * simultaneous initializations. */
127 mutex_lock(&lguest_lock);
128 /* You can't initialize twice! Close the device and start again... */
129 if (file->private_data) {
134 if (copy_from_user(args, input, sizeof(args)) != 0) {
139 lg = kzalloc(sizeof(*lg), GFP_KERNEL);
145 /* Populate the easy fields of our "struct lguest" */
146 lg->mem_base = (void __user *)(long)args[0];
147 lg->pfn_limit = args[1];
149 /* This is the first cpu */
150 err = cpu_start(&lg->cpus[0], 0, args[3]);
154 /* We need a complete page for the Guest registers: they are accessible
155 * to the Guest and we can only grant it access to whole pages. */
156 lg->regs_page = get_zeroed_page(GFP_KERNEL);
157 if (!lg->regs_page) {
161 /* We actually put the registers at the bottom of the page. */
162 lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs);
164 /* Initialize the Guest's shadow page tables, using the toplevel
165 * address the Launcher gave us. This allocates memory, so can
167 err = init_guest_pagetable(lg, args[2]);
171 /* Now we initialize the Guest's registers, handing it the start
173 lguest_arch_setup_regs(lg, args[3]);
175 /* The timer for lguest's clock needs initialization. */
178 /* We keep a pointer to the Launcher task (ie. current task) for when
179 * other Guests want to wake this one (inter-Guest I/O). */
181 /* We need to keep a pointer to the Launcher's memory map, because if
182 * the Launcher dies we need to clean it up. If we don't keep a
183 * reference, it is destroyed before close() is called. */
184 lg->mm = get_task_mm(lg->tsk);
186 /* Initialize the queue for the waker to wait on */
187 init_waitqueue_head(&lg->break_wq);
189 /* We remember which CPU's pages this Guest used last, for optimization
190 * when the same Guest runs on the same CPU twice. */
191 lg->last_pages = NULL;
193 /* We keep our "struct lguest" in the file's private_data. */
194 file->private_data = lg;
196 mutex_unlock(&lguest_lock);
198 /* And because this is a write() call, we return the length used. */
202 free_page(lg->regs_page);
206 mutex_unlock(&lguest_lock);
210 /*L:010 The first operation the Launcher does must be a write. All writes
211 * start with an unsigned long number: for the first write this must be
212 * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
213 * writes of other values to send interrupts. */
214 static ssize_t write(struct file *file, const char __user *in,
215 size_t size, loff_t *off)
217 /* Once the guest is initialized, we hold the "struct lguest" in the
218 * file private data. */
219 struct lguest *lg = file->private_data;
220 const unsigned long __user *input = (const unsigned long __user *)in;
223 if (get_user(req, input) != 0)
227 /* If you haven't initialized, you must do that first. */
228 if (req != LHREQ_INITIALIZE && !lg)
231 /* Once the Guest is dead, all you can do is read() why it died. */
235 /* If you're not the task which owns the Guest, you can only break */
236 if (lg && current != lg->tsk && req != LHREQ_BREAK)
240 case LHREQ_INITIALIZE:
241 return initialize(file, input);
243 return user_send_irq(lg, input);
245 return break_guest_out(lg, input);
251 /*L:060 The final piece of interface code is the close() routine. It reverses
252 * everything done in initialize(). This is usually called because the
255 * Note that the close routine returns 0 or a negative error number: it can't
256 * really fail, but it can whine. I blame Sun for this wart, and K&R C for
257 * letting them do it. :*/
258 static int close(struct inode *inode, struct file *file)
260 struct lguest *lg = file->private_data;
262 /* If we never successfully initialized, there's nothing to clean up */
266 /* We need the big lock, to protect from inter-guest I/O and other
267 * Launchers initializing guests. */
268 mutex_lock(&lguest_lock);
269 /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */
270 hrtimer_cancel(&lg->hrt);
271 /* Free up the shadow page tables for the Guest. */
272 free_guest_pagetable(lg);
273 /* Now all the memory cleanups are done, it's safe to release the
274 * Launcher's memory management structure. */
276 /* If lg->dead doesn't contain an error code it will be NULL or a
277 * kmalloc()ed string, either of which is ok to hand to kfree(). */
278 if (!IS_ERR(lg->dead))
280 /* We can free up the register page we allocated. */
281 free_page(lg->regs_page);
282 /* We clear the entire structure, which also marks it as free for the
284 memset(lg, 0, sizeof(*lg));
285 /* Release lock and exit. */
286 mutex_unlock(&lguest_lock);
292 * Welcome to our journey through the Launcher!
294 * The Launcher is the Host userspace program which sets up, runs and services
295 * the Guest. In fact, many comments in the Drivers which refer to "the Host"
296 * doing things are inaccurate: the Launcher does all the device handling for
297 * the Guest, but the Guest can't know that.
299 * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we
300 * shall see more of that later.
302 * We begin our understanding with the Host kernel interface which the Launcher
303 * uses: reading and writing a character device called /dev/lguest. All the
304 * work happens in the read(), write() and close() routines: */
305 static struct file_operations lguest_fops = {
306 .owner = THIS_MODULE,
312 /* This is a textbook example of a "misc" character device. Populate a "struct
313 * miscdevice" and register it with misc_register(). */
314 static struct miscdevice lguest_dev = {
315 .minor = MISC_DYNAMIC_MINOR,
317 .fops = &lguest_fops,
320 int __init lguest_device_init(void)
322 return misc_register(&lguest_dev);
325 void __exit lguest_device_remove(void)
327 misc_deregister(&lguest_dev);