Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / hw / remote / proxy-memory-listener.c
bloba926f61ebe978538e00b5f5e61f6c6e7aa7b025c
1 /*
2 * Copyright © 2018, 2021 Oracle and/or its affiliates.
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
7 */
9 #include "qemu/osdep.h"
11 #include "qemu/int128.h"
12 #include "qemu/range.h"
13 #include "exec/memory.h"
14 #include "exec/cpu-common.h"
15 #include "exec/ram_addr.h"
16 #include "qapi/error.h"
17 #include "qemu/error-report.h"
18 #include "hw/remote/mpqemu-link.h"
19 #include "hw/remote/proxy-memory-listener.h"
22 * TODO: get_fd_from_hostaddr(), proxy_mrs_can_merge() and
23 * proxy_memory_listener_commit() defined below perform tasks similar to the
24 * functions defined in vhost-user.c. These functions are good candidates
25 * for refactoring.
29 static void proxy_memory_listener_reset(MemoryListener *listener)
31 ProxyMemoryListener *proxy_listener = container_of(listener,
32 ProxyMemoryListener,
33 listener);
34 int mrs;
36 for (mrs = 0; mrs < proxy_listener->n_mr_sections; mrs++) {
37 memory_region_unref(proxy_listener->mr_sections[mrs].mr);
40 g_free(proxy_listener->mr_sections);
41 proxy_listener->mr_sections = NULL;
42 proxy_listener->n_mr_sections = 0;
45 static int get_fd_from_hostaddr(uint64_t host, ram_addr_t *offset)
47 MemoryRegion *mr;
48 ram_addr_t off;
50 /**
51 * Assumes that the host address is a valid address as it's
52 * coming from the MemoryListener system. In the case host
53 * address is not valid, the following call would return
54 * the default subregion of "system_memory" region, and
55 * not NULL. So it's not possible to check for NULL here.
57 mr = memory_region_from_host((void *)(uintptr_t)host, &off);
59 if (offset) {
60 *offset = off;
63 return memory_region_get_fd(mr);
66 static bool proxy_mrs_can_merge(uint64_t host, uint64_t prev_host, size_t size)
68 if (((prev_host + size) != host)) {
69 return false;
72 if (get_fd_from_hostaddr(host, NULL) !=
73 get_fd_from_hostaddr(prev_host, NULL)) {
74 return false;
77 return true;
80 static bool try_merge(ProxyMemoryListener *proxy_listener,
81 MemoryRegionSection *section)
83 uint64_t mrs_size, mrs_gpa, mrs_page;
84 MemoryRegionSection *prev_sec;
85 bool merged = false;
86 uintptr_t mrs_host;
87 RAMBlock *mrs_rb;
89 if (!proxy_listener->n_mr_sections) {
90 return false;
93 mrs_rb = section->mr->ram_block;
94 mrs_page = (uint64_t)qemu_ram_pagesize(mrs_rb);
95 mrs_size = int128_get64(section->size);
96 mrs_gpa = section->offset_within_address_space;
97 mrs_host = (uintptr_t)memory_region_get_ram_ptr(section->mr) +
98 section->offset_within_region;
100 if (get_fd_from_hostaddr(mrs_host, NULL) < 0) {
101 return true;
104 mrs_host = mrs_host & ~(mrs_page - 1);
105 mrs_gpa = mrs_gpa & ~(mrs_page - 1);
106 mrs_size = ROUND_UP(mrs_size, mrs_page);
108 prev_sec = proxy_listener->mr_sections +
109 (proxy_listener->n_mr_sections - 1);
110 uint64_t prev_gpa_start = prev_sec->offset_within_address_space;
111 uint64_t prev_size = int128_get64(prev_sec->size);
112 uint64_t prev_gpa_end = range_get_last(prev_gpa_start, prev_size);
113 uint64_t prev_host_start =
114 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr) +
115 prev_sec->offset_within_region;
116 uint64_t prev_host_end = range_get_last(prev_host_start, prev_size);
118 if (mrs_gpa <= (prev_gpa_end + 1)) {
119 g_assert(mrs_gpa > prev_gpa_start);
121 if ((section->mr == prev_sec->mr) &&
122 proxy_mrs_can_merge(mrs_host, prev_host_start,
123 (mrs_gpa - prev_gpa_start))) {
124 uint64_t max_end = MAX(prev_host_end, mrs_host + mrs_size);
125 merged = true;
126 prev_sec->offset_within_address_space =
127 MIN(prev_gpa_start, mrs_gpa);
128 prev_sec->offset_within_region =
129 MIN(prev_host_start, mrs_host) -
130 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr);
131 prev_sec->size = int128_make64(max_end - MIN(prev_host_start,
132 mrs_host));
136 return merged;
139 static void proxy_memory_listener_region_addnop(MemoryListener *listener,
140 MemoryRegionSection *section)
142 ProxyMemoryListener *proxy_listener = container_of(listener,
143 ProxyMemoryListener,
144 listener);
146 if (!memory_region_is_ram(section->mr) ||
147 memory_region_is_rom(section->mr)) {
148 return;
151 if (try_merge(proxy_listener, section)) {
152 return;
155 ++proxy_listener->n_mr_sections;
156 proxy_listener->mr_sections = g_renew(MemoryRegionSection,
157 proxy_listener->mr_sections,
158 proxy_listener->n_mr_sections);
159 proxy_listener->mr_sections[proxy_listener->n_mr_sections - 1] = *section;
160 proxy_listener->mr_sections[proxy_listener->n_mr_sections - 1].fv = NULL;
161 memory_region_ref(section->mr);
164 static void proxy_memory_listener_commit(MemoryListener *listener)
166 ProxyMemoryListener *proxy_listener = container_of(listener,
167 ProxyMemoryListener,
168 listener);
169 MPQemuMsg msg;
170 MemoryRegionSection *section;
171 ram_addr_t offset;
172 uintptr_t host_addr;
173 int region;
174 Error *local_err = NULL;
176 memset(&msg, 0, sizeof(MPQemuMsg));
178 msg.cmd = MPQEMU_CMD_SYNC_SYSMEM;
179 msg.num_fds = proxy_listener->n_mr_sections;
180 msg.size = sizeof(SyncSysmemMsg);
181 if (msg.num_fds > REMOTE_MAX_FDS) {
182 error_report("Number of fds is more than %d", REMOTE_MAX_FDS);
183 return;
186 for (region = 0; region < proxy_listener->n_mr_sections; region++) {
187 section = &proxy_listener->mr_sections[region];
188 msg.data.sync_sysmem.gpas[region] =
189 section->offset_within_address_space;
190 msg.data.sync_sysmem.sizes[region] = int128_get64(section->size);
191 host_addr = (uintptr_t)memory_region_get_ram_ptr(section->mr) +
192 section->offset_within_region;
193 msg.fds[region] = get_fd_from_hostaddr(host_addr, &offset);
194 msg.data.sync_sysmem.offsets[region] = offset;
196 if (!mpqemu_msg_send(&msg, proxy_listener->ioc, &local_err)) {
197 error_report_err(local_err);
201 void proxy_memory_listener_deconfigure(ProxyMemoryListener *proxy_listener)
203 memory_listener_unregister(&proxy_listener->listener);
205 proxy_memory_listener_reset(&proxy_listener->listener);
208 void proxy_memory_listener_configure(ProxyMemoryListener *proxy_listener,
209 QIOChannel *ioc)
211 proxy_listener->n_mr_sections = 0;
212 proxy_listener->mr_sections = NULL;
214 proxy_listener->ioc = ioc;
216 proxy_listener->listener.begin = proxy_memory_listener_reset;
217 proxy_listener->listener.commit = proxy_memory_listener_commit;
218 proxy_listener->listener.region_add = proxy_memory_listener_region_addnop;
219 proxy_listener->listener.region_nop = proxy_memory_listener_region_addnop;
220 proxy_listener->listener.priority = MEMORY_LISTENER_PRIORITY_DEV_BACKEND;
221 proxy_listener->listener.name = "proxy";
223 memory_listener_register(&proxy_listener->listener,
224 &address_space_memory);