Rusty Russell | 9f54288 | 2011-07-22 14:39:50 +0930 | [diff] [blame] | 1 | /*P:200 This contains all the /dev/lguest code, whereby the userspace |
| 2 | * launcher controls and communicates with the Guest. For example, |
| 3 | * the first write will tell us the Guest's memory layout and entry |
| 4 | * point. A read will run the Guest until something happens, such as |
Rusty Russell | d9bab50 | 2015-02-11 15:28:01 +1030 | [diff] [blame] | 5 | * a signal or the Guest accessing a device. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 6 | :*/ |
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> |
Glauber de Oliveira Costa | ca94f2b | 2008-01-18 23:59:07 -0200 | [diff] [blame] | 10 | #include <linux/sched.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 11 | #include <linux/sched/mm.h> |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 12 | #include <linux/file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Paul Gortmaker | 39a0e33 | 2011-07-21 13:03:20 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 15 | #include "lg.h" |
| 16 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 17 | /*L:052 |
Rusty Russell | d9bab50 | 2015-02-11 15:28:01 +1030 | [diff] [blame] | 18 | The Launcher can get the registers, and also set some of them. |
| 19 | */ |
Rusty Russell | 18c1373 | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 20 | static int getreg_setup(struct lg_cpu *cpu, const unsigned long __user *input) |
| 21 | { |
| 22 | unsigned long which; |
| 23 | |
| 24 | /* We re-use the ptrace structure to specify which register to read. */ |
| 25 | if (get_user(which, input) != 0) |
| 26 | return -EFAULT; |
| 27 | |
| 28 | /* |
| 29 | * We set up the cpu register pointer, and their next read will |
| 30 | * actually get the value (instead of running the guest). |
| 31 | * |
| 32 | * The last argument 'true' says we can access any register. |
| 33 | */ |
| 34 | cpu->reg_read = lguest_arch_regptr(cpu, which, true); |
| 35 | if (!cpu->reg_read) |
| 36 | return -ENOENT; |
| 37 | |
| 38 | /* And because this is a write() call, we return the length used. */ |
| 39 | return sizeof(unsigned long) * 2; |
| 40 | } |
| 41 | |
| 42 | static int setreg(struct lg_cpu *cpu, const unsigned long __user *input) |
| 43 | { |
| 44 | unsigned long which, value, *reg; |
| 45 | |
| 46 | /* We re-use the ptrace structure to specify which register to read. */ |
| 47 | if (get_user(which, input) != 0) |
| 48 | return -EFAULT; |
| 49 | input++; |
| 50 | if (get_user(value, input) != 0) |
| 51 | return -EFAULT; |
| 52 | |
| 53 | /* The last argument 'false' means we can't access all registers. */ |
| 54 | reg = lguest_arch_regptr(cpu, which, false); |
| 55 | if (!reg) |
| 56 | return -ENOENT; |
| 57 | |
| 58 | *reg = value; |
| 59 | |
| 60 | /* And because this is a write() call, we return the length used. */ |
| 61 | return sizeof(unsigned long) * 3; |
| 62 | } |
| 63 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 64 | /*L:050 |
| 65 | * Sending an interrupt is done by writing LHREQ_IRQ and an interrupt |
| 66 | * number to /dev/lguest. |
| 67 | */ |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 68 | 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] | 69 | { |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 70 | unsigned long irq; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 71 | |
| 72 | if (get_user(irq, input) != 0) |
| 73 | return -EFAULT; |
| 74 | if (irq >= LGUEST_IRQS) |
| 75 | return -EINVAL; |
Rusty Russell | 9f155a9 | 2009-06-12 22:27:08 -0600 | [diff] [blame] | 76 | |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 77 | /* |
| 78 | * Next time the Guest runs, the core code will see if it can deliver |
| 79 | * this interrupt. |
| 80 | */ |
Rusty Russell | 9f155a9 | 2009-06-12 22:27:08 -0600 | [diff] [blame] | 81 | set_interrupt(cpu, irq); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
Rusty Russell | 8ed3130 | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 85 | /*L:053 |
| 86 | * Deliver a trap: this is used by the Launcher if it can't emulate |
| 87 | * an instruction. |
| 88 | */ |
| 89 | static int trap(struct lg_cpu *cpu, const unsigned long __user *input) |
| 90 | { |
| 91 | unsigned long trapnum; |
| 92 | |
| 93 | if (get_user(trapnum, input) != 0) |
| 94 | return -EFAULT; |
| 95 | |
| 96 | if (!deliver_trap(cpu, trapnum)) |
| 97 | return -EINVAL; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 102 | /*L:040 |
| 103 | * Once our Guest is initialized, the Launcher makes it run by reading |
| 104 | * from /dev/lguest. |
| 105 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 106 | static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o) |
| 107 | { |
| 108 | struct lguest *lg = file->private_data; |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 109 | struct lg_cpu *cpu; |
| 110 | unsigned int cpu_id = *o; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 111 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 112 | /* You must write LHREQ_INITIALIZE first! */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 113 | if (!lg) |
| 114 | return -EINVAL; |
| 115 | |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 116 | /* Watch out for arbitrary vcpu indexes! */ |
| 117 | if (cpu_id >= lg->nr_cpus) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | cpu = &lg->cpus[cpu_id]; |
| 121 | |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 122 | /* 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] | 123 | if (current != cpu->tsk) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 124 | return -EPERM; |
| 125 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 126 | /* If the Guest is already dead, we indicate why */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 127 | if (lg->dead) { |
| 128 | size_t len; |
| 129 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 130 | /* lg->dead either contains an error code, or a string. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 131 | if (IS_ERR(lg->dead)) |
| 132 | return PTR_ERR(lg->dead); |
| 133 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 134 | /* We can only return as much as the buffer they read with. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 135 | len = min(size, strlen(lg->dead)+1); |
| 136 | if (copy_to_user(user, lg->dead, len) != 0) |
| 137 | return -EFAULT; |
| 138 | return len; |
| 139 | } |
| 140 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 141 | /* |
| 142 | * If we returned from read() last time because the Guest sent I/O, |
| 143 | * clear the flag. |
| 144 | */ |
Rusty Russell | 69a09dc | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 145 | if (cpu->pending.trap) |
| 146 | cpu->pending.trap = 0; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 147 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 148 | /* Run the Guest until something interesting happens. */ |
Glauber de Oliveira Costa | d0953d4 | 2008-01-07 11:05:25 -0200 | [diff] [blame] | 149 | return run_guest(cpu, (unsigned long __user *)user); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 152 | /*L:025 |
| 153 | * This actually initializes a CPU. For the moment, a Guest is only |
| 154 | * uniprocessor, so "id" is always 0. |
| 155 | */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 156 | static int lg_cpu_start(struct lg_cpu *cpu, unsigned id, unsigned long start_ip) |
| 157 | { |
Cosmin Paraschiv | c2ecd51 | 2013-04-30 09:23:09 +0930 | [diff] [blame] | 158 | /* We have a limited number of CPUs in the lguest struct. */ |
Rusty Russell | 24adf12 | 2008-05-02 21:50:51 -0500 | [diff] [blame] | 159 | if (id >= ARRAY_SIZE(cpu->lg->cpus)) |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 160 | return -EINVAL; |
| 161 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 162 | /* 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] | 163 | cpu->id = id; |
Cosmin Paraschiv | c2ecd51 | 2013-04-30 09:23:09 +0930 | [diff] [blame] | 164 | cpu->lg = container_of(cpu, struct lguest, cpus[id]); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 165 | cpu->lg->nr_cpus++; |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 166 | |
| 167 | /* Each CPU has a timer it can set. */ |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 168 | init_clockdev(cpu); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 169 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 170 | /* |
| 171 | * We need a complete page for the Guest registers: they are accessible |
| 172 | * to the Guest and we can only grant it access to whole pages. |
| 173 | */ |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 174 | cpu->regs_page = get_zeroed_page(GFP_KERNEL); |
| 175 | if (!cpu->regs_page) |
| 176 | return -ENOMEM; |
| 177 | |
Cosmin Paraschiv | c2ecd51 | 2013-04-30 09:23:09 +0930 | [diff] [blame] | 178 | /* We actually put the registers at the end of the page. */ |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 179 | cpu->regs = (void *)cpu->regs_page + PAGE_SIZE - sizeof(*cpu->regs); |
| 180 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 181 | /* |
| 182 | * Now we initialize the Guest's registers, handing it the start |
| 183 | * address. |
| 184 | */ |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 185 | lguest_arch_setup_regs(cpu, start_ip); |
| 186 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 187 | /* |
| 188 | * We keep a pointer to the Launcher task (ie. current task) for when |
| 189 | * other Guests want to wake this one (eg. console input). |
| 190 | */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 191 | cpu->tsk = current; |
| 192 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 193 | /* |
| 194 | * We need to keep a pointer to the Launcher's memory map, because if |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 195 | * the Launcher dies we need to clean it up. If we don't keep a |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 196 | * reference, it is destroyed before close() is called. |
| 197 | */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 198 | cpu->mm = get_task_mm(cpu->tsk); |
| 199 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 200 | /* |
| 201 | * We remember which CPU's pages this Guest used last, for optimization |
| 202 | * when the same Guest runs on the same CPU twice. |
| 203 | */ |
Glauber de Oliveira Costa | f34f8c5 | 2008-01-17 19:13:26 -0200 | [diff] [blame] | 204 | cpu->last_pages = NULL; |
| 205 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 206 | /* No error == success. */ |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 210 | /*L:020 |
| 211 | * The initialization write supplies 3 pointer sized (32 or 64 bit) values (in |
| 212 | * addition to the LHREQ_INITIALIZE value). These are: |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 213 | * |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 214 | * base: The start of the Guest-physical memory inside the Launcher memory. |
| 215 | * |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 216 | * pfnlimit: The highest (Guest-physical) page number the Guest should be |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 217 | * allowed to access. The Guest memory lives inside the Launcher, so it sets |
| 218 | * this to ensure the Guest can only reach its own memory. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 219 | * |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 220 | * start: The first instruction to execute ("eip" in x86-speak). |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 221 | */ |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 222 | static int initialize(struct file *file, const unsigned long __user *input) |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 223 | { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 224 | /* "struct lguest" contains all we (the Host) know about a Guest. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 225 | struct lguest *lg; |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 226 | int err; |
Rusty Russell | 7313d52 | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 227 | unsigned long args[4]; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 228 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 229 | /* |
| 230 | * We grab the Big Lguest lock, which protects against multiple |
| 231 | * simultaneous initializations. |
| 232 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 233 | mutex_lock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 234 | /* You can't initialize twice! Close the device and start again... */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 235 | if (file->private_data) { |
| 236 | err = -EBUSY; |
| 237 | goto unlock; |
| 238 | } |
| 239 | |
| 240 | if (copy_from_user(args, input, sizeof(args)) != 0) { |
| 241 | err = -EFAULT; |
| 242 | goto unlock; |
| 243 | } |
| 244 | |
Rusty Russell | 48245cc | 2007-10-22 11:03:27 +1000 | [diff] [blame] | 245 | lg = kzalloc(sizeof(*lg), GFP_KERNEL); |
| 246 | if (!lg) { |
| 247 | err = -ENOMEM; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 248 | goto unlock; |
| 249 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 250 | |
| 251 | /* Populate the easy fields of our "struct lguest" */ |
Al Viro | 74dbf71 | 2008-03-29 03:08:28 +0000 | [diff] [blame] | 252 | lg->mem_base = (void __user *)args[0]; |
Rusty Russell | 3c6b5bf | 2007-10-22 11:03:26 +1000 | [diff] [blame] | 253 | lg->pfn_limit = args[1]; |
Rusty Russell | 7313d52 | 2015-02-11 15:15:10 +1030 | [diff] [blame] | 254 | lg->device_limit = args[3]; |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 255 | |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 256 | /* This is the first cpu (cpu 0) and it will start booting at args[2] */ |
| 257 | err = lg_cpu_start(&lg->cpus[0], 0, args[2]); |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 258 | if (err) |
Rusty Russell | d9bab50 | 2015-02-11 15:28:01 +1030 | [diff] [blame] | 259 | goto free_lg; |
Glauber de Oliveira Costa | 4dcc53d | 2008-01-07 11:05:24 -0200 | [diff] [blame] | 260 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 261 | /* |
Rusty Russell | 9f54288 | 2011-07-22 14:39:50 +0930 | [diff] [blame] | 262 | * Initialize the Guest's shadow page tables. This allocates |
| 263 | * memory, so can fail. |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 264 | */ |
Matias Zabaljauregui | 58a2456 | 2008-09-29 01:40:07 -0300 | [diff] [blame] | 265 | err = init_guest_pagetable(lg); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 266 | if (err) |
| 267 | goto free_regs; |
| 268 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 269 | /* We keep our "struct lguest" in the file's private_data. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 270 | file->private_data = lg; |
| 271 | |
| 272 | mutex_unlock(&lguest_lock); |
| 273 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 274 | /* And because this is a write() call, we return the length used. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 275 | return sizeof(args); |
| 276 | |
| 277 | free_regs: |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 278 | /* FIXME: This should be in free_vcpu */ |
| 279 | free_page(lg->cpus[0].regs_page); |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 280 | free_lg: |
Adrian Bunk | 4305441 | 2007-11-14 16:59:00 -0800 | [diff] [blame] | 281 | kfree(lg); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 282 | unlock: |
| 283 | mutex_unlock(&lguest_lock); |
| 284 | return err; |
| 285 | } |
| 286 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 287 | /*L:010 |
| 288 | * The first operation the Launcher does must be a write. All writes |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 289 | * 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] | 290 | * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 291 | * writes of other values to send interrupts or set up receipt of notifications. |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 292 | * |
| 293 | * Note that we overload the "offset" in the /dev/lguest file to indicate what |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 294 | * CPU number we're dealing with. Currently this is always 0 since we only |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 295 | * support uniprocessor Guests, but you can see the beginnings of SMP support |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 296 | * here. |
| 297 | */ |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 298 | static ssize_t write(struct file *file, const char __user *in, |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 299 | size_t size, loff_t *off) |
| 300 | { |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 301 | /* |
| 302 | * Once the Guest is initialized, we hold the "struct lguest" in the |
| 303 | * file private data. |
| 304 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 305 | struct lguest *lg = file->private_data; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 306 | const unsigned long __user *input = (const unsigned long __user *)in; |
| 307 | unsigned long req; |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 308 | struct lg_cpu *uninitialized_var(cpu); |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 309 | unsigned int cpu_id = *off; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 310 | |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 311 | /* The first value tells us what this request is. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 312 | if (get_user(req, input) != 0) |
| 313 | return -EFAULT; |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 314 | input++; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 315 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 316 | /* If you haven't initialized, you must do that first. */ |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 317 | if (req != LHREQ_INITIALIZE) { |
| 318 | if (!lg || (cpu_id >= lg->nr_cpus)) |
| 319 | return -EINVAL; |
| 320 | cpu = &lg->cpus[cpu_id]; |
Eugene Teo | f73d1e6 | 2008-02-09 23:53:17 +0800 | [diff] [blame] | 321 | |
| 322 | /* Once the Guest is dead, you can only read() why it died. */ |
| 323 | if (lg->dead) |
| 324 | return -ENOENT; |
Glauber de Oliveira Costa | 7ea07a1 | 2008-01-07 11:05:26 -0200 | [diff] [blame] | 325 | } |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 326 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 327 | switch (req) { |
| 328 | case LHREQ_INITIALIZE: |
Jes Sorensen | 511801d | 2007-10-22 11:03:31 +1000 | [diff] [blame] | 329 | return initialize(file, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 330 | case LHREQ_IRQ: |
Glauber de Oliveira Costa | 177e449 | 2008-01-07 11:05:29 -0200 | [diff] [blame] | 331 | return user_send_irq(cpu, input); |
Rusty Russell | 18c1373 | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 332 | case LHREQ_GETREG: |
| 333 | return getreg_setup(cpu, input); |
| 334 | case LHREQ_SETREG: |
| 335 | return setreg(cpu, input); |
Rusty Russell | 8ed3130 | 2015-02-11 15:15:09 +1030 | [diff] [blame] | 336 | case LHREQ_TRAP: |
| 337 | return trap(cpu, input); |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 338 | default: |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | } |
| 342 | |
Martin Kepplinger | 67c50bf | 2015-03-24 11:51:20 +1030 | [diff] [blame] | 343 | static int open(struct inode *inode, struct file *file) |
| 344 | { |
| 345 | file->private_data = NULL; |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 350 | /*L:060 |
| 351 | * The final piece of interface code is the close() routine. It reverses |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 352 | * everything done in initialize(). This is usually called because the |
| 353 | * Launcher exited. |
| 354 | * |
| 355 | * Note that the close routine returns 0 or a negative error number: it can't |
| 356 | * really fail, but it can whine. I blame Sun for this wart, and K&R C for |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 357 | * letting them do it. |
| 358 | :*/ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 359 | static int close(struct inode *inode, struct file *file) |
| 360 | { |
| 361 | struct lguest *lg = file->private_data; |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 362 | unsigned int i; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 363 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 364 | /* If we never successfully initialized, there's nothing to clean up */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 365 | if (!lg) |
| 366 | return 0; |
| 367 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 368 | /* |
| 369 | * We need the big lock, to protect from inter-guest I/O and other |
| 370 | * Launchers initializing guests. |
| 371 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 372 | mutex_lock(&lguest_lock); |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 373 | |
| 374 | /* Free up the shadow page tables for the Guest. */ |
| 375 | free_guest_pagetable(lg); |
| 376 | |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 377 | for (i = 0; i < lg->nr_cpus; i++) { |
Glauber de Oliveira Costa | ad8d8f3 | 2008-01-07 11:05:28 -0200 | [diff] [blame] | 378 | /* Cancels the hrtimer set via LHCALL_SET_CLOCKEVENT. */ |
| 379 | hrtimer_cancel(&lg->cpus[i].hrt); |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 380 | /* We can free up the register page we allocated. */ |
| 381 | free_page(lg->cpus[i].regs_page); |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 382 | /* |
| 383 | * Now all the memory cleanups are done, it's safe to release |
| 384 | * the Launcher's memory management structure. |
| 385 | */ |
Glauber de Oliveira Costa | 66686c2 | 2008-01-07 11:05:34 -0200 | [diff] [blame] | 386 | mmput(lg->cpus[i].mm); |
Glauber de Oliveira Costa | a53a35a | 2008-01-07 11:05:32 -0200 | [diff] [blame] | 387 | } |
Rusty Russell | df60aee | 2009-06-12 22:27:09 -0600 | [diff] [blame] | 388 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 389 | /* |
| 390 | * If lg->dead doesn't contain an error code it will be NULL or a |
| 391 | * kmalloc()ed string, either of which is ok to hand to kfree(). |
| 392 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 393 | if (!IS_ERR(lg->dead)) |
| 394 | kfree(lg->dead); |
Mark Wallis | 05dfdbb | 2009-01-26 17:32:35 +1100 | [diff] [blame] | 395 | /* Free the memory allocated to the lguest_struct */ |
| 396 | kfree(lg); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 397 | /* Release lock and exit. */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 398 | mutex_unlock(&lguest_lock); |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 399 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 403 | /*L:000 |
| 404 | * Welcome to our journey through the Launcher! |
| 405 | * |
| 406 | * The Launcher is the Host userspace program which sets up, runs and services |
| 407 | * the Guest. In fact, many comments in the Drivers which refer to "the Host" |
| 408 | * doing things are inaccurate: the Launcher does all the device handling for |
Rusty Russell | e1e7296 | 2007-10-25 15:02:50 +1000 | [diff] [blame] | 409 | * the Guest, but the Guest can't know that. |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 410 | * |
| 411 | * Just to confuse you: to the Host kernel, the Launcher *is* the Guest and we |
| 412 | * shall see more of that later. |
| 413 | * |
| 414 | * We begin our understanding with the Host kernel interface which the Launcher |
| 415 | * uses: reading and writing a character device called /dev/lguest. All the |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 416 | * work happens in the read(), write() and close() routines: |
| 417 | */ |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 418 | static const struct file_operations lguest_fops = { |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 419 | .owner = THIS_MODULE, |
Martin Kepplinger | 67c50bf | 2015-03-24 11:51:20 +1030 | [diff] [blame] | 420 | .open = open, |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 421 | .release = close, |
| 422 | .write = write, |
| 423 | .read = read, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 424 | .llseek = default_llseek, |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 425 | }; |
Rusty Russell | 9f54288 | 2011-07-22 14:39:50 +0930 | [diff] [blame] | 426 | /*:*/ |
Rusty Russell | dde7978 | 2007-07-26 10:41:03 -0700 | [diff] [blame] | 427 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 428 | /* |
| 429 | * This is a textbook example of a "misc" character device. Populate a "struct |
| 430 | * miscdevice" and register it with misc_register(). |
| 431 | */ |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 432 | static struct miscdevice lguest_dev = { |
| 433 | .minor = MISC_DYNAMIC_MINOR, |
| 434 | .name = "lguest", |
| 435 | .fops = &lguest_fops, |
| 436 | }; |
| 437 | |
| 438 | int __init lguest_device_init(void) |
| 439 | { |
| 440 | return misc_register(&lguest_dev); |
| 441 | } |
| 442 | |
| 443 | void __exit lguest_device_remove(void) |
| 444 | { |
| 445 | misc_deregister(&lguest_dev); |
| 446 | } |