virtio.c 108 KB

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