blob: e3a51ccc81eaf7775af9ee26768fb7ab0732ea55 [file] [log] [blame]
Anthony PERARD3285cf42010-08-19 12:27:56 +01001/*
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 Bonzini6b620ca2012-01-13 17:44:23 +01007 * 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 PERARD3285cf42010-08-19 12:27:56 +01009 */
10
Arun Sharma9ce94e72010-09-06 20:07:14 +010011#include <sys/mman.h>
12
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020013#include "hw/pci/pci.h"
Anthony PERARDc9622472010-10-05 16:40:22 +010014#include "hw/pc.h"
Anthony PERARD3285cf42010-08-19 12:27:56 +010015#include "hw/xen_common.h"
16#include "hw/xen_backend.h"
Anthony PERARD39f42432012-10-03 13:48:19 +000017#include "qmp-commands.h"
Anthony PERARD3285cf42010-08-19 12:27:56 +010018
Anthony PERARDb4dd7802011-05-24 14:34:21 +010019#include "range.h"
Jun Nakajima432d2682010-08-31 16:41:25 +010020#include "xen-mapcache.h"
21#include "trace.h"
Avi Kivityce76b8a2011-12-18 16:27:48 +020022#include "exec-memory.h"
Jun Nakajima432d2682010-08-31 16:41:25 +010023
Arun Sharma9ce94e72010-09-06 20:07:14 +010024#include <xen/hvm/ioreq.h>
25#include <xen/hvm/params.h>
Anthony PERARD8a369e22011-07-20 08:17:43 +000026#include <xen/hvm/e820.h>
Arun Sharma9ce94e72010-09-06 20:07:14 +010027
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 Kivityce76b8a2011-12-18 16:27:48 +020038static MemoryRegion ram_memory, ram_640k, ram_lo, ram_hi;
Avi Kivityc65adf92011-12-18 16:40:50 +020039static MemoryRegion *framebuffer;
Anthony PERARD39f42432012-10-03 13:48:19 +000040static bool xen_in_migration;
Avi Kivityce76b8a2011-12-18 16:27:48 +020041
Arun Sharma9ce94e72010-09-06 20:07:14 +010042/* Compatibility with older version */
43#if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
44static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
45{
46 return shared_page->vcpu_iodata[i].vp_eport;
47}
48static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
49{
50 return &shared_page->vcpu_iodata[vcpu].vp_ioreq;
51}
52# define FMT_ioreq_size PRIx64
53#else
54static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
55{
56 return shared_page->vcpu_ioreq[i].vp_eport;
57}
58static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
59{
60 return &shared_page->vcpu_ioreq[vcpu];
61}
62# define FMT_ioreq_size "u"
63#endif
Stefano Stabellinifda1f762012-04-13 17:46:01 +000064#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
65#define HVM_PARAM_BUFIOREQ_EVTCHN 26
66#endif
Arun Sharma9ce94e72010-09-06 20:07:14 +010067
68#define BUFFER_IO_MAX_DELAY 100
69
Anthony PERARDb4dd7802011-05-24 14:34:21 +010070typedef struct XenPhysmap {
Avi Kivitya8170e52012-10-23 12:30:10 +020071 hwaddr start_addr;
Anthony PERARDb4dd7802011-05-24 14:34:21 +010072 ram_addr_t size;
Stefano Stabellinid1814e02012-01-19 15:56:11 +000073 char *name;
Avi Kivitya8170e52012-10-23 12:30:10 +020074 hwaddr phys_offset;
Anthony PERARDb4dd7802011-05-24 14:34:21 +010075
76 QLIST_ENTRY(XenPhysmap) list;
77} XenPhysmap;
78
Arun Sharma9ce94e72010-09-06 20:07:14 +010079typedef 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 Stabellinifda1f762012-04-13 17:46:01 +000085 /* evtchn local port for buffered io */
86 evtchn_port_t bufioreq_local_port;
Arun Sharma9ce94e72010-09-06 20:07:14 +010087 /* the evtchn fd for polling */
88 XenEvtchn xce_handle;
89 /* which vcpu we are serving */
90 int send_vcpu;
91
Anthony PERARD29321332010-09-06 20:07:33 +010092 struct xs_handle *xenstore;
Avi Kivity20581d22011-12-19 12:07:50 +020093 MemoryListener memory_listener;
Anthony PERARDb4dd7802011-05-24 14:34:21 +010094 QLIST_HEAD(, XenPhysmap) physmap;
Avi Kivitya8170e52012-10-23 12:30:10 +020095 hwaddr free_phys_offset;
Anthony PERARDb4dd7802011-05-24 14:34:21 +010096 const XenPhysmap *log_for_dirtybit;
Anthony PERARD29321332010-09-06 20:07:33 +010097
Arun Sharma9ce94e72010-09-06 20:07:14 +010098 Notifier exit;
Gerd Hoffmannda98c8e2012-02-23 13:45:20 +010099 Notifier suspend;
Arun Sharma9ce94e72010-09-06 20:07:14 +0100100} XenIOState;
101
Anthony PERARD41445302010-07-16 14:55:39 +0100102/* Xen specific function for piix pci */
103
104int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
105{
106 return irq_num + ((pci_dev->devfn >> 3) << 2);
107}
108
109void xen_piix3_set_irq(void *opaque, int irq_num, int level)
110{
111 xc_hvm_set_pci_intx_level(xen_xc, xen_domid, 0, 0, irq_num >> 2,
112 irq_num & 3, level);
113}
114
115void xen_piix_pci_write_config_client(uint32_t address, uint32_t val, int len)
116{
117 int i;
118
119 /* Scan for updates to PCI link routes (0x60-0x63). */
120 for (i = 0; i < len; i++) {
121 uint8_t v = (val >> (8 * i)) & 0xff;
122 if (v & 0x80) {
123 v = 0;
124 }
125 v &= 0xf;
126 if (((address + i) >= 0x60) && ((address + i) <= 0x63)) {
127 xc_hvm_set_pci_link_route(xen_xc, xen_domid, address + i - 0x60, v);
128 }
129 }
130}
131
Wei Liuf1dbf012012-04-12 10:01:43 +0000132void xen_hvm_inject_msi(uint64_t addr, uint32_t data)
133{
Stefano Stabellini4c9f8d12012-04-17 16:56:51 +0000134 xen_xc_hvm_inject_msi(xen_xc, xen_domid, addr, data);
Wei Liuf1dbf012012-04-12 10:01:43 +0000135}
136
Gerd Hoffmannda98c8e2012-02-23 13:45:20 +0100137static void xen_suspend_notifier(Notifier *notifier, void *data)
Anthony PERARDc9622472010-10-05 16:40:22 +0100138{
Gerd Hoffmannda98c8e2012-02-23 13:45:20 +0100139 xc_set_hvm_param(xen_xc, xen_domid, HVM_PARAM_ACPI_S_STATE, 3);
Anthony PERARDc9622472010-10-05 16:40:22 +0100140}
141
Anthony PERARD9c11a8a2010-06-30 17:50:10 +0100142/* Xen Interrupt Controller */
143
144static void xen_set_irq(void *opaque, int irq, int level)
145{
146 xc_hvm_set_isa_irq_level(xen_xc, xen_domid, irq, level);
147}
148
149qemu_irq *xen_interrupt_controller_init(void)
150{
151 return qemu_allocate_irqs(xen_set_irq, NULL, 16);
152}
153
Jun Nakajima432d2682010-08-31 16:41:25 +0100154/* Memory Ops */
155
156static void xen_ram_init(ram_addr_t ram_size)
157{
Avi Kivityce76b8a2011-12-18 16:27:48 +0200158 MemoryRegion *sysmem = get_system_memory();
Jun Nakajima432d2682010-08-31 16:41:25 +0100159 ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
Avi Kivityce76b8a2011-12-18 16:27:48 +0200160 ram_addr_t block_len;
Jun Nakajima432d2682010-08-31 16:41:25 +0100161
Avi Kivityce76b8a2011-12-18 16:27:48 +0200162 block_len = ram_size;
Anthony PERARD8a369e22011-07-20 08:17:43 +0000163 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 Kivityce76b8a2011-12-18 16:27:48 +0200167 block_len += HVM_BELOW_4G_MMIO_LENGTH;
Anthony PERARD8a369e22011-07-20 08:17:43 +0000168 }
Avi Kivityc5705a72011-12-20 15:59:12 +0200169 memory_region_init_ram(&ram_memory, "xen.ram", block_len);
170 vmstate_register_ram_global(&ram_memory);
Jun Nakajima432d2682010-08-31 16:41:25 +0100171
Anthony PERARD8a369e22011-07-20 08:17:43 +0000172 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 Nakajima432d2682010-08-31 16:41:25 +0100175 } else {
176 below_4g_mem_size = ram_size;
177 }
178
Avi Kivityce76b8a2011-12-18 16:27:48 +0200179 memory_region_init_alias(&ram_640k, "xen.ram.640k",
180 &ram_memory, 0, 0xa0000);
181 memory_region_add_subregion(sysmem, 0, &ram_640k);
Anthony PERARD8a369e22011-07-20 08:17:43 +0000182 /* 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 Kivityce76b8a2011-12-18 16:27:48 +0200188 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 Nakajima432d2682010-08-31 16:41:25 +0100191 if (above_4g_mem_size > 0) {
Avi Kivityce76b8a2011-12-18 16:27:48 +0200192 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 Nakajima432d2682010-08-31 16:41:25 +0100196 }
Jun Nakajima432d2682010-08-31 16:41:25 +0100197}
198
Avi Kivityfce537d2011-12-18 15:48:55 +0200199void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t size, MemoryRegion *mr)
Jun Nakajima432d2682010-08-31 16:41:25 +0100200{
201 unsigned long nr_pfn;
202 xen_pfn_t *pfn_list;
203 int i;
204
Anthony PERARDc2345722012-01-25 12:36:06 +0000205 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 Kivityce76b8a2011-12-18 16:27:48 +0200213 if (mr == &ram_memory) {
214 return;
215 }
216
Jun Nakajima432d2682010-08-31 16:41:25 +0100217 trace_xen_ram_alloc(ram_addr, size);
218
219 nr_pfn = size >> TARGET_PAGE_BITS;
Anthony Liguori7267c092011-08-20 22:09:37 -0500220 pfn_list = g_malloc(sizeof (*pfn_list) * nr_pfn);
Jun Nakajima432d2682010-08-31 16:41:25 +0100221
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 PERARDf15fbc42011-07-20 08:17:42 +0000227 hw_error("xen: failed to populate ram at " RAM_ADDR_FMT, ram_addr);
Jun Nakajima432d2682010-08-31 16:41:25 +0100228 }
229
Anthony Liguori7267c092011-08-20 22:09:37 -0500230 g_free(pfn_list);
Jun Nakajima432d2682010-08-31 16:41:25 +0100231}
232
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100233static XenPhysmap *get_physmapping(XenIOState *state,
Avi Kivitya8170e52012-10-23 12:30:10 +0200234 hwaddr start_addr, ram_addr_t size)
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100235{
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
Avi Kivitya8170e52012-10-23 12:30:10 +0200248static hwaddr xen_phys_offset_to_gaddr(hwaddr start_addr,
Anthony PERARDcd1ba7d2012-01-18 12:21:38 +0000249 ram_addr_t size, void *opaque)
250{
Avi Kivitya8170e52012-10-23 12:30:10 +0200251 hwaddr addr = start_addr & TARGET_PAGE_MASK;
Anthony PERARDcd1ba7d2012-01-18 12:21:38 +0000252 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 PERARDb4dd7802011-05-24 14:34:21 +0100264#if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 340
265static int xen_add_to_physmap(XenIOState *state,
Avi Kivitya8170e52012-10-23 12:30:10 +0200266 hwaddr start_addr,
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100267 ram_addr_t size,
Avi Kivity20581d22011-12-19 12:07:50 +0200268 MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200269 hwaddr offset_within_region)
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100270{
271 unsigned long i = 0;
272 int rc = 0;
273 XenPhysmap *physmap = NULL;
Avi Kivitya8170e52012-10-23 12:30:10 +0200274 hwaddr pfn, start_gpfn;
275 hwaddr phys_offset = memory_region_get_ram_addr(mr);
Stefano Stabellinid1814e02012-01-19 15:56:11 +0000276 char path[80], value[17];
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100277
278 if (get_physmapping(state, start_addr, size)) {
279 return 0;
280 }
281 if (size <= 0) {
282 return -1;
283 }
284
Stefano Stabelliniebed8502011-06-15 17:29:27 +0100285 /* 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 Kivity20581d22011-12-19 12:07:50 +0200289 if (mr == framebuffer && start_addr > 0xbffff) {
290 goto go_physmap;
Stefano Stabelliniebed8502011-06-15 17:29:27 +0100291 }
292 return -1;
293
294go_physmap:
Avi Kivity20581d22011-12-19 12:07:50 +0200295 DPRINTF("mapping vram to %llx - %llx\n", start_addr, start_addr + size);
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100296
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 Liguori7267c092011-08-20 22:09:37 -0500311 physmap = g_malloc(sizeof (XenPhysmap));
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100312
313 physmap->start_addr = start_addr;
314 physmap->size = size;
Stefano Stabellinid1814e02012-01-19 15:56:11 +0000315 physmap->name = (char *)mr->name;
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100316 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 Stabellinid1814e02012-01-19 15:56:11 +0000324
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 PERARDb4dd7802011-05-24 14:34:21 +0100348 return 0;
349}
350
351static int xen_remove_from_physmap(XenIOState *state,
Avi Kivitya8170e52012-10-23 12:30:10 +0200352 hwaddr start_addr,
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100353 ram_addr_t size)
354{
355 unsigned long i = 0;
356 int rc = 0;
357 XenPhysmap *physmap = NULL;
Avi Kivitya8170e52012-10-23 12:30:10 +0200358 hwaddr phys_offset = 0;
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100359
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
396static int xen_add_to_physmap(XenIOState *state,
Avi Kivitya8170e52012-10-23 12:30:10 +0200397 hwaddr start_addr,
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100398 ram_addr_t size,
Avi Kivity20581d22011-12-19 12:07:50 +0200399 MemoryRegion *mr,
Avi Kivitya8170e52012-10-23 12:30:10 +0200400 hwaddr offset_within_region)
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100401{
402 return -ENOSYS;
403}
404
405static int xen_remove_from_physmap(XenIOState *state,
Avi Kivitya8170e52012-10-23 12:30:10 +0200406 hwaddr start_addr,
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100407 ram_addr_t size)
408{
409 return -ENOSYS;
410}
411#endif
412
Avi Kivity20581d22011-12-19 12:07:50 +0200413static void xen_set_memory(struct MemoryListener *listener,
414 MemoryRegionSection *section,
415 bool add)
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100416{
Avi Kivity20581d22011-12-19 12:07:50 +0200417 XenIOState *state = container_of(listener, XenIOState, memory_listener);
Avi Kivitya8170e52012-10-23 12:30:10 +0200418 hwaddr start_addr = section->offset_within_address_space;
Avi Kivity20581d22011-12-19 12:07:50 +0200419 ram_addr_t size = section->size;
420 bool log_dirty = memory_region_is_logging(section->mr);
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100421 hvmmem_type_t mem_type;
422
Avi Kivity20581d22011-12-19 12:07:50 +0200423 if (!memory_region_is_ram(section->mr)) {
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100424 return;
425 }
426
Avi Kivity20581d22011-12-19 12:07:50 +0200427 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 PERARDb4dd7802011-05-24 14:34:21 +0100433
434 start_addr &= TARGET_PAGE_MASK;
435 size = TARGET_PAGE_ALIGN(size);
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100436
Avi Kivity20581d22011-12-19 12:07:50 +0200437 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 PERARDb4dd7802011-05-24 14:34:21 +0100449 }
Avi Kivity20581d22011-12-19 12:07:50 +0200450 } else {
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100451 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 PERARDb4dd7802011-05-24 14:34:21 +0100454 }
455}
456
Avi Kivity20581d22011-12-19 12:07:50 +0200457static void xen_region_add(MemoryListener *listener,
458 MemoryRegionSection *section)
459{
460 xen_set_memory(listener, section, true);
461}
462
463static void xen_region_del(MemoryListener *listener,
464 MemoryRegionSection *section)
465{
466 xen_set_memory(listener, section, false);
467}
468
Anthony PERARDb18620c2012-01-05 12:47:08 +0000469static void xen_sync_dirty_bitmap(XenIOState *state,
Avi Kivitya8170e52012-10-23 12:30:10 +0200470 hwaddr start_addr,
Anthony PERARDb18620c2012-01-05 12:47:08 +0000471 ram_addr_t size)
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100472{
Avi Kivitya8170e52012-10-23 12:30:10 +0200473 hwaddr npages = size >> TARGET_PAGE_BITS;
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100474 const int width = sizeof(unsigned long) * 8;
475 unsigned long bitmap[(npages + width - 1) / width];
476 int rc, i, j;
477 const XenPhysmap *physmap = NULL;
478
479 physmap = get_physmapping(state, start_addr, size);
480 if (physmap == NULL) {
481 /* not handled */
Anthony PERARDb18620c2012-01-05 12:47:08 +0000482 return;
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100483 }
484
485 if (state->log_for_dirtybit == NULL) {
486 state->log_for_dirtybit = physmap;
487 } else if (state->log_for_dirtybit != physmap) {
Anthony PERARDb18620c2012-01-05 12:47:08 +0000488 /* Only one range for dirty bitmap can be tracked. */
489 return;
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100490 }
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100491
492 rc = xc_hvm_track_dirty_vram(xen_xc, xen_domid,
493 start_addr >> TARGET_PAGE_BITS, npages,
494 bitmap);
Anthony PERARDb18620c2012-01-05 12:47:08 +0000495 if (rc < 0) {
496 if (rc != -ENODATA) {
Anthony PERARD8aba7dc2012-10-03 13:49:40 +0000497 memory_region_set_dirty(framebuffer, 0, size);
498 DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
Anthony PERARDb18620c2012-01-05 12:47:08 +0000499 ", 0x" TARGET_FMT_plx "): %s\n",
500 start_addr, start_addr + size, strerror(-rc));
501 }
502 return;
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100503 }
504
505 for (i = 0; i < ARRAY_SIZE(bitmap); i++) {
506 unsigned long map = bitmap[i];
507 while (map != 0) {
508 j = ffsl(map) - 1;
509 map &= ~(1ul << j);
Avi Kivity5a970652011-12-21 14:21:27 +0200510 memory_region_set_dirty(framebuffer,
Blue Swirlfd4aa972011-10-16 16:04:59 +0000511 (i * width + j) * TARGET_PAGE_SIZE,
512 TARGET_PAGE_SIZE);
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100513 };
514 }
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100515}
516
Avi Kivity20581d22011-12-19 12:07:50 +0200517static void xen_log_start(MemoryListener *listener,
518 MemoryRegionSection *section)
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100519{
Avi Kivity20581d22011-12-19 12:07:50 +0200520 XenIOState *state = container_of(listener, XenIOState, memory_listener);
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100521
Anthony PERARDb18620c2012-01-05 12:47:08 +0000522 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
523 section->size);
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100524}
525
Avi Kivity20581d22011-12-19 12:07:50 +0200526static void xen_log_stop(MemoryListener *listener, MemoryRegionSection *section)
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100527{
Avi Kivity20581d22011-12-19 12:07:50 +0200528 XenIOState *state = container_of(listener, XenIOState, memory_listener);
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100529
530 state->log_for_dirtybit = NULL;
531 /* Disable dirty bit tracking */
Anthony PERARDb18620c2012-01-05 12:47:08 +0000532 xc_hvm_track_dirty_vram(xen_xc, xen_domid, 0, 0, NULL);
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100533}
534
Avi Kivity20581d22011-12-19 12:07:50 +0200535static void xen_log_sync(MemoryListener *listener, MemoryRegionSection *section)
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100536{
Avi Kivity20581d22011-12-19 12:07:50 +0200537 XenIOState *state = container_of(listener, XenIOState, memory_listener);
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100538
Anthony PERARDb18620c2012-01-05 12:47:08 +0000539 xen_sync_dirty_bitmap(state, section->offset_within_address_space,
540 section->size);
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100541}
542
Avi Kivity20581d22011-12-19 12:07:50 +0200543static void xen_log_global_start(MemoryListener *listener)
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100544{
Anthony PERARD39f42432012-10-03 13:48:19 +0000545 if (xen_enabled()) {
546 xen_in_migration = true;
547 }
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100548}
549
Avi Kivity20581d22011-12-19 12:07:50 +0200550static void xen_log_global_stop(MemoryListener *listener)
551{
Anthony PERARD39f42432012-10-03 13:48:19 +0000552 xen_in_migration = false;
Avi Kivity20581d22011-12-19 12:07:50 +0200553}
554
555static MemoryListener xen_memory_listener = {
556 .region_add = xen_region_add,
557 .region_del = xen_region_del,
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100558 .log_start = xen_log_start,
559 .log_stop = xen_log_stop,
Avi Kivity20581d22011-12-19 12:07:50 +0200560 .log_sync = xen_log_sync,
561 .log_global_start = xen_log_global_start,
562 .log_global_stop = xen_log_global_stop,
Avi Kivity72e22d22012-02-08 15:05:50 +0200563 .priority = 10,
Anthony PERARDb4dd7802011-05-24 14:34:21 +0100564};
Jun Nakajima432d2682010-08-31 16:41:25 +0100565
Anthony PERARD39f42432012-10-03 13:48:19 +0000566void qmp_xen_set_global_dirty_log(bool enable, Error **errp)
567{
568 if (enable) {
569 memory_global_dirty_log_start();
570 } else {
571 memory_global_dirty_log_stop();
572 }
573}
574
Anthony PERARD29d3ccd2010-06-30 12:58:34 +0100575/* VCPU Operations, MMIO, IO ring ... */
576
577static void xen_reset_vcpu(void *opaque)
578{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100579 CPUArchState *env = opaque;
Anthony PERARD29d3ccd2010-06-30 12:58:34 +0100580
581 env->halted = 1;
582}
583
584void xen_vcpu_init(void)
585{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100586 CPUArchState *first_cpu;
Anthony PERARD29d3ccd2010-06-30 12:58:34 +0100587
588 if ((first_cpu = qemu_get_cpu(0))) {
589 qemu_register_reset(xen_reset_vcpu, first_cpu);
590 xen_reset_vcpu(first_cpu);
591 }
Stefano Stabellini95d5d752011-11-21 11:10:21 +0000592 /* if rtc_clock is left to default (host_clock), disable it */
593 if (rtc_clock == host_clock) {
594 qemu_clock_enable(rtc_clock, false);
595 }
Anthony PERARD29d3ccd2010-06-30 12:58:34 +0100596}
597
Arun Sharma9ce94e72010-09-06 20:07:14 +0100598/* get the ioreq packets from share mem */
599static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
600{
601 ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
602
603 if (req->state != STATE_IOREQ_READY) {
604 DPRINTF("I/O request not ready: "
605 "%x, ptr: %x, port: %"PRIx64", "
606 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
607 req->state, req->data_is_ptr, req->addr,
608 req->data, req->count, req->size);
609 return NULL;
610 }
611
612 xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
613
614 req->state = STATE_IOREQ_INPROCESS;
615 return req;
616}
617
618/* use poll to get the port notification */
619/* ioreq_vec--out,the */
620/* retval--the number of ioreq packet */
621static ioreq_t *cpu_get_ioreq(XenIOState *state)
622{
623 int i;
624 evtchn_port_t port;
625
626 port = xc_evtchn_pending(state->xce_handle);
Stefano Stabellinifda1f762012-04-13 17:46:01 +0000627 if (port == state->bufioreq_local_port) {
628 qemu_mod_timer(state->buffered_io_timer,
629 BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
630 return NULL;
631 }
632
Arun Sharma9ce94e72010-09-06 20:07:14 +0100633 if (port != -1) {
634 for (i = 0; i < smp_cpus; i++) {
635 if (state->ioreq_local_port[i] == port) {
636 break;
637 }
638 }
639
640 if (i == smp_cpus) {
641 hw_error("Fatal error while trying to get io event!\n");
642 }
643
644 /* unmask the wanted port again */
645 xc_evtchn_unmask(state->xce_handle, port);
646
647 /* get the io packet from shared memory */
648 state->send_vcpu = i;
649 return cpu_get_ioreq_from_shared_memory(state, i);
650 }
651
652 /* read error or read nothing */
653 return NULL;
654}
655
656static uint32_t do_inp(pio_addr_t addr, unsigned long size)
657{
658 switch (size) {
659 case 1:
660 return cpu_inb(addr);
661 case 2:
662 return cpu_inw(addr);
663 case 4:
664 return cpu_inl(addr);
665 default:
666 hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
667 }
668}
669
670static void do_outp(pio_addr_t addr,
671 unsigned long size, uint32_t val)
672{
673 switch (size) {
674 case 1:
675 return cpu_outb(addr, val);
676 case 2:
677 return cpu_outw(addr, val);
678 case 4:
679 return cpu_outl(addr, val);
680 default:
681 hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
682 }
683}
684
685static void cpu_ioreq_pio(ioreq_t *req)
686{
687 int i, sign;
688
689 sign = req->df ? -1 : 1;
690
691 if (req->dir == IOREQ_READ) {
692 if (!req->data_is_ptr) {
693 req->data = do_inp(req->addr, req->size);
694 } else {
695 uint32_t tmp;
696
697 for (i = 0; i < req->count; i++) {
698 tmp = do_inp(req->addr, req->size);
Dongxiao Xu14d40182012-08-22 10:17:43 +0000699 cpu_physical_memory_write(
700 req->data + (sign * i * (int64_t)req->size),
Arun Sharma9ce94e72010-09-06 20:07:14 +0100701 (uint8_t *) &tmp, req->size);
702 }
703 }
704 } else if (req->dir == IOREQ_WRITE) {
705 if (!req->data_is_ptr) {
706 do_outp(req->addr, req->size, req->data);
707 } else {
708 for (i = 0; i < req->count; i++) {
709 uint32_t tmp = 0;
710
Dongxiao Xu14d40182012-08-22 10:17:43 +0000711 cpu_physical_memory_read(
712 req->data + (sign * i * (int64_t)req->size),
Arun Sharma9ce94e72010-09-06 20:07:14 +0100713 (uint8_t*) &tmp, req->size);
714 do_outp(req->addr, req->size, tmp);
715 }
716 }
717 }
718}
719
720static void cpu_ioreq_move(ioreq_t *req)
721{
722 int i, sign;
723
724 sign = req->df ? -1 : 1;
725
726 if (!req->data_is_ptr) {
727 if (req->dir == IOREQ_READ) {
728 for (i = 0; i < req->count; i++) {
Dongxiao Xu14d40182012-08-22 10:17:43 +0000729 cpu_physical_memory_read(
730 req->addr + (sign * i * (int64_t)req->size),
Arun Sharma9ce94e72010-09-06 20:07:14 +0100731 (uint8_t *) &req->data, req->size);
732 }
733 } else if (req->dir == IOREQ_WRITE) {
734 for (i = 0; i < req->count; i++) {
Dongxiao Xu14d40182012-08-22 10:17:43 +0000735 cpu_physical_memory_write(
736 req->addr + (sign * i * (int64_t)req->size),
Arun Sharma9ce94e72010-09-06 20:07:14 +0100737 (uint8_t *) &req->data, req->size);
738 }
739 }
740 } else {
Stefano Stabellini2b734342011-05-20 16:57:24 +0000741 uint64_t tmp;
Arun Sharma9ce94e72010-09-06 20:07:14 +0100742
743 if (req->dir == IOREQ_READ) {
744 for (i = 0; i < req->count; i++) {
Dongxiao Xu14d40182012-08-22 10:17:43 +0000745 cpu_physical_memory_read(
746 req->addr + (sign * i * (int64_t)req->size),
Arun Sharma9ce94e72010-09-06 20:07:14 +0100747 (uint8_t*) &tmp, req->size);
Dongxiao Xu14d40182012-08-22 10:17:43 +0000748 cpu_physical_memory_write(
749 req->data + (sign * i * (int64_t)req->size),
Arun Sharma9ce94e72010-09-06 20:07:14 +0100750 (uint8_t*) &tmp, req->size);
751 }
752 } else if (req->dir == IOREQ_WRITE) {
753 for (i = 0; i < req->count; i++) {
Dongxiao Xu14d40182012-08-22 10:17:43 +0000754 cpu_physical_memory_read(
755 req->data + (sign * i * (int64_t)req->size),
Arun Sharma9ce94e72010-09-06 20:07:14 +0100756 (uint8_t*) &tmp, req->size);
Dongxiao Xu14d40182012-08-22 10:17:43 +0000757 cpu_physical_memory_write(
758 req->addr + (sign * i * (int64_t)req->size),
Arun Sharma9ce94e72010-09-06 20:07:14 +0100759 (uint8_t*) &tmp, req->size);
760 }
761 }
762 }
763}
764
765static void handle_ioreq(ioreq_t *req)
766{
767 if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
768 (req->size < sizeof (target_ulong))) {
769 req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
770 }
771
772 switch (req->type) {
773 case IOREQ_TYPE_PIO:
774 cpu_ioreq_pio(req);
775 break;
776 case IOREQ_TYPE_COPY:
777 cpu_ioreq_move(req);
778 break;
779 case IOREQ_TYPE_TIMEOFFSET:
780 break;
781 case IOREQ_TYPE_INVALIDATE:
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200782 xen_invalidate_map_cache();
Arun Sharma9ce94e72010-09-06 20:07:14 +0100783 break;
784 default:
785 hw_error("Invalid ioreq type 0x%x\n", req->type);
786 }
787}
788
Stefano Stabellinifda1f762012-04-13 17:46:01 +0000789static int handle_buffered_iopage(XenIOState *state)
Arun Sharma9ce94e72010-09-06 20:07:14 +0100790{
791 buf_ioreq_t *buf_req = NULL;
792 ioreq_t req;
793 int qw;
794
795 if (!state->buffered_io_page) {
Stefano Stabellinifda1f762012-04-13 17:46:01 +0000796 return 0;
Arun Sharma9ce94e72010-09-06 20:07:14 +0100797 }
798
Stefano Stabellinifda1f762012-04-13 17:46:01 +0000799 memset(&req, 0x00, sizeof(req));
800
Arun Sharma9ce94e72010-09-06 20:07:14 +0100801 while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
802 buf_req = &state->buffered_io_page->buf_ioreq[
803 state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
804 req.size = 1UL << buf_req->size;
805 req.count = 1;
806 req.addr = buf_req->addr;
807 req.data = buf_req->data;
808 req.state = STATE_IOREQ_READY;
809 req.dir = buf_req->dir;
810 req.df = 1;
811 req.type = buf_req->type;
812 req.data_is_ptr = 0;
813 qw = (req.size == 8);
814 if (qw) {
815 buf_req = &state->buffered_io_page->buf_ioreq[
816 (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
817 req.data |= ((uint64_t)buf_req->data) << 32;
818 }
819
820 handle_ioreq(&req);
821
822 xen_mb();
823 state->buffered_io_page->read_pointer += qw ? 2 : 1;
824 }
Stefano Stabellinifda1f762012-04-13 17:46:01 +0000825
826 return req.count;
Arun Sharma9ce94e72010-09-06 20:07:14 +0100827}
828
829static void handle_buffered_io(void *opaque)
830{
831 XenIOState *state = opaque;
832
Stefano Stabellinifda1f762012-04-13 17:46:01 +0000833 if (handle_buffered_iopage(state)) {
834 qemu_mod_timer(state->buffered_io_timer,
835 BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
836 } else {
837 qemu_del_timer(state->buffered_io_timer);
838 xc_evtchn_unmask(state->xce_handle, state->bufioreq_local_port);
839 }
Arun Sharma9ce94e72010-09-06 20:07:14 +0100840}
841
842static void cpu_handle_ioreq(void *opaque)
843{
844 XenIOState *state = opaque;
845 ioreq_t *req = cpu_get_ioreq(state);
846
847 handle_buffered_iopage(state);
848 if (req) {
849 handle_ioreq(req);
850
851 if (req->state != STATE_IOREQ_INPROCESS) {
852 fprintf(stderr, "Badness in I/O request ... not in service?!: "
853 "%x, ptr: %x, port: %"PRIx64", "
854 "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
855 req->state, req->data_is_ptr, req->addr,
856 req->data, req->count, req->size);
John V. Baboval180640e2012-05-17 10:33:09 +0000857 destroy_hvm_domain(false);
Arun Sharma9ce94e72010-09-06 20:07:14 +0100858 return;
859 }
860
861 xen_wmb(); /* Update ioreq contents /then/ update state. */
862
863 /*
864 * We do this before we send the response so that the tools
865 * have the opportunity to pick up on the reset before the
866 * guest resumes and does a hlt with interrupts disabled which
867 * causes Xen to powerdown the domain.
868 */
Luiz Capitulino13548692011-07-29 15:36:43 -0300869 if (runstate_is_running()) {
Arun Sharma9ce94e72010-09-06 20:07:14 +0100870 if (qemu_shutdown_requested_get()) {
John V. Baboval180640e2012-05-17 10:33:09 +0000871 destroy_hvm_domain(false);
Arun Sharma9ce94e72010-09-06 20:07:14 +0100872 }
873 if (qemu_reset_requested_get()) {
Jan Kiszkae063eb12011-06-14 18:29:43 +0200874 qemu_system_reset(VMRESET_REPORT);
John V. Baboval180640e2012-05-17 10:33:09 +0000875 destroy_hvm_domain(true);
Arun Sharma9ce94e72010-09-06 20:07:14 +0100876 }
877 }
878
879 req->state = STATE_IORESP_READY;
880 xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
881 }
882}
883
Stefano Stabellini0f517262011-06-30 18:26:29 +0100884static int store_dev_info(int domid, CharDriverState *cs, const char *string)
885{
886 struct xs_handle *xs = NULL;
887 char *path = NULL;
888 char *newpath = NULL;
889 char *pts = NULL;
890 int ret = -1;
891
892 /* Only continue if we're talking to a pty. */
893 if (strncmp(cs->filename, "pty:", 4)) {
894 return 0;
895 }
896 pts = cs->filename + 4;
897
898 /* We now have everything we need to set the xenstore entry. */
899 xs = xs_open(0);
900 if (xs == NULL) {
901 fprintf(stderr, "Could not contact XenStore\n");
902 goto out;
903 }
904
905 path = xs_get_domain_path(xs, domid);
906 if (path == NULL) {
907 fprintf(stderr, "xs_get_domain_path() error\n");
908 goto out;
909 }
910 newpath = realloc(path, (strlen(path) + strlen(string) +
911 strlen("/tty") + 1));
912 if (newpath == NULL) {
913 fprintf(stderr, "realloc error\n");
914 goto out;
915 }
916 path = newpath;
917
918 strcat(path, string);
919 strcat(path, "/tty");
920 if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
921 fprintf(stderr, "xs_write for '%s' fail", string);
922 goto out;
923 }
924 ret = 0;
925
926out:
927 free(path);
928 xs_close(xs);
929
930 return ret;
931}
932
933void xenstore_store_pv_console_info(int i, CharDriverState *chr)
934{
935 if (i == 0) {
936 store_dev_info(xen_domid, chr, "/console");
937 } else {
938 char buf[32];
939 snprintf(buf, sizeof(buf), "/device/console/%d", i);
940 store_dev_info(xen_domid, chr, buf);
941 }
942}
943
Anthony PERARDfb4bb2b2011-07-15 00:33:42 +0000944static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
Anthony PERARD29321332010-09-06 20:07:33 +0100945{
946 char path[50];
947
Anthony PERARDfb4bb2b2011-07-15 00:33:42 +0000948 if (xs == NULL) {
949 fprintf(stderr, "xenstore connection not initialized\n");
950 exit(1);
951 }
952
Anthony PERARD29321332010-09-06 20:07:33 +0100953 snprintf(path, sizeof (path), "/local/domain/0/device-model/%u/state", xen_domid);
Anthony PERARDfb4bb2b2011-07-15 00:33:42 +0000954 if (!xs_write(xs, XBT_NULL, path, state, strlen(state))) {
Anthony PERARD29321332010-09-06 20:07:33 +0100955 fprintf(stderr, "error recording dm state\n");
956 exit(1);
957 }
958}
959
Arun Sharma9ce94e72010-09-06 20:07:14 +0100960static void xen_main_loop_prepare(XenIOState *state)
961{
962 int evtchn_fd = -1;
963
964 if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
965 evtchn_fd = xc_evtchn_fd(state->xce_handle);
966 }
967
968 state->buffered_io_timer = qemu_new_timer_ms(rt_clock, handle_buffered_io,
969 state);
Arun Sharma9ce94e72010-09-06 20:07:14 +0100970
971 if (evtchn_fd != -1) {
972 qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
973 }
974}
975
976
Anthony PERARD3285cf42010-08-19 12:27:56 +0100977/* Initialise Xen */
978
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300979static void xen_change_state_handler(void *opaque, int running,
980 RunState state)
Anthony PERARDfb4bb2b2011-07-15 00:33:42 +0000981{
982 if (running) {
983 /* record state running */
984 xenstore_record_dm_state(xenstore, "running");
985 }
986}
987
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300988static void xen_hvm_change_state_handler(void *opaque, int running,
989 RunState rstate)
Arun Sharma9ce94e72010-09-06 20:07:14 +0100990{
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300991 XenIOState *xstate = opaque;
Arun Sharma9ce94e72010-09-06 20:07:14 +0100992 if (running) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300993 xen_main_loop_prepare(xstate);
Arun Sharma9ce94e72010-09-06 20:07:14 +0100994 }
995}
996
Jan Kiszka9e8dd452011-06-20 14:06:26 +0200997static void xen_exit_notifier(Notifier *n, void *data)
Arun Sharma9ce94e72010-09-06 20:07:14 +0100998{
999 XenIOState *state = container_of(n, XenIOState, exit);
1000
1001 xc_evtchn_close(state->xce_handle);
Anthony PERARD29321332010-09-06 20:07:33 +01001002 xs_daemon_close(state->xenstore);
Arun Sharma9ce94e72010-09-06 20:07:14 +01001003}
1004
Anthony PERARD3285cf42010-08-19 12:27:56 +01001005int xen_init(void)
1006{
1007 xen_xc = xen_xc_interface_open(0, 0, 0);
1008 if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
1009 xen_be_printf(NULL, 0, "can't open xen interface\n");
1010 return -1;
1011 }
Anthony PERARDfb4bb2b2011-07-15 00:33:42 +00001012 qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
Anthony PERARD3285cf42010-08-19 12:27:56 +01001013
1014 return 0;
1015}
Anthony PERARD29d3ccd2010-06-30 12:58:34 +01001016
Stefano Stabellinid1814e02012-01-19 15:56:11 +00001017static void xen_read_physmap(XenIOState *state)
1018{
1019 XenPhysmap *physmap = NULL;
1020 unsigned int len, num, i;
1021 char path[80], *value = NULL;
1022 char **entries = NULL;
1023
1024 snprintf(path, sizeof(path),
1025 "/local/domain/0/device-model/%d/physmap", xen_domid);
1026 entries = xs_directory(state->xenstore, 0, path, &num);
1027 if (entries == NULL)
1028 return;
1029
1030 for (i = 0; i < num; i++) {
1031 physmap = g_malloc(sizeof (XenPhysmap));
1032 physmap->phys_offset = strtoull(entries[i], NULL, 16);
1033 snprintf(path, sizeof(path),
1034 "/local/domain/0/device-model/%d/physmap/%s/start_addr",
1035 xen_domid, entries[i]);
1036 value = xs_read(state->xenstore, 0, path, &len);
1037 if (value == NULL) {
1038 free(physmap);
1039 continue;
1040 }
1041 physmap->start_addr = strtoull(value, NULL, 16);
1042 free(value);
1043
1044 snprintf(path, sizeof(path),
1045 "/local/domain/0/device-model/%d/physmap/%s/size",
1046 xen_domid, entries[i]);
1047 value = xs_read(state->xenstore, 0, path, &len);
1048 if (value == NULL) {
1049 free(physmap);
1050 continue;
1051 }
1052 physmap->size = strtoull(value, NULL, 16);
1053 free(value);
1054
1055 snprintf(path, sizeof(path),
1056 "/local/domain/0/device-model/%d/physmap/%s/name",
1057 xen_domid, entries[i]);
1058 physmap->name = xs_read(state->xenstore, 0, path, &len);
1059
1060 QLIST_INSERT_HEAD(&state->physmap, physmap, list);
1061 }
1062 free(entries);
Stefano Stabellinid1814e02012-01-19 15:56:11 +00001063}
1064
Anthony PERARD29d3ccd2010-06-30 12:58:34 +01001065int xen_hvm_init(void)
1066{
Arun Sharma9ce94e72010-09-06 20:07:14 +01001067 int i, rc;
1068 unsigned long ioreq_pfn;
Stefano Stabellinifda1f762012-04-13 17:46:01 +00001069 unsigned long bufioreq_evtchn;
Arun Sharma9ce94e72010-09-06 20:07:14 +01001070 XenIOState *state;
1071
Anthony Liguori7267c092011-08-20 22:09:37 -05001072 state = g_malloc0(sizeof (XenIOState));
Arun Sharma9ce94e72010-09-06 20:07:14 +01001073
1074 state->xce_handle = xen_xc_evtchn_open(NULL, 0);
1075 if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
1076 perror("xen: event channel open");
1077 return -errno;
1078 }
1079
Anthony PERARD29321332010-09-06 20:07:33 +01001080 state->xenstore = xs_daemon_open();
1081 if (state->xenstore == NULL) {
1082 perror("xen: xenstore open");
1083 return -errno;
1084 }
1085
Arun Sharma9ce94e72010-09-06 20:07:14 +01001086 state->exit.notify = xen_exit_notifier;
1087 qemu_add_exit_notifier(&state->exit);
1088
Gerd Hoffmannda98c8e2012-02-23 13:45:20 +01001089 state->suspend.notify = xen_suspend_notifier;
1090 qemu_register_suspend_notifier(&state->suspend);
1091
Arun Sharma9ce94e72010-09-06 20:07:14 +01001092 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn);
1093 DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
1094 state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
1095 PROT_READ|PROT_WRITE, ioreq_pfn);
1096 if (state->shared_page == NULL) {
1097 hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
1098 errno, xen_xc);
1099 }
1100
1101 xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
1102 DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
1103 state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
1104 PROT_READ|PROT_WRITE, ioreq_pfn);
1105 if (state->buffered_io_page == NULL) {
1106 hw_error("map buffered IO page returned error %d", errno);
1107 }
1108
Anthony Liguori7267c092011-08-20 22:09:37 -05001109 state->ioreq_local_port = g_malloc0(smp_cpus * sizeof (evtchn_port_t));
Arun Sharma9ce94e72010-09-06 20:07:14 +01001110
1111 /* FIXME: how about if we overflow the page here? */
1112 for (i = 0; i < smp_cpus; i++) {
1113 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
1114 xen_vcpu_eport(state->shared_page, i));
1115 if (rc == -1) {
1116 fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
1117 return -1;
1118 }
1119 state->ioreq_local_port[i] = rc;
1120 }
1121
Stefano Stabellinifda1f762012-04-13 17:46:01 +00001122 rc = xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_EVTCHN,
1123 &bufioreq_evtchn);
1124 if (rc < 0) {
1125 fprintf(stderr, "failed to get HVM_PARAM_BUFIOREQ_EVTCHN\n");
1126 return -1;
1127 }
1128 rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
1129 (uint32_t)bufioreq_evtchn);
1130 if (rc == -1) {
1131 fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
1132 return -1;
1133 }
1134 state->bufioreq_local_port = rc;
1135
Jun Nakajima432d2682010-08-31 16:41:25 +01001136 /* Init RAM management */
Anthony PERARDcd1ba7d2012-01-18 12:21:38 +00001137 xen_map_cache_init(xen_phys_offset_to_gaddr, state);
Jun Nakajima432d2682010-08-31 16:41:25 +01001138 xen_ram_init(ram_size);
1139
Anthony PERARDfb4bb2b2011-07-15 00:33:42 +00001140 qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
Arun Sharma9ce94e72010-09-06 20:07:14 +01001141
Avi Kivity20581d22011-12-19 12:07:50 +02001142 state->memory_listener = xen_memory_listener;
Anthony PERARDb4dd7802011-05-24 14:34:21 +01001143 QLIST_INIT(&state->physmap);
Avi Kivityf6790af2012-10-02 20:13:51 +02001144 memory_listener_register(&state->memory_listener, &address_space_memory);
Anthony PERARDb4dd7802011-05-24 14:34:21 +01001145 state->log_for_dirtybit = NULL;
1146
Stefano Stabelliniad35a7d2011-06-24 15:54:48 +01001147 /* Initialize backend core & drivers */
1148 if (xen_be_init() != 0) {
1149 fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
1150 exit(1);
1151 }
1152 xen_be_register("console", &xen_console_ops);
Stefano Stabellini37cdfcf2011-06-24 17:36:11 +01001153 xen_be_register("vkbd", &xen_kbdmouse_ops);
Stefano Stabelliniad35a7d2011-06-24 15:54:48 +01001154 xen_be_register("qdisk", &xen_blkdev_ops);
Stefano Stabellinid1814e02012-01-19 15:56:11 +00001155 xen_read_physmap(state);
Stefano Stabelliniad35a7d2011-06-24 15:54:48 +01001156
Anthony PERARD29d3ccd2010-06-30 12:58:34 +01001157 return 0;
1158}
Arun Sharma9ce94e72010-09-06 20:07:14 +01001159
John V. Baboval180640e2012-05-17 10:33:09 +00001160void destroy_hvm_domain(bool reboot)
Arun Sharma9ce94e72010-09-06 20:07:14 +01001161{
1162 XenXC xc_handle;
1163 int sts;
1164
1165 xc_handle = xen_xc_interface_open(0, 0, 0);
1166 if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
1167 fprintf(stderr, "Cannot acquire xenctrl handle\n");
1168 } else {
John V. Baboval180640e2012-05-17 10:33:09 +00001169 sts = xc_domain_shutdown(xc_handle, xen_domid,
1170 reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff);
Arun Sharma9ce94e72010-09-06 20:07:14 +01001171 if (sts != 0) {
John V. Baboval180640e2012-05-17 10:33:09 +00001172 fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
1173 "sts %d, %s\n", reboot ? "reboot" : "poweroff",
1174 sts, strerror(errno));
Arun Sharma9ce94e72010-09-06 20:07:14 +01001175 } else {
John V. Baboval180640e2012-05-17 10:33:09 +00001176 fprintf(stderr, "Issued domain %d %s\n", xen_domid,
1177 reboot ? "reboot" : "poweroff");
Arun Sharma9ce94e72010-09-06 20:07:14 +01001178 }
1179 xc_interface_close(xc_handle);
1180 }
1181}
Avi Kivityc65adf92011-12-18 16:40:50 +02001182
1183void xen_register_framebuffer(MemoryRegion *mr)
1184{
1185 framebuffer = mr;
1186}
Allen Kayeaab4d62012-06-21 15:40:09 +00001187
1188void xen_shutdown_fatal_error(const char *fmt, ...)
1189{
1190 va_list ap;
1191
1192 va_start(ap, fmt);
1193 vfprintf(stderr, fmt, ap);
1194 va_end(ap);
1195 fprintf(stderr, "Will destroy the domain.\n");
1196 /* destroy the domain */
1197 qemu_system_shutdown_request();
1198}
Anthony PERARD910b38e2012-10-03 13:48:45 +00001199
1200void xen_modified_memory(ram_addr_t start, ram_addr_t length)
1201{
1202 if (unlikely(xen_in_migration)) {
1203 int rc;
1204 ram_addr_t start_pfn, nb_pages;
1205
1206 if (length == 0) {
1207 length = TARGET_PAGE_SIZE;
1208 }
1209 start_pfn = start >> TARGET_PAGE_BITS;
1210 nb_pages = ((start + length + TARGET_PAGE_SIZE - 1) >> TARGET_PAGE_BITS)
1211 - start_pfn;
1212 rc = xc_hvm_modified_memory(xen_xc, xen_domid, start_pfn, nb_pages);
1213 if (rc) {
1214 fprintf(stderr,
1215 "%s failed for "RAM_ADDR_FMT" ("RAM_ADDR_FMT"): %i, %s\n",
1216 __func__, start, nb_pages, rc, strerror(-rc));
1217 }
1218 }
1219}