xref: /openbmc/qemu/system/physmem.c (revision 44a7e0aaf7b59e90ae50c0a9149a0bfc990d7906)
1 /*
2  * RAM allocation and memory access
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "exec/page-vary.h"
22 #include "qapi/error.h"
23 
24 #include "qemu/cutils.h"
25 #include "qemu/cacheflush.h"
26 #include "qemu/hbitmap.h"
27 #include "qemu/madvise.h"
28 #include "qemu/lockable.h"
29 
30 #ifdef CONFIG_TCG
31 #include "hw/core/tcg-cpu-ops.h"
32 #endif /* CONFIG_TCG */
33 
34 #include "exec/exec-all.h"
35 #include "exec/page-protection.h"
36 #include "exec/target_page.h"
37 #include "hw/qdev-core.h"
38 #include "hw/qdev-properties.h"
39 #include "hw/boards.h"
40 #include "sysemu/xen.h"
41 #include "sysemu/kvm.h"
42 #include "sysemu/tcg.h"
43 #include "sysemu/qtest.h"
44 #include "qemu/timer.h"
45 #include "qemu/config-file.h"
46 #include "qemu/error-report.h"
47 #include "qemu/qemu-print.h"
48 #include "qemu/log.h"
49 #include "qemu/memalign.h"
50 #include "exec/memory.h"
51 #include "exec/ioport.h"
52 #include "sysemu/dma.h"
53 #include "sysemu/hostmem.h"
54 #include "sysemu/hw_accel.h"
55 #include "sysemu/xen-mapcache.h"
56 #include "trace.h"
57 
58 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
59 #include <linux/falloc.h>
60 #endif
61 
62 #include "qemu/rcu_queue.h"
63 #include "qemu/main-loop.h"
64 #include "exec/translate-all.h"
65 #include "sysemu/replay.h"
66 
67 #include "exec/memory-internal.h"
68 #include "exec/ram_addr.h"
69 
70 #include "qemu/pmem.h"
71 
72 #include "migration/vmstate.h"
73 
74 #include "qemu/range.h"
75 #ifndef _WIN32
76 #include "qemu/mmap-alloc.h"
77 #endif
78 
79 #include "monitor/monitor.h"
80 
81 #ifdef CONFIG_LIBDAXCTL
82 #include <daxctl/libdaxctl.h>
83 #endif
84 
85 //#define DEBUG_SUBPAGE
86 
87 /* ram_list is read under rcu_read_lock()/rcu_read_unlock().  Writes
88  * are protected by the ramlist lock.
89  */
90 RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list.blocks) };
91 
92 static MemoryRegion *system_memory;
93 static MemoryRegion *system_io;
94 
95 AddressSpace address_space_io;
96 AddressSpace address_space_memory;
97 
98 static MemoryRegion io_mem_unassigned;
99 
100 typedef struct PhysPageEntry PhysPageEntry;
101 
102 struct PhysPageEntry {
103     /* How many bits skip to next level (in units of L2_SIZE). 0 for a leaf. */
104     uint32_t skip : 6;
105      /* index into phys_sections (!skip) or phys_map_nodes (skip) */
106     uint32_t ptr : 26;
107 };
108 
109 #define PHYS_MAP_NODE_NIL (((uint32_t)~0) >> 6)
110 
111 /* Size of the L2 (and L3, etc) page tables.  */
112 #define ADDR_SPACE_BITS 64
113 
114 #define P_L2_BITS 9
115 #define P_L2_SIZE (1 << P_L2_BITS)
116 
117 #define P_L2_LEVELS (((ADDR_SPACE_BITS - TARGET_PAGE_BITS - 1) / P_L2_BITS) + 1)
118 
119 typedef PhysPageEntry Node[P_L2_SIZE];
120 
121 typedef struct PhysPageMap {
122     struct rcu_head rcu;
123 
124     unsigned sections_nb;
125     unsigned sections_nb_alloc;
126     unsigned nodes_nb;
127     unsigned nodes_nb_alloc;
128     Node *nodes;
129     MemoryRegionSection *sections;
130 } PhysPageMap;
131 
132 struct AddressSpaceDispatch {
133     MemoryRegionSection *mru_section;
134     /* This is a multi-level map on the physical address space.
135      * The bottom level has pointers to MemoryRegionSections.
136      */
137     PhysPageEntry phys_map;
138     PhysPageMap map;
139 };
140 
141 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
142 typedef struct subpage_t {
143     MemoryRegion iomem;
144     FlatView *fv;
145     hwaddr base;
146     uint16_t sub_section[];
147 } subpage_t;
148 
149 #define PHYS_SECTION_UNASSIGNED 0
150 
151 static void io_mem_init(void);
152 static void memory_map_init(void);
153 static void tcg_log_global_after_sync(MemoryListener *listener);
154 static void tcg_commit(MemoryListener *listener);
155 
156 /**
157  * CPUAddressSpace: all the information a CPU needs about an AddressSpace
158  * @cpu: the CPU whose AddressSpace this is
159  * @as: the AddressSpace itself
160  * @memory_dispatch: its dispatch pointer (cached, RCU protected)
161  * @tcg_as_listener: listener for tracking changes to the AddressSpace
162  */
163 typedef struct CPUAddressSpace {
164     CPUState *cpu;
165     AddressSpace *as;
166     struct AddressSpaceDispatch *memory_dispatch;
167     MemoryListener tcg_as_listener;
168 } CPUAddressSpace;
169 
170 struct DirtyBitmapSnapshot {
171     ram_addr_t start;
172     ram_addr_t end;
173     unsigned long dirty[];
174 };
175 
phys_map_node_reserve(PhysPageMap * map,unsigned nodes)176 static void phys_map_node_reserve(PhysPageMap *map, unsigned nodes)
177 {
178     static unsigned alloc_hint = 16;
179     if (map->nodes_nb + nodes > map->nodes_nb_alloc) {
180         map->nodes_nb_alloc = MAX(alloc_hint, map->nodes_nb + nodes);
181         map->nodes = g_renew(Node, map->nodes, map->nodes_nb_alloc);
182         alloc_hint = map->nodes_nb_alloc;
183     }
184 }
185 
phys_map_node_alloc(PhysPageMap * map,bool leaf)186 static uint32_t phys_map_node_alloc(PhysPageMap *map, bool leaf)
187 {
188     unsigned i;
189     uint32_t ret;
190     PhysPageEntry e;
191     PhysPageEntry *p;
192 
193     ret = map->nodes_nb++;
194     p = map->nodes[ret];
195     assert(ret != PHYS_MAP_NODE_NIL);
196     assert(ret != map->nodes_nb_alloc);
197 
198     e.skip = leaf ? 0 : 1;
199     e.ptr = leaf ? PHYS_SECTION_UNASSIGNED : PHYS_MAP_NODE_NIL;
200     for (i = 0; i < P_L2_SIZE; ++i) {
201         memcpy(&p[i], &e, sizeof(e));
202     }
203     return ret;
204 }
205 
phys_page_set_level(PhysPageMap * map,PhysPageEntry * lp,hwaddr * index,uint64_t * nb,uint16_t leaf,int level)206 static void phys_page_set_level(PhysPageMap *map, PhysPageEntry *lp,
207                                 hwaddr *index, uint64_t *nb, uint16_t leaf,
208                                 int level)
209 {
210     PhysPageEntry *p;
211     hwaddr step = (hwaddr)1 << (level * P_L2_BITS);
212 
213     if (lp->skip && lp->ptr == PHYS_MAP_NODE_NIL) {
214         lp->ptr = phys_map_node_alloc(map, level == 0);
215     }
216     p = map->nodes[lp->ptr];
217     lp = &p[(*index >> (level * P_L2_BITS)) & (P_L2_SIZE - 1)];
218 
219     while (*nb && lp < &p[P_L2_SIZE]) {
220         if ((*index & (step - 1)) == 0 && *nb >= step) {
221             lp->skip = 0;
222             lp->ptr = leaf;
223             *index += step;
224             *nb -= step;
225         } else {
226             phys_page_set_level(map, lp, index, nb, leaf, level - 1);
227         }
228         ++lp;
229     }
230 }
231 
phys_page_set(AddressSpaceDispatch * d,hwaddr index,uint64_t nb,uint16_t leaf)232 static void phys_page_set(AddressSpaceDispatch *d,
233                           hwaddr index, uint64_t nb,
234                           uint16_t leaf)
235 {
236     /* Wildly overreserve - it doesn't matter much. */
237     phys_map_node_reserve(&d->map, 3 * P_L2_LEVELS);
238 
239     phys_page_set_level(&d->map, &d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
240 }
241 
242 /* Compact a non leaf page entry. Simply detect that the entry has a single child,
243  * and update our entry so we can skip it and go directly to the destination.
244  */
phys_page_compact(PhysPageEntry * lp,Node * nodes)245 static void phys_page_compact(PhysPageEntry *lp, Node *nodes)
246 {
247     unsigned valid_ptr = P_L2_SIZE;
248     int valid = 0;
249     PhysPageEntry *p;
250     int i;
251 
252     if (lp->ptr == PHYS_MAP_NODE_NIL) {
253         return;
254     }
255 
256     p = nodes[lp->ptr];
257     for (i = 0; i < P_L2_SIZE; i++) {
258         if (p[i].ptr == PHYS_MAP_NODE_NIL) {
259             continue;
260         }
261 
262         valid_ptr = i;
263         valid++;
264         if (p[i].skip) {
265             phys_page_compact(&p[i], nodes);
266         }
267     }
268 
269     /* We can only compress if there's only one child. */
270     if (valid != 1) {
271         return;
272     }
273 
274     assert(valid_ptr < P_L2_SIZE);
275 
276     /* Don't compress if it won't fit in the # of bits we have. */
277     if (P_L2_LEVELS >= (1 << 6) &&
278         lp->skip + p[valid_ptr].skip >= (1 << 6)) {
279         return;
280     }
281 
282     lp->ptr = p[valid_ptr].ptr;
283     if (!p[valid_ptr].skip) {
284         /* If our only child is a leaf, make this a leaf. */
285         /* By design, we should have made this node a leaf to begin with so we
286          * should never reach here.
287          * But since it's so simple to handle this, let's do it just in case we
288          * change this rule.
289          */
290         lp->skip = 0;
291     } else {
292         lp->skip += p[valid_ptr].skip;
293     }
294 }
295 
address_space_dispatch_compact(AddressSpaceDispatch * d)296 void address_space_dispatch_compact(AddressSpaceDispatch *d)
297 {
298     if (d->phys_map.skip) {
299         phys_page_compact(&d->phys_map, d->map.nodes);
300     }
301 }
302 
section_covers_addr(const MemoryRegionSection * section,hwaddr addr)303 static inline bool section_covers_addr(const MemoryRegionSection *section,
304                                        hwaddr addr)
305 {
306     /* Memory topology clips a memory region to [0, 2^64); size.hi > 0 means
307      * the section must cover the entire address space.
308      */
309     return int128_gethi(section->size) ||
310            range_covers_byte(section->offset_within_address_space,
311                              int128_getlo(section->size), addr);
312 }
313 
phys_page_find(AddressSpaceDispatch * d,hwaddr addr)314 static MemoryRegionSection *phys_page_find(AddressSpaceDispatch *d, hwaddr addr)
315 {
316     PhysPageEntry lp = d->phys_map, *p;
317     Node *nodes = d->map.nodes;
318     MemoryRegionSection *sections = d->map.sections;
319     hwaddr index = addr >> TARGET_PAGE_BITS;
320     int i;
321 
322     for (i = P_L2_LEVELS; lp.skip && (i -= lp.skip) >= 0;) {
323         if (lp.ptr == PHYS_MAP_NODE_NIL) {
324             return &sections[PHYS_SECTION_UNASSIGNED];
325         }
326         p = nodes[lp.ptr];
327         lp = p[(index >> (i * P_L2_BITS)) & (P_L2_SIZE - 1)];
328     }
329 
330     if (section_covers_addr(&sections[lp.ptr], addr)) {
331         return &sections[lp.ptr];
332     } else {
333         return &sections[PHYS_SECTION_UNASSIGNED];
334     }
335 }
336 
337 /* Called from RCU critical section */
address_space_lookup_region(AddressSpaceDispatch * d,hwaddr addr,bool resolve_subpage)338 static MemoryRegionSection *address_space_lookup_region(AddressSpaceDispatch *d,
339                                                         hwaddr addr,
340                                                         bool resolve_subpage)
341 {
342     MemoryRegionSection *section = qatomic_read(&d->mru_section);
343     subpage_t *subpage;
344 
345     if (!section || section == &d->map.sections[PHYS_SECTION_UNASSIGNED] ||
346         !section_covers_addr(section, addr)) {
347         section = phys_page_find(d, addr);
348         qatomic_set(&d->mru_section, section);
349     }
350     if (resolve_subpage && section->mr->subpage) {
351         subpage = container_of(section->mr, subpage_t, iomem);
352         section = &d->map.sections[subpage->sub_section[SUBPAGE_IDX(addr)]];
353     }
354     return section;
355 }
356 
357 /* Called from RCU critical section */
358 static MemoryRegionSection *
address_space_translate_internal(AddressSpaceDispatch * d,hwaddr addr,hwaddr * xlat,hwaddr * plen,bool resolve_subpage)359 address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *xlat,
360                                  hwaddr *plen, bool resolve_subpage)
361 {
362     MemoryRegionSection *section;
363     MemoryRegion *mr;
364     Int128 diff;
365 
366     section = address_space_lookup_region(d, addr, resolve_subpage);
367     /* Compute offset within MemoryRegionSection */
368     addr -= section->offset_within_address_space;
369 
370     /* Compute offset within MemoryRegion */
371     *xlat = addr + section->offset_within_region;
372 
373     mr = section->mr;
374 
375     /* MMIO registers can be expected to perform full-width accesses based only
376      * on their address, without considering adjacent registers that could
377      * decode to completely different MemoryRegions.  When such registers
378      * exist (e.g. I/O ports 0xcf8 and 0xcf9 on most PC chipsets), MMIO
379      * regions overlap wildly.  For this reason we cannot clamp the accesses
380      * here.
381      *
382      * If the length is small (as is the case for address_space_ldl/stl),
383      * everything works fine.  If the incoming length is large, however,
384      * the caller really has to do the clamping through memory_access_size.
385      */
386     if (memory_region_is_ram(mr)) {
387         diff = int128_sub(section->size, int128_make64(addr));
388         *plen = int128_get64(int128_min(diff, int128_make64(*plen)));
389     }
390     return section;
391 }
392 
393 /**
394  * address_space_translate_iommu - translate an address through an IOMMU
395  * memory region and then through the target address space.
396  *
397  * @iommu_mr: the IOMMU memory region that we start the translation from
398  * @addr: the address to be translated through the MMU
399  * @xlat: the translated address offset within the destination memory region.
400  *        It cannot be %NULL.
401  * @plen_out: valid read/write length of the translated address. It
402  *            cannot be %NULL.
403  * @page_mask_out: page mask for the translated address. This
404  *            should only be meaningful for IOMMU translated
405  *            addresses, since there may be huge pages that this bit
406  *            would tell. It can be %NULL if we don't care about it.
407  * @is_write: whether the translation operation is for write
408  * @is_mmio: whether this can be MMIO, set true if it can
409  * @target_as: the address space targeted by the IOMMU
410  * @attrs: transaction attributes
411  *
412  * This function is called from RCU critical section.  It is the common
413  * part of flatview_do_translate and address_space_translate_cached.
414  */
address_space_translate_iommu(IOMMUMemoryRegion * iommu_mr,hwaddr * xlat,hwaddr * plen_out,hwaddr * page_mask_out,bool is_write,bool is_mmio,AddressSpace ** target_as,MemTxAttrs attrs)415 static MemoryRegionSection address_space_translate_iommu(IOMMUMemoryRegion *iommu_mr,
416                                                          hwaddr *xlat,
417                                                          hwaddr *plen_out,
418                                                          hwaddr *page_mask_out,
419                                                          bool is_write,
420                                                          bool is_mmio,
421                                                          AddressSpace **target_as,
422                                                          MemTxAttrs attrs)
423 {
424     MemoryRegionSection *section;
425     hwaddr page_mask = (hwaddr)-1;
426 
427     do {
428         hwaddr addr = *xlat;
429         IOMMUMemoryRegionClass *imrc = memory_region_get_iommu_class_nocheck(iommu_mr);
430         int iommu_idx = 0;
431         IOMMUTLBEntry iotlb;
432 
433         if (imrc->attrs_to_index) {
434             iommu_idx = imrc->attrs_to_index(iommu_mr, attrs);
435         }
436 
437         iotlb = imrc->translate(iommu_mr, addr, is_write ?
438                                 IOMMU_WO : IOMMU_RO, iommu_idx);
439 
440         if (!(iotlb.perm & (1 << is_write))) {
441             goto unassigned;
442         }
443 
444         addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
445                 | (addr & iotlb.addr_mask));
446         page_mask &= iotlb.addr_mask;
447         *plen_out = MIN(*plen_out, (addr | iotlb.addr_mask) - addr + 1);
448         *target_as = iotlb.target_as;
449 
450         section = address_space_translate_internal(
451                 address_space_to_dispatch(iotlb.target_as), addr, xlat,
452                 plen_out, is_mmio);
453 
454         iommu_mr = memory_region_get_iommu(section->mr);
455     } while (unlikely(iommu_mr));
456 
457     if (page_mask_out) {
458         *page_mask_out = page_mask;
459     }
460     return *section;
461 
462 unassigned:
463     return (MemoryRegionSection) { .mr = &io_mem_unassigned };
464 }
465 
466 /**
467  * flatview_do_translate - translate an address in FlatView
468  *
469  * @fv: the flat view that we want to translate on
470  * @addr: the address to be translated in above address space
471  * @xlat: the translated address offset within memory region. It
472  *        cannot be @NULL.
473  * @plen_out: valid read/write length of the translated address. It
474  *            can be @NULL when we don't care about it.
475  * @page_mask_out: page mask for the translated address. This
476  *            should only be meaningful for IOMMU translated
477  *            addresses, since there may be huge pages that this bit
478  *            would tell. It can be @NULL if we don't care about it.
479  * @is_write: whether the translation operation is for write
480  * @is_mmio: whether this can be MMIO, set true if it can
481  * @target_as: the address space targeted by the IOMMU
482  * @attrs: memory transaction attributes
483  *
484  * This function is called from RCU critical section
485  */
flatview_do_translate(FlatView * fv,hwaddr addr,hwaddr * xlat,hwaddr * plen_out,hwaddr * page_mask_out,bool is_write,bool is_mmio,AddressSpace ** target_as,MemTxAttrs attrs)486 static MemoryRegionSection flatview_do_translate(FlatView *fv,
487                                                  hwaddr addr,
488                                                  hwaddr *xlat,
489                                                  hwaddr *plen_out,
490                                                  hwaddr *page_mask_out,
491                                                  bool is_write,
492                                                  bool is_mmio,
493                                                  AddressSpace **target_as,
494                                                  MemTxAttrs attrs)
495 {
496     MemoryRegionSection *section;
497     IOMMUMemoryRegion *iommu_mr;
498     hwaddr plen = (hwaddr)(-1);
499 
500     if (!plen_out) {
501         plen_out = &plen;
502     }
503 
504     section = address_space_translate_internal(
505             flatview_to_dispatch(fv), addr, xlat,
506             plen_out, is_mmio);
507 
508     iommu_mr = memory_region_get_iommu(section->mr);
509     if (unlikely(iommu_mr)) {
510         return address_space_translate_iommu(iommu_mr, xlat,
511                                              plen_out, page_mask_out,
512                                              is_write, is_mmio,
513                                              target_as, attrs);
514     }
515     if (page_mask_out) {
516         /* Not behind an IOMMU, use default page size. */
517         *page_mask_out = ~TARGET_PAGE_MASK;
518     }
519 
520     return *section;
521 }
522 
523 /* Called from RCU critical section */
address_space_get_iotlb_entry(AddressSpace * as,hwaddr addr,bool is_write,MemTxAttrs attrs)524 IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
525                                             bool is_write, MemTxAttrs attrs)
526 {
527     MemoryRegionSection section;
528     hwaddr xlat, page_mask;
529 
530     /*
531      * This can never be MMIO, and we don't really care about plen,
532      * but page mask.
533      */
534     section = flatview_do_translate(address_space_to_flatview(as), addr, &xlat,
535                                     NULL, &page_mask, is_write, false, &as,
536                                     attrs);
537 
538     /* Illegal translation */
539     if (section.mr == &io_mem_unassigned) {
540         goto iotlb_fail;
541     }
542 
543     /* Convert memory region offset into address space offset */
544     xlat += section.offset_within_address_space -
545         section.offset_within_region;
546 
547     return (IOMMUTLBEntry) {
548         .target_as = as,
549         .iova = addr & ~page_mask,
550         .translated_addr = xlat & ~page_mask,
551         .addr_mask = page_mask,
552         /* IOTLBs are for DMAs, and DMA only allows on RAMs. */
553         .perm = IOMMU_RW,
554     };
555 
556 iotlb_fail:
557     return (IOMMUTLBEntry) {0};
558 }
559 
560 /* Called from RCU critical section */
flatview_translate(FlatView * fv,hwaddr addr,hwaddr * xlat,hwaddr * plen,bool is_write,MemTxAttrs attrs)561 MemoryRegion *flatview_translate(FlatView *fv, hwaddr addr, hwaddr *xlat,
562                                  hwaddr *plen, bool is_write,
563                                  MemTxAttrs attrs)
564 {
565     MemoryRegion *mr;
566     MemoryRegionSection section;
567     AddressSpace *as = NULL;
568 
569     /* This can be MMIO, so setup MMIO bit. */
570     section = flatview_do_translate(fv, addr, xlat, plen, NULL,
571                                     is_write, true, &as, attrs);
572     mr = section.mr;
573 
574     if (xen_enabled() && memory_access_is_direct(mr, is_write)) {
575         hwaddr page = ((addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE) - addr;
576         *plen = MIN(page, *plen);
577     }
578 
579     return mr;
580 }
581 
582 typedef struct TCGIOMMUNotifier {
583     IOMMUNotifier n;
584     MemoryRegion *mr;
585     CPUState *cpu;
586     int iommu_idx;
587     bool active;
588 } TCGIOMMUNotifier;
589 
tcg_iommu_unmap_notify(IOMMUNotifier * n,IOMMUTLBEntry * iotlb)590 static void tcg_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
591 {
592     TCGIOMMUNotifier *notifier = container_of(n, TCGIOMMUNotifier, n);
593 
594     if (!notifier->active) {
595         return;
596     }
597     tlb_flush(notifier->cpu);
598     notifier->active = false;
599     /* We leave the notifier struct on the list to avoid reallocating it later.
600      * Generally the number of IOMMUs a CPU deals with will be small.
601      * In any case we can't unregister the iommu notifier from a notify
602      * callback.
603      */
604 }
605 
tcg_register_iommu_notifier(CPUState * cpu,IOMMUMemoryRegion * iommu_mr,int iommu_idx)606 static void tcg_register_iommu_notifier(CPUState *cpu,
607                                         IOMMUMemoryRegion *iommu_mr,
608                                         int iommu_idx)
609 {
610     /* Make sure this CPU has an IOMMU notifier registered for this
611      * IOMMU/IOMMU index combination, so that we can flush its TLB
612      * when the IOMMU tells us the mappings we've cached have changed.
613      */
614     MemoryRegion *mr = MEMORY_REGION(iommu_mr);
615     TCGIOMMUNotifier *notifier = NULL;
616     int i;
617 
618     for (i = 0; i < cpu->iommu_notifiers->len; i++) {
619         notifier = g_array_index(cpu->iommu_notifiers, TCGIOMMUNotifier *, i);
620         if (notifier->mr == mr && notifier->iommu_idx == iommu_idx) {
621             break;
622         }
623     }
624     if (i == cpu->iommu_notifiers->len) {
625         /* Not found, add a new entry at the end of the array */
626         cpu->iommu_notifiers = g_array_set_size(cpu->iommu_notifiers, i + 1);
627         notifier = g_new0(TCGIOMMUNotifier, 1);
628         g_array_index(cpu->iommu_notifiers, TCGIOMMUNotifier *, i) = notifier;
629 
630         notifier->mr = mr;
631         notifier->iommu_idx = iommu_idx;
632         notifier->cpu = cpu;
633         /* Rather than trying to register interest in the specific part
634          * of the iommu's address space that we've accessed and then
635          * expand it later as subsequent accesses touch more of it, we
636          * just register interest in the whole thing, on the assumption
637          * that iommu reconfiguration will be rare.
638          */
639         iommu_notifier_init(&notifier->n,
640                             tcg_iommu_unmap_notify,
641                             IOMMU_NOTIFIER_UNMAP,
642                             0,
643                             HWADDR_MAX,
644                             iommu_idx);
645         memory_region_register_iommu_notifier(notifier->mr, &notifier->n,
646                                               &error_fatal);
647     }
648 
649     if (!notifier->active) {
650         notifier->active = true;
651     }
652 }
653 
tcg_iommu_free_notifier_list(CPUState * cpu)654 void tcg_iommu_free_notifier_list(CPUState *cpu)
655 {
656     /* Destroy the CPU's notifier list */
657     int i;
658     TCGIOMMUNotifier *notifier;
659 
660     for (i = 0; i < cpu->iommu_notifiers->len; i++) {
661         notifier = g_array_index(cpu->iommu_notifiers, TCGIOMMUNotifier *, i);
662         memory_region_unregister_iommu_notifier(notifier->mr, &notifier->n);
663         g_free(notifier);
664     }
665     g_array_free(cpu->iommu_notifiers, true);
666 }
667 
tcg_iommu_init_notifier_list(CPUState * cpu)668 void tcg_iommu_init_notifier_list(CPUState *cpu)
669 {
670     cpu->iommu_notifiers = g_array_new(false, true, sizeof(TCGIOMMUNotifier *));
671 }
672 
673 /* Called from RCU critical section */
674 MemoryRegionSection *
address_space_translate_for_iotlb(CPUState * cpu,int asidx,hwaddr orig_addr,hwaddr * xlat,hwaddr * plen,MemTxAttrs attrs,int * prot)675 address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr orig_addr,
676                                   hwaddr *xlat, hwaddr *plen,
677                                   MemTxAttrs attrs, int *prot)
678 {
679     MemoryRegionSection *section;
680     IOMMUMemoryRegion *iommu_mr;
681     IOMMUMemoryRegionClass *imrc;
682     IOMMUTLBEntry iotlb;
683     int iommu_idx;
684     hwaddr addr = orig_addr;
685     AddressSpaceDispatch *d = cpu->cpu_ases[asidx].memory_dispatch;
686 
687     for (;;) {
688         section = address_space_translate_internal(d, addr, &addr, plen, false);
689 
690         iommu_mr = memory_region_get_iommu(section->mr);
691         if (!iommu_mr) {
692             break;
693         }
694 
695         imrc = memory_region_get_iommu_class_nocheck(iommu_mr);
696 
697         iommu_idx = imrc->attrs_to_index(iommu_mr, attrs);
698         tcg_register_iommu_notifier(cpu, iommu_mr, iommu_idx);
699         /* We need all the permissions, so pass IOMMU_NONE so the IOMMU
700          * doesn't short-cut its translation table walk.
701          */
702         iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE, iommu_idx);
703         addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
704                 | (addr & iotlb.addr_mask));
705         /* Update the caller's prot bits to remove permissions the IOMMU
706          * is giving us a failure response for. If we get down to no
707          * permissions left at all we can give up now.
708          */
709         if (!(iotlb.perm & IOMMU_RO)) {
710             *prot &= ~(PAGE_READ | PAGE_EXEC);
711         }
712         if (!(iotlb.perm & IOMMU_WO)) {
713             *prot &= ~PAGE_WRITE;
714         }
715 
716         if (!*prot) {
717             goto translate_fail;
718         }
719 
720         d = flatview_to_dispatch(address_space_to_flatview(iotlb.target_as));
721     }
722 
723     assert(!memory_region_is_iommu(section->mr));
724     *xlat = addr;
725     return section;
726 
727 translate_fail:
728     /*
729      * We should be given a page-aligned address -- certainly
730      * tlb_set_page_with_attrs() does so.  The page offset of xlat
731      * is used to index sections[], and PHYS_SECTION_UNASSIGNED = 0.
732      * The page portion of xlat will be logged by memory_region_access_valid()
733      * when this memory access is rejected, so use the original untranslated
734      * physical address.
735      */
736     assert((orig_addr & ~TARGET_PAGE_MASK) == 0);
737     *xlat = orig_addr;
738     return &d->map.sections[PHYS_SECTION_UNASSIGNED];
739 }
740 
cpu_address_space_init(CPUState * cpu,int asidx,const char * prefix,MemoryRegion * mr)741 void cpu_address_space_init(CPUState *cpu, int asidx,
742                             const char *prefix, MemoryRegion *mr)
743 {
744     CPUAddressSpace *newas;
745     AddressSpace *as = g_new0(AddressSpace, 1);
746     char *as_name;
747 
748     assert(mr);
749     as_name = g_strdup_printf("%s-%d", prefix, cpu->cpu_index);
750     address_space_init(as, mr, as_name);
751     g_free(as_name);
752 
753     /* Target code should have set num_ases before calling us */
754     assert(asidx < cpu->num_ases);
755 
756     if (asidx == 0) {
757         /* address space 0 gets the convenience alias */
758         cpu->as = as;
759     }
760 
761     /* KVM cannot currently support multiple address spaces. */
762     assert(asidx == 0 || !kvm_enabled());
763 
764     if (!cpu->cpu_ases) {
765         cpu->cpu_ases = g_new0(CPUAddressSpace, cpu->num_ases);
766         cpu->cpu_ases_count = cpu->num_ases;
767     }
768 
769     newas = &cpu->cpu_ases[asidx];
770     newas->cpu = cpu;
771     newas->as = as;
772     if (tcg_enabled()) {
773         newas->tcg_as_listener.log_global_after_sync = tcg_log_global_after_sync;
774         newas->tcg_as_listener.commit = tcg_commit;
775         newas->tcg_as_listener.name = "tcg";
776         memory_listener_register(&newas->tcg_as_listener, as);
777     }
778 }
779 
cpu_address_space_destroy(CPUState * cpu,int asidx)780 void cpu_address_space_destroy(CPUState *cpu, int asidx)
781 {
782     CPUAddressSpace *cpuas;
783 
784     assert(cpu->cpu_ases);
785     assert(asidx >= 0 && asidx < cpu->num_ases);
786     /* KVM cannot currently support multiple address spaces. */
787     assert(asidx == 0 || !kvm_enabled());
788 
789     cpuas = &cpu->cpu_ases[asidx];
790     if (tcg_enabled()) {
791         memory_listener_unregister(&cpuas->tcg_as_listener);
792     }
793 
794     address_space_destroy(cpuas->as);
795     g_free_rcu(cpuas->as, rcu);
796 
797     if (asidx == 0) {
798         /* reset the convenience alias for address space 0 */
799         cpu->as = NULL;
800     }
801 
802     if (--cpu->cpu_ases_count == 0) {
803         g_free(cpu->cpu_ases);
804         cpu->cpu_ases = NULL;
805     }
806 }
807 
cpu_get_address_space(CPUState * cpu,int asidx)808 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx)
809 {
810     /* Return the AddressSpace corresponding to the specified index */
811     return cpu->cpu_ases[asidx].as;
812 }
813 
814 /* Called from RCU critical section */
qemu_get_ram_block(ram_addr_t addr)815 static RAMBlock *qemu_get_ram_block(ram_addr_t addr)
816 {
817     RAMBlock *block;
818 
819     block = qatomic_rcu_read(&ram_list.mru_block);
820     if (block && addr - block->offset < block->max_length) {
821         return block;
822     }
823     RAMBLOCK_FOREACH(block) {
824         if (addr - block->offset < block->max_length) {
825             goto found;
826         }
827     }
828 
829     fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
830     abort();
831 
832 found:
833     /* It is safe to write mru_block outside the BQL.  This
834      * is what happens:
835      *
836      *     mru_block = xxx
837      *     rcu_read_unlock()
838      *                                        xxx removed from list
839      *                  rcu_read_lock()
840      *                  read mru_block
841      *                                        mru_block = NULL;
842      *                                        call_rcu(reclaim_ramblock, xxx);
843      *                  rcu_read_unlock()
844      *
845      * qatomic_rcu_set is not needed here.  The block was already published
846      * when it was placed into the list.  Here we're just making an extra
847      * copy of the pointer.
848      */
849     ram_list.mru_block = block;
850     return block;
851 }
852 
tlb_reset_dirty_range_all(ram_addr_t start,ram_addr_t length)853 void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length)
854 {
855     CPUState *cpu;
856     ram_addr_t start1;
857     RAMBlock *block;
858     ram_addr_t end;
859 
860     assert(tcg_enabled());
861     end = TARGET_PAGE_ALIGN(start + length);
862     start &= TARGET_PAGE_MASK;
863 
864     RCU_READ_LOCK_GUARD();
865     block = qemu_get_ram_block(start);
866     assert(block == qemu_get_ram_block(end - 1));
867     start1 = (uintptr_t)ramblock_ptr(block, start - block->offset);
868     CPU_FOREACH(cpu) {
869         tlb_reset_dirty(cpu, start1, length);
870     }
871 }
872 
873 /* Note: start and end must be within the same ram block.  */
cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,ram_addr_t length,unsigned client)874 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
875                                               ram_addr_t length,
876                                               unsigned client)
877 {
878     DirtyMemoryBlocks *blocks;
879     unsigned long end, page, start_page;
880     bool dirty = false;
881     RAMBlock *ramblock;
882     uint64_t mr_offset, mr_size;
883 
884     if (length == 0) {
885         return false;
886     }
887 
888     end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
889     start_page = start >> TARGET_PAGE_BITS;
890     page = start_page;
891 
892     WITH_RCU_READ_LOCK_GUARD() {
893         blocks = qatomic_rcu_read(&ram_list.dirty_memory[client]);
894         ramblock = qemu_get_ram_block(start);
895         /* Range sanity check on the ramblock */
896         assert(start >= ramblock->offset &&
897                start + length <= ramblock->offset + ramblock->used_length);
898 
899         while (page < end) {
900             unsigned long idx = page / DIRTY_MEMORY_BLOCK_SIZE;
901             unsigned long offset = page % DIRTY_MEMORY_BLOCK_SIZE;
902             unsigned long num = MIN(end - page,
903                                     DIRTY_MEMORY_BLOCK_SIZE - offset);
904 
905             dirty |= bitmap_test_and_clear_atomic(blocks->blocks[idx],
906                                                   offset, num);
907             page += num;
908         }
909 
910         mr_offset = (ram_addr_t)(start_page << TARGET_PAGE_BITS) - ramblock->offset;
911         mr_size = (end - start_page) << TARGET_PAGE_BITS;
912         memory_region_clear_dirty_bitmap(ramblock->mr, mr_offset, mr_size);
913     }
914 
915     if (dirty) {
916         cpu_physical_memory_dirty_bits_cleared(start, length);
917     }
918 
919     return dirty;
920 }
921 
cpu_physical_memory_snapshot_and_clear_dirty(MemoryRegion * mr,hwaddr offset,hwaddr length,unsigned client)922 DirtyBitmapSnapshot *cpu_physical_memory_snapshot_and_clear_dirty
923     (MemoryRegion *mr, hwaddr offset, hwaddr length, unsigned client)
924 {
925     DirtyMemoryBlocks *blocks;
926     ram_addr_t start, first, last;
927     unsigned long align = 1UL << (TARGET_PAGE_BITS + BITS_PER_LEVEL);
928     DirtyBitmapSnapshot *snap;
929     unsigned long page, end, dest;
930 
931     start = memory_region_get_ram_addr(mr);
932     /* We know we're only called for RAM MemoryRegions */
933     assert(start != RAM_ADDR_INVALID);
934     start += offset;
935 
936     first = QEMU_ALIGN_DOWN(start, align);
937     last  = QEMU_ALIGN_UP(start + length, align);
938 
939     snap = g_malloc0(sizeof(*snap) +
940                      ((last - first) >> (TARGET_PAGE_BITS + 3)));
941     snap->start = first;
942     snap->end   = last;
943 
944     page = first >> TARGET_PAGE_BITS;
945     end  = last  >> TARGET_PAGE_BITS;
946     dest = 0;
947 
948     WITH_RCU_READ_LOCK_GUARD() {
949         blocks = qatomic_rcu_read(&ram_list.dirty_memory[client]);
950 
951         while (page < end) {
952             unsigned long idx = page / DIRTY_MEMORY_BLOCK_SIZE;
953             unsigned long ofs = page % DIRTY_MEMORY_BLOCK_SIZE;
954             unsigned long num = MIN(end - page,
955                                     DIRTY_MEMORY_BLOCK_SIZE - ofs);
956 
957             assert(QEMU_IS_ALIGNED(ofs, (1 << BITS_PER_LEVEL)));
958             assert(QEMU_IS_ALIGNED(num,    (1 << BITS_PER_LEVEL)));
959             ofs >>= BITS_PER_LEVEL;
960 
961             bitmap_copy_and_clear_atomic(snap->dirty + dest,
962                                          blocks->blocks[idx] + ofs,
963                                          num);
964             page += num;
965             dest += num >> BITS_PER_LEVEL;
966         }
967     }
968 
969     cpu_physical_memory_dirty_bits_cleared(start, length);
970 
971     memory_region_clear_dirty_bitmap(mr, offset, length);
972 
973     return snap;
974 }
975 
cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot * snap,ram_addr_t start,ram_addr_t length)976 bool cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot *snap,
977                                             ram_addr_t start,
978                                             ram_addr_t length)
979 {
980     unsigned long page, end;
981 
982     assert(start >= snap->start);
983     assert(start + length <= snap->end);
984 
985     end = TARGET_PAGE_ALIGN(start + length - snap->start) >> TARGET_PAGE_BITS;
986     page = (start - snap->start) >> TARGET_PAGE_BITS;
987 
988     while (page < end) {
989         if (test_bit(page, snap->dirty)) {
990             return true;
991         }
992         page++;
993     }
994     return false;
995 }
996 
997 /* Called from RCU critical section */
memory_region_section_get_iotlb(CPUState * cpu,MemoryRegionSection * section)998 hwaddr memory_region_section_get_iotlb(CPUState *cpu,
999                                        MemoryRegionSection *section)
1000 {
1001     AddressSpaceDispatch *d = flatview_to_dispatch(section->fv);
1002     return section - d->map.sections;
1003 }
1004 
1005 static int subpage_register(subpage_t *mmio, uint32_t start, uint32_t end,
1006                             uint16_t section);
1007 static subpage_t *subpage_init(FlatView *fv, hwaddr base);
1008 
phys_section_add(PhysPageMap * map,MemoryRegionSection * section)1009 static uint16_t phys_section_add(PhysPageMap *map,
1010                                  MemoryRegionSection *section)
1011 {
1012     /* The physical section number is ORed with a page-aligned
1013      * pointer to produce the iotlb entries.  Thus it should
1014      * never overflow into the page-aligned value.
1015      */
1016     assert(map->sections_nb < TARGET_PAGE_SIZE);
1017 
1018     if (map->sections_nb == map->sections_nb_alloc) {
1019         map->sections_nb_alloc = MAX(map->sections_nb_alloc * 2, 16);
1020         map->sections = g_renew(MemoryRegionSection, map->sections,
1021                                 map->sections_nb_alloc);
1022     }
1023     map->sections[map->sections_nb] = *section;
1024     memory_region_ref(section->mr);
1025     return map->sections_nb++;
1026 }
1027 
phys_section_destroy(MemoryRegion * mr)1028 static void phys_section_destroy(MemoryRegion *mr)
1029 {
1030     bool have_sub_page = mr->subpage;
1031 
1032     memory_region_unref(mr);
1033 
1034     if (have_sub_page) {
1035         subpage_t *subpage = container_of(mr, subpage_t, iomem);
1036         object_unref(OBJECT(&subpage->iomem));
1037         g_free(subpage);
1038     }
1039 }
1040 
phys_sections_free(PhysPageMap * map)1041 static void phys_sections_free(PhysPageMap *map)
1042 {
1043     while (map->sections_nb > 0) {
1044         MemoryRegionSection *section = &map->sections[--map->sections_nb];
1045         phys_section_destroy(section->mr);
1046     }
1047     g_free(map->sections);
1048     g_free(map->nodes);
1049 }
1050 
register_subpage(FlatView * fv,MemoryRegionSection * section)1051 static void register_subpage(FlatView *fv, MemoryRegionSection *section)
1052 {
1053     AddressSpaceDispatch *d = flatview_to_dispatch(fv);
1054     subpage_t *subpage;
1055     hwaddr base = section->offset_within_address_space
1056         & TARGET_PAGE_MASK;
1057     MemoryRegionSection *existing = phys_page_find(d, base);
1058     MemoryRegionSection subsection = {
1059         .offset_within_address_space = base,
1060         .size = int128_make64(TARGET_PAGE_SIZE),
1061     };
1062     hwaddr start, end;
1063 
1064     assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
1065 
1066     if (!(existing->mr->subpage)) {
1067         subpage = subpage_init(fv, base);
1068         subsection.fv = fv;
1069         subsection.mr = &subpage->iomem;
1070         phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
1071                       phys_section_add(&d->map, &subsection));
1072     } else {
1073         subpage = container_of(existing->mr, subpage_t, iomem);
1074     }
1075     start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
1076     end = start + int128_get64(section->size) - 1;
1077     subpage_register(subpage, start, end,
1078                      phys_section_add(&d->map, section));
1079 }
1080 
1081 
register_multipage(FlatView * fv,MemoryRegionSection * section)1082 static void register_multipage(FlatView *fv,
1083                                MemoryRegionSection *section)
1084 {
1085     AddressSpaceDispatch *d = flatview_to_dispatch(fv);
1086     hwaddr start_addr = section->offset_within_address_space;
1087     uint16_t section_index = phys_section_add(&d->map, section);
1088     uint64_t num_pages = int128_get64(int128_rshift(section->size,
1089                                                     TARGET_PAGE_BITS));
1090 
1091     assert(num_pages);
1092     phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);
1093 }
1094 
1095 /*
1096  * The range in *section* may look like this:
1097  *
1098  *      |s|PPPPPPP|s|
1099  *
1100  * where s stands for subpage and P for page.
1101  */
flatview_add_to_dispatch(FlatView * fv,MemoryRegionSection * section)1102 void flatview_add_to_dispatch(FlatView *fv, MemoryRegionSection *section)
1103 {
1104     MemoryRegionSection remain = *section;
1105     Int128 page_size = int128_make64(TARGET_PAGE_SIZE);
1106 
1107     /* register first subpage */
1108     if (remain.offset_within_address_space & ~TARGET_PAGE_MASK) {
1109         uint64_t left = TARGET_PAGE_ALIGN(remain.offset_within_address_space)
1110                         - remain.offset_within_address_space;
1111 
1112         MemoryRegionSection now = remain;
1113         now.size = int128_min(int128_make64(left), now.size);
1114         register_subpage(fv, &now);
1115         if (int128_eq(remain.size, now.size)) {
1116             return;
1117         }
1118         remain.size = int128_sub(remain.size, now.size);
1119         remain.offset_within_address_space += int128_get64(now.size);
1120         remain.offset_within_region += int128_get64(now.size);
1121     }
1122 
1123     /* register whole pages */
1124     if (int128_ge(remain.size, page_size)) {
1125         MemoryRegionSection now = remain;
1126         now.size = int128_and(now.size, int128_neg(page_size));
1127         register_multipage(fv, &now);
1128         if (int128_eq(remain.size, now.size)) {
1129             return;
1130         }
1131         remain.size = int128_sub(remain.size, now.size);
1132         remain.offset_within_address_space += int128_get64(now.size);
1133         remain.offset_within_region += int128_get64(now.size);
1134     }
1135 
1136     /* register last subpage */
1137     register_subpage(fv, &remain);
1138 }
1139 
qemu_flush_coalesced_mmio_buffer(void)1140 void qemu_flush_coalesced_mmio_buffer(void)
1141 {
1142     if (kvm_enabled())
1143         kvm_flush_coalesced_mmio_buffer();
1144 }
1145 
qemu_mutex_lock_ramlist(void)1146 void qemu_mutex_lock_ramlist(void)
1147 {
1148     qemu_mutex_lock(&ram_list.mutex);
1149 }
1150 
qemu_mutex_unlock_ramlist(void)1151 void qemu_mutex_unlock_ramlist(void)
1152 {
1153     qemu_mutex_unlock(&ram_list.mutex);
1154 }
1155 
ram_block_format(void)1156 GString *ram_block_format(void)
1157 {
1158     RAMBlock *block;
1159     char *psize;
1160     GString *buf = g_string_new("");
1161 
1162     RCU_READ_LOCK_GUARD();
1163     g_string_append_printf(buf, "%24s %8s  %18s %18s %18s %18s %3s\n",
1164                            "Block Name", "PSize", "Offset", "Used", "Total",
1165                            "HVA", "RO");
1166 
1167     RAMBLOCK_FOREACH(block) {
1168         psize = size_to_str(block->page_size);
1169         g_string_append_printf(buf, "%24s %8s  0x%016" PRIx64 " 0x%016" PRIx64
1170                                " 0x%016" PRIx64 " 0x%016" PRIx64 " %3s\n",
1171                                block->idstr, psize,
1172                                (uint64_t)block->offset,
1173                                (uint64_t)block->used_length,
1174                                (uint64_t)block->max_length,
1175                                (uint64_t)(uintptr_t)block->host,
1176                                block->mr->readonly ? "ro" : "rw");
1177 
1178         g_free(psize);
1179     }
1180 
1181     return buf;
1182 }
1183 
find_min_backend_pagesize(Object * obj,void * opaque)1184 static int find_min_backend_pagesize(Object *obj, void *opaque)
1185 {
1186     long *hpsize_min = opaque;
1187 
1188     if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
1189         HostMemoryBackend *backend = MEMORY_BACKEND(obj);
1190         long hpsize = host_memory_backend_pagesize(backend);
1191 
1192         if (host_memory_backend_is_mapped(backend) && (hpsize < *hpsize_min)) {
1193             *hpsize_min = hpsize;
1194         }
1195     }
1196 
1197     return 0;
1198 }
1199 
find_max_backend_pagesize(Object * obj,void * opaque)1200 static int find_max_backend_pagesize(Object *obj, void *opaque)
1201 {
1202     long *hpsize_max = opaque;
1203 
1204     if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
1205         HostMemoryBackend *backend = MEMORY_BACKEND(obj);
1206         long hpsize = host_memory_backend_pagesize(backend);
1207 
1208         if (host_memory_backend_is_mapped(backend) && (hpsize > *hpsize_max)) {
1209             *hpsize_max = hpsize;
1210         }
1211     }
1212 
1213     return 0;
1214 }
1215 
1216 /*
1217  * TODO: We assume right now that all mapped host memory backends are
1218  * used as RAM, however some might be used for different purposes.
1219  */
qemu_minrampagesize(void)1220 long qemu_minrampagesize(void)
1221 {
1222     long hpsize = LONG_MAX;
1223     Object *memdev_root = object_resolve_path("/objects", NULL);
1224 
1225     object_child_foreach(memdev_root, find_min_backend_pagesize, &hpsize);
1226     return hpsize;
1227 }
1228 
qemu_maxrampagesize(void)1229 long qemu_maxrampagesize(void)
1230 {
1231     long pagesize = 0;
1232     Object *memdev_root = object_resolve_path("/objects", NULL);
1233 
1234     object_child_foreach(memdev_root, find_max_backend_pagesize, &pagesize);
1235     return pagesize;
1236 }
1237 
1238 #ifdef CONFIG_POSIX
get_file_size(int fd)1239 static int64_t get_file_size(int fd)
1240 {
1241     int64_t size;
1242 #if defined(__linux__)
1243     struct stat st;
1244 
1245     if (fstat(fd, &st) < 0) {
1246         return -errno;
1247     }
1248 
1249     /* Special handling for devdax character devices */
1250     if (S_ISCHR(st.st_mode)) {
1251         g_autofree char *subsystem_path = NULL;
1252         g_autofree char *subsystem = NULL;
1253 
1254         subsystem_path = g_strdup_printf("/sys/dev/char/%d:%d/subsystem",
1255                                          major(st.st_rdev), minor(st.st_rdev));
1256         subsystem = g_file_read_link(subsystem_path, NULL);
1257 
1258         if (subsystem && g_str_has_suffix(subsystem, "/dax")) {
1259             g_autofree char *size_path = NULL;
1260             g_autofree char *size_str = NULL;
1261 
1262             size_path = g_strdup_printf("/sys/dev/char/%d:%d/size",
1263                                     major(st.st_rdev), minor(st.st_rdev));
1264 
1265             if (g_file_get_contents(size_path, &size_str, NULL, NULL)) {
1266                 return g_ascii_strtoll(size_str, NULL, 0);
1267             }
1268         }
1269     }
1270 #endif /* defined(__linux__) */
1271 
1272     /* st.st_size may be zero for special files yet lseek(2) works */
1273     size = lseek(fd, 0, SEEK_END);
1274     if (size < 0) {
1275         return -errno;
1276     }
1277     return size;
1278 }
1279 
get_file_align(int fd)1280 static int64_t get_file_align(int fd)
1281 {
1282     int64_t align = -1;
1283 #if defined(__linux__) && defined(CONFIG_LIBDAXCTL)
1284     struct stat st;
1285 
1286     if (fstat(fd, &st) < 0) {
1287         return -errno;
1288     }
1289 
1290     /* Special handling for devdax character devices */
1291     if (S_ISCHR(st.st_mode)) {
1292         g_autofree char *path = NULL;
1293         g_autofree char *rpath = NULL;
1294         struct daxctl_ctx *ctx;
1295         struct daxctl_region *region;
1296         int rc = 0;
1297 
1298         path = g_strdup_printf("/sys/dev/char/%d:%d",
1299                     major(st.st_rdev), minor(st.st_rdev));
1300         rpath = realpath(path, NULL);
1301         if (!rpath) {
1302             return -errno;
1303         }
1304 
1305         rc = daxctl_new(&ctx);
1306         if (rc) {
1307             return -1;
1308         }
1309 
1310         daxctl_region_foreach(ctx, region) {
1311             if (strstr(rpath, daxctl_region_get_path(region))) {
1312                 align = daxctl_region_get_align(region);
1313                 break;
1314             }
1315         }
1316         daxctl_unref(ctx);
1317     }
1318 #endif /* defined(__linux__) && defined(CONFIG_LIBDAXCTL) */
1319 
1320     return align;
1321 }
1322 
file_ram_open(const char * path,const char * region_name,bool readonly,bool * created)1323 static int file_ram_open(const char *path,
1324                          const char *region_name,
1325                          bool readonly,
1326                          bool *created)
1327 {
1328     char *filename;
1329     char *sanitized_name;
1330     char *c;
1331     int fd = -1;
1332 
1333     *created = false;
1334     for (;;) {
1335         fd = open(path, readonly ? O_RDONLY : O_RDWR);
1336         if (fd >= 0) {
1337             /*
1338              * open(O_RDONLY) won't fail with EISDIR. Check manually if we
1339              * opened a directory and fail similarly to how we fail ENOENT
1340              * in readonly mode. Note that mkstemp() would imply O_RDWR.
1341              */
1342             if (readonly) {
1343                 struct stat file_stat;
1344 
1345                 if (fstat(fd, &file_stat)) {
1346                     close(fd);
1347                     if (errno == EINTR) {
1348                         continue;
1349                     }
1350                     return -errno;
1351                 } else if (S_ISDIR(file_stat.st_mode)) {
1352                     close(fd);
1353                     return -EISDIR;
1354                 }
1355             }
1356             /* @path names an existing file, use it */
1357             break;
1358         }
1359         if (errno == ENOENT) {
1360             if (readonly) {
1361                 /* Refuse to create new, readonly files. */
1362                 return -ENOENT;
1363             }
1364             /* @path names a file that doesn't exist, create it */
1365             fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0644);
1366             if (fd >= 0) {
1367                 *created = true;
1368                 break;
1369             }
1370         } else if (errno == EISDIR) {
1371             /* @path names a directory, create a file there */
1372             /* Make name safe to use with mkstemp by replacing '/' with '_'. */
1373             sanitized_name = g_strdup(region_name);
1374             for (c = sanitized_name; *c != '\0'; c++) {
1375                 if (*c == '/') {
1376                     *c = '_';
1377                 }
1378             }
1379 
1380             filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
1381                                        sanitized_name);
1382             g_free(sanitized_name);
1383 
1384             fd = mkstemp(filename);
1385             if (fd >= 0) {
1386                 unlink(filename);
1387                 g_free(filename);
1388                 break;
1389             }
1390             g_free(filename);
1391         }
1392         if (errno != EEXIST && errno != EINTR) {
1393             return -errno;
1394         }
1395         /*
1396          * Try again on EINTR and EEXIST.  The latter happens when
1397          * something else creates the file between our two open().
1398          */
1399     }
1400 
1401     return fd;
1402 }
1403 
file_ram_alloc(RAMBlock * block,ram_addr_t memory,int fd,bool truncate,off_t offset,Error ** errp)1404 static void *file_ram_alloc(RAMBlock *block,
1405                             ram_addr_t memory,
1406                             int fd,
1407                             bool truncate,
1408                             off_t offset,
1409                             Error **errp)
1410 {
1411     uint32_t qemu_map_flags;
1412     void *area;
1413 
1414     block->page_size = qemu_fd_getpagesize(fd);
1415     if (block->mr->align % block->page_size) {
1416         error_setg(errp, "alignment 0x%" PRIx64
1417                    " must be multiples of page size 0x%zx",
1418                    block->mr->align, block->page_size);
1419         return NULL;
1420     } else if (block->mr->align && !is_power_of_2(block->mr->align)) {
1421         error_setg(errp, "alignment 0x%" PRIx64
1422                    " must be a power of two", block->mr->align);
1423         return NULL;
1424     } else if (offset % block->page_size) {
1425         error_setg(errp, "offset 0x%" PRIx64
1426                    " must be multiples of page size 0x%zx",
1427                    offset, block->page_size);
1428         return NULL;
1429     }
1430     block->mr->align = MAX(block->page_size, block->mr->align);
1431 #if defined(__s390x__)
1432     if (kvm_enabled()) {
1433         block->mr->align = MAX(block->mr->align, QEMU_VMALLOC_ALIGN);
1434     }
1435 #endif
1436 
1437     if (memory < block->page_size) {
1438         error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to "
1439                    "or larger than page size 0x%zx",
1440                    memory, block->page_size);
1441         return NULL;
1442     }
1443 
1444     memory = ROUND_UP(memory, block->page_size);
1445 
1446     /*
1447      * ftruncate is not supported by hugetlbfs in older
1448      * hosts, so don't bother bailing out on errors.
1449      * If anything goes wrong with it under other filesystems,
1450      * mmap will fail.
1451      *
1452      * Do not truncate the non-empty backend file to avoid corrupting
1453      * the existing data in the file. Disabling shrinking is not
1454      * enough. For example, the current vNVDIMM implementation stores
1455      * the guest NVDIMM labels at the end of the backend file. If the
1456      * backend file is later extended, QEMU will not be able to find
1457      * those labels. Therefore, extending the non-empty backend file
1458      * is disabled as well.
1459      */
1460     if (truncate && ftruncate(fd, offset + memory)) {
1461         perror("ftruncate");
1462     }
1463 
1464     qemu_map_flags = (block->flags & RAM_READONLY) ? QEMU_MAP_READONLY : 0;
1465     qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
1466     qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
1467     qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
1468     area = qemu_ram_mmap(fd, memory, block->mr->align, qemu_map_flags, offset);
1469     if (area == MAP_FAILED) {
1470         error_setg_errno(errp, errno,
1471                          "unable to map backing store for guest RAM");
1472         return NULL;
1473     }
1474 
1475     block->fd = fd;
1476     block->fd_offset = offset;
1477     return area;
1478 }
1479 #endif
1480 
1481 /* Allocate space within the ram_addr_t space that governs the
1482  * dirty bitmaps.
1483  * Called with the ramlist lock held.
1484  */
find_ram_offset(ram_addr_t size)1485 static ram_addr_t find_ram_offset(ram_addr_t size)
1486 {
1487     RAMBlock *block, *next_block;
1488     ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
1489 
1490     assert(size != 0); /* it would hand out same offset multiple times */
1491 
1492     if (QLIST_EMPTY_RCU(&ram_list.blocks)) {
1493         return 0;
1494     }
1495 
1496     RAMBLOCK_FOREACH(block) {
1497         ram_addr_t candidate, next = RAM_ADDR_MAX;
1498 
1499         /* Align blocks to start on a 'long' in the bitmap
1500          * which makes the bitmap sync'ing take the fast path.
1501          */
1502         candidate = block->offset + block->max_length;
1503         candidate = ROUND_UP(candidate, BITS_PER_LONG << TARGET_PAGE_BITS);
1504 
1505         /* Search for the closest following block
1506          * and find the gap.
1507          */
1508         RAMBLOCK_FOREACH(next_block) {
1509             if (next_block->offset >= candidate) {
1510                 next = MIN(next, next_block->offset);
1511             }
1512         }
1513 
1514         /* If it fits remember our place and remember the size
1515          * of gap, but keep going so that we might find a smaller
1516          * gap to fill so avoiding fragmentation.
1517          */
1518         if (next - candidate >= size && next - candidate < mingap) {
1519             offset = candidate;
1520             mingap = next - candidate;
1521         }
1522 
1523         trace_find_ram_offset_loop(size, candidate, offset, next, mingap);
1524     }
1525 
1526     if (offset == RAM_ADDR_MAX) {
1527         fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
1528                 (uint64_t)size);
1529         abort();
1530     }
1531 
1532     trace_find_ram_offset(size, offset);
1533 
1534     return offset;
1535 }
1536 
qemu_ram_setup_dump(void * addr,ram_addr_t size)1537 static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
1538 {
1539     int ret;
1540 
1541     /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
1542     if (!machine_dump_guest_core(current_machine)) {
1543         ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
1544         if (ret) {
1545             perror("qemu_madvise");
1546             fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
1547                             "but dump-guest-core=off specified\n");
1548         }
1549     }
1550 }
1551 
qemu_ram_get_idstr(RAMBlock * rb)1552 const char *qemu_ram_get_idstr(RAMBlock *rb)
1553 {
1554     return rb->idstr;
1555 }
1556 
qemu_ram_get_host_addr(RAMBlock * rb)1557 void *qemu_ram_get_host_addr(RAMBlock *rb)
1558 {
1559     return rb->host;
1560 }
1561 
qemu_ram_get_offset(RAMBlock * rb)1562 ram_addr_t qemu_ram_get_offset(RAMBlock *rb)
1563 {
1564     return rb->offset;
1565 }
1566 
qemu_ram_get_used_length(RAMBlock * rb)1567 ram_addr_t qemu_ram_get_used_length(RAMBlock *rb)
1568 {
1569     return rb->used_length;
1570 }
1571 
qemu_ram_get_max_length(RAMBlock * rb)1572 ram_addr_t qemu_ram_get_max_length(RAMBlock *rb)
1573 {
1574     return rb->max_length;
1575 }
1576 
qemu_ram_is_shared(RAMBlock * rb)1577 bool qemu_ram_is_shared(RAMBlock *rb)
1578 {
1579     return rb->flags & RAM_SHARED;
1580 }
1581 
qemu_ram_is_noreserve(RAMBlock * rb)1582 bool qemu_ram_is_noreserve(RAMBlock *rb)
1583 {
1584     return rb->flags & RAM_NORESERVE;
1585 }
1586 
1587 /* Note: Only set at the start of postcopy */
qemu_ram_is_uf_zeroable(RAMBlock * rb)1588 bool qemu_ram_is_uf_zeroable(RAMBlock *rb)
1589 {
1590     return rb->flags & RAM_UF_ZEROPAGE;
1591 }
1592 
qemu_ram_set_uf_zeroable(RAMBlock * rb)1593 void qemu_ram_set_uf_zeroable(RAMBlock *rb)
1594 {
1595     rb->flags |= RAM_UF_ZEROPAGE;
1596 }
1597 
qemu_ram_is_migratable(RAMBlock * rb)1598 bool qemu_ram_is_migratable(RAMBlock *rb)
1599 {
1600     return rb->flags & RAM_MIGRATABLE;
1601 }
1602 
qemu_ram_set_migratable(RAMBlock * rb)1603 void qemu_ram_set_migratable(RAMBlock *rb)
1604 {
1605     rb->flags |= RAM_MIGRATABLE;
1606 }
1607 
qemu_ram_unset_migratable(RAMBlock * rb)1608 void qemu_ram_unset_migratable(RAMBlock *rb)
1609 {
1610     rb->flags &= ~RAM_MIGRATABLE;
1611 }
1612 
qemu_ram_is_named_file(RAMBlock * rb)1613 bool qemu_ram_is_named_file(RAMBlock *rb)
1614 {
1615     return rb->flags & RAM_NAMED_FILE;
1616 }
1617 
qemu_ram_get_fd(RAMBlock * rb)1618 int qemu_ram_get_fd(RAMBlock *rb)
1619 {
1620     return rb->fd;
1621 }
1622 
1623 /* Called with the BQL held.  */
qemu_ram_set_idstr(RAMBlock * new_block,const char * name,DeviceState * dev)1624 void qemu_ram_set_idstr(RAMBlock *new_block, const char *name, DeviceState *dev)
1625 {
1626     RAMBlock *block;
1627 
1628     assert(new_block);
1629     assert(!new_block->idstr[0]);
1630 
1631     if (dev) {
1632         char *id = qdev_get_dev_path(dev);
1633         if (id) {
1634             snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
1635             g_free(id);
1636         }
1637     }
1638     pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
1639 
1640     RCU_READ_LOCK_GUARD();
1641     RAMBLOCK_FOREACH(block) {
1642         if (block != new_block &&
1643             !strcmp(block->idstr, new_block->idstr)) {
1644             fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
1645                     new_block->idstr);
1646             abort();
1647         }
1648     }
1649 }
1650 
1651 /* Called with the BQL held.  */
qemu_ram_unset_idstr(RAMBlock * block)1652 void qemu_ram_unset_idstr(RAMBlock *block)
1653 {
1654     /* FIXME: arch_init.c assumes that this is not called throughout
1655      * migration.  Ignore the problem since hot-unplug during migration
1656      * does not work anyway.
1657      */
1658     if (block) {
1659         memset(block->idstr, 0, sizeof(block->idstr));
1660     }
1661 }
1662 
qemu_ram_pagesize(RAMBlock * rb)1663 size_t qemu_ram_pagesize(RAMBlock *rb)
1664 {
1665     return rb->page_size;
1666 }
1667 
1668 /* Returns the largest size of page in use */
qemu_ram_pagesize_largest(void)1669 size_t qemu_ram_pagesize_largest(void)
1670 {
1671     RAMBlock *block;
1672     size_t largest = 0;
1673 
1674     RAMBLOCK_FOREACH(block) {
1675         largest = MAX(largest, qemu_ram_pagesize(block));
1676     }
1677 
1678     return largest;
1679 }
1680 
memory_try_enable_merging(void * addr,size_t len)1681 static int memory_try_enable_merging(void *addr, size_t len)
1682 {
1683     if (!machine_mem_merge(current_machine)) {
1684         /* disabled by the user */
1685         return 0;
1686     }
1687 
1688     return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
1689 }
1690 
1691 /*
1692  * Resizing RAM while migrating can result in the migration being canceled.
1693  * Care has to be taken if the guest might have already detected the memory.
1694  *
1695  * As memory core doesn't know how is memory accessed, it is up to
1696  * resize callback to update device state and/or add assertions to detect
1697  * misuse, if necessary.
1698  */
qemu_ram_resize(RAMBlock * block,ram_addr_t newsize,Error ** errp)1699 int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp)
1700 {
1701     const ram_addr_t oldsize = block->used_length;
1702     const ram_addr_t unaligned_size = newsize;
1703 
1704     assert(block);
1705 
1706     newsize = TARGET_PAGE_ALIGN(newsize);
1707     newsize = REAL_HOST_PAGE_ALIGN(newsize);
1708 
1709     if (block->used_length == newsize) {
1710         /*
1711          * We don't have to resize the ram block (which only knows aligned
1712          * sizes), however, we have to notify if the unaligned size changed.
1713          */
1714         if (unaligned_size != memory_region_size(block->mr)) {
1715             memory_region_set_size(block->mr, unaligned_size);
1716             if (block->resized) {
1717                 block->resized(block->idstr, unaligned_size, block->host);
1718             }
1719         }
1720         return 0;
1721     }
1722 
1723     if (!(block->flags & RAM_RESIZEABLE)) {
1724         error_setg_errno(errp, EINVAL,
1725                          "Size mismatch: %s: 0x" RAM_ADDR_FMT
1726                          " != 0x" RAM_ADDR_FMT, block->idstr,
1727                          newsize, block->used_length);
1728         return -EINVAL;
1729     }
1730 
1731     if (block->max_length < newsize) {
1732         error_setg_errno(errp, EINVAL,
1733                          "Size too large: %s: 0x" RAM_ADDR_FMT
1734                          " > 0x" RAM_ADDR_FMT, block->idstr,
1735                          newsize, block->max_length);
1736         return -EINVAL;
1737     }
1738 
1739     /* Notify before modifying the ram block and touching the bitmaps. */
1740     if (block->host) {
1741         ram_block_notify_resize(block->host, oldsize, newsize);
1742     }
1743 
1744     cpu_physical_memory_clear_dirty_range(block->offset, block->used_length);
1745     block->used_length = newsize;
1746     cpu_physical_memory_set_dirty_range(block->offset, block->used_length,
1747                                         DIRTY_CLIENTS_ALL);
1748     memory_region_set_size(block->mr, unaligned_size);
1749     if (block->resized) {
1750         block->resized(block->idstr, unaligned_size, block->host);
1751     }
1752     return 0;
1753 }
1754 
1755 /*
1756  * Trigger sync on the given ram block for range [start, start + length]
1757  * with the backing store if one is available.
1758  * Otherwise no-op.
1759  * @Note: this is supposed to be a synchronous op.
1760  */
qemu_ram_msync(RAMBlock * block,ram_addr_t start,ram_addr_t length)1761 void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length)
1762 {
1763     /* The requested range should fit in within the block range */
1764     g_assert((start + length) <= block->used_length);
1765 
1766 #ifdef CONFIG_LIBPMEM
1767     /* The lack of support for pmem should not block the sync */
1768     if (ramblock_is_pmem(block)) {
1769         void *addr = ramblock_ptr(block, start);
1770         pmem_persist(addr, length);
1771         return;
1772     }
1773 #endif
1774     if (block->fd >= 0) {
1775         /**
1776          * Case there is no support for PMEM or the memory has not been
1777          * specified as persistent (or is not one) - use the msync.
1778          * Less optimal but still achieves the same goal
1779          */
1780         void *addr = ramblock_ptr(block, start);
1781         if (qemu_msync(addr, length, block->fd)) {
1782             warn_report("%s: failed to sync memory range: start: "
1783                     RAM_ADDR_FMT " length: " RAM_ADDR_FMT,
1784                     __func__, start, length);
1785         }
1786     }
1787 }
1788 
1789 /* Called with ram_list.mutex held */
dirty_memory_extend(ram_addr_t new_ram_size)1790 static void dirty_memory_extend(ram_addr_t new_ram_size)
1791 {
1792     unsigned int old_num_blocks = ram_list.num_dirty_blocks;
1793     unsigned int new_num_blocks = DIV_ROUND_UP(new_ram_size,
1794                                                DIRTY_MEMORY_BLOCK_SIZE);
1795     int i;
1796 
1797     /* Only need to extend if block count increased */
1798     if (new_num_blocks <= old_num_blocks) {
1799         return;
1800     }
1801 
1802     for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
1803         DirtyMemoryBlocks *old_blocks;
1804         DirtyMemoryBlocks *new_blocks;
1805         int j;
1806 
1807         old_blocks = qatomic_rcu_read(&ram_list.dirty_memory[i]);
1808         new_blocks = g_malloc(sizeof(*new_blocks) +
1809                               sizeof(new_blocks->blocks[0]) * new_num_blocks);
1810 
1811         if (old_num_blocks) {
1812             memcpy(new_blocks->blocks, old_blocks->blocks,
1813                    old_num_blocks * sizeof(old_blocks->blocks[0]));
1814         }
1815 
1816         for (j = old_num_blocks; j < new_num_blocks; j++) {
1817             new_blocks->blocks[j] = bitmap_new(DIRTY_MEMORY_BLOCK_SIZE);
1818         }
1819 
1820         qatomic_rcu_set(&ram_list.dirty_memory[i], new_blocks);
1821 
1822         if (old_blocks) {
1823             g_free_rcu(old_blocks, rcu);
1824         }
1825     }
1826 
1827     ram_list.num_dirty_blocks = new_num_blocks;
1828 }
1829 
ram_block_add(RAMBlock * new_block,Error ** errp)1830 static void ram_block_add(RAMBlock *new_block, Error **errp)
1831 {
1832     const bool noreserve = qemu_ram_is_noreserve(new_block);
1833     const bool shared = qemu_ram_is_shared(new_block);
1834     RAMBlock *block;
1835     RAMBlock *last_block = NULL;
1836     bool free_on_error = false;
1837     ram_addr_t ram_size;
1838     Error *err = NULL;
1839 
1840     qemu_mutex_lock_ramlist();
1841     new_block->offset = find_ram_offset(new_block->max_length);
1842 
1843     if (!new_block->host) {
1844         if (xen_enabled()) {
1845             xen_ram_alloc(new_block->offset, new_block->max_length,
1846                           new_block->mr, &err);
1847             if (err) {
1848                 error_propagate(errp, err);
1849                 qemu_mutex_unlock_ramlist();
1850                 return;
1851             }
1852         } else {
1853             new_block->host = qemu_anon_ram_alloc(new_block->max_length,
1854                                                   &new_block->mr->align,
1855                                                   shared, noreserve);
1856             if (!new_block->host) {
1857                 error_setg_errno(errp, errno,
1858                                  "cannot set up guest memory '%s'",
1859                                  memory_region_name(new_block->mr));
1860                 qemu_mutex_unlock_ramlist();
1861                 return;
1862             }
1863             memory_try_enable_merging(new_block->host, new_block->max_length);
1864             free_on_error = true;
1865         }
1866     }
1867 
1868     if (new_block->flags & RAM_GUEST_MEMFD) {
1869         int ret;
1870 
1871         if (!kvm_enabled()) {
1872             error_setg(errp, "cannot set up private guest memory for %s: KVM required",
1873                        object_get_typename(OBJECT(current_machine->cgs)));
1874             goto out_free;
1875         }
1876         assert(new_block->guest_memfd < 0);
1877 
1878         ret = ram_block_discard_require(true);
1879         if (ret < 0) {
1880             error_setg_errno(errp, -ret,
1881                              "cannot set up private guest memory: discard currently blocked");
1882             error_append_hint(errp, "Are you using assigned devices?\n");
1883             goto out_free;
1884         }
1885 
1886         new_block->guest_memfd = kvm_create_guest_memfd(new_block->max_length,
1887                                                         0, errp);
1888         if (new_block->guest_memfd < 0) {
1889             qemu_mutex_unlock_ramlist();
1890             goto out_free;
1891         }
1892     }
1893 
1894     ram_size = (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS;
1895     dirty_memory_extend(ram_size);
1896     /* Keep the list sorted from biggest to smallest block.  Unlike QTAILQ,
1897      * QLIST (which has an RCU-friendly variant) does not have insertion at
1898      * tail, so save the last element in last_block.
1899      */
1900     RAMBLOCK_FOREACH(block) {
1901         last_block = block;
1902         if (block->max_length < new_block->max_length) {
1903             break;
1904         }
1905     }
1906     if (block) {
1907         QLIST_INSERT_BEFORE_RCU(block, new_block, next);
1908     } else if (last_block) {
1909         QLIST_INSERT_AFTER_RCU(last_block, new_block, next);
1910     } else { /* list is empty */
1911         QLIST_INSERT_HEAD_RCU(&ram_list.blocks, new_block, next);
1912     }
1913     ram_list.mru_block = NULL;
1914 
1915     /* Write list before version */
1916     smp_wmb();
1917     ram_list.version++;
1918     qemu_mutex_unlock_ramlist();
1919 
1920     cpu_physical_memory_set_dirty_range(new_block->offset,
1921                                         new_block->used_length,
1922                                         DIRTY_CLIENTS_ALL);
1923 
1924     if (new_block->host) {
1925         qemu_ram_setup_dump(new_block->host, new_block->max_length);
1926         qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_HUGEPAGE);
1927         /*
1928          * MADV_DONTFORK is also needed by KVM in absence of synchronous MMU
1929          * Configure it unless the machine is a qtest server, in which case
1930          * KVM is not used and it may be forked (eg for fuzzing purposes).
1931          */
1932         if (!qtest_enabled()) {
1933             qemu_madvise(new_block->host, new_block->max_length,
1934                          QEMU_MADV_DONTFORK);
1935         }
1936         ram_block_notify_add(new_block->host, new_block->used_length,
1937                              new_block->max_length);
1938     }
1939     return;
1940 
1941 out_free:
1942     if (free_on_error) {
1943         qemu_anon_ram_free(new_block->host, new_block->max_length);
1944         new_block->host = NULL;
1945     }
1946 }
1947 
1948 #ifdef CONFIG_POSIX
qemu_ram_alloc_from_fd(ram_addr_t size,MemoryRegion * mr,uint32_t ram_flags,int fd,off_t offset,Error ** errp)1949 RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
1950                                  uint32_t ram_flags, int fd, off_t offset,
1951                                  Error **errp)
1952 {
1953     RAMBlock *new_block;
1954     Error *local_err = NULL;
1955     int64_t file_size, file_align;
1956 
1957     /* Just support these ram flags by now. */
1958     assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_NORESERVE |
1959                           RAM_PROTECTED | RAM_NAMED_FILE | RAM_READONLY |
1960                           RAM_READONLY_FD | RAM_GUEST_MEMFD)) == 0);
1961 
1962     if (xen_enabled()) {
1963         error_setg(errp, "-mem-path not supported with Xen");
1964         return NULL;
1965     }
1966 
1967     if (kvm_enabled() && !kvm_has_sync_mmu()) {
1968         error_setg(errp,
1969                    "host lacks kvm mmu notifiers, -mem-path unsupported");
1970         return NULL;
1971     }
1972 
1973     size = TARGET_PAGE_ALIGN(size);
1974     size = REAL_HOST_PAGE_ALIGN(size);
1975 
1976     file_size = get_file_size(fd);
1977     if (file_size && file_size < offset + size) {
1978         error_setg(errp, "%s backing store size 0x%" PRIx64
1979                    " is too small for 'size' option 0x" RAM_ADDR_FMT
1980                    " plus 'offset' option 0x%" PRIx64,
1981                    memory_region_name(mr), file_size, size,
1982                    (uint64_t)offset);
1983         return NULL;
1984     }
1985 
1986     file_align = get_file_align(fd);
1987     if (file_align > 0 && file_align > mr->align) {
1988         error_setg(errp, "backing store align 0x%" PRIx64
1989                    " is larger than 'align' option 0x%" PRIx64,
1990                    file_align, mr->align);
1991         return NULL;
1992     }
1993 
1994     new_block = g_malloc0(sizeof(*new_block));
1995     new_block->mr = mr;
1996     new_block->used_length = size;
1997     new_block->max_length = size;
1998     new_block->flags = ram_flags;
1999     new_block->guest_memfd = -1;
2000     new_block->host = file_ram_alloc(new_block, size, fd, !file_size, offset,
2001                                      errp);
2002     if (!new_block->host) {
2003         g_free(new_block);
2004         return NULL;
2005     }
2006 
2007     ram_block_add(new_block, &local_err);
2008     if (local_err) {
2009         g_free(new_block);
2010         error_propagate(errp, local_err);
2011         return NULL;
2012     }
2013     return new_block;
2014 
2015 }
2016 
2017 
qemu_ram_alloc_from_file(ram_addr_t size,MemoryRegion * mr,uint32_t ram_flags,const char * mem_path,off_t offset,Error ** errp)2018 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
2019                                    uint32_t ram_flags, const char *mem_path,
2020                                    off_t offset, Error **errp)
2021 {
2022     int fd;
2023     bool created;
2024     RAMBlock *block;
2025 
2026     fd = file_ram_open(mem_path, memory_region_name(mr),
2027                        !!(ram_flags & RAM_READONLY_FD), &created);
2028     if (fd < 0) {
2029         error_setg_errno(errp, -fd, "can't open backing store %s for guest RAM",
2030                          mem_path);
2031         if (!(ram_flags & RAM_READONLY_FD) && !(ram_flags & RAM_SHARED) &&
2032             fd == -EACCES) {
2033             /*
2034              * If we can open the file R/O (note: will never create a new file)
2035              * and we are dealing with a private mapping, there are still ways
2036              * to consume such files and get RAM instead of ROM.
2037              */
2038             fd = file_ram_open(mem_path, memory_region_name(mr), true,
2039                                &created);
2040             if (fd < 0) {
2041                 return NULL;
2042             }
2043             assert(!created);
2044             close(fd);
2045             error_append_hint(errp, "Consider opening the backing store"
2046                 " read-only but still creating writable RAM using"
2047                 " '-object memory-backend-file,readonly=on,rom=off...'"
2048                 " (see \"VM templating\" documentation)\n");
2049         }
2050         return NULL;
2051     }
2052 
2053     block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, offset, errp);
2054     if (!block) {
2055         if (created) {
2056             unlink(mem_path);
2057         }
2058         close(fd);
2059         return NULL;
2060     }
2061 
2062     return block;
2063 }
2064 #endif
2065 
2066 static
qemu_ram_alloc_internal(ram_addr_t size,ram_addr_t max_size,void (* resized)(const char *,uint64_t length,void * host),void * host,uint32_t ram_flags,MemoryRegion * mr,Error ** errp)2067 RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
2068                                   void (*resized)(const char*,
2069                                                   uint64_t length,
2070                                                   void *host),
2071                                   void *host, uint32_t ram_flags,
2072                                   MemoryRegion *mr, Error **errp)
2073 {
2074     RAMBlock *new_block;
2075     Error *local_err = NULL;
2076     int align;
2077 
2078     assert((ram_flags & ~(RAM_SHARED | RAM_RESIZEABLE | RAM_PREALLOC |
2079                           RAM_NORESERVE | RAM_GUEST_MEMFD)) == 0);
2080     assert(!host ^ (ram_flags & RAM_PREALLOC));
2081 
2082     align = qemu_real_host_page_size();
2083     align = MAX(align, TARGET_PAGE_SIZE);
2084     size = ROUND_UP(size, align);
2085     max_size = ROUND_UP(max_size, align);
2086 
2087     new_block = g_malloc0(sizeof(*new_block));
2088     new_block->mr = mr;
2089     new_block->resized = resized;
2090     new_block->used_length = size;
2091     new_block->max_length = max_size;
2092     assert(max_size >= size);
2093     new_block->fd = -1;
2094     new_block->guest_memfd = -1;
2095     new_block->page_size = qemu_real_host_page_size();
2096     new_block->host = host;
2097     new_block->flags = ram_flags;
2098     ram_block_add(new_block, &local_err);
2099     if (local_err) {
2100         g_free(new_block);
2101         error_propagate(errp, local_err);
2102         return NULL;
2103     }
2104     return new_block;
2105 }
2106 
qemu_ram_alloc_from_ptr(ram_addr_t size,void * host,MemoryRegion * mr,Error ** errp)2107 RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
2108                                    MemoryRegion *mr, Error **errp)
2109 {
2110     return qemu_ram_alloc_internal(size, size, NULL, host, RAM_PREALLOC, mr,
2111                                    errp);
2112 }
2113 
qemu_ram_alloc(ram_addr_t size,uint32_t ram_flags,MemoryRegion * mr,Error ** errp)2114 RAMBlock *qemu_ram_alloc(ram_addr_t size, uint32_t ram_flags,
2115                          MemoryRegion *mr, Error **errp)
2116 {
2117     assert((ram_flags & ~(RAM_SHARED | RAM_NORESERVE | RAM_GUEST_MEMFD)) == 0);
2118     return qemu_ram_alloc_internal(size, size, NULL, NULL, ram_flags, mr, errp);
2119 }
2120 
qemu_ram_alloc_resizeable(ram_addr_t size,ram_addr_t maxsz,void (* resized)(const char *,uint64_t length,void * host),MemoryRegion * mr,Error ** errp)2121 RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t maxsz,
2122                                      void (*resized)(const char*,
2123                                                      uint64_t length,
2124                                                      void *host),
2125                                      MemoryRegion *mr, Error **errp)
2126 {
2127     return qemu_ram_alloc_internal(size, maxsz, resized, NULL,
2128                                    RAM_RESIZEABLE, mr, errp);
2129 }
2130 
reclaim_ramblock(RAMBlock * block)2131 static void reclaim_ramblock(RAMBlock *block)
2132 {
2133     if (block->flags & RAM_PREALLOC) {
2134         ;
2135     } else if (xen_enabled()) {
2136         xen_invalidate_map_cache_entry(block->host);
2137 #ifndef _WIN32
2138     } else if (block->fd >= 0) {
2139         qemu_ram_munmap(block->fd, block->host, block->max_length);
2140         close(block->fd);
2141 #endif
2142     } else {
2143         qemu_anon_ram_free(block->host, block->max_length);
2144     }
2145 
2146     if (block->guest_memfd >= 0) {
2147         close(block->guest_memfd);
2148         ram_block_discard_require(false);
2149     }
2150 
2151     g_free(block);
2152 }
2153 
qemu_ram_free(RAMBlock * block)2154 void qemu_ram_free(RAMBlock *block)
2155 {
2156     if (!block) {
2157         return;
2158     }
2159 
2160     if (block->host) {
2161         ram_block_notify_remove(block->host, block->used_length,
2162                                 block->max_length);
2163     }
2164 
2165     qemu_mutex_lock_ramlist();
2166     QLIST_REMOVE_RCU(block, next);
2167     ram_list.mru_block = NULL;
2168     /* Write list before version */
2169     smp_wmb();
2170     ram_list.version++;
2171     call_rcu(block, reclaim_ramblock, rcu);
2172     qemu_mutex_unlock_ramlist();
2173 }
2174 
2175 #ifndef _WIN32
qemu_ram_remap(ram_addr_t addr,ram_addr_t length)2176 void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
2177 {
2178     RAMBlock *block;
2179     ram_addr_t offset;
2180     int flags;
2181     void *area, *vaddr;
2182     int prot;
2183 
2184     RAMBLOCK_FOREACH(block) {
2185         offset = addr - block->offset;
2186         if (offset < block->max_length) {
2187             vaddr = ramblock_ptr(block, offset);
2188             if (block->flags & RAM_PREALLOC) {
2189                 ;
2190             } else if (xen_enabled()) {
2191                 abort();
2192             } else {
2193                 flags = MAP_FIXED;
2194                 flags |= block->flags & RAM_SHARED ?
2195                          MAP_SHARED : MAP_PRIVATE;
2196                 flags |= block->flags & RAM_NORESERVE ? MAP_NORESERVE : 0;
2197                 prot = PROT_READ;
2198                 prot |= block->flags & RAM_READONLY ? 0 : PROT_WRITE;
2199                 if (block->fd >= 0) {
2200                     area = mmap(vaddr, length, prot, flags, block->fd,
2201                                 offset + block->fd_offset);
2202                 } else {
2203                     flags |= MAP_ANONYMOUS;
2204                     area = mmap(vaddr, length, prot, flags, -1, 0);
2205                 }
2206                 if (area != vaddr) {
2207                     error_report("Could not remap addr: "
2208                                  RAM_ADDR_FMT "@" RAM_ADDR_FMT "",
2209                                  length, addr);
2210                     exit(1);
2211                 }
2212                 memory_try_enable_merging(vaddr, length);
2213                 qemu_ram_setup_dump(vaddr, length);
2214             }
2215         }
2216     }
2217 }
2218 #endif /* !_WIN32 */
2219 
2220 /*
2221  * Return a host pointer to guest's ram.
2222  * For Xen, foreign mappings get created if they don't already exist.
2223  *
2224  * @block: block for the RAM to lookup (optional and may be NULL).
2225  * @addr: address within the memory region.
2226  * @size: pointer to requested size (optional and may be NULL).
2227  *        size may get modified and return a value smaller than
2228  *        what was requested.
2229  * @lock: wether to lock the mapping in xen-mapcache until invalidated.
2230  * @is_write: hint wether to map RW or RO in the xen-mapcache.
2231  *            (optional and may always be set to true).
2232  *
2233  * Called within RCU critical section.
2234  */
qemu_ram_ptr_length(RAMBlock * block,ram_addr_t addr,hwaddr * size,bool lock,bool is_write)2235 static void *qemu_ram_ptr_length(RAMBlock *block, ram_addr_t addr,
2236                                  hwaddr *size, bool lock,
2237                                  bool is_write)
2238 {
2239     hwaddr len = 0;
2240 
2241     if (size && *size == 0) {
2242         return NULL;
2243     }
2244 
2245     if (block == NULL) {
2246         block = qemu_get_ram_block(addr);
2247         addr -= block->offset;
2248     }
2249     if (size) {
2250         *size = MIN(*size, block->max_length - addr);
2251         len = *size;
2252     }
2253 
2254     if (xen_enabled() && block->host == NULL) {
2255         /* We need to check if the requested address is in the RAM
2256          * because we don't want to map the entire memory in QEMU.
2257          * In that case just map the requested area.
2258          */
2259         if (xen_mr_is_memory(block->mr)) {
2260             return xen_map_cache(block->mr, block->offset + addr,
2261                                  len, block->offset,
2262                                  lock, lock, is_write);
2263         }
2264 
2265         block->host = xen_map_cache(block->mr, block->offset,
2266                                     block->max_length,
2267                                     block->offset,
2268                                     1, lock, is_write);
2269     }
2270 
2271     return ramblock_ptr(block, addr);
2272 }
2273 
2274 /*
2275  * Return a host pointer to ram allocated with qemu_ram_alloc.
2276  * This should not be used for general purpose DMA.  Use address_space_map
2277  * or address_space_rw instead. For local memory (e.g. video ram) that the
2278  * device owns, use memory_region_get_ram_ptr.
2279  *
2280  * Called within RCU critical section.
2281  */
qemu_map_ram_ptr(RAMBlock * ram_block,ram_addr_t addr)2282 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr)
2283 {
2284     return qemu_ram_ptr_length(ram_block, addr, NULL, false, true);
2285 }
2286 
2287 /* Return the offset of a hostpointer within a ramblock */
qemu_ram_block_host_offset(RAMBlock * rb,void * host)2288 ram_addr_t qemu_ram_block_host_offset(RAMBlock *rb, void *host)
2289 {
2290     ram_addr_t res = (uint8_t *)host - (uint8_t *)rb->host;
2291     assert((uintptr_t)host >= (uintptr_t)rb->host);
2292     assert(res < rb->max_length);
2293 
2294     return res;
2295 }
2296 
qemu_ram_block_from_host(void * ptr,bool round_offset,ram_addr_t * offset)2297 RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
2298                                    ram_addr_t *offset)
2299 {
2300     RAMBlock *block;
2301     uint8_t *host = ptr;
2302 
2303     if (xen_enabled()) {
2304         ram_addr_t ram_addr;
2305         RCU_READ_LOCK_GUARD();
2306         ram_addr = xen_ram_addr_from_mapcache(ptr);
2307         if (ram_addr == RAM_ADDR_INVALID) {
2308             return NULL;
2309         }
2310 
2311         block = qemu_get_ram_block(ram_addr);
2312         if (block) {
2313             *offset = ram_addr - block->offset;
2314         }
2315         return block;
2316     }
2317 
2318     RCU_READ_LOCK_GUARD();
2319     block = qatomic_rcu_read(&ram_list.mru_block);
2320     if (block && block->host && host - block->host < block->max_length) {
2321         goto found;
2322     }
2323 
2324     RAMBLOCK_FOREACH(block) {
2325         /* This case append when the block is not mapped. */
2326         if (block->host == NULL) {
2327             continue;
2328         }
2329         if (host - block->host < block->max_length) {
2330             goto found;
2331         }
2332     }
2333 
2334     return NULL;
2335 
2336 found:
2337     *offset = (host - block->host);
2338     if (round_offset) {
2339         *offset &= TARGET_PAGE_MASK;
2340     }
2341     return block;
2342 }
2343 
2344 /*
2345  * Finds the named RAMBlock
2346  *
2347  * name: The name of RAMBlock to find
2348  *
2349  * Returns: RAMBlock (or NULL if not found)
2350  */
qemu_ram_block_by_name(const char * name)2351 RAMBlock *qemu_ram_block_by_name(const char *name)
2352 {
2353     RAMBlock *block;
2354 
2355     RAMBLOCK_FOREACH(block) {
2356         if (!strcmp(name, block->idstr)) {
2357             return block;
2358         }
2359     }
2360 
2361     return NULL;
2362 }
2363 
2364 /*
2365  * Some of the system routines need to translate from a host pointer
2366  * (typically a TLB entry) back to a ram offset.
2367  */
qemu_ram_addr_from_host(void * ptr)2368 ram_addr_t qemu_ram_addr_from_host(void *ptr)
2369 {
2370     RAMBlock *block;
2371     ram_addr_t offset;
2372 
2373     block = qemu_ram_block_from_host(ptr, false, &offset);
2374     if (!block) {
2375         return RAM_ADDR_INVALID;
2376     }
2377 
2378     return block->offset + offset;
2379 }
2380 
qemu_ram_addr_from_host_nofail(void * ptr)2381 ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr)
2382 {
2383     ram_addr_t ram_addr;
2384 
2385     ram_addr = qemu_ram_addr_from_host(ptr);
2386     if (ram_addr == RAM_ADDR_INVALID) {
2387         error_report("Bad ram pointer %p", ptr);
2388         abort();
2389     }
2390     return ram_addr;
2391 }
2392 
2393 static MemTxResult flatview_read(FlatView *fv, hwaddr addr,
2394                                  MemTxAttrs attrs, void *buf, hwaddr len);
2395 static MemTxResult flatview_write(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
2396                                   const void *buf, hwaddr len);
2397 static bool flatview_access_valid(FlatView *fv, hwaddr addr, hwaddr len,
2398                                   bool is_write, MemTxAttrs attrs);
2399 
subpage_read(void * opaque,hwaddr addr,uint64_t * data,unsigned len,MemTxAttrs attrs)2400 static MemTxResult subpage_read(void *opaque, hwaddr addr, uint64_t *data,
2401                                 unsigned len, MemTxAttrs attrs)
2402 {
2403     subpage_t *subpage = opaque;
2404     uint8_t buf[8];
2405     MemTxResult res;
2406 
2407 #if defined(DEBUG_SUBPAGE)
2408     printf("%s: subpage %p len %u addr " HWADDR_FMT_plx "\n", __func__,
2409            subpage, len, addr);
2410 #endif
2411     res = flatview_read(subpage->fv, addr + subpage->base, attrs, buf, len);
2412     if (res) {
2413         return res;
2414     }
2415     *data = ldn_p(buf, len);
2416     return MEMTX_OK;
2417 }
2418 
subpage_write(void * opaque,hwaddr addr,uint64_t value,unsigned len,MemTxAttrs attrs)2419 static MemTxResult subpage_write(void *opaque, hwaddr addr,
2420                                  uint64_t value, unsigned len, MemTxAttrs attrs)
2421 {
2422     subpage_t *subpage = opaque;
2423     uint8_t buf[8];
2424 
2425 #if defined(DEBUG_SUBPAGE)
2426     printf("%s: subpage %p len %u addr " HWADDR_FMT_plx
2427            " value %"PRIx64"\n",
2428            __func__, subpage, len, addr, value);
2429 #endif
2430     stn_p(buf, len, value);
2431     return flatview_write(subpage->fv, addr + subpage->base, attrs, buf, len);
2432 }
2433 
subpage_accepts(void * opaque,hwaddr addr,unsigned len,bool is_write,MemTxAttrs attrs)2434 static bool subpage_accepts(void *opaque, hwaddr addr,
2435                             unsigned len, bool is_write,
2436                             MemTxAttrs attrs)
2437 {
2438     subpage_t *subpage = opaque;
2439 #if defined(DEBUG_SUBPAGE)
2440     printf("%s: subpage %p %c len %u addr " HWADDR_FMT_plx "\n",
2441            __func__, subpage, is_write ? 'w' : 'r', len, addr);
2442 #endif
2443 
2444     return flatview_access_valid(subpage->fv, addr + subpage->base,
2445                                  len, is_write, attrs);
2446 }
2447 
2448 static const MemoryRegionOps subpage_ops = {
2449     .read_with_attrs = subpage_read,
2450     .write_with_attrs = subpage_write,
2451     .impl.min_access_size = 1,
2452     .impl.max_access_size = 8,
2453     .valid.min_access_size = 1,
2454     .valid.max_access_size = 8,
2455     .valid.accepts = subpage_accepts,
2456     .endianness = DEVICE_NATIVE_ENDIAN,
2457 };
2458 
subpage_register(subpage_t * mmio,uint32_t start,uint32_t end,uint16_t section)2459 static int subpage_register(subpage_t *mmio, uint32_t start, uint32_t end,
2460                             uint16_t section)
2461 {
2462     int idx, eidx;
2463 
2464     if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2465         return -1;
2466     idx = SUBPAGE_IDX(start);
2467     eidx = SUBPAGE_IDX(end);
2468 #if defined(DEBUG_SUBPAGE)
2469     printf("%s: %p start %08x end %08x idx %08x eidx %08x section %d\n",
2470            __func__, mmio, start, end, idx, eidx, section);
2471 #endif
2472     for (; idx <= eidx; idx++) {
2473         mmio->sub_section[idx] = section;
2474     }
2475 
2476     return 0;
2477 }
2478 
subpage_init(FlatView * fv,hwaddr base)2479 static subpage_t *subpage_init(FlatView *fv, hwaddr base)
2480 {
2481     subpage_t *mmio;
2482 
2483     /* mmio->sub_section is set to PHYS_SECTION_UNASSIGNED with g_malloc0 */
2484     mmio = g_malloc0(sizeof(subpage_t) + TARGET_PAGE_SIZE * sizeof(uint16_t));
2485     mmio->fv = fv;
2486     mmio->base = base;
2487     memory_region_init_io(&mmio->iomem, NULL, &subpage_ops, mmio,
2488                           NULL, TARGET_PAGE_SIZE);
2489     mmio->iomem.subpage = true;
2490 #if defined(DEBUG_SUBPAGE)
2491     printf("%s: %p base " HWADDR_FMT_plx " len %08x\n", __func__,
2492            mmio, base, TARGET_PAGE_SIZE);
2493 #endif
2494 
2495     return mmio;
2496 }
2497 
dummy_section(PhysPageMap * map,FlatView * fv,MemoryRegion * mr)2498 static uint16_t dummy_section(PhysPageMap *map, FlatView *fv, MemoryRegion *mr)
2499 {
2500     assert(fv);
2501     MemoryRegionSection section = {
2502         .fv = fv,
2503         .mr = mr,
2504         .offset_within_address_space = 0,
2505         .offset_within_region = 0,
2506         .size = int128_2_64(),
2507     };
2508 
2509     return phys_section_add(map, &section);
2510 }
2511 
iotlb_to_section(CPUState * cpu,hwaddr index,MemTxAttrs attrs)2512 MemoryRegionSection *iotlb_to_section(CPUState *cpu,
2513                                       hwaddr index, MemTxAttrs attrs)
2514 {
2515     int asidx = cpu_asidx_from_attrs(cpu, attrs);
2516     CPUAddressSpace *cpuas = &cpu->cpu_ases[asidx];
2517     AddressSpaceDispatch *d = cpuas->memory_dispatch;
2518     int section_index = index & ~TARGET_PAGE_MASK;
2519     MemoryRegionSection *ret;
2520 
2521     assert(section_index < d->map.sections_nb);
2522     ret = d->map.sections + section_index;
2523     assert(ret->mr);
2524     assert(ret->mr->ops);
2525 
2526     return ret;
2527 }
2528 
io_mem_init(void)2529 static void io_mem_init(void)
2530 {
2531     memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, NULL,
2532                           NULL, UINT64_MAX);
2533 }
2534 
address_space_dispatch_new(FlatView * fv)2535 AddressSpaceDispatch *address_space_dispatch_new(FlatView *fv)
2536 {
2537     AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1);
2538     uint16_t n;
2539 
2540     n = dummy_section(&d->map, fv, &io_mem_unassigned);
2541     assert(n == PHYS_SECTION_UNASSIGNED);
2542 
2543     d->phys_map  = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };
2544 
2545     return d;
2546 }
2547 
address_space_dispatch_free(AddressSpaceDispatch * d)2548 void address_space_dispatch_free(AddressSpaceDispatch *d)
2549 {
2550     phys_sections_free(&d->map);
2551     g_free(d);
2552 }
2553 
do_nothing(CPUState * cpu,run_on_cpu_data d)2554 static void do_nothing(CPUState *cpu, run_on_cpu_data d)
2555 {
2556 }
2557 
tcg_log_global_after_sync(MemoryListener * listener)2558 static void tcg_log_global_after_sync(MemoryListener *listener)
2559 {
2560     CPUAddressSpace *cpuas;
2561 
2562     /* Wait for the CPU to end the current TB.  This avoids the following
2563      * incorrect race:
2564      *
2565      *      vCPU                         migration
2566      *      ----------------------       -------------------------
2567      *      TLB check -> slow path
2568      *        notdirty_mem_write
2569      *          write to RAM
2570      *          mark dirty
2571      *                                   clear dirty flag
2572      *      TLB check -> fast path
2573      *                                   read memory
2574      *        write to RAM
2575      *
2576      * by pushing the migration thread's memory read after the vCPU thread has
2577      * written the memory.
2578      */
2579     if (replay_mode == REPLAY_MODE_NONE) {
2580         /*
2581          * VGA can make calls to this function while updating the screen.
2582          * In record/replay mode this causes a deadlock, because
2583          * run_on_cpu waits for rr mutex. Therefore no races are possible
2584          * in this case and no need for making run_on_cpu when
2585          * record/replay is enabled.
2586          */
2587         cpuas = container_of(listener, CPUAddressSpace, tcg_as_listener);
2588         run_on_cpu(cpuas->cpu, do_nothing, RUN_ON_CPU_NULL);
2589     }
2590 }
2591 
tcg_commit_cpu(CPUState * cpu,run_on_cpu_data data)2592 static void tcg_commit_cpu(CPUState *cpu, run_on_cpu_data data)
2593 {
2594     CPUAddressSpace *cpuas = data.host_ptr;
2595 
2596     cpuas->memory_dispatch = address_space_to_dispatch(cpuas->as);
2597     tlb_flush(cpu);
2598 }
2599 
tcg_commit(MemoryListener * listener)2600 static void tcg_commit(MemoryListener *listener)
2601 {
2602     CPUAddressSpace *cpuas;
2603     CPUState *cpu;
2604 
2605     assert(tcg_enabled());
2606     /* since each CPU stores ram addresses in its TLB cache, we must
2607        reset the modified entries */
2608     cpuas = container_of(listener, CPUAddressSpace, tcg_as_listener);
2609     cpu = cpuas->cpu;
2610 
2611     /*
2612      * Defer changes to as->memory_dispatch until the cpu is quiescent.
2613      * Otherwise we race between (1) other cpu threads and (2) ongoing
2614      * i/o for the current cpu thread, with data cached by mmu_lookup().
2615      *
2616      * In addition, queueing the work function will kick the cpu back to
2617      * the main loop, which will end the RCU critical section and reclaim
2618      * the memory data structures.
2619      *
2620      * That said, the listener is also called during realize, before
2621      * all of the tcg machinery for run-on is initialized: thus halt_cond.
2622      */
2623     if (cpu->halt_cond) {
2624         async_run_on_cpu(cpu, tcg_commit_cpu, RUN_ON_CPU_HOST_PTR(cpuas));
2625     } else {
2626         tcg_commit_cpu(cpu, RUN_ON_CPU_HOST_PTR(cpuas));
2627     }
2628 }
2629 
memory_map_init(void)2630 static void memory_map_init(void)
2631 {
2632     system_memory = g_malloc(sizeof(*system_memory));
2633 
2634     memory_region_init(system_memory, NULL, "system", UINT64_MAX);
2635     address_space_init(&address_space_memory, system_memory, "memory");
2636 
2637     system_io = g_malloc(sizeof(*system_io));
2638     memory_region_init_io(system_io, NULL, &unassigned_io_ops, NULL, "io",
2639                           65536);
2640     address_space_init(&address_space_io, system_io, "I/O");
2641 }
2642 
get_system_memory(void)2643 MemoryRegion *get_system_memory(void)
2644 {
2645     return system_memory;
2646 }
2647 
get_system_io(void)2648 MemoryRegion *get_system_io(void)
2649 {
2650     return system_io;
2651 }
2652 
invalidate_and_set_dirty(MemoryRegion * mr,hwaddr addr,hwaddr length)2653 static void invalidate_and_set_dirty(MemoryRegion *mr, hwaddr addr,
2654                                      hwaddr length)
2655 {
2656     uint8_t dirty_log_mask = memory_region_get_dirty_log_mask(mr);
2657     ram_addr_t ramaddr = memory_region_get_ram_addr(mr);
2658 
2659     /* We know we're only called for RAM MemoryRegions */
2660     assert(ramaddr != RAM_ADDR_INVALID);
2661     addr += ramaddr;
2662 
2663     /* No early return if dirty_log_mask is or becomes 0, because
2664      * cpu_physical_memory_set_dirty_range will still call
2665      * xen_modified_memory.
2666      */
2667     if (dirty_log_mask) {
2668         dirty_log_mask =
2669             cpu_physical_memory_range_includes_clean(addr, length, dirty_log_mask);
2670     }
2671     if (dirty_log_mask & (1 << DIRTY_MEMORY_CODE)) {
2672         assert(tcg_enabled());
2673         tb_invalidate_phys_range(addr, addr + length - 1);
2674         dirty_log_mask &= ~(1 << DIRTY_MEMORY_CODE);
2675     }
2676     cpu_physical_memory_set_dirty_range(addr, length, dirty_log_mask);
2677 }
2678 
memory_region_flush_rom_device(MemoryRegion * mr,hwaddr addr,hwaddr size)2679 void memory_region_flush_rom_device(MemoryRegion *mr, hwaddr addr, hwaddr size)
2680 {
2681     /*
2682      * In principle this function would work on other memory region types too,
2683      * but the ROM device use case is the only one where this operation is
2684      * necessary.  Other memory regions should use the
2685      * address_space_read/write() APIs.
2686      */
2687     assert(memory_region_is_romd(mr));
2688 
2689     invalidate_and_set_dirty(mr, addr, size);
2690 }
2691 
memory_access_size(MemoryRegion * mr,unsigned l,hwaddr addr)2692 int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
2693 {
2694     unsigned access_size_max = mr->ops->valid.max_access_size;
2695 
2696     /* Regions are assumed to support 1-4 byte accesses unless
2697        otherwise specified.  */
2698     if (access_size_max == 0) {
2699         access_size_max = 4;
2700     }
2701 
2702     /* Bound the maximum access by the alignment of the address.  */
2703     if (!mr->ops->impl.unaligned) {
2704         unsigned align_size_max = addr & -addr;
2705         if (align_size_max != 0 && align_size_max < access_size_max) {
2706             access_size_max = align_size_max;
2707         }
2708     }
2709 
2710     /* Don't attempt accesses larger than the maximum.  */
2711     if (l > access_size_max) {
2712         l = access_size_max;
2713     }
2714     l = pow2floor(l);
2715 
2716     return l;
2717 }
2718 
prepare_mmio_access(MemoryRegion * mr)2719 bool prepare_mmio_access(MemoryRegion *mr)
2720 {
2721     bool release_lock = false;
2722 
2723     if (!bql_locked()) {
2724         bql_lock();
2725         release_lock = true;
2726     }
2727     if (mr->flush_coalesced_mmio) {
2728         qemu_flush_coalesced_mmio_buffer();
2729     }
2730 
2731     return release_lock;
2732 }
2733 
2734 /**
2735  * flatview_access_allowed
2736  * @mr: #MemoryRegion to be accessed
2737  * @attrs: memory transaction attributes
2738  * @addr: address within that memory region
2739  * @len: the number of bytes to access
2740  *
2741  * Check if a memory transaction is allowed.
2742  *
2743  * Returns: true if transaction is allowed, false if denied.
2744  */
flatview_access_allowed(MemoryRegion * mr,MemTxAttrs attrs,hwaddr addr,hwaddr len)2745 static bool flatview_access_allowed(MemoryRegion *mr, MemTxAttrs attrs,
2746                                     hwaddr addr, hwaddr len)
2747 {
2748     if (likely(!attrs.memory)) {
2749         return true;
2750     }
2751     if (memory_region_is_ram(mr)) {
2752         return true;
2753     }
2754     qemu_log_mask(LOG_GUEST_ERROR,
2755                   "Invalid access to non-RAM device at "
2756                   "addr 0x%" HWADDR_PRIX ", size %" HWADDR_PRIu ", "
2757                   "region '%s'\n", addr, len, memory_region_name(mr));
2758     return false;
2759 }
2760 
flatview_write_continue_step(MemTxAttrs attrs,const uint8_t * buf,hwaddr len,hwaddr mr_addr,hwaddr * l,MemoryRegion * mr)2761 static MemTxResult flatview_write_continue_step(MemTxAttrs attrs,
2762                                                 const uint8_t *buf,
2763                                                 hwaddr len, hwaddr mr_addr,
2764                                                 hwaddr *l, MemoryRegion *mr)
2765 {
2766     if (!flatview_access_allowed(mr, attrs, mr_addr, *l)) {
2767         return MEMTX_ACCESS_ERROR;
2768     }
2769 
2770     if (!memory_access_is_direct(mr, true)) {
2771         uint64_t val;
2772         MemTxResult result;
2773         bool release_lock = prepare_mmio_access(mr);
2774 
2775         *l = memory_access_size(mr, *l, mr_addr);
2776         /*
2777          * XXX: could force current_cpu to NULL to avoid
2778          * potential bugs
2779          */
2780 
2781         /*
2782          * Assure Coverity (and ourselves) that we are not going to OVERRUN
2783          * the buffer by following ldn_he_p().
2784          */
2785 #ifdef QEMU_STATIC_ANALYSIS
2786         assert((*l == 1 && len >= 1) ||
2787                (*l == 2 && len >= 2) ||
2788                (*l == 4 && len >= 4) ||
2789                (*l == 8 && len >= 8));
2790 #endif
2791         val = ldn_he_p(buf, *l);
2792         result = memory_region_dispatch_write(mr, mr_addr, val,
2793                                               size_memop(*l), attrs);
2794         if (release_lock) {
2795             bql_unlock();
2796         }
2797 
2798         return result;
2799     } else {
2800         /* RAM case */
2801         uint8_t *ram_ptr = qemu_ram_ptr_length(mr->ram_block, mr_addr, l,
2802                                                false, true);
2803 
2804         memmove(ram_ptr, buf, *l);
2805         invalidate_and_set_dirty(mr, mr_addr, *l);
2806 
2807         return MEMTX_OK;
2808     }
2809 }
2810 
2811 /* Called within RCU critical section.  */
flatview_write_continue(FlatView * fv,hwaddr addr,MemTxAttrs attrs,const void * ptr,hwaddr len,hwaddr mr_addr,hwaddr l,MemoryRegion * mr)2812 static MemTxResult flatview_write_continue(FlatView *fv, hwaddr addr,
2813                                            MemTxAttrs attrs,
2814                                            const void *ptr,
2815                                            hwaddr len, hwaddr mr_addr,
2816                                            hwaddr l, MemoryRegion *mr)
2817 {
2818     MemTxResult result = MEMTX_OK;
2819     const uint8_t *buf = ptr;
2820 
2821     for (;;) {
2822         result |= flatview_write_continue_step(attrs, buf, len, mr_addr, &l,
2823                                                mr);
2824 
2825         len -= l;
2826         buf += l;
2827         addr += l;
2828 
2829         if (!len) {
2830             break;
2831         }
2832 
2833         l = len;
2834         mr = flatview_translate(fv, addr, &mr_addr, &l, true, attrs);
2835     }
2836 
2837     return result;
2838 }
2839 
2840 /* Called from RCU critical section.  */
flatview_write(FlatView * fv,hwaddr addr,MemTxAttrs attrs,const void * buf,hwaddr len)2841 static MemTxResult flatview_write(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
2842                                   const void *buf, hwaddr len)
2843 {
2844     hwaddr l;
2845     hwaddr mr_addr;
2846     MemoryRegion *mr;
2847 
2848     l = len;
2849     mr = flatview_translate(fv, addr, &mr_addr, &l, true, attrs);
2850     if (!flatview_access_allowed(mr, attrs, addr, len)) {
2851         return MEMTX_ACCESS_ERROR;
2852     }
2853     return flatview_write_continue(fv, addr, attrs, buf, len,
2854                                    mr_addr, l, mr);
2855 }
2856 
flatview_read_continue_step(MemTxAttrs attrs,uint8_t * buf,hwaddr len,hwaddr mr_addr,hwaddr * l,MemoryRegion * mr)2857 static MemTxResult flatview_read_continue_step(MemTxAttrs attrs, uint8_t *buf,
2858                                                hwaddr len, hwaddr mr_addr,
2859                                                hwaddr *l,
2860                                                MemoryRegion *mr)
2861 {
2862     if (!flatview_access_allowed(mr, attrs, mr_addr, *l)) {
2863         return MEMTX_ACCESS_ERROR;
2864     }
2865 
2866     if (!memory_access_is_direct(mr, false)) {
2867         /* I/O case */
2868         uint64_t val;
2869         MemTxResult result;
2870         bool release_lock = prepare_mmio_access(mr);
2871 
2872         *l = memory_access_size(mr, *l, mr_addr);
2873         result = memory_region_dispatch_read(mr, mr_addr, &val, size_memop(*l),
2874                                              attrs);
2875 
2876         /*
2877          * Assure Coverity (and ourselves) that we are not going to OVERRUN
2878          * the buffer by following stn_he_p().
2879          */
2880 #ifdef QEMU_STATIC_ANALYSIS
2881         assert((*l == 1 && len >= 1) ||
2882                (*l == 2 && len >= 2) ||
2883                (*l == 4 && len >= 4) ||
2884                (*l == 8 && len >= 8));
2885 #endif
2886         stn_he_p(buf, *l, val);
2887 
2888         if (release_lock) {
2889             bql_unlock();
2890         }
2891         return result;
2892     } else {
2893         /* RAM case */
2894         uint8_t *ram_ptr = qemu_ram_ptr_length(mr->ram_block, mr_addr, l,
2895                                                false, false);
2896 
2897         memcpy(buf, ram_ptr, *l);
2898 
2899         return MEMTX_OK;
2900     }
2901 }
2902 
2903 /* Called within RCU critical section.  */
flatview_read_continue(FlatView * fv,hwaddr addr,MemTxAttrs attrs,void * ptr,hwaddr len,hwaddr mr_addr,hwaddr l,MemoryRegion * mr)2904 MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
2905                                    MemTxAttrs attrs, void *ptr,
2906                                    hwaddr len, hwaddr mr_addr, hwaddr l,
2907                                    MemoryRegion *mr)
2908 {
2909     MemTxResult result = MEMTX_OK;
2910     uint8_t *buf = ptr;
2911 
2912     fuzz_dma_read_cb(addr, len, mr);
2913     for (;;) {
2914         result |= flatview_read_continue_step(attrs, buf, len, mr_addr, &l, mr);
2915 
2916         len -= l;
2917         buf += l;
2918         addr += l;
2919 
2920         if (!len) {
2921             break;
2922         }
2923 
2924         l = len;
2925         mr = flatview_translate(fv, addr, &mr_addr, &l, false, attrs);
2926     }
2927 
2928     return result;
2929 }
2930 
2931 /* Called from RCU critical section.  */
flatview_read(FlatView * fv,hwaddr addr,MemTxAttrs attrs,void * buf,hwaddr len)2932 static MemTxResult flatview_read(FlatView *fv, hwaddr addr,
2933                                  MemTxAttrs attrs, void *buf, hwaddr len)
2934 {
2935     hwaddr l;
2936     hwaddr mr_addr;
2937     MemoryRegion *mr;
2938 
2939     l = len;
2940     mr = flatview_translate(fv, addr, &mr_addr, &l, false, attrs);
2941     if (!flatview_access_allowed(mr, attrs, addr, len)) {
2942         return MEMTX_ACCESS_ERROR;
2943     }
2944     return flatview_read_continue(fv, addr, attrs, buf, len,
2945                                   mr_addr, l, mr);
2946 }
2947 
address_space_read_full(AddressSpace * as,hwaddr addr,MemTxAttrs attrs,void * buf,hwaddr len)2948 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
2949                                     MemTxAttrs attrs, void *buf, hwaddr len)
2950 {
2951     MemTxResult result = MEMTX_OK;
2952     FlatView *fv;
2953 
2954     if (len > 0) {
2955         RCU_READ_LOCK_GUARD();
2956         fv = address_space_to_flatview(as);
2957         result = flatview_read(fv, addr, attrs, buf, len);
2958     }
2959 
2960     return result;
2961 }
2962 
address_space_write(AddressSpace * as,hwaddr addr,MemTxAttrs attrs,const void * buf,hwaddr len)2963 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
2964                                 MemTxAttrs attrs,
2965                                 const void *buf, hwaddr len)
2966 {
2967     MemTxResult result = MEMTX_OK;
2968     FlatView *fv;
2969 
2970     if (len > 0) {
2971         RCU_READ_LOCK_GUARD();
2972         fv = address_space_to_flatview(as);
2973         result = flatview_write(fv, addr, attrs, buf, len);
2974     }
2975 
2976     return result;
2977 }
2978 
address_space_rw(AddressSpace * as,hwaddr addr,MemTxAttrs attrs,void * buf,hwaddr len,bool is_write)2979 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
2980                              void *buf, hwaddr len, bool is_write)
2981 {
2982     if (is_write) {
2983         return address_space_write(as, addr, attrs, buf, len);
2984     } else {
2985         return address_space_read_full(as, addr, attrs, buf, len);
2986     }
2987 }
2988 
address_space_set(AddressSpace * as,hwaddr addr,uint8_t c,hwaddr len,MemTxAttrs attrs)2989 MemTxResult address_space_set(AddressSpace *as, hwaddr addr,
2990                               uint8_t c, hwaddr len, MemTxAttrs attrs)
2991 {
2992 #define FILLBUF_SIZE 512
2993     uint8_t fillbuf[FILLBUF_SIZE];
2994     int l;
2995     MemTxResult error = MEMTX_OK;
2996 
2997     memset(fillbuf, c, FILLBUF_SIZE);
2998     while (len > 0) {
2999         l = len < FILLBUF_SIZE ? len : FILLBUF_SIZE;
3000         error |= address_space_write(as, addr, attrs, fillbuf, l);
3001         len -= l;
3002         addr += l;
3003     }
3004 
3005     return error;
3006 }
3007 
cpu_physical_memory_rw(hwaddr addr,void * buf,hwaddr len,bool is_write)3008 void cpu_physical_memory_rw(hwaddr addr, void *buf,
3009                             hwaddr len, bool is_write)
3010 {
3011     address_space_rw(&address_space_memory, addr, MEMTXATTRS_UNSPECIFIED,
3012                      buf, len, is_write);
3013 }
3014 
3015 enum write_rom_type {
3016     WRITE_DATA,
3017     FLUSH_CACHE,
3018 };
3019 
address_space_write_rom_internal(AddressSpace * as,hwaddr addr,MemTxAttrs attrs,const void * ptr,hwaddr len,enum write_rom_type type)3020 static inline MemTxResult address_space_write_rom_internal(AddressSpace *as,
3021                                                            hwaddr addr,
3022                                                            MemTxAttrs attrs,
3023                                                            const void *ptr,
3024                                                            hwaddr len,
3025                                                            enum write_rom_type type)
3026 {
3027     hwaddr l;
3028     uint8_t *ram_ptr;
3029     hwaddr addr1;
3030     MemoryRegion *mr;
3031     const uint8_t *buf = ptr;
3032 
3033     RCU_READ_LOCK_GUARD();
3034     while (len > 0) {
3035         l = len;
3036         mr = address_space_translate(as, addr, &addr1, &l, true, attrs);
3037 
3038         if (!(memory_region_is_ram(mr) ||
3039               memory_region_is_romd(mr))) {
3040             l = memory_access_size(mr, l, addr1);
3041         } else {
3042             /* ROM/RAM case */
3043             ram_ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
3044             switch (type) {
3045             case WRITE_DATA:
3046                 memcpy(ram_ptr, buf, l);
3047                 invalidate_and_set_dirty(mr, addr1, l);
3048                 break;
3049             case FLUSH_CACHE:
3050                 flush_idcache_range((uintptr_t)ram_ptr, (uintptr_t)ram_ptr, l);
3051                 break;
3052             }
3053         }
3054         len -= l;
3055         buf += l;
3056         addr += l;
3057     }
3058     return MEMTX_OK;
3059 }
3060 
3061 /* used for ROM loading : can write in RAM and ROM */
address_space_write_rom(AddressSpace * as,hwaddr addr,MemTxAttrs attrs,const void * buf,hwaddr len)3062 MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
3063                                     MemTxAttrs attrs,
3064                                     const void *buf, hwaddr len)
3065 {
3066     return address_space_write_rom_internal(as, addr, attrs,
3067                                             buf, len, WRITE_DATA);
3068 }
3069 
cpu_flush_icache_range(hwaddr start,hwaddr len)3070 void cpu_flush_icache_range(hwaddr start, hwaddr len)
3071 {
3072     /*
3073      * This function should do the same thing as an icache flush that was
3074      * triggered from within the guest. For TCG we are always cache coherent,
3075      * so there is no need to flush anything. For KVM / Xen we need to flush
3076      * the host's instruction cache at least.
3077      */
3078     if (tcg_enabled()) {
3079         return;
3080     }
3081 
3082     address_space_write_rom_internal(&address_space_memory,
3083                                      start, MEMTXATTRS_UNSPECIFIED,
3084                                      NULL, len, FLUSH_CACHE);
3085 }
3086 
3087 /*
3088  * A magic value stored in the first 8 bytes of the bounce buffer struct. Used
3089  * to detect illegal pointers passed to address_space_unmap.
3090  */
3091 #define BOUNCE_BUFFER_MAGIC 0xb4017ceb4ffe12ed
3092 
3093 typedef struct {
3094     uint64_t magic;
3095     MemoryRegion *mr;
3096     hwaddr addr;
3097     size_t len;
3098     uint8_t buffer[];
3099 } BounceBuffer;
3100 
3101 static void
address_space_unregister_map_client_do(AddressSpaceMapClient * client)3102 address_space_unregister_map_client_do(AddressSpaceMapClient *client)
3103 {
3104     QLIST_REMOVE(client, link);
3105     g_free(client);
3106 }
3107 
address_space_notify_map_clients_locked(AddressSpace * as)3108 static void address_space_notify_map_clients_locked(AddressSpace *as)
3109 {
3110     AddressSpaceMapClient *client;
3111 
3112     while (!QLIST_EMPTY(&as->map_client_list)) {
3113         client = QLIST_FIRST(&as->map_client_list);
3114         qemu_bh_schedule(client->bh);
3115         address_space_unregister_map_client_do(client);
3116     }
3117 }
3118 
address_space_register_map_client(AddressSpace * as,QEMUBH * bh)3119 void address_space_register_map_client(AddressSpace *as, QEMUBH *bh)
3120 {
3121     AddressSpaceMapClient *client = g_malloc(sizeof(*client));
3122 
3123     QEMU_LOCK_GUARD(&as->map_client_list_lock);
3124     client->bh = bh;
3125     QLIST_INSERT_HEAD(&as->map_client_list, client, link);
3126     /* Write map_client_list before reading bounce_buffer_size. */
3127     smp_mb();
3128     if (qatomic_read(&as->bounce_buffer_size) < as->max_bounce_buffer_size) {
3129         address_space_notify_map_clients_locked(as);
3130     }
3131 }
3132 
cpu_exec_init_all(void)3133 void cpu_exec_init_all(void)
3134 {
3135     qemu_mutex_init(&ram_list.mutex);
3136     /* The data structures we set up here depend on knowing the page size,
3137      * so no more changes can be made after this point.
3138      * In an ideal world, nothing we did before we had finished the
3139      * machine setup would care about the target page size, and we could
3140      * do this much later, rather than requiring board models to state
3141      * up front what their requirements are.
3142      */
3143     finalize_target_page_bits();
3144     io_mem_init();
3145     memory_map_init();
3146 }
3147 
address_space_unregister_map_client(AddressSpace * as,QEMUBH * bh)3148 void address_space_unregister_map_client(AddressSpace *as, QEMUBH *bh)
3149 {
3150     AddressSpaceMapClient *client;
3151 
3152     QEMU_LOCK_GUARD(&as->map_client_list_lock);
3153     QLIST_FOREACH(client, &as->map_client_list, link) {
3154         if (client->bh == bh) {
3155             address_space_unregister_map_client_do(client);
3156             break;
3157         }
3158     }
3159 }
3160 
address_space_notify_map_clients(AddressSpace * as)3161 static void address_space_notify_map_clients(AddressSpace *as)
3162 {
3163     QEMU_LOCK_GUARD(&as->map_client_list_lock);
3164     address_space_notify_map_clients_locked(as);
3165 }
3166 
flatview_access_valid(FlatView * fv,hwaddr addr,hwaddr len,bool is_write,MemTxAttrs attrs)3167 static bool flatview_access_valid(FlatView *fv, hwaddr addr, hwaddr len,
3168                                   bool is_write, MemTxAttrs attrs)
3169 {
3170     MemoryRegion *mr;
3171     hwaddr l, xlat;
3172 
3173     while (len > 0) {
3174         l = len;
3175         mr = flatview_translate(fv, addr, &xlat, &l, is_write, attrs);
3176         if (!memory_access_is_direct(mr, is_write)) {
3177             l = memory_access_size(mr, l, addr);
3178             if (!memory_region_access_valid(mr, xlat, l, is_write, attrs)) {
3179                 return false;
3180             }
3181         }
3182 
3183         len -= l;
3184         addr += l;
3185     }
3186     return true;
3187 }
3188 
address_space_access_valid(AddressSpace * as,hwaddr addr,hwaddr len,bool is_write,MemTxAttrs attrs)3189 bool address_space_access_valid(AddressSpace *as, hwaddr addr,
3190                                 hwaddr len, bool is_write,
3191                                 MemTxAttrs attrs)
3192 {
3193     FlatView *fv;
3194 
3195     RCU_READ_LOCK_GUARD();
3196     fv = address_space_to_flatview(as);
3197     return flatview_access_valid(fv, addr, len, is_write, attrs);
3198 }
3199 
3200 static hwaddr
flatview_extend_translation(FlatView * fv,hwaddr addr,hwaddr target_len,MemoryRegion * mr,hwaddr base,hwaddr len,bool is_write,MemTxAttrs attrs)3201 flatview_extend_translation(FlatView *fv, hwaddr addr,
3202                             hwaddr target_len,
3203                             MemoryRegion *mr, hwaddr base, hwaddr len,
3204                             bool is_write, MemTxAttrs attrs)
3205 {
3206     hwaddr done = 0;
3207     hwaddr xlat;
3208     MemoryRegion *this_mr;
3209 
3210     for (;;) {
3211         target_len -= len;
3212         addr += len;
3213         done += len;
3214         if (target_len == 0) {
3215             return done;
3216         }
3217 
3218         len = target_len;
3219         this_mr = flatview_translate(fv, addr, &xlat,
3220                                      &len, is_write, attrs);
3221         if (this_mr != mr || xlat != base + done) {
3222             return done;
3223         }
3224     }
3225 }
3226 
3227 /* Map a physical memory region into a host virtual address.
3228  * May map a subset of the requested range, given by and returned in *plen.
3229  * May return NULL if resources needed to perform the mapping are exhausted.
3230  * Use only for reads OR writes - not for read-modify-write operations.
3231  * Use address_space_register_map_client() to know when retrying the map
3232  * operation is likely to succeed.
3233  */
address_space_map(AddressSpace * as,hwaddr addr,hwaddr * plen,bool is_write,MemTxAttrs attrs)3234 void *address_space_map(AddressSpace *as,
3235                         hwaddr addr,
3236                         hwaddr *plen,
3237                         bool is_write,
3238                         MemTxAttrs attrs)
3239 {
3240     hwaddr len = *plen;
3241     hwaddr l, xlat;
3242     MemoryRegion *mr;
3243     FlatView *fv;
3244 
3245     trace_address_space_map(as, addr, len, is_write, *(uint32_t *) &attrs);
3246 
3247     if (len == 0) {
3248         return NULL;
3249     }
3250 
3251     l = len;
3252     RCU_READ_LOCK_GUARD();
3253     fv = address_space_to_flatview(as);
3254     mr = flatview_translate(fv, addr, &xlat, &l, is_write, attrs);
3255 
3256     if (!memory_access_is_direct(mr, is_write)) {
3257         size_t used = qatomic_read(&as->bounce_buffer_size);
3258         for (;;) {
3259             hwaddr alloc = MIN(as->max_bounce_buffer_size - used, l);
3260             size_t new_size = used + alloc;
3261             size_t actual =
3262                 qatomic_cmpxchg(&as->bounce_buffer_size, used, new_size);
3263             if (actual == used) {
3264                 l = alloc;
3265                 break;
3266             }
3267             used = actual;
3268         }
3269 
3270         if (l == 0) {
3271             *plen = 0;
3272             return NULL;
3273         }
3274 
3275         BounceBuffer *bounce = g_malloc0(l + sizeof(BounceBuffer));
3276         bounce->magic = BOUNCE_BUFFER_MAGIC;
3277         memory_region_ref(mr);
3278         bounce->mr = mr;
3279         bounce->addr = addr;
3280         bounce->len = l;
3281 
3282         if (!is_write) {
3283             flatview_read(fv, addr, attrs,
3284                           bounce->buffer, l);
3285         }
3286 
3287         *plen = l;
3288         return bounce->buffer;
3289     }
3290 
3291     memory_region_ref(mr);
3292     *plen = flatview_extend_translation(fv, addr, len, mr, xlat,
3293                                         l, is_write, attrs);
3294     fuzz_dma_read_cb(addr, *plen, mr);
3295     return qemu_ram_ptr_length(mr->ram_block, xlat, plen, true, is_write);
3296 }
3297 
3298 /* Unmaps a memory region previously mapped by address_space_map().
3299  * Will also mark the memory as dirty if is_write is true.  access_len gives
3300  * the amount of memory that was actually read or written by the caller.
3301  */
address_space_unmap(AddressSpace * as,void * buffer,hwaddr len,bool is_write,hwaddr access_len)3302 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
3303                          bool is_write, hwaddr access_len)
3304 {
3305     MemoryRegion *mr;
3306     ram_addr_t addr1;
3307 
3308     mr = memory_region_from_host(buffer, &addr1);
3309     if (mr != NULL) {
3310         if (is_write) {
3311             invalidate_and_set_dirty(mr, addr1, access_len);
3312         }
3313         if (xen_enabled()) {
3314             xen_invalidate_map_cache_entry(buffer);
3315         }
3316         memory_region_unref(mr);
3317         return;
3318     }
3319 
3320 
3321     BounceBuffer *bounce = container_of(buffer, BounceBuffer, buffer);
3322     assert(bounce->magic == BOUNCE_BUFFER_MAGIC);
3323 
3324     if (is_write) {
3325         address_space_write(as, bounce->addr, MEMTXATTRS_UNSPECIFIED,
3326                             bounce->buffer, access_len);
3327     }
3328 
3329     qatomic_sub(&as->bounce_buffer_size, bounce->len);
3330     bounce->magic = ~BOUNCE_BUFFER_MAGIC;
3331     memory_region_unref(bounce->mr);
3332     g_free(bounce);
3333     /* Write bounce_buffer_size before reading map_client_list. */
3334     smp_mb();
3335     address_space_notify_map_clients(as);
3336 }
3337 
cpu_physical_memory_map(hwaddr addr,hwaddr * plen,bool is_write)3338 void *cpu_physical_memory_map(hwaddr addr,
3339                               hwaddr *plen,
3340                               bool is_write)
3341 {
3342     return address_space_map(&address_space_memory, addr, plen, is_write,
3343                              MEMTXATTRS_UNSPECIFIED);
3344 }
3345 
cpu_physical_memory_unmap(void * buffer,hwaddr len,bool is_write,hwaddr access_len)3346 void cpu_physical_memory_unmap(void *buffer, hwaddr len,
3347                                bool is_write, hwaddr access_len)
3348 {
3349     return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
3350 }
3351 
3352 #define ARG1_DECL                AddressSpace *as
3353 #define ARG1                     as
3354 #define SUFFIX
3355 #define TRANSLATE(...)           address_space_translate(as, __VA_ARGS__)
3356 #define RCU_READ_LOCK(...)       rcu_read_lock()
3357 #define RCU_READ_UNLOCK(...)     rcu_read_unlock()
3358 #include "memory_ldst.c.inc"
3359 
address_space_cache_init(MemoryRegionCache * cache,AddressSpace * as,hwaddr addr,hwaddr len,bool is_write)3360 int64_t address_space_cache_init(MemoryRegionCache *cache,
3361                                  AddressSpace *as,
3362                                  hwaddr addr,
3363                                  hwaddr len,
3364                                  bool is_write)
3365 {
3366     AddressSpaceDispatch *d;
3367     hwaddr l;
3368     MemoryRegion *mr;
3369     Int128 diff;
3370 
3371     assert(len > 0);
3372 
3373     l = len;
3374     cache->fv = address_space_get_flatview(as);
3375     d = flatview_to_dispatch(cache->fv);
3376     cache->mrs = *address_space_translate_internal(d, addr, &cache->xlat, &l, true);
3377 
3378     /*
3379      * cache->xlat is now relative to cache->mrs.mr, not to the section itself.
3380      * Take that into account to compute how many bytes are there between
3381      * cache->xlat and the end of the section.
3382      */
3383     diff = int128_sub(cache->mrs.size,
3384                       int128_make64(cache->xlat - cache->mrs.offset_within_region));
3385     l = int128_get64(int128_min(diff, int128_make64(l)));
3386 
3387     mr = cache->mrs.mr;
3388     memory_region_ref(mr);
3389     if (memory_access_is_direct(mr, is_write)) {
3390         /* We don't care about the memory attributes here as we're only
3391          * doing this if we found actual RAM, which behaves the same
3392          * regardless of attributes; so UNSPECIFIED is fine.
3393          */
3394         l = flatview_extend_translation(cache->fv, addr, len, mr,
3395                                         cache->xlat, l, is_write,
3396                                         MEMTXATTRS_UNSPECIFIED);
3397         cache->ptr = qemu_ram_ptr_length(mr->ram_block, cache->xlat, &l, true,
3398                                          is_write);
3399     } else {
3400         cache->ptr = NULL;
3401     }
3402 
3403     cache->len = l;
3404     cache->is_write = is_write;
3405     return l;
3406 }
3407 
address_space_cache_invalidate(MemoryRegionCache * cache,hwaddr addr,hwaddr access_len)3408 void address_space_cache_invalidate(MemoryRegionCache *cache,
3409                                     hwaddr addr,
3410                                     hwaddr access_len)
3411 {
3412     assert(cache->is_write);
3413     if (likely(cache->ptr)) {
3414         invalidate_and_set_dirty(cache->mrs.mr, addr + cache->xlat, access_len);
3415     }
3416 }
3417 
address_space_cache_destroy(MemoryRegionCache * cache)3418 void address_space_cache_destroy(MemoryRegionCache *cache)
3419 {
3420     if (!cache->mrs.mr) {
3421         return;
3422     }
3423 
3424     if (xen_enabled()) {
3425         xen_invalidate_map_cache_entry(cache->ptr);
3426     }
3427     memory_region_unref(cache->mrs.mr);
3428     flatview_unref(cache->fv);
3429     cache->mrs.mr = NULL;
3430     cache->fv = NULL;
3431 }
3432 
3433 /* Called from RCU critical section.  This function has the same
3434  * semantics as address_space_translate, but it only works on a
3435  * predefined range of a MemoryRegion that was mapped with
3436  * address_space_cache_init.
3437  */
address_space_translate_cached(MemoryRegionCache * cache,hwaddr addr,hwaddr * xlat,hwaddr * plen,bool is_write,MemTxAttrs attrs)3438 static inline MemoryRegion *address_space_translate_cached(
3439     MemoryRegionCache *cache, hwaddr addr, hwaddr *xlat,
3440     hwaddr *plen, bool is_write, MemTxAttrs attrs)
3441 {
3442     MemoryRegionSection section;
3443     MemoryRegion *mr;
3444     IOMMUMemoryRegion *iommu_mr;
3445     AddressSpace *target_as;
3446 
3447     assert(!cache->ptr);
3448     *xlat = addr + cache->xlat;
3449 
3450     mr = cache->mrs.mr;
3451     iommu_mr = memory_region_get_iommu(mr);
3452     if (!iommu_mr) {
3453         /* MMIO region.  */
3454         return mr;
3455     }
3456 
3457     section = address_space_translate_iommu(iommu_mr, xlat, plen,
3458                                             NULL, is_write, true,
3459                                             &target_as, attrs);
3460     return section.mr;
3461 }
3462 
3463 /* Called within RCU critical section.  */
address_space_write_continue_cached(MemTxAttrs attrs,const void * ptr,hwaddr len,hwaddr mr_addr,hwaddr l,MemoryRegion * mr)3464 static MemTxResult address_space_write_continue_cached(MemTxAttrs attrs,
3465                                                        const void *ptr,
3466                                                        hwaddr len,
3467                                                        hwaddr mr_addr,
3468                                                        hwaddr l,
3469                                                        MemoryRegion *mr)
3470 {
3471     MemTxResult result = MEMTX_OK;
3472     const uint8_t *buf = ptr;
3473 
3474     for (;;) {
3475         result |= flatview_write_continue_step(attrs, buf, len, mr_addr, &l,
3476                                                mr);
3477 
3478         len -= l;
3479         buf += l;
3480         mr_addr += l;
3481 
3482         if (!len) {
3483             break;
3484         }
3485 
3486         l = len;
3487     }
3488 
3489     return result;
3490 }
3491 
3492 /* Called within RCU critical section.  */
address_space_read_continue_cached(MemTxAttrs attrs,void * ptr,hwaddr len,hwaddr mr_addr,hwaddr l,MemoryRegion * mr)3493 static MemTxResult address_space_read_continue_cached(MemTxAttrs attrs,
3494                                                       void *ptr, hwaddr len,
3495                                                       hwaddr mr_addr, hwaddr l,
3496                                                       MemoryRegion *mr)
3497 {
3498     MemTxResult result = MEMTX_OK;
3499     uint8_t *buf = ptr;
3500 
3501     for (;;) {
3502         result |= flatview_read_continue_step(attrs, buf, len, mr_addr, &l, mr);
3503         len -= l;
3504         buf += l;
3505         mr_addr += l;
3506 
3507         if (!len) {
3508             break;
3509         }
3510         l = len;
3511     }
3512 
3513     return result;
3514 }
3515 
3516 /* Called from RCU critical section. address_space_read_cached uses this
3517  * out of line function when the target is an MMIO or IOMMU region.
3518  */
3519 MemTxResult
address_space_read_cached_slow(MemoryRegionCache * cache,hwaddr addr,void * buf,hwaddr len)3520 address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr,
3521                                    void *buf, hwaddr len)
3522 {
3523     hwaddr mr_addr, l;
3524     MemoryRegion *mr;
3525 
3526     l = len;
3527     mr = address_space_translate_cached(cache, addr, &mr_addr, &l, false,
3528                                         MEMTXATTRS_UNSPECIFIED);
3529     return address_space_read_continue_cached(MEMTXATTRS_UNSPECIFIED,
3530                                               buf, len, mr_addr, l, mr);
3531 }
3532 
3533 /* Called from RCU critical section. address_space_write_cached uses this
3534  * out of line function when the target is an MMIO or IOMMU region.
3535  */
3536 MemTxResult
address_space_write_cached_slow(MemoryRegionCache * cache,hwaddr addr,const void * buf,hwaddr len)3537 address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr,
3538                                     const void *buf, hwaddr len)
3539 {
3540     hwaddr mr_addr, l;
3541     MemoryRegion *mr;
3542 
3543     l = len;
3544     mr = address_space_translate_cached(cache, addr, &mr_addr, &l, true,
3545                                         MEMTXATTRS_UNSPECIFIED);
3546     return address_space_write_continue_cached(MEMTXATTRS_UNSPECIFIED,
3547                                                buf, len, mr_addr, l, mr);
3548 }
3549 
3550 #define ARG1_DECL                MemoryRegionCache *cache
3551 #define ARG1                     cache
3552 #define SUFFIX                   _cached_slow
3553 #define TRANSLATE(...)           address_space_translate_cached(cache, __VA_ARGS__)
3554 #define RCU_READ_LOCK()          ((void)0)
3555 #define RCU_READ_UNLOCK()        ((void)0)
3556 #include "memory_ldst.c.inc"
3557 
3558 /* virtual memory access for debug (includes writing to ROM) */
cpu_memory_rw_debug(CPUState * cpu,vaddr addr,void * ptr,size_t len,bool is_write)3559 int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
3560                         void *ptr, size_t len, bool is_write)
3561 {
3562     hwaddr phys_addr;
3563     vaddr l, page;
3564     uint8_t *buf = ptr;
3565 
3566     cpu_synchronize_state(cpu);
3567     while (len > 0) {
3568         int asidx;
3569         MemTxAttrs attrs;
3570         MemTxResult res;
3571 
3572         page = addr & TARGET_PAGE_MASK;
3573         phys_addr = cpu_get_phys_page_attrs_debug(cpu, page, &attrs);
3574         asidx = cpu_asidx_from_attrs(cpu, attrs);
3575         /* if no physical page mapped, return an error */
3576         if (phys_addr == -1)
3577             return -1;
3578         l = (page + TARGET_PAGE_SIZE) - addr;
3579         if (l > len)
3580             l = len;
3581         phys_addr += (addr & ~TARGET_PAGE_MASK);
3582         if (is_write) {
3583             res = address_space_write_rom(cpu->cpu_ases[asidx].as, phys_addr,
3584                                           attrs, buf, l);
3585         } else {
3586             res = address_space_read(cpu->cpu_ases[asidx].as, phys_addr,
3587                                      attrs, buf, l);
3588         }
3589         if (res != MEMTX_OK) {
3590             return -1;
3591         }
3592         len -= l;
3593         buf += l;
3594         addr += l;
3595     }
3596     return 0;
3597 }
3598 
cpu_physical_memory_is_io(hwaddr phys_addr)3599 bool cpu_physical_memory_is_io(hwaddr phys_addr)
3600 {
3601     MemoryRegion*mr;
3602     hwaddr l = 1;
3603 
3604     RCU_READ_LOCK_GUARD();
3605     mr = address_space_translate(&address_space_memory,
3606                                  phys_addr, &phys_addr, &l, false,
3607                                  MEMTXATTRS_UNSPECIFIED);
3608 
3609     return !(memory_region_is_ram(mr) || memory_region_is_romd(mr));
3610 }
3611 
qemu_ram_foreach_block(RAMBlockIterFunc func,void * opaque)3612 int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
3613 {
3614     RAMBlock *block;
3615     int ret = 0;
3616 
3617     RCU_READ_LOCK_GUARD();
3618     RAMBLOCK_FOREACH(block) {
3619         ret = func(block, opaque);
3620         if (ret) {
3621             break;
3622         }
3623     }
3624     return ret;
3625 }
3626 
3627 /*
3628  * Unmap pages of memory from start to start+length such that
3629  * they a) read as 0, b) Trigger whatever fault mechanism
3630  * the OS provides for postcopy.
3631  * The pages must be unmapped by the end of the function.
3632  * Returns: 0 on success, none-0 on failure
3633  *
3634  */
ram_block_discard_range(RAMBlock * rb,uint64_t start,size_t length)3635 int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length)
3636 {
3637     int ret = -1;
3638 
3639     uint8_t *host_startaddr = rb->host + start;
3640 
3641     if (!QEMU_PTR_IS_ALIGNED(host_startaddr, rb->page_size)) {
3642         error_report("%s: Unaligned start address: %p",
3643                      __func__, host_startaddr);
3644         goto err;
3645     }
3646 
3647     if ((start + length) <= rb->max_length) {
3648         bool need_madvise, need_fallocate;
3649         if (!QEMU_IS_ALIGNED(length, rb->page_size)) {
3650             error_report("%s: Unaligned length: %zx", __func__, length);
3651             goto err;
3652         }
3653 
3654         errno = ENOTSUP; /* If we are missing MADVISE etc */
3655 
3656         /* The logic here is messy;
3657          *    madvise DONTNEED fails for hugepages
3658          *    fallocate works on hugepages and shmem
3659          *    shared anonymous memory requires madvise REMOVE
3660          */
3661         need_madvise = (rb->page_size == qemu_real_host_page_size());
3662         need_fallocate = rb->fd != -1;
3663         if (need_fallocate) {
3664             /* For a file, this causes the area of the file to be zero'd
3665              * if read, and for hugetlbfs also causes it to be unmapped
3666              * so a userfault will trigger.
3667              */
3668 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
3669             /*
3670              * fallocate() will fail with readonly files. Let's print a
3671              * proper error message.
3672              */
3673             if (rb->flags & RAM_READONLY_FD) {
3674                 error_report("%s: Discarding RAM with readonly files is not"
3675                              " supported", __func__);
3676                 goto err;
3677 
3678             }
3679             /*
3680              * We'll discard data from the actual file, even though we only
3681              * have a MAP_PRIVATE mapping, possibly messing with other
3682              * MAP_PRIVATE/MAP_SHARED mappings. There is no easy way to
3683              * change that behavior whithout violating the promised
3684              * semantics of ram_block_discard_range().
3685              *
3686              * Only warn, because it works as long as nobody else uses that
3687              * file.
3688              */
3689             if (!qemu_ram_is_shared(rb)) {
3690                 warn_report_once("%s: Discarding RAM"
3691                                  " in private file mappings is possibly"
3692                                  " dangerous, because it will modify the"
3693                                  " underlying file and will affect other"
3694                                  " users of the file", __func__);
3695             }
3696 
3697             ret = fallocate(rb->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
3698                             start, length);
3699             if (ret) {
3700                 ret = -errno;
3701                 error_report("%s: Failed to fallocate %s:%" PRIx64 " +%zx (%d)",
3702                              __func__, rb->idstr, start, length, ret);
3703                 goto err;
3704             }
3705 #else
3706             ret = -ENOSYS;
3707             error_report("%s: fallocate not available/file"
3708                          "%s:%" PRIx64 " +%zx (%d)",
3709                          __func__, rb->idstr, start, length, ret);
3710             goto err;
3711 #endif
3712         }
3713         if (need_madvise) {
3714             /* For normal RAM this causes it to be unmapped,
3715              * for shared memory it causes the local mapping to disappear
3716              * and to fall back on the file contents (which we just
3717              * fallocate'd away).
3718              */
3719 #if defined(CONFIG_MADVISE)
3720             if (qemu_ram_is_shared(rb) && rb->fd < 0) {
3721                 ret = madvise(host_startaddr, length, QEMU_MADV_REMOVE);
3722             } else {
3723                 ret = madvise(host_startaddr, length, QEMU_MADV_DONTNEED);
3724             }
3725             if (ret) {
3726                 ret = -errno;
3727                 error_report("%s: Failed to discard range "
3728                              "%s:%" PRIx64 " +%zx (%d)",
3729                              __func__, rb->idstr, start, length, ret);
3730                 goto err;
3731             }
3732 #else
3733             ret = -ENOSYS;
3734             error_report("%s: MADVISE not available %s:%" PRIx64 " +%zx (%d)",
3735                          __func__, rb->idstr, start, length, ret);
3736             goto err;
3737 #endif
3738         }
3739         trace_ram_block_discard_range(rb->idstr, host_startaddr, length,
3740                                       need_madvise, need_fallocate, ret);
3741     } else {
3742         error_report("%s: Overrun block '%s' (%" PRIu64 "/%zx/" RAM_ADDR_FMT")",
3743                      __func__, rb->idstr, start, length, rb->max_length);
3744     }
3745 
3746 err:
3747     return ret;
3748 }
3749 
ram_block_discard_guest_memfd_range(RAMBlock * rb,uint64_t start,size_t length)3750 int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t start,
3751                                         size_t length)
3752 {
3753     int ret = -1;
3754 
3755 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
3756     ret = fallocate(rb->guest_memfd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
3757                     start, length);
3758 
3759     if (ret) {
3760         ret = -errno;
3761         error_report("%s: Failed to fallocate %s:%" PRIx64 " +%zx (%d)",
3762                      __func__, rb->idstr, start, length, ret);
3763     }
3764 #else
3765     ret = -ENOSYS;
3766     error_report("%s: fallocate not available %s:%" PRIx64 " +%zx (%d)",
3767                  __func__, rb->idstr, start, length, ret);
3768 #endif
3769 
3770     return ret;
3771 }
3772 
ramblock_is_pmem(RAMBlock * rb)3773 bool ramblock_is_pmem(RAMBlock *rb)
3774 {
3775     return rb->flags & RAM_PMEM;
3776 }
3777 
mtree_print_phys_entries(int start,int end,int skip,int ptr)3778 static void mtree_print_phys_entries(int start, int end, int skip, int ptr)
3779 {
3780     if (start == end - 1) {
3781         qemu_printf("\t%3d      ", start);
3782     } else {
3783         qemu_printf("\t%3d..%-3d ", start, end - 1);
3784     }
3785     qemu_printf(" skip=%d ", skip);
3786     if (ptr == PHYS_MAP_NODE_NIL) {
3787         qemu_printf(" ptr=NIL");
3788     } else if (!skip) {
3789         qemu_printf(" ptr=#%d", ptr);
3790     } else {
3791         qemu_printf(" ptr=[%d]", ptr);
3792     }
3793     qemu_printf("\n");
3794 }
3795 
3796 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
3797                            int128_sub((size), int128_one())) : 0)
3798 
mtree_print_dispatch(AddressSpaceDispatch * d,MemoryRegion * root)3799 void mtree_print_dispatch(AddressSpaceDispatch *d, MemoryRegion *root)
3800 {
3801     int i;
3802 
3803     qemu_printf("  Dispatch\n");
3804     qemu_printf("    Physical sections\n");
3805 
3806     for (i = 0; i < d->map.sections_nb; ++i) {
3807         MemoryRegionSection *s = d->map.sections + i;
3808         const char *names[] = { " [unassigned]", " [not dirty]",
3809                                 " [ROM]", " [watch]" };
3810 
3811         qemu_printf("      #%d @" HWADDR_FMT_plx ".." HWADDR_FMT_plx
3812                     " %s%s%s%s%s",
3813             i,
3814             s->offset_within_address_space,
3815             s->offset_within_address_space + MR_SIZE(s->size),
3816             s->mr->name ? s->mr->name : "(noname)",
3817             i < ARRAY_SIZE(names) ? names[i] : "",
3818             s->mr == root ? " [ROOT]" : "",
3819             s == d->mru_section ? " [MRU]" : "",
3820             s->mr->is_iommu ? " [iommu]" : "");
3821 
3822         if (s->mr->alias) {
3823             qemu_printf(" alias=%s", s->mr->alias->name ?
3824                     s->mr->alias->name : "noname");
3825         }
3826         qemu_printf("\n");
3827     }
3828 
3829     qemu_printf("    Nodes (%d bits per level, %d levels) ptr=[%d] skip=%d\n",
3830                P_L2_BITS, P_L2_LEVELS, d->phys_map.ptr, d->phys_map.skip);
3831     for (i = 0; i < d->map.nodes_nb; ++i) {
3832         int j, jprev;
3833         PhysPageEntry prev;
3834         Node *n = d->map.nodes + i;
3835 
3836         qemu_printf("      [%d]\n", i);
3837 
3838         for (j = 0, jprev = 0, prev = *n[0]; j < ARRAY_SIZE(*n); ++j) {
3839             PhysPageEntry *pe = *n + j;
3840 
3841             if (pe->ptr == prev.ptr && pe->skip == prev.skip) {
3842                 continue;
3843             }
3844 
3845             mtree_print_phys_entries(jprev, j, prev.skip, prev.ptr);
3846 
3847             jprev = j;
3848             prev = *pe;
3849         }
3850 
3851         if (jprev != ARRAY_SIZE(*n)) {
3852             mtree_print_phys_entries(jprev, j, prev.skip, prev.ptr);
3853         }
3854     }
3855 }
3856 
3857 /* Require any discards to work. */
3858 static unsigned int ram_block_discard_required_cnt;
3859 /* Require only coordinated discards to work. */
3860 static unsigned int ram_block_coordinated_discard_required_cnt;
3861 /* Disable any discards. */
3862 static unsigned int ram_block_discard_disabled_cnt;
3863 /* Disable only uncoordinated discards. */
3864 static unsigned int ram_block_uncoordinated_discard_disabled_cnt;
3865 static QemuMutex ram_block_discard_disable_mutex;
3866 
ram_block_discard_disable_mutex_lock(void)3867 static void ram_block_discard_disable_mutex_lock(void)
3868 {
3869     static gsize initialized;
3870 
3871     if (g_once_init_enter(&initialized)) {
3872         qemu_mutex_init(&ram_block_discard_disable_mutex);
3873         g_once_init_leave(&initialized, 1);
3874     }
3875     qemu_mutex_lock(&ram_block_discard_disable_mutex);
3876 }
3877 
ram_block_discard_disable_mutex_unlock(void)3878 static void ram_block_discard_disable_mutex_unlock(void)
3879 {
3880     qemu_mutex_unlock(&ram_block_discard_disable_mutex);
3881 }
3882 
ram_block_discard_disable(bool state)3883 int ram_block_discard_disable(bool state)
3884 {
3885     int ret = 0;
3886 
3887     ram_block_discard_disable_mutex_lock();
3888     if (!state) {
3889         ram_block_discard_disabled_cnt--;
3890     } else if (ram_block_discard_required_cnt ||
3891                ram_block_coordinated_discard_required_cnt) {
3892         ret = -EBUSY;
3893     } else {
3894         ram_block_discard_disabled_cnt++;
3895     }
3896     ram_block_discard_disable_mutex_unlock();
3897     return ret;
3898 }
3899 
ram_block_uncoordinated_discard_disable(bool state)3900 int ram_block_uncoordinated_discard_disable(bool state)
3901 {
3902     int ret = 0;
3903 
3904     ram_block_discard_disable_mutex_lock();
3905     if (!state) {
3906         ram_block_uncoordinated_discard_disabled_cnt--;
3907     } else if (ram_block_discard_required_cnt) {
3908         ret = -EBUSY;
3909     } else {
3910         ram_block_uncoordinated_discard_disabled_cnt++;
3911     }
3912     ram_block_discard_disable_mutex_unlock();
3913     return ret;
3914 }
3915 
ram_block_discard_require(bool state)3916 int ram_block_discard_require(bool state)
3917 {
3918     int ret = 0;
3919 
3920     ram_block_discard_disable_mutex_lock();
3921     if (!state) {
3922         ram_block_discard_required_cnt--;
3923     } else if (ram_block_discard_disabled_cnt ||
3924                ram_block_uncoordinated_discard_disabled_cnt) {
3925         ret = -EBUSY;
3926     } else {
3927         ram_block_discard_required_cnt++;
3928     }
3929     ram_block_discard_disable_mutex_unlock();
3930     return ret;
3931 }
3932 
ram_block_coordinated_discard_require(bool state)3933 int ram_block_coordinated_discard_require(bool state)
3934 {
3935     int ret = 0;
3936 
3937     ram_block_discard_disable_mutex_lock();
3938     if (!state) {
3939         ram_block_coordinated_discard_required_cnt--;
3940     } else if (ram_block_discard_disabled_cnt) {
3941         ret = -EBUSY;
3942     } else {
3943         ram_block_coordinated_discard_required_cnt++;
3944     }
3945     ram_block_discard_disable_mutex_unlock();
3946     return ret;
3947 }
3948 
ram_block_discard_is_disabled(void)3949 bool ram_block_discard_is_disabled(void)
3950 {
3951     return qatomic_read(&ram_block_discard_disabled_cnt) ||
3952            qatomic_read(&ram_block_uncoordinated_discard_disabled_cnt);
3953 }
3954 
ram_block_discard_is_required(void)3955 bool ram_block_discard_is_required(void)
3956 {
3957     return qatomic_read(&ram_block_discard_required_cnt) ||
3958            qatomic_read(&ram_block_coordinated_discard_required_cnt);
3959 }
3960