exec.c 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600
  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, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  19. */
  20. #include "config.h"
  21. #ifdef _WIN32
  22. #define WIN32_LEAN_AND_MEAN
  23. #include <windows.h>
  24. #else
  25. #include <sys/types.h>
  26. #include <sys/mman.h>
  27. #endif
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <unistd.h>
  34. #include <inttypes.h>
  35. #include "cpu.h"
  36. #include "exec-all.h"
  37. #include "qemu-common.h"
  38. #include "tcg.h"
  39. #include "hw/hw.h"
  40. #include "osdep.h"
  41. #include "kvm.h"
  42. #if defined(CONFIG_USER_ONLY)
  43. #include <qemu.h>
  44. #endif
  45. //#define DEBUG_TB_INVALIDATE
  46. //#define DEBUG_FLUSH
  47. //#define DEBUG_TLB
  48. //#define DEBUG_UNASSIGNED
  49. /* make various TB consistency checks */
  50. //#define DEBUG_TB_CHECK
  51. //#define DEBUG_TLB_CHECK
  52. //#define DEBUG_IOPORT
  53. //#define DEBUG_SUBPAGE
  54. #if !defined(CONFIG_USER_ONLY)
  55. /* TB consistency checks only implemented for usermode emulation. */
  56. #undef DEBUG_TB_CHECK
  57. #endif
  58. #define SMC_BITMAP_USE_THRESHOLD 10
  59. #define MMAP_AREA_START 0x00000000
  60. #define MMAP_AREA_END 0xa8000000
  61. #if defined(TARGET_SPARC64)
  62. #define TARGET_PHYS_ADDR_SPACE_BITS 41
  63. #elif defined(TARGET_SPARC)
  64. #define TARGET_PHYS_ADDR_SPACE_BITS 36
  65. #elif defined(TARGET_ALPHA)
  66. #define TARGET_PHYS_ADDR_SPACE_BITS 42
  67. #define TARGET_VIRT_ADDR_SPACE_BITS 42
  68. #elif defined(TARGET_PPC64)
  69. #define TARGET_PHYS_ADDR_SPACE_BITS 42
  70. #elif defined(TARGET_X86_64) && !defined(USE_KQEMU)
  71. #define TARGET_PHYS_ADDR_SPACE_BITS 42
  72. #elif defined(TARGET_I386) && !defined(USE_KQEMU)
  73. #define TARGET_PHYS_ADDR_SPACE_BITS 36
  74. #else
  75. /* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
  76. #define TARGET_PHYS_ADDR_SPACE_BITS 32
  77. #endif
  78. static TranslationBlock *tbs;
  79. int code_gen_max_blocks;
  80. TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
  81. static int nb_tbs;
  82. /* any access to the tbs or the page table must use this lock */
  83. spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
  84. #if defined(__arm__) || defined(__sparc_v9__)
  85. /* The prologue must be reachable with a direct jump. ARM and Sparc64
  86. have limited branch ranges (possibly also PPC) so place it in a
  87. section close to code segment. */
  88. #define code_gen_section \
  89. __attribute__((__section__(".gen_code"))) \
  90. __attribute__((aligned (32)))
  91. #else
  92. #define code_gen_section \
  93. __attribute__((aligned (32)))
  94. #endif
  95. uint8_t code_gen_prologue[1024] code_gen_section;
  96. static uint8_t *code_gen_buffer;
  97. static unsigned long code_gen_buffer_size;
  98. /* threshold to flush the translated code buffer */
  99. static unsigned long code_gen_buffer_max_size;
  100. uint8_t *code_gen_ptr;
  101. #if !defined(CONFIG_USER_ONLY)
  102. ram_addr_t phys_ram_size;
  103. int phys_ram_fd;
  104. uint8_t *phys_ram_base;
  105. uint8_t *phys_ram_dirty;
  106. static int in_migration;
  107. static ram_addr_t phys_ram_alloc_offset = 0;
  108. #endif
  109. CPUState *first_cpu;
  110. /* current CPU in the current thread. It is only valid inside
  111. cpu_exec() */
  112. CPUState *cpu_single_env;
  113. /* 0 = Do not count executed instructions.
  114. 1 = Precise instruction counting.
  115. 2 = Adaptive rate instruction counting. */
  116. int use_icount = 0;
  117. /* Current instruction counter. While executing translated code this may
  118. include some instructions that have not yet been executed. */
  119. int64_t qemu_icount;
  120. typedef struct PageDesc {
  121. /* list of TBs intersecting this ram page */
  122. TranslationBlock *first_tb;
  123. /* in order to optimize self modifying code, we count the number
  124. of lookups we do to a given page to use a bitmap */
  125. unsigned int code_write_count;
  126. uint8_t *code_bitmap;
  127. #if defined(CONFIG_USER_ONLY)
  128. unsigned long flags;
  129. #endif
  130. } PageDesc;
  131. typedef struct PhysPageDesc {
  132. /* offset in host memory of the page + io_index in the low bits */
  133. ram_addr_t phys_offset;
  134. ram_addr_t region_offset;
  135. } PhysPageDesc;
  136. #define L2_BITS 10
  137. #if defined(CONFIG_USER_ONLY) && defined(TARGET_VIRT_ADDR_SPACE_BITS)
  138. /* XXX: this is a temporary hack for alpha target.
  139. * In the future, this is to be replaced by a multi-level table
  140. * to actually be able to handle the complete 64 bits address space.
  141. */
  142. #define L1_BITS (TARGET_VIRT_ADDR_SPACE_BITS - L2_BITS - TARGET_PAGE_BITS)
  143. #else
  144. #define L1_BITS (32 - L2_BITS - TARGET_PAGE_BITS)
  145. #endif
  146. #define L1_SIZE (1 << L1_BITS)
  147. #define L2_SIZE (1 << L2_BITS)
  148. unsigned long qemu_real_host_page_size;
  149. unsigned long qemu_host_page_bits;
  150. unsigned long qemu_host_page_size;
  151. unsigned long qemu_host_page_mask;
  152. /* XXX: for system emulation, it could just be an array */
  153. static PageDesc *l1_map[L1_SIZE];
  154. static PhysPageDesc **l1_phys_map;
  155. #if !defined(CONFIG_USER_ONLY)
  156. static void io_mem_init(void);
  157. /* io memory support */
  158. CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
  159. CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
  160. void *io_mem_opaque[IO_MEM_NB_ENTRIES];
  161. char io_mem_used[IO_MEM_NB_ENTRIES];
  162. static int io_mem_watch;
  163. #endif
  164. /* log support */
  165. static const char *logfilename = "/tmp/qemu.log";
  166. FILE *logfile;
  167. int loglevel;
  168. static int log_append = 0;
  169. /* statistics */
  170. static int tlb_flush_count;
  171. static int tb_flush_count;
  172. static int tb_phys_invalidate_count;
  173. #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
  174. typedef struct subpage_t {
  175. target_phys_addr_t base;
  176. CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4];
  177. CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4];
  178. void *opaque[TARGET_PAGE_SIZE][2][4];
  179. ram_addr_t region_offset[TARGET_PAGE_SIZE][2][4];
  180. } subpage_t;
  181. #ifdef _WIN32
  182. static void map_exec(void *addr, long size)
  183. {
  184. DWORD old_protect;
  185. VirtualProtect(addr, size,
  186. PAGE_EXECUTE_READWRITE, &old_protect);
  187. }
  188. #else
  189. static void map_exec(void *addr, long size)
  190. {
  191. unsigned long start, end, page_size;
  192. page_size = getpagesize();
  193. start = (unsigned long)addr;
  194. start &= ~(page_size - 1);
  195. end = (unsigned long)addr + size;
  196. end += page_size - 1;
  197. end &= ~(page_size - 1);
  198. mprotect((void *)start, end - start,
  199. PROT_READ | PROT_WRITE | PROT_EXEC);
  200. }
  201. #endif
  202. static void page_init(void)
  203. {
  204. /* NOTE: we can always suppose that qemu_host_page_size >=
  205. TARGET_PAGE_SIZE */
  206. #ifdef _WIN32
  207. {
  208. SYSTEM_INFO system_info;
  209. GetSystemInfo(&system_info);
  210. qemu_real_host_page_size = system_info.dwPageSize;
  211. }
  212. #else
  213. qemu_real_host_page_size = getpagesize();
  214. #endif
  215. if (qemu_host_page_size == 0)
  216. qemu_host_page_size = qemu_real_host_page_size;
  217. if (qemu_host_page_size < TARGET_PAGE_SIZE)
  218. qemu_host_page_size = TARGET_PAGE_SIZE;
  219. qemu_host_page_bits = 0;
  220. while ((1 << qemu_host_page_bits) < qemu_host_page_size)
  221. qemu_host_page_bits++;
  222. qemu_host_page_mask = ~(qemu_host_page_size - 1);
  223. l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
  224. memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
  225. #if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
  226. {
  227. long long startaddr, endaddr;
  228. FILE *f;
  229. int n;
  230. mmap_lock();
  231. last_brk = (unsigned long)sbrk(0);
  232. f = fopen("/proc/self/maps", "r");
  233. if (f) {
  234. do {
  235. n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
  236. if (n == 2) {
  237. startaddr = MIN(startaddr,
  238. (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
  239. endaddr = MIN(endaddr,
  240. (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
  241. page_set_flags(startaddr & TARGET_PAGE_MASK,
  242. TARGET_PAGE_ALIGN(endaddr),
  243. PAGE_RESERVED);
  244. }
  245. } while (!feof(f));
  246. fclose(f);
  247. }
  248. mmap_unlock();
  249. }
  250. #endif
  251. }
  252. static inline PageDesc **page_l1_map(target_ulong index)
  253. {
  254. #if TARGET_LONG_BITS > 32
  255. /* Host memory outside guest VM. For 32-bit targets we have already
  256. excluded high addresses. */
  257. if (index > ((target_ulong)L2_SIZE * L1_SIZE))
  258. return NULL;
  259. #endif
  260. return &l1_map[index >> L2_BITS];
  261. }
  262. static inline PageDesc *page_find_alloc(target_ulong index)
  263. {
  264. PageDesc **lp, *p;
  265. lp = page_l1_map(index);
  266. if (!lp)
  267. return NULL;
  268. p = *lp;
  269. if (!p) {
  270. /* allocate if not found */
  271. #if defined(CONFIG_USER_ONLY)
  272. size_t len = sizeof(PageDesc) * L2_SIZE;
  273. /* Don't use qemu_malloc because it may recurse. */
  274. p = mmap(0, len, PROT_READ | PROT_WRITE,
  275. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  276. *lp = p;
  277. if (h2g_valid(p)) {
  278. unsigned long addr = h2g(p);
  279. page_set_flags(addr & TARGET_PAGE_MASK,
  280. TARGET_PAGE_ALIGN(addr + len),
  281. PAGE_RESERVED);
  282. }
  283. #else
  284. p = qemu_mallocz(sizeof(PageDesc) * L2_SIZE);
  285. *lp = p;
  286. #endif
  287. }
  288. return p + (index & (L2_SIZE - 1));
  289. }
  290. static inline PageDesc *page_find(target_ulong index)
  291. {
  292. PageDesc **lp, *p;
  293. lp = page_l1_map(index);
  294. if (!lp)
  295. return NULL;
  296. p = *lp;
  297. if (!p)
  298. return 0;
  299. return p + (index & (L2_SIZE - 1));
  300. }
  301. static PhysPageDesc *phys_page_find_alloc(target_phys_addr_t index, int alloc)
  302. {
  303. void **lp, **p;
  304. PhysPageDesc *pd;
  305. p = (void **)l1_phys_map;
  306. #if TARGET_PHYS_ADDR_SPACE_BITS > 32
  307. #if TARGET_PHYS_ADDR_SPACE_BITS > (32 + L1_BITS)
  308. #error unsupported TARGET_PHYS_ADDR_SPACE_BITS
  309. #endif
  310. lp = p + ((index >> (L1_BITS + L2_BITS)) & (L1_SIZE - 1));
  311. p = *lp;
  312. if (!p) {
  313. /* allocate if not found */
  314. if (!alloc)
  315. return NULL;
  316. p = qemu_vmalloc(sizeof(void *) * L1_SIZE);
  317. memset(p, 0, sizeof(void *) * L1_SIZE);
  318. *lp = p;
  319. }
  320. #endif
  321. lp = p + ((index >> L2_BITS) & (L1_SIZE - 1));
  322. pd = *lp;
  323. if (!pd) {
  324. int i;
  325. /* allocate if not found */
  326. if (!alloc)
  327. return NULL;
  328. pd = qemu_vmalloc(sizeof(PhysPageDesc) * L2_SIZE);
  329. *lp = pd;
  330. for (i = 0; i < L2_SIZE; i++) {
  331. pd[i].phys_offset = IO_MEM_UNASSIGNED;
  332. pd[i].region_offset = (index + i) << TARGET_PAGE_BITS;
  333. }
  334. }
  335. return ((PhysPageDesc *)pd) + (index & (L2_SIZE - 1));
  336. }
  337. static inline PhysPageDesc *phys_page_find(target_phys_addr_t index)
  338. {
  339. return phys_page_find_alloc(index, 0);
  340. }
  341. #if !defined(CONFIG_USER_ONLY)
  342. static void tlb_protect_code(ram_addr_t ram_addr);
  343. static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
  344. target_ulong vaddr);
  345. #define mmap_lock() do { } while(0)
  346. #define mmap_unlock() do { } while(0)
  347. #endif
  348. #define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
  349. #if defined(CONFIG_USER_ONLY)
  350. /* Currently it is not recommanded to allocate big chunks of data in
  351. user mode. It will change when a dedicated libc will be used */
  352. #define USE_STATIC_CODE_GEN_BUFFER
  353. #endif
  354. #ifdef USE_STATIC_CODE_GEN_BUFFER
  355. static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE];
  356. #endif
  357. static void code_gen_alloc(unsigned long tb_size)
  358. {
  359. #ifdef USE_STATIC_CODE_GEN_BUFFER
  360. code_gen_buffer = static_code_gen_buffer;
  361. code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
  362. map_exec(code_gen_buffer, code_gen_buffer_size);
  363. #else
  364. code_gen_buffer_size = tb_size;
  365. if (code_gen_buffer_size == 0) {
  366. #if defined(CONFIG_USER_ONLY)
  367. /* in user mode, phys_ram_size is not meaningful */
  368. code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
  369. #else
  370. /* XXX: needs ajustments */
  371. code_gen_buffer_size = (unsigned long)(phys_ram_size / 4);
  372. #endif
  373. }
  374. if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
  375. code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
  376. /* The code gen buffer location may have constraints depending on
  377. the host cpu and OS */
  378. #if defined(__linux__)
  379. {
  380. int flags;
  381. void *start = NULL;
  382. flags = MAP_PRIVATE | MAP_ANONYMOUS;
  383. #if defined(__x86_64__)
  384. flags |= MAP_32BIT;
  385. /* Cannot map more than that */
  386. if (code_gen_buffer_size > (800 * 1024 * 1024))
  387. code_gen_buffer_size = (800 * 1024 * 1024);
  388. #elif defined(__sparc_v9__)
  389. // Map the buffer below 2G, so we can use direct calls and branches
  390. flags |= MAP_FIXED;
  391. start = (void *) 0x60000000UL;
  392. if (code_gen_buffer_size > (512 * 1024 * 1024))
  393. code_gen_buffer_size = (512 * 1024 * 1024);
  394. #elif defined(__arm__)
  395. /* Map the buffer below 32M, so we can use direct calls and branches */
  396. flags |= MAP_FIXED;
  397. start = (void *) 0x01000000UL;
  398. if (code_gen_buffer_size > 16 * 1024 * 1024)
  399. code_gen_buffer_size = 16 * 1024 * 1024;
  400. #endif
  401. code_gen_buffer = mmap(start, code_gen_buffer_size,
  402. PROT_WRITE | PROT_READ | PROT_EXEC,
  403. flags, -1, 0);
  404. if (code_gen_buffer == MAP_FAILED) {
  405. fprintf(stderr, "Could not allocate dynamic translator buffer\n");
  406. exit(1);
  407. }
  408. }
  409. #elif defined(__FreeBSD__)
  410. {
  411. int flags;
  412. void *addr = NULL;
  413. flags = MAP_PRIVATE | MAP_ANONYMOUS;
  414. #if defined(__x86_64__)
  415. /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
  416. * 0x40000000 is free */
  417. flags |= MAP_FIXED;
  418. addr = (void *)0x40000000;
  419. /* Cannot map more than that */
  420. if (code_gen_buffer_size > (800 * 1024 * 1024))
  421. code_gen_buffer_size = (800 * 1024 * 1024);
  422. #endif
  423. code_gen_buffer = mmap(addr, code_gen_buffer_size,
  424. PROT_WRITE | PROT_READ | PROT_EXEC,
  425. flags, -1, 0);
  426. if (code_gen_buffer == MAP_FAILED) {
  427. fprintf(stderr, "Could not allocate dynamic translator buffer\n");
  428. exit(1);
  429. }
  430. }
  431. #else
  432. code_gen_buffer = qemu_malloc(code_gen_buffer_size);
  433. map_exec(code_gen_buffer, code_gen_buffer_size);
  434. #endif
  435. #endif /* !USE_STATIC_CODE_GEN_BUFFER */
  436. map_exec(code_gen_prologue, sizeof(code_gen_prologue));
  437. code_gen_buffer_max_size = code_gen_buffer_size -
  438. code_gen_max_block_size();
  439. code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
  440. tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
  441. }
  442. /* Must be called before using the QEMU cpus. 'tb_size' is the size
  443. (in bytes) allocated to the translation buffer. Zero means default
  444. size. */
  445. void cpu_exec_init_all(unsigned long tb_size)
  446. {
  447. cpu_gen_init();
  448. code_gen_alloc(tb_size);
  449. code_gen_ptr = code_gen_buffer;
  450. page_init();
  451. #if !defined(CONFIG_USER_ONLY)
  452. io_mem_init();
  453. #endif
  454. }
  455. #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
  456. #define CPU_COMMON_SAVE_VERSION 1
  457. static void cpu_common_save(QEMUFile *f, void *opaque)
  458. {
  459. CPUState *env = opaque;
  460. qemu_put_be32s(f, &env->halted);
  461. qemu_put_be32s(f, &env->interrupt_request);
  462. }
  463. static int cpu_common_load(QEMUFile *f, void *opaque, int version_id)
  464. {
  465. CPUState *env = opaque;
  466. if (version_id != CPU_COMMON_SAVE_VERSION)
  467. return -EINVAL;
  468. qemu_get_be32s(f, &env->halted);
  469. qemu_get_be32s(f, &env->interrupt_request);
  470. env->interrupt_request &= ~CPU_INTERRUPT_EXIT;
  471. tlb_flush(env, 1);
  472. return 0;
  473. }
  474. #endif
  475. void cpu_exec_init(CPUState *env)
  476. {
  477. CPUState **penv;
  478. int cpu_index;
  479. env->next_cpu = NULL;
  480. penv = &first_cpu;
  481. cpu_index = 0;
  482. while (*penv != NULL) {
  483. penv = (CPUState **)&(*penv)->next_cpu;
  484. cpu_index++;
  485. }
  486. env->cpu_index = cpu_index;
  487. TAILQ_INIT(&env->breakpoints);
  488. TAILQ_INIT(&env->watchpoints);
  489. *penv = env;
  490. #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
  491. register_savevm("cpu_common", cpu_index, CPU_COMMON_SAVE_VERSION,
  492. cpu_common_save, cpu_common_load, env);
  493. register_savevm("cpu", cpu_index, CPU_SAVE_VERSION,
  494. cpu_save, cpu_load, env);
  495. #endif
  496. }
  497. static inline void invalidate_page_bitmap(PageDesc *p)
  498. {
  499. if (p->code_bitmap) {
  500. qemu_free(p->code_bitmap);
  501. p->code_bitmap = NULL;
  502. }
  503. p->code_write_count = 0;
  504. }
  505. /* set to NULL all the 'first_tb' fields in all PageDescs */
  506. static void page_flush_tb(void)
  507. {
  508. int i, j;
  509. PageDesc *p;
  510. for(i = 0; i < L1_SIZE; i++) {
  511. p = l1_map[i];
  512. if (p) {
  513. for(j = 0; j < L2_SIZE; j++) {
  514. p->first_tb = NULL;
  515. invalidate_page_bitmap(p);
  516. p++;
  517. }
  518. }
  519. }
  520. }
  521. /* flush all the translation blocks */
  522. /* XXX: tb_flush is currently not thread safe */
  523. void tb_flush(CPUState *env1)
  524. {
  525. CPUState *env;
  526. #if defined(DEBUG_FLUSH)
  527. printf("qemu: flush code_size=%ld nb_tbs=%d avg_tb_size=%ld\n",
  528. (unsigned long)(code_gen_ptr - code_gen_buffer),
  529. nb_tbs, nb_tbs > 0 ?
  530. ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
  531. #endif
  532. if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
  533. cpu_abort(env1, "Internal error: code buffer overflow\n");
  534. nb_tbs = 0;
  535. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  536. memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
  537. }
  538. memset (tb_phys_hash, 0, CODE_GEN_PHYS_HASH_SIZE * sizeof (void *));
  539. page_flush_tb();
  540. code_gen_ptr = code_gen_buffer;
  541. /* XXX: flush processor icache at this point if cache flush is
  542. expensive */
  543. tb_flush_count++;
  544. }
  545. #ifdef DEBUG_TB_CHECK
  546. static void tb_invalidate_check(target_ulong address)
  547. {
  548. TranslationBlock *tb;
  549. int i;
  550. address &= TARGET_PAGE_MASK;
  551. for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
  552. for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
  553. if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
  554. address >= tb->pc + tb->size)) {
  555. printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
  556. address, (long)tb->pc, tb->size);
  557. }
  558. }
  559. }
  560. }
  561. /* verify that all the pages have correct rights for code */
  562. static void tb_page_check(void)
  563. {
  564. TranslationBlock *tb;
  565. int i, flags1, flags2;
  566. for(i = 0;i < CODE_GEN_PHYS_HASH_SIZE; i++) {
  567. for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
  568. flags1 = page_get_flags(tb->pc);
  569. flags2 = page_get_flags(tb->pc + tb->size - 1);
  570. if ((flags1 & PAGE_WRITE) || (flags2 & PAGE_WRITE)) {
  571. printf("ERROR page flags: PC=%08lx size=%04x f1=%x f2=%x\n",
  572. (long)tb->pc, tb->size, flags1, flags2);
  573. }
  574. }
  575. }
  576. }
  577. static void tb_jmp_check(TranslationBlock *tb)
  578. {
  579. TranslationBlock *tb1;
  580. unsigned int n1;
  581. /* suppress any remaining jumps to this TB */
  582. tb1 = tb->jmp_first;
  583. for(;;) {
  584. n1 = (long)tb1 & 3;
  585. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  586. if (n1 == 2)
  587. break;
  588. tb1 = tb1->jmp_next[n1];
  589. }
  590. /* check end of list */
  591. if (tb1 != tb) {
  592. printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
  593. }
  594. }
  595. #endif
  596. /* invalidate one TB */
  597. static inline void tb_remove(TranslationBlock **ptb, TranslationBlock *tb,
  598. int next_offset)
  599. {
  600. TranslationBlock *tb1;
  601. for(;;) {
  602. tb1 = *ptb;
  603. if (tb1 == tb) {
  604. *ptb = *(TranslationBlock **)((char *)tb1 + next_offset);
  605. break;
  606. }
  607. ptb = (TranslationBlock **)((char *)tb1 + next_offset);
  608. }
  609. }
  610. static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
  611. {
  612. TranslationBlock *tb1;
  613. unsigned int n1;
  614. for(;;) {
  615. tb1 = *ptb;
  616. n1 = (long)tb1 & 3;
  617. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  618. if (tb1 == tb) {
  619. *ptb = tb1->page_next[n1];
  620. break;
  621. }
  622. ptb = &tb1->page_next[n1];
  623. }
  624. }
  625. static inline void tb_jmp_remove(TranslationBlock *tb, int n)
  626. {
  627. TranslationBlock *tb1, **ptb;
  628. unsigned int n1;
  629. ptb = &tb->jmp_next[n];
  630. tb1 = *ptb;
  631. if (tb1) {
  632. /* find tb(n) in circular list */
  633. for(;;) {
  634. tb1 = *ptb;
  635. n1 = (long)tb1 & 3;
  636. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  637. if (n1 == n && tb1 == tb)
  638. break;
  639. if (n1 == 2) {
  640. ptb = &tb1->jmp_first;
  641. } else {
  642. ptb = &tb1->jmp_next[n1];
  643. }
  644. }
  645. /* now we can suppress tb(n) from the list */
  646. *ptb = tb->jmp_next[n];
  647. tb->jmp_next[n] = NULL;
  648. }
  649. }
  650. /* reset the jump entry 'n' of a TB so that it is not chained to
  651. another TB */
  652. static inline void tb_reset_jump(TranslationBlock *tb, int n)
  653. {
  654. tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
  655. }
  656. void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
  657. {
  658. CPUState *env;
  659. PageDesc *p;
  660. unsigned int h, n1;
  661. target_phys_addr_t phys_pc;
  662. TranslationBlock *tb1, *tb2;
  663. /* remove the TB from the hash list */
  664. phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
  665. h = tb_phys_hash_func(phys_pc);
  666. tb_remove(&tb_phys_hash[h], tb,
  667. offsetof(TranslationBlock, phys_hash_next));
  668. /* remove the TB from the page list */
  669. if (tb->page_addr[0] != page_addr) {
  670. p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
  671. tb_page_remove(&p->first_tb, tb);
  672. invalidate_page_bitmap(p);
  673. }
  674. if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
  675. p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
  676. tb_page_remove(&p->first_tb, tb);
  677. invalidate_page_bitmap(p);
  678. }
  679. tb_invalidated_flag = 1;
  680. /* remove the TB from the hash list */
  681. h = tb_jmp_cache_hash_func(tb->pc);
  682. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  683. if (env->tb_jmp_cache[h] == tb)
  684. env->tb_jmp_cache[h] = NULL;
  685. }
  686. /* suppress this TB from the two jump lists */
  687. tb_jmp_remove(tb, 0);
  688. tb_jmp_remove(tb, 1);
  689. /* suppress any remaining jumps to this TB */
  690. tb1 = tb->jmp_first;
  691. for(;;) {
  692. n1 = (long)tb1 & 3;
  693. if (n1 == 2)
  694. break;
  695. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  696. tb2 = tb1->jmp_next[n1];
  697. tb_reset_jump(tb1, n1);
  698. tb1->jmp_next[n1] = NULL;
  699. tb1 = tb2;
  700. }
  701. tb->jmp_first = (TranslationBlock *)((long)tb | 2); /* fail safe */
  702. tb_phys_invalidate_count++;
  703. }
  704. static inline void set_bits(uint8_t *tab, int start, int len)
  705. {
  706. int end, mask, end1;
  707. end = start + len;
  708. tab += start >> 3;
  709. mask = 0xff << (start & 7);
  710. if ((start & ~7) == (end & ~7)) {
  711. if (start < end) {
  712. mask &= ~(0xff << (end & 7));
  713. *tab |= mask;
  714. }
  715. } else {
  716. *tab++ |= mask;
  717. start = (start + 8) & ~7;
  718. end1 = end & ~7;
  719. while (start < end1) {
  720. *tab++ = 0xff;
  721. start += 8;
  722. }
  723. if (start < end) {
  724. mask = ~(0xff << (end & 7));
  725. *tab |= mask;
  726. }
  727. }
  728. }
  729. static void build_page_bitmap(PageDesc *p)
  730. {
  731. int n, tb_start, tb_end;
  732. TranslationBlock *tb;
  733. p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
  734. tb = p->first_tb;
  735. while (tb != NULL) {
  736. n = (long)tb & 3;
  737. tb = (TranslationBlock *)((long)tb & ~3);
  738. /* NOTE: this is subtle as a TB may span two physical pages */
  739. if (n == 0) {
  740. /* NOTE: tb_end may be after the end of the page, but
  741. it is not a problem */
  742. tb_start = tb->pc & ~TARGET_PAGE_MASK;
  743. tb_end = tb_start + tb->size;
  744. if (tb_end > TARGET_PAGE_SIZE)
  745. tb_end = TARGET_PAGE_SIZE;
  746. } else {
  747. tb_start = 0;
  748. tb_end = ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
  749. }
  750. set_bits(p->code_bitmap, tb_start, tb_end - tb_start);
  751. tb = tb->page_next[n];
  752. }
  753. }
  754. TranslationBlock *tb_gen_code(CPUState *env,
  755. target_ulong pc, target_ulong cs_base,
  756. int flags, int cflags)
  757. {
  758. TranslationBlock *tb;
  759. uint8_t *tc_ptr;
  760. target_ulong phys_pc, phys_page2, virt_page2;
  761. int code_gen_size;
  762. phys_pc = get_phys_addr_code(env, pc);
  763. tb = tb_alloc(pc);
  764. if (!tb) {
  765. /* flush must be done */
  766. tb_flush(env);
  767. /* cannot fail at this point */
  768. tb = tb_alloc(pc);
  769. /* Don't forget to invalidate previous TB info. */
  770. tb_invalidated_flag = 1;
  771. }
  772. tc_ptr = code_gen_ptr;
  773. tb->tc_ptr = tc_ptr;
  774. tb->cs_base = cs_base;
  775. tb->flags = flags;
  776. tb->cflags = cflags;
  777. cpu_gen_code(env, tb, &code_gen_size);
  778. code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
  779. /* check next page if needed */
  780. virt_page2 = (pc + tb->size - 1) & TARGET_PAGE_MASK;
  781. phys_page2 = -1;
  782. if ((pc & TARGET_PAGE_MASK) != virt_page2) {
  783. phys_page2 = get_phys_addr_code(env, virt_page2);
  784. }
  785. tb_link_phys(tb, phys_pc, phys_page2);
  786. return tb;
  787. }
  788. /* invalidate all TBs which intersect with the target physical page
  789. starting in range [start;end[. NOTE: start and end must refer to
  790. the same physical page. 'is_cpu_write_access' should be true if called
  791. from a real cpu write access: the virtual CPU will exit the current
  792. TB if code is modified inside this TB. */
  793. void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
  794. int is_cpu_write_access)
  795. {
  796. TranslationBlock *tb, *tb_next, *saved_tb;
  797. CPUState *env = cpu_single_env;
  798. target_ulong tb_start, tb_end;
  799. PageDesc *p;
  800. int n;
  801. #ifdef TARGET_HAS_PRECISE_SMC
  802. int current_tb_not_found = is_cpu_write_access;
  803. TranslationBlock *current_tb = NULL;
  804. int current_tb_modified = 0;
  805. target_ulong current_pc = 0;
  806. target_ulong current_cs_base = 0;
  807. int current_flags = 0;
  808. #endif /* TARGET_HAS_PRECISE_SMC */
  809. p = page_find(start >> TARGET_PAGE_BITS);
  810. if (!p)
  811. return;
  812. if (!p->code_bitmap &&
  813. ++p->code_write_count >= SMC_BITMAP_USE_THRESHOLD &&
  814. is_cpu_write_access) {
  815. /* build code bitmap */
  816. build_page_bitmap(p);
  817. }
  818. /* we remove all the TBs in the range [start, end[ */
  819. /* XXX: see if in some cases it could be faster to invalidate all the code */
  820. tb = p->first_tb;
  821. while (tb != NULL) {
  822. n = (long)tb & 3;
  823. tb = (TranslationBlock *)((long)tb & ~3);
  824. tb_next = tb->page_next[n];
  825. /* NOTE: this is subtle as a TB may span two physical pages */
  826. if (n == 0) {
  827. /* NOTE: tb_end may be after the end of the page, but
  828. it is not a problem */
  829. tb_start = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
  830. tb_end = tb_start + tb->size;
  831. } else {
  832. tb_start = tb->page_addr[1];
  833. tb_end = tb_start + ((tb->pc + tb->size) & ~TARGET_PAGE_MASK);
  834. }
  835. if (!(tb_end <= start || tb_start >= end)) {
  836. #ifdef TARGET_HAS_PRECISE_SMC
  837. if (current_tb_not_found) {
  838. current_tb_not_found = 0;
  839. current_tb = NULL;
  840. if (env->mem_io_pc) {
  841. /* now we have a real cpu fault */
  842. current_tb = tb_find_pc(env->mem_io_pc);
  843. }
  844. }
  845. if (current_tb == tb &&
  846. (current_tb->cflags & CF_COUNT_MASK) != 1) {
  847. /* If we are modifying the current TB, we must stop
  848. its execution. We could be more precise by checking
  849. that the modification is after the current PC, but it
  850. would require a specialized function to partially
  851. restore the CPU state */
  852. current_tb_modified = 1;
  853. cpu_restore_state(current_tb, env,
  854. env->mem_io_pc, NULL);
  855. cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
  856. &current_flags);
  857. }
  858. #endif /* TARGET_HAS_PRECISE_SMC */
  859. /* we need to do that to handle the case where a signal
  860. occurs while doing tb_phys_invalidate() */
  861. saved_tb = NULL;
  862. if (env) {
  863. saved_tb = env->current_tb;
  864. env->current_tb = NULL;
  865. }
  866. tb_phys_invalidate(tb, -1);
  867. if (env) {
  868. env->current_tb = saved_tb;
  869. if (env->interrupt_request && env->current_tb)
  870. cpu_interrupt(env, env->interrupt_request);
  871. }
  872. }
  873. tb = tb_next;
  874. }
  875. #if !defined(CONFIG_USER_ONLY)
  876. /* if no code remaining, no need to continue to use slow writes */
  877. if (!p->first_tb) {
  878. invalidate_page_bitmap(p);
  879. if (is_cpu_write_access) {
  880. tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
  881. }
  882. }
  883. #endif
  884. #ifdef TARGET_HAS_PRECISE_SMC
  885. if (current_tb_modified) {
  886. /* we generate a block containing just the instruction
  887. modifying the memory. It will ensure that it cannot modify
  888. itself */
  889. env->current_tb = NULL;
  890. tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
  891. cpu_resume_from_signal(env, NULL);
  892. }
  893. #endif
  894. }
  895. /* len must be <= 8 and start must be a multiple of len */
  896. static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
  897. {
  898. PageDesc *p;
  899. int offset, b;
  900. #if 0
  901. if (1) {
  902. qemu_log("modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
  903. cpu_single_env->mem_io_vaddr, len,
  904. cpu_single_env->eip,
  905. cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
  906. }
  907. #endif
  908. p = page_find(start >> TARGET_PAGE_BITS);
  909. if (!p)
  910. return;
  911. if (p->code_bitmap) {
  912. offset = start & ~TARGET_PAGE_MASK;
  913. b = p->code_bitmap[offset >> 3] >> (offset & 7);
  914. if (b & ((1 << len) - 1))
  915. goto do_invalidate;
  916. } else {
  917. do_invalidate:
  918. tb_invalidate_phys_page_range(start, start + len, 1);
  919. }
  920. }
  921. #if !defined(CONFIG_SOFTMMU)
  922. static void tb_invalidate_phys_page(target_phys_addr_t addr,
  923. unsigned long pc, void *puc)
  924. {
  925. TranslationBlock *tb;
  926. PageDesc *p;
  927. int n;
  928. #ifdef TARGET_HAS_PRECISE_SMC
  929. TranslationBlock *current_tb = NULL;
  930. CPUState *env = cpu_single_env;
  931. int current_tb_modified = 0;
  932. target_ulong current_pc = 0;
  933. target_ulong current_cs_base = 0;
  934. int current_flags = 0;
  935. #endif
  936. addr &= TARGET_PAGE_MASK;
  937. p = page_find(addr >> TARGET_PAGE_BITS);
  938. if (!p)
  939. return;
  940. tb = p->first_tb;
  941. #ifdef TARGET_HAS_PRECISE_SMC
  942. if (tb && pc != 0) {
  943. current_tb = tb_find_pc(pc);
  944. }
  945. #endif
  946. while (tb != NULL) {
  947. n = (long)tb & 3;
  948. tb = (TranslationBlock *)((long)tb & ~3);
  949. #ifdef TARGET_HAS_PRECISE_SMC
  950. if (current_tb == tb &&
  951. (current_tb->cflags & CF_COUNT_MASK) != 1) {
  952. /* If we are modifying the current TB, we must stop
  953. its execution. We could be more precise by checking
  954. that the modification is after the current PC, but it
  955. would require a specialized function to partially
  956. restore the CPU state */
  957. current_tb_modified = 1;
  958. cpu_restore_state(current_tb, env, pc, puc);
  959. cpu_get_tb_cpu_state(env, &current_pc, &current_cs_base,
  960. &current_flags);
  961. }
  962. #endif /* TARGET_HAS_PRECISE_SMC */
  963. tb_phys_invalidate(tb, addr);
  964. tb = tb->page_next[n];
  965. }
  966. p->first_tb = NULL;
  967. #ifdef TARGET_HAS_PRECISE_SMC
  968. if (current_tb_modified) {
  969. /* we generate a block containing just the instruction
  970. modifying the memory. It will ensure that it cannot modify
  971. itself */
  972. env->current_tb = NULL;
  973. tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
  974. cpu_resume_from_signal(env, puc);
  975. }
  976. #endif
  977. }
  978. #endif
  979. /* add the tb in the target page and protect it if necessary */
  980. static inline void tb_alloc_page(TranslationBlock *tb,
  981. unsigned int n, target_ulong page_addr)
  982. {
  983. PageDesc *p;
  984. TranslationBlock *last_first_tb;
  985. tb->page_addr[n] = page_addr;
  986. p = page_find_alloc(page_addr >> TARGET_PAGE_BITS);
  987. tb->page_next[n] = p->first_tb;
  988. last_first_tb = p->first_tb;
  989. p->first_tb = (TranslationBlock *)((long)tb | n);
  990. invalidate_page_bitmap(p);
  991. #if defined(TARGET_HAS_SMC) || 1
  992. #if defined(CONFIG_USER_ONLY)
  993. if (p->flags & PAGE_WRITE) {
  994. target_ulong addr;
  995. PageDesc *p2;
  996. int prot;
  997. /* force the host page as non writable (writes will have a
  998. page fault + mprotect overhead) */
  999. page_addr &= qemu_host_page_mask;
  1000. prot = 0;
  1001. for(addr = page_addr; addr < page_addr + qemu_host_page_size;
  1002. addr += TARGET_PAGE_SIZE) {
  1003. p2 = page_find (addr >> TARGET_PAGE_BITS);
  1004. if (!p2)
  1005. continue;
  1006. prot |= p2->flags;
  1007. p2->flags &= ~PAGE_WRITE;
  1008. page_get_flags(addr);
  1009. }
  1010. mprotect(g2h(page_addr), qemu_host_page_size,
  1011. (prot & PAGE_BITS) & ~PAGE_WRITE);
  1012. #ifdef DEBUG_TB_INVALIDATE
  1013. printf("protecting code page: 0x" TARGET_FMT_lx "\n",
  1014. page_addr);
  1015. #endif
  1016. }
  1017. #else
  1018. /* if some code is already present, then the pages are already
  1019. protected. So we handle the case where only the first TB is
  1020. allocated in a physical page */
  1021. if (!last_first_tb) {
  1022. tlb_protect_code(page_addr);
  1023. }
  1024. #endif
  1025. #endif /* TARGET_HAS_SMC */
  1026. }
  1027. /* Allocate a new translation block. Flush the translation buffer if
  1028. too many translation blocks or too much generated code. */
  1029. TranslationBlock *tb_alloc(target_ulong pc)
  1030. {
  1031. TranslationBlock *tb;
  1032. if (nb_tbs >= code_gen_max_blocks ||
  1033. (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
  1034. return NULL;
  1035. tb = &tbs[nb_tbs++];
  1036. tb->pc = pc;
  1037. tb->cflags = 0;
  1038. return tb;
  1039. }
  1040. void tb_free(TranslationBlock *tb)
  1041. {
  1042. /* In practice this is mostly used for single use temporary TB
  1043. Ignore the hard cases and just back up if this TB happens to
  1044. be the last one generated. */
  1045. if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
  1046. code_gen_ptr = tb->tc_ptr;
  1047. nb_tbs--;
  1048. }
  1049. }
  1050. /* add a new TB and link it to the physical page tables. phys_page2 is
  1051. (-1) to indicate that only one page contains the TB. */
  1052. void tb_link_phys(TranslationBlock *tb,
  1053. target_ulong phys_pc, target_ulong phys_page2)
  1054. {
  1055. unsigned int h;
  1056. TranslationBlock **ptb;
  1057. /* Grab the mmap lock to stop another thread invalidating this TB
  1058. before we are done. */
  1059. mmap_lock();
  1060. /* add in the physical hash table */
  1061. h = tb_phys_hash_func(phys_pc);
  1062. ptb = &tb_phys_hash[h];
  1063. tb->phys_hash_next = *ptb;
  1064. *ptb = tb;
  1065. /* add in the page list */
  1066. tb_alloc_page(tb, 0, phys_pc & TARGET_PAGE_MASK);
  1067. if (phys_page2 != -1)
  1068. tb_alloc_page(tb, 1, phys_page2);
  1069. else
  1070. tb->page_addr[1] = -1;
  1071. tb->jmp_first = (TranslationBlock *)((long)tb | 2);
  1072. tb->jmp_next[0] = NULL;
  1073. tb->jmp_next[1] = NULL;
  1074. /* init original jump addresses */
  1075. if (tb->tb_next_offset[0] != 0xffff)
  1076. tb_reset_jump(tb, 0);
  1077. if (tb->tb_next_offset[1] != 0xffff)
  1078. tb_reset_jump(tb, 1);
  1079. #ifdef DEBUG_TB_CHECK
  1080. tb_page_check();
  1081. #endif
  1082. mmap_unlock();
  1083. }
  1084. /* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
  1085. tb[1].tc_ptr. Return NULL if not found */
  1086. TranslationBlock *tb_find_pc(unsigned long tc_ptr)
  1087. {
  1088. int m_min, m_max, m;
  1089. unsigned long v;
  1090. TranslationBlock *tb;
  1091. if (nb_tbs <= 0)
  1092. return NULL;
  1093. if (tc_ptr < (unsigned long)code_gen_buffer ||
  1094. tc_ptr >= (unsigned long)code_gen_ptr)
  1095. return NULL;
  1096. /* binary search (cf Knuth) */
  1097. m_min = 0;
  1098. m_max = nb_tbs - 1;
  1099. while (m_min <= m_max) {
  1100. m = (m_min + m_max) >> 1;
  1101. tb = &tbs[m];
  1102. v = (unsigned long)tb->tc_ptr;
  1103. if (v == tc_ptr)
  1104. return tb;
  1105. else if (tc_ptr < v) {
  1106. m_max = m - 1;
  1107. } else {
  1108. m_min = m + 1;
  1109. }
  1110. }
  1111. return &tbs[m_max];
  1112. }
  1113. static void tb_reset_jump_recursive(TranslationBlock *tb);
  1114. static inline void tb_reset_jump_recursive2(TranslationBlock *tb, int n)
  1115. {
  1116. TranslationBlock *tb1, *tb_next, **ptb;
  1117. unsigned int n1;
  1118. tb1 = tb->jmp_next[n];
  1119. if (tb1 != NULL) {
  1120. /* find head of list */
  1121. for(;;) {
  1122. n1 = (long)tb1 & 3;
  1123. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  1124. if (n1 == 2)
  1125. break;
  1126. tb1 = tb1->jmp_next[n1];
  1127. }
  1128. /* we are now sure now that tb jumps to tb1 */
  1129. tb_next = tb1;
  1130. /* remove tb from the jmp_first list */
  1131. ptb = &tb_next->jmp_first;
  1132. for(;;) {
  1133. tb1 = *ptb;
  1134. n1 = (long)tb1 & 3;
  1135. tb1 = (TranslationBlock *)((long)tb1 & ~3);
  1136. if (n1 == n && tb1 == tb)
  1137. break;
  1138. ptb = &tb1->jmp_next[n1];
  1139. }
  1140. *ptb = tb->jmp_next[n];
  1141. tb->jmp_next[n] = NULL;
  1142. /* suppress the jump to next tb in generated code */
  1143. tb_reset_jump(tb, n);
  1144. /* suppress jumps in the tb on which we could have jumped */
  1145. tb_reset_jump_recursive(tb_next);
  1146. }
  1147. }
  1148. static void tb_reset_jump_recursive(TranslationBlock *tb)
  1149. {
  1150. tb_reset_jump_recursive2(tb, 0);
  1151. tb_reset_jump_recursive2(tb, 1);
  1152. }
  1153. #if defined(TARGET_HAS_ICE)
  1154. static void breakpoint_invalidate(CPUState *env, target_ulong pc)
  1155. {
  1156. target_phys_addr_t addr;
  1157. target_ulong pd;
  1158. ram_addr_t ram_addr;
  1159. PhysPageDesc *p;
  1160. addr = cpu_get_phys_page_debug(env, pc);
  1161. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  1162. if (!p) {
  1163. pd = IO_MEM_UNASSIGNED;
  1164. } else {
  1165. pd = p->phys_offset;
  1166. }
  1167. ram_addr = (pd & TARGET_PAGE_MASK) | (pc & ~TARGET_PAGE_MASK);
  1168. tb_invalidate_phys_page_range(ram_addr, ram_addr + 1, 0);
  1169. }
  1170. #endif
  1171. /* Add a watchpoint. */
  1172. int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
  1173. int flags, CPUWatchpoint **watchpoint)
  1174. {
  1175. target_ulong len_mask = ~(len - 1);
  1176. CPUWatchpoint *wp;
  1177. /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
  1178. if ((len != 1 && len != 2 && len != 4 && len != 8) || (addr & ~len_mask)) {
  1179. fprintf(stderr, "qemu: tried to set invalid watchpoint at "
  1180. TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
  1181. return -EINVAL;
  1182. }
  1183. wp = qemu_malloc(sizeof(*wp));
  1184. wp->vaddr = addr;
  1185. wp->len_mask = len_mask;
  1186. wp->flags = flags;
  1187. /* keep all GDB-injected watchpoints in front */
  1188. if (flags & BP_GDB)
  1189. TAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
  1190. else
  1191. TAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
  1192. tlb_flush_page(env, addr);
  1193. if (watchpoint)
  1194. *watchpoint = wp;
  1195. return 0;
  1196. }
  1197. /* Remove a specific watchpoint. */
  1198. int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
  1199. int flags)
  1200. {
  1201. target_ulong len_mask = ~(len - 1);
  1202. CPUWatchpoint *wp;
  1203. TAILQ_FOREACH(wp, &env->watchpoints, entry) {
  1204. if (addr == wp->vaddr && len_mask == wp->len_mask
  1205. && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
  1206. cpu_watchpoint_remove_by_ref(env, wp);
  1207. return 0;
  1208. }
  1209. }
  1210. return -ENOENT;
  1211. }
  1212. /* Remove a specific watchpoint by reference. */
  1213. void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
  1214. {
  1215. TAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
  1216. tlb_flush_page(env, watchpoint->vaddr);
  1217. qemu_free(watchpoint);
  1218. }
  1219. /* Remove all matching watchpoints. */
  1220. void cpu_watchpoint_remove_all(CPUState *env, int mask)
  1221. {
  1222. CPUWatchpoint *wp, *next;
  1223. TAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
  1224. if (wp->flags & mask)
  1225. cpu_watchpoint_remove_by_ref(env, wp);
  1226. }
  1227. }
  1228. /* Add a breakpoint. */
  1229. int cpu_breakpoint_insert(CPUState *env, target_ulong pc, int flags,
  1230. CPUBreakpoint **breakpoint)
  1231. {
  1232. #if defined(TARGET_HAS_ICE)
  1233. CPUBreakpoint *bp;
  1234. bp = qemu_malloc(sizeof(*bp));
  1235. bp->pc = pc;
  1236. bp->flags = flags;
  1237. /* keep all GDB-injected breakpoints in front */
  1238. if (flags & BP_GDB)
  1239. TAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
  1240. else
  1241. TAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
  1242. breakpoint_invalidate(env, pc);
  1243. if (breakpoint)
  1244. *breakpoint = bp;
  1245. return 0;
  1246. #else
  1247. return -ENOSYS;
  1248. #endif
  1249. }
  1250. /* Remove a specific breakpoint. */
  1251. int cpu_breakpoint_remove(CPUState *env, target_ulong pc, int flags)
  1252. {
  1253. #if defined(TARGET_HAS_ICE)
  1254. CPUBreakpoint *bp;
  1255. TAILQ_FOREACH(bp, &env->breakpoints, entry) {
  1256. if (bp->pc == pc && bp->flags == flags) {
  1257. cpu_breakpoint_remove_by_ref(env, bp);
  1258. return 0;
  1259. }
  1260. }
  1261. return -ENOENT;
  1262. #else
  1263. return -ENOSYS;
  1264. #endif
  1265. }
  1266. /* Remove a specific breakpoint by reference. */
  1267. void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
  1268. {
  1269. #if defined(TARGET_HAS_ICE)
  1270. TAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
  1271. breakpoint_invalidate(env, breakpoint->pc);
  1272. qemu_free(breakpoint);
  1273. #endif
  1274. }
  1275. /* Remove all matching breakpoints. */
  1276. void cpu_breakpoint_remove_all(CPUState *env, int mask)
  1277. {
  1278. #if defined(TARGET_HAS_ICE)
  1279. CPUBreakpoint *bp, *next;
  1280. TAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
  1281. if (bp->flags & mask)
  1282. cpu_breakpoint_remove_by_ref(env, bp);
  1283. }
  1284. #endif
  1285. }
  1286. /* enable or disable single step mode. EXCP_DEBUG is returned by the
  1287. CPU loop after each instruction */
  1288. void cpu_single_step(CPUState *env, int enabled)
  1289. {
  1290. #if defined(TARGET_HAS_ICE)
  1291. if (env->singlestep_enabled != enabled) {
  1292. env->singlestep_enabled = enabled;
  1293. /* must flush all the translated code to avoid inconsistancies */
  1294. /* XXX: only flush what is necessary */
  1295. tb_flush(env);
  1296. }
  1297. #endif
  1298. }
  1299. /* enable or disable low levels log */
  1300. void cpu_set_log(int log_flags)
  1301. {
  1302. loglevel = log_flags;
  1303. if (loglevel && !logfile) {
  1304. logfile = fopen(logfilename, log_append ? "a" : "w");
  1305. if (!logfile) {
  1306. perror(logfilename);
  1307. _exit(1);
  1308. }
  1309. #if !defined(CONFIG_SOFTMMU)
  1310. /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
  1311. {
  1312. static char logfile_buf[4096];
  1313. setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
  1314. }
  1315. #else
  1316. setvbuf(logfile, NULL, _IOLBF, 0);
  1317. #endif
  1318. log_append = 1;
  1319. }
  1320. if (!loglevel && logfile) {
  1321. fclose(logfile);
  1322. logfile = NULL;
  1323. }
  1324. }
  1325. void cpu_set_log_filename(const char *filename)
  1326. {
  1327. logfilename = strdup(filename);
  1328. if (logfile) {
  1329. fclose(logfile);
  1330. logfile = NULL;
  1331. }
  1332. cpu_set_log(loglevel);
  1333. }
  1334. /* mask must never be zero, except for A20 change call */
  1335. void cpu_interrupt(CPUState *env, int mask)
  1336. {
  1337. #if !defined(USE_NPTL)
  1338. TranslationBlock *tb;
  1339. static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
  1340. #endif
  1341. int old_mask;
  1342. if (mask & CPU_INTERRUPT_EXIT) {
  1343. env->exit_request = 1;
  1344. mask &= ~CPU_INTERRUPT_EXIT;
  1345. }
  1346. old_mask = env->interrupt_request;
  1347. env->interrupt_request |= mask;
  1348. #if defined(USE_NPTL)
  1349. /* FIXME: TB unchaining isn't SMP safe. For now just ignore the
  1350. problem and hope the cpu will stop of its own accord. For userspace
  1351. emulation this often isn't actually as bad as it sounds. Often
  1352. signals are used primarily to interrupt blocking syscalls. */
  1353. #else
  1354. if (use_icount) {
  1355. env->icount_decr.u16.high = 0xffff;
  1356. #ifndef CONFIG_USER_ONLY
  1357. if (!can_do_io(env)
  1358. && (mask & ~old_mask) != 0) {
  1359. cpu_abort(env, "Raised interrupt while not in I/O function");
  1360. }
  1361. #endif
  1362. } else {
  1363. tb = env->current_tb;
  1364. /* if the cpu is currently executing code, we must unlink it and
  1365. all the potentially executing TB */
  1366. if (tb && !testandset(&interrupt_lock)) {
  1367. env->current_tb = NULL;
  1368. tb_reset_jump_recursive(tb);
  1369. resetlock(&interrupt_lock);
  1370. }
  1371. }
  1372. #endif
  1373. }
  1374. void cpu_reset_interrupt(CPUState *env, int mask)
  1375. {
  1376. env->interrupt_request &= ~mask;
  1377. }
  1378. const CPULogItem cpu_log_items[] = {
  1379. { CPU_LOG_TB_OUT_ASM, "out_asm",
  1380. "show generated host assembly code for each compiled TB" },
  1381. { CPU_LOG_TB_IN_ASM, "in_asm",
  1382. "show target assembly code for each compiled TB" },
  1383. { CPU_LOG_TB_OP, "op",
  1384. "show micro ops for each compiled TB" },
  1385. { CPU_LOG_TB_OP_OPT, "op_opt",
  1386. "show micro ops "
  1387. #ifdef TARGET_I386
  1388. "before eflags optimization and "
  1389. #endif
  1390. "after liveness analysis" },
  1391. { CPU_LOG_INT, "int",
  1392. "show interrupts/exceptions in short format" },
  1393. { CPU_LOG_EXEC, "exec",
  1394. "show trace before each executed TB (lots of logs)" },
  1395. { CPU_LOG_TB_CPU, "cpu",
  1396. "show CPU state before block translation" },
  1397. #ifdef TARGET_I386
  1398. { CPU_LOG_PCALL, "pcall",
  1399. "show protected mode far calls/returns/exceptions" },
  1400. { CPU_LOG_RESET, "cpu_reset",
  1401. "show CPU state before CPU resets" },
  1402. #endif
  1403. #ifdef DEBUG_IOPORT
  1404. { CPU_LOG_IOPORT, "ioport",
  1405. "show all i/o ports accesses" },
  1406. #endif
  1407. { 0, NULL, NULL },
  1408. };
  1409. static int cmp1(const char *s1, int n, const char *s2)
  1410. {
  1411. if (strlen(s2) != n)
  1412. return 0;
  1413. return memcmp(s1, s2, n) == 0;
  1414. }
  1415. /* takes a comma separated list of log masks. Return 0 if error. */
  1416. int cpu_str_to_log_mask(const char *str)
  1417. {
  1418. const CPULogItem *item;
  1419. int mask;
  1420. const char *p, *p1;
  1421. p = str;
  1422. mask = 0;
  1423. for(;;) {
  1424. p1 = strchr(p, ',');
  1425. if (!p1)
  1426. p1 = p + strlen(p);
  1427. if(cmp1(p,p1-p,"all")) {
  1428. for(item = cpu_log_items; item->mask != 0; item++) {
  1429. mask |= item->mask;
  1430. }
  1431. } else {
  1432. for(item = cpu_log_items; item->mask != 0; item++) {
  1433. if (cmp1(p, p1 - p, item->name))
  1434. goto found;
  1435. }
  1436. return 0;
  1437. }
  1438. found:
  1439. mask |= item->mask;
  1440. if (*p1 != ',')
  1441. break;
  1442. p = p1 + 1;
  1443. }
  1444. return mask;
  1445. }
  1446. void cpu_abort(CPUState *env, const char *fmt, ...)
  1447. {
  1448. va_list ap;
  1449. va_list ap2;
  1450. va_start(ap, fmt);
  1451. va_copy(ap2, ap);
  1452. fprintf(stderr, "qemu: fatal: ");
  1453. vfprintf(stderr, fmt, ap);
  1454. fprintf(stderr, "\n");
  1455. #ifdef TARGET_I386
  1456. cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
  1457. #else
  1458. cpu_dump_state(env, stderr, fprintf, 0);
  1459. #endif
  1460. if (qemu_log_enabled()) {
  1461. qemu_log("qemu: fatal: ");
  1462. qemu_log_vprintf(fmt, ap2);
  1463. qemu_log("\n");
  1464. #ifdef TARGET_I386
  1465. log_cpu_state(env, X86_DUMP_FPU | X86_DUMP_CCOP);
  1466. #else
  1467. log_cpu_state(env, 0);
  1468. #endif
  1469. qemu_log_flush();
  1470. qemu_log_close();
  1471. }
  1472. va_end(ap2);
  1473. va_end(ap);
  1474. abort();
  1475. }
  1476. CPUState *cpu_copy(CPUState *env)
  1477. {
  1478. CPUState *new_env = cpu_init(env->cpu_model_str);
  1479. CPUState *next_cpu = new_env->next_cpu;
  1480. int cpu_index = new_env->cpu_index;
  1481. #if defined(TARGET_HAS_ICE)
  1482. CPUBreakpoint *bp;
  1483. CPUWatchpoint *wp;
  1484. #endif
  1485. memcpy(new_env, env, sizeof(CPUState));
  1486. /* Preserve chaining and index. */
  1487. new_env->next_cpu = next_cpu;
  1488. new_env->cpu_index = cpu_index;
  1489. /* Clone all break/watchpoints.
  1490. Note: Once we support ptrace with hw-debug register access, make sure
  1491. BP_CPU break/watchpoints are handled correctly on clone. */
  1492. TAILQ_INIT(&env->breakpoints);
  1493. TAILQ_INIT(&env->watchpoints);
  1494. #if defined(TARGET_HAS_ICE)
  1495. TAILQ_FOREACH(bp, &env->breakpoints, entry) {
  1496. cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
  1497. }
  1498. TAILQ_FOREACH(wp, &env->watchpoints, entry) {
  1499. cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
  1500. wp->flags, NULL);
  1501. }
  1502. #endif
  1503. return new_env;
  1504. }
  1505. #if !defined(CONFIG_USER_ONLY)
  1506. static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
  1507. {
  1508. unsigned int i;
  1509. /* Discard jump cache entries for any tb which might potentially
  1510. overlap the flushed page. */
  1511. i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
  1512. memset (&env->tb_jmp_cache[i], 0,
  1513. TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
  1514. i = tb_jmp_cache_hash_page(addr);
  1515. memset (&env->tb_jmp_cache[i], 0,
  1516. TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
  1517. }
  1518. /* NOTE: if flush_global is true, also flush global entries (not
  1519. implemented yet) */
  1520. void tlb_flush(CPUState *env, int flush_global)
  1521. {
  1522. int i;
  1523. #if defined(DEBUG_TLB)
  1524. printf("tlb_flush:\n");
  1525. #endif
  1526. /* must reset current TB so that interrupts cannot modify the
  1527. links while we are modifying them */
  1528. env->current_tb = NULL;
  1529. for(i = 0; i < CPU_TLB_SIZE; i++) {
  1530. env->tlb_table[0][i].addr_read = -1;
  1531. env->tlb_table[0][i].addr_write = -1;
  1532. env->tlb_table[0][i].addr_code = -1;
  1533. env->tlb_table[1][i].addr_read = -1;
  1534. env->tlb_table[1][i].addr_write = -1;
  1535. env->tlb_table[1][i].addr_code = -1;
  1536. #if (NB_MMU_MODES >= 3)
  1537. env->tlb_table[2][i].addr_read = -1;
  1538. env->tlb_table[2][i].addr_write = -1;
  1539. env->tlb_table[2][i].addr_code = -1;
  1540. #if (NB_MMU_MODES == 4)
  1541. env->tlb_table[3][i].addr_read = -1;
  1542. env->tlb_table[3][i].addr_write = -1;
  1543. env->tlb_table[3][i].addr_code = -1;
  1544. #endif
  1545. #endif
  1546. }
  1547. memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
  1548. #ifdef USE_KQEMU
  1549. if (env->kqemu_enabled) {
  1550. kqemu_flush(env, flush_global);
  1551. }
  1552. #endif
  1553. tlb_flush_count++;
  1554. }
  1555. static inline void tlb_flush_entry(CPUTLBEntry *tlb_entry, target_ulong addr)
  1556. {
  1557. if (addr == (tlb_entry->addr_read &
  1558. (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
  1559. addr == (tlb_entry->addr_write &
  1560. (TARGET_PAGE_MASK | TLB_INVALID_MASK)) ||
  1561. addr == (tlb_entry->addr_code &
  1562. (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
  1563. tlb_entry->addr_read = -1;
  1564. tlb_entry->addr_write = -1;
  1565. tlb_entry->addr_code = -1;
  1566. }
  1567. }
  1568. void tlb_flush_page(CPUState *env, target_ulong addr)
  1569. {
  1570. int i;
  1571. #if defined(DEBUG_TLB)
  1572. printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
  1573. #endif
  1574. /* must reset current TB so that interrupts cannot modify the
  1575. links while we are modifying them */
  1576. env->current_tb = NULL;
  1577. addr &= TARGET_PAGE_MASK;
  1578. i = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  1579. tlb_flush_entry(&env->tlb_table[0][i], addr);
  1580. tlb_flush_entry(&env->tlb_table[1][i], addr);
  1581. #if (NB_MMU_MODES >= 3)
  1582. tlb_flush_entry(&env->tlb_table[2][i], addr);
  1583. #if (NB_MMU_MODES == 4)
  1584. tlb_flush_entry(&env->tlb_table[3][i], addr);
  1585. #endif
  1586. #endif
  1587. tlb_flush_jmp_cache(env, addr);
  1588. #ifdef USE_KQEMU
  1589. if (env->kqemu_enabled) {
  1590. kqemu_flush_page(env, addr);
  1591. }
  1592. #endif
  1593. }
  1594. /* update the TLBs so that writes to code in the virtual page 'addr'
  1595. can be detected */
  1596. static void tlb_protect_code(ram_addr_t ram_addr)
  1597. {
  1598. cpu_physical_memory_reset_dirty(ram_addr,
  1599. ram_addr + TARGET_PAGE_SIZE,
  1600. CODE_DIRTY_FLAG);
  1601. }
  1602. /* update the TLB so that writes in physical page 'phys_addr' are no longer
  1603. tested for self modifying code */
  1604. static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
  1605. target_ulong vaddr)
  1606. {
  1607. phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] |= CODE_DIRTY_FLAG;
  1608. }
  1609. static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
  1610. unsigned long start, unsigned long length)
  1611. {
  1612. unsigned long addr;
  1613. if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
  1614. addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
  1615. if ((addr - start) < length) {
  1616. tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
  1617. }
  1618. }
  1619. }
  1620. void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
  1621. int dirty_flags)
  1622. {
  1623. CPUState *env;
  1624. unsigned long length, start1;
  1625. int i, mask, len;
  1626. uint8_t *p;
  1627. start &= TARGET_PAGE_MASK;
  1628. end = TARGET_PAGE_ALIGN(end);
  1629. length = end - start;
  1630. if (length == 0)
  1631. return;
  1632. len = length >> TARGET_PAGE_BITS;
  1633. #ifdef USE_KQEMU
  1634. /* XXX: should not depend on cpu context */
  1635. env = first_cpu;
  1636. if (env->kqemu_enabled) {
  1637. ram_addr_t addr;
  1638. addr = start;
  1639. for(i = 0; i < len; i++) {
  1640. kqemu_set_notdirty(env, addr);
  1641. addr += TARGET_PAGE_SIZE;
  1642. }
  1643. }
  1644. #endif
  1645. mask = ~dirty_flags;
  1646. p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
  1647. for(i = 0; i < len; i++)
  1648. p[i] &= mask;
  1649. /* we modify the TLB cache so that the dirty bit will be set again
  1650. when accessing the range */
  1651. start1 = start + (unsigned long)phys_ram_base;
  1652. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  1653. for(i = 0; i < CPU_TLB_SIZE; i++)
  1654. tlb_reset_dirty_range(&env->tlb_table[0][i], start1, length);
  1655. for(i = 0; i < CPU_TLB_SIZE; i++)
  1656. tlb_reset_dirty_range(&env->tlb_table[1][i], start1, length);
  1657. #if (NB_MMU_MODES >= 3)
  1658. for(i = 0; i < CPU_TLB_SIZE; i++)
  1659. tlb_reset_dirty_range(&env->tlb_table[2][i], start1, length);
  1660. #if (NB_MMU_MODES == 4)
  1661. for(i = 0; i < CPU_TLB_SIZE; i++)
  1662. tlb_reset_dirty_range(&env->tlb_table[3][i], start1, length);
  1663. #endif
  1664. #endif
  1665. }
  1666. }
  1667. int cpu_physical_memory_set_dirty_tracking(int enable)
  1668. {
  1669. in_migration = enable;
  1670. return 0;
  1671. }
  1672. int cpu_physical_memory_get_dirty_tracking(void)
  1673. {
  1674. return in_migration;
  1675. }
  1676. void cpu_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr)
  1677. {
  1678. if (kvm_enabled())
  1679. kvm_physical_sync_dirty_bitmap(start_addr, end_addr);
  1680. }
  1681. static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
  1682. {
  1683. ram_addr_t ram_addr;
  1684. if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
  1685. ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
  1686. tlb_entry->addend - (unsigned long)phys_ram_base;
  1687. if (!cpu_physical_memory_is_dirty(ram_addr)) {
  1688. tlb_entry->addr_write |= TLB_NOTDIRTY;
  1689. }
  1690. }
  1691. }
  1692. /* update the TLB according to the current state of the dirty bits */
  1693. void cpu_tlb_update_dirty(CPUState *env)
  1694. {
  1695. int i;
  1696. for(i = 0; i < CPU_TLB_SIZE; i++)
  1697. tlb_update_dirty(&env->tlb_table[0][i]);
  1698. for(i = 0; i < CPU_TLB_SIZE; i++)
  1699. tlb_update_dirty(&env->tlb_table[1][i]);
  1700. #if (NB_MMU_MODES >= 3)
  1701. for(i = 0; i < CPU_TLB_SIZE; i++)
  1702. tlb_update_dirty(&env->tlb_table[2][i]);
  1703. #if (NB_MMU_MODES == 4)
  1704. for(i = 0; i < CPU_TLB_SIZE; i++)
  1705. tlb_update_dirty(&env->tlb_table[3][i]);
  1706. #endif
  1707. #endif
  1708. }
  1709. static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
  1710. {
  1711. if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
  1712. tlb_entry->addr_write = vaddr;
  1713. }
  1714. /* update the TLB corresponding to virtual page vaddr
  1715. so that it is no longer dirty */
  1716. static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
  1717. {
  1718. int i;
  1719. vaddr &= TARGET_PAGE_MASK;
  1720. i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  1721. tlb_set_dirty1(&env->tlb_table[0][i], vaddr);
  1722. tlb_set_dirty1(&env->tlb_table[1][i], vaddr);
  1723. #if (NB_MMU_MODES >= 3)
  1724. tlb_set_dirty1(&env->tlb_table[2][i], vaddr);
  1725. #if (NB_MMU_MODES == 4)
  1726. tlb_set_dirty1(&env->tlb_table[3][i], vaddr);
  1727. #endif
  1728. #endif
  1729. }
  1730. /* add a new TLB entry. At most one entry for a given virtual address
  1731. is permitted. Return 0 if OK or 2 if the page could not be mapped
  1732. (can only happen in non SOFTMMU mode for I/O pages or pages
  1733. conflicting with the host address space). */
  1734. int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
  1735. target_phys_addr_t paddr, int prot,
  1736. int mmu_idx, int is_softmmu)
  1737. {
  1738. PhysPageDesc *p;
  1739. unsigned long pd;
  1740. unsigned int index;
  1741. target_ulong address;
  1742. target_ulong code_address;
  1743. target_phys_addr_t addend;
  1744. int ret;
  1745. CPUTLBEntry *te;
  1746. CPUWatchpoint *wp;
  1747. target_phys_addr_t iotlb;
  1748. p = phys_page_find(paddr >> TARGET_PAGE_BITS);
  1749. if (!p) {
  1750. pd = IO_MEM_UNASSIGNED;
  1751. } else {
  1752. pd = p->phys_offset;
  1753. }
  1754. #if defined(DEBUG_TLB)
  1755. printf("tlb_set_page: vaddr=" TARGET_FMT_lx " paddr=0x%08x prot=%x idx=%d smmu=%d pd=0x%08lx\n",
  1756. vaddr, (int)paddr, prot, mmu_idx, is_softmmu, pd);
  1757. #endif
  1758. ret = 0;
  1759. address = vaddr;
  1760. if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
  1761. /* IO memory case (romd handled later) */
  1762. address |= TLB_MMIO;
  1763. }
  1764. addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
  1765. if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
  1766. /* Normal RAM. */
  1767. iotlb = pd & TARGET_PAGE_MASK;
  1768. if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
  1769. iotlb |= IO_MEM_NOTDIRTY;
  1770. else
  1771. iotlb |= IO_MEM_ROM;
  1772. } else {
  1773. /* IO handlers are currently passed a phsical address.
  1774. It would be nice to pass an offset from the base address
  1775. of that region. This would avoid having to special case RAM,
  1776. and avoid full address decoding in every device.
  1777. We can't use the high bits of pd for this because
  1778. IO_MEM_ROMD uses these as a ram address. */
  1779. iotlb = (pd & ~TARGET_PAGE_MASK);
  1780. if (p) {
  1781. iotlb += p->region_offset;
  1782. } else {
  1783. iotlb += paddr;
  1784. }
  1785. }
  1786. code_address = address;
  1787. /* Make accesses to pages with watchpoints go via the
  1788. watchpoint trap routines. */
  1789. TAILQ_FOREACH(wp, &env->watchpoints, entry) {
  1790. if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
  1791. iotlb = io_mem_watch + paddr;
  1792. /* TODO: The memory case can be optimized by not trapping
  1793. reads of pages with a write breakpoint. */
  1794. address |= TLB_MMIO;
  1795. }
  1796. }
  1797. index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
  1798. env->iotlb[mmu_idx][index] = iotlb - vaddr;
  1799. te = &env->tlb_table[mmu_idx][index];
  1800. te->addend = addend - vaddr;
  1801. if (prot & PAGE_READ) {
  1802. te->addr_read = address;
  1803. } else {
  1804. te->addr_read = -1;
  1805. }
  1806. if (prot & PAGE_EXEC) {
  1807. te->addr_code = code_address;
  1808. } else {
  1809. te->addr_code = -1;
  1810. }
  1811. if (prot & PAGE_WRITE) {
  1812. if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
  1813. (pd & IO_MEM_ROMD)) {
  1814. /* Write access calls the I/O callback. */
  1815. te->addr_write = address | TLB_MMIO;
  1816. } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
  1817. !cpu_physical_memory_is_dirty(pd)) {
  1818. te->addr_write = address | TLB_NOTDIRTY;
  1819. } else {
  1820. te->addr_write = address;
  1821. }
  1822. } else {
  1823. te->addr_write = -1;
  1824. }
  1825. return ret;
  1826. }
  1827. #else
  1828. void tlb_flush(CPUState *env, int flush_global)
  1829. {
  1830. }
  1831. void tlb_flush_page(CPUState *env, target_ulong addr)
  1832. {
  1833. }
  1834. int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
  1835. target_phys_addr_t paddr, int prot,
  1836. int mmu_idx, int is_softmmu)
  1837. {
  1838. return 0;
  1839. }
  1840. /* dump memory mappings */
  1841. void page_dump(FILE *f)
  1842. {
  1843. unsigned long start, end;
  1844. int i, j, prot, prot1;
  1845. PageDesc *p;
  1846. fprintf(f, "%-8s %-8s %-8s %s\n",
  1847. "start", "end", "size", "prot");
  1848. start = -1;
  1849. end = -1;
  1850. prot = 0;
  1851. for(i = 0; i <= L1_SIZE; i++) {
  1852. if (i < L1_SIZE)
  1853. p = l1_map[i];
  1854. else
  1855. p = NULL;
  1856. for(j = 0;j < L2_SIZE; j++) {
  1857. if (!p)
  1858. prot1 = 0;
  1859. else
  1860. prot1 = p[j].flags;
  1861. if (prot1 != prot) {
  1862. end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
  1863. if (start != -1) {
  1864. fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
  1865. start, end, end - start,
  1866. prot & PAGE_READ ? 'r' : '-',
  1867. prot & PAGE_WRITE ? 'w' : '-',
  1868. prot & PAGE_EXEC ? 'x' : '-');
  1869. }
  1870. if (prot1 != 0)
  1871. start = end;
  1872. else
  1873. start = -1;
  1874. prot = prot1;
  1875. }
  1876. if (!p)
  1877. break;
  1878. }
  1879. }
  1880. }
  1881. int page_get_flags(target_ulong address)
  1882. {
  1883. PageDesc *p;
  1884. p = page_find(address >> TARGET_PAGE_BITS);
  1885. if (!p)
  1886. return 0;
  1887. return p->flags;
  1888. }
  1889. /* modify the flags of a page and invalidate the code if
  1890. necessary. The flag PAGE_WRITE_ORG is positionned automatically
  1891. depending on PAGE_WRITE */
  1892. void page_set_flags(target_ulong start, target_ulong end, int flags)
  1893. {
  1894. PageDesc *p;
  1895. target_ulong addr;
  1896. /* mmap_lock should already be held. */
  1897. start = start & TARGET_PAGE_MASK;
  1898. end = TARGET_PAGE_ALIGN(end);
  1899. if (flags & PAGE_WRITE)
  1900. flags |= PAGE_WRITE_ORG;
  1901. for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
  1902. p = page_find_alloc(addr >> TARGET_PAGE_BITS);
  1903. /* We may be called for host regions that are outside guest
  1904. address space. */
  1905. if (!p)
  1906. return;
  1907. /* if the write protection is set, then we invalidate the code
  1908. inside */
  1909. if (!(p->flags & PAGE_WRITE) &&
  1910. (flags & PAGE_WRITE) &&
  1911. p->first_tb) {
  1912. tb_invalidate_phys_page(addr, 0, NULL);
  1913. }
  1914. p->flags = flags;
  1915. }
  1916. }
  1917. int page_check_range(target_ulong start, target_ulong len, int flags)
  1918. {
  1919. PageDesc *p;
  1920. target_ulong end;
  1921. target_ulong addr;
  1922. if (start + len < start)
  1923. /* we've wrapped around */
  1924. return -1;
  1925. end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
  1926. start = start & TARGET_PAGE_MASK;
  1927. for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
  1928. p = page_find(addr >> TARGET_PAGE_BITS);
  1929. if( !p )
  1930. return -1;
  1931. if( !(p->flags & PAGE_VALID) )
  1932. return -1;
  1933. if ((flags & PAGE_READ) && !(p->flags & PAGE_READ))
  1934. return -1;
  1935. if (flags & PAGE_WRITE) {
  1936. if (!(p->flags & PAGE_WRITE_ORG))
  1937. return -1;
  1938. /* unprotect the page if it was put read-only because it
  1939. contains translated code */
  1940. if (!(p->flags & PAGE_WRITE)) {
  1941. if (!page_unprotect(addr, 0, NULL))
  1942. return -1;
  1943. }
  1944. return 0;
  1945. }
  1946. }
  1947. return 0;
  1948. }
  1949. /* called from signal handler: invalidate the code and unprotect the
  1950. page. Return TRUE if the fault was succesfully handled. */
  1951. int page_unprotect(target_ulong address, unsigned long pc, void *puc)
  1952. {
  1953. unsigned int page_index, prot, pindex;
  1954. PageDesc *p, *p1;
  1955. target_ulong host_start, host_end, addr;
  1956. /* Technically this isn't safe inside a signal handler. However we
  1957. know this only ever happens in a synchronous SEGV handler, so in
  1958. practice it seems to be ok. */
  1959. mmap_lock();
  1960. host_start = address & qemu_host_page_mask;
  1961. page_index = host_start >> TARGET_PAGE_BITS;
  1962. p1 = page_find(page_index);
  1963. if (!p1) {
  1964. mmap_unlock();
  1965. return 0;
  1966. }
  1967. host_end = host_start + qemu_host_page_size;
  1968. p = p1;
  1969. prot = 0;
  1970. for(addr = host_start;addr < host_end; addr += TARGET_PAGE_SIZE) {
  1971. prot |= p->flags;
  1972. p++;
  1973. }
  1974. /* if the page was really writable, then we change its
  1975. protection back to writable */
  1976. if (prot & PAGE_WRITE_ORG) {
  1977. pindex = (address - host_start) >> TARGET_PAGE_BITS;
  1978. if (!(p1[pindex].flags & PAGE_WRITE)) {
  1979. mprotect((void *)g2h(host_start), qemu_host_page_size,
  1980. (prot & PAGE_BITS) | PAGE_WRITE);
  1981. p1[pindex].flags |= PAGE_WRITE;
  1982. /* and since the content will be modified, we must invalidate
  1983. the corresponding translated code. */
  1984. tb_invalidate_phys_page(address, pc, puc);
  1985. #ifdef DEBUG_TB_CHECK
  1986. tb_invalidate_check(address);
  1987. #endif
  1988. mmap_unlock();
  1989. return 1;
  1990. }
  1991. }
  1992. mmap_unlock();
  1993. return 0;
  1994. }
  1995. static inline void tlb_set_dirty(CPUState *env,
  1996. unsigned long addr, target_ulong vaddr)
  1997. {
  1998. }
  1999. #endif /* defined(CONFIG_USER_ONLY) */
  2000. #if !defined(CONFIG_USER_ONLY)
  2001. static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
  2002. ram_addr_t memory, ram_addr_t region_offset);
  2003. static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
  2004. ram_addr_t orig_memory, ram_addr_t region_offset);
  2005. #define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
  2006. need_subpage) \
  2007. do { \
  2008. if (addr > start_addr) \
  2009. start_addr2 = 0; \
  2010. else { \
  2011. start_addr2 = start_addr & ~TARGET_PAGE_MASK; \
  2012. if (start_addr2 > 0) \
  2013. need_subpage = 1; \
  2014. } \
  2015. \
  2016. if ((start_addr + orig_size) - addr >= TARGET_PAGE_SIZE) \
  2017. end_addr2 = TARGET_PAGE_SIZE - 1; \
  2018. else { \
  2019. end_addr2 = (start_addr + orig_size - 1) & ~TARGET_PAGE_MASK; \
  2020. if (end_addr2 < TARGET_PAGE_SIZE - 1) \
  2021. need_subpage = 1; \
  2022. } \
  2023. } while (0)
  2024. /* register physical memory. 'size' must be a multiple of the target
  2025. page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
  2026. io memory page. The address used when calling the IO function is
  2027. the offset from the start of the region, plus region_offset. Both
  2028. start_region and regon_offset are rounded down to a page boundary
  2029. before calculating this offset. This should not be a problem unless
  2030. the low bits of start_addr and region_offset differ. */
  2031. void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,
  2032. ram_addr_t size,
  2033. ram_addr_t phys_offset,
  2034. ram_addr_t region_offset)
  2035. {
  2036. target_phys_addr_t addr, end_addr;
  2037. PhysPageDesc *p;
  2038. CPUState *env;
  2039. ram_addr_t orig_size = size;
  2040. void *subpage;
  2041. #ifdef USE_KQEMU
  2042. /* XXX: should not depend on cpu context */
  2043. env = first_cpu;
  2044. if (env->kqemu_enabled) {
  2045. kqemu_set_phys_mem(start_addr, size, phys_offset);
  2046. }
  2047. #endif
  2048. if (kvm_enabled())
  2049. kvm_set_phys_mem(start_addr, size, phys_offset);
  2050. if (phys_offset == IO_MEM_UNASSIGNED) {
  2051. region_offset = start_addr;
  2052. }
  2053. region_offset &= TARGET_PAGE_MASK;
  2054. size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
  2055. end_addr = start_addr + (target_phys_addr_t)size;
  2056. for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
  2057. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  2058. if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
  2059. ram_addr_t orig_memory = p->phys_offset;
  2060. target_phys_addr_t start_addr2, end_addr2;
  2061. int need_subpage = 0;
  2062. CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
  2063. need_subpage);
  2064. if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
  2065. if (!(orig_memory & IO_MEM_SUBPAGE)) {
  2066. subpage = subpage_init((addr & TARGET_PAGE_MASK),
  2067. &p->phys_offset, orig_memory,
  2068. p->region_offset);
  2069. } else {
  2070. subpage = io_mem_opaque[(orig_memory & ~TARGET_PAGE_MASK)
  2071. >> IO_MEM_SHIFT];
  2072. }
  2073. subpage_register(subpage, start_addr2, end_addr2, phys_offset,
  2074. region_offset);
  2075. p->region_offset = 0;
  2076. } else {
  2077. p->phys_offset = phys_offset;
  2078. if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
  2079. (phys_offset & IO_MEM_ROMD))
  2080. phys_offset += TARGET_PAGE_SIZE;
  2081. }
  2082. } else {
  2083. p = phys_page_find_alloc(addr >> TARGET_PAGE_BITS, 1);
  2084. p->phys_offset = phys_offset;
  2085. p->region_offset = region_offset;
  2086. if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM ||
  2087. (phys_offset & IO_MEM_ROMD)) {
  2088. phys_offset += TARGET_PAGE_SIZE;
  2089. } else {
  2090. target_phys_addr_t start_addr2, end_addr2;
  2091. int need_subpage = 0;
  2092. CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
  2093. end_addr2, need_subpage);
  2094. if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
  2095. subpage = subpage_init((addr & TARGET_PAGE_MASK),
  2096. &p->phys_offset, IO_MEM_UNASSIGNED,
  2097. addr & TARGET_PAGE_MASK);
  2098. subpage_register(subpage, start_addr2, end_addr2,
  2099. phys_offset, region_offset);
  2100. p->region_offset = 0;
  2101. }
  2102. }
  2103. }
  2104. region_offset += TARGET_PAGE_SIZE;
  2105. }
  2106. /* since each CPU stores ram addresses in its TLB cache, we must
  2107. reset the modified entries */
  2108. /* XXX: slow ! */
  2109. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  2110. tlb_flush(env, 1);
  2111. }
  2112. }
  2113. /* XXX: temporary until new memory mapping API */
  2114. ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
  2115. {
  2116. PhysPageDesc *p;
  2117. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  2118. if (!p)
  2119. return IO_MEM_UNASSIGNED;
  2120. return p->phys_offset;
  2121. }
  2122. void qemu_register_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
  2123. {
  2124. if (kvm_enabled())
  2125. kvm_coalesce_mmio_region(addr, size);
  2126. }
  2127. void qemu_unregister_coalesced_mmio(target_phys_addr_t addr, ram_addr_t size)
  2128. {
  2129. if (kvm_enabled())
  2130. kvm_uncoalesce_mmio_region(addr, size);
  2131. }
  2132. /* XXX: better than nothing */
  2133. ram_addr_t qemu_ram_alloc(ram_addr_t size)
  2134. {
  2135. ram_addr_t addr;
  2136. if ((phys_ram_alloc_offset + size) > phys_ram_size) {
  2137. fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 ")\n",
  2138. (uint64_t)size, (uint64_t)phys_ram_size);
  2139. abort();
  2140. }
  2141. addr = phys_ram_alloc_offset;
  2142. phys_ram_alloc_offset = TARGET_PAGE_ALIGN(phys_ram_alloc_offset + size);
  2143. if (kvm_enabled())
  2144. kvm_setup_guest_memory(phys_ram_base + addr, size);
  2145. return addr;
  2146. }
  2147. void qemu_ram_free(ram_addr_t addr)
  2148. {
  2149. }
  2150. static uint32_t unassigned_mem_readb(void *opaque, target_phys_addr_t addr)
  2151. {
  2152. #ifdef DEBUG_UNASSIGNED
  2153. printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
  2154. #endif
  2155. #if defined(TARGET_SPARC)
  2156. do_unassigned_access(addr, 0, 0, 0, 1);
  2157. #endif
  2158. return 0;
  2159. }
  2160. static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
  2161. {
  2162. #ifdef DEBUG_UNASSIGNED
  2163. printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
  2164. #endif
  2165. #if defined(TARGET_SPARC)
  2166. do_unassigned_access(addr, 0, 0, 0, 2);
  2167. #endif
  2168. return 0;
  2169. }
  2170. static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
  2171. {
  2172. #ifdef DEBUG_UNASSIGNED
  2173. printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
  2174. #endif
  2175. #if defined(TARGET_SPARC)
  2176. do_unassigned_access(addr, 0, 0, 0, 4);
  2177. #endif
  2178. return 0;
  2179. }
  2180. static void unassigned_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  2181. {
  2182. #ifdef DEBUG_UNASSIGNED
  2183. printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
  2184. #endif
  2185. #if defined(TARGET_SPARC)
  2186. do_unassigned_access(addr, 1, 0, 0, 1);
  2187. #endif
  2188. }
  2189. static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
  2190. {
  2191. #ifdef DEBUG_UNASSIGNED
  2192. printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
  2193. #endif
  2194. #if defined(TARGET_SPARC)
  2195. do_unassigned_access(addr, 1, 0, 0, 2);
  2196. #endif
  2197. }
  2198. static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  2199. {
  2200. #ifdef DEBUG_UNASSIGNED
  2201. printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
  2202. #endif
  2203. #if defined(TARGET_SPARC)
  2204. do_unassigned_access(addr, 1, 0, 0, 4);
  2205. #endif
  2206. }
  2207. static CPUReadMemoryFunc *unassigned_mem_read[3] = {
  2208. unassigned_mem_readb,
  2209. unassigned_mem_readw,
  2210. unassigned_mem_readl,
  2211. };
  2212. static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
  2213. unassigned_mem_writeb,
  2214. unassigned_mem_writew,
  2215. unassigned_mem_writel,
  2216. };
  2217. static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
  2218. uint32_t val)
  2219. {
  2220. int dirty_flags;
  2221. dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
  2222. if (!(dirty_flags & CODE_DIRTY_FLAG)) {
  2223. #if !defined(CONFIG_USER_ONLY)
  2224. tb_invalidate_phys_page_fast(ram_addr, 1);
  2225. dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
  2226. #endif
  2227. }
  2228. stb_p(phys_ram_base + ram_addr, val);
  2229. #ifdef USE_KQEMU
  2230. if (cpu_single_env->kqemu_enabled &&
  2231. (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
  2232. kqemu_modify_page(cpu_single_env, ram_addr);
  2233. #endif
  2234. dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
  2235. phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
  2236. /* we remove the notdirty callback only if the code has been
  2237. flushed */
  2238. if (dirty_flags == 0xff)
  2239. tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
  2240. }
  2241. static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
  2242. uint32_t val)
  2243. {
  2244. int dirty_flags;
  2245. dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
  2246. if (!(dirty_flags & CODE_DIRTY_FLAG)) {
  2247. #if !defined(CONFIG_USER_ONLY)
  2248. tb_invalidate_phys_page_fast(ram_addr, 2);
  2249. dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
  2250. #endif
  2251. }
  2252. stw_p(phys_ram_base + ram_addr, val);
  2253. #ifdef USE_KQEMU
  2254. if (cpu_single_env->kqemu_enabled &&
  2255. (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
  2256. kqemu_modify_page(cpu_single_env, ram_addr);
  2257. #endif
  2258. dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
  2259. phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
  2260. /* we remove the notdirty callback only if the code has been
  2261. flushed */
  2262. if (dirty_flags == 0xff)
  2263. tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
  2264. }
  2265. static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
  2266. uint32_t val)
  2267. {
  2268. int dirty_flags;
  2269. dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
  2270. if (!(dirty_flags & CODE_DIRTY_FLAG)) {
  2271. #if !defined(CONFIG_USER_ONLY)
  2272. tb_invalidate_phys_page_fast(ram_addr, 4);
  2273. dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
  2274. #endif
  2275. }
  2276. stl_p(phys_ram_base + ram_addr, val);
  2277. #ifdef USE_KQEMU
  2278. if (cpu_single_env->kqemu_enabled &&
  2279. (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
  2280. kqemu_modify_page(cpu_single_env, ram_addr);
  2281. #endif
  2282. dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
  2283. phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
  2284. /* we remove the notdirty callback only if the code has been
  2285. flushed */
  2286. if (dirty_flags == 0xff)
  2287. tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
  2288. }
  2289. static CPUReadMemoryFunc *error_mem_read[3] = {
  2290. NULL, /* never used */
  2291. NULL, /* never used */
  2292. NULL, /* never used */
  2293. };
  2294. static CPUWriteMemoryFunc *notdirty_mem_write[3] = {
  2295. notdirty_mem_writeb,
  2296. notdirty_mem_writew,
  2297. notdirty_mem_writel,
  2298. };
  2299. /* Generate a debug exception if a watchpoint has been hit. */
  2300. static void check_watchpoint(int offset, int len_mask, int flags)
  2301. {
  2302. CPUState *env = cpu_single_env;
  2303. target_ulong pc, cs_base;
  2304. TranslationBlock *tb;
  2305. target_ulong vaddr;
  2306. CPUWatchpoint *wp;
  2307. int cpu_flags;
  2308. if (env->watchpoint_hit) {
  2309. /* We re-entered the check after replacing the TB. Now raise
  2310. * the debug interrupt so that is will trigger after the
  2311. * current instruction. */
  2312. cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
  2313. return;
  2314. }
  2315. vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
  2316. TAILQ_FOREACH(wp, &env->watchpoints, entry) {
  2317. if ((vaddr == (wp->vaddr & len_mask) ||
  2318. (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
  2319. wp->flags |= BP_WATCHPOINT_HIT;
  2320. if (!env->watchpoint_hit) {
  2321. env->watchpoint_hit = wp;
  2322. tb = tb_find_pc(env->mem_io_pc);
  2323. if (!tb) {
  2324. cpu_abort(env, "check_watchpoint: could not find TB for "
  2325. "pc=%p", (void *)env->mem_io_pc);
  2326. }
  2327. cpu_restore_state(tb, env, env->mem_io_pc, NULL);
  2328. tb_phys_invalidate(tb, -1);
  2329. if (wp->flags & BP_STOP_BEFORE_ACCESS) {
  2330. env->exception_index = EXCP_DEBUG;
  2331. } else {
  2332. cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
  2333. tb_gen_code(env, pc, cs_base, cpu_flags, 1);
  2334. }
  2335. cpu_resume_from_signal(env, NULL);
  2336. }
  2337. } else {
  2338. wp->flags &= ~BP_WATCHPOINT_HIT;
  2339. }
  2340. }
  2341. }
  2342. /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
  2343. so these check for a hit then pass through to the normal out-of-line
  2344. phys routines. */
  2345. static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
  2346. {
  2347. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_READ);
  2348. return ldub_phys(addr);
  2349. }
  2350. static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
  2351. {
  2352. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_READ);
  2353. return lduw_phys(addr);
  2354. }
  2355. static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
  2356. {
  2357. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);
  2358. return ldl_phys(addr);
  2359. }
  2360. static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
  2361. uint32_t val)
  2362. {
  2363. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x0, BP_MEM_WRITE);
  2364. stb_phys(addr, val);
  2365. }
  2366. static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
  2367. uint32_t val)
  2368. {
  2369. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x1, BP_MEM_WRITE);
  2370. stw_phys(addr, val);
  2371. }
  2372. static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
  2373. uint32_t val)
  2374. {
  2375. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~0x3, BP_MEM_WRITE);
  2376. stl_phys(addr, val);
  2377. }
  2378. static CPUReadMemoryFunc *watch_mem_read[3] = {
  2379. watch_mem_readb,
  2380. watch_mem_readw,
  2381. watch_mem_readl,
  2382. };
  2383. static CPUWriteMemoryFunc *watch_mem_write[3] = {
  2384. watch_mem_writeb,
  2385. watch_mem_writew,
  2386. watch_mem_writel,
  2387. };
  2388. static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
  2389. unsigned int len)
  2390. {
  2391. uint32_t ret;
  2392. unsigned int idx;
  2393. idx = SUBPAGE_IDX(addr);
  2394. #if defined(DEBUG_SUBPAGE)
  2395. printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
  2396. mmio, len, addr, idx);
  2397. #endif
  2398. ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len],
  2399. addr + mmio->region_offset[idx][0][len]);
  2400. return ret;
  2401. }
  2402. static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
  2403. uint32_t value, unsigned int len)
  2404. {
  2405. unsigned int idx;
  2406. idx = SUBPAGE_IDX(addr);
  2407. #if defined(DEBUG_SUBPAGE)
  2408. printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
  2409. mmio, len, addr, idx, value);
  2410. #endif
  2411. (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len],
  2412. addr + mmio->region_offset[idx][1][len],
  2413. value);
  2414. }
  2415. static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
  2416. {
  2417. #if defined(DEBUG_SUBPAGE)
  2418. printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
  2419. #endif
  2420. return subpage_readlen(opaque, addr, 0);
  2421. }
  2422. static void subpage_writeb (void *opaque, target_phys_addr_t addr,
  2423. uint32_t value)
  2424. {
  2425. #if defined(DEBUG_SUBPAGE)
  2426. printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
  2427. #endif
  2428. subpage_writelen(opaque, addr, value, 0);
  2429. }
  2430. static uint32_t subpage_readw (void *opaque, target_phys_addr_t addr)
  2431. {
  2432. #if defined(DEBUG_SUBPAGE)
  2433. printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
  2434. #endif
  2435. return subpage_readlen(opaque, addr, 1);
  2436. }
  2437. static void subpage_writew (void *opaque, target_phys_addr_t addr,
  2438. uint32_t value)
  2439. {
  2440. #if defined(DEBUG_SUBPAGE)
  2441. printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
  2442. #endif
  2443. subpage_writelen(opaque, addr, value, 1);
  2444. }
  2445. static uint32_t subpage_readl (void *opaque, target_phys_addr_t addr)
  2446. {
  2447. #if defined(DEBUG_SUBPAGE)
  2448. printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
  2449. #endif
  2450. return subpage_readlen(opaque, addr, 2);
  2451. }
  2452. static void subpage_writel (void *opaque,
  2453. target_phys_addr_t addr, uint32_t value)
  2454. {
  2455. #if defined(DEBUG_SUBPAGE)
  2456. printf("%s: addr " TARGET_FMT_plx " val %08x\n", __func__, addr, value);
  2457. #endif
  2458. subpage_writelen(opaque, addr, value, 2);
  2459. }
  2460. static CPUReadMemoryFunc *subpage_read[] = {
  2461. &subpage_readb,
  2462. &subpage_readw,
  2463. &subpage_readl,
  2464. };
  2465. static CPUWriteMemoryFunc *subpage_write[] = {
  2466. &subpage_writeb,
  2467. &subpage_writew,
  2468. &subpage_writel,
  2469. };
  2470. static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
  2471. ram_addr_t memory, ram_addr_t region_offset)
  2472. {
  2473. int idx, eidx;
  2474. unsigned int i;
  2475. if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
  2476. return -1;
  2477. idx = SUBPAGE_IDX(start);
  2478. eidx = SUBPAGE_IDX(end);
  2479. #if defined(DEBUG_SUBPAGE)
  2480. printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
  2481. mmio, start, end, idx, eidx, memory);
  2482. #endif
  2483. memory >>= IO_MEM_SHIFT;
  2484. for (; idx <= eidx; idx++) {
  2485. for (i = 0; i < 4; i++) {
  2486. if (io_mem_read[memory][i]) {
  2487. mmio->mem_read[idx][i] = &io_mem_read[memory][i];
  2488. mmio->opaque[idx][0][i] = io_mem_opaque[memory];
  2489. mmio->region_offset[idx][0][i] = region_offset;
  2490. }
  2491. if (io_mem_write[memory][i]) {
  2492. mmio->mem_write[idx][i] = &io_mem_write[memory][i];
  2493. mmio->opaque[idx][1][i] = io_mem_opaque[memory];
  2494. mmio->region_offset[idx][1][i] = region_offset;
  2495. }
  2496. }
  2497. }
  2498. return 0;
  2499. }
  2500. static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
  2501. ram_addr_t orig_memory, ram_addr_t region_offset)
  2502. {
  2503. subpage_t *mmio;
  2504. int subpage_memory;
  2505. mmio = qemu_mallocz(sizeof(subpage_t));
  2506. mmio->base = base;
  2507. subpage_memory = cpu_register_io_memory(0, subpage_read, subpage_write, mmio);
  2508. #if defined(DEBUG_SUBPAGE)
  2509. printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
  2510. mmio, base, TARGET_PAGE_SIZE, subpage_memory);
  2511. #endif
  2512. *phys = subpage_memory | IO_MEM_SUBPAGE;
  2513. subpage_register(mmio, 0, TARGET_PAGE_SIZE - 1, orig_memory,
  2514. region_offset);
  2515. return mmio;
  2516. }
  2517. static int get_free_io_mem_idx(void)
  2518. {
  2519. int i;
  2520. for (i = 0; i<IO_MEM_NB_ENTRIES; i++)
  2521. if (!io_mem_used[i]) {
  2522. io_mem_used[i] = 1;
  2523. return i;
  2524. }
  2525. return -1;
  2526. }
  2527. static void io_mem_init(void)
  2528. {
  2529. int i;
  2530. cpu_register_io_memory(IO_MEM_ROM >> IO_MEM_SHIFT, error_mem_read, unassigned_mem_write, NULL);
  2531. cpu_register_io_memory(IO_MEM_UNASSIGNED >> IO_MEM_SHIFT, unassigned_mem_read, unassigned_mem_write, NULL);
  2532. cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
  2533. for (i=0; i<5; i++)
  2534. io_mem_used[i] = 1;
  2535. io_mem_watch = cpu_register_io_memory(0, watch_mem_read,
  2536. watch_mem_write, NULL);
  2537. /* alloc dirty bits array */
  2538. phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
  2539. memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
  2540. }
  2541. /* mem_read and mem_write are arrays of functions containing the
  2542. function to access byte (index 0), word (index 1) and dword (index
  2543. 2). Functions can be omitted with a NULL function pointer. The
  2544. registered functions may be modified dynamically later.
  2545. If io_index is non zero, the corresponding io zone is
  2546. modified. If it is zero, a new io zone is allocated. The return
  2547. value can be used with cpu_register_physical_memory(). (-1) is
  2548. returned if error. */
  2549. int cpu_register_io_memory(int io_index,
  2550. CPUReadMemoryFunc **mem_read,
  2551. CPUWriteMemoryFunc **mem_write,
  2552. void *opaque)
  2553. {
  2554. int i, subwidth = 0;
  2555. if (io_index <= 0) {
  2556. io_index = get_free_io_mem_idx();
  2557. if (io_index == -1)
  2558. return io_index;
  2559. } else {
  2560. if (io_index >= IO_MEM_NB_ENTRIES)
  2561. return -1;
  2562. }
  2563. for(i = 0;i < 3; i++) {
  2564. if (!mem_read[i] || !mem_write[i])
  2565. subwidth = IO_MEM_SUBWIDTH;
  2566. io_mem_read[io_index][i] = mem_read[i];
  2567. io_mem_write[io_index][i] = mem_write[i];
  2568. }
  2569. io_mem_opaque[io_index] = opaque;
  2570. return (io_index << IO_MEM_SHIFT) | subwidth;
  2571. }
  2572. void cpu_unregister_io_memory(int io_table_address)
  2573. {
  2574. int i;
  2575. int io_index = io_table_address >> IO_MEM_SHIFT;
  2576. for (i=0;i < 3; i++) {
  2577. io_mem_read[io_index][i] = unassigned_mem_read[i];
  2578. io_mem_write[io_index][i] = unassigned_mem_write[i];
  2579. }
  2580. io_mem_opaque[io_index] = NULL;
  2581. io_mem_used[io_index] = 0;
  2582. }
  2583. CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
  2584. {
  2585. return io_mem_write[io_index >> IO_MEM_SHIFT];
  2586. }
  2587. CPUReadMemoryFunc **cpu_get_io_memory_read(int io_index)
  2588. {
  2589. return io_mem_read[io_index >> IO_MEM_SHIFT];
  2590. }
  2591. #endif /* !defined(CONFIG_USER_ONLY) */
  2592. /* physical memory access (slow version, mainly for debug) */
  2593. #if defined(CONFIG_USER_ONLY)
  2594. void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
  2595. int len, int is_write)
  2596. {
  2597. int l, flags;
  2598. target_ulong page;
  2599. void * p;
  2600. while (len > 0) {
  2601. page = addr & TARGET_PAGE_MASK;
  2602. l = (page + TARGET_PAGE_SIZE) - addr;
  2603. if (l > len)
  2604. l = len;
  2605. flags = page_get_flags(page);
  2606. if (!(flags & PAGE_VALID))
  2607. return;
  2608. if (is_write) {
  2609. if (!(flags & PAGE_WRITE))
  2610. return;
  2611. /* XXX: this code should not depend on lock_user */
  2612. if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
  2613. /* FIXME - should this return an error rather than just fail? */
  2614. return;
  2615. memcpy(p, buf, l);
  2616. unlock_user(p, addr, l);
  2617. } else {
  2618. if (!(flags & PAGE_READ))
  2619. return;
  2620. /* XXX: this code should not depend on lock_user */
  2621. if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
  2622. /* FIXME - should this return an error rather than just fail? */
  2623. return;
  2624. memcpy(buf, p, l);
  2625. unlock_user(p, addr, 0);
  2626. }
  2627. len -= l;
  2628. buf += l;
  2629. addr += l;
  2630. }
  2631. }
  2632. #else
  2633. void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
  2634. int len, int is_write)
  2635. {
  2636. int l, io_index;
  2637. uint8_t *ptr;
  2638. uint32_t val;
  2639. target_phys_addr_t page;
  2640. unsigned long pd;
  2641. PhysPageDesc *p;
  2642. while (len > 0) {
  2643. page = addr & TARGET_PAGE_MASK;
  2644. l = (page + TARGET_PAGE_SIZE) - addr;
  2645. if (l > len)
  2646. l = len;
  2647. p = phys_page_find(page >> TARGET_PAGE_BITS);
  2648. if (!p) {
  2649. pd = IO_MEM_UNASSIGNED;
  2650. } else {
  2651. pd = p->phys_offset;
  2652. }
  2653. if (is_write) {
  2654. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  2655. target_phys_addr_t addr1 = addr;
  2656. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  2657. if (p)
  2658. addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  2659. /* XXX: could force cpu_single_env to NULL to avoid
  2660. potential bugs */
  2661. if (l >= 4 && ((addr1 & 3) == 0)) {
  2662. /* 32 bit write access */
  2663. val = ldl_p(buf);
  2664. io_mem_write[io_index][2](io_mem_opaque[io_index], addr1, val);
  2665. l = 4;
  2666. } else if (l >= 2 && ((addr1 & 1) == 0)) {
  2667. /* 16 bit write access */
  2668. val = lduw_p(buf);
  2669. io_mem_write[io_index][1](io_mem_opaque[io_index], addr1, val);
  2670. l = 2;
  2671. } else {
  2672. /* 8 bit write access */
  2673. val = ldub_p(buf);
  2674. io_mem_write[io_index][0](io_mem_opaque[io_index], addr1, val);
  2675. l = 1;
  2676. }
  2677. } else {
  2678. unsigned long addr1;
  2679. addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  2680. /* RAM case */
  2681. ptr = phys_ram_base + addr1;
  2682. memcpy(ptr, buf, l);
  2683. if (!cpu_physical_memory_is_dirty(addr1)) {
  2684. /* invalidate code */
  2685. tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
  2686. /* set dirty bit */
  2687. phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
  2688. (0xff & ~CODE_DIRTY_FLAG);
  2689. }
  2690. }
  2691. } else {
  2692. if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
  2693. !(pd & IO_MEM_ROMD)) {
  2694. target_phys_addr_t addr1 = addr;
  2695. /* I/O case */
  2696. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  2697. if (p)
  2698. addr1 = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  2699. if (l >= 4 && ((addr1 & 3) == 0)) {
  2700. /* 32 bit read access */
  2701. val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr1);
  2702. stl_p(buf, val);
  2703. l = 4;
  2704. } else if (l >= 2 && ((addr1 & 1) == 0)) {
  2705. /* 16 bit read access */
  2706. val = io_mem_read[io_index][1](io_mem_opaque[io_index], addr1);
  2707. stw_p(buf, val);
  2708. l = 2;
  2709. } else {
  2710. /* 8 bit read access */
  2711. val = io_mem_read[io_index][0](io_mem_opaque[io_index], addr1);
  2712. stb_p(buf, val);
  2713. l = 1;
  2714. }
  2715. } else {
  2716. /* RAM case */
  2717. ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
  2718. (addr & ~TARGET_PAGE_MASK);
  2719. memcpy(buf, ptr, l);
  2720. }
  2721. }
  2722. len -= l;
  2723. buf += l;
  2724. addr += l;
  2725. }
  2726. }
  2727. /* used for ROM loading : can write in RAM and ROM */
  2728. void cpu_physical_memory_write_rom(target_phys_addr_t addr,
  2729. const uint8_t *buf, int len)
  2730. {
  2731. int l;
  2732. uint8_t *ptr;
  2733. target_phys_addr_t page;
  2734. unsigned long pd;
  2735. PhysPageDesc *p;
  2736. while (len > 0) {
  2737. page = addr & TARGET_PAGE_MASK;
  2738. l = (page + TARGET_PAGE_SIZE) - addr;
  2739. if (l > len)
  2740. l = len;
  2741. p = phys_page_find(page >> TARGET_PAGE_BITS);
  2742. if (!p) {
  2743. pd = IO_MEM_UNASSIGNED;
  2744. } else {
  2745. pd = p->phys_offset;
  2746. }
  2747. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
  2748. (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM &&
  2749. !(pd & IO_MEM_ROMD)) {
  2750. /* do nothing */
  2751. } else {
  2752. unsigned long addr1;
  2753. addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  2754. /* ROM/RAM case */
  2755. ptr = phys_ram_base + addr1;
  2756. memcpy(ptr, buf, l);
  2757. }
  2758. len -= l;
  2759. buf += l;
  2760. addr += l;
  2761. }
  2762. }
  2763. typedef struct {
  2764. void *buffer;
  2765. target_phys_addr_t addr;
  2766. target_phys_addr_t len;
  2767. } BounceBuffer;
  2768. static BounceBuffer bounce;
  2769. typedef struct MapClient {
  2770. void *opaque;
  2771. void (*callback)(void *opaque);
  2772. LIST_ENTRY(MapClient) link;
  2773. } MapClient;
  2774. static LIST_HEAD(map_client_list, MapClient) map_client_list
  2775. = LIST_HEAD_INITIALIZER(map_client_list);
  2776. void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
  2777. {
  2778. MapClient *client = qemu_malloc(sizeof(*client));
  2779. client->opaque = opaque;
  2780. client->callback = callback;
  2781. LIST_INSERT_HEAD(&map_client_list, client, link);
  2782. return client;
  2783. }
  2784. void cpu_unregister_map_client(void *_client)
  2785. {
  2786. MapClient *client = (MapClient *)_client;
  2787. LIST_REMOVE(client, link);
  2788. qemu_free(client);
  2789. }
  2790. static void cpu_notify_map_clients(void)
  2791. {
  2792. MapClient *client;
  2793. while (!LIST_EMPTY(&map_client_list)) {
  2794. client = LIST_FIRST(&map_client_list);
  2795. client->callback(client->opaque);
  2796. cpu_unregister_map_client(client);
  2797. }
  2798. }
  2799. /* Map a physical memory region into a host virtual address.
  2800. * May map a subset of the requested range, given by and returned in *plen.
  2801. * May return NULL if resources needed to perform the mapping are exhausted.
  2802. * Use only for reads OR writes - not for read-modify-write operations.
  2803. * Use cpu_register_map_client() to know when retrying the map operation is
  2804. * likely to succeed.
  2805. */
  2806. void *cpu_physical_memory_map(target_phys_addr_t addr,
  2807. target_phys_addr_t *plen,
  2808. int is_write)
  2809. {
  2810. target_phys_addr_t len = *plen;
  2811. target_phys_addr_t done = 0;
  2812. int l;
  2813. uint8_t *ret = NULL;
  2814. uint8_t *ptr;
  2815. target_phys_addr_t page;
  2816. unsigned long pd;
  2817. PhysPageDesc *p;
  2818. unsigned long addr1;
  2819. while (len > 0) {
  2820. page = addr & TARGET_PAGE_MASK;
  2821. l = (page + TARGET_PAGE_SIZE) - addr;
  2822. if (l > len)
  2823. l = len;
  2824. p = phys_page_find(page >> TARGET_PAGE_BITS);
  2825. if (!p) {
  2826. pd = IO_MEM_UNASSIGNED;
  2827. } else {
  2828. pd = p->phys_offset;
  2829. }
  2830. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  2831. if (done || bounce.buffer) {
  2832. break;
  2833. }
  2834. bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
  2835. bounce.addr = addr;
  2836. bounce.len = l;
  2837. if (!is_write) {
  2838. cpu_physical_memory_rw(addr, bounce.buffer, l, 0);
  2839. }
  2840. ptr = bounce.buffer;
  2841. } else {
  2842. addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  2843. ptr = phys_ram_base + addr1;
  2844. }
  2845. if (!done) {
  2846. ret = ptr;
  2847. } else if (ret + done != ptr) {
  2848. break;
  2849. }
  2850. len -= l;
  2851. addr += l;
  2852. done += l;
  2853. }
  2854. *plen = done;
  2855. return ret;
  2856. }
  2857. /* Unmaps a memory region previously mapped by cpu_physical_memory_map().
  2858. * Will also mark the memory as dirty if is_write == 1. access_len gives
  2859. * the amount of memory that was actually read or written by the caller.
  2860. */
  2861. void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,
  2862. int is_write, target_phys_addr_t access_len)
  2863. {
  2864. if (buffer != bounce.buffer) {
  2865. if (is_write) {
  2866. unsigned long addr1 = (uint8_t *)buffer - phys_ram_base;
  2867. while (access_len) {
  2868. unsigned l;
  2869. l = TARGET_PAGE_SIZE;
  2870. if (l > access_len)
  2871. l = access_len;
  2872. if (!cpu_physical_memory_is_dirty(addr1)) {
  2873. /* invalidate code */
  2874. tb_invalidate_phys_page_range(addr1, addr1 + l, 0);
  2875. /* set dirty bit */
  2876. phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
  2877. (0xff & ~CODE_DIRTY_FLAG);
  2878. }
  2879. addr1 += l;
  2880. access_len -= l;
  2881. }
  2882. }
  2883. return;
  2884. }
  2885. if (is_write) {
  2886. cpu_physical_memory_write(bounce.addr, bounce.buffer, access_len);
  2887. }
  2888. qemu_free(bounce.buffer);
  2889. bounce.buffer = NULL;
  2890. cpu_notify_map_clients();
  2891. }
  2892. /* warning: addr must be aligned */
  2893. uint32_t ldl_phys(target_phys_addr_t addr)
  2894. {
  2895. int io_index;
  2896. uint8_t *ptr;
  2897. uint32_t val;
  2898. unsigned long pd;
  2899. PhysPageDesc *p;
  2900. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  2901. if (!p) {
  2902. pd = IO_MEM_UNASSIGNED;
  2903. } else {
  2904. pd = p->phys_offset;
  2905. }
  2906. if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
  2907. !(pd & IO_MEM_ROMD)) {
  2908. /* I/O case */
  2909. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  2910. if (p)
  2911. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  2912. val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
  2913. } else {
  2914. /* RAM case */
  2915. ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
  2916. (addr & ~TARGET_PAGE_MASK);
  2917. val = ldl_p(ptr);
  2918. }
  2919. return val;
  2920. }
  2921. /* warning: addr must be aligned */
  2922. uint64_t ldq_phys(target_phys_addr_t addr)
  2923. {
  2924. int io_index;
  2925. uint8_t *ptr;
  2926. uint64_t val;
  2927. unsigned long pd;
  2928. PhysPageDesc *p;
  2929. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  2930. if (!p) {
  2931. pd = IO_MEM_UNASSIGNED;
  2932. } else {
  2933. pd = p->phys_offset;
  2934. }
  2935. if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
  2936. !(pd & IO_MEM_ROMD)) {
  2937. /* I/O case */
  2938. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  2939. if (p)
  2940. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  2941. #ifdef TARGET_WORDS_BIGENDIAN
  2942. val = (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr) << 32;
  2943. val |= io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4);
  2944. #else
  2945. val = io_mem_read[io_index][2](io_mem_opaque[io_index], addr);
  2946. val |= (uint64_t)io_mem_read[io_index][2](io_mem_opaque[io_index], addr + 4) << 32;
  2947. #endif
  2948. } else {
  2949. /* RAM case */
  2950. ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
  2951. (addr & ~TARGET_PAGE_MASK);
  2952. val = ldq_p(ptr);
  2953. }
  2954. return val;
  2955. }
  2956. /* XXX: optimize */
  2957. uint32_t ldub_phys(target_phys_addr_t addr)
  2958. {
  2959. uint8_t val;
  2960. cpu_physical_memory_read(addr, &val, 1);
  2961. return val;
  2962. }
  2963. /* XXX: optimize */
  2964. uint32_t lduw_phys(target_phys_addr_t addr)
  2965. {
  2966. uint16_t val;
  2967. cpu_physical_memory_read(addr, (uint8_t *)&val, 2);
  2968. return tswap16(val);
  2969. }
  2970. /* warning: addr must be aligned. The ram page is not masked as dirty
  2971. and the code inside is not invalidated. It is useful if the dirty
  2972. bits are used to track modified PTEs */
  2973. void stl_phys_notdirty(target_phys_addr_t addr, uint32_t val)
  2974. {
  2975. int io_index;
  2976. uint8_t *ptr;
  2977. unsigned long pd;
  2978. PhysPageDesc *p;
  2979. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  2980. if (!p) {
  2981. pd = IO_MEM_UNASSIGNED;
  2982. } else {
  2983. pd = p->phys_offset;
  2984. }
  2985. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  2986. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  2987. if (p)
  2988. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  2989. io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
  2990. } else {
  2991. unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  2992. ptr = phys_ram_base + addr1;
  2993. stl_p(ptr, val);
  2994. if (unlikely(in_migration)) {
  2995. if (!cpu_physical_memory_is_dirty(addr1)) {
  2996. /* invalidate code */
  2997. tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
  2998. /* set dirty bit */
  2999. phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
  3000. (0xff & ~CODE_DIRTY_FLAG);
  3001. }
  3002. }
  3003. }
  3004. }
  3005. void stq_phys_notdirty(target_phys_addr_t addr, uint64_t val)
  3006. {
  3007. int io_index;
  3008. uint8_t *ptr;
  3009. unsigned long pd;
  3010. PhysPageDesc *p;
  3011. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  3012. if (!p) {
  3013. pd = IO_MEM_UNASSIGNED;
  3014. } else {
  3015. pd = p->phys_offset;
  3016. }
  3017. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  3018. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  3019. if (p)
  3020. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  3021. #ifdef TARGET_WORDS_BIGENDIAN
  3022. io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val >> 32);
  3023. io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val);
  3024. #else
  3025. io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
  3026. io_mem_write[io_index][2](io_mem_opaque[io_index], addr + 4, val >> 32);
  3027. #endif
  3028. } else {
  3029. ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
  3030. (addr & ~TARGET_PAGE_MASK);
  3031. stq_p(ptr, val);
  3032. }
  3033. }
  3034. /* warning: addr must be aligned */
  3035. void stl_phys(target_phys_addr_t addr, uint32_t val)
  3036. {
  3037. int io_index;
  3038. uint8_t *ptr;
  3039. unsigned long pd;
  3040. PhysPageDesc *p;
  3041. p = phys_page_find(addr >> TARGET_PAGE_BITS);
  3042. if (!p) {
  3043. pd = IO_MEM_UNASSIGNED;
  3044. } else {
  3045. pd = p->phys_offset;
  3046. }
  3047. if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM) {
  3048. io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
  3049. if (p)
  3050. addr = (addr & ~TARGET_PAGE_MASK) + p->region_offset;
  3051. io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
  3052. } else {
  3053. unsigned long addr1;
  3054. addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
  3055. /* RAM case */
  3056. ptr = phys_ram_base + addr1;
  3057. stl_p(ptr, val);
  3058. if (!cpu_physical_memory_is_dirty(addr1)) {
  3059. /* invalidate code */
  3060. tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
  3061. /* set dirty bit */
  3062. phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
  3063. (0xff & ~CODE_DIRTY_FLAG);
  3064. }
  3065. }
  3066. }
  3067. /* XXX: optimize */
  3068. void stb_phys(target_phys_addr_t addr, uint32_t val)
  3069. {
  3070. uint8_t v = val;
  3071. cpu_physical_memory_write(addr, &v, 1);
  3072. }
  3073. /* XXX: optimize */
  3074. void stw_phys(target_phys_addr_t addr, uint32_t val)
  3075. {
  3076. uint16_t v = tswap16(val);
  3077. cpu_physical_memory_write(addr, (const uint8_t *)&v, 2);
  3078. }
  3079. /* XXX: optimize */
  3080. void stq_phys(target_phys_addr_t addr, uint64_t val)
  3081. {
  3082. val = tswap64(val);
  3083. cpu_physical_memory_write(addr, (const uint8_t *)&val, 8);
  3084. }
  3085. #endif
  3086. /* virtual memory access for debug */
  3087. int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
  3088. uint8_t *buf, int len, int is_write)
  3089. {
  3090. int l;
  3091. target_phys_addr_t phys_addr;
  3092. target_ulong page;
  3093. while (len > 0) {
  3094. page = addr & TARGET_PAGE_MASK;
  3095. phys_addr = cpu_get_phys_page_debug(env, page);
  3096. /* if no physical page mapped, return an error */
  3097. if (phys_addr == -1)
  3098. return -1;
  3099. l = (page + TARGET_PAGE_SIZE) - addr;
  3100. if (l > len)
  3101. l = len;
  3102. cpu_physical_memory_rw(phys_addr + (addr & ~TARGET_PAGE_MASK),
  3103. buf, l, is_write);
  3104. len -= l;
  3105. buf += l;
  3106. addr += l;
  3107. }
  3108. return 0;
  3109. }
  3110. /* in deterministic execution mode, instructions doing device I/Os
  3111. must be at the end of the TB */
  3112. void cpu_io_recompile(CPUState *env, void *retaddr)
  3113. {
  3114. TranslationBlock *tb;
  3115. uint32_t n, cflags;
  3116. target_ulong pc, cs_base;
  3117. uint64_t flags;
  3118. tb = tb_find_pc((unsigned long)retaddr);
  3119. if (!tb) {
  3120. cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p",
  3121. retaddr);
  3122. }
  3123. n = env->icount_decr.u16.low + tb->icount;
  3124. cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
  3125. /* Calculate how many instructions had been executed before the fault
  3126. occurred. */
  3127. n = n - env->icount_decr.u16.low;
  3128. /* Generate a new TB ending on the I/O insn. */
  3129. n++;
  3130. /* On MIPS and SH, delay slot instructions can only be restarted if
  3131. they were already the first instruction in the TB. If this is not
  3132. the first instruction in a TB then re-execute the preceding
  3133. branch. */
  3134. #if defined(TARGET_MIPS)
  3135. if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
  3136. env->active_tc.PC -= 4;
  3137. env->icount_decr.u16.low++;
  3138. env->hflags &= ~MIPS_HFLAG_BMASK;
  3139. }
  3140. #elif defined(TARGET_SH4)
  3141. if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
  3142. && n > 1) {
  3143. env->pc -= 2;
  3144. env->icount_decr.u16.low++;
  3145. env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
  3146. }
  3147. #endif
  3148. /* This should never happen. */
  3149. if (n > CF_COUNT_MASK)
  3150. cpu_abort(env, "TB too big during recompile");
  3151. cflags = n | CF_LAST_IO;
  3152. pc = tb->pc;
  3153. cs_base = tb->cs_base;
  3154. flags = tb->flags;
  3155. tb_phys_invalidate(tb, -1);
  3156. /* FIXME: In theory this could raise an exception. In practice
  3157. we have already translated the block once so it's probably ok. */
  3158. tb_gen_code(env, pc, cs_base, flags, cflags);
  3159. /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
  3160. the first in the TB) then we end up generating a whole new TB and
  3161. repeating the fault, which is horribly inefficient.
  3162. Better would be to execute just this insn uncached, or generate a
  3163. second new TB. */
  3164. cpu_resume_from_signal(env, NULL);
  3165. }
  3166. void dump_exec_info(FILE *f,
  3167. int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
  3168. {
  3169. int i, target_code_size, max_target_code_size;
  3170. int direct_jmp_count, direct_jmp2_count, cross_page;
  3171. TranslationBlock *tb;
  3172. target_code_size = 0;
  3173. max_target_code_size = 0;
  3174. cross_page = 0;
  3175. direct_jmp_count = 0;
  3176. direct_jmp2_count = 0;
  3177. for(i = 0; i < nb_tbs; i++) {
  3178. tb = &tbs[i];
  3179. target_code_size += tb->size;
  3180. if (tb->size > max_target_code_size)
  3181. max_target_code_size = tb->size;
  3182. if (tb->page_addr[1] != -1)
  3183. cross_page++;
  3184. if (tb->tb_next_offset[0] != 0xffff) {
  3185. direct_jmp_count++;
  3186. if (tb->tb_next_offset[1] != 0xffff) {
  3187. direct_jmp2_count++;
  3188. }
  3189. }
  3190. }
  3191. /* XXX: avoid using doubles ? */
  3192. cpu_fprintf(f, "Translation buffer state:\n");
  3193. cpu_fprintf(f, "gen code size %ld/%ld\n",
  3194. code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
  3195. cpu_fprintf(f, "TB count %d/%d\n",
  3196. nb_tbs, code_gen_max_blocks);
  3197. cpu_fprintf(f, "TB avg target size %d max=%d bytes\n",
  3198. nb_tbs ? target_code_size / nb_tbs : 0,
  3199. max_target_code_size);
  3200. cpu_fprintf(f, "TB avg host size %d bytes (expansion ratio: %0.1f)\n",
  3201. nb_tbs ? (code_gen_ptr - code_gen_buffer) / nb_tbs : 0,
  3202. target_code_size ? (double) (code_gen_ptr - code_gen_buffer) / target_code_size : 0);
  3203. cpu_fprintf(f, "cross page TB count %d (%d%%)\n",
  3204. cross_page,
  3205. nb_tbs ? (cross_page * 100) / nb_tbs : 0);
  3206. cpu_fprintf(f, "direct jump count %d (%d%%) (2 jumps=%d %d%%)\n",
  3207. direct_jmp_count,
  3208. nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
  3209. direct_jmp2_count,
  3210. nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
  3211. cpu_fprintf(f, "\nStatistics:\n");
  3212. cpu_fprintf(f, "TB flush count %d\n", tb_flush_count);
  3213. cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
  3214. cpu_fprintf(f, "TLB flush count %d\n", tlb_flush_count);
  3215. tcg_dump_info(f, cpu_fprintf);
  3216. }
  3217. #if !defined(CONFIG_USER_ONLY)
  3218. #define MMUSUFFIX _cmmu
  3219. #define GETPC() NULL
  3220. #define env cpu_single_env
  3221. #define SOFTMMU_CODE_ACCESS
  3222. #define SHIFT 0
  3223. #include "softmmu_template.h"
  3224. #define SHIFT 1
  3225. #include "softmmu_template.h"
  3226. #define SHIFT 2
  3227. #include "softmmu_template.h"
  3228. #define SHIFT 3
  3229. #include "softmmu_template.h"
  3230. #undef env
  3231. #endif