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