exec.c 141 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849
  1. /*
  2. * virtual page mapping and translated block handling
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #ifdef _WIN32
  21. #include <windows.h>
  22. #else
  23. #include <sys/types.h>
  24. #include <sys/mman.h>
  25. #endif
  26. #include "qemu-common.h"
  27. #include "cpu.h"
  28. #include "tcg.h"
  29. #include "hw/hw.h"
  30. #include "hw/qdev.h"
  31. #include "osdep.h"
  32. #include "kvm.h"
  33. #include "hw/xen.h"
  34. #include "qemu-timer.h"
  35. #include "memory.h"
  36. #include "exec-memory.h"
  37. #if defined(CONFIG_USER_ONLY)
  38. #include <qemu.h>
  39. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  40. #include <sys/param.h>
  41. #if __FreeBSD_version >= 700104
  42. #define HAVE_KINFO_GETVMMAP
  43. #define sigqueue sigqueue_freebsd /* avoid redefinition */
  44. #include <sys/time.h>
  45. #include <sys/proc.h>
  46. #include <machine/profile.h>
  47. #define _KERNEL
  48. #include <sys/user.h>
  49. #undef _KERNEL
  50. #undef sigqueue
  51. #include <libutil.h>
  52. #endif
  53. #endif
  54. #else /* !CONFIG_USER_ONLY */
  55. #include "xen-mapcache.h"
  56. #include "trace.h"
  57. #endif
  58. //#define DEBUG_TB_INVALIDATE
  59. //#define DEBUG_FLUSH
  60. //#define DEBUG_TLB
  61. //#define DEBUG_UNASSIGNED
  62. /* make various TB consistency checks */
  63. //#define DEBUG_TB_CHECK
  64. //#define DEBUG_TLB_CHECK
  65. //#define DEBUG_IOPORT
  66. //#define DEBUG_SUBPAGE
  67. #if !defined(CONFIG_USER_ONLY)
  68. /* TB consistency checks only implemented for usermode emulation. */
  69. #undef DEBUG_TB_CHECK
  70. #endif
  71. #define SMC_BITMAP_USE_THRESHOLD 10
  72. static TranslationBlock *tbs;
  73. static int code_gen_max_blocks;
  74. TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
  75. static int nb_tbs;
  76. /* any access to the tbs or the page table must use this lock */
  77. spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
  78. #if defined(__arm__) || defined(__sparc_v9__)
  79. /* The prologue must be reachable with a direct jump. ARM and Sparc64
  80. have limited branch ranges (possibly also PPC) so place it in a
  81. section close to code segment. */
  82. #define code_gen_section \
  83. __attribute__((__section__(".gen_code"))) \
  84. __attribute__((aligned (32)))
  85. #elif defined(_WIN32)
  86. /* Maximum alignment for Win32 is 16. */
  87. #define code_gen_section \
  88. __attribute__((aligned (16)))
  89. #else
  90. #define code_gen_section \
  91. __attribute__((aligned (32)))
  92. #endif
  93. uint8_t code_gen_prologue[1024] code_gen_section;
  94. static uint8_t *code_gen_buffer;
  95. static unsigned long code_gen_buffer_size;
  96. /* threshold to flush the translated code buffer */
  97. static unsigned long code_gen_buffer_max_size;
  98. static uint8_t *code_gen_ptr;
  99. #if !defined(CONFIG_USER_ONLY)
  100. int phys_ram_fd;
  101. static int in_migration;
  102. RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list.blocks) };
  103. static MemoryRegion *system_memory;
  104. static MemoryRegion *system_io;
  105. #endif
  106. CPUState *first_cpu;
  107. /* current CPU in the current thread. It is only valid inside
  108. cpu_exec() */
  109. DEFINE_TLS(CPUState *,cpu_single_env);
  110. /* 0 = Do not count executed instructions.
  111. 1 = Precise instruction counting.
  112. 2 = Adaptive rate instruction counting. */
  113. int use_icount = 0;
  114. typedef struct PageDesc {
  115. /* list of TBs intersecting this ram page */
  116. TranslationBlock *first_tb;
  117. /* in order to optimize self modifying code, we count the number
  118. of lookups we do to a given page to use a bitmap */
  119. unsigned int code_write_count;
  120. uint8_t *code_bitmap;
  121. #if defined(CONFIG_USER_ONLY)
  122. unsigned long flags;
  123. #endif
  124. } PageDesc;
  125. /* In system mode we want L1_MAP to be based on ram offsets,
  126. while in user mode we want it to be based on virtual addresses. */
  127. #if !defined(CONFIG_USER_ONLY)
  128. #if HOST_LONG_BITS < TARGET_PHYS_ADDR_SPACE_BITS
  129. # define L1_MAP_ADDR_SPACE_BITS HOST_LONG_BITS
  130. #else
  131. # define L1_MAP_ADDR_SPACE_BITS TARGET_PHYS_ADDR_SPACE_BITS
  132. #endif
  133. #else
  134. # define L1_MAP_ADDR_SPACE_BITS TARGET_VIRT_ADDR_SPACE_BITS
  135. #endif
  136. /* Size of the L2 (and L3, etc) page tables. */
  137. #define L2_BITS 10
  138. #define L2_SIZE (1 << L2_BITS)
  139. /* The bits remaining after N lower levels of page tables. */
  140. #define P_L1_BITS_REM \
  141. ((TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
  142. #define V_L1_BITS_REM \
  143. ((L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS) % L2_BITS)
  144. /* Size of the L1 page table. Avoid silly small sizes. */
  145. #if P_L1_BITS_REM < 4
  146. #define P_L1_BITS (P_L1_BITS_REM + L2_BITS)
  147. #else
  148. #define P_L1_BITS P_L1_BITS_REM
  149. #endif
  150. #if V_L1_BITS_REM < 4
  151. #define V_L1_BITS (V_L1_BITS_REM + L2_BITS)
  152. #else
  153. #define V_L1_BITS V_L1_BITS_REM
  154. #endif
  155. #define P_L1_SIZE ((target_phys_addr_t)1 << P_L1_BITS)
  156. #define V_L1_SIZE ((target_ulong)1 << V_L1_BITS)
  157. #define P_L1_SHIFT (TARGET_PHYS_ADDR_SPACE_BITS - TARGET_PAGE_BITS - P_L1_BITS)
  158. #define V_L1_SHIFT (L1_MAP_ADDR_SPACE_BITS - TARGET_PAGE_BITS - V_L1_BITS)
  159. unsigned long qemu_real_host_page_size;
  160. unsigned long qemu_host_page_size;
  161. unsigned long qemu_host_page_mask;
  162. /* This is a multi-level map on the virtual address space.
  163. The bottom level has pointers to PageDesc. */
  164. static void *l1_map[V_L1_SIZE];
  165. #if !defined(CONFIG_USER_ONLY)
  166. typedef struct PhysPageDesc {
  167. /* offset in host memory of the page + io_index in the low bits */
  168. ram_addr_t phys_offset;
  169. ram_addr_t region_offset;
  170. } PhysPageDesc;
  171. /* This is a multi-level map on the physical address space.
  172. The bottom level has pointers to PhysPageDesc. */
  173. static void *l1_phys_map[P_L1_SIZE];
  174. static void io_mem_init(void);
  175. static void memory_map_init(void);
  176. /* io memory support */
  177. CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
  178. CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
  179. void *io_mem_opaque[IO_MEM_NB_ENTRIES];
  180. static char io_mem_used[IO_MEM_NB_ENTRIES];
  181. static int io_mem_watch;
  182. #endif
  183. /* log support */
  184. #ifdef WIN32
  185. static const char *logfilename = "qemu.log";
  186. #else
  187. static const char *logfilename = "/tmp/qemu.log";
  188. #endif
  189. FILE *logfile;
  190. int loglevel;
  191. static int log_append = 0;
  192. /* statistics */
  193. #if !defined(CONFIG_USER_ONLY)
  194. static int tlb_flush_count;
  195. #endif
  196. static int tb_flush_count;
  197. static int tb_phys_invalidate_count;
  198. #ifdef _WIN32
  199. static void map_exec(void *addr, long size)
  200. {
  201. DWORD old_protect;
  202. VirtualProtect(addr, size,
  203. PAGE_EXECUTE_READWRITE, &old_protect);
  204. }
  205. #else
  206. static void map_exec(void *addr, long size)
  207. {
  208. unsigned long start, end, page_size;
  209. page_size = getpagesize();
  210. start = (unsigned long)addr;
  211. start &= ~(page_size - 1);
  212. end = (unsigned long)addr + size;
  213. end += page_size - 1;
  214. end &= ~(page_size - 1);
  215. mprotect((void *)start, end - start,
  216. PROT_READ | PROT_WRITE | PROT_EXEC);
  217. }
  218. #endif
  219. static void page_init(void)
  220. {
  221. /* NOTE: we can always suppose that qemu_host_page_size >=
  222. TARGET_PAGE_SIZE */
  223. #ifdef _WIN32
  224. {
  225. SYSTEM_INFO system_info;
  226. GetSystemInfo(&system_info);
  227. qemu_real_host_page_size = system_info.dwPageSize;
  228. }
  229. #else
  230. qemu_real_host_page_size = getpagesize();
  231. #endif
  232. if (qemu_host_page_size == 0)
  233. qemu_host_page_size = qemu_real_host_page_size;
  234. if (qemu_host_page_size < TARGET_PAGE_SIZE)
  235. qemu_host_page_size = TARGET_PAGE_SIZE;
  236. qemu_host_page_mask = ~(qemu_host_page_size - 1);
  237. #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY)
  238. {
  239. #ifdef HAVE_KINFO_GETVMMAP
  240. struct kinfo_vmentry *freep;
  241. int i, cnt;
  242. freep = kinfo_getvmmap(getpid(), &cnt);
  243. if (freep) {
  244. mmap_lock();
  245. for (i = 0; i < cnt; i++) {
  246. unsigned long startaddr, endaddr;
  247. startaddr = freep[i].kve_start;
  248. endaddr = freep[i].kve_end;
  249. if (h2g_valid(startaddr)) {
  250. startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
  251. if (h2g_valid(endaddr)) {
  252. endaddr = h2g(endaddr);
  253. page_set_flags(startaddr, endaddr, PAGE_RESERVED);
  254. } else {
  255. #if TARGET_ABI_BITS <= L1_MAP_ADDR_SPACE_BITS
  256. endaddr = ~0ul;
  257. page_set_flags(startaddr, endaddr, PAGE_RESERVED);
  258. #endif
  259. }
  260. }
  261. }
  262. free(freep);
  263. mmap_unlock();
  264. }
  265. #else
  266. FILE *f;
  267. last_brk = (unsigned long)sbrk(0);
  268. f = fopen("/compat/linux/proc/self/maps", "r");
  269. if (f) {
  270. mmap_lock();
  271. do {
  272. unsigned long startaddr, endaddr;
  273. int n;
  274. n = fscanf (f, "%lx-%lx %*[^\n]\n", &startaddr, &endaddr);
  275. if (n == 2 && h2g_valid(startaddr)) {
  276. startaddr = h2g(startaddr) & TARGET_PAGE_MASK;
  277. if (h2g_valid(endaddr)) {
  278. endaddr = h2g(endaddr);
  279. } else {
  280. endaddr = ~0ul;
  281. }
  282. page_set_flags(startaddr, endaddr, PAGE_RESERVED);
  283. }
  284. } while (!feof(f));
  285. fclose(f);
  286. mmap_unlock();
  287. }
  288. #endif
  289. }
  290. #endif
  291. }
  292. static PageDesc *page_find_alloc(tb_page_addr_t index, int alloc)
  293. {
  294. PageDesc *pd;
  295. void **lp;
  296. int i;
  297. #if defined(CONFIG_USER_ONLY)
  298. /* We can't use g_malloc because it may recurse into a locked mutex. */
  299. # define ALLOC(P, SIZE) \
  300. do { \
  301. P = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, \
  302. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
  303. } while (0)
  304. #else
  305. # define ALLOC(P, SIZE) \
  306. do { P = g_malloc0(SIZE); } while (0)
  307. #endif
  308. /* Level 1. Always allocated. */
  309. lp = l1_map + ((index >> V_L1_SHIFT) & (V_L1_SIZE - 1));
  310. /* Level 2..N-1. */
  311. for (i = V_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
  312. void **p = *lp;
  313. if (p == NULL) {
  314. if (!alloc) {
  315. return NULL;
  316. }
  317. ALLOC(p, sizeof(void *) * L2_SIZE);
  318. *lp = p;
  319. }
  320. lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
  321. }
  322. pd = *lp;
  323. if (pd == NULL) {
  324. if (!alloc) {
  325. return NULL;
  326. }
  327. ALLOC(pd, sizeof(PageDesc) * L2_SIZE);
  328. *lp = pd;
  329. }
  330. #undef ALLOC
  331. return pd + (index & (L2_SIZE - 1));
  332. }
  333. static inline PageDesc *page_find(tb_page_addr_t index)
  334. {
  335. return page_find_alloc(index, 0);
  336. }
  337. #if !defined(CONFIG_USER_ONLY)
  338. static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
  339. {
  340. PhysPageDesc *pd;
  341. void **lp;
  342. int i;
  343. /* Level 1. Always allocated. */
  344. lp = l1_phys_map + ((index >> P_L1_SHIFT) & (P_L1_SIZE - 1));
  345. /* Level 2..N-1. */
  346. for (i = P_L1_SHIFT / L2_BITS - 1; i > 0; i--) {
  347. void **p = *lp;
  348. if (p == NULL) {
  349. if (!alloc) {
  350. return NULL;
  351. }
  352. *lp = p = g_malloc0(sizeof(void *) * L2_SIZE);
  353. }
  354. lp = p + ((index >> (i * L2_BITS)) & (L2_SIZE - 1));
  355. }
  356. pd = *lp;
  357. if (pd == NULL) {
  358. int i;
  359. if (!alloc) {
  360. return NULL;
  361. }
  362. *lp = pd = g_malloc(sizeof(PhysPageDesc) * L2_SIZE);
  363. for (i = 0; i < L2_SIZE; i++) {
  364. pd[i].phys_offset = IO_MEM_UNASSIGNED;
  365. pd[i].region_offset = (index + i) << TARGET_PAGE_BITS;
  366. }
  367. }
  368. return pd + (index & (L2_SIZE - 1));
  369. }
  370. static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
  371. {
  372. return phys_page_find_alloc(index, 0);
  373. }
  374. static void tlb_protect_code(ram_addr_t ram_addr);
  375. static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
  376. target_ulong vaddr);
  377. #define mmap_lock() do { } while(0)
  378. #define mmap_unlock() do { } while(0)
  379. #endif
  380. #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
  381. #if defined(CONFIG_USER_ONLY)
  382. /* Currently it is not recommended to allocate big chunks of data in
  383. user mode. It will change when a dedicated libc will be used */
  384. #define USE_STATIC_CODE_GEN_BUFFER
  385. #endif
  386. #ifdef USE_STATIC_CODE_GEN_BUFFER
  387. static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
  388. __attribute__((aligned (CODE_GEN_ALIGN)));
  389. #endif
  390. static void code_gen_alloc(unsigned long tb_size)
  391. {
  392. #ifdef USE_STATIC_CODE_GEN_BUFFER
  393. code_gen_buffer = static_code_gen_buffer;
  394. code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
  395. map_exec(code_gen_buffer, code_gen_buffer_size);
  396. #else
  397. code_gen_buffer_size = tb_size;
  398. if (code_gen_buffer_size == 0) {
  399. #if defined(CONFIG_USER_ONLY)
  400. code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
  401. #else
  402. /* XXX: needs adjustments */
  403. code_gen_buffer_size = (unsigned long)(ram_size / 4);
  404. #endif
  405. }
  406. if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
  407. code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
  408. /* The code gen buffer location may have constraints depending on
  409. the host cpu and OS */
  410. #if defined(__linux__)
  411. {
  412. int flags;
  413. void *start = NULL;
  414. flags = MAP_PRIVATE | MAP_ANONYMOUS;
  415. #if defined(__x86_64__)
  416. flags |= MAP_32BIT;
  417. /* Cannot map more than that */
  418. if (code_gen_buffer_size > (800 * 1024 * 1024))
  419. code_gen_buffer_size = (800 * 1024 * 1024);
  420. #elif defined(__sparc_v9__)
  421. // Map the buffer below 2G, so we can use direct calls and branches
  422. flags |= MAP_FIXED;
  423. start = (void *) 0x60000000UL;
  424. if (code_gen_buffer_size > (512 * 1024 * 1024))
  425. code_gen_buffer_size = (512 * 1024 * 1024);
  426. #elif defined(__arm__)
  427. /* Map the buffer below 32M, so we can use direct calls and branches */
  428. flags |= MAP_FIXED;
  429. start = (void *) 0x01000000UL;
  430. if (code_gen_buffer_size > 16 * 1024 * 1024)
  431. code_gen_buffer_size = 16 * 1024 * 1024;
  432. #elif defined(__s390x__)
  433. /* Map the buffer so that we can use direct calls and branches. */
  434. /* We have a +- 4GB range on the branches; leave some slop. */
  435. if (code_gen_buffer_size > (3ul * 1024 * 1024 * 1024)) {
  436. code_gen_buffer_size = 3ul * 1024 * 1024 * 1024;
  437. }
  438. start = (void *)0x90000000UL;
  439. #endif
  440. code_gen_buffer = mmap(start, code_gen_buffer_size,
  441. PROT_WRITE | PROT_READ | PROT_EXEC,
  442. flags, -1, 0);
  443. if (code_gen_buffer == MAP_FAILED) {
  444. fprintf(stderr, "Could not allocate dynamic translator buffer\n");
  445. exit(1);
  446. }
  447. }
  448. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
  449. || defined(__DragonFly__) || defined(__OpenBSD__) \
  450. || defined(__NetBSD__)
  451. {
  452. int flags;
  453. void *addr = NULL;
  454. flags = MAP_PRIVATE | MAP_ANONYMOUS;
  455. #if defined(__x86_64__)
  456. /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
  457. * 0x40000000 is free */
  458. flags |= MAP_FIXED;
  459. addr = (void *)0x40000000;
  460. /* Cannot map more than that */
  461. if (code_gen_buffer_size > (800 * 1024 * 1024))
  462. code_gen_buffer_size = (800 * 1024 * 1024);
  463. #elif defined(__sparc_v9__)
  464. // Map the buffer below 2G, so we can use direct calls and branches
  465. flags |= MAP_FIXED;
  466. addr = (void *) 0x60000000UL;
  467. if (code_gen_buffer_size > (512 * 1024 * 1024)) {
  468. code_gen_buffer_size = (512 * 1024 * 1024);
  469. }
  470. #endif
  471. code_gen_buffer = mmap(addr, code_gen_buffer_size,
  472. PROT_WRITE | PROT_READ | PROT_EXEC,
  473. flags, -1, 0);
  474. if (code_gen_buffer == MAP_FAILED) {
  475. fprintf(stderr, "Could not allocate dynamic translator buffer\n");
  476. exit(1);
  477. }
  478. }
  479. #else
  480. code_gen_buffer = g_malloc(code_gen_buffer_size);
  481. map_exec(code_gen_buffer, code_gen_buffer_size);
  482. #endif
  483. #endif /* !USE_STATIC_CODE_GEN_BUFFER */
  484. map_exec(code_gen_prologue, sizeof(code_gen_prologue));
  485. code_gen_buffer_max_size = code_gen_buffer_size -
  486. (TCG_MAX_OP_SIZE * OPC_BUF_SIZE);
  487. code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
  488. tbs = g_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
  489. }
  490. /* Must be called before using the QEMU cpus. 'tb_size' is the size
  491. (in bytes) allocated to the translation buffer. Zero means default
  492. size. */
  493. void tcg_exec_init(unsigned long tb_size)
  494. {
  495. cpu_gen_init();
  496. code_gen_alloc(tb_size);
  497. code_gen_ptr = code_gen_buffer;
  498. page_init();
  499. #if !defined(CONFIG_USER_ONLY) || !defined(CONFIG_USE_GUEST_BASE)
  500. /* There's no guest base to take into account, so go ahead and
  501. initialize the prologue now. */
  502. tcg_prologue_init(&tcg_ctx);
  503. #endif
  504. }
  505. bool tcg_enabled(void)
  506. {
  507. return code_gen_buffer != NULL;
  508. }
  509. void cpu_exec_init_all(void)
  510. {
  511. #if !defined(CONFIG_USER_ONLY)
  512. memory_map_init();
  513. io_mem_init();
  514. #endif
  515. }
  516. #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
  517. static int cpu_common_post_load(void *opaque, int version_id)
  518. {
  519. CPUState *env = opaque;
  520. /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
  521. version_id is increased. */
  522. env->interrupt_request &= ~0x01;
  523. tlb_flush(env, 1);
  524. return 0;
  525. }
  526. static const VMStateDescription vmstate_cpu_common = {
  527. .name = "cpu_common",
  528. .version_id = 1,
  529. .minimum_version_id = 1,
  530. .minimum_version_id_old = 1,
  531. .post_load = cpu_common_post_load,
  532. .fields = (VMStateField []) {
  533. VMSTATE_UINT32(halted, CPUState),
  534. VMSTATE_UINT32(interrupt_request, CPUState),
  535. VMSTATE_END_OF_LIST()
  536. }
  537. };
  538. #endif
  539. CPUState *qemu_get_cpu(int cpu)
  540. {
  541. CPUState *env = first_cpu;
  542. while (env) {
  543. if (env->cpu_index == cpu)
  544. break;
  545. env = env->next_cpu;
  546. }
  547. return env;
  548. }
  549. void cpu_exec_init(CPUState *env)
  550. {
  551. CPUState **penv;
  552. int cpu_index;
  553. #if defined(CONFIG_USER_ONLY)
  554. cpu_list_lock();
  555. #endif
  556. env->next_cpu = NULL;
  557. penv = &first_cpu;
  558. cpu_index = 0;
  559. while (*penv != NULL) {
  560. penv = &(*penv)->next_cpu;
  561. cpu_index++;
  562. }
  563. env->cpu_index = cpu_index;
  564. env->numa_node = 0;
  565. QTAILQ_INIT(&env->breakpoints);
  566. QTAILQ_INIT(&env->watchpoints);
  567. #ifndef CONFIG_USER_ONLY
  568. env->thread_id = qemu_get_thread_id();
  569. #endif
  570. *penv = env;
  571. #if defined(CONFIG_USER_ONLY)
  572. cpu_list_unlock();
  573. #endif
  574. #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
  575. vmstate_register(NULL, cpu_index, &vmstate_cpu_common, env);
  576. register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
  577. cpu_save, cpu_load, env);
  578. #endif
  579. }
  580. /* Allocate a new translation block. Flush the translation buffer if
  581. too many translation blocks or too much generated code. */
  582. static TranslationBlock *tb_alloc(target_ulong pc)
  583. {
  584. TranslationBlock *tb;
  585. if (nb_tbs >= code_gen_max_blocks ||
  586. (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
  587. return NULL;
  588. tb = &tbs[nb_tbs++];
  589. tb->pc = pc;
  590. tb->cflags = 0;
  591. return tb;
  592. }
  593. void tb_free(TranslationBlock *tb)
  594. {
  595. /* In practice this is mostly used for single use temporary TB
  596. Ignore the hard cases and just back up if this TB happens to
  597. be the last one generated. */
  598. if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
  599. code_gen_ptr = tb->tc_ptr;
  600. nb_tbs--;
  601. }
  602. }
  603. static inline void invalidate_page_bitmap(PageDesc *p)
  604. {
  605. if (p->code_bitmap) {
  606. g_free(p->code_bitmap);
  607. p->code_bitmap = NULL;
  608. }
  609. p->code_write_count = 0;
  610. }
  611. /* Set to NULL all the 'first_tb' fields in all PageDescs. */
  612. static void page_flush_tb_1 (int level, void **lp)
  613. {
  614. int i;
  615. if (*lp == NULL) {
  616. return;
  617. }
  618. if (level == 0) {
  619. PageDesc *pd = *lp;
  620. for (i = 0; i < L2_SIZE; ++i) {
  621. pd[i].first_tb = NULL;
  622. invalidate_page_bitmap(pd + i);
  623. }
  624. } else {
  625. void **pp = *lp;
  626. for (i = 0; i < L2_SIZE; ++i) {
  627. page_flush_tb_1 (level - 1, pp + i);
  628. }
  629. }
  630. }
  631. static void page_flush_tb(void)
  632. {
  633. int i;
  634. for (i = 0; i < V_L1_SIZE; i++) {
  635. page_flush_tb_1(V_L1_SHIFT / L2_BITS - 1, l1_map + i);
  636. }
  637. }
  638. /* flush all the translation blocks */
  639. /* XXX: tb_flush is currently not thread safe */
  640. void tb_flush(CPUState *env1)
  641. {
  642. CPUState *env;
  643. #if defined(DEBUG_FLUSH)
  644. printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
  645. (unsigned long)(code_gen_ptr - code_gen_buffer),
  646. nb_tbs, nb_tbs > 0 ?
  647. ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
  648. #endif
  649. if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
  650. cpu_abort(env1, "Internal error: code buffer overflow\n");
  651. nb_tbs = 0;
  652. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  653. memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
  654. }
  655. memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
  656. page_flush_tb();
  657. code_gen_ptr = code_gen_buffer;
  658. /* XXX: flush processor icache at this point if cache flush is
  659. expensive */
  660. tb_flush_count++;
  661. }
  662. #ifdef DEBUG_TB_CHECK
  663. static void tb_invalidate_check(target_ulong address)
  664. {
  665. TranslationBlock *tb;
  666. int i;
  667. address &= TARGET_PAGE_MASK;
  668. for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
  669. for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
  670. if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
  671. address >= tb->pc + tb->size)) {
  672. printf("ERROR invalidate: address=" TARGET_FMT_lx
  673. " PC=%08lx size=%04x\n",
  674. address, (long)tb->pc, tb->size);
  675. }
  676. }
  677. }
  678. }
  679. /* verify that all the pages have correct rights for code */
  680. static void tb_page_check(void)
  681. {
  682. TranslationBlock *tb;
  683. int i, flags1, flags2;
  684. for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
  685. for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
  686. flags1 = page_get_flags(tb->pc);
  687. flags2 = page_get_flags(tb->pc + tb->size - 1);
  688. if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
  689. printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
  690. (long)tb->pc, tb->size, flags1, flags2);
  691. }
  692. }
  693. }
  694. }
  695. #endif
  696. /* invalidate one TB */
  697. static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
  698. int next_offset)
  699. {
  700. TranslationBlock *tb1;
  701. for(;;) {
  702. tb1 = *ptb;
  703. if (tb1 == tb) {
  704. *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
  705. break;
  706. }
  707. ptb = (TranslationBlock **)((char *)tb1 + next_offset);
  708. }
  709. }
  710. static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
  711. {
  712. TranslationBlock *tb1;
  713. unsigned int n1;
  714. for(;;) {
  715. tb1 = *ptb;
  716. n1 = (long)tb1 & 3;
  717. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  718. if (tb1 == tb) {
  719. *ptb = tb1->page_next[n1];
  720. break;
  721. }
  722. ptb = &tb1->page_next[n1];
  723. }
  724. }
  725. static inline void tb_jmp_remove(TranslationBlock *tb, int n)
  726. {
  727. TranslationBlock *tb1, **ptb;
  728. unsigned int n1;
  729. ptb = &tb->jmp_next[n];
  730. tb1 = *ptb;
  731. if (tb1) {
  732. /* find tb(n) in circular list */
  733. for(;;) {
  734. tb1 = *ptb;
  735. n1 = (long)tb1 & 3;
  736. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  737. if (n1 == n && tb1 == tb)
  738. break;
  739. if (n1 == 2) {
  740. ptb = &tb1->jmp_first;
  741. } else {
  742. ptb = &tb1->jmp_next[n1];
  743. }
  744. }
  745. /* now we can suppress tb(n) from the list */
  746. *ptb = tb->jmp_next[n];
  747. tb->jmp_next[n] = NULL;
  748. }
  749. }
  750. /* reset the jump entry 'n' of a TB so that it is not chained to
  751. another TB */
  752. static inline void tb_reset_jump(TranslationBlock *tb, int n)
  753. {
  754. tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
  755. }
  756. void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
  757. {
  758. CPUState *env;
  759. PageDesc *p;
  760. unsigned int h, n1;
  761. tb_page_addr_t phys_pc;
  762. TranslationBlock *tb1, *tb2;
  763. /* remove the TB from the hash list */
  764. phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
  765. h = tb_phys_hash_func(phys_pc);
  766. tb_remove(&tb_phys_hash[h], tb,
  767. offsetof(TranslationBlock, phys_hash_next));
  768. /* remove the TB from the page list */
  769. if (tb->page_addr[0] != page_addr) {
  770. p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
  771. tb_page_remove(&p->first_tb, tb);
  772. invalidate_page_bitmap(p);
  773. }
  774. if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
  775. p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
  776. tb_page_remove(&p->first_tb, tb);
  777. invalidate_page_bitmap(p);
  778. }
  779. tb_invalidated_flag = 1;
  780. /* remove the TB from the hash list */
  781. h = tb_jmp_cache_hash_func(tb->pc);
  782. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  783. if (env->tb_jmp_cache[h] == tb)
  784. env->tb_jmp_cache[h] = NULL;
  785. }
  786. /* suppress this TB from the two jump lists */
  787. tb_jmp_remove(tb, 0);
  788. tb_jmp_remove(tb, 1);
  789. /* suppress any remaining jumps to this TB */
  790. tb1 = tb->jmp_first;
  791. for(;;) {
  792. n1 = (long)tb1 & 3;
  793. if (n1 == 2)
  794. break;
  795. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  796. tb2 = tb1->jmp_next[n1];
  797. tb_reset_jump(tb1, n1);
  798. tb1->jmp_next[n1] = NULL;
  799. tb1 = tb2;
  800. }
  801. tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
  802. tb_phys_invalidate_count++;
  803. }
  804. static inline void set_bits(uint8_t *tab, int start, int len)
  805. {
  806. int end, mask, end1;
  807. end = start + len;
  808. tab += start >> 3;
  809. mask = 0xff << (start & 7);
  810. if ((start & ~7) == (end & ~7)) {
  811. if (start < end) {
  812. mask &= ~(0xff << (end & 7));
  813. *tab |= mask;
  814. }
  815. } else {
  816. *tab++ |= mask;
  817. start = (start + 8) & ~7;
  818. end1 = end & ~7;
  819. while (start < end1) {
  820. *tab++ = 0xff;
  821. start += 8;
  822. }
  823. if (start < end) {
  824. mask = ~(0xff << (end & 7));
  825. *tab |= mask;
  826. }
  827. }
  828. }
  829. static void build_page_bitmap(PageDesc *p)
  830. {
  831. int n, tb_start, tb_end;
  832. TranslationBlock *tb;
  833. p->code_bitmap = g_malloc0(TARGET_PAGE_SIZE / 8);
  834. tb = p->first_tb;
  835. while (tb != NULL) {
  836. n = (long)tb & 3;
  837. tb = (TranslationBlock *)((long)tb & ~3);
  838. /* NOTE: this is subtle as a TB may span two physical pages */
  839. if (n == 0) {
  840. /* NOTE: tb_end may be after the end of the page, but
  841. it is not a problem */
  842. tb_start = tb->pc & ~TARGET_PAGE_MASK;
  843. tb_end = tb_start + tb->size;
  844. if (tb_end > TARGET_PAGE_SIZE)
  845. tb_end = TARGET_PAGE_SIZE;
  846. } else {
  847. tb_start = 0;
  848. tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
  849. }
  850. set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
  851. tb = tb->page_next[n];
  852. }
  853. }
  854. TranslationBlock *tb_gen_code(CPUState *env,
  855. target_ulong pc, target_ulong cs_base,
  856. int flags, int cflags)
  857. {
  858. TranslationBlock *tb;
  859. uint8_t *tc_ptr;
  860. tb_page_addr_t phys_pc, phys_page2;
  861. target_ulong virt_page2;
  862. int code_gen_size;
  863. phys_pc = get_page_addr_code(env, pc);
  864. tb = tb_alloc(pc);
  865. if (!tb) {
  866. /* flush must be done */
  867. tb_flush(env);
  868. /* cannot fail at this point */
  869. tb = tb_alloc(pc);
  870. /* Don't forget to invalidate previous TB info. */
  871. tb_invalidated_flag = 1;
  872. }
  873. tc_ptr = code_gen_ptr;
  874. tb->tc_ptr = tc_ptr;
  875. tb->cs_base = cs_base;
  876. tb->flags = flags;
  877. tb->cflags = cflags;
  878. cpu_gen_code(env, tb, &code_gen_size);
  879. code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
  880. /* check next page if needed */
  881. virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
  882. phys_page2 = -1;
  883. if ((pc & TARGET_PAGE_MASK) != virt_page2) {
  884. phys_page2 = get_page_addr_code(env, virt_page2);
  885. }
  886. tb_link_page(tb, phys_pc, phys_page2);
  887. return tb;
  888. }
  889. /* invalidate all TBs which intersect with the target physical page
  890. starting in range [start;end[. NOTE: start and end must refer to
  891. the same physical page. 'is_cpu_write_access' should be true if called
  892. from a real cpu write access: the virtual CPU will exit the current
  893. TB if code is modified inside this TB. */
  894. void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
  895. int is_cpu_write_access)
  896. {
  897. TranslationBlock *tb, *tb_next, *saved_tb;
  898. CPUState *env = cpu_single_env;
  899. tb_page_addr_t tb_start, tb_end;
  900. PageDesc *p;
  901. int n;
  902. #ifdef TARGET_HAS_PRECISE_SMC
  903. int current_tb_not_found = is_cpu_write_access;
  904. TranslationBlock *current_tb = NULL;
  905. int current_tb_modified = 0;
  906. target_ulong current_pc = 0;
  907. target_ulong current_cs_base = 0;
  908. int current_flags = 0;
  909. #endif /* TARGET_HAS_PRECISE_SMC */
  910. p = page_find(start >> TARGET_PAGE_BITS);
  911. if (!p)
  912. return;
  913. if (!p->code_bitmap &&
  914. ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
  915. is_cpu_write_access) {
  916. /* build code bitmap */
  917. build_page_bitmap(p);
  918. }
  919. /* we remove all the TBs in the range [start, end[ */
  920. /* XXX: see if in some cases it could be faster to invalidate all the code */
  921. tb = p->first_tb;
  922. while (tb != NULL) {
  923. n = (long)tb & 3;
  924. tb = (TranslationBlock *)((long)tb & ~3);
  925. tb_next = tb->page_next[n];
  926. /* NOTE: this is subtle as a TB may span two physical pages */
  927. if (n == 0) {
  928. /* NOTE: tb_end may be after the end of the page, but
  929. it is not a problem */
  930. tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
  931. tb_end = tb_start + tb->size;
  932. } else {
  933. tb_start = tb->page_addr[1];
  934. tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
  935. }
  936. if (!(tb_end <= start || tb_start >= end)) {
  937. #ifdef TARGET_HAS_PRECISE_SMC
  938. if (current_tb_not_found) {
  939. current_tb_not_found = 0;
  940. current_tb = NULL;
  941. if (env->mem_io_pc) {
  942. /* now we have a real cpu fault */
  943. current_tb = tb_find_pc(env->mem_io_pc);
  944. }
  945. }
  946. if (current_tb == tb &&
  947. (current_tb->cflags & CF_COUNT_MASK) != 1) {
  948. /* If we are modifying the current TB, we must stop
  949. its execution. We could be more precise by checking
  950. that the modification is after the current PC, but it
  951. would require a specialized function to partially
  952. restore the CPU state */
  953. current_tb_modified = 1;
  954. cpu_restore_state(current_tb, env, env->mem_io_pc);
  955. cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
  956. &current_flags);
  957. }
  958. #endif /* TARGET_HAS_PRECISE_SMC */
  959. /* we need to do that to handle the case where a signal
  960. occurs while doing tb_phys_invalidate() */
  961. saved_tb = NULL;
  962. if (env) {
  963. saved_tb = env->current_tb;
  964. env->current_tb = NULL;
  965. }
  966. tb_phys_invalidate(tb, -1);
  967. if (env) {
  968. env->current_tb = saved_tb;
  969. if (env->interrupt_request && env->current_tb)
  970. cpu_interrupt(env, env->interrupt_request);
  971. }
  972. }
  973. tb = tb_next;
  974. }
  975. #if !defined(CONFIG_USER_ONLY)
  976. /* if no code remaining, no need to continue to use slow writes */
  977. if (!p->first_tb) {
  978. invalidate_page_bitmap(p);
  979. if (is_cpu_write_access) {
  980. tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
  981. }
  982. }
  983. #endif
  984. #ifdef TARGET_HAS_PRECISE_SMC
  985. if (current_tb_modified) {
  986. /* we generate a block containing just the instruction
  987. modifying the memory. It will ensure that it cannot modify
  988. itself */
  989. env->current_tb = NULL;
  990. tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
  991. cpu_resume_from_signal(env, NULL);
  992. }
  993. #endif
  994. }
  995. /* len must be <= 8 and start must be a multiple of len */
  996. static inline void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
  997. {
  998. PageDesc *p;
  999. int offset, b;
  1000. #if 0
  1001. if (1) {
  1002. qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
  1003. cpu_single_env->mem_io_vaddr, len,
  1004. cpu_single_env->eip,
  1005. cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
  1006. }
  1007. #endif
  1008. p = page_find(start >> TARGET_PAGE_BITS);
  1009. if (!p)
  1010. return;
  1011. if (p->code_bitmap) {
  1012. offset = start & ~TARGET_PAGE_MASK;
  1013. b = p->code_bitmap[offset >> 3] >> (offset & 7);
  1014. if (b & ((1 << len) - 1))
  1015. goto do_invalidate;
  1016. } else {
  1017. do_invalidate:
  1018. tb_invalidate_phys_page_range(start, start + len, 1);
  1019. }
  1020. }
  1021. #if !defined(CONFIG_SOFTMMU)
  1022. static void tb_invalidate_phys_page(tb_page_addr_t addr,
  1023. unsigned long pc, void *puc)
  1024. {
  1025. TranslationBlock *tb;
  1026. PageDesc *p;
  1027. int n;
  1028. #ifdef TARGET_HAS_PRECISE_SMC
  1029. TranslationBlock *current_tb = NULL;
  1030. CPUState *env = cpu_single_env;
  1031. int current_tb_modified = 0;
  1032. target_ulong current_pc = 0;
  1033. target_ulong current_cs_base = 0;
  1034. int current_flags = 0;
  1035. #endif
  1036. addr &= TARGET_PAGE_MASK;
  1037. p = page_find(addr >> TARGET_PAGE_BITS);
  1038. if (!p)
  1039. return;
  1040. tb = p->first_tb;
  1041. #ifdef TARGET_HAS_PRECISE_SMC
  1042. if (tb && pc != 0) {
  1043. current_tb = tb_find_pc(pc);
  1044. }
  1045. #endif
  1046. while (tb != NULL) {
  1047. n = (long)tb & 3;
  1048. tb = (TranslationBlock *)((long)tb & ~3);
  1049. #ifdef TARGET_HAS_PRECISE_SMC
  1050. if (current_tb == tb &&
  1051. (current_tb->cflags & CF_COUNT_MASK) != 1) {
  1052. /* If we are modifying the current TB, we must stop
  1053. its execution. We could be more precise by checking
  1054. that the modification is after the current PC, but it
  1055. would require a specialized function to partially
  1056. restore the CPU state */
  1057. current_tb_modified = 1;
  1058. cpu_restore_state(current_tb, env, pc);
  1059. cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
  1060. &current_flags);
  1061. }
  1062. #endif /* TARGET_HAS_PRECISE_SMC */
  1063. tb_phys_invalidate(tb, addr);
  1064. tb = tb->page_next[n];
  1065. }
  1066. p->first_tb = NULL;
  1067. #ifdef TARGET_HAS_PRECISE_SMC
  1068. if (current_tb_modified) {
  1069. /* we generate a block containing just the instruction
  1070. modifying the memory. It will ensure that it cannot modify
  1071. itself */
  1072. env->current_tb = NULL;
  1073. tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
  1074. cpu_resume_from_signal(env, puc);
  1075. }
  1076. #endif
  1077. }
  1078. #endif
  1079. /* add the tb in the target page and protect it if necessary */
  1080. static inline void tb_alloc_page(TranslationBlock *tb,
  1081. unsigned int n, tb_page_addr_t page_addr)
  1082. {
  1083. PageDesc *p;
  1084. #ifndef CONFIG_USER_ONLY
  1085. bool page_already_protected;
  1086. #endif
  1087. tb->page_addr[n] = page_addr;
  1088. p = page_find_alloc(page_addr >> TARGET_PAGE_BITS, 1);
  1089. tb->page_next[n] = p->first_tb;
  1090. #ifndef CONFIG_USER_ONLY
  1091. page_already_protected = p->first_tb != NULL;
  1092. #endif
  1093. p->first_tb = (TranslationBlock *)((long)tb | n);
  1094. invalidate_page_bitmap(p);
  1095. #if defined(TARGET_HAS_SMC) || 1
  1096. #if defined(CONFIG_USER_ONLY)
  1097. if (p->flags & PAGE_WRITE) {
  1098. target_ulong addr;
  1099. PageDesc *p2;
  1100. int prot;
  1101. /* force the host page as non writable (writes will have a
  1102. page fault + mprotect overhead) */
  1103. page_addr &= qemu_host_page_mask;
  1104. prot = 0;
  1105. for(addr = page_addr; addr < page_addr + qemu_host_page_size;
  1106. addr += TARGET_PAGE_SIZE) {
  1107. p2 = page_find (addr >> TARGET_PAGE_BITS);
  1108. if (!p2)
  1109. continue;
  1110. prot |= p2->flags;
  1111. p2->flags &= ~PAGE_WRITE;
  1112. }
  1113. mprotect(g2h(page_addr), qemu_host_page_size,
  1114. (prot & PAGE_BITS) & ~PAGE_WRITE);
  1115. #ifdef DEBUG_TB_INVALIDATE
  1116. printf("protecting code page: 0x" TARGET_FMT_lx "\n",
  1117. page_addr);
  1118. #endif
  1119. }
  1120. #else
  1121. /* if some code is already present, then the pages are already
  1122. protected. So we handle the case where only the first TB is
  1123. allocated in a physical page */
  1124. if (!page_already_protected) {
  1125. tlb_protect_code(page_addr);
  1126. }
  1127. #endif
  1128. #endif /* TARGET_HAS_SMC */
  1129. }
  1130. /* add a new TB and link it to the physical page tables. phys_page2 is
  1131. (-1) to indicate that only one page contains the TB. */
  1132. void tb_link_page(TranslationBlock *tb,
  1133. tb_page_addr_t phys_pc, tb_page_addr_t phys_page2)
  1134. {
  1135. unsigned int h;
  1136. TranslationBlock **ptb;
  1137. /* Grab the mmap lock to stop another thread invalidating this TB
  1138. before we are done. */
  1139. mmap_lock();
  1140. /* add in the physical hash table */
  1141. h = tb_phys_hash_func(phys_pc);
  1142. ptb = &tb_phys_hash[h];
  1143. tb->phys_hash_next = *ptb;
  1144. *ptb = tb;
  1145. /* add in the page list */
  1146. tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
  1147. if (phys_page2 != -1)
  1148. tb_alloc_page(tb, 1, phys_page2);
  1149. else
  1150. tb->page_addr[1] = -1;
  1151. tb->jmp_first = (TranslationBlock *)((long)tb | 2);
  1152. tb->jmp_next[0] = NULL;
  1153. tb->jmp_next[1] = NULL;
  1154. /* init original jump addresses */
  1155. if (tb->tb_next_offset[0] != 0xffff)
  1156. tb_reset_jump(tb, 0);
  1157. if (tb->tb_next_offset[1] != 0xffff)
  1158. tb_reset_jump(tb, 1);
  1159. #ifdef DEBUG_TB_CHECK
  1160. tb_page_check();
  1161. #endif
  1162. mmap_unlock();
  1163. }
  1164. /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
  1165. tb[1].tc_ptr. Return NULL if not found */
  1166. TranslationBlock *tb_find_pc(unsigned long tc_ptr)
  1167. {
  1168. int m_min, m_max, m;
  1169. unsigned long v;
  1170. TranslationBlock *tb;
  1171. if (nb_tbs <= 0)
  1172. return NULL;
  1173. if (tc_ptr < (unsigned long)code_gen_buffer ||
  1174. tc_ptr >= (unsigned long)code_gen_ptr)
  1175. return NULL;
  1176. /* binary search (cf Knuth) */
  1177. m_min = 0;
  1178. m_max = nb_tbs - 1;
  1179. while (m_min <= m_max) {
  1180. m = (m_min + m_max) >> 1;
  1181. tb = &tbs[m];
  1182. v = (unsigned long)tb->tc_ptr;
  1183. if (v == tc_ptr)
  1184. return tb;
  1185. else if (tc_ptr < v) {
  1186. m_max = m - 1;
  1187. } else {
  1188. m_min = m + 1;
  1189. }
  1190. }
  1191. return &tbs[m_max];
  1192. }
  1193. static void tb_reset_jump_recursive(TranslationBlock *tb);
  1194. static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
  1195. {
  1196. TranslationBlock *tb1, *tb_next, **ptb;
  1197. unsigned int n1;
  1198. tb1 = tb->jmp_next[n];
  1199. if (tb1 != NULL) {
  1200. /* find head of list */
  1201. for(;;) {
  1202. n1 = (long)tb1 & 3;
  1203. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  1204. if (n1 == 2)
  1205. break;
  1206. tb1 = tb1->jmp_next[n1];
  1207. }
  1208. /* we are now sure now that tb jumps to tb1 */
  1209. tb_next = tb1;
  1210. /* remove tb from the jmp_first list */
  1211. ptb = &tb_next->jmp_first;
  1212. for(;;) {
  1213. tb1 = *ptb;
  1214. n1 = (long)tb1 & 3;
  1215. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  1216. if (n1 == n && tb1 == tb)
  1217. break;
  1218. ptb = &tb1->jmp_next[n1];
  1219. }
  1220. *ptb = tb->jmp_next[n];
  1221. tb->jmp_next[n] = NULL;
  1222. /* suppress the jump to next tb in generated code */
  1223. tb_reset_jump(tb, n);
  1224. /* suppress jumps in the tb on which we could have jumped */
  1225. tb_reset_jump_recursive(tb_next);
  1226. }
  1227. }
  1228. static void tb_reset_jump_recursive(TranslationBlock *tb)
  1229. {
  1230. tb_reset_jump_recursive2(tb, 0);
  1231. tb_reset_jump_recursive2(tb, 1);
  1232. }
  1233. #if defined(TARGET_HAS_ICE)
  1234. #if defined(CONFIG_USER_ONLY)
  1235. static void breakpoint_invalidate(CPUState *env, target_ulong pc)
  1236. {
  1237. tb_invalidate_phys_page_range(pc, pc + 1, 0);
  1238. }
  1239. #else
  1240. static void breakpoint_invalidate(CPUState *env, target_ulong pc)
  1241. {
  1242. target_phys_addr_t addr;
  1243. target_ulong pd;
  1244. ram_addr_t ram_addr;
  1245. PhysPageDesc *p;
  1246. addr = cpu_get_phys_page_debug(env, pc);
  1247. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  1248. if (!p) {
  1249. pd = IO_MEM_UNASSIGNED;
  1250. } else {
  1251. pd = p->phys_offset;
  1252. }
  1253. ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
  1254. tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
  1255. }
  1256. #endif
  1257. #endif /* TARGET_HAS_ICE */
  1258. #if defined(CONFIG_USER_ONLY)
  1259. void cpu_watchpoint_remove_all(CPUState *env, int mask)
  1260. {
  1261. }
  1262. int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
  1263. int flags, CPUWatchpoint **watchpoint)
  1264. {
  1265. return -ENOSYS;
  1266. }
  1267. #else
  1268. /* Add a watchpoint. */
  1269. int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
  1270. int flags, CPUWatchpoint **watchpoint)
  1271. {
  1272. target_ulong len_mask = ~(len - 1);
  1273. CPUWatchpoint *wp;
  1274. /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
  1275. if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) {
  1276. fprintf(stderr, "qemu: tried to set invalid watchpoint at "
  1277. TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
  1278. return -EINVAL;
  1279. }
  1280. wp = g_malloc(sizeof(*wp));
  1281. wp->vaddr = addr;
  1282. wp->len_mask = len_mask;
  1283. wp->flags = flags;
  1284. /* keep all GDB-injected watchpoints in front */
  1285. if (flags & BP_GDB)
  1286. QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
  1287. else
  1288. QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
  1289. tlb_flush_page(env, addr);
  1290. if (watchpoint)
  1291. *watchpoint = wp;
  1292. return 0;
  1293. }
  1294. /* Remove a specific watchpoint. */
  1295. int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
  1296. int flags)
  1297. {
  1298. target_ulong len_mask = ~(len - 1);
  1299. CPUWatchpoint *wp;
  1300. QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
  1301. if (addr == wp->vaddr && len_mask == wp->len_mask
  1302. && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
  1303. cpu_watchpoint_remove_by_ref(env, wp);
  1304. return 0;
  1305. }
  1306. }
  1307. return -ENOENT;
  1308. }
  1309. /* Remove a specific watchpoint by reference. */
  1310. void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
  1311. {
  1312. QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
  1313. tlb_flush_page(env, watchpoint->vaddr);
  1314. g_free(watchpoint);
  1315. }
  1316. /* Remove all matching watchpoints. */
  1317. void cpu_watchpoint_remove_all(CPUState *env, int mask)
  1318. {
  1319. CPUWatchpoint *wp, *next;
  1320. QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
  1321. if (wp->flags & mask)
  1322. cpu_watchpoint_remove_by_ref(env, wp);
  1323. }
  1324. }
  1325. #endif
  1326. /* Add a breakpoint. */
  1327. int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
  1328. CPUBreakpoint **breakpoint)
  1329. {
  1330. #if defined(TARGET_HAS_ICE)
  1331. CPUBreakpoint *bp;
  1332. bp = g_malloc(sizeof(*bp));
  1333. bp->pc = pc;
  1334. bp->flags = flags;
  1335. /* keep all GDB-injected breakpoints in front */
  1336. if (flags & BP_GDB)
  1337. QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
  1338. else
  1339. QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
  1340. breakpoint_invalidate(env, pc);
  1341. if (breakpoint)
  1342. *breakpoint = bp;
  1343. return 0;
  1344. #else
  1345. return -ENOSYS;
  1346. #endif
  1347. }
  1348. /* Remove a specific breakpoint. */
  1349. int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags)
  1350. {
  1351. #if defined(TARGET_HAS_ICE)
  1352. CPUBreakpoint *bp;
  1353. QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
  1354. if (bp->pc == pc && bp->flags == flags) {
  1355. cpu_breakpoint_remove_by_ref(env, bp);
  1356. return 0;
  1357. }
  1358. }
  1359. return -ENOENT;
  1360. #else
  1361. return -ENOSYS;
  1362. #endif
  1363. }
  1364. /* Remove a specific breakpoint by reference. */
  1365. void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
  1366. {
  1367. #if defined(TARGET_HAS_ICE)
  1368. QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
  1369. breakpoint_invalidate(env, breakpoint->pc);
  1370. g_free(breakpoint);
  1371. #endif
  1372. }
  1373. /* Remove all matching breakpoints. */
  1374. void cpu_breakpoint_remove_all(CPUState *env, int mask)
  1375. {
  1376. #if defined(TARGET_HAS_ICE)
  1377. CPUBreakpoint *bp, *next;
  1378. QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
  1379. if (bp->flags & mask)
  1380. cpu_breakpoint_remove_by_ref(env, bp);
  1381. }
  1382. #endif
  1383. }
  1384. /* enable or disable single step mode. EXCP_DEBUG is returned by the
  1385. CPU loop after each instruction */
  1386. void cpu_single_step(CPUState *env, int enabled)
  1387. {
  1388. #if defined(TARGET_HAS_ICE)
  1389. if (env->singlestep_enabled != enabled) {
  1390. env->singlestep_enabled = enabled;
  1391. if (kvm_enabled())
  1392. kvm_update_guest_debug(env, 0);
  1393. else {
  1394. /* must flush all the translated code to avoid inconsistencies */
  1395. /* XXX: only flush what is necessary */
  1396. tb_flush(env);
  1397. }
  1398. }
  1399. #endif
  1400. }
  1401. /* enable or disable low levels log */
  1402. void cpu_set_log(int log_flags)
  1403. {
  1404. loglevel = log_flags;
  1405. if (loglevel && !logfile) {
  1406. logfile = fopen(logfilename, log_append ? "a" : "w");
  1407. if (!logfile) {
  1408. perror(logfilename);
  1409. _exit(1);
  1410. }
  1411. #if !defined(CONFIG_SOFTMMU)
  1412. /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
  1413. {
  1414. static char logfile_buf[4096];
  1415. setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
  1416. }
  1417. #elif !defined(_WIN32)
  1418. /* Win32 doesn't support line-buffering and requires size >= 2 */
  1419. setvbuf(logfile, NULL, _IOLBF, 0);
  1420. #endif
  1421. log_append = 1;
  1422. }
  1423. if (!loglevel && logfile) {
  1424. fclose(logfile);
  1425. logfile = NULL;
  1426. }
  1427. }
  1428. void cpu_set_log_filename(const char *filename)
  1429. {
  1430. logfilename = strdup(filename);
  1431. if (logfile) {
  1432. fclose(logfile);
  1433. logfile = NULL;
  1434. }
  1435. cpu_set_log(loglevel);
  1436. }
  1437. static void cpu_unlink_tb(CPUState *env)
  1438. {
  1439. /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
  1440. problem and hope the cpu will stop of its own accord. For userspace
  1441. emulation this often isn't actually as bad as it sounds. Often
  1442. signals are used primarily to interrupt blocking syscalls. */
  1443. TranslationBlock *tb;
  1444. static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
  1445. spin_lock(&interrupt_lock);
  1446. tb = env->current_tb;
  1447. /* if the cpu is currently executing code, we must unlink it and
  1448. all the potentially executing TB */
  1449. if (tb) {
  1450. env->current_tb = NULL;
  1451. tb_reset_jump_recursive(tb);
  1452. }
  1453. spin_unlock(&interrupt_lock);
  1454. }
  1455. #ifndef CONFIG_USER_ONLY
  1456. /* mask must never be zero, except for A20 change call */
  1457. static void tcg_handle_interrupt(CPUState *env, int mask)
  1458. {
  1459. int old_mask;
  1460. old_mask = env->interrupt_request;
  1461. env->interrupt_request |= mask;
  1462. /*
  1463. * If called from iothread context, wake the target cpu in
  1464. * case its halted.
  1465. */
  1466. if (!qemu_cpu_is_self(env)) {
  1467. qemu_cpu_kick(env);
  1468. return;
  1469. }
  1470. if (use_icount) {
  1471. env->icount_decr.u16.high = 0xffff;
  1472. if (!can_do_io(env)
  1473. && (mask & ~old_mask) != 0) {
  1474. cpu_abort(env, "Raised interrupt while not in I/O function");
  1475. }
  1476. } else {
  1477. cpu_unlink_tb(env);
  1478. }
  1479. }
  1480. CPUInterruptHandler cpu_interrupt_handler = tcg_handle_interrupt;
  1481. #else /* CONFIG_USER_ONLY */
  1482. void cpu_interrupt(CPUState *env, int mask)
  1483. {
  1484. env->interrupt_request |= mask;
  1485. cpu_unlink_tb(env);
  1486. }
  1487. #endif /* CONFIG_USER_ONLY */
  1488. void cpu_reset_interrupt(CPUState *env, int mask)
  1489. {
  1490. env->interrupt_request &= ~mask;
  1491. }
  1492. void cpu_exit(CPUState *env)
  1493. {
  1494. env->exit_request = 1;
  1495. cpu_unlink_tb(env);
  1496. }
  1497. const CPULogItem cpu_log_items[] = {
  1498. { CPU_LOG_TB_OUT_ASM, "out_asm",
  1499. "show generated host assembly code for each compiled TB" },
  1500. { CPU_LOG_TB_IN_ASM, "in_asm",
  1501. "show target assembly code for each compiled TB" },
  1502. { CPU_LOG_TB_OP, "op",
  1503. "show micro ops for each compiled TB" },
  1504. { CPU_LOG_TB_OP_OPT, "op_opt",
  1505. "show micro ops "
  1506. #ifdef TARGET_I386
  1507. "before eflags optimization and "
  1508. #endif
  1509. "after liveness analysis" },
  1510. { CPU_LOG_INT, "int",
  1511. "show interrupts/exceptions in short format" },
  1512. { CPU_LOG_EXEC, "exec",
  1513. "show trace before each executed TB (lots of logs)" },
  1514. { CPU_LOG_TB_CPU, "cpu",
  1515. "show CPU state before block translation" },
  1516. #ifdef TARGET_I386
  1517. { CPU_LOG_PCALL, "pcall",
  1518. "show protected mode far calls/returns/exceptions" },
  1519. { CPU_LOG_RESET, "cpu_reset",
  1520. "show CPU state before CPU resets" },
  1521. #endif
  1522. #ifdef DEBUG_IOPORT
  1523. { CPU_LOG_IOPORT, "ioport",
  1524. "show all i/o ports accesses" },
  1525. #endif
  1526. { 0, NULL, NULL },
  1527. };
  1528. #ifndef CONFIG_USER_ONLY
  1529. static QLIST_HEAD(memory_client_list, CPUPhysMemoryClient) memory_client_list
  1530. = QLIST_HEAD_INITIALIZER(memory_client_list);
  1531. static void cpu_notify_set_memory(target_phys_addr_t start_addr,
  1532. ram_addr_t size,
  1533. ram_addr_t phys_offset,
  1534. bool log_dirty)
  1535. {
  1536. CPUPhysMemoryClient *client;
  1537. QLIST_FOREACH(client, &memory_client_list, list) {
  1538. client->set_memory(client, start_addr, size, phys_offset, log_dirty);
  1539. }
  1540. }
  1541. static int cpu_notify_sync_dirty_bitmap(target_phys_addr_t start,
  1542. target_phys_addr_t end)
  1543. {
  1544. CPUPhysMemoryClient *client;
  1545. QLIST_FOREACH(client, &memory_client_list, list) {
  1546. int r = client->sync_dirty_bitmap(client, start, end);
  1547. if (r < 0)
  1548. return r;
  1549. }
  1550. return 0;
  1551. }
  1552. static int cpu_notify_migration_log(int enable)
  1553. {
  1554. CPUPhysMemoryClient *client;
  1555. QLIST_FOREACH(client, &memory_client_list, list) {
  1556. int r = client->migration_log(client, enable);
  1557. if (r < 0)
  1558. return r;
  1559. }
  1560. return 0;
  1561. }
  1562. struct last_map {
  1563. target_phys_addr_t start_addr;
  1564. ram_addr_t size;
  1565. ram_addr_t phys_offset;
  1566. };
  1567. /* The l1_phys_map provides the upper P_L1_BITs of the guest physical
  1568. * address. Each intermediate table provides the next L2_BITs of guest
  1569. * physical address space. The number of levels vary based on host and
  1570. * guest configuration, making it efficient to build the final guest
  1571. * physical address by seeding the L1 offset and shifting and adding in
  1572. * each L2 offset as we recurse through them. */
  1573. static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level,
  1574. void **lp, target_phys_addr_t addr,
  1575. struct last_map *map)
  1576. {
  1577. int i;
  1578. if (*lp == NULL) {
  1579. return;
  1580. }
  1581. if (level == 0) {
  1582. PhysPageDesc *pd = *lp;
  1583. addr <<= L2_BITS + TARGET_PAGE_BITS;
  1584. for (i = 0; i < L2_SIZE; ++i) {
  1585. if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
  1586. target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS;
  1587. if (map->size &&
  1588. start_addr == map->start_addr + map->size &&
  1589. pd[i].phys_offset == map->phys_offset + map->size) {
  1590. map->size += TARGET_PAGE_SIZE;
  1591. continue;
  1592. } else if (map->size) {
  1593. client->set_memory(client, map->start_addr,
  1594. map->size, map->phys_offset, false);
  1595. }
  1596. map->start_addr = start_addr;
  1597. map->size = TARGET_PAGE_SIZE;
  1598. map->phys_offset = pd[i].phys_offset;
  1599. }
  1600. }
  1601. } else {
  1602. void **pp = *lp;
  1603. for (i = 0; i < L2_SIZE; ++i) {
  1604. phys_page_for_each_1(client, level - 1, pp + i,
  1605. (addr << L2_BITS) | i, map);
  1606. }
  1607. }
  1608. }
  1609. static void phys_page_for_each(CPUPhysMemoryClient *client)
  1610. {
  1611. int i;
  1612. struct last_map map = { };
  1613. for (i = 0; i < P_L1_SIZE; ++i) {
  1614. phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
  1615. l1_phys_map + i, i, &map);
  1616. }
  1617. if (map.size) {
  1618. client->set_memory(client, map.start_addr, map.size, map.phys_offset,
  1619. false);
  1620. }
  1621. }
  1622. void cpu_register_phys_memory_client(CPUPhysMemoryClient *client)
  1623. {
  1624. QLIST_INSERT_HEAD(&memory_client_list, client, list);
  1625. phys_page_for_each(client);
  1626. }
  1627. void cpu_unregister_phys_memory_client(CPUPhysMemoryClient *client)
  1628. {
  1629. QLIST_REMOVE(client, list);
  1630. }
  1631. #endif
  1632. static int cmp1(const char *s1, int n, const char *s2)
  1633. {
  1634. if (strlen(s2) != n)
  1635. return 0;
  1636. return memcmp(s1, s2, n) == 0;
  1637. }
  1638. /* takes a comma separated list of log masks. Return 0 if error. */
  1639. int cpu_str_to_log_mask(const char *str)
  1640. {
  1641. const CPULogItem *item;
  1642. int mask;
  1643. const char *p, *p1;
  1644. p = str;
  1645. mask = 0;
  1646. for(;;) {
  1647. p1 = strchr(p, ',');
  1648. if (!p1)
  1649. p1 = p + strlen(p);
  1650. if(cmp1(p,p1-p,"all")) {
  1651. for(item = cpu_log_items; item->mask != 0; item++) {
  1652. mask |= item->mask;
  1653. }
  1654. } else {
  1655. for(item = cpu_log_items; item->mask != 0; item++) {
  1656. if (cmp1(p, p1 - p, item->name))
  1657. goto found;
  1658. }
  1659. return 0;
  1660. }
  1661. found:
  1662. mask |= item->mask;
  1663. if (*p1 != ',')
  1664. break;
  1665. p = p1 + 1;
  1666. }
  1667. return mask;
  1668. }
  1669. void cpu_abort(CPUState *env, const char *fmt, ...)
  1670. {
  1671. va_list ap;
  1672. va_list ap2;
  1673. va_start(ap, fmt);
  1674. va_copy(ap2, ap);
  1675. fprintf(stderr, "qemu: fatal: ");
  1676. vfprintf(stderr, fmt, ap);
  1677. fprintf(stderr, "\n");
  1678. #ifdef TARGET_I386
  1679. cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
  1680. #else
  1681. cpu_dump_state(env, stderr, fprintf, 0);
  1682. #endif
  1683. if (qemu_log_enabled()) {
  1684. qemu_log("qemu: fatal: ");
  1685. qemu_log_vprintf(fmt, ap2);
  1686. qemu_log("\n");
  1687. #ifdef TARGET_I386
  1688. log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
  1689. #else
  1690. log_cpu_state(env, 0);
  1691. #endif
  1692. qemu_log_flush();
  1693. qemu_log_close();
  1694. }
  1695. va_end(ap2);
  1696. va_end(ap);
  1697. #if defined(CONFIG_USER_ONLY)
  1698. {
  1699. struct sigaction act;
  1700. sigfillset(&act.sa_mask);
  1701. act.sa_handler = SIG_DFL;
  1702. sigaction(SIGABRT, &act, NULL);
  1703. }
  1704. #endif
  1705. abort();
  1706. }
  1707. CPUState *cpu_copy(CPUState *env)
  1708. {
  1709. CPUState *new_env = cpu_init(env->cpu_model_str);
  1710. CPUState *next_cpu = new_env->next_cpu;
  1711. int cpu_index = new_env->cpu_index;
  1712. #if defined(TARGET_HAS_ICE)
  1713. CPUBreakpoint *bp;
  1714. CPUWatchpoint *wp;
  1715. #endif
  1716. memcpy(new_env, env, sizeof(CPUState));
  1717. /* Preserve chaining and index. */
  1718. new_env->next_cpu = next_cpu;
  1719. new_env->cpu_index = cpu_index;
  1720. /* Clone all break/watchpoints.
  1721. Note: Once we support ptrace with hw-debug register access, make sure
  1722. BP_CPU break/watchpoints are handled correctly on clone. */
  1723. QTAILQ_INIT(&env->breakpoints);
  1724. QTAILQ_INIT(&env->watchpoints);
  1725. #if defined(TARGET_HAS_ICE)
  1726. QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
  1727. cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
  1728. }
  1729. QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
  1730. cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
  1731. wp->flags, NULL);
  1732. }
  1733. #endif
  1734. return new_env;
  1735. }
  1736. #if !defined(CONFIG_USER_ONLY)
  1737. static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
  1738. {
  1739. unsigned int i;
  1740. /* Discard jump cache entries for any tb which might potentially
  1741. overlap the flushed page. */
  1742. i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
  1743. memset (&env->tb_jmp_cache[i], 0,
  1744. TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
  1745. i = tb_jmp_cache_hash_page(addr);
  1746. memset (&env->tb_jmp_cache[i], 0,
  1747. TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
  1748. }
  1749. static CPUTLBEntry s_cputlb_empty_entry = {
  1750. .addr_read = -1,
  1751. .addr_write = -1,
  1752. .addr_code = -1,
  1753. .addend = -1,
  1754. };
  1755. /* NOTE: if flush_global is true, also flush global entries (not
  1756. implemented yet) */
  1757. void tlb_flush(CPUState *env, int flush_global)
  1758. {
  1759. int i;
  1760. #if defined(DEBUG_TLB)
  1761. printf("tlb_flush:\n");
  1762. #endif
  1763. /* must reset current TB so that interrupts cannot modify the
  1764. links while we are modifying them */
  1765. env->current_tb = NULL;
  1766. for(i = 0; i < CPU_TLB_SIZE; i++) {
  1767. int mmu_idx;
  1768. for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
  1769. env->tlb_table[mmu_idx][i] = s_cputlb_empty_entry;
  1770. }
  1771. }
  1772. memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
  1773. env->tlb_flush_addr = -1;
  1774. env->tlb_flush_mask = 0;
  1775. tlb_flush_count++;
  1776. }
  1777. static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
  1778. {
  1779. if (addr == (tlb_entry->addr_read &
  1780. (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
  1781. addr == (tlb_entry->addr_write &
  1782. (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
  1783. addr == (tlb_entry->addr_code &
  1784. (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
  1785. *tlb_entry = s_cputlb_empty_entry;
  1786. }
  1787. }
  1788. void tlb_flush_page(CPUState *env, target_ulong addr)
  1789. {
  1790. int i;
  1791. int mmu_idx;
  1792. #if defined(DEBUG_TLB)
  1793. printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
  1794. #endif
  1795. /* Check if we need to flush due to large pages. */
  1796. if ((addr & env->tlb_flush_mask) == env->tlb_flush_addr) {
  1797. #if defined(DEBUG_TLB)
  1798. printf("tlb_flush_page: forced full flush ("
  1799. TARGET_FMT_lx "/" TARGET_FMT_lx ")\n",
  1800. env->tlb_flush_addr, env->tlb_flush_mask);
  1801. #endif
  1802. tlb_flush(env, 1);
  1803. return;
  1804. }
  1805. /* must reset current TB so that interrupts cannot modify the
  1806. links while we are modifying them */
  1807. env->current_tb = NULL;
  1808. addr &= TARGET_PAGE_MASK;
  1809. i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  1810. for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
  1811. tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
  1812. tlb_flush_jmp_cache(env, addr);
  1813. }
  1814. /* update the TLBs so that writes to code in the virtual page 'addr'
  1815. can be detected */
  1816. static void tlb_protect_code(ram_addr_t ram_addr)
  1817. {
  1818. cpu_physical_memory_reset_dirty(ram_addr,
  1819. ram_addr + TARGET_PAGE_SIZE,
  1820. CODE_DIRTY_FLAG);
  1821. }
  1822. /* update the TLB so that writes in physical page 'phys_addr' are no longer
  1823. tested for self modifying code */
  1824. static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
  1825. target_ulong vaddr)
  1826. {
  1827. cpu_physical_memory_set_dirty_flags(ram_addr, CODE_DIRTY_FLAG);
  1828. }
  1829. static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
  1830. unsigned long start, unsigned long length)
  1831. {
  1832. unsigned long addr;
  1833. if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
  1834. addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
  1835. if ((addr - start) < length) {
  1836. tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
  1837. }
  1838. }
  1839. }
  1840. /* Note: start and end must be within the same ram block. */
  1841. void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
  1842. int dirty_flags)
  1843. {
  1844. CPUState *env;
  1845. unsigned long length, start1;
  1846. int i;
  1847. start &= TARGET_PAGE_MASK;
  1848. end = TARGET_PAGE_ALIGN(end);
  1849. length = end - start;
  1850. if (length == 0)
  1851. return;
  1852. cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
  1853. /* we modify the TLB cache so that the dirty bit will be set again
  1854. when accessing the range */
  1855. start1 = (unsigned long)qemu_safe_ram_ptr(start);
  1856. /* Check that we don't span multiple blocks - this breaks the
  1857. address comparisons below. */
  1858. if ((unsigned long)qemu_safe_ram_ptr(end - 1) - start1
  1859. != (end - 1) - start) {
  1860. abort();
  1861. }
  1862. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  1863. int mmu_idx;
  1864. for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
  1865. for(i = 0; i < CPU_TLB_SIZE; i++)
  1866. tlb_reset_dirty_range(&env->tlb_table[mmu_idx][i],
  1867. start1, length);
  1868. }
  1869. }
  1870. }
  1871. int cpu_physical_memory_set_dirty_tracking(int enable)
  1872. {
  1873. int ret = 0;
  1874. in_migration = enable;
  1875. ret = cpu_notify_migration_log(!!enable);
  1876. return ret;
  1877. }
  1878. int cpu_physical_memory_get_dirty_tracking(void)
  1879. {
  1880. return in_migration;
  1881. }
  1882. int cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
  1883. target_phys_addr_t end_addr)
  1884. {
  1885. int ret;
  1886. ret = cpu_notify_sync_dirty_bitmap(start_addr, end_addr);
  1887. return ret;
  1888. }
  1889. int cpu_physical_log_start(target_phys_addr_t start_addr,
  1890. ram_addr_t size)
  1891. {
  1892. CPUPhysMemoryClient *client;
  1893. QLIST_FOREACH(client, &memory_client_list, list) {
  1894. if (client->log_start) {
  1895. int r = client->log_start(client, start_addr, size);
  1896. if (r < 0) {
  1897. return r;
  1898. }
  1899. }
  1900. }
  1901. return 0;
  1902. }
  1903. int cpu_physical_log_stop(target_phys_addr_t start_addr,
  1904. ram_addr_t size)
  1905. {
  1906. CPUPhysMemoryClient *client;
  1907. QLIST_FOREACH(client, &memory_client_list, list) {
  1908. if (client->log_stop) {
  1909. int r = client->log_stop(client, start_addr, size);
  1910. if (r < 0) {
  1911. return r;
  1912. }
  1913. }
  1914. }
  1915. return 0;
  1916. }
  1917. static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
  1918. {
  1919. ram_addr_t ram_addr;
  1920. void *p;
  1921. if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
  1922. p = (void *)(unsigned long)((tlb_entry->addr_write & TARGET_PAGE_MASK)
  1923. + tlb_entry->addend);
  1924. ram_addr = qemu_ram_addr_from_host_nofail(p);
  1925. if (!cpu_physical_memory_is_dirty(ram_addr)) {
  1926. tlb_entry->addr_write |= TLB_NOTDIRTY;
  1927. }
  1928. }
  1929. }
  1930. /* update the TLB according to the current state of the dirty bits */
  1931. void cpu_tlb_update_dirty(CPUState *env)
  1932. {
  1933. int i;
  1934. int mmu_idx;
  1935. for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
  1936. for(i = 0; i < CPU_TLB_SIZE; i++)
  1937. tlb_update_dirty(&env->tlb_table[mmu_idx][i]);
  1938. }
  1939. }
  1940. static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
  1941. {
  1942. if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
  1943. tlb_entry->addr_write = vaddr;
  1944. }
  1945. /* update the TLB corresponding to virtual page vaddr
  1946. so that it is no longer dirty */
  1947. static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
  1948. {
  1949. int i;
  1950. int mmu_idx;
  1951. vaddr &= TARGET_PAGE_MASK;
  1952. i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  1953. for (mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++)
  1954. tlb_set_dirty1(&env->tlb_table[mmu_idx][i], vaddr);
  1955. }
  1956. /* Our TLB does not support large pages, so remember the area covered by
  1957. large pages and trigger a full TLB flush if these are invalidated. */
  1958. static void tlb_add_large_page(CPUState *env, target_ulong vaddr,
  1959. target_ulong size)
  1960. {
  1961. target_ulong mask = ~(size - 1);
  1962. if (env->tlb_flush_addr == (target_ulong)-1) {
  1963. env->tlb_flush_addr = vaddr & mask;
  1964. env->tlb_flush_mask = mask;
  1965. return;
  1966. }
  1967. /* Extend the existing region to include the new page.
  1968. This is a compromise between unnecessary flushes and the cost
  1969. of maintaining a full variable size TLB. */
  1970. mask &= env->tlb_flush_mask;
  1971. while (((env->tlb_flush_addr ^ vaddr) & mask) != 0) {
  1972. mask <<= 1;
  1973. }
  1974. env->tlb_flush_addr &= mask;
  1975. env->tlb_flush_mask = mask;
  1976. }
  1977. /* Add a new TLB entry. At most one entry for a given virtual address
  1978. is permitted. Only a single TARGET_PAGE_SIZE region is mapped, the
  1979. supplied size is only used by tlb_flush_page. */
  1980. void tlb_set_page(CPUState *env, target_ulong vaddr,
  1981. target_phys_addr_t paddr, int prot,
  1982. int mmu_idx, target_ulong size)
  1983. {
  1984. PhysPageDesc *p;
  1985. unsigned long pd;
  1986. unsigned int index;
  1987. target_ulong address;
  1988. target_ulong code_address;
  1989. unsigned long addend;
  1990. CPUTLBEntry *te;
  1991. CPUWatchpoint *wp;
  1992. target_phys_addr_t iotlb;
  1993. assert(size >= TARGET_PAGE_SIZE);
  1994. if (size != TARGET_PAGE_SIZE) {
  1995. tlb_add_large_page(env, vaddr, size);
  1996. }
  1997. p = phys_page_find(paddr >> TARGET_PAGE_BITS);
  1998. if (!p) {
  1999. pd = IO_MEM_UNASSIGNED;
  2000. } else {
  2001. pd = p->phys_offset;
  2002. }
  2003. #if defined(DEBUG_TLB)
  2004. printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x" TARGET_FMT_plx
  2005. " prot=%x idx=%d pd=0x%08lx\n",
  2006. vaddr, paddr, prot, mmu_idx, pd);
  2007. #endif
  2008. address = vaddr;
  2009. if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
  2010. /* IO memory case (romd handled later) */
  2011. address |= TLB_MMIO;
  2012. }
  2013. addend = (unsigned long)qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
  2014. if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
  2015. /* Normal RAM. */
  2016. iotlb = pd & TARGET_PAGE_MASK;
  2017. if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
  2018. iotlb |= IO_MEM_NOTDIRTY;
  2019. else
  2020. iotlb |= IO_MEM_ROM;
  2021. } else {
  2022. /* IO handlers are currently passed a physical address.
  2023. It would be nice to pass an offset from the base address
  2024. of that region. This would avoid having to special case RAM,
  2025. and avoid full address decoding in every device.
  2026. We can't use the high bits of pd for this because
  2027. IO_MEM_ROMD uses these as a ram address. */
  2028. iotlb = (pd & ~TARGET_PAGE_MASK);
  2029. if (p) {
  2030. iotlb += p->region_offset;
  2031. } else {
  2032. iotlb += paddr;
  2033. }
  2034. }
  2035. code_address = address;
  2036. /* Make accesses to pages with watchpoints go via the
  2037. watchpoint trap routines. */
  2038. QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
  2039. if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
  2040. /* Avoid trapping reads of pages with a write breakpoint. */
  2041. if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
  2042. iotlb = io_mem_watch + paddr;
  2043. address |= TLB_MMIO;
  2044. break;
  2045. }
  2046. }
  2047. }
  2048. index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  2049. env->iotlb[mmu_idx][index] = iotlb - vaddr;
  2050. te = &env->tlb_table[mmu_idx][index];
  2051. te->addend = addend - vaddr;
  2052. if (prot & PAGE_READ) {
  2053. te->addr_read = address;
  2054. } else {
  2055. te->addr_read = -1;
  2056. }
  2057. if (prot & PAGE_EXEC) {
  2058. te->addr_code = code_address;
  2059. } else {
  2060. te->addr_code = -1;
  2061. }
  2062. if (prot & PAGE_WRITE) {
  2063. if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
  2064. (pd & IO_MEM_ROMD)) {
  2065. /* Write access calls the I/O callback. */
  2066. te->addr_write = address | TLB_MMIO;
  2067. } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
  2068. !cpu_physical_memory_is_dirty(pd)) {
  2069. te->addr_write = address | TLB_NOTDIRTY;
  2070. } else {
  2071. te->addr_write = address;
  2072. }
  2073. } else {
  2074. te->addr_write = -1;
  2075. }
  2076. }
  2077. #else
  2078. void tlb_flush(CPUState *env, int flush_global)
  2079. {
  2080. }
  2081. void tlb_flush_page(CPUState *env, target_ulong addr)
  2082. {
  2083. }
  2084. /*
  2085. * Walks guest process memory "regions" one by one
  2086. * and calls callback function 'fn' for each region.
  2087. */
  2088. struct walk_memory_regions_data
  2089. {
  2090. walk_memory_regions_fn fn;
  2091. void *priv;
  2092. unsigned long start;
  2093. int prot;
  2094. };
  2095. static int walk_memory_regions_end(struct walk_memory_regions_data *data,
  2096. abi_ulong end, int new_prot)
  2097. {
  2098. if (data->start != -1ul) {
  2099. int rc = data->fn(data->priv, data->start, end, data->prot);
  2100. if (rc != 0) {
  2101. return rc;
  2102. }
  2103. }
  2104. data->start = (new_prot ? end : -1ul);
  2105. data->prot = new_prot;
  2106. return 0;
  2107. }
  2108. static int walk_memory_regions_1(struct walk_memory_regions_data *data,
  2109. abi_ulong base, int level, void **lp)
  2110. {
  2111. abi_ulong pa;
  2112. int i, rc;
  2113. if (*lp == NULL) {
  2114. return walk_memory_regions_end(data, base, 0);
  2115. }
  2116. if (level == 0) {
  2117. PageDesc *pd = *lp;
  2118. for (i = 0; i < L2_SIZE; ++i) {
  2119. int prot = pd[i].flags;
  2120. pa = base | (i << TARGET_PAGE_BITS);
  2121. if (prot != data->prot) {
  2122. rc = walk_memory_regions_end(data, pa, prot);
  2123. if (rc != 0) {
  2124. return rc;
  2125. }
  2126. }
  2127. }
  2128. } else {
  2129. void **pp = *lp;
  2130. for (i = 0; i < L2_SIZE; ++i) {
  2131. pa = base | ((abi_ulong)i <<
  2132. (TARGET_PAGE_BITS + L2_BITS * level));
  2133. rc = walk_memory_regions_1(data, pa, level - 1, pp + i);
  2134. if (rc != 0) {
  2135. return rc;
  2136. }
  2137. }
  2138. }
  2139. return 0;
  2140. }
  2141. int walk_memory_regions(void *priv, walk_memory_regions_fn fn)
  2142. {
  2143. struct walk_memory_regions_data data;
  2144. unsigned long i;
  2145. data.fn = fn;
  2146. data.priv = priv;
  2147. data.start = -1ul;
  2148. data.prot = 0;
  2149. for (i = 0; i < V_L1_SIZE; i++) {
  2150. int rc = walk_memory_regions_1(&data, (abi_ulong)i << V_L1_SHIFT,
  2151. V_L1_SHIFT / L2_BITS - 1, l1_map + i);
  2152. if (rc != 0) {
  2153. return rc;
  2154. }
  2155. }
  2156. return walk_memory_regions_end(&data, 0, 0);
  2157. }
  2158. static int dump_region(void *priv, abi_ulong start,
  2159. abi_ulong end, unsigned long prot)
  2160. {
  2161. FILE *f = (FILE *)priv;
  2162. (void) fprintf(f, TARGET_ABI_FMT_lx"-"TARGET_ABI_FMT_lx
  2163. " "TARGET_ABI_FMT_lx" %c%c%c\n",
  2164. start, end, end - start,
  2165. ((prot & PAGE_READ) ? 'r' : '-'),
  2166. ((prot & PAGE_WRITE) ? 'w' : '-'),
  2167. ((prot & PAGE_EXEC) ? 'x' : '-'));
  2168. return (0);
  2169. }
  2170. /* dump memory mappings */
  2171. void page_dump(FILE *f)
  2172. {
  2173. (void) fprintf(f, "%-8s %-8s %-8s %s\n",
  2174. "start", "end", "size", "prot");
  2175. walk_memory_regions(f, dump_region);
  2176. }
  2177. int page_get_flags(target_ulong address)
  2178. {
  2179. PageDesc *p;
  2180. p = page_find(address >> TARGET_PAGE_BITS);
  2181. if (!p)
  2182. return 0;
  2183. return p->flags;
  2184. }
  2185. /* Modify the flags of a page and invalidate the code if necessary.
  2186. The flag PAGE_WRITE_ORG is positioned automatically depending
  2187. on PAGE_WRITE. The mmap_lock should already be held. */
  2188. void page_set_flags(target_ulong start, target_ulong end, int flags)
  2189. {
  2190. target_ulong addr, len;
  2191. /* This function should never be called with addresses outside the
  2192. guest address space. If this assert fires, it probably indicates
  2193. a missing call to h2g_valid. */
  2194. #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
  2195. assert(end < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
  2196. #endif
  2197. assert(start < end);
  2198. start = start & TARGET_PAGE_MASK;
  2199. end = TARGET_PAGE_ALIGN(end);
  2200. if (flags & PAGE_WRITE) {
  2201. flags |= PAGE_WRITE_ORG;
  2202. }
  2203. for (addr = start, len = end - start;
  2204. len != 0;
  2205. len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
  2206. PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
  2207. /* If the write protection bit is set, then we invalidate
  2208. the code inside. */
  2209. if (!(p->flags & PAGE_WRITE) &&
  2210. (flags & PAGE_WRITE) &&
  2211. p->first_tb) {
  2212. tb_invalidate_phys_page(addr, 0, NULL);
  2213. }
  2214. p->flags = flags;
  2215. }
  2216. }
  2217. int page_check_range(target_ulong start, target_ulong len, int flags)
  2218. {
  2219. PageDesc *p;
  2220. target_ulong end;
  2221. target_ulong addr;
  2222. /* This function should never be called with addresses outside the
  2223. guest address space. If this assert fires, it probably indicates
  2224. a missing call to h2g_valid. */
  2225. #if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
  2226. assert(start < ((abi_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
  2227. #endif
  2228. if (len == 0) {
  2229. return 0;
  2230. }
  2231. if (start + len - 1 < start) {
  2232. /* We've wrapped around. */
  2233. return -1;
  2234. }
  2235. end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
  2236. start = start & TARGET_PAGE_MASK;
  2237. for (addr = start, len = end - start;
  2238. len != 0;
  2239. len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
  2240. p = page_find(addr >> TARGET_PAGE_BITS);
  2241. if( !p )
  2242. return -1;
  2243. if( !(p->flags & PAGE_VALID) )
  2244. return -1;
  2245. if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
  2246. return -1;
  2247. if (flags & PAGE_WRITE) {
  2248. if (!(p->flags & PAGE_WRITE_ORG))
  2249. return -1;
  2250. /* unprotect the page if it was put read-only because it
  2251. contains translated code */
  2252. if (!(p->flags & PAGE_WRITE)) {
  2253. if (!page_unprotect(addr, 0, NULL))
  2254. return -1;
  2255. }
  2256. return 0;
  2257. }
  2258. }
  2259. return 0;
  2260. }
  2261. /* called from signal handler: invalidate the code and unprotect the
  2262. page. Return TRUE if the fault was successfully handled. */
  2263. int page_unprotect(target_ulong address, unsigned long pc, void *puc)
  2264. {
  2265. unsigned int prot;
  2266. PageDesc *p;
  2267. target_ulong host_start, host_end, addr;
  2268. /* Technically this isn't safe inside a signal handler. However we
  2269. know this only ever happens in a synchronous SEGV handler, so in
  2270. practice it seems to be ok. */
  2271. mmap_lock();
  2272. p = page_find(address >> TARGET_PAGE_BITS);
  2273. if (!p) {
  2274. mmap_unlock();
  2275. return 0;
  2276. }
  2277. /* if the page was really writable, then we change its
  2278. protection back to writable */
  2279. if ((p->flags & PAGE_WRITE_ORG) && !(p->flags & PAGE_WRITE)) {
  2280. host_start = address & qemu_host_page_mask;
  2281. host_end = host_start + qemu_host_page_size;
  2282. prot = 0;
  2283. for (addr = host_start ; addr < host_end ; addr += TARGET_PAGE_SIZE) {
  2284. p = page_find(addr >> TARGET_PAGE_BITS);
  2285. p->flags |= PAGE_WRITE;
  2286. prot |= p->flags;
  2287. /* and since the content will be modified, we must invalidate
  2288. the corresponding translated code. */
  2289. tb_invalidate_phys_page(addr, pc, puc);
  2290. #ifdef DEBUG_TB_CHECK
  2291. tb_invalidate_check(addr);
  2292. #endif
  2293. }
  2294. mprotect((void *)g2h(host_start), qemu_host_page_size,
  2295. prot & PAGE_BITS);
  2296. mmap_unlock();
  2297. return 1;
  2298. }
  2299. mmap_unlock();
  2300. return 0;
  2301. }
  2302. static inline void tlb_set_dirty(CPUState *env,
  2303. unsigned long addr, target_ulong vaddr)
  2304. {
  2305. }
  2306. #endif /* defined(CONFIG_USER_ONLY) */
  2307. #if !defined(CONFIG_USER_ONLY)
  2308. #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
  2309. typedef struct subpage_t {
  2310. target_phys_addr_t base;
  2311. ram_addr_t sub_io_index[TARGET_PAGE_SIZE];
  2312. ram_addr_t region_offset[TARGET_PAGE_SIZE];
  2313. } subpage_t;
  2314. static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
  2315. ram_addr_t memory, ram_addr_t region_offset);
  2316. static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
  2317. ram_addr_t orig_memory,
  2318. ram_addr_t region_offset);
  2319. #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
  2320. need_subpage) \
  2321. do { \
  2322. if (addr > start_addr) \
  2323. start_addr2 = 0; \
  2324. else { \
  2325. start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
  2326. if (start_addr2 > 0) \
  2327. need_subpage = 1; \
  2328. } \
  2329. \
  2330. if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
  2331. end_addr2 = TARGET_PAGE_SIZE - 1; \
  2332. else { \
  2333. end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
  2334. if (end_addr2 < TARGET_PAGE_SIZE - 1) \
  2335. need_subpage = 1; \
  2336. } \
  2337. } while (0)
  2338. /* register physical memory.
  2339. For RAM, 'size' must be a multiple of the target page size.
  2340. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
  2341. io memory page. The address used when calling the IO function is
  2342. the offset from the start of the region, plus region_offset. Both
  2343. start_addr and region_offset are rounded down to a page boundary
  2344. before calculating this offset. This should not be a problem unless
  2345. the low bits of start_addr and region_offset differ. */
  2346. void cpu_register_physical_memory_log(target_phys_addr_t start_addr,
  2347. ram_addr_t size,
  2348. ram_addr_t phys_offset,
  2349. ram_addr_t region_offset,
  2350. bool log_dirty)
  2351. {
  2352. target_phys_addr_t addr, end_addr;
  2353. PhysPageDesc *p;
  2354. CPUState *env;
  2355. ram_addr_t orig_size = size;
  2356. subpage_t *subpage;
  2357. assert(size);
  2358. cpu_notify_set_memory(start_addr, size, phys_offset, log_dirty);
  2359. if (phys_offset == IO_MEM_UNASSIGNED) {
  2360. region_offset = start_addr;
  2361. }
  2362. region_offset &= TARGET_PAGE_MASK;
  2363. size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
  2364. end_addr = start_addr + (target_phys_addr_t)size;
  2365. addr = start_addr;
  2366. do {
  2367. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  2368. if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
  2369. ram_addr_t orig_memory = p->phys_offset;
  2370. target_phys_addr_t start_addr2, end_addr2;
  2371. int need_subpage = 0;
  2372. CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
  2373. need_subpage);
  2374. if (need_subpage) {
  2375. if (!(orig_memory & IO_MEM_SUBPAGE)) {
  2376. subpage = subpage_init((addr & TARGET_PAGE_MASK),
  2377. &p->phys_offset, orig_memory,
  2378. p->region_offset);
  2379. } else {
  2380. subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
  2381. >> IO_MEM_SHIFT];
  2382. }
  2383. subpage_register(subpage, start_addr2, end_addr2, phys_offset,
  2384. region_offset);
  2385. p->region_offset = 0;
  2386. } else {
  2387. p->phys_offset = phys_offset;
  2388. if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
  2389. (phys_offset & IO_MEM_ROMD))
  2390. phys_offset += TARGET_PAGE_SIZE;
  2391. }
  2392. } else {
  2393. p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
  2394. p->phys_offset = phys_offset;
  2395. p->region_offset = region_offset;
  2396. if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
  2397. (phys_offset & IO_MEM_ROMD)) {
  2398. phys_offset += TARGET_PAGE_SIZE;
  2399. } else {
  2400. target_phys_addr_t start_addr2, end_addr2;
  2401. int need_subpage = 0;
  2402. CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
  2403. end_addr2, need_subpage);
  2404. if (need_subpage) {
  2405. subpage = subpage_init((addr & TARGET_PAGE_MASK),
  2406. &p->phys_offset, IO_MEM_UNASSIGNED,
  2407. addr & TARGET_PAGE_MASK);
  2408. subpage_register(subpage, start_addr2, end_addr2,
  2409. phys_offset, region_offset);
  2410. p->region_offset = 0;
  2411. }
  2412. }
  2413. }
  2414. region_offset += TARGET_PAGE_SIZE;
  2415. addr += TARGET_PAGE_SIZE;
  2416. } while (addr != end_addr);
  2417. /* since each CPU stores ram addresses in its TLB cache, we must
  2418. reset the modified entries */
  2419. /* XXX: slow ! */
  2420. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  2421. tlb_flush(env, 1);
  2422. }
  2423. }
  2424. /* XXX: temporary until new memory mapping API */
  2425. ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
  2426. {
  2427. PhysPageDesc *p;
  2428. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  2429. if (!p)
  2430. return IO_MEM_UNASSIGNED;
  2431. return p->phys_offset;
  2432. }
  2433. void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
  2434. {
  2435. if (kvm_enabled())
  2436. kvm_coalesce_mmio_region(addr, size);
  2437. }
  2438. void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
  2439. {
  2440. if (kvm_enabled())
  2441. kvm_uncoalesce_mmio_region(addr, size);
  2442. }
  2443. void qemu_flush_coalesced_mmio_buffer(void)
  2444. {
  2445. if (kvm_enabled())
  2446. kvm_flush_coalesced_mmio_buffer();
  2447. }
  2448. #if defined(__linux__) && !defined(TARGET_S390X)
  2449. #include <sys/vfs.h>
  2450. #define HUGETLBFS_MAGIC 0x958458f6
  2451. static long gethugepagesize(const char *path)
  2452. {
  2453. struct statfs fs;
  2454. int ret;
  2455. do {
  2456. ret = statfs(path, &fs);
  2457. } while (ret != 0 && errno == EINTR);
  2458. if (ret != 0) {
  2459. perror(path);
  2460. return 0;
  2461. }
  2462. if (fs.f_type != HUGETLBFS_MAGIC)
  2463. fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
  2464. return fs.f_bsize;
  2465. }
  2466. static void *file_ram_alloc(RAMBlock *block,
  2467. ram_addr_t memory,
  2468. const char *path)
  2469. {
  2470. char *filename;
  2471. void *area;
  2472. int fd;
  2473. #ifdef MAP_POPULATE
  2474. int flags;
  2475. #endif
  2476. unsigned long hpagesize;
  2477. hpagesize = gethugepagesize(path);
  2478. if (!hpagesize) {
  2479. return NULL;
  2480. }
  2481. if (memory < hpagesize) {
  2482. return NULL;
  2483. }
  2484. if (kvm_enabled() && !kvm_has_sync_mmu()) {
  2485. fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
  2486. return NULL;
  2487. }
  2488. if (asprintf(&filename, "%s/qemu_back_mem.XXXXXX", path) == -1) {
  2489. return NULL;
  2490. }
  2491. fd = mkstemp(filename);
  2492. if (fd < 0) {
  2493. perror("unable to create backing store for hugepages");
  2494. free(filename);
  2495. return NULL;
  2496. }
  2497. unlink(filename);
  2498. free(filename);
  2499. memory = (memory+hpagesize-1) & ~(hpagesize-1);
  2500. /*
  2501. * ftruncate is not supported by hugetlbfs in older
  2502. * hosts, so don't bother bailing out on errors.
  2503. * If anything goes wrong with it under other filesystems,
  2504. * mmap will fail.
  2505. */
  2506. if (ftruncate(fd, memory))
  2507. perror("ftruncate");
  2508. #ifdef MAP_POPULATE
  2509. /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
  2510. * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
  2511. * to sidestep this quirk.
  2512. */
  2513. flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
  2514. area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
  2515. #else
  2516. area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  2517. #endif
  2518. if (area == MAP_FAILED) {
  2519. perror("file_ram_alloc: can't mmap RAM pages");
  2520. close(fd);
  2521. return (NULL);
  2522. }
  2523. block->fd = fd;
  2524. return area;
  2525. }
  2526. #endif
  2527. static ram_addr_t find_ram_offset(ram_addr_t size)
  2528. {
  2529. RAMBlock *block, *next_block;
  2530. ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
  2531. if (QLIST_EMPTY(&ram_list.blocks))
  2532. return 0;
  2533. QLIST_FOREACH(block, &ram_list.blocks, next) {
  2534. ram_addr_t end, next = RAM_ADDR_MAX;
  2535. end = block->offset + block->length;
  2536. QLIST_FOREACH(next_block, &ram_list.blocks, next) {
  2537. if (next_block->offset >= end) {
  2538. next = MIN(next, next_block->offset);
  2539. }
  2540. }
  2541. if (next - end >= size && next - end < mingap) {
  2542. offset = end;
  2543. mingap = next - end;
  2544. }
  2545. }
  2546. if (offset == RAM_ADDR_MAX) {
  2547. fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
  2548. (uint64_t)size);
  2549. abort();
  2550. }
  2551. return offset;
  2552. }
  2553. static ram_addr_t last_ram_offset(void)
  2554. {
  2555. RAMBlock *block;
  2556. ram_addr_t last = 0;
  2557. QLIST_FOREACH(block, &ram_list.blocks, next)
  2558. last = MAX(last, block->offset + block->length);
  2559. return last;
  2560. }
  2561. ram_addr_t qemu_ram_alloc_from_ptr(DeviceState *dev, const char *name,
  2562. ram_addr_t size, void *host)
  2563. {
  2564. RAMBlock *new_block, *block;
  2565. size = TARGET_PAGE_ALIGN(size);
  2566. new_block = g_malloc0(sizeof(*new_block));
  2567. if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
  2568. char *id = dev->parent_bus->info->get_dev_path(dev);
  2569. if (id) {
  2570. snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
  2571. g_free(id);
  2572. }
  2573. }
  2574. pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
  2575. QLIST_FOREACH(block, &ram_list.blocks, next) {
  2576. if (!strcmp(block->idstr, new_block->idstr)) {
  2577. fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
  2578. new_block->idstr);
  2579. abort();
  2580. }
  2581. }
  2582. new_block->offset = find_ram_offset(size);
  2583. if (host) {
  2584. new_block->host = host;
  2585. new_block->flags |= RAM_PREALLOC_MASK;
  2586. } else {
  2587. if (mem_path) {
  2588. #if defined (__linux__) && !defined(TARGET_S390X)
  2589. new_block->host = file_ram_alloc(new_block, size, mem_path);
  2590. if (!new_block->host) {
  2591. new_block->host = qemu_vmalloc(size);
  2592. qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
  2593. }
  2594. #else
  2595. fprintf(stderr, "-mem-path option unsupported\n");
  2596. exit(1);
  2597. #endif
  2598. } else {
  2599. #if defined(TARGET_S390X) && defined(CONFIG_KVM)
  2600. /* S390 KVM requires the topmost vma of the RAM to be smaller than
  2601. an system defined value, which is at least 256GB. Larger systems
  2602. have larger values. We put the guest between the end of data
  2603. segment (system break) and this value. We use 32GB as a base to
  2604. have enough room for the system break to grow. */
  2605. new_block->host = mmap((void*)0x800000000, size,
  2606. PROT_EXEC|PROT_READ|PROT_WRITE,
  2607. MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  2608. if (new_block->host == MAP_FAILED) {
  2609. fprintf(stderr, "Allocating RAM failed\n");
  2610. abort();
  2611. }
  2612. #else
  2613. if (xen_enabled()) {
  2614. xen_ram_alloc(new_block->offset, size);
  2615. } else {
  2616. new_block->host = qemu_vmalloc(size);
  2617. }
  2618. #endif
  2619. qemu_madvise(new_block->host, size, QEMU_MADV_MERGEABLE);
  2620. }
  2621. }
  2622. new_block->length = size;
  2623. QLIST_INSERT_HEAD(&ram_list.blocks, new_block, next);
  2624. ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
  2625. last_ram_offset() >> TARGET_PAGE_BITS);
  2626. memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
  2627. 0xff, size >> TARGET_PAGE_BITS);
  2628. if (kvm_enabled())
  2629. kvm_setup_guest_memory(new_block->host, size);
  2630. return new_block->offset;
  2631. }
  2632. ram_addr_t qemu_ram_alloc(DeviceState *dev, const char *name, ram_addr_t size)
  2633. {
  2634. return qemu_ram_alloc_from_ptr(dev, name, size, NULL);
  2635. }
  2636. void qemu_ram_free_from_ptr(ram_addr_t addr)
  2637. {
  2638. RAMBlock *block;
  2639. QLIST_FOREACH(block, &ram_list.blocks, next) {
  2640. if (addr == block->offset) {
  2641. QLIST_REMOVE(block, next);
  2642. g_free(block);
  2643. return;
  2644. }
  2645. }
  2646. }
  2647. void qemu_ram_free(ram_addr_t addr)
  2648. {
  2649. RAMBlock *block;
  2650. QLIST_FOREACH(block, &ram_list.blocks, next) {
  2651. if (addr == block->offset) {
  2652. QLIST_REMOVE(block, next);
  2653. if (block->flags & RAM_PREALLOC_MASK) {
  2654. ;
  2655. } else if (mem_path) {
  2656. #if defined (__linux__) && !defined(TARGET_S390X)
  2657. if (block->fd) {
  2658. munmap(block->host, block->length);
  2659. close(block->fd);
  2660. } else {
  2661. qemu_vfree(block->host);
  2662. }
  2663. #else
  2664. abort();
  2665. #endif
  2666. } else {
  2667. #if defined(TARGET_S390X) && defined(CONFIG_KVM)
  2668. munmap(block->host, block->length);
  2669. #else
  2670. if (xen_enabled()) {
  2671. xen_invalidate_map_cache_entry(block->host);
  2672. } else {
  2673. qemu_vfree(block->host);
  2674. }
  2675. #endif
  2676. }
  2677. g_free(block);
  2678. return;
  2679. }
  2680. }
  2681. }
  2682. #ifndef _WIN32
  2683. void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
  2684. {
  2685. RAMBlock *block;
  2686. ram_addr_t offset;
  2687. int flags;
  2688. void *area, *vaddr;
  2689. QLIST_FOREACH(block, &ram_list.blocks, next) {
  2690. offset = addr - block->offset;
  2691. if (offset < block->length) {
  2692. vaddr = block->host + offset;
  2693. if (block->flags & RAM_PREALLOC_MASK) {
  2694. ;
  2695. } else {
  2696. flags = MAP_FIXED;
  2697. munmap(vaddr, length);
  2698. if (mem_path) {
  2699. #if defined(__linux__) && !defined(TARGET_S390X)
  2700. if (block->fd) {
  2701. #ifdef MAP_POPULATE
  2702. flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED :
  2703. MAP_PRIVATE;
  2704. #else
  2705. flags |= MAP_PRIVATE;
  2706. #endif
  2707. area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
  2708. flags, block->fd, offset);
  2709. } else {
  2710. flags |= MAP_PRIVATE | MAP_ANONYMOUS;
  2711. area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
  2712. flags, -1, 0);
  2713. }
  2714. #else
  2715. abort();
  2716. #endif
  2717. } else {
  2718. #if defined(TARGET_S390X) && defined(CONFIG_KVM)
  2719. flags |= MAP_SHARED | MAP_ANONYMOUS;
  2720. area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE,
  2721. flags, -1, 0);
  2722. #else
  2723. flags |= MAP_PRIVATE | MAP_ANONYMOUS;
  2724. area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
  2725. flags, -1, 0);
  2726. #endif
  2727. }
  2728. if (area != vaddr) {
  2729. fprintf(stderr, "Could not remap addr: "
  2730. RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
  2731. length, addr);
  2732. exit(1);
  2733. }
  2734. qemu_madvise(vaddr, length, QEMU_MADV_MERGEABLE);
  2735. }
  2736. return;
  2737. }
  2738. }
  2739. }
  2740. #endif /* !_WIN32 */
  2741. /* Return a host pointer to ram allocated with qemu_ram_alloc.
  2742. With the exception of the softmmu code in this file, this should
  2743. only be used for local memory (e.g. video ram) that the device owns,
  2744. and knows it isn't going to access beyond the end of the block.
  2745. It should not be used for general purpose DMA.
  2746. Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
  2747. */
  2748. void *qemu_get_ram_ptr(ram_addr_t addr)
  2749. {
  2750. RAMBlock *block;
  2751. QLIST_FOREACH(block, &ram_list.blocks, next) {
  2752. if (addr - block->offset < block->length) {
  2753. /* Move this entry to to start of the list. */
  2754. if (block != QLIST_FIRST(&ram_list.blocks)) {
  2755. QLIST_REMOVE(block, next);
  2756. QLIST_INSERT_HEAD(&ram_list.blocks, block, next);
  2757. }
  2758. if (xen_enabled()) {
  2759. /* We need to check if the requested address is in the RAM
  2760. * because we don't want to map the entire memory in QEMU.
  2761. * In that case just map until the end of the page.
  2762. */
  2763. if (block->offset == 0) {
  2764. return xen_map_cache(addr, 0, 0);
  2765. } else if (block->host == NULL) {
  2766. block->host =
  2767. xen_map_cache(block->offset, block->length, 1);
  2768. }
  2769. }
  2770. return block->host + (addr - block->offset);
  2771. }
  2772. }
  2773. fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
  2774. abort();
  2775. return NULL;
  2776. }
  2777. /* Return a host pointer to ram allocated with qemu_ram_alloc.
  2778. * Same as qemu_get_ram_ptr but avoid reordering ramblocks.
  2779. */
  2780. void *qemu_safe_ram_ptr(ram_addr_t addr)
  2781. {
  2782. RAMBlock *block;
  2783. QLIST_FOREACH(block, &ram_list.blocks, next) {
  2784. if (addr - block->offset < block->length) {
  2785. if (xen_enabled()) {
  2786. /* We need to check if the requested address is in the RAM
  2787. * because we don't want to map the entire memory in QEMU.
  2788. * In that case just map until the end of the page.
  2789. */
  2790. if (block->offset == 0) {
  2791. return xen_map_cache(addr, 0, 0);
  2792. } else if (block->host == NULL) {
  2793. block->host =
  2794. xen_map_cache(block->offset, block->length, 1);
  2795. }
  2796. }
  2797. return block->host + (addr - block->offset);
  2798. }
  2799. }
  2800. fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
  2801. abort();
  2802. return NULL;
  2803. }
  2804. /* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
  2805. * but takes a size argument */
  2806. void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
  2807. {
  2808. if (*size == 0) {
  2809. return NULL;
  2810. }
  2811. if (xen_enabled()) {
  2812. return xen_map_cache(addr, *size, 1);
  2813. } else {
  2814. RAMBlock *block;
  2815. QLIST_FOREACH(block, &ram_list.blocks, next) {
  2816. if (addr - block->offset < block->length) {
  2817. if (addr - block->offset + *size > block->length)
  2818. *size = block->length - addr + block->offset;
  2819. return block->host + (addr - block->offset);
  2820. }
  2821. }
  2822. fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
  2823. abort();
  2824. }
  2825. }
  2826. void qemu_put_ram_ptr(void *addr)
  2827. {
  2828. trace_qemu_put_ram_ptr(addr);
  2829. }
  2830. int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
  2831. {
  2832. RAMBlock *block;
  2833. uint8_t *host = ptr;
  2834. if (xen_enabled()) {
  2835. *ram_addr = xen_ram_addr_from_mapcache(ptr);
  2836. return 0;
  2837. }
  2838. QLIST_FOREACH(block, &ram_list.blocks, next) {
  2839. /* This case append when the block is not mapped. */
  2840. if (block->host == NULL) {
  2841. continue;
  2842. }
  2843. if (host - block->host < block->length) {
  2844. *ram_addr = block->offset + (host - block->host);
  2845. return 0;
  2846. }
  2847. }
  2848. return -1;
  2849. }
  2850. /* Some of the softmmu routines need to translate from a host pointer
  2851. (typically a TLB entry) back to a ram offset. */
  2852. ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
  2853. {
  2854. ram_addr_t ram_addr;
  2855. if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
  2856. fprintf(stderr, "Bad ram pointer %p\n", ptr);
  2857. abort();
  2858. }
  2859. return ram_addr;
  2860. }
  2861. static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
  2862. {
  2863. #ifdef DEBUG_UNASSIGNED
  2864. printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
  2865. #endif
  2866. #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
  2867. cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 1);
  2868. #endif
  2869. return 0;
  2870. }
  2871. static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
  2872. {
  2873. #ifdef DEBUG_UNASSIGNED
  2874. printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
  2875. #endif
  2876. #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
  2877. cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 2);
  2878. #endif
  2879. return 0;
  2880. }
  2881. static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
  2882. {
  2883. #ifdef DEBUG_UNASSIGNED
  2884. printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
  2885. #endif
  2886. #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
  2887. cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, 4);
  2888. #endif
  2889. return 0;
  2890. }
  2891. static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  2892. {
  2893. #ifdef DEBUG_UNASSIGNED
  2894. printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
  2895. #endif
  2896. #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
  2897. cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 1);
  2898. #endif
  2899. }
  2900. static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
  2901. {
  2902. #ifdef DEBUG_UNASSIGNED
  2903. printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
  2904. #endif
  2905. #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
  2906. cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 2);
  2907. #endif
  2908. }
  2909. static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  2910. {
  2911. #ifdef DEBUG_UNASSIGNED
  2912. printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
  2913. #endif
  2914. #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
  2915. cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, 4);
  2916. #endif
  2917. }
  2918. static CPUReadMemoryFunc * const unassigned_mem_read[3] = {
  2919. unassigned_mem_readb,
  2920. unassigned_mem_readw,
  2921. unassigned_mem_readl,
  2922. };
  2923. static CPUWriteMemoryFunc * const unassigned_mem_write[3] = {
  2924. unassigned_mem_writeb,
  2925. unassigned_mem_writew,
  2926. unassigned_mem_writel,
  2927. };
  2928. static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
  2929. uint32_t val)
  2930. {
  2931. int dirty_flags;
  2932. dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
  2933. if (!(dirty_flags & CODE_DIRTY_FLAG)) {
  2934. #if !defined(CONFIG_USER_ONLY)
  2935. tb_invalidate_phys_page_fast(ram_addr, 1);
  2936. dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
  2937. #endif
  2938. }
  2939. stb_p(qemu_get_ram_ptr(ram_addr), val);
  2940. dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
  2941. cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
  2942. /* we remove the notdirty callback only if the code has been
  2943. flushed */
  2944. if (dirty_flags == 0xff)
  2945. tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
  2946. }
  2947. static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
  2948. uint32_t val)
  2949. {
  2950. int dirty_flags;
  2951. dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
  2952. if (!(dirty_flags & CODE_DIRTY_FLAG)) {
  2953. #if !defined(CONFIG_USER_ONLY)
  2954. tb_invalidate_phys_page_fast(ram_addr, 2);
  2955. dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
  2956. #endif
  2957. }
  2958. stw_p(qemu_get_ram_ptr(ram_addr), val);
  2959. dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
  2960. cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
  2961. /* we remove the notdirty callback only if the code has been
  2962. flushed */
  2963. if (dirty_flags == 0xff)
  2964. tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
  2965. }
  2966. static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
  2967. uint32_t val)
  2968. {
  2969. int dirty_flags;
  2970. dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
  2971. if (!(dirty_flags & CODE_DIRTY_FLAG)) {
  2972. #if !defined(CONFIG_USER_ONLY)
  2973. tb_invalidate_phys_page_fast(ram_addr, 4);
  2974. dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
  2975. #endif
  2976. }
  2977. stl_p(qemu_get_ram_ptr(ram_addr), val);
  2978. dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
  2979. cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
  2980. /* we remove the notdirty callback only if the code has been
  2981. flushed */
  2982. if (dirty_flags == 0xff)
  2983. tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
  2984. }
  2985. static CPUReadMemoryFunc * const error_mem_read[3] = {
  2986. NULL, /* never used */
  2987. NULL, /* never used */
  2988. NULL, /* never used */
  2989. };
  2990. static CPUWriteMemoryFunc * const notdirty_mem_write[3] = {
  2991. notdirty_mem_writeb,
  2992. notdirty_mem_writew,
  2993. notdirty_mem_writel,
  2994. };
  2995. /* Generate a debug exception if a watchpoint has been hit. */
  2996. static void check_watchpoint(int offset, int len_mask, int flags)
  2997. {
  2998. CPUState *env = cpu_single_env;
  2999. target_ulong pc, cs_base;
  3000. TranslationBlock *tb;
  3001. target_ulong vaddr;
  3002. CPUWatchpoint *wp;
  3003. int cpu_flags;
  3004. if (env->watchpoint_hit) {
  3005. /* We re-entered the check after replacing the TB. Now raise
  3006. * the debug interrupt so that is will trigger after the
  3007. * current instruction. */
  3008. cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
  3009. return;
  3010. }
  3011. vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
  3012. QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
  3013. if ((vaddr == (wp->vaddr & len_mask) ||
  3014. (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
  3015. wp->flags |= BP_WATCHPOINT_HIT;
  3016. if (!env->watchpoint_hit) {
  3017. env->watchpoint_hit = wp;
  3018. tb = tb_find_pc(env->mem_io_pc);
  3019. if (!tb) {
  3020. cpu_abort(env, "check_watchpoint: could not find TB for "
  3021. "pc=%p", (void *)env->mem_io_pc);
  3022. }
  3023. cpu_restore_state(tb, env, env->mem_io_pc);
  3024. tb_phys_invalidate(tb, -1);
  3025. if (wp->flags & BP_STOP_BEFORE_ACCESS) {
  3026. env->exception_index = EXCP_DEBUG;
  3027. } else {
  3028. cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
  3029. tb_gen_code(env, pc, cs_base, cpu_flags, 1);
  3030. }
  3031. cpu_resume_from_signal(env, NULL);
  3032. }
  3033. } else {
  3034. wp->flags &= ~BP_WATCHPOINT_HIT;
  3035. }
  3036. }
  3037. }
  3038. /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
  3039. so these check for a hit then pass through to the normal out-of-line
  3040. phys routines. */
  3041. static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
  3042. {
  3043. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
  3044. return ldub_phys(addr);
  3045. }
  3046. static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
  3047. {
  3048. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
  3049. return lduw_phys(addr);
  3050. }
  3051. static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
  3052. {
  3053. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
  3054. return ldl_phys(addr);
  3055. }
  3056. static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
  3057. uint32_t val)
  3058. {
  3059. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
  3060. stb_phys(addr, val);
  3061. }
  3062. static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
  3063. uint32_t val)
  3064. {
  3065. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
  3066. stw_phys(addr, val);
  3067. }
  3068. static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
  3069. uint32_t val)
  3070. {
  3071. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
  3072. stl_phys(addr, val);
  3073. }
  3074. static CPUReadMemoryFunc * const watch_mem_read[3] = {
  3075. watch_mem_readb,
  3076. watch_mem_readw,
  3077. watch_mem_readl,
  3078. };
  3079. static CPUWriteMemoryFunc * const watch_mem_write[3] = {
  3080. watch_mem_writeb,
  3081. watch_mem_writew,
  3082. watch_mem_writel,
  3083. };
  3084. static inline uint32_t subpage_readlen (subpage_t *mmio,
  3085. target_phys_addr_t addr,
  3086. unsigned int len)
  3087. {
  3088. unsigned int idx = SUBPAGE_IDX(addr);
  3089. #if defined(DEBUG_SUBPAGE)
  3090. printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
  3091. mmio, len, addr, idx);
  3092. #endif
  3093. addr += mmio->region_offset[idx];
  3094. idx = mmio->sub_io_index[idx];
  3095. return io_mem_read[idx][len](io_mem_opaque[idx], addr);
  3096. }
  3097. static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
  3098. uint32_t value, unsigned int len)
  3099. {
  3100. unsigned int idx = SUBPAGE_IDX(addr);
  3101. #if defined(DEBUG_SUBPAGE)
  3102. printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n",
  3103. __func__, mmio, len, addr, idx, value);
  3104. #endif
  3105. addr += mmio->region_offset[idx];
  3106. idx = mmio->sub_io_index[idx];
  3107. io_mem_write[idx][len](io_mem_opaque[idx], addr, value);
  3108. }
  3109. static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
  3110. {
  3111. return subpage_readlen(opaque, addr, 0);
  3112. }
  3113. static void subpage_writeb (void *opaque, target_phys_addr_t addr,
  3114. uint32_t value)
  3115. {
  3116. subpage_writelen(opaque, addr, value, 0);
  3117. }
  3118. static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
  3119. {
  3120. return subpage_readlen(opaque, addr, 1);
  3121. }
  3122. static void subpage_writew (void *opaque, target_phys_addr_t addr,
  3123. uint32_t value)
  3124. {
  3125. subpage_writelen(opaque, addr, value, 1);
  3126. }
  3127. static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
  3128. {
  3129. return subpage_readlen(opaque, addr, 2);
  3130. }
  3131. static void subpage_writel (void *opaque, target_phys_addr_t addr,
  3132. uint32_t value)
  3133. {
  3134. subpage_writelen(opaque, addr, value, 2);
  3135. }
  3136. static CPUReadMemoryFunc * const subpage_read[] = {
  3137. &subpage_readb,
  3138. &subpage_readw,
  3139. &subpage_readl,
  3140. };
  3141. static CPUWriteMemoryFunc * const subpage_write[] = {
  3142. &subpage_writeb,
  3143. &subpage_writew,
  3144. &subpage_writel,
  3145. };
  3146. static uint32_t subpage_ram_readb(void *opaque, target_phys_addr_t addr)
  3147. {
  3148. ram_addr_t raddr = addr;
  3149. void *ptr = qemu_get_ram_ptr(raddr);
  3150. return ldub_p(ptr);
  3151. }
  3152. static void subpage_ram_writeb(void *opaque, target_phys_addr_t addr,
  3153. uint32_t value)
  3154. {
  3155. ram_addr_t raddr = addr;
  3156. void *ptr = qemu_get_ram_ptr(raddr);
  3157. stb_p(ptr, value);
  3158. }
  3159. static uint32_t subpage_ram_readw(void *opaque, target_phys_addr_t addr)
  3160. {
  3161. ram_addr_t raddr = addr;
  3162. void *ptr = qemu_get_ram_ptr(raddr);
  3163. return lduw_p(ptr);
  3164. }
  3165. static void subpage_ram_writew(void *opaque, target_phys_addr_t addr,
  3166. uint32_t value)
  3167. {
  3168. ram_addr_t raddr = addr;
  3169. void *ptr = qemu_get_ram_ptr(raddr);
  3170. stw_p(ptr, value);
  3171. }
  3172. static uint32_t subpage_ram_readl(void *opaque, target_phys_addr_t addr)
  3173. {
  3174. ram_addr_t raddr = addr;
  3175. void *ptr = qemu_get_ram_ptr(raddr);
  3176. return ldl_p(ptr);
  3177. }
  3178. static void subpage_ram_writel(void *opaque, target_phys_addr_t addr,
  3179. uint32_t value)
  3180. {
  3181. ram_addr_t raddr = addr;
  3182. void *ptr = qemu_get_ram_ptr(raddr);
  3183. stl_p(ptr, value);
  3184. }
  3185. static CPUReadMemoryFunc * const subpage_ram_read[] = {
  3186. &subpage_ram_readb,
  3187. &subpage_ram_readw,
  3188. &subpage_ram_readl,
  3189. };
  3190. static CPUWriteMemoryFunc * const subpage_ram_write[] = {
  3191. &subpage_ram_writeb,
  3192. &subpage_ram_writew,
  3193. &subpage_ram_writel,
  3194. };
  3195. static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
  3196. ram_addr_t memory, ram_addr_t region_offset)
  3197. {
  3198. int idx, eidx;
  3199. if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
  3200. return -1;
  3201. idx = SUBPAGE_IDX(start);
  3202. eidx = SUBPAGE_IDX(end);
  3203. #if defined(DEBUG_SUBPAGE)
  3204. printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
  3205. mmio, start, end, idx, eidx, memory);
  3206. #endif
  3207. if ((memory & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
  3208. memory = IO_MEM_SUBPAGE_RAM;
  3209. }
  3210. memory = (memory >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  3211. for (; idx <= eidx; idx++) {
  3212. mmio->sub_io_index[idx] = memory;
  3213. mmio->region_offset[idx] = region_offset;
  3214. }
  3215. return 0;
  3216. }
  3217. static subpage_t *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
  3218. ram_addr_t orig_memory,
  3219. ram_addr_t region_offset)
  3220. {
  3221. subpage_t *mmio;
  3222. int subpage_memory;
  3223. mmio = g_malloc0(sizeof(subpage_t));
  3224. mmio->base = base;
  3225. subpage_memory = cpu_register_io_memory(subpage_read, subpage_write, mmio,
  3226. DEVICE_NATIVE_ENDIAN);
  3227. #if defined(DEBUG_SUBPAGE)
  3228. printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
  3229. mmio, base, TARGET_PAGE_SIZE, subpage_memory);
  3230. #endif
  3231. *phys = subpage_memory | IO_MEM_SUBPAGE;
  3232. subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, orig_memory, region_offset);
  3233. return mmio;
  3234. }
  3235. static int get_free_io_mem_idx(void)
  3236. {
  3237. int i;
  3238. for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
  3239. if (!io_mem_used[i]) {
  3240. io_mem_used[i] = 1;
  3241. return i;
  3242. }
  3243. fprintf(stderr, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES);
  3244. return -1;
  3245. }
  3246. /*
  3247. * Usually, devices operate in little endian mode. There are devices out
  3248. * there that operate in big endian too. Each device gets byte swapped
  3249. * mmio if plugged onto a CPU that does the other endianness.
  3250. *
  3251. * CPU Device swap?
  3252. *
  3253. * little little no
  3254. * little big yes
  3255. * big little yes
  3256. * big big no
  3257. */
  3258. typedef struct SwapEndianContainer {
  3259. CPUReadMemoryFunc *read[3];
  3260. CPUWriteMemoryFunc *write[3];
  3261. void *opaque;
  3262. } SwapEndianContainer;
  3263. static uint32_t swapendian_mem_readb (void *opaque, target_phys_addr_t addr)
  3264. {
  3265. uint32_t val;
  3266. SwapEndianContainer *c = opaque;
  3267. val = c->read[0](c->opaque, addr);
  3268. return val;
  3269. }
  3270. static uint32_t swapendian_mem_readw(void *opaque, target_phys_addr_t addr)
  3271. {
  3272. uint32_t val;
  3273. SwapEndianContainer *c = opaque;
  3274. val = bswap16(c->read[1](c->opaque, addr));
  3275. return val;
  3276. }
  3277. static uint32_t swapendian_mem_readl(void *opaque, target_phys_addr_t addr)
  3278. {
  3279. uint32_t val;
  3280. SwapEndianContainer *c = opaque;
  3281. val = bswap32(c->read[2](c->opaque, addr));
  3282. return val;
  3283. }
  3284. static CPUReadMemoryFunc * const swapendian_readfn[3]={
  3285. swapendian_mem_readb,
  3286. swapendian_mem_readw,
  3287. swapendian_mem_readl
  3288. };
  3289. static void swapendian_mem_writeb(void *opaque, target_phys_addr_t addr,
  3290. uint32_t val)
  3291. {
  3292. SwapEndianContainer *c = opaque;
  3293. c->write[0](c->opaque, addr, val);
  3294. }
  3295. static void swapendian_mem_writew(void *opaque, target_phys_addr_t addr,
  3296. uint32_t val)
  3297. {
  3298. SwapEndianContainer *c = opaque;
  3299. c->write[1](c->opaque, addr, bswap16(val));
  3300. }
  3301. static void swapendian_mem_writel(void *opaque, target_phys_addr_t addr,
  3302. uint32_t val)
  3303. {
  3304. SwapEndianContainer *c = opaque;
  3305. c->write[2](c->opaque, addr, bswap32(val));
  3306. }
  3307. static CPUWriteMemoryFunc * const swapendian_writefn[3]={
  3308. swapendian_mem_writeb,
  3309. swapendian_mem_writew,
  3310. swapendian_mem_writel
  3311. };
  3312. static void swapendian_init(int io_index)
  3313. {
  3314. SwapEndianContainer *c = g_malloc(sizeof(SwapEndianContainer));
  3315. int i;
  3316. /* Swap mmio for big endian targets */
  3317. c->opaque = io_mem_opaque[io_index];
  3318. for (i = 0; i < 3; i++) {
  3319. c->read[i] = io_mem_read[io_index][i];
  3320. c->write[i] = io_mem_write[io_index][i];
  3321. io_mem_read[io_index][i] = swapendian_readfn[i];
  3322. io_mem_write[io_index][i] = swapendian_writefn[i];
  3323. }
  3324. io_mem_opaque[io_index] = c;
  3325. }
  3326. static void swapendian_del(int io_index)
  3327. {
  3328. if (io_mem_read[io_index][0] == swapendian_readfn[0]) {
  3329. g_free(io_mem_opaque[io_index]);
  3330. }
  3331. }
  3332. /* mem_read and mem_write are arrays of functions containing the
  3333. function to access byte (index 0), word (index 1) and dword (index
  3334. 2). Functions can be omitted with a NULL function pointer.
  3335. If io_index is non zero, the corresponding io zone is
  3336. modified. If it is zero, a new io zone is allocated. The return
  3337. value can be used with cpu_register_physical_memory(). (-1) is
  3338. returned if error. */
  3339. static int cpu_register_io_memory_fixed(int io_index,
  3340. CPUReadMemoryFunc * const *mem_read,
  3341. CPUWriteMemoryFunc * const *mem_write,
  3342. void *opaque, enum device_endian endian)
  3343. {
  3344. int i;
  3345. if (io_index <= 0) {
  3346. io_index = get_free_io_mem_idx();
  3347. if (io_index == -1)
  3348. return io_index;
  3349. } else {
  3350. io_index >>= IO_MEM_SHIFT;
  3351. if (io_index >= IO_MEM_NB_ENTRIES)
  3352. return -1;
  3353. }
  3354. for (i = 0; i < 3; ++i) {
  3355. io_mem_read[io_index][i]
  3356. = (mem_read[i] ? mem_read[i] : unassigned_mem_read[i]);
  3357. }
  3358. for (i = 0; i < 3; ++i) {
  3359. io_mem_write[io_index][i]
  3360. = (mem_write[i] ? mem_write[i] : unassigned_mem_write[i]);
  3361. }
  3362. io_mem_opaque[io_index] = opaque;
  3363. switch (endian) {
  3364. case DEVICE_BIG_ENDIAN:
  3365. #ifndef TARGET_WORDS_BIGENDIAN
  3366. swapendian_init(io_index);
  3367. #endif
  3368. break;
  3369. case DEVICE_LITTLE_ENDIAN:
  3370. #ifdef TARGET_WORDS_BIGENDIAN
  3371. swapendian_init(io_index);
  3372. #endif
  3373. break;
  3374. case DEVICE_NATIVE_ENDIAN:
  3375. default:
  3376. break;
  3377. }
  3378. return (io_index << IO_MEM_SHIFT);
  3379. }
  3380. int cpu_register_io_memory(CPUReadMemoryFunc * const *mem_read,
  3381. CPUWriteMemoryFunc * const *mem_write,
  3382. void *opaque, enum device_endian endian)
  3383. {
  3384. return cpu_register_io_memory_fixed(0, mem_read, mem_write, opaque, endian);
  3385. }
  3386. void cpu_unregister_io_memory(int io_table_address)
  3387. {
  3388. int i;
  3389. int io_index = io_table_address >> IO_MEM_SHIFT;
  3390. swapendian_del(io_index);
  3391. for (i=0;i < 3; i++) {
  3392. io_mem_read[io_index][i] = unassigned_mem_read[i];
  3393. io_mem_write[io_index][i] = unassigned_mem_write[i];
  3394. }
  3395. io_mem_opaque[io_index] = NULL;
  3396. io_mem_used[io_index] = 0;
  3397. }
  3398. static void io_mem_init(void)
  3399. {
  3400. int i;
  3401. cpu_register_io_memory_fixed(IO_MEM_ROM, error_mem_read,
  3402. unassigned_mem_write, NULL,
  3403. DEVICE_NATIVE_ENDIAN);
  3404. cpu_register_io_memory_fixed(IO_MEM_UNASSIGNED, unassigned_mem_read,
  3405. unassigned_mem_write, NULL,
  3406. DEVICE_NATIVE_ENDIAN);
  3407. cpu_register_io_memory_fixed(IO_MEM_NOTDIRTY, error_mem_read,
  3408. notdirty_mem_write, NULL,
  3409. DEVICE_NATIVE_ENDIAN);
  3410. cpu_register_io_memory_fixed(IO_MEM_SUBPAGE_RAM, subpage_ram_read,
  3411. subpage_ram_write, NULL,
  3412. DEVICE_NATIVE_ENDIAN);
  3413. for (i=0; i<5; i++)
  3414. io_mem_used[i] = 1;
  3415. io_mem_watch = cpu_register_io_memory(watch_mem_read,
  3416. watch_mem_write, NULL,
  3417. DEVICE_NATIVE_ENDIAN);
  3418. }
  3419. static void memory_map_init(void)
  3420. {
  3421. system_memory = g_malloc(sizeof(*system_memory));
  3422. memory_region_init(system_memory, "system", INT64_MAX);
  3423. set_system_memory_map(system_memory);
  3424. system_io = g_malloc(sizeof(*system_io));
  3425. memory_region_init(system_io, "io", 65536);
  3426. set_system_io_map(system_io);
  3427. }
  3428. MemoryRegion *get_system_memory(void)
  3429. {
  3430. return system_memory;
  3431. }
  3432. MemoryRegion *get_system_io(void)
  3433. {
  3434. return system_io;
  3435. }
  3436. #endif /* !defined(CONFIG_USER_ONLY) */
  3437. /* physical memory access (slow version, mainly for debug) */
  3438. #if defined(CONFIG_USER_ONLY)
  3439. int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
  3440. uint8_t *buf, int len, int is_write)
  3441. {
  3442. int l, flags;
  3443. target_ulong page;
  3444. void * p;
  3445. while (len > 0) {
  3446. page = addr & TARGET_PAGE_MASK;
  3447. l = (page + TARGET_PAGE_SIZE) - addr;
  3448. if (l > len)
  3449. l = len;
  3450. flags = page_get_flags(page);
  3451. if (!(flags & PAGE_VALID))
  3452. return -1;
  3453. if (is_write) {
  3454. if (!(flags & PAGE_WRITE))
  3455. return -1;
  3456. /* XXX: this code should not depend on lock_user */
  3457. if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
  3458. return -1;
  3459. memcpy(p, buf, l);
  3460. unlock_user(p, addr, l);
  3461. } else {
  3462. if (!(flags & PAGE_READ))
  3463. return -1;
  3464. /* XXX: this code should not depend on lock_user */
  3465. if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
  3466. return -1;
  3467. memcpy(buf, p, l);
  3468. unlock_user(p, addr, 0);
  3469. }
  3470. len -= l;
  3471. buf += l;
  3472. addr += l;
  3473. }
  3474. return 0;
  3475. }
  3476. #else
  3477. void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
  3478. int len, int is_write)
  3479. {
  3480. int l, io_index;
  3481. uint8_t *ptr;
  3482. uint32_t val;
  3483. target_phys_addr_t page;
  3484. ram_addr_t pd;
  3485. PhysPageDesc *p;
  3486. while (len > 0) {
  3487. page = addr & TARGET_PAGE_MASK;
  3488. l = (page + TARGET_PAGE_SIZE) - addr;
  3489. if (l > len)
  3490. l = len;
  3491. p = phys_page_find(page >> TARGET_PAGE_BITS);
  3492. if (!p) {
  3493. pd = IO_MEM_UNASSIGNED;
  3494. } else {
  3495. pd = p->phys_offset;
  3496. }
  3497. if (is_write) {
  3498. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  3499. target_phys_addr_t addr1 = addr;
  3500. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  3501. if (p)
  3502. addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  3503. /* XXX: could force cpu_single_env to NULL to avoid
  3504. potential bugs */
  3505. if (l >= 4 && ((addr1 & 3) == 0)) {
  3506. /* 32 bit write access */
  3507. val = ldl_p(buf);
  3508. io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
  3509. l = 4;
  3510. } else if (l >= 2 && ((addr1 & 1) == 0)) {
  3511. /* 16 bit write access */
  3512. val = lduw_p(buf);
  3513. io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
  3514. l = 2;
  3515. } else {
  3516. /* 8 bit write access */
  3517. val = ldub_p(buf);
  3518. io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
  3519. l = 1;
  3520. }
  3521. } else {
  3522. ram_addr_t addr1;
  3523. addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  3524. /* RAM case */
  3525. ptr = qemu_get_ram_ptr(addr1);
  3526. memcpy(ptr, buf, l);
  3527. if (!cpu_physical_memory_is_dirty(addr1)) {
  3528. /* invalidate code */
  3529. tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
  3530. /* set dirty bit */
  3531. cpu_physical_memory_set_dirty_flags(
  3532. addr1, (0xff & ~CODE_DIRTY_FLAG));
  3533. }
  3534. qemu_put_ram_ptr(ptr);
  3535. }
  3536. } else {
  3537. if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
  3538. !(pd & IO_MEM_ROMD)) {
  3539. target_phys_addr_t addr1 = addr;
  3540. /* I/O case */
  3541. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  3542. if (p)
  3543. addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  3544. if (l >= 4 && ((addr1 & 3) == 0)) {
  3545. /* 32 bit read access */
  3546. val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
  3547. stl_p(buf, val);
  3548. l = 4;
  3549. } else if (l >= 2 && ((addr1 & 1) == 0)) {
  3550. /* 16 bit read access */
  3551. val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
  3552. stw_p(buf, val);
  3553. l = 2;
  3554. } else {
  3555. /* 8 bit read access */
  3556. val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
  3557. stb_p(buf, val);
  3558. l = 1;
  3559. }
  3560. } else {
  3561. /* RAM case */
  3562. ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK);
  3563. memcpy(buf, ptr + (addr & ~TARGET_PAGE_MASK), l);
  3564. qemu_put_ram_ptr(ptr);
  3565. }
  3566. }
  3567. len -= l;
  3568. buf += l;
  3569. addr += l;
  3570. }
  3571. }
  3572. /* used for ROM loading : can write in RAM and ROM */
  3573. void cpu_physical_memory_write_rom(target_phys_addr_t addr,
  3574. const uint8_t *buf, int len)
  3575. {
  3576. int l;
  3577. uint8_t *ptr;
  3578. target_phys_addr_t page;
  3579. unsigned long pd;
  3580. PhysPageDesc *p;
  3581. while (len > 0) {
  3582. page = addr & TARGET_PAGE_MASK;
  3583. l = (page + TARGET_PAGE_SIZE) - addr;
  3584. if (l > len)
  3585. l = len;
  3586. p = phys_page_find(page >> TARGET_PAGE_BITS);
  3587. if (!p) {
  3588. pd = IO_MEM_UNASSIGNED;
  3589. } else {
  3590. pd = p->phys_offset;
  3591. }
  3592. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
  3593. (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
  3594. !(pd & IO_MEM_ROMD)) {
  3595. /* do nothing */
  3596. } else {
  3597. unsigned long addr1;
  3598. addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  3599. /* ROM/RAM case */
  3600. ptr = qemu_get_ram_ptr(addr1);
  3601. memcpy(ptr, buf, l);
  3602. qemu_put_ram_ptr(ptr);
  3603. }
  3604. len -= l;
  3605. buf += l;
  3606. addr += l;
  3607. }
  3608. }
  3609. typedef struct {
  3610. void *buffer;
  3611. target_phys_addr_t addr;
  3612. target_phys_addr_t len;
  3613. } BounceBuffer;
  3614. static BounceBuffer bounce;
  3615. typedef struct MapClient {
  3616. void *opaque;
  3617. void (*callback)(void *opaque);
  3618. QLIST_ENTRY(MapClient) link;
  3619. } MapClient;
  3620. static QLIST_HEAD(map_client_list, MapClient) map_client_list
  3621. = QLIST_HEAD_INITIALIZER(map_client_list);
  3622. void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
  3623. {
  3624. MapClient *client = g_malloc(sizeof(*client));
  3625. client->opaque = opaque;
  3626. client->callback = callback;
  3627. QLIST_INSERT_HEAD(&map_client_list, client, link);
  3628. return client;
  3629. }
  3630. void cpu_unregister_map_client(void *_client)
  3631. {
  3632. MapClient *client = (MapClient *)_client;
  3633. QLIST_REMOVE(client, link);
  3634. g_free(client);
  3635. }
  3636. static void cpu_notify_map_clients(void)
  3637. {
  3638. MapClient *client;
  3639. while (!QLIST_EMPTY(&map_client_list)) {
  3640. client = QLIST_FIRST(&map_client_list);
  3641. client->callback(client->opaque);
  3642. cpu_unregister_map_client(client);
  3643. }
  3644. }
  3645. /* Map a physical memory region into a host virtual address.
  3646. * May map a subset of the requested range, given by and returned in *plen.
  3647. * May return NULL if resources needed to perform the mapping are exhausted.
  3648. * Use only for reads OR writes - not for read-modify-write operations.
  3649. * Use cpu_register_map_client() to know when retrying the map operation is
  3650. * likely to succeed.
  3651. */
  3652. void *cpu_physical_memory_map(target_phys_addr_t addr,
  3653. target_phys_addr_t *plen,
  3654. int is_write)
  3655. {
  3656. target_phys_addr_t len = *plen;
  3657. target_phys_addr_t todo = 0;
  3658. int l;
  3659. target_phys_addr_t page;
  3660. unsigned long pd;
  3661. PhysPageDesc *p;
  3662. ram_addr_t raddr = RAM_ADDR_MAX;
  3663. ram_addr_t rlen;
  3664. void *ret;
  3665. while (len > 0) {
  3666. page = addr & TARGET_PAGE_MASK;
  3667. l = (page + TARGET_PAGE_SIZE) - addr;
  3668. if (l > len)
  3669. l = len;
  3670. p = phys_page_find(page >> TARGET_PAGE_BITS);
  3671. if (!p) {
  3672. pd = IO_MEM_UNASSIGNED;
  3673. } else {
  3674. pd = p->phys_offset;
  3675. }
  3676. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  3677. if (todo || bounce.buffer) {
  3678. break;
  3679. }
  3680. bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
  3681. bounce.addr = addr;
  3682. bounce.len = l;
  3683. if (!is_write) {
  3684. cpu_physical_memory_read(addr, bounce.buffer, l);
  3685. }
  3686. *plen = l;
  3687. return bounce.buffer;
  3688. }
  3689. if (!todo) {
  3690. raddr = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  3691. }
  3692. len -= l;
  3693. addr += l;
  3694. todo += l;
  3695. }
  3696. rlen = todo;
  3697. ret = qemu_ram_ptr_length(raddr, &rlen);
  3698. *plen = rlen;
  3699. return ret;
  3700. }
  3701. /* Unmaps a memory region previously mapped by cpu_physical_memory_map().
  3702. * Will also mark the memory as dirty if is_write == 1. access_len gives
  3703. * the amount of memory that was actually read or written by the caller.
  3704. */
  3705. void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
  3706. int is_write, target_phys_addr_t access_len)
  3707. {
  3708. if (buffer != bounce.buffer) {
  3709. if (is_write) {
  3710. ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
  3711. while (access_len) {
  3712. unsigned l;
  3713. l = TARGET_PAGE_SIZE;
  3714. if (l > access_len)
  3715. l = access_len;
  3716. if (!cpu_physical_memory_is_dirty(addr1)) {
  3717. /* invalidate code */
  3718. tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
  3719. /* set dirty bit */
  3720. cpu_physical_memory_set_dirty_flags(
  3721. addr1, (0xff & ~CODE_DIRTY_FLAG));
  3722. }
  3723. addr1 += l;
  3724. access_len -= l;
  3725. }
  3726. }
  3727. if (xen_enabled()) {
  3728. xen_invalidate_map_cache_entry(buffer);
  3729. }
  3730. return;
  3731. }
  3732. if (is_write) {
  3733. cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
  3734. }
  3735. qemu_vfree(bounce.buffer);
  3736. bounce.buffer = NULL;
  3737. cpu_notify_map_clients();
  3738. }
  3739. /* warning: addr must be aligned */
  3740. static inline uint32_t ldl_phys_internal(target_phys_addr_t addr,
  3741. enum device_endian endian)
  3742. {
  3743. int io_index;
  3744. uint8_t *ptr;
  3745. uint32_t val;
  3746. unsigned long pd;
  3747. PhysPageDesc *p;
  3748. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  3749. if (!p) {
  3750. pd = IO_MEM_UNASSIGNED;
  3751. } else {
  3752. pd = p->phys_offset;
  3753. }
  3754. if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
  3755. !(pd & IO_MEM_ROMD)) {
  3756. /* I/O case */
  3757. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  3758. if (p)
  3759. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  3760. val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
  3761. #if defined(TARGET_WORDS_BIGENDIAN)
  3762. if (endian == DEVICE_LITTLE_ENDIAN) {
  3763. val = bswap32(val);
  3764. }
  3765. #else
  3766. if (endian == DEVICE_BIG_ENDIAN) {
  3767. val = bswap32(val);
  3768. }
  3769. #endif
  3770. } else {
  3771. /* RAM case */
  3772. ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
  3773. (addr & ~TARGET_PAGE_MASK);
  3774. switch (endian) {
  3775. case DEVICE_LITTLE_ENDIAN:
  3776. val = ldl_le_p(ptr);
  3777. break;
  3778. case DEVICE_BIG_ENDIAN:
  3779. val = ldl_be_p(ptr);
  3780. break;
  3781. default:
  3782. val = ldl_p(ptr);
  3783. break;
  3784. }
  3785. }
  3786. return val;
  3787. }
  3788. uint32_t ldl_phys(target_phys_addr_t addr)
  3789. {
  3790. return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
  3791. }
  3792. uint32_t ldl_le_phys(target_phys_addr_t addr)
  3793. {
  3794. return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
  3795. }
  3796. uint32_t ldl_be_phys(target_phys_addr_t addr)
  3797. {
  3798. return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
  3799. }
  3800. /* warning: addr must be aligned */
  3801. static inline uint64_t ldq_phys_internal(target_phys_addr_t addr,
  3802. enum device_endian endian)
  3803. {
  3804. int io_index;
  3805. uint8_t *ptr;
  3806. uint64_t val;
  3807. unsigned long pd;
  3808. PhysPageDesc *p;
  3809. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  3810. if (!p) {
  3811. pd = IO_MEM_UNASSIGNED;
  3812. } else {
  3813. pd = p->phys_offset;
  3814. }
  3815. if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
  3816. !(pd & IO_MEM_ROMD)) {
  3817. /* I/O case */
  3818. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  3819. if (p)
  3820. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  3821. /* XXX This is broken when device endian != cpu endian.
  3822. Fix and add "endian" variable check */
  3823. #ifdef TARGET_WORDS_BIGENDIAN
  3824. val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
  3825. val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
  3826. #else
  3827. val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
  3828. val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
  3829. #endif
  3830. } else {
  3831. /* RAM case */
  3832. ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
  3833. (addr & ~TARGET_PAGE_MASK);
  3834. switch (endian) {
  3835. case DEVICE_LITTLE_ENDIAN:
  3836. val = ldq_le_p(ptr);
  3837. break;
  3838. case DEVICE_BIG_ENDIAN:
  3839. val = ldq_be_p(ptr);
  3840. break;
  3841. default:
  3842. val = ldq_p(ptr);
  3843. break;
  3844. }
  3845. }
  3846. return val;
  3847. }
  3848. uint64_t ldq_phys(target_phys_addr_t addr)
  3849. {
  3850. return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
  3851. }
  3852. uint64_t ldq_le_phys(target_phys_addr_t addr)
  3853. {
  3854. return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
  3855. }
  3856. uint64_t ldq_be_phys(target_phys_addr_t addr)
  3857. {
  3858. return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
  3859. }
  3860. /* XXX: optimize */
  3861. uint32_t ldub_phys(target_phys_addr_t addr)
  3862. {
  3863. uint8_t val;
  3864. cpu_physical_memory_read(addr, &val, 1);
  3865. return val;
  3866. }
  3867. /* warning: addr must be aligned */
  3868. static inline uint32_t lduw_phys_internal(target_phys_addr_t addr,
  3869. enum device_endian endian)
  3870. {
  3871. int io_index;
  3872. uint8_t *ptr;
  3873. uint64_t val;
  3874. unsigned long pd;
  3875. PhysPageDesc *p;
  3876. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  3877. if (!p) {
  3878. pd = IO_MEM_UNASSIGNED;
  3879. } else {
  3880. pd = p->phys_offset;
  3881. }
  3882. if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
  3883. !(pd & IO_MEM_ROMD)) {
  3884. /* I/O case */
  3885. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  3886. if (p)
  3887. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  3888. val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr);
  3889. #if defined(TARGET_WORDS_BIGENDIAN)
  3890. if (endian == DEVICE_LITTLE_ENDIAN) {
  3891. val = bswap16(val);
  3892. }
  3893. #else
  3894. if (endian == DEVICE_BIG_ENDIAN) {
  3895. val = bswap16(val);
  3896. }
  3897. #endif
  3898. } else {
  3899. /* RAM case */
  3900. ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
  3901. (addr & ~TARGET_PAGE_MASK);
  3902. switch (endian) {
  3903. case DEVICE_LITTLE_ENDIAN:
  3904. val = lduw_le_p(ptr);
  3905. break;
  3906. case DEVICE_BIG_ENDIAN:
  3907. val = lduw_be_p(ptr);
  3908. break;
  3909. default:
  3910. val = lduw_p(ptr);
  3911. break;
  3912. }
  3913. }
  3914. return val;
  3915. }
  3916. uint32_t lduw_phys(target_phys_addr_t addr)
  3917. {
  3918. return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
  3919. }
  3920. uint32_t lduw_le_phys(target_phys_addr_t addr)
  3921. {
  3922. return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
  3923. }
  3924. uint32_t lduw_be_phys(target_phys_addr_t addr)
  3925. {
  3926. return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
  3927. }
  3928. /* warning: addr must be aligned. The ram page is not masked as dirty
  3929. and the code inside is not invalidated. It is useful if the dirty
  3930. bits are used to track modified PTEs */
  3931. void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
  3932. {
  3933. int io_index;
  3934. uint8_t *ptr;
  3935. unsigned long pd;
  3936. PhysPageDesc *p;
  3937. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  3938. if (!p) {
  3939. pd = IO_MEM_UNASSIGNED;
  3940. } else {
  3941. pd = p->phys_offset;
  3942. }
  3943. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  3944. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  3945. if (p)
  3946. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  3947. io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
  3948. } else {
  3949. unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  3950. ptr = qemu_get_ram_ptr(addr1);
  3951. stl_p(ptr, val);
  3952. if (unlikely(in_migration)) {
  3953. if (!cpu_physical_memory_is_dirty(addr1)) {
  3954. /* invalidate code */
  3955. tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
  3956. /* set dirty bit */
  3957. cpu_physical_memory_set_dirty_flags(
  3958. addr1, (0xff & ~CODE_DIRTY_FLAG));
  3959. }
  3960. }
  3961. }
  3962. }
  3963. void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
  3964. {
  3965. int io_index;
  3966. uint8_t *ptr;
  3967. unsigned long pd;
  3968. PhysPageDesc *p;
  3969. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  3970. if (!p) {
  3971. pd = IO_MEM_UNASSIGNED;
  3972. } else {
  3973. pd = p->phys_offset;
  3974. }
  3975. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  3976. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  3977. if (p)
  3978. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  3979. #ifdef TARGET_WORDS_BIGENDIAN
  3980. io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
  3981. io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
  3982. #else
  3983. io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
  3984. io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
  3985. #endif
  3986. } else {
  3987. ptr = qemu_get_ram_ptr(pd & TARGET_PAGE_MASK) +
  3988. (addr & ~TARGET_PAGE_MASK);
  3989. stq_p(ptr, val);
  3990. }
  3991. }
  3992. /* warning: addr must be aligned */
  3993. static inline void stl_phys_internal(target_phys_addr_t addr, uint32_t val,
  3994. enum device_endian endian)
  3995. {
  3996. int io_index;
  3997. uint8_t *ptr;
  3998. unsigned long pd;
  3999. PhysPageDesc *p;
  4000. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  4001. if (!p) {
  4002. pd = IO_MEM_UNASSIGNED;
  4003. } else {
  4004. pd = p->phys_offset;
  4005. }
  4006. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  4007. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  4008. if (p)
  4009. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  4010. #if defined(TARGET_WORDS_BIGENDIAN)
  4011. if (endian == DEVICE_LITTLE_ENDIAN) {
  4012. val = bswap32(val);
  4013. }
  4014. #else
  4015. if (endian == DEVICE_BIG_ENDIAN) {
  4016. val = bswap32(val);
  4017. }
  4018. #endif
  4019. io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
  4020. } else {
  4021. unsigned long addr1;
  4022. addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  4023. /* RAM case */
  4024. ptr = qemu_get_ram_ptr(addr1);
  4025. switch (endian) {
  4026. case DEVICE_LITTLE_ENDIAN:
  4027. stl_le_p(ptr, val);
  4028. break;
  4029. case DEVICE_BIG_ENDIAN:
  4030. stl_be_p(ptr, val);
  4031. break;
  4032. default:
  4033. stl_p(ptr, val);
  4034. break;
  4035. }
  4036. if (!cpu_physical_memory_is_dirty(addr1)) {
  4037. /* invalidate code */
  4038. tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
  4039. /* set dirty bit */
  4040. cpu_physical_memory_set_dirty_flags(addr1,
  4041. (0xff & ~CODE_DIRTY_FLAG));
  4042. }
  4043. }
  4044. }
  4045. void stl_phys(target_phys_addr_t addr, uint32_t val)
  4046. {
  4047. stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
  4048. }
  4049. void stl_le_phys(target_phys_addr_t addr, uint32_t val)
  4050. {
  4051. stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
  4052. }
  4053. void stl_be_phys(target_phys_addr_t addr, uint32_t val)
  4054. {
  4055. stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
  4056. }
  4057. /* XXX: optimize */
  4058. void stb_phys(target_phys_addr_t addr, uint32_t val)
  4059. {
  4060. uint8_t v = val;
  4061. cpu_physical_memory_write(addr, &v, 1);
  4062. }
  4063. /* warning: addr must be aligned */
  4064. static inline void stw_phys_internal(target_phys_addr_t addr, uint32_t val,
  4065. enum device_endian endian)
  4066. {
  4067. int io_index;
  4068. uint8_t *ptr;
  4069. unsigned long pd;
  4070. PhysPageDesc *p;
  4071. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  4072. if (!p) {
  4073. pd = IO_MEM_UNASSIGNED;
  4074. } else {
  4075. pd = p->phys_offset;
  4076. }
  4077. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  4078. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  4079. if (p)
  4080. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  4081. #if defined(TARGET_WORDS_BIGENDIAN)
  4082. if (endian == DEVICE_LITTLE_ENDIAN) {
  4083. val = bswap16(val);
  4084. }
  4085. #else
  4086. if (endian == DEVICE_BIG_ENDIAN) {
  4087. val = bswap16(val);
  4088. }
  4089. #endif
  4090. io_mem_write[io_index][1](io_mem_opaque[io_index], addr, val);
  4091. } else {
  4092. unsigned long addr1;
  4093. addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  4094. /* RAM case */
  4095. ptr = qemu_get_ram_ptr(addr1);
  4096. switch (endian) {
  4097. case DEVICE_LITTLE_ENDIAN:
  4098. stw_le_p(ptr, val);
  4099. break;
  4100. case DEVICE_BIG_ENDIAN:
  4101. stw_be_p(ptr, val);
  4102. break;
  4103. default:
  4104. stw_p(ptr, val);
  4105. break;
  4106. }
  4107. if (!cpu_physical_memory_is_dirty(addr1)) {
  4108. /* invalidate code */
  4109. tb_invalidate_phys_page_range(addr1, addr1 + 2, 0);
  4110. /* set dirty bit */
  4111. cpu_physical_memory_set_dirty_flags(addr1,
  4112. (0xff & ~CODE_DIRTY_FLAG));
  4113. }
  4114. }
  4115. }
  4116. void stw_phys(target_phys_addr_t addr, uint32_t val)
  4117. {
  4118. stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
  4119. }
  4120. void stw_le_phys(target_phys_addr_t addr, uint32_t val)
  4121. {
  4122. stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
  4123. }
  4124. void stw_be_phys(target_phys_addr_t addr, uint32_t val)
  4125. {
  4126. stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
  4127. }
  4128. /* XXX: optimize */
  4129. void stq_phys(target_phys_addr_t addr, uint64_t val)
  4130. {
  4131. val = tswap64(val);
  4132. cpu_physical_memory_write(addr, &val, 8);
  4133. }
  4134. void stq_le_phys(target_phys_addr_t addr, uint64_t val)
  4135. {
  4136. val = cpu_to_le64(val);
  4137. cpu_physical_memory_write(addr, &val, 8);
  4138. }
  4139. void stq_be_phys(target_phys_addr_t addr, uint64_t val)
  4140. {
  4141. val = cpu_to_be64(val);
  4142. cpu_physical_memory_write(addr, &val, 8);
  4143. }
  4144. /* virtual memory access for debug (includes writing to ROM) */
  4145. int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
  4146. uint8_t *buf, int len, int is_write)
  4147. {
  4148. int l;
  4149. target_phys_addr_t phys_addr;
  4150. target_ulong page;
  4151. while (len > 0) {
  4152. page = addr & TARGET_PAGE_MASK;
  4153. phys_addr = cpu_get_phys_page_debug(env, page);
  4154. /* if no physical page mapped, return an error */
  4155. if (phys_addr == -1)
  4156. return -1;
  4157. l = (page + TARGET_PAGE_SIZE) - addr;
  4158. if (l > len)
  4159. l = len;
  4160. phys_addr += (addr & ~TARGET_PAGE_MASK);
  4161. if (is_write)
  4162. cpu_physical_memory_write_rom(phys_addr, buf, l);
  4163. else
  4164. cpu_physical_memory_rw(phys_addr, buf, l, is_write);
  4165. len -= l;
  4166. buf += l;
  4167. addr += l;
  4168. }
  4169. return 0;
  4170. }
  4171. #endif
  4172. /* in deterministic execution mode, instructions doing device I/Os
  4173. must be at the end of the TB */
  4174. void cpu_io_recompile(CPUState *env, void *retaddr)
  4175. {
  4176. TranslationBlock *tb;
  4177. uint32_t n, cflags;
  4178. target_ulong pc, cs_base;
  4179. uint64_t flags;
  4180. tb = tb_find_pc((unsigned long)retaddr);
  4181. if (!tb) {
  4182. cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
  4183. retaddr);
  4184. }
  4185. n = env->icount_decr.u16.low + tb->icount;
  4186. cpu_restore_state(tb, env, (unsigned long)retaddr);
  4187. /* Calculate how many instructions had been executed before the fault
  4188. occurred. */
  4189. n = n - env->icount_decr.u16.low;
  4190. /* Generate a new TB ending on the I/O insn. */
  4191. n++;
  4192. /* On MIPS and SH, delay slot instructions can only be restarted if
  4193. they were already the first instruction in the TB. If this is not
  4194. the first instruction in a TB then re-execute the preceding
  4195. branch. */
  4196. #if defined(TARGET_MIPS)
  4197. if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
  4198. env->active_tc.PC -= 4;
  4199. env->icount_decr.u16.low++;
  4200. env->hflags &= ~MIPS_HFLAG_BMASK;
  4201. }
  4202. #elif defined(TARGET_SH4)
  4203. if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
  4204. && n > 1) {
  4205. env->pc -= 2;
  4206. env->icount_decr.u16.low++;
  4207. env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
  4208. }
  4209. #endif
  4210. /* This should never happen. */
  4211. if (n > CF_COUNT_MASK)
  4212. cpu_abort(env, "TB too big during recompile");
  4213. cflags = n | CF_LAST_IO;
  4214. pc = tb->pc;
  4215. cs_base = tb->cs_base;
  4216. flags = tb->flags;
  4217. tb_phys_invalidate(tb, -1);
  4218. /* FIXME: In theory this could raise an exception. In practice
  4219. we have already translated the block once so it's probably ok. */
  4220. tb_gen_code(env, pc, cs_base, flags, cflags);
  4221. /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
  4222. the first in the TB) then we end up generating a whole new TB and
  4223. repeating the fault, which is horribly inefficient.
  4224. Better would be to execute just this insn uncached, or generate a
  4225. second new TB. */
  4226. cpu_resume_from_signal(env, NULL);
  4227. }
  4228. #if !defined(CONFIG_USER_ONLY)
  4229. void dump_exec_info(FILE *f, fprintf_function cpu_fprintf)
  4230. {
  4231. int i, target_code_size, max_target_code_size;
  4232. int direct_jmp_count, direct_jmp2_count, cross_page;
  4233. TranslationBlock *tb;
  4234. target_code_size = 0;
  4235. max_target_code_size = 0;
  4236. cross_page = 0;
  4237. direct_jmp_count = 0;
  4238. direct_jmp2_count = 0;
  4239. for(i = 0; i < nb_tbs; i++) {
  4240. tb = &tbs[i];
  4241. target_code_size += tb->size;
  4242. if (tb->size > max_target_code_size)
  4243. max_target_code_size = tb->size;
  4244. if (tb->page_addr[1] != -1)
  4245. cross_page++;
  4246. if (tb->tb_next_offset[0] != 0xffff) {
  4247. direct_jmp_count++;
  4248. if (tb->tb_next_offset[1] != 0xffff) {
  4249. direct_jmp2_count++;
  4250. }
  4251. }
  4252. }
  4253. /* XXX: avoid using doubles ? */
  4254. cpu_fprintf(f, "Translation buffer state:\n");
  4255. cpu_fprintf(f, "gen code size %td/%ld\n",
  4256. code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
  4257. cpu_fprintf(f, "TB count %d/%d\n",
  4258. nb_tbs, code_gen_max_blocks);
  4259. cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
  4260. nb_tbs ? target_code_size / nb_tbs : 0,
  4261. max_target_code_size);
  4262. cpu_fprintf(f, "TB avg host size %td bytes (expansion ratio: %0.1f)\n",
  4263. nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
  4264. target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
  4265. cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
  4266. cross_page,
  4267. nb_tbs ? (cross_page * 100) / nb_tbs : 0);
  4268. cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
  4269. direct_jmp_count,
  4270. nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
  4271. direct_jmp2_count,
  4272. nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
  4273. cpu_fprintf(f, "\nStatistics:\n");
  4274. cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
  4275. cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
  4276. cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
  4277. tcg_dump_info(f, cpu_fprintf);
  4278. }
  4279. #define MMUSUFFIX _cmmu
  4280. #undef GETPC
  4281. #define GETPC() NULL
  4282. #define env cpu_single_env
  4283. #define SOFTMMU_CODE_ACCESS
  4284. #define SHIFT 0
  4285. #include "softmmu_template.h"
  4286. #define SHIFT 1
  4287. #include "softmmu_template.h"
  4288. #define SHIFT 2
  4289. #include "softmmu_template.h"
  4290. #define SHIFT 3
  4291. #include "softmmu_template.h"
  4292. #undef env
  4293. #endif