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 | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [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 | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 91 | /* If we returned from read() last time because the Guest sent I/O, |
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 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 100 | /*L:025 This actually initializes a CPU. For the moment, a Guest is only |
| 101 | * uniprocessor, so "id" is always 0. */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 102 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
| 103 | { |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 104 | /* We have a limited number the number of CPUs in the lguest struct. */ |
Rusty Russell | 24adf12 | 2008-05-02 21:50:51 -0500 | [diff] [blame] | 105 | if (id >= ARRAY_SIZE(cpu->lg->cpus)) |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 106 | return -EINVAL; |
| 107 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 108 | /* Set up this CPU's id, and pointer back to the lguest struct. */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 109 | cpu->id = id; |
| 110 | cpu->lg = container_of((cpu - id), struct lguest, cpus[0]); |
| 111 | cpu->lg->nr_cpus++; |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 112 | |
| 113 | /* Each CPU has a timer it can set. */ |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 114 | init_clockdev(cpu); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 115 | |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 116 | /* We need a complete page for the Guest registers: they are accessible |
| 117 | * to the Guest and we can only grant it access to whole pages. */ |
| 118 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); |
| 119 | if (!cpu->regs_page) |
| 120 | return -ENOMEM; |
| 121 | |
| 122 | /* We actually put the registers at the bottom of the page. */ |
| 123 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); |
| 124 | |
| 125 | /* Now we initialize the Guest's registers, handing it the start |
| 126 | * address. */ |
| 127 | lguest_arch_setup_regs(cpu, start_ip); |
| 128 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 129 | /* Initialize the queue for the Waker to wait on */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 130 | init_waitqueue_head(&cpu->break_wq); |
| 131 | |
| 132 | /* We keep a pointer to the Launcher task (ie. current task) for when |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 133 | * other Guests want to wake this one (eg. console input). */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 134 | cpu->tsk = current; |
| 135 | |
| 136 | /* We need to keep a pointer to the Launcher's memory map, because if |
| 137 | * the Launcher dies we need to clean it up. If we don't keep a |
| 138 | * reference, it is destroyed before close() is called. */ |
| 139 | cpu->mm = get_task_mm(cpu->tsk); |
| 140 | |
Glauber de Oliveira Costa | f34f8c5 | 2008-01-17 19:13:26 -0200 | [diff] [blame] | 141 | /* We remember which CPU's pages this Guest used last, for optimization |
| 142 | * when the same Guest runs on the same CPU twice. */ |
| 143 | cpu->last_pages = NULL; |
| 144 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 145 | /* No error == success. */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 149 | /*L:020 The initialization write supplies 3 pointer sized (32 or 64 bit) |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 150 | * values (in addition to the LHREQ_INITIALIZE value). These are: |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 151 | * |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 152 | * base: The start of the Guest-physical memory inside the Launcher memory. |
| 153 | * |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 154 | * pfnlimit: The highest (Guest-physical) page number the Guest should be |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 155 | * allowed to access. The Guest memory lives inside the Launcher, so it sets |
| 156 | * this to ensure the Guest can only reach its own memory. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 157 | * |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 158 | * start: The first instruction to execute ("eip" in x86-speak). |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 159 | */ |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 160 | static int initialize(struct file *file, const unsigned long __user *input) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 161 | { |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 162 | /* "struct lguest" contains everything we (the Host) know about a |
| 163 | * Guest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 164 | struct lguest *lg; |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 165 | int err; |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 166 | unsigned long args[3]; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 167 | |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 168 | /* We grab the Big Lguest lock, which protects against multiple |
| 169 | * simultaneous initializations. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 170 | mutex_lock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 171 | /* You can't initialize twice! Close the device and start again... */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 172 | if (file->private_data) { |
| 173 | err = -EBUSY; |
| 174 | goto unlock; |
| 175 | } |
| 176 | |
| 177 | if (copy_from_user(args, input, sizeof(args)) != 0) { |
| 178 | err = -EFAULT; |
| 179 | goto unlock; |
| 180 | } |
| 181 | |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 182 | lg = kzalloc(sizeof(*lg), GFP_KERNEL); |
| 183 | if (!lg) { |
| 184 | err = -ENOMEM; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 185 | goto unlock; |
| 186 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 187 | |
| 188 | /* Populate the easy fields of our "struct lguest" */ |
Al Viro | 74dbf71 | 2008-03-29 03:08:28 +0000 | [diff] [blame] | 189 | lg->mem_base = (void __user *)args[0]; |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 190 | lg->pfn_limit = args[1]; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 191 | |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 192 | /* This is the first cpu (cpu 0) and it will start booting at args[2] */ |
| 193 | err = lg_cpu_start(&lg->cpus[0], 0, args[2]); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 194 | if (err) |
| 195 | goto release_guest; |
| 196 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 197 | /* Initialize the Guest's shadow page tables, using the toplevel |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 198 | * address the Launcher gave us. This allocates memory, so can fail. */ |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 199 | err = init_guest_pagetable(lg); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 200 | if (err) |
| 201 | goto free_regs; |
| 202 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 203 | /* We keep our "struct lguest" in the file's private_data. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 204 | file->private_data = lg; |
| 205 | |
| 206 | mutex_unlock(&lguest_lock); |
| 207 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 208 | /* And because this is a write() call, we return the length used. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 209 | return sizeof(args); |
| 210 | |
| 211 | free_regs: |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 212 | /* FIXME: This should be in free_vcpu */ |
| 213 | free_page(lg->cpus[0].regs_page); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 214 | release_guest: |
Adrian Bunk | 4305441 | 2007-11-14 16:59:00 -0800 | [diff] [blame] | 215 | kfree(lg); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 216 | unlock: |
| 217 | mutex_unlock(&lguest_lock); |
| 218 | return err; |
| 219 | } |
| 220 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 221 | /*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] | 222 | * 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] | 223 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 224 | * writes of other values to send interrupts. |
| 225 | * |
| 226 | * Note that we overload the "offset" in the /dev/lguest file to indicate what |
| 227 | * CPU number we're dealing with. Currently this is always 0, since we only |
| 228 | * support uniprocessor Guests, but you can see the beginnings of SMP support |
| 229 | * here. */ |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 230 | static ssize_t write(struct file *file, const char __user *in, |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 231 | size_t size, loff_t *off) |
| 232 | { |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 233 | /* Once the Guest is initialized, we hold the "struct lguest" in the |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 234 | * file private data. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 235 | struct lguest *lg = file->private_data; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 236 | const unsigned long __user *input = (const unsigned long __user *)in; |
| 237 | unsigned long req; |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 238 | struct lg_cpu *uninitialized_var(cpu); |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 239 | unsigned int cpu_id = *off; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 240 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 241 | /* The first value tells us what this request is. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 242 | if (get_user(req, input) != 0) |
| 243 | return -EFAULT; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 244 | input++; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 245 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 246 | /* If you haven't initialized, you must do that first. */ |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 247 | if (req != LHREQ_INITIALIZE) { |
| 248 | if (!lg || (cpu_id >= lg->nr_cpus)) |
| 249 | return -EINVAL; |
| 250 | cpu = &lg->cpus[cpu_id]; |
Eugene Teo | f73d1e6 | 2008-02-09 23:53:17 +0800 | [diff] [blame] | 251 | |
| 252 | /* Once the Guest is dead, you can only read() why it died. */ |
| 253 | if (lg->dead) |
| 254 | return -ENOENT; |
| 255 | |
| 256 | /* If you're not the task which owns the Guest, all you can do |
| 257 | * is break the Launcher out of running the Guest. */ |
| 258 | if (current != cpu->tsk && req != LHREQ_BREAK) |
| 259 | return -EPERM; |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 260 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 261 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 262 | switch (req) { |
| 263 | case LHREQ_INITIALIZE: |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 264 | return initialize(file, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 265 | case LHREQ_IRQ: |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 266 | return user_send_irq(cpu, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 267 | case LHREQ_BREAK: |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 268 | return break_guest_out(cpu, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 269 | default: |
| 270 | return -EINVAL; |
| 271 | } |
| 272 | } |
| 273 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 274 | /*L:060 The final piece of interface code is the close() routine. It reverses |
| 275 | * everything done in initialize(). This is usually called because the |
| 276 | * Launcher exited. |
| 277 | * |
| 278 | * Note that the close routine returns 0 or a negative error number: it can't |
| 279 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for |
| 280 | * letting them do it. :*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 281 | static int close(struct inode *inode, struct file *file) |
| 282 | { |
| 283 | struct lguest *lg = file->private_data; |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 284 | unsigned int i; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 285 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 286 | /* If we never successfully initialized, there's nothing to clean up */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 287 | if (!lg) |
| 288 | return 0; |
| 289 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 290 | /* We need the big lock, to protect from inter-guest I/O and other |
| 291 | * Launchers initializing guests. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 292 | mutex_lock(&lguest_lock); |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 293 | |
| 294 | /* Free up the shadow page tables for the Guest. */ |
| 295 | free_guest_pagetable(lg); |
| 296 | |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 297 | for (i = 0; i < lg->nr_cpus; i++) { |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 298 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ |
| 299 | hrtimer_cancel(&lg->cpus[i].hrt); |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 300 | /* We can free up the register page we allocated. */ |
| 301 | free_page(lg->cpus[i].regs_page); |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 302 | /* Now all the memory cleanups are done, it's safe to release |
| 303 | * the Launcher's memory management structure. */ |
| 304 | mmput(lg->cpus[i].mm); |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 305 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 306 | /* If lg->dead doesn't contain an error code it will be NULL or a |
| 307 | * kmalloc()ed string, either of which is ok to hand to kfree(). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 308 | if (!IS_ERR(lg->dead)) |
| 309 | kfree(lg->dead); |
Mark Wallis | 05dfdbb | 2009-01-26 17:32:35 +1100 | [diff] [blame] | 310 | /* Free the memory allocated to the lguest_struct */ |
| 311 | kfree(lg); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 312 | /* Release lock and exit. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 313 | mutex_unlock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 314 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 318 | /*L:000 |
| 319 | * Welcome to our journey through the Launcher! |
| 320 | * |
| 321 | * The Launcher is the Host userspace program which sets up, runs and services |
| 322 | * the Guest. In fact, many comments in the Drivers which refer to "the Host" |
| 323 | * doing things are inaccurate: the Launcher does all the device handling for |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 324 | * the Guest, but the Guest can't know that. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 325 | * |
| 326 | * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we |
| 327 | * shall see more of that later. |
| 328 | * |
| 329 | * We begin our understanding with the Host kernel interface which the Launcher |
| 330 | * uses: reading and writing a character device called /dev/lguest. All the |
| 331 | * work happens in the read(), write() and close() routines: */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 332 | static struct file_operations lguest_fops = { |
| 333 | .owner = THIS_MODULE, |
| 334 | .release = close, |
| 335 | .write = write, |
| 336 | .read = read, |
| 337 | }; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 338 | |
| 339 | /* This is a textbook example of a "misc" character device. Populate a "struct |
| 340 | * miscdevice" and register it with misc_register(). */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 341 | static struct miscdevice lguest_dev = { |
| 342 | .minor = MISC_DYNAMIC_MINOR, |
| 343 | .name = "lguest", |
| 344 | .fops = &lguest_fops, |
| 345 | }; |
| 346 | |
| 347 | int __init lguest_device_init(void) |
| 348 | { |
| 349 | return misc_register(&lguest_dev); |
| 350 | } |
| 351 | |
| 352 | void __exit lguest_device_remove(void) |
| 353 | { |
| 354 | misc_deregister(&lguest_dev); |
| 355 | } |