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> |
Glauber de Oliveira Costa | ca94f2b | 2008-01-18 23:59:07 -0200 | [diff] [blame^] | 9 | #include <linux/sched.h> |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 10 | #include "lg.h" |
| 11 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 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. */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 17 | static int break_guest_out(struct lg_cpu *cpu, const unsigned long __user*input) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 18 | { |
| 19 | unsigned long on; |
| 20 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 21 | /* Fetch whether they're turning break on or off. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 22 | if (get_user(on, input) != 0) |
| 23 | return -EFAULT; |
| 24 | |
| 25 | if (on) { |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 26 | cpu->break_out = 1; |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 27 | /* Pop it out of the Guest (may be running on different CPU) */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 28 | wake_up_process(cpu->tsk); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 29 | /* Wait for them to reset it */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 30 | return wait_event_interruptible(cpu->break_wq, !cpu->break_out); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 31 | } else { |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 32 | cpu->break_out = 0; |
| 33 | wake_up(&cpu->break_wq); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | } |
| 37 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 38 | /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
| 39 | * number to /dev/lguest. */ |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 40 | static int user_send_irq(struct lg_cpu *cpu, const unsigned long __user *input) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 41 | { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 42 | unsigned long irq; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 43 | |
| 44 | if (get_user(irq, input) != 0) |
| 45 | return -EFAULT; |
| 46 | if (irq >= LGUEST_IRQS) |
| 47 | return -EINVAL; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 48 | /* Next time the Guest runs, the core code will see if it can deliver |
| 49 | * this interrupt. */ |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 50 | set_bit(irq, cpu->irqs_pending); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 54 | /*L:040 Once our Guest is initialized, the Launcher makes it run by reading |
| 55 | * from /dev/lguest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 56 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
| 57 | { |
| 58 | struct lguest *lg = file->private_data; |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 59 | struct lg_cpu *cpu; |
| 60 | unsigned int cpu_id = *o; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 61 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 62 | /* You must write LHREQ_INITIALIZE first! */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 63 | if (!lg) |
| 64 | return -EINVAL; |
| 65 | |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 66 | /* Watch out for arbitrary vcpu indexes! */ |
| 67 | if (cpu_id >= lg->nr_cpus) |
| 68 | return -EINVAL; |
| 69 | |
| 70 | cpu = &lg->cpus[cpu_id]; |
| 71 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 72 | /* If you're not the task which owns the Guest, go away. */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 73 | if (current != cpu->tsk) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 74 | return -EPERM; |
| 75 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 76 | /* If the guest is already dead, we indicate why */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 77 | if (lg->dead) { |
| 78 | size_t len; |
| 79 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 80 | /* lg->dead either contains an error code, or a string. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 81 | if (IS_ERR(lg->dead)) |
| 82 | return PTR_ERR(lg->dead); |
| 83 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 84 | /* We can only return as much as the buffer they read with. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 85 | len = min(size, strlen(lg->dead)+1); |
| 86 | if (copy_to_user(user, lg->dead, len) != 0) |
| 87 | return -EFAULT; |
| 88 | return len; |
| 89 | } |
| 90 | |
Rusty Russell | 1504527 | 2007-10-22 11:24:10 +1000 | [diff] [blame] | 91 | /* If we returned from read() last time because the Guest notified, |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 92 | * clear the flag. */ |
Glauber de Oliveira Costa | 5e232f4 | 2008-01-07 11:05:36 -0200 | [diff] [blame] | 93 | if (cpu->pending_notify) |
| 94 | cpu->pending_notify = 0; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 95 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 96 | /* Run the Guest until something interesting happens. */ |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 97 | return run_guest(cpu, (unsigned long __user *)user); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 100 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
| 101 | { |
| 102 | if (id >= NR_CPUS) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | cpu->id = id; |
| 106 | cpu->lg = container_of((cpu - id), struct lguest, cpus[0]); |
| 107 | cpu->lg->nr_cpus++; |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 108 | init_clockdev(cpu); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 109 | |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 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; |
| 115 | |
| 116 | /* We actually put the registers at the bottom of the page. */ |
| 117 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); |
| 118 | |
| 119 | /* Now we initialize the Guest's registers, handing it the start |
| 120 | * address. */ |
| 121 | lguest_arch_setup_regs(cpu, start_ip); |
| 122 | |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 123 | /* Initialize the queue for the waker to wait on */ |
| 124 | init_waitqueue_head(&cpu->break_wq); |
| 125 | |
| 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; |
| 129 | |
| 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); |
| 134 | |
Glauber de Oliveira Costa | f34f8c5 | 2008-01-17 19:13:26 -0200 | [diff] [blame] | 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; |
| 138 | |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 142 | /*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] | 143 | * values (in addition to the LHREQ_INITIALIZE value). These are: |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 144 | * |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 145 | * base: The start of the Guest-physical memory inside the Launcher memory. |
| 146 | * |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 147 | * pfnlimit: The highest (Guest-physical) page number the Guest should be |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 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. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 150 | * |
| 151 | * pgdir: The (Guest-physical) address of the top of the initial Guest |
| 152 | * pagetables (which are set up by the Launcher). |
| 153 | * |
| 154 | * start: The first instruction to execute ("eip" in x86-speak). |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 155 | */ |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 156 | static int initialize(struct file *file, const unsigned long __user *input) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 157 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 158 | /* "struct lguest" contains everything we (the Host) know about a |
| 159 | * Guest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 160 | struct lguest *lg; |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 161 | int err; |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 162 | unsigned long args[4]; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 163 | |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 164 | /* We grab the Big Lguest lock, which protects against multiple |
| 165 | * simultaneous initializations. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 166 | mutex_lock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 167 | /* You can't initialize twice! Close the device and start again... */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 168 | if (file->private_data) { |
| 169 | err = -EBUSY; |
| 170 | goto unlock; |
| 171 | } |
| 172 | |
| 173 | if (copy_from_user(args, input, sizeof(args)) != 0) { |
| 174 | err = -EFAULT; |
| 175 | goto unlock; |
| 176 | } |
| 177 | |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 178 | lg = kzalloc(sizeof(*lg), GFP_KERNEL); |
| 179 | if (!lg) { |
| 180 | err = -ENOMEM; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 181 | goto unlock; |
| 182 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 183 | |
| 184 | /* Populate the easy fields of our "struct lguest" */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 185 | lg->mem_base = (void __user *)(long)args[0]; |
| 186 | lg->pfn_limit = args[1]; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 187 | |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 188 | /* This is the first cpu */ |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 189 | err = lg_cpu_start(&lg->cpus[0], 0, args[3]); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 190 | if (err) |
| 191 | goto release_guest; |
| 192 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 193 | /* Initialize the Guest's shadow page tables, using the toplevel |
| 194 | * address the Launcher gave us. This allocates memory, so can |
| 195 | * fail. */ |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 196 | err = init_guest_pagetable(lg, args[2]); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 197 | if (err) |
| 198 | goto free_regs; |
| 199 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 200 | /* We keep our "struct lguest" in the file's private_data. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 201 | file->private_data = lg; |
| 202 | |
| 203 | mutex_unlock(&lguest_lock); |
| 204 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 205 | /* And because this is a write() call, we return the length used. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 206 | return sizeof(args); |
| 207 | |
| 208 | free_regs: |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 209 | /* FIXME: This should be in free_vcpu */ |
| 210 | free_page(lg->cpus[0].regs_page); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 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; |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 230 | struct lg_cpu *uninitialized_var(cpu); |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 231 | unsigned int cpu_id = *off; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 232 | |
| 233 | if (get_user(req, input) != 0) |
| 234 | return -EFAULT; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 235 | input++; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 236 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 237 | /* If you haven't initialized, you must do that first. */ |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 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; |
| 244 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 245 | |
| 246 | /* 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] | 247 | if (lg && lg->dead) |
| 248 | return -ENOENT; |
| 249 | |
| 250 | /* If you're not the task which owns the Guest, you can only break */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 251 | if (lg && current != cpu->tsk && req != LHREQ_BREAK) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 252 | return -EPERM; |
| 253 | |
| 254 | switch (req) { |
| 255 | case LHREQ_INITIALIZE: |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 256 | return initialize(file, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 257 | case LHREQ_IRQ: |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 258 | return user_send_irq(cpu, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 259 | case LHREQ_BREAK: |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 260 | return break_guest_out(cpu, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 261 | default: |
| 262 | return -EINVAL; |
| 263 | } |
| 264 | } |
| 265 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 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. |
| 269 | * |
| 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. :*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 273 | static int close(struct inode *inode, struct file *file) |
| 274 | { |
| 275 | struct lguest *lg = file->private_data; |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 276 | unsigned int i; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 277 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 278 | /* If we never successfully initialized, there's nothing to clean up */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 279 | if (!lg) |
| 280 | return 0; |
| 281 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 282 | /* We need the big lock, to protect from inter-guest I/O and other |
| 283 | * Launchers initializing guests. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 284 | mutex_lock(&lguest_lock); |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 285 | |
| 286 | /* Free up the shadow page tables for the Guest. */ |
| 287 | free_guest_pagetable(lg); |
| 288 | |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 289 | for (i = 0; i < lg->nr_cpus; i++) { |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 290 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ |
| 291 | hrtimer_cancel(&lg->cpus[i].hrt); |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 292 | /* We can free up the register page we allocated. */ |
| 293 | free_page(lg->cpus[i].regs_page); |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 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); |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 297 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 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(). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 300 | if (!IS_ERR(lg->dead)) |
| 301 | kfree(lg->dead); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 302 | /* We clear the entire structure, which also marks it as free for the |
| 303 | * next user. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 304 | memset(lg, 0, sizeof(*lg)); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 305 | /* Release lock and exit. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 306 | mutex_unlock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 307 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 311 | /*L:000 |
| 312 | * Welcome to our journey through the Launcher! |
| 313 | * |
| 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 |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 317 | * the Guest, but the Guest can't know that. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 318 | * |
| 319 | * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we |
| 320 | * shall see more of that later. |
| 321 | * |
| 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: */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 325 | static struct file_operations lguest_fops = { |
| 326 | .owner = THIS_MODULE, |
| 327 | .release = close, |
| 328 | .write = write, |
| 329 | .read = read, |
| 330 | }; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 331 | |
| 332 | /* This is a textbook example of a "misc" character device. Populate a "struct |
| 333 | * miscdevice" and register it with misc_register(). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 334 | static struct miscdevice lguest_dev = { |
| 335 | .minor = MISC_DYNAMIC_MINOR, |
| 336 | .name = "lguest", |
| 337 | .fops = &lguest_fops, |
| 338 | }; |
| 339 | |
| 340 | int __init lguest_device_init(void) |
| 341 | { |
| 342 | return misc_register(&lguest_dev); |
| 343 | } |
| 344 | |
| 345 | void __exit lguest_device_remove(void) |
| 346 | { |
| 347 | misc_deregister(&lguest_dev); |
| 348 | } |