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.
9 #include "qemu/osdep.h"
10 #include "qemu-common.h"
12 #include "qemu/compiler.h"
13 #include "qemu/int128.h"
14 #include "qemu/range.h"
15 #include "exec/memory.h"
16 #include "exec/cpu-common.h"
17 #include "exec/ram_addr.h"
18 #include "exec/address-spaces.h"
19 #include "qapi/error.h"
20 #include "hw/remote/mpqemu-link.h"
21 #include "hw/remote/proxy-memory-listener.h"
24 * TODO: get_fd_from_hostaddr(), proxy_mrs_can_merge() and
25 * proxy_memory_listener_commit() defined below perform tasks similar to the
26 * functions defined in vhost-user.c. These functions are good candidates
31 static void proxy_memory_listener_reset(MemoryListener
*listener
)
33 ProxyMemoryListener
*proxy_listener
= container_of(listener
,
38 for (mrs
= 0; mrs
< proxy_listener
->n_mr_sections
; mrs
++) {
39 memory_region_unref(proxy_listener
->mr_sections
[mrs
].mr
);
42 g_free(proxy_listener
->mr_sections
);
43 proxy_listener
->mr_sections
= NULL
;
44 proxy_listener
->n_mr_sections
= 0;
47 static int get_fd_from_hostaddr(uint64_t host
, ram_addr_t
*offset
)
53 * Assumes that the host address is a valid address as it's
54 * coming from the MemoryListener system. In the case host
55 * address is not valid, the following call would return
56 * the default subregion of "system_memory" region, and
57 * not NULL. So it's not possible to check for NULL here.
59 mr
= memory_region_from_host((void *)(uintptr_t)host
, &off
);
65 return memory_region_get_fd(mr
);
68 static bool proxy_mrs_can_merge(uint64_t host
, uint64_t prev_host
, size_t size
)
70 if (((prev_host
+ size
) != host
)) {
74 if (get_fd_from_hostaddr(host
, NULL
) !=
75 get_fd_from_hostaddr(prev_host
, NULL
)) {
82 static bool try_merge(ProxyMemoryListener
*proxy_listener
,
83 MemoryRegionSection
*section
)
85 uint64_t mrs_size
, mrs_gpa
, mrs_page
;
86 MemoryRegionSection
*prev_sec
;
91 if (!proxy_listener
->n_mr_sections
) {
95 mrs_rb
= section
->mr
->ram_block
;
96 mrs_page
= (uint64_t)qemu_ram_pagesize(mrs_rb
);
97 mrs_size
= int128_get64(section
->size
);
98 mrs_gpa
= section
->offset_within_address_space
;
99 mrs_host
= (uintptr_t)memory_region_get_ram_ptr(section
->mr
) +
100 section
->offset_within_region
;
102 if (get_fd_from_hostaddr(mrs_host
, NULL
) < 0) {
106 mrs_host
= mrs_host
& ~(mrs_page
- 1);
107 mrs_gpa
= mrs_gpa
& ~(mrs_page
- 1);
108 mrs_size
= ROUND_UP(mrs_size
, mrs_page
);
110 prev_sec
= proxy_listener
->mr_sections
+
111 (proxy_listener
->n_mr_sections
- 1);
112 uint64_t prev_gpa_start
= prev_sec
->offset_within_address_space
;
113 uint64_t prev_size
= int128_get64(prev_sec
->size
);
114 uint64_t prev_gpa_end
= range_get_last(prev_gpa_start
, prev_size
);
115 uint64_t prev_host_start
=
116 (uintptr_t)memory_region_get_ram_ptr(prev_sec
->mr
) +
117 prev_sec
->offset_within_region
;
118 uint64_t prev_host_end
= range_get_last(prev_host_start
, prev_size
);
120 if (mrs_gpa
<= (prev_gpa_end
+ 1)) {
121 g_assert(mrs_gpa
> prev_gpa_start
);
123 if ((section
->mr
== prev_sec
->mr
) &&
124 proxy_mrs_can_merge(mrs_host
, prev_host_start
,
125 (mrs_gpa
- prev_gpa_start
))) {
126 uint64_t max_end
= MAX(prev_host_end
, mrs_host
+ mrs_size
);
128 prev_sec
->offset_within_address_space
=
129 MIN(prev_gpa_start
, mrs_gpa
);
130 prev_sec
->offset_within_region
=
131 MIN(prev_host_start
, mrs_host
) -
132 (uintptr_t)memory_region_get_ram_ptr(prev_sec
->mr
);
133 prev_sec
->size
= int128_make64(max_end
- MIN(prev_host_start
,
141 static void proxy_memory_listener_region_addnop(MemoryListener
*listener
,
142 MemoryRegionSection
*section
)
144 ProxyMemoryListener
*proxy_listener
= container_of(listener
,
148 if (!memory_region_is_ram(section
->mr
) ||
149 memory_region_is_rom(section
->mr
)) {
153 if (try_merge(proxy_listener
, section
)) {
157 ++proxy_listener
->n_mr_sections
;
158 proxy_listener
->mr_sections
= g_renew(MemoryRegionSection
,
159 proxy_listener
->mr_sections
,
160 proxy_listener
->n_mr_sections
);
161 proxy_listener
->mr_sections
[proxy_listener
->n_mr_sections
- 1] = *section
;
162 proxy_listener
->mr_sections
[proxy_listener
->n_mr_sections
- 1].fv
= NULL
;
163 memory_region_ref(section
->mr
);
166 static void proxy_memory_listener_commit(MemoryListener
*listener
)
168 ProxyMemoryListener
*proxy_listener
= container_of(listener
,
172 MemoryRegionSection
*section
;
176 Error
*local_err
= NULL
;
178 memset(&msg
, 0, sizeof(MPQemuMsg
));
180 msg
.cmd
= MPQEMU_CMD_SYNC_SYSMEM
;
181 msg
.num_fds
= proxy_listener
->n_mr_sections
;
182 msg
.size
= sizeof(SyncSysmemMsg
);
183 if (msg
.num_fds
> REMOTE_MAX_FDS
) {
184 error_report("Number of fds is more than %d", REMOTE_MAX_FDS
);
188 for (region
= 0; region
< proxy_listener
->n_mr_sections
; region
++) {
189 section
= &proxy_listener
->mr_sections
[region
];
190 msg
.data
.sync_sysmem
.gpas
[region
] =
191 section
->offset_within_address_space
;
192 msg
.data
.sync_sysmem
.sizes
[region
] = int128_get64(section
->size
);
193 host_addr
= (uintptr_t)memory_region_get_ram_ptr(section
->mr
) +
194 section
->offset_within_region
;
195 msg
.fds
[region
] = get_fd_from_hostaddr(host_addr
, &offset
);
196 msg
.data
.sync_sysmem
.offsets
[region
] = offset
;
198 if (!mpqemu_msg_send(&msg
, proxy_listener
->ioc
, &local_err
)) {
199 error_report_err(local_err
);
203 void proxy_memory_listener_deconfigure(ProxyMemoryListener
*proxy_listener
)
205 memory_listener_unregister(&proxy_listener
->listener
);
207 proxy_memory_listener_reset(&proxy_listener
->listener
);
210 void proxy_memory_listener_configure(ProxyMemoryListener
*proxy_listener
,
213 proxy_listener
->n_mr_sections
= 0;
214 proxy_listener
->mr_sections
= NULL
;
216 proxy_listener
->ioc
= ioc
;
218 proxy_listener
->listener
.begin
= proxy_memory_listener_reset
;
219 proxy_listener
->listener
.commit
= proxy_memory_listener_commit
;
220 proxy_listener
->listener
.region_add
= proxy_memory_listener_region_addnop
;
221 proxy_listener
->listener
.region_nop
= proxy_memory_listener_region_addnop
;
222 proxy_listener
->listener
.priority
= 10;
224 memory_listener_register(&proxy_listener
->listener
,
225 &address_space_memory
);