]> git.proxmox.com Git - qemu.git/blob - xen-all.c
xen: Set running state in xenstore.
[qemu.git] / xen-all.c
1 /*
2 * Copyright (C) 2010 Citrix Ltd.
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
6 *
7 */
8
9 #include <sys/mman.h>
10
11 #include "hw/pci.h"
12 #include "hw/xen_common.h"
13 #include "hw/xen_backend.h"
14
15 #include "xen-mapcache.h"
16 #include "trace.h"
17
18 #include <xen/hvm/ioreq.h>
19 #include <xen/hvm/params.h>
20
21 //#define DEBUG_XEN
22
23 #ifdef DEBUG_XEN
24 #define DPRINTF(fmt, ...) \
25 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
26 #else
27 #define DPRINTF(fmt, ...) \
28 do { } while (0)
29 #endif
30
31 /* Compatibility with older version */
32 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
33 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
34 {
35 return shared_page->vcpu_iodata[i].vp_eport;
36 }
37 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
38 {
39 return &shared_page->vcpu_iodata[vcpu].vp_ioreq;
40 }
41 # define FMT_ioreq_size PRIx64
42 #else
43 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
44 {
45 return shared_page->vcpu_ioreq[i].vp_eport;
46 }
47 static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
48 {
49 return &shared_page->vcpu_ioreq[vcpu];
50 }
51 # define FMT_ioreq_size "u"
52 #endif
53
54 #define BUFFER_IO_MAX_DELAY 100
55
56 typedef struct XenIOState {
57 shared_iopage_t *shared_page;
58 buffered_iopage_t *buffered_io_page;
59 QEMUTimer *buffered_io_timer;
60 /* the evtchn port for polling the notification, */
61 evtchn_port_t *ioreq_local_port;
62 /* the evtchn fd for polling */
63 XenEvtchn xce_handle;
64 /* which vcpu we are serving */
65 int send_vcpu;
66
67 struct xs_handle *xenstore;
68
69 Notifier exit;
70 } XenIOState;
71
72 /* Xen specific function for piix pci */
73
74 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
75 {
76 return irq_num + ((pci_dev->devfn >> 3) << 2);
77 }
78
79 void xen_piix3_set_irq(void *opaque, int irq_num, int level)
80 {
81 xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2,
82 irq_num & 3, level);
83 }
84
85 void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
86 {
87 int i;
88
89 /* Scan for updates to PCI link routes (0x60-0x63). */
90 for (i = 0; i < len; i++) {
91 uint8_t v = (val >> (8 * i)) & 0xff;
92 if (v & 0x80) {
93 v = 0;
94 }
95 v &= 0xf;
96 if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
97 xc_hvm_set_pci_link_route(xen_xc, xen_domid, address + i - 0x60, v);
98 }
99 }
100 }
101
102 /* Xen Interrupt Controller */
103
104 static void xen_set_irq(void *opaque, int irq, int level)
105 {
106 xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level);
107 }
108
109 qemu_irq *xen_interrupt_controller_init(void)
110 {
111 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
112 }
113
114 /* Memory Ops */
115
116 static void xen_ram_init(ram_addr_t ram_size)
117 {
118 RAMBlock *new_block;
119 ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
120
121 new_block = qemu_mallocz(sizeof (*new_block));
122 pstrcpy(new_block->idstr, sizeof (new_block->idstr), "xen.ram");
123 new_block->host = NULL;
124 new_block->offset = 0;
125 new_block->length = ram_size;
126
127 QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
128
129 ram_list.phys_dirty = qemu_realloc(ram_list.phys_dirty,
130 new_block->length >> TARGET_PAGE_BITS);
131 memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
132 0xff, new_block->length >> TARGET_PAGE_BITS);
133
134 if (ram_size >= 0xe0000000 ) {
135 above_4g_mem_size = ram_size - 0xe0000000;
136 below_4g_mem_size = 0xe0000000;
137 } else {
138 below_4g_mem_size = ram_size;
139 }
140
141 cpu_register_physical_memory(0, below_4g_mem_size, new_block->offset);
142 #if TARGET_PHYS_ADDR_BITS > 32
143 if (above_4g_mem_size > 0) {
144 cpu_register_physical_memory(0x100000000ULL, above_4g_mem_size,
145 new_block->offset + below_4g_mem_size);
146 }
147 #endif
148 }
149
150 void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size)
151 {
152 unsigned long nr_pfn;
153 xen_pfn_t *pfn_list;
154 int i;
155
156 trace_xen_ram_alloc(ram_addr, size);
157
158 nr_pfn = size >> TARGET_PAGE_BITS;
159 pfn_list = qemu_malloc(sizeof (*pfn_list) * nr_pfn);
160
161 for (i = 0; i < nr_pfn; i++) {
162 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
163 }
164
165 if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
166 hw_error("xen: failed to populate ram at %lx", ram_addr);
167 }
168
169 qemu_free(pfn_list);
170 }
171
172
173 /* VCPU Operations, MMIO, IO ring ... */
174
175 static void xen_reset_vcpu(void *opaque)
176 {
177 CPUState *env = opaque;
178
179 env->halted = 1;
180 }
181
182 void xen_vcpu_init(void)
183 {
184 CPUState *first_cpu;
185
186 if ((first_cpu = qemu_get_cpu(0))) {
187 qemu_register_reset(xen_reset_vcpu, first_cpu);
188 xen_reset_vcpu(first_cpu);
189 }
190 }
191
192 /* get the ioreq packets from share mem */
193 static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
194 {
195 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
196
197 if (req->state != STATE_IOREQ_READY) {
198 DPRINTF("I/O request not ready: "
199 "%x, ptr: %x, port: %"PRIx64", "
200 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
201 req->state, req->data_is_ptr, req->addr,
202 req->data, req->count, req->size);
203 return NULL;
204 }
205
206 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
207
208 req->state = STATE_IOREQ_INPROCESS;
209 return req;
210 }
211
212 /* use poll to get the port notification */
213 /* ioreq_vec--out,the */
214 /* retval--the number of ioreq packet */
215 static ioreq_t *cpu_get_ioreq(XenIOState *state)
216 {
217 int i;
218 evtchn_port_t port;
219
220 port = xc_evtchn_pending(state->xce_handle);
221 if (port != -1) {
222 for (i = 0; i < smp_cpus; i++) {
223 if (state->ioreq_local_port[i] == port) {
224 break;
225 }
226 }
227
228 if (i == smp_cpus) {
229 hw_error("Fatal error while trying to get io event!\n");
230 }
231
232 /* unmask the wanted port again */
233 xc_evtchn_unmask(state->xce_handle, port);
234
235 /* get the io packet from shared memory */
236 state->send_vcpu = i;
237 return cpu_get_ioreq_from_shared_memory(state, i);
238 }
239
240 /* read error or read nothing */
241 return NULL;
242 }
243
244 static uint32_t do_inp(pio_addr_t addr, unsigned long size)
245 {
246 switch (size) {
247 case 1:
248 return cpu_inb(addr);
249 case 2:
250 return cpu_inw(addr);
251 case 4:
252 return cpu_inl(addr);
253 default:
254 hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
255 }
256 }
257
258 static void do_outp(pio_addr_t addr,
259 unsigned long size, uint32_t val)
260 {
261 switch (size) {
262 case 1:
263 return cpu_outb(addr, val);
264 case 2:
265 return cpu_outw(addr, val);
266 case 4:
267 return cpu_outl(addr, val);
268 default:
269 hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
270 }
271 }
272
273 static void cpu_ioreq_pio(ioreq_t *req)
274 {
275 int i, sign;
276
277 sign = req->df ? -1 : 1;
278
279 if (req->dir == IOREQ_READ) {
280 if (!req->data_is_ptr) {
281 req->data = do_inp(req->addr, req->size);
282 } else {
283 uint32_t tmp;
284
285 for (i = 0; i < req->count; i++) {
286 tmp = do_inp(req->addr, req->size);
287 cpu_physical_memory_write(req->data + (sign * i * req->size),
288 (uint8_t *) &tmp, req->size);
289 }
290 }
291 } else if (req->dir == IOREQ_WRITE) {
292 if (!req->data_is_ptr) {
293 do_outp(req->addr, req->size, req->data);
294 } else {
295 for (i = 0; i < req->count; i++) {
296 uint32_t tmp = 0;
297
298 cpu_physical_memory_read(req->data + (sign * i * req->size),
299 (uint8_t*) &tmp, req->size);
300 do_outp(req->addr, req->size, tmp);
301 }
302 }
303 }
304 }
305
306 static void cpu_ioreq_move(ioreq_t *req)
307 {
308 int i, sign;
309
310 sign = req->df ? -1 : 1;
311
312 if (!req->data_is_ptr) {
313 if (req->dir == IOREQ_READ) {
314 for (i = 0; i < req->count; i++) {
315 cpu_physical_memory_read(req->addr + (sign * i * req->size),
316 (uint8_t *) &req->data, req->size);
317 }
318 } else if (req->dir == IOREQ_WRITE) {
319 for (i = 0; i < req->count; i++) {
320 cpu_physical_memory_write(req->addr + (sign * i * req->size),
321 (uint8_t *) &req->data, req->size);
322 }
323 }
324 } else {
325 target_ulong tmp;
326
327 if (req->dir == IOREQ_READ) {
328 for (i = 0; i < req->count; i++) {
329 cpu_physical_memory_read(req->addr + (sign * i * req->size),
330 (uint8_t*) &tmp, req->size);
331 cpu_physical_memory_write(req->data + (sign * i * req->size),
332 (uint8_t*) &tmp, req->size);
333 }
334 } else if (req->dir == IOREQ_WRITE) {
335 for (i = 0; i < req->count; i++) {
336 cpu_physical_memory_read(req->data + (sign * i * req->size),
337 (uint8_t*) &tmp, req->size);
338 cpu_physical_memory_write(req->addr + (sign * i * req->size),
339 (uint8_t*) &tmp, req->size);
340 }
341 }
342 }
343 }
344
345 static void handle_ioreq(ioreq_t *req)
346 {
347 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
348 (req->size < sizeof (target_ulong))) {
349 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
350 }
351
352 switch (req->type) {
353 case IOREQ_TYPE_PIO:
354 cpu_ioreq_pio(req);
355 break;
356 case IOREQ_TYPE_COPY:
357 cpu_ioreq_move(req);
358 break;
359 case IOREQ_TYPE_TIMEOFFSET:
360 break;
361 case IOREQ_TYPE_INVALIDATE:
362 qemu_invalidate_map_cache();
363 break;
364 default:
365 hw_error("Invalid ioreq type 0x%x\n", req->type);
366 }
367 }
368
369 static void handle_buffered_iopage(XenIOState *state)
370 {
371 buf_ioreq_t *buf_req = NULL;
372 ioreq_t req;
373 int qw;
374
375 if (!state->buffered_io_page) {
376 return;
377 }
378
379 while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
380 buf_req = &state->buffered_io_page->buf_ioreq[
381 state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
382 req.size = 1UL << buf_req->size;
383 req.count = 1;
384 req.addr = buf_req->addr;
385 req.data = buf_req->data;
386 req.state = STATE_IOREQ_READY;
387 req.dir = buf_req->dir;
388 req.df = 1;
389 req.type = buf_req->type;
390 req.data_is_ptr = 0;
391 qw = (req.size == 8);
392 if (qw) {
393 buf_req = &state->buffered_io_page->buf_ioreq[
394 (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
395 req.data |= ((uint64_t)buf_req->data) << 32;
396 }
397
398 handle_ioreq(&req);
399
400 xen_mb();
401 state->buffered_io_page->read_pointer += qw ? 2 : 1;
402 }
403 }
404
405 static void handle_buffered_io(void *opaque)
406 {
407 XenIOState *state = opaque;
408
409 handle_buffered_iopage(state);
410 qemu_mod_timer(state->buffered_io_timer,
411 BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
412 }
413
414 static void cpu_handle_ioreq(void *opaque)
415 {
416 XenIOState *state = opaque;
417 ioreq_t *req = cpu_get_ioreq(state);
418
419 handle_buffered_iopage(state);
420 if (req) {
421 handle_ioreq(req);
422
423 if (req->state != STATE_IOREQ_INPROCESS) {
424 fprintf(stderr, "Badness in I/O request ... not in service?!: "
425 "%x, ptr: %x, port: %"PRIx64", "
426 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
427 req->state, req->data_is_ptr, req->addr,
428 req->data, req->count, req->size);
429 destroy_hvm_domain();
430 return;
431 }
432
433 xen_wmb(); /* Update ioreq contents /then/ update state. */
434
435 /*
436 * We do this before we send the response so that the tools
437 * have the opportunity to pick up on the reset before the
438 * guest resumes and does a hlt with interrupts disabled which
439 * causes Xen to powerdown the domain.
440 */
441 if (vm_running) {
442 if (qemu_shutdown_requested_get()) {
443 destroy_hvm_domain();
444 }
445 if (qemu_reset_requested_get()) {
446 qemu_system_reset();
447 }
448 }
449
450 req->state = STATE_IORESP_READY;
451 xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
452 }
453 }
454
455 static void xenstore_record_dm_state(XenIOState *s, const char *state)
456 {
457 char path[50];
458
459 snprintf(path, sizeof (path), "/local/domain/0/device-model/%u/state", xen_domid);
460 if (!xs_write(s->xenstore, XBT_NULL, path, state, strlen(state))) {
461 fprintf(stderr, "error recording dm state\n");
462 exit(1);
463 }
464 }
465
466 static void xen_main_loop_prepare(XenIOState *state)
467 {
468 int evtchn_fd = -1;
469
470 if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
471 evtchn_fd = xc_evtchn_fd(state->xce_handle);
472 }
473
474 state->buffered_io_timer = qemu_new_timer_ms(rt_clock, handle_buffered_io,
475 state);
476 qemu_mod_timer(state->buffered_io_timer, qemu_get_clock_ms(rt_clock));
477
478 if (evtchn_fd != -1) {
479 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
480 }
481
482 /* record state running */
483 xenstore_record_dm_state(state, "running");
484 }
485
486
487 /* Initialise Xen */
488
489 static void xen_vm_change_state_handler(void *opaque, int running, int reason)
490 {
491 XenIOState *state = opaque;
492 if (running) {
493 xen_main_loop_prepare(state);
494 }
495 }
496
497 static void xen_exit_notifier(Notifier *n)
498 {
499 XenIOState *state = container_of(n, XenIOState, exit);
500
501 xc_evtchn_close(state->xce_handle);
502 xs_daemon_close(state->xenstore);
503 }
504
505 int xen_init(void)
506 {
507 xen_xc = xen_xc_interface_open(0, 0, 0);
508 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
509 xen_be_printf(NULL, 0, "can't open xen interface\n");
510 return -1;
511 }
512
513 return 0;
514 }
515
516 int xen_hvm_init(void)
517 {
518 int i, rc;
519 unsigned long ioreq_pfn;
520 XenIOState *state;
521
522 state = qemu_mallocz(sizeof (XenIOState));
523
524 state->xce_handle = xen_xc_evtchn_open(NULL, 0);
525 if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
526 perror("xen: event channel open");
527 return -errno;
528 }
529
530 state->xenstore = xs_daemon_open();
531 if (state->xenstore == NULL) {
532 perror("xen: xenstore open");
533 return -errno;
534 }
535
536 state->exit.notify = xen_exit_notifier;
537 qemu_add_exit_notifier(&state->exit);
538
539 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn);
540 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
541 state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
542 PROT_READ|PROT_WRITE, ioreq_pfn);
543 if (state->shared_page == NULL) {
544 hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
545 errno, xen_xc);
546 }
547
548 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
549 DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
550 state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
551 PROT_READ|PROT_WRITE, ioreq_pfn);
552 if (state->buffered_io_page == NULL) {
553 hw_error("map buffered IO page returned error %d", errno);
554 }
555
556 state->ioreq_local_port = qemu_mallocz(smp_cpus * sizeof (evtchn_port_t));
557
558 /* FIXME: how about if we overflow the page here? */
559 for (i = 0; i < smp_cpus; i++) {
560 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
561 xen_vcpu_eport(state->shared_page, i));
562 if (rc == -1) {
563 fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
564 return -1;
565 }
566 state->ioreq_local_port[i] = rc;
567 }
568
569 /* Init RAM management */
570 qemu_map_cache_init();
571 xen_ram_init(ram_size);
572
573 qemu_add_vm_change_state_handler(xen_vm_change_state_handler, state);
574
575 return 0;
576 }
577
578 void destroy_hvm_domain(void)
579 {
580 XenXC xc_handle;
581 int sts;
582
583 xc_handle = xen_xc_interface_open(0, 0, 0);
584 if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
585 fprintf(stderr, "Cannot acquire xenctrl handle\n");
586 } else {
587 sts = xc_domain_shutdown(xc_handle, xen_domid, SHUTDOWN_poweroff);
588 if (sts != 0) {
589 fprintf(stderr, "? xc_domain_shutdown failed to issue poweroff, "
590 "sts %d, %s\n", sts, strerror(errno));
591 } else {
592 fprintf(stderr, "Issued domain %d poweroff\n", xen_domid);
593 }
594 xc_interface_close(xc_handle);
595 }
596 }