memory.h 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  1. /*
  2. * Physical memory management API
  3. *
  4. * Copyright 2011 Red Hat, Inc. and/or its affiliates
  5. *
  6. * Authors:
  7. * Avi Kivity <avi@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. */
  13. #ifndef MEMORY_H
  14. #define MEMORY_H
  15. #ifndef CONFIG_USER_ONLY
  16. #include "exec/cpu-common.h"
  17. #include "exec/hwaddr.h"
  18. #include "exec/memattrs.h"
  19. #include "exec/ramlist.h"
  20. #include "qemu/queue.h"
  21. #include "qemu/int128.h"
  22. #include "qemu/notify.h"
  23. #include "qom/object.h"
  24. #include "qemu/rcu.h"
  25. #include "hw/qdev-core.h"
  26. #define RAM_ADDR_INVALID (~(ram_addr_t)0)
  27. #define MAX_PHYS_ADDR_SPACE_BITS 62
  28. #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
  29. #define TYPE_MEMORY_REGION "qemu:memory-region"
  30. #define MEMORY_REGION(obj) \
  31. OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
  32. #define TYPE_IOMMU_MEMORY_REGION "qemu:iommu-memory-region"
  33. #define IOMMU_MEMORY_REGION(obj) \
  34. OBJECT_CHECK(IOMMUMemoryRegion, (obj), TYPE_IOMMU_MEMORY_REGION)
  35. #define IOMMU_MEMORY_REGION_CLASS(klass) \
  36. OBJECT_CLASS_CHECK(IOMMUMemoryRegionClass, (klass), \
  37. TYPE_IOMMU_MEMORY_REGION)
  38. #define IOMMU_MEMORY_REGION_GET_CLASS(obj) \
  39. OBJECT_GET_CLASS(IOMMUMemoryRegionClass, (obj), \
  40. TYPE_IOMMU_MEMORY_REGION)
  41. typedef struct MemoryRegionOps MemoryRegionOps;
  42. typedef struct MemoryRegionMmio MemoryRegionMmio;
  43. struct MemoryRegionMmio {
  44. CPUReadMemoryFunc *read[3];
  45. CPUWriteMemoryFunc *write[3];
  46. };
  47. typedef struct IOMMUTLBEntry IOMMUTLBEntry;
  48. /* See address_space_translate: bit 0 is read, bit 1 is write. */
  49. typedef enum {
  50. IOMMU_NONE = 0,
  51. IOMMU_RO = 1,
  52. IOMMU_WO = 2,
  53. IOMMU_RW = 3,
  54. } IOMMUAccessFlags;
  55. #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
  56. struct IOMMUTLBEntry {
  57. AddressSpace *target_as;
  58. hwaddr iova;
  59. hwaddr translated_addr;
  60. hwaddr addr_mask; /* 0xfff = 4k translation */
  61. IOMMUAccessFlags perm;
  62. };
  63. /*
  64. * Bitmap for different IOMMUNotifier capabilities. Each notifier can
  65. * register with one or multiple IOMMU Notifier capability bit(s).
  66. */
  67. typedef enum {
  68. IOMMU_NOTIFIER_NONE = 0,
  69. /* Notify cache invalidations */
  70. IOMMU_NOTIFIER_UNMAP = 0x1,
  71. /* Notify entry changes (newly created entries) */
  72. IOMMU_NOTIFIER_MAP = 0x2,
  73. } IOMMUNotifierFlag;
  74. #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
  75. struct IOMMUNotifier;
  76. typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier,
  77. IOMMUTLBEntry *data);
  78. struct IOMMUNotifier {
  79. IOMMUNotify notify;
  80. IOMMUNotifierFlag notifier_flags;
  81. /* Notify for address space range start <= addr <= end */
  82. hwaddr start;
  83. hwaddr end;
  84. int iommu_idx;
  85. QLIST_ENTRY(IOMMUNotifier) node;
  86. };
  87. typedef struct IOMMUNotifier IOMMUNotifier;
  88. static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
  89. IOMMUNotifierFlag flags,
  90. hwaddr start, hwaddr end,
  91. int iommu_idx)
  92. {
  93. n->notify = fn;
  94. n->notifier_flags = flags;
  95. n->start = start;
  96. n->end = end;
  97. n->iommu_idx = iommu_idx;
  98. }
  99. /*
  100. * Memory region callbacks
  101. */
  102. struct MemoryRegionOps {
  103. /* Read from the memory region. @addr is relative to @mr; @size is
  104. * in bytes. */
  105. uint64_t (*read)(void *opaque,
  106. hwaddr addr,
  107. unsigned size);
  108. /* Write to the memory region. @addr is relative to @mr; @size is
  109. * in bytes. */
  110. void (*write)(void *opaque,
  111. hwaddr addr,
  112. uint64_t data,
  113. unsigned size);
  114. MemTxResult (*read_with_attrs)(void *opaque,
  115. hwaddr addr,
  116. uint64_t *data,
  117. unsigned size,
  118. MemTxAttrs attrs);
  119. MemTxResult (*write_with_attrs)(void *opaque,
  120. hwaddr addr,
  121. uint64_t data,
  122. unsigned size,
  123. MemTxAttrs attrs);
  124. /* Instruction execution pre-callback:
  125. * @addr is the address of the access relative to the @mr.
  126. * @size is the size of the area returned by the callback.
  127. * @offset is the location of the pointer inside @mr.
  128. *
  129. * Returns a pointer to a location which contains guest code.
  130. */
  131. void *(*request_ptr)(void *opaque, hwaddr addr, unsigned *size,
  132. unsigned *offset);
  133. enum device_endian endianness;
  134. /* Guest-visible constraints: */
  135. struct {
  136. /* If nonzero, specify bounds on access sizes beyond which a machine
  137. * check is thrown.
  138. */
  139. unsigned min_access_size;
  140. unsigned max_access_size;
  141. /* If true, unaligned accesses are supported. Otherwise unaligned
  142. * accesses throw machine checks.
  143. */
  144. bool unaligned;
  145. /*
  146. * If present, and returns #false, the transaction is not accepted
  147. * by the device (and results in machine dependent behaviour such
  148. * as a machine check exception).
  149. */
  150. bool (*accepts)(void *opaque, hwaddr addr,
  151. unsigned size, bool is_write,
  152. MemTxAttrs attrs);
  153. } valid;
  154. /* Internal implementation constraints: */
  155. struct {
  156. /* If nonzero, specifies the minimum size implemented. Smaller sizes
  157. * will be rounded upwards and a partial result will be returned.
  158. */
  159. unsigned min_access_size;
  160. /* If nonzero, specifies the maximum size implemented. Larger sizes
  161. * will be done as a series of accesses with smaller sizes.
  162. */
  163. unsigned max_access_size;
  164. /* If true, unaligned accesses are supported. Otherwise all accesses
  165. * are converted to (possibly multiple) naturally aligned accesses.
  166. */
  167. bool unaligned;
  168. } impl;
  169. /* If .read and .write are not present, old_mmio may be used for
  170. * backwards compatibility with old mmio registration
  171. */
  172. const MemoryRegionMmio old_mmio;
  173. };
  174. enum IOMMUMemoryRegionAttr {
  175. IOMMU_ATTR_SPAPR_TCE_FD
  176. };
  177. /**
  178. * IOMMUMemoryRegionClass:
  179. *
  180. * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
  181. * and provide an implementation of at least the @translate method here
  182. * to handle requests to the memory region. Other methods are optional.
  183. *
  184. * The IOMMU implementation must use the IOMMU notifier infrastructure
  185. * to report whenever mappings are changed, by calling
  186. * memory_region_notify_iommu() (or, if necessary, by calling
  187. * memory_region_notify_one() for each registered notifier).
  188. *
  189. * Conceptually an IOMMU provides a mapping from input address
  190. * to an output TLB entry. If the IOMMU is aware of memory transaction
  191. * attributes and the output TLB entry depends on the transaction
  192. * attributes, we represent this using IOMMU indexes. Each index
  193. * selects a particular translation table that the IOMMU has:
  194. * @attrs_to_index returns the IOMMU index for a set of transaction attributes
  195. * @translate takes an input address and an IOMMU index
  196. * and the mapping returned can only depend on the input address and the
  197. * IOMMU index.
  198. *
  199. * Most IOMMUs don't care about the transaction attributes and support
  200. * only a single IOMMU index. A more complex IOMMU might have one index
  201. * for secure transactions and one for non-secure transactions.
  202. */
  203. typedef struct IOMMUMemoryRegionClass {
  204. /* private */
  205. struct DeviceClass parent_class;
  206. /*
  207. * Return a TLB entry that contains a given address.
  208. *
  209. * The IOMMUAccessFlags indicated via @flag are optional and may
  210. * be specified as IOMMU_NONE to indicate that the caller needs
  211. * the full translation information for both reads and writes. If
  212. * the access flags are specified then the IOMMU implementation
  213. * may use this as an optimization, to stop doing a page table
  214. * walk as soon as it knows that the requested permissions are not
  215. * allowed. If IOMMU_NONE is passed then the IOMMU must do the
  216. * full page table walk and report the permissions in the returned
  217. * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
  218. * return different mappings for reads and writes.)
  219. *
  220. * The returned information remains valid while the caller is
  221. * holding the big QEMU lock or is inside an RCU critical section;
  222. * if the caller wishes to cache the mapping beyond that it must
  223. * register an IOMMU notifier so it can invalidate its cached
  224. * information when the IOMMU mapping changes.
  225. *
  226. * @iommu: the IOMMUMemoryRegion
  227. * @hwaddr: address to be translated within the memory region
  228. * @flag: requested access permissions
  229. * @iommu_idx: IOMMU index for the translation
  230. */
  231. IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr,
  232. IOMMUAccessFlags flag, int iommu_idx);
  233. /* Returns minimum supported page size in bytes.
  234. * If this method is not provided then the minimum is assumed to
  235. * be TARGET_PAGE_SIZE.
  236. *
  237. * @iommu: the IOMMUMemoryRegion
  238. */
  239. uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu);
  240. /* Called when IOMMU Notifier flag changes (ie when the set of
  241. * events which IOMMU users are requesting notification for changes).
  242. * Optional method -- need not be provided if the IOMMU does not
  243. * need to know exactly which events must be notified.
  244. *
  245. * @iommu: the IOMMUMemoryRegion
  246. * @old_flags: events which previously needed to be notified
  247. * @new_flags: events which now need to be notified
  248. */
  249. void (*notify_flag_changed)(IOMMUMemoryRegion *iommu,
  250. IOMMUNotifierFlag old_flags,
  251. IOMMUNotifierFlag new_flags);
  252. /* Called to handle memory_region_iommu_replay().
  253. *
  254. * The default implementation of memory_region_iommu_replay() is to
  255. * call the IOMMU translate method for every page in the address space
  256. * with flag == IOMMU_NONE and then call the notifier if translate
  257. * returns a valid mapping. If this method is implemented then it
  258. * overrides the default behaviour, and must provide the full semantics
  259. * of memory_region_iommu_replay(), by calling @notifier for every
  260. * translation present in the IOMMU.
  261. *
  262. * Optional method -- an IOMMU only needs to provide this method
  263. * if the default is inefficient or produces undesirable side effects.
  264. *
  265. * Note: this is not related to record-and-replay functionality.
  266. */
  267. void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier);
  268. /* Get IOMMU misc attributes. This is an optional method that
  269. * can be used to allow users of the IOMMU to get implementation-specific
  270. * information. The IOMMU implements this method to handle calls
  271. * by IOMMU users to memory_region_iommu_get_attr() by filling in
  272. * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
  273. * the IOMMU supports. If the method is unimplemented then
  274. * memory_region_iommu_get_attr() will always return -EINVAL.
  275. *
  276. * @iommu: the IOMMUMemoryRegion
  277. * @attr: attribute being queried
  278. * @data: memory to fill in with the attribute data
  279. *
  280. * Returns 0 on success, or a negative errno; in particular
  281. * returns -EINVAL for unrecognized or unimplemented attribute types.
  282. */
  283. int (*get_attr)(IOMMUMemoryRegion *iommu, enum IOMMUMemoryRegionAttr attr,
  284. void *data);
  285. /* Return the IOMMU index to use for a given set of transaction attributes.
  286. *
  287. * Optional method: if an IOMMU only supports a single IOMMU index then
  288. * the default implementation of memory_region_iommu_attrs_to_index()
  289. * will return 0.
  290. *
  291. * The indexes supported by an IOMMU must be contiguous, starting at 0.
  292. *
  293. * @iommu: the IOMMUMemoryRegion
  294. * @attrs: memory transaction attributes
  295. */
  296. int (*attrs_to_index)(IOMMUMemoryRegion *iommu, MemTxAttrs attrs);
  297. /* Return the number of IOMMU indexes this IOMMU supports.
  298. *
  299. * Optional method: if this method is not provided, then
  300. * memory_region_iommu_num_indexes() will return 1, indicating that
  301. * only a single IOMMU index is supported.
  302. *
  303. * @iommu: the IOMMUMemoryRegion
  304. */
  305. int (*num_indexes)(IOMMUMemoryRegion *iommu);
  306. } IOMMUMemoryRegionClass;
  307. typedef struct CoalescedMemoryRange CoalescedMemoryRange;
  308. typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
  309. struct MemoryRegion {
  310. Object parent_obj;
  311. /* All fields are private - violators will be prosecuted */
  312. /* The following fields should fit in a cache line */
  313. bool romd_mode;
  314. bool ram;
  315. bool subpage;
  316. bool readonly; /* For RAM regions */
  317. bool rom_device;
  318. bool flush_coalesced_mmio;
  319. bool global_locking;
  320. uint8_t dirty_log_mask;
  321. bool is_iommu;
  322. RAMBlock *ram_block;
  323. Object *owner;
  324. const MemoryRegionOps *ops;
  325. void *opaque;
  326. MemoryRegion *container;
  327. Int128 size;
  328. hwaddr addr;
  329. void (*destructor)(MemoryRegion *mr);
  330. uint64_t align;
  331. bool terminates;
  332. bool ram_device;
  333. bool enabled;
  334. bool warning_printed; /* For reservations */
  335. uint8_t vga_logging_count;
  336. MemoryRegion *alias;
  337. hwaddr alias_offset;
  338. int32_t priority;
  339. QTAILQ_HEAD(subregions, MemoryRegion) subregions;
  340. QTAILQ_ENTRY(MemoryRegion) subregions_link;
  341. QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
  342. const char *name;
  343. unsigned ioeventfd_nb;
  344. MemoryRegionIoeventfd *ioeventfds;
  345. };
  346. struct IOMMUMemoryRegion {
  347. MemoryRegion parent_obj;
  348. QLIST_HEAD(, IOMMUNotifier) iommu_notify;
  349. IOMMUNotifierFlag iommu_notify_flags;
  350. };
  351. #define IOMMU_NOTIFIER_FOREACH(n, mr) \
  352. QLIST_FOREACH((n), &(mr)->iommu_notify, node)
  353. /**
  354. * MemoryListener: callbacks structure for updates to the physical memory map
  355. *
  356. * Allows a component to adjust to changes in the guest-visible memory map.
  357. * Use with memory_listener_register() and memory_listener_unregister().
  358. */
  359. struct MemoryListener {
  360. void (*begin)(MemoryListener *listener);
  361. void (*commit)(MemoryListener *listener);
  362. void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
  363. void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
  364. void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
  365. void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
  366. int old, int new);
  367. void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
  368. int old, int new);
  369. void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
  370. void (*log_global_start)(MemoryListener *listener);
  371. void (*log_global_stop)(MemoryListener *listener);
  372. void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
  373. bool match_data, uint64_t data, EventNotifier *e);
  374. void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
  375. bool match_data, uint64_t data, EventNotifier *e);
  376. void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
  377. hwaddr addr, hwaddr len);
  378. void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
  379. hwaddr addr, hwaddr len);
  380. /* Lower = earlier (during add), later (during del) */
  381. unsigned priority;
  382. AddressSpace *address_space;
  383. QTAILQ_ENTRY(MemoryListener) link;
  384. QTAILQ_ENTRY(MemoryListener) link_as;
  385. };
  386. /**
  387. * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
  388. */
  389. struct AddressSpace {
  390. /* All fields are private. */
  391. struct rcu_head rcu;
  392. char *name;
  393. MemoryRegion *root;
  394. /* Accessed via RCU. */
  395. struct FlatView *current_map;
  396. int ioeventfd_nb;
  397. struct MemoryRegionIoeventfd *ioeventfds;
  398. QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners;
  399. QTAILQ_ENTRY(AddressSpace) address_spaces_link;
  400. };
  401. typedef struct AddressSpaceDispatch AddressSpaceDispatch;
  402. typedef struct FlatRange FlatRange;
  403. /* Flattened global view of current active memory hierarchy. Kept in sorted
  404. * order.
  405. */
  406. struct FlatView {
  407. struct rcu_head rcu;
  408. unsigned ref;
  409. FlatRange *ranges;
  410. unsigned nr;
  411. unsigned nr_allocated;
  412. struct AddressSpaceDispatch *dispatch;
  413. MemoryRegion *root;
  414. };
  415. static inline FlatView *address_space_to_flatview(AddressSpace *as)
  416. {
  417. return atomic_rcu_read(&as->current_map);
  418. }
  419. /**
  420. * MemoryRegionSection: describes a fragment of a #MemoryRegion
  421. *
  422. * @mr: the region, or %NULL if empty
  423. * @fv: the flat view of the address space the region is mapped in
  424. * @offset_within_region: the beginning of the section, relative to @mr's start
  425. * @size: the size of the section; will not exceed @mr's boundaries
  426. * @offset_within_address_space: the address of the first byte of the section
  427. * relative to the region's address space
  428. * @readonly: writes to this section are ignored
  429. */
  430. struct MemoryRegionSection {
  431. MemoryRegion *mr;
  432. FlatView *fv;
  433. hwaddr offset_within_region;
  434. Int128 size;
  435. hwaddr offset_within_address_space;
  436. bool readonly;
  437. };
  438. /**
  439. * memory_region_init: Initialize a memory region
  440. *
  441. * The region typically acts as a container for other memory regions. Use
  442. * memory_region_add_subregion() to add subregions.
  443. *
  444. * @mr: the #MemoryRegion to be initialized
  445. * @owner: the object that tracks the region's reference count
  446. * @name: used for debugging; not visible to the user or ABI
  447. * @size: size of the region; any subregions beyond this size will be clipped
  448. */
  449. void memory_region_init(MemoryRegion *mr,
  450. struct Object *owner,
  451. const char *name,
  452. uint64_t size);
  453. /**
  454. * memory_region_ref: Add 1 to a memory region's reference count
  455. *
  456. * Whenever memory regions are accessed outside the BQL, they need to be
  457. * preserved against hot-unplug. MemoryRegions actually do not have their
  458. * own reference count; they piggyback on a QOM object, their "owner".
  459. * This function adds a reference to the owner.
  460. *
  461. * All MemoryRegions must have an owner if they can disappear, even if the
  462. * device they belong to operates exclusively under the BQL. This is because
  463. * the region could be returned at any time by memory_region_find, and this
  464. * is usually under guest control.
  465. *
  466. * @mr: the #MemoryRegion
  467. */
  468. void memory_region_ref(MemoryRegion *mr);
  469. /**
  470. * memory_region_unref: Remove 1 to a memory region's reference count
  471. *
  472. * Whenever memory regions are accessed outside the BQL, they need to be
  473. * preserved against hot-unplug. MemoryRegions actually do not have their
  474. * own reference count; they piggyback on a QOM object, their "owner".
  475. * This function removes a reference to the owner and possibly destroys it.
  476. *
  477. * @mr: the #MemoryRegion
  478. */
  479. void memory_region_unref(MemoryRegion *mr);
  480. /**
  481. * memory_region_init_io: Initialize an I/O memory region.
  482. *
  483. * Accesses into the region will cause the callbacks in @ops to be called.
  484. * if @size is nonzero, subregions will be clipped to @size.
  485. *
  486. * @mr: the #MemoryRegion to be initialized.
  487. * @owner: the object that tracks the region's reference count
  488. * @ops: a structure containing read and write callbacks to be used when
  489. * I/O is performed on the region.
  490. * @opaque: passed to the read and write callbacks of the @ops structure.
  491. * @name: used for debugging; not visible to the user or ABI
  492. * @size: size of the region.
  493. */
  494. void memory_region_init_io(MemoryRegion *mr,
  495. struct Object *owner,
  496. const MemoryRegionOps *ops,
  497. void *opaque,
  498. const char *name,
  499. uint64_t size);
  500. /**
  501. * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
  502. * into the region will modify memory
  503. * directly.
  504. *
  505. * @mr: the #MemoryRegion to be initialized.
  506. * @owner: the object that tracks the region's reference count
  507. * @name: Region name, becomes part of RAMBlock name used in migration stream
  508. * must be unique within any device
  509. * @size: size of the region.
  510. * @errp: pointer to Error*, to store an error if it happens.
  511. *
  512. * Note that this function does not do anything to cause the data in the
  513. * RAM memory region to be migrated; that is the responsibility of the caller.
  514. */
  515. void memory_region_init_ram_nomigrate(MemoryRegion *mr,
  516. struct Object *owner,
  517. const char *name,
  518. uint64_t size,
  519. Error **errp);
  520. /**
  521. * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
  522. * Accesses into the region will
  523. * modify memory directly.
  524. *
  525. * @mr: the #MemoryRegion to be initialized.
  526. * @owner: the object that tracks the region's reference count
  527. * @name: Region name, becomes part of RAMBlock name used in migration stream
  528. * must be unique within any device
  529. * @size: size of the region.
  530. * @share: allow remapping RAM to different addresses
  531. * @errp: pointer to Error*, to store an error if it happens.
  532. *
  533. * Note that this function is similar to memory_region_init_ram_nomigrate.
  534. * The only difference is part of the RAM region can be remapped.
  535. */
  536. void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
  537. struct Object *owner,
  538. const char *name,
  539. uint64_t size,
  540. bool share,
  541. Error **errp);
  542. /**
  543. * memory_region_init_resizeable_ram: Initialize memory region with resizeable
  544. * RAM. Accesses into the region will
  545. * modify memory directly. Only an initial
  546. * portion of this RAM is actually used.
  547. * The used size can change across reboots.
  548. *
  549. * @mr: the #MemoryRegion to be initialized.
  550. * @owner: the object that tracks the region's reference count
  551. * @name: Region name, becomes part of RAMBlock name used in migration stream
  552. * must be unique within any device
  553. * @size: used size of the region.
  554. * @max_size: max size of the region.
  555. * @resized: callback to notify owner about used size change.
  556. * @errp: pointer to Error*, to store an error if it happens.
  557. *
  558. * Note that this function does not do anything to cause the data in the
  559. * RAM memory region to be migrated; that is the responsibility of the caller.
  560. */
  561. void memory_region_init_resizeable_ram(MemoryRegion *mr,
  562. struct Object *owner,
  563. const char *name,
  564. uint64_t size,
  565. uint64_t max_size,
  566. void (*resized)(const char*,
  567. uint64_t length,
  568. void *host),
  569. Error **errp);
  570. #ifdef __linux__
  571. /**
  572. * memory_region_init_ram_from_file: Initialize RAM memory region with a
  573. * mmap-ed backend.
  574. *
  575. * @mr: the #MemoryRegion to be initialized.
  576. * @owner: the object that tracks the region's reference count
  577. * @name: Region name, becomes part of RAMBlock name used in migration stream
  578. * must be unique within any device
  579. * @size: size of the region.
  580. * @align: alignment of the region base address; if 0, the default alignment
  581. * (getpagesize()) will be used.
  582. * @share: %true if memory must be mmaped with the MAP_SHARED flag
  583. * @path: the path in which to allocate the RAM.
  584. * @errp: pointer to Error*, to store an error if it happens.
  585. *
  586. * Note that this function does not do anything to cause the data in the
  587. * RAM memory region to be migrated; that is the responsibility of the caller.
  588. */
  589. void memory_region_init_ram_from_file(MemoryRegion *mr,
  590. struct Object *owner,
  591. const char *name,
  592. uint64_t size,
  593. uint64_t align,
  594. bool share,
  595. const char *path,
  596. Error **errp);
  597. /**
  598. * memory_region_init_ram_from_fd: Initialize RAM memory region with a
  599. * mmap-ed backend.
  600. *
  601. * @mr: the #MemoryRegion to be initialized.
  602. * @owner: the object that tracks the region's reference count
  603. * @name: the name of the region.
  604. * @size: size of the region.
  605. * @share: %true if memory must be mmaped with the MAP_SHARED flag
  606. * @fd: the fd to mmap.
  607. * @errp: pointer to Error*, to store an error if it happens.
  608. *
  609. * Note that this function does not do anything to cause the data in the
  610. * RAM memory region to be migrated; that is the responsibility of the caller.
  611. */
  612. void memory_region_init_ram_from_fd(MemoryRegion *mr,
  613. struct Object *owner,
  614. const char *name,
  615. uint64_t size,
  616. bool share,
  617. int fd,
  618. Error **errp);
  619. #endif
  620. /**
  621. * memory_region_init_ram_ptr: Initialize RAM memory region from a
  622. * user-provided pointer. Accesses into the
  623. * region will modify memory directly.
  624. *
  625. * @mr: the #MemoryRegion to be initialized.
  626. * @owner: the object that tracks the region's reference count
  627. * @name: Region name, becomes part of RAMBlock name used in migration stream
  628. * must be unique within any device
  629. * @size: size of the region.
  630. * @ptr: memory to be mapped; must contain at least @size bytes.
  631. *
  632. * Note that this function does not do anything to cause the data in the
  633. * RAM memory region to be migrated; that is the responsibility of the caller.
  634. */
  635. void memory_region_init_ram_ptr(MemoryRegion *mr,
  636. struct Object *owner,
  637. const char *name,
  638. uint64_t size,
  639. void *ptr);
  640. /**
  641. * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
  642. * a user-provided pointer.
  643. *
  644. * A RAM device represents a mapping to a physical device, such as to a PCI
  645. * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
  646. * into the VM address space and access to the region will modify memory
  647. * directly. However, the memory region should not be included in a memory
  648. * dump (device may not be enabled/mapped at the time of the dump), and
  649. * operations incompatible with manipulating MMIO should be avoided. Replaces
  650. * skip_dump flag.
  651. *
  652. * @mr: the #MemoryRegion to be initialized.
  653. * @owner: the object that tracks the region's reference count
  654. * @name: the name of the region.
  655. * @size: size of the region.
  656. * @ptr: memory to be mapped; must contain at least @size bytes.
  657. *
  658. * Note that this function does not do anything to cause the data in the
  659. * RAM memory region to be migrated; that is the responsibility of the caller.
  660. * (For RAM device memory regions, migrating the contents rarely makes sense.)
  661. */
  662. void memory_region_init_ram_device_ptr(MemoryRegion *mr,
  663. struct Object *owner,
  664. const char *name,
  665. uint64_t size,
  666. void *ptr);
  667. /**
  668. * memory_region_init_alias: Initialize a memory region that aliases all or a
  669. * part of another memory region.
  670. *
  671. * @mr: the #MemoryRegion to be initialized.
  672. * @owner: the object that tracks the region's reference count
  673. * @name: used for debugging; not visible to the user or ABI
  674. * @orig: the region to be referenced; @mr will be equivalent to
  675. * @orig between @offset and @offset + @size - 1.
  676. * @offset: start of the section in @orig to be referenced.
  677. * @size: size of the region.
  678. */
  679. void memory_region_init_alias(MemoryRegion *mr,
  680. struct Object *owner,
  681. const char *name,
  682. MemoryRegion *orig,
  683. hwaddr offset,
  684. uint64_t size);
  685. /**
  686. * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
  687. *
  688. * This has the same effect as calling memory_region_init_ram_nomigrate()
  689. * and then marking the resulting region read-only with
  690. * memory_region_set_readonly().
  691. *
  692. * Note that this function does not do anything to cause the data in the
  693. * RAM side of the memory region to be migrated; that is the responsibility
  694. * of the caller.
  695. *
  696. * @mr: the #MemoryRegion to be initialized.
  697. * @owner: the object that tracks the region's reference count
  698. * @name: Region name, becomes part of RAMBlock name used in migration stream
  699. * must be unique within any device
  700. * @size: size of the region.
  701. * @errp: pointer to Error*, to store an error if it happens.
  702. */
  703. void memory_region_init_rom_nomigrate(MemoryRegion *mr,
  704. struct Object *owner,
  705. const char *name,
  706. uint64_t size,
  707. Error **errp);
  708. /**
  709. * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
  710. * Writes are handled via callbacks.
  711. *
  712. * Note that this function does not do anything to cause the data in the
  713. * RAM side of the memory region to be migrated; that is the responsibility
  714. * of the caller.
  715. *
  716. * @mr: the #MemoryRegion to be initialized.
  717. * @owner: the object that tracks the region's reference count
  718. * @ops: callbacks for write access handling (must not be NULL).
  719. * @opaque: passed to the read and write callbacks of the @ops structure.
  720. * @name: Region name, becomes part of RAMBlock name used in migration stream
  721. * must be unique within any device
  722. * @size: size of the region.
  723. * @errp: pointer to Error*, to store an error if it happens.
  724. */
  725. void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
  726. struct Object *owner,
  727. const MemoryRegionOps *ops,
  728. void *opaque,
  729. const char *name,
  730. uint64_t size,
  731. Error **errp);
  732. /**
  733. * memory_region_init_iommu: Initialize a memory region of a custom type
  734. * that translates addresses
  735. *
  736. * An IOMMU region translates addresses and forwards accesses to a target
  737. * memory region.
  738. *
  739. * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
  740. * @_iommu_mr should be a pointer to enough memory for an instance of
  741. * that subclass, @instance_size is the size of that subclass, and
  742. * @mrtypename is its name. This function will initialize @_iommu_mr as an
  743. * instance of the subclass, and its methods will then be called to handle
  744. * accesses to the memory region. See the documentation of
  745. * #IOMMUMemoryRegionClass for further details.
  746. *
  747. * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
  748. * @instance_size: the IOMMUMemoryRegion subclass instance size
  749. * @mrtypename: the type name of the #IOMMUMemoryRegion
  750. * @owner: the object that tracks the region's reference count
  751. * @name: used for debugging; not visible to the user or ABI
  752. * @size: size of the region.
  753. */
  754. void memory_region_init_iommu(void *_iommu_mr,
  755. size_t instance_size,
  756. const char *mrtypename,
  757. Object *owner,
  758. const char *name,
  759. uint64_t size);
  760. /**
  761. * memory_region_init_ram - Initialize RAM memory region. Accesses into the
  762. * region will modify memory directly.
  763. *
  764. * @mr: the #MemoryRegion to be initialized
  765. * @owner: the object that tracks the region's reference count (must be
  766. * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
  767. * @name: name of the memory region
  768. * @size: size of the region in bytes
  769. * @errp: pointer to Error*, to store an error if it happens.
  770. *
  771. * This function allocates RAM for a board model or device, and
  772. * arranges for it to be migrated (by calling vmstate_register_ram()
  773. * if @owner is a DeviceState, or vmstate_register_ram_global() if
  774. * @owner is NULL).
  775. *
  776. * TODO: Currently we restrict @owner to being either NULL (for
  777. * global RAM regions with no owner) or devices, so that we can
  778. * give the RAM block a unique name for migration purposes.
  779. * We should lift this restriction and allow arbitrary Objects.
  780. * If you pass a non-NULL non-device @owner then we will assert.
  781. */
  782. void memory_region_init_ram(MemoryRegion *mr,
  783. struct Object *owner,
  784. const char *name,
  785. uint64_t size,
  786. Error **errp);
  787. /**
  788. * memory_region_init_rom: Initialize a ROM memory region.
  789. *
  790. * This has the same effect as calling memory_region_init_ram()
  791. * and then marking the resulting region read-only with
  792. * memory_region_set_readonly(). This includes arranging for the
  793. * contents to be migrated.
  794. *
  795. * TODO: Currently we restrict @owner to being either NULL (for
  796. * global RAM regions with no owner) or devices, so that we can
  797. * give the RAM block a unique name for migration purposes.
  798. * We should lift this restriction and allow arbitrary Objects.
  799. * If you pass a non-NULL non-device @owner then we will assert.
  800. *
  801. * @mr: the #MemoryRegion to be initialized.
  802. * @owner: the object that tracks the region's reference count
  803. * @name: Region name, becomes part of RAMBlock name used in migration stream
  804. * must be unique within any device
  805. * @size: size of the region.
  806. * @errp: pointer to Error*, to store an error if it happens.
  807. */
  808. void memory_region_init_rom(MemoryRegion *mr,
  809. struct Object *owner,
  810. const char *name,
  811. uint64_t size,
  812. Error **errp);
  813. /**
  814. * memory_region_init_rom_device: Initialize a ROM memory region.
  815. * Writes are handled via callbacks.
  816. *
  817. * This function initializes a memory region backed by RAM for reads
  818. * and callbacks for writes, and arranges for the RAM backing to
  819. * be migrated (by calling vmstate_register_ram()
  820. * if @owner is a DeviceState, or vmstate_register_ram_global() if
  821. * @owner is NULL).
  822. *
  823. * TODO: Currently we restrict @owner to being either NULL (for
  824. * global RAM regions with no owner) or devices, so that we can
  825. * give the RAM block a unique name for migration purposes.
  826. * We should lift this restriction and allow arbitrary Objects.
  827. * If you pass a non-NULL non-device @owner then we will assert.
  828. *
  829. * @mr: the #MemoryRegion to be initialized.
  830. * @owner: the object that tracks the region's reference count
  831. * @ops: callbacks for write access handling (must not be NULL).
  832. * @name: Region name, becomes part of RAMBlock name used in migration stream
  833. * must be unique within any device
  834. * @size: size of the region.
  835. * @errp: pointer to Error*, to store an error if it happens.
  836. */
  837. void memory_region_init_rom_device(MemoryRegion *mr,
  838. struct Object *owner,
  839. const MemoryRegionOps *ops,
  840. void *opaque,
  841. const char *name,
  842. uint64_t size,
  843. Error **errp);
  844. /**
  845. * memory_region_owner: get a memory region's owner.
  846. *
  847. * @mr: the memory region being queried.
  848. */
  849. struct Object *memory_region_owner(MemoryRegion *mr);
  850. /**
  851. * memory_region_size: get a memory region's size.
  852. *
  853. * @mr: the memory region being queried.
  854. */
  855. uint64_t memory_region_size(MemoryRegion *mr);
  856. /**
  857. * memory_region_is_ram: check whether a memory region is random access
  858. *
  859. * Returns %true is a memory region is random access.
  860. *
  861. * @mr: the memory region being queried
  862. */
  863. static inline bool memory_region_is_ram(MemoryRegion *mr)
  864. {
  865. return mr->ram;
  866. }
  867. /**
  868. * memory_region_is_ram_device: check whether a memory region is a ram device
  869. *
  870. * Returns %true is a memory region is a device backed ram region
  871. *
  872. * @mr: the memory region being queried
  873. */
  874. bool memory_region_is_ram_device(MemoryRegion *mr);
  875. /**
  876. * memory_region_is_romd: check whether a memory region is in ROMD mode
  877. *
  878. * Returns %true if a memory region is a ROM device and currently set to allow
  879. * direct reads.
  880. *
  881. * @mr: the memory region being queried
  882. */
  883. static inline bool memory_region_is_romd(MemoryRegion *mr)
  884. {
  885. return mr->rom_device && mr->romd_mode;
  886. }
  887. /**
  888. * memory_region_get_iommu: check whether a memory region is an iommu
  889. *
  890. * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
  891. * otherwise NULL.
  892. *
  893. * @mr: the memory region being queried
  894. */
  895. static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
  896. {
  897. if (mr->alias) {
  898. return memory_region_get_iommu(mr->alias);
  899. }
  900. if (mr->is_iommu) {
  901. return (IOMMUMemoryRegion *) mr;
  902. }
  903. return NULL;
  904. }
  905. /**
  906. * memory_region_get_iommu_class_nocheck: returns iommu memory region class
  907. * if an iommu or NULL if not
  908. *
  909. * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
  910. * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
  911. *
  912. * @mr: the memory region being queried
  913. */
  914. static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck(
  915. IOMMUMemoryRegion *iommu_mr)
  916. {
  917. return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class);
  918. }
  919. #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
  920. /**
  921. * memory_region_iommu_get_min_page_size: get minimum supported page size
  922. * for an iommu
  923. *
  924. * Returns minimum supported page size for an iommu.
  925. *
  926. * @iommu_mr: the memory region being queried
  927. */
  928. uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
  929. /**
  930. * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
  931. *
  932. * The notification type will be decided by entry.perm bits:
  933. *
  934. * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
  935. * - For MAP (newly added entry) notifies: set entry.perm to the
  936. * permission of the page (which is definitely !IOMMU_NONE).
  937. *
  938. * Note: for any IOMMU implementation, an in-place mapping change
  939. * should be notified with an UNMAP followed by a MAP.
  940. *
  941. * @iommu_mr: the memory region that was changed
  942. * @iommu_idx: the IOMMU index for the translation table which has changed
  943. * @entry: the new entry in the IOMMU translation table. The entry
  944. * replaces all old entries for the same virtual I/O address range.
  945. * Deleted entries have .@perm == 0.
  946. */
  947. void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
  948. int iommu_idx,
  949. IOMMUTLBEntry entry);
  950. /**
  951. * memory_region_notify_one: notify a change in an IOMMU translation
  952. * entry to a single notifier
  953. *
  954. * This works just like memory_region_notify_iommu(), but it only
  955. * notifies a specific notifier, not all of them.
  956. *
  957. * @notifier: the notifier to be notified
  958. * @entry: the new entry in the IOMMU translation table. The entry
  959. * replaces all old entries for the same virtual I/O address range.
  960. * Deleted entries have .@perm == 0.
  961. */
  962. void memory_region_notify_one(IOMMUNotifier *notifier,
  963. IOMMUTLBEntry *entry);
  964. /**
  965. * memory_region_register_iommu_notifier: register a notifier for changes to
  966. * IOMMU translation entries.
  967. *
  968. * @mr: the memory region to observe
  969. * @n: the IOMMUNotifier to be added; the notify callback receives a
  970. * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
  971. * ceases to be valid on exit from the notifier.
  972. */
  973. void memory_region_register_iommu_notifier(MemoryRegion *mr,
  974. IOMMUNotifier *n);
  975. /**
  976. * memory_region_iommu_replay: replay existing IOMMU translations to
  977. * a notifier with the minimum page granularity returned by
  978. * mr->iommu_ops->get_page_size().
  979. *
  980. * Note: this is not related to record-and-replay functionality.
  981. *
  982. * @iommu_mr: the memory region to observe
  983. * @n: the notifier to which to replay iommu mappings
  984. */
  985. void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
  986. /**
  987. * memory_region_iommu_replay_all: replay existing IOMMU translations
  988. * to all the notifiers registered.
  989. *
  990. * Note: this is not related to record-and-replay functionality.
  991. *
  992. * @iommu_mr: the memory region to observe
  993. */
  994. void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr);
  995. /**
  996. * memory_region_unregister_iommu_notifier: unregister a notifier for
  997. * changes to IOMMU translation entries.
  998. *
  999. * @mr: the memory region which was observed and for which notity_stopped()
  1000. * needs to be called
  1001. * @n: the notifier to be removed.
  1002. */
  1003. void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
  1004. IOMMUNotifier *n);
  1005. /**
  1006. * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
  1007. * defined on the IOMMU.
  1008. *
  1009. * Returns 0 on success, or a negative errno otherwise. In particular,
  1010. * -EINVAL indicates that the IOMMU does not support the requested
  1011. * attribute.
  1012. *
  1013. * @iommu_mr: the memory region
  1014. * @attr: the requested attribute
  1015. * @data: a pointer to the requested attribute data
  1016. */
  1017. int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
  1018. enum IOMMUMemoryRegionAttr attr,
  1019. void *data);
  1020. /**
  1021. * memory_region_iommu_attrs_to_index: return the IOMMU index to
  1022. * use for translations with the given memory transaction attributes.
  1023. *
  1024. * @iommu_mr: the memory region
  1025. * @attrs: the memory transaction attributes
  1026. */
  1027. int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr,
  1028. MemTxAttrs attrs);
  1029. /**
  1030. * memory_region_iommu_num_indexes: return the total number of IOMMU
  1031. * indexes that this IOMMU supports.
  1032. *
  1033. * @iommu_mr: the memory region
  1034. */
  1035. int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr);
  1036. /**
  1037. * memory_region_name: get a memory region's name
  1038. *
  1039. * Returns the string that was used to initialize the memory region.
  1040. *
  1041. * @mr: the memory region being queried
  1042. */
  1043. const char *memory_region_name(const MemoryRegion *mr);
  1044. /**
  1045. * memory_region_is_logging: return whether a memory region is logging writes
  1046. *
  1047. * Returns %true if the memory region is logging writes for the given client
  1048. *
  1049. * @mr: the memory region being queried
  1050. * @client: the client being queried
  1051. */
  1052. bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
  1053. /**
  1054. * memory_region_get_dirty_log_mask: return the clients for which a
  1055. * memory region is logging writes.
  1056. *
  1057. * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
  1058. * are the bit indices.
  1059. *
  1060. * @mr: the memory region being queried
  1061. */
  1062. uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
  1063. /**
  1064. * memory_region_is_rom: check whether a memory region is ROM
  1065. *
  1066. * Returns %true is a memory region is read-only memory.
  1067. *
  1068. * @mr: the memory region being queried
  1069. */
  1070. static inline bool memory_region_is_rom(MemoryRegion *mr)
  1071. {
  1072. return mr->ram && mr->readonly;
  1073. }
  1074. /**
  1075. * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
  1076. *
  1077. * Returns a file descriptor backing a file-based RAM memory region,
  1078. * or -1 if the region is not a file-based RAM memory region.
  1079. *
  1080. * @mr: the RAM or alias memory region being queried.
  1081. */
  1082. int memory_region_get_fd(MemoryRegion *mr);
  1083. /**
  1084. * memory_region_from_host: Convert a pointer into a RAM memory region
  1085. * and an offset within it.
  1086. *
  1087. * Given a host pointer inside a RAM memory region (created with
  1088. * memory_region_init_ram() or memory_region_init_ram_ptr()), return
  1089. * the MemoryRegion and the offset within it.
  1090. *
  1091. * Use with care; by the time this function returns, the returned pointer is
  1092. * not protected by RCU anymore. If the caller is not within an RCU critical
  1093. * section and does not hold the iothread lock, it must have other means of
  1094. * protecting the pointer, such as a reference to the region that includes
  1095. * the incoming ram_addr_t.
  1096. *
  1097. * @ptr: the host pointer to be converted
  1098. * @offset: the offset within memory region
  1099. */
  1100. MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
  1101. /**
  1102. * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
  1103. *
  1104. * Returns a host pointer to a RAM memory region (created with
  1105. * memory_region_init_ram() or memory_region_init_ram_ptr()).
  1106. *
  1107. * Use with care; by the time this function returns, the returned pointer is
  1108. * not protected by RCU anymore. If the caller is not within an RCU critical
  1109. * section and does not hold the iothread lock, it must have other means of
  1110. * protecting the pointer, such as a reference to the region that includes
  1111. * the incoming ram_addr_t.
  1112. *
  1113. * @mr: the memory region being queried.
  1114. */
  1115. void *memory_region_get_ram_ptr(MemoryRegion *mr);
  1116. /* memory_region_ram_resize: Resize a RAM region.
  1117. *
  1118. * Only legal before guest might have detected the memory size: e.g. on
  1119. * incoming migration, or right after reset.
  1120. *
  1121. * @mr: a memory region created with @memory_region_init_resizeable_ram.
  1122. * @newsize: the new size the region
  1123. * @errp: pointer to Error*, to store an error if it happens.
  1124. */
  1125. void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
  1126. Error **errp);
  1127. /**
  1128. * memory_region_set_log: Turn dirty logging on or off for a region.
  1129. *
  1130. * Turns dirty logging on or off for a specified client (display, migration).
  1131. * Only meaningful for RAM regions.
  1132. *
  1133. * @mr: the memory region being updated.
  1134. * @log: whether dirty logging is to be enabled or disabled.
  1135. * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
  1136. */
  1137. void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
  1138. /**
  1139. * memory_region_get_dirty: Check whether a range of bytes is dirty
  1140. * for a specified client.
  1141. *
  1142. * Checks whether a range of bytes has been written to since the last
  1143. * call to memory_region_reset_dirty() with the same @client. Dirty logging
  1144. * must be enabled.
  1145. *
  1146. * @mr: the memory region being queried.
  1147. * @addr: the address (relative to the start of the region) being queried.
  1148. * @size: the size of the range being queried.
  1149. * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
  1150. * %DIRTY_MEMORY_VGA.
  1151. */
  1152. bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
  1153. hwaddr size, unsigned client);
  1154. /**
  1155. * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
  1156. *
  1157. * Marks a range of bytes as dirty, after it has been dirtied outside
  1158. * guest code.
  1159. *
  1160. * @mr: the memory region being dirtied.
  1161. * @addr: the address (relative to the start of the region) being dirtied.
  1162. * @size: size of the range being dirtied.
  1163. */
  1164. void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
  1165. hwaddr size);
  1166. /**
  1167. * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
  1168. * bitmap and clear it.
  1169. *
  1170. * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
  1171. * returns the snapshot. The snapshot can then be used to query dirty
  1172. * status, using memory_region_snapshot_get_dirty. Snapshotting allows
  1173. * querying the same page multiple times, which is especially useful for
  1174. * display updates where the scanlines often are not page aligned.
  1175. *
  1176. * The dirty bitmap region which gets copyed into the snapshot (and
  1177. * cleared afterwards) can be larger than requested. The boundaries
  1178. * are rounded up/down so complete bitmap longs (covering 64 pages on
  1179. * 64bit hosts) can be copied over into the bitmap snapshot. Which
  1180. * isn't a problem for display updates as the extra pages are outside
  1181. * the visible area, and in case the visible area changes a full
  1182. * display redraw is due anyway. Should other use cases for this
  1183. * function emerge we might have to revisit this implementation
  1184. * detail.
  1185. *
  1186. * Use g_free to release DirtyBitmapSnapshot.
  1187. *
  1188. * @mr: the memory region being queried.
  1189. * @addr: the address (relative to the start of the region) being queried.
  1190. * @size: the size of the range being queried.
  1191. * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
  1192. */
  1193. DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
  1194. hwaddr addr,
  1195. hwaddr size,
  1196. unsigned client);
  1197. /**
  1198. * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
  1199. * in the specified dirty bitmap snapshot.
  1200. *
  1201. * @mr: the memory region being queried.
  1202. * @snap: the dirty bitmap snapshot
  1203. * @addr: the address (relative to the start of the region) being queried.
  1204. * @size: the size of the range being queried.
  1205. */
  1206. bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
  1207. DirtyBitmapSnapshot *snap,
  1208. hwaddr addr, hwaddr size);
  1209. /**
  1210. * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
  1211. * client.
  1212. *
  1213. * Marks a range of pages as no longer dirty.
  1214. *
  1215. * @mr: the region being updated.
  1216. * @addr: the start of the subrange being cleaned.
  1217. * @size: the size of the subrange being cleaned.
  1218. * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
  1219. * %DIRTY_MEMORY_VGA.
  1220. */
  1221. void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
  1222. hwaddr size, unsigned client);
  1223. /**
  1224. * memory_region_set_readonly: Turn a memory region read-only (or read-write)
  1225. *
  1226. * Allows a memory region to be marked as read-only (turning it into a ROM).
  1227. * only useful on RAM regions.
  1228. *
  1229. * @mr: the region being updated.
  1230. * @readonly: whether rhe region is to be ROM or RAM.
  1231. */
  1232. void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
  1233. /**
  1234. * memory_region_rom_device_set_romd: enable/disable ROMD mode
  1235. *
  1236. * Allows a ROM device (initialized with memory_region_init_rom_device() to
  1237. * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
  1238. * device is mapped to guest memory and satisfies read access directly.
  1239. * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
  1240. * Writes are always handled by the #MemoryRegion.write function.
  1241. *
  1242. * @mr: the memory region to be updated
  1243. * @romd_mode: %true to put the region into ROMD mode
  1244. */
  1245. void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
  1246. /**
  1247. * memory_region_set_coalescing: Enable memory coalescing for the region.
  1248. *
  1249. * Enabled writes to a region to be queued for later processing. MMIO ->write
  1250. * callbacks may be delayed until a non-coalesced MMIO is issued.
  1251. * Only useful for IO regions. Roughly similar to write-combining hardware.
  1252. *
  1253. * @mr: the memory region to be write coalesced
  1254. */
  1255. void memory_region_set_coalescing(MemoryRegion *mr);
  1256. /**
  1257. * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
  1258. * a region.
  1259. *
  1260. * Like memory_region_set_coalescing(), but works on a sub-range of a region.
  1261. * Multiple calls can be issued coalesced disjoint ranges.
  1262. *
  1263. * @mr: the memory region to be updated.
  1264. * @offset: the start of the range within the region to be coalesced.
  1265. * @size: the size of the subrange to be coalesced.
  1266. */
  1267. void memory_region_add_coalescing(MemoryRegion *mr,
  1268. hwaddr offset,
  1269. uint64_t size);
  1270. /**
  1271. * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
  1272. *
  1273. * Disables any coalescing caused by memory_region_set_coalescing() or
  1274. * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
  1275. * hardware.
  1276. *
  1277. * @mr: the memory region to be updated.
  1278. */
  1279. void memory_region_clear_coalescing(MemoryRegion *mr);
  1280. /**
  1281. * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
  1282. * accesses.
  1283. *
  1284. * Ensure that pending coalesced MMIO request are flushed before the memory
  1285. * region is accessed. This property is automatically enabled for all regions
  1286. * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
  1287. *
  1288. * @mr: the memory region to be updated.
  1289. */
  1290. void memory_region_set_flush_coalesced(MemoryRegion *mr);
  1291. /**
  1292. * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
  1293. * accesses.
  1294. *
  1295. * Clear the automatic coalesced MMIO flushing enabled via
  1296. * memory_region_set_flush_coalesced. Note that this service has no effect on
  1297. * memory regions that have MMIO coalescing enabled for themselves. For them,
  1298. * automatic flushing will stop once coalescing is disabled.
  1299. *
  1300. * @mr: the memory region to be updated.
  1301. */
  1302. void memory_region_clear_flush_coalesced(MemoryRegion *mr);
  1303. /**
  1304. * memory_region_clear_global_locking: Declares that access processing does
  1305. * not depend on the QEMU global lock.
  1306. *
  1307. * By clearing this property, accesses to the memory region will be processed
  1308. * outside of QEMU's global lock (unless the lock is held on when issuing the
  1309. * access request). In this case, the device model implementing the access
  1310. * handlers is responsible for synchronization of concurrency.
  1311. *
  1312. * @mr: the memory region to be updated.
  1313. */
  1314. void memory_region_clear_global_locking(MemoryRegion *mr);
  1315. /**
  1316. * memory_region_add_eventfd: Request an eventfd to be triggered when a word
  1317. * is written to a location.
  1318. *
  1319. * Marks a word in an IO region (initialized with memory_region_init_io())
  1320. * as a trigger for an eventfd event. The I/O callback will not be called.
  1321. * The caller must be prepared to handle failure (that is, take the required
  1322. * action if the callback _is_ called).
  1323. *
  1324. * @mr: the memory region being updated.
  1325. * @addr: the address within @mr that is to be monitored
  1326. * @size: the size of the access to trigger the eventfd
  1327. * @match_data: whether to match against @data, instead of just @addr
  1328. * @data: the data to match against the guest write
  1329. * @e: event notifier to be triggered when @addr, @size, and @data all match.
  1330. **/
  1331. void memory_region_add_eventfd(MemoryRegion *mr,
  1332. hwaddr addr,
  1333. unsigned size,
  1334. bool match_data,
  1335. uint64_t data,
  1336. EventNotifier *e);
  1337. /**
  1338. * memory_region_del_eventfd: Cancel an eventfd.
  1339. *
  1340. * Cancels an eventfd trigger requested by a previous
  1341. * memory_region_add_eventfd() call.
  1342. *
  1343. * @mr: the memory region being updated.
  1344. * @addr: the address within @mr that is to be monitored
  1345. * @size: the size of the access to trigger the eventfd
  1346. * @match_data: whether to match against @data, instead of just @addr
  1347. * @data: the data to match against the guest write
  1348. * @e: event notifier to be triggered when @addr, @size, and @data all match.
  1349. */
  1350. void memory_region_del_eventfd(MemoryRegion *mr,
  1351. hwaddr addr,
  1352. unsigned size,
  1353. bool match_data,
  1354. uint64_t data,
  1355. EventNotifier *e);
  1356. /**
  1357. * memory_region_add_subregion: Add a subregion to a container.
  1358. *
  1359. * Adds a subregion at @offset. The subregion may not overlap with other
  1360. * subregions (except for those explicitly marked as overlapping). A region
  1361. * may only be added once as a subregion (unless removed with
  1362. * memory_region_del_subregion()); use memory_region_init_alias() if you
  1363. * want a region to be a subregion in multiple locations.
  1364. *
  1365. * @mr: the region to contain the new subregion; must be a container
  1366. * initialized with memory_region_init().
  1367. * @offset: the offset relative to @mr where @subregion is added.
  1368. * @subregion: the subregion to be added.
  1369. */
  1370. void memory_region_add_subregion(MemoryRegion *mr,
  1371. hwaddr offset,
  1372. MemoryRegion *subregion);
  1373. /**
  1374. * memory_region_add_subregion_overlap: Add a subregion to a container
  1375. * with overlap.
  1376. *
  1377. * Adds a subregion at @offset. The subregion may overlap with other
  1378. * subregions. Conflicts are resolved by having a higher @priority hide a
  1379. * lower @priority. Subregions without priority are taken as @priority 0.
  1380. * A region may only be added once as a subregion (unless removed with
  1381. * memory_region_del_subregion()); use memory_region_init_alias() if you
  1382. * want a region to be a subregion in multiple locations.
  1383. *
  1384. * @mr: the region to contain the new subregion; must be a container
  1385. * initialized with memory_region_init().
  1386. * @offset: the offset relative to @mr where @subregion is added.
  1387. * @subregion: the subregion to be added.
  1388. * @priority: used for resolving overlaps; highest priority wins.
  1389. */
  1390. void memory_region_add_subregion_overlap(MemoryRegion *mr,
  1391. hwaddr offset,
  1392. MemoryRegion *subregion,
  1393. int priority);
  1394. /**
  1395. * memory_region_get_ram_addr: Get the ram address associated with a memory
  1396. * region
  1397. */
  1398. ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
  1399. uint64_t memory_region_get_alignment(const MemoryRegion *mr);
  1400. /**
  1401. * memory_region_del_subregion: Remove a subregion.
  1402. *
  1403. * Removes a subregion from its container.
  1404. *
  1405. * @mr: the container to be updated.
  1406. * @subregion: the region being removed; must be a current subregion of @mr.
  1407. */
  1408. void memory_region_del_subregion(MemoryRegion *mr,
  1409. MemoryRegion *subregion);
  1410. /*
  1411. * memory_region_set_enabled: dynamically enable or disable a region
  1412. *
  1413. * Enables or disables a memory region. A disabled memory region
  1414. * ignores all accesses to itself and its subregions. It does not
  1415. * obscure sibling subregions with lower priority - it simply behaves as
  1416. * if it was removed from the hierarchy.
  1417. *
  1418. * Regions default to being enabled.
  1419. *
  1420. * @mr: the region to be updated
  1421. * @enabled: whether to enable or disable the region
  1422. */
  1423. void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
  1424. /*
  1425. * memory_region_set_address: dynamically update the address of a region
  1426. *
  1427. * Dynamically updates the address of a region, relative to its container.
  1428. * May be used on regions are currently part of a memory hierarchy.
  1429. *
  1430. * @mr: the region to be updated
  1431. * @addr: new address, relative to container region
  1432. */
  1433. void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
  1434. /*
  1435. * memory_region_set_size: dynamically update the size of a region.
  1436. *
  1437. * Dynamically updates the size of a region.
  1438. *
  1439. * @mr: the region to be updated
  1440. * @size: used size of the region.
  1441. */
  1442. void memory_region_set_size(MemoryRegion *mr, uint64_t size);
  1443. /*
  1444. * memory_region_set_alias_offset: dynamically update a memory alias's offset
  1445. *
  1446. * Dynamically updates the offset into the target region that an alias points
  1447. * to, as if the fourth argument to memory_region_init_alias() has changed.
  1448. *
  1449. * @mr: the #MemoryRegion to be updated; should be an alias.
  1450. * @offset: the new offset into the target memory region
  1451. */
  1452. void memory_region_set_alias_offset(MemoryRegion *mr,
  1453. hwaddr offset);
  1454. /**
  1455. * memory_region_present: checks if an address relative to a @container
  1456. * translates into #MemoryRegion within @container
  1457. *
  1458. * Answer whether a #MemoryRegion within @container covers the address
  1459. * @addr.
  1460. *
  1461. * @container: a #MemoryRegion within which @addr is a relative address
  1462. * @addr: the area within @container to be searched
  1463. */
  1464. bool memory_region_present(MemoryRegion *container, hwaddr addr);
  1465. /**
  1466. * memory_region_is_mapped: returns true if #MemoryRegion is mapped
  1467. * into any address space.
  1468. *
  1469. * @mr: a #MemoryRegion which should be checked if it's mapped
  1470. */
  1471. bool memory_region_is_mapped(MemoryRegion *mr);
  1472. /**
  1473. * memory_region_find: translate an address/size relative to a
  1474. * MemoryRegion into a #MemoryRegionSection.
  1475. *
  1476. * Locates the first #MemoryRegion within @mr that overlaps the range
  1477. * given by @addr and @size.
  1478. *
  1479. * Returns a #MemoryRegionSection that describes a contiguous overlap.
  1480. * It will have the following characteristics:
  1481. * .@size = 0 iff no overlap was found
  1482. * .@mr is non-%NULL iff an overlap was found
  1483. *
  1484. * Remember that in the return value the @offset_within_region is
  1485. * relative to the returned region (in the .@mr field), not to the
  1486. * @mr argument.
  1487. *
  1488. * Similarly, the .@offset_within_address_space is relative to the
  1489. * address space that contains both regions, the passed and the
  1490. * returned one. However, in the special case where the @mr argument
  1491. * has no container (and thus is the root of the address space), the
  1492. * following will hold:
  1493. * .@offset_within_address_space >= @addr
  1494. * .@offset_within_address_space + .@size <= @addr + @size
  1495. *
  1496. * @mr: a MemoryRegion within which @addr is a relative address
  1497. * @addr: start of the area within @as to be searched
  1498. * @size: size of the area to be searched
  1499. */
  1500. MemoryRegionSection memory_region_find(MemoryRegion *mr,
  1501. hwaddr addr, uint64_t size);
  1502. /**
  1503. * memory_global_dirty_log_sync: synchronize the dirty log for all memory
  1504. *
  1505. * Synchronizes the dirty page log for all address spaces.
  1506. */
  1507. void memory_global_dirty_log_sync(void);
  1508. /**
  1509. * memory_region_transaction_begin: Start a transaction.
  1510. *
  1511. * During a transaction, changes will be accumulated and made visible
  1512. * only when the transaction ends (is committed).
  1513. */
  1514. void memory_region_transaction_begin(void);
  1515. /**
  1516. * memory_region_transaction_commit: Commit a transaction and make changes
  1517. * visible to the guest.
  1518. */
  1519. void memory_region_transaction_commit(void);
  1520. /**
  1521. * memory_listener_register: register callbacks to be called when memory
  1522. * sections are mapped or unmapped into an address
  1523. * space
  1524. *
  1525. * @listener: an object containing the callbacks to be called
  1526. * @filter: if non-%NULL, only regions in this address space will be observed
  1527. */
  1528. void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
  1529. /**
  1530. * memory_listener_unregister: undo the effect of memory_listener_register()
  1531. *
  1532. * @listener: an object containing the callbacks to be removed
  1533. */
  1534. void memory_listener_unregister(MemoryListener *listener);
  1535. /**
  1536. * memory_global_dirty_log_start: begin dirty logging for all regions
  1537. */
  1538. void memory_global_dirty_log_start(void);
  1539. /**
  1540. * memory_global_dirty_log_stop: end dirty logging for all regions
  1541. */
  1542. void memory_global_dirty_log_stop(void);
  1543. void mtree_info(fprintf_function mon_printf, void *f, bool flatview,
  1544. bool dispatch_tree);
  1545. /**
  1546. * memory_region_request_mmio_ptr: request a pointer to an mmio
  1547. * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer.
  1548. * When the device wants to invalidate the pointer it will call
  1549. * memory_region_invalidate_mmio_ptr.
  1550. *
  1551. * @mr: #MemoryRegion to check
  1552. * @addr: address within that region
  1553. *
  1554. * Returns true on success, false otherwise.
  1555. */
  1556. bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr);
  1557. /**
  1558. * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio
  1559. * previously requested.
  1560. * In the end that means that if something wants to execute from this area it
  1561. * will need to request the pointer again.
  1562. *
  1563. * @mr: #MemoryRegion associated to the pointer.
  1564. * @offset: offset within the memory region
  1565. * @size: size of that area.
  1566. */
  1567. void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset,
  1568. unsigned size);
  1569. /**
  1570. * memory_region_dispatch_read: perform a read directly to the specified
  1571. * MemoryRegion.
  1572. *
  1573. * @mr: #MemoryRegion to access
  1574. * @addr: address within that region
  1575. * @pval: pointer to uint64_t which the data is written to
  1576. * @size: size of the access in bytes
  1577. * @attrs: memory transaction attributes to use for the access
  1578. */
  1579. MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
  1580. hwaddr addr,
  1581. uint64_t *pval,
  1582. unsigned size,
  1583. MemTxAttrs attrs);
  1584. /**
  1585. * memory_region_dispatch_write: perform a write directly to the specified
  1586. * MemoryRegion.
  1587. *
  1588. * @mr: #MemoryRegion to access
  1589. * @addr: address within that region
  1590. * @data: data to write
  1591. * @size: size of the access in bytes
  1592. * @attrs: memory transaction attributes to use for the access
  1593. */
  1594. MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
  1595. hwaddr addr,
  1596. uint64_t data,
  1597. unsigned size,
  1598. MemTxAttrs attrs);
  1599. /**
  1600. * address_space_init: initializes an address space
  1601. *
  1602. * @as: an uninitialized #AddressSpace
  1603. * @root: a #MemoryRegion that routes addresses for the address space
  1604. * @name: an address space name. The name is only used for debugging
  1605. * output.
  1606. */
  1607. void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
  1608. /**
  1609. * address_space_destroy: destroy an address space
  1610. *
  1611. * Releases all resources associated with an address space. After an address space
  1612. * is destroyed, its root memory region (given by address_space_init()) may be destroyed
  1613. * as well.
  1614. *
  1615. * @as: address space to be destroyed
  1616. */
  1617. void address_space_destroy(AddressSpace *as);
  1618. /**
  1619. * address_space_rw: read from or write to an address space.
  1620. *
  1621. * Return a MemTxResult indicating whether the operation succeeded
  1622. * or failed (eg unassigned memory, device rejected the transaction,
  1623. * IOMMU fault).
  1624. *
  1625. * @as: #AddressSpace to be accessed
  1626. * @addr: address within that address space
  1627. * @attrs: memory transaction attributes
  1628. * @buf: buffer with the data transferred
  1629. * @len: the number of bytes to read or write
  1630. * @is_write: indicates the transfer direction
  1631. */
  1632. MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
  1633. MemTxAttrs attrs, uint8_t *buf,
  1634. int len, bool is_write);
  1635. /**
  1636. * address_space_write: write to address space.
  1637. *
  1638. * Return a MemTxResult indicating whether the operation succeeded
  1639. * or failed (eg unassigned memory, device rejected the transaction,
  1640. * IOMMU fault).
  1641. *
  1642. * @as: #AddressSpace to be accessed
  1643. * @addr: address within that address space
  1644. * @attrs: memory transaction attributes
  1645. * @buf: buffer with the data transferred
  1646. * @len: the number of bytes to write
  1647. */
  1648. MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
  1649. MemTxAttrs attrs,
  1650. const uint8_t *buf, int len);
  1651. /* address_space_ld*: load from an address space
  1652. * address_space_st*: store to an address space
  1653. *
  1654. * These functions perform a load or store of the byte, word,
  1655. * longword or quad to the specified address within the AddressSpace.
  1656. * The _le suffixed functions treat the data as little endian;
  1657. * _be indicates big endian; no suffix indicates "same endianness
  1658. * as guest CPU".
  1659. *
  1660. * The "guest CPU endianness" accessors are deprecated for use outside
  1661. * target-* code; devices should be CPU-agnostic and use either the LE
  1662. * or the BE accessors.
  1663. *
  1664. * @as #AddressSpace to be accessed
  1665. * @addr: address within that address space
  1666. * @val: data value, for stores
  1667. * @attrs: memory transaction attributes
  1668. * @result: location to write the success/failure of the transaction;
  1669. * if NULL, this information is discarded
  1670. */
  1671. #define SUFFIX
  1672. #define ARG1 as
  1673. #define ARG1_DECL AddressSpace *as
  1674. #include "exec/memory_ldst.inc.h"
  1675. #define SUFFIX
  1676. #define ARG1 as
  1677. #define ARG1_DECL AddressSpace *as
  1678. #include "exec/memory_ldst_phys.inc.h"
  1679. struct MemoryRegionCache {
  1680. void *ptr;
  1681. hwaddr xlat;
  1682. hwaddr len;
  1683. FlatView *fv;
  1684. MemoryRegionSection mrs;
  1685. bool is_write;
  1686. };
  1687. #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
  1688. /* address_space_ld*_cached: load from a cached #MemoryRegion
  1689. * address_space_st*_cached: store into a cached #MemoryRegion
  1690. *
  1691. * These functions perform a load or store of the byte, word,
  1692. * longword or quad to the specified address. The address is
  1693. * a physical address in the AddressSpace, but it must lie within
  1694. * a #MemoryRegion that was mapped with address_space_cache_init.
  1695. *
  1696. * The _le suffixed functions treat the data as little endian;
  1697. * _be indicates big endian; no suffix indicates "same endianness
  1698. * as guest CPU".
  1699. *
  1700. * The "guest CPU endianness" accessors are deprecated for use outside
  1701. * target-* code; devices should be CPU-agnostic and use either the LE
  1702. * or the BE accessors.
  1703. *
  1704. * @cache: previously initialized #MemoryRegionCache to be accessed
  1705. * @addr: address within the address space
  1706. * @val: data value, for stores
  1707. * @attrs: memory transaction attributes
  1708. * @result: location to write the success/failure of the transaction;
  1709. * if NULL, this information is discarded
  1710. */
  1711. #define SUFFIX _cached_slow
  1712. #define ARG1 cache
  1713. #define ARG1_DECL MemoryRegionCache *cache
  1714. #include "exec/memory_ldst.inc.h"
  1715. /* Inline fast path for direct RAM access. */
  1716. static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache,
  1717. hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
  1718. {
  1719. assert(addr < cache->len);
  1720. if (likely(cache->ptr)) {
  1721. return ldub_p(cache->ptr + addr);
  1722. } else {
  1723. return address_space_ldub_cached_slow(cache, addr, attrs, result);
  1724. }
  1725. }
  1726. static inline void address_space_stb_cached(MemoryRegionCache *cache,
  1727. hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result)
  1728. {
  1729. assert(addr < cache->len);
  1730. if (likely(cache->ptr)) {
  1731. stb_p(cache->ptr + addr, val);
  1732. } else {
  1733. address_space_stb_cached_slow(cache, addr, val, attrs, result);
  1734. }
  1735. }
  1736. #define ENDIANNESS _le
  1737. #include "exec/memory_ldst_cached.inc.h"
  1738. #define ENDIANNESS _be
  1739. #include "exec/memory_ldst_cached.inc.h"
  1740. #define SUFFIX _cached
  1741. #define ARG1 cache
  1742. #define ARG1_DECL MemoryRegionCache *cache
  1743. #include "exec/memory_ldst_phys.inc.h"
  1744. /* address_space_cache_init: prepare for repeated access to a physical
  1745. * memory region
  1746. *
  1747. * @cache: #MemoryRegionCache to be filled
  1748. * @as: #AddressSpace to be accessed
  1749. * @addr: address within that address space
  1750. * @len: length of buffer
  1751. * @is_write: indicates the transfer direction
  1752. *
  1753. * Will only work with RAM, and may map a subset of the requested range by
  1754. * returning a value that is less than @len. On failure, return a negative
  1755. * errno value.
  1756. *
  1757. * Because it only works with RAM, this function can be used for
  1758. * read-modify-write operations. In this case, is_write should be %true.
  1759. *
  1760. * Note that addresses passed to the address_space_*_cached functions
  1761. * are relative to @addr.
  1762. */
  1763. int64_t address_space_cache_init(MemoryRegionCache *cache,
  1764. AddressSpace *as,
  1765. hwaddr addr,
  1766. hwaddr len,
  1767. bool is_write);
  1768. /**
  1769. * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
  1770. *
  1771. * @cache: The #MemoryRegionCache to operate on.
  1772. * @addr: The first physical address that was written, relative to the
  1773. * address that was passed to @address_space_cache_init.
  1774. * @access_len: The number of bytes that were written starting at @addr.
  1775. */
  1776. void address_space_cache_invalidate(MemoryRegionCache *cache,
  1777. hwaddr addr,
  1778. hwaddr access_len);
  1779. /**
  1780. * address_space_cache_destroy: free a #MemoryRegionCache
  1781. *
  1782. * @cache: The #MemoryRegionCache whose memory should be released.
  1783. */
  1784. void address_space_cache_destroy(MemoryRegionCache *cache);
  1785. /* address_space_get_iotlb_entry: translate an address into an IOTLB
  1786. * entry. Should be called from an RCU critical section.
  1787. */
  1788. IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
  1789. bool is_write, MemTxAttrs attrs);
  1790. /* address_space_translate: translate an address range into an address space
  1791. * into a MemoryRegion and an address range into that section. Should be
  1792. * called from an RCU critical section, to avoid that the last reference
  1793. * to the returned region disappears after address_space_translate returns.
  1794. *
  1795. * @fv: #FlatView to be accessed
  1796. * @addr: address within that address space
  1797. * @xlat: pointer to address within the returned memory region section's
  1798. * #MemoryRegion.
  1799. * @len: pointer to length
  1800. * @is_write: indicates the transfer direction
  1801. * @attrs: memory attributes
  1802. */
  1803. MemoryRegion *flatview_translate(FlatView *fv,
  1804. hwaddr addr, hwaddr *xlat,
  1805. hwaddr *len, bool is_write,
  1806. MemTxAttrs attrs);
  1807. static inline MemoryRegion *address_space_translate(AddressSpace *as,
  1808. hwaddr addr, hwaddr *xlat,
  1809. hwaddr *len, bool is_write,
  1810. MemTxAttrs attrs)
  1811. {
  1812. return flatview_translate(address_space_to_flatview(as),
  1813. addr, xlat, len, is_write, attrs);
  1814. }
  1815. /* address_space_access_valid: check for validity of accessing an address
  1816. * space range
  1817. *
  1818. * Check whether memory is assigned to the given address space range, and
  1819. * access is permitted by any IOMMU regions that are active for the address
  1820. * space.
  1821. *
  1822. * For now, addr and len should be aligned to a page size. This limitation
  1823. * will be lifted in the future.
  1824. *
  1825. * @as: #AddressSpace to be accessed
  1826. * @addr: address within that address space
  1827. * @len: length of the area to be checked
  1828. * @is_write: indicates the transfer direction
  1829. * @attrs: memory attributes
  1830. */
  1831. bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len,
  1832. bool is_write, MemTxAttrs attrs);
  1833. /* address_space_map: map a physical memory region into a host virtual address
  1834. *
  1835. * May map a subset of the requested range, given by and returned in @plen.
  1836. * May return %NULL if resources needed to perform the mapping are exhausted.
  1837. * Use only for reads OR writes - not for read-modify-write operations.
  1838. * Use cpu_register_map_client() to know when retrying the map operation is
  1839. * likely to succeed.
  1840. *
  1841. * @as: #AddressSpace to be accessed
  1842. * @addr: address within that address space
  1843. * @plen: pointer to length of buffer; updated on return
  1844. * @is_write: indicates the transfer direction
  1845. * @attrs: memory attributes
  1846. */
  1847. void *address_space_map(AddressSpace *as, hwaddr addr,
  1848. hwaddr *plen, bool is_write, MemTxAttrs attrs);
  1849. /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
  1850. *
  1851. * Will also mark the memory as dirty if @is_write == %true. @access_len gives
  1852. * the amount of memory that was actually read or written by the caller.
  1853. *
  1854. * @as: #AddressSpace used
  1855. * @buffer: host pointer as returned by address_space_map()
  1856. * @len: buffer length as returned by address_space_map()
  1857. * @access_len: amount of data actually transferred
  1858. * @is_write: indicates the transfer direction
  1859. */
  1860. void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
  1861. int is_write, hwaddr access_len);
  1862. /* Internal functions, part of the implementation of address_space_read. */
  1863. MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
  1864. MemTxAttrs attrs, uint8_t *buf, int len);
  1865. MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
  1866. MemTxAttrs attrs, uint8_t *buf,
  1867. int len, hwaddr addr1, hwaddr l,
  1868. MemoryRegion *mr);
  1869. void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
  1870. /* Internal functions, part of the implementation of address_space_read_cached
  1871. * and address_space_write_cached. */
  1872. void address_space_read_cached_slow(MemoryRegionCache *cache,
  1873. hwaddr addr, void *buf, int len);
  1874. void address_space_write_cached_slow(MemoryRegionCache *cache,
  1875. hwaddr addr, const void *buf, int len);
  1876. static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
  1877. {
  1878. if (is_write) {
  1879. return memory_region_is_ram(mr) &&
  1880. !mr->readonly && !memory_region_is_ram_device(mr);
  1881. } else {
  1882. return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
  1883. memory_region_is_romd(mr);
  1884. }
  1885. }
  1886. /**
  1887. * address_space_read: read from an address space.
  1888. *
  1889. * Return a MemTxResult indicating whether the operation succeeded
  1890. * or failed (eg unassigned memory, device rejected the transaction,
  1891. * IOMMU fault). Called within RCU critical section.
  1892. *
  1893. * @as: #AddressSpace to be accessed
  1894. * @addr: address within that address space
  1895. * @attrs: memory transaction attributes
  1896. * @buf: buffer with the data transferred
  1897. */
  1898. static inline __attribute__((__always_inline__))
  1899. MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
  1900. MemTxAttrs attrs, uint8_t *buf,
  1901. int len)
  1902. {
  1903. MemTxResult result = MEMTX_OK;
  1904. hwaddr l, addr1;
  1905. void *ptr;
  1906. MemoryRegion *mr;
  1907. FlatView *fv;
  1908. if (__builtin_constant_p(len)) {
  1909. if (len) {
  1910. rcu_read_lock();
  1911. fv = address_space_to_flatview(as);
  1912. l = len;
  1913. mr = flatview_translate(fv, addr, &addr1, &l, false, attrs);
  1914. if (len == l && memory_access_is_direct(mr, false)) {
  1915. ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
  1916. memcpy(buf, ptr, len);
  1917. } else {
  1918. result = flatview_read_continue(fv, addr, attrs, buf, len,
  1919. addr1, l, mr);
  1920. }
  1921. rcu_read_unlock();
  1922. }
  1923. } else {
  1924. result = address_space_read_full(as, addr, attrs, buf, len);
  1925. }
  1926. return result;
  1927. }
  1928. /**
  1929. * address_space_read_cached: read from a cached RAM region
  1930. *
  1931. * @cache: Cached region to be addressed
  1932. * @addr: address relative to the base of the RAM region
  1933. * @buf: buffer with the data transferred
  1934. * @len: length of the data transferred
  1935. */
  1936. static inline void
  1937. address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
  1938. void *buf, int len)
  1939. {
  1940. assert(addr < cache->len && len <= cache->len - addr);
  1941. if (likely(cache->ptr)) {
  1942. memcpy(buf, cache->ptr + addr, len);
  1943. } else {
  1944. address_space_read_cached_slow(cache, addr, buf, len);
  1945. }
  1946. }
  1947. /**
  1948. * address_space_write_cached: write to a cached RAM region
  1949. *
  1950. * @cache: Cached region to be addressed
  1951. * @addr: address relative to the base of the RAM region
  1952. * @buf: buffer with the data transferred
  1953. * @len: length of the data transferred
  1954. */
  1955. static inline void
  1956. address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
  1957. void *buf, int len)
  1958. {
  1959. assert(addr < cache->len && len <= cache->len - addr);
  1960. if (likely(cache->ptr)) {
  1961. memcpy(cache->ptr + addr, buf, len);
  1962. } else {
  1963. address_space_write_cached_slow(cache, addr, buf, len);
  1964. }
  1965. }
  1966. #endif
  1967. #endif