xref: /openbmc/qemu/hw/i386/xen/xen-hvm.c (revision 4f7b1ecb)
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  * Contributions after 2012-01-13 are licensed under the terms of the
8  * GNU GPL, version 2 or (at your option) any later version.
9  */
10 
11 #include "qemu/osdep.h"
12 #include "qemu/units.h"
13 #include "qapi/error.h"
14 #include "qapi/qapi-commands-migration.h"
15 #include "trace.h"
16 
17 #include "hw/i386/pc.h"
18 #include "hw/irq.h"
19 #include "hw/i386/apic-msidef.h"
20 #include "hw/xen/xen-x86.h"
21 #include "qemu/range.h"
22 
23 #include "hw/xen/xen-hvm-common.h"
24 #include "hw/xen/arch_hvm.h"
25 #include <xen/hvm/e820.h>
26 #include "exec/target_page.h"
27 
28 static MemoryRegion ram_640k, ram_lo, ram_hi;
29 static MemoryRegion *framebuffer;
30 static bool xen_in_migration;
31 
32 /* Compatibility with older version */
33 
34 /*
35  * This allows QEMU to build on a system that has Xen 4.5 or earlier installed.
36  * This is here (not in hw/xen/xen_native.h) because xen/hvm/ioreq.h needs to
37  * be included before this block and hw/xen/xen_native.h needs to be included
38  * before xen/hvm/ioreq.h
39  */
40 #ifndef IOREQ_TYPE_VMWARE_PORT
41 #define IOREQ_TYPE_VMWARE_PORT  3
42 struct vmware_regs {
43     uint32_t esi;
44     uint32_t edi;
45     uint32_t ebx;
46     uint32_t ecx;
47     uint32_t edx;
48 };
49 typedef struct vmware_regs vmware_regs_t;
50 
51 struct shared_vmport_iopage {
52     struct vmware_regs vcpu_vmport_regs[1];
53 };
54 typedef struct shared_vmport_iopage shared_vmport_iopage_t;
55 #endif
56 
57 static shared_vmport_iopage_t *shared_vmport_page;
58 
59 static QLIST_HEAD(, XenPhysmap) xen_physmap;
60 static const XenPhysmap *log_for_dirtybit;
61 /* Buffer used by xen_sync_dirty_bitmap */
62 static unsigned long *dirty_bitmap;
63 static Notifier suspend;
64 static Notifier wakeup;
65 
66 /* Xen specific function for piix pci */
67 
68 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
69 {
70     return irq_num + (PCI_SLOT(pci_dev->devfn) << 2);
71 }
72 
73 void xen_intx_set_irq(void *opaque, int irq_num, int level)
74 {
75     xen_set_pci_intx_level(xen_domid, 0, 0, irq_num >> 2,
76                            irq_num & 3, level);
77 }
78 
79 int xen_set_pci_link_route(uint8_t link, uint8_t irq)
80 {
81     return xendevicemodel_set_pci_link_route(xen_dmod, xen_domid, link, irq);
82 }
83 
84 int xen_is_pirq_msi(uint32_t msi_data)
85 {
86     /* If vector is 0, the msi is remapped into a pirq, passed as
87      * dest_id.
88      */
89     return ((msi_data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT) == 0;
90 }
91 
92 void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
93 {
94     xen_inject_msi(xen_domid, addr, data);
95 }
96 
97 static void xen_suspend_notifier(Notifier *notifier, void *data)
98 {
99     xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
100 }
101 
102 /* Xen Interrupt Controller */
103 
104 static void xen_set_irq(void *opaque, int irq, int level)
105 {
106     xen_set_isa_irq_level(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(PCMachineState *pcms,
117                          ram_addr_t ram_size, MemoryRegion **ram_memory_p)
118 {
119     X86MachineState *x86ms = X86_MACHINE(pcms);
120     MemoryRegion *sysmem = get_system_memory();
121     ram_addr_t block_len;
122     uint64_t user_lowmem =
123         object_property_get_uint(qdev_get_machine(),
124                                  PC_MACHINE_MAX_RAM_BELOW_4G,
125                                  &error_abort);
126 
127     /* Handle the machine opt max-ram-below-4g.  It is basically doing
128      * min(xen limit, user limit).
129      */
130     if (!user_lowmem) {
131         user_lowmem = HVM_BELOW_4G_RAM_END; /* default */
132     }
133     if (HVM_BELOW_4G_RAM_END <= user_lowmem) {
134         user_lowmem = HVM_BELOW_4G_RAM_END;
135     }
136 
137     if (ram_size >= user_lowmem) {
138         x86ms->above_4g_mem_size = ram_size - user_lowmem;
139         x86ms->below_4g_mem_size = user_lowmem;
140     } else {
141         x86ms->above_4g_mem_size = 0;
142         x86ms->below_4g_mem_size = ram_size;
143     }
144     if (!x86ms->above_4g_mem_size) {
145         block_len = ram_size;
146     } else {
147         /*
148          * Xen does not allocate the memory continuously, it keeps a
149          * hole of the size computed above or passed in.
150          */
151         block_len = (4 * GiB) + x86ms->above_4g_mem_size;
152     }
153     memory_region_init_ram(&xen_memory, NULL, "xen.ram", block_len,
154                            &error_fatal);
155     *ram_memory_p = &xen_memory;
156 
157     memory_region_init_alias(&ram_640k, NULL, "xen.ram.640k",
158                              &xen_memory, 0, 0xa0000);
159     memory_region_add_subregion(sysmem, 0, &ram_640k);
160     /* Skip of the VGA IO memory space, it will be registered later by the VGA
161      * emulated device.
162      *
163      * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
164      * the Options ROM, so it is registered here as RAM.
165      */
166     memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo",
167                              &xen_memory, 0xc0000,
168                              x86ms->below_4g_mem_size - 0xc0000);
169     memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
170     if (x86ms->above_4g_mem_size > 0) {
171         memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi",
172                                  &xen_memory, 0x100000000ULL,
173                                  x86ms->above_4g_mem_size);
174         memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
175     }
176 }
177 
178 static XenPhysmap *get_physmapping(hwaddr start_addr, ram_addr_t size,
179                                    int page_mask)
180 {
181     XenPhysmap *physmap = NULL;
182 
183     start_addr &= page_mask;
184 
185     QLIST_FOREACH(physmap, &xen_physmap, list) {
186         if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
187             return physmap;
188         }
189     }
190     return NULL;
191 }
192 
193 static hwaddr xen_phys_offset_to_gaddr(hwaddr phys_offset, ram_addr_t size,
194                                        int page_mask)
195 {
196     hwaddr addr = phys_offset & page_mask;
197     XenPhysmap *physmap = NULL;
198 
199     QLIST_FOREACH(physmap, &xen_physmap, list) {
200         if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
201             return physmap->start_addr + (phys_offset - physmap->phys_offset);
202         }
203     }
204 
205     return phys_offset;
206 }
207 
208 #ifdef XEN_COMPAT_PHYSMAP
209 static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
210 {
211     char path[80], value[17];
212 
213     snprintf(path, sizeof(path),
214             "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
215             xen_domid, (uint64_t)physmap->phys_offset);
216     snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->start_addr);
217     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
218         return -1;
219     }
220     snprintf(path, sizeof(path),
221             "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
222             xen_domid, (uint64_t)physmap->phys_offset);
223     snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)physmap->size);
224     if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
225         return -1;
226     }
227     if (physmap->name) {
228         snprintf(path, sizeof(path),
229                 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
230                 xen_domid, (uint64_t)physmap->phys_offset);
231         if (!xs_write(state->xenstore, 0, path,
232                       physmap->name, strlen(physmap->name))) {
233             return -1;
234         }
235     }
236     return 0;
237 }
238 #else
239 static int xen_save_physmap(XenIOState *state, XenPhysmap *physmap)
240 {
241     return 0;
242 }
243 #endif
244 
245 static int xen_add_to_physmap(XenIOState *state,
246                               hwaddr start_addr,
247                               ram_addr_t size,
248                               MemoryRegion *mr,
249                               hwaddr offset_within_region)
250 {
251     unsigned target_page_bits = qemu_target_page_bits();
252     int page_size = qemu_target_page_size();
253     int page_mask = -page_size;
254     unsigned long nr_pages;
255     int rc = 0;
256     XenPhysmap *physmap = NULL;
257     hwaddr pfn, start_gpfn;
258     hwaddr phys_offset = memory_region_get_ram_addr(mr);
259     const char *mr_name;
260 
261     if (get_physmapping(start_addr, size, page_mask)) {
262         return 0;
263     }
264     if (size <= 0) {
265         return -1;
266     }
267 
268     /* Xen can only handle a single dirty log region for now and we want
269      * the linear framebuffer to be that region.
270      * Avoid tracking any regions that is not videoram and avoid tracking
271      * the legacy vga region. */
272     if (mr == framebuffer && start_addr > 0xbffff) {
273         goto go_physmap;
274     }
275     return -1;
276 
277 go_physmap:
278     DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
279             start_addr, start_addr + size);
280 
281     mr_name = memory_region_name(mr);
282 
283     physmap = g_new(XenPhysmap, 1);
284 
285     physmap->start_addr = start_addr;
286     physmap->size = size;
287     physmap->name = mr_name;
288     physmap->phys_offset = phys_offset;
289 
290     QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
291 
292     if (runstate_check(RUN_STATE_INMIGRATE)) {
293         /* Now when we have a physmap entry we can replace a dummy mapping with
294          * a real one of guest foreign memory. */
295         uint8_t *p = xen_replace_cache_entry(phys_offset, start_addr, size);
296         assert(p && p == memory_region_get_ram_ptr(mr));
297 
298         return 0;
299     }
300 
301     pfn = phys_offset >> target_page_bits;
302     start_gpfn = start_addr >> target_page_bits;
303     nr_pages = size >> target_page_bits;
304     rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, nr_pages, pfn,
305                                         start_gpfn);
306     if (rc) {
307         int saved_errno = errno;
308 
309         error_report("relocate_memory %lu pages from GFN %"HWADDR_PRIx
310                      " to GFN %"HWADDR_PRIx" failed: %s",
311                      nr_pages, pfn, start_gpfn, strerror(saved_errno));
312         errno = saved_errno;
313         return -1;
314     }
315 
316     rc = xendevicemodel_pin_memory_cacheattr(xen_dmod, xen_domid,
317                                    start_addr >> target_page_bits,
318                                    (start_addr + size - 1) >> target_page_bits,
319                                    XEN_DOMCTL_MEM_CACHEATTR_WB);
320     if (rc) {
321         error_report("pin_memory_cacheattr failed: %s", strerror(errno));
322     }
323     return xen_save_physmap(state, physmap);
324 }
325 
326 static int xen_remove_from_physmap(XenIOState *state,
327                                    hwaddr start_addr,
328                                    ram_addr_t size)
329 {
330     unsigned target_page_bits = qemu_target_page_bits();
331     int page_size = qemu_target_page_size();
332     int page_mask = -page_size;
333     int rc = 0;
334     XenPhysmap *physmap = NULL;
335     hwaddr phys_offset = 0;
336 
337     physmap = get_physmapping(start_addr, size, page_mask);
338     if (physmap == NULL) {
339         return -1;
340     }
341 
342     phys_offset = physmap->phys_offset;
343     size = physmap->size;
344 
345     DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", at "
346             "%"HWADDR_PRIx"\n", start_addr, start_addr + size, phys_offset);
347 
348     size >>= target_page_bits;
349     start_addr >>= target_page_bits;
350     phys_offset >>= target_page_bits;
351     rc = xendevicemodel_relocate_memory(xen_dmod, xen_domid, size, start_addr,
352                                         phys_offset);
353     if (rc) {
354         int saved_errno = errno;
355 
356         error_report("relocate_memory "RAM_ADDR_FMT" pages"
357                      " from GFN %"HWADDR_PRIx
358                      " to GFN %"HWADDR_PRIx" failed: %s",
359                      size, start_addr, phys_offset, strerror(saved_errno));
360         errno = saved_errno;
361         return -1;
362     }
363 
364     QLIST_REMOVE(physmap, list);
365     if (log_for_dirtybit == physmap) {
366         log_for_dirtybit = NULL;
367         g_free(dirty_bitmap);
368         dirty_bitmap = NULL;
369     }
370     g_free(physmap);
371 
372     return 0;
373 }
374 
375 static void xen_sync_dirty_bitmap(XenIOState *state,
376                                   hwaddr start_addr,
377                                   ram_addr_t size)
378 {
379     unsigned target_page_bits = qemu_target_page_bits();
380     int page_size = qemu_target_page_size();
381     int page_mask = -page_size;
382     hwaddr npages = size >> target_page_bits;
383     const int width = sizeof(unsigned long) * 8;
384     size_t bitmap_size = DIV_ROUND_UP(npages, width);
385     int rc, i, j;
386     const XenPhysmap *physmap = NULL;
387 
388     physmap = get_physmapping(start_addr, size, page_mask);
389     if (physmap == NULL) {
390         /* not handled */
391         return;
392     }
393 
394     if (log_for_dirtybit == NULL) {
395         log_for_dirtybit = physmap;
396         dirty_bitmap = g_new(unsigned long, bitmap_size);
397     } else if (log_for_dirtybit != physmap) {
398         /* Only one range for dirty bitmap can be tracked. */
399         return;
400     }
401 
402     rc = xen_track_dirty_vram(xen_domid, start_addr >> target_page_bits,
403                               npages, dirty_bitmap);
404     if (rc < 0) {
405 #ifndef ENODATA
406 #define ENODATA  ENOENT
407 #endif
408         if (errno == ENODATA) {
409             memory_region_set_dirty(framebuffer, 0, size);
410             DPRINTF("xen: track_dirty_vram failed (0x" HWADDR_FMT_plx
411                     ", 0x" HWADDR_FMT_plx "): %s\n",
412                     start_addr, start_addr + size, strerror(errno));
413         }
414         return;
415     }
416 
417     for (i = 0; i < bitmap_size; i++) {
418         unsigned long map = dirty_bitmap[i];
419         while (map != 0) {
420             j = ctzl(map);
421             map &= ~(1ul << j);
422             memory_region_set_dirty(framebuffer,
423                                     (i * width + j) * page_size, page_size);
424         };
425     }
426 }
427 
428 static void xen_log_start(MemoryListener *listener,
429                           MemoryRegionSection *section,
430                           int old, int new)
431 {
432     XenIOState *state = container_of(listener, XenIOState, memory_listener);
433 
434     if (new & ~old & (1 << DIRTY_MEMORY_VGA)) {
435         xen_sync_dirty_bitmap(state, section->offset_within_address_space,
436                               int128_get64(section->size));
437     }
438 }
439 
440 static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section,
441                          int old, int new)
442 {
443     if (old & ~new & (1 << DIRTY_MEMORY_VGA)) {
444         log_for_dirtybit = NULL;
445         g_free(dirty_bitmap);
446         dirty_bitmap = NULL;
447         /* Disable dirty bit tracking */
448         xen_track_dirty_vram(xen_domid, 0, 0, NULL);
449     }
450 }
451 
452 static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
453 {
454     XenIOState *state = container_of(listener, XenIOState, memory_listener);
455 
456     xen_sync_dirty_bitmap(state, section->offset_within_address_space,
457                           int128_get64(section->size));
458 }
459 
460 static bool xen_log_global_start(MemoryListener *listener, Error **errp)
461 {
462     if (xen_enabled()) {
463         xen_in_migration = true;
464     }
465     return true;
466 }
467 
468 static void xen_log_global_stop(MemoryListener *listener)
469 {
470     xen_in_migration = false;
471 }
472 
473 static const MemoryListener xen_memory_listener = {
474     .name = "xen-memory",
475     .region_add = xen_region_add,
476     .region_del = xen_region_del,
477     .log_start = xen_log_start,
478     .log_stop = xen_log_stop,
479     .log_sync = xen_log_sync,
480     .log_global_start = xen_log_global_start,
481     .log_global_stop = xen_log_global_stop,
482     .priority = MEMORY_LISTENER_PRIORITY_ACCEL,
483 };
484 
485 static void regs_to_cpu(vmware_regs_t *vmport_regs, ioreq_t *req)
486 {
487     X86CPU *cpu;
488     CPUX86State *env;
489 
490     cpu = X86_CPU(current_cpu);
491     env = &cpu->env;
492     env->regs[R_EAX] = req->data;
493     env->regs[R_EBX] = vmport_regs->ebx;
494     env->regs[R_ECX] = vmport_regs->ecx;
495     env->regs[R_EDX] = vmport_regs->edx;
496     env->regs[R_ESI] = vmport_regs->esi;
497     env->regs[R_EDI] = vmport_regs->edi;
498 }
499 
500 static void regs_from_cpu(vmware_regs_t *vmport_regs)
501 {
502     X86CPU *cpu = X86_CPU(current_cpu);
503     CPUX86State *env = &cpu->env;
504 
505     vmport_regs->ebx = env->regs[R_EBX];
506     vmport_regs->ecx = env->regs[R_ECX];
507     vmport_regs->edx = env->regs[R_EDX];
508     vmport_regs->esi = env->regs[R_ESI];
509     vmport_regs->edi = env->regs[R_EDI];
510 }
511 
512 static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
513 {
514     vmware_regs_t *vmport_regs;
515 
516     assert(shared_vmport_page);
517     vmport_regs =
518         &shared_vmport_page->vcpu_vmport_regs[state->send_vcpu];
519     QEMU_BUILD_BUG_ON(sizeof(*req) < sizeof(*vmport_regs));
520 
521     current_cpu = state->cpu_by_vcpu_id[state->send_vcpu];
522     regs_to_cpu(vmport_regs, req);
523     cpu_ioreq_pio(req);
524     regs_from_cpu(vmport_regs);
525     current_cpu = NULL;
526 }
527 
528 #ifdef XEN_COMPAT_PHYSMAP
529 static void xen_read_physmap(XenIOState *state)
530 {
531     XenPhysmap *physmap = NULL;
532     unsigned int len, num, i;
533     char path[80], *value = NULL;
534     char **entries = NULL;
535 
536     snprintf(path, sizeof(path),
537             "/local/domain/0/device-model/%d/physmap", xen_domid);
538     entries = xs_directory(state->xenstore, 0, path, &num);
539     if (entries == NULL)
540         return;
541 
542     for (i = 0; i < num; i++) {
543         physmap = g_new(XenPhysmap, 1);
544         physmap->phys_offset = strtoull(entries[i], NULL, 16);
545         snprintf(path, sizeof(path),
546                 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
547                 xen_domid, entries[i]);
548         value = xs_read(state->xenstore, 0, path, &len);
549         if (value == NULL) {
550             g_free(physmap);
551             continue;
552         }
553         physmap->start_addr = strtoull(value, NULL, 16);
554         free(value);
555 
556         snprintf(path, sizeof(path),
557                 "/local/domain/0/device-model/%d/physmap/%s/size",
558                 xen_domid, entries[i]);
559         value = xs_read(state->xenstore, 0, path, &len);
560         if (value == NULL) {
561             g_free(physmap);
562             continue;
563         }
564         physmap->size = strtoull(value, NULL, 16);
565         free(value);
566 
567         snprintf(path, sizeof(path),
568                 "/local/domain/0/device-model/%d/physmap/%s/name",
569                 xen_domid, entries[i]);
570         physmap->name = xs_read(state->xenstore, 0, path, &len);
571 
572         QLIST_INSERT_HEAD(&xen_physmap, physmap, list);
573     }
574     free(entries);
575 }
576 #else
577 static void xen_read_physmap(XenIOState *state)
578 {
579 }
580 #endif
581 
582 static void xen_wakeup_notifier(Notifier *notifier, void *data)
583 {
584     xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 0);
585 }
586 
587 static bool xen_check_stubdomain(struct xs_handle *xsh)
588 {
589     char *dm_path = g_strdup_printf(
590         "/local/domain/%d/image/device-model-domid", xen_domid);
591     char *val;
592     int32_t dm_domid;
593     bool is_stubdom = false;
594 
595     val = xs_read(xsh, 0, dm_path, NULL);
596     if (val) {
597         if (sscanf(val, "%d", &dm_domid) == 1) {
598             is_stubdom = dm_domid != 0;
599         }
600         free(val);
601     }
602 
603     g_free(dm_path);
604     return is_stubdom;
605 }
606 
607 void xen_hvm_init_pc(PCMachineState *pcms, MemoryRegion **ram_memory)
608 {
609     MachineState *ms = MACHINE(pcms);
610     unsigned int max_cpus = ms->smp.max_cpus;
611     int rc;
612     xen_pfn_t ioreq_pfn;
613     XenIOState *state;
614 
615     state = g_new0(XenIOState, 1);
616 
617     xen_register_ioreq(state, max_cpus, &xen_memory_listener);
618 
619     xen_is_stubdomain = xen_check_stubdomain(state->xenstore);
620 
621     QLIST_INIT(&xen_physmap);
622     xen_read_physmap(state);
623 
624     suspend.notify = xen_suspend_notifier;
625     qemu_register_suspend_notifier(&suspend);
626 
627     wakeup.notify = xen_wakeup_notifier;
628     qemu_register_wakeup_notifier(&wakeup);
629 
630     rc = xen_get_vmport_regs_pfn(xen_xc, xen_domid, &ioreq_pfn);
631     if (!rc) {
632         DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
633         shared_vmport_page =
634             xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
635                                  1, &ioreq_pfn, NULL);
636         if (shared_vmport_page == NULL) {
637             error_report("map shared vmport IO page returned error %d handle=%p",
638                          errno, xen_xc);
639             goto err;
640         }
641     } else if (rc != -ENOSYS) {
642         error_report("get vmport regs pfn returned error %d, rc=%d",
643                      errno, rc);
644         goto err;
645     }
646 
647     xen_ram_init(pcms, ms->ram_size, ram_memory);
648 
649     /* Disable ACPI build because Xen handles it */
650     pcms->acpi_build_enabled = false;
651 
652     return;
653 
654 err:
655     error_report("xen hardware virtual machine initialisation failed");
656     exit(1);
657 }
658 
659 void xen_register_framebuffer(MemoryRegion *mr)
660 {
661     framebuffer = mr;
662 }
663 
664 void xen_hvm_modified_memory(ram_addr_t start, ram_addr_t length)
665 {
666     unsigned target_page_bits = qemu_target_page_bits();
667     int page_size = qemu_target_page_size();
668     int page_mask = -page_size;
669 
670     if (unlikely(xen_in_migration)) {
671         int rc;
672         ram_addr_t start_pfn, nb_pages;
673 
674         start = xen_phys_offset_to_gaddr(start, length, page_mask);
675 
676         if (length == 0) {
677             length = page_size;
678         }
679         start_pfn = start >> target_page_bits;
680         nb_pages = ((start + length + page_size - 1) >> target_page_bits)
681             - start_pfn;
682         rc = xen_modified_memory(xen_domid, start_pfn, nb_pages);
683         if (rc) {
684             fprintf(stderr,
685                     "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
686                     __func__, start, nb_pages, errno, strerror(errno));
687         }
688     }
689 }
690 
691 void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
692 {
693     if (enable) {
694         memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION, errp);
695     } else {
696         memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION);
697     }
698 }
699 
700 void arch_xen_set_memory(XenIOState *state, MemoryRegionSection *section,
701                                 bool add)
702 {
703     unsigned target_page_bits = qemu_target_page_bits();
704     int page_size = qemu_target_page_size();
705     int page_mask = -page_size;
706     hwaddr start_addr = section->offset_within_address_space;
707     ram_addr_t size = int128_get64(section->size);
708     bool log_dirty = memory_region_is_logging(section->mr, DIRTY_MEMORY_VGA);
709     hvmmem_type_t mem_type;
710 
711     if (!memory_region_is_ram(section->mr)) {
712         return;
713     }
714 
715     if (log_dirty != add) {
716         return;
717     }
718 
719     trace_xen_client_set_memory(start_addr, size, log_dirty);
720 
721     start_addr &= page_mask;
722     size = ROUND_UP(size, page_size);
723 
724     if (add) {
725         if (!memory_region_is_rom(section->mr)) {
726             xen_add_to_physmap(state, start_addr, size,
727                                section->mr, section->offset_within_region);
728         } else {
729             mem_type = HVMMEM_ram_ro;
730             if (xen_set_mem_type(xen_domid, mem_type,
731                                  start_addr >> target_page_bits,
732                                  size >> target_page_bits)) {
733                 DPRINTF("xen_set_mem_type error, addr: "HWADDR_FMT_plx"\n",
734                         start_addr);
735             }
736         }
737     } else {
738         if (xen_remove_from_physmap(state, start_addr, size) < 0) {
739             DPRINTF("physmapping does not exist at "HWADDR_FMT_plx"\n", start_addr);
740         }
741     }
742 }
743 
744 void arch_handle_ioreq(XenIOState *state, ioreq_t *req)
745 {
746     switch (req->type) {
747     case IOREQ_TYPE_VMWARE_PORT:
748             handle_vmport_ioreq(state, req);
749         break;
750     default:
751         hw_error("Invalid ioreq type 0x%x\n", req->type);
752     }
753 
754     return;
755 }
756