exec.c 123 KB

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