xen-all.c 35 KB

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