memory.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 <stdint.h>
  17. #include <stdbool.h>
  18. #include "qemu-common.h"
  19. #include "cpu-common.h"
  20. #include "targphys.h"
  21. #include "qemu-queue.h"
  22. #include "iorange.h"
  23. #include "ioport.h"
  24. #include "int128.h"
  25. typedef struct MemoryRegionOps MemoryRegionOps;
  26. typedef struct MemoryRegion MemoryRegion;
  27. typedef struct MemoryRegionPortio MemoryRegionPortio;
  28. typedef struct MemoryRegionMmio MemoryRegionMmio;
  29. /* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic
  30. * registration.
  31. */
  32. #define DIRTY_MEMORY_VGA 0
  33. #define DIRTY_MEMORY_CODE 1
  34. #define DIRTY_MEMORY_MIGRATION 3
  35. struct MemoryRegionMmio {
  36. CPUReadMemoryFunc *read[3];
  37. CPUWriteMemoryFunc *write[3];
  38. };
  39. /*
  40. * Memory region callbacks
  41. */
  42. struct MemoryRegionOps {
  43. /* Read from the memory region. @addr is relative to @mr; @size is
  44. * in bytes. */
  45. uint64_t (*read)(void *opaque,
  46. target_phys_addr_t addr,
  47. unsigned size);
  48. /* Write to the memory region. @addr is relative to @mr; @size is
  49. * in bytes. */
  50. void (*write)(void *opaque,
  51. target_phys_addr_t addr,
  52. uint64_t data,
  53. unsigned size);
  54. enum device_endian endianness;
  55. /* Guest-visible constraints: */
  56. struct {
  57. /* If nonzero, specify bounds on access sizes beyond which a machine
  58. * check is thrown.
  59. */
  60. unsigned min_access_size;
  61. unsigned max_access_size;
  62. /* If true, unaligned accesses are supported. Otherwise unaligned
  63. * accesses throw machine checks.
  64. */
  65. bool unaligned;
  66. } valid;
  67. /* Internal implementation constraints: */
  68. struct {
  69. /* If nonzero, specifies the minimum size implemented. Smaller sizes
  70. * will be rounded upwards and a partial result will be returned.
  71. */
  72. unsigned min_access_size;
  73. /* If nonzero, specifies the maximum size implemented. Larger sizes
  74. * will be done as a series of accesses with smaller sizes.
  75. */
  76. unsigned max_access_size;
  77. /* If true, unaligned accesses are supported. Otherwise all accesses
  78. * are converted to (possibly multiple) naturally aligned accesses.
  79. */
  80. bool unaligned;
  81. } impl;
  82. /* If .read and .write are not present, old_portio may be used for
  83. * backwards compatibility with old portio registration
  84. */
  85. const MemoryRegionPortio *old_portio;
  86. /* If .read and .write are not present, old_mmio may be used for
  87. * backwards compatibility with old mmio registration
  88. */
  89. const MemoryRegionMmio old_mmio;
  90. };
  91. typedef struct CoalescedMemoryRange CoalescedMemoryRange;
  92. typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
  93. struct MemoryRegion {
  94. /* All fields are private - violators will be prosecuted */
  95. const MemoryRegionOps *ops;
  96. void *opaque;
  97. MemoryRegion *parent;
  98. Int128 size;
  99. target_phys_addr_t addr;
  100. target_phys_addr_t offset;
  101. bool backend_registered;
  102. void (*destructor)(MemoryRegion *mr);
  103. ram_addr_t ram_addr;
  104. IORange iorange;
  105. bool terminates;
  106. bool readable;
  107. bool readonly; /* For RAM regions */
  108. MemoryRegion *alias;
  109. target_phys_addr_t alias_offset;
  110. unsigned priority;
  111. bool may_overlap;
  112. QTAILQ_HEAD(subregions, MemoryRegion) subregions;
  113. QTAILQ_ENTRY(MemoryRegion) subregions_link;
  114. QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
  115. const char *name;
  116. uint8_t dirty_log_mask;
  117. unsigned ioeventfd_nb;
  118. MemoryRegionIoeventfd *ioeventfds;
  119. };
  120. struct MemoryRegionPortio {
  121. uint32_t offset;
  122. uint32_t len;
  123. unsigned size;
  124. IOPortReadFunc *read;
  125. IOPortWriteFunc *write;
  126. };
  127. #define PORTIO_END_OF_LIST() { }
  128. /**
  129. * memory_region_init: Initialize a memory region
  130. *
  131. * The region typically acts as a container for other memory regions. Us
  132. * memory_region_add_subregion() to add subregions.
  133. *
  134. * @mr: the #MemoryRegion to be initialized
  135. * @name: used for debugging; not visible to the user or ABI
  136. * @size: size of the region; any subregions beyond this size will be clipped
  137. */
  138. void memory_region_init(MemoryRegion *mr,
  139. const char *name,
  140. uint64_t size);
  141. /**
  142. * memory_region_init_io: Initialize an I/O memory region.
  143. *
  144. * Accesses into the region will be cause the callbacks in @ops to be called.
  145. * if @size is nonzero, subregions will be clipped to @size.
  146. *
  147. * @mr: the #MemoryRegion to be initialized.
  148. * @ops: a structure containing read and write callbacks to be used when
  149. * I/O is performed on the region.
  150. * @opaque: passed to to the read and write callbacks of the @ops structure.
  151. * @name: used for debugging; not visible to the user or ABI
  152. * @size: size of the region.
  153. */
  154. void memory_region_init_io(MemoryRegion *mr,
  155. const MemoryRegionOps *ops,
  156. void *opaque,
  157. const char *name,
  158. uint64_t size);
  159. /**
  160. * memory_region_init_ram: Initialize RAM memory region. Accesses into the
  161. * region will be modify memory directly.
  162. *
  163. * @mr: the #MemoryRegion to be initialized.
  164. * @dev: a device associated with the region; may be %NULL.
  165. * @name: the name of the region; the pair (@dev, @name) must be globally
  166. * unique. The name is part of the save/restore ABI and so cannot be
  167. * changed.
  168. * @size: size of the region.
  169. */
  170. void memory_region_init_ram(MemoryRegion *mr,
  171. DeviceState *dev, /* FIXME: layering violation */
  172. const char *name,
  173. uint64_t size);
  174. /**
  175. * memory_region_init_ram: Initialize RAM memory region from a user-provided.
  176. * pointer. Accesses into the region will be modify
  177. * memory directly.
  178. *
  179. * @mr: the #MemoryRegion to be initialized.
  180. * @dev: a device associated with the region; may be %NULL.
  181. * @name: the name of the region; the pair (@dev, @name) must be globally
  182. * unique. The name is part of the save/restore ABI and so cannot be
  183. * changed.
  184. * @size: size of the region.
  185. * @ptr: memory to be mapped; must contain at least @size bytes.
  186. */
  187. void memory_region_init_ram_ptr(MemoryRegion *mr,
  188. DeviceState *dev, /* FIXME: layering violation */
  189. const char *name,
  190. uint64_t size,
  191. void *ptr);
  192. /**
  193. * memory_region_init_alias: Initialize a memory region that aliases all or a
  194. * part of another memory region.
  195. *
  196. * @mr: the #MemoryRegion to be initialized.
  197. * @name: used for debugging; not visible to the user or ABI
  198. * @orig: the region to be referenced; @mr will be equivalent to
  199. * @orig between @offset and @offset + @size - 1.
  200. * @offset: start of the section in @orig to be referenced.
  201. * @size: size of the region.
  202. */
  203. void memory_region_init_alias(MemoryRegion *mr,
  204. const char *name,
  205. MemoryRegion *orig,
  206. target_phys_addr_t offset,
  207. uint64_t size);
  208. /**
  209. * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
  210. * handled via callbacks.
  211. *
  212. * @mr: the #MemoryRegion to be initialized.
  213. * @ops: callbacks for write access handling.
  214. * @dev: a device associated with the region; may be %NULL.
  215. * @name: the name of the region; the pair (@dev, @name) must be globally
  216. * unique. The name is part of the save/restore ABI and so cannot be
  217. * changed.
  218. * @size: size of the region.
  219. */
  220. void memory_region_init_rom_device(MemoryRegion *mr,
  221. const MemoryRegionOps *ops,
  222. void *opaque,
  223. DeviceState *dev, /* FIXME: layering violation */
  224. const char *name,
  225. uint64_t size);
  226. /**
  227. * memory_region_destroy: Destroy a memory region and relaim all resources.
  228. *
  229. * @mr: the region to be destroyed. May not currently be a subregion
  230. * (see memory_region_add_subregion()) or referenced in an alias
  231. * (see memory_region_init_alias()).
  232. */
  233. void memory_region_destroy(MemoryRegion *mr);
  234. /**
  235. * memory_region_size: get a memory region's size.
  236. *
  237. * @mr: the memory region being queried.
  238. */
  239. uint64_t memory_region_size(MemoryRegion *mr);
  240. /**
  241. * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
  242. *
  243. * Returns a host pointer to a RAM memory region (created with
  244. * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
  245. * care.
  246. *
  247. * @mr: the memory region being queried.
  248. */
  249. void *memory_region_get_ram_ptr(MemoryRegion *mr);
  250. /**
  251. * memory_region_set_offset: Sets an offset to be added to MemoryRegionOps
  252. * callbacks.
  253. *
  254. * This function is deprecated and should not be used in new code.
  255. */
  256. void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset);
  257. /**
  258. * memory_region_set_log: Turn dirty logging on or off for a region.
  259. *
  260. * Turns dirty logging on or off for a specified client (display, migration).
  261. * Only meaningful for RAM regions.
  262. *
  263. * @mr: the memory region being updated.
  264. * @log: whether dirty logging is to be enabled or disabled.
  265. * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
  266. * %DIRTY_MEMORY_VGA.
  267. */
  268. void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
  269. /**
  270. * memory_region_get_dirty: Check whether a page is dirty for a specified
  271. * client.
  272. *
  273. * Checks whether a page has been written to since the last
  274. * call to memory_region_reset_dirty() with the same @client. Dirty logging
  275. * must be enabled.
  276. *
  277. * @mr: the memory region being queried.
  278. * @addr: the address (relative to the start of the region) being queried.
  279. * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
  280. * %DIRTY_MEMORY_VGA.
  281. */
  282. bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
  283. unsigned client);
  284. /**
  285. * memory_region_set_dirty: Mark a page as dirty in a memory region.
  286. *
  287. * Marks a page as dirty, after it has been dirtied outside guest code.
  288. *
  289. * @mr: the memory region being queried.
  290. * @addr: the address (relative to the start of the region) being dirtied.
  291. */
  292. void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr);
  293. /**
  294. * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
  295. * any external TLBs (e.g. kvm)
  296. *
  297. * Flushes dirty information from accelerators such as kvm and vhost-net
  298. * and makes it available to users of the memory API.
  299. *
  300. * @mr: the region being flushed.
  301. */
  302. void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
  303. /**
  304. * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
  305. * client.
  306. *
  307. * Marks a range of pages as no longer dirty.
  308. *
  309. * @mr: the region being updated.
  310. * @addr: the start of the subrange being cleaned.
  311. * @size: the size of the subrange being cleaned.
  312. * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
  313. * %DIRTY_MEMORY_VGA.
  314. */
  315. void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
  316. target_phys_addr_t size, unsigned client);
  317. /**
  318. * memory_region_set_readonly: Turn a memory region read-only (or read-write)
  319. *
  320. * Allows a memory region to be marked as read-only (turning it into a ROM).
  321. * only useful on RAM regions.
  322. *
  323. * @mr: the region being updated.
  324. * @readonly: whether rhe region is to be ROM or RAM.
  325. */
  326. void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
  327. /**
  328. * memory_region_rom_device_set_readable: enable/disable ROM readability
  329. *
  330. * Allows a ROM device (initialized with memory_region_init_rom_device() to
  331. * to be marked as readable (default) or not readable. When it is readable,
  332. * the device is mapped to guest memory. When not readable, reads are
  333. * forwarded to the #MemoryRegion.read function.
  334. *
  335. * @mr: the memory region to be updated
  336. * @readable: whether reads are satisified directly (%true) or via callbacks
  337. * (%false)
  338. */
  339. void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable);
  340. /**
  341. * memory_region_set_coalescing: Enable memory coalescing for the region.
  342. *
  343. * Enabled writes to a region to be queued for later processing. MMIO ->write
  344. * callbacks may be delayed until a non-coalesced MMIO is issued.
  345. * Only useful for IO regions. Roughly similar to write-combining hardware.
  346. *
  347. * @mr: the memory region to be write coalesced
  348. */
  349. void memory_region_set_coalescing(MemoryRegion *mr);
  350. /**
  351. * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
  352. * a region.
  353. *
  354. * Like memory_region_set_coalescing(), but works on a sub-range of a region.
  355. * Multiple calls can be issued coalesced disjoint ranges.
  356. *
  357. * @mr: the memory region to be updated.
  358. * @offset: the start of the range within the region to be coalesced.
  359. * @size: the size of the subrange to be coalesced.
  360. */
  361. void memory_region_add_coalescing(MemoryRegion *mr,
  362. target_phys_addr_t offset,
  363. uint64_t size);
  364. /**
  365. * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
  366. *
  367. * Disables any coalescing caused by memory_region_set_coalescing() or
  368. * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
  369. * hardware.
  370. *
  371. * @mr: the memory region to be updated.
  372. */
  373. void memory_region_clear_coalescing(MemoryRegion *mr);
  374. /**
  375. * memory_region_add_eventfd: Request an eventfd to be triggered when a word
  376. * is written to a location.
  377. *
  378. * Marks a word in an IO region (initialized with memory_region_init_io())
  379. * as a trigger for an eventfd event. The I/O callback will not be called.
  380. * The caller must be prepared to handle failure (hat is, take the required
  381. * action if the callback _is_ called).
  382. *
  383. * @mr: the memory region being updated.
  384. * @addr: the address within @mr that is to be monitored
  385. * @size: the size of the access to trigger the eventfd
  386. * @match_data: whether to match against @data, instead of just @addr
  387. * @data: the data to match against the guest write
  388. * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
  389. **/
  390. void memory_region_add_eventfd(MemoryRegion *mr,
  391. target_phys_addr_t addr,
  392. unsigned size,
  393. bool match_data,
  394. uint64_t data,
  395. int fd);
  396. /**
  397. * memory_region_del_eventfd: Cancel and eventfd.
  398. *
  399. * Cancels an eventfd trigger request by a previous memory_region_add_eventfd()
  400. * call.
  401. *
  402. * @mr: the memory region being updated.
  403. * @addr: the address within @mr that is to be monitored
  404. * @size: the size of the access to trigger the eventfd
  405. * @match_data: whether to match against @data, instead of just @addr
  406. * @data: the data to match against the guest write
  407. * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
  408. */
  409. void memory_region_del_eventfd(MemoryRegion *mr,
  410. target_phys_addr_t addr,
  411. unsigned size,
  412. bool match_data,
  413. uint64_t data,
  414. int fd);
  415. /**
  416. * memory_region_add_subregion: Add a sub-region to a container.
  417. *
  418. * Adds a sub-region at @offset. The sub-region may not overlap with other
  419. * subregions (except for those explicitly marked as overlapping). A region
  420. * may only be added once as a subregion (unless removed with
  421. * memory_region_del_subregion()); use memory_region_init_alias() if you
  422. * want a region to be a subregion in multiple locations.
  423. *
  424. * @mr: the region to contain the new subregion; must be a container
  425. * initialized with memory_region_init().
  426. * @offset: the offset relative to @mr where @subregion is added.
  427. * @subregion: the subregion to be added.
  428. */
  429. void memory_region_add_subregion(MemoryRegion *mr,
  430. target_phys_addr_t offset,
  431. MemoryRegion *subregion);
  432. /**
  433. * memory_region_add_subregion: Add a sub-region to a container, with overlap.
  434. *
  435. * Adds a sub-region at @offset. The sub-region may overlap with other
  436. * subregions. Conflicts are resolved by having a higher @priority hide a
  437. * lower @priority. Subregions without priority are taken as @priority 0.
  438. * A region may only be added once as a subregion (unless removed with
  439. * memory_region_del_subregion()); use memory_region_init_alias() if you
  440. * want a region to be a subregion in multiple locations.
  441. *
  442. * @mr: the region to contain the new subregion; must be a container
  443. * initialized with memory_region_init().
  444. * @offset: the offset relative to @mr where @subregion is added.
  445. * @subregion: the subregion to be added.
  446. * @priority: used for resolving overlaps; highest priority wins.
  447. */
  448. void memory_region_add_subregion_overlap(MemoryRegion *mr,
  449. target_phys_addr_t offset,
  450. MemoryRegion *subregion,
  451. unsigned priority);
  452. /**
  453. * memory_region_del_subregion: Remove a subregion.
  454. *
  455. * Removes a subregion from its container.
  456. *
  457. * @mr: the container to be updated.
  458. * @subregion: the region being removed; must be a current subregion of @mr.
  459. */
  460. void memory_region_del_subregion(MemoryRegion *mr,
  461. MemoryRegion *subregion);
  462. /* Start a transaction; changes will be accumulated and made visible only
  463. * when the transaction ends.
  464. */
  465. void memory_region_transaction_begin(void);
  466. /* Commit a transaction and make changes visible to the guest.
  467. */
  468. void memory_region_transaction_commit(void);
  469. void mtree_info(fprintf_function mon_printf, void *f);
  470. #endif
  471. #endif