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