exec.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791
  1. /*
  2. * Virtual page mapping
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qapi/error.h"
  21. #ifndef _WIN32
  22. #endif
  23. #include "qemu/cutils.h"
  24. #include "cpu.h"
  25. #include "exec/exec-all.h"
  26. #include "exec/target_page.h"
  27. #include "tcg.h"
  28. #include "hw/qdev-core.h"
  29. #include "hw/qdev-properties.h"
  30. #if !defined(CONFIG_USER_ONLY)
  31. #include "hw/boards.h"
  32. #include "hw/xen/xen.h"
  33. #endif
  34. #include "sysemu/kvm.h"
  35. #include "sysemu/sysemu.h"
  36. #include "qemu/timer.h"
  37. #include "qemu/config-file.h"
  38. #include "qemu/error-report.h"
  39. #if defined(CONFIG_USER_ONLY)
  40. #include "qemu.h"
  41. #else /* !CONFIG_USER_ONLY */
  42. #include "hw/hw.h"
  43. #include "exec/memory.h"
  44. #include "exec/ioport.h"
  45. #include "sysemu/dma.h"
  46. #include "sysemu/numa.h"
  47. #include "sysemu/hw_accel.h"
  48. #include "exec/address-spaces.h"
  49. #include "sysemu/xen-mapcache.h"
  50. #include "trace-root.h"
  51. #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
  52. #include <fcntl.h>
  53. #include <linux/falloc.h>
  54. #endif
  55. #endif
  56. #include "qemu/rcu_queue.h"
  57. #include "qemu/main-loop.h"
  58. #include "translate-all.h"
  59. #include "sysemu/replay.h"
  60. #include "exec/memory-internal.h"
  61. #include "exec/ram_addr.h"
  62. #include "exec/log.h"
  63. #include "migration/vmstate.h"
  64. #include "qemu/range.h"
  65. #ifndef _WIN32
  66. #include "qemu/mmap-alloc.h"
  67. #endif
  68. #include "monitor/monitor.h"
  69. //#define DEBUG_SUBPAGE
  70. #if !defined(CONFIG_USER_ONLY)
  71. /* ram_list is read under rcu_read_lock()/rcu_read_unlock(). Writes
  72. * are protected by the ramlist lock.
  73. */
  74. RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list.blocks) };
  75. static MemoryRegion *system_memory;
  76. static MemoryRegion *system_io;
  77. AddressSpace address_space_io;
  78. AddressSpace address_space_memory;
  79. MemoryRegion io_mem_rom, io_mem_notdirty;
  80. static MemoryRegion io_mem_unassigned;
  81. /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
  82. #define RAM_PREALLOC (1 << 0)
  83. /* RAM is mmap-ed with MAP_SHARED */
  84. #define RAM_SHARED (1 << 1)
  85. /* Only a portion of RAM (used_length) is actually used, and migrated.
  86. * This used_length size can change across reboots.
  87. */
  88. #define RAM_RESIZEABLE (1 << 2)
  89. #endif
  90. #ifdef TARGET_PAGE_BITS_VARY
  91. int target_page_bits;
  92. bool target_page_bits_decided;
  93. #endif
  94. struct CPUTailQ cpus = QTAILQ_HEAD_INITIALIZER(cpus);
  95. /* current CPU in the current thread. It is only valid inside
  96. cpu_exec() */
  97. __thread CPUState *current_cpu;
  98. /* 0 = Do not count executed instructions.
  99. 1 = Precise instruction counting.
  100. 2 = Adaptive rate instruction counting. */
  101. int use_icount;
  102. uintptr_t qemu_host_page_size;
  103. intptr_t qemu_host_page_mask;
  104. bool set_preferred_target_page_bits(int bits)
  105. {
  106. /* The target page size is the lowest common denominator for all
  107. * the CPUs in the system, so we can only make it smaller, never
  108. * larger. And we can't make it smaller once we've committed to
  109. * a particular size.
  110. */
  111. #ifdef TARGET_PAGE_BITS_VARY
  112. assert(bits >= TARGET_PAGE_BITS_MIN);
  113. if (target_page_bits == 0 || target_page_bits > bits) {
  114. if (target_page_bits_decided) {
  115. return false;
  116. }
  117. target_page_bits = bits;
  118. }
  119. #endif
  120. return true;
  121. }
  122. #if !defined(CONFIG_USER_ONLY)
  123. static void finalize_target_page_bits(void)
  124. {
  125. #ifdef TARGET_PAGE_BITS_VARY
  126. if (target_page_bits == 0) {
  127. target_page_bits = TARGET_PAGE_BITS_MIN;
  128. }
  129. target_page_bits_decided = true;
  130. #endif
  131. }
  132. typedef struct PhysPageEntry PhysPageEntry;
  133. struct PhysPageEntry {
  134. /* How many bits skip to next level (in units of L2_SIZE). 0 for a leaf. */
  135. uint32_t skip : 6;
  136. /* index into phys_sections (!skip) or phys_map_nodes (skip) */
  137. uint32_t ptr : 26;
  138. };
  139. #define PHYS_MAP_NODE_NIL (((uint32_t)~0) >> 6)
  140. /* Size of the L2 (and L3, etc) page tables. */
  141. #define ADDR_SPACE_BITS 64
  142. #define P_L2_BITS 9
  143. #define P_L2_SIZE (1 << P_L2_BITS)
  144. #define P_L2_LEVELS (((ADDR_SPACE_BITS - TARGET_PAGE_BITS - 1) / P_L2_BITS) + 1)
  145. typedef PhysPageEntry Node[P_L2_SIZE];
  146. typedef struct PhysPageMap {
  147. struct rcu_head rcu;
  148. unsigned sections_nb;
  149. unsigned sections_nb_alloc;
  150. unsigned nodes_nb;
  151. unsigned nodes_nb_alloc;
  152. Node *nodes;
  153. MemoryRegionSection *sections;
  154. } PhysPageMap;
  155. struct AddressSpaceDispatch {
  156. MemoryRegionSection *mru_section;
  157. /* This is a multi-level map on the physical address space.
  158. * The bottom level has pointers to MemoryRegionSections.
  159. */
  160. PhysPageEntry phys_map;
  161. PhysPageMap map;
  162. };
  163. #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
  164. typedef struct subpage_t {
  165. MemoryRegion iomem;
  166. FlatView *fv;
  167. hwaddr base;
  168. uint16_t sub_section[];
  169. } subpage_t;
  170. #define PHYS_SECTION_UNASSIGNED 0
  171. #define PHYS_SECTION_NOTDIRTY 1
  172. #define PHYS_SECTION_ROM 2
  173. #define PHYS_SECTION_WATCH 3
  174. static void io_mem_init(void);
  175. static void memory_map_init(void);
  176. static void tcg_commit(MemoryListener *listener);
  177. static MemoryRegion io_mem_watch;
  178. /**
  179. * CPUAddressSpace: all the information a CPU needs about an AddressSpace
  180. * @cpu: the CPU whose AddressSpace this is
  181. * @as: the AddressSpace itself
  182. * @memory_dispatch: its dispatch pointer (cached, RCU protected)
  183. * @tcg_as_listener: listener for tracking changes to the AddressSpace
  184. */
  185. struct CPUAddressSpace {
  186. CPUState *cpu;
  187. AddressSpace *as;
  188. struct AddressSpaceDispatch *memory_dispatch;
  189. MemoryListener tcg_as_listener;
  190. };
  191. struct DirtyBitmapSnapshot {
  192. ram_addr_t start;
  193. ram_addr_t end;
  194. unsigned long dirty[];
  195. };
  196. #endif
  197. #if !defined(CONFIG_USER_ONLY)
  198. static void phys_map_node_reserve(PhysPageMap *map, unsigned nodes)
  199. {
  200. static unsigned alloc_hint = 16;
  201. if (map->nodes_nb + nodes > map->nodes_nb_alloc) {
  202. map->nodes_nb_alloc = MAX(map->nodes_nb_alloc, alloc_hint);
  203. map->nodes_nb_alloc = MAX(map->nodes_nb_alloc, map->nodes_nb + nodes);
  204. map->nodes = g_renew(Node, map->nodes, map->nodes_nb_alloc);
  205. alloc_hint = map->nodes_nb_alloc;
  206. }
  207. }
  208. static uint32_t phys_map_node_alloc(PhysPageMap *map, bool leaf)
  209. {
  210. unsigned i;
  211. uint32_t ret;
  212. PhysPageEntry e;
  213. PhysPageEntry *p;
  214. ret = map->nodes_nb++;
  215. p = map->nodes[ret];
  216. assert(ret != PHYS_MAP_NODE_NIL);
  217. assert(ret != map->nodes_nb_alloc);
  218. e.skip = leaf ? 0 : 1;
  219. e.ptr = leaf ? PHYS_SECTION_UNASSIGNED : PHYS_MAP_NODE_NIL;
  220. for (i = 0; i < P_L2_SIZE; ++i) {
  221. memcpy(&p[i], &e, sizeof(e));
  222. }
  223. return ret;
  224. }
  225. static void phys_page_set_level(PhysPageMap *map, PhysPageEntry *lp,
  226. hwaddr *index, hwaddr *nb, uint16_t leaf,
  227. int level)
  228. {
  229. PhysPageEntry *p;
  230. hwaddr step = (hwaddr)1 << (level * P_L2_BITS);
  231. if (lp->skip && lp->ptr == PHYS_MAP_NODE_NIL) {
  232. lp->ptr = phys_map_node_alloc(map, level == 0);
  233. }
  234. p = map->nodes[lp->ptr];
  235. lp = &p[(*index >> (level * P_L2_BITS)) & (P_L2_SIZE - 1)];
  236. while (*nb && lp < &p[P_L2_SIZE]) {
  237. if ((*index & (step - 1)) == 0 && *nb >= step) {
  238. lp->skip = 0;
  239. lp->ptr = leaf;
  240. *index += step;
  241. *nb -= step;
  242. } else {
  243. phys_page_set_level(map, lp, index, nb, leaf, level - 1);
  244. }
  245. ++lp;
  246. }
  247. }
  248. static void phys_page_set(AddressSpaceDispatch *d,
  249. hwaddr index, hwaddr nb,
  250. uint16_t leaf)
  251. {
  252. /* Wildly overreserve - it doesn't matter much. */
  253. phys_map_node_reserve(&d->map, 3 * P_L2_LEVELS);
  254. phys_page_set_level(&d->map, &d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
  255. }
  256. /* Compact a non leaf page entry. Simply detect that the entry has a single child,
  257. * and update our entry so we can skip it and go directly to the destination.
  258. */
  259. static void phys_page_compact(PhysPageEntry *lp, Node *nodes)
  260. {
  261. unsigned valid_ptr = P_L2_SIZE;
  262. int valid = 0;
  263. PhysPageEntry *p;
  264. int i;
  265. if (lp->ptr == PHYS_MAP_NODE_NIL) {
  266. return;
  267. }
  268. p = nodes[lp->ptr];
  269. for (i = 0; i < P_L2_SIZE; i++) {
  270. if (p[i].ptr == PHYS_MAP_NODE_NIL) {
  271. continue;
  272. }
  273. valid_ptr = i;
  274. valid++;
  275. if (p[i].skip) {
  276. phys_page_compact(&p[i], nodes);
  277. }
  278. }
  279. /* We can only compress if there's only one child. */
  280. if (valid != 1) {
  281. return;
  282. }
  283. assert(valid_ptr < P_L2_SIZE);
  284. /* Don't compress if it won't fit in the # of bits we have. */
  285. if (lp->skip + p[valid_ptr].skip >= (1 << 3)) {
  286. return;
  287. }
  288. lp->ptr = p[valid_ptr].ptr;
  289. if (!p[valid_ptr].skip) {
  290. /* If our only child is a leaf, make this a leaf. */
  291. /* By design, we should have made this node a leaf to begin with so we
  292. * should never reach here.
  293. * But since it's so simple to handle this, let's do it just in case we
  294. * change this rule.
  295. */
  296. lp->skip = 0;
  297. } else {
  298. lp->skip += p[valid_ptr].skip;
  299. }
  300. }
  301. void address_space_dispatch_compact(AddressSpaceDispatch *d)
  302. {
  303. if (d->phys_map.skip) {
  304. phys_page_compact(&d->phys_map, d->map.nodes);
  305. }
  306. }
  307. static inline bool section_covers_addr(const MemoryRegionSection *section,
  308. hwaddr addr)
  309. {
  310. /* Memory topology clips a memory region to [0, 2^64); size.hi > 0 means
  311. * the section must cover the entire address space.
  312. */
  313. return int128_gethi(section->size) ||
  314. range_covers_byte(section->offset_within_address_space,
  315. int128_getlo(section->size), addr);
  316. }
  317. static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr addr)
  318. {
  319. PhysPageEntry lp = d->phys_map, *p;
  320. Node *nodes = d->map.nodes;
  321. MemoryRegionSection *sections = d->map.sections;
  322. hwaddr index = addr >> TARGET_PAGE_BITS;
  323. int i;
  324. for (i = P_L2_LEVELS; lp.skip && (i -= lp.skip) >= 0;) {
  325. if (lp.ptr == PHYS_MAP_NODE_NIL) {
  326. return &sections[PHYS_SECTION_UNASSIGNED];
  327. }
  328. p = nodes[lp.ptr];
  329. lp = p[(index >> (i * P_L2_BITS)) & (P_L2_SIZE - 1)];
  330. }
  331. if (section_covers_addr(&sections[lp.ptr], addr)) {
  332. return &sections[lp.ptr];
  333. } else {
  334. return &sections[PHYS_SECTION_UNASSIGNED];
  335. }
  336. }
  337. bool memory_region_is_unassigned(MemoryRegion *mr)
  338. {
  339. return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device
  340. && mr != &io_mem_watch;
  341. }
  342. /* Called from RCU critical section */
  343. static MemoryRegionSection *address_space_lookup_region(AddressSpaceDispatch *d,
  344. hwaddr addr,
  345. bool resolve_subpage)
  346. {
  347. MemoryRegionSection *section = atomic_read(&d->mru_section);
  348. subpage_t *subpage;
  349. if (!section || section == &d->map.sections[PHYS_SECTION_UNASSIGNED] ||
  350. !section_covers_addr(section, addr)) {
  351. section = phys_page_find(d, addr);
  352. atomic_set(&d->mru_section, section);
  353. }
  354. if (resolve_subpage && section->mr->subpage) {
  355. subpage = container_of(section->mr, subpage_t, iomem);
  356. section = &d->map.sections[subpage->sub_section[SUBPAGE_IDX(addr)]];
  357. }
  358. return section;
  359. }
  360. /* Called from RCU critical section */
  361. static MemoryRegionSection *
  362. address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *xlat,
  363. hwaddr *plen, bool resolve_subpage)
  364. {
  365. MemoryRegionSection *section;
  366. MemoryRegion *mr;
  367. Int128 diff;
  368. section = address_space_lookup_region(d, addr, resolve_subpage);
  369. /* Compute offset within MemoryRegionSection */
  370. addr -= section->offset_within_address_space;
  371. /* Compute offset within MemoryRegion */
  372. *xlat = addr + section->offset_within_region;
  373. mr = section->mr;
  374. /* MMIO registers can be expected to perform full-width accesses based only
  375. * on their address, without considering adjacent registers that could
  376. * decode to completely different MemoryRegions. When such registers
  377. * exist (e.g. I/O ports 0xcf8 and 0xcf9 on most PC chipsets), MMIO
  378. * regions overlap wildly. For this reason we cannot clamp the accesses
  379. * here.
  380. *
  381. * If the length is small (as is the case for address_space_ldl/stl),
  382. * everything works fine. If the incoming length is large, however,
  383. * the caller really has to do the clamping through memory_access_size.
  384. */
  385. if (memory_region_is_ram(mr)) {
  386. diff = int128_sub(section->size, int128_make64(addr));
  387. *plen = int128_get64(int128_min(diff, int128_make64(*plen)));
  388. }
  389. return section;
  390. }
  391. /**
  392. * flatview_do_translate - translate an address in FlatView
  393. *
  394. * @fv: the flat view that we want to translate on
  395. * @addr: the address to be translated in above address space
  396. * @xlat: the translated address offset within memory region. It
  397. * cannot be @NULL.
  398. * @plen_out: valid read/write length of the translated address. It
  399. * can be @NULL when we don't care about it.
  400. * @page_mask_out: page mask for the translated address. This
  401. * should only be meaningful for IOMMU translated
  402. * addresses, since there may be huge pages that this bit
  403. * would tell. It can be @NULL if we don't care about it.
  404. * @is_write: whether the translation operation is for write
  405. * @is_mmio: whether this can be MMIO, set true if it can
  406. *
  407. * This function is called from RCU critical section
  408. */
  409. static MemoryRegionSection flatview_do_translate(FlatView *fv,
  410. hwaddr addr,
  411. hwaddr *xlat,
  412. hwaddr *plen_out,
  413. hwaddr *page_mask_out,
  414. bool is_write,
  415. bool is_mmio,
  416. AddressSpace **target_as)
  417. {
  418. IOMMUTLBEntry iotlb;
  419. MemoryRegionSection *section;
  420. IOMMUMemoryRegion *iommu_mr;
  421. IOMMUMemoryRegionClass *imrc;
  422. hwaddr page_mask = (hwaddr)(-1);
  423. hwaddr plen = (hwaddr)(-1);
  424. if (plen_out) {
  425. plen = *plen_out;
  426. }
  427. for (;;) {
  428. section = address_space_translate_internal(
  429. flatview_to_dispatch(fv), addr, &addr,
  430. &plen, is_mmio);
  431. iommu_mr = memory_region_get_iommu(section->mr);
  432. if (!iommu_mr) {
  433. break;
  434. }
  435. imrc = memory_region_get_iommu_class_nocheck(iommu_mr);
  436. iotlb = imrc->translate(iommu_mr, addr, is_write ?
  437. IOMMU_WO : IOMMU_RO);
  438. addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
  439. | (addr & iotlb.addr_mask));
  440. page_mask &= iotlb.addr_mask;
  441. plen = MIN(plen, (addr | iotlb.addr_mask) - addr + 1);
  442. if (!(iotlb.perm & (1 << is_write))) {
  443. goto translate_fail;
  444. }
  445. fv = address_space_to_flatview(iotlb.target_as);
  446. *target_as = iotlb.target_as;
  447. }
  448. *xlat = addr;
  449. if (page_mask == (hwaddr)(-1)) {
  450. /* Not behind an IOMMU, use default page size. */
  451. page_mask = ~TARGET_PAGE_MASK;
  452. }
  453. if (page_mask_out) {
  454. *page_mask_out = page_mask;
  455. }
  456. if (plen_out) {
  457. *plen_out = plen;
  458. }
  459. return *section;
  460. translate_fail:
  461. return (MemoryRegionSection) { .mr = &io_mem_unassigned };
  462. }
  463. /* Called from RCU critical section */
  464. IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
  465. bool is_write)
  466. {
  467. MemoryRegionSection section;
  468. hwaddr xlat, page_mask;
  469. /*
  470. * This can never be MMIO, and we don't really care about plen,
  471. * but page mask.
  472. */
  473. section = flatview_do_translate(address_space_to_flatview(as), addr, &xlat,
  474. NULL, &page_mask, is_write, false, &as);
  475. /* Illegal translation */
  476. if (section.mr == &io_mem_unassigned) {
  477. goto iotlb_fail;
  478. }
  479. /* Convert memory region offset into address space offset */
  480. xlat += section.offset_within_address_space -
  481. section.offset_within_region;
  482. return (IOMMUTLBEntry) {
  483. .target_as = as,
  484. .iova = addr & ~page_mask,
  485. .translated_addr = xlat & ~page_mask,
  486. .addr_mask = page_mask,
  487. /* IOTLBs are for DMAs, and DMA only allows on RAMs. */
  488. .perm = IOMMU_RW,
  489. };
  490. iotlb_fail:
  491. return (IOMMUTLBEntry) {0};
  492. }
  493. /* Called from RCU critical section */
  494. MemoryRegion *flatview_translate(FlatView *fv, hwaddr addr, hwaddr *xlat,
  495. hwaddr *plen, bool is_write)
  496. {
  497. MemoryRegion *mr;
  498. MemoryRegionSection section;
  499. AddressSpace *as = NULL;
  500. /* This can be MMIO, so setup MMIO bit. */
  501. section = flatview_do_translate(fv, addr, xlat, plen, NULL,
  502. is_write, true, &as);
  503. mr = section.mr;
  504. if (xen_enabled() && memory_access_is_direct(mr, is_write)) {
  505. hwaddr page = ((addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE) - addr;
  506. *plen = MIN(page, *plen);
  507. }
  508. return mr;
  509. }
  510. /* Called from RCU critical section */
  511. MemoryRegionSection *
  512. address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
  513. hwaddr *xlat, hwaddr *plen)
  514. {
  515. MemoryRegionSection *section;
  516. AddressSpaceDispatch *d = atomic_rcu_read(&cpu->cpu_ases[asidx].memory_dispatch);
  517. section = address_space_translate_internal(d, addr, xlat, plen, false);
  518. assert(!memory_region_is_iommu(section->mr));
  519. return section;
  520. }
  521. #endif
  522. #if !defined(CONFIG_USER_ONLY)
  523. static int cpu_common_post_load(void *opaque, int version_id)
  524. {
  525. CPUState *cpu = opaque;
  526. /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
  527. version_id is increased. */
  528. cpu->interrupt_request &= ~0x01;
  529. tlb_flush(cpu);
  530. return 0;
  531. }
  532. static int cpu_common_pre_load(void *opaque)
  533. {
  534. CPUState *cpu = opaque;
  535. cpu->exception_index = -1;
  536. return 0;
  537. }
  538. static bool cpu_common_exception_index_needed(void *opaque)
  539. {
  540. CPUState *cpu = opaque;
  541. return tcg_enabled() && cpu->exception_index != -1;
  542. }
  543. static const VMStateDescription vmstate_cpu_common_exception_index = {
  544. .name = "cpu_common/exception_index",
  545. .version_id = 1,
  546. .minimum_version_id = 1,
  547. .needed = cpu_common_exception_index_needed,
  548. .fields = (VMStateField[]) {
  549. VMSTATE_INT32(exception_index, CPUState),
  550. VMSTATE_END_OF_LIST()
  551. }
  552. };
  553. static bool cpu_common_crash_occurred_needed(void *opaque)
  554. {
  555. CPUState *cpu = opaque;
  556. return cpu->crash_occurred;
  557. }
  558. static const VMStateDescription vmstate_cpu_common_crash_occurred = {
  559. .name = "cpu_common/crash_occurred",
  560. .version_id = 1,
  561. .minimum_version_id = 1,
  562. .needed = cpu_common_crash_occurred_needed,
  563. .fields = (VMStateField[]) {
  564. VMSTATE_BOOL(crash_occurred, CPUState),
  565. VMSTATE_END_OF_LIST()
  566. }
  567. };
  568. const VMStateDescription vmstate_cpu_common = {
  569. .name = "cpu_common",
  570. .version_id = 1,
  571. .minimum_version_id = 1,
  572. .pre_load = cpu_common_pre_load,
  573. .post_load = cpu_common_post_load,
  574. .fields = (VMStateField[]) {
  575. VMSTATE_UINT32(halted, CPUState),
  576. VMSTATE_UINT32(interrupt_request, CPUState),
  577. VMSTATE_END_OF_LIST()
  578. },
  579. .subsections = (const VMStateDescription*[]) {
  580. &vmstate_cpu_common_exception_index,
  581. &vmstate_cpu_common_crash_occurred,
  582. NULL
  583. }
  584. };
  585. #endif
  586. CPUState *qemu_get_cpu(int index)
  587. {
  588. CPUState *cpu;
  589. CPU_FOREACH(cpu) {
  590. if (cpu->cpu_index == index) {
  591. return cpu;
  592. }
  593. }
  594. return NULL;
  595. }
  596. #if !defined(CONFIG_USER_ONLY)
  597. void cpu_address_space_init(CPUState *cpu, AddressSpace *as, int asidx)
  598. {
  599. CPUAddressSpace *newas;
  600. /* Target code should have set num_ases before calling us */
  601. assert(asidx < cpu->num_ases);
  602. if (asidx == 0) {
  603. /* address space 0 gets the convenience alias */
  604. cpu->as = as;
  605. }
  606. /* KVM cannot currently support multiple address spaces. */
  607. assert(asidx == 0 || !kvm_enabled());
  608. if (!cpu->cpu_ases) {
  609. cpu->cpu_ases = g_new0(CPUAddressSpace, cpu->num_ases);
  610. }
  611. newas = &cpu->cpu_ases[asidx];
  612. newas->cpu = cpu;
  613. newas->as = as;
  614. if (tcg_enabled()) {
  615. newas->tcg_as_listener.commit = tcg_commit;
  616. memory_listener_register(&newas->tcg_as_listener, as);
  617. }
  618. }
  619. AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx)
  620. {
  621. /* Return the AddressSpace corresponding to the specified index */
  622. return cpu->cpu_ases[asidx].as;
  623. }
  624. #endif
  625. void cpu_exec_unrealizefn(CPUState *cpu)
  626. {
  627. CPUClass *cc = CPU_GET_CLASS(cpu);
  628. cpu_list_remove(cpu);
  629. if (cc->vmsd != NULL) {
  630. vmstate_unregister(NULL, cc->vmsd, cpu);
  631. }
  632. if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
  633. vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
  634. }
  635. }
  636. Property cpu_common_props[] = {
  637. #ifndef CONFIG_USER_ONLY
  638. /* Create a memory property for softmmu CPU object,
  639. * so users can wire up its memory. (This can't go in qom/cpu.c
  640. * because that file is compiled only once for both user-mode
  641. * and system builds.) The default if no link is set up is to use
  642. * the system address space.
  643. */
  644. DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
  645. MemoryRegion *),
  646. #endif
  647. DEFINE_PROP_END_OF_LIST(),
  648. };
  649. void cpu_exec_initfn(CPUState *cpu)
  650. {
  651. cpu->as = NULL;
  652. cpu->num_ases = 0;
  653. #ifndef CONFIG_USER_ONLY
  654. cpu->thread_id = qemu_get_thread_id();
  655. cpu->memory = system_memory;
  656. object_ref(OBJECT(cpu->memory));
  657. #endif
  658. }
  659. void cpu_exec_realizefn(CPUState *cpu, Error **errp)
  660. {
  661. CPUClass *cc = CPU_GET_CLASS(cpu);
  662. static bool tcg_target_initialized;
  663. cpu_list_add(cpu);
  664. if (tcg_enabled() && !tcg_target_initialized) {
  665. tcg_target_initialized = true;
  666. cc->tcg_initialize();
  667. }
  668. #ifndef CONFIG_USER_ONLY
  669. if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
  670. vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
  671. }
  672. if (cc->vmsd != NULL) {
  673. vmstate_register(NULL, cpu->cpu_index, cc->vmsd, cpu);
  674. }
  675. #endif
  676. }
  677. #if defined(CONFIG_USER_ONLY)
  678. static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
  679. {
  680. mmap_lock();
  681. tb_lock();
  682. tb_invalidate_phys_page_range(pc, pc + 1, 0);
  683. tb_unlock();
  684. mmap_unlock();
  685. }
  686. #else
  687. static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
  688. {
  689. MemTxAttrs attrs;
  690. hwaddr phys = cpu_get_phys_page_attrs_debug(cpu, pc, &attrs);
  691. int asidx = cpu_asidx_from_attrs(cpu, attrs);
  692. if (phys != -1) {
  693. /* Locks grabbed by tb_invalidate_phys_addr */
  694. tb_invalidate_phys_addr(cpu->cpu_ases[asidx].as,
  695. phys | (pc & ~TARGET_PAGE_MASK));
  696. }
  697. }
  698. #endif
  699. #if defined(CONFIG_USER_ONLY)
  700. void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
  701. {
  702. }
  703. int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len,
  704. int flags)
  705. {
  706. return -ENOSYS;
  707. }
  708. void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint)
  709. {
  710. }
  711. int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
  712. int flags, CPUWatchpoint **watchpoint)
  713. {
  714. return -ENOSYS;
  715. }
  716. #else
  717. /* Add a watchpoint. */
  718. int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
  719. int flags, CPUWatchpoint **watchpoint)
  720. {
  721. CPUWatchpoint *wp;
  722. /* forbid ranges which are empty or run off the end of the address space */
  723. if (len == 0 || (addr + len - 1) < addr) {
  724. error_report("tried to set invalid watchpoint at %"
  725. VADDR_PRIx ", len=%" VADDR_PRIu, addr, len);
  726. return -EINVAL;
  727. }
  728. wp = g_malloc(sizeof(*wp));
  729. wp->vaddr = addr;
  730. wp->len = len;
  731. wp->flags = flags;
  732. /* keep all GDB-injected watchpoints in front */
  733. if (flags & BP_GDB) {
  734. QTAILQ_INSERT_HEAD(&cpu->watchpoints, wp, entry);
  735. } else {
  736. QTAILQ_INSERT_TAIL(&cpu->watchpoints, wp, entry);
  737. }
  738. tlb_flush_page(cpu, addr);
  739. if (watchpoint)
  740. *watchpoint = wp;
  741. return 0;
  742. }
  743. /* Remove a specific watchpoint. */
  744. int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len,
  745. int flags)
  746. {
  747. CPUWatchpoint *wp;
  748. QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
  749. if (addr == wp->vaddr && len == wp->len
  750. && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
  751. cpu_watchpoint_remove_by_ref(cpu, wp);
  752. return 0;
  753. }
  754. }
  755. return -ENOENT;
  756. }
  757. /* Remove a specific watchpoint by reference. */
  758. void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint)
  759. {
  760. QTAILQ_REMOVE(&cpu->watchpoints, watchpoint, entry);
  761. tlb_flush_page(cpu, watchpoint->vaddr);
  762. g_free(watchpoint);
  763. }
  764. /* Remove all matching watchpoints. */
  765. void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
  766. {
  767. CPUWatchpoint *wp, *next;
  768. QTAILQ_FOREACH_SAFE(wp, &cpu->watchpoints, entry, next) {
  769. if (wp->flags & mask) {
  770. cpu_watchpoint_remove_by_ref(cpu, wp);
  771. }
  772. }
  773. }
  774. /* Return true if this watchpoint address matches the specified
  775. * access (ie the address range covered by the watchpoint overlaps
  776. * partially or completely with the address range covered by the
  777. * access).
  778. */
  779. static inline bool cpu_watchpoint_address_matches(CPUWatchpoint *wp,
  780. vaddr addr,
  781. vaddr len)
  782. {
  783. /* We know the lengths are non-zero, but a little caution is
  784. * required to avoid errors in the case where the range ends
  785. * exactly at the top of the address space and so addr + len
  786. * wraps round to zero.
  787. */
  788. vaddr wpend = wp->vaddr + wp->len - 1;
  789. vaddr addrend = addr + len - 1;
  790. return !(addr > wpend || wp->vaddr > addrend);
  791. }
  792. #endif
  793. /* Add a breakpoint. */
  794. int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
  795. CPUBreakpoint **breakpoint)
  796. {
  797. CPUBreakpoint *bp;
  798. bp = g_malloc(sizeof(*bp));
  799. bp->pc = pc;
  800. bp->flags = flags;
  801. /* keep all GDB-injected breakpoints in front */
  802. if (flags & BP_GDB) {
  803. QTAILQ_INSERT_HEAD(&cpu->breakpoints, bp, entry);
  804. } else {
  805. QTAILQ_INSERT_TAIL(&cpu->breakpoints, bp, entry);
  806. }
  807. breakpoint_invalidate(cpu, pc);
  808. if (breakpoint) {
  809. *breakpoint = bp;
  810. }
  811. return 0;
  812. }
  813. /* Remove a specific breakpoint. */
  814. int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags)
  815. {
  816. CPUBreakpoint *bp;
  817. QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
  818. if (bp->pc == pc && bp->flags == flags) {
  819. cpu_breakpoint_remove_by_ref(cpu, bp);
  820. return 0;
  821. }
  822. }
  823. return -ENOENT;
  824. }
  825. /* Remove a specific breakpoint by reference. */
  826. void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint)
  827. {
  828. QTAILQ_REMOVE(&cpu->breakpoints, breakpoint, entry);
  829. breakpoint_invalidate(cpu, breakpoint->pc);
  830. g_free(breakpoint);
  831. }
  832. /* Remove all matching breakpoints. */
  833. void cpu_breakpoint_remove_all(CPUState *cpu, int mask)
  834. {
  835. CPUBreakpoint *bp, *next;
  836. QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) {
  837. if (bp->flags & mask) {
  838. cpu_breakpoint_remove_by_ref(cpu, bp);
  839. }
  840. }
  841. }
  842. /* enable or disable single step mode. EXCP_DEBUG is returned by the
  843. CPU loop after each instruction */
  844. void cpu_single_step(CPUState *cpu, int enabled)
  845. {
  846. if (cpu->singlestep_enabled != enabled) {
  847. cpu->singlestep_enabled = enabled;
  848. if (kvm_enabled()) {
  849. kvm_update_guest_debug(cpu, 0);
  850. } else {
  851. /* must flush all the translated code to avoid inconsistencies */
  852. /* XXX: only flush what is necessary */
  853. tb_flush(cpu);
  854. }
  855. }
  856. }
  857. void cpu_abort(CPUState *cpu, const char *fmt, ...)
  858. {
  859. va_list ap;
  860. va_list ap2;
  861. va_start(ap, fmt);
  862. va_copy(ap2, ap);
  863. fprintf(stderr, "qemu: fatal: ");
  864. vfprintf(stderr, fmt, ap);
  865. fprintf(stderr, "\n");
  866. cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
  867. if (qemu_log_separate()) {
  868. qemu_log_lock();
  869. qemu_log("qemu: fatal: ");
  870. qemu_log_vprintf(fmt, ap2);
  871. qemu_log("\n");
  872. log_cpu_state(cpu, CPU_DUMP_FPU | CPU_DUMP_CCOP);
  873. qemu_log_flush();
  874. qemu_log_unlock();
  875. qemu_log_close();
  876. }
  877. va_end(ap2);
  878. va_end(ap);
  879. replay_finish();
  880. #if defined(CONFIG_USER_ONLY)
  881. {
  882. struct sigaction act;
  883. sigfillset(&act.sa_mask);
  884. act.sa_handler = SIG_DFL;
  885. sigaction(SIGABRT, &act, NULL);
  886. }
  887. #endif
  888. abort();
  889. }
  890. #if !defined(CONFIG_USER_ONLY)
  891. /* Called from RCU critical section */
  892. static RAMBlock *qemu_get_ram_block(ram_addr_t addr)
  893. {
  894. RAMBlock *block;
  895. block = atomic_rcu_read(&ram_list.mru_block);
  896. if (block && addr - block->offset < block->max_length) {
  897. return block;
  898. }
  899. RAMBLOCK_FOREACH(block) {
  900. if (addr - block->offset < block->max_length) {
  901. goto found;
  902. }
  903. }
  904. fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
  905. abort();
  906. found:
  907. /* It is safe to write mru_block outside the iothread lock. This
  908. * is what happens:
  909. *
  910. * mru_block = xxx
  911. * rcu_read_unlock()
  912. * xxx removed from list
  913. * rcu_read_lock()
  914. * read mru_block
  915. * mru_block = NULL;
  916. * call_rcu(reclaim_ramblock, xxx);
  917. * rcu_read_unlock()
  918. *
  919. * atomic_rcu_set is not needed here. The block was already published
  920. * when it was placed into the list. Here we're just making an extra
  921. * copy of the pointer.
  922. */
  923. ram_list.mru_block = block;
  924. return block;
  925. }
  926. static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length)
  927. {
  928. CPUState *cpu;
  929. ram_addr_t start1;
  930. RAMBlock *block;
  931. ram_addr_t end;
  932. end = TARGET_PAGE_ALIGN(start + length);
  933. start &= TARGET_PAGE_MASK;
  934. rcu_read_lock();
  935. block = qemu_get_ram_block(start);
  936. assert(block == qemu_get_ram_block(end - 1));
  937. start1 = (uintptr_t)ramblock_ptr(block, start - block->offset);
  938. CPU_FOREACH(cpu) {
  939. tlb_reset_dirty(cpu, start1, length);
  940. }
  941. rcu_read_unlock();
  942. }
  943. /* Note: start and end must be within the same ram block. */
  944. bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
  945. ram_addr_t length,
  946. unsigned client)
  947. {
  948. DirtyMemoryBlocks *blocks;
  949. unsigned long end, page;
  950. bool dirty = false;
  951. if (length == 0) {
  952. return false;
  953. }
  954. end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
  955. page = start >> TARGET_PAGE_BITS;
  956. rcu_read_lock();
  957. blocks = atomic_rcu_read(&ram_list.dirty_memory[client]);
  958. while (page < end) {
  959. unsigned long idx = page / DIRTY_MEMORY_BLOCK_SIZE;
  960. unsigned long offset = page % DIRTY_MEMORY_BLOCK_SIZE;
  961. unsigned long num = MIN(end - page, DIRTY_MEMORY_BLOCK_SIZE - offset);
  962. dirty |= bitmap_test_and_clear_atomic(blocks->blocks[idx],
  963. offset, num);
  964. page += num;
  965. }
  966. rcu_read_unlock();
  967. if (dirty && tcg_enabled()) {
  968. tlb_reset_dirty_range_all(start, length);
  969. }
  970. return dirty;
  971. }
  972. DirtyBitmapSnapshot *cpu_physical_memory_snapshot_and_clear_dirty
  973. (ram_addr_t start, ram_addr_t length, unsigned client)
  974. {
  975. DirtyMemoryBlocks *blocks;
  976. unsigned long align = 1UL << (TARGET_PAGE_BITS + BITS_PER_LEVEL);
  977. ram_addr_t first = QEMU_ALIGN_DOWN(start, align);
  978. ram_addr_t last = QEMU_ALIGN_UP(start + length, align);
  979. DirtyBitmapSnapshot *snap;
  980. unsigned long page, end, dest;
  981. snap = g_malloc0(sizeof(*snap) +
  982. ((last - first) >> (TARGET_PAGE_BITS + 3)));
  983. snap->start = first;
  984. snap->end = last;
  985. page = first >> TARGET_PAGE_BITS;
  986. end = last >> TARGET_PAGE_BITS;
  987. dest = 0;
  988. rcu_read_lock();
  989. blocks = atomic_rcu_read(&ram_list.dirty_memory[client]);
  990. while (page < end) {
  991. unsigned long idx = page / DIRTY_MEMORY_BLOCK_SIZE;
  992. unsigned long offset = page % DIRTY_MEMORY_BLOCK_SIZE;
  993. unsigned long num = MIN(end - page, DIRTY_MEMORY_BLOCK_SIZE - offset);
  994. assert(QEMU_IS_ALIGNED(offset, (1 << BITS_PER_LEVEL)));
  995. assert(QEMU_IS_ALIGNED(num, (1 << BITS_PER_LEVEL)));
  996. offset >>= BITS_PER_LEVEL;
  997. bitmap_copy_and_clear_atomic(snap->dirty + dest,
  998. blocks->blocks[idx] + offset,
  999. num);
  1000. page += num;
  1001. dest += num >> BITS_PER_LEVEL;
  1002. }
  1003. rcu_read_unlock();
  1004. if (tcg_enabled()) {
  1005. tlb_reset_dirty_range_all(start, length);
  1006. }
  1007. return snap;
  1008. }
  1009. bool cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot *snap,
  1010. ram_addr_t start,
  1011. ram_addr_t length)
  1012. {
  1013. unsigned long page, end;
  1014. assert(start >= snap->start);
  1015. assert(start + length <= snap->end);
  1016. end = TARGET_PAGE_ALIGN(start + length - snap->start) >> TARGET_PAGE_BITS;
  1017. page = (start - snap->start) >> TARGET_PAGE_BITS;
  1018. while (page < end) {
  1019. if (test_bit(page, snap->dirty)) {
  1020. return true;
  1021. }
  1022. page++;
  1023. }
  1024. return false;
  1025. }
  1026. /* Called from RCU critical section */
  1027. hwaddr memory_region_section_get_iotlb(CPUState *cpu,
  1028. MemoryRegionSection *section,
  1029. target_ulong vaddr,
  1030. hwaddr paddr, hwaddr xlat,
  1031. int prot,
  1032. target_ulong *address)
  1033. {
  1034. hwaddr iotlb;
  1035. CPUWatchpoint *wp;
  1036. if (memory_region_is_ram(section->mr)) {
  1037. /* Normal RAM. */
  1038. iotlb = memory_region_get_ram_addr(section->mr) + xlat;
  1039. if (!section->readonly) {
  1040. iotlb |= PHYS_SECTION_NOTDIRTY;
  1041. } else {
  1042. iotlb |= PHYS_SECTION_ROM;
  1043. }
  1044. } else {
  1045. AddressSpaceDispatch *d;
  1046. d = flatview_to_dispatch(section->fv);
  1047. iotlb = section - d->map.sections;
  1048. iotlb += xlat;
  1049. }
  1050. /* Make accesses to pages with watchpoints go via the
  1051. watchpoint trap routines. */
  1052. QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
  1053. if (cpu_watchpoint_address_matches(wp, vaddr, TARGET_PAGE_SIZE)) {
  1054. /* Avoid trapping reads of pages with a write breakpoint. */
  1055. if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
  1056. iotlb = PHYS_SECTION_WATCH + paddr;
  1057. *address |= TLB_MMIO;
  1058. break;
  1059. }
  1060. }
  1061. }
  1062. return iotlb;
  1063. }
  1064. #endif /* defined(CONFIG_USER_ONLY) */
  1065. #if !defined(CONFIG_USER_ONLY)
  1066. static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
  1067. uint16_t section);
  1068. static subpage_t *subpage_init(FlatView *fv, hwaddr base);
  1069. static void *(*phys_mem_alloc)(size_t size, uint64_t *align) =
  1070. qemu_anon_ram_alloc;
  1071. /*
  1072. * Set a custom physical guest memory alloator.
  1073. * Accelerators with unusual needs may need this. Hopefully, we can
  1074. * get rid of it eventually.
  1075. */
  1076. void phys_mem_set_alloc(void *(*alloc)(size_t, uint64_t *align))
  1077. {
  1078. phys_mem_alloc = alloc;
  1079. }
  1080. static uint16_t phys_section_add(PhysPageMap *map,
  1081. MemoryRegionSection *section)
  1082. {
  1083. /* The physical section number is ORed with a page-aligned
  1084. * pointer to produce the iotlb entries. Thus it should
  1085. * never overflow into the page-aligned value.
  1086. */
  1087. assert(map->sections_nb < TARGET_PAGE_SIZE);
  1088. if (map->sections_nb == map->sections_nb_alloc) {
  1089. map->sections_nb_alloc = MAX(map->sections_nb_alloc * 2, 16);
  1090. map->sections = g_renew(MemoryRegionSection, map->sections,
  1091. map->sections_nb_alloc);
  1092. }
  1093. map->sections[map->sections_nb] = *section;
  1094. memory_region_ref(section->mr);
  1095. return map->sections_nb++;
  1096. }
  1097. static void phys_section_destroy(MemoryRegion *mr)
  1098. {
  1099. bool have_sub_page = mr->subpage;
  1100. memory_region_unref(mr);
  1101. if (have_sub_page) {
  1102. subpage_t *subpage = container_of(mr, subpage_t, iomem);
  1103. object_unref(OBJECT(&subpage->iomem));
  1104. g_free(subpage);
  1105. }
  1106. }
  1107. static void phys_sections_free(PhysPageMap *map)
  1108. {
  1109. while (map->sections_nb > 0) {
  1110. MemoryRegionSection *section = &map->sections[--map->sections_nb];
  1111. phys_section_destroy(section->mr);
  1112. }
  1113. g_free(map->sections);
  1114. g_free(map->nodes);
  1115. }
  1116. static void register_subpage(FlatView *fv, MemoryRegionSection *section)
  1117. {
  1118. AddressSpaceDispatch *d = flatview_to_dispatch(fv);
  1119. subpage_t *subpage;
  1120. hwaddr base = section->offset_within_address_space
  1121. & TARGET_PAGE_MASK;
  1122. MemoryRegionSection *existing = phys_page_find(d, base);
  1123. MemoryRegionSection subsection = {
  1124. .offset_within_address_space = base,
  1125. .size = int128_make64(TARGET_PAGE_SIZE),
  1126. };
  1127. hwaddr start, end;
  1128. assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
  1129. if (!(existing->mr->subpage)) {
  1130. subpage = subpage_init(fv, base);
  1131. subsection.fv = fv;
  1132. subsection.mr = &subpage->iomem;
  1133. phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
  1134. phys_section_add(&d->map, &subsection));
  1135. } else {
  1136. subpage = container_of(existing->mr, subpage_t, iomem);
  1137. }
  1138. start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
  1139. end = start + int128_get64(section->size) - 1;
  1140. subpage_register(subpage, start, end,
  1141. phys_section_add(&d->map, section));
  1142. }
  1143. static void register_multipage(FlatView *fv,
  1144. MemoryRegionSection *section)
  1145. {
  1146. AddressSpaceDispatch *d = flatview_to_dispatch(fv);
  1147. hwaddr start_addr = section->offset_within_address_space;
  1148. uint16_t section_index = phys_section_add(&d->map, section);
  1149. uint64_t num_pages = int128_get64(int128_rshift(section->size,
  1150. TARGET_PAGE_BITS));
  1151. assert(num_pages);
  1152. phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);
  1153. }
  1154. void flatview_add_to_dispatch(FlatView *fv, MemoryRegionSection *section)
  1155. {
  1156. MemoryRegionSection now = *section, remain = *section;
  1157. Int128 page_size = int128_make64(TARGET_PAGE_SIZE);
  1158. if (now.offset_within_address_space & ~TARGET_PAGE_MASK) {
  1159. uint64_t left = TARGET_PAGE_ALIGN(now.offset_within_address_space)
  1160. - now.offset_within_address_space;
  1161. now.size = int128_min(int128_make64(left), now.size);
  1162. register_subpage(fv, &now);
  1163. } else {
  1164. now.size = int128_zero();
  1165. }
  1166. while (int128_ne(remain.size, now.size)) {
  1167. remain.size = int128_sub(remain.size, now.size);
  1168. remain.offset_within_address_space += int128_get64(now.size);
  1169. remain.offset_within_region += int128_get64(now.size);
  1170. now = remain;
  1171. if (int128_lt(remain.size, page_size)) {
  1172. register_subpage(fv, &now);
  1173. } else if (remain.offset_within_address_space & ~TARGET_PAGE_MASK) {
  1174. now.size = page_size;
  1175. register_subpage(fv, &now);
  1176. } else {
  1177. now.size = int128_and(now.size, int128_neg(page_size));
  1178. register_multipage(fv, &now);
  1179. }
  1180. }
  1181. }
  1182. void qemu_flush_coalesced_mmio_buffer(void)
  1183. {
  1184. if (kvm_enabled())
  1185. kvm_flush_coalesced_mmio_buffer();
  1186. }
  1187. void qemu_mutex_lock_ramlist(void)
  1188. {
  1189. qemu_mutex_lock(&ram_list.mutex);
  1190. }
  1191. void qemu_mutex_unlock_ramlist(void)
  1192. {
  1193. qemu_mutex_unlock(&ram_list.mutex);
  1194. }
  1195. void ram_block_dump(Monitor *mon)
  1196. {
  1197. RAMBlock *block;
  1198. char *psize;
  1199. rcu_read_lock();
  1200. monitor_printf(mon, "%24s %8s %18s %18s %18s\n",
  1201. "Block Name", "PSize", "Offset", "Used", "Total");
  1202. RAMBLOCK_FOREACH(block) {
  1203. psize = size_to_str(block->page_size);
  1204. monitor_printf(mon, "%24s %8s 0x%016" PRIx64 " 0x%016" PRIx64
  1205. " 0x%016" PRIx64 "\n", block->idstr, psize,
  1206. (uint64_t)block->offset,
  1207. (uint64_t)block->used_length,
  1208. (uint64_t)block->max_length);
  1209. g_free(psize);
  1210. }
  1211. rcu_read_unlock();
  1212. }
  1213. #ifdef __linux__
  1214. /*
  1215. * FIXME TOCTTOU: this iterates over memory backends' mem-path, which
  1216. * may or may not name the same files / on the same filesystem now as
  1217. * when we actually open and map them. Iterate over the file
  1218. * descriptors instead, and use qemu_fd_getpagesize().
  1219. */
  1220. static int find_max_supported_pagesize(Object *obj, void *opaque)
  1221. {
  1222. char *mem_path;
  1223. long *hpsize_min = opaque;
  1224. if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
  1225. mem_path = object_property_get_str(obj, "mem-path", NULL);
  1226. if (mem_path) {
  1227. long hpsize = qemu_mempath_getpagesize(mem_path);
  1228. g_free(mem_path);
  1229. if (hpsize < *hpsize_min) {
  1230. *hpsize_min = hpsize;
  1231. }
  1232. } else {
  1233. *hpsize_min = getpagesize();
  1234. }
  1235. }
  1236. return 0;
  1237. }
  1238. long qemu_getrampagesize(void)
  1239. {
  1240. long hpsize = LONG_MAX;
  1241. long mainrampagesize;
  1242. Object *memdev_root;
  1243. if (mem_path) {
  1244. mainrampagesize = qemu_mempath_getpagesize(mem_path);
  1245. } else {
  1246. mainrampagesize = getpagesize();
  1247. }
  1248. /* it's possible we have memory-backend objects with
  1249. * hugepage-backed RAM. these may get mapped into system
  1250. * address space via -numa parameters or memory hotplug
  1251. * hooks. we want to take these into account, but we
  1252. * also want to make sure these supported hugepage
  1253. * sizes are applicable across the entire range of memory
  1254. * we may boot from, so we take the min across all
  1255. * backends, and assume normal pages in cases where a
  1256. * backend isn't backed by hugepages.
  1257. */
  1258. memdev_root = object_resolve_path("/objects", NULL);
  1259. if (memdev_root) {
  1260. object_child_foreach(memdev_root, find_max_supported_pagesize, &hpsize);
  1261. }
  1262. if (hpsize == LONG_MAX) {
  1263. /* No additional memory regions found ==> Report main RAM page size */
  1264. return mainrampagesize;
  1265. }
  1266. /* If NUMA is disabled or the NUMA nodes are not backed with a
  1267. * memory-backend, then there is at least one node using "normal" RAM,
  1268. * so if its page size is smaller we have got to report that size instead.
  1269. */
  1270. if (hpsize > mainrampagesize &&
  1271. (nb_numa_nodes == 0 || numa_info[0].node_memdev == NULL)) {
  1272. static bool warned;
  1273. if (!warned) {
  1274. error_report("Huge page support disabled (n/a for main memory).");
  1275. warned = true;
  1276. }
  1277. return mainrampagesize;
  1278. }
  1279. return hpsize;
  1280. }
  1281. #else
  1282. long qemu_getrampagesize(void)
  1283. {
  1284. return getpagesize();
  1285. }
  1286. #endif
  1287. #ifdef __linux__
  1288. static int64_t get_file_size(int fd)
  1289. {
  1290. int64_t size = lseek(fd, 0, SEEK_END);
  1291. if (size < 0) {
  1292. return -errno;
  1293. }
  1294. return size;
  1295. }
  1296. static int file_ram_open(const char *path,
  1297. const char *region_name,
  1298. bool *created,
  1299. Error **errp)
  1300. {
  1301. char *filename;
  1302. char *sanitized_name;
  1303. char *c;
  1304. int fd = -1;
  1305. *created = false;
  1306. for (;;) {
  1307. fd = open(path, O_RDWR);
  1308. if (fd >= 0) {
  1309. /* @path names an existing file, use it */
  1310. break;
  1311. }
  1312. if (errno == ENOENT) {
  1313. /* @path names a file that doesn't exist, create it */
  1314. fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0644);
  1315. if (fd >= 0) {
  1316. *created = true;
  1317. break;
  1318. }
  1319. } else if (errno == EISDIR) {
  1320. /* @path names a directory, create a file there */
  1321. /* Make name safe to use with mkstemp by replacing '/' with '_'. */
  1322. sanitized_name = g_strdup(region_name);
  1323. for (c = sanitized_name; *c != '\0'; c++) {
  1324. if (*c == '/') {
  1325. *c = '_';
  1326. }
  1327. }
  1328. filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
  1329. sanitized_name);
  1330. g_free(sanitized_name);
  1331. fd = mkstemp(filename);
  1332. if (fd >= 0) {
  1333. unlink(filename);
  1334. g_free(filename);
  1335. break;
  1336. }
  1337. g_free(filename);
  1338. }
  1339. if (errno != EEXIST && errno != EINTR) {
  1340. error_setg_errno(errp, errno,
  1341. "can't open backing store %s for guest RAM",
  1342. path);
  1343. return -1;
  1344. }
  1345. /*
  1346. * Try again on EINTR and EEXIST. The latter happens when
  1347. * something else creates the file between our two open().
  1348. */
  1349. }
  1350. return fd;
  1351. }
  1352. static void *file_ram_alloc(RAMBlock *block,
  1353. ram_addr_t memory,
  1354. int fd,
  1355. bool truncate,
  1356. Error **errp)
  1357. {
  1358. void *area;
  1359. block->page_size = qemu_fd_getpagesize(fd);
  1360. block->mr->align = block->page_size;
  1361. #if defined(__s390x__)
  1362. if (kvm_enabled()) {
  1363. block->mr->align = MAX(block->mr->align, QEMU_VMALLOC_ALIGN);
  1364. }
  1365. #endif
  1366. if (memory < block->page_size) {
  1367. error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to "
  1368. "or larger than page size 0x%zx",
  1369. memory, block->page_size);
  1370. return NULL;
  1371. }
  1372. memory = ROUND_UP(memory, block->page_size);
  1373. /*
  1374. * ftruncate is not supported by hugetlbfs in older
  1375. * hosts, so don't bother bailing out on errors.
  1376. * If anything goes wrong with it under other filesystems,
  1377. * mmap will fail.
  1378. *
  1379. * Do not truncate the non-empty backend file to avoid corrupting
  1380. * the existing data in the file. Disabling shrinking is not
  1381. * enough. For example, the current vNVDIMM implementation stores
  1382. * the guest NVDIMM labels at the end of the backend file. If the
  1383. * backend file is later extended, QEMU will not be able to find
  1384. * those labels. Therefore, extending the non-empty backend file
  1385. * is disabled as well.
  1386. */
  1387. if (truncate && ftruncate(fd, memory)) {
  1388. perror("ftruncate");
  1389. }
  1390. area = qemu_ram_mmap(fd, memory, block->mr->align,
  1391. block->flags & RAM_SHARED);
  1392. if (area == MAP_FAILED) {
  1393. error_setg_errno(errp, errno,
  1394. "unable to map backing store for guest RAM");
  1395. return NULL;
  1396. }
  1397. if (mem_prealloc) {
  1398. os_mem_prealloc(fd, area, memory, smp_cpus, errp);
  1399. if (errp && *errp) {
  1400. qemu_ram_munmap(area, memory);
  1401. return NULL;
  1402. }
  1403. }
  1404. block->fd = fd;
  1405. return area;
  1406. }
  1407. #endif
  1408. /* Called with the ramlist lock held. */
  1409. static ram_addr_t find_ram_offset(ram_addr_t size)
  1410. {
  1411. RAMBlock *block, *next_block;
  1412. ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
  1413. assert(size != 0); /* it would hand out same offset multiple times */
  1414. if (QLIST_EMPTY_RCU(&ram_list.blocks)) {
  1415. return 0;
  1416. }
  1417. RAMBLOCK_FOREACH(block) {
  1418. ram_addr_t end, next = RAM_ADDR_MAX;
  1419. end = block->offset + block->max_length;
  1420. RAMBLOCK_FOREACH(next_block) {
  1421. if (next_block->offset >= end) {
  1422. next = MIN(next, next_block->offset);
  1423. }
  1424. }
  1425. if (next - end >= size && next - end < mingap) {
  1426. offset = end;
  1427. mingap = next - end;
  1428. }
  1429. }
  1430. if (offset == RAM_ADDR_MAX) {
  1431. fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
  1432. (uint64_t)size);
  1433. abort();
  1434. }
  1435. return offset;
  1436. }
  1437. unsigned long last_ram_page(void)
  1438. {
  1439. RAMBlock *block;
  1440. ram_addr_t last = 0;
  1441. rcu_read_lock();
  1442. RAMBLOCK_FOREACH(block) {
  1443. last = MAX(last, block->offset + block->max_length);
  1444. }
  1445. rcu_read_unlock();
  1446. return last >> TARGET_PAGE_BITS;
  1447. }
  1448. static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
  1449. {
  1450. int ret;
  1451. /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
  1452. if (!machine_dump_guest_core(current_machine)) {
  1453. ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
  1454. if (ret) {
  1455. perror("qemu_madvise");
  1456. fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
  1457. "but dump_guest_core=off specified\n");
  1458. }
  1459. }
  1460. }
  1461. const char *qemu_ram_get_idstr(RAMBlock *rb)
  1462. {
  1463. return rb->idstr;
  1464. }
  1465. bool qemu_ram_is_shared(RAMBlock *rb)
  1466. {
  1467. return rb->flags & RAM_SHARED;
  1468. }
  1469. /* Called with iothread lock held. */
  1470. void qemu_ram_set_idstr(RAMBlock *new_block, const char *name, DeviceState *dev)
  1471. {
  1472. RAMBlock *block;
  1473. assert(new_block);
  1474. assert(!new_block->idstr[0]);
  1475. if (dev) {
  1476. char *id = qdev_get_dev_path(dev);
  1477. if (id) {
  1478. snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
  1479. g_free(id);
  1480. }
  1481. }
  1482. pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
  1483. rcu_read_lock();
  1484. RAMBLOCK_FOREACH(block) {
  1485. if (block != new_block &&
  1486. !strcmp(block->idstr, new_block->idstr)) {
  1487. fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
  1488. new_block->idstr);
  1489. abort();
  1490. }
  1491. }
  1492. rcu_read_unlock();
  1493. }
  1494. /* Called with iothread lock held. */
  1495. void qemu_ram_unset_idstr(RAMBlock *block)
  1496. {
  1497. /* FIXME: arch_init.c assumes that this is not called throughout
  1498. * migration. Ignore the problem since hot-unplug during migration
  1499. * does not work anyway.
  1500. */
  1501. if (block) {
  1502. memset(block->idstr, 0, sizeof(block->idstr));
  1503. }
  1504. }
  1505. size_t qemu_ram_pagesize(RAMBlock *rb)
  1506. {
  1507. return rb->page_size;
  1508. }
  1509. /* Returns the largest size of page in use */
  1510. size_t qemu_ram_pagesize_largest(void)
  1511. {
  1512. RAMBlock *block;
  1513. size_t largest = 0;
  1514. RAMBLOCK_FOREACH(block) {
  1515. largest = MAX(largest, qemu_ram_pagesize(block));
  1516. }
  1517. return largest;
  1518. }
  1519. static int memory_try_enable_merging(void *addr, size_t len)
  1520. {
  1521. if (!machine_mem_merge(current_machine)) {
  1522. /* disabled by the user */
  1523. return 0;
  1524. }
  1525. return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
  1526. }
  1527. /* Only legal before guest might have detected the memory size: e.g. on
  1528. * incoming migration, or right after reset.
  1529. *
  1530. * As memory core doesn't know how is memory accessed, it is up to
  1531. * resize callback to update device state and/or add assertions to detect
  1532. * misuse, if necessary.
  1533. */
  1534. int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp)
  1535. {
  1536. assert(block);
  1537. newsize = HOST_PAGE_ALIGN(newsize);
  1538. if (block->used_length == newsize) {
  1539. return 0;
  1540. }
  1541. if (!(block->flags & RAM_RESIZEABLE)) {
  1542. error_setg_errno(errp, EINVAL,
  1543. "Length mismatch: %s: 0x" RAM_ADDR_FMT
  1544. " in != 0x" RAM_ADDR_FMT, block->idstr,
  1545. newsize, block->used_length);
  1546. return -EINVAL;
  1547. }
  1548. if (block->max_length < newsize) {
  1549. error_setg_errno(errp, EINVAL,
  1550. "Length too large: %s: 0x" RAM_ADDR_FMT
  1551. " > 0x" RAM_ADDR_FMT, block->idstr,
  1552. newsize, block->max_length);
  1553. return -EINVAL;
  1554. }
  1555. cpu_physical_memory_clear_dirty_range(block->offset, block->used_length);
  1556. block->used_length = newsize;
  1557. cpu_physical_memory_set_dirty_range(block->offset, block->used_length,
  1558. DIRTY_CLIENTS_ALL);
  1559. memory_region_set_size(block->mr, newsize);
  1560. if (block->resized) {
  1561. block->resized(block->idstr, newsize, block->host);
  1562. }
  1563. return 0;
  1564. }
  1565. /* Called with ram_list.mutex held */
  1566. static void dirty_memory_extend(ram_addr_t old_ram_size,
  1567. ram_addr_t new_ram_size)
  1568. {
  1569. ram_addr_t old_num_blocks = DIV_ROUND_UP(old_ram_size,
  1570. DIRTY_MEMORY_BLOCK_SIZE);
  1571. ram_addr_t new_num_blocks = DIV_ROUND_UP(new_ram_size,
  1572. DIRTY_MEMORY_BLOCK_SIZE);
  1573. int i;
  1574. /* Only need to extend if block count increased */
  1575. if (new_num_blocks <= old_num_blocks) {
  1576. return;
  1577. }
  1578. for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
  1579. DirtyMemoryBlocks *old_blocks;
  1580. DirtyMemoryBlocks *new_blocks;
  1581. int j;
  1582. old_blocks = atomic_rcu_read(&ram_list.dirty_memory[i]);
  1583. new_blocks = g_malloc(sizeof(*new_blocks) +
  1584. sizeof(new_blocks->blocks[0]) * new_num_blocks);
  1585. if (old_num_blocks) {
  1586. memcpy(new_blocks->blocks, old_blocks->blocks,
  1587. old_num_blocks * sizeof(old_blocks->blocks[0]));
  1588. }
  1589. for (j = old_num_blocks; j < new_num_blocks; j++) {
  1590. new_blocks->blocks[j] = bitmap_new(DIRTY_MEMORY_BLOCK_SIZE);
  1591. }
  1592. atomic_rcu_set(&ram_list.dirty_memory[i], new_blocks);
  1593. if (old_blocks) {
  1594. g_free_rcu(old_blocks, rcu);
  1595. }
  1596. }
  1597. }
  1598. static void ram_block_add(RAMBlock *new_block, Error **errp)
  1599. {
  1600. RAMBlock *block;
  1601. RAMBlock *last_block = NULL;
  1602. ram_addr_t old_ram_size, new_ram_size;
  1603. Error *err = NULL;
  1604. old_ram_size = last_ram_page();
  1605. qemu_mutex_lock_ramlist();
  1606. new_block->offset = find_ram_offset(new_block->max_length);
  1607. if (!new_block->host) {
  1608. if (xen_enabled()) {
  1609. xen_ram_alloc(new_block->offset, new_block->max_length,
  1610. new_block->mr, &err);
  1611. if (err) {
  1612. error_propagate(errp, err);
  1613. qemu_mutex_unlock_ramlist();
  1614. return;
  1615. }
  1616. } else {
  1617. new_block->host = phys_mem_alloc(new_block->max_length,
  1618. &new_block->mr->align);
  1619. if (!new_block->host) {
  1620. error_setg_errno(errp, errno,
  1621. "cannot set up guest memory '%s'",
  1622. memory_region_name(new_block->mr));
  1623. qemu_mutex_unlock_ramlist();
  1624. return;
  1625. }
  1626. memory_try_enable_merging(new_block->host, new_block->max_length);
  1627. }
  1628. }
  1629. new_ram_size = MAX(old_ram_size,
  1630. (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS);
  1631. if (new_ram_size > old_ram_size) {
  1632. dirty_memory_extend(old_ram_size, new_ram_size);
  1633. }
  1634. /* Keep the list sorted from biggest to smallest block. Unlike QTAILQ,
  1635. * QLIST (which has an RCU-friendly variant) does not have insertion at
  1636. * tail, so save the last element in last_block.
  1637. */
  1638. RAMBLOCK_FOREACH(block) {
  1639. last_block = block;
  1640. if (block->max_length < new_block->max_length) {
  1641. break;
  1642. }
  1643. }
  1644. if (block) {
  1645. QLIST_INSERT_BEFORE_RCU(block, new_block, next);
  1646. } else if (last_block) {
  1647. QLIST_INSERT_AFTER_RCU(last_block, new_block, next);
  1648. } else { /* list is empty */
  1649. QLIST_INSERT_HEAD_RCU(&ram_list.blocks, new_block, next);
  1650. }
  1651. ram_list.mru_block = NULL;
  1652. /* Write list before version */
  1653. smp_wmb();
  1654. ram_list.version++;
  1655. qemu_mutex_unlock_ramlist();
  1656. cpu_physical_memory_set_dirty_range(new_block->offset,
  1657. new_block->used_length,
  1658. DIRTY_CLIENTS_ALL);
  1659. if (new_block->host) {
  1660. qemu_ram_setup_dump(new_block->host, new_block->max_length);
  1661. qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_HUGEPAGE);
  1662. /* MADV_DONTFORK is also needed by KVM in absence of synchronous MMU */
  1663. qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_DONTFORK);
  1664. ram_block_notify_add(new_block->host, new_block->max_length);
  1665. }
  1666. }
  1667. #ifdef __linux__
  1668. RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
  1669. bool share, int fd,
  1670. Error **errp)
  1671. {
  1672. RAMBlock *new_block;
  1673. Error *local_err = NULL;
  1674. int64_t file_size;
  1675. if (xen_enabled()) {
  1676. error_setg(errp, "-mem-path not supported with Xen");
  1677. return NULL;
  1678. }
  1679. if (kvm_enabled() && !kvm_has_sync_mmu()) {
  1680. error_setg(errp,
  1681. "host lacks kvm mmu notifiers, -mem-path unsupported");
  1682. return NULL;
  1683. }
  1684. if (phys_mem_alloc != qemu_anon_ram_alloc) {
  1685. /*
  1686. * file_ram_alloc() needs to allocate just like
  1687. * phys_mem_alloc, but we haven't bothered to provide
  1688. * a hook there.
  1689. */
  1690. error_setg(errp,
  1691. "-mem-path not supported with this accelerator");
  1692. return NULL;
  1693. }
  1694. size = HOST_PAGE_ALIGN(size);
  1695. file_size = get_file_size(fd);
  1696. if (file_size > 0 && file_size < size) {
  1697. error_setg(errp, "backing store %s size 0x%" PRIx64
  1698. " does not match 'size' option 0x" RAM_ADDR_FMT,
  1699. mem_path, file_size, size);
  1700. return NULL;
  1701. }
  1702. new_block = g_malloc0(sizeof(*new_block));
  1703. new_block->mr = mr;
  1704. new_block->used_length = size;
  1705. new_block->max_length = size;
  1706. new_block->flags = share ? RAM_SHARED : 0;
  1707. new_block->host = file_ram_alloc(new_block, size, fd, !file_size, errp);
  1708. if (!new_block->host) {
  1709. g_free(new_block);
  1710. return NULL;
  1711. }
  1712. ram_block_add(new_block, &local_err);
  1713. if (local_err) {
  1714. g_free(new_block);
  1715. error_propagate(errp, local_err);
  1716. return NULL;
  1717. }
  1718. return new_block;
  1719. }
  1720. RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
  1721. bool share, const char *mem_path,
  1722. Error **errp)
  1723. {
  1724. int fd;
  1725. bool created;
  1726. RAMBlock *block;
  1727. fd = file_ram_open(mem_path, memory_region_name(mr), &created, errp);
  1728. if (fd < 0) {
  1729. return NULL;
  1730. }
  1731. block = qemu_ram_alloc_from_fd(size, mr, share, fd, errp);
  1732. if (!block) {
  1733. if (created) {
  1734. unlink(mem_path);
  1735. }
  1736. close(fd);
  1737. return NULL;
  1738. }
  1739. return block;
  1740. }
  1741. #endif
  1742. static
  1743. RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
  1744. void (*resized)(const char*,
  1745. uint64_t length,
  1746. void *host),
  1747. void *host, bool resizeable,
  1748. MemoryRegion *mr, Error **errp)
  1749. {
  1750. RAMBlock *new_block;
  1751. Error *local_err = NULL;
  1752. size = HOST_PAGE_ALIGN(size);
  1753. max_size = HOST_PAGE_ALIGN(max_size);
  1754. new_block = g_malloc0(sizeof(*new_block));
  1755. new_block->mr = mr;
  1756. new_block->resized = resized;
  1757. new_block->used_length = size;
  1758. new_block->max_length = max_size;
  1759. assert(max_size >= size);
  1760. new_block->fd = -1;
  1761. new_block->page_size = getpagesize();
  1762. new_block->host = host;
  1763. if (host) {
  1764. new_block->flags |= RAM_PREALLOC;
  1765. }
  1766. if (resizeable) {
  1767. new_block->flags |= RAM_RESIZEABLE;
  1768. }
  1769. ram_block_add(new_block, &local_err);
  1770. if (local_err) {
  1771. g_free(new_block);
  1772. error_propagate(errp, local_err);
  1773. return NULL;
  1774. }
  1775. return new_block;
  1776. }
  1777. RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
  1778. MemoryRegion *mr, Error **errp)
  1779. {
  1780. return qemu_ram_alloc_internal(size, size, NULL, host, false, mr, errp);
  1781. }
  1782. RAMBlock *qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error **errp)
  1783. {
  1784. return qemu_ram_alloc_internal(size, size, NULL, NULL, false, mr, errp);
  1785. }
  1786. RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t maxsz,
  1787. void (*resized)(const char*,
  1788. uint64_t length,
  1789. void *host),
  1790. MemoryRegion *mr, Error **errp)
  1791. {
  1792. return qemu_ram_alloc_internal(size, maxsz, resized, NULL, true, mr, errp);
  1793. }
  1794. static void reclaim_ramblock(RAMBlock *block)
  1795. {
  1796. if (block->flags & RAM_PREALLOC) {
  1797. ;
  1798. } else if (xen_enabled()) {
  1799. xen_invalidate_map_cache_entry(block->host);
  1800. #ifndef _WIN32
  1801. } else if (block->fd >= 0) {
  1802. qemu_ram_munmap(block->host, block->max_length);
  1803. close(block->fd);
  1804. #endif
  1805. } else {
  1806. qemu_anon_ram_free(block->host, block->max_length);
  1807. }
  1808. g_free(block);
  1809. }
  1810. void qemu_ram_free(RAMBlock *block)
  1811. {
  1812. if (!block) {
  1813. return;
  1814. }
  1815. if (block->host) {
  1816. ram_block_notify_remove(block->host, block->max_length);
  1817. }
  1818. qemu_mutex_lock_ramlist();
  1819. QLIST_REMOVE_RCU(block, next);
  1820. ram_list.mru_block = NULL;
  1821. /* Write list before version */
  1822. smp_wmb();
  1823. ram_list.version++;
  1824. call_rcu(block, reclaim_ramblock, rcu);
  1825. qemu_mutex_unlock_ramlist();
  1826. }
  1827. #ifndef _WIN32
  1828. void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
  1829. {
  1830. RAMBlock *block;
  1831. ram_addr_t offset;
  1832. int flags;
  1833. void *area, *vaddr;
  1834. RAMBLOCK_FOREACH(block) {
  1835. offset = addr - block->offset;
  1836. if (offset < block->max_length) {
  1837. vaddr = ramblock_ptr(block, offset);
  1838. if (block->flags & RAM_PREALLOC) {
  1839. ;
  1840. } else if (xen_enabled()) {
  1841. abort();
  1842. } else {
  1843. flags = MAP_FIXED;
  1844. if (block->fd >= 0) {
  1845. flags |= (block->flags & RAM_SHARED ?
  1846. MAP_SHARED : MAP_PRIVATE);
  1847. area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
  1848. flags, block->fd, offset);
  1849. } else {
  1850. /*
  1851. * Remap needs to match alloc. Accelerators that
  1852. * set phys_mem_alloc never remap. If they did,
  1853. * we'd need a remap hook here.
  1854. */
  1855. assert(phys_mem_alloc == qemu_anon_ram_alloc);
  1856. flags |= MAP_PRIVATE | MAP_ANONYMOUS;
  1857. area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
  1858. flags, -1, 0);
  1859. }
  1860. if (area != vaddr) {
  1861. fprintf(stderr, "Could not remap addr: "
  1862. RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
  1863. length, addr);
  1864. exit(1);
  1865. }
  1866. memory_try_enable_merging(vaddr, length);
  1867. qemu_ram_setup_dump(vaddr, length);
  1868. }
  1869. }
  1870. }
  1871. }
  1872. #endif /* !_WIN32 */
  1873. /* Return a host pointer to ram allocated with qemu_ram_alloc.
  1874. * This should not be used for general purpose DMA. Use address_space_map
  1875. * or address_space_rw instead. For local memory (e.g. video ram) that the
  1876. * device owns, use memory_region_get_ram_ptr.
  1877. *
  1878. * Called within RCU critical section.
  1879. */
  1880. void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr)
  1881. {
  1882. RAMBlock *block = ram_block;
  1883. if (block == NULL) {
  1884. block = qemu_get_ram_block(addr);
  1885. addr -= block->offset;
  1886. }
  1887. if (xen_enabled() && block->host == NULL) {
  1888. /* We need to check if the requested address is in the RAM
  1889. * because we don't want to map the entire memory in QEMU.
  1890. * In that case just map until the end of the page.
  1891. */
  1892. if (block->offset == 0) {
  1893. return xen_map_cache(addr, 0, 0, false);
  1894. }
  1895. block->host = xen_map_cache(block->offset, block->max_length, 1, false);
  1896. }
  1897. return ramblock_ptr(block, addr);
  1898. }
  1899. /* Return a host pointer to guest's ram. Similar to qemu_map_ram_ptr
  1900. * but takes a size argument.
  1901. *
  1902. * Called within RCU critical section.
  1903. */
  1904. static void *qemu_ram_ptr_length(RAMBlock *ram_block, ram_addr_t addr,
  1905. hwaddr *size, bool lock)
  1906. {
  1907. RAMBlock *block = ram_block;
  1908. if (*size == 0) {
  1909. return NULL;
  1910. }
  1911. if (block == NULL) {
  1912. block = qemu_get_ram_block(addr);
  1913. addr -= block->offset;
  1914. }
  1915. *size = MIN(*size, block->max_length - addr);
  1916. if (xen_enabled() && block->host == NULL) {
  1917. /* We need to check if the requested address is in the RAM
  1918. * because we don't want to map the entire memory in QEMU.
  1919. * In that case just map the requested area.
  1920. */
  1921. if (block->offset == 0) {
  1922. return xen_map_cache(addr, *size, lock, lock);
  1923. }
  1924. block->host = xen_map_cache(block->offset, block->max_length, 1, lock);
  1925. }
  1926. return ramblock_ptr(block, addr);
  1927. }
  1928. /*
  1929. * Translates a host ptr back to a RAMBlock, a ram_addr and an offset
  1930. * in that RAMBlock.
  1931. *
  1932. * ptr: Host pointer to look up
  1933. * round_offset: If true round the result offset down to a page boundary
  1934. * *ram_addr: set to result ram_addr
  1935. * *offset: set to result offset within the RAMBlock
  1936. *
  1937. * Returns: RAMBlock (or NULL if not found)
  1938. *
  1939. * By the time this function returns, the returned pointer is not protected
  1940. * by RCU anymore. If the caller is not within an RCU critical section and
  1941. * does not hold the iothread lock, it must have other means of protecting the
  1942. * pointer, such as a reference to the region that includes the incoming
  1943. * ram_addr_t.
  1944. */
  1945. RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
  1946. ram_addr_t *offset)
  1947. {
  1948. RAMBlock *block;
  1949. uint8_t *host = ptr;
  1950. if (xen_enabled()) {
  1951. ram_addr_t ram_addr;
  1952. rcu_read_lock();
  1953. ram_addr = xen_ram_addr_from_mapcache(ptr);
  1954. block = qemu_get_ram_block(ram_addr);
  1955. if (block) {
  1956. *offset = ram_addr - block->offset;
  1957. }
  1958. rcu_read_unlock();
  1959. return block;
  1960. }
  1961. rcu_read_lock();
  1962. block = atomic_rcu_read(&ram_list.mru_block);
  1963. if (block && block->host && host - block->host < block->max_length) {
  1964. goto found;
  1965. }
  1966. RAMBLOCK_FOREACH(block) {
  1967. /* This case append when the block is not mapped. */
  1968. if (block->host == NULL) {
  1969. continue;
  1970. }
  1971. if (host - block->host < block->max_length) {
  1972. goto found;
  1973. }
  1974. }
  1975. rcu_read_unlock();
  1976. return NULL;
  1977. found:
  1978. *offset = (host - block->host);
  1979. if (round_offset) {
  1980. *offset &= TARGET_PAGE_MASK;
  1981. }
  1982. rcu_read_unlock();
  1983. return block;
  1984. }
  1985. /*
  1986. * Finds the named RAMBlock
  1987. *
  1988. * name: The name of RAMBlock to find
  1989. *
  1990. * Returns: RAMBlock (or NULL if not found)
  1991. */
  1992. RAMBlock *qemu_ram_block_by_name(const char *name)
  1993. {
  1994. RAMBlock *block;
  1995. RAMBLOCK_FOREACH(block) {
  1996. if (!strcmp(name, block->idstr)) {
  1997. return block;
  1998. }
  1999. }
  2000. return NULL;
  2001. }
  2002. /* Some of the softmmu routines need to translate from a host pointer
  2003. (typically a TLB entry) back to a ram offset. */
  2004. ram_addr_t qemu_ram_addr_from_host(void *ptr)
  2005. {
  2006. RAMBlock *block;
  2007. ram_addr_t offset;
  2008. block = qemu_ram_block_from_host(ptr, false, &offset);
  2009. if (!block) {
  2010. return RAM_ADDR_INVALID;
  2011. }
  2012. return block->offset + offset;
  2013. }
  2014. /* Called within RCU critical section. */
  2015. void memory_notdirty_write_prepare(NotDirtyInfo *ndi,
  2016. CPUState *cpu,
  2017. vaddr mem_vaddr,
  2018. ram_addr_t ram_addr,
  2019. unsigned size)
  2020. {
  2021. ndi->cpu = cpu;
  2022. ndi->ram_addr = ram_addr;
  2023. ndi->mem_vaddr = mem_vaddr;
  2024. ndi->size = size;
  2025. ndi->locked = false;
  2026. assert(tcg_enabled());
  2027. if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
  2028. ndi->locked = true;
  2029. tb_lock();
  2030. tb_invalidate_phys_page_fast(ram_addr, size);
  2031. }
  2032. }
  2033. /* Called within RCU critical section. */
  2034. void memory_notdirty_write_complete(NotDirtyInfo *ndi)
  2035. {
  2036. if (ndi->locked) {
  2037. tb_unlock();
  2038. }
  2039. /* Set both VGA and migration bits for simplicity and to remove
  2040. * the notdirty callback faster.
  2041. */
  2042. cpu_physical_memory_set_dirty_range(ndi->ram_addr, ndi->size,
  2043. DIRTY_CLIENTS_NOCODE);
  2044. /* we remove the notdirty callback only if the code has been
  2045. flushed */
  2046. if (!cpu_physical_memory_is_clean(ndi->ram_addr)) {
  2047. tlb_set_dirty(ndi->cpu, ndi->mem_vaddr);
  2048. }
  2049. }
  2050. /* Called within RCU critical section. */
  2051. static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
  2052. uint64_t val, unsigned size)
  2053. {
  2054. NotDirtyInfo ndi;
  2055. memory_notdirty_write_prepare(&ndi, current_cpu, current_cpu->mem_io_vaddr,
  2056. ram_addr, size);
  2057. switch (size) {
  2058. case 1:
  2059. stb_p(qemu_map_ram_ptr(NULL, ram_addr), val);
  2060. break;
  2061. case 2:
  2062. stw_p(qemu_map_ram_ptr(NULL, ram_addr), val);
  2063. break;
  2064. case 4:
  2065. stl_p(qemu_map_ram_ptr(NULL, ram_addr), val);
  2066. break;
  2067. case 8:
  2068. stq_p(qemu_map_ram_ptr(NULL, ram_addr), val);
  2069. break;
  2070. default:
  2071. abort();
  2072. }
  2073. memory_notdirty_write_complete(&ndi);
  2074. }
  2075. static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
  2076. unsigned size, bool is_write)
  2077. {
  2078. return is_write;
  2079. }
  2080. static const MemoryRegionOps notdirty_mem_ops = {
  2081. .write = notdirty_mem_write,
  2082. .valid.accepts = notdirty_mem_accepts,
  2083. .endianness = DEVICE_NATIVE_ENDIAN,
  2084. .valid = {
  2085. .min_access_size = 1,
  2086. .max_access_size = 8,
  2087. .unaligned = false,
  2088. },
  2089. .impl = {
  2090. .min_access_size = 1,
  2091. .max_access_size = 8,
  2092. .unaligned = false,
  2093. },
  2094. };
  2095. /* Generate a debug exception if a watchpoint has been hit. */
  2096. static void check_watchpoint(int offset, int len, MemTxAttrs attrs, int flags)
  2097. {
  2098. CPUState *cpu = current_cpu;
  2099. CPUClass *cc = CPU_GET_CLASS(cpu);
  2100. target_ulong vaddr;
  2101. CPUWatchpoint *wp;
  2102. assert(tcg_enabled());
  2103. if (cpu->watchpoint_hit) {
  2104. /* We re-entered the check after replacing the TB. Now raise
  2105. * the debug interrupt so that is will trigger after the
  2106. * current instruction. */
  2107. cpu_interrupt(cpu, CPU_INTERRUPT_DEBUG);
  2108. return;
  2109. }
  2110. vaddr = (cpu->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
  2111. vaddr = cc->adjust_watchpoint_address(cpu, vaddr, len);
  2112. QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
  2113. if (cpu_watchpoint_address_matches(wp, vaddr, len)
  2114. && (wp->flags & flags)) {
  2115. if (flags == BP_MEM_READ) {
  2116. wp->flags |= BP_WATCHPOINT_HIT_READ;
  2117. } else {
  2118. wp->flags |= BP_WATCHPOINT_HIT_WRITE;
  2119. }
  2120. wp->hitaddr = vaddr;
  2121. wp->hitattrs = attrs;
  2122. if (!cpu->watchpoint_hit) {
  2123. if (wp->flags & BP_CPU &&
  2124. !cc->debug_check_watchpoint(cpu, wp)) {
  2125. wp->flags &= ~BP_WATCHPOINT_HIT;
  2126. continue;
  2127. }
  2128. cpu->watchpoint_hit = wp;
  2129. /* Both tb_lock and iothread_mutex will be reset when
  2130. * cpu_loop_exit or cpu_loop_exit_noexc longjmp
  2131. * back into the cpu_exec main loop.
  2132. */
  2133. tb_lock();
  2134. tb_check_watchpoint(cpu);
  2135. if (wp->flags & BP_STOP_BEFORE_ACCESS) {
  2136. cpu->exception_index = EXCP_DEBUG;
  2137. cpu_loop_exit(cpu);
  2138. } else {
  2139. /* Force execution of one insn next time. */
  2140. cpu->cflags_next_tb = 1 | curr_cflags();
  2141. cpu_loop_exit_noexc(cpu);
  2142. }
  2143. }
  2144. } else {
  2145. wp->flags &= ~BP_WATCHPOINT_HIT;
  2146. }
  2147. }
  2148. }
  2149. /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
  2150. so these check for a hit then pass through to the normal out-of-line
  2151. phys routines. */
  2152. static MemTxResult watch_mem_read(void *opaque, hwaddr addr, uint64_t *pdata,
  2153. unsigned size, MemTxAttrs attrs)
  2154. {
  2155. MemTxResult res;
  2156. uint64_t data;
  2157. int asidx = cpu_asidx_from_attrs(current_cpu, attrs);
  2158. AddressSpace *as = current_cpu->cpu_ases[asidx].as;
  2159. check_watchpoint(addr & ~TARGET_PAGE_MASK, size, attrs, BP_MEM_READ);
  2160. switch (size) {
  2161. case 1:
  2162. data = address_space_ldub(as, addr, attrs, &res);
  2163. break;
  2164. case 2:
  2165. data = address_space_lduw(as, addr, attrs, &res);
  2166. break;
  2167. case 4:
  2168. data = address_space_ldl(as, addr, attrs, &res);
  2169. break;
  2170. case 8:
  2171. data = address_space_ldq(as, addr, attrs, &res);
  2172. break;
  2173. default: abort();
  2174. }
  2175. *pdata = data;
  2176. return res;
  2177. }
  2178. static MemTxResult watch_mem_write(void *opaque, hwaddr addr,
  2179. uint64_t val, unsigned size,
  2180. MemTxAttrs attrs)
  2181. {
  2182. MemTxResult res;
  2183. int asidx = cpu_asidx_from_attrs(current_cpu, attrs);
  2184. AddressSpace *as = current_cpu->cpu_ases[asidx].as;
  2185. check_watchpoint(addr & ~TARGET_PAGE_MASK, size, attrs, BP_MEM_WRITE);
  2186. switch (size) {
  2187. case 1:
  2188. address_space_stb(as, addr, val, attrs, &res);
  2189. break;
  2190. case 2:
  2191. address_space_stw(as, addr, val, attrs, &res);
  2192. break;
  2193. case 4:
  2194. address_space_stl(as, addr, val, attrs, &res);
  2195. break;
  2196. case 8:
  2197. address_space_stq(as, addr, val, attrs, &res);
  2198. break;
  2199. default: abort();
  2200. }
  2201. return res;
  2202. }
  2203. static const MemoryRegionOps watch_mem_ops = {
  2204. .read_with_attrs = watch_mem_read,
  2205. .write_with_attrs = watch_mem_write,
  2206. .endianness = DEVICE_NATIVE_ENDIAN,
  2207. .valid = {
  2208. .min_access_size = 1,
  2209. .max_access_size = 8,
  2210. .unaligned = false,
  2211. },
  2212. .impl = {
  2213. .min_access_size = 1,
  2214. .max_access_size = 8,
  2215. .unaligned = false,
  2216. },
  2217. };
  2218. static MemTxResult flatview_read(FlatView *fv, hwaddr addr,
  2219. MemTxAttrs attrs, uint8_t *buf, int len);
  2220. static MemTxResult flatview_write(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
  2221. const uint8_t *buf, int len);
  2222. static bool flatview_access_valid(FlatView *fv, hwaddr addr, int len,
  2223. bool is_write);
  2224. static MemTxResult subpage_read(void *opaque, hwaddr addr, uint64_t *data,
  2225. unsigned len, MemTxAttrs attrs)
  2226. {
  2227. subpage_t *subpage = opaque;
  2228. uint8_t buf[8];
  2229. MemTxResult res;
  2230. #if defined(DEBUG_SUBPAGE)
  2231. printf("%s: subpage %p len %u addr " TARGET_FMT_plx "\n", __func__,
  2232. subpage, len, addr);
  2233. #endif
  2234. res = flatview_read(subpage->fv, addr + subpage->base, attrs, buf, len);
  2235. if (res) {
  2236. return res;
  2237. }
  2238. switch (len) {
  2239. case 1:
  2240. *data = ldub_p(buf);
  2241. return MEMTX_OK;
  2242. case 2:
  2243. *data = lduw_p(buf);
  2244. return MEMTX_OK;
  2245. case 4:
  2246. *data = ldl_p(buf);
  2247. return MEMTX_OK;
  2248. case 8:
  2249. *data = ldq_p(buf);
  2250. return MEMTX_OK;
  2251. default:
  2252. abort();
  2253. }
  2254. }
  2255. static MemTxResult subpage_write(void *opaque, hwaddr addr,
  2256. uint64_t value, unsigned len, MemTxAttrs attrs)
  2257. {
  2258. subpage_t *subpage = opaque;
  2259. uint8_t buf[8];
  2260. #if defined(DEBUG_SUBPAGE)
  2261. printf("%s: subpage %p len %u addr " TARGET_FMT_plx
  2262. " value %"PRIx64"\n",
  2263. __func__, subpage, len, addr, value);
  2264. #endif
  2265. switch (len) {
  2266. case 1:
  2267. stb_p(buf, value);
  2268. break;
  2269. case 2:
  2270. stw_p(buf, value);
  2271. break;
  2272. case 4:
  2273. stl_p(buf, value);
  2274. break;
  2275. case 8:
  2276. stq_p(buf, value);
  2277. break;
  2278. default:
  2279. abort();
  2280. }
  2281. return flatview_write(subpage->fv, addr + subpage->base, attrs, buf, len);
  2282. }
  2283. static bool subpage_accepts(void *opaque, hwaddr addr,
  2284. unsigned len, bool is_write)
  2285. {
  2286. subpage_t *subpage = opaque;
  2287. #if defined(DEBUG_SUBPAGE)
  2288. printf("%s: subpage %p %c len %u addr " TARGET_FMT_plx "\n",
  2289. __func__, subpage, is_write ? 'w' : 'r', len, addr);
  2290. #endif
  2291. return flatview_access_valid(subpage->fv, addr + subpage->base,
  2292. len, is_write);
  2293. }
  2294. static const MemoryRegionOps subpage_ops = {
  2295. .read_with_attrs = subpage_read,
  2296. .write_with_attrs = subpage_write,
  2297. .impl.min_access_size = 1,
  2298. .impl.max_access_size = 8,
  2299. .valid.min_access_size = 1,
  2300. .valid.max_access_size = 8,
  2301. .valid.accepts = subpage_accepts,
  2302. .endianness = DEVICE_NATIVE_ENDIAN,
  2303. };
  2304. static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
  2305. uint16_t section)
  2306. {
  2307. int idx, eidx;
  2308. if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
  2309. return -1;
  2310. idx = SUBPAGE_IDX(start);
  2311. eidx = SUBPAGE_IDX(end);
  2312. #if defined(DEBUG_SUBPAGE)
  2313. printf("%s: %p start %08x end %08x idx %08x eidx %08x section %d\n",
  2314. __func__, mmio, start, end, idx, eidx, section);
  2315. #endif
  2316. for (; idx <= eidx; idx++) {
  2317. mmio->sub_section[idx] = section;
  2318. }
  2319. return 0;
  2320. }
  2321. static subpage_t *subpage_init(FlatView *fv, hwaddr base)
  2322. {
  2323. subpage_t *mmio;
  2324. mmio = g_malloc0(sizeof(subpage_t) + TARGET_PAGE_SIZE * sizeof(uint16_t));
  2325. mmio->fv = fv;
  2326. mmio->base = base;
  2327. memory_region_init_io(&mmio->iomem, NULL, &subpage_ops, mmio,
  2328. NULL, TARGET_PAGE_SIZE);
  2329. mmio->iomem.subpage = true;
  2330. #if defined(DEBUG_SUBPAGE)
  2331. printf("%s: %p base " TARGET_FMT_plx " len %08x\n", __func__,
  2332. mmio, base, TARGET_PAGE_SIZE);
  2333. #endif
  2334. subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, PHYS_SECTION_UNASSIGNED);
  2335. return mmio;
  2336. }
  2337. static uint16_t dummy_section(PhysPageMap *map, FlatView *fv, MemoryRegion *mr)
  2338. {
  2339. assert(fv);
  2340. MemoryRegionSection section = {
  2341. .fv = fv,
  2342. .mr = mr,
  2343. .offset_within_address_space = 0,
  2344. .offset_within_region = 0,
  2345. .size = int128_2_64(),
  2346. };
  2347. return phys_section_add(map, &section);
  2348. }
  2349. MemoryRegion *iotlb_to_region(CPUState *cpu, hwaddr index, MemTxAttrs attrs)
  2350. {
  2351. int asidx = cpu_asidx_from_attrs(cpu, attrs);
  2352. CPUAddressSpace *cpuas = &cpu->cpu_ases[asidx];
  2353. AddressSpaceDispatch *d = atomic_rcu_read(&cpuas->memory_dispatch);
  2354. MemoryRegionSection *sections = d->map.sections;
  2355. return sections[index & ~TARGET_PAGE_MASK].mr;
  2356. }
  2357. static void io_mem_init(void)
  2358. {
  2359. memory_region_init_io(&io_mem_rom, NULL, &unassigned_mem_ops, NULL, NULL, UINT64_MAX);
  2360. memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, NULL,
  2361. NULL, UINT64_MAX);
  2362. /* io_mem_notdirty calls tb_invalidate_phys_page_fast,
  2363. * which can be called without the iothread mutex.
  2364. */
  2365. memory_region_init_io(&io_mem_notdirty, NULL, &notdirty_mem_ops, NULL,
  2366. NULL, UINT64_MAX);
  2367. memory_region_clear_global_locking(&io_mem_notdirty);
  2368. memory_region_init_io(&io_mem_watch, NULL, &watch_mem_ops, NULL,
  2369. NULL, UINT64_MAX);
  2370. }
  2371. AddressSpaceDispatch *address_space_dispatch_new(FlatView *fv)
  2372. {
  2373. AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1);
  2374. uint16_t n;
  2375. n = dummy_section(&d->map, fv, &io_mem_unassigned);
  2376. assert(n == PHYS_SECTION_UNASSIGNED);
  2377. n = dummy_section(&d->map, fv, &io_mem_notdirty);
  2378. assert(n == PHYS_SECTION_NOTDIRTY);
  2379. n = dummy_section(&d->map, fv, &io_mem_rom);
  2380. assert(n == PHYS_SECTION_ROM);
  2381. n = dummy_section(&d->map, fv, &io_mem_watch);
  2382. assert(n == PHYS_SECTION_WATCH);
  2383. d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };
  2384. return d;
  2385. }
  2386. void address_space_dispatch_free(AddressSpaceDispatch *d)
  2387. {
  2388. phys_sections_free(&d->map);
  2389. g_free(d);
  2390. }
  2391. static void tcg_commit(MemoryListener *listener)
  2392. {
  2393. CPUAddressSpace *cpuas;
  2394. AddressSpaceDispatch *d;
  2395. /* since each CPU stores ram addresses in its TLB cache, we must
  2396. reset the modified entries */
  2397. cpuas = container_of(listener, CPUAddressSpace, tcg_as_listener);
  2398. cpu_reloading_memory_map();
  2399. /* The CPU and TLB are protected by the iothread lock.
  2400. * We reload the dispatch pointer now because cpu_reloading_memory_map()
  2401. * may have split the RCU critical section.
  2402. */
  2403. d = address_space_to_dispatch(cpuas->as);
  2404. atomic_rcu_set(&cpuas->memory_dispatch, d);
  2405. tlb_flush(cpuas->cpu);
  2406. }
  2407. static void memory_map_init(void)
  2408. {
  2409. system_memory = g_malloc(sizeof(*system_memory));
  2410. memory_region_init(system_memory, NULL, "system", UINT64_MAX);
  2411. address_space_init(&address_space_memory, system_memory, "memory");
  2412. system_io = g_malloc(sizeof(*system_io));
  2413. memory_region_init_io(system_io, NULL, &unassigned_io_ops, NULL, "io",
  2414. 65536);
  2415. address_space_init(&address_space_io, system_io, "I/O");
  2416. }
  2417. MemoryRegion *get_system_memory(void)
  2418. {
  2419. return system_memory;
  2420. }
  2421. MemoryRegion *get_system_io(void)
  2422. {
  2423. return system_io;
  2424. }
  2425. #endif /* !defined(CONFIG_USER_ONLY) */
  2426. /* physical memory access (slow version, mainly for debug) */
  2427. #if defined(CONFIG_USER_ONLY)
  2428. int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
  2429. uint8_t *buf, int len, int is_write)
  2430. {
  2431. int l, flags;
  2432. target_ulong page;
  2433. void * p;
  2434. while (len > 0) {
  2435. page = addr & TARGET_PAGE_MASK;
  2436. l = (page + TARGET_PAGE_SIZE) - addr;
  2437. if (l > len)
  2438. l = len;
  2439. flags = page_get_flags(page);
  2440. if (!(flags & PAGE_VALID))
  2441. return -1;
  2442. if (is_write) {
  2443. if (!(flags & PAGE_WRITE))
  2444. return -1;
  2445. /* XXX: this code should not depend on lock_user */
  2446. if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
  2447. return -1;
  2448. memcpy(p, buf, l);
  2449. unlock_user(p, addr, l);
  2450. } else {
  2451. if (!(flags & PAGE_READ))
  2452. return -1;
  2453. /* XXX: this code should not depend on lock_user */
  2454. if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
  2455. return -1;
  2456. memcpy(buf, p, l);
  2457. unlock_user(p, addr, 0);
  2458. }
  2459. len -= l;
  2460. buf += l;
  2461. addr += l;
  2462. }
  2463. return 0;
  2464. }
  2465. #else
  2466. static void invalidate_and_set_dirty(MemoryRegion *mr, hwaddr addr,
  2467. hwaddr length)
  2468. {
  2469. uint8_t dirty_log_mask = memory_region_get_dirty_log_mask(mr);
  2470. addr += memory_region_get_ram_addr(mr);
  2471. /* No early return if dirty_log_mask is or becomes 0, because
  2472. * cpu_physical_memory_set_dirty_range will still call
  2473. * xen_modified_memory.
  2474. */
  2475. if (dirty_log_mask) {
  2476. dirty_log_mask =
  2477. cpu_physical_memory_range_includes_clean(addr, length, dirty_log_mask);
  2478. }
  2479. if (dirty_log_mask & (1 << DIRTY_MEMORY_CODE)) {
  2480. assert(tcg_enabled());
  2481. tb_lock();
  2482. tb_invalidate_phys_range(addr, addr + length);
  2483. tb_unlock();
  2484. dirty_log_mask &= ~(1 << DIRTY_MEMORY_CODE);
  2485. }
  2486. cpu_physical_memory_set_dirty_range(addr, length, dirty_log_mask);
  2487. }
  2488. static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
  2489. {
  2490. unsigned access_size_max = mr->ops->valid.max_access_size;
  2491. /* Regions are assumed to support 1-4 byte accesses unless
  2492. otherwise specified. */
  2493. if (access_size_max == 0) {
  2494. access_size_max = 4;
  2495. }
  2496. /* Bound the maximum access by the alignment of the address. */
  2497. if (!mr->ops->impl.unaligned) {
  2498. unsigned align_size_max = addr & -addr;
  2499. if (align_size_max != 0 && align_size_max < access_size_max) {
  2500. access_size_max = align_size_max;
  2501. }
  2502. }
  2503. /* Don't attempt accesses larger than the maximum. */
  2504. if (l > access_size_max) {
  2505. l = access_size_max;
  2506. }
  2507. l = pow2floor(l);
  2508. return l;
  2509. }
  2510. static bool prepare_mmio_access(MemoryRegion *mr)
  2511. {
  2512. bool unlocked = !qemu_mutex_iothread_locked();
  2513. bool release_lock = false;
  2514. if (unlocked && mr->global_locking) {
  2515. qemu_mutex_lock_iothread();
  2516. unlocked = false;
  2517. release_lock = true;
  2518. }
  2519. if (mr->flush_coalesced_mmio) {
  2520. if (unlocked) {
  2521. qemu_mutex_lock_iothread();
  2522. }
  2523. qemu_flush_coalesced_mmio_buffer();
  2524. if (unlocked) {
  2525. qemu_mutex_unlock_iothread();
  2526. }
  2527. }
  2528. return release_lock;
  2529. }
  2530. /* Called within RCU critical section. */
  2531. static MemTxResult flatview_write_continue(FlatView *fv, hwaddr addr,
  2532. MemTxAttrs attrs,
  2533. const uint8_t *buf,
  2534. int len, hwaddr addr1,
  2535. hwaddr l, MemoryRegion *mr)
  2536. {
  2537. uint8_t *ptr;
  2538. uint64_t val;
  2539. MemTxResult result = MEMTX_OK;
  2540. bool release_lock = false;
  2541. for (;;) {
  2542. if (!memory_access_is_direct(mr, true)) {
  2543. release_lock |= prepare_mmio_access(mr);
  2544. l = memory_access_size(mr, l, addr1);
  2545. /* XXX: could force current_cpu to NULL to avoid
  2546. potential bugs */
  2547. switch (l) {
  2548. case 8:
  2549. /* 64 bit write access */
  2550. val = ldq_p(buf);
  2551. result |= memory_region_dispatch_write(mr, addr1, val, 8,
  2552. attrs);
  2553. break;
  2554. case 4:
  2555. /* 32 bit write access */
  2556. val = (uint32_t)ldl_p(buf);
  2557. result |= memory_region_dispatch_write(mr, addr1, val, 4,
  2558. attrs);
  2559. break;
  2560. case 2:
  2561. /* 16 bit write access */
  2562. val = lduw_p(buf);
  2563. result |= memory_region_dispatch_write(mr, addr1, val, 2,
  2564. attrs);
  2565. break;
  2566. case 1:
  2567. /* 8 bit write access */
  2568. val = ldub_p(buf);
  2569. result |= memory_region_dispatch_write(mr, addr1, val, 1,
  2570. attrs);
  2571. break;
  2572. default:
  2573. abort();
  2574. }
  2575. } else {
  2576. /* RAM case */
  2577. ptr = qemu_ram_ptr_length(mr->ram_block, addr1, &l, false);
  2578. memcpy(ptr, buf, l);
  2579. invalidate_and_set_dirty(mr, addr1, l);
  2580. }
  2581. if (release_lock) {
  2582. qemu_mutex_unlock_iothread();
  2583. release_lock = false;
  2584. }
  2585. len -= l;
  2586. buf += l;
  2587. addr += l;
  2588. if (!len) {
  2589. break;
  2590. }
  2591. l = len;
  2592. mr = flatview_translate(fv, addr, &addr1, &l, true);
  2593. }
  2594. return result;
  2595. }
  2596. /* Called from RCU critical section. */
  2597. static MemTxResult flatview_write(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
  2598. const uint8_t *buf, int len)
  2599. {
  2600. hwaddr l;
  2601. hwaddr addr1;
  2602. MemoryRegion *mr;
  2603. MemTxResult result = MEMTX_OK;
  2604. l = len;
  2605. mr = flatview_translate(fv, addr, &addr1, &l, true);
  2606. result = flatview_write_continue(fv, addr, attrs, buf, len,
  2607. addr1, l, mr);
  2608. return result;
  2609. }
  2610. /* Called within RCU critical section. */
  2611. MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
  2612. MemTxAttrs attrs, uint8_t *buf,
  2613. int len, hwaddr addr1, hwaddr l,
  2614. MemoryRegion *mr)
  2615. {
  2616. uint8_t *ptr;
  2617. uint64_t val;
  2618. MemTxResult result = MEMTX_OK;
  2619. bool release_lock = false;
  2620. for (;;) {
  2621. if (!memory_access_is_direct(mr, false)) {
  2622. /* I/O case */
  2623. release_lock |= prepare_mmio_access(mr);
  2624. l = memory_access_size(mr, l, addr1);
  2625. switch (l) {
  2626. case 8:
  2627. /* 64 bit read access */
  2628. result |= memory_region_dispatch_read(mr, addr1, &val, 8,
  2629. attrs);
  2630. stq_p(buf, val);
  2631. break;
  2632. case 4:
  2633. /* 32 bit read access */
  2634. result |= memory_region_dispatch_read(mr, addr1, &val, 4,
  2635. attrs);
  2636. stl_p(buf, val);
  2637. break;
  2638. case 2:
  2639. /* 16 bit read access */
  2640. result |= memory_region_dispatch_read(mr, addr1, &val, 2,
  2641. attrs);
  2642. stw_p(buf, val);
  2643. break;
  2644. case 1:
  2645. /* 8 bit read access */
  2646. result |= memory_region_dispatch_read(mr, addr1, &val, 1,
  2647. attrs);
  2648. stb_p(buf, val);
  2649. break;
  2650. default:
  2651. abort();
  2652. }
  2653. } else {
  2654. /* RAM case */
  2655. ptr = qemu_ram_ptr_length(mr->ram_block, addr1, &l, false);
  2656. memcpy(buf, ptr, l);
  2657. }
  2658. if (release_lock) {
  2659. qemu_mutex_unlock_iothread();
  2660. release_lock = false;
  2661. }
  2662. len -= l;
  2663. buf += l;
  2664. addr += l;
  2665. if (!len) {
  2666. break;
  2667. }
  2668. l = len;
  2669. mr = flatview_translate(fv, addr, &addr1, &l, false);
  2670. }
  2671. return result;
  2672. }
  2673. /* Called from RCU critical section. */
  2674. static MemTxResult flatview_read(FlatView *fv, hwaddr addr,
  2675. MemTxAttrs attrs, uint8_t *buf, int len)
  2676. {
  2677. hwaddr l;
  2678. hwaddr addr1;
  2679. MemoryRegion *mr;
  2680. l = len;
  2681. mr = flatview_translate(fv, addr, &addr1, &l, false);
  2682. return flatview_read_continue(fv, addr, attrs, buf, len,
  2683. addr1, l, mr);
  2684. }
  2685. MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
  2686. MemTxAttrs attrs, uint8_t *buf, int len)
  2687. {
  2688. MemTxResult result = MEMTX_OK;
  2689. FlatView *fv;
  2690. if (len > 0) {
  2691. rcu_read_lock();
  2692. fv = address_space_to_flatview(as);
  2693. result = flatview_read(fv, addr, attrs, buf, len);
  2694. rcu_read_unlock();
  2695. }
  2696. return result;
  2697. }
  2698. MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
  2699. MemTxAttrs attrs,
  2700. const uint8_t *buf, int len)
  2701. {
  2702. MemTxResult result = MEMTX_OK;
  2703. FlatView *fv;
  2704. if (len > 0) {
  2705. rcu_read_lock();
  2706. fv = address_space_to_flatview(as);
  2707. result = flatview_write(fv, addr, attrs, buf, len);
  2708. rcu_read_unlock();
  2709. }
  2710. return result;
  2711. }
  2712. MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
  2713. uint8_t *buf, int len, bool is_write)
  2714. {
  2715. if (is_write) {
  2716. return address_space_write(as, addr, attrs, buf, len);
  2717. } else {
  2718. return address_space_read_full(as, addr, attrs, buf, len);
  2719. }
  2720. }
  2721. void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
  2722. int len, int is_write)
  2723. {
  2724. address_space_rw(&address_space_memory, addr, MEMTXATTRS_UNSPECIFIED,
  2725. buf, len, is_write);
  2726. }
  2727. enum write_rom_type {
  2728. WRITE_DATA,
  2729. FLUSH_CACHE,
  2730. };
  2731. static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,
  2732. hwaddr addr, const uint8_t *buf, int len, enum write_rom_type type)
  2733. {
  2734. hwaddr l;
  2735. uint8_t *ptr;
  2736. hwaddr addr1;
  2737. MemoryRegion *mr;
  2738. rcu_read_lock();
  2739. while (len > 0) {
  2740. l = len;
  2741. mr = address_space_translate(as, addr, &addr1, &l, true);
  2742. if (!(memory_region_is_ram(mr) ||
  2743. memory_region_is_romd(mr))) {
  2744. l = memory_access_size(mr, l, addr1);
  2745. } else {
  2746. /* ROM/RAM case */
  2747. ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
  2748. switch (type) {
  2749. case WRITE_DATA:
  2750. memcpy(ptr, buf, l);
  2751. invalidate_and_set_dirty(mr, addr1, l);
  2752. break;
  2753. case FLUSH_CACHE:
  2754. flush_icache_range((uintptr_t)ptr, (uintptr_t)ptr + l);
  2755. break;
  2756. }
  2757. }
  2758. len -= l;
  2759. buf += l;
  2760. addr += l;
  2761. }
  2762. rcu_read_unlock();
  2763. }
  2764. /* used for ROM loading : can write in RAM and ROM */
  2765. void cpu_physical_memory_write_rom(AddressSpace *as, hwaddr addr,
  2766. const uint8_t *buf, int len)
  2767. {
  2768. cpu_physical_memory_write_rom_internal(as, addr, buf, len, WRITE_DATA);
  2769. }
  2770. void cpu_flush_icache_range(hwaddr start, int len)
  2771. {
  2772. /*
  2773. * This function should do the same thing as an icache flush that was
  2774. * triggered from within the guest. For TCG we are always cache coherent,
  2775. * so there is no need to flush anything. For KVM / Xen we need to flush
  2776. * the host's instruction cache at least.
  2777. */
  2778. if (tcg_enabled()) {
  2779. return;
  2780. }
  2781. cpu_physical_memory_write_rom_internal(&address_space_memory,
  2782. start, NULL, len, FLUSH_CACHE);
  2783. }
  2784. typedef struct {
  2785. MemoryRegion *mr;
  2786. void *buffer;
  2787. hwaddr addr;
  2788. hwaddr len;
  2789. bool in_use;
  2790. } BounceBuffer;
  2791. static BounceBuffer bounce;
  2792. typedef struct MapClient {
  2793. QEMUBH *bh;
  2794. QLIST_ENTRY(MapClient) link;
  2795. } MapClient;
  2796. QemuMutex map_client_list_lock;
  2797. static QLIST_HEAD(map_client_list, MapClient) map_client_list
  2798. = QLIST_HEAD_INITIALIZER(map_client_list);
  2799. static void cpu_unregister_map_client_do(MapClient *client)
  2800. {
  2801. QLIST_REMOVE(client, link);
  2802. g_free(client);
  2803. }
  2804. static void cpu_notify_map_clients_locked(void)
  2805. {
  2806. MapClient *client;
  2807. while (!QLIST_EMPTY(&map_client_list)) {
  2808. client = QLIST_FIRST(&map_client_list);
  2809. qemu_bh_schedule(client->bh);
  2810. cpu_unregister_map_client_do(client);
  2811. }
  2812. }
  2813. void cpu_register_map_client(QEMUBH *bh)
  2814. {
  2815. MapClient *client = g_malloc(sizeof(*client));
  2816. qemu_mutex_lock(&map_client_list_lock);
  2817. client->bh = bh;
  2818. QLIST_INSERT_HEAD(&map_client_list, client, link);
  2819. if (!atomic_read(&bounce.in_use)) {
  2820. cpu_notify_map_clients_locked();
  2821. }
  2822. qemu_mutex_unlock(&map_client_list_lock);
  2823. }
  2824. void cpu_exec_init_all(void)
  2825. {
  2826. qemu_mutex_init(&ram_list.mutex);
  2827. /* The data structures we set up here depend on knowing the page size,
  2828. * so no more changes can be made after this point.
  2829. * In an ideal world, nothing we did before we had finished the
  2830. * machine setup would care about the target page size, and we could
  2831. * do this much later, rather than requiring board models to state
  2832. * up front what their requirements are.
  2833. */
  2834. finalize_target_page_bits();
  2835. io_mem_init();
  2836. memory_map_init();
  2837. qemu_mutex_init(&map_client_list_lock);
  2838. }
  2839. void cpu_unregister_map_client(QEMUBH *bh)
  2840. {
  2841. MapClient *client;
  2842. qemu_mutex_lock(&map_client_list_lock);
  2843. QLIST_FOREACH(client, &map_client_list, link) {
  2844. if (client->bh == bh) {
  2845. cpu_unregister_map_client_do(client);
  2846. break;
  2847. }
  2848. }
  2849. qemu_mutex_unlock(&map_client_list_lock);
  2850. }
  2851. static void cpu_notify_map_clients(void)
  2852. {
  2853. qemu_mutex_lock(&map_client_list_lock);
  2854. cpu_notify_map_clients_locked();
  2855. qemu_mutex_unlock(&map_client_list_lock);
  2856. }
  2857. static bool flatview_access_valid(FlatView *fv, hwaddr addr, int len,
  2858. bool is_write)
  2859. {
  2860. MemoryRegion *mr;
  2861. hwaddr l, xlat;
  2862. while (len > 0) {
  2863. l = len;
  2864. mr = flatview_translate(fv, addr, &xlat, &l, is_write);
  2865. if (!memory_access_is_direct(mr, is_write)) {
  2866. l = memory_access_size(mr, l, addr);
  2867. if (!memory_region_access_valid(mr, xlat, l, is_write)) {
  2868. return false;
  2869. }
  2870. }
  2871. len -= l;
  2872. addr += l;
  2873. }
  2874. return true;
  2875. }
  2876. bool address_space_access_valid(AddressSpace *as, hwaddr addr,
  2877. int len, bool is_write)
  2878. {
  2879. FlatView *fv;
  2880. bool result;
  2881. rcu_read_lock();
  2882. fv = address_space_to_flatview(as);
  2883. result = flatview_access_valid(fv, addr, len, is_write);
  2884. rcu_read_unlock();
  2885. return result;
  2886. }
  2887. static hwaddr
  2888. flatview_extend_translation(FlatView *fv, hwaddr addr,
  2889. hwaddr target_len,
  2890. MemoryRegion *mr, hwaddr base, hwaddr len,
  2891. bool is_write)
  2892. {
  2893. hwaddr done = 0;
  2894. hwaddr xlat;
  2895. MemoryRegion *this_mr;
  2896. for (;;) {
  2897. target_len -= len;
  2898. addr += len;
  2899. done += len;
  2900. if (target_len == 0) {
  2901. return done;
  2902. }
  2903. len = target_len;
  2904. this_mr = flatview_translate(fv, addr, &xlat,
  2905. &len, is_write);
  2906. if (this_mr != mr || xlat != base + done) {
  2907. return done;
  2908. }
  2909. }
  2910. }
  2911. /* Map a physical memory region into a host virtual address.
  2912. * May map a subset of the requested range, given by and returned in *plen.
  2913. * May return NULL if resources needed to perform the mapping are exhausted.
  2914. * Use only for reads OR writes - not for read-modify-write operations.
  2915. * Use cpu_register_map_client() to know when retrying the map operation is
  2916. * likely to succeed.
  2917. */
  2918. void *address_space_map(AddressSpace *as,
  2919. hwaddr addr,
  2920. hwaddr *plen,
  2921. bool is_write)
  2922. {
  2923. hwaddr len = *plen;
  2924. hwaddr l, xlat;
  2925. MemoryRegion *mr;
  2926. void *ptr;
  2927. FlatView *fv;
  2928. if (len == 0) {
  2929. return NULL;
  2930. }
  2931. l = len;
  2932. rcu_read_lock();
  2933. fv = address_space_to_flatview(as);
  2934. mr = flatview_translate(fv, addr, &xlat, &l, is_write);
  2935. if (!memory_access_is_direct(mr, is_write)) {
  2936. if (atomic_xchg(&bounce.in_use, true)) {
  2937. rcu_read_unlock();
  2938. return NULL;
  2939. }
  2940. /* Avoid unbounded allocations */
  2941. l = MIN(l, TARGET_PAGE_SIZE);
  2942. bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, l);
  2943. bounce.addr = addr;
  2944. bounce.len = l;
  2945. memory_region_ref(mr);
  2946. bounce.mr = mr;
  2947. if (!is_write) {
  2948. flatview_read(fv, addr, MEMTXATTRS_UNSPECIFIED,
  2949. bounce.buffer, l);
  2950. }
  2951. rcu_read_unlock();
  2952. *plen = l;
  2953. return bounce.buffer;
  2954. }
  2955. memory_region_ref(mr);
  2956. *plen = flatview_extend_translation(fv, addr, len, mr, xlat,
  2957. l, is_write);
  2958. ptr = qemu_ram_ptr_length(mr->ram_block, xlat, plen, true);
  2959. rcu_read_unlock();
  2960. return ptr;
  2961. }
  2962. /* Unmaps a memory region previously mapped by address_space_map().
  2963. * Will also mark the memory as dirty if is_write == 1. access_len gives
  2964. * the amount of memory that was actually read or written by the caller.
  2965. */
  2966. void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
  2967. int is_write, hwaddr access_len)
  2968. {
  2969. if (buffer != bounce.buffer) {
  2970. MemoryRegion *mr;
  2971. ram_addr_t addr1;
  2972. mr = memory_region_from_host(buffer, &addr1);
  2973. assert(mr != NULL);
  2974. if (is_write) {
  2975. invalidate_and_set_dirty(mr, addr1, access_len);
  2976. }
  2977. if (xen_enabled()) {
  2978. xen_invalidate_map_cache_entry(buffer);
  2979. }
  2980. memory_region_unref(mr);
  2981. return;
  2982. }
  2983. if (is_write) {
  2984. address_space_write(as, bounce.addr, MEMTXATTRS_UNSPECIFIED,
  2985. bounce.buffer, access_len);
  2986. }
  2987. qemu_vfree(bounce.buffer);
  2988. bounce.buffer = NULL;
  2989. memory_region_unref(bounce.mr);
  2990. atomic_mb_set(&bounce.in_use, false);
  2991. cpu_notify_map_clients();
  2992. }
  2993. void *cpu_physical_memory_map(hwaddr addr,
  2994. hwaddr *plen,
  2995. int is_write)
  2996. {
  2997. return address_space_map(&address_space_memory, addr, plen, is_write);
  2998. }
  2999. void cpu_physical_memory_unmap(void *buffer, hwaddr len,
  3000. int is_write, hwaddr access_len)
  3001. {
  3002. return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
  3003. }
  3004. #define ARG1_DECL AddressSpace *as
  3005. #define ARG1 as
  3006. #define SUFFIX
  3007. #define TRANSLATE(...) address_space_translate(as, __VA_ARGS__)
  3008. #define IS_DIRECT(mr, is_write) memory_access_is_direct(mr, is_write)
  3009. #define MAP_RAM(mr, ofs) qemu_map_ram_ptr((mr)->ram_block, ofs)
  3010. #define INVALIDATE(mr, ofs, len) invalidate_and_set_dirty(mr, ofs, len)
  3011. #define RCU_READ_LOCK(...) rcu_read_lock()
  3012. #define RCU_READ_UNLOCK(...) rcu_read_unlock()
  3013. #include "memory_ldst.inc.c"
  3014. int64_t address_space_cache_init(MemoryRegionCache *cache,
  3015. AddressSpace *as,
  3016. hwaddr addr,
  3017. hwaddr len,
  3018. bool is_write)
  3019. {
  3020. cache->len = len;
  3021. cache->as = as;
  3022. cache->xlat = addr;
  3023. return len;
  3024. }
  3025. void address_space_cache_invalidate(MemoryRegionCache *cache,
  3026. hwaddr addr,
  3027. hwaddr access_len)
  3028. {
  3029. }
  3030. void address_space_cache_destroy(MemoryRegionCache *cache)
  3031. {
  3032. cache->as = NULL;
  3033. }
  3034. #define ARG1_DECL MemoryRegionCache *cache
  3035. #define ARG1 cache
  3036. #define SUFFIX _cached
  3037. #define TRANSLATE(addr, ...) \
  3038. address_space_translate(cache->as, cache->xlat + (addr), __VA_ARGS__)
  3039. #define IS_DIRECT(mr, is_write) true
  3040. #define MAP_RAM(mr, ofs) qemu_map_ram_ptr((mr)->ram_block, ofs)
  3041. #define INVALIDATE(mr, ofs, len) invalidate_and_set_dirty(mr, ofs, len)
  3042. #define RCU_READ_LOCK() rcu_read_lock()
  3043. #define RCU_READ_UNLOCK() rcu_read_unlock()
  3044. #include "memory_ldst.inc.c"
  3045. /* virtual memory access for debug (includes writing to ROM) */
  3046. int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
  3047. uint8_t *buf, int len, int is_write)
  3048. {
  3049. int l;
  3050. hwaddr phys_addr;
  3051. target_ulong page;
  3052. cpu_synchronize_state(cpu);
  3053. while (len > 0) {
  3054. int asidx;
  3055. MemTxAttrs attrs;
  3056. page = addr & TARGET_PAGE_MASK;
  3057. phys_addr = cpu_get_phys_page_attrs_debug(cpu, page, &attrs);
  3058. asidx = cpu_asidx_from_attrs(cpu, attrs);
  3059. /* if no physical page mapped, return an error */
  3060. if (phys_addr == -1)
  3061. return -1;
  3062. l = (page + TARGET_PAGE_SIZE) - addr;
  3063. if (l > len)
  3064. l = len;
  3065. phys_addr += (addr & ~TARGET_PAGE_MASK);
  3066. if (is_write) {
  3067. cpu_physical_memory_write_rom(cpu->cpu_ases[asidx].as,
  3068. phys_addr, buf, l);
  3069. } else {
  3070. address_space_rw(cpu->cpu_ases[asidx].as, phys_addr,
  3071. MEMTXATTRS_UNSPECIFIED,
  3072. buf, l, 0);
  3073. }
  3074. len -= l;
  3075. buf += l;
  3076. addr += l;
  3077. }
  3078. return 0;
  3079. }
  3080. /*
  3081. * Allows code that needs to deal with migration bitmaps etc to still be built
  3082. * target independent.
  3083. */
  3084. size_t qemu_target_page_size(void)
  3085. {
  3086. return TARGET_PAGE_SIZE;
  3087. }
  3088. int qemu_target_page_bits(void)
  3089. {
  3090. return TARGET_PAGE_BITS;
  3091. }
  3092. int qemu_target_page_bits_min(void)
  3093. {
  3094. return TARGET_PAGE_BITS_MIN;
  3095. }
  3096. #endif
  3097. /*
  3098. * A helper function for the _utterly broken_ virtio device model to find out if
  3099. * it's running on a big endian machine. Don't do this at home kids!
  3100. */
  3101. bool target_words_bigendian(void);
  3102. bool target_words_bigendian(void)
  3103. {
  3104. #if defined(TARGET_WORDS_BIGENDIAN)
  3105. return true;
  3106. #else
  3107. return false;
  3108. #endif
  3109. }
  3110. #ifndef CONFIG_USER_ONLY
  3111. bool cpu_physical_memory_is_io(hwaddr phys_addr)
  3112. {
  3113. MemoryRegion*mr;
  3114. hwaddr l = 1;
  3115. bool res;
  3116. rcu_read_lock();
  3117. mr = address_space_translate(&address_space_memory,
  3118. phys_addr, &phys_addr, &l, false);
  3119. res = !(memory_region_is_ram(mr) || memory_region_is_romd(mr));
  3120. rcu_read_unlock();
  3121. return res;
  3122. }
  3123. int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
  3124. {
  3125. RAMBlock *block;
  3126. int ret = 0;
  3127. rcu_read_lock();
  3128. RAMBLOCK_FOREACH(block) {
  3129. ret = func(block->idstr, block->host, block->offset,
  3130. block->used_length, opaque);
  3131. if (ret) {
  3132. break;
  3133. }
  3134. }
  3135. rcu_read_unlock();
  3136. return ret;
  3137. }
  3138. /*
  3139. * Unmap pages of memory from start to start+length such that
  3140. * they a) read as 0, b) Trigger whatever fault mechanism
  3141. * the OS provides for postcopy.
  3142. * The pages must be unmapped by the end of the function.
  3143. * Returns: 0 on success, none-0 on failure
  3144. *
  3145. */
  3146. int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length)
  3147. {
  3148. int ret = -1;
  3149. uint8_t *host_startaddr = rb->host + start;
  3150. if ((uintptr_t)host_startaddr & (rb->page_size - 1)) {
  3151. error_report("ram_block_discard_range: Unaligned start address: %p",
  3152. host_startaddr);
  3153. goto err;
  3154. }
  3155. if ((start + length) <= rb->used_length) {
  3156. uint8_t *host_endaddr = host_startaddr + length;
  3157. if ((uintptr_t)host_endaddr & (rb->page_size - 1)) {
  3158. error_report("ram_block_discard_range: Unaligned end address: %p",
  3159. host_endaddr);
  3160. goto err;
  3161. }
  3162. errno = ENOTSUP; /* If we are missing MADVISE etc */
  3163. if (rb->page_size == qemu_host_page_size) {
  3164. #if defined(CONFIG_MADVISE)
  3165. /* Note: We need the madvise MADV_DONTNEED behaviour of definitely
  3166. * freeing the page.
  3167. */
  3168. ret = madvise(host_startaddr, length, MADV_DONTNEED);
  3169. #endif
  3170. } else {
  3171. /* Huge page case - unfortunately it can't do DONTNEED, but
  3172. * it can do the equivalent by FALLOC_FL_PUNCH_HOLE in the
  3173. * huge page file.
  3174. */
  3175. #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
  3176. ret = fallocate(rb->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  3177. start, length);
  3178. #endif
  3179. }
  3180. if (ret) {
  3181. ret = -errno;
  3182. error_report("ram_block_discard_range: Failed to discard range "
  3183. "%s:%" PRIx64 " +%zx (%d)",
  3184. rb->idstr, start, length, ret);
  3185. }
  3186. } else {
  3187. error_report("ram_block_discard_range: Overrun block '%s' (%" PRIu64
  3188. "/%zx/" RAM_ADDR_FMT")",
  3189. rb->idstr, start, length, rb->used_length);
  3190. }
  3191. err:
  3192. return ret;
  3193. }
  3194. #endif
  3195. void page_size_init(void)
  3196. {
  3197. /* NOTE: we can always suppose that qemu_host_page_size >=
  3198. TARGET_PAGE_SIZE */
  3199. if (qemu_host_page_size == 0) {
  3200. qemu_host_page_size = qemu_real_host_page_size;
  3201. }
  3202. if (qemu_host_page_size < TARGET_PAGE_SIZE) {
  3203. qemu_host_page_size = TARGET_PAGE_SIZE;
  3204. }
  3205. qemu_host_page_mask = -(intptr_t)qemu_host_page_size;
  3206. }
  3207. #if !defined(CONFIG_USER_ONLY)
  3208. static void mtree_print_phys_entries(fprintf_function mon, void *f,
  3209. int start, int end, int skip, int ptr)
  3210. {
  3211. if (start == end - 1) {
  3212. mon(f, "\t%3d ", start);
  3213. } else {
  3214. mon(f, "\t%3d..%-3d ", start, end - 1);
  3215. }
  3216. mon(f, " skip=%d ", skip);
  3217. if (ptr == PHYS_MAP_NODE_NIL) {
  3218. mon(f, " ptr=NIL");
  3219. } else if (!skip) {
  3220. mon(f, " ptr=#%d", ptr);
  3221. } else {
  3222. mon(f, " ptr=[%d]", ptr);
  3223. }
  3224. mon(f, "\n");
  3225. }
  3226. #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
  3227. int128_sub((size), int128_one())) : 0)
  3228. void mtree_print_dispatch(fprintf_function mon, void *f,
  3229. AddressSpaceDispatch *d, MemoryRegion *root)
  3230. {
  3231. int i;
  3232. mon(f, " Dispatch\n");
  3233. mon(f, " Physical sections\n");
  3234. for (i = 0; i < d->map.sections_nb; ++i) {
  3235. MemoryRegionSection *s = d->map.sections + i;
  3236. const char *names[] = { " [unassigned]", " [not dirty]",
  3237. " [ROM]", " [watch]" };
  3238. mon(f, " #%d @" TARGET_FMT_plx ".." TARGET_FMT_plx " %s%s%s%s%s",
  3239. i,
  3240. s->offset_within_address_space,
  3241. s->offset_within_address_space + MR_SIZE(s->mr->size),
  3242. s->mr->name ? s->mr->name : "(noname)",
  3243. i < ARRAY_SIZE(names) ? names[i] : "",
  3244. s->mr == root ? " [ROOT]" : "",
  3245. s == d->mru_section ? " [MRU]" : "",
  3246. s->mr->is_iommu ? " [iommu]" : "");
  3247. if (s->mr->alias) {
  3248. mon(f, " alias=%s", s->mr->alias->name ?
  3249. s->mr->alias->name : "noname");
  3250. }
  3251. mon(f, "\n");
  3252. }
  3253. mon(f, " Nodes (%d bits per level, %d levels) ptr=[%d] skip=%d\n",
  3254. P_L2_BITS, P_L2_LEVELS, d->phys_map.ptr, d->phys_map.skip);
  3255. for (i = 0; i < d->map.nodes_nb; ++i) {
  3256. int j, jprev;
  3257. PhysPageEntry prev;
  3258. Node *n = d->map.nodes + i;
  3259. mon(f, " [%d]\n", i);
  3260. for (j = 0, jprev = 0, prev = *n[0]; j < ARRAY_SIZE(*n); ++j) {
  3261. PhysPageEntry *pe = *n + j;
  3262. if (pe->ptr == prev.ptr && pe->skip == prev.skip) {
  3263. continue;
  3264. }
  3265. mtree_print_phys_entries(mon, f, jprev, j, prev.skip, prev.ptr);
  3266. jprev = j;
  3267. prev = *pe;
  3268. }
  3269. if (jprev != ARRAY_SIZE(*n)) {
  3270. mtree_print_phys_entries(mon, f, jprev, j, prev.skip, prev.ptr);
  3271. }
  3272. }
  3273. }
  3274. #endif