exec.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616
  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 "config.h"
  20. #ifdef _WIN32
  21. #include <windows.h>
  22. #else
  23. #include <sys/types.h>
  24. #include <sys/mman.h>
  25. #endif
  26. #include "qemu-common.h"
  27. #include "cpu.h"
  28. #include "tcg.h"
  29. #include "hw/hw.h"
  30. #include "hw/qdev.h"
  31. #include "qemu/osdep.h"
  32. #include "sysemu/kvm.h"
  33. #include "hw/xen.h"
  34. #include "qemu/timer.h"
  35. #include "qemu/config-file.h"
  36. #include "exec/memory.h"
  37. #include "sysemu/dma.h"
  38. #include "exec/address-spaces.h"
  39. #if defined(CONFIG_USER_ONLY)
  40. #include <qemu.h>
  41. #else /* !CONFIG_USER_ONLY */
  42. #include "sysemu/xen-mapcache.h"
  43. #include "trace.h"
  44. #endif
  45. #include "exec/cpu-all.h"
  46. #include "exec/cputlb.h"
  47. #include "translate-all.h"
  48. #include "exec/memory-internal.h"
  49. //#define DEBUG_UNASSIGNED
  50. //#define DEBUG_SUBPAGE
  51. #if !defined(CONFIG_USER_ONLY)
  52. int phys_ram_fd;
  53. static int in_migration;
  54. RAMList ram_list = { .blocks = QTAILQ_HEAD_INITIALIZER(ram_list.blocks) };
  55. static MemoryRegion *system_memory;
  56. static MemoryRegion *system_io;
  57. AddressSpace address_space_io;
  58. AddressSpace address_space_memory;
  59. DMAContext dma_context_memory;
  60. MemoryRegion io_mem_ram, io_mem_rom, io_mem_unassigned, io_mem_notdirty;
  61. static MemoryRegion io_mem_subpage_ram;
  62. #endif
  63. CPUArchState *first_cpu;
  64. /* current CPU in the current thread. It is only valid inside
  65. cpu_exec() */
  66. DEFINE_TLS(CPUArchState *,cpu_single_env);
  67. /* 0 = Do not count executed instructions.
  68. 1 = Precise instruction counting.
  69. 2 = Adaptive rate instruction counting. */
  70. int use_icount;
  71. #if !defined(CONFIG_USER_ONLY)
  72. static MemoryRegionSection *phys_sections;
  73. static unsigned phys_sections_nb, phys_sections_nb_alloc;
  74. static uint16_t phys_section_unassigned;
  75. static uint16_t phys_section_notdirty;
  76. static uint16_t phys_section_rom;
  77. static uint16_t phys_section_watch;
  78. /* Simple allocator for PhysPageEntry nodes */
  79. static PhysPageEntry (*phys_map_nodes)[L2_SIZE];
  80. static unsigned phys_map_nodes_nb, phys_map_nodes_nb_alloc;
  81. #define PHYS_MAP_NODE_NIL (((uint16_t)~0) >> 1)
  82. static void io_mem_init(void);
  83. static void memory_map_init(void);
  84. static void *qemu_safe_ram_ptr(ram_addr_t addr);
  85. static MemoryRegion io_mem_watch;
  86. #endif
  87. #if !defined(CONFIG_USER_ONLY)
  88. static void phys_map_node_reserve(unsigned nodes)
  89. {
  90. if (phys_map_nodes_nb + nodes > phys_map_nodes_nb_alloc) {
  91. typedef PhysPageEntry Node[L2_SIZE];
  92. phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc * 2, 16);
  93. phys_map_nodes_nb_alloc = MAX(phys_map_nodes_nb_alloc,
  94. phys_map_nodes_nb + nodes);
  95. phys_map_nodes = g_renew(Node, phys_map_nodes,
  96. phys_map_nodes_nb_alloc);
  97. }
  98. }
  99. static uint16_t phys_map_node_alloc(void)
  100. {
  101. unsigned i;
  102. uint16_t ret;
  103. ret = phys_map_nodes_nb++;
  104. assert(ret != PHYS_MAP_NODE_NIL);
  105. assert(ret != phys_map_nodes_nb_alloc);
  106. for (i = 0; i < L2_SIZE; ++i) {
  107. phys_map_nodes[ret][i].is_leaf = 0;
  108. phys_map_nodes[ret][i].ptr = PHYS_MAP_NODE_NIL;
  109. }
  110. return ret;
  111. }
  112. static void phys_map_nodes_reset(void)
  113. {
  114. phys_map_nodes_nb = 0;
  115. }
  116. static void phys_page_set_level(PhysPageEntry *lp, hwaddr *index,
  117. hwaddr *nb, uint16_t leaf,
  118. int level)
  119. {
  120. PhysPageEntry *p;
  121. int i;
  122. hwaddr step = (hwaddr)1 << (level * L2_BITS);
  123. if (!lp->is_leaf && lp->ptr == PHYS_MAP_NODE_NIL) {
  124. lp->ptr = phys_map_node_alloc();
  125. p = phys_map_nodes[lp->ptr];
  126. if (level == 0) {
  127. for (i = 0; i < L2_SIZE; i++) {
  128. p[i].is_leaf = 1;
  129. p[i].ptr = phys_section_unassigned;
  130. }
  131. }
  132. } else {
  133. p = phys_map_nodes[lp->ptr];
  134. }
  135. lp = &p[(*index >> (level * L2_BITS)) & (L2_SIZE - 1)];
  136. while (*nb && lp < &p[L2_SIZE]) {
  137. if ((*index & (step - 1)) == 0 && *nb >= step) {
  138. lp->is_leaf = true;
  139. lp->ptr = leaf;
  140. *index += step;
  141. *nb -= step;
  142. } else {
  143. phys_page_set_level(lp, index, nb, leaf, level - 1);
  144. }
  145. ++lp;
  146. }
  147. }
  148. static void phys_page_set(AddressSpaceDispatch *d,
  149. hwaddr index, hwaddr nb,
  150. uint16_t leaf)
  151. {
  152. /* Wildly overreserve - it doesn't matter much. */
  153. phys_map_node_reserve(3 * P_L2_LEVELS);
  154. phys_page_set_level(&d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
  155. }
  156. MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr index)
  157. {
  158. PhysPageEntry lp = d->phys_map;
  159. PhysPageEntry *p;
  160. int i;
  161. uint16_t s_index = phys_section_unassigned;
  162. for (i = P_L2_LEVELS - 1; i >= 0 && !lp.is_leaf; i--) {
  163. if (lp.ptr == PHYS_MAP_NODE_NIL) {
  164. goto not_found;
  165. }
  166. p = phys_map_nodes[lp.ptr];
  167. lp = p[(index >> (i * L2_BITS)) & (L2_SIZE - 1)];
  168. }
  169. s_index = lp.ptr;
  170. not_found:
  171. return &phys_sections[s_index];
  172. }
  173. bool memory_region_is_unassigned(MemoryRegion *mr)
  174. {
  175. return mr != &io_mem_ram && mr != &io_mem_rom
  176. && mr != &io_mem_notdirty && !mr->rom_device
  177. && mr != &io_mem_watch;
  178. }
  179. #endif
  180. void cpu_exec_init_all(void)
  181. {
  182. #if !defined(CONFIG_USER_ONLY)
  183. qemu_mutex_init(&ram_list.mutex);
  184. memory_map_init();
  185. io_mem_init();
  186. #endif
  187. }
  188. #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
  189. static int cpu_common_post_load(void *opaque, int version_id)
  190. {
  191. CPUArchState *env = opaque;
  192. /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
  193. version_id is increased. */
  194. env->interrupt_request &= ~0x01;
  195. tlb_flush(env, 1);
  196. return 0;
  197. }
  198. static const VMStateDescription vmstate_cpu_common = {
  199. .name = "cpu_common",
  200. .version_id = 1,
  201. .minimum_version_id = 1,
  202. .minimum_version_id_old = 1,
  203. .post_load = cpu_common_post_load,
  204. .fields = (VMStateField []) {
  205. VMSTATE_UINT32(halted, CPUArchState),
  206. VMSTATE_UINT32(interrupt_request, CPUArchState),
  207. VMSTATE_END_OF_LIST()
  208. }
  209. };
  210. #endif
  211. CPUState *qemu_get_cpu(int index)
  212. {
  213. CPUArchState *env = first_cpu;
  214. CPUState *cpu = NULL;
  215. while (env) {
  216. cpu = ENV_GET_CPU(env);
  217. if (cpu->cpu_index == index) {
  218. break;
  219. }
  220. env = env->next_cpu;
  221. }
  222. return cpu;
  223. }
  224. void cpu_exec_init(CPUArchState *env)
  225. {
  226. CPUState *cpu = ENV_GET_CPU(env);
  227. CPUArchState **penv;
  228. int cpu_index;
  229. #if defined(CONFIG_USER_ONLY)
  230. cpu_list_lock();
  231. #endif
  232. env->next_cpu = NULL;
  233. penv = &first_cpu;
  234. cpu_index = 0;
  235. while (*penv != NULL) {
  236. penv = &(*penv)->next_cpu;
  237. cpu_index++;
  238. }
  239. cpu->cpu_index = cpu_index;
  240. cpu->numa_node = 0;
  241. QTAILQ_INIT(&env->breakpoints);
  242. QTAILQ_INIT(&env->watchpoints);
  243. #ifndef CONFIG_USER_ONLY
  244. cpu->thread_id = qemu_get_thread_id();
  245. #endif
  246. *penv = env;
  247. #if defined(CONFIG_USER_ONLY)
  248. cpu_list_unlock();
  249. #endif
  250. #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
  251. vmstate_register(NULL, cpu_index, &vmstate_cpu_common, env);
  252. register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
  253. cpu_save, cpu_load, env);
  254. #endif
  255. }
  256. #if defined(TARGET_HAS_ICE)
  257. #if defined(CONFIG_USER_ONLY)
  258. static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
  259. {
  260. tb_invalidate_phys_page_range(pc, pc + 1, 0);
  261. }
  262. #else
  263. static void breakpoint_invalidate(CPUArchState *env, target_ulong pc)
  264. {
  265. tb_invalidate_phys_addr(cpu_get_phys_page_debug(env, pc) |
  266. (pc & ~TARGET_PAGE_MASK));
  267. }
  268. #endif
  269. #endif /* TARGET_HAS_ICE */
  270. #if defined(CONFIG_USER_ONLY)
  271. void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
  272. {
  273. }
  274. int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
  275. int flags, CPUWatchpoint **watchpoint)
  276. {
  277. return -ENOSYS;
  278. }
  279. #else
  280. /* Add a watchpoint. */
  281. int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len,
  282. int flags, CPUWatchpoint **watchpoint)
  283. {
  284. target_ulong len_mask = ~(len - 1);
  285. CPUWatchpoint *wp;
  286. /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */
  287. if ((len & (len - 1)) || (addr & ~len_mask) ||
  288. len == 0 || len > TARGET_PAGE_SIZE) {
  289. fprintf(stderr, "qemu: tried to set invalid watchpoint at "
  290. TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len);
  291. return -EINVAL;
  292. }
  293. wp = g_malloc(sizeof(*wp));
  294. wp->vaddr = addr;
  295. wp->len_mask = len_mask;
  296. wp->flags = flags;
  297. /* keep all GDB-injected watchpoints in front */
  298. if (flags & BP_GDB)
  299. QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
  300. else
  301. QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
  302. tlb_flush_page(env, addr);
  303. if (watchpoint)
  304. *watchpoint = wp;
  305. return 0;
  306. }
  307. /* Remove a specific watchpoint. */
  308. int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr, target_ulong len,
  309. int flags)
  310. {
  311. target_ulong len_mask = ~(len - 1);
  312. CPUWatchpoint *wp;
  313. QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
  314. if (addr == wp->vaddr && len_mask == wp->len_mask
  315. && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
  316. cpu_watchpoint_remove_by_ref(env, wp);
  317. return 0;
  318. }
  319. }
  320. return -ENOENT;
  321. }
  322. /* Remove a specific watchpoint by reference. */
  323. void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint)
  324. {
  325. QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
  326. tlb_flush_page(env, watchpoint->vaddr);
  327. g_free(watchpoint);
  328. }
  329. /* Remove all matching watchpoints. */
  330. void cpu_watchpoint_remove_all(CPUArchState *env, int mask)
  331. {
  332. CPUWatchpoint *wp, *next;
  333. QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
  334. if (wp->flags & mask)
  335. cpu_watchpoint_remove_by_ref(env, wp);
  336. }
  337. }
  338. #endif
  339. /* Add a breakpoint. */
  340. int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags,
  341. CPUBreakpoint **breakpoint)
  342. {
  343. #if defined(TARGET_HAS_ICE)
  344. CPUBreakpoint *bp;
  345. bp = g_malloc(sizeof(*bp));
  346. bp->pc = pc;
  347. bp->flags = flags;
  348. /* keep all GDB-injected breakpoints in front */
  349. if (flags & BP_GDB)
  350. QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
  351. else
  352. QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
  353. breakpoint_invalidate(env, pc);
  354. if (breakpoint)
  355. *breakpoint = bp;
  356. return 0;
  357. #else
  358. return -ENOSYS;
  359. #endif
  360. }
  361. /* Remove a specific breakpoint. */
  362. int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags)
  363. {
  364. #if defined(TARGET_HAS_ICE)
  365. CPUBreakpoint *bp;
  366. QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
  367. if (bp->pc == pc && bp->flags == flags) {
  368. cpu_breakpoint_remove_by_ref(env, bp);
  369. return 0;
  370. }
  371. }
  372. return -ENOENT;
  373. #else
  374. return -ENOSYS;
  375. #endif
  376. }
  377. /* Remove a specific breakpoint by reference. */
  378. void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint)
  379. {
  380. #if defined(TARGET_HAS_ICE)
  381. QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
  382. breakpoint_invalidate(env, breakpoint->pc);
  383. g_free(breakpoint);
  384. #endif
  385. }
  386. /* Remove all matching breakpoints. */
  387. void cpu_breakpoint_remove_all(CPUArchState *env, int mask)
  388. {
  389. #if defined(TARGET_HAS_ICE)
  390. CPUBreakpoint *bp, *next;
  391. QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
  392. if (bp->flags & mask)
  393. cpu_breakpoint_remove_by_ref(env, bp);
  394. }
  395. #endif
  396. }
  397. /* enable or disable single step mode. EXCP_DEBUG is returned by the
  398. CPU loop after each instruction */
  399. void cpu_single_step(CPUArchState *env, int enabled)
  400. {
  401. #if defined(TARGET_HAS_ICE)
  402. if (env->singlestep_enabled != enabled) {
  403. env->singlestep_enabled = enabled;
  404. if (kvm_enabled())
  405. kvm_update_guest_debug(env, 0);
  406. else {
  407. /* must flush all the translated code to avoid inconsistencies */
  408. /* XXX: only flush what is necessary */
  409. tb_flush(env);
  410. }
  411. }
  412. #endif
  413. }
  414. void cpu_reset_interrupt(CPUArchState *env, int mask)
  415. {
  416. env->interrupt_request &= ~mask;
  417. }
  418. void cpu_exit(CPUArchState *env)
  419. {
  420. env->exit_request = 1;
  421. env->tcg_exit_req = 1;
  422. }
  423. void cpu_abort(CPUArchState *env, const char *fmt, ...)
  424. {
  425. va_list ap;
  426. va_list ap2;
  427. va_start(ap, fmt);
  428. va_copy(ap2, ap);
  429. fprintf(stderr, "qemu: fatal: ");
  430. vfprintf(stderr, fmt, ap);
  431. fprintf(stderr, "\n");
  432. cpu_dump_state(env, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
  433. if (qemu_log_enabled()) {
  434. qemu_log("qemu: fatal: ");
  435. qemu_log_vprintf(fmt, ap2);
  436. qemu_log("\n");
  437. log_cpu_state(env, CPU_DUMP_FPU | CPU_DUMP_CCOP);
  438. qemu_log_flush();
  439. qemu_log_close();
  440. }
  441. va_end(ap2);
  442. va_end(ap);
  443. #if defined(CONFIG_USER_ONLY)
  444. {
  445. struct sigaction act;
  446. sigfillset(&act.sa_mask);
  447. act.sa_handler = SIG_DFL;
  448. sigaction(SIGABRT, &act, NULL);
  449. }
  450. #endif
  451. abort();
  452. }
  453. CPUArchState *cpu_copy(CPUArchState *env)
  454. {
  455. CPUArchState *new_env = cpu_init(env->cpu_model_str);
  456. CPUArchState *next_cpu = new_env->next_cpu;
  457. #if defined(TARGET_HAS_ICE)
  458. CPUBreakpoint *bp;
  459. CPUWatchpoint *wp;
  460. #endif
  461. memcpy(new_env, env, sizeof(CPUArchState));
  462. /* Preserve chaining. */
  463. new_env->next_cpu = next_cpu;
  464. /* Clone all break/watchpoints.
  465. Note: Once we support ptrace with hw-debug register access, make sure
  466. BP_CPU break/watchpoints are handled correctly on clone. */
  467. QTAILQ_INIT(&env->breakpoints);
  468. QTAILQ_INIT(&env->watchpoints);
  469. #if defined(TARGET_HAS_ICE)
  470. QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
  471. cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
  472. }
  473. QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
  474. cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
  475. wp->flags, NULL);
  476. }
  477. #endif
  478. return new_env;
  479. }
  480. #if !defined(CONFIG_USER_ONLY)
  481. static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t end,
  482. uintptr_t length)
  483. {
  484. uintptr_t start1;
  485. /* we modify the TLB cache so that the dirty bit will be set again
  486. when accessing the range */
  487. start1 = (uintptr_t)qemu_safe_ram_ptr(start);
  488. /* Check that we don't span multiple blocks - this breaks the
  489. address comparisons below. */
  490. if ((uintptr_t)qemu_safe_ram_ptr(end - 1) - start1
  491. != (end - 1) - start) {
  492. abort();
  493. }
  494. cpu_tlb_reset_dirty_all(start1, length);
  495. }
  496. /* Note: start and end must be within the same ram block. */
  497. void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
  498. int dirty_flags)
  499. {
  500. uintptr_t length;
  501. start &= TARGET_PAGE_MASK;
  502. end = TARGET_PAGE_ALIGN(end);
  503. length = end - start;
  504. if (length == 0)
  505. return;
  506. cpu_physical_memory_mask_dirty_range(start, length, dirty_flags);
  507. if (tcg_enabled()) {
  508. tlb_reset_dirty_range_all(start, end, length);
  509. }
  510. }
  511. static int cpu_physical_memory_set_dirty_tracking(int enable)
  512. {
  513. int ret = 0;
  514. in_migration = enable;
  515. return ret;
  516. }
  517. hwaddr memory_region_section_get_iotlb(CPUArchState *env,
  518. MemoryRegionSection *section,
  519. target_ulong vaddr,
  520. hwaddr paddr,
  521. int prot,
  522. target_ulong *address)
  523. {
  524. hwaddr iotlb;
  525. CPUWatchpoint *wp;
  526. if (memory_region_is_ram(section->mr)) {
  527. /* Normal RAM. */
  528. iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
  529. + memory_region_section_addr(section, paddr);
  530. if (!section->readonly) {
  531. iotlb |= phys_section_notdirty;
  532. } else {
  533. iotlb |= phys_section_rom;
  534. }
  535. } else {
  536. /* IO handlers are currently passed a physical address.
  537. It would be nice to pass an offset from the base address
  538. of that region. This would avoid having to special case RAM,
  539. and avoid full address decoding in every device.
  540. We can't use the high bits of pd for this because
  541. IO_MEM_ROMD uses these as a ram address. */
  542. iotlb = section - phys_sections;
  543. iotlb += memory_region_section_addr(section, paddr);
  544. }
  545. /* Make accesses to pages with watchpoints go via the
  546. watchpoint trap routines. */
  547. QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
  548. if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
  549. /* Avoid trapping reads of pages with a write breakpoint. */
  550. if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
  551. iotlb = phys_section_watch + paddr;
  552. *address |= TLB_MMIO;
  553. break;
  554. }
  555. }
  556. }
  557. return iotlb;
  558. }
  559. #endif /* defined(CONFIG_USER_ONLY) */
  560. #if !defined(CONFIG_USER_ONLY)
  561. #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
  562. typedef struct subpage_t {
  563. MemoryRegion iomem;
  564. hwaddr base;
  565. uint16_t sub_section[TARGET_PAGE_SIZE];
  566. } subpage_t;
  567. static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
  568. uint16_t section);
  569. static subpage_t *subpage_init(hwaddr base);
  570. static void destroy_page_desc(uint16_t section_index)
  571. {
  572. MemoryRegionSection *section = &phys_sections[section_index];
  573. MemoryRegion *mr = section->mr;
  574. if (mr->subpage) {
  575. subpage_t *subpage = container_of(mr, subpage_t, iomem);
  576. memory_region_destroy(&subpage->iomem);
  577. g_free(subpage);
  578. }
  579. }
  580. static void destroy_l2_mapping(PhysPageEntry *lp, unsigned level)
  581. {
  582. unsigned i;
  583. PhysPageEntry *p;
  584. if (lp->ptr == PHYS_MAP_NODE_NIL) {
  585. return;
  586. }
  587. p = phys_map_nodes[lp->ptr];
  588. for (i = 0; i < L2_SIZE; ++i) {
  589. if (!p[i].is_leaf) {
  590. destroy_l2_mapping(&p[i], level - 1);
  591. } else {
  592. destroy_page_desc(p[i].ptr);
  593. }
  594. }
  595. lp->is_leaf = 0;
  596. lp->ptr = PHYS_MAP_NODE_NIL;
  597. }
  598. static void destroy_all_mappings(AddressSpaceDispatch *d)
  599. {
  600. destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
  601. phys_map_nodes_reset();
  602. }
  603. static uint16_t phys_section_add(MemoryRegionSection *section)
  604. {
  605. if (phys_sections_nb == phys_sections_nb_alloc) {
  606. phys_sections_nb_alloc = MAX(phys_sections_nb_alloc * 2, 16);
  607. phys_sections = g_renew(MemoryRegionSection, phys_sections,
  608. phys_sections_nb_alloc);
  609. }
  610. phys_sections[phys_sections_nb] = *section;
  611. return phys_sections_nb++;
  612. }
  613. static void phys_sections_clear(void)
  614. {
  615. phys_sections_nb = 0;
  616. }
  617. static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)
  618. {
  619. subpage_t *subpage;
  620. hwaddr base = section->offset_within_address_space
  621. & TARGET_PAGE_MASK;
  622. MemoryRegionSection *existing = phys_page_find(d, base >> TARGET_PAGE_BITS);
  623. MemoryRegionSection subsection = {
  624. .offset_within_address_space = base,
  625. .size = TARGET_PAGE_SIZE,
  626. };
  627. hwaddr start, end;
  628. assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
  629. if (!(existing->mr->subpage)) {
  630. subpage = subpage_init(base);
  631. subsection.mr = &subpage->iomem;
  632. phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
  633. phys_section_add(&subsection));
  634. } else {
  635. subpage = container_of(existing->mr, subpage_t, iomem);
  636. }
  637. start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
  638. end = start + section->size - 1;
  639. subpage_register(subpage, start, end, phys_section_add(section));
  640. }
  641. static void register_multipage(AddressSpaceDispatch *d, MemoryRegionSection *section)
  642. {
  643. hwaddr start_addr = section->offset_within_address_space;
  644. ram_addr_t size = section->size;
  645. hwaddr addr;
  646. uint16_t section_index = phys_section_add(section);
  647. assert(size);
  648. addr = start_addr;
  649. phys_page_set(d, addr >> TARGET_PAGE_BITS, size >> TARGET_PAGE_BITS,
  650. section_index);
  651. }
  652. static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
  653. {
  654. AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);
  655. MemoryRegionSection now = *section, remain = *section;
  656. if ((now.offset_within_address_space & ~TARGET_PAGE_MASK)
  657. || (now.size < TARGET_PAGE_SIZE)) {
  658. now.size = MIN(TARGET_PAGE_ALIGN(now.offset_within_address_space)
  659. - now.offset_within_address_space,
  660. now.size);
  661. register_subpage(d, &now);
  662. remain.size -= now.size;
  663. remain.offset_within_address_space += now.size;
  664. remain.offset_within_region += now.size;
  665. }
  666. while (remain.size >= TARGET_PAGE_SIZE) {
  667. now = remain;
  668. if (remain.offset_within_region & ~TARGET_PAGE_MASK) {
  669. now.size = TARGET_PAGE_SIZE;
  670. register_subpage(d, &now);
  671. } else {
  672. now.size &= TARGET_PAGE_MASK;
  673. register_multipage(d, &now);
  674. }
  675. remain.size -= now.size;
  676. remain.offset_within_address_space += now.size;
  677. remain.offset_within_region += now.size;
  678. }
  679. now = remain;
  680. if (now.size) {
  681. register_subpage(d, &now);
  682. }
  683. }
  684. void qemu_flush_coalesced_mmio_buffer(void)
  685. {
  686. if (kvm_enabled())
  687. kvm_flush_coalesced_mmio_buffer();
  688. }
  689. void qemu_mutex_lock_ramlist(void)
  690. {
  691. qemu_mutex_lock(&ram_list.mutex);
  692. }
  693. void qemu_mutex_unlock_ramlist(void)
  694. {
  695. qemu_mutex_unlock(&ram_list.mutex);
  696. }
  697. #if defined(__linux__) && !defined(TARGET_S390X)
  698. #include <sys/vfs.h>
  699. #define HUGETLBFS_MAGIC 0x958458f6
  700. static long gethugepagesize(const char *path)
  701. {
  702. struct statfs fs;
  703. int ret;
  704. do {
  705. ret = statfs(path, &fs);
  706. } while (ret != 0 && errno == EINTR);
  707. if (ret != 0) {
  708. perror(path);
  709. return 0;
  710. }
  711. if (fs.f_type != HUGETLBFS_MAGIC)
  712. fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
  713. return fs.f_bsize;
  714. }
  715. static void *file_ram_alloc(RAMBlock *block,
  716. ram_addr_t memory,
  717. const char *path)
  718. {
  719. char *filename;
  720. void *area;
  721. int fd;
  722. #ifdef MAP_POPULATE
  723. int flags;
  724. #endif
  725. unsigned long hpagesize;
  726. hpagesize = gethugepagesize(path);
  727. if (!hpagesize) {
  728. return NULL;
  729. }
  730. if (memory < hpagesize) {
  731. return NULL;
  732. }
  733. if (kvm_enabled() && !kvm_has_sync_mmu()) {
  734. fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n");
  735. return NULL;
  736. }
  737. filename = g_strdup_printf("%s/qemu_back_mem.XXXXXX", path);
  738. fd = mkstemp(filename);
  739. if (fd < 0) {
  740. perror("unable to create backing store for hugepages");
  741. g_free(filename);
  742. return NULL;
  743. }
  744. unlink(filename);
  745. g_free(filename);
  746. memory = (memory+hpagesize-1) & ~(hpagesize-1);
  747. /*
  748. * ftruncate is not supported by hugetlbfs in older
  749. * hosts, so don't bother bailing out on errors.
  750. * If anything goes wrong with it under other filesystems,
  751. * mmap will fail.
  752. */
  753. if (ftruncate(fd, memory))
  754. perror("ftruncate");
  755. #ifdef MAP_POPULATE
  756. /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
  757. * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
  758. * to sidestep this quirk.
  759. */
  760. flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE;
  761. area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0);
  762. #else
  763. area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  764. #endif
  765. if (area == MAP_FAILED) {
  766. perror("file_ram_alloc: can't mmap RAM pages");
  767. close(fd);
  768. return (NULL);
  769. }
  770. block->fd = fd;
  771. return area;
  772. }
  773. #endif
  774. static ram_addr_t find_ram_offset(ram_addr_t size)
  775. {
  776. RAMBlock *block, *next_block;
  777. ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
  778. if (QTAILQ_EMPTY(&ram_list.blocks))
  779. return 0;
  780. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  781. ram_addr_t end, next = RAM_ADDR_MAX;
  782. end = block->offset + block->length;
  783. QTAILQ_FOREACH(next_block, &ram_list.blocks, next) {
  784. if (next_block->offset >= end) {
  785. next = MIN(next, next_block->offset);
  786. }
  787. }
  788. if (next - end >= size && next - end < mingap) {
  789. offset = end;
  790. mingap = next - end;
  791. }
  792. }
  793. if (offset == RAM_ADDR_MAX) {
  794. fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
  795. (uint64_t)size);
  796. abort();
  797. }
  798. return offset;
  799. }
  800. ram_addr_t last_ram_offset(void)
  801. {
  802. RAMBlock *block;
  803. ram_addr_t last = 0;
  804. QTAILQ_FOREACH(block, &ram_list.blocks, next)
  805. last = MAX(last, block->offset + block->length);
  806. return last;
  807. }
  808. static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
  809. {
  810. int ret;
  811. QemuOpts *machine_opts;
  812. /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
  813. machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
  814. if (machine_opts &&
  815. !qemu_opt_get_bool(machine_opts, "dump-guest-core", true)) {
  816. ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
  817. if (ret) {
  818. perror("qemu_madvise");
  819. fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
  820. "but dump_guest_core=off specified\n");
  821. }
  822. }
  823. }
  824. void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
  825. {
  826. RAMBlock *new_block, *block;
  827. new_block = NULL;
  828. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  829. if (block->offset == addr) {
  830. new_block = block;
  831. break;
  832. }
  833. }
  834. assert(new_block);
  835. assert(!new_block->idstr[0]);
  836. if (dev) {
  837. char *id = qdev_get_dev_path(dev);
  838. if (id) {
  839. snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
  840. g_free(id);
  841. }
  842. }
  843. pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
  844. /* This assumes the iothread lock is taken here too. */
  845. qemu_mutex_lock_ramlist();
  846. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  847. if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
  848. fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
  849. new_block->idstr);
  850. abort();
  851. }
  852. }
  853. qemu_mutex_unlock_ramlist();
  854. }
  855. static int memory_try_enable_merging(void *addr, size_t len)
  856. {
  857. QemuOpts *opts;
  858. opts = qemu_opts_find(qemu_find_opts("machine"), 0);
  859. if (opts && !qemu_opt_get_bool(opts, "mem-merge", true)) {
  860. /* disabled by the user */
  861. return 0;
  862. }
  863. return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
  864. }
  865. ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
  866. MemoryRegion *mr)
  867. {
  868. RAMBlock *block, *new_block;
  869. size = TARGET_PAGE_ALIGN(size);
  870. new_block = g_malloc0(sizeof(*new_block));
  871. /* This assumes the iothread lock is taken here too. */
  872. qemu_mutex_lock_ramlist();
  873. new_block->mr = mr;
  874. new_block->offset = find_ram_offset(size);
  875. if (host) {
  876. new_block->host = host;
  877. new_block->flags |= RAM_PREALLOC_MASK;
  878. } else {
  879. if (mem_path) {
  880. #if defined (__linux__) && !defined(TARGET_S390X)
  881. new_block->host = file_ram_alloc(new_block, size, mem_path);
  882. if (!new_block->host) {
  883. new_block->host = qemu_vmalloc(size);
  884. memory_try_enable_merging(new_block->host, size);
  885. }
  886. #else
  887. fprintf(stderr, "-mem-path option unsupported\n");
  888. exit(1);
  889. #endif
  890. } else {
  891. if (xen_enabled()) {
  892. xen_ram_alloc(new_block->offset, size, mr);
  893. } else if (kvm_enabled()) {
  894. /* some s390/kvm configurations have special constraints */
  895. new_block->host = kvm_vmalloc(size);
  896. } else {
  897. new_block->host = qemu_vmalloc(size);
  898. }
  899. memory_try_enable_merging(new_block->host, size);
  900. }
  901. }
  902. new_block->length = size;
  903. /* Keep the list sorted from biggest to smallest block. */
  904. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  905. if (block->length < new_block->length) {
  906. break;
  907. }
  908. }
  909. if (block) {
  910. QTAILQ_INSERT_BEFORE(block, new_block, next);
  911. } else {
  912. QTAILQ_INSERT_TAIL(&ram_list.blocks, new_block, next);
  913. }
  914. ram_list.mru_block = NULL;
  915. ram_list.version++;
  916. qemu_mutex_unlock_ramlist();
  917. ram_list.phys_dirty = g_realloc(ram_list.phys_dirty,
  918. last_ram_offset() >> TARGET_PAGE_BITS);
  919. memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS),
  920. 0, size >> TARGET_PAGE_BITS);
  921. cpu_physical_memory_set_dirty_range(new_block->offset, size, 0xff);
  922. qemu_ram_setup_dump(new_block->host, size);
  923. qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
  924. if (kvm_enabled())
  925. kvm_setup_guest_memory(new_block->host, size);
  926. return new_block->offset;
  927. }
  928. ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr)
  929. {
  930. return qemu_ram_alloc_from_ptr(size, NULL, mr);
  931. }
  932. void qemu_ram_free_from_ptr(ram_addr_t addr)
  933. {
  934. RAMBlock *block;
  935. /* This assumes the iothread lock is taken here too. */
  936. qemu_mutex_lock_ramlist();
  937. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  938. if (addr == block->offset) {
  939. QTAILQ_REMOVE(&ram_list.blocks, block, next);
  940. ram_list.mru_block = NULL;
  941. ram_list.version++;
  942. g_free(block);
  943. break;
  944. }
  945. }
  946. qemu_mutex_unlock_ramlist();
  947. }
  948. void qemu_ram_free(ram_addr_t addr)
  949. {
  950. RAMBlock *block;
  951. /* This assumes the iothread lock is taken here too. */
  952. qemu_mutex_lock_ramlist();
  953. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  954. if (addr == block->offset) {
  955. QTAILQ_REMOVE(&ram_list.blocks, block, next);
  956. ram_list.mru_block = NULL;
  957. ram_list.version++;
  958. if (block->flags & RAM_PREALLOC_MASK) {
  959. ;
  960. } else if (mem_path) {
  961. #if defined (__linux__) && !defined(TARGET_S390X)
  962. if (block->fd) {
  963. munmap(block->host, block->length);
  964. close(block->fd);
  965. } else {
  966. qemu_vfree(block->host);
  967. }
  968. #else
  969. abort();
  970. #endif
  971. } else {
  972. #if defined(TARGET_S390X) && defined(CONFIG_KVM)
  973. munmap(block->host, block->length);
  974. #else
  975. if (xen_enabled()) {
  976. xen_invalidate_map_cache_entry(block->host);
  977. } else {
  978. qemu_vfree(block->host);
  979. }
  980. #endif
  981. }
  982. g_free(block);
  983. break;
  984. }
  985. }
  986. qemu_mutex_unlock_ramlist();
  987. }
  988. #ifndef _WIN32
  989. void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
  990. {
  991. RAMBlock *block;
  992. ram_addr_t offset;
  993. int flags;
  994. void *area, *vaddr;
  995. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  996. offset = addr - block->offset;
  997. if (offset < block->length) {
  998. vaddr = block->host + offset;
  999. if (block->flags & RAM_PREALLOC_MASK) {
  1000. ;
  1001. } else {
  1002. flags = MAP_FIXED;
  1003. munmap(vaddr, length);
  1004. if (mem_path) {
  1005. #if defined(__linux__) && !defined(TARGET_S390X)
  1006. if (block->fd) {
  1007. #ifdef MAP_POPULATE
  1008. flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED :
  1009. MAP_PRIVATE;
  1010. #else
  1011. flags |= MAP_PRIVATE;
  1012. #endif
  1013. area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
  1014. flags, block->fd, offset);
  1015. } else {
  1016. flags |= MAP_PRIVATE | MAP_ANONYMOUS;
  1017. area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
  1018. flags, -1, 0);
  1019. }
  1020. #else
  1021. abort();
  1022. #endif
  1023. } else {
  1024. #if defined(TARGET_S390X) && defined(CONFIG_KVM)
  1025. flags |= MAP_SHARED | MAP_ANONYMOUS;
  1026. area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE,
  1027. flags, -1, 0);
  1028. #else
  1029. flags |= MAP_PRIVATE | MAP_ANONYMOUS;
  1030. area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
  1031. flags, -1, 0);
  1032. #endif
  1033. }
  1034. if (area != vaddr) {
  1035. fprintf(stderr, "Could not remap addr: "
  1036. RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
  1037. length, addr);
  1038. exit(1);
  1039. }
  1040. memory_try_enable_merging(vaddr, length);
  1041. qemu_ram_setup_dump(vaddr, length);
  1042. }
  1043. return;
  1044. }
  1045. }
  1046. }
  1047. #endif /* !_WIN32 */
  1048. /* Return a host pointer to ram allocated with qemu_ram_alloc.
  1049. With the exception of the softmmu code in this file, this should
  1050. only be used for local memory (e.g. video ram) that the device owns,
  1051. and knows it isn't going to access beyond the end of the block.
  1052. It should not be used for general purpose DMA.
  1053. Use cpu_physical_memory_map/cpu_physical_memory_rw instead.
  1054. */
  1055. void *qemu_get_ram_ptr(ram_addr_t addr)
  1056. {
  1057. RAMBlock *block;
  1058. /* The list is protected by the iothread lock here. */
  1059. block = ram_list.mru_block;
  1060. if (block && addr - block->offset < block->length) {
  1061. goto found;
  1062. }
  1063. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  1064. if (addr - block->offset < block->length) {
  1065. goto found;
  1066. }
  1067. }
  1068. fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
  1069. abort();
  1070. found:
  1071. ram_list.mru_block = block;
  1072. if (xen_enabled()) {
  1073. /* We need to check if the requested address is in the RAM
  1074. * because we don't want to map the entire memory in QEMU.
  1075. * In that case just map until the end of the page.
  1076. */
  1077. if (block->offset == 0) {
  1078. return xen_map_cache(addr, 0, 0);
  1079. } else if (block->host == NULL) {
  1080. block->host =
  1081. xen_map_cache(block->offset, block->length, 1);
  1082. }
  1083. }
  1084. return block->host + (addr - block->offset);
  1085. }
  1086. /* Return a host pointer to ram allocated with qemu_ram_alloc. Same as
  1087. * qemu_get_ram_ptr but do not touch ram_list.mru_block.
  1088. *
  1089. * ??? Is this still necessary?
  1090. */
  1091. static void *qemu_safe_ram_ptr(ram_addr_t addr)
  1092. {
  1093. RAMBlock *block;
  1094. /* The list is protected by the iothread lock here. */
  1095. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  1096. if (addr - block->offset < block->length) {
  1097. if (xen_enabled()) {
  1098. /* We need to check if the requested address is in the RAM
  1099. * because we don't want to map the entire memory in QEMU.
  1100. * In that case just map until the end of the page.
  1101. */
  1102. if (block->offset == 0) {
  1103. return xen_map_cache(addr, 0, 0);
  1104. } else if (block->host == NULL) {
  1105. block->host =
  1106. xen_map_cache(block->offset, block->length, 1);
  1107. }
  1108. }
  1109. return block->host + (addr - block->offset);
  1110. }
  1111. }
  1112. fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
  1113. abort();
  1114. return NULL;
  1115. }
  1116. /* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
  1117. * but takes a size argument */
  1118. static void *qemu_ram_ptr_length(ram_addr_t addr, ram_addr_t *size)
  1119. {
  1120. if (*size == 0) {
  1121. return NULL;
  1122. }
  1123. if (xen_enabled()) {
  1124. return xen_map_cache(addr, *size, 1);
  1125. } else {
  1126. RAMBlock *block;
  1127. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  1128. if (addr - block->offset < block->length) {
  1129. if (addr - block->offset + *size > block->length)
  1130. *size = block->length - addr + block->offset;
  1131. return block->host + (addr - block->offset);
  1132. }
  1133. }
  1134. fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
  1135. abort();
  1136. }
  1137. }
  1138. void qemu_put_ram_ptr(void *addr)
  1139. {
  1140. trace_qemu_put_ram_ptr(addr);
  1141. }
  1142. int qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
  1143. {
  1144. RAMBlock *block;
  1145. uint8_t *host = ptr;
  1146. if (xen_enabled()) {
  1147. *ram_addr = xen_ram_addr_from_mapcache(ptr);
  1148. return 0;
  1149. }
  1150. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  1151. /* This case append when the block is not mapped. */
  1152. if (block->host == NULL) {
  1153. continue;
  1154. }
  1155. if (host - block->host < block->length) {
  1156. *ram_addr = block->offset + (host - block->host);
  1157. return 0;
  1158. }
  1159. }
  1160. return -1;
  1161. }
  1162. /* Some of the softmmu routines need to translate from a host pointer
  1163. (typically a TLB entry) back to a ram offset. */
  1164. ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
  1165. {
  1166. ram_addr_t ram_addr;
  1167. if (qemu_ram_addr_from_host(ptr, &ram_addr)) {
  1168. fprintf(stderr, "Bad ram pointer %p\n", ptr);
  1169. abort();
  1170. }
  1171. return ram_addr;
  1172. }
  1173. static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
  1174. unsigned size)
  1175. {
  1176. #ifdef DEBUG_UNASSIGNED
  1177. printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
  1178. #endif
  1179. #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
  1180. cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, size);
  1181. #endif
  1182. return 0;
  1183. }
  1184. static void unassigned_mem_write(void *opaque, hwaddr addr,
  1185. uint64_t val, unsigned size)
  1186. {
  1187. #ifdef DEBUG_UNASSIGNED
  1188. printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
  1189. #endif
  1190. #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
  1191. cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, size);
  1192. #endif
  1193. }
  1194. static const MemoryRegionOps unassigned_mem_ops = {
  1195. .read = unassigned_mem_read,
  1196. .write = unassigned_mem_write,
  1197. .endianness = DEVICE_NATIVE_ENDIAN,
  1198. };
  1199. static uint64_t error_mem_read(void *opaque, hwaddr addr,
  1200. unsigned size)
  1201. {
  1202. abort();
  1203. }
  1204. static void error_mem_write(void *opaque, hwaddr addr,
  1205. uint64_t value, unsigned size)
  1206. {
  1207. abort();
  1208. }
  1209. static const MemoryRegionOps error_mem_ops = {
  1210. .read = error_mem_read,
  1211. .write = error_mem_write,
  1212. .endianness = DEVICE_NATIVE_ENDIAN,
  1213. };
  1214. static const MemoryRegionOps rom_mem_ops = {
  1215. .read = error_mem_read,
  1216. .write = unassigned_mem_write,
  1217. .endianness = DEVICE_NATIVE_ENDIAN,
  1218. };
  1219. static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
  1220. uint64_t val, unsigned size)
  1221. {
  1222. int dirty_flags;
  1223. dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
  1224. if (!(dirty_flags & CODE_DIRTY_FLAG)) {
  1225. #if !defined(CONFIG_USER_ONLY)
  1226. tb_invalidate_phys_page_fast(ram_addr, size);
  1227. dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr);
  1228. #endif
  1229. }
  1230. switch (size) {
  1231. case 1:
  1232. stb_p(qemu_get_ram_ptr(ram_addr), val);
  1233. break;
  1234. case 2:
  1235. stw_p(qemu_get_ram_ptr(ram_addr), val);
  1236. break;
  1237. case 4:
  1238. stl_p(qemu_get_ram_ptr(ram_addr), val);
  1239. break;
  1240. default:
  1241. abort();
  1242. }
  1243. dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
  1244. cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags);
  1245. /* we remove the notdirty callback only if the code has been
  1246. flushed */
  1247. if (dirty_flags == 0xff)
  1248. tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
  1249. }
  1250. static const MemoryRegionOps notdirty_mem_ops = {
  1251. .read = error_mem_read,
  1252. .write = notdirty_mem_write,
  1253. .endianness = DEVICE_NATIVE_ENDIAN,
  1254. };
  1255. /* Generate a debug exception if a watchpoint has been hit. */
  1256. static void check_watchpoint(int offset, int len_mask, int flags)
  1257. {
  1258. CPUArchState *env = cpu_single_env;
  1259. target_ulong pc, cs_base;
  1260. target_ulong vaddr;
  1261. CPUWatchpoint *wp;
  1262. int cpu_flags;
  1263. if (env->watchpoint_hit) {
  1264. /* We re-entered the check after replacing the TB. Now raise
  1265. * the debug interrupt so that is will trigger after the
  1266. * current instruction. */
  1267. cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
  1268. return;
  1269. }
  1270. vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
  1271. QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
  1272. if ((vaddr == (wp->vaddr & len_mask) ||
  1273. (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
  1274. wp->flags |= BP_WATCHPOINT_HIT;
  1275. if (!env->watchpoint_hit) {
  1276. env->watchpoint_hit = wp;
  1277. tb_check_watchpoint(env);
  1278. if (wp->flags & BP_STOP_BEFORE_ACCESS) {
  1279. env->exception_index = EXCP_DEBUG;
  1280. cpu_loop_exit(env);
  1281. } else {
  1282. cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
  1283. tb_gen_code(env, pc, cs_base, cpu_flags, 1);
  1284. cpu_resume_from_signal(env, NULL);
  1285. }
  1286. }
  1287. } else {
  1288. wp->flags &= ~BP_WATCHPOINT_HIT;
  1289. }
  1290. }
  1291. }
  1292. /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
  1293. so these check for a hit then pass through to the normal out-of-line
  1294. phys routines. */
  1295. static uint64_t watch_mem_read(void *opaque, hwaddr addr,
  1296. unsigned size)
  1297. {
  1298. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_READ);
  1299. switch (size) {
  1300. case 1: return ldub_phys(addr);
  1301. case 2: return lduw_phys(addr);
  1302. case 4: return ldl_phys(addr);
  1303. default: abort();
  1304. }
  1305. }
  1306. static void watch_mem_write(void *opaque, hwaddr addr,
  1307. uint64_t val, unsigned size)
  1308. {
  1309. check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_WRITE);
  1310. switch (size) {
  1311. case 1:
  1312. stb_phys(addr, val);
  1313. break;
  1314. case 2:
  1315. stw_phys(addr, val);
  1316. break;
  1317. case 4:
  1318. stl_phys(addr, val);
  1319. break;
  1320. default: abort();
  1321. }
  1322. }
  1323. static const MemoryRegionOps watch_mem_ops = {
  1324. .read = watch_mem_read,
  1325. .write = watch_mem_write,
  1326. .endianness = DEVICE_NATIVE_ENDIAN,
  1327. };
  1328. static uint64_t subpage_read(void *opaque, hwaddr addr,
  1329. unsigned len)
  1330. {
  1331. subpage_t *mmio = opaque;
  1332. unsigned int idx = SUBPAGE_IDX(addr);
  1333. MemoryRegionSection *section;
  1334. #if defined(DEBUG_SUBPAGE)
  1335. printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
  1336. mmio, len, addr, idx);
  1337. #endif
  1338. section = &phys_sections[mmio->sub_section[idx]];
  1339. addr += mmio->base;
  1340. addr -= section->offset_within_address_space;
  1341. addr += section->offset_within_region;
  1342. return io_mem_read(section->mr, addr, len);
  1343. }
  1344. static void subpage_write(void *opaque, hwaddr addr,
  1345. uint64_t value, unsigned len)
  1346. {
  1347. subpage_t *mmio = opaque;
  1348. unsigned int idx = SUBPAGE_IDX(addr);
  1349. MemoryRegionSection *section;
  1350. #if defined(DEBUG_SUBPAGE)
  1351. printf("%s: subpage %p len %d addr " TARGET_FMT_plx
  1352. " idx %d value %"PRIx64"\n",
  1353. __func__, mmio, len, addr, idx, value);
  1354. #endif
  1355. section = &phys_sections[mmio->sub_section[idx]];
  1356. addr += mmio->base;
  1357. addr -= section->offset_within_address_space;
  1358. addr += section->offset_within_region;
  1359. io_mem_write(section->mr, addr, value, len);
  1360. }
  1361. static const MemoryRegionOps subpage_ops = {
  1362. .read = subpage_read,
  1363. .write = subpage_write,
  1364. .endianness = DEVICE_NATIVE_ENDIAN,
  1365. };
  1366. static uint64_t subpage_ram_read(void *opaque, hwaddr addr,
  1367. unsigned size)
  1368. {
  1369. ram_addr_t raddr = addr;
  1370. void *ptr = qemu_get_ram_ptr(raddr);
  1371. switch (size) {
  1372. case 1: return ldub_p(ptr);
  1373. case 2: return lduw_p(ptr);
  1374. case 4: return ldl_p(ptr);
  1375. default: abort();
  1376. }
  1377. }
  1378. static void subpage_ram_write(void *opaque, hwaddr addr,
  1379. uint64_t value, unsigned size)
  1380. {
  1381. ram_addr_t raddr = addr;
  1382. void *ptr = qemu_get_ram_ptr(raddr);
  1383. switch (size) {
  1384. case 1: return stb_p(ptr, value);
  1385. case 2: return stw_p(ptr, value);
  1386. case 4: return stl_p(ptr, value);
  1387. default: abort();
  1388. }
  1389. }
  1390. static const MemoryRegionOps subpage_ram_ops = {
  1391. .read = subpage_ram_read,
  1392. .write = subpage_ram_write,
  1393. .endianness = DEVICE_NATIVE_ENDIAN,
  1394. };
  1395. static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
  1396. uint16_t section)
  1397. {
  1398. int idx, eidx;
  1399. if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
  1400. return -1;
  1401. idx = SUBPAGE_IDX(start);
  1402. eidx = SUBPAGE_IDX(end);
  1403. #if defined(DEBUG_SUBPAGE)
  1404. printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
  1405. mmio, start, end, idx, eidx, memory);
  1406. #endif
  1407. if (memory_region_is_ram(phys_sections[section].mr)) {
  1408. MemoryRegionSection new_section = phys_sections[section];
  1409. new_section.mr = &io_mem_subpage_ram;
  1410. section = phys_section_add(&new_section);
  1411. }
  1412. for (; idx <= eidx; idx++) {
  1413. mmio->sub_section[idx] = section;
  1414. }
  1415. return 0;
  1416. }
  1417. static subpage_t *subpage_init(hwaddr base)
  1418. {
  1419. subpage_t *mmio;
  1420. mmio = g_malloc0(sizeof(subpage_t));
  1421. mmio->base = base;
  1422. memory_region_init_io(&mmio->iomem, &subpage_ops, mmio,
  1423. "subpage", TARGET_PAGE_SIZE);
  1424. mmio->iomem.subpage = true;
  1425. #if defined(DEBUG_SUBPAGE)
  1426. printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__,
  1427. mmio, base, TARGET_PAGE_SIZE, subpage_memory);
  1428. #endif
  1429. subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, phys_section_unassigned);
  1430. return mmio;
  1431. }
  1432. static uint16_t dummy_section(MemoryRegion *mr)
  1433. {
  1434. MemoryRegionSection section = {
  1435. .mr = mr,
  1436. .offset_within_address_space = 0,
  1437. .offset_within_region = 0,
  1438. .size = UINT64_MAX,
  1439. };
  1440. return phys_section_add(&section);
  1441. }
  1442. MemoryRegion *iotlb_to_region(hwaddr index)
  1443. {
  1444. return phys_sections[index & ~TARGET_PAGE_MASK].mr;
  1445. }
  1446. static void io_mem_init(void)
  1447. {
  1448. memory_region_init_io(&io_mem_ram, &error_mem_ops, NULL, "ram", UINT64_MAX);
  1449. memory_region_init_io(&io_mem_rom, &rom_mem_ops, NULL, "rom", UINT64_MAX);
  1450. memory_region_init_io(&io_mem_unassigned, &unassigned_mem_ops, NULL,
  1451. "unassigned", UINT64_MAX);
  1452. memory_region_init_io(&io_mem_notdirty, &notdirty_mem_ops, NULL,
  1453. "notdirty", UINT64_MAX);
  1454. memory_region_init_io(&io_mem_subpage_ram, &subpage_ram_ops, NULL,
  1455. "subpage-ram", UINT64_MAX);
  1456. memory_region_init_io(&io_mem_watch, &watch_mem_ops, NULL,
  1457. "watch", UINT64_MAX);
  1458. }
  1459. static void mem_begin(MemoryListener *listener)
  1460. {
  1461. AddressSpaceDispatch *d = container_of(listener, AddressSpaceDispatch, listener);
  1462. destroy_all_mappings(d);
  1463. d->phys_map.ptr = PHYS_MAP_NODE_NIL;
  1464. }
  1465. static void core_begin(MemoryListener *listener)
  1466. {
  1467. phys_sections_clear();
  1468. phys_section_unassigned = dummy_section(&io_mem_unassigned);
  1469. phys_section_notdirty = dummy_section(&io_mem_notdirty);
  1470. phys_section_rom = dummy_section(&io_mem_rom);
  1471. phys_section_watch = dummy_section(&io_mem_watch);
  1472. }
  1473. static void tcg_commit(MemoryListener *listener)
  1474. {
  1475. CPUArchState *env;
  1476. /* since each CPU stores ram addresses in its TLB cache, we must
  1477. reset the modified entries */
  1478. /* XXX: slow ! */
  1479. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  1480. tlb_flush(env, 1);
  1481. }
  1482. }
  1483. static void core_log_global_start(MemoryListener *listener)
  1484. {
  1485. cpu_physical_memory_set_dirty_tracking(1);
  1486. }
  1487. static void core_log_global_stop(MemoryListener *listener)
  1488. {
  1489. cpu_physical_memory_set_dirty_tracking(0);
  1490. }
  1491. static void io_region_add(MemoryListener *listener,
  1492. MemoryRegionSection *section)
  1493. {
  1494. MemoryRegionIORange *mrio = g_new(MemoryRegionIORange, 1);
  1495. mrio->mr = section->mr;
  1496. mrio->offset = section->offset_within_region;
  1497. iorange_init(&mrio->iorange, &memory_region_iorange_ops,
  1498. section->offset_within_address_space, section->size);
  1499. ioport_register(&mrio->iorange);
  1500. }
  1501. static void io_region_del(MemoryListener *listener,
  1502. MemoryRegionSection *section)
  1503. {
  1504. isa_unassign_ioport(section->offset_within_address_space, section->size);
  1505. }
  1506. static MemoryListener core_memory_listener = {
  1507. .begin = core_begin,
  1508. .log_global_start = core_log_global_start,
  1509. .log_global_stop = core_log_global_stop,
  1510. .priority = 1,
  1511. };
  1512. static MemoryListener io_memory_listener = {
  1513. .region_add = io_region_add,
  1514. .region_del = io_region_del,
  1515. .priority = 0,
  1516. };
  1517. static MemoryListener tcg_memory_listener = {
  1518. .commit = tcg_commit,
  1519. };
  1520. void address_space_init_dispatch(AddressSpace *as)
  1521. {
  1522. AddressSpaceDispatch *d = g_new(AddressSpaceDispatch, 1);
  1523. d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .is_leaf = 0 };
  1524. d->listener = (MemoryListener) {
  1525. .begin = mem_begin,
  1526. .region_add = mem_add,
  1527. .region_nop = mem_add,
  1528. .priority = 0,
  1529. };
  1530. as->dispatch = d;
  1531. memory_listener_register(&d->listener, as);
  1532. }
  1533. void address_space_destroy_dispatch(AddressSpace *as)
  1534. {
  1535. AddressSpaceDispatch *d = as->dispatch;
  1536. memory_listener_unregister(&d->listener);
  1537. destroy_l2_mapping(&d->phys_map, P_L2_LEVELS - 1);
  1538. g_free(d);
  1539. as->dispatch = NULL;
  1540. }
  1541. static void memory_map_init(void)
  1542. {
  1543. system_memory = g_malloc(sizeof(*system_memory));
  1544. memory_region_init(system_memory, "system", INT64_MAX);
  1545. address_space_init(&address_space_memory, system_memory);
  1546. address_space_memory.name = "memory";
  1547. system_io = g_malloc(sizeof(*system_io));
  1548. memory_region_init(system_io, "io", 65536);
  1549. address_space_init(&address_space_io, system_io);
  1550. address_space_io.name = "I/O";
  1551. memory_listener_register(&core_memory_listener, &address_space_memory);
  1552. memory_listener_register(&io_memory_listener, &address_space_io);
  1553. memory_listener_register(&tcg_memory_listener, &address_space_memory);
  1554. dma_context_init(&dma_context_memory, &address_space_memory,
  1555. NULL, NULL, NULL);
  1556. }
  1557. MemoryRegion *get_system_memory(void)
  1558. {
  1559. return system_memory;
  1560. }
  1561. MemoryRegion *get_system_io(void)
  1562. {
  1563. return system_io;
  1564. }
  1565. #endif /* !defined(CONFIG_USER_ONLY) */
  1566. /* physical memory access (slow version, mainly for debug) */
  1567. #if defined(CONFIG_USER_ONLY)
  1568. int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
  1569. uint8_t *buf, int len, int is_write)
  1570. {
  1571. int l, flags;
  1572. target_ulong page;
  1573. void * p;
  1574. while (len > 0) {
  1575. page = addr & TARGET_PAGE_MASK;
  1576. l = (page + TARGET_PAGE_SIZE) - addr;
  1577. if (l > len)
  1578. l = len;
  1579. flags = page_get_flags(page);
  1580. if (!(flags & PAGE_VALID))
  1581. return -1;
  1582. if (is_write) {
  1583. if (!(flags & PAGE_WRITE))
  1584. return -1;
  1585. /* XXX: this code should not depend on lock_user */
  1586. if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
  1587. return -1;
  1588. memcpy(p, buf, l);
  1589. unlock_user(p, addr, l);
  1590. } else {
  1591. if (!(flags & PAGE_READ))
  1592. return -1;
  1593. /* XXX: this code should not depend on lock_user */
  1594. if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
  1595. return -1;
  1596. memcpy(buf, p, l);
  1597. unlock_user(p, addr, 0);
  1598. }
  1599. len -= l;
  1600. buf += l;
  1601. addr += l;
  1602. }
  1603. return 0;
  1604. }
  1605. #else
  1606. static void invalidate_and_set_dirty(hwaddr addr,
  1607. hwaddr length)
  1608. {
  1609. if (!cpu_physical_memory_is_dirty(addr)) {
  1610. /* invalidate code */
  1611. tb_invalidate_phys_page_range(addr, addr + length, 0);
  1612. /* set dirty bit */
  1613. cpu_physical_memory_set_dirty_flags(addr, (0xff & ~CODE_DIRTY_FLAG));
  1614. }
  1615. xen_modified_memory(addr, length);
  1616. }
  1617. void address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
  1618. int len, bool is_write)
  1619. {
  1620. AddressSpaceDispatch *d = as->dispatch;
  1621. int l;
  1622. uint8_t *ptr;
  1623. uint32_t val;
  1624. hwaddr page;
  1625. MemoryRegionSection *section;
  1626. while (len > 0) {
  1627. page = addr & TARGET_PAGE_MASK;
  1628. l = (page + TARGET_PAGE_SIZE) - addr;
  1629. if (l > len)
  1630. l = len;
  1631. section = phys_page_find(d, page >> TARGET_PAGE_BITS);
  1632. if (is_write) {
  1633. if (!memory_region_is_ram(section->mr)) {
  1634. hwaddr addr1;
  1635. addr1 = memory_region_section_addr(section, addr);
  1636. /* XXX: could force cpu_single_env to NULL to avoid
  1637. potential bugs */
  1638. if (l >= 4 && ((addr1 & 3) == 0)) {
  1639. /* 32 bit write access */
  1640. val = ldl_p(buf);
  1641. io_mem_write(section->mr, addr1, val, 4);
  1642. l = 4;
  1643. } else if (l >= 2 && ((addr1 & 1) == 0)) {
  1644. /* 16 bit write access */
  1645. val = lduw_p(buf);
  1646. io_mem_write(section->mr, addr1, val, 2);
  1647. l = 2;
  1648. } else {
  1649. /* 8 bit write access */
  1650. val = ldub_p(buf);
  1651. io_mem_write(section->mr, addr1, val, 1);
  1652. l = 1;
  1653. }
  1654. } else if (!section->readonly) {
  1655. ram_addr_t addr1;
  1656. addr1 = memory_region_get_ram_addr(section->mr)
  1657. + memory_region_section_addr(section, addr);
  1658. /* RAM case */
  1659. ptr = qemu_get_ram_ptr(addr1);
  1660. memcpy(ptr, buf, l);
  1661. invalidate_and_set_dirty(addr1, l);
  1662. qemu_put_ram_ptr(ptr);
  1663. }
  1664. } else {
  1665. if (!(memory_region_is_ram(section->mr) ||
  1666. memory_region_is_romd(section->mr))) {
  1667. hwaddr addr1;
  1668. /* I/O case */
  1669. addr1 = memory_region_section_addr(section, addr);
  1670. if (l >= 4 && ((addr1 & 3) == 0)) {
  1671. /* 32 bit read access */
  1672. val = io_mem_read(section->mr, addr1, 4);
  1673. stl_p(buf, val);
  1674. l = 4;
  1675. } else if (l >= 2 && ((addr1 & 1) == 0)) {
  1676. /* 16 bit read access */
  1677. val = io_mem_read(section->mr, addr1, 2);
  1678. stw_p(buf, val);
  1679. l = 2;
  1680. } else {
  1681. /* 8 bit read access */
  1682. val = io_mem_read(section->mr, addr1, 1);
  1683. stb_p(buf, val);
  1684. l = 1;
  1685. }
  1686. } else {
  1687. /* RAM case */
  1688. ptr = qemu_get_ram_ptr(section->mr->ram_addr
  1689. + memory_region_section_addr(section,
  1690. addr));
  1691. memcpy(buf, ptr, l);
  1692. qemu_put_ram_ptr(ptr);
  1693. }
  1694. }
  1695. len -= l;
  1696. buf += l;
  1697. addr += l;
  1698. }
  1699. }
  1700. void address_space_write(AddressSpace *as, hwaddr addr,
  1701. const uint8_t *buf, int len)
  1702. {
  1703. address_space_rw(as, addr, (uint8_t *)buf, len, true);
  1704. }
  1705. /**
  1706. * address_space_read: read from an address space.
  1707. *
  1708. * @as: #AddressSpace to be accessed
  1709. * @addr: address within that address space
  1710. * @buf: buffer with the data transferred
  1711. */
  1712. void address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
  1713. {
  1714. address_space_rw(as, addr, buf, len, false);
  1715. }
  1716. void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
  1717. int len, int is_write)
  1718. {
  1719. return address_space_rw(&address_space_memory, addr, buf, len, is_write);
  1720. }
  1721. /* used for ROM loading : can write in RAM and ROM */
  1722. void cpu_physical_memory_write_rom(hwaddr addr,
  1723. const uint8_t *buf, int len)
  1724. {
  1725. AddressSpaceDispatch *d = address_space_memory.dispatch;
  1726. int l;
  1727. uint8_t *ptr;
  1728. hwaddr page;
  1729. MemoryRegionSection *section;
  1730. while (len > 0) {
  1731. page = addr & TARGET_PAGE_MASK;
  1732. l = (page + TARGET_PAGE_SIZE) - addr;
  1733. if (l > len)
  1734. l = len;
  1735. section = phys_page_find(d, page >> TARGET_PAGE_BITS);
  1736. if (!(memory_region_is_ram(section->mr) ||
  1737. memory_region_is_romd(section->mr))) {
  1738. /* do nothing */
  1739. } else {
  1740. unsigned long addr1;
  1741. addr1 = memory_region_get_ram_addr(section->mr)
  1742. + memory_region_section_addr(section, addr);
  1743. /* ROM/RAM case */
  1744. ptr = qemu_get_ram_ptr(addr1);
  1745. memcpy(ptr, buf, l);
  1746. invalidate_and_set_dirty(addr1, l);
  1747. qemu_put_ram_ptr(ptr);
  1748. }
  1749. len -= l;
  1750. buf += l;
  1751. addr += l;
  1752. }
  1753. }
  1754. typedef struct {
  1755. void *buffer;
  1756. hwaddr addr;
  1757. hwaddr len;
  1758. } BounceBuffer;
  1759. static BounceBuffer bounce;
  1760. typedef struct MapClient {
  1761. void *opaque;
  1762. void (*callback)(void *opaque);
  1763. QLIST_ENTRY(MapClient) link;
  1764. } MapClient;
  1765. static QLIST_HEAD(map_client_list, MapClient) map_client_list
  1766. = QLIST_HEAD_INITIALIZER(map_client_list);
  1767. void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
  1768. {
  1769. MapClient *client = g_malloc(sizeof(*client));
  1770. client->opaque = opaque;
  1771. client->callback = callback;
  1772. QLIST_INSERT_HEAD(&map_client_list, client, link);
  1773. return client;
  1774. }
  1775. static void cpu_unregister_map_client(void *_client)
  1776. {
  1777. MapClient *client = (MapClient *)_client;
  1778. QLIST_REMOVE(client, link);
  1779. g_free(client);
  1780. }
  1781. static void cpu_notify_map_clients(void)
  1782. {
  1783. MapClient *client;
  1784. while (!QLIST_EMPTY(&map_client_list)) {
  1785. client = QLIST_FIRST(&map_client_list);
  1786. client->callback(client->opaque);
  1787. cpu_unregister_map_client(client);
  1788. }
  1789. }
  1790. /* Map a physical memory region into a host virtual address.
  1791. * May map a subset of the requested range, given by and returned in *plen.
  1792. * May return NULL if resources needed to perform the mapping are exhausted.
  1793. * Use only for reads OR writes - not for read-modify-write operations.
  1794. * Use cpu_register_map_client() to know when retrying the map operation is
  1795. * likely to succeed.
  1796. */
  1797. void *address_space_map(AddressSpace *as,
  1798. hwaddr addr,
  1799. hwaddr *plen,
  1800. bool is_write)
  1801. {
  1802. AddressSpaceDispatch *d = as->dispatch;
  1803. hwaddr len = *plen;
  1804. hwaddr todo = 0;
  1805. int l;
  1806. hwaddr page;
  1807. MemoryRegionSection *section;
  1808. ram_addr_t raddr = RAM_ADDR_MAX;
  1809. ram_addr_t rlen;
  1810. void *ret;
  1811. while (len > 0) {
  1812. page = addr & TARGET_PAGE_MASK;
  1813. l = (page + TARGET_PAGE_SIZE) - addr;
  1814. if (l > len)
  1815. l = len;
  1816. section = phys_page_find(d, page >> TARGET_PAGE_BITS);
  1817. if (!(memory_region_is_ram(section->mr) && !section->readonly)) {
  1818. if (todo || bounce.buffer) {
  1819. break;
  1820. }
  1821. bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE);
  1822. bounce.addr = addr;
  1823. bounce.len = l;
  1824. if (!is_write) {
  1825. address_space_read(as, addr, bounce.buffer, l);
  1826. }
  1827. *plen = l;
  1828. return bounce.buffer;
  1829. }
  1830. if (!todo) {
  1831. raddr = memory_region_get_ram_addr(section->mr)
  1832. + memory_region_section_addr(section, addr);
  1833. }
  1834. len -= l;
  1835. addr += l;
  1836. todo += l;
  1837. }
  1838. rlen = todo;
  1839. ret = qemu_ram_ptr_length(raddr, &rlen);
  1840. *plen = rlen;
  1841. return ret;
  1842. }
  1843. /* Unmaps a memory region previously mapped by address_space_map().
  1844. * Will also mark the memory as dirty if is_write == 1. access_len gives
  1845. * the amount of memory that was actually read or written by the caller.
  1846. */
  1847. void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
  1848. int is_write, hwaddr access_len)
  1849. {
  1850. if (buffer != bounce.buffer) {
  1851. if (is_write) {
  1852. ram_addr_t addr1 = qemu_ram_addr_from_host_nofail(buffer);
  1853. while (access_len) {
  1854. unsigned l;
  1855. l = TARGET_PAGE_SIZE;
  1856. if (l > access_len)
  1857. l = access_len;
  1858. invalidate_and_set_dirty(addr1, l);
  1859. addr1 += l;
  1860. access_len -= l;
  1861. }
  1862. }
  1863. if (xen_enabled()) {
  1864. xen_invalidate_map_cache_entry(buffer);
  1865. }
  1866. return;
  1867. }
  1868. if (is_write) {
  1869. address_space_write(as, bounce.addr, bounce.buffer, access_len);
  1870. }
  1871. qemu_vfree(bounce.buffer);
  1872. bounce.buffer = NULL;
  1873. cpu_notify_map_clients();
  1874. }
  1875. void *cpu_physical_memory_map(hwaddr addr,
  1876. hwaddr *plen,
  1877. int is_write)
  1878. {
  1879. return address_space_map(&address_space_memory, addr, plen, is_write);
  1880. }
  1881. void cpu_physical_memory_unmap(void *buffer, hwaddr len,
  1882. int is_write, hwaddr access_len)
  1883. {
  1884. return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
  1885. }
  1886. /* warning: addr must be aligned */
  1887. static inline uint32_t ldl_phys_internal(hwaddr addr,
  1888. enum device_endian endian)
  1889. {
  1890. uint8_t *ptr;
  1891. uint32_t val;
  1892. MemoryRegionSection *section;
  1893. section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
  1894. if (!(memory_region_is_ram(section->mr) ||
  1895. memory_region_is_romd(section->mr))) {
  1896. /* I/O case */
  1897. addr = memory_region_section_addr(section, addr);
  1898. val = io_mem_read(section->mr, addr, 4);
  1899. #if defined(TARGET_WORDS_BIGENDIAN)
  1900. if (endian == DEVICE_LITTLE_ENDIAN) {
  1901. val = bswap32(val);
  1902. }
  1903. #else
  1904. if (endian == DEVICE_BIG_ENDIAN) {
  1905. val = bswap32(val);
  1906. }
  1907. #endif
  1908. } else {
  1909. /* RAM case */
  1910. ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
  1911. & TARGET_PAGE_MASK)
  1912. + memory_region_section_addr(section, addr));
  1913. switch (endian) {
  1914. case DEVICE_LITTLE_ENDIAN:
  1915. val = ldl_le_p(ptr);
  1916. break;
  1917. case DEVICE_BIG_ENDIAN:
  1918. val = ldl_be_p(ptr);
  1919. break;
  1920. default:
  1921. val = ldl_p(ptr);
  1922. break;
  1923. }
  1924. }
  1925. return val;
  1926. }
  1927. uint32_t ldl_phys(hwaddr addr)
  1928. {
  1929. return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
  1930. }
  1931. uint32_t ldl_le_phys(hwaddr addr)
  1932. {
  1933. return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
  1934. }
  1935. uint32_t ldl_be_phys(hwaddr addr)
  1936. {
  1937. return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN);
  1938. }
  1939. /* warning: addr must be aligned */
  1940. static inline uint64_t ldq_phys_internal(hwaddr addr,
  1941. enum device_endian endian)
  1942. {
  1943. uint8_t *ptr;
  1944. uint64_t val;
  1945. MemoryRegionSection *section;
  1946. section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
  1947. if (!(memory_region_is_ram(section->mr) ||
  1948. memory_region_is_romd(section->mr))) {
  1949. /* I/O case */
  1950. addr = memory_region_section_addr(section, addr);
  1951. /* XXX This is broken when device endian != cpu endian.
  1952. Fix and add "endian" variable check */
  1953. #ifdef TARGET_WORDS_BIGENDIAN
  1954. val = io_mem_read(section->mr, addr, 4) << 32;
  1955. val |= io_mem_read(section->mr, addr + 4, 4);
  1956. #else
  1957. val = io_mem_read(section->mr, addr, 4);
  1958. val |= io_mem_read(section->mr, addr + 4, 4) << 32;
  1959. #endif
  1960. } else {
  1961. /* RAM case */
  1962. ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
  1963. & TARGET_PAGE_MASK)
  1964. + memory_region_section_addr(section, addr));
  1965. switch (endian) {
  1966. case DEVICE_LITTLE_ENDIAN:
  1967. val = ldq_le_p(ptr);
  1968. break;
  1969. case DEVICE_BIG_ENDIAN:
  1970. val = ldq_be_p(ptr);
  1971. break;
  1972. default:
  1973. val = ldq_p(ptr);
  1974. break;
  1975. }
  1976. }
  1977. return val;
  1978. }
  1979. uint64_t ldq_phys(hwaddr addr)
  1980. {
  1981. return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
  1982. }
  1983. uint64_t ldq_le_phys(hwaddr addr)
  1984. {
  1985. return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
  1986. }
  1987. uint64_t ldq_be_phys(hwaddr addr)
  1988. {
  1989. return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN);
  1990. }
  1991. /* XXX: optimize */
  1992. uint32_t ldub_phys(hwaddr addr)
  1993. {
  1994. uint8_t val;
  1995. cpu_physical_memory_read(addr, &val, 1);
  1996. return val;
  1997. }
  1998. /* warning: addr must be aligned */
  1999. static inline uint32_t lduw_phys_internal(hwaddr addr,
  2000. enum device_endian endian)
  2001. {
  2002. uint8_t *ptr;
  2003. uint64_t val;
  2004. MemoryRegionSection *section;
  2005. section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
  2006. if (!(memory_region_is_ram(section->mr) ||
  2007. memory_region_is_romd(section->mr))) {
  2008. /* I/O case */
  2009. addr = memory_region_section_addr(section, addr);
  2010. val = io_mem_read(section->mr, addr, 2);
  2011. #if defined(TARGET_WORDS_BIGENDIAN)
  2012. if (endian == DEVICE_LITTLE_ENDIAN) {
  2013. val = bswap16(val);
  2014. }
  2015. #else
  2016. if (endian == DEVICE_BIG_ENDIAN) {
  2017. val = bswap16(val);
  2018. }
  2019. #endif
  2020. } else {
  2021. /* RAM case */
  2022. ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
  2023. & TARGET_PAGE_MASK)
  2024. + memory_region_section_addr(section, addr));
  2025. switch (endian) {
  2026. case DEVICE_LITTLE_ENDIAN:
  2027. val = lduw_le_p(ptr);
  2028. break;
  2029. case DEVICE_BIG_ENDIAN:
  2030. val = lduw_be_p(ptr);
  2031. break;
  2032. default:
  2033. val = lduw_p(ptr);
  2034. break;
  2035. }
  2036. }
  2037. return val;
  2038. }
  2039. uint32_t lduw_phys(hwaddr addr)
  2040. {
  2041. return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN);
  2042. }
  2043. uint32_t lduw_le_phys(hwaddr addr)
  2044. {
  2045. return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN);
  2046. }
  2047. uint32_t lduw_be_phys(hwaddr addr)
  2048. {
  2049. return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN);
  2050. }
  2051. /* warning: addr must be aligned. The ram page is not masked as dirty
  2052. and the code inside is not invalidated. It is useful if the dirty
  2053. bits are used to track modified PTEs */
  2054. void stl_phys_notdirty(hwaddr addr, uint32_t val)
  2055. {
  2056. uint8_t *ptr;
  2057. MemoryRegionSection *section;
  2058. section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
  2059. if (!memory_region_is_ram(section->mr) || section->readonly) {
  2060. addr = memory_region_section_addr(section, addr);
  2061. if (memory_region_is_ram(section->mr)) {
  2062. section = &phys_sections[phys_section_rom];
  2063. }
  2064. io_mem_write(section->mr, addr, val, 4);
  2065. } else {
  2066. unsigned long addr1 = (memory_region_get_ram_addr(section->mr)
  2067. & TARGET_PAGE_MASK)
  2068. + memory_region_section_addr(section, addr);
  2069. ptr = qemu_get_ram_ptr(addr1);
  2070. stl_p(ptr, val);
  2071. if (unlikely(in_migration)) {
  2072. if (!cpu_physical_memory_is_dirty(addr1)) {
  2073. /* invalidate code */
  2074. tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
  2075. /* set dirty bit */
  2076. cpu_physical_memory_set_dirty_flags(
  2077. addr1, (0xff & ~CODE_DIRTY_FLAG));
  2078. }
  2079. }
  2080. }
  2081. }
  2082. void stq_phys_notdirty(hwaddr addr, uint64_t val)
  2083. {
  2084. uint8_t *ptr;
  2085. MemoryRegionSection *section;
  2086. section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
  2087. if (!memory_region_is_ram(section->mr) || section->readonly) {
  2088. addr = memory_region_section_addr(section, addr);
  2089. if (memory_region_is_ram(section->mr)) {
  2090. section = &phys_sections[phys_section_rom];
  2091. }
  2092. #ifdef TARGET_WORDS_BIGENDIAN
  2093. io_mem_write(section->mr, addr, val >> 32, 4);
  2094. io_mem_write(section->mr, addr + 4, (uint32_t)val, 4);
  2095. #else
  2096. io_mem_write(section->mr, addr, (uint32_t)val, 4);
  2097. io_mem_write(section->mr, addr + 4, val >> 32, 4);
  2098. #endif
  2099. } else {
  2100. ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(section->mr)
  2101. & TARGET_PAGE_MASK)
  2102. + memory_region_section_addr(section, addr));
  2103. stq_p(ptr, val);
  2104. }
  2105. }
  2106. /* warning: addr must be aligned */
  2107. static inline void stl_phys_internal(hwaddr addr, uint32_t val,
  2108. enum device_endian endian)
  2109. {
  2110. uint8_t *ptr;
  2111. MemoryRegionSection *section;
  2112. section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
  2113. if (!memory_region_is_ram(section->mr) || section->readonly) {
  2114. addr = memory_region_section_addr(section, addr);
  2115. if (memory_region_is_ram(section->mr)) {
  2116. section = &phys_sections[phys_section_rom];
  2117. }
  2118. #if defined(TARGET_WORDS_BIGENDIAN)
  2119. if (endian == DEVICE_LITTLE_ENDIAN) {
  2120. val = bswap32(val);
  2121. }
  2122. #else
  2123. if (endian == DEVICE_BIG_ENDIAN) {
  2124. val = bswap32(val);
  2125. }
  2126. #endif
  2127. io_mem_write(section->mr, addr, val, 4);
  2128. } else {
  2129. unsigned long addr1;
  2130. addr1 = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
  2131. + memory_region_section_addr(section, addr);
  2132. /* RAM case */
  2133. ptr = qemu_get_ram_ptr(addr1);
  2134. switch (endian) {
  2135. case DEVICE_LITTLE_ENDIAN:
  2136. stl_le_p(ptr, val);
  2137. break;
  2138. case DEVICE_BIG_ENDIAN:
  2139. stl_be_p(ptr, val);
  2140. break;
  2141. default:
  2142. stl_p(ptr, val);
  2143. break;
  2144. }
  2145. invalidate_and_set_dirty(addr1, 4);
  2146. }
  2147. }
  2148. void stl_phys(hwaddr addr, uint32_t val)
  2149. {
  2150. stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
  2151. }
  2152. void stl_le_phys(hwaddr addr, uint32_t val)
  2153. {
  2154. stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
  2155. }
  2156. void stl_be_phys(hwaddr addr, uint32_t val)
  2157. {
  2158. stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
  2159. }
  2160. /* XXX: optimize */
  2161. void stb_phys(hwaddr addr, uint32_t val)
  2162. {
  2163. uint8_t v = val;
  2164. cpu_physical_memory_write(addr, &v, 1);
  2165. }
  2166. /* warning: addr must be aligned */
  2167. static inline void stw_phys_internal(hwaddr addr, uint32_t val,
  2168. enum device_endian endian)
  2169. {
  2170. uint8_t *ptr;
  2171. MemoryRegionSection *section;
  2172. section = phys_page_find(address_space_memory.dispatch, addr >> TARGET_PAGE_BITS);
  2173. if (!memory_region_is_ram(section->mr) || section->readonly) {
  2174. addr = memory_region_section_addr(section, addr);
  2175. if (memory_region_is_ram(section->mr)) {
  2176. section = &phys_sections[phys_section_rom];
  2177. }
  2178. #if defined(TARGET_WORDS_BIGENDIAN)
  2179. if (endian == DEVICE_LITTLE_ENDIAN) {
  2180. val = bswap16(val);
  2181. }
  2182. #else
  2183. if (endian == DEVICE_BIG_ENDIAN) {
  2184. val = bswap16(val);
  2185. }
  2186. #endif
  2187. io_mem_write(section->mr, addr, val, 2);
  2188. } else {
  2189. unsigned long addr1;
  2190. addr1 = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
  2191. + memory_region_section_addr(section, addr);
  2192. /* RAM case */
  2193. ptr = qemu_get_ram_ptr(addr1);
  2194. switch (endian) {
  2195. case DEVICE_LITTLE_ENDIAN:
  2196. stw_le_p(ptr, val);
  2197. break;
  2198. case DEVICE_BIG_ENDIAN:
  2199. stw_be_p(ptr, val);
  2200. break;
  2201. default:
  2202. stw_p(ptr, val);
  2203. break;
  2204. }
  2205. invalidate_and_set_dirty(addr1, 2);
  2206. }
  2207. }
  2208. void stw_phys(hwaddr addr, uint32_t val)
  2209. {
  2210. stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN);
  2211. }
  2212. void stw_le_phys(hwaddr addr, uint32_t val)
  2213. {
  2214. stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN);
  2215. }
  2216. void stw_be_phys(hwaddr addr, uint32_t val)
  2217. {
  2218. stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN);
  2219. }
  2220. /* XXX: optimize */
  2221. void stq_phys(hwaddr addr, uint64_t val)
  2222. {
  2223. val = tswap64(val);
  2224. cpu_physical_memory_write(addr, &val, 8);
  2225. }
  2226. void stq_le_phys(hwaddr addr, uint64_t val)
  2227. {
  2228. val = cpu_to_le64(val);
  2229. cpu_physical_memory_write(addr, &val, 8);
  2230. }
  2231. void stq_be_phys(hwaddr addr, uint64_t val)
  2232. {
  2233. val = cpu_to_be64(val);
  2234. cpu_physical_memory_write(addr, &val, 8);
  2235. }
  2236. /* virtual memory access for debug (includes writing to ROM) */
  2237. int cpu_memory_rw_debug(CPUArchState *env, target_ulong addr,
  2238. uint8_t *buf, int len, int is_write)
  2239. {
  2240. int l;
  2241. hwaddr phys_addr;
  2242. target_ulong page;
  2243. while (len > 0) {
  2244. page = addr & TARGET_PAGE_MASK;
  2245. phys_addr = cpu_get_phys_page_debug(env, page);
  2246. /* if no physical page mapped, return an error */
  2247. if (phys_addr == -1)
  2248. return -1;
  2249. l = (page + TARGET_PAGE_SIZE) - addr;
  2250. if (l > len)
  2251. l = len;
  2252. phys_addr += (addr & ~TARGET_PAGE_MASK);
  2253. if (is_write)
  2254. cpu_physical_memory_write_rom(phys_addr, buf, l);
  2255. else
  2256. cpu_physical_memory_rw(phys_addr, buf, l, is_write);
  2257. len -= l;
  2258. buf += l;
  2259. addr += l;
  2260. }
  2261. return 0;
  2262. }
  2263. #endif
  2264. #if !defined(CONFIG_USER_ONLY)
  2265. /*
  2266. * A helper function for the _utterly broken_ virtio device model to find out if
  2267. * it's running on a big endian machine. Don't do this at home kids!
  2268. */
  2269. bool virtio_is_big_endian(void);
  2270. bool virtio_is_big_endian(void)
  2271. {
  2272. #if defined(TARGET_WORDS_BIGENDIAN)
  2273. return true;
  2274. #else
  2275. return false;
  2276. #endif
  2277. }
  2278. #endif
  2279. #ifndef CONFIG_USER_ONLY
  2280. bool cpu_physical_memory_is_io(hwaddr phys_addr)
  2281. {
  2282. MemoryRegionSection *section;
  2283. section = phys_page_find(address_space_memory.dispatch,
  2284. phys_addr >> TARGET_PAGE_BITS);
  2285. return !(memory_region_is_ram(section->mr) ||
  2286. memory_region_is_romd(section->mr));
  2287. }
  2288. #endif