virtio.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718
  1. /*
  2. * Virtio Support
  3. *
  4. * Copyright IBM, Corp. 2007
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qapi/error.h"
  15. #include "cpu.h"
  16. #include "trace.h"
  17. #include "exec/address-spaces.h"
  18. #include "qemu/error-report.h"
  19. #include "qemu/main-loop.h"
  20. #include "qemu/module.h"
  21. #include "hw/virtio/virtio.h"
  22. #include "migration/qemu-file-types.h"
  23. #include "qemu/atomic.h"
  24. #include "hw/virtio/virtio-bus.h"
  25. #include "hw/qdev-properties.h"
  26. #include "hw/virtio/virtio-access.h"
  27. #include "sysemu/dma.h"
  28. #include "sysemu/runstate.h"
  29. /*
  30. * The alignment to use between consumer and producer parts of vring.
  31. * x86 pagesize again. This is the default, used by transports like PCI
  32. * which don't provide a means for the guest to tell the host the alignment.
  33. */
  34. #define VIRTIO_PCI_VRING_ALIGN 4096
  35. typedef struct VRingDesc
  36. {
  37. uint64_t addr;
  38. uint32_t len;
  39. uint16_t flags;
  40. uint16_t next;
  41. } VRingDesc;
  42. typedef struct VRingPackedDesc {
  43. uint64_t addr;
  44. uint32_t len;
  45. uint16_t id;
  46. uint16_t flags;
  47. } VRingPackedDesc;
  48. typedef struct VRingAvail
  49. {
  50. uint16_t flags;
  51. uint16_t idx;
  52. uint16_t ring[0];
  53. } VRingAvail;
  54. typedef struct VRingUsedElem
  55. {
  56. uint32_t id;
  57. uint32_t len;
  58. } VRingUsedElem;
  59. typedef struct VRingUsed
  60. {
  61. uint16_t flags;
  62. uint16_t idx;
  63. VRingUsedElem ring[0];
  64. } VRingUsed;
  65. typedef struct VRingMemoryRegionCaches {
  66. struct rcu_head rcu;
  67. MemoryRegionCache desc;
  68. MemoryRegionCache avail;
  69. MemoryRegionCache used;
  70. } VRingMemoryRegionCaches;
  71. typedef struct VRing
  72. {
  73. unsigned int num;
  74. unsigned int num_default;
  75. unsigned int align;
  76. hwaddr desc;
  77. hwaddr avail;
  78. hwaddr used;
  79. VRingMemoryRegionCaches *caches;
  80. } VRing;
  81. typedef struct VRingPackedDescEvent {
  82. uint16_t off_wrap;
  83. uint16_t flags;
  84. } VRingPackedDescEvent ;
  85. struct VirtQueue
  86. {
  87. VRing vring;
  88. VirtQueueElement *used_elems;
  89. /* Next head to pop */
  90. uint16_t last_avail_idx;
  91. bool last_avail_wrap_counter;
  92. /* Last avail_idx read from VQ. */
  93. uint16_t shadow_avail_idx;
  94. bool shadow_avail_wrap_counter;
  95. uint16_t used_idx;
  96. bool used_wrap_counter;
  97. /* Last used index value we have signalled on */
  98. uint16_t signalled_used;
  99. /* Last used index value we have signalled on */
  100. bool signalled_used_valid;
  101. /* Notification enabled? */
  102. bool notification;
  103. uint16_t queue_index;
  104. unsigned int inuse;
  105. uint16_t vector;
  106. VirtIOHandleOutput handle_output;
  107. VirtIOHandleAIOOutput handle_aio_output;
  108. VirtIODevice *vdev;
  109. EventNotifier guest_notifier;
  110. EventNotifier host_notifier;
  111. bool host_notifier_enabled;
  112. QLIST_ENTRY(VirtQueue) node;
  113. };
  114. static void virtio_free_region_cache(VRingMemoryRegionCaches *caches)
  115. {
  116. if (!caches) {
  117. return;
  118. }
  119. address_space_cache_destroy(&caches->desc);
  120. address_space_cache_destroy(&caches->avail);
  121. address_space_cache_destroy(&caches->used);
  122. g_free(caches);
  123. }
  124. static void virtio_virtqueue_reset_region_cache(struct VirtQueue *vq)
  125. {
  126. VRingMemoryRegionCaches *caches;
  127. caches = atomic_read(&vq->vring.caches);
  128. atomic_rcu_set(&vq->vring.caches, NULL);
  129. if (caches) {
  130. call_rcu(caches, virtio_free_region_cache, rcu);
  131. }
  132. }
  133. static void virtio_init_region_cache(VirtIODevice *vdev, int n)
  134. {
  135. VirtQueue *vq = &vdev->vq[n];
  136. VRingMemoryRegionCaches *old = vq->vring.caches;
  137. VRingMemoryRegionCaches *new = NULL;
  138. hwaddr addr, size;
  139. int64_t len;
  140. bool packed;
  141. addr = vq->vring.desc;
  142. if (!addr) {
  143. goto out_no_cache;
  144. }
  145. new = g_new0(VRingMemoryRegionCaches, 1);
  146. size = virtio_queue_get_desc_size(vdev, n);
  147. packed = virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ?
  148. true : false;
  149. len = address_space_cache_init(&new->desc, vdev->dma_as,
  150. addr, size, packed);
  151. if (len < size) {
  152. virtio_error(vdev, "Cannot map desc");
  153. goto err_desc;
  154. }
  155. size = virtio_queue_get_used_size(vdev, n);
  156. len = address_space_cache_init(&new->used, vdev->dma_as,
  157. vq->vring.used, size, true);
  158. if (len < size) {
  159. virtio_error(vdev, "Cannot map used");
  160. goto err_used;
  161. }
  162. size = virtio_queue_get_avail_size(vdev, n);
  163. len = address_space_cache_init(&new->avail, vdev->dma_as,
  164. vq->vring.avail, size, false);
  165. if (len < size) {
  166. virtio_error(vdev, "Cannot map avail");
  167. goto err_avail;
  168. }
  169. atomic_rcu_set(&vq->vring.caches, new);
  170. if (old) {
  171. call_rcu(old, virtio_free_region_cache, rcu);
  172. }
  173. return;
  174. err_avail:
  175. address_space_cache_destroy(&new->avail);
  176. err_used:
  177. address_space_cache_destroy(&new->used);
  178. err_desc:
  179. address_space_cache_destroy(&new->desc);
  180. out_no_cache:
  181. g_free(new);
  182. virtio_virtqueue_reset_region_cache(vq);
  183. }
  184. /* virt queue functions */
  185. void virtio_queue_update_rings(VirtIODevice *vdev, int n)
  186. {
  187. VRing *vring = &vdev->vq[n].vring;
  188. if (!vring->num || !vring->desc || !vring->align) {
  189. /* not yet setup -> nothing to do */
  190. return;
  191. }
  192. vring->avail = vring->desc + vring->num * sizeof(VRingDesc);
  193. vring->used = vring_align(vring->avail +
  194. offsetof(VRingAvail, ring[vring->num]),
  195. vring->align);
  196. virtio_init_region_cache(vdev, n);
  197. }
  198. /* Called within rcu_read_lock(). */
  199. static void vring_split_desc_read(VirtIODevice *vdev, VRingDesc *desc,
  200. MemoryRegionCache *cache, int i)
  201. {
  202. address_space_read_cached(cache, i * sizeof(VRingDesc),
  203. desc, sizeof(VRingDesc));
  204. virtio_tswap64s(vdev, &desc->addr);
  205. virtio_tswap32s(vdev, &desc->len);
  206. virtio_tswap16s(vdev, &desc->flags);
  207. virtio_tswap16s(vdev, &desc->next);
  208. }
  209. static void vring_packed_event_read(VirtIODevice *vdev,
  210. MemoryRegionCache *cache,
  211. VRingPackedDescEvent *e)
  212. {
  213. hwaddr off_off = offsetof(VRingPackedDescEvent, off_wrap);
  214. hwaddr off_flags = offsetof(VRingPackedDescEvent, flags);
  215. address_space_read_cached(cache, off_flags, &e->flags,
  216. sizeof(e->flags));
  217. /* Make sure flags is seen before off_wrap */
  218. smp_rmb();
  219. address_space_read_cached(cache, off_off, &e->off_wrap,
  220. sizeof(e->off_wrap));
  221. virtio_tswap16s(vdev, &e->off_wrap);
  222. virtio_tswap16s(vdev, &e->flags);
  223. }
  224. static void vring_packed_off_wrap_write(VirtIODevice *vdev,
  225. MemoryRegionCache *cache,
  226. uint16_t off_wrap)
  227. {
  228. hwaddr off = offsetof(VRingPackedDescEvent, off_wrap);
  229. virtio_tswap16s(vdev, &off_wrap);
  230. address_space_write_cached(cache, off, &off_wrap, sizeof(off_wrap));
  231. address_space_cache_invalidate(cache, off, sizeof(off_wrap));
  232. }
  233. static void vring_packed_flags_write(VirtIODevice *vdev,
  234. MemoryRegionCache *cache, uint16_t flags)
  235. {
  236. hwaddr off = offsetof(VRingPackedDescEvent, flags);
  237. virtio_tswap16s(vdev, &flags);
  238. address_space_write_cached(cache, off, &flags, sizeof(flags));
  239. address_space_cache_invalidate(cache, off, sizeof(flags));
  240. }
  241. /* Called within rcu_read_lock(). */
  242. static VRingMemoryRegionCaches *vring_get_region_caches(struct VirtQueue *vq)
  243. {
  244. VRingMemoryRegionCaches *caches = atomic_rcu_read(&vq->vring.caches);
  245. assert(caches != NULL);
  246. return caches;
  247. }
  248. /* Called within rcu_read_lock(). */
  249. static inline uint16_t vring_avail_flags(VirtQueue *vq)
  250. {
  251. VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
  252. hwaddr pa = offsetof(VRingAvail, flags);
  253. return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
  254. }
  255. /* Called within rcu_read_lock(). */
  256. static inline uint16_t vring_avail_idx(VirtQueue *vq)
  257. {
  258. VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
  259. hwaddr pa = offsetof(VRingAvail, idx);
  260. vq->shadow_avail_idx = virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
  261. return vq->shadow_avail_idx;
  262. }
  263. /* Called within rcu_read_lock(). */
  264. static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
  265. {
  266. VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
  267. hwaddr pa = offsetof(VRingAvail, ring[i]);
  268. return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
  269. }
  270. /* Called within rcu_read_lock(). */
  271. static inline uint16_t vring_get_used_event(VirtQueue *vq)
  272. {
  273. return vring_avail_ring(vq, vq->vring.num);
  274. }
  275. /* Called within rcu_read_lock(). */
  276. static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem,
  277. int i)
  278. {
  279. VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
  280. hwaddr pa = offsetof(VRingUsed, ring[i]);
  281. virtio_tswap32s(vq->vdev, &uelem->id);
  282. virtio_tswap32s(vq->vdev, &uelem->len);
  283. address_space_write_cached(&caches->used, pa, uelem, sizeof(VRingUsedElem));
  284. address_space_cache_invalidate(&caches->used, pa, sizeof(VRingUsedElem));
  285. }
  286. /* Called within rcu_read_lock(). */
  287. static uint16_t vring_used_idx(VirtQueue *vq)
  288. {
  289. VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
  290. hwaddr pa = offsetof(VRingUsed, idx);
  291. return virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
  292. }
  293. /* Called within rcu_read_lock(). */
  294. static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
  295. {
  296. VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
  297. hwaddr pa = offsetof(VRingUsed, idx);
  298. virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val);
  299. address_space_cache_invalidate(&caches->used, pa, sizeof(val));
  300. vq->used_idx = val;
  301. }
  302. /* Called within rcu_read_lock(). */
  303. static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
  304. {
  305. VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
  306. VirtIODevice *vdev = vq->vdev;
  307. hwaddr pa = offsetof(VRingUsed, flags);
  308. uint16_t flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
  309. virtio_stw_phys_cached(vdev, &caches->used, pa, flags | mask);
  310. address_space_cache_invalidate(&caches->used, pa, sizeof(flags));
  311. }
  312. /* Called within rcu_read_lock(). */
  313. static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
  314. {
  315. VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
  316. VirtIODevice *vdev = vq->vdev;
  317. hwaddr pa = offsetof(VRingUsed, flags);
  318. uint16_t flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
  319. virtio_stw_phys_cached(vdev, &caches->used, pa, flags & ~mask);
  320. address_space_cache_invalidate(&caches->used, pa, sizeof(flags));
  321. }
  322. /* Called within rcu_read_lock(). */
  323. static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val)
  324. {
  325. VRingMemoryRegionCaches *caches;
  326. hwaddr pa;
  327. if (!vq->notification) {
  328. return;
  329. }
  330. caches = vring_get_region_caches(vq);
  331. pa = offsetof(VRingUsed, ring[vq->vring.num]);
  332. virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val);
  333. address_space_cache_invalidate(&caches->used, pa, sizeof(val));
  334. }
  335. static void virtio_queue_split_set_notification(VirtQueue *vq, int enable)
  336. {
  337. RCU_READ_LOCK_GUARD();
  338. if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
  339. vring_set_avail_event(vq, vring_avail_idx(vq));
  340. } else if (enable) {
  341. vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
  342. } else {
  343. vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
  344. }
  345. if (enable) {
  346. /* Expose avail event/used flags before caller checks the avail idx. */
  347. smp_mb();
  348. }
  349. }
  350. static void virtio_queue_packed_set_notification(VirtQueue *vq, int enable)
  351. {
  352. uint16_t off_wrap;
  353. VRingPackedDescEvent e;
  354. VRingMemoryRegionCaches *caches;
  355. RCU_READ_LOCK_GUARD();
  356. caches = vring_get_region_caches(vq);
  357. vring_packed_event_read(vq->vdev, &caches->used, &e);
  358. if (!enable) {
  359. e.flags = VRING_PACKED_EVENT_FLAG_DISABLE;
  360. } else if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
  361. off_wrap = vq->shadow_avail_idx | vq->shadow_avail_wrap_counter << 15;
  362. vring_packed_off_wrap_write(vq->vdev, &caches->used, off_wrap);
  363. /* Make sure off_wrap is wrote before flags */
  364. smp_wmb();
  365. e.flags = VRING_PACKED_EVENT_FLAG_DESC;
  366. } else {
  367. e.flags = VRING_PACKED_EVENT_FLAG_ENABLE;
  368. }
  369. vring_packed_flags_write(vq->vdev, &caches->used, e.flags);
  370. if (enable) {
  371. /* Expose avail event/used flags before caller checks the avail idx. */
  372. smp_mb();
  373. }
  374. }
  375. void virtio_queue_set_notification(VirtQueue *vq, int enable)
  376. {
  377. vq->notification = enable;
  378. if (!vq->vring.desc) {
  379. return;
  380. }
  381. if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
  382. virtio_queue_packed_set_notification(vq, enable);
  383. } else {
  384. virtio_queue_split_set_notification(vq, enable);
  385. }
  386. }
  387. int virtio_queue_ready(VirtQueue *vq)
  388. {
  389. return vq->vring.avail != 0;
  390. }
  391. static void vring_packed_desc_read_flags(VirtIODevice *vdev,
  392. uint16_t *flags,
  393. MemoryRegionCache *cache,
  394. int i)
  395. {
  396. address_space_read_cached(cache,
  397. i * sizeof(VRingPackedDesc) +
  398. offsetof(VRingPackedDesc, flags),
  399. flags, sizeof(*flags));
  400. virtio_tswap16s(vdev, flags);
  401. }
  402. static void vring_packed_desc_read(VirtIODevice *vdev,
  403. VRingPackedDesc *desc,
  404. MemoryRegionCache *cache,
  405. int i, bool strict_order)
  406. {
  407. hwaddr off = i * sizeof(VRingPackedDesc);
  408. vring_packed_desc_read_flags(vdev, &desc->flags, cache, i);
  409. if (strict_order) {
  410. /* Make sure flags is read before the rest fields. */
  411. smp_rmb();
  412. }
  413. address_space_read_cached(cache, off + offsetof(VRingPackedDesc, addr),
  414. &desc->addr, sizeof(desc->addr));
  415. address_space_read_cached(cache, off + offsetof(VRingPackedDesc, id),
  416. &desc->id, sizeof(desc->id));
  417. address_space_read_cached(cache, off + offsetof(VRingPackedDesc, len),
  418. &desc->len, sizeof(desc->len));
  419. virtio_tswap64s(vdev, &desc->addr);
  420. virtio_tswap16s(vdev, &desc->id);
  421. virtio_tswap32s(vdev, &desc->len);
  422. }
  423. static void vring_packed_desc_write_data(VirtIODevice *vdev,
  424. VRingPackedDesc *desc,
  425. MemoryRegionCache *cache,
  426. int i)
  427. {
  428. hwaddr off_id = i * sizeof(VRingPackedDesc) +
  429. offsetof(VRingPackedDesc, id);
  430. hwaddr off_len = i * sizeof(VRingPackedDesc) +
  431. offsetof(VRingPackedDesc, len);
  432. virtio_tswap32s(vdev, &desc->len);
  433. virtio_tswap16s(vdev, &desc->id);
  434. address_space_write_cached(cache, off_id, &desc->id, sizeof(desc->id));
  435. address_space_cache_invalidate(cache, off_id, sizeof(desc->id));
  436. address_space_write_cached(cache, off_len, &desc->len, sizeof(desc->len));
  437. address_space_cache_invalidate(cache, off_len, sizeof(desc->len));
  438. }
  439. static void vring_packed_desc_write_flags(VirtIODevice *vdev,
  440. VRingPackedDesc *desc,
  441. MemoryRegionCache *cache,
  442. int i)
  443. {
  444. hwaddr off = i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, flags);
  445. virtio_tswap16s(vdev, &desc->flags);
  446. address_space_write_cached(cache, off, &desc->flags, sizeof(desc->flags));
  447. address_space_cache_invalidate(cache, off, sizeof(desc->flags));
  448. }
  449. static void vring_packed_desc_write(VirtIODevice *vdev,
  450. VRingPackedDesc *desc,
  451. MemoryRegionCache *cache,
  452. int i, bool strict_order)
  453. {
  454. vring_packed_desc_write_data(vdev, desc, cache, i);
  455. if (strict_order) {
  456. /* Make sure data is wrote before flags. */
  457. smp_wmb();
  458. }
  459. vring_packed_desc_write_flags(vdev, desc, cache, i);
  460. }
  461. static inline bool is_desc_avail(uint16_t flags, bool wrap_counter)
  462. {
  463. bool avail, used;
  464. avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
  465. used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
  466. return (avail != used) && (avail == wrap_counter);
  467. }
  468. /* Fetch avail_idx from VQ memory only when we really need to know if
  469. * guest has added some buffers.
  470. * Called within rcu_read_lock(). */
  471. static int virtio_queue_empty_rcu(VirtQueue *vq)
  472. {
  473. if (unlikely(vq->vdev->broken)) {
  474. return 1;
  475. }
  476. if (unlikely(!vq->vring.avail)) {
  477. return 1;
  478. }
  479. if (vq->shadow_avail_idx != vq->last_avail_idx) {
  480. return 0;
  481. }
  482. return vring_avail_idx(vq) == vq->last_avail_idx;
  483. }
  484. static int virtio_queue_split_empty(VirtQueue *vq)
  485. {
  486. bool empty;
  487. if (unlikely(vq->vdev->broken)) {
  488. return 1;
  489. }
  490. if (unlikely(!vq->vring.avail)) {
  491. return 1;
  492. }
  493. if (vq->shadow_avail_idx != vq->last_avail_idx) {
  494. return 0;
  495. }
  496. RCU_READ_LOCK_GUARD();
  497. empty = vring_avail_idx(vq) == vq->last_avail_idx;
  498. return empty;
  499. }
  500. static int virtio_queue_packed_empty_rcu(VirtQueue *vq)
  501. {
  502. struct VRingPackedDesc desc;
  503. VRingMemoryRegionCaches *cache;
  504. if (unlikely(!vq->vring.desc)) {
  505. return 1;
  506. }
  507. cache = vring_get_region_caches(vq);
  508. vring_packed_desc_read_flags(vq->vdev, &desc.flags, &cache->desc,
  509. vq->last_avail_idx);
  510. return !is_desc_avail(desc.flags, vq->last_avail_wrap_counter);
  511. }
  512. static int virtio_queue_packed_empty(VirtQueue *vq)
  513. {
  514. RCU_READ_LOCK_GUARD();
  515. return virtio_queue_packed_empty_rcu(vq);
  516. }
  517. int virtio_queue_empty(VirtQueue *vq)
  518. {
  519. if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
  520. return virtio_queue_packed_empty(vq);
  521. } else {
  522. return virtio_queue_split_empty(vq);
  523. }
  524. }
  525. static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
  526. unsigned int len)
  527. {
  528. AddressSpace *dma_as = vq->vdev->dma_as;
  529. unsigned int offset;
  530. int i;
  531. offset = 0;
  532. for (i = 0; i < elem->in_num; i++) {
  533. size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
  534. dma_memory_unmap(dma_as, elem->in_sg[i].iov_base,
  535. elem->in_sg[i].iov_len,
  536. DMA_DIRECTION_FROM_DEVICE, size);
  537. offset += size;
  538. }
  539. for (i = 0; i < elem->out_num; i++)
  540. dma_memory_unmap(dma_as, elem->out_sg[i].iov_base,
  541. elem->out_sg[i].iov_len,
  542. DMA_DIRECTION_TO_DEVICE,
  543. elem->out_sg[i].iov_len);
  544. }
  545. /* virtqueue_detach_element:
  546. * @vq: The #VirtQueue
  547. * @elem: The #VirtQueueElement
  548. * @len: number of bytes written
  549. *
  550. * Detach the element from the virtqueue. This function is suitable for device
  551. * reset or other situations where a #VirtQueueElement is simply freed and will
  552. * not be pushed or discarded.
  553. */
  554. void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
  555. unsigned int len)
  556. {
  557. vq->inuse -= elem->ndescs;
  558. virtqueue_unmap_sg(vq, elem, len);
  559. }
  560. static void virtqueue_split_rewind(VirtQueue *vq, unsigned int num)
  561. {
  562. vq->last_avail_idx -= num;
  563. }
  564. static void virtqueue_packed_rewind(VirtQueue *vq, unsigned int num)
  565. {
  566. if (vq->last_avail_idx < num) {
  567. vq->last_avail_idx = vq->vring.num + vq->last_avail_idx - num;
  568. vq->last_avail_wrap_counter ^= 1;
  569. } else {
  570. vq->last_avail_idx -= num;
  571. }
  572. }
  573. /* virtqueue_unpop:
  574. * @vq: The #VirtQueue
  575. * @elem: The #VirtQueueElement
  576. * @len: number of bytes written
  577. *
  578. * Pretend the most recent element wasn't popped from the virtqueue. The next
  579. * call to virtqueue_pop() will refetch the element.
  580. */
  581. void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
  582. unsigned int len)
  583. {
  584. if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
  585. virtqueue_packed_rewind(vq, 1);
  586. } else {
  587. virtqueue_split_rewind(vq, 1);
  588. }
  589. virtqueue_detach_element(vq, elem, len);
  590. }
  591. /* virtqueue_rewind:
  592. * @vq: The #VirtQueue
  593. * @num: Number of elements to push back
  594. *
  595. * Pretend that elements weren't popped from the virtqueue. The next
  596. * virtqueue_pop() will refetch the oldest element.
  597. *
  598. * Use virtqueue_unpop() instead if you have a VirtQueueElement.
  599. *
  600. * Returns: true on success, false if @num is greater than the number of in use
  601. * elements.
  602. */
  603. bool virtqueue_rewind(VirtQueue *vq, unsigned int num)
  604. {
  605. if (num > vq->inuse) {
  606. return false;
  607. }
  608. vq->inuse -= num;
  609. if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
  610. virtqueue_packed_rewind(vq, num);
  611. } else {
  612. virtqueue_split_rewind(vq, num);
  613. }
  614. return true;
  615. }
  616. static void virtqueue_split_fill(VirtQueue *vq, const VirtQueueElement *elem,
  617. unsigned int len, unsigned int idx)
  618. {
  619. VRingUsedElem uelem;
  620. if (unlikely(!vq->vring.used)) {
  621. return;
  622. }
  623. idx = (idx + vq->used_idx) % vq->vring.num;
  624. uelem.id = elem->index;
  625. uelem.len = len;
  626. vring_used_write(vq, &uelem, idx);
  627. }
  628. static void virtqueue_packed_fill(VirtQueue *vq, const VirtQueueElement *elem,
  629. unsigned int len, unsigned int idx)
  630. {
  631. vq->used_elems[idx].index = elem->index;
  632. vq->used_elems[idx].len = len;
  633. vq->used_elems[idx].ndescs = elem->ndescs;
  634. }
  635. static void virtqueue_packed_fill_desc(VirtQueue *vq,
  636. const VirtQueueElement *elem,
  637. unsigned int idx,
  638. bool strict_order)
  639. {
  640. uint16_t head;
  641. VRingMemoryRegionCaches *caches;
  642. VRingPackedDesc desc = {
  643. .id = elem->index,
  644. .len = elem->len,
  645. };
  646. bool wrap_counter = vq->used_wrap_counter;
  647. if (unlikely(!vq->vring.desc)) {
  648. return;
  649. }
  650. head = vq->used_idx + idx;
  651. if (head >= vq->vring.num) {
  652. head -= vq->vring.num;
  653. wrap_counter ^= 1;
  654. }
  655. if (wrap_counter) {
  656. desc.flags |= (1 << VRING_PACKED_DESC_F_AVAIL);
  657. desc.flags |= (1 << VRING_PACKED_DESC_F_USED);
  658. } else {
  659. desc.flags &= ~(1 << VRING_PACKED_DESC_F_AVAIL);
  660. desc.flags &= ~(1 << VRING_PACKED_DESC_F_USED);
  661. }
  662. caches = vring_get_region_caches(vq);
  663. vring_packed_desc_write(vq->vdev, &desc, &caches->desc, head, strict_order);
  664. }
  665. /* Called within rcu_read_lock(). */
  666. void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
  667. unsigned int len, unsigned int idx)
  668. {
  669. trace_virtqueue_fill(vq, elem, len, idx);
  670. virtqueue_unmap_sg(vq, elem, len);
  671. if (unlikely(vq->vdev->broken)) {
  672. return;
  673. }
  674. if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
  675. virtqueue_packed_fill(vq, elem, len, idx);
  676. } else {
  677. virtqueue_split_fill(vq, elem, len, idx);
  678. }
  679. }
  680. /* Called within rcu_read_lock(). */
  681. static void virtqueue_split_flush(VirtQueue *vq, unsigned int count)
  682. {
  683. uint16_t old, new;
  684. if (unlikely(!vq->vring.used)) {
  685. return;
  686. }
  687. /* Make sure buffer is written before we update index. */
  688. smp_wmb();
  689. trace_virtqueue_flush(vq, count);
  690. old = vq->used_idx;
  691. new = old + count;
  692. vring_used_idx_set(vq, new);
  693. vq->inuse -= count;
  694. if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)))
  695. vq->signalled_used_valid = false;
  696. }
  697. static void virtqueue_packed_flush(VirtQueue *vq, unsigned int count)
  698. {
  699. unsigned int i, ndescs = 0;
  700. if (unlikely(!vq->vring.desc)) {
  701. return;
  702. }
  703. for (i = 1; i < count; i++) {
  704. virtqueue_packed_fill_desc(vq, &vq->used_elems[i], i, false);
  705. ndescs += vq->used_elems[i].ndescs;
  706. }
  707. virtqueue_packed_fill_desc(vq, &vq->used_elems[0], 0, true);
  708. ndescs += vq->used_elems[0].ndescs;
  709. vq->inuse -= ndescs;
  710. vq->used_idx += ndescs;
  711. if (vq->used_idx >= vq->vring.num) {
  712. vq->used_idx -= vq->vring.num;
  713. vq->used_wrap_counter ^= 1;
  714. }
  715. }
  716. void virtqueue_flush(VirtQueue *vq, unsigned int count)
  717. {
  718. if (unlikely(vq->vdev->broken)) {
  719. vq->inuse -= count;
  720. return;
  721. }
  722. if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
  723. virtqueue_packed_flush(vq, count);
  724. } else {
  725. virtqueue_split_flush(vq, count);
  726. }
  727. }
  728. void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
  729. unsigned int len)
  730. {
  731. RCU_READ_LOCK_GUARD();
  732. virtqueue_fill(vq, elem, len, 0);
  733. virtqueue_flush(vq, 1);
  734. }
  735. /* Called within rcu_read_lock(). */
  736. static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
  737. {
  738. uint16_t num_heads = vring_avail_idx(vq) - idx;
  739. /* Check it isn't doing very strange things with descriptor numbers. */
  740. if (num_heads > vq->vring.num) {
  741. virtio_error(vq->vdev, "Guest moved used index from %u to %u",
  742. idx, vq->shadow_avail_idx);
  743. return -EINVAL;
  744. }
  745. /* On success, callers read a descriptor at vq->last_avail_idx.
  746. * Make sure descriptor read does not bypass avail index read. */
  747. if (num_heads) {
  748. smp_rmb();
  749. }
  750. return num_heads;
  751. }
  752. /* Called within rcu_read_lock(). */
  753. static bool virtqueue_get_head(VirtQueue *vq, unsigned int idx,
  754. unsigned int *head)
  755. {
  756. /* Grab the next descriptor number they're advertising, and increment
  757. * the index we've seen. */
  758. *head = vring_avail_ring(vq, idx % vq->vring.num);
  759. /* If their number is silly, that's a fatal mistake. */
  760. if (*head >= vq->vring.num) {
  761. virtio_error(vq->vdev, "Guest says index %u is available", *head);
  762. return false;
  763. }
  764. return true;
  765. }
  766. enum {
  767. VIRTQUEUE_READ_DESC_ERROR = -1,
  768. VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */
  769. VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */
  770. };
  771. static int virtqueue_split_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
  772. MemoryRegionCache *desc_cache,
  773. unsigned int max, unsigned int *next)
  774. {
  775. /* If this descriptor says it doesn't chain, we're done. */
  776. if (!(desc->flags & VRING_DESC_F_NEXT)) {
  777. return VIRTQUEUE_READ_DESC_DONE;
  778. }
  779. /* Check they're not leading us off end of descriptors. */
  780. *next = desc->next;
  781. /* Make sure compiler knows to grab that: we don't want it changing! */
  782. smp_wmb();
  783. if (*next >= max) {
  784. virtio_error(vdev, "Desc next is %u", *next);
  785. return VIRTQUEUE_READ_DESC_ERROR;
  786. }
  787. vring_split_desc_read(vdev, desc, desc_cache, *next);
  788. return VIRTQUEUE_READ_DESC_MORE;
  789. }
  790. static void virtqueue_split_get_avail_bytes(VirtQueue *vq,
  791. unsigned int *in_bytes, unsigned int *out_bytes,
  792. unsigned max_in_bytes, unsigned max_out_bytes)
  793. {
  794. VirtIODevice *vdev = vq->vdev;
  795. unsigned int max, idx;
  796. unsigned int total_bufs, in_total, out_total;
  797. VRingMemoryRegionCaches *caches;
  798. MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
  799. int64_t len = 0;
  800. int rc;
  801. RCU_READ_LOCK_GUARD();
  802. idx = vq->last_avail_idx;
  803. total_bufs = in_total = out_total = 0;
  804. max = vq->vring.num;
  805. caches = vring_get_region_caches(vq);
  806. while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
  807. MemoryRegionCache *desc_cache = &caches->desc;
  808. unsigned int num_bufs;
  809. VRingDesc desc;
  810. unsigned int i;
  811. num_bufs = total_bufs;
  812. if (!virtqueue_get_head(vq, idx++, &i)) {
  813. goto err;
  814. }
  815. vring_split_desc_read(vdev, &desc, desc_cache, i);
  816. if (desc.flags & VRING_DESC_F_INDIRECT) {
  817. if (!desc.len || (desc.len % sizeof(VRingDesc))) {
  818. virtio_error(vdev, "Invalid size for indirect buffer table");
  819. goto err;
  820. }
  821. /* If we've got too many, that implies a descriptor loop. */
  822. if (num_bufs >= max) {
  823. virtio_error(vdev, "Looped descriptor");
  824. goto err;
  825. }
  826. /* loop over the indirect descriptor table */
  827. len = address_space_cache_init(&indirect_desc_cache,
  828. vdev->dma_as,
  829. desc.addr, desc.len, false);
  830. desc_cache = &indirect_desc_cache;
  831. if (len < desc.len) {
  832. virtio_error(vdev, "Cannot map indirect buffer");
  833. goto err;
  834. }
  835. max = desc.len / sizeof(VRingDesc);
  836. num_bufs = i = 0;
  837. vring_split_desc_read(vdev, &desc, desc_cache, i);
  838. }
  839. do {
  840. /* If we've got too many, that implies a descriptor loop. */
  841. if (++num_bufs > max) {
  842. virtio_error(vdev, "Looped descriptor");
  843. goto err;
  844. }
  845. if (desc.flags & VRING_DESC_F_WRITE) {
  846. in_total += desc.len;
  847. } else {
  848. out_total += desc.len;
  849. }
  850. if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
  851. goto done;
  852. }
  853. rc = virtqueue_split_read_next_desc(vdev, &desc, desc_cache, max, &i);
  854. } while (rc == VIRTQUEUE_READ_DESC_MORE);
  855. if (rc == VIRTQUEUE_READ_DESC_ERROR) {
  856. goto err;
  857. }
  858. if (desc_cache == &indirect_desc_cache) {
  859. address_space_cache_destroy(&indirect_desc_cache);
  860. total_bufs++;
  861. } else {
  862. total_bufs = num_bufs;
  863. }
  864. }
  865. if (rc < 0) {
  866. goto err;
  867. }
  868. done:
  869. address_space_cache_destroy(&indirect_desc_cache);
  870. if (in_bytes) {
  871. *in_bytes = in_total;
  872. }
  873. if (out_bytes) {
  874. *out_bytes = out_total;
  875. }
  876. return;
  877. err:
  878. in_total = out_total = 0;
  879. goto done;
  880. }
  881. static int virtqueue_packed_read_next_desc(VirtQueue *vq,
  882. VRingPackedDesc *desc,
  883. MemoryRegionCache
  884. *desc_cache,
  885. unsigned int max,
  886. unsigned int *next,
  887. bool indirect)
  888. {
  889. /* If this descriptor says it doesn't chain, we're done. */
  890. if (!indirect && !(desc->flags & VRING_DESC_F_NEXT)) {
  891. return VIRTQUEUE_READ_DESC_DONE;
  892. }
  893. ++*next;
  894. if (*next == max) {
  895. if (indirect) {
  896. return VIRTQUEUE_READ_DESC_DONE;
  897. } else {
  898. (*next) -= vq->vring.num;
  899. }
  900. }
  901. vring_packed_desc_read(vq->vdev, desc, desc_cache, *next, false);
  902. return VIRTQUEUE_READ_DESC_MORE;
  903. }
  904. static void virtqueue_packed_get_avail_bytes(VirtQueue *vq,
  905. unsigned int *in_bytes,
  906. unsigned int *out_bytes,
  907. unsigned max_in_bytes,
  908. unsigned max_out_bytes)
  909. {
  910. VirtIODevice *vdev = vq->vdev;
  911. unsigned int max, idx;
  912. unsigned int total_bufs, in_total, out_total;
  913. MemoryRegionCache *desc_cache;
  914. VRingMemoryRegionCaches *caches;
  915. MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
  916. int64_t len = 0;
  917. VRingPackedDesc desc;
  918. bool wrap_counter;
  919. RCU_READ_LOCK_GUARD();
  920. idx = vq->last_avail_idx;
  921. wrap_counter = vq->last_avail_wrap_counter;
  922. total_bufs = in_total = out_total = 0;
  923. max = vq->vring.num;
  924. caches = vring_get_region_caches(vq);
  925. for (;;) {
  926. unsigned int num_bufs = total_bufs;
  927. unsigned int i = idx;
  928. int rc;
  929. desc_cache = &caches->desc;
  930. vring_packed_desc_read(vdev, &desc, desc_cache, idx, true);
  931. if (!is_desc_avail(desc.flags, wrap_counter)) {
  932. break;
  933. }
  934. if (desc.flags & VRING_DESC_F_INDIRECT) {
  935. if (desc.len % sizeof(VRingPackedDesc)) {
  936. virtio_error(vdev, "Invalid size for indirect buffer table");
  937. goto err;
  938. }
  939. /* If we've got too many, that implies a descriptor loop. */
  940. if (num_bufs >= max) {
  941. virtio_error(vdev, "Looped descriptor");
  942. goto err;
  943. }
  944. /* loop over the indirect descriptor table */
  945. len = address_space_cache_init(&indirect_desc_cache,
  946. vdev->dma_as,
  947. desc.addr, desc.len, false);
  948. desc_cache = &indirect_desc_cache;
  949. if (len < desc.len) {
  950. virtio_error(vdev, "Cannot map indirect buffer");
  951. goto err;
  952. }
  953. max = desc.len / sizeof(VRingPackedDesc);
  954. num_bufs = i = 0;
  955. vring_packed_desc_read(vdev, &desc, desc_cache, i, false);
  956. }
  957. do {
  958. /* If we've got too many, that implies a descriptor loop. */
  959. if (++num_bufs > max) {
  960. virtio_error(vdev, "Looped descriptor");
  961. goto err;
  962. }
  963. if (desc.flags & VRING_DESC_F_WRITE) {
  964. in_total += desc.len;
  965. } else {
  966. out_total += desc.len;
  967. }
  968. if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
  969. goto done;
  970. }
  971. rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max,
  972. &i, desc_cache ==
  973. &indirect_desc_cache);
  974. } while (rc == VIRTQUEUE_READ_DESC_MORE);
  975. if (desc_cache == &indirect_desc_cache) {
  976. address_space_cache_destroy(&indirect_desc_cache);
  977. total_bufs++;
  978. idx++;
  979. } else {
  980. idx += num_bufs - total_bufs;
  981. total_bufs = num_bufs;
  982. }
  983. if (idx >= vq->vring.num) {
  984. idx -= vq->vring.num;
  985. wrap_counter ^= 1;
  986. }
  987. }
  988. /* Record the index and wrap counter for a kick we want */
  989. vq->shadow_avail_idx = idx;
  990. vq->shadow_avail_wrap_counter = wrap_counter;
  991. done:
  992. address_space_cache_destroy(&indirect_desc_cache);
  993. if (in_bytes) {
  994. *in_bytes = in_total;
  995. }
  996. if (out_bytes) {
  997. *out_bytes = out_total;
  998. }
  999. return;
  1000. err:
  1001. in_total = out_total = 0;
  1002. goto done;
  1003. }
  1004. void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
  1005. unsigned int *out_bytes,
  1006. unsigned max_in_bytes, unsigned max_out_bytes)
  1007. {
  1008. uint16_t desc_size;
  1009. VRingMemoryRegionCaches *caches;
  1010. if (unlikely(!vq->vring.desc)) {
  1011. goto err;
  1012. }
  1013. caches = vring_get_region_caches(vq);
  1014. desc_size = virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ?
  1015. sizeof(VRingPackedDesc) : sizeof(VRingDesc);
  1016. if (caches->desc.len < vq->vring.num * desc_size) {
  1017. virtio_error(vq->vdev, "Cannot map descriptor ring");
  1018. goto err;
  1019. }
  1020. if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
  1021. virtqueue_packed_get_avail_bytes(vq, in_bytes, out_bytes,
  1022. max_in_bytes, max_out_bytes);
  1023. } else {
  1024. virtqueue_split_get_avail_bytes(vq, in_bytes, out_bytes,
  1025. max_in_bytes, max_out_bytes);
  1026. }
  1027. return;
  1028. err:
  1029. if (in_bytes) {
  1030. *in_bytes = 0;
  1031. }
  1032. if (out_bytes) {
  1033. *out_bytes = 0;
  1034. }
  1035. }
  1036. int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
  1037. unsigned int out_bytes)
  1038. {
  1039. unsigned int in_total, out_total;
  1040. virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes);
  1041. return in_bytes <= in_total && out_bytes <= out_total;
  1042. }
  1043. static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg,
  1044. hwaddr *addr, struct iovec *iov,
  1045. unsigned int max_num_sg, bool is_write,
  1046. hwaddr pa, size_t sz)
  1047. {
  1048. bool ok = false;
  1049. unsigned num_sg = *p_num_sg;
  1050. assert(num_sg <= max_num_sg);
  1051. if (!sz) {
  1052. virtio_error(vdev, "virtio: zero sized buffers are not allowed");
  1053. goto out;
  1054. }
  1055. while (sz) {
  1056. hwaddr len = sz;
  1057. if (num_sg == max_num_sg) {
  1058. virtio_error(vdev, "virtio: too many write descriptors in "
  1059. "indirect table");
  1060. goto out;
  1061. }
  1062. iov[num_sg].iov_base = dma_memory_map(vdev->dma_as, pa, &len,
  1063. is_write ?
  1064. DMA_DIRECTION_FROM_DEVICE :
  1065. DMA_DIRECTION_TO_DEVICE);
  1066. if (!iov[num_sg].iov_base) {
  1067. virtio_error(vdev, "virtio: bogus descriptor or out of resources");
  1068. goto out;
  1069. }
  1070. iov[num_sg].iov_len = len;
  1071. addr[num_sg] = pa;
  1072. sz -= len;
  1073. pa += len;
  1074. num_sg++;
  1075. }
  1076. ok = true;
  1077. out:
  1078. *p_num_sg = num_sg;
  1079. return ok;
  1080. }
  1081. /* Only used by error code paths before we have a VirtQueueElement (therefore
  1082. * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to
  1083. * yet.
  1084. */
  1085. static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num,
  1086. struct iovec *iov)
  1087. {
  1088. unsigned int i;
  1089. for (i = 0; i < out_num + in_num; i++) {
  1090. int is_write = i >= out_num;
  1091. cpu_physical_memory_unmap(iov->iov_base, iov->iov_len, is_write, 0);
  1092. iov++;
  1093. }
  1094. }
  1095. static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg,
  1096. hwaddr *addr, unsigned int num_sg,
  1097. int is_write)
  1098. {
  1099. unsigned int i;
  1100. hwaddr len;
  1101. for (i = 0; i < num_sg; i++) {
  1102. len = sg[i].iov_len;
  1103. sg[i].iov_base = dma_memory_map(vdev->dma_as,
  1104. addr[i], &len, is_write ?
  1105. DMA_DIRECTION_FROM_DEVICE :
  1106. DMA_DIRECTION_TO_DEVICE);
  1107. if (!sg[i].iov_base) {
  1108. error_report("virtio: error trying to map MMIO memory");
  1109. exit(1);
  1110. }
  1111. if (len != sg[i].iov_len) {
  1112. error_report("virtio: unexpected memory split");
  1113. exit(1);
  1114. }
  1115. }
  1116. }
  1117. void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem)
  1118. {
  1119. virtqueue_map_iovec(vdev, elem->in_sg, elem->in_addr, elem->in_num, 1);
  1120. virtqueue_map_iovec(vdev, elem->out_sg, elem->out_addr, elem->out_num, 0);
  1121. }
  1122. static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num)
  1123. {
  1124. VirtQueueElement *elem;
  1125. size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));
  1126. size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]);
  1127. size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]);
  1128. size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0]));
  1129. size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
  1130. size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);
  1131. assert(sz >= sizeof(VirtQueueElement));
  1132. elem = g_malloc(out_sg_end);
  1133. trace_virtqueue_alloc_element(elem, sz, in_num, out_num);
  1134. elem->out_num = out_num;
  1135. elem->in_num = in_num;
  1136. elem->in_addr = (void *)elem + in_addr_ofs;
  1137. elem->out_addr = (void *)elem + out_addr_ofs;
  1138. elem->in_sg = (void *)elem + in_sg_ofs;
  1139. elem->out_sg = (void *)elem + out_sg_ofs;
  1140. return elem;
  1141. }
  1142. static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)
  1143. {
  1144. unsigned int i, head, max;
  1145. VRingMemoryRegionCaches *caches;
  1146. MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
  1147. MemoryRegionCache *desc_cache;
  1148. int64_t len;
  1149. VirtIODevice *vdev = vq->vdev;
  1150. VirtQueueElement *elem = NULL;
  1151. unsigned out_num, in_num, elem_entries;
  1152. hwaddr addr[VIRTQUEUE_MAX_SIZE];
  1153. struct iovec iov[VIRTQUEUE_MAX_SIZE];
  1154. VRingDesc desc;
  1155. int rc;
  1156. RCU_READ_LOCK_GUARD();
  1157. if (virtio_queue_empty_rcu(vq)) {
  1158. goto done;
  1159. }
  1160. /* Needed after virtio_queue_empty(), see comment in
  1161. * virtqueue_num_heads(). */
  1162. smp_rmb();
  1163. /* When we start there are none of either input nor output. */
  1164. out_num = in_num = elem_entries = 0;
  1165. max = vq->vring.num;
  1166. if (vq->inuse >= vq->vring.num) {
  1167. virtio_error(vdev, "Virtqueue size exceeded");
  1168. goto done;
  1169. }
  1170. if (!virtqueue_get_head(vq, vq->last_avail_idx++, &head)) {
  1171. goto done;
  1172. }
  1173. if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
  1174. vring_set_avail_event(vq, vq->last_avail_idx);
  1175. }
  1176. i = head;
  1177. caches = vring_get_region_caches(vq);
  1178. if (caches->desc.len < max * sizeof(VRingDesc)) {
  1179. virtio_error(vdev, "Cannot map descriptor ring");
  1180. goto done;
  1181. }
  1182. desc_cache = &caches->desc;
  1183. vring_split_desc_read(vdev, &desc, desc_cache, i);
  1184. if (desc.flags & VRING_DESC_F_INDIRECT) {
  1185. if (!desc.len || (desc.len % sizeof(VRingDesc))) {
  1186. virtio_error(vdev, "Invalid size for indirect buffer table");
  1187. goto done;
  1188. }
  1189. /* loop over the indirect descriptor table */
  1190. len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as,
  1191. desc.addr, desc.len, false);
  1192. desc_cache = &indirect_desc_cache;
  1193. if (len < desc.len) {
  1194. virtio_error(vdev, "Cannot map indirect buffer");
  1195. goto done;
  1196. }
  1197. max = desc.len / sizeof(VRingDesc);
  1198. i = 0;
  1199. vring_split_desc_read(vdev, &desc, desc_cache, i);
  1200. }
  1201. /* Collect all the descriptors */
  1202. do {
  1203. bool map_ok;
  1204. if (desc.flags & VRING_DESC_F_WRITE) {
  1205. map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
  1206. iov + out_num,
  1207. VIRTQUEUE_MAX_SIZE - out_num, true,
  1208. desc.addr, desc.len);
  1209. } else {
  1210. if (in_num) {
  1211. virtio_error(vdev, "Incorrect order for descriptors");
  1212. goto err_undo_map;
  1213. }
  1214. map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
  1215. VIRTQUEUE_MAX_SIZE, false,
  1216. desc.addr, desc.len);
  1217. }
  1218. if (!map_ok) {
  1219. goto err_undo_map;
  1220. }
  1221. /* If we've got too many, that implies a descriptor loop. */
  1222. if (++elem_entries > max) {
  1223. virtio_error(vdev, "Looped descriptor");
  1224. goto err_undo_map;
  1225. }
  1226. rc = virtqueue_split_read_next_desc(vdev, &desc, desc_cache, max, &i);
  1227. } while (rc == VIRTQUEUE_READ_DESC_MORE);
  1228. if (rc == VIRTQUEUE_READ_DESC_ERROR) {
  1229. goto err_undo_map;
  1230. }
  1231. /* Now copy what we have collected and mapped */
  1232. elem = virtqueue_alloc_element(sz, out_num, in_num);
  1233. elem->index = head;
  1234. elem->ndescs = 1;
  1235. for (i = 0; i < out_num; i++) {
  1236. elem->out_addr[i] = addr[i];
  1237. elem->out_sg[i] = iov[i];
  1238. }
  1239. for (i = 0; i < in_num; i++) {
  1240. elem->in_addr[i] = addr[out_num + i];
  1241. elem->in_sg[i] = iov[out_num + i];
  1242. }
  1243. vq->inuse++;
  1244. trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
  1245. done:
  1246. address_space_cache_destroy(&indirect_desc_cache);
  1247. return elem;
  1248. err_undo_map:
  1249. virtqueue_undo_map_desc(out_num, in_num, iov);
  1250. goto done;
  1251. }
  1252. static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)
  1253. {
  1254. unsigned int i, max;
  1255. VRingMemoryRegionCaches *caches;
  1256. MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
  1257. MemoryRegionCache *desc_cache;
  1258. int64_t len;
  1259. VirtIODevice *vdev = vq->vdev;
  1260. VirtQueueElement *elem = NULL;
  1261. unsigned out_num, in_num, elem_entries;
  1262. hwaddr addr[VIRTQUEUE_MAX_SIZE];
  1263. struct iovec iov[VIRTQUEUE_MAX_SIZE];
  1264. VRingPackedDesc desc;
  1265. uint16_t id;
  1266. int rc;
  1267. RCU_READ_LOCK_GUARD();
  1268. if (virtio_queue_packed_empty_rcu(vq)) {
  1269. goto done;
  1270. }
  1271. /* When we start there are none of either input nor output. */
  1272. out_num = in_num = elem_entries = 0;
  1273. max = vq->vring.num;
  1274. if (vq->inuse >= vq->vring.num) {
  1275. virtio_error(vdev, "Virtqueue size exceeded");
  1276. goto done;
  1277. }
  1278. i = vq->last_avail_idx;
  1279. caches = vring_get_region_caches(vq);
  1280. if (caches->desc.len < max * sizeof(VRingDesc)) {
  1281. virtio_error(vdev, "Cannot map descriptor ring");
  1282. goto done;
  1283. }
  1284. desc_cache = &caches->desc;
  1285. vring_packed_desc_read(vdev, &desc, desc_cache, i, true);
  1286. id = desc.id;
  1287. if (desc.flags & VRING_DESC_F_INDIRECT) {
  1288. if (desc.len % sizeof(VRingPackedDesc)) {
  1289. virtio_error(vdev, "Invalid size for indirect buffer table");
  1290. goto done;
  1291. }
  1292. /* loop over the indirect descriptor table */
  1293. len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as,
  1294. desc.addr, desc.len, false);
  1295. desc_cache = &indirect_desc_cache;
  1296. if (len < desc.len) {
  1297. virtio_error(vdev, "Cannot map indirect buffer");
  1298. goto done;
  1299. }
  1300. max = desc.len / sizeof(VRingPackedDesc);
  1301. i = 0;
  1302. vring_packed_desc_read(vdev, &desc, desc_cache, i, false);
  1303. }
  1304. /* Collect all the descriptors */
  1305. do {
  1306. bool map_ok;
  1307. if (desc.flags & VRING_DESC_F_WRITE) {
  1308. map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
  1309. iov + out_num,
  1310. VIRTQUEUE_MAX_SIZE - out_num, true,
  1311. desc.addr, desc.len);
  1312. } else {
  1313. if (in_num) {
  1314. virtio_error(vdev, "Incorrect order for descriptors");
  1315. goto err_undo_map;
  1316. }
  1317. map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
  1318. VIRTQUEUE_MAX_SIZE, false,
  1319. desc.addr, desc.len);
  1320. }
  1321. if (!map_ok) {
  1322. goto err_undo_map;
  1323. }
  1324. /* If we've got too many, that implies a descriptor loop. */
  1325. if (++elem_entries > max) {
  1326. virtio_error(vdev, "Looped descriptor");
  1327. goto err_undo_map;
  1328. }
  1329. rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max, &i,
  1330. desc_cache ==
  1331. &indirect_desc_cache);
  1332. } while (rc == VIRTQUEUE_READ_DESC_MORE);
  1333. /* Now copy what we have collected and mapped */
  1334. elem = virtqueue_alloc_element(sz, out_num, in_num);
  1335. for (i = 0; i < out_num; i++) {
  1336. elem->out_addr[i] = addr[i];
  1337. elem->out_sg[i] = iov[i];
  1338. }
  1339. for (i = 0; i < in_num; i++) {
  1340. elem->in_addr[i] = addr[out_num + i];
  1341. elem->in_sg[i] = iov[out_num + i];
  1342. }
  1343. elem->index = id;
  1344. elem->ndescs = (desc_cache == &indirect_desc_cache) ? 1 : elem_entries;
  1345. vq->last_avail_idx += elem->ndescs;
  1346. vq->inuse += elem->ndescs;
  1347. if (vq->last_avail_idx >= vq->vring.num) {
  1348. vq->last_avail_idx -= vq->vring.num;
  1349. vq->last_avail_wrap_counter ^= 1;
  1350. }
  1351. vq->shadow_avail_idx = vq->last_avail_idx;
  1352. vq->shadow_avail_wrap_counter = vq->last_avail_wrap_counter;
  1353. trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
  1354. done:
  1355. address_space_cache_destroy(&indirect_desc_cache);
  1356. return elem;
  1357. err_undo_map:
  1358. virtqueue_undo_map_desc(out_num, in_num, iov);
  1359. goto done;
  1360. }
  1361. void *virtqueue_pop(VirtQueue *vq, size_t sz)
  1362. {
  1363. if (unlikely(vq->vdev->broken)) {
  1364. return NULL;
  1365. }
  1366. if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
  1367. return virtqueue_packed_pop(vq, sz);
  1368. } else {
  1369. return virtqueue_split_pop(vq, sz);
  1370. }
  1371. }
  1372. static unsigned int virtqueue_packed_drop_all(VirtQueue *vq)
  1373. {
  1374. VRingMemoryRegionCaches *caches;
  1375. MemoryRegionCache *desc_cache;
  1376. unsigned int dropped = 0;
  1377. VirtQueueElement elem = {};
  1378. VirtIODevice *vdev = vq->vdev;
  1379. VRingPackedDesc desc;
  1380. caches = vring_get_region_caches(vq);
  1381. desc_cache = &caches->desc;
  1382. virtio_queue_set_notification(vq, 0);
  1383. while (vq->inuse < vq->vring.num) {
  1384. unsigned int idx = vq->last_avail_idx;
  1385. /*
  1386. * works similar to virtqueue_pop but does not map buffers
  1387. * and does not allocate any memory.
  1388. */
  1389. vring_packed_desc_read(vdev, &desc, desc_cache,
  1390. vq->last_avail_idx , true);
  1391. if (!is_desc_avail(desc.flags, vq->last_avail_wrap_counter)) {
  1392. break;
  1393. }
  1394. elem.index = desc.id;
  1395. elem.ndescs = 1;
  1396. while (virtqueue_packed_read_next_desc(vq, &desc, desc_cache,
  1397. vq->vring.num, &idx, false)) {
  1398. ++elem.ndescs;
  1399. }
  1400. /*
  1401. * immediately push the element, nothing to unmap
  1402. * as both in_num and out_num are set to 0.
  1403. */
  1404. virtqueue_push(vq, &elem, 0);
  1405. dropped++;
  1406. vq->last_avail_idx += elem.ndescs;
  1407. if (vq->last_avail_idx >= vq->vring.num) {
  1408. vq->last_avail_idx -= vq->vring.num;
  1409. vq->last_avail_wrap_counter ^= 1;
  1410. }
  1411. }
  1412. return dropped;
  1413. }
  1414. static unsigned int virtqueue_split_drop_all(VirtQueue *vq)
  1415. {
  1416. unsigned int dropped = 0;
  1417. VirtQueueElement elem = {};
  1418. VirtIODevice *vdev = vq->vdev;
  1419. bool fEventIdx = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
  1420. while (!virtio_queue_empty(vq) && vq->inuse < vq->vring.num) {
  1421. /* works similar to virtqueue_pop but does not map buffers
  1422. * and does not allocate any memory */
  1423. smp_rmb();
  1424. if (!virtqueue_get_head(vq, vq->last_avail_idx, &elem.index)) {
  1425. break;
  1426. }
  1427. vq->inuse++;
  1428. vq->last_avail_idx++;
  1429. if (fEventIdx) {
  1430. vring_set_avail_event(vq, vq->last_avail_idx);
  1431. }
  1432. /* immediately push the element, nothing to unmap
  1433. * as both in_num and out_num are set to 0 */
  1434. virtqueue_push(vq, &elem, 0);
  1435. dropped++;
  1436. }
  1437. return dropped;
  1438. }
  1439. /* virtqueue_drop_all:
  1440. * @vq: The #VirtQueue
  1441. * Drops all queued buffers and indicates them to the guest
  1442. * as if they are done. Useful when buffers can not be
  1443. * processed but must be returned to the guest.
  1444. */
  1445. unsigned int virtqueue_drop_all(VirtQueue *vq)
  1446. {
  1447. struct VirtIODevice *vdev = vq->vdev;
  1448. if (unlikely(vdev->broken)) {
  1449. return 0;
  1450. }
  1451. if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  1452. return virtqueue_packed_drop_all(vq);
  1453. } else {
  1454. return virtqueue_split_drop_all(vq);
  1455. }
  1456. }
  1457. /* Reading and writing a structure directly to QEMUFile is *awful*, but
  1458. * it is what QEMU has always done by mistake. We can change it sooner
  1459. * or later by bumping the version number of the affected vm states.
  1460. * In the meanwhile, since the in-memory layout of VirtQueueElement
  1461. * has changed, we need to marshal to and from the layout that was
  1462. * used before the change.
  1463. */
  1464. typedef struct VirtQueueElementOld {
  1465. unsigned int index;
  1466. unsigned int out_num;
  1467. unsigned int in_num;
  1468. hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
  1469. hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
  1470. struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
  1471. struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
  1472. } VirtQueueElementOld;
  1473. void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)
  1474. {
  1475. VirtQueueElement *elem;
  1476. VirtQueueElementOld data;
  1477. int i;
  1478. qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
  1479. /* TODO: teach all callers that this can fail, and return failure instead
  1480. * of asserting here.
  1481. * This is just one thing (there are probably more) that must be
  1482. * fixed before we can allow NDEBUG compilation.
  1483. */
  1484. assert(ARRAY_SIZE(data.in_addr) >= data.in_num);
  1485. assert(ARRAY_SIZE(data.out_addr) >= data.out_num);
  1486. elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
  1487. elem->index = data.index;
  1488. for (i = 0; i < elem->in_num; i++) {
  1489. elem->in_addr[i] = data.in_addr[i];
  1490. }
  1491. for (i = 0; i < elem->out_num; i++) {
  1492. elem->out_addr[i] = data.out_addr[i];
  1493. }
  1494. for (i = 0; i < elem->in_num; i++) {
  1495. /* Base is overwritten by virtqueue_map. */
  1496. elem->in_sg[i].iov_base = 0;
  1497. elem->in_sg[i].iov_len = data.in_sg[i].iov_len;
  1498. }
  1499. for (i = 0; i < elem->out_num; i++) {
  1500. /* Base is overwritten by virtqueue_map. */
  1501. elem->out_sg[i].iov_base = 0;
  1502. elem->out_sg[i].iov_len = data.out_sg[i].iov_len;
  1503. }
  1504. if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  1505. qemu_get_be32s(f, &elem->ndescs);
  1506. }
  1507. virtqueue_map(vdev, elem);
  1508. return elem;
  1509. }
  1510. void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f,
  1511. VirtQueueElement *elem)
  1512. {
  1513. VirtQueueElementOld data;
  1514. int i;
  1515. memset(&data, 0, sizeof(data));
  1516. data.index = elem->index;
  1517. data.in_num = elem->in_num;
  1518. data.out_num = elem->out_num;
  1519. for (i = 0; i < elem->in_num; i++) {
  1520. data.in_addr[i] = elem->in_addr[i];
  1521. }
  1522. for (i = 0; i < elem->out_num; i++) {
  1523. data.out_addr[i] = elem->out_addr[i];
  1524. }
  1525. for (i = 0; i < elem->in_num; i++) {
  1526. /* Base is overwritten by virtqueue_map when loading. Do not
  1527. * save it, as it would leak the QEMU address space layout. */
  1528. data.in_sg[i].iov_len = elem->in_sg[i].iov_len;
  1529. }
  1530. for (i = 0; i < elem->out_num; i++) {
  1531. /* Do not save iov_base as above. */
  1532. data.out_sg[i].iov_len = elem->out_sg[i].iov_len;
  1533. }
  1534. if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  1535. qemu_put_be32s(f, &elem->ndescs);
  1536. }
  1537. qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
  1538. }
  1539. /* virtio device */
  1540. static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
  1541. {
  1542. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  1543. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  1544. if (unlikely(vdev->broken)) {
  1545. return;
  1546. }
  1547. if (k->notify) {
  1548. k->notify(qbus->parent, vector);
  1549. }
  1550. }
  1551. void virtio_update_irq(VirtIODevice *vdev)
  1552. {
  1553. virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
  1554. }
  1555. static int virtio_validate_features(VirtIODevice *vdev)
  1556. {
  1557. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1558. if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM) &&
  1559. !virtio_vdev_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
  1560. return -EFAULT;
  1561. }
  1562. if (k->validate_features) {
  1563. return k->validate_features(vdev);
  1564. } else {
  1565. return 0;
  1566. }
  1567. }
  1568. int virtio_set_status(VirtIODevice *vdev, uint8_t val)
  1569. {
  1570. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1571. trace_virtio_set_status(vdev, val);
  1572. if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  1573. if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) &&
  1574. val & VIRTIO_CONFIG_S_FEATURES_OK) {
  1575. int ret = virtio_validate_features(vdev);
  1576. if (ret) {
  1577. return ret;
  1578. }
  1579. }
  1580. }
  1581. if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) !=
  1582. (val & VIRTIO_CONFIG_S_DRIVER_OK)) {
  1583. virtio_set_started(vdev, val & VIRTIO_CONFIG_S_DRIVER_OK);
  1584. }
  1585. if (k->set_status) {
  1586. k->set_status(vdev, val);
  1587. }
  1588. vdev->status = val;
  1589. return 0;
  1590. }
  1591. static enum virtio_device_endian virtio_default_endian(void)
  1592. {
  1593. if (target_words_bigendian()) {
  1594. return VIRTIO_DEVICE_ENDIAN_BIG;
  1595. } else {
  1596. return VIRTIO_DEVICE_ENDIAN_LITTLE;
  1597. }
  1598. }
  1599. static enum virtio_device_endian virtio_current_cpu_endian(void)
  1600. {
  1601. CPUClass *cc = CPU_GET_CLASS(current_cpu);
  1602. if (cc->virtio_is_big_endian(current_cpu)) {
  1603. return VIRTIO_DEVICE_ENDIAN_BIG;
  1604. } else {
  1605. return VIRTIO_DEVICE_ENDIAN_LITTLE;
  1606. }
  1607. }
  1608. void virtio_reset(void *opaque)
  1609. {
  1610. VirtIODevice *vdev = opaque;
  1611. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1612. int i;
  1613. virtio_set_status(vdev, 0);
  1614. if (current_cpu) {
  1615. /* Guest initiated reset */
  1616. vdev->device_endian = virtio_current_cpu_endian();
  1617. } else {
  1618. /* System reset */
  1619. vdev->device_endian = virtio_default_endian();
  1620. }
  1621. if (k->reset) {
  1622. k->reset(vdev);
  1623. }
  1624. vdev->start_on_kick = false;
  1625. vdev->started = false;
  1626. vdev->broken = false;
  1627. vdev->guest_features = 0;
  1628. vdev->queue_sel = 0;
  1629. vdev->status = 0;
  1630. atomic_set(&vdev->isr, 0);
  1631. vdev->config_vector = VIRTIO_NO_VECTOR;
  1632. virtio_notify_vector(vdev, vdev->config_vector);
  1633. for(i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  1634. vdev->vq[i].vring.desc = 0;
  1635. vdev->vq[i].vring.avail = 0;
  1636. vdev->vq[i].vring.used = 0;
  1637. vdev->vq[i].last_avail_idx = 0;
  1638. vdev->vq[i].shadow_avail_idx = 0;
  1639. vdev->vq[i].used_idx = 0;
  1640. vdev->vq[i].last_avail_wrap_counter = true;
  1641. vdev->vq[i].shadow_avail_wrap_counter = true;
  1642. vdev->vq[i].used_wrap_counter = true;
  1643. virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR);
  1644. vdev->vq[i].signalled_used = 0;
  1645. vdev->vq[i].signalled_used_valid = false;
  1646. vdev->vq[i].notification = true;
  1647. vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
  1648. vdev->vq[i].inuse = 0;
  1649. virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
  1650. }
  1651. }
  1652. uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
  1653. {
  1654. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1655. uint8_t val;
  1656. if (addr + sizeof(val) > vdev->config_len) {
  1657. return (uint32_t)-1;
  1658. }
  1659. k->get_config(vdev, vdev->config);
  1660. val = ldub_p(vdev->config + addr);
  1661. return val;
  1662. }
  1663. uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
  1664. {
  1665. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1666. uint16_t val;
  1667. if (addr + sizeof(val) > vdev->config_len) {
  1668. return (uint32_t)-1;
  1669. }
  1670. k->get_config(vdev, vdev->config);
  1671. val = lduw_p(vdev->config + addr);
  1672. return val;
  1673. }
  1674. uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
  1675. {
  1676. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1677. uint32_t val;
  1678. if (addr + sizeof(val) > vdev->config_len) {
  1679. return (uint32_t)-1;
  1680. }
  1681. k->get_config(vdev, vdev->config);
  1682. val = ldl_p(vdev->config + addr);
  1683. return val;
  1684. }
  1685. void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
  1686. {
  1687. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1688. uint8_t val = data;
  1689. if (addr + sizeof(val) > vdev->config_len) {
  1690. return;
  1691. }
  1692. stb_p(vdev->config + addr, val);
  1693. if (k->set_config) {
  1694. k->set_config(vdev, vdev->config);
  1695. }
  1696. }
  1697. void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
  1698. {
  1699. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1700. uint16_t val = data;
  1701. if (addr + sizeof(val) > vdev->config_len) {
  1702. return;
  1703. }
  1704. stw_p(vdev->config + addr, val);
  1705. if (k->set_config) {
  1706. k->set_config(vdev, vdev->config);
  1707. }
  1708. }
  1709. void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
  1710. {
  1711. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1712. uint32_t val = data;
  1713. if (addr + sizeof(val) > vdev->config_len) {
  1714. return;
  1715. }
  1716. stl_p(vdev->config + addr, val);
  1717. if (k->set_config) {
  1718. k->set_config(vdev, vdev->config);
  1719. }
  1720. }
  1721. uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr)
  1722. {
  1723. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1724. uint8_t val;
  1725. if (addr + sizeof(val) > vdev->config_len) {
  1726. return (uint32_t)-1;
  1727. }
  1728. k->get_config(vdev, vdev->config);
  1729. val = ldub_p(vdev->config + addr);
  1730. return val;
  1731. }
  1732. uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr)
  1733. {
  1734. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1735. uint16_t val;
  1736. if (addr + sizeof(val) > vdev->config_len) {
  1737. return (uint32_t)-1;
  1738. }
  1739. k->get_config(vdev, vdev->config);
  1740. val = lduw_le_p(vdev->config + addr);
  1741. return val;
  1742. }
  1743. uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr)
  1744. {
  1745. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1746. uint32_t val;
  1747. if (addr + sizeof(val) > vdev->config_len) {
  1748. return (uint32_t)-1;
  1749. }
  1750. k->get_config(vdev, vdev->config);
  1751. val = ldl_le_p(vdev->config + addr);
  1752. return val;
  1753. }
  1754. void virtio_config_modern_writeb(VirtIODevice *vdev,
  1755. uint32_t addr, uint32_t data)
  1756. {
  1757. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1758. uint8_t val = data;
  1759. if (addr + sizeof(val) > vdev->config_len) {
  1760. return;
  1761. }
  1762. stb_p(vdev->config + addr, val);
  1763. if (k->set_config) {
  1764. k->set_config(vdev, vdev->config);
  1765. }
  1766. }
  1767. void virtio_config_modern_writew(VirtIODevice *vdev,
  1768. uint32_t addr, uint32_t data)
  1769. {
  1770. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1771. uint16_t val = data;
  1772. if (addr + sizeof(val) > vdev->config_len) {
  1773. return;
  1774. }
  1775. stw_le_p(vdev->config + addr, val);
  1776. if (k->set_config) {
  1777. k->set_config(vdev, vdev->config);
  1778. }
  1779. }
  1780. void virtio_config_modern_writel(VirtIODevice *vdev,
  1781. uint32_t addr, uint32_t data)
  1782. {
  1783. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  1784. uint32_t val = data;
  1785. if (addr + sizeof(val) > vdev->config_len) {
  1786. return;
  1787. }
  1788. stl_le_p(vdev->config + addr, val);
  1789. if (k->set_config) {
  1790. k->set_config(vdev, vdev->config);
  1791. }
  1792. }
  1793. void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
  1794. {
  1795. if (!vdev->vq[n].vring.num) {
  1796. return;
  1797. }
  1798. vdev->vq[n].vring.desc = addr;
  1799. virtio_queue_update_rings(vdev, n);
  1800. }
  1801. hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
  1802. {
  1803. return vdev->vq[n].vring.desc;
  1804. }
  1805. void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
  1806. hwaddr avail, hwaddr used)
  1807. {
  1808. if (!vdev->vq[n].vring.num) {
  1809. return;
  1810. }
  1811. vdev->vq[n].vring.desc = desc;
  1812. vdev->vq[n].vring.avail = avail;
  1813. vdev->vq[n].vring.used = used;
  1814. virtio_init_region_cache(vdev, n);
  1815. }
  1816. void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
  1817. {
  1818. /* Don't allow guest to flip queue between existent and
  1819. * nonexistent states, or to set it to an invalid size.
  1820. */
  1821. if (!!num != !!vdev->vq[n].vring.num ||
  1822. num > VIRTQUEUE_MAX_SIZE ||
  1823. num < 0) {
  1824. return;
  1825. }
  1826. vdev->vq[n].vring.num = num;
  1827. }
  1828. VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector)
  1829. {
  1830. return QLIST_FIRST(&vdev->vector_queues[vector]);
  1831. }
  1832. VirtQueue *virtio_vector_next_queue(VirtQueue *vq)
  1833. {
  1834. return QLIST_NEXT(vq, node);
  1835. }
  1836. int virtio_queue_get_num(VirtIODevice *vdev, int n)
  1837. {
  1838. return vdev->vq[n].vring.num;
  1839. }
  1840. int virtio_queue_get_max_num(VirtIODevice *vdev, int n)
  1841. {
  1842. return vdev->vq[n].vring.num_default;
  1843. }
  1844. int virtio_get_num_queues(VirtIODevice *vdev)
  1845. {
  1846. int i;
  1847. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  1848. if (!virtio_queue_get_num(vdev, i)) {
  1849. break;
  1850. }
  1851. }
  1852. return i;
  1853. }
  1854. void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
  1855. {
  1856. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  1857. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  1858. /* virtio-1 compliant devices cannot change the alignment */
  1859. if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  1860. error_report("tried to modify queue alignment for virtio-1 device");
  1861. return;
  1862. }
  1863. /* Check that the transport told us it was going to do this
  1864. * (so a buggy transport will immediately assert rather than
  1865. * silently failing to migrate this state)
  1866. */
  1867. assert(k->has_variable_vring_alignment);
  1868. if (align) {
  1869. vdev->vq[n].vring.align = align;
  1870. virtio_queue_update_rings(vdev, n);
  1871. }
  1872. }
  1873. static bool virtio_queue_notify_aio_vq(VirtQueue *vq)
  1874. {
  1875. bool ret = false;
  1876. if (vq->vring.desc && vq->handle_aio_output) {
  1877. VirtIODevice *vdev = vq->vdev;
  1878. trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
  1879. ret = vq->handle_aio_output(vdev, vq);
  1880. if (unlikely(vdev->start_on_kick)) {
  1881. virtio_set_started(vdev, true);
  1882. }
  1883. }
  1884. return ret;
  1885. }
  1886. static void virtio_queue_notify_vq(VirtQueue *vq)
  1887. {
  1888. if (vq->vring.desc && vq->handle_output) {
  1889. VirtIODevice *vdev = vq->vdev;
  1890. if (unlikely(vdev->broken)) {
  1891. return;
  1892. }
  1893. trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
  1894. vq->handle_output(vdev, vq);
  1895. if (unlikely(vdev->start_on_kick)) {
  1896. virtio_set_started(vdev, true);
  1897. }
  1898. }
  1899. }
  1900. void virtio_queue_notify(VirtIODevice *vdev, int n)
  1901. {
  1902. VirtQueue *vq = &vdev->vq[n];
  1903. if (unlikely(!vq->vring.desc || vdev->broken)) {
  1904. return;
  1905. }
  1906. trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
  1907. if (vq->host_notifier_enabled) {
  1908. event_notifier_set(&vq->host_notifier);
  1909. } else if (vq->handle_output) {
  1910. vq->handle_output(vdev, vq);
  1911. if (unlikely(vdev->start_on_kick)) {
  1912. virtio_set_started(vdev, true);
  1913. }
  1914. }
  1915. }
  1916. uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
  1917. {
  1918. return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector :
  1919. VIRTIO_NO_VECTOR;
  1920. }
  1921. void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
  1922. {
  1923. VirtQueue *vq = &vdev->vq[n];
  1924. if (n < VIRTIO_QUEUE_MAX) {
  1925. if (vdev->vector_queues &&
  1926. vdev->vq[n].vector != VIRTIO_NO_VECTOR) {
  1927. QLIST_REMOVE(vq, node);
  1928. }
  1929. vdev->vq[n].vector = vector;
  1930. if (vdev->vector_queues &&
  1931. vector != VIRTIO_NO_VECTOR) {
  1932. QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node);
  1933. }
  1934. }
  1935. }
  1936. VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
  1937. VirtIOHandleOutput handle_output)
  1938. {
  1939. int i;
  1940. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  1941. if (vdev->vq[i].vring.num == 0)
  1942. break;
  1943. }
  1944. if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
  1945. abort();
  1946. vdev->vq[i].vring.num = queue_size;
  1947. vdev->vq[i].vring.num_default = queue_size;
  1948. vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
  1949. vdev->vq[i].handle_output = handle_output;
  1950. vdev->vq[i].handle_aio_output = NULL;
  1951. vdev->vq[i].used_elems = g_malloc0(sizeof(VirtQueueElement) *
  1952. queue_size);
  1953. return &vdev->vq[i];
  1954. }
  1955. void virtio_del_queue(VirtIODevice *vdev, int n)
  1956. {
  1957. if (n < 0 || n >= VIRTIO_QUEUE_MAX) {
  1958. abort();
  1959. }
  1960. vdev->vq[n].vring.num = 0;
  1961. vdev->vq[n].vring.num_default = 0;
  1962. vdev->vq[n].handle_output = NULL;
  1963. vdev->vq[n].handle_aio_output = NULL;
  1964. g_free(vdev->vq[n].used_elems);
  1965. }
  1966. static void virtio_set_isr(VirtIODevice *vdev, int value)
  1967. {
  1968. uint8_t old = atomic_read(&vdev->isr);
  1969. /* Do not write ISR if it does not change, so that its cacheline remains
  1970. * shared in the common case where the guest does not read it.
  1971. */
  1972. if ((old & value) != value) {
  1973. atomic_or(&vdev->isr, value);
  1974. }
  1975. }
  1976. static bool virtio_split_should_notify(VirtIODevice *vdev, VirtQueue *vq)
  1977. {
  1978. uint16_t old, new;
  1979. bool v;
  1980. /* We need to expose used array entries before checking used event. */
  1981. smp_mb();
  1982. /* Always notify when queue is empty (when feature acknowledge) */
  1983. if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
  1984. !vq->inuse && virtio_queue_empty(vq)) {
  1985. return true;
  1986. }
  1987. if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
  1988. return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
  1989. }
  1990. v = vq->signalled_used_valid;
  1991. vq->signalled_used_valid = true;
  1992. old = vq->signalled_used;
  1993. new = vq->signalled_used = vq->used_idx;
  1994. return !v || vring_need_event(vring_get_used_event(vq), new, old);
  1995. }
  1996. static bool vring_packed_need_event(VirtQueue *vq, bool wrap,
  1997. uint16_t off_wrap, uint16_t new,
  1998. uint16_t old)
  1999. {
  2000. int off = off_wrap & ~(1 << 15);
  2001. if (wrap != off_wrap >> 15) {
  2002. off -= vq->vring.num;
  2003. }
  2004. return vring_need_event(off, new, old);
  2005. }
  2006. static bool virtio_packed_should_notify(VirtIODevice *vdev, VirtQueue *vq)
  2007. {
  2008. VRingPackedDescEvent e;
  2009. uint16_t old, new;
  2010. bool v;
  2011. VRingMemoryRegionCaches *caches;
  2012. caches = vring_get_region_caches(vq);
  2013. vring_packed_event_read(vdev, &caches->avail, &e);
  2014. old = vq->signalled_used;
  2015. new = vq->signalled_used = vq->used_idx;
  2016. v = vq->signalled_used_valid;
  2017. vq->signalled_used_valid = true;
  2018. if (e.flags == VRING_PACKED_EVENT_FLAG_DISABLE) {
  2019. return false;
  2020. } else if (e.flags == VRING_PACKED_EVENT_FLAG_ENABLE) {
  2021. return true;
  2022. }
  2023. return !v || vring_packed_need_event(vq, vq->used_wrap_counter,
  2024. e.off_wrap, new, old);
  2025. }
  2026. /* Called within rcu_read_lock(). */
  2027. static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
  2028. {
  2029. if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  2030. return virtio_packed_should_notify(vdev, vq);
  2031. } else {
  2032. return virtio_split_should_notify(vdev, vq);
  2033. }
  2034. }
  2035. void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)
  2036. {
  2037. WITH_RCU_READ_LOCK_GUARD() {
  2038. if (!virtio_should_notify(vdev, vq)) {
  2039. return;
  2040. }
  2041. }
  2042. trace_virtio_notify_irqfd(vdev, vq);
  2043. /*
  2044. * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
  2045. * windows drivers included in virtio-win 1.8.0 (circa 2015) are
  2046. * incorrectly polling this bit during crashdump and hibernation
  2047. * in MSI mode, causing a hang if this bit is never updated.
  2048. * Recent releases of Windows do not really shut down, but rather
  2049. * log out and hibernate to make the next startup faster. Hence,
  2050. * this manifested as a more serious hang during shutdown with
  2051. *
  2052. * Next driver release from 2016 fixed this problem, so working around it
  2053. * is not a must, but it's easy to do so let's do it here.
  2054. *
  2055. * Note: it's safe to update ISR from any thread as it was switched
  2056. * to an atomic operation.
  2057. */
  2058. virtio_set_isr(vq->vdev, 0x1);
  2059. event_notifier_set(&vq->guest_notifier);
  2060. }
  2061. static void virtio_irq(VirtQueue *vq)
  2062. {
  2063. virtio_set_isr(vq->vdev, 0x1);
  2064. virtio_notify_vector(vq->vdev, vq->vector);
  2065. }
  2066. void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
  2067. {
  2068. WITH_RCU_READ_LOCK_GUARD() {
  2069. if (!virtio_should_notify(vdev, vq)) {
  2070. return;
  2071. }
  2072. }
  2073. trace_virtio_notify(vdev, vq);
  2074. virtio_irq(vq);
  2075. }
  2076. void virtio_notify_config(VirtIODevice *vdev)
  2077. {
  2078. if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
  2079. return;
  2080. virtio_set_isr(vdev, 0x3);
  2081. vdev->generation++;
  2082. virtio_notify_vector(vdev, vdev->config_vector);
  2083. }
  2084. static bool virtio_device_endian_needed(void *opaque)
  2085. {
  2086. VirtIODevice *vdev = opaque;
  2087. assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
  2088. if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  2089. return vdev->device_endian != virtio_default_endian();
  2090. }
  2091. /* Devices conforming to VIRTIO 1.0 or later are always LE. */
  2092. return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE;
  2093. }
  2094. static bool virtio_64bit_features_needed(void *opaque)
  2095. {
  2096. VirtIODevice *vdev = opaque;
  2097. return (vdev->host_features >> 32) != 0;
  2098. }
  2099. static bool virtio_virtqueue_needed(void *opaque)
  2100. {
  2101. VirtIODevice *vdev = opaque;
  2102. return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1);
  2103. }
  2104. static bool virtio_packed_virtqueue_needed(void *opaque)
  2105. {
  2106. VirtIODevice *vdev = opaque;
  2107. return virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED);
  2108. }
  2109. static bool virtio_ringsize_needed(void *opaque)
  2110. {
  2111. VirtIODevice *vdev = opaque;
  2112. int i;
  2113. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  2114. if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) {
  2115. return true;
  2116. }
  2117. }
  2118. return false;
  2119. }
  2120. static bool virtio_extra_state_needed(void *opaque)
  2121. {
  2122. VirtIODevice *vdev = opaque;
  2123. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  2124. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  2125. return k->has_extra_state &&
  2126. k->has_extra_state(qbus->parent);
  2127. }
  2128. static bool virtio_broken_needed(void *opaque)
  2129. {
  2130. VirtIODevice *vdev = opaque;
  2131. return vdev->broken;
  2132. }
  2133. static bool virtio_started_needed(void *opaque)
  2134. {
  2135. VirtIODevice *vdev = opaque;
  2136. return vdev->started;
  2137. }
  2138. static const VMStateDescription vmstate_virtqueue = {
  2139. .name = "virtqueue_state",
  2140. .version_id = 1,
  2141. .minimum_version_id = 1,
  2142. .fields = (VMStateField[]) {
  2143. VMSTATE_UINT64(vring.avail, struct VirtQueue),
  2144. VMSTATE_UINT64(vring.used, struct VirtQueue),
  2145. VMSTATE_END_OF_LIST()
  2146. }
  2147. };
  2148. static const VMStateDescription vmstate_packed_virtqueue = {
  2149. .name = "packed_virtqueue_state",
  2150. .version_id = 1,
  2151. .minimum_version_id = 1,
  2152. .fields = (VMStateField[]) {
  2153. VMSTATE_UINT16(last_avail_idx, struct VirtQueue),
  2154. VMSTATE_BOOL(last_avail_wrap_counter, struct VirtQueue),
  2155. VMSTATE_UINT16(used_idx, struct VirtQueue),
  2156. VMSTATE_BOOL(used_wrap_counter, struct VirtQueue),
  2157. VMSTATE_UINT32(inuse, struct VirtQueue),
  2158. VMSTATE_END_OF_LIST()
  2159. }
  2160. };
  2161. static const VMStateDescription vmstate_virtio_virtqueues = {
  2162. .name = "virtio/virtqueues",
  2163. .version_id = 1,
  2164. .minimum_version_id = 1,
  2165. .needed = &virtio_virtqueue_needed,
  2166. .fields = (VMStateField[]) {
  2167. VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
  2168. VIRTIO_QUEUE_MAX, 0, vmstate_virtqueue, VirtQueue),
  2169. VMSTATE_END_OF_LIST()
  2170. }
  2171. };
  2172. static const VMStateDescription vmstate_virtio_packed_virtqueues = {
  2173. .name = "virtio/packed_virtqueues",
  2174. .version_id = 1,
  2175. .minimum_version_id = 1,
  2176. .needed = &virtio_packed_virtqueue_needed,
  2177. .fields = (VMStateField[]) {
  2178. VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
  2179. VIRTIO_QUEUE_MAX, 0, vmstate_packed_virtqueue, VirtQueue),
  2180. VMSTATE_END_OF_LIST()
  2181. }
  2182. };
  2183. static const VMStateDescription vmstate_ringsize = {
  2184. .name = "ringsize_state",
  2185. .version_id = 1,
  2186. .minimum_version_id = 1,
  2187. .fields = (VMStateField[]) {
  2188. VMSTATE_UINT32(vring.num_default, struct VirtQueue),
  2189. VMSTATE_END_OF_LIST()
  2190. }
  2191. };
  2192. static const VMStateDescription vmstate_virtio_ringsize = {
  2193. .name = "virtio/ringsize",
  2194. .version_id = 1,
  2195. .minimum_version_id = 1,
  2196. .needed = &virtio_ringsize_needed,
  2197. .fields = (VMStateField[]) {
  2198. VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
  2199. VIRTIO_QUEUE_MAX, 0, vmstate_ringsize, VirtQueue),
  2200. VMSTATE_END_OF_LIST()
  2201. }
  2202. };
  2203. static int get_extra_state(QEMUFile *f, void *pv, size_t size,
  2204. const VMStateField *field)
  2205. {
  2206. VirtIODevice *vdev = pv;
  2207. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  2208. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  2209. if (!k->load_extra_state) {
  2210. return -1;
  2211. } else {
  2212. return k->load_extra_state(qbus->parent, f);
  2213. }
  2214. }
  2215. static int put_extra_state(QEMUFile *f, void *pv, size_t size,
  2216. const VMStateField *field, QJSON *vmdesc)
  2217. {
  2218. VirtIODevice *vdev = pv;
  2219. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  2220. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  2221. k->save_extra_state(qbus->parent, f);
  2222. return 0;
  2223. }
  2224. static const VMStateInfo vmstate_info_extra_state = {
  2225. .name = "virtqueue_extra_state",
  2226. .get = get_extra_state,
  2227. .put = put_extra_state,
  2228. };
  2229. static const VMStateDescription vmstate_virtio_extra_state = {
  2230. .name = "virtio/extra_state",
  2231. .version_id = 1,
  2232. .minimum_version_id = 1,
  2233. .needed = &virtio_extra_state_needed,
  2234. .fields = (VMStateField[]) {
  2235. {
  2236. .name = "extra_state",
  2237. .version_id = 0,
  2238. .field_exists = NULL,
  2239. .size = 0,
  2240. .info = &vmstate_info_extra_state,
  2241. .flags = VMS_SINGLE,
  2242. .offset = 0,
  2243. },
  2244. VMSTATE_END_OF_LIST()
  2245. }
  2246. };
  2247. static const VMStateDescription vmstate_virtio_device_endian = {
  2248. .name = "virtio/device_endian",
  2249. .version_id = 1,
  2250. .minimum_version_id = 1,
  2251. .needed = &virtio_device_endian_needed,
  2252. .fields = (VMStateField[]) {
  2253. VMSTATE_UINT8(device_endian, VirtIODevice),
  2254. VMSTATE_END_OF_LIST()
  2255. }
  2256. };
  2257. static const VMStateDescription vmstate_virtio_64bit_features = {
  2258. .name = "virtio/64bit_features",
  2259. .version_id = 1,
  2260. .minimum_version_id = 1,
  2261. .needed = &virtio_64bit_features_needed,
  2262. .fields = (VMStateField[]) {
  2263. VMSTATE_UINT64(guest_features, VirtIODevice),
  2264. VMSTATE_END_OF_LIST()
  2265. }
  2266. };
  2267. static const VMStateDescription vmstate_virtio_broken = {
  2268. .name = "virtio/broken",
  2269. .version_id = 1,
  2270. .minimum_version_id = 1,
  2271. .needed = &virtio_broken_needed,
  2272. .fields = (VMStateField[]) {
  2273. VMSTATE_BOOL(broken, VirtIODevice),
  2274. VMSTATE_END_OF_LIST()
  2275. }
  2276. };
  2277. static const VMStateDescription vmstate_virtio_started = {
  2278. .name = "virtio/started",
  2279. .version_id = 1,
  2280. .minimum_version_id = 1,
  2281. .needed = &virtio_started_needed,
  2282. .fields = (VMStateField[]) {
  2283. VMSTATE_BOOL(started, VirtIODevice),
  2284. VMSTATE_END_OF_LIST()
  2285. }
  2286. };
  2287. static const VMStateDescription vmstate_virtio = {
  2288. .name = "virtio",
  2289. .version_id = 1,
  2290. .minimum_version_id = 1,
  2291. .minimum_version_id_old = 1,
  2292. .fields = (VMStateField[]) {
  2293. VMSTATE_END_OF_LIST()
  2294. },
  2295. .subsections = (const VMStateDescription*[]) {
  2296. &vmstate_virtio_device_endian,
  2297. &vmstate_virtio_64bit_features,
  2298. &vmstate_virtio_virtqueues,
  2299. &vmstate_virtio_ringsize,
  2300. &vmstate_virtio_broken,
  2301. &vmstate_virtio_extra_state,
  2302. &vmstate_virtio_started,
  2303. &vmstate_virtio_packed_virtqueues,
  2304. NULL
  2305. }
  2306. };
  2307. int virtio_save(VirtIODevice *vdev, QEMUFile *f)
  2308. {
  2309. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  2310. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  2311. VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
  2312. uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff);
  2313. int i;
  2314. if (k->save_config) {
  2315. k->save_config(qbus->parent, f);
  2316. }
  2317. qemu_put_8s(f, &vdev->status);
  2318. qemu_put_8s(f, &vdev->isr);
  2319. qemu_put_be16s(f, &vdev->queue_sel);
  2320. qemu_put_be32s(f, &guest_features_lo);
  2321. qemu_put_be32(f, vdev->config_len);
  2322. qemu_put_buffer(f, vdev->config, vdev->config_len);
  2323. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  2324. if (vdev->vq[i].vring.num == 0)
  2325. break;
  2326. }
  2327. qemu_put_be32(f, i);
  2328. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  2329. if (vdev->vq[i].vring.num == 0)
  2330. break;
  2331. qemu_put_be32(f, vdev->vq[i].vring.num);
  2332. if (k->has_variable_vring_alignment) {
  2333. qemu_put_be32(f, vdev->vq[i].vring.align);
  2334. }
  2335. /*
  2336. * Save desc now, the rest of the ring addresses are saved in
  2337. * subsections for VIRTIO-1 devices.
  2338. */
  2339. qemu_put_be64(f, vdev->vq[i].vring.desc);
  2340. qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
  2341. if (k->save_queue) {
  2342. k->save_queue(qbus->parent, i, f);
  2343. }
  2344. }
  2345. if (vdc->save != NULL) {
  2346. vdc->save(vdev, f);
  2347. }
  2348. if (vdc->vmsd) {
  2349. int ret = vmstate_save_state(f, vdc->vmsd, vdev, NULL);
  2350. if (ret) {
  2351. return ret;
  2352. }
  2353. }
  2354. /* Subsections */
  2355. return vmstate_save_state(f, &vmstate_virtio, vdev, NULL);
  2356. }
  2357. /* A wrapper for use as a VMState .put function */
  2358. static int virtio_device_put(QEMUFile *f, void *opaque, size_t size,
  2359. const VMStateField *field, QJSON *vmdesc)
  2360. {
  2361. return virtio_save(VIRTIO_DEVICE(opaque), f);
  2362. }
  2363. /* A wrapper for use as a VMState .get function */
  2364. static int virtio_device_get(QEMUFile *f, void *opaque, size_t size,
  2365. const VMStateField *field)
  2366. {
  2367. VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
  2368. DeviceClass *dc = DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev));
  2369. return virtio_load(vdev, f, dc->vmsd->version_id);
  2370. }
  2371. const VMStateInfo virtio_vmstate_info = {
  2372. .name = "virtio",
  2373. .get = virtio_device_get,
  2374. .put = virtio_device_put,
  2375. };
  2376. static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val)
  2377. {
  2378. VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
  2379. bool bad = (val & ~(vdev->host_features)) != 0;
  2380. val &= vdev->host_features;
  2381. if (k->set_features) {
  2382. k->set_features(vdev, val);
  2383. }
  2384. vdev->guest_features = val;
  2385. return bad ? -1 : 0;
  2386. }
  2387. int virtio_set_features(VirtIODevice *vdev, uint64_t val)
  2388. {
  2389. int ret;
  2390. /*
  2391. * The driver must not attempt to set features after feature negotiation
  2392. * has finished.
  2393. */
  2394. if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) {
  2395. return -EINVAL;
  2396. }
  2397. ret = virtio_set_features_nocheck(vdev, val);
  2398. if (!ret) {
  2399. if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
  2400. /* VIRTIO_RING_F_EVENT_IDX changes the size of the caches. */
  2401. int i;
  2402. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  2403. if (vdev->vq[i].vring.num != 0) {
  2404. virtio_init_region_cache(vdev, i);
  2405. }
  2406. }
  2407. }
  2408. if (!virtio_device_started(vdev, vdev->status) &&
  2409. !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  2410. vdev->start_on_kick = true;
  2411. }
  2412. }
  2413. return ret;
  2414. }
  2415. size_t virtio_feature_get_config_size(VirtIOFeature *feature_sizes,
  2416. uint64_t host_features)
  2417. {
  2418. size_t config_size = 0;
  2419. int i;
  2420. for (i = 0; feature_sizes[i].flags != 0; i++) {
  2421. if (host_features & feature_sizes[i].flags) {
  2422. config_size = MAX(feature_sizes[i].end, config_size);
  2423. }
  2424. }
  2425. return config_size;
  2426. }
  2427. int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
  2428. {
  2429. int i, ret;
  2430. int32_t config_len;
  2431. uint32_t num;
  2432. uint32_t features;
  2433. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  2434. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  2435. VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
  2436. /*
  2437. * We poison the endianness to ensure it does not get used before
  2438. * subsections have been loaded.
  2439. */
  2440. vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN;
  2441. if (k->load_config) {
  2442. ret = k->load_config(qbus->parent, f);
  2443. if (ret)
  2444. return ret;
  2445. }
  2446. qemu_get_8s(f, &vdev->status);
  2447. qemu_get_8s(f, &vdev->isr);
  2448. qemu_get_be16s(f, &vdev->queue_sel);
  2449. if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) {
  2450. return -1;
  2451. }
  2452. qemu_get_be32s(f, &features);
  2453. /*
  2454. * Temporarily set guest_features low bits - needed by
  2455. * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
  2456. * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
  2457. *
  2458. * Note: devices should always test host features in future - don't create
  2459. * new dependencies like this.
  2460. */
  2461. vdev->guest_features = features;
  2462. config_len = qemu_get_be32(f);
  2463. /*
  2464. * There are cases where the incoming config can be bigger or smaller
  2465. * than what we have; so load what we have space for, and skip
  2466. * any excess that's in the stream.
  2467. */
  2468. qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len));
  2469. while (config_len > vdev->config_len) {
  2470. qemu_get_byte(f);
  2471. config_len--;
  2472. }
  2473. num = qemu_get_be32(f);
  2474. if (num > VIRTIO_QUEUE_MAX) {
  2475. error_report("Invalid number of virtqueues: 0x%x", num);
  2476. return -1;
  2477. }
  2478. for (i = 0; i < num; i++) {
  2479. vdev->vq[i].vring.num = qemu_get_be32(f);
  2480. if (k->has_variable_vring_alignment) {
  2481. vdev->vq[i].vring.align = qemu_get_be32(f);
  2482. }
  2483. vdev->vq[i].vring.desc = qemu_get_be64(f);
  2484. qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
  2485. vdev->vq[i].signalled_used_valid = false;
  2486. vdev->vq[i].notification = true;
  2487. if (!vdev->vq[i].vring.desc && vdev->vq[i].last_avail_idx) {
  2488. error_report("VQ %d address 0x0 "
  2489. "inconsistent with Host index 0x%x",
  2490. i, vdev->vq[i].last_avail_idx);
  2491. return -1;
  2492. }
  2493. if (k->load_queue) {
  2494. ret = k->load_queue(qbus->parent, i, f);
  2495. if (ret)
  2496. return ret;
  2497. }
  2498. }
  2499. virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
  2500. if (vdc->load != NULL) {
  2501. ret = vdc->load(vdev, f, version_id);
  2502. if (ret) {
  2503. return ret;
  2504. }
  2505. }
  2506. if (vdc->vmsd) {
  2507. ret = vmstate_load_state(f, vdc->vmsd, vdev, version_id);
  2508. if (ret) {
  2509. return ret;
  2510. }
  2511. }
  2512. /* Subsections */
  2513. ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1);
  2514. if (ret) {
  2515. return ret;
  2516. }
  2517. if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) {
  2518. vdev->device_endian = virtio_default_endian();
  2519. }
  2520. if (virtio_64bit_features_needed(vdev)) {
  2521. /*
  2522. * Subsection load filled vdev->guest_features. Run them
  2523. * through virtio_set_features to sanity-check them against
  2524. * host_features.
  2525. */
  2526. uint64_t features64 = vdev->guest_features;
  2527. if (virtio_set_features_nocheck(vdev, features64) < 0) {
  2528. error_report("Features 0x%" PRIx64 " unsupported. "
  2529. "Allowed features: 0x%" PRIx64,
  2530. features64, vdev->host_features);
  2531. return -1;
  2532. }
  2533. } else {
  2534. if (virtio_set_features_nocheck(vdev, features) < 0) {
  2535. error_report("Features 0x%x unsupported. "
  2536. "Allowed features: 0x%" PRIx64,
  2537. features, vdev->host_features);
  2538. return -1;
  2539. }
  2540. }
  2541. if (!virtio_device_started(vdev, vdev->status) &&
  2542. !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  2543. vdev->start_on_kick = true;
  2544. }
  2545. RCU_READ_LOCK_GUARD();
  2546. for (i = 0; i < num; i++) {
  2547. if (vdev->vq[i].vring.desc) {
  2548. uint16_t nheads;
  2549. /*
  2550. * VIRTIO-1 devices migrate desc, used, and avail ring addresses so
  2551. * only the region cache needs to be set up. Legacy devices need
  2552. * to calculate used and avail ring addresses based on the desc
  2553. * address.
  2554. */
  2555. if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  2556. virtio_init_region_cache(vdev, i);
  2557. } else {
  2558. virtio_queue_update_rings(vdev, i);
  2559. }
  2560. if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  2561. vdev->vq[i].shadow_avail_idx = vdev->vq[i].last_avail_idx;
  2562. vdev->vq[i].shadow_avail_wrap_counter =
  2563. vdev->vq[i].last_avail_wrap_counter;
  2564. continue;
  2565. }
  2566. nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
  2567. /* Check it isn't doing strange things with descriptor numbers. */
  2568. if (nheads > vdev->vq[i].vring.num) {
  2569. error_report("VQ %d size 0x%x Guest index 0x%x "
  2570. "inconsistent with Host index 0x%x: delta 0x%x",
  2571. i, vdev->vq[i].vring.num,
  2572. vring_avail_idx(&vdev->vq[i]),
  2573. vdev->vq[i].last_avail_idx, nheads);
  2574. return -1;
  2575. }
  2576. vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]);
  2577. vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]);
  2578. /*
  2579. * Some devices migrate VirtQueueElements that have been popped
  2580. * from the avail ring but not yet returned to the used ring.
  2581. * Since max ring size < UINT16_MAX it's safe to use modulo
  2582. * UINT16_MAX + 1 subtraction.
  2583. */
  2584. vdev->vq[i].inuse = (uint16_t)(vdev->vq[i].last_avail_idx -
  2585. vdev->vq[i].used_idx);
  2586. if (vdev->vq[i].inuse > vdev->vq[i].vring.num) {
  2587. error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
  2588. "used_idx 0x%x",
  2589. i, vdev->vq[i].vring.num,
  2590. vdev->vq[i].last_avail_idx,
  2591. vdev->vq[i].used_idx);
  2592. return -1;
  2593. }
  2594. }
  2595. }
  2596. if (vdc->post_load) {
  2597. ret = vdc->post_load(vdev);
  2598. if (ret) {
  2599. return ret;
  2600. }
  2601. }
  2602. return 0;
  2603. }
  2604. void virtio_cleanup(VirtIODevice *vdev)
  2605. {
  2606. qemu_del_vm_change_state_handler(vdev->vmstate);
  2607. }
  2608. static void virtio_vmstate_change(void *opaque, int running, RunState state)
  2609. {
  2610. VirtIODevice *vdev = opaque;
  2611. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  2612. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  2613. bool backend_run = running && virtio_device_started(vdev, vdev->status);
  2614. vdev->vm_running = running;
  2615. if (backend_run) {
  2616. virtio_set_status(vdev, vdev->status);
  2617. }
  2618. if (k->vmstate_change) {
  2619. k->vmstate_change(qbus->parent, backend_run);
  2620. }
  2621. if (!backend_run) {
  2622. virtio_set_status(vdev, vdev->status);
  2623. }
  2624. }
  2625. void virtio_instance_init_common(Object *proxy_obj, void *data,
  2626. size_t vdev_size, const char *vdev_name)
  2627. {
  2628. DeviceState *vdev = data;
  2629. object_initialize_child(proxy_obj, "virtio-backend", vdev, vdev_size,
  2630. vdev_name, &error_abort, NULL);
  2631. qdev_alias_all_properties(vdev, proxy_obj);
  2632. }
  2633. void virtio_init(VirtIODevice *vdev, const char *name,
  2634. uint16_t device_id, size_t config_size)
  2635. {
  2636. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  2637. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  2638. int i;
  2639. int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0;
  2640. if (nvectors) {
  2641. vdev->vector_queues =
  2642. g_malloc0(sizeof(*vdev->vector_queues) * nvectors);
  2643. }
  2644. vdev->start_on_kick = false;
  2645. vdev->started = false;
  2646. vdev->device_id = device_id;
  2647. vdev->status = 0;
  2648. atomic_set(&vdev->isr, 0);
  2649. vdev->queue_sel = 0;
  2650. vdev->config_vector = VIRTIO_NO_VECTOR;
  2651. vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX);
  2652. vdev->vm_running = runstate_is_running();
  2653. vdev->broken = false;
  2654. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  2655. vdev->vq[i].vector = VIRTIO_NO_VECTOR;
  2656. vdev->vq[i].vdev = vdev;
  2657. vdev->vq[i].queue_index = i;
  2658. vdev->vq[i].host_notifier_enabled = false;
  2659. }
  2660. vdev->name = name;
  2661. vdev->config_len = config_size;
  2662. if (vdev->config_len) {
  2663. vdev->config = g_malloc0(config_size);
  2664. } else {
  2665. vdev->config = NULL;
  2666. }
  2667. vdev->vmstate = qdev_add_vm_change_state_handler(DEVICE(vdev),
  2668. virtio_vmstate_change, vdev);
  2669. vdev->device_endian = virtio_default_endian();
  2670. vdev->use_guest_notifier_mask = true;
  2671. }
  2672. hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
  2673. {
  2674. return vdev->vq[n].vring.desc;
  2675. }
  2676. bool virtio_queue_enabled(VirtIODevice *vdev, int n)
  2677. {
  2678. return virtio_queue_get_desc_addr(vdev, n) != 0;
  2679. }
  2680. hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
  2681. {
  2682. return vdev->vq[n].vring.avail;
  2683. }
  2684. hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
  2685. {
  2686. return vdev->vq[n].vring.used;
  2687. }
  2688. hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
  2689. {
  2690. return sizeof(VRingDesc) * vdev->vq[n].vring.num;
  2691. }
  2692. hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
  2693. {
  2694. int s;
  2695. if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  2696. return sizeof(struct VRingPackedDescEvent);
  2697. }
  2698. s = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  2699. return offsetof(VRingAvail, ring) +
  2700. sizeof(uint16_t) * vdev->vq[n].vring.num + s;
  2701. }
  2702. hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n)
  2703. {
  2704. int s;
  2705. if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  2706. return sizeof(struct VRingPackedDescEvent);
  2707. }
  2708. s = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  2709. return offsetof(VRingUsed, ring) +
  2710. sizeof(VRingUsedElem) * vdev->vq[n].vring.num + s;
  2711. }
  2712. static unsigned int virtio_queue_packed_get_last_avail_idx(VirtIODevice *vdev,
  2713. int n)
  2714. {
  2715. unsigned int avail, used;
  2716. avail = vdev->vq[n].last_avail_idx;
  2717. avail |= ((uint16_t)vdev->vq[n].last_avail_wrap_counter) << 15;
  2718. used = vdev->vq[n].used_idx;
  2719. used |= ((uint16_t)vdev->vq[n].used_wrap_counter) << 15;
  2720. return avail | used << 16;
  2721. }
  2722. static uint16_t virtio_queue_split_get_last_avail_idx(VirtIODevice *vdev,
  2723. int n)
  2724. {
  2725. return vdev->vq[n].last_avail_idx;
  2726. }
  2727. unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
  2728. {
  2729. if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  2730. return virtio_queue_packed_get_last_avail_idx(vdev, n);
  2731. } else {
  2732. return virtio_queue_split_get_last_avail_idx(vdev, n);
  2733. }
  2734. }
  2735. static void virtio_queue_packed_set_last_avail_idx(VirtIODevice *vdev,
  2736. int n, unsigned int idx)
  2737. {
  2738. struct VirtQueue *vq = &vdev->vq[n];
  2739. vq->last_avail_idx = vq->shadow_avail_idx = idx & 0x7fff;
  2740. vq->last_avail_wrap_counter =
  2741. vq->shadow_avail_wrap_counter = !!(idx & 0x8000);
  2742. idx >>= 16;
  2743. vq->used_idx = idx & 0x7ffff;
  2744. vq->used_wrap_counter = !!(idx & 0x8000);
  2745. }
  2746. static void virtio_queue_split_set_last_avail_idx(VirtIODevice *vdev,
  2747. int n, unsigned int idx)
  2748. {
  2749. vdev->vq[n].last_avail_idx = idx;
  2750. vdev->vq[n].shadow_avail_idx = idx;
  2751. }
  2752. void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n,
  2753. unsigned int idx)
  2754. {
  2755. if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  2756. virtio_queue_packed_set_last_avail_idx(vdev, n, idx);
  2757. } else {
  2758. virtio_queue_split_set_last_avail_idx(vdev, n, idx);
  2759. }
  2760. }
  2761. static void virtio_queue_packed_restore_last_avail_idx(VirtIODevice *vdev,
  2762. int n)
  2763. {
  2764. /* We don't have a reference like avail idx in shared memory */
  2765. return;
  2766. }
  2767. static void virtio_queue_split_restore_last_avail_idx(VirtIODevice *vdev,
  2768. int n)
  2769. {
  2770. RCU_READ_LOCK_GUARD();
  2771. if (vdev->vq[n].vring.desc) {
  2772. vdev->vq[n].last_avail_idx = vring_used_idx(&vdev->vq[n]);
  2773. vdev->vq[n].shadow_avail_idx = vdev->vq[n].last_avail_idx;
  2774. }
  2775. }
  2776. void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n)
  2777. {
  2778. if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  2779. virtio_queue_packed_restore_last_avail_idx(vdev, n);
  2780. } else {
  2781. virtio_queue_split_restore_last_avail_idx(vdev, n);
  2782. }
  2783. }
  2784. static void virtio_queue_packed_update_used_idx(VirtIODevice *vdev, int n)
  2785. {
  2786. /* used idx was updated through set_last_avail_idx() */
  2787. return;
  2788. }
  2789. static void virtio_split_packed_update_used_idx(VirtIODevice *vdev, int n)
  2790. {
  2791. RCU_READ_LOCK_GUARD();
  2792. if (vdev->vq[n].vring.desc) {
  2793. vdev->vq[n].used_idx = vring_used_idx(&vdev->vq[n]);
  2794. }
  2795. }
  2796. void virtio_queue_update_used_idx(VirtIODevice *vdev, int n)
  2797. {
  2798. if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
  2799. return virtio_queue_packed_update_used_idx(vdev, n);
  2800. } else {
  2801. return virtio_split_packed_update_used_idx(vdev, n);
  2802. }
  2803. }
  2804. void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
  2805. {
  2806. vdev->vq[n].signalled_used_valid = false;
  2807. }
  2808. VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
  2809. {
  2810. return vdev->vq + n;
  2811. }
  2812. uint16_t virtio_get_queue_index(VirtQueue *vq)
  2813. {
  2814. return vq->queue_index;
  2815. }
  2816. static void virtio_queue_guest_notifier_read(EventNotifier *n)
  2817. {
  2818. VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
  2819. if (event_notifier_test_and_clear(n)) {
  2820. virtio_irq(vq);
  2821. }
  2822. }
  2823. void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
  2824. bool with_irqfd)
  2825. {
  2826. if (assign && !with_irqfd) {
  2827. event_notifier_set_handler(&vq->guest_notifier,
  2828. virtio_queue_guest_notifier_read);
  2829. } else {
  2830. event_notifier_set_handler(&vq->guest_notifier, NULL);
  2831. }
  2832. if (!assign) {
  2833. /* Test and clear notifier before closing it,
  2834. * in case poll callback didn't have time to run. */
  2835. virtio_queue_guest_notifier_read(&vq->guest_notifier);
  2836. }
  2837. }
  2838. EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
  2839. {
  2840. return &vq->guest_notifier;
  2841. }
  2842. static void virtio_queue_host_notifier_aio_read(EventNotifier *n)
  2843. {
  2844. VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
  2845. if (event_notifier_test_and_clear(n)) {
  2846. virtio_queue_notify_aio_vq(vq);
  2847. }
  2848. }
  2849. static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier *n)
  2850. {
  2851. VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
  2852. virtio_queue_set_notification(vq, 0);
  2853. }
  2854. static bool virtio_queue_host_notifier_aio_poll(void *opaque)
  2855. {
  2856. EventNotifier *n = opaque;
  2857. VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
  2858. bool progress;
  2859. if (!vq->vring.desc || virtio_queue_empty(vq)) {
  2860. return false;
  2861. }
  2862. progress = virtio_queue_notify_aio_vq(vq);
  2863. /* In case the handler function re-enabled notifications */
  2864. virtio_queue_set_notification(vq, 0);
  2865. return progress;
  2866. }
  2867. static void virtio_queue_host_notifier_aio_poll_end(EventNotifier *n)
  2868. {
  2869. VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
  2870. /* Caller polls once more after this to catch requests that race with us */
  2871. virtio_queue_set_notification(vq, 1);
  2872. }
  2873. void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
  2874. VirtIOHandleAIOOutput handle_output)
  2875. {
  2876. if (handle_output) {
  2877. vq->handle_aio_output = handle_output;
  2878. aio_set_event_notifier(ctx, &vq->host_notifier, true,
  2879. virtio_queue_host_notifier_aio_read,
  2880. virtio_queue_host_notifier_aio_poll);
  2881. aio_set_event_notifier_poll(ctx, &vq->host_notifier,
  2882. virtio_queue_host_notifier_aio_poll_begin,
  2883. virtio_queue_host_notifier_aio_poll_end);
  2884. } else {
  2885. aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL, NULL);
  2886. /* Test and clear notifier before after disabling event,
  2887. * in case poll callback didn't have time to run. */
  2888. virtio_queue_host_notifier_aio_read(&vq->host_notifier);
  2889. vq->handle_aio_output = NULL;
  2890. }
  2891. }
  2892. void virtio_queue_host_notifier_read(EventNotifier *n)
  2893. {
  2894. VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
  2895. if (event_notifier_test_and_clear(n)) {
  2896. virtio_queue_notify_vq(vq);
  2897. }
  2898. }
  2899. EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
  2900. {
  2901. return &vq->host_notifier;
  2902. }
  2903. void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled)
  2904. {
  2905. vq->host_notifier_enabled = enabled;
  2906. }
  2907. int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n,
  2908. MemoryRegion *mr, bool assign)
  2909. {
  2910. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  2911. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  2912. if (k->set_host_notifier_mr) {
  2913. return k->set_host_notifier_mr(qbus->parent, n, mr, assign);
  2914. }
  2915. return -1;
  2916. }
  2917. void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
  2918. {
  2919. g_free(vdev->bus_name);
  2920. vdev->bus_name = g_strdup(bus_name);
  2921. }
  2922. void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice *vdev, const char *fmt, ...)
  2923. {
  2924. va_list ap;
  2925. va_start(ap, fmt);
  2926. error_vreport(fmt, ap);
  2927. va_end(ap);
  2928. if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  2929. vdev->status = vdev->status | VIRTIO_CONFIG_S_NEEDS_RESET;
  2930. virtio_notify_config(vdev);
  2931. }
  2932. vdev->broken = true;
  2933. }
  2934. static void virtio_memory_listener_commit(MemoryListener *listener)
  2935. {
  2936. VirtIODevice *vdev = container_of(listener, VirtIODevice, listener);
  2937. int i;
  2938. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  2939. if (vdev->vq[i].vring.num == 0) {
  2940. break;
  2941. }
  2942. virtio_init_region_cache(vdev, i);
  2943. }
  2944. }
  2945. static void virtio_device_realize(DeviceState *dev, Error **errp)
  2946. {
  2947. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  2948. VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
  2949. Error *err = NULL;
  2950. /* Devices should either use vmsd or the load/save methods */
  2951. assert(!vdc->vmsd || !vdc->load);
  2952. if (vdc->realize != NULL) {
  2953. vdc->realize(dev, &err);
  2954. if (err != NULL) {
  2955. error_propagate(errp, err);
  2956. return;
  2957. }
  2958. }
  2959. virtio_bus_device_plugged(vdev, &err);
  2960. if (err != NULL) {
  2961. error_propagate(errp, err);
  2962. vdc->unrealize(dev, NULL);
  2963. return;
  2964. }
  2965. vdev->listener.commit = virtio_memory_listener_commit;
  2966. memory_listener_register(&vdev->listener, vdev->dma_as);
  2967. }
  2968. static void virtio_device_unrealize(DeviceState *dev, Error **errp)
  2969. {
  2970. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  2971. VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
  2972. Error *err = NULL;
  2973. virtio_bus_device_unplugged(vdev);
  2974. if (vdc->unrealize != NULL) {
  2975. vdc->unrealize(dev, &err);
  2976. if (err != NULL) {
  2977. error_propagate(errp, err);
  2978. return;
  2979. }
  2980. }
  2981. g_free(vdev->bus_name);
  2982. vdev->bus_name = NULL;
  2983. }
  2984. static void virtio_device_free_virtqueues(VirtIODevice *vdev)
  2985. {
  2986. int i;
  2987. if (!vdev->vq) {
  2988. return;
  2989. }
  2990. for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
  2991. if (vdev->vq[i].vring.num == 0) {
  2992. break;
  2993. }
  2994. virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
  2995. }
  2996. g_free(vdev->vq);
  2997. }
  2998. static void virtio_device_instance_finalize(Object *obj)
  2999. {
  3000. VirtIODevice *vdev = VIRTIO_DEVICE(obj);
  3001. memory_listener_unregister(&vdev->listener);
  3002. virtio_device_free_virtqueues(vdev);
  3003. g_free(vdev->config);
  3004. g_free(vdev->vector_queues);
  3005. }
  3006. static Property virtio_properties[] = {
  3007. DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features),
  3008. DEFINE_PROP_BOOL("use-started", VirtIODevice, use_started, true),
  3009. DEFINE_PROP_END_OF_LIST(),
  3010. };
  3011. static int virtio_device_start_ioeventfd_impl(VirtIODevice *vdev)
  3012. {
  3013. VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
  3014. int i, n, r, err;
  3015. memory_region_transaction_begin();
  3016. for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
  3017. VirtQueue *vq = &vdev->vq[n];
  3018. if (!virtio_queue_get_num(vdev, n)) {
  3019. continue;
  3020. }
  3021. r = virtio_bus_set_host_notifier(qbus, n, true);
  3022. if (r < 0) {
  3023. err = r;
  3024. goto assign_error;
  3025. }
  3026. event_notifier_set_handler(&vq->host_notifier,
  3027. virtio_queue_host_notifier_read);
  3028. }
  3029. for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
  3030. /* Kick right away to begin processing requests already in vring */
  3031. VirtQueue *vq = &vdev->vq[n];
  3032. if (!vq->vring.num) {
  3033. continue;
  3034. }
  3035. event_notifier_set(&vq->host_notifier);
  3036. }
  3037. memory_region_transaction_commit();
  3038. return 0;
  3039. assign_error:
  3040. i = n; /* save n for a second iteration after transaction is committed. */
  3041. while (--n >= 0) {
  3042. VirtQueue *vq = &vdev->vq[n];
  3043. if (!virtio_queue_get_num(vdev, n)) {
  3044. continue;
  3045. }
  3046. event_notifier_set_handler(&vq->host_notifier, NULL);
  3047. r = virtio_bus_set_host_notifier(qbus, n, false);
  3048. assert(r >= 0);
  3049. }
  3050. memory_region_transaction_commit();
  3051. while (--i >= 0) {
  3052. if (!virtio_queue_get_num(vdev, i)) {
  3053. continue;
  3054. }
  3055. virtio_bus_cleanup_host_notifier(qbus, i);
  3056. }
  3057. return err;
  3058. }
  3059. int virtio_device_start_ioeventfd(VirtIODevice *vdev)
  3060. {
  3061. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  3062. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  3063. return virtio_bus_start_ioeventfd(vbus);
  3064. }
  3065. static void virtio_device_stop_ioeventfd_impl(VirtIODevice *vdev)
  3066. {
  3067. VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
  3068. int n, r;
  3069. memory_region_transaction_begin();
  3070. for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
  3071. VirtQueue *vq = &vdev->vq[n];
  3072. if (!virtio_queue_get_num(vdev, n)) {
  3073. continue;
  3074. }
  3075. event_notifier_set_handler(&vq->host_notifier, NULL);
  3076. r = virtio_bus_set_host_notifier(qbus, n, false);
  3077. assert(r >= 0);
  3078. }
  3079. memory_region_transaction_commit();
  3080. for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
  3081. if (!virtio_queue_get_num(vdev, n)) {
  3082. continue;
  3083. }
  3084. virtio_bus_cleanup_host_notifier(qbus, n);
  3085. }
  3086. }
  3087. int virtio_device_grab_ioeventfd(VirtIODevice *vdev)
  3088. {
  3089. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  3090. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  3091. return virtio_bus_grab_ioeventfd(vbus);
  3092. }
  3093. void virtio_device_release_ioeventfd(VirtIODevice *vdev)
  3094. {
  3095. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  3096. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  3097. virtio_bus_release_ioeventfd(vbus);
  3098. }
  3099. static void virtio_device_class_init(ObjectClass *klass, void *data)
  3100. {
  3101. /* Set the default value here. */
  3102. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  3103. DeviceClass *dc = DEVICE_CLASS(klass);
  3104. dc->realize = virtio_device_realize;
  3105. dc->unrealize = virtio_device_unrealize;
  3106. dc->bus_type = TYPE_VIRTIO_BUS;
  3107. dc->props = virtio_properties;
  3108. vdc->start_ioeventfd = virtio_device_start_ioeventfd_impl;
  3109. vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl;
  3110. vdc->legacy_features |= VIRTIO_LEGACY_FEATURES;
  3111. }
  3112. bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev)
  3113. {
  3114. BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
  3115. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  3116. return virtio_bus_ioeventfd_enabled(vbus);
  3117. }
  3118. static const TypeInfo virtio_device_info = {
  3119. .name = TYPE_VIRTIO_DEVICE,
  3120. .parent = TYPE_DEVICE,
  3121. .instance_size = sizeof(VirtIODevice),
  3122. .class_init = virtio_device_class_init,
  3123. .instance_finalize = virtio_device_instance_finalize,
  3124. .abstract = true,
  3125. .class_size = sizeof(VirtioDeviceClass),
  3126. };
  3127. static void virtio_register_types(void)
  3128. {
  3129. type_register_static(&virtio_device_info);
  3130. }
  3131. type_init(virtio_register_types)