virtio.c 108 KB

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