xen-all.c 28 KB

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