]> git.proxmox.com Git - qemu.git/blame - xen-all.c
cpu_ioreq_pio, cpu_ioreq_move: introduce read_phys_req_item, write_phys_req_item
[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 *
6b620ca3
PB
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.
3285cf4f
AP
9 */
10
9ce94e7c
AS
11#include <sys/mman.h>
12
41445300 13#include "hw/pci.h"
c9622478 14#include "hw/pc.h"
3285cf4f
AP
15#include "hw/xen_common.h"
16#include "hw/xen_backend.h"
39f42439 17#include "qmp-commands.h"
3285cf4f 18
b4dd7802 19#include "range.h"
432d268c
JN
20#include "xen-mapcache.h"
21#include "trace.h"
ce76b8a8 22#include "exec-memory.h"
432d268c 23
9ce94e7c
AS
24#include <xen/hvm/ioreq.h>
25#include <xen/hvm/params.h>
8a369e20 26#include <xen/hvm/e820.h>
9ce94e7c
AS
27
28//#define DEBUG_XEN
29
30#ifdef DEBUG_XEN
31#define DPRINTF(fmt, ...) \
32 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
33#else
34#define DPRINTF(fmt, ...) \
35 do { } while (0)
36#endif
37
ce76b8a8 38static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
c65adf9b 39static MemoryRegion *framebuffer;
39f42439 40static bool xen_in_migration;
ce76b8a8 41
9ce94e7c
AS
42/* Compatibility with older version */
43#if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
44static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
45{
46 return shared_page->vcpu_iodata[i].vp_eport;
47}
48static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
49{
50 return &shared_page->vcpu_iodata[vcpu].vp_ioreq;
51}
52# define FMT_ioreq_size PRIx64
53#else
54static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
55{
56 return shared_page->vcpu_ioreq[i].vp_eport;
57}
58static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
59{
60 return &shared_page->vcpu_ioreq[vcpu];
61}
62# define FMT_ioreq_size "u"
63#endif
fda1f768
SS
64#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
65#define HVM_PARAM_BUFIOREQ_EVTCHN 26
66#endif
9ce94e7c
AS
67
68#define BUFFER_IO_MAX_DELAY 100
69
b4dd7802 70typedef struct XenPhysmap {
a8170e5e 71 hwaddr start_addr;
b4dd7802 72 ram_addr_t size;
d1814e08 73 char *name;
a8170e5e 74 hwaddr phys_offset;
b4dd7802
AP
75
76 QLIST_ENTRY(XenPhysmap) list;
77} XenPhysmap;
78
9ce94e7c
AS
79typedef struct XenIOState {
80 shared_iopage_t *shared_page;
81 buffered_iopage_t *buffered_io_page;
82 QEMUTimer *buffered_io_timer;
83 /* the evtchn port for polling the notification, */
84 evtchn_port_t *ioreq_local_port;
fda1f768
SS
85 /* evtchn local port for buffered io */
86 evtchn_port_t bufioreq_local_port;
9ce94e7c
AS
87 /* the evtchn fd for polling */
88 XenEvtchn xce_handle;
89 /* which vcpu we are serving */
90 int send_vcpu;
91
29321335 92 struct xs_handle *xenstore;
20581d20 93 MemoryListener memory_listener;
b4dd7802 94 QLIST_HEAD(, XenPhysmap) physmap;
a8170e5e 95 hwaddr free_phys_offset;
b4dd7802 96 const XenPhysmap *log_for_dirtybit;
29321335 97
9ce94e7c 98 Notifier exit;
da98c8eb 99 Notifier suspend;
9ce94e7c
AS
100} XenIOState;
101
41445300
AP
102/* Xen specific function for piix pci */
103
104int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
105{
106 return irq_num + ((pci_dev->devfn >> 3) << 2);
107}
108
109void xen_piix3_set_irq(void *opaque, int irq_num, int level)
110{
111 xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2,
112 irq_num & 3, level);
113}
114
115void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
116{
117 int i;
118
119 /* Scan for updates to PCI link routes (0x60-0x63). */
120 for (i = 0; i < len; i++) {
121 uint8_t v = (val >> (8 * i)) & 0xff;
122 if (v & 0x80) {
123 v = 0;
124 }
125 v &= 0xf;
126 if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
127 xc_hvm_set_pci_link_route(xen_xc, xen_domid, address + i - 0x60, v);
128 }
129 }
130}
131
f1dbf015
WL
132void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
133{
4c9f8d1b 134 xen_xc_hvm_inject_msi(xen_xc, xen_domid, addr, data);
f1dbf015
WL
135}
136
da98c8eb 137static void xen_suspend_notifier(Notifier *notifier, void *data)
c9622478 138{
da98c8eb 139 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
c9622478
AP
140}
141
9c11a8ac
AP
142/* Xen Interrupt Controller */
143
144static void xen_set_irq(void *opaque, int irq, int level)
145{
146 xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level);
147}
148
149qemu_irq *xen_interrupt_controller_init(void)
150{
151 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
152}
153
432d268c
JN
154/* Memory Ops */
155
156static void xen_ram_init(ram_addr_t ram_size)
157{
ce76b8a8 158 MemoryRegion *sysmem = get_system_memory();
432d268c 159 ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
ce76b8a8 160 ram_addr_t block_len;
432d268c 161
ce76b8a8 162 block_len = ram_size;
8a369e20
AP
163 if (ram_size >= HVM_BELOW_4G_RAM_END) {
164 /* Xen does not allocate the memory continuously, and keep a hole at
165 * HVM_BELOW_4G_MMIO_START of HVM_BELOW_4G_MMIO_LENGTH
166 */
ce76b8a8 167 block_len += HVM_BELOW_4G_MMIO_LENGTH;
8a369e20 168 }
c5705a77
AK
169 memory_region_init_ram(&ram_memory, "xen.ram", block_len);
170 vmstate_register_ram_global(&ram_memory);
432d268c 171
8a369e20
AP
172 if (ram_size >= HVM_BELOW_4G_RAM_END) {
173 above_4g_mem_size = ram_size - HVM_BELOW_4G_RAM_END;
174 below_4g_mem_size = HVM_BELOW_4G_RAM_END;
432d268c
JN
175 } else {
176 below_4g_mem_size = ram_size;
177 }
178
ce76b8a8
AK
179 memory_region_init_alias(&ram_640k, "xen.ram.640k",
180 &ram_memory, 0, 0xa0000);
181 memory_region_add_subregion(sysmem, 0, &ram_640k);
8a369e20
AP
182 /* Skip of the VGA IO memory space, it will be registered later by the VGA
183 * emulated device.
184 *
185 * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load
186 * the Options ROM, so it is registered here as RAM.
187 */
ce76b8a8
AK
188 memory_region_init_alias(&ram_lo, "xen.ram.lo",
189 &ram_memory, 0xc0000, below_4g_mem_size - 0xc0000);
190 memory_region_add_subregion(sysmem, 0xc0000, &ram_lo);
432d268c 191 if (above_4g_mem_size > 0) {
ce76b8a8
AK
192 memory_region_init_alias(&ram_hi, "xen.ram.hi",
193 &ram_memory, 0x100000000ULL,
194 above_4g_mem_size);
195 memory_region_add_subregion(sysmem, 0x100000000ULL, &ram_hi);
432d268c 196 }
432d268c
JN
197}
198
fce537d4 199void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr)
432d268c
JN
200{
201 unsigned long nr_pfn;
202 xen_pfn_t *pfn_list;
203 int i;
204
c234572d
AP
205 if (runstate_check(RUN_STATE_INMIGRATE)) {
206 /* RAM already populated in Xen */
207 fprintf(stderr, "%s: do not alloc "RAM_ADDR_FMT
208 " bytes of ram at "RAM_ADDR_FMT" when runstate is INMIGRATE\n",
209 __func__, size, ram_addr);
210 return;
211 }
212
ce76b8a8
AK
213 if (mr == &ram_memory) {
214 return;
215 }
216
432d268c
JN
217 trace_xen_ram_alloc(ram_addr, size);
218
219 nr_pfn = size >> TARGET_PAGE_BITS;
7267c094 220 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
432d268c
JN
221
222 for (i = 0; i < nr_pfn; i++) {
223 pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i;
224 }
225
226 if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) {
f15fbc4b 227 hw_error("xen: failed to populate ram at " RAM_ADDR_FMT, ram_addr);
432d268c
JN
228 }
229
7267c094 230 g_free(pfn_list);
432d268c
JN
231}
232
b4dd7802 233static XenPhysmap *get_physmapping(XenIOState *state,
a8170e5e 234 hwaddr start_addr, ram_addr_t size)
b4dd7802
AP
235{
236 XenPhysmap *physmap = NULL;
237
238 start_addr &= TARGET_PAGE_MASK;
239
240 QLIST_FOREACH(physmap, &state->physmap, list) {
241 if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) {
242 return physmap;
243 }
244 }
245 return NULL;
246}
247
a8170e5e 248static hwaddr xen_phys_offset_to_gaddr(hwaddr start_addr,
cd1ba7de
AP
249 ram_addr_t size, void *opaque)
250{
a8170e5e 251 hwaddr addr = start_addr & TARGET_PAGE_MASK;
cd1ba7de
AP
252 XenIOState *xen_io_state = opaque;
253 XenPhysmap *physmap = NULL;
254
255 QLIST_FOREACH(physmap, &xen_io_state->physmap, list) {
256 if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
257 return physmap->start_addr;
258 }
259 }
260
261 return start_addr;
262}
263
b4dd7802
AP
264#if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
265static int xen_add_to_physmap(XenIOState *state,
a8170e5e 266 hwaddr start_addr,
b4dd7802 267 ram_addr_t size,
20581d20 268 MemoryRegion *mr,
a8170e5e 269 hwaddr offset_within_region)
b4dd7802
AP
270{
271 unsigned long i = 0;
272 int rc = 0;
273 XenPhysmap *physmap = NULL;
a8170e5e
AK
274 hwaddr pfn, start_gpfn;
275 hwaddr phys_offset = memory_region_get_ram_addr(mr);
d1814e08 276 char path[80], value[17];
b4dd7802
AP
277
278 if (get_physmapping(state, start_addr, size)) {
279 return 0;
280 }
281 if (size <= 0) {
282 return -1;
283 }
284
ebed8505
SS
285 /* Xen can only handle a single dirty log region for now and we want
286 * the linear framebuffer to be that region.
287 * Avoid tracking any regions that is not videoram and avoid tracking
288 * the legacy vga region. */
20581d20
AK
289 if (mr == framebuffer && start_addr > 0xbffff) {
290 goto go_physmap;
ebed8505
SS
291 }
292 return -1;
293
294go_physmap:
f1b8caf1
SE
295 DPRINTF("mapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx"\n",
296 start_addr, start_addr + size);
b4dd7802
AP
297
298 pfn = phys_offset >> TARGET_PAGE_BITS;
299 start_gpfn = start_addr >> TARGET_PAGE_BITS;
300 for (i = 0; i < size >> TARGET_PAGE_BITS; i++) {
301 unsigned long idx = pfn + i;
302 xen_pfn_t gpfn = start_gpfn + i;
303
304 rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
305 if (rc) {
306 DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
307 PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
308 return -rc;
309 }
310 }
311
7267c094 312 physmap = g_malloc(sizeof (XenPhysmap));
b4dd7802
AP
313
314 physmap->start_addr = start_addr;
315 physmap->size = size;
d1814e08 316 physmap->name = (char *)mr->name;
b4dd7802
AP
317 physmap->phys_offset = phys_offset;
318
319 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
320
321 xc_domain_pin_memory_cacheattr(xen_xc, xen_domid,
322 start_addr >> TARGET_PAGE_BITS,
323 (start_addr + size) >> TARGET_PAGE_BITS,
324 XEN_DOMCTL_MEM_CACHEATTR_WB);
d1814e08
SS
325
326 snprintf(path, sizeof(path),
327 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/start_addr",
328 xen_domid, (uint64_t)phys_offset);
329 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)start_addr);
330 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
331 return -1;
332 }
333 snprintf(path, sizeof(path),
334 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/size",
335 xen_domid, (uint64_t)phys_offset);
336 snprintf(value, sizeof(value), "%"PRIx64, (uint64_t)size);
337 if (!xs_write(state->xenstore, 0, path, value, strlen(value))) {
338 return -1;
339 }
340 if (mr->name) {
341 snprintf(path, sizeof(path),
342 "/local/domain/0/device-model/%d/physmap/%"PRIx64"/name",
343 xen_domid, (uint64_t)phys_offset);
344 if (!xs_write(state->xenstore, 0, path, mr->name, strlen(mr->name))) {
345 return -1;
346 }
347 }
348
b4dd7802
AP
349 return 0;
350}
351
352static int xen_remove_from_physmap(XenIOState *state,
a8170e5e 353 hwaddr start_addr,
b4dd7802
AP
354 ram_addr_t size)
355{
356 unsigned long i = 0;
357 int rc = 0;
358 XenPhysmap *physmap = NULL;
a8170e5e 359 hwaddr phys_offset = 0;
b4dd7802
AP
360
361 physmap = get_physmapping(state, start_addr, size);
362 if (physmap == NULL) {
363 return -1;
364 }
365
366 phys_offset = physmap->phys_offset;
367 size = physmap->size;
368
f1b8caf1
SE
369 DPRINTF("unmapping vram to %"HWADDR_PRIx" - %"HWADDR_PRIx", from ",
370 "%"HWADDR_PRIx"\n", phys_offset, phys_offset + size, start_addr);
b4dd7802
AP
371
372 size >>= TARGET_PAGE_BITS;
373 start_addr >>= TARGET_PAGE_BITS;
374 phys_offset >>= TARGET_PAGE_BITS;
375 for (i = 0; i < size; i++) {
376 unsigned long idx = start_addr + i;
377 xen_pfn_t gpfn = phys_offset + i;
378
379 rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn);
380 if (rc) {
381 fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %"
382 PRI_xen_pfn" failed: %d\n", idx, gpfn, rc);
383 return -rc;
384 }
385 }
386
387 QLIST_REMOVE(physmap, list);
388 if (state->log_for_dirtybit == physmap) {
389 state->log_for_dirtybit = NULL;
390 }
391 free(physmap);
392
393 return 0;
394}
395
396#else
397static int xen_add_to_physmap(XenIOState *state,
a8170e5e 398 hwaddr start_addr,
b4dd7802 399 ram_addr_t size,
20581d20 400 MemoryRegion *mr,
a8170e5e 401 hwaddr offset_within_region)
b4dd7802
AP
402{
403 return -ENOSYS;
404}
405
406static int xen_remove_from_physmap(XenIOState *state,
a8170e5e 407 hwaddr start_addr,
b4dd7802
AP
408 ram_addr_t size)
409{
410 return -ENOSYS;
411}
412#endif
413
20581d20
AK
414static void xen_set_memory(struct MemoryListener *listener,
415 MemoryRegionSection *section,
416 bool add)
b4dd7802 417{
20581d20 418 XenIOState *state = container_of(listener, XenIOState, memory_listener);
a8170e5e 419 hwaddr start_addr = section->offset_within_address_space;
20581d20
AK
420 ram_addr_t size = section->size;
421 bool log_dirty = memory_region_is_logging(section->mr);
b4dd7802
AP
422 hvmmem_type_t mem_type;
423
20581d20 424 if (!memory_region_is_ram(section->mr)) {
b4dd7802
AP
425 return;
426 }
427
20581d20
AK
428 if (!(section->mr != &ram_memory
429 && ( (log_dirty && add) || (!log_dirty && !add)))) {
430 return;
431 }
432
433 trace_xen_client_set_memory(start_addr, size, log_dirty);
b4dd7802
AP
434
435 start_addr &= TARGET_PAGE_MASK;
436 size = TARGET_PAGE_ALIGN(size);
20581d20
AK
437
438 if (add) {
439 if (!memory_region_is_rom(section->mr)) {
440 xen_add_to_physmap(state, start_addr, size,
441 section->mr, section->offset_within_region);
442 } else {
443 mem_type = HVMMEM_ram_ro;
444 if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type,
445 start_addr >> TARGET_PAGE_BITS,
446 size >> TARGET_PAGE_BITS)) {
447 DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx"\n",
448 start_addr);
449 }
b4dd7802 450 }
20581d20 451 } else {
b4dd7802
AP
452 if (xen_remove_from_physmap(state, start_addr, size) < 0) {
453 DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr);
454 }
b4dd7802
AP
455 }
456}
457
20581d20
AK
458static void xen_region_add(MemoryListener *listener,
459 MemoryRegionSection *section)
460{
461 xen_set_memory(listener, section, true);
462}
463
464static void xen_region_del(MemoryListener *listener,
465 MemoryRegionSection *section)
466{
467 xen_set_memory(listener, section, false);
468}
469
b18620cf 470static void xen_sync_dirty_bitmap(XenIOState *state,
a8170e5e 471 hwaddr start_addr,
b18620cf 472 ram_addr_t size)
b4dd7802 473{
a8170e5e 474 hwaddr npages = size >> TARGET_PAGE_BITS;
b4dd7802
AP
475 const int width = sizeof(unsigned long) * 8;
476 unsigned long bitmap[(npages + width - 1) / width];
477 int rc, i, j;
478 const XenPhysmap *physmap = NULL;
479
480 physmap = get_physmapping(state, start_addr, size);
481 if (physmap == NULL) {
482 /* not handled */
b18620cf 483 return;
b4dd7802
AP
484 }
485
486 if (state->log_for_dirtybit == NULL) {
487 state->log_for_dirtybit = physmap;
488 } else if (state->log_for_dirtybit != physmap) {
b18620cf
AP
489 /* Only one range for dirty bitmap can be tracked. */
490 return;
b4dd7802 491 }
b4dd7802
AP
492
493 rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
494 start_addr >> TARGET_PAGE_BITS, npages,
495 bitmap);
b18620cf
AP
496 if (rc < 0) {
497 if (rc != -ENODATA) {
8aba7dc0
AP
498 memory_region_set_dirty(framebuffer, 0, size);
499 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
b18620cf
AP
500 ", 0x" TARGET_FMT_plx "): %s\n",
501 start_addr, start_addr + size, strerror(-rc));
502 }
503 return;
b4dd7802
AP
504 }
505
506 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
507 unsigned long map = bitmap[i];
508 while (map != 0) {
509 j = ffsl(map) - 1;
510 map &= ~(1ul << j);
5a97065b 511 memory_region_set_dirty(framebuffer,
fd4aa979
BS
512 (i * width + j) * TARGET_PAGE_SIZE,
513 TARGET_PAGE_SIZE);
b4dd7802
AP
514 };
515 }
b4dd7802
AP
516}
517
20581d20
AK
518static void xen_log_start(MemoryListener *listener,
519 MemoryRegionSection *section)
b4dd7802 520{
20581d20 521 XenIOState *state = container_of(listener, XenIOState, memory_listener);
b4dd7802 522
b18620cf
AP
523 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
524 section->size);
b4dd7802
AP
525}
526
20581d20 527static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section)
b4dd7802 528{
20581d20 529 XenIOState *state = container_of(listener, XenIOState, memory_listener);
b4dd7802
AP
530
531 state->log_for_dirtybit = NULL;
532 /* Disable dirty bit tracking */
b18620cf 533 xc_hvm_track_dirty_vram(xen_xc, xen_domid, 0, 0, NULL);
b4dd7802
AP
534}
535
20581d20 536static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
b4dd7802 537{
20581d20 538 XenIOState *state = container_of(listener, XenIOState, memory_listener);
b4dd7802 539
b18620cf
AP
540 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
541 section->size);
b4dd7802
AP
542}
543
20581d20
AK
544static void xen_log_global_start(MemoryListener *listener)
545{
39f42439
AP
546 if (xen_enabled()) {
547 xen_in_migration = true;
548 }
20581d20
AK
549}
550
551static void xen_log_global_stop(MemoryListener *listener)
b4dd7802 552{
39f42439 553 xen_in_migration = false;
b4dd7802
AP
554}
555
20581d20
AK
556static MemoryListener xen_memory_listener = {
557 .region_add = xen_region_add,
558 .region_del = xen_region_del,
b4dd7802
AP
559 .log_start = xen_log_start,
560 .log_stop = xen_log_stop,
20581d20
AK
561 .log_sync = xen_log_sync,
562 .log_global_start = xen_log_global_start,
563 .log_global_stop = xen_log_global_stop,
72e22d2f 564 .priority = 10,
b4dd7802 565};
432d268c 566
39f42439
AP
567void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
568{
569 if (enable) {
570 memory_global_dirty_log_start();
571 } else {
572 memory_global_dirty_log_stop();
573 }
574}
575
29d3ccde
AP
576/* VCPU Operations, MMIO, IO ring ... */
577
578static void xen_reset_vcpu(void *opaque)
579{
9349b4f9 580 CPUArchState *env = opaque;
29d3ccde
AP
581
582 env->halted = 1;
583}
584
585void xen_vcpu_init(void)
586{
9349b4f9 587 CPUArchState *first_cpu;
29d3ccde
AP
588
589 if ((first_cpu = qemu_get_cpu(0))) {
590 qemu_register_reset(xen_reset_vcpu, first_cpu);
591 xen_reset_vcpu(first_cpu);
592 }
95d5d75e
SS
593 /* if rtc_clock is left to default (host_clock), disable it */
594 if (rtc_clock == host_clock) {
595 qemu_clock_enable(rtc_clock, false);
596 }
29d3ccde
AP
597}
598
9ce94e7c
AS
599/* get the ioreq packets from share mem */
600static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
601{
602 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
603
604 if (req->state != STATE_IOREQ_READY) {
605 DPRINTF("I/O request not ready: "
606 "%x, ptr: %x, port: %"PRIx64", "
607 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
608 req->state, req->data_is_ptr, req->addr,
609 req->data, req->count, req->size);
610 return NULL;
611 }
612
613 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
614
615 req->state = STATE_IOREQ_INPROCESS;
616 return req;
617}
618
619/* use poll to get the port notification */
620/* ioreq_vec--out,the */
621/* retval--the number of ioreq packet */
622static ioreq_t *cpu_get_ioreq(XenIOState *state)
623{
624 int i;
625 evtchn_port_t port;
626
627 port = xc_evtchn_pending(state->xce_handle);
fda1f768
SS
628 if (port == state->bufioreq_local_port) {
629 qemu_mod_timer(state->buffered_io_timer,
630 BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
631 return NULL;
632 }
633
9ce94e7c
AS
634 if (port != -1) {
635 for (i = 0; i < smp_cpus; i++) {
636 if (state->ioreq_local_port[i] == port) {
637 break;
638 }
639 }
640
641 if (i == smp_cpus) {
642 hw_error("Fatal error while trying to get io event!\n");
643 }
644
645 /* unmask the wanted port again */
646 xc_evtchn_unmask(state->xce_handle, port);
647
648 /* get the io packet from shared memory */
649 state->send_vcpu = i;
650 return cpu_get_ioreq_from_shared_memory(state, i);
651 }
652
653 /* read error or read nothing */
654 return NULL;
655}
656
657static uint32_t do_inp(pio_addr_t addr, unsigned long size)
658{
659 switch (size) {
660 case 1:
661 return cpu_inb(addr);
662 case 2:
663 return cpu_inw(addr);
664 case 4:
665 return cpu_inl(addr);
666 default:
667 hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
668 }
669}
670
671static void do_outp(pio_addr_t addr,
672 unsigned long size, uint32_t val)
673{
674 switch (size) {
675 case 1:
676 return cpu_outb(addr, val);
677 case 2:
678 return cpu_outw(addr, val);
679 case 4:
680 return cpu_outl(addr, val);
681 default:
682 hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
683 }
684}
685
a3864829
IJ
686/*
687 * Helper functions which read/write an object from/to physical guest
688 * memory, as part of the implementation of an ioreq.
689 *
690 * Equivalent to
691 * cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
692 * val, req->size, 0/1)
693 * except without the integer overflow problems.
694 */
695static void rw_phys_req_item(hwaddr addr,
696 ioreq_t *req, uint32_t i, void *val, int rw)
697{
698 /* Do everything unsigned so overflow just results in a truncated result
699 * and accesses to undesired parts of guest memory, which is up
700 * to the guest */
701 hwaddr offset = (hwaddr)req->size * i;
702 if (req->df) {
703 addr -= offset;
704 } else {
705 addr += offset;
706 }
707 cpu_physical_memory_rw(addr, val, req->size, rw);
708}
709
710static inline void read_phys_req_item(hwaddr addr,
711 ioreq_t *req, uint32_t i, void *val)
9ce94e7c 712{
a3864829
IJ
713 rw_phys_req_item(addr, req, i, val, 0);
714}
715static inline void write_phys_req_item(hwaddr addr,
716 ioreq_t *req, uint32_t i, void *val)
717{
718 rw_phys_req_item(addr, req, i, val, 1);
719}
9ce94e7c 720
a3864829
IJ
721
722static void cpu_ioreq_pio(ioreq_t *req)
723{
724 int i;
9ce94e7c
AS
725
726 if (req->dir == IOREQ_READ) {
727 if (!req->data_is_ptr) {
728 req->data = do_inp(req->addr, req->size);
729 } else {
730 uint32_t tmp;
731
732 for (i = 0; i < req->count; i++) {
733 tmp = do_inp(req->addr, req->size);
a3864829 734 write_phys_req_item(req->data, req, i, &tmp);
9ce94e7c
AS
735 }
736 }
737 } else if (req->dir == IOREQ_WRITE) {
738 if (!req->data_is_ptr) {
739 do_outp(req->addr, req->size, req->data);
740 } else {
741 for (i = 0; i < req->count; i++) {
742 uint32_t tmp = 0;
743
a3864829 744 read_phys_req_item(req->data, req, i, &tmp);
9ce94e7c
AS
745 do_outp(req->addr, req->size, tmp);
746 }
747 }
748 }
749}
750
751static void cpu_ioreq_move(ioreq_t *req)
752{
a3864829 753 int i;
9ce94e7c
AS
754
755 if (!req->data_is_ptr) {
756 if (req->dir == IOREQ_READ) {
757 for (i = 0; i < req->count; i++) {
a3864829 758 read_phys_req_item(req->addr, req, i, &req->data);
9ce94e7c
AS
759 }
760 } else if (req->dir == IOREQ_WRITE) {
761 for (i = 0; i < req->count; i++) {
a3864829 762 write_phys_req_item(req->addr, req, i, &req->data);
9ce94e7c
AS
763 }
764 }
765 } else {
2b734340 766 uint64_t tmp;
9ce94e7c
AS
767
768 if (req->dir == IOREQ_READ) {
769 for (i = 0; i < req->count; i++) {
a3864829
IJ
770 read_phys_req_item(req->addr, req, i, &tmp);
771 write_phys_req_item(req->data, req, i, &tmp);
9ce94e7c
AS
772 }
773 } else if (req->dir == IOREQ_WRITE) {
774 for (i = 0; i < req->count; i++) {
a3864829
IJ
775 read_phys_req_item(req->data, req, i, &tmp);
776 write_phys_req_item(req->addr, req, i, &tmp);
9ce94e7c
AS
777 }
778 }
779 }
780}
781
782static void handle_ioreq(ioreq_t *req)
783{
784 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
785 (req->size < sizeof (target_ulong))) {
786 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
787 }
788
789 switch (req->type) {
790 case IOREQ_TYPE_PIO:
791 cpu_ioreq_pio(req);
792 break;
793 case IOREQ_TYPE_COPY:
794 cpu_ioreq_move(req);
795 break;
796 case IOREQ_TYPE_TIMEOFFSET:
797 break;
798 case IOREQ_TYPE_INVALIDATE:
e41d7c69 799 xen_invalidate_map_cache();
9ce94e7c
AS
800 break;
801 default:
802 hw_error("Invalid ioreq type 0x%x\n", req->type);
803 }
804}
805
fda1f768 806static int handle_buffered_iopage(XenIOState *state)
9ce94e7c
AS
807{
808 buf_ioreq_t *buf_req = NULL;
809 ioreq_t req;
810 int qw;
811
812 if (!state->buffered_io_page) {
fda1f768 813 return 0;
9ce94e7c
AS
814 }
815
fda1f768
SS
816 memset(&req, 0x00, sizeof(req));
817
9ce94e7c
AS
818 while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
819 buf_req = &state->buffered_io_page->buf_ioreq[
820 state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
821 req.size = 1UL << buf_req->size;
822 req.count = 1;
823 req.addr = buf_req->addr;
824 req.data = buf_req->data;
825 req.state = STATE_IOREQ_READY;
826 req.dir = buf_req->dir;
827 req.df = 1;
828 req.type = buf_req->type;
829 req.data_is_ptr = 0;
830 qw = (req.size == 8);
831 if (qw) {
832 buf_req = &state->buffered_io_page->buf_ioreq[
833 (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
834 req.data |= ((uint64_t)buf_req->data) << 32;
835 }
836
837 handle_ioreq(&req);
838
839 xen_mb();
840 state->buffered_io_page->read_pointer += qw ? 2 : 1;
841 }
fda1f768
SS
842
843 return req.count;
9ce94e7c
AS
844}
845
846static void handle_buffered_io(void *opaque)
847{
848 XenIOState *state = opaque;
849
fda1f768
SS
850 if (handle_buffered_iopage(state)) {
851 qemu_mod_timer(state->buffered_io_timer,
852 BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
853 } else {
854 qemu_del_timer(state->buffered_io_timer);
855 xc_evtchn_unmask(state->xce_handle, state->bufioreq_local_port);
856 }
9ce94e7c
AS
857}
858
859static void cpu_handle_ioreq(void *opaque)
860{
861 XenIOState *state = opaque;
862 ioreq_t *req = cpu_get_ioreq(state);
863
864 handle_buffered_iopage(state);
865 if (req) {
866 handle_ioreq(req);
867
868 if (req->state != STATE_IOREQ_INPROCESS) {
869 fprintf(stderr, "Badness in I/O request ... not in service?!: "
870 "%x, ptr: %x, port: %"PRIx64", "
871 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
872 req->state, req->data_is_ptr, req->addr,
873 req->data, req->count, req->size);
180640ea 874 destroy_hvm_domain(false);
9ce94e7c
AS
875 return;
876 }
877
878 xen_wmb(); /* Update ioreq contents /then/ update state. */
879
880 /*
881 * We do this before we send the response so that the tools
882 * have the opportunity to pick up on the reset before the
883 * guest resumes and does a hlt with interrupts disabled which
884 * causes Xen to powerdown the domain.
885 */
1354869c 886 if (runstate_is_running()) {
9ce94e7c 887 if (qemu_shutdown_requested_get()) {
180640ea 888 destroy_hvm_domain(false);
9ce94e7c
AS
889 }
890 if (qemu_reset_requested_get()) {
e063eb1f 891 qemu_system_reset(VMRESET_REPORT);
180640ea 892 destroy_hvm_domain(true);
9ce94e7c
AS
893 }
894 }
895
896 req->state = STATE_IORESP_READY;
897 xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
898 }
899}
900
0f51726a
SS
901static int store_dev_info(int domid, CharDriverState *cs, const char *string)
902{
903 struct xs_handle *xs = NULL;
904 char *path = NULL;
905 char *newpath = NULL;
906 char *pts = NULL;
907 int ret = -1;
908
909 /* Only continue if we're talking to a pty. */
910 if (strncmp(cs->filename, "pty:", 4)) {
911 return 0;
912 }
913 pts = cs->filename + 4;
914
915 /* We now have everything we need to set the xenstore entry. */
916 xs = xs_open(0);
917 if (xs == NULL) {
918 fprintf(stderr, "Could not contact XenStore\n");
919 goto out;
920 }
921
922 path = xs_get_domain_path(xs, domid);
923 if (path == NULL) {
924 fprintf(stderr, "xs_get_domain_path() error\n");
925 goto out;
926 }
927 newpath = realloc(path, (strlen(path) + strlen(string) +
928 strlen("/tty") + 1));
929 if (newpath == NULL) {
930 fprintf(stderr, "realloc error\n");
931 goto out;
932 }
933 path = newpath;
934
935 strcat(path, string);
936 strcat(path, "/tty");
937 if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
938 fprintf(stderr, "xs_write for '%s' fail", string);
939 goto out;
940 }
941 ret = 0;
942
943out:
944 free(path);
945 xs_close(xs);
946
947 return ret;
948}
949
950void xenstore_store_pv_console_info(int i, CharDriverState *chr)
951{
952 if (i == 0) {
953 store_dev_info(xen_domid, chr, "/console");
954 } else {
955 char buf[32];
956 snprintf(buf, sizeof(buf), "/device/console/%d", i);
957 store_dev_info(xen_domid, chr, buf);
958 }
959}
960
fb4bb2b5 961static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
29321335
AP
962{
963 char path[50];
964
fb4bb2b5
AP
965 if (xs == NULL) {
966 fprintf(stderr, "xenstore connection not initialized\n");
967 exit(1);
968 }
969
29321335 970 snprintf(path, sizeof (path), "/local/domain/0/device-model/%u/state", xen_domid);
fb4bb2b5 971 if (!xs_write(xs, XBT_NULL, path, state, strlen(state))) {
29321335
AP
972 fprintf(stderr, "error recording dm state\n");
973 exit(1);
974 }
975}
976
9ce94e7c
AS
977static void xen_main_loop_prepare(XenIOState *state)
978{
979 int evtchn_fd = -1;
980
981 if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
982 evtchn_fd = xc_evtchn_fd(state->xce_handle);
983 }
984
985 state->buffered_io_timer = qemu_new_timer_ms(rt_clock, handle_buffered_io,
986 state);
9ce94e7c
AS
987
988 if (evtchn_fd != -1) {
989 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
990 }
991}
992
993
3285cf4f
AP
994/* Initialise Xen */
995
1dfb4dd9
LC
996static void xen_change_state_handler(void *opaque, int running,
997 RunState state)
fb4bb2b5
AP
998{
999 if (running) {
1000 /* record state running */
1001 xenstore_record_dm_state(xenstore, "running");
1002 }
1003}
1004
1dfb4dd9
LC
1005static void xen_hvm_change_state_handler(void *opaque, int running,
1006 RunState rstate)
9ce94e7c 1007{
1dfb4dd9 1008 XenIOState *xstate = opaque;
9ce94e7c 1009 if (running) {
1dfb4dd9 1010 xen_main_loop_prepare(xstate);
9ce94e7c
AS
1011 }
1012}
1013
9e8dd451 1014static void xen_exit_notifier(Notifier *n, void *data)
9ce94e7c
AS
1015{
1016 XenIOState *state = container_of(n, XenIOState, exit);
1017
1018 xc_evtchn_close(state->xce_handle);
29321335 1019 xs_daemon_close(state->xenstore);
9ce94e7c
AS
1020}
1021
3285cf4f
AP
1022int xen_init(void)
1023{
1024 xen_xc = xen_xc_interface_open(0, 0, 0);
1025 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
1026 xen_be_printf(NULL, 0, "can't open xen interface\n");
1027 return -1;
1028 }
fb4bb2b5 1029 qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
3285cf4f
AP
1030
1031 return 0;
1032}
29d3ccde 1033
d1814e08
SS
1034static void xen_read_physmap(XenIOState *state)
1035{
1036 XenPhysmap *physmap = NULL;
1037 unsigned int len, num, i;
1038 char path[80], *value = NULL;
1039 char **entries = NULL;
1040
1041 snprintf(path, sizeof(path),
1042 "/local/domain/0/device-model/%d/physmap", xen_domid);
1043 entries = xs_directory(state->xenstore, 0, path, &num);
1044 if (entries == NULL)
1045 return;
1046
1047 for (i = 0; i < num; i++) {
1048 physmap = g_malloc(sizeof (XenPhysmap));
1049 physmap->phys_offset = strtoull(entries[i], NULL, 16);
1050 snprintf(path, sizeof(path),
1051 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1052 xen_domid, entries[i]);
1053 value = xs_read(state->xenstore, 0, path, &len);
1054 if (value == NULL) {
1055 free(physmap);
1056 continue;
1057 }
1058 physmap->start_addr = strtoull(value, NULL, 16);
1059 free(value);
1060
1061 snprintf(path, sizeof(path),
1062 "/local/domain/0/device-model/%d/physmap/%s/size",
1063 xen_domid, entries[i]);
1064 value = xs_read(state->xenstore, 0, path, &len);
1065 if (value == NULL) {
1066 free(physmap);
1067 continue;
1068 }
1069 physmap->size = strtoull(value, NULL, 16);
1070 free(value);
1071
1072 snprintf(path, sizeof(path),
1073 "/local/domain/0/device-model/%d/physmap/%s/name",
1074 xen_domid, entries[i]);
1075 physmap->name = xs_read(state->xenstore, 0, path, &len);
1076
1077 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
1078 }
1079 free(entries);
d1814e08
SS
1080}
1081
29d3ccde
AP
1082int xen_hvm_init(void)
1083{
9ce94e7c
AS
1084 int i, rc;
1085 unsigned long ioreq_pfn;
fda1f768 1086 unsigned long bufioreq_evtchn;
9ce94e7c
AS
1087 XenIOState *state;
1088
7267c094 1089 state = g_malloc0(sizeof (XenIOState));
9ce94e7c
AS
1090
1091 state->xce_handle = xen_xc_evtchn_open(NULL, 0);
1092 if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
1093 perror("xen: event channel open");
1094 return -errno;
1095 }
1096
29321335
AP
1097 state->xenstore = xs_daemon_open();
1098 if (state->xenstore == NULL) {
1099 perror("xen: xenstore open");
1100 return -errno;
1101 }
1102
9ce94e7c
AS
1103 state->exit.notify = xen_exit_notifier;
1104 qemu_add_exit_notifier(&state->exit);
1105
da98c8eb
GH
1106 state->suspend.notify = xen_suspend_notifier;
1107 qemu_register_suspend_notifier(&state->suspend);
1108
9ce94e7c
AS
1109 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn);
1110 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
1111 state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
1112 PROT_READ|PROT_WRITE, ioreq_pfn);
1113 if (state->shared_page == NULL) {
1114 hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
1115 errno, xen_xc);
1116 }
1117
1118 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
1119 DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
1120 state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
1121 PROT_READ|PROT_WRITE, ioreq_pfn);
1122 if (state->buffered_io_page == NULL) {
1123 hw_error("map buffered IO page returned error %d", errno);
1124 }
1125
7267c094 1126 state->ioreq_local_port = g_malloc0(smp_cpus * sizeof (evtchn_port_t));
9ce94e7c
AS
1127
1128 /* FIXME: how about if we overflow the page here? */
1129 for (i = 0; i < smp_cpus; i++) {
1130 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
1131 xen_vcpu_eport(state->shared_page, i));
1132 if (rc == -1) {
1133 fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
1134 return -1;
1135 }
1136 state->ioreq_local_port[i] = rc;
1137 }
1138
fda1f768
SS
1139 rc = xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_EVTCHN,
1140 &bufioreq_evtchn);
1141 if (rc < 0) {
1142 fprintf(stderr, "failed to get HVM_PARAM_BUFIOREQ_EVTCHN\n");
1143 return -1;
1144 }
1145 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
1146 (uint32_t)bufioreq_evtchn);
1147 if (rc == -1) {
1148 fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
1149 return -1;
1150 }
1151 state->bufioreq_local_port = rc;
1152
432d268c 1153 /* Init RAM management */
cd1ba7de 1154 xen_map_cache_init(xen_phys_offset_to_gaddr, state);
432d268c
JN
1155 xen_ram_init(ram_size);
1156
fb4bb2b5 1157 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
9ce94e7c 1158
20581d20 1159 state->memory_listener = xen_memory_listener;
b4dd7802 1160 QLIST_INIT(&state->physmap);
f6790af6 1161 memory_listener_register(&state->memory_listener, &address_space_memory);
b4dd7802
AP
1162 state->log_for_dirtybit = NULL;
1163
ad35a7da
SS
1164 /* Initialize backend core & drivers */
1165 if (xen_be_init() != 0) {
1166 fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
1167 exit(1);
1168 }
1169 xen_be_register("console", &xen_console_ops);
37cdfcf1 1170 xen_be_register("vkbd", &xen_kbdmouse_ops);
ad35a7da 1171 xen_be_register("qdisk", &xen_blkdev_ops);
d1814e08 1172 xen_read_physmap(state);
ad35a7da 1173
29d3ccde
AP
1174 return 0;
1175}
9ce94e7c 1176
180640ea 1177void destroy_hvm_domain(bool reboot)
9ce94e7c
AS
1178{
1179 XenXC xc_handle;
1180 int sts;
1181
1182 xc_handle = xen_xc_interface_open(0, 0, 0);
1183 if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
1184 fprintf(stderr, "Cannot acquire xenctrl handle\n");
1185 } else {
180640ea
JB
1186 sts = xc_domain_shutdown(xc_handle, xen_domid,
1187 reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff);
9ce94e7c 1188 if (sts != 0) {
180640ea
JB
1189 fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
1190 "sts %d, %s\n", reboot ? "reboot" : "poweroff",
1191 sts, strerror(errno));
9ce94e7c 1192 } else {
180640ea
JB
1193 fprintf(stderr, "Issued domain %d %s\n", xen_domid,
1194 reboot ? "reboot" : "poweroff");
9ce94e7c
AS
1195 }
1196 xc_interface_close(xc_handle);
1197 }
1198}
c65adf9b
AK
1199
1200void xen_register_framebuffer(MemoryRegion *mr)
1201{
1202 framebuffer = mr;
1203}
eaab4d60
AK
1204
1205void xen_shutdown_fatal_error(const char *fmt, ...)
1206{
1207 va_list ap;
1208
1209 va_start(ap, fmt);
1210 vfprintf(stderr, fmt, ap);
1211 va_end(ap);
1212 fprintf(stderr, "Will destroy the domain.\n");
1213 /* destroy the domain */
1214 qemu_system_shutdown_request();
1215}
910b38e4
AP
1216
1217void xen_modified_memory(ram_addr_t start, ram_addr_t length)
1218{
1219 if (unlikely(xen_in_migration)) {
1220 int rc;
1221 ram_addr_t start_pfn, nb_pages;
1222
1223 if (length == 0) {
1224 length = TARGET_PAGE_SIZE;
1225 }
1226 start_pfn = start >> TARGET_PAGE_BITS;
1227 nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
1228 - start_pfn;
1229 rc = xc_hvm_modified_memory(xen_xc, xen_domid, start_pfn, nb_pages);
1230 if (rc) {
1231 fprintf(stderr,
1232 "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
1233 __func__, start, nb_pages, rc, strerror(-rc));
1234 }
1235 }
1236}