Rusty Russell | f938d2c | 2007-07-26 10:41:02 -0700 | [diff] [blame] | 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 memory size, pagetable, entry point and kernel address offset. |
| 4 | * A read will run the Guest until a signal is pending (-EINTR), or the Guest |
| 5 | * does a DMA out to the Launcher. Writes are also used to get a DMA buffer |
| 6 | * registered by the Guest and to send the Guest an interrupt. :*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 7 | #include <linux/uaccess.h> |
| 8 | #include <linux/miscdevice.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include "lg.h" |
| 11 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 12 | /*L:030 setup_regs() doesn't really belong in this file, but it gives us an |
| 13 | * early glimpse deeper into the Host so it's worth having here. |
| 14 | * |
| 15 | * Most of the Guest's registers are left alone: we used get_zeroed_page() to |
| 16 | * allocate the structure, so they will be 0. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 17 | static void setup_regs(struct lguest_regs *regs, unsigned long start) |
| 18 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 19 | /* There are four "segment" registers which the Guest needs to boot: |
| 20 | * The "code segment" register (cs) refers to the kernel code segment |
| 21 | * __KERNEL_CS, and the "data", "extra" and "stack" segment registers |
| 22 | * refer to the kernel data segment __KERNEL_DS. |
| 23 | * |
| 24 | * The privilege level is packed into the lower bits. The Guest runs |
| 25 | * at privilege level 1 (GUEST_PL).*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 26 | regs->ds = regs->es = regs->ss = __KERNEL_DS|GUEST_PL; |
| 27 | regs->cs = __KERNEL_CS|GUEST_PL; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 28 | |
| 29 | /* The "eflags" register contains miscellaneous flags. Bit 1 (0x002) |
| 30 | * is supposed to always be "1". Bit 9 (0x200) controls whether |
| 31 | * interrupts are enabled. We always leave interrupts enabled while |
| 32 | * running the Guest. */ |
| 33 | regs->eflags = 0x202; |
| 34 | |
| 35 | /* The "Extended Instruction Pointer" register says where the Guest is |
| 36 | * running. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 37 | regs->eip = start; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 38 | |
| 39 | /* %esi points to our boot information, at physical address 0, so don't |
| 40 | * touch it. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 43 | /*L:310 To send DMA into the Guest, the Launcher needs to be able to ask for a |
| 44 | * DMA buffer. This is done by writing LHREQ_GETDMA and the key to |
| 45 | * /dev/lguest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 46 | static long user_get_dma(struct lguest *lg, const u32 __user *input) |
| 47 | { |
| 48 | unsigned long key, udma, irq; |
| 49 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 50 | /* Fetch the key they wrote to us. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 51 | if (get_user(key, input) != 0) |
| 52 | return -EFAULT; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 53 | /* Look for a free Guest DMA buffer bound to that key. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 54 | udma = get_dma_buffer(lg, key, &irq); |
| 55 | if (!udma) |
| 56 | return -ENOENT; |
| 57 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 58 | /* We need to tell the Launcher what interrupt the Guest expects after |
| 59 | * the buffer is filled. We stash it in udma->used_len. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 60 | lgwrite_u32(lg, udma + offsetof(struct lguest_dma, used_len), irq); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 61 | |
| 62 | /* The (guest-physical) address of the DMA buffer is returned from |
| 63 | * the write(). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 64 | return udma; |
| 65 | } |
| 66 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 67 | /*L:315 To force the Guest to stop running and return to the Launcher, the |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 68 | * Waker sets writes LHREQ_BREAK and the value "1" to /dev/lguest. The |
| 69 | * Launcher then writes LHREQ_BREAK and "0" to release the Waker. */ |
| 70 | static int break_guest_out(struct lguest *lg, const u32 __user *input) |
| 71 | { |
| 72 | unsigned long on; |
| 73 | |
| 74 | /* Fetch whether they're turning break on or off.. */ |
| 75 | if (get_user(on, input) != 0) |
| 76 | return -EFAULT; |
| 77 | |
| 78 | if (on) { |
| 79 | lg->break_out = 1; |
| 80 | /* Pop it out (may be running on different CPU) */ |
| 81 | wake_up_process(lg->tsk); |
| 82 | /* Wait for them to reset it */ |
| 83 | return wait_event_interruptible(lg->break_wq, !lg->break_out); |
| 84 | } else { |
| 85 | lg->break_out = 0; |
| 86 | wake_up(&lg->break_wq); |
| 87 | return 0; |
| 88 | } |
| 89 | } |
| 90 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 91 | /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
| 92 | * number to /dev/lguest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 93 | static int user_send_irq(struct lguest *lg, const u32 __user *input) |
| 94 | { |
| 95 | u32 irq; |
| 96 | |
| 97 | if (get_user(irq, input) != 0) |
| 98 | return -EFAULT; |
| 99 | if (irq >= LGUEST_IRQS) |
| 100 | return -EINVAL; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 101 | /* Next time the Guest runs, the core code will see if it can deliver |
| 102 | * this interrupt. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 103 | set_bit(irq, lg->irqs_pending); |
| 104 | return 0; |
| 105 | } |
| 106 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 107 | /*L:040 Once our Guest is initialized, the Launcher makes it run by reading |
| 108 | * from /dev/lguest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 109 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
| 110 | { |
| 111 | struct lguest *lg = file->private_data; |
| 112 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 113 | /* You must write LHREQ_INITIALIZE first! */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 114 | if (!lg) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | /* If you're not the task which owns the guest, go away. */ |
| 118 | if (current != lg->tsk) |
| 119 | return -EPERM; |
| 120 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 121 | /* If the guest is already dead, we indicate why */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 122 | if (lg->dead) { |
| 123 | size_t len; |
| 124 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 125 | /* lg->dead either contains an error code, or a string. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 126 | if (IS_ERR(lg->dead)) |
| 127 | return PTR_ERR(lg->dead); |
| 128 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 129 | /* We can only return as much as the buffer they read with. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 130 | len = min(size, strlen(lg->dead)+1); |
| 131 | if (copy_to_user(user, lg->dead, len) != 0) |
| 132 | return -EFAULT; |
| 133 | return len; |
| 134 | } |
| 135 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 136 | /* If we returned from read() last time because the Guest sent DMA, |
| 137 | * clear the flag. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 138 | if (lg->dma_is_pending) |
| 139 | lg->dma_is_pending = 0; |
| 140 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 141 | /* Run the Guest until something interesting happens. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 142 | return run_guest(lg, (unsigned long __user *)user); |
| 143 | } |
| 144 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 145 | /*L:020 The initialization write supplies 4 32-bit values (in addition to the |
| 146 | * 32-bit LHREQ_INITIALIZE value). These are: |
| 147 | * |
| 148 | * pfnlimit: The highest (Guest-physical) page number the Guest should be |
| 149 | * allowed to access. The Launcher has to live in Guest memory, so it sets |
| 150 | * this to ensure the Guest can't reach it. |
| 151 | * |
| 152 | * pgdir: The (Guest-physical) address of the top of the initial Guest |
| 153 | * pagetables (which are set up by the Launcher). |
| 154 | * |
| 155 | * start: The first instruction to execute ("eip" in x86-speak). |
| 156 | * |
| 157 | * page_offset: The PAGE_OFFSET constant in the Guest kernel. We should |
| 158 | * probably wean the code off this, but it's a very useful constant! Any |
| 159 | * address above this is within the Guest kernel, and any kernel address can |
| 160 | * quickly converted from physical to virtual by adding PAGE_OFFSET. It's |
| 161 | * 0xC0000000 (3G) by default, but it's configurable at kernel build time. |
| 162 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 163 | static int initialize(struct file *file, const u32 __user *input) |
| 164 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 165 | /* "struct lguest" contains everything we (the Host) know about a |
| 166 | * Guest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 167 | struct lguest *lg; |
| 168 | int err, i; |
| 169 | u32 args[4]; |
| 170 | |
| 171 | /* We grab the Big Lguest lock, which protects the global array |
| 172 | * "lguests" and multiple simultaneous initializations. */ |
| 173 | mutex_lock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 174 | /* You can't initialize twice! Close the device and start again... */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 175 | if (file->private_data) { |
| 176 | err = -EBUSY; |
| 177 | goto unlock; |
| 178 | } |
| 179 | |
| 180 | if (copy_from_user(args, input, sizeof(args)) != 0) { |
| 181 | err = -EFAULT; |
| 182 | goto unlock; |
| 183 | } |
| 184 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 185 | /* Find an unused guest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 186 | i = find_free_guest(); |
| 187 | if (i < 0) { |
| 188 | err = -ENOSPC; |
| 189 | goto unlock; |
| 190 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 191 | /* OK, we have an index into the "lguest" array: "lg" is a convenient |
| 192 | * pointer. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 193 | lg = &lguests[i]; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 194 | |
| 195 | /* Populate the easy fields of our "struct lguest" */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 196 | lg->guestid = i; |
| 197 | lg->pfn_limit = args[0]; |
| 198 | lg->page_offset = args[3]; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 199 | |
| 200 | /* We need a complete page for the Guest registers: they are accessible |
| 201 | * to the Guest and we can only grant it access to whole pages. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 202 | lg->regs_page = get_zeroed_page(GFP_KERNEL); |
| 203 | if (!lg->regs_page) { |
| 204 | err = -ENOMEM; |
| 205 | goto release_guest; |
| 206 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 207 | /* We actually put the registers at the bottom of the page. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 208 | lg->regs = (void *)lg->regs_page + PAGE_SIZE - sizeof(*lg->regs); |
| 209 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 210 | /* Initialize the Guest's shadow page tables, using the toplevel |
| 211 | * address the Launcher gave us. This allocates memory, so can |
| 212 | * fail. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 213 | err = init_guest_pagetable(lg, args[1]); |
| 214 | if (err) |
| 215 | goto free_regs; |
| 216 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 217 | /* Now we initialize the Guest's registers, handing it the start |
| 218 | * address. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 219 | setup_regs(lg->regs, args[2]); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 220 | |
| 221 | /* There are a couple of GDT entries the Guest expects when first |
| 222 | * booting. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 223 | setup_guest_gdt(lg); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 224 | |
| 225 | /* The timer for lguest's clock needs initialization. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 226 | init_clockdev(lg); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 227 | |
| 228 | /* We keep a pointer to the Launcher task (ie. current task) for when |
| 229 | * other Guests want to wake this one (inter-Guest I/O). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 230 | lg->tsk = current; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 231 | /* We need to keep a pointer to the Launcher's memory map, because if |
| 232 | * the Launcher dies we need to clean it up. If we don't keep a |
| 233 | * reference, it is destroyed before close() is called. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 234 | lg->mm = get_task_mm(lg->tsk); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 235 | |
| 236 | /* Initialize the queue for the waker to wait on */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 237 | init_waitqueue_head(&lg->break_wq); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 238 | |
| 239 | /* We remember which CPU's pages this Guest used last, for optimization |
| 240 | * when the same Guest runs on the same CPU twice. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 241 | lg->last_pages = NULL; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 242 | |
| 243 | /* We keep our "struct lguest" in the file's private_data. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 244 | file->private_data = lg; |
| 245 | |
| 246 | mutex_unlock(&lguest_lock); |
| 247 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 248 | /* And because this is a write() call, we return the length used. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 249 | return sizeof(args); |
| 250 | |
| 251 | free_regs: |
| 252 | free_page(lg->regs_page); |
| 253 | release_guest: |
| 254 | memset(lg, 0, sizeof(*lg)); |
| 255 | unlock: |
| 256 | mutex_unlock(&lguest_lock); |
| 257 | return err; |
| 258 | } |
| 259 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 260 | /*L:010 The first operation the Launcher does must be a write. All writes |
| 261 | * start with a 32 bit number: for the first write this must be |
| 262 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
| 263 | * writes of other values to get DMA buffers and send interrupts. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 264 | static ssize_t write(struct file *file, const char __user *input, |
| 265 | size_t size, loff_t *off) |
| 266 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 267 | /* Once the guest is initialized, we hold the "struct lguest" in the |
| 268 | * file private data. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 269 | struct lguest *lg = file->private_data; |
| 270 | u32 req; |
| 271 | |
| 272 | if (get_user(req, input) != 0) |
| 273 | return -EFAULT; |
| 274 | input += sizeof(req); |
| 275 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 276 | /* If you haven't initialized, you must do that first. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 277 | if (req != LHREQ_INITIALIZE && !lg) |
| 278 | return -EINVAL; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 279 | |
| 280 | /* Once the Guest is dead, all you can do is read() why it died. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 281 | if (lg && lg->dead) |
| 282 | return -ENOENT; |
| 283 | |
| 284 | /* If you're not the task which owns the Guest, you can only break */ |
| 285 | if (lg && current != lg->tsk && req != LHREQ_BREAK) |
| 286 | return -EPERM; |
| 287 | |
| 288 | switch (req) { |
| 289 | case LHREQ_INITIALIZE: |
| 290 | return initialize(file, (const u32 __user *)input); |
| 291 | case LHREQ_GETDMA: |
| 292 | return user_get_dma(lg, (const u32 __user *)input); |
| 293 | case LHREQ_IRQ: |
| 294 | return user_send_irq(lg, (const u32 __user *)input); |
| 295 | case LHREQ_BREAK: |
| 296 | return break_guest_out(lg, (const u32 __user *)input); |
| 297 | default: |
| 298 | return -EINVAL; |
| 299 | } |
| 300 | } |
| 301 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 302 | /*L:060 The final piece of interface code is the close() routine. It reverses |
| 303 | * everything done in initialize(). This is usually called because the |
| 304 | * Launcher exited. |
| 305 | * |
| 306 | * Note that the close routine returns 0 or a negative error number: it can't |
| 307 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for |
| 308 | * letting them do it. :*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 309 | static int close(struct inode *inode, struct file *file) |
| 310 | { |
| 311 | struct lguest *lg = file->private_data; |
| 312 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 313 | /* If we never successfully initialized, there's nothing to clean up */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 314 | if (!lg) |
| 315 | return 0; |
| 316 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 317 | /* We need the big lock, to protect from inter-guest I/O and other |
| 318 | * Launchers initializing guests. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 319 | mutex_lock(&lguest_lock); |
| 320 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ |
| 321 | hrtimer_cancel(&lg->hrt); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 322 | /* Free any DMA buffers the Guest had bound. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 323 | release_all_dma(lg); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 324 | /* Free up the shadow page tables for the Guest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 325 | free_guest_pagetable(lg); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 326 | /* Now all the memory cleanups are done, it's safe to release the |
| 327 | * Launcher's memory management structure. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 328 | mmput(lg->mm); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 329 | /* If lg->dead doesn't contain an error code it will be NULL or a |
| 330 | * kmalloc()ed string, either of which is ok to hand to kfree(). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 331 | if (!IS_ERR(lg->dead)) |
| 332 | kfree(lg->dead); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 333 | /* We can free up the register page we allocated. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 334 | free_page(lg->regs_page); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 335 | /* We clear the entire structure, which also marks it as free for the |
| 336 | * next user. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 337 | memset(lg, 0, sizeof(*lg)); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 338 | /* Release lock and exit. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 339 | mutex_unlock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 340 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 344 | /*L:000 |
| 345 | * Welcome to our journey through the Launcher! |
| 346 | * |
| 347 | * The Launcher is the Host userspace program which sets up, runs and services |
| 348 | * the Guest. In fact, many comments in the Drivers which refer to "the Host" |
| 349 | * doing things are inaccurate: the Launcher does all the device handling for |
| 350 | * the Guest. The Guest can't tell what's done by the the Launcher and what by |
| 351 | * the Host. |
| 352 | * |
| 353 | * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we |
| 354 | * shall see more of that later. |
| 355 | * |
| 356 | * We begin our understanding with the Host kernel interface which the Launcher |
| 357 | * uses: reading and writing a character device called /dev/lguest. All the |
| 358 | * work happens in the read(), write() and close() routines: */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 359 | static struct file_operations lguest_fops = { |
| 360 | .owner = THIS_MODULE, |
| 361 | .release = close, |
| 362 | .write = write, |
| 363 | .read = read, |
| 364 | }; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 365 | |
| 366 | /* This is a textbook example of a "misc" character device. Populate a "struct |
| 367 | * miscdevice" and register it with misc_register(). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 368 | static struct miscdevice lguest_dev = { |
| 369 | .minor = MISC_DYNAMIC_MINOR, |
| 370 | .name = "lguest", |
| 371 | .fops = &lguest_fops, |
| 372 | }; |
| 373 | |
| 374 | int __init lguest_device_init(void) |
| 375 | { |
| 376 | return misc_register(&lguest_dev); |
| 377 | } |
| 378 | |
| 379 | void __exit lguest_device_remove(void) |
| 380 | { |
| 381 | misc_deregister(&lguest_dev); |
| 382 | } |