exec.c 125 KB

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