2
0

ivshmem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * Inter-VM Shared Memory PCI device.
  3. *
  4. * Author:
  5. * Cam Macdonell <cam@cs.ualberta.ca>
  6. *
  7. * Based On: cirrus_vga.c
  8. * Copyright (c) 2004 Fabrice Bellard
  9. * Copyright (c) 2004 Makoto Suzuki (suzu)
  10. *
  11. * and rtl8139.c
  12. * Copyright (c) 2006 Igor Kovalenko
  13. *
  14. * This code is licensed under the GNU GPL v2.
  15. *
  16. * Contributions after 2012-01-13 are licensed under the terms of the
  17. * GNU GPL, version 2 or (at your option) any later version.
  18. */
  19. #include "hw.h"
  20. #include "pc.h"
  21. #include "pci/pci.h"
  22. #include "pci/msix.h"
  23. #include "sysemu/kvm.h"
  24. #include "migration/migration.h"
  25. #include "qapi/qmp/qerror.h"
  26. #include "qemu/event_notifier.h"
  27. #include "char/char.h"
  28. #include <sys/mman.h>
  29. #include <sys/types.h>
  30. #define PCI_VENDOR_ID_IVSHMEM PCI_VENDOR_ID_REDHAT_QUMRANET
  31. #define PCI_DEVICE_ID_IVSHMEM 0x1110
  32. #define IVSHMEM_IOEVENTFD 0
  33. #define IVSHMEM_MSI 1
  34. #define IVSHMEM_PEER 0
  35. #define IVSHMEM_MASTER 1
  36. #define IVSHMEM_REG_BAR_SIZE 0x100
  37. //#define DEBUG_IVSHMEM
  38. #ifdef DEBUG_IVSHMEM
  39. #define IVSHMEM_DPRINTF(fmt, ...) \
  40. do {printf("IVSHMEM: " fmt, ## __VA_ARGS__); } while (0)
  41. #else
  42. #define IVSHMEM_DPRINTF(fmt, ...)
  43. #endif
  44. typedef struct Peer {
  45. int nb_eventfds;
  46. EventNotifier *eventfds;
  47. } Peer;
  48. typedef struct EventfdEntry {
  49. PCIDevice *pdev;
  50. int vector;
  51. } EventfdEntry;
  52. typedef struct IVShmemState {
  53. PCIDevice dev;
  54. uint32_t intrmask;
  55. uint32_t intrstatus;
  56. uint32_t doorbell;
  57. CharDriverState **eventfd_chr;
  58. CharDriverState *server_chr;
  59. MemoryRegion ivshmem_mmio;
  60. /* We might need to register the BAR before we actually have the memory.
  61. * So prepare a container MemoryRegion for the BAR immediately and
  62. * add a subregion when we have the memory.
  63. */
  64. MemoryRegion bar;
  65. MemoryRegion ivshmem;
  66. uint64_t ivshmem_size; /* size of shared memory region */
  67. uint32_t ivshmem_attr;
  68. uint32_t ivshmem_64bit;
  69. int shm_fd; /* shared memory file descriptor */
  70. Peer *peers;
  71. int nb_peers; /* how many guests we have space for */
  72. int max_peer; /* maximum numbered peer */
  73. int vm_id;
  74. uint32_t vectors;
  75. uint32_t features;
  76. EventfdEntry *eventfd_table;
  77. Error *migration_blocker;
  78. char * shmobj;
  79. char * sizearg;
  80. char * role;
  81. int role_val; /* scalar to avoid multiple string comparisons */
  82. } IVShmemState;
  83. /* registers for the Inter-VM shared memory device */
  84. enum ivshmem_registers {
  85. INTRMASK = 0,
  86. INTRSTATUS = 4,
  87. IVPOSITION = 8,
  88. DOORBELL = 12,
  89. };
  90. static inline uint32_t ivshmem_has_feature(IVShmemState *ivs,
  91. unsigned int feature) {
  92. return (ivs->features & (1 << feature));
  93. }
  94. static inline bool is_power_of_two(uint64_t x) {
  95. return (x & (x - 1)) == 0;
  96. }
  97. /* accessing registers - based on rtl8139 */
  98. static void ivshmem_update_irq(IVShmemState *s, int val)
  99. {
  100. int isr;
  101. isr = (s->intrstatus & s->intrmask) & 0xffffffff;
  102. /* don't print ISR resets */
  103. if (isr) {
  104. IVSHMEM_DPRINTF("Set IRQ to %d (%04x %04x)\n",
  105. isr ? 1 : 0, s->intrstatus, s->intrmask);
  106. }
  107. qemu_set_irq(s->dev.irq[0], (isr != 0));
  108. }
  109. static void ivshmem_IntrMask_write(IVShmemState *s, uint32_t val)
  110. {
  111. IVSHMEM_DPRINTF("IntrMask write(w) val = 0x%04x\n", val);
  112. s->intrmask = val;
  113. ivshmem_update_irq(s, val);
  114. }
  115. static uint32_t ivshmem_IntrMask_read(IVShmemState *s)
  116. {
  117. uint32_t ret = s->intrmask;
  118. IVSHMEM_DPRINTF("intrmask read(w) val = 0x%04x\n", ret);
  119. return ret;
  120. }
  121. static void ivshmem_IntrStatus_write(IVShmemState *s, uint32_t val)
  122. {
  123. IVSHMEM_DPRINTF("IntrStatus write(w) val = 0x%04x\n", val);
  124. s->intrstatus = val;
  125. ivshmem_update_irq(s, val);
  126. }
  127. static uint32_t ivshmem_IntrStatus_read(IVShmemState *s)
  128. {
  129. uint32_t ret = s->intrstatus;
  130. /* reading ISR clears all interrupts */
  131. s->intrstatus = 0;
  132. ivshmem_update_irq(s, 0);
  133. return ret;
  134. }
  135. static void ivshmem_io_write(void *opaque, hwaddr addr,
  136. uint64_t val, unsigned size)
  137. {
  138. IVShmemState *s = opaque;
  139. uint16_t dest = val >> 16;
  140. uint16_t vector = val & 0xff;
  141. addr &= 0xfc;
  142. IVSHMEM_DPRINTF("writing to addr " TARGET_FMT_plx "\n", addr);
  143. switch (addr)
  144. {
  145. case INTRMASK:
  146. ivshmem_IntrMask_write(s, val);
  147. break;
  148. case INTRSTATUS:
  149. ivshmem_IntrStatus_write(s, val);
  150. break;
  151. case DOORBELL:
  152. /* check that dest VM ID is reasonable */
  153. if (dest > s->max_peer) {
  154. IVSHMEM_DPRINTF("Invalid destination VM ID (%d)\n", dest);
  155. break;
  156. }
  157. /* check doorbell range */
  158. if (vector < s->peers[dest].nb_eventfds) {
  159. IVSHMEM_DPRINTF("Notifying VM %d on vector %d\n", dest, vector);
  160. event_notifier_set(&s->peers[dest].eventfds[vector]);
  161. }
  162. break;
  163. default:
  164. IVSHMEM_DPRINTF("Invalid VM Doorbell VM %d\n", dest);
  165. }
  166. }
  167. static uint64_t ivshmem_io_read(void *opaque, hwaddr addr,
  168. unsigned size)
  169. {
  170. IVShmemState *s = opaque;
  171. uint32_t ret;
  172. switch (addr)
  173. {
  174. case INTRMASK:
  175. ret = ivshmem_IntrMask_read(s);
  176. break;
  177. case INTRSTATUS:
  178. ret = ivshmem_IntrStatus_read(s);
  179. break;
  180. case IVPOSITION:
  181. /* return my VM ID if the memory is mapped */
  182. if (s->shm_fd > 0) {
  183. ret = s->vm_id;
  184. } else {
  185. ret = -1;
  186. }
  187. break;
  188. default:
  189. IVSHMEM_DPRINTF("why are we reading " TARGET_FMT_plx "\n", addr);
  190. ret = 0;
  191. }
  192. return ret;
  193. }
  194. static const MemoryRegionOps ivshmem_mmio_ops = {
  195. .read = ivshmem_io_read,
  196. .write = ivshmem_io_write,
  197. .endianness = DEVICE_NATIVE_ENDIAN,
  198. .impl = {
  199. .min_access_size = 4,
  200. .max_access_size = 4,
  201. },
  202. };
  203. static void ivshmem_receive(void *opaque, const uint8_t *buf, int size)
  204. {
  205. IVShmemState *s = opaque;
  206. ivshmem_IntrStatus_write(s, *buf);
  207. IVSHMEM_DPRINTF("ivshmem_receive 0x%02x\n", *buf);
  208. }
  209. static int ivshmem_can_receive(void * opaque)
  210. {
  211. return 8;
  212. }
  213. static void ivshmem_event(void *opaque, int event)
  214. {
  215. IVSHMEM_DPRINTF("ivshmem_event %d\n", event);
  216. }
  217. static void fake_irqfd(void *opaque, const uint8_t *buf, int size) {
  218. EventfdEntry *entry = opaque;
  219. PCIDevice *pdev = entry->pdev;
  220. IVSHMEM_DPRINTF("interrupt on vector %p %d\n", pdev, entry->vector);
  221. msix_notify(pdev, entry->vector);
  222. }
  223. static CharDriverState* create_eventfd_chr_device(void * opaque, EventNotifier *n,
  224. int vector)
  225. {
  226. /* create a event character device based on the passed eventfd */
  227. IVShmemState *s = opaque;
  228. CharDriverState * chr;
  229. int eventfd = event_notifier_get_fd(n);
  230. chr = qemu_chr_open_eventfd(eventfd);
  231. if (chr == NULL) {
  232. fprintf(stderr, "creating eventfd for eventfd %d failed\n", eventfd);
  233. exit(-1);
  234. }
  235. /* if MSI is supported we need multiple interrupts */
  236. if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
  237. s->eventfd_table[vector].pdev = &s->dev;
  238. s->eventfd_table[vector].vector = vector;
  239. qemu_chr_add_handlers(chr, ivshmem_can_receive, fake_irqfd,
  240. ivshmem_event, &s->eventfd_table[vector]);
  241. } else {
  242. qemu_chr_add_handlers(chr, ivshmem_can_receive, ivshmem_receive,
  243. ivshmem_event, s);
  244. }
  245. return chr;
  246. }
  247. static int check_shm_size(IVShmemState *s, int fd) {
  248. /* check that the guest isn't going to try and map more memory than the
  249. * the object has allocated return -1 to indicate error */
  250. struct stat buf;
  251. fstat(fd, &buf);
  252. if (s->ivshmem_size > buf.st_size) {
  253. fprintf(stderr,
  254. "IVSHMEM ERROR: Requested memory size greater"
  255. " than shared object size (%" PRIu64 " > %" PRIu64")\n",
  256. s->ivshmem_size, (uint64_t)buf.st_size);
  257. return -1;
  258. } else {
  259. return 0;
  260. }
  261. }
  262. /* create the shared memory BAR when we are not using the server, so we can
  263. * create the BAR and map the memory immediately */
  264. static void create_shared_memory_BAR(IVShmemState *s, int fd) {
  265. void * ptr;
  266. s->shm_fd = fd;
  267. ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  268. memory_region_init_ram_ptr(&s->ivshmem, "ivshmem.bar2",
  269. s->ivshmem_size, ptr);
  270. vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
  271. memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
  272. /* region for shared memory */
  273. pci_register_bar(&s->dev, 2, s->ivshmem_attr, &s->bar);
  274. }
  275. static void ivshmem_add_eventfd(IVShmemState *s, int posn, int i)
  276. {
  277. memory_region_add_eventfd(&s->ivshmem_mmio,
  278. DOORBELL,
  279. 4,
  280. true,
  281. (posn << 16) | i,
  282. &s->peers[posn].eventfds[i]);
  283. }
  284. static void ivshmem_del_eventfd(IVShmemState *s, int posn, int i)
  285. {
  286. memory_region_del_eventfd(&s->ivshmem_mmio,
  287. DOORBELL,
  288. 4,
  289. true,
  290. (posn << 16) | i,
  291. &s->peers[posn].eventfds[i]);
  292. }
  293. static void close_guest_eventfds(IVShmemState *s, int posn)
  294. {
  295. int i, guest_curr_max;
  296. if (!ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
  297. return;
  298. }
  299. guest_curr_max = s->peers[posn].nb_eventfds;
  300. memory_region_transaction_begin();
  301. for (i = 0; i < guest_curr_max; i++) {
  302. ivshmem_del_eventfd(s, posn, i);
  303. }
  304. memory_region_transaction_commit();
  305. for (i = 0; i < guest_curr_max; i++) {
  306. event_notifier_cleanup(&s->peers[posn].eventfds[i]);
  307. }
  308. g_free(s->peers[posn].eventfds);
  309. s->peers[posn].nb_eventfds = 0;
  310. }
  311. /* this function increase the dynamic storage need to store data about other
  312. * guests */
  313. static void increase_dynamic_storage(IVShmemState *s, int new_min_size) {
  314. int j, old_nb_alloc;
  315. old_nb_alloc = s->nb_peers;
  316. while (new_min_size >= s->nb_peers)
  317. s->nb_peers = s->nb_peers * 2;
  318. IVSHMEM_DPRINTF("bumping storage to %d guests\n", s->nb_peers);
  319. s->peers = g_realloc(s->peers, s->nb_peers * sizeof(Peer));
  320. /* zero out new pointers */
  321. for (j = old_nb_alloc; j < s->nb_peers; j++) {
  322. s->peers[j].eventfds = NULL;
  323. s->peers[j].nb_eventfds = 0;
  324. }
  325. }
  326. static void ivshmem_read(void *opaque, const uint8_t * buf, int flags)
  327. {
  328. IVShmemState *s = opaque;
  329. int incoming_fd, tmp_fd;
  330. int guest_max_eventfd;
  331. long incoming_posn;
  332. memcpy(&incoming_posn, buf, sizeof(long));
  333. /* pick off s->server_chr->msgfd and store it, posn should accompany msg */
  334. tmp_fd = qemu_chr_fe_get_msgfd(s->server_chr);
  335. IVSHMEM_DPRINTF("posn is %ld, fd is %d\n", incoming_posn, tmp_fd);
  336. /* make sure we have enough space for this guest */
  337. if (incoming_posn >= s->nb_peers) {
  338. increase_dynamic_storage(s, incoming_posn);
  339. }
  340. if (tmp_fd == -1) {
  341. /* if posn is positive and unseen before then this is our posn*/
  342. if ((incoming_posn >= 0) &&
  343. (s->peers[incoming_posn].eventfds == NULL)) {
  344. /* receive our posn */
  345. s->vm_id = incoming_posn;
  346. return;
  347. } else {
  348. /* otherwise an fd == -1 means an existing guest has gone away */
  349. IVSHMEM_DPRINTF("posn %ld has gone away\n", incoming_posn);
  350. close_guest_eventfds(s, incoming_posn);
  351. return;
  352. }
  353. }
  354. /* because of the implementation of get_msgfd, we need a dup */
  355. incoming_fd = dup(tmp_fd);
  356. if (incoming_fd == -1) {
  357. fprintf(stderr, "could not allocate file descriptor %s\n",
  358. strerror(errno));
  359. return;
  360. }
  361. /* if the position is -1, then it's shared memory region fd */
  362. if (incoming_posn == -1) {
  363. void * map_ptr;
  364. s->max_peer = 0;
  365. if (check_shm_size(s, incoming_fd) == -1) {
  366. exit(-1);
  367. }
  368. /* mmap the region and map into the BAR2 */
  369. map_ptr = mmap(0, s->ivshmem_size, PROT_READ|PROT_WRITE, MAP_SHARED,
  370. incoming_fd, 0);
  371. memory_region_init_ram_ptr(&s->ivshmem,
  372. "ivshmem.bar2", s->ivshmem_size, map_ptr);
  373. vmstate_register_ram(&s->ivshmem, &s->dev.qdev);
  374. IVSHMEM_DPRINTF("guest h/w addr = %" PRIu64 ", size = %" PRIu64 "\n",
  375. s->ivshmem_offset, s->ivshmem_size);
  376. memory_region_add_subregion(&s->bar, 0, &s->ivshmem);
  377. /* only store the fd if it is successfully mapped */
  378. s->shm_fd = incoming_fd;
  379. return;
  380. }
  381. /* each guest has an array of eventfds, and we keep track of how many
  382. * guests for each VM */
  383. guest_max_eventfd = s->peers[incoming_posn].nb_eventfds;
  384. if (guest_max_eventfd == 0) {
  385. /* one eventfd per MSI vector */
  386. s->peers[incoming_posn].eventfds = g_new(EventNotifier, s->vectors);
  387. }
  388. /* this is an eventfd for a particular guest VM */
  389. IVSHMEM_DPRINTF("eventfds[%ld][%d] = %d\n", incoming_posn,
  390. guest_max_eventfd, incoming_fd);
  391. event_notifier_init_fd(&s->peers[incoming_posn].eventfds[guest_max_eventfd],
  392. incoming_fd);
  393. /* increment count for particular guest */
  394. s->peers[incoming_posn].nb_eventfds++;
  395. /* keep track of the maximum VM ID */
  396. if (incoming_posn > s->max_peer) {
  397. s->max_peer = incoming_posn;
  398. }
  399. if (incoming_posn == s->vm_id) {
  400. s->eventfd_chr[guest_max_eventfd] = create_eventfd_chr_device(s,
  401. &s->peers[s->vm_id].eventfds[guest_max_eventfd],
  402. guest_max_eventfd);
  403. }
  404. if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD)) {
  405. ivshmem_add_eventfd(s, incoming_posn, guest_max_eventfd);
  406. }
  407. }
  408. /* Select the MSI-X vectors used by device.
  409. * ivshmem maps events to vectors statically, so
  410. * we just enable all vectors on init and after reset. */
  411. static void ivshmem_use_msix(IVShmemState * s)
  412. {
  413. int i;
  414. if (!msix_present(&s->dev)) {
  415. return;
  416. }
  417. for (i = 0; i < s->vectors; i++) {
  418. msix_vector_use(&s->dev, i);
  419. }
  420. }
  421. static void ivshmem_reset(DeviceState *d)
  422. {
  423. IVShmemState *s = DO_UPCAST(IVShmemState, dev.qdev, d);
  424. s->intrstatus = 0;
  425. ivshmem_use_msix(s);
  426. }
  427. static uint64_t ivshmem_get_size(IVShmemState * s) {
  428. uint64_t value;
  429. char *ptr;
  430. value = strtoull(s->sizearg, &ptr, 10);
  431. switch (*ptr) {
  432. case 0: case 'M': case 'm':
  433. value <<= 20;
  434. break;
  435. case 'G': case 'g':
  436. value <<= 30;
  437. break;
  438. default:
  439. fprintf(stderr, "qemu: invalid ram size: %s\n", s->sizearg);
  440. exit(1);
  441. }
  442. /* BARs must be a power of 2 */
  443. if (!is_power_of_two(value)) {
  444. fprintf(stderr, "ivshmem: size must be power of 2\n");
  445. exit(1);
  446. }
  447. return value;
  448. }
  449. static void ivshmem_setup_msi(IVShmemState * s)
  450. {
  451. if (msix_init_exclusive_bar(&s->dev, s->vectors, 1)) {
  452. IVSHMEM_DPRINTF("msix initialization failed\n");
  453. exit(1);
  454. }
  455. IVSHMEM_DPRINTF("msix initialized (%d vectors)\n", s->vectors);
  456. /* allocate QEMU char devices for receiving interrupts */
  457. s->eventfd_table = g_malloc0(s->vectors * sizeof(EventfdEntry));
  458. ivshmem_use_msix(s);
  459. }
  460. static void ivshmem_save(QEMUFile* f, void *opaque)
  461. {
  462. IVShmemState *proxy = opaque;
  463. IVSHMEM_DPRINTF("ivshmem_save\n");
  464. pci_device_save(&proxy->dev, f);
  465. if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
  466. msix_save(&proxy->dev, f);
  467. } else {
  468. qemu_put_be32(f, proxy->intrstatus);
  469. qemu_put_be32(f, proxy->intrmask);
  470. }
  471. }
  472. static int ivshmem_load(QEMUFile* f, void *opaque, int version_id)
  473. {
  474. IVSHMEM_DPRINTF("ivshmem_load\n");
  475. IVShmemState *proxy = opaque;
  476. int ret;
  477. if (version_id > 0) {
  478. return -EINVAL;
  479. }
  480. if (proxy->role_val == IVSHMEM_PEER) {
  481. fprintf(stderr, "ivshmem: 'peer' devices are not migratable\n");
  482. return -EINVAL;
  483. }
  484. ret = pci_device_load(&proxy->dev, f);
  485. if (ret) {
  486. return ret;
  487. }
  488. if (ivshmem_has_feature(proxy, IVSHMEM_MSI)) {
  489. msix_load(&proxy->dev, f);
  490. ivshmem_use_msix(proxy);
  491. } else {
  492. proxy->intrstatus = qemu_get_be32(f);
  493. proxy->intrmask = qemu_get_be32(f);
  494. }
  495. return 0;
  496. }
  497. static void ivshmem_write_config(PCIDevice *pci_dev, uint32_t address,
  498. uint32_t val, int len)
  499. {
  500. pci_default_write_config(pci_dev, address, val, len);
  501. msix_write_config(pci_dev, address, val, len);
  502. }
  503. static int pci_ivshmem_init(PCIDevice *dev)
  504. {
  505. IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
  506. uint8_t *pci_conf;
  507. if (s->sizearg == NULL)
  508. s->ivshmem_size = 4 << 20; /* 4 MB default */
  509. else {
  510. s->ivshmem_size = ivshmem_get_size(s);
  511. }
  512. register_savevm(&s->dev.qdev, "ivshmem", 0, 0, ivshmem_save, ivshmem_load,
  513. dev);
  514. /* IRQFD requires MSI */
  515. if (ivshmem_has_feature(s, IVSHMEM_IOEVENTFD) &&
  516. !ivshmem_has_feature(s, IVSHMEM_MSI)) {
  517. fprintf(stderr, "ivshmem: ioeventfd/irqfd requires MSI\n");
  518. exit(1);
  519. }
  520. /* check that role is reasonable */
  521. if (s->role) {
  522. if (strncmp(s->role, "peer", 5) == 0) {
  523. s->role_val = IVSHMEM_PEER;
  524. } else if (strncmp(s->role, "master", 7) == 0) {
  525. s->role_val = IVSHMEM_MASTER;
  526. } else {
  527. fprintf(stderr, "ivshmem: 'role' must be 'peer' or 'master'\n");
  528. exit(1);
  529. }
  530. } else {
  531. s->role_val = IVSHMEM_MASTER; /* default */
  532. }
  533. if (s->role_val == IVSHMEM_PEER) {
  534. error_set(&s->migration_blocker, QERR_DEVICE_FEATURE_BLOCKS_MIGRATION,
  535. "peer mode", "ivshmem");
  536. migrate_add_blocker(s->migration_blocker);
  537. }
  538. pci_conf = s->dev.config;
  539. pci_conf[PCI_COMMAND] = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  540. pci_config_set_interrupt_pin(pci_conf, 1);
  541. s->shm_fd = 0;
  542. memory_region_init_io(&s->ivshmem_mmio, &ivshmem_mmio_ops, s,
  543. "ivshmem-mmio", IVSHMEM_REG_BAR_SIZE);
  544. /* region for registers*/
  545. pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY,
  546. &s->ivshmem_mmio);
  547. memory_region_init(&s->bar, "ivshmem-bar2-container", s->ivshmem_size);
  548. s->ivshmem_attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
  549. PCI_BASE_ADDRESS_MEM_PREFETCH;
  550. if (s->ivshmem_64bit) {
  551. s->ivshmem_attr |= PCI_BASE_ADDRESS_MEM_TYPE_64;
  552. }
  553. if ((s->server_chr != NULL) &&
  554. (strncmp(s->server_chr->filename, "unix:", 5) == 0)) {
  555. /* if we get a UNIX socket as the parameter we will talk
  556. * to the ivshmem server to receive the memory region */
  557. if (s->shmobj != NULL) {
  558. fprintf(stderr, "WARNING: do not specify both 'chardev' "
  559. "and 'shm' with ivshmem\n");
  560. }
  561. IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
  562. s->server_chr->filename);
  563. if (ivshmem_has_feature(s, IVSHMEM_MSI)) {
  564. ivshmem_setup_msi(s);
  565. }
  566. /* we allocate enough space for 16 guests and grow as needed */
  567. s->nb_peers = 16;
  568. s->vm_id = -1;
  569. /* allocate/initialize space for interrupt handling */
  570. s->peers = g_malloc0(s->nb_peers * sizeof(Peer));
  571. pci_register_bar(&s->dev, 2, s->ivshmem_attr, &s->bar);
  572. s->eventfd_chr = g_malloc0(s->vectors * sizeof(CharDriverState *));
  573. qemu_chr_add_handlers(s->server_chr, ivshmem_can_receive, ivshmem_read,
  574. ivshmem_event, s);
  575. } else {
  576. /* just map the file immediately, we're not using a server */
  577. int fd;
  578. if (s->shmobj == NULL) {
  579. fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
  580. }
  581. IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
  582. /* try opening with O_EXCL and if it succeeds zero the memory
  583. * by truncating to 0 */
  584. if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR|O_EXCL,
  585. S_IRWXU|S_IRWXG|S_IRWXO)) > 0) {
  586. /* truncate file to length PCI device's memory */
  587. if (ftruncate(fd, s->ivshmem_size) != 0) {
  588. fprintf(stderr, "ivshmem: could not truncate shared file\n");
  589. }
  590. } else if ((fd = shm_open(s->shmobj, O_CREAT|O_RDWR,
  591. S_IRWXU|S_IRWXG|S_IRWXO)) < 0) {
  592. fprintf(stderr, "ivshmem: could not open shared file\n");
  593. exit(-1);
  594. }
  595. if (check_shm_size(s, fd) == -1) {
  596. exit(-1);
  597. }
  598. create_shared_memory_BAR(s, fd);
  599. }
  600. s->dev.config_write = ivshmem_write_config;
  601. return 0;
  602. }
  603. static void pci_ivshmem_uninit(PCIDevice *dev)
  604. {
  605. IVShmemState *s = DO_UPCAST(IVShmemState, dev, dev);
  606. if (s->migration_blocker) {
  607. migrate_del_blocker(s->migration_blocker);
  608. error_free(s->migration_blocker);
  609. }
  610. memory_region_destroy(&s->ivshmem_mmio);
  611. memory_region_del_subregion(&s->bar, &s->ivshmem);
  612. vmstate_unregister_ram(&s->ivshmem, &s->dev.qdev);
  613. memory_region_destroy(&s->ivshmem);
  614. memory_region_destroy(&s->bar);
  615. unregister_savevm(&dev->qdev, "ivshmem", s);
  616. }
  617. static Property ivshmem_properties[] = {
  618. DEFINE_PROP_CHR("chardev", IVShmemState, server_chr),
  619. DEFINE_PROP_STRING("size", IVShmemState, sizearg),
  620. DEFINE_PROP_UINT32("vectors", IVShmemState, vectors, 1),
  621. DEFINE_PROP_BIT("ioeventfd", IVShmemState, features, IVSHMEM_IOEVENTFD, false),
  622. DEFINE_PROP_BIT("msi", IVShmemState, features, IVSHMEM_MSI, true),
  623. DEFINE_PROP_STRING("shm", IVShmemState, shmobj),
  624. DEFINE_PROP_STRING("role", IVShmemState, role),
  625. DEFINE_PROP_UINT32("use64", IVShmemState, ivshmem_64bit, 1),
  626. DEFINE_PROP_END_OF_LIST(),
  627. };
  628. static void ivshmem_class_init(ObjectClass *klass, void *data)
  629. {
  630. DeviceClass *dc = DEVICE_CLASS(klass);
  631. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  632. k->init = pci_ivshmem_init;
  633. k->exit = pci_ivshmem_uninit;
  634. k->vendor_id = PCI_VENDOR_ID_IVSHMEM;
  635. k->device_id = PCI_DEVICE_ID_IVSHMEM;
  636. k->class_id = PCI_CLASS_MEMORY_RAM;
  637. dc->reset = ivshmem_reset;
  638. dc->props = ivshmem_properties;
  639. }
  640. static const TypeInfo ivshmem_info = {
  641. .name = "ivshmem",
  642. .parent = TYPE_PCI_DEVICE,
  643. .instance_size = sizeof(IVShmemState),
  644. .class_init = ivshmem_class_init,
  645. };
  646. static void ivshmem_register_types(void)
  647. {
  648. type_register_static(&ivshmem_info);
  649. }
  650. type_init(ivshmem_register_types)