2 * Copyright (C) 2010 Citrix Ltd.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
13 #include "hw/xen_common.h"
14 #include "hw/xen_backend.h"
17 #include "xen-mapcache.h"
19 #include "exec-memory.h"
21 #include <xen/hvm/ioreq.h>
22 #include <xen/hvm/params.h>
23 #include <xen/hvm/e820.h>
28 #define DPRINTF(fmt, ...) \
29 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
31 #define DPRINTF(fmt, ...) \
35 static MemoryRegion ram_memory
, ram_640k
, ram_lo
, ram_hi
;
36 static MemoryRegion
*framebuffer
;
38 /* Compatibility with older version */
39 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
40 static inline uint32_t xen_vcpu_eport(shared_iopage_t
*shared_page
, int i
)
42 return shared_page
->vcpu_iodata
[i
].vp_eport
;
44 static inline ioreq_t
*xen_vcpu_ioreq(shared_iopage_t
*shared_page
, int vcpu
)
46 return &shared_page
->vcpu_iodata
[vcpu
].vp_ioreq
;
48 # define FMT_ioreq_size PRIx64
50 static inline uint32_t xen_vcpu_eport(shared_iopage_t
*shared_page
, int i
)
52 return shared_page
->vcpu_ioreq
[i
].vp_eport
;
54 static inline ioreq_t
*xen_vcpu_ioreq(shared_iopage_t
*shared_page
, int vcpu
)
56 return &shared_page
->vcpu_ioreq
[vcpu
];
58 # define FMT_ioreq_size "u"
61 #define BUFFER_IO_MAX_DELAY 100
63 typedef struct XenPhysmap
{
64 target_phys_addr_t start_addr
;
66 target_phys_addr_t phys_offset
;
68 QLIST_ENTRY(XenPhysmap
) list
;
71 typedef struct XenIOState
{
72 shared_iopage_t
*shared_page
;
73 buffered_iopage_t
*buffered_io_page
;
74 QEMUTimer
*buffered_io_timer
;
75 /* the evtchn port for polling the notification, */
76 evtchn_port_t
*ioreq_local_port
;
77 /* the evtchn fd for polling */
79 /* which vcpu we are serving */
82 struct xs_handle
*xenstore
;
83 CPUPhysMemoryClient client
;
84 QLIST_HEAD(, XenPhysmap
) physmap
;
85 const XenPhysmap
*log_for_dirtybit
;
90 /* Xen specific function for piix pci */
92 int xen_pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
94 return irq_num
+ ((pci_dev
->devfn
>> 3) << 2);
97 void xen_piix3_set_irq(void *opaque
, int irq_num
, int level
)
99 xc_hvm_set_pci_intx_level(xen_xc
, xen_domid
, 0, 0, irq_num
>> 2,
103 void xen_piix_pci_write_config_client(uint32_t address
, uint32_t val
, int len
)
107 /* Scan for updates to PCI link routes (0x60-0x63). */
108 for (i
= 0; i
< len
; i
++) {
109 uint8_t v
= (val
>> (8 * i
)) & 0xff;
114 if (((address
+ i
) >= 0x60) && ((address
+ i
) <= 0x63)) {
115 xc_hvm_set_pci_link_route(xen_xc
, xen_domid
, address
+ i
- 0x60, v
);
120 void xen_cmos_set_s3_resume(void *opaque
, int irq
, int level
)
122 pc_cmos_set_s3_resume(opaque
, irq
, level
);
124 xc_set_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_ACPI_S_STATE
, 3);
128 /* Xen Interrupt Controller */
130 static void xen_set_irq(void *opaque
, int irq
, int level
)
132 xc_hvm_set_isa_irq_level(xen_xc
, xen_domid
, irq
, level
);
135 qemu_irq
*xen_interrupt_controller_init(void)
137 return qemu_allocate_irqs(xen_set_irq
, NULL
, 16);
142 static void xen_ram_init(ram_addr_t ram_size
)
144 MemoryRegion
*sysmem
= get_system_memory();
145 ram_addr_t below_4g_mem_size
, above_4g_mem_size
= 0;
146 ram_addr_t block_len
;
148 block_len
= ram_size
;
149 if (ram_size
>= HVM_BELOW_4G_RAM_END
) {
150 /* Xen does not allocate the memory continuously, and keep a hole at
151 * HVM_BELOW_4G_MMIO_START of HVM_BELOW_4G_MMIO_LENGTH
153 block_len
+= HVM_BELOW_4G_MMIO_LENGTH
;
155 memory_region_init_ram(&ram_memory
, NULL
, "xen.ram", block_len
);
157 if (ram_size
>= HVM_BELOW_4G_RAM_END
) {
158 above_4g_mem_size
= ram_size
- HVM_BELOW_4G_RAM_END
;
159 below_4g_mem_size
= HVM_BELOW_4G_RAM_END
;
161 below_4g_mem_size
= ram_size
;
164 memory_region_init_alias(&ram_640k
, "xen.ram.640k",
165 &ram_memory
, 0, 0xa0000);
166 memory_region_add_subregion(sysmem
, 0, &ram_640k
);
167 /* Skip of the VGA IO memory space, it will be registered later by the VGA
170 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
171 * the Options ROM, so it is registered here as RAM.
173 memory_region_init_alias(&ram_lo
, "xen.ram.lo",
174 &ram_memory
, 0xc0000, below_4g_mem_size
- 0xc0000);
175 memory_region_add_subregion(sysmem
, 0xc0000, &ram_lo
);
176 if (above_4g_mem_size
> 0) {
177 memory_region_init_alias(&ram_hi
, "xen.ram.hi",
178 &ram_memory
, 0x100000000ULL
,
180 memory_region_add_subregion(sysmem
, 0x100000000ULL
, &ram_hi
);
184 void xen_ram_alloc(ram_addr_t ram_addr
, ram_addr_t size
, MemoryRegion
*mr
)
186 unsigned long nr_pfn
;
190 if (mr
== &ram_memory
) {
194 trace_xen_ram_alloc(ram_addr
, size
);
196 nr_pfn
= size
>> TARGET_PAGE_BITS
;
197 pfn_list
= g_malloc(sizeof (*pfn_list
) * nr_pfn
);
199 for (i
= 0; i
< nr_pfn
; i
++) {
200 pfn_list
[i
] = (ram_addr
>> TARGET_PAGE_BITS
) + i
;
203 if (xc_domain_populate_physmap_exact(xen_xc
, xen_domid
, nr_pfn
, 0, 0, pfn_list
)) {
204 hw_error("xen: failed to populate ram at " RAM_ADDR_FMT
, ram_addr
);
210 static XenPhysmap
*get_physmapping(XenIOState
*state
,
211 target_phys_addr_t start_addr
, ram_addr_t size
)
213 XenPhysmap
*physmap
= NULL
;
215 start_addr
&= TARGET_PAGE_MASK
;
217 QLIST_FOREACH(physmap
, &state
->physmap
, list
) {
218 if (range_covers_byte(physmap
->start_addr
, physmap
->size
, start_addr
)) {
225 #if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
226 static int xen_add_to_physmap(XenIOState
*state
,
227 target_phys_addr_t start_addr
,
229 target_phys_addr_t phys_offset
)
233 XenPhysmap
*physmap
= NULL
;
234 target_phys_addr_t pfn
, start_gpfn
;
237 if (get_physmapping(state
, start_addr
, size
)) {
244 /* Xen can only handle a single dirty log region for now and we want
245 * the linear framebuffer to be that region.
246 * Avoid tracking any regions that is not videoram and avoid tracking
247 * the legacy vga region. */
248 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
249 if (!strcmp(block
->idstr
, "vga.vram") && block
->offset
== phys_offset
250 && start_addr
> 0xbffff) {
257 DPRINTF("mapping vram to %llx - %llx, from %llx\n",
258 start_addr
, start_addr
+ size
, phys_offset
);
260 pfn
= phys_offset
>> TARGET_PAGE_BITS
;
261 start_gpfn
= start_addr
>> TARGET_PAGE_BITS
;
262 for (i
= 0; i
< size
>> TARGET_PAGE_BITS
; i
++) {
263 unsigned long idx
= pfn
+ i
;
264 xen_pfn_t gpfn
= start_gpfn
+ i
;
266 rc
= xc_domain_add_to_physmap(xen_xc
, xen_domid
, XENMAPSPACE_gmfn
, idx
, gpfn
);
268 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn
" to PFN %"
269 PRI_xen_pfn
" failed: %d\n", idx
, gpfn
, rc
);
274 physmap
= g_malloc(sizeof (XenPhysmap
));
276 physmap
->start_addr
= start_addr
;
277 physmap
->size
= size
;
278 physmap
->phys_offset
= phys_offset
;
280 QLIST_INSERT_HEAD(&state
->physmap
, physmap
, list
);
282 xc_domain_pin_memory_cacheattr(xen_xc
, xen_domid
,
283 start_addr
>> TARGET_PAGE_BITS
,
284 (start_addr
+ size
) >> TARGET_PAGE_BITS
,
285 XEN_DOMCTL_MEM_CACHEATTR_WB
);
289 static int xen_remove_from_physmap(XenIOState
*state
,
290 target_phys_addr_t start_addr
,
295 XenPhysmap
*physmap
= NULL
;
296 target_phys_addr_t phys_offset
= 0;
298 physmap
= get_physmapping(state
, start_addr
, size
);
299 if (physmap
== NULL
) {
303 phys_offset
= physmap
->phys_offset
;
304 size
= physmap
->size
;
306 DPRINTF("unmapping vram to %llx - %llx, from %llx\n",
307 phys_offset
, phys_offset
+ size
, start_addr
);
309 size
>>= TARGET_PAGE_BITS
;
310 start_addr
>>= TARGET_PAGE_BITS
;
311 phys_offset
>>= TARGET_PAGE_BITS
;
312 for (i
= 0; i
< size
; i
++) {
313 unsigned long idx
= start_addr
+ i
;
314 xen_pfn_t gpfn
= phys_offset
+ i
;
316 rc
= xc_domain_add_to_physmap(xen_xc
, xen_domid
, XENMAPSPACE_gmfn
, idx
, gpfn
);
318 fprintf(stderr
, "add_to_physmap MFN %"PRI_xen_pfn
" to PFN %"
319 PRI_xen_pfn
" failed: %d\n", idx
, gpfn
, rc
);
324 QLIST_REMOVE(physmap
, list
);
325 if (state
->log_for_dirtybit
== physmap
) {
326 state
->log_for_dirtybit
= NULL
;
334 static int xen_add_to_physmap(XenIOState
*state
,
335 target_phys_addr_t start_addr
,
337 target_phys_addr_t phys_offset
)
342 static int xen_remove_from_physmap(XenIOState
*state
,
343 target_phys_addr_t start_addr
,
350 static void xen_client_set_memory(struct CPUPhysMemoryClient
*client
,
351 target_phys_addr_t start_addr
,
353 ram_addr_t phys_offset
,
356 XenIOState
*state
= container_of(client
, XenIOState
, client
);
357 ram_addr_t flags
= phys_offset
& ~TARGET_PAGE_MASK
;
358 hvmmem_type_t mem_type
;
360 if (!(start_addr
!= phys_offset
361 && ( (log_dirty
&& flags
< IO_MEM_UNASSIGNED
)
362 || (!log_dirty
&& flags
== IO_MEM_UNASSIGNED
)))) {
366 trace_xen_client_set_memory(start_addr
, size
, phys_offset
, log_dirty
);
368 start_addr
&= TARGET_PAGE_MASK
;
369 size
= TARGET_PAGE_ALIGN(size
);
370 phys_offset
&= TARGET_PAGE_MASK
;
374 xen_add_to_physmap(state
, start_addr
, size
, phys_offset
);
377 mem_type
= HVMMEM_ram_ro
;
378 if (xc_hvm_set_mem_type(xen_xc
, xen_domid
, mem_type
,
379 start_addr
>> TARGET_PAGE_BITS
,
380 size
>> TARGET_PAGE_BITS
)) {
381 DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx
"\n",
385 case IO_MEM_UNASSIGNED
:
386 if (xen_remove_from_physmap(state
, start_addr
, size
) < 0) {
387 DPRINTF("physmapping does not exist at "TARGET_FMT_plx
"\n", start_addr
);
393 static int xen_sync_dirty_bitmap(XenIOState
*state
,
394 target_phys_addr_t start_addr
,
397 target_phys_addr_t npages
= size
>> TARGET_PAGE_BITS
;
398 target_phys_addr_t vram_offset
= 0;
399 const int width
= sizeof(unsigned long) * 8;
400 unsigned long bitmap
[(npages
+ width
- 1) / width
];
402 const XenPhysmap
*physmap
= NULL
;
404 physmap
= get_physmapping(state
, start_addr
, size
);
405 if (physmap
== NULL
) {
410 if (state
->log_for_dirtybit
== NULL
) {
411 state
->log_for_dirtybit
= physmap
;
412 } else if (state
->log_for_dirtybit
!= physmap
) {
415 vram_offset
= physmap
->phys_offset
;
417 rc
= xc_hvm_track_dirty_vram(xen_xc
, xen_domid
,
418 start_addr
>> TARGET_PAGE_BITS
, npages
,
424 for (i
= 0; i
< ARRAY_SIZE(bitmap
); i
++) {
425 unsigned long map
= bitmap
[i
];
429 cpu_physical_memory_set_dirty(vram_offset
+ (i
* width
+ j
) * TARGET_PAGE_SIZE
);
436 static int xen_log_start(CPUPhysMemoryClient
*client
, target_phys_addr_t phys_addr
, ram_addr_t size
)
438 XenIOState
*state
= container_of(client
, XenIOState
, client
);
440 return xen_sync_dirty_bitmap(state
, phys_addr
, size
);
443 static int xen_log_stop(CPUPhysMemoryClient
*client
, target_phys_addr_t phys_addr
, ram_addr_t size
)
445 XenIOState
*state
= container_of(client
, XenIOState
, client
);
447 state
->log_for_dirtybit
= NULL
;
448 /* Disable dirty bit tracking */
449 return xc_hvm_track_dirty_vram(xen_xc
, xen_domid
, 0, 0, NULL
);
452 static int xen_client_sync_dirty_bitmap(struct CPUPhysMemoryClient
*client
,
453 target_phys_addr_t start_addr
,
454 target_phys_addr_t end_addr
)
456 XenIOState
*state
= container_of(client
, XenIOState
, client
);
458 return xen_sync_dirty_bitmap(state
, start_addr
, end_addr
- start_addr
);
461 static int xen_client_migration_log(struct CPUPhysMemoryClient
*client
,
467 static CPUPhysMemoryClient xen_cpu_phys_memory_client
= {
468 .set_memory
= xen_client_set_memory
,
469 .sync_dirty_bitmap
= xen_client_sync_dirty_bitmap
,
470 .migration_log
= xen_client_migration_log
,
471 .log_start
= xen_log_start
,
472 .log_stop
= xen_log_stop
,
475 /* VCPU Operations, MMIO, IO ring ... */
477 static void xen_reset_vcpu(void *opaque
)
479 CPUState
*env
= opaque
;
484 void xen_vcpu_init(void)
488 if ((first_cpu
= qemu_get_cpu(0))) {
489 qemu_register_reset(xen_reset_vcpu
, first_cpu
);
490 xen_reset_vcpu(first_cpu
);
494 /* get the ioreq packets from share mem */
495 static ioreq_t
*cpu_get_ioreq_from_shared_memory(XenIOState
*state
, int vcpu
)
497 ioreq_t
*req
= xen_vcpu_ioreq(state
->shared_page
, vcpu
);
499 if (req
->state
!= STATE_IOREQ_READY
) {
500 DPRINTF("I/O request not ready: "
501 "%x, ptr: %x, port: %"PRIx64
", "
502 "data: %"PRIx64
", count: %" FMT_ioreq_size
", size: %" FMT_ioreq_size
"\n",
503 req
->state
, req
->data_is_ptr
, req
->addr
,
504 req
->data
, req
->count
, req
->size
);
508 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
510 req
->state
= STATE_IOREQ_INPROCESS
;
514 /* use poll to get the port notification */
515 /* ioreq_vec--out,the */
516 /* retval--the number of ioreq packet */
517 static ioreq_t
*cpu_get_ioreq(XenIOState
*state
)
522 port
= xc_evtchn_pending(state
->xce_handle
);
524 for (i
= 0; i
< smp_cpus
; i
++) {
525 if (state
->ioreq_local_port
[i
] == port
) {
531 hw_error("Fatal error while trying to get io event!\n");
534 /* unmask the wanted port again */
535 xc_evtchn_unmask(state
->xce_handle
, port
);
537 /* get the io packet from shared memory */
538 state
->send_vcpu
= i
;
539 return cpu_get_ioreq_from_shared_memory(state
, i
);
542 /* read error or read nothing */
546 static uint32_t do_inp(pio_addr_t addr
, unsigned long size
)
550 return cpu_inb(addr
);
552 return cpu_inw(addr
);
554 return cpu_inl(addr
);
556 hw_error("inp: bad size: %04"FMT_pioaddr
" %lx", addr
, size
);
560 static void do_outp(pio_addr_t addr
,
561 unsigned long size
, uint32_t val
)
565 return cpu_outb(addr
, val
);
567 return cpu_outw(addr
, val
);
569 return cpu_outl(addr
, val
);
571 hw_error("outp: bad size: %04"FMT_pioaddr
" %lx", addr
, size
);
575 static void cpu_ioreq_pio(ioreq_t
*req
)
579 sign
= req
->df
? -1 : 1;
581 if (req
->dir
== IOREQ_READ
) {
582 if (!req
->data_is_ptr
) {
583 req
->data
= do_inp(req
->addr
, req
->size
);
587 for (i
= 0; i
< req
->count
; i
++) {
588 tmp
= do_inp(req
->addr
, req
->size
);
589 cpu_physical_memory_write(req
->data
+ (sign
* i
* req
->size
),
590 (uint8_t *) &tmp
, req
->size
);
593 } else if (req
->dir
== IOREQ_WRITE
) {
594 if (!req
->data_is_ptr
) {
595 do_outp(req
->addr
, req
->size
, req
->data
);
597 for (i
= 0; i
< req
->count
; i
++) {
600 cpu_physical_memory_read(req
->data
+ (sign
* i
* req
->size
),
601 (uint8_t*) &tmp
, req
->size
);
602 do_outp(req
->addr
, req
->size
, tmp
);
608 static void cpu_ioreq_move(ioreq_t
*req
)
612 sign
= req
->df
? -1 : 1;
614 if (!req
->data_is_ptr
) {
615 if (req
->dir
== IOREQ_READ
) {
616 for (i
= 0; i
< req
->count
; i
++) {
617 cpu_physical_memory_read(req
->addr
+ (sign
* i
* req
->size
),
618 (uint8_t *) &req
->data
, req
->size
);
620 } else if (req
->dir
== IOREQ_WRITE
) {
621 for (i
= 0; i
< req
->count
; i
++) {
622 cpu_physical_memory_write(req
->addr
+ (sign
* i
* req
->size
),
623 (uint8_t *) &req
->data
, req
->size
);
629 if (req
->dir
== IOREQ_READ
) {
630 for (i
= 0; i
< req
->count
; i
++) {
631 cpu_physical_memory_read(req
->addr
+ (sign
* i
* req
->size
),
632 (uint8_t*) &tmp
, req
->size
);
633 cpu_physical_memory_write(req
->data
+ (sign
* i
* req
->size
),
634 (uint8_t*) &tmp
, req
->size
);
636 } else if (req
->dir
== IOREQ_WRITE
) {
637 for (i
= 0; i
< req
->count
; i
++) {
638 cpu_physical_memory_read(req
->data
+ (sign
* i
* req
->size
),
639 (uint8_t*) &tmp
, req
->size
);
640 cpu_physical_memory_write(req
->addr
+ (sign
* i
* req
->size
),
641 (uint8_t*) &tmp
, req
->size
);
647 static void handle_ioreq(ioreq_t
*req
)
649 if (!req
->data_is_ptr
&& (req
->dir
== IOREQ_WRITE
) &&
650 (req
->size
< sizeof (target_ulong
))) {
651 req
->data
&= ((target_ulong
) 1 << (8 * req
->size
)) - 1;
658 case IOREQ_TYPE_COPY
:
661 case IOREQ_TYPE_TIMEOFFSET
:
663 case IOREQ_TYPE_INVALIDATE
:
664 xen_invalidate_map_cache();
667 hw_error("Invalid ioreq type 0x%x\n", req
->type
);
671 static void handle_buffered_iopage(XenIOState
*state
)
673 buf_ioreq_t
*buf_req
= NULL
;
677 if (!state
->buffered_io_page
) {
681 while (state
->buffered_io_page
->read_pointer
!= state
->buffered_io_page
->write_pointer
) {
682 buf_req
= &state
->buffered_io_page
->buf_ioreq
[
683 state
->buffered_io_page
->read_pointer
% IOREQ_BUFFER_SLOT_NUM
];
684 req
.size
= 1UL << buf_req
->size
;
686 req
.addr
= buf_req
->addr
;
687 req
.data
= buf_req
->data
;
688 req
.state
= STATE_IOREQ_READY
;
689 req
.dir
= buf_req
->dir
;
691 req
.type
= buf_req
->type
;
693 qw
= (req
.size
== 8);
695 buf_req
= &state
->buffered_io_page
->buf_ioreq
[
696 (state
->buffered_io_page
->read_pointer
+ 1) % IOREQ_BUFFER_SLOT_NUM
];
697 req
.data
|= ((uint64_t)buf_req
->data
) << 32;
703 state
->buffered_io_page
->read_pointer
+= qw
? 2 : 1;
707 static void handle_buffered_io(void *opaque
)
709 XenIOState
*state
= opaque
;
711 handle_buffered_iopage(state
);
712 qemu_mod_timer(state
->buffered_io_timer
,
713 BUFFER_IO_MAX_DELAY
+ qemu_get_clock_ms(rt_clock
));
716 static void cpu_handle_ioreq(void *opaque
)
718 XenIOState
*state
= opaque
;
719 ioreq_t
*req
= cpu_get_ioreq(state
);
721 handle_buffered_iopage(state
);
725 if (req
->state
!= STATE_IOREQ_INPROCESS
) {
726 fprintf(stderr
, "Badness in I/O request ... not in service?!: "
727 "%x, ptr: %x, port: %"PRIx64
", "
728 "data: %"PRIx64
", count: %" FMT_ioreq_size
", size: %" FMT_ioreq_size
"\n",
729 req
->state
, req
->data_is_ptr
, req
->addr
,
730 req
->data
, req
->count
, req
->size
);
731 destroy_hvm_domain();
735 xen_wmb(); /* Update ioreq contents /then/ update state. */
738 * We do this before we send the response so that the tools
739 * have the opportunity to pick up on the reset before the
740 * guest resumes and does a hlt with interrupts disabled which
741 * causes Xen to powerdown the domain.
743 if (runstate_is_running()) {
744 if (qemu_shutdown_requested_get()) {
745 destroy_hvm_domain();
747 if (qemu_reset_requested_get()) {
748 qemu_system_reset(VMRESET_REPORT
);
752 req
->state
= STATE_IORESP_READY
;
753 xc_evtchn_notify(state
->xce_handle
, state
->ioreq_local_port
[state
->send_vcpu
]);
757 static int store_dev_info(int domid
, CharDriverState
*cs
, const char *string
)
759 struct xs_handle
*xs
= NULL
;
761 char *newpath
= NULL
;
765 /* Only continue if we're talking to a pty. */
766 if (strncmp(cs
->filename
, "pty:", 4)) {
769 pts
= cs
->filename
+ 4;
771 /* We now have everything we need to set the xenstore entry. */
774 fprintf(stderr
, "Could not contact XenStore\n");
778 path
= xs_get_domain_path(xs
, domid
);
780 fprintf(stderr
, "xs_get_domain_path() error\n");
783 newpath
= realloc(path
, (strlen(path
) + strlen(string
) +
784 strlen("/tty") + 1));
785 if (newpath
== NULL
) {
786 fprintf(stderr
, "realloc error\n");
791 strcat(path
, string
);
792 strcat(path
, "/tty");
793 if (!xs_write(xs
, XBT_NULL
, path
, pts
, strlen(pts
))) {
794 fprintf(stderr
, "xs_write for '%s' fail", string
);
806 void xenstore_store_pv_console_info(int i
, CharDriverState
*chr
)
809 store_dev_info(xen_domid
, chr
, "/console");
812 snprintf(buf
, sizeof(buf
), "/device/console/%d", i
);
813 store_dev_info(xen_domid
, chr
, buf
);
817 static void xenstore_record_dm_state(struct xs_handle
*xs
, const char *state
)
822 fprintf(stderr
, "xenstore connection not initialized\n");
826 snprintf(path
, sizeof (path
), "/local/domain/0/device-model/%u/state", xen_domid
);
827 if (!xs_write(xs
, XBT_NULL
, path
, state
, strlen(state
))) {
828 fprintf(stderr
, "error recording dm state\n");
833 static void xen_main_loop_prepare(XenIOState
*state
)
837 if (state
->xce_handle
!= XC_HANDLER_INITIAL_VALUE
) {
838 evtchn_fd
= xc_evtchn_fd(state
->xce_handle
);
841 state
->buffered_io_timer
= qemu_new_timer_ms(rt_clock
, handle_buffered_io
,
843 qemu_mod_timer(state
->buffered_io_timer
, qemu_get_clock_ms(rt_clock
));
845 if (evtchn_fd
!= -1) {
846 qemu_set_fd_handler(evtchn_fd
, cpu_handle_ioreq
, NULL
, state
);
853 static void xen_change_state_handler(void *opaque
, int running
,
857 /* record state running */
858 xenstore_record_dm_state(xenstore
, "running");
862 static void xen_hvm_change_state_handler(void *opaque
, int running
,
865 XenIOState
*xstate
= opaque
;
867 xen_main_loop_prepare(xstate
);
871 static void xen_exit_notifier(Notifier
*n
, void *data
)
873 XenIOState
*state
= container_of(n
, XenIOState
, exit
);
875 xc_evtchn_close(state
->xce_handle
);
876 xs_daemon_close(state
->xenstore
);
881 xen_xc
= xen_xc_interface_open(0, 0, 0);
882 if (xen_xc
== XC_HANDLER_INITIAL_VALUE
) {
883 xen_be_printf(NULL
, 0, "can't open xen interface\n");
886 qemu_add_vm_change_state_handler(xen_change_state_handler
, NULL
);
891 int xen_hvm_init(void)
894 unsigned long ioreq_pfn
;
897 state
= g_malloc0(sizeof (XenIOState
));
899 state
->xce_handle
= xen_xc_evtchn_open(NULL
, 0);
900 if (state
->xce_handle
== XC_HANDLER_INITIAL_VALUE
) {
901 perror("xen: event channel open");
905 state
->xenstore
= xs_daemon_open();
906 if (state
->xenstore
== NULL
) {
907 perror("xen: xenstore open");
911 state
->exit
.notify
= xen_exit_notifier
;
912 qemu_add_exit_notifier(&state
->exit
);
914 xc_get_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_IOREQ_PFN
, &ioreq_pfn
);
915 DPRINTF("shared page at pfn %lx\n", ioreq_pfn
);
916 state
->shared_page
= xc_map_foreign_range(xen_xc
, xen_domid
, XC_PAGE_SIZE
,
917 PROT_READ
|PROT_WRITE
, ioreq_pfn
);
918 if (state
->shared_page
== NULL
) {
919 hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT
,
923 xc_get_hvm_param(xen_xc
, xen_domid
, HVM_PARAM_BUFIOREQ_PFN
, &ioreq_pfn
);
924 DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn
);
925 state
->buffered_io_page
= xc_map_foreign_range(xen_xc
, xen_domid
, XC_PAGE_SIZE
,
926 PROT_READ
|PROT_WRITE
, ioreq_pfn
);
927 if (state
->buffered_io_page
== NULL
) {
928 hw_error("map buffered IO page returned error %d", errno
);
931 state
->ioreq_local_port
= g_malloc0(smp_cpus
* sizeof (evtchn_port_t
));
933 /* FIXME: how about if we overflow the page here? */
934 for (i
= 0; i
< smp_cpus
; i
++) {
935 rc
= xc_evtchn_bind_interdomain(state
->xce_handle
, xen_domid
,
936 xen_vcpu_eport(state
->shared_page
, i
));
938 fprintf(stderr
, "bind interdomain ioctl error %d\n", errno
);
941 state
->ioreq_local_port
[i
] = rc
;
944 /* Init RAM management */
945 xen_map_cache_init();
946 xen_ram_init(ram_size
);
948 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler
, state
);
950 state
->client
= xen_cpu_phys_memory_client
;
951 QLIST_INIT(&state
->physmap
);
952 cpu_register_phys_memory_client(&state
->client
);
953 state
->log_for_dirtybit
= NULL
;
955 /* Initialize backend core & drivers */
956 if (xen_be_init() != 0) {
957 fprintf(stderr
, "%s: xen backend core setup failed\n", __FUNCTION__
);
960 xen_be_register("console", &xen_console_ops
);
961 xen_be_register("vkbd", &xen_kbdmouse_ops
);
962 xen_be_register("qdisk", &xen_blkdev_ops
);
967 void destroy_hvm_domain(void)
972 xc_handle
= xen_xc_interface_open(0, 0, 0);
973 if (xc_handle
== XC_HANDLER_INITIAL_VALUE
) {
974 fprintf(stderr
, "Cannot acquire xenctrl handle\n");
976 sts
= xc_domain_shutdown(xc_handle
, xen_domid
, SHUTDOWN_poweroff
);
978 fprintf(stderr
, "? xc_domain_shutdown failed to issue poweroff, "
979 "sts %d, %s\n", sts
, strerror(errno
));
981 fprintf(stderr
, "Issued domain %d poweroff\n", xen_domid
);
983 xc_interface_close(xc_handle
);
987 void xen_register_framebuffer(MemoryRegion
*mr
)