You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
11KB

  1. /*
  2. * QEMU memory mapping
  3. *
  4. * Copyright Fujitsu, Corp. 2011, 2012
  5. *
  6. * Authors:
  7. * Wen Congyang <wency@cn.fujitsu.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qapi/error.h"
  15. #include "qemu-common.h"
  16. #include "cpu.h"
  17. #include "sysemu/memory_mapping.h"
  18. #include "exec/memory.h"
  19. #include "exec/address-spaces.h"
  20. //#define DEBUG_GUEST_PHYS_REGION_ADD
  21. static void memory_mapping_list_add_mapping_sorted(MemoryMappingList *list,
  22. MemoryMapping *mapping)
  23. {
  24. MemoryMapping *p;
  25. QTAILQ_FOREACH(p, &list->head, next) {
  26. if (p->phys_addr >= mapping->phys_addr) {
  27. QTAILQ_INSERT_BEFORE(p, mapping, next);
  28. return;
  29. }
  30. }
  31. QTAILQ_INSERT_TAIL(&list->head, mapping, next);
  32. }
  33. static void create_new_memory_mapping(MemoryMappingList *list,
  34. hwaddr phys_addr,
  35. hwaddr virt_addr,
  36. ram_addr_t length)
  37. {
  38. MemoryMapping *memory_mapping;
  39. memory_mapping = g_malloc(sizeof(MemoryMapping));
  40. memory_mapping->phys_addr = phys_addr;
  41. memory_mapping->virt_addr = virt_addr;
  42. memory_mapping->length = length;
  43. list->last_mapping = memory_mapping;
  44. list->num++;
  45. memory_mapping_list_add_mapping_sorted(list, memory_mapping);
  46. }
  47. static inline bool mapping_contiguous(MemoryMapping *map,
  48. hwaddr phys_addr,
  49. hwaddr virt_addr)
  50. {
  51. return phys_addr == map->phys_addr + map->length &&
  52. virt_addr == map->virt_addr + map->length;
  53. }
  54. /*
  55. * [map->phys_addr, map->phys_addr + map->length) and
  56. * [phys_addr, phys_addr + length) have intersection?
  57. */
  58. static inline bool mapping_have_same_region(MemoryMapping *map,
  59. hwaddr phys_addr,
  60. ram_addr_t length)
  61. {
  62. return !(phys_addr + length < map->phys_addr ||
  63. phys_addr >= map->phys_addr + map->length);
  64. }
  65. /*
  66. * [map->phys_addr, map->phys_addr + map->length) and
  67. * [phys_addr, phys_addr + length) have intersection. The virtual address in the
  68. * intersection are the same?
  69. */
  70. static inline bool mapping_conflict(MemoryMapping *map,
  71. hwaddr phys_addr,
  72. hwaddr virt_addr)
  73. {
  74. return virt_addr - map->virt_addr != phys_addr - map->phys_addr;
  75. }
  76. /*
  77. * [map->virt_addr, map->virt_addr + map->length) and
  78. * [virt_addr, virt_addr + length) have intersection. And the physical address
  79. * in the intersection are the same.
  80. */
  81. static inline void mapping_merge(MemoryMapping *map,
  82. hwaddr virt_addr,
  83. ram_addr_t length)
  84. {
  85. if (virt_addr < map->virt_addr) {
  86. map->length += map->virt_addr - virt_addr;
  87. map->virt_addr = virt_addr;
  88. }
  89. if ((virt_addr + length) >
  90. (map->virt_addr + map->length)) {
  91. map->length = virt_addr + length - map->virt_addr;
  92. }
  93. }
  94. void memory_mapping_list_add_merge_sorted(MemoryMappingList *list,
  95. hwaddr phys_addr,
  96. hwaddr virt_addr,
  97. ram_addr_t length)
  98. {
  99. MemoryMapping *memory_mapping, *last_mapping;
  100. if (QTAILQ_EMPTY(&list->head)) {
  101. create_new_memory_mapping(list, phys_addr, virt_addr, length);
  102. return;
  103. }
  104. last_mapping = list->last_mapping;
  105. if (last_mapping) {
  106. if (mapping_contiguous(last_mapping, phys_addr, virt_addr)) {
  107. last_mapping->length += length;
  108. return;
  109. }
  110. }
  111. QTAILQ_FOREACH(memory_mapping, &list->head, next) {
  112. if (mapping_contiguous(memory_mapping, phys_addr, virt_addr)) {
  113. memory_mapping->length += length;
  114. list->last_mapping = memory_mapping;
  115. return;
  116. }
  117. if (phys_addr + length < memory_mapping->phys_addr) {
  118. /* create a new region before memory_mapping */
  119. break;
  120. }
  121. if (mapping_have_same_region(memory_mapping, phys_addr, length)) {
  122. if (mapping_conflict(memory_mapping, phys_addr, virt_addr)) {
  123. continue;
  124. }
  125. /* merge this region into memory_mapping */
  126. mapping_merge(memory_mapping, virt_addr, length);
  127. list->last_mapping = memory_mapping;
  128. return;
  129. }
  130. }
  131. /* this region can not be merged into any existed memory mapping. */
  132. create_new_memory_mapping(list, phys_addr, virt_addr, length);
  133. }
  134. void memory_mapping_list_free(MemoryMappingList *list)
  135. {
  136. MemoryMapping *p, *q;
  137. QTAILQ_FOREACH_SAFE(p, &list->head, next, q) {
  138. QTAILQ_REMOVE(&list->head, p, next);
  139. g_free(p);
  140. }
  141. list->num = 0;
  142. list->last_mapping = NULL;
  143. }
  144. void memory_mapping_list_init(MemoryMappingList *list)
  145. {
  146. list->num = 0;
  147. list->last_mapping = NULL;
  148. QTAILQ_INIT(&list->head);
  149. }
  150. void guest_phys_blocks_free(GuestPhysBlockList *list)
  151. {
  152. GuestPhysBlock *p, *q;
  153. QTAILQ_FOREACH_SAFE(p, &list->head, next, q) {
  154. QTAILQ_REMOVE(&list->head, p, next);
  155. memory_region_unref(p->mr);
  156. g_free(p);
  157. }
  158. list->num = 0;
  159. }
  160. void guest_phys_blocks_init(GuestPhysBlockList *list)
  161. {
  162. list->num = 0;
  163. QTAILQ_INIT(&list->head);
  164. }
  165. typedef struct GuestPhysListener {
  166. GuestPhysBlockList *list;
  167. MemoryListener listener;
  168. } GuestPhysListener;
  169. static void guest_phys_blocks_region_add(MemoryListener *listener,
  170. MemoryRegionSection *section)
  171. {
  172. GuestPhysListener *g;
  173. uint64_t section_size;
  174. hwaddr target_start, target_end;
  175. uint8_t *host_addr;
  176. GuestPhysBlock *predecessor;
  177. /* we only care about RAM */
  178. if (!memory_region_is_ram(section->mr) ||
  179. memory_region_is_ram_device(section->mr) ||
  180. memory_region_is_nonvolatile(section->mr)) {
  181. return;
  182. }
  183. g = container_of(listener, GuestPhysListener, listener);
  184. section_size = int128_get64(section->size);
  185. target_start = section->offset_within_address_space;
  186. target_end = target_start + section_size;
  187. host_addr = memory_region_get_ram_ptr(section->mr) +
  188. section->offset_within_region;
  189. predecessor = NULL;
  190. /* find continuity in guest physical address space */
  191. if (!QTAILQ_EMPTY(&g->list->head)) {
  192. hwaddr predecessor_size;
  193. predecessor = QTAILQ_LAST(&g->list->head);
  194. predecessor_size = predecessor->target_end - predecessor->target_start;
  195. /* the memory API guarantees monotonically increasing traversal */
  196. g_assert(predecessor->target_end <= target_start);
  197. /* we want continuity in both guest-physical and host-virtual memory */
  198. if (predecessor->target_end < target_start ||
  199. predecessor->host_addr + predecessor_size != host_addr) {
  200. predecessor = NULL;
  201. }
  202. }
  203. if (predecessor == NULL) {
  204. /* isolated mapping, allocate it and add it to the list */
  205. GuestPhysBlock *block = g_malloc0(sizeof *block);
  206. block->target_start = target_start;
  207. block->target_end = target_end;
  208. block->host_addr = host_addr;
  209. block->mr = section->mr;
  210. memory_region_ref(section->mr);
  211. QTAILQ_INSERT_TAIL(&g->list->head, block, next);
  212. ++g->list->num;
  213. } else {
  214. /* expand predecessor until @target_end; predecessor's start doesn't
  215. * change
  216. */
  217. predecessor->target_end = target_end;
  218. }
  219. #ifdef DEBUG_GUEST_PHYS_REGION_ADD
  220. fprintf(stderr, "%s: target_start=" TARGET_FMT_plx " target_end="
  221. TARGET_FMT_plx ": %s (count: %u)\n", __func__, target_start,
  222. target_end, predecessor ? "joined" : "added", g->list->num);
  223. #endif
  224. }
  225. void guest_phys_blocks_append(GuestPhysBlockList *list)
  226. {
  227. GuestPhysListener g = { 0 };
  228. g.list = list;
  229. g.listener.region_add = &guest_phys_blocks_region_add;
  230. memory_listener_register(&g.listener, &address_space_memory);
  231. memory_listener_unregister(&g.listener);
  232. }
  233. static CPUState *find_paging_enabled_cpu(CPUState *start_cpu)
  234. {
  235. CPUState *cpu;
  236. CPU_FOREACH(cpu) {
  237. if (cpu_paging_enabled(cpu)) {
  238. return cpu;
  239. }
  240. }
  241. return NULL;
  242. }
  243. void qemu_get_guest_memory_mapping(MemoryMappingList *list,
  244. const GuestPhysBlockList *guest_phys_blocks,
  245. Error **errp)
  246. {
  247. CPUState *cpu, *first_paging_enabled_cpu;
  248. GuestPhysBlock *block;
  249. ram_addr_t offset, length;
  250. first_paging_enabled_cpu = find_paging_enabled_cpu(first_cpu);
  251. if (first_paging_enabled_cpu) {
  252. for (cpu = first_paging_enabled_cpu; cpu != NULL;
  253. cpu = CPU_NEXT(cpu)) {
  254. Error *err = NULL;
  255. cpu_get_memory_mapping(cpu, list, &err);
  256. if (err) {
  257. error_propagate(errp, err);
  258. return;
  259. }
  260. }
  261. return;
  262. }
  263. /*
  264. * If the guest doesn't use paging, the virtual address is equal to physical
  265. * address.
  266. */
  267. QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
  268. offset = block->target_start;
  269. length = block->target_end - block->target_start;
  270. create_new_memory_mapping(list, offset, offset, length);
  271. }
  272. }
  273. void qemu_get_guest_simple_memory_mapping(MemoryMappingList *list,
  274. const GuestPhysBlockList *guest_phys_blocks)
  275. {
  276. GuestPhysBlock *block;
  277. QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
  278. create_new_memory_mapping(list, block->target_start, 0,
  279. block->target_end - block->target_start);
  280. }
  281. }
  282. void memory_mapping_filter(MemoryMappingList *list, int64_t begin,
  283. int64_t length)
  284. {
  285. MemoryMapping *cur, *next;
  286. QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) {
  287. if (cur->phys_addr >= begin + length ||
  288. cur->phys_addr + cur->length <= begin) {
  289. QTAILQ_REMOVE(&list->head, cur, next);
  290. g_free(cur);
  291. list->num--;
  292. continue;
  293. }
  294. if (cur->phys_addr < begin) {
  295. cur->length -= begin - cur->phys_addr;
  296. if (cur->virt_addr) {
  297. cur->virt_addr += begin - cur->phys_addr;
  298. }
  299. cur->phys_addr = begin;
  300. }
  301. if (cur->phys_addr + cur->length > begin + length) {
  302. cur->length -= cur->phys_addr + cur->length - begin - length;
  303. }
  304. }
  305. }