193d43e7eSAnthony Xu /*
293d43e7eSAnthony Xu * Copyright (C) 2010 Citrix Ltd.
393d43e7eSAnthony Xu *
493d43e7eSAnthony Xu * This work is licensed under the terms of the GNU GPL, version 2. See
593d43e7eSAnthony Xu * the COPYING file in the top-level directory.
693d43e7eSAnthony Xu *
793d43e7eSAnthony Xu * Contributions after 2012-01-13 are licensed under the terms of the
893d43e7eSAnthony Xu * GNU GPL, version 2 or (at your option) any later version.
993d43e7eSAnthony Xu */
1093d43e7eSAnthony Xu
1193d43e7eSAnthony Xu #include "qemu/osdep.h"
12039a93b0SPhilippe Mathieu-Daudé #include "qemu/units.h"
13e688df6bSMarkus Armbruster #include "qapi/error.h"
1428af9ba2SPhilippe Mathieu-Daudé #include "qapi/qapi-commands-migration.h"
1593d43e7eSAnthony Xu #include "trace.h"
1693d43e7eSAnthony Xu
17f17068c1SStefano Stabellini #include "hw/i386/pc.h"
18f17068c1SStefano Stabellini #include "hw/irq.h"
19f17068c1SStefano Stabellini #include "hw/i386/apic-msidef.h"
20f17068c1SStefano Stabellini #include "hw/xen/xen-x86.h"
21f17068c1SStefano Stabellini #include "qemu/range.h"
22f17068c1SStefano Stabellini
23f17068c1SStefano Stabellini #include "hw/xen/xen-hvm-common.h"
24f17068c1SStefano Stabellini #include "hw/xen/arch_hvm.h"
2593d43e7eSAnthony Xu #include <xen/hvm/e820.h>
26825f292dSPhilippe Mathieu-Daudé #include "exec/target_page.h"
2793d43e7eSAnthony Xu
28f17068c1SStefano Stabellini static MemoryRegion ram_640k, ram_lo, ram_hi;
2993d43e7eSAnthony Xu static MemoryRegion *framebuffer;
3093d43e7eSAnthony Xu static bool xen_in_migration;
3193d43e7eSAnthony Xu
3293d43e7eSAnthony Xu /* Compatibility with older version */
3393d43e7eSAnthony Xu
34e2abfe5eSDavid Woodhouse /*
35e2abfe5eSDavid Woodhouse * This allows QEMU to build on a system that has Xen 4.5 or earlier installed.
36e2abfe5eSDavid Woodhouse * This is here (not in hw/xen/xen_native.h) because xen/hvm/ioreq.h needs to
37e2abfe5eSDavid Woodhouse * be included before this block and hw/xen/xen_native.h needs to be included
38e2abfe5eSDavid Woodhouse * before xen/hvm/ioreq.h
3993d43e7eSAnthony Xu */
4093d43e7eSAnthony Xu #ifndef IOREQ_TYPE_VMWARE_PORT
4193d43e7eSAnthony Xu #define IOREQ_TYPE_VMWARE_PORT 3
4293d43e7eSAnthony Xu struct vmware_regs {
4393d43e7eSAnthony Xu uint32_t esi;
4493d43e7eSAnthony Xu uint32_t edi;
4593d43e7eSAnthony Xu uint32_t ebx;
4693d43e7eSAnthony Xu uint32_t ecx;
4793d43e7eSAnthony Xu uint32_t edx;
4893d43e7eSAnthony Xu };
4993d43e7eSAnthony Xu typedef struct vmware_regs vmware_regs_t;
5093d43e7eSAnthony Xu
5193d43e7eSAnthony Xu struct shared_vmport_iopage {
5293d43e7eSAnthony Xu struct vmware_regs vcpu_vmport_regs[1];
5393d43e7eSAnthony Xu };
5493d43e7eSAnthony Xu typedef struct shared_vmport_iopage shared_vmport_iopage_t;
5593d43e7eSAnthony Xu #endif
56f17068c1SStefano Stabellini
579269b9d1SStefano Stabellini static shared_vmport_iopage_t *shared_vmport_page;
5893d43e7eSAnthony Xu
5904a8f72eSIgor Druzhinin static QLIST_HEAD(, XenPhysmap) xen_physmap;
609269b9d1SStefano Stabellini static const XenPhysmap *log_for_dirtybit;
619269b9d1SStefano Stabellini /* Buffer used by xen_sync_dirty_bitmap */
629269b9d1SStefano Stabellini static unsigned long *dirty_bitmap;
639269b9d1SStefano Stabellini static Notifier suspend;
649269b9d1SStefano Stabellini static Notifier wakeup;
6504a8f72eSIgor Druzhinin
6693d43e7eSAnthony Xu /* Xen specific function for piix pci */
6793d43e7eSAnthony Xu
xen_pci_slot_get_pirq(PCIDevice * pci_dev,int irq_num)6893d43e7eSAnthony Xu int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
6993d43e7eSAnthony Xu {
708d40def6SPhilippe Mathieu-Daudé return irq_num + (PCI_SLOT(pci_dev->devfn) << 2);
7193d43e7eSAnthony Xu }
7293d43e7eSAnthony Xu
xen_intx_set_irq(void * opaque,int irq_num,int level)7327047bd2SBernhard Beschow void xen_intx_set_irq(void *opaque, int irq_num, int level)
7493d43e7eSAnthony Xu {
7593d43e7eSAnthony Xu xen_set_pci_intx_level(xen_domid, 0, 0, irq_num >> 2,
7693d43e7eSAnthony Xu irq_num & 3, level);
7793d43e7eSAnthony Xu }
7893d43e7eSAnthony Xu
xen_set_pci_link_route(uint8_t link,uint8_t irq)7921d87050SBernhard Beschow int xen_set_pci_link_route(uint8_t link, uint8_t irq)
8021d87050SBernhard Beschow {
8121d87050SBernhard Beschow return xendevicemodel_set_pci_link_route(xen_dmod, xen_domid, link, irq);
8221d87050SBernhard Beschow }
8321d87050SBernhard Beschow
xen_is_pirq_msi(uint32_t msi_data)8493d43e7eSAnthony Xu int xen_is_pirq_msi(uint32_t msi_data)
8593d43e7eSAnthony Xu {
8693d43e7eSAnthony Xu /* If vector is 0, the msi is remapped into a pirq, passed as
8793d43e7eSAnthony Xu * dest_id.
8893d43e7eSAnthony Xu */
8993d43e7eSAnthony Xu return ((msi_data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT) == 0;
9093d43e7eSAnthony Xu }
9193d43e7eSAnthony Xu
xen_hvm_inject_msi(uint64_t addr,uint32_t data)9293d43e7eSAnthony Xu void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
9393d43e7eSAnthony Xu {
9493d43e7eSAnthony Xu xen_inject_msi(xen_domid, addr, data);
9593d43e7eSAnthony Xu }
9693d43e7eSAnthony Xu
xen_suspend_notifier(Notifier * notifier,void * data)9793d43e7eSAnthony Xu static void xen_suspend_notifier(Notifier *notifier, void *data)
9893d43e7eSAnthony Xu {
9993d43e7eSAnthony Xu xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
10093d43e7eSAnthony Xu }
10193d43e7eSAnthony Xu
10293d43e7eSAnthony Xu /* Xen Interrupt Controller */
10393d43e7eSAnthony Xu
xen_set_irq(void * opaque,int irq,int level)10493d43e7eSAnthony Xu static void xen_set_irq(void *opaque, int irq, int level)
10593d43e7eSAnthony Xu {
10693d43e7eSAnthony Xu xen_set_isa_irq_level(xen_domid, irq, level);
10793d43e7eSAnthony Xu }
10893d43e7eSAnthony Xu
xen_interrupt_controller_init(void)10993d43e7eSAnthony Xu qemu_irq *xen_interrupt_controller_init(void)
11093d43e7eSAnthony Xu {
11193d43e7eSAnthony Xu return qemu_allocate_irqs(xen_set_irq, NULL, 16);
11293d43e7eSAnthony Xu }
11393d43e7eSAnthony Xu
11493d43e7eSAnthony Xu /* Memory Ops */
11593d43e7eSAnthony Xu
xen_ram_init(PCMachineState * pcms,ram_addr_t ram_size,MemoryRegion ** ram_memory_p)11693d43e7eSAnthony Xu static void xen_ram_init(PCMachineState *pcms,
11793d43e7eSAnthony Xu ram_addr_t ram_size, MemoryRegion **ram_memory_p)
11893d43e7eSAnthony Xu {
119f0bb276bSPaolo Bonzini X86MachineState *x86ms = X86_MACHINE(pcms);
12093d43e7eSAnthony Xu MemoryRegion *sysmem = get_system_memory();
12193d43e7eSAnthony Xu ram_addr_t block_len;
122f0bb276bSPaolo Bonzini uint64_t user_lowmem =
123f0bb276bSPaolo Bonzini object_property_get_uint(qdev_get_machine(),
1249a45729dSGerd Hoffmann PC_MACHINE_MAX_RAM_BELOW_4G,
12593d43e7eSAnthony Xu &error_abort);
12693d43e7eSAnthony Xu
12793d43e7eSAnthony Xu /* Handle the machine opt max-ram-below-4g. It is basically doing
12893d43e7eSAnthony Xu * min(xen limit, user limit).
12993d43e7eSAnthony Xu */
13093d43e7eSAnthony Xu if (!user_lowmem) {
13193d43e7eSAnthony Xu user_lowmem = HVM_BELOW_4G_RAM_END; /* default */
13293d43e7eSAnthony Xu }
13393d43e7eSAnthony Xu if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
13493d43e7eSAnthony Xu user_lowmem = HVM_BELOW_4G_RAM_END;
13593d43e7eSAnthony Xu }
13693d43e7eSAnthony Xu
13793d43e7eSAnthony Xu if (ram_size >= user_lowmem) {
138f0bb276bSPaolo Bonzini x86ms->above_4g_mem_size = ram_size - user_lowmem;
139f0bb276bSPaolo Bonzini x86ms->below_4g_mem_size = user_lowmem;
14093d43e7eSAnthony Xu } else {
141f0bb276bSPaolo Bonzini x86ms->above_4g_mem_size = 0;
142f0bb276bSPaolo Bonzini x86ms->below_4g_mem_size = ram_size;
14393d43e7eSAnthony Xu }
144f0bb276bSPaolo Bonzini if (!x86ms->above_4g_mem_size) {
14593d43e7eSAnthony Xu block_len = ram_size;
14693d43e7eSAnthony Xu } else {
14793d43e7eSAnthony Xu /*
14893d43e7eSAnthony Xu * Xen does not allocate the memory continuously, it keeps a
14993d43e7eSAnthony Xu * hole of the size computed above or passed in.
15093d43e7eSAnthony Xu */
151039a93b0SPhilippe Mathieu-Daudé block_len = (4 * GiB) + x86ms->above_4g_mem_size;
15293d43e7eSAnthony Xu }
153b934c3faSPhilippe Mathieu-Daudé memory_region_init_ram(&xen_memory, NULL, "xen.ram", block_len,
15493d43e7eSAnthony Xu &error_fatal);
155b934c3faSPhilippe Mathieu-Daudé *ram_memory_p = &xen_memory;
15693d43e7eSAnthony Xu
15793d43e7eSAnthony Xu memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
158b934c3faSPhilippe Mathieu-Daudé &xen_memory, 0, 0xa0000);
15993d43e7eSAnthony Xu memory_region_add_subregion(sysmem, 0, &ram_640k);
16093d43e7eSAnthony Xu /* Skip of the VGA IO memory space, it will be registered later by the VGA
16193d43e7eSAnthony Xu * emulated device.
16293d43e7eSAnthony Xu *
16393d43e7eSAnthony Xu * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
16493d43e7eSAnthony Xu * the Options ROM, so it is registered here as RAM.
16593d43e7eSAnthony Xu */
16693d43e7eSAnthony Xu memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
167b934c3faSPhilippe Mathieu-Daudé &xen_memory, 0xc0000,
168f0bb276bSPaolo Bonzini x86ms->below_4g_mem_size - 0xc0000);
16993d43e7eSAnthony Xu memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
170f0bb276bSPaolo Bonzini if (x86ms->above_4g_mem_size > 0) {
17193d43e7eSAnthony Xu memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
172b934c3faSPhilippe Mathieu-Daudé &xen_memory, 0x100000000ULL,
173f0bb276bSPaolo Bonzini x86ms->above_4g_mem_size);
17493d43e7eSAnthony Xu memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
17593d43e7eSAnthony Xu }
17693d43e7eSAnthony Xu }
17793d43e7eSAnthony Xu
get_physmapping(hwaddr start_addr,ram_addr_t size,int page_mask)1788ebb8682SPhilippe Mathieu-Daudé static XenPhysmap *get_physmapping(hwaddr start_addr, ram_addr_t size,
1798ebb8682SPhilippe Mathieu-Daudé int page_mask)
18093d43e7eSAnthony Xu {
18193d43e7eSAnthony Xu XenPhysmap *physmap = NULL;
18293d43e7eSAnthony Xu
1838ebb8682SPhilippe Mathieu-Daudé start_addr &= page_mask;
18493d43e7eSAnthony Xu
18504a8f72eSIgor Druzhinin QLIST_FOREACH(physmap, &xen_physmap, list) {
18693d43e7eSAnthony Xu if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
18793d43e7eSAnthony Xu return physmap;
18893d43e7eSAnthony Xu }
18993d43e7eSAnthony Xu }
19093d43e7eSAnthony Xu return NULL;
19193d43e7eSAnthony Xu }
19293d43e7eSAnthony Xu
xen_phys_offset_to_gaddr(hwaddr phys_offset,ram_addr_t size,int page_mask)1938ebb8682SPhilippe Mathieu-Daudé static hwaddr xen_phys_offset_to_gaddr(hwaddr phys_offset, ram_addr_t size,
1948ebb8682SPhilippe Mathieu-Daudé int page_mask)
19593d43e7eSAnthony Xu {
1968ebb8682SPhilippe Mathieu-Daudé hwaddr addr = phys_offset & page_mask;
19793d43e7eSAnthony Xu XenPhysmap *physmap = NULL;
19893d43e7eSAnthony Xu
19904a8f72eSIgor Druzhinin QLIST_FOREACH(physmap, &xen_physmap, list) {
20093d43e7eSAnthony Xu if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
20104a8f72eSIgor Druzhinin return physmap->start_addr + (phys_offset - physmap->phys_offset);
20293d43e7eSAnthony Xu }
20393d43e7eSAnthony Xu }
20493d43e7eSAnthony Xu
20504a8f72eSIgor Druzhinin return phys_offset;
20693d43e7eSAnthony Xu }
20793d43e7eSAnthony Xu
20804a8f72eSIgor Druzhinin #ifdef XEN_COMPAT_PHYSMAP
xen_save_physmap(XenIOState * state,XenPhysmap * physmap)209697b66d0SIgor Druzhinin static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
210697b66d0SIgor Druzhinin {
211697b66d0SIgor Druzhinin char path[80], value[17];
212697b66d0SIgor Druzhinin
213697b66d0SIgor Druzhinin snprintf(path, sizeof(path),
214697b66d0SIgor Druzhinin "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
215697b66d0SIgor Druzhinin xen_domid, (uint64_t)physmap->phys_offset);
216697b66d0SIgor Druzhinin snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->start_addr);
217697b66d0SIgor Druzhinin if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
218697b66d0SIgor Druzhinin return -1;
219697b66d0SIgor Druzhinin }
220697b66d0SIgor Druzhinin snprintf(path, sizeof(path),
221697b66d0SIgor Druzhinin "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
222697b66d0SIgor Druzhinin xen_domid, (uint64_t)physmap->phys_offset);
223697b66d0SIgor Druzhinin snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->size);
224697b66d0SIgor Druzhinin if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
225697b66d0SIgor Druzhinin return -1;
226697b66d0SIgor Druzhinin }
227697b66d0SIgor Druzhinin if (physmap->name) {
228697b66d0SIgor Druzhinin snprintf(path, sizeof(path),
229697b66d0SIgor Druzhinin "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
230697b66d0SIgor Druzhinin xen_domid, (uint64_t)physmap->phys_offset);
231697b66d0SIgor Druzhinin if (!xs_write(state->xenstore, 0, path,
232697b66d0SIgor Druzhinin physmap->name, strlen(physmap->name))) {
233697b66d0SIgor Druzhinin return -1;
234697b66d0SIgor Druzhinin }
235697b66d0SIgor Druzhinin }
236697b66d0SIgor Druzhinin return 0;
237697b66d0SIgor Druzhinin }
238331b5189SIgor Druzhinin #else
xen_save_physmap(XenIOState * state,XenPhysmap * physmap)239331b5189SIgor Druzhinin static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
240331b5189SIgor Druzhinin {
241331b5189SIgor Druzhinin return 0;
242331b5189SIgor Druzhinin }
243331b5189SIgor Druzhinin #endif
244697b66d0SIgor Druzhinin
xen_add_to_physmap(XenIOState * state,hwaddr start_addr,ram_addr_t size,MemoryRegion * mr,hwaddr offset_within_region)24593d43e7eSAnthony Xu static int xen_add_to_physmap(XenIOState *state,
24693d43e7eSAnthony Xu hwaddr start_addr,
24793d43e7eSAnthony Xu ram_addr_t size,
24893d43e7eSAnthony Xu MemoryRegion *mr,
24993d43e7eSAnthony Xu hwaddr offset_within_region)
25093d43e7eSAnthony Xu {
251825f292dSPhilippe Mathieu-Daudé unsigned target_page_bits = qemu_target_page_bits();
252825f292dSPhilippe Mathieu-Daudé int page_size = qemu_target_page_size();
253825f292dSPhilippe Mathieu-Daudé int page_mask = -page_size;
2542cbf8903SRoss Lagerwall unsigned long nr_pages;
25593d43e7eSAnthony Xu int rc = 0;
25693d43e7eSAnthony Xu XenPhysmap *physmap = NULL;
25793d43e7eSAnthony Xu hwaddr pfn, start_gpfn;
25893d43e7eSAnthony Xu hwaddr phys_offset = memory_region_get_ram_addr(mr);
25993d43e7eSAnthony Xu const char *mr_name;
26093d43e7eSAnthony Xu
261825f292dSPhilippe Mathieu-Daudé if (get_physmapping(start_addr, size, page_mask)) {
26293d43e7eSAnthony Xu return 0;
26393d43e7eSAnthony Xu }
26493d43e7eSAnthony Xu if (size <= 0) {
26593d43e7eSAnthony Xu return -1;
26693d43e7eSAnthony Xu }
26793d43e7eSAnthony Xu
26893d43e7eSAnthony Xu /* Xen can only handle a single dirty log region for now and we want
26993d43e7eSAnthony Xu * the linear framebuffer to be that region.
27093d43e7eSAnthony Xu * Avoid tracking any regions that is not videoram and avoid tracking
27193d43e7eSAnthony Xu * the legacy vga region. */
27293d43e7eSAnthony Xu if (mr == framebuffer && start_addr > 0xbffff) {
27393d43e7eSAnthony Xu goto go_physmap;
27493d43e7eSAnthony Xu }
27593d43e7eSAnthony Xu return -1;
27693d43e7eSAnthony Xu
27793d43e7eSAnthony Xu go_physmap:
27893d43e7eSAnthony Xu DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
27993d43e7eSAnthony Xu start_addr, start_addr + size);
28093d43e7eSAnthony Xu
281331b5189SIgor Druzhinin mr_name = memory_region_name(mr);
282331b5189SIgor Druzhinin
283b21e2380SMarkus Armbruster physmap = g_new(XenPhysmap, 1);
284331b5189SIgor Druzhinin
285331b5189SIgor Druzhinin physmap->start_addr = start_addr;
286331b5189SIgor Druzhinin physmap->size = size;
287331b5189SIgor Druzhinin physmap->name = mr_name;
288331b5189SIgor Druzhinin physmap->phys_offset = phys_offset;
289331b5189SIgor Druzhinin
29004a8f72eSIgor Druzhinin QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
291331b5189SIgor Druzhinin
292331b5189SIgor Druzhinin if (runstate_check(RUN_STATE_INMIGRATE)) {
293331b5189SIgor Druzhinin /* Now when we have a physmap entry we can replace a dummy mapping with
294331b5189SIgor Druzhinin * a real one of guest foreign memory. */
295331b5189SIgor Druzhinin uint8_t *p = xen_replace_cache_entry(phys_offset, start_addr, size);
296331b5189SIgor Druzhinin assert(p && p == memory_region_get_ram_ptr(mr));
297331b5189SIgor Druzhinin
298331b5189SIgor Druzhinin return 0;
299331b5189SIgor Druzhinin }
300331b5189SIgor Druzhinin
301825f292dSPhilippe Mathieu-Daudé pfn = phys_offset >> target_page_bits;
302825f292dSPhilippe Mathieu-Daudé start_gpfn = start_addr >> target_page_bits;
303825f292dSPhilippe Mathieu-Daudé nr_pages = size >> target_page_bits;
3042cbf8903SRoss Lagerwall rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, nr_pages, pfn,
3052cbf8903SRoss Lagerwall start_gpfn);
30693d43e7eSAnthony Xu if (rc) {
3072cbf8903SRoss Lagerwall int saved_errno = errno;
3082cbf8903SRoss Lagerwall
3092cbf8903SRoss Lagerwall error_report("relocate_memory %lu pages from GFN %"HWADDR_PRIx
3102cbf8903SRoss Lagerwall " to GFN %"HWADDR_PRIx" failed: %s",
3112cbf8903SRoss Lagerwall nr_pages, pfn, start_gpfn, strerror(saved_errno));
3122cbf8903SRoss Lagerwall errno = saved_errno;
3132cbf8903SRoss Lagerwall return -1;
31493d43e7eSAnthony Xu }
31593d43e7eSAnthony Xu
3162cbf8903SRoss Lagerwall rc = xendevicemodel_pin_memory_cacheattr(xen_dmod, xen_domid,
317825f292dSPhilippe Mathieu-Daudé start_addr >> target_page_bits,
318825f292dSPhilippe Mathieu-Daudé (start_addr + size - 1) >> target_page_bits,
31993d43e7eSAnthony Xu XEN_DOMCTL_MEM_CACHEATTR_WB);
3202cbf8903SRoss Lagerwall if (rc) {
3212cbf8903SRoss Lagerwall error_report("pin_memory_cacheattr failed: %s", strerror(errno));
3222cbf8903SRoss Lagerwall }
323697b66d0SIgor Druzhinin return xen_save_physmap(state, physmap);
32493d43e7eSAnthony Xu }
32593d43e7eSAnthony Xu
xen_remove_from_physmap(XenIOState * state,hwaddr start_addr,ram_addr_t size)32693d43e7eSAnthony Xu static int xen_remove_from_physmap(XenIOState *state,
32793d43e7eSAnthony Xu hwaddr start_addr,
32893d43e7eSAnthony Xu ram_addr_t size)
32993d43e7eSAnthony Xu {
330825f292dSPhilippe Mathieu-Daudé unsigned target_page_bits = qemu_target_page_bits();
331825f292dSPhilippe Mathieu-Daudé int page_size = qemu_target_page_size();
332825f292dSPhilippe Mathieu-Daudé int page_mask = -page_size;
33393d43e7eSAnthony Xu int rc = 0;
33493d43e7eSAnthony Xu XenPhysmap *physmap = NULL;
33593d43e7eSAnthony Xu hwaddr phys_offset = 0;
33693d43e7eSAnthony Xu
337825f292dSPhilippe Mathieu-Daudé physmap = get_physmapping(start_addr, size, page_mask);
33893d43e7eSAnthony Xu if (physmap == NULL) {
33993d43e7eSAnthony Xu return -1;
34093d43e7eSAnthony Xu }
34193d43e7eSAnthony Xu
34293d43e7eSAnthony Xu phys_offset = physmap->phys_offset;
34393d43e7eSAnthony Xu size = physmap->size;
34493d43e7eSAnthony Xu
34593d43e7eSAnthony Xu DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
34693d43e7eSAnthony Xu "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
34793d43e7eSAnthony Xu
348825f292dSPhilippe Mathieu-Daudé size >>= target_page_bits;
349825f292dSPhilippe Mathieu-Daudé start_addr >>= target_page_bits;
350825f292dSPhilippe Mathieu-Daudé phys_offset >>= target_page_bits;
3512cbf8903SRoss Lagerwall rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, size, start_addr,
3522cbf8903SRoss Lagerwall phys_offset);
35393d43e7eSAnthony Xu if (rc) {
3542cbf8903SRoss Lagerwall int saved_errno = errno;
3552cbf8903SRoss Lagerwall
3562cbf8903SRoss Lagerwall error_report("relocate_memory "RAM_ADDR_FMT" pages"
3572cbf8903SRoss Lagerwall " from GFN %"HWADDR_PRIx
3582cbf8903SRoss Lagerwall " to GFN %"HWADDR_PRIx" failed: %s",
3592cbf8903SRoss Lagerwall size, start_addr, phys_offset, strerror(saved_errno));
3602cbf8903SRoss Lagerwall errno = saved_errno;
3612cbf8903SRoss Lagerwall return -1;
36293d43e7eSAnthony Xu }
36393d43e7eSAnthony Xu
36493d43e7eSAnthony Xu QLIST_REMOVE(physmap, list);
3659269b9d1SStefano Stabellini if (log_for_dirtybit == physmap) {
3669269b9d1SStefano Stabellini log_for_dirtybit = NULL;
3679269b9d1SStefano Stabellini g_free(dirty_bitmap);
3689269b9d1SStefano Stabellini dirty_bitmap = NULL;
36993d43e7eSAnthony Xu }
37093d43e7eSAnthony Xu g_free(physmap);
37193d43e7eSAnthony Xu
37293d43e7eSAnthony Xu return 0;
37393d43e7eSAnthony Xu }
37493d43e7eSAnthony Xu
xen_sync_dirty_bitmap(XenIOState * state,hwaddr start_addr,ram_addr_t size)37593d43e7eSAnthony Xu static void xen_sync_dirty_bitmap(XenIOState *state,
37693d43e7eSAnthony Xu hwaddr start_addr,
37793d43e7eSAnthony Xu ram_addr_t size)
37893d43e7eSAnthony Xu {
379825f292dSPhilippe Mathieu-Daudé unsigned target_page_bits = qemu_target_page_bits();
380825f292dSPhilippe Mathieu-Daudé int page_size = qemu_target_page_size();
381825f292dSPhilippe Mathieu-Daudé int page_mask = -page_size;
382825f292dSPhilippe Mathieu-Daudé hwaddr npages = size >> target_page_bits;
38393d43e7eSAnthony Xu const int width = sizeof(unsigned long) * 8;
38434fbbc16SAnthony PERARD size_t bitmap_size = DIV_ROUND_UP(npages, width);
38593d43e7eSAnthony Xu int rc, i, j;
38693d43e7eSAnthony Xu const XenPhysmap *physmap = NULL;
38793d43e7eSAnthony Xu
388825f292dSPhilippe Mathieu-Daudé physmap = get_physmapping(start_addr, size, page_mask);
38993d43e7eSAnthony Xu if (physmap == NULL) {
39093d43e7eSAnthony Xu /* not handled */
39193d43e7eSAnthony Xu return;
39293d43e7eSAnthony Xu }
39393d43e7eSAnthony Xu
3949269b9d1SStefano Stabellini if (log_for_dirtybit == NULL) {
3959269b9d1SStefano Stabellini log_for_dirtybit = physmap;
3969269b9d1SStefano Stabellini dirty_bitmap = g_new(unsigned long, bitmap_size);
3979269b9d1SStefano Stabellini } else if (log_for_dirtybit != physmap) {
39893d43e7eSAnthony Xu /* Only one range for dirty bitmap can be tracked. */
39993d43e7eSAnthony Xu return;
40093d43e7eSAnthony Xu }
40193d43e7eSAnthony Xu
402825f292dSPhilippe Mathieu-Daudé rc = xen_track_dirty_vram(xen_domid, start_addr >> target_page_bits,
4039269b9d1SStefano Stabellini npages, dirty_bitmap);
40493d43e7eSAnthony Xu if (rc < 0) {
40593d43e7eSAnthony Xu #ifndef ENODATA
40693d43e7eSAnthony Xu #define ENODATA ENOENT
40793d43e7eSAnthony Xu #endif
40893d43e7eSAnthony Xu if (errno == ENODATA) {
40993d43e7eSAnthony Xu memory_region_set_dirty(framebuffer, 0, size);
410883f2c59SPhilippe Mathieu-Daudé DPRINTF("xen: track_dirty_vram failed (0x" HWADDR_FMT_plx
411883f2c59SPhilippe Mathieu-Daudé ", 0x" HWADDR_FMT_plx "): %s\n",
41293d43e7eSAnthony Xu start_addr, start_addr + size, strerror(errno));
41393d43e7eSAnthony Xu }
41493d43e7eSAnthony Xu return;
41593d43e7eSAnthony Xu }
41693d43e7eSAnthony Xu
41734fbbc16SAnthony PERARD for (i = 0; i < bitmap_size; i++) {
4189269b9d1SStefano Stabellini unsigned long map = dirty_bitmap[i];
41993d43e7eSAnthony Xu while (map != 0) {
42093d43e7eSAnthony Xu j = ctzl(map);
42193d43e7eSAnthony Xu map &= ~(1ul << j);
42293d43e7eSAnthony Xu memory_region_set_dirty(framebuffer,
423825f292dSPhilippe Mathieu-Daudé (i * width + j) * page_size, page_size);
42493d43e7eSAnthony Xu };
42593d43e7eSAnthony Xu }
42693d43e7eSAnthony Xu }
42793d43e7eSAnthony Xu
xen_log_start(MemoryListener * listener,MemoryRegionSection * section,int old,int new)42893d43e7eSAnthony Xu static void xen_log_start(MemoryListener *listener,
42993d43e7eSAnthony Xu MemoryRegionSection *section,
43093d43e7eSAnthony Xu int old, int new)
43193d43e7eSAnthony Xu {
43293d43e7eSAnthony Xu XenIOState *state = container_of(listener, XenIOState, memory_listener);
43393d43e7eSAnthony Xu
43493d43e7eSAnthony Xu if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
43593d43e7eSAnthony Xu xen_sync_dirty_bitmap(state, section->offset_within_address_space,
43693d43e7eSAnthony Xu int128_get64(section->size));
43793d43e7eSAnthony Xu }
43893d43e7eSAnthony Xu }
43993d43e7eSAnthony Xu
xen_log_stop(MemoryListener * listener,MemoryRegionSection * section,int old,int new)44093d43e7eSAnthony Xu static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
44193d43e7eSAnthony Xu int old, int new)
44293d43e7eSAnthony Xu {
44393d43e7eSAnthony Xu if (old & ~new & (1 << DIRTY_MEMORY_VGA)) {
4449269b9d1SStefano Stabellini log_for_dirtybit = NULL;
4459269b9d1SStefano Stabellini g_free(dirty_bitmap);
4469269b9d1SStefano Stabellini dirty_bitmap = NULL;
44793d43e7eSAnthony Xu /* Disable dirty bit tracking */
44893d43e7eSAnthony Xu xen_track_dirty_vram(xen_domid, 0, 0, NULL);
44993d43e7eSAnthony Xu }
45093d43e7eSAnthony Xu }
45193d43e7eSAnthony Xu
xen_log_sync(MemoryListener * listener,MemoryRegionSection * section)45293d43e7eSAnthony Xu static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
45393d43e7eSAnthony Xu {
45493d43e7eSAnthony Xu XenIOState *state = container_of(listener, XenIOState, memory_listener);
45593d43e7eSAnthony Xu
45693d43e7eSAnthony Xu xen_sync_dirty_bitmap(state, section->offset_within_address_space,
45793d43e7eSAnthony Xu int128_get64(section->size));
45893d43e7eSAnthony Xu }
45993d43e7eSAnthony Xu
xen_log_global_start(MemoryListener * listener,Error ** errp)4603688fec8SCédric Le Goater static bool xen_log_global_start(MemoryListener *listener, Error **errp)
46193d43e7eSAnthony Xu {
46293d43e7eSAnthony Xu if (xen_enabled()) {
46393d43e7eSAnthony Xu xen_in_migration = true;
46493d43e7eSAnthony Xu }
4653688fec8SCédric Le Goater return true;
46693d43e7eSAnthony Xu }
46793d43e7eSAnthony Xu
xen_log_global_stop(MemoryListener * listener)46893d43e7eSAnthony Xu static void xen_log_global_stop(MemoryListener *listener)
46993d43e7eSAnthony Xu {
47093d43e7eSAnthony Xu xen_in_migration = false;
47193d43e7eSAnthony Xu }
47293d43e7eSAnthony Xu
473bcb40db0SPeter Maydell static const MemoryListener xen_memory_listener = {
474142518bdSPeter Xu .name = "xen-memory",
47593d43e7eSAnthony Xu .region_add = xen_region_add,
47693d43e7eSAnthony Xu .region_del = xen_region_del,
47793d43e7eSAnthony Xu .log_start = xen_log_start,
47893d43e7eSAnthony Xu .log_stop = xen_log_stop,
47993d43e7eSAnthony Xu .log_sync = xen_log_sync,
48093d43e7eSAnthony Xu .log_global_start = xen_log_global_start,
48193d43e7eSAnthony Xu .log_global_stop = xen_log_global_stop,
4825369a36cSIsaku Yamahata .priority = MEMORY_LISTENER_PRIORITY_ACCEL,
48393d43e7eSAnthony Xu };
48493d43e7eSAnthony Xu
regs_to_cpu(vmware_regs_t * vmport_regs,ioreq_t * req)48593d43e7eSAnthony Xu static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
48693d43e7eSAnthony Xu {
48793d43e7eSAnthony Xu X86CPU *cpu;
48893d43e7eSAnthony Xu CPUX86State *env;
48993d43e7eSAnthony Xu
49093d43e7eSAnthony Xu cpu = X86_CPU(current_cpu);
49193d43e7eSAnthony Xu env = &cpu->env;
49293d43e7eSAnthony Xu env->regs[R_EAX] = req->data;
49393d43e7eSAnthony Xu env->regs[R_EBX] = vmport_regs->ebx;
49493d43e7eSAnthony Xu env->regs[R_ECX] = vmport_regs->ecx;
49593d43e7eSAnthony Xu env->regs[R_EDX] = vmport_regs->edx;
49693d43e7eSAnthony Xu env->regs[R_ESI] = vmport_regs->esi;
49793d43e7eSAnthony Xu env->regs[R_EDI] = vmport_regs->edi;
49893d43e7eSAnthony Xu }
49993d43e7eSAnthony Xu
regs_from_cpu(vmware_regs_t * vmport_regs)50093d43e7eSAnthony Xu static void regs_from_cpu(vmware_regs_t *vmport_regs)
50193d43e7eSAnthony Xu {
50293d43e7eSAnthony Xu X86CPU *cpu = X86_CPU(current_cpu);
50393d43e7eSAnthony Xu CPUX86State *env = &cpu->env;
50493d43e7eSAnthony Xu
50593d43e7eSAnthony Xu vmport_regs->ebx = env->regs[R_EBX];
50693d43e7eSAnthony Xu vmport_regs->ecx = env->regs[R_ECX];
50793d43e7eSAnthony Xu vmport_regs->edx = env->regs[R_EDX];
50893d43e7eSAnthony Xu vmport_regs->esi = env->regs[R_ESI];
50993d43e7eSAnthony Xu vmport_regs->edi = env->regs[R_EDI];
51093d43e7eSAnthony Xu }
51193d43e7eSAnthony Xu
handle_vmport_ioreq(XenIOState * state,ioreq_t * req)51293d43e7eSAnthony Xu static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
51393d43e7eSAnthony Xu {
51493d43e7eSAnthony Xu vmware_regs_t *vmport_regs;
51593d43e7eSAnthony Xu
5169269b9d1SStefano Stabellini assert(shared_vmport_page);
51793d43e7eSAnthony Xu vmport_regs =
5189269b9d1SStefano Stabellini &shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
51993d43e7eSAnthony Xu QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
52093d43e7eSAnthony Xu
52193d43e7eSAnthony Xu current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
52293d43e7eSAnthony Xu regs_to_cpu(vmport_regs, req);
52393d43e7eSAnthony Xu cpu_ioreq_pio(req);
52493d43e7eSAnthony Xu regs_from_cpu(vmport_regs);
52593d43e7eSAnthony Xu current_cpu = NULL;
52693d43e7eSAnthony Xu }
52793d43e7eSAnthony Xu
528331b5189SIgor Druzhinin #ifdef XEN_COMPAT_PHYSMAP
xen_read_physmap(XenIOState * state)52993d43e7eSAnthony Xu static void xen_read_physmap(XenIOState *state)
53093d43e7eSAnthony Xu {
53193d43e7eSAnthony Xu XenPhysmap *physmap = NULL;
53293d43e7eSAnthony Xu unsigned int len, num, i;
53393d43e7eSAnthony Xu char path[80], *value = NULL;
53493d43e7eSAnthony Xu char **entries = NULL;
53593d43e7eSAnthony Xu
53693d43e7eSAnthony Xu snprintf(path, sizeof(path),
53793d43e7eSAnthony Xu "/local/domain/0/device-model/%d/physmap", xen_domid);
53893d43e7eSAnthony Xu entries = xs_directory(state->xenstore, 0, path, &num);
53993d43e7eSAnthony Xu if (entries == NULL)
54093d43e7eSAnthony Xu return;
54193d43e7eSAnthony Xu
54293d43e7eSAnthony Xu for (i = 0; i < num; i++) {
543b21e2380SMarkus Armbruster physmap = g_new(XenPhysmap, 1);
54493d43e7eSAnthony Xu physmap->phys_offset = strtoull(entries[i], NULL, 16);
54593d43e7eSAnthony Xu snprintf(path, sizeof(path),
54693d43e7eSAnthony Xu "/local/domain/0/device-model/%d/physmap/%s/start_addr",
54793d43e7eSAnthony Xu xen_domid, entries[i]);
54893d43e7eSAnthony Xu value = xs_read(state->xenstore, 0, path, &len);
54993d43e7eSAnthony Xu if (value == NULL) {
55093d43e7eSAnthony Xu g_free(physmap);
55193d43e7eSAnthony Xu continue;
55293d43e7eSAnthony Xu }
55393d43e7eSAnthony Xu physmap->start_addr = strtoull(value, NULL, 16);
55493d43e7eSAnthony Xu free(value);
55593d43e7eSAnthony Xu
55693d43e7eSAnthony Xu snprintf(path, sizeof(path),
55793d43e7eSAnthony Xu "/local/domain/0/device-model/%d/physmap/%s/size",
55893d43e7eSAnthony Xu xen_domid, entries[i]);
55993d43e7eSAnthony Xu value = xs_read(state->xenstore, 0, path, &len);
56093d43e7eSAnthony Xu if (value == NULL) {
56193d43e7eSAnthony Xu g_free(physmap);
56293d43e7eSAnthony Xu continue;
56393d43e7eSAnthony Xu }
56493d43e7eSAnthony Xu physmap->size = strtoull(value, NULL, 16);
56593d43e7eSAnthony Xu free(value);
56693d43e7eSAnthony Xu
56793d43e7eSAnthony Xu snprintf(path, sizeof(path),
56893d43e7eSAnthony Xu "/local/domain/0/device-model/%d/physmap/%s/name",
56993d43e7eSAnthony Xu xen_domid, entries[i]);
57093d43e7eSAnthony Xu physmap->name = xs_read(state->xenstore, 0, path, &len);
57193d43e7eSAnthony Xu
57204a8f72eSIgor Druzhinin QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
57393d43e7eSAnthony Xu }
57493d43e7eSAnthony Xu free(entries);
57593d43e7eSAnthony Xu }
576331b5189SIgor Druzhinin #else
xen_read_physmap(XenIOState * state)577331b5189SIgor Druzhinin static void xen_read_physmap(XenIOState *state)
578331b5189SIgor Druzhinin {
579331b5189SIgor Druzhinin }
580331b5189SIgor Druzhinin #endif
58193d43e7eSAnthony Xu
xen_wakeup_notifier(Notifier * notifier,void * data)58293d43e7eSAnthony Xu static void xen_wakeup_notifier(Notifier *notifier, void *data)
58393d43e7eSAnthony Xu {
58493d43e7eSAnthony Xu xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
58593d43e7eSAnthony Xu }
58693d43e7eSAnthony Xu
xen_check_stubdomain(struct xs_handle * xsh)587b2bbadc6SMarek Marczykowski-Górecki static bool xen_check_stubdomain(struct xs_handle *xsh)
588b2bbadc6SMarek Marczykowski-Górecki {
589b2bbadc6SMarek Marczykowski-Górecki char *dm_path = g_strdup_printf(
590b2bbadc6SMarek Marczykowski-Górecki "/local/domain/%d/image/device-model-domid", xen_domid);
591b2bbadc6SMarek Marczykowski-Górecki char *val;
592b2bbadc6SMarek Marczykowski-Górecki int32_t dm_domid;
593b2bbadc6SMarek Marczykowski-Górecki bool is_stubdom = false;
594b2bbadc6SMarek Marczykowski-Górecki
595b2bbadc6SMarek Marczykowski-Górecki val = xs_read(xsh, 0, dm_path, NULL);
596b2bbadc6SMarek Marczykowski-Górecki if (val) {
597b2bbadc6SMarek Marczykowski-Górecki if (sscanf(val, "%d", &dm_domid) == 1) {
598b2bbadc6SMarek Marczykowski-Górecki is_stubdom = dm_domid != 0;
599b2bbadc6SMarek Marczykowski-Górecki }
600b2bbadc6SMarek Marczykowski-Górecki free(val);
601b2bbadc6SMarek Marczykowski-Górecki }
602b2bbadc6SMarek Marczykowski-Górecki
603b2bbadc6SMarek Marczykowski-Górecki g_free(dm_path);
604b2bbadc6SMarek Marczykowski-Górecki return is_stubdom;
605b2bbadc6SMarek Marczykowski-Górecki }
606b2bbadc6SMarek Marczykowski-Górecki
xen_hvm_init_pc(PCMachineState * pcms,MemoryRegion ** ram_memory)6075650ac00SPhilippe Mathieu-Daudé void xen_hvm_init_pc(PCMachineState *pcms, MemoryRegion **ram_memory)
60893d43e7eSAnthony Xu {
6090e11fc69SLike Xu MachineState *ms = MACHINE(pcms);
6100e11fc69SLike Xu unsigned int max_cpus = ms->smp.max_cpus;
611f17068c1SStefano Stabellini int rc;
61293d43e7eSAnthony Xu xen_pfn_t ioreq_pfn;
61393d43e7eSAnthony Xu XenIOState *state;
61493d43e7eSAnthony Xu
615b21e2380SMarkus Armbruster state = g_new0(XenIOState, 1);
61693d43e7eSAnthony Xu
617*b2150e40SEdgar E. Iglesias xen_register_ioreq(state, max_cpus,
618*b2150e40SEdgar E. Iglesias HVM_IOREQSRV_BUFIOREQ_ATOMIC,
619*b2150e40SEdgar E. Iglesias &xen_memory_listener);
62004a8f72eSIgor Druzhinin
621b2bbadc6SMarek Marczykowski-Górecki xen_is_stubdomain = xen_check_stubdomain(state->xenstore);
622b2bbadc6SMarek Marczykowski-Górecki
62304a8f72eSIgor Druzhinin QLIST_INIT(&xen_physmap);
62493d43e7eSAnthony Xu xen_read_physmap(state);
62593d43e7eSAnthony Xu
6269269b9d1SStefano Stabellini suspend.notify = xen_suspend_notifier;
6279269b9d1SStefano Stabellini qemu_register_suspend_notifier(&suspend);
62833087aacSVikram Garhwal
6299269b9d1SStefano Stabellini wakeup.notify = xen_wakeup_notifier;
6309269b9d1SStefano Stabellini qemu_register_wakeup_notifier(&wakeup);
63133087aacSVikram Garhwal
63233087aacSVikram Garhwal rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
63333087aacSVikram Garhwal if (!rc) {
63433087aacSVikram Garhwal DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
6359269b9d1SStefano Stabellini shared_vmport_page =
63633087aacSVikram Garhwal xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
63733087aacSVikram Garhwal 1, &ioreq_pfn, NULL);
6389269b9d1SStefano Stabellini if (shared_vmport_page == NULL) {
63933087aacSVikram Garhwal error_report("map shared vmport IO page returned error %d handle=%p",
64033087aacSVikram Garhwal errno, xen_xc);
64133087aacSVikram Garhwal goto err;
64233087aacSVikram Garhwal }
64333087aacSVikram Garhwal } else if (rc != -ENOSYS) {
64433087aacSVikram Garhwal error_report("get vmport regs pfn returned error %d, rc=%d",
64533087aacSVikram Garhwal errno, rc);
64633087aacSVikram Garhwal goto err;
64733087aacSVikram Garhwal }
64833087aacSVikram Garhwal
64933087aacSVikram Garhwal xen_ram_init(pcms, ms->ram_size, ram_memory);
65033087aacSVikram Garhwal
65193d43e7eSAnthony Xu /* Disable ACPI build because Xen handles it */
65293d43e7eSAnthony Xu pcms->acpi_build_enabled = false;
65393d43e7eSAnthony Xu
65493d43e7eSAnthony Xu return;
65593d43e7eSAnthony Xu
65693d43e7eSAnthony Xu err:
65793d43e7eSAnthony Xu error_report("xen hardware virtual machine initialisation failed");
65893d43e7eSAnthony Xu exit(1);
65993d43e7eSAnthony Xu }
66093d43e7eSAnthony Xu
xen_register_framebuffer(MemoryRegion * mr)66193d43e7eSAnthony Xu void xen_register_framebuffer(MemoryRegion *mr)
66293d43e7eSAnthony Xu {
66393d43e7eSAnthony Xu framebuffer = mr;
66493d43e7eSAnthony Xu }
66593d43e7eSAnthony Xu
xen_hvm_modified_memory(ram_addr_t start,ram_addr_t length)66693d43e7eSAnthony Xu void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length)
66793d43e7eSAnthony Xu {
668825f292dSPhilippe Mathieu-Daudé unsigned target_page_bits = qemu_target_page_bits();
669825f292dSPhilippe Mathieu-Daudé int page_size = qemu_target_page_size();
670825f292dSPhilippe Mathieu-Daudé int page_mask = -page_size;
671825f292dSPhilippe Mathieu-Daudé
67293d43e7eSAnthony Xu if (unlikely(xen_in_migration)) {
67393d43e7eSAnthony Xu int rc;
67493d43e7eSAnthony Xu ram_addr_t start_pfn, nb_pages;
67593d43e7eSAnthony Xu
676825f292dSPhilippe Mathieu-Daudé start = xen_phys_offset_to_gaddr(start, length, page_mask);
67704a8f72eSIgor Druzhinin
67893d43e7eSAnthony Xu if (length == 0) {
679825f292dSPhilippe Mathieu-Daudé length = page_size;
68093d43e7eSAnthony Xu }
681825f292dSPhilippe Mathieu-Daudé start_pfn = start >> target_page_bits;
682825f292dSPhilippe Mathieu-Daudé nb_pages = ((start + length + page_size - 1) >> target_page_bits)
68393d43e7eSAnthony Xu - start_pfn;
68493d43e7eSAnthony Xu rc = xen_modified_memory(xen_domid, start_pfn, nb_pages);
68593d43e7eSAnthony Xu if (rc) {
68693d43e7eSAnthony Xu fprintf(stderr,
68793d43e7eSAnthony Xu "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
6887cdcca72SRoss Lagerwall __func__, start, nb_pages, errno, strerror(errno));
68993d43e7eSAnthony Xu }
69093d43e7eSAnthony Xu }
69193d43e7eSAnthony Xu }
69293d43e7eSAnthony Xu
qmp_xen_set_global_dirty_log(bool enable,Error ** errp)69393d43e7eSAnthony Xu void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
69493d43e7eSAnthony Xu {
69593d43e7eSAnthony Xu if (enable) {
696639ec3fbSCédric Le Goater memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION, errp);
69793d43e7eSAnthony Xu } else {
69863b41db4SHyman Huang(黄勇) memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION);
69993d43e7eSAnthony Xu }
70093d43e7eSAnthony Xu }
701f17068c1SStefano Stabellini
arch_xen_set_memory(XenIOState * state,MemoryRegionSection * section,bool add)702f17068c1SStefano Stabellini void arch_xen_set_memory(XenIOState *state, MemoryRegionSection *section,
703f17068c1SStefano Stabellini bool add)
704f17068c1SStefano Stabellini {
705825f292dSPhilippe Mathieu-Daudé unsigned target_page_bits = qemu_target_page_bits();
706825f292dSPhilippe Mathieu-Daudé int page_size = qemu_target_page_size();
707825f292dSPhilippe Mathieu-Daudé int page_mask = -page_size;
708f17068c1SStefano Stabellini hwaddr start_addr = section->offset_within_address_space;
709f17068c1SStefano Stabellini ram_addr_t size = int128_get64(section->size);
710f17068c1SStefano Stabellini bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA);
711f17068c1SStefano Stabellini hvmmem_type_t mem_type;
712f17068c1SStefano Stabellini
713f17068c1SStefano Stabellini if (!memory_region_is_ram(section->mr)) {
714f17068c1SStefano Stabellini return;
715f17068c1SStefano Stabellini }
716f17068c1SStefano Stabellini
717f17068c1SStefano Stabellini if (log_dirty != add) {
718f17068c1SStefano Stabellini return;
719f17068c1SStefano Stabellini }
720f17068c1SStefano Stabellini
721f17068c1SStefano Stabellini trace_xen_client_set_memory(start_addr, size, log_dirty);
722f17068c1SStefano Stabellini
723825f292dSPhilippe Mathieu-Daudé start_addr &= page_mask;
724825f292dSPhilippe Mathieu-Daudé size = ROUND_UP(size, page_size);
725f17068c1SStefano Stabellini
726f17068c1SStefano Stabellini if (add) {
727f17068c1SStefano Stabellini if (!memory_region_is_rom(section->mr)) {
728f17068c1SStefano Stabellini xen_add_to_physmap(state, start_addr, size,
729f17068c1SStefano Stabellini section->mr, section->offset_within_region);
730f17068c1SStefano Stabellini } else {
731f17068c1SStefano Stabellini mem_type = HVMMEM_ram_ro;
732f17068c1SStefano Stabellini if (xen_set_mem_type(xen_domid, mem_type,
733825f292dSPhilippe Mathieu-Daudé start_addr >> target_page_bits,
734825f292dSPhilippe Mathieu-Daudé size >> target_page_bits)) {
735f17068c1SStefano Stabellini DPRINTF("xen_set_mem_type error, addr: "HWADDR_FMT_plx"\n",
736f17068c1SStefano Stabellini start_addr);
737f17068c1SStefano Stabellini }
738f17068c1SStefano Stabellini }
739f17068c1SStefano Stabellini } else {
740f17068c1SStefano Stabellini if (xen_remove_from_physmap(state, start_addr, size) < 0) {
741f17068c1SStefano Stabellini DPRINTF("physmapping does not exist at "HWADDR_FMT_plx"\n", start_addr);
742f17068c1SStefano Stabellini }
743f17068c1SStefano Stabellini }
744f17068c1SStefano Stabellini }
745f17068c1SStefano Stabellini
arch_handle_ioreq(XenIOState * state,ioreq_t * req)746f17068c1SStefano Stabellini void arch_handle_ioreq(XenIOState *state, ioreq_t *req)
747f17068c1SStefano Stabellini {
748f17068c1SStefano Stabellini switch (req->type) {
749f17068c1SStefano Stabellini case IOREQ_TYPE_VMWARE_PORT:
750f17068c1SStefano Stabellini handle_vmport_ioreq(state, req);
751f17068c1SStefano Stabellini break;
752f17068c1SStefano Stabellini default:
753f17068c1SStefano Stabellini hw_error("Invalid ioreq type 0x%x\n", req->type);
754f17068c1SStefano Stabellini }
755f17068c1SStefano Stabellini
756f17068c1SStefano Stabellini return;
757f17068c1SStefano Stabellini }
758