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