xen-all.c 36 KB

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