Anthony PERARD | 3285cf4 | 2010-08-19 12:27:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Citrix Ltd. |
| 3 | * |
| 4 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 5 | * the COPYING file in the top-level directory. |
| 6 | * |
| 7 | */ |
| 8 | |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 9 | #include <sys/mman.h> |
| 10 | |
Anthony PERARD | 4144530 | 2010-07-16 14:55:39 +0100 | [diff] [blame] | 11 | #include "hw/pci.h" |
Anthony PERARD | c962247 | 2010-10-05 16:40:22 +0100 | [diff] [blame] | 12 | #include "hw/pc.h" |
Anthony PERARD | 3285cf4 | 2010-08-19 12:27:56 +0100 | [diff] [blame] | 13 | #include "hw/xen_common.h" |
| 14 | #include "hw/xen_backend.h" |
| 15 | |
Anthony PERARD | b4dd780 | 2011-05-24 14:34:21 +0100 | [diff] [blame] | 16 | #include "range.h" |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 17 | #include "xen-mapcache.h" |
| 18 | #include "trace.h" |
| 19 | |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 20 | #include <xen/hvm/ioreq.h> |
| 21 | #include <xen/hvm/params.h> |
Anthony PERARD | 8a369e2 | 2011-07-20 08:17:43 +0000 | [diff] [blame] | 22 | #include <xen/hvm/e820.h> |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 23 | |
| 24 | //#define DEBUG_XEN |
| 25 | |
| 26 | #ifdef DEBUG_XEN |
| 27 | #define DPRINTF(fmt, ...) \ |
| 28 | do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0) |
| 29 | #else |
| 30 | #define DPRINTF(fmt, ...) \ |
| 31 | do { } while (0) |
| 32 | #endif |
| 33 | |
| 34 | /* Compatibility with older version */ |
| 35 | #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a |
| 36 | static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i) |
| 37 | { |
| 38 | return shared_page->vcpu_iodata[i].vp_eport; |
| 39 | } |
| 40 | static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu) |
| 41 | { |
| 42 | return &shared_page->vcpu_iodata[vcpu].vp_ioreq; |
| 43 | } |
| 44 | # define FMT_ioreq_size PRIx64 |
| 45 | #else |
| 46 | static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i) |
| 47 | { |
| 48 | return shared_page->vcpu_ioreq[i].vp_eport; |
| 49 | } |
| 50 | static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu) |
| 51 | { |
| 52 | return &shared_page->vcpu_ioreq[vcpu]; |
| 53 | } |
| 54 | # define FMT_ioreq_size "u" |
| 55 | #endif |
| 56 | |
| 57 | #define BUFFER_IO_MAX_DELAY 100 |
| 58 | |
Anthony PERARD | b4dd780 | 2011-05-24 14:34:21 +0100 | [diff] [blame] | 59 | typedef struct XenPhysmap { |
| 60 | target_phys_addr_t start_addr; |
| 61 | ram_addr_t size; |
| 62 | target_phys_addr_t phys_offset; |
| 63 | |
| 64 | QLIST_ENTRY(XenPhysmap) list; |
| 65 | } XenPhysmap; |
| 66 | |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 67 | typedef struct XenIOState { |
| 68 | shared_iopage_t *shared_page; |
| 69 | buffered_iopage_t *buffered_io_page; |
| 70 | QEMUTimer *buffered_io_timer; |
| 71 | /* the evtchn port for polling the notification, */ |
| 72 | evtchn_port_t *ioreq_local_port; |
| 73 | /* the evtchn fd for polling */ |
| 74 | XenEvtchn xce_handle; |
| 75 | /* which vcpu we are serving */ |
| 76 | int send_vcpu; |
| 77 | |
Anthony PERARD | 2932133 | 2010-09-06 20:07:33 +0100 | [diff] [blame] | 78 | struct xs_handle *xenstore; |
Anthony PERARD | b4dd780 | 2011-05-24 14:34:21 +0100 | [diff] [blame] | 79 | CPUPhysMemoryClient client; |
| 80 | QLIST_HEAD(, XenPhysmap) physmap; |
| 81 | const XenPhysmap *log_for_dirtybit; |
Anthony PERARD | 2932133 | 2010-09-06 20:07:33 +0100 | [diff] [blame] | 82 | |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 83 | Notifier exit; |
| 84 | } XenIOState; |
| 85 | |
Anthony PERARD | 4144530 | 2010-07-16 14:55:39 +0100 | [diff] [blame] | 86 | /* Xen specific function for piix pci */ |
| 87 | |
| 88 | int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num) |
| 89 | { |
| 90 | return irq_num + ((pci_dev->devfn >> 3) << 2); |
| 91 | } |
| 92 | |
| 93 | void xen_piix3_set_irq(void *opaque, int irq_num, int level) |
| 94 | { |
| 95 | xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2, |
| 96 | irq_num & 3, level); |
| 97 | } |
| 98 | |
| 99 | void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len) |
| 100 | { |
| 101 | int i; |
| 102 | |
| 103 | /* Scan for updates to PCI link routes (0x60-0x63). */ |
| 104 | for (i = 0; i < len; i++) { |
| 105 | uint8_t v = (val >> (8 * i)) & 0xff; |
| 106 | if (v & 0x80) { |
| 107 | v = 0; |
| 108 | } |
| 109 | v &= 0xf; |
| 110 | if (((address + i) >= 0x60) && ((address + i) <= 0x63)) { |
| 111 | xc_hvm_set_pci_link_route(xen_xc, xen_domid, address + i - 0x60, v); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Anthony PERARD | c962247 | 2010-10-05 16:40:22 +0100 | [diff] [blame] | 116 | void xen_cmos_set_s3_resume(void *opaque, int irq, int level) |
| 117 | { |
| 118 | pc_cmos_set_s3_resume(opaque, irq, level); |
| 119 | if (level) { |
| 120 | xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3); |
| 121 | } |
| 122 | } |
| 123 | |
Anthony PERARD | 9c11a8a | 2010-06-30 17:50:10 +0100 | [diff] [blame] | 124 | /* Xen Interrupt Controller */ |
| 125 | |
| 126 | static void xen_set_irq(void *opaque, int irq, int level) |
| 127 | { |
| 128 | xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level); |
| 129 | } |
| 130 | |
| 131 | qemu_irq *xen_interrupt_controller_init(void) |
| 132 | { |
| 133 | return qemu_allocate_irqs(xen_set_irq, NULL, 16); |
| 134 | } |
| 135 | |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 136 | /* Memory Ops */ |
| 137 | |
| 138 | static void xen_ram_init(ram_addr_t ram_size) |
| 139 | { |
| 140 | RAMBlock *new_block; |
| 141 | ram_addr_t below_4g_mem_size, above_4g_mem_size = 0; |
| 142 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 143 | new_block = g_malloc0(sizeof (*new_block)); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 144 | pstrcpy(new_block->idstr, sizeof (new_block->idstr), "xen.ram"); |
| 145 | new_block->host = NULL; |
| 146 | new_block->offset = 0; |
| 147 | new_block->length = ram_size; |
Anthony PERARD | 8a369e2 | 2011-07-20 08:17:43 +0000 | [diff] [blame] | 148 | if (ram_size >= HVM_BELOW_4G_RAM_END) { |
| 149 | /* Xen does not allocate the memory continuously, and keep a hole at |
| 150 | * HVM_BELOW_4G_MMIO_START of HVM_BELOW_4G_MMIO_LENGTH |
| 151 | */ |
| 152 | new_block->length += HVM_BELOW_4G_MMIO_LENGTH; |
| 153 | } |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 154 | |
| 155 | QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next); |
| 156 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 157 | ram_list.phys_dirty = g_realloc(ram_list.phys_dirty, |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 158 | new_block->length >> TARGET_PAGE_BITS); |
| 159 | memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS), |
| 160 | 0xff, new_block->length >> TARGET_PAGE_BITS); |
| 161 | |
Anthony PERARD | 8a369e2 | 2011-07-20 08:17:43 +0000 | [diff] [blame] | 162 | if (ram_size >= HVM_BELOW_4G_RAM_END) { |
| 163 | above_4g_mem_size = ram_size - HVM_BELOW_4G_RAM_END; |
| 164 | below_4g_mem_size = HVM_BELOW_4G_RAM_END; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 165 | } else { |
| 166 | below_4g_mem_size = ram_size; |
| 167 | } |
| 168 | |
Anthony PERARD | 8a369e2 | 2011-07-20 08:17:43 +0000 | [diff] [blame] | 169 | cpu_register_physical_memory(0, 0xa0000, 0); |
| 170 | /* Skip of the VGA IO memory space, it will be registered later by the VGA |
| 171 | * emulated device. |
| 172 | * |
| 173 | * The area between 0xc0000 and 0x100000 will be used by SeaBIOS to load |
| 174 | * the Options ROM, so it is registered here as RAM. |
| 175 | */ |
| 176 | cpu_register_physical_memory(0xc0000, below_4g_mem_size - 0xc0000, |
| 177 | 0xc0000); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 178 | if (above_4g_mem_size > 0) { |
| 179 | cpu_register_physical_memory(0x100000000ULL, above_4g_mem_size, |
Anthony PERARD | 8a369e2 | 2011-07-20 08:17:43 +0000 | [diff] [blame] | 180 | 0x100000000ULL); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 181 | } |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size) |
| 185 | { |
| 186 | unsigned long nr_pfn; |
| 187 | xen_pfn_t *pfn_list; |
| 188 | int i; |
| 189 | |
| 190 | trace_xen_ram_alloc(ram_addr, size); |
| 191 | |
| 192 | nr_pfn = size >> TARGET_PAGE_BITS; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 193 | pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 194 | |
| 195 | for (i = 0; i < nr_pfn; i++) { |
| 196 | pfn_list[i] = (ram_addr >> TARGET_PAGE_BITS) + i; |
| 197 | } |
| 198 | |
| 199 | if (xc_domain_populate_physmap_exact(xen_xc, xen_domid, nr_pfn, 0, 0, pfn_list)) { |
Anthony PERARD | f15fbc4 | 2011-07-20 08:17:42 +0000 | [diff] [blame] | 200 | hw_error("xen: failed to populate ram at " RAM_ADDR_FMT, ram_addr); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 203 | g_free(pfn_list); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Anthony PERARD | b4dd780 | 2011-05-24 14:34:21 +0100 | [diff] [blame] | 206 | static XenPhysmap *get_physmapping(XenIOState *state, |
| 207 | target_phys_addr_t start_addr, ram_addr_t size) |
| 208 | { |
| 209 | XenPhysmap *physmap = NULL; |
| 210 | |
| 211 | start_addr &= TARGET_PAGE_MASK; |
| 212 | |
| 213 | QLIST_FOREACH(physmap, &state->physmap, list) { |
| 214 | if (range_covers_byte(physmap->start_addr, physmap->size, start_addr)) { |
| 215 | return physmap; |
| 216 | } |
| 217 | } |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | #if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340 |
| 222 | static int xen_add_to_physmap(XenIOState *state, |
| 223 | target_phys_addr_t start_addr, |
| 224 | ram_addr_t size, |
| 225 | target_phys_addr_t phys_offset) |
| 226 | { |
| 227 | unsigned long i = 0; |
| 228 | int rc = 0; |
| 229 | XenPhysmap *physmap = NULL; |
| 230 | target_phys_addr_t pfn, start_gpfn; |
Stefano Stabellini | ebed850 | 2011-06-15 17:29:27 +0100 | [diff] [blame] | 231 | RAMBlock *block; |
Anthony PERARD | b4dd780 | 2011-05-24 14:34:21 +0100 | [diff] [blame] | 232 | |
| 233 | if (get_physmapping(state, start_addr, size)) { |
| 234 | return 0; |
| 235 | } |
| 236 | if (size <= 0) { |
| 237 | return -1; |
| 238 | } |
| 239 | |
Stefano Stabellini | ebed850 | 2011-06-15 17:29:27 +0100 | [diff] [blame] | 240 | /* Xen can only handle a single dirty log region for now and we want |
| 241 | * the linear framebuffer to be that region. |
| 242 | * Avoid tracking any regions that is not videoram and avoid tracking |
| 243 | * the legacy vga region. */ |
| 244 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
| 245 | if (!strcmp(block->idstr, "vga.vram") && block->offset == phys_offset |
| 246 | && start_addr > 0xbffff) { |
| 247 | goto go_physmap; |
| 248 | } |
| 249 | } |
| 250 | return -1; |
| 251 | |
| 252 | go_physmap: |
Anthony PERARD | b4dd780 | 2011-05-24 14:34:21 +0100 | [diff] [blame] | 253 | DPRINTF("mapping vram to %llx - %llx, from %llx\n", |
| 254 | start_addr, start_addr + size, phys_offset); |
| 255 | |
| 256 | pfn = phys_offset >> TARGET_PAGE_BITS; |
| 257 | start_gpfn = start_addr >> TARGET_PAGE_BITS; |
| 258 | for (i = 0; i < size >> TARGET_PAGE_BITS; i++) { |
| 259 | unsigned long idx = pfn + i; |
| 260 | xen_pfn_t gpfn = start_gpfn + i; |
| 261 | |
| 262 | rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn); |
| 263 | if (rc) { |
| 264 | DPRINTF("add_to_physmap MFN %"PRI_xen_pfn" to PFN %" |
| 265 | PRI_xen_pfn" failed: %d\n", idx, gpfn, rc); |
| 266 | return -rc; |
| 267 | } |
| 268 | } |
| 269 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 270 | physmap = g_malloc(sizeof (XenPhysmap)); |
Anthony PERARD | b4dd780 | 2011-05-24 14:34:21 +0100 | [diff] [blame] | 271 | |
| 272 | physmap->start_addr = start_addr; |
| 273 | physmap->size = size; |
| 274 | physmap->phys_offset = phys_offset; |
| 275 | |
| 276 | QLIST_INSERT_HEAD(&state->physmap, physmap, list); |
| 277 | |
| 278 | xc_domain_pin_memory_cacheattr(xen_xc, xen_domid, |
| 279 | start_addr >> TARGET_PAGE_BITS, |
| 280 | (start_addr + size) >> TARGET_PAGE_BITS, |
| 281 | XEN_DOMCTL_MEM_CACHEATTR_WB); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int xen_remove_from_physmap(XenIOState *state, |
| 286 | target_phys_addr_t start_addr, |
| 287 | ram_addr_t size) |
| 288 | { |
| 289 | unsigned long i = 0; |
| 290 | int rc = 0; |
| 291 | XenPhysmap *physmap = NULL; |
| 292 | target_phys_addr_t phys_offset = 0; |
| 293 | |
| 294 | physmap = get_physmapping(state, start_addr, size); |
| 295 | if (physmap == NULL) { |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | phys_offset = physmap->phys_offset; |
| 300 | size = physmap->size; |
| 301 | |
| 302 | DPRINTF("unmapping vram to %llx - %llx, from %llx\n", |
| 303 | phys_offset, phys_offset + size, start_addr); |
| 304 | |
| 305 | size >>= TARGET_PAGE_BITS; |
| 306 | start_addr >>= TARGET_PAGE_BITS; |
| 307 | phys_offset >>= TARGET_PAGE_BITS; |
| 308 | for (i = 0; i < size; i++) { |
| 309 | unsigned long idx = start_addr + i; |
| 310 | xen_pfn_t gpfn = phys_offset + i; |
| 311 | |
| 312 | rc = xc_domain_add_to_physmap(xen_xc, xen_domid, XENMAPSPACE_gmfn, idx, gpfn); |
| 313 | if (rc) { |
| 314 | fprintf(stderr, "add_to_physmap MFN %"PRI_xen_pfn" to PFN %" |
| 315 | PRI_xen_pfn" failed: %d\n", idx, gpfn, rc); |
| 316 | return -rc; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | QLIST_REMOVE(physmap, list); |
| 321 | if (state->log_for_dirtybit == physmap) { |
| 322 | state->log_for_dirtybit = NULL; |
| 323 | } |
| 324 | free(physmap); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | #else |
| 330 | static int xen_add_to_physmap(XenIOState *state, |
| 331 | target_phys_addr_t start_addr, |
| 332 | ram_addr_t size, |
| 333 | target_phys_addr_t phys_offset) |
| 334 | { |
| 335 | return -ENOSYS; |
| 336 | } |
| 337 | |
| 338 | static int xen_remove_from_physmap(XenIOState *state, |
| 339 | target_phys_addr_t start_addr, |
| 340 | ram_addr_t size) |
| 341 | { |
| 342 | return -ENOSYS; |
| 343 | } |
| 344 | #endif |
| 345 | |
| 346 | static void xen_client_set_memory(struct CPUPhysMemoryClient *client, |
| 347 | target_phys_addr_t start_addr, |
| 348 | ram_addr_t size, |
| 349 | ram_addr_t phys_offset, |
| 350 | bool log_dirty) |
| 351 | { |
| 352 | XenIOState *state = container_of(client, XenIOState, client); |
| 353 | ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK; |
| 354 | hvmmem_type_t mem_type; |
| 355 | |
| 356 | if (!(start_addr != phys_offset |
| 357 | && ( (log_dirty && flags < IO_MEM_UNASSIGNED) |
| 358 | || (!log_dirty && flags == IO_MEM_UNASSIGNED)))) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | trace_xen_client_set_memory(start_addr, size, phys_offset, log_dirty); |
| 363 | |
| 364 | start_addr &= TARGET_PAGE_MASK; |
| 365 | size = TARGET_PAGE_ALIGN(size); |
| 366 | phys_offset &= TARGET_PAGE_MASK; |
| 367 | |
| 368 | switch (flags) { |
| 369 | case IO_MEM_RAM: |
| 370 | xen_add_to_physmap(state, start_addr, size, phys_offset); |
| 371 | break; |
| 372 | case IO_MEM_ROM: |
| 373 | mem_type = HVMMEM_ram_ro; |
| 374 | if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type, |
| 375 | start_addr >> TARGET_PAGE_BITS, |
| 376 | size >> TARGET_PAGE_BITS)) { |
| 377 | DPRINTF("xc_hvm_set_mem_type error, addr: "TARGET_FMT_plx"\n", |
| 378 | start_addr); |
| 379 | } |
| 380 | break; |
| 381 | case IO_MEM_UNASSIGNED: |
| 382 | if (xen_remove_from_physmap(state, start_addr, size) < 0) { |
| 383 | DPRINTF("physmapping does not exist at "TARGET_FMT_plx"\n", start_addr); |
| 384 | } |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | static int xen_sync_dirty_bitmap(XenIOState *state, |
| 390 | target_phys_addr_t start_addr, |
| 391 | ram_addr_t size) |
| 392 | { |
| 393 | target_phys_addr_t npages = size >> TARGET_PAGE_BITS; |
| 394 | target_phys_addr_t vram_offset = 0; |
| 395 | const int width = sizeof(unsigned long) * 8; |
| 396 | unsigned long bitmap[(npages + width - 1) / width]; |
| 397 | int rc, i, j; |
| 398 | const XenPhysmap *physmap = NULL; |
| 399 | |
| 400 | physmap = get_physmapping(state, start_addr, size); |
| 401 | if (physmap == NULL) { |
| 402 | /* not handled */ |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | if (state->log_for_dirtybit == NULL) { |
| 407 | state->log_for_dirtybit = physmap; |
| 408 | } else if (state->log_for_dirtybit != physmap) { |
| 409 | return -1; |
| 410 | } |
| 411 | vram_offset = physmap->phys_offset; |
| 412 | |
| 413 | rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid, |
| 414 | start_addr >> TARGET_PAGE_BITS, npages, |
| 415 | bitmap); |
| 416 | if (rc) { |
| 417 | return rc; |
| 418 | } |
| 419 | |
| 420 | for (i = 0; i < ARRAY_SIZE(bitmap); i++) { |
| 421 | unsigned long map = bitmap[i]; |
| 422 | while (map != 0) { |
| 423 | j = ffsl(map) - 1; |
| 424 | map &= ~(1ul << j); |
| 425 | cpu_physical_memory_set_dirty(vram_offset + (i * width + j) * TARGET_PAGE_SIZE); |
| 426 | }; |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static int xen_log_start(CPUPhysMemoryClient *client, target_phys_addr_t phys_addr, ram_addr_t size) |
| 433 | { |
| 434 | XenIOState *state = container_of(client, XenIOState, client); |
| 435 | |
| 436 | return xen_sync_dirty_bitmap(state, phys_addr, size); |
| 437 | } |
| 438 | |
| 439 | static int xen_log_stop(CPUPhysMemoryClient *client, target_phys_addr_t phys_addr, ram_addr_t size) |
| 440 | { |
| 441 | XenIOState *state = container_of(client, XenIOState, client); |
| 442 | |
| 443 | state->log_for_dirtybit = NULL; |
| 444 | /* Disable dirty bit tracking */ |
| 445 | return xc_hvm_track_dirty_vram(xen_xc, xen_domid, 0, 0, NULL); |
| 446 | } |
| 447 | |
| 448 | static int xen_client_sync_dirty_bitmap(struct CPUPhysMemoryClient *client, |
| 449 | target_phys_addr_t start_addr, |
| 450 | target_phys_addr_t end_addr) |
| 451 | { |
| 452 | XenIOState *state = container_of(client, XenIOState, client); |
| 453 | |
| 454 | return xen_sync_dirty_bitmap(state, start_addr, end_addr - start_addr); |
| 455 | } |
| 456 | |
| 457 | static int xen_client_migration_log(struct CPUPhysMemoryClient *client, |
| 458 | int enable) |
| 459 | { |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | static CPUPhysMemoryClient xen_cpu_phys_memory_client = { |
| 464 | .set_memory = xen_client_set_memory, |
| 465 | .sync_dirty_bitmap = xen_client_sync_dirty_bitmap, |
| 466 | .migration_log = xen_client_migration_log, |
| 467 | .log_start = xen_log_start, |
| 468 | .log_stop = xen_log_stop, |
| 469 | }; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 470 | |
Anthony PERARD | 29d3ccd | 2010-06-30 12:58:34 +0100 | [diff] [blame] | 471 | /* VCPU Operations, MMIO, IO ring ... */ |
| 472 | |
| 473 | static void xen_reset_vcpu(void *opaque) |
| 474 | { |
| 475 | CPUState *env = opaque; |
| 476 | |
| 477 | env->halted = 1; |
| 478 | } |
| 479 | |
| 480 | void xen_vcpu_init(void) |
| 481 | { |
| 482 | CPUState *first_cpu; |
| 483 | |
| 484 | if ((first_cpu = qemu_get_cpu(0))) { |
| 485 | qemu_register_reset(xen_reset_vcpu, first_cpu); |
| 486 | xen_reset_vcpu(first_cpu); |
| 487 | } |
| 488 | } |
| 489 | |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 490 | /* get the ioreq packets from share mem */ |
| 491 | static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu) |
| 492 | { |
| 493 | ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu); |
| 494 | |
| 495 | if (req->state != STATE_IOREQ_READY) { |
| 496 | DPRINTF("I/O request not ready: " |
| 497 | "%x, ptr: %x, port: %"PRIx64", " |
| 498 | "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n", |
| 499 | req->state, req->data_is_ptr, req->addr, |
| 500 | req->data, req->count, req->size); |
| 501 | return NULL; |
| 502 | } |
| 503 | |
| 504 | xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */ |
| 505 | |
| 506 | req->state = STATE_IOREQ_INPROCESS; |
| 507 | return req; |
| 508 | } |
| 509 | |
| 510 | /* use poll to get the port notification */ |
| 511 | /* ioreq_vec--out,the */ |
| 512 | /* retval--the number of ioreq packet */ |
| 513 | static ioreq_t *cpu_get_ioreq(XenIOState *state) |
| 514 | { |
| 515 | int i; |
| 516 | evtchn_port_t port; |
| 517 | |
| 518 | port = xc_evtchn_pending(state->xce_handle); |
| 519 | if (port != -1) { |
| 520 | for (i = 0; i < smp_cpus; i++) { |
| 521 | if (state->ioreq_local_port[i] == port) { |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (i == smp_cpus) { |
| 527 | hw_error("Fatal error while trying to get io event!\n"); |
| 528 | } |
| 529 | |
| 530 | /* unmask the wanted port again */ |
| 531 | xc_evtchn_unmask(state->xce_handle, port); |
| 532 | |
| 533 | /* get the io packet from shared memory */ |
| 534 | state->send_vcpu = i; |
| 535 | return cpu_get_ioreq_from_shared_memory(state, i); |
| 536 | } |
| 537 | |
| 538 | /* read error or read nothing */ |
| 539 | return NULL; |
| 540 | } |
| 541 | |
| 542 | static uint32_t do_inp(pio_addr_t addr, unsigned long size) |
| 543 | { |
| 544 | switch (size) { |
| 545 | case 1: |
| 546 | return cpu_inb(addr); |
| 547 | case 2: |
| 548 | return cpu_inw(addr); |
| 549 | case 4: |
| 550 | return cpu_inl(addr); |
| 551 | default: |
| 552 | hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | static void do_outp(pio_addr_t addr, |
| 557 | unsigned long size, uint32_t val) |
| 558 | { |
| 559 | switch (size) { |
| 560 | case 1: |
| 561 | return cpu_outb(addr, val); |
| 562 | case 2: |
| 563 | return cpu_outw(addr, val); |
| 564 | case 4: |
| 565 | return cpu_outl(addr, val); |
| 566 | default: |
| 567 | hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | static void cpu_ioreq_pio(ioreq_t *req) |
| 572 | { |
| 573 | int i, sign; |
| 574 | |
| 575 | sign = req->df ? -1 : 1; |
| 576 | |
| 577 | if (req->dir == IOREQ_READ) { |
| 578 | if (!req->data_is_ptr) { |
| 579 | req->data = do_inp(req->addr, req->size); |
| 580 | } else { |
| 581 | uint32_t tmp; |
| 582 | |
| 583 | for (i = 0; i < req->count; i++) { |
| 584 | tmp = do_inp(req->addr, req->size); |
| 585 | cpu_physical_memory_write(req->data + (sign * i * req->size), |
| 586 | (uint8_t *) &tmp, req->size); |
| 587 | } |
| 588 | } |
| 589 | } else if (req->dir == IOREQ_WRITE) { |
| 590 | if (!req->data_is_ptr) { |
| 591 | do_outp(req->addr, req->size, req->data); |
| 592 | } else { |
| 593 | for (i = 0; i < req->count; i++) { |
| 594 | uint32_t tmp = 0; |
| 595 | |
| 596 | cpu_physical_memory_read(req->data + (sign * i * req->size), |
| 597 | (uint8_t*) &tmp, req->size); |
| 598 | do_outp(req->addr, req->size, tmp); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | static void cpu_ioreq_move(ioreq_t *req) |
| 605 | { |
| 606 | int i, sign; |
| 607 | |
| 608 | sign = req->df ? -1 : 1; |
| 609 | |
| 610 | if (!req->data_is_ptr) { |
| 611 | if (req->dir == IOREQ_READ) { |
| 612 | for (i = 0; i < req->count; i++) { |
| 613 | cpu_physical_memory_read(req->addr + (sign * i * req->size), |
| 614 | (uint8_t *) &req->data, req->size); |
| 615 | } |
| 616 | } else if (req->dir == IOREQ_WRITE) { |
| 617 | for (i = 0; i < req->count; i++) { |
| 618 | cpu_physical_memory_write(req->addr + (sign * i * req->size), |
| 619 | (uint8_t *) &req->data, req->size); |
| 620 | } |
| 621 | } |
| 622 | } else { |
Stefano Stabellini | 2b73434 | 2011-05-20 16:57:24 +0000 | [diff] [blame] | 623 | uint64_t tmp; |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 624 | |
| 625 | if (req->dir == IOREQ_READ) { |
| 626 | for (i = 0; i < req->count; i++) { |
| 627 | cpu_physical_memory_read(req->addr + (sign * i * req->size), |
| 628 | (uint8_t*) &tmp, req->size); |
| 629 | cpu_physical_memory_write(req->data + (sign * i * req->size), |
| 630 | (uint8_t*) &tmp, req->size); |
| 631 | } |
| 632 | } else if (req->dir == IOREQ_WRITE) { |
| 633 | for (i = 0; i < req->count; i++) { |
| 634 | cpu_physical_memory_read(req->data + (sign * i * req->size), |
| 635 | (uint8_t*) &tmp, req->size); |
| 636 | cpu_physical_memory_write(req->addr + (sign * i * req->size), |
| 637 | (uint8_t*) &tmp, req->size); |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | static void handle_ioreq(ioreq_t *req) |
| 644 | { |
| 645 | if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) && |
| 646 | (req->size < sizeof (target_ulong))) { |
| 647 | req->data &= ((target_ulong) 1 << (8 * req->size)) - 1; |
| 648 | } |
| 649 | |
| 650 | switch (req->type) { |
| 651 | case IOREQ_TYPE_PIO: |
| 652 | cpu_ioreq_pio(req); |
| 653 | break; |
| 654 | case IOREQ_TYPE_COPY: |
| 655 | cpu_ioreq_move(req); |
| 656 | break; |
| 657 | case IOREQ_TYPE_TIMEOFFSET: |
| 658 | break; |
| 659 | case IOREQ_TYPE_INVALIDATE: |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 660 | xen_invalidate_map_cache(); |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 661 | break; |
| 662 | default: |
| 663 | hw_error("Invalid ioreq type 0x%x\n", req->type); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | static void handle_buffered_iopage(XenIOState *state) |
| 668 | { |
| 669 | buf_ioreq_t *buf_req = NULL; |
| 670 | ioreq_t req; |
| 671 | int qw; |
| 672 | |
| 673 | if (!state->buffered_io_page) { |
| 674 | return; |
| 675 | } |
| 676 | |
| 677 | while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) { |
| 678 | buf_req = &state->buffered_io_page->buf_ioreq[ |
| 679 | state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM]; |
| 680 | req.size = 1UL << buf_req->size; |
| 681 | req.count = 1; |
| 682 | req.addr = buf_req->addr; |
| 683 | req.data = buf_req->data; |
| 684 | req.state = STATE_IOREQ_READY; |
| 685 | req.dir = buf_req->dir; |
| 686 | req.df = 1; |
| 687 | req.type = buf_req->type; |
| 688 | req.data_is_ptr = 0; |
| 689 | qw = (req.size == 8); |
| 690 | if (qw) { |
| 691 | buf_req = &state->buffered_io_page->buf_ioreq[ |
| 692 | (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM]; |
| 693 | req.data |= ((uint64_t)buf_req->data) << 32; |
| 694 | } |
| 695 | |
| 696 | handle_ioreq(&req); |
| 697 | |
| 698 | xen_mb(); |
| 699 | state->buffered_io_page->read_pointer += qw ? 2 : 1; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | static void handle_buffered_io(void *opaque) |
| 704 | { |
| 705 | XenIOState *state = opaque; |
| 706 | |
| 707 | handle_buffered_iopage(state); |
| 708 | qemu_mod_timer(state->buffered_io_timer, |
| 709 | BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock)); |
| 710 | } |
| 711 | |
| 712 | static void cpu_handle_ioreq(void *opaque) |
| 713 | { |
| 714 | XenIOState *state = opaque; |
| 715 | ioreq_t *req = cpu_get_ioreq(state); |
| 716 | |
| 717 | handle_buffered_iopage(state); |
| 718 | if (req) { |
| 719 | handle_ioreq(req); |
| 720 | |
| 721 | if (req->state != STATE_IOREQ_INPROCESS) { |
| 722 | fprintf(stderr, "Badness in I/O request ... not in service?!: " |
| 723 | "%x, ptr: %x, port: %"PRIx64", " |
| 724 | "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n", |
| 725 | req->state, req->data_is_ptr, req->addr, |
| 726 | req->data, req->count, req->size); |
| 727 | destroy_hvm_domain(); |
| 728 | return; |
| 729 | } |
| 730 | |
| 731 | xen_wmb(); /* Update ioreq contents /then/ update state. */ |
| 732 | |
| 733 | /* |
| 734 | * We do this before we send the response so that the tools |
| 735 | * have the opportunity to pick up on the reset before the |
| 736 | * guest resumes and does a hlt with interrupts disabled which |
| 737 | * causes Xen to powerdown the domain. |
| 738 | */ |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 739 | if (runstate_is_running()) { |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 740 | if (qemu_shutdown_requested_get()) { |
| 741 | destroy_hvm_domain(); |
| 742 | } |
| 743 | if (qemu_reset_requested_get()) { |
Jan Kiszka | e063eb1 | 2011-06-14 18:29:43 +0200 | [diff] [blame] | 744 | qemu_system_reset(VMRESET_REPORT); |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
| 748 | req->state = STATE_IORESP_READY; |
| 749 | xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]); |
| 750 | } |
| 751 | } |
| 752 | |
Stefano Stabellini | 0f51726 | 2011-06-30 18:26:29 +0100 | [diff] [blame] | 753 | static int store_dev_info(int domid, CharDriverState *cs, const char *string) |
| 754 | { |
| 755 | struct xs_handle *xs = NULL; |
| 756 | char *path = NULL; |
| 757 | char *newpath = NULL; |
| 758 | char *pts = NULL; |
| 759 | int ret = -1; |
| 760 | |
| 761 | /* Only continue if we're talking to a pty. */ |
| 762 | if (strncmp(cs->filename, "pty:", 4)) { |
| 763 | return 0; |
| 764 | } |
| 765 | pts = cs->filename + 4; |
| 766 | |
| 767 | /* We now have everything we need to set the xenstore entry. */ |
| 768 | xs = xs_open(0); |
| 769 | if (xs == NULL) { |
| 770 | fprintf(stderr, "Could not contact XenStore\n"); |
| 771 | goto out; |
| 772 | } |
| 773 | |
| 774 | path = xs_get_domain_path(xs, domid); |
| 775 | if (path == NULL) { |
| 776 | fprintf(stderr, "xs_get_domain_path() error\n"); |
| 777 | goto out; |
| 778 | } |
| 779 | newpath = realloc(path, (strlen(path) + strlen(string) + |
| 780 | strlen("/tty") + 1)); |
| 781 | if (newpath == NULL) { |
| 782 | fprintf(stderr, "realloc error\n"); |
| 783 | goto out; |
| 784 | } |
| 785 | path = newpath; |
| 786 | |
| 787 | strcat(path, string); |
| 788 | strcat(path, "/tty"); |
| 789 | if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) { |
| 790 | fprintf(stderr, "xs_write for '%s' fail", string); |
| 791 | goto out; |
| 792 | } |
| 793 | ret = 0; |
| 794 | |
| 795 | out: |
| 796 | free(path); |
| 797 | xs_close(xs); |
| 798 | |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | void xenstore_store_pv_console_info(int i, CharDriverState *chr) |
| 803 | { |
| 804 | if (i == 0) { |
| 805 | store_dev_info(xen_domid, chr, "/console"); |
| 806 | } else { |
| 807 | char buf[32]; |
| 808 | snprintf(buf, sizeof(buf), "/device/console/%d", i); |
| 809 | store_dev_info(xen_domid, chr, buf); |
| 810 | } |
| 811 | } |
| 812 | |
Anthony PERARD | fb4bb2b | 2011-07-15 00:33:42 +0000 | [diff] [blame] | 813 | static void xenstore_record_dm_state(struct xs_handle *xs, const char *state) |
Anthony PERARD | 2932133 | 2010-09-06 20:07:33 +0100 | [diff] [blame] | 814 | { |
| 815 | char path[50]; |
| 816 | |
Anthony PERARD | fb4bb2b | 2011-07-15 00:33:42 +0000 | [diff] [blame] | 817 | if (xs == NULL) { |
| 818 | fprintf(stderr, "xenstore connection not initialized\n"); |
| 819 | exit(1); |
| 820 | } |
| 821 | |
Anthony PERARD | 2932133 | 2010-09-06 20:07:33 +0100 | [diff] [blame] | 822 | snprintf(path, sizeof (path), "/local/domain/0/device-model/%u/state", xen_domid); |
Anthony PERARD | fb4bb2b | 2011-07-15 00:33:42 +0000 | [diff] [blame] | 823 | if (!xs_write(xs, XBT_NULL, path, state, strlen(state))) { |
Anthony PERARD | 2932133 | 2010-09-06 20:07:33 +0100 | [diff] [blame] | 824 | fprintf(stderr, "error recording dm state\n"); |
| 825 | exit(1); |
| 826 | } |
| 827 | } |
| 828 | |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 829 | static void xen_main_loop_prepare(XenIOState *state) |
| 830 | { |
| 831 | int evtchn_fd = -1; |
| 832 | |
| 833 | if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) { |
| 834 | evtchn_fd = xc_evtchn_fd(state->xce_handle); |
| 835 | } |
| 836 | |
| 837 | state->buffered_io_timer = qemu_new_timer_ms(rt_clock, handle_buffered_io, |
| 838 | state); |
| 839 | qemu_mod_timer(state->buffered_io_timer, qemu_get_clock_ms(rt_clock)); |
| 840 | |
| 841 | if (evtchn_fd != -1) { |
| 842 | qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | |
Anthony PERARD | 3285cf4 | 2010-08-19 12:27:56 +0100 | [diff] [blame] | 847 | /* Initialise Xen */ |
| 848 | |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 849 | static void xen_change_state_handler(void *opaque, int running, |
| 850 | RunState state) |
Anthony PERARD | fb4bb2b | 2011-07-15 00:33:42 +0000 | [diff] [blame] | 851 | { |
| 852 | if (running) { |
| 853 | /* record state running */ |
| 854 | xenstore_record_dm_state(xenstore, "running"); |
| 855 | } |
| 856 | } |
| 857 | |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 858 | static void xen_hvm_change_state_handler(void *opaque, int running, |
| 859 | RunState rstate) |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 860 | { |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 861 | XenIOState *xstate = opaque; |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 862 | if (running) { |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 863 | xen_main_loop_prepare(xstate); |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 867 | static void xen_exit_notifier(Notifier *n, void *data) |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 868 | { |
| 869 | XenIOState *state = container_of(n, XenIOState, exit); |
| 870 | |
| 871 | xc_evtchn_close(state->xce_handle); |
Anthony PERARD | 2932133 | 2010-09-06 20:07:33 +0100 | [diff] [blame] | 872 | xs_daemon_close(state->xenstore); |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 873 | } |
| 874 | |
Anthony PERARD | 3285cf4 | 2010-08-19 12:27:56 +0100 | [diff] [blame] | 875 | int xen_init(void) |
| 876 | { |
| 877 | xen_xc = xen_xc_interface_open(0, 0, 0); |
| 878 | if (xen_xc == XC_HANDLER_INITIAL_VALUE) { |
| 879 | xen_be_printf(NULL, 0, "can't open xen interface\n"); |
| 880 | return -1; |
| 881 | } |
Anthony PERARD | fb4bb2b | 2011-07-15 00:33:42 +0000 | [diff] [blame] | 882 | qemu_add_vm_change_state_handler(xen_change_state_handler, NULL); |
Anthony PERARD | 3285cf4 | 2010-08-19 12:27:56 +0100 | [diff] [blame] | 883 | |
| 884 | return 0; |
| 885 | } |
Anthony PERARD | 29d3ccd | 2010-06-30 12:58:34 +0100 | [diff] [blame] | 886 | |
| 887 | int xen_hvm_init(void) |
| 888 | { |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 889 | int i, rc; |
| 890 | unsigned long ioreq_pfn; |
| 891 | XenIOState *state; |
| 892 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 893 | state = g_malloc0(sizeof (XenIOState)); |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 894 | |
| 895 | state->xce_handle = xen_xc_evtchn_open(NULL, 0); |
| 896 | if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) { |
| 897 | perror("xen: event channel open"); |
| 898 | return -errno; |
| 899 | } |
| 900 | |
Anthony PERARD | 2932133 | 2010-09-06 20:07:33 +0100 | [diff] [blame] | 901 | state->xenstore = xs_daemon_open(); |
| 902 | if (state->xenstore == NULL) { |
| 903 | perror("xen: xenstore open"); |
| 904 | return -errno; |
| 905 | } |
| 906 | |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 907 | state->exit.notify = xen_exit_notifier; |
| 908 | qemu_add_exit_notifier(&state->exit); |
| 909 | |
| 910 | xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn); |
| 911 | DPRINTF("shared page at pfn %lx\n", ioreq_pfn); |
| 912 | state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE, |
| 913 | PROT_READ|PROT_WRITE, ioreq_pfn); |
| 914 | if (state->shared_page == NULL) { |
| 915 | hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT, |
| 916 | errno, xen_xc); |
| 917 | } |
| 918 | |
| 919 | xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn); |
| 920 | DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn); |
| 921 | state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE, |
| 922 | PROT_READ|PROT_WRITE, ioreq_pfn); |
| 923 | if (state->buffered_io_page == NULL) { |
| 924 | hw_error("map buffered IO page returned error %d", errno); |
| 925 | } |
| 926 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 927 | state->ioreq_local_port = g_malloc0(smp_cpus * sizeof (evtchn_port_t)); |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 928 | |
| 929 | /* FIXME: how about if we overflow the page here? */ |
| 930 | for (i = 0; i < smp_cpus; i++) { |
| 931 | rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid, |
| 932 | xen_vcpu_eport(state->shared_page, i)); |
| 933 | if (rc == -1) { |
| 934 | fprintf(stderr, "bind interdomain ioctl error %d\n", errno); |
| 935 | return -1; |
| 936 | } |
| 937 | state->ioreq_local_port[i] = rc; |
| 938 | } |
| 939 | |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 940 | /* Init RAM management */ |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 941 | xen_map_cache_init(); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 942 | xen_ram_init(ram_size); |
| 943 | |
Anthony PERARD | fb4bb2b | 2011-07-15 00:33:42 +0000 | [diff] [blame] | 944 | qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state); |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 945 | |
Anthony PERARD | b4dd780 | 2011-05-24 14:34:21 +0100 | [diff] [blame] | 946 | state->client = xen_cpu_phys_memory_client; |
| 947 | QLIST_INIT(&state->physmap); |
| 948 | cpu_register_phys_memory_client(&state->client); |
| 949 | state->log_for_dirtybit = NULL; |
| 950 | |
Stefano Stabellini | ad35a7d | 2011-06-24 15:54:48 +0100 | [diff] [blame] | 951 | /* Initialize backend core & drivers */ |
| 952 | if (xen_be_init() != 0) { |
| 953 | fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__); |
| 954 | exit(1); |
| 955 | } |
| 956 | xen_be_register("console", &xen_console_ops); |
Stefano Stabellini | 37cdfcf | 2011-06-24 17:36:11 +0100 | [diff] [blame] | 957 | xen_be_register("vkbd", &xen_kbdmouse_ops); |
Stefano Stabellini | ad35a7d | 2011-06-24 15:54:48 +0100 | [diff] [blame] | 958 | xen_be_register("qdisk", &xen_blkdev_ops); |
| 959 | |
Anthony PERARD | 29d3ccd | 2010-06-30 12:58:34 +0100 | [diff] [blame] | 960 | return 0; |
| 961 | } |
Arun Sharma | 9ce94e7 | 2010-09-06 20:07:14 +0100 | [diff] [blame] | 962 | |
| 963 | void destroy_hvm_domain(void) |
| 964 | { |
| 965 | XenXC xc_handle; |
| 966 | int sts; |
| 967 | |
| 968 | xc_handle = xen_xc_interface_open(0, 0, 0); |
| 969 | if (xc_handle == XC_HANDLER_INITIAL_VALUE) { |
| 970 | fprintf(stderr, "Cannot acquire xenctrl handle\n"); |
| 971 | } else { |
| 972 | sts = xc_domain_shutdown(xc_handle, xen_domid, SHUTDOWN_poweroff); |
| 973 | if (sts != 0) { |
| 974 | fprintf(stderr, "? xc_domain_shutdown failed to issue poweroff, " |
| 975 | "sts %d, %s\n", sts, strerror(errno)); |
| 976 | } else { |
| 977 | fprintf(stderr, "Issued domain %d poweroff\n", xen_domid); |
| 978 | } |
| 979 | xc_interface_close(xc_handle); |
| 980 | } |
| 981 | } |