xen-all.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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,
  474. EventNotifier *e)
  475. {
  476. }
  477. static void xen_eventfd_del(MemoryListener *listener,
  478. MemoryRegionSection *section,
  479. bool match_data, uint64_t data,
  480. EventNotifier *e)
  481. {
  482. }
  483. static MemoryListener xen_memory_listener = {
  484. .begin = xen_begin,
  485. .commit = xen_commit,
  486. .region_add = xen_region_add,
  487. .region_del = xen_region_del,
  488. .region_nop = xen_region_nop,
  489. .log_start = xen_log_start,
  490. .log_stop = xen_log_stop,
  491. .log_sync = xen_log_sync,
  492. .log_global_start = xen_log_global_start,
  493. .log_global_stop = xen_log_global_stop,
  494. .eventfd_add = xen_eventfd_add,
  495. .eventfd_del = xen_eventfd_del,
  496. .priority = 10,
  497. };
  498. /* VCPU Operations, MMIO, IO ring ... */
  499. static void xen_reset_vcpu(void *opaque)
  500. {
  501. CPUArchState *env = opaque;
  502. env->halted = 1;
  503. }
  504. void xen_vcpu_init(void)
  505. {
  506. CPUArchState *first_cpu;
  507. if ((first_cpu = qemu_get_cpu(0))) {
  508. qemu_register_reset(xen_reset_vcpu, first_cpu);
  509. xen_reset_vcpu(first_cpu);
  510. }
  511. /* if rtc_clock is left to default (host_clock), disable it */
  512. if (rtc_clock == host_clock) {
  513. qemu_clock_enable(rtc_clock, false);
  514. }
  515. }
  516. /* get the ioreq packets from share mem */
  517. static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
  518. {
  519. ioreq_t *req = xen_vcpu_ioreq(state->shared_page, vcpu);
  520. if (req->state != STATE_IOREQ_READY) {
  521. DPRINTF("I/O request not ready: "
  522. "%x, ptr: %x, port: %"PRIx64", "
  523. "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
  524. req->state, req->data_is_ptr, req->addr,
  525. req->data, req->count, req->size);
  526. return NULL;
  527. }
  528. xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
  529. req->state = STATE_IOREQ_INPROCESS;
  530. return req;
  531. }
  532. /* use poll to get the port notification */
  533. /* ioreq_vec--out,the */
  534. /* retval--the number of ioreq packet */
  535. static ioreq_t *cpu_get_ioreq(XenIOState *state)
  536. {
  537. int i;
  538. evtchn_port_t port;
  539. port = xc_evtchn_pending(state->xce_handle);
  540. if (port == state->bufioreq_local_port) {
  541. qemu_mod_timer(state->buffered_io_timer,
  542. BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
  543. return NULL;
  544. }
  545. if (port != -1) {
  546. for (i = 0; i < smp_cpus; i++) {
  547. if (state->ioreq_local_port[i] == port) {
  548. break;
  549. }
  550. }
  551. if (i == smp_cpus) {
  552. hw_error("Fatal error while trying to get io event!\n");
  553. }
  554. /* unmask the wanted port again */
  555. xc_evtchn_unmask(state->xce_handle, port);
  556. /* get the io packet from shared memory */
  557. state->send_vcpu = i;
  558. return cpu_get_ioreq_from_shared_memory(state, i);
  559. }
  560. /* read error or read nothing */
  561. return NULL;
  562. }
  563. static uint32_t do_inp(pio_addr_t addr, unsigned long size)
  564. {
  565. switch (size) {
  566. case 1:
  567. return cpu_inb(addr);
  568. case 2:
  569. return cpu_inw(addr);
  570. case 4:
  571. return cpu_inl(addr);
  572. default:
  573. hw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
  574. }
  575. }
  576. static void do_outp(pio_addr_t addr,
  577. unsigned long size, uint32_t val)
  578. {
  579. switch (size) {
  580. case 1:
  581. return cpu_outb(addr, val);
  582. case 2:
  583. return cpu_outw(addr, val);
  584. case 4:
  585. return cpu_outl(addr, val);
  586. default:
  587. hw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
  588. }
  589. }
  590. static void cpu_ioreq_pio(ioreq_t *req)
  591. {
  592. int i, sign;
  593. sign = req->df ? -1 : 1;
  594. if (req->dir == IOREQ_READ) {
  595. if (!req->data_is_ptr) {
  596. req->data = do_inp(req->addr, req->size);
  597. } else {
  598. uint32_t tmp;
  599. for (i = 0; i < req->count; i++) {
  600. tmp = do_inp(req->addr, req->size);
  601. cpu_physical_memory_write(
  602. req->data + (sign * i * (int64_t)req->size),
  603. (uint8_t *) &tmp, req->size);
  604. }
  605. }
  606. } else if (req->dir == IOREQ_WRITE) {
  607. if (!req->data_is_ptr) {
  608. do_outp(req->addr, req->size, req->data);
  609. } else {
  610. for (i = 0; i < req->count; i++) {
  611. uint32_t tmp = 0;
  612. cpu_physical_memory_read(
  613. req->data + (sign * i * (int64_t)req->size),
  614. (uint8_t*) &tmp, req->size);
  615. do_outp(req->addr, req->size, tmp);
  616. }
  617. }
  618. }
  619. }
  620. static void cpu_ioreq_move(ioreq_t *req)
  621. {
  622. int i, sign;
  623. sign = req->df ? -1 : 1;
  624. if (!req->data_is_ptr) {
  625. if (req->dir == IOREQ_READ) {
  626. for (i = 0; i < req->count; i++) {
  627. cpu_physical_memory_read(
  628. req->addr + (sign * i * (int64_t)req->size),
  629. (uint8_t *) &req->data, req->size);
  630. }
  631. } else if (req->dir == IOREQ_WRITE) {
  632. for (i = 0; i < req->count; i++) {
  633. cpu_physical_memory_write(
  634. req->addr + (sign * i * (int64_t)req->size),
  635. (uint8_t *) &req->data, req->size);
  636. }
  637. }
  638. } else {
  639. uint64_t tmp;
  640. if (req->dir == IOREQ_READ) {
  641. for (i = 0; i < req->count; i++) {
  642. cpu_physical_memory_read(
  643. req->addr + (sign * i * (int64_t)req->size),
  644. (uint8_t*) &tmp, req->size);
  645. cpu_physical_memory_write(
  646. req->data + (sign * i * (int64_t)req->size),
  647. (uint8_t*) &tmp, req->size);
  648. }
  649. } else if (req->dir == IOREQ_WRITE) {
  650. for (i = 0; i < req->count; i++) {
  651. cpu_physical_memory_read(
  652. req->data + (sign * i * (int64_t)req->size),
  653. (uint8_t*) &tmp, req->size);
  654. cpu_physical_memory_write(
  655. req->addr + (sign * i * (int64_t)req->size),
  656. (uint8_t*) &tmp, req->size);
  657. }
  658. }
  659. }
  660. }
  661. static void handle_ioreq(ioreq_t *req)
  662. {
  663. if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
  664. (req->size < sizeof (target_ulong))) {
  665. req->data &= ((target_ulong) 1 << (8 * req->size)) - 1;
  666. }
  667. switch (req->type) {
  668. case IOREQ_TYPE_PIO:
  669. cpu_ioreq_pio(req);
  670. break;
  671. case IOREQ_TYPE_COPY:
  672. cpu_ioreq_move(req);
  673. break;
  674. case IOREQ_TYPE_TIMEOFFSET:
  675. break;
  676. case IOREQ_TYPE_INVALIDATE:
  677. xen_invalidate_map_cache();
  678. break;
  679. default:
  680. hw_error("Invalid ioreq type 0x%x\n", req->type);
  681. }
  682. }
  683. static int handle_buffered_iopage(XenIOState *state)
  684. {
  685. buf_ioreq_t *buf_req = NULL;
  686. ioreq_t req;
  687. int qw;
  688. if (!state->buffered_io_page) {
  689. return 0;
  690. }
  691. memset(&req, 0x00, sizeof(req));
  692. while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) {
  693. buf_req = &state->buffered_io_page->buf_ioreq[
  694. state->buffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
  695. req.size = 1UL << buf_req->size;
  696. req.count = 1;
  697. req.addr = buf_req->addr;
  698. req.data = buf_req->data;
  699. req.state = STATE_IOREQ_READY;
  700. req.dir = buf_req->dir;
  701. req.df = 1;
  702. req.type = buf_req->type;
  703. req.data_is_ptr = 0;
  704. qw = (req.size == 8);
  705. if (qw) {
  706. buf_req = &state->buffered_io_page->buf_ioreq[
  707. (state->buffered_io_page->read_pointer + 1) % IOREQ_BUFFER_SLOT_NUM];
  708. req.data |= ((uint64_t)buf_req->data) << 32;
  709. }
  710. handle_ioreq(&req);
  711. xen_mb();
  712. state->buffered_io_page->read_pointer += qw ? 2 : 1;
  713. }
  714. return req.count;
  715. }
  716. static void handle_buffered_io(void *opaque)
  717. {
  718. XenIOState *state = opaque;
  719. if (handle_buffered_iopage(state)) {
  720. qemu_mod_timer(state->buffered_io_timer,
  721. BUFFER_IO_MAX_DELAY + qemu_get_clock_ms(rt_clock));
  722. } else {
  723. qemu_del_timer(state->buffered_io_timer);
  724. xc_evtchn_unmask(state->xce_handle, state->bufioreq_local_port);
  725. }
  726. }
  727. static void cpu_handle_ioreq(void *opaque)
  728. {
  729. XenIOState *state = opaque;
  730. ioreq_t *req = cpu_get_ioreq(state);
  731. handle_buffered_iopage(state);
  732. if (req) {
  733. handle_ioreq(req);
  734. if (req->state != STATE_IOREQ_INPROCESS) {
  735. fprintf(stderr, "Badness in I/O request ... not in service?!: "
  736. "%x, ptr: %x, port: %"PRIx64", "
  737. "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
  738. req->state, req->data_is_ptr, req->addr,
  739. req->data, req->count, req->size);
  740. destroy_hvm_domain(false);
  741. return;
  742. }
  743. xen_wmb(); /* Update ioreq contents /then/ update state. */
  744. /*
  745. * We do this before we send the response so that the tools
  746. * have the opportunity to pick up on the reset before the
  747. * guest resumes and does a hlt with interrupts disabled which
  748. * causes Xen to powerdown the domain.
  749. */
  750. if (runstate_is_running()) {
  751. if (qemu_shutdown_requested_get()) {
  752. destroy_hvm_domain(false);
  753. }
  754. if (qemu_reset_requested_get()) {
  755. qemu_system_reset(VMRESET_REPORT);
  756. destroy_hvm_domain(true);
  757. }
  758. }
  759. req->state = STATE_IORESP_READY;
  760. xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
  761. }
  762. }
  763. static int store_dev_info(int domid, CharDriverState *cs, const char *string)
  764. {
  765. struct xs_handle *xs = NULL;
  766. char *path = NULL;
  767. char *newpath = NULL;
  768. char *pts = NULL;
  769. int ret = -1;
  770. /* Only continue if we're talking to a pty. */
  771. if (strncmp(cs->filename, "pty:", 4)) {
  772. return 0;
  773. }
  774. pts = cs->filename + 4;
  775. /* We now have everything we need to set the xenstore entry. */
  776. xs = xs_open(0);
  777. if (xs == NULL) {
  778. fprintf(stderr, "Could not contact XenStore\n");
  779. goto out;
  780. }
  781. path = xs_get_domain_path(xs, domid);
  782. if (path == NULL) {
  783. fprintf(stderr, "xs_get_domain_path() error\n");
  784. goto out;
  785. }
  786. newpath = realloc(path, (strlen(path) + strlen(string) +
  787. strlen("/tty") + 1));
  788. if (newpath == NULL) {
  789. fprintf(stderr, "realloc error\n");
  790. goto out;
  791. }
  792. path = newpath;
  793. strcat(path, string);
  794. strcat(path, "/tty");
  795. if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
  796. fprintf(stderr, "xs_write for '%s' fail", string);
  797. goto out;
  798. }
  799. ret = 0;
  800. out:
  801. free(path);
  802. xs_close(xs);
  803. return ret;
  804. }
  805. void xenstore_store_pv_console_info(int i, CharDriverState *chr)
  806. {
  807. if (i == 0) {
  808. store_dev_info(xen_domid, chr, "/console");
  809. } else {
  810. char buf[32];
  811. snprintf(buf, sizeof(buf), "/device/console/%d", i);
  812. store_dev_info(xen_domid, chr, buf);
  813. }
  814. }
  815. static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
  816. {
  817. char path[50];
  818. if (xs == NULL) {
  819. fprintf(stderr, "xenstore connection not initialized\n");
  820. exit(1);
  821. }
  822. snprintf(path, sizeof (path), "/local/domain/0/device-model/%u/state", xen_domid);
  823. if (!xs_write(xs, XBT_NULL, path, state, strlen(state))) {
  824. fprintf(stderr, "error recording dm state\n");
  825. exit(1);
  826. }
  827. }
  828. static void xen_main_loop_prepare(XenIOState *state)
  829. {
  830. int evtchn_fd = -1;
  831. if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
  832. evtchn_fd = xc_evtchn_fd(state->xce_handle);
  833. }
  834. state->buffered_io_timer = qemu_new_timer_ms(rt_clock, handle_buffered_io,
  835. state);
  836. if (evtchn_fd != -1) {
  837. qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
  838. }
  839. }
  840. /* Initialise Xen */
  841. static void xen_change_state_handler(void *opaque, int running,
  842. RunState state)
  843. {
  844. if (running) {
  845. /* record state running */
  846. xenstore_record_dm_state(xenstore, "running");
  847. }
  848. }
  849. static void xen_hvm_change_state_handler(void *opaque, int running,
  850. RunState rstate)
  851. {
  852. XenIOState *xstate = opaque;
  853. if (running) {
  854. xen_main_loop_prepare(xstate);
  855. }
  856. }
  857. static void xen_exit_notifier(Notifier *n, void *data)
  858. {
  859. XenIOState *state = container_of(n, XenIOState, exit);
  860. xc_evtchn_close(state->xce_handle);
  861. xs_daemon_close(state->xenstore);
  862. }
  863. int xen_init(void)
  864. {
  865. xen_xc = xen_xc_interface_open(0, 0, 0);
  866. if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
  867. xen_be_printf(NULL, 0, "can't open xen interface\n");
  868. return -1;
  869. }
  870. qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
  871. return 0;
  872. }
  873. static void xen_read_physmap(XenIOState *state)
  874. {
  875. XenPhysmap *physmap = NULL;
  876. unsigned int len, num, i;
  877. char path[80], *value = NULL;
  878. char **entries = NULL;
  879. snprintf(path, sizeof(path),
  880. "/local/domain/0/device-model/%d/physmap", xen_domid);
  881. entries = xs_directory(state->xenstore, 0, path, &num);
  882. if (entries == NULL)
  883. return;
  884. for (i = 0; i < num; i++) {
  885. physmap = g_malloc(sizeof (XenPhysmap));
  886. physmap->phys_offset = strtoull(entries[i], NULL, 16);
  887. snprintf(path, sizeof(path),
  888. "/local/domain/0/device-model/%d/physmap/%s/start_addr",
  889. xen_domid, entries[i]);
  890. value = xs_read(state->xenstore, 0, path, &len);
  891. if (value == NULL) {
  892. free(physmap);
  893. continue;
  894. }
  895. physmap->start_addr = strtoull(value, NULL, 16);
  896. free(value);
  897. snprintf(path, sizeof(path),
  898. "/local/domain/0/device-model/%d/physmap/%s/size",
  899. xen_domid, entries[i]);
  900. value = xs_read(state->xenstore, 0, path, &len);
  901. if (value == NULL) {
  902. free(physmap);
  903. continue;
  904. }
  905. physmap->size = strtoull(value, NULL, 16);
  906. free(value);
  907. snprintf(path, sizeof(path),
  908. "/local/domain/0/device-model/%d/physmap/%s/name",
  909. xen_domid, entries[i]);
  910. physmap->name = xs_read(state->xenstore, 0, path, &len);
  911. QLIST_INSERT_HEAD(&state->physmap, physmap, list);
  912. }
  913. free(entries);
  914. return;
  915. }
  916. int xen_hvm_init(void)
  917. {
  918. int i, rc;
  919. unsigned long ioreq_pfn;
  920. unsigned long bufioreq_evtchn;
  921. XenIOState *state;
  922. state = g_malloc0(sizeof (XenIOState));
  923. state->xce_handle = xen_xc_evtchn_open(NULL, 0);
  924. if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
  925. perror("xen: event channel open");
  926. return -errno;
  927. }
  928. state->xenstore = xs_daemon_open();
  929. if (state->xenstore == NULL) {
  930. perror("xen: xenstore open");
  931. return -errno;
  932. }
  933. state->exit.notify = xen_exit_notifier;
  934. qemu_add_exit_notifier(&state->exit);
  935. state->suspend.notify = xen_suspend_notifier;
  936. qemu_register_suspend_notifier(&state->suspend);
  937. xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn);
  938. DPRINTF("shared page at pfn %lx\n", ioreq_pfn);
  939. state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
  940. PROT_READ|PROT_WRITE, ioreq_pfn);
  941. if (state->shared_page == NULL) {
  942. hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
  943. errno, xen_xc);
  944. }
  945. xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
  946. DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
  947. state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
  948. PROT_READ|PROT_WRITE, ioreq_pfn);
  949. if (state->buffered_io_page == NULL) {
  950. hw_error("map buffered IO page returned error %d", errno);
  951. }
  952. state->ioreq_local_port = g_malloc0(smp_cpus * sizeof (evtchn_port_t));
  953. /* FIXME: how about if we overflow the page here? */
  954. for (i = 0; i < smp_cpus; i++) {
  955. rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
  956. xen_vcpu_eport(state->shared_page, i));
  957. if (rc == -1) {
  958. fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
  959. return -1;
  960. }
  961. state->ioreq_local_port[i] = rc;
  962. }
  963. rc = xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_EVTCHN,
  964. &bufioreq_evtchn);
  965. if (rc < 0) {
  966. fprintf(stderr, "failed to get HVM_PARAM_BUFIOREQ_EVTCHN\n");
  967. return -1;
  968. }
  969. rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
  970. (uint32_t)bufioreq_evtchn);
  971. if (rc == -1) {
  972. fprintf(stderr, "bind interdomain ioctl error %d\n", errno);
  973. return -1;
  974. }
  975. state->bufioreq_local_port = rc;
  976. /* Init RAM management */
  977. xen_map_cache_init(xen_phys_offset_to_gaddr, state);
  978. xen_ram_init(ram_size);
  979. qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
  980. state->memory_listener = xen_memory_listener;
  981. QLIST_INIT(&state->physmap);
  982. memory_listener_register(&state->memory_listener, get_system_memory());
  983. state->log_for_dirtybit = NULL;
  984. /* Initialize backend core & drivers */
  985. if (xen_be_init() != 0) {
  986. fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__);
  987. exit(1);
  988. }
  989. xen_be_register("console", &xen_console_ops);
  990. xen_be_register("vkbd", &xen_kbdmouse_ops);
  991. xen_be_register("qdisk", &xen_blkdev_ops);
  992. xen_read_physmap(state);
  993. return 0;
  994. }
  995. void destroy_hvm_domain(bool reboot)
  996. {
  997. XenXC xc_handle;
  998. int sts;
  999. xc_handle = xen_xc_interface_open(0, 0, 0);
  1000. if (xc_handle == XC_HANDLER_INITIAL_VALUE) {
  1001. fprintf(stderr, "Cannot acquire xenctrl handle\n");
  1002. } else {
  1003. sts = xc_domain_shutdown(xc_handle, xen_domid,
  1004. reboot ? SHUTDOWN_reboot : SHUTDOWN_poweroff);
  1005. if (sts != 0) {
  1006. fprintf(stderr, "xc_domain_shutdown failed to issue %s, "
  1007. "sts %d, %s\n", reboot ? "reboot" : "poweroff",
  1008. sts, strerror(errno));
  1009. } else {
  1010. fprintf(stderr, "Issued domain %d %s\n", xen_domid,
  1011. reboot ? "reboot" : "poweroff");
  1012. }
  1013. xc_interface_close(xc_handle);
  1014. }
  1015. }
  1016. void xen_register_framebuffer(MemoryRegion *mr)
  1017. {
  1018. framebuffer = mr;
  1019. }
  1020. void xen_shutdown_fatal_error(const char *fmt, ...)
  1021. {
  1022. va_list ap;
  1023. va_start(ap, fmt);
  1024. vfprintf(stderr, fmt, ap);
  1025. va_end(ap);
  1026. fprintf(stderr, "Will destroy the domain.\n");
  1027. /* destroy the domain */
  1028. qemu_system_shutdown_request();
  1029. }