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