blob: a6df1b2811b0c6a00e64ca6043def17d4c6cbfb4 [file] [log] [blame]
Dr. David Alan Gilberteb59db52015-11-05 18:10:55 +00001/*
2 * Postcopy migration for RAM
3 *
4 * Copyright 2013 Red Hat, Inc. and/or its affiliates
5 *
6 * Authors:
7 * Dave Gilbert <dgilbert@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 */
13#ifndef QEMU_POSTCOPY_RAM_H
14#define QEMU_POSTCOPY_RAM_H
15
Peter Xu4146b772024-06-19 18:30:40 -040016#include "qapi/qapi-types-migration.h"
17
Dr. David Alan Gilberteb59db52015-11-05 18:10:55 +000018/* Return true if the host supports everything we need to do postcopy-ram */
Peter Xu74c38cf2023-04-25 21:15:14 -040019bool postcopy_ram_supported_by_host(MigrationIncomingState *mis,
20 Error **errp);
Dr. David Alan Gilberteb59db52015-11-05 18:10:55 +000021
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000022/*
Dr. David Alan Gilbertf0a227a2015-11-05 18:11:04 +000023 * Make all of RAM sensitive to accesses to areas that haven't yet been written
24 * and wire up anything necessary to deal with it.
25 */
Wei Yang2a7eb142019-10-10 09:13:15 +080026int postcopy_ram_incoming_setup(MigrationIncomingState *mis);
Dr. David Alan Gilbertf0a227a2015-11-05 18:11:04 +000027
28/*
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +000029 * Initialise postcopy-ram, setting the RAM to a state where we can go into
30 * postcopy later; must be called prior to any precopy.
31 * called from ram.c's similarly named ram_postcopy_incoming_init
32 */
David Hildenbrandc1361802018-06-20 22:27:36 +020033int postcopy_ram_incoming_init(MigrationIncomingState *mis);
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +000034
35/*
36 * At the end of a migration where postcopy_ram_incoming_init was called.
37 */
38int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis);
39
40/*
Dr. David Alan Gilbertf9527102015-11-05 18:11:20 +000041 * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
42 * however leaving it until after precopy means that most of the precopy
43 * data is still THPd
44 */
45int postcopy_ram_prepare_discard(MigrationIncomingState *mis);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000046
47/*
48 * Called at the start of each RAMBlock by the bitmap code.
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000049 */
Wei Yang810cf2b2019-07-24 09:07:21 +080050void postcopy_discard_send_init(MigrationState *ms, const char *name);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000051
52/*
53 * Called by the bitmap code for each chunk to discard.
54 * May send a discard message, may just leave it queued to
55 * be sent later.
56 * @start,@length: a range of pages in the migration bitmap in the
57 * RAM block passed to postcopy_discard_send_init() (length=1 is one page)
58 */
Wei Yang810cf2b2019-07-24 09:07:21 +080059void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
60 unsigned long length);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000061
62/*
63 * Called at the end of each RAMBlock by the bitmap code.
Wei Yang810cf2b2019-07-24 09:07:21 +080064 * Sends any outstanding discard messages.
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000065 */
Wei Yang810cf2b2019-07-24 09:07:21 +080066void postcopy_discard_send_finish(MigrationState *ms);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000067
Dr. David Alan Gilbert696ed9a2015-11-05 18:11:10 +000068/*
69 * Place a page (from) at (host) efficiently
70 * There are restrictions on how 'from' must be mapped, in general best
71 * to use other postcopy_ routines to allocate.
72 * returns 0 on success
73 */
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +000074int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
Alexey Perevalov8be46202017-10-05 14:13:18 +030075 RAMBlock *rb);
Dr. David Alan Gilbert696ed9a2015-11-05 18:11:10 +000076
77/*
78 * Place a zero page at (host) atomically
79 * returns 0 on success
80 */
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +000081int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
Alexey Perevalov8be46202017-10-05 14:13:18 +030082 RAMBlock *rb);
Dr. David Alan Gilbert696ed9a2015-11-05 18:11:10 +000083
Juan Quintelabac3b212017-04-24 16:50:35 +020084/* The current postcopy state is read/set by postcopy_state_get/set
85 * which update it atomically.
86 * The state is updated as postcopy messages are received, and
87 * in general only one thread should be writing to the state at any one
88 * time, initially the main thread and then the listen thread;
89 * Corner cases are where either thread finishes early and/or errors.
90 * The state is checked as messages are received to ensure that
91 * the source is sending us messages in the correct order.
92 * The state is also used by the RAM reception code to know if it
93 * has to place pages atomically, and the cleanup code at the end of
94 * the main thread to know if it has to delay cleanup until the end
95 * of postcopy.
96 */
97typedef enum {
98 POSTCOPY_INCOMING_NONE = 0, /* Initial state - no postcopy */
99 POSTCOPY_INCOMING_ADVISE,
100 POSTCOPY_INCOMING_DISCARD,
101 POSTCOPY_INCOMING_LISTENING,
102 POSTCOPY_INCOMING_RUNNING,
103 POSTCOPY_INCOMING_END
104} PostcopyState;
105
Juan Quintelabac3b212017-04-24 16:50:35 +0200106PostcopyState postcopy_state_get(void);
107/* Set the state and return the old state */
108PostcopyState postcopy_state_set(PostcopyState new_state);
109
Peter Xu9ab7ef92018-02-08 18:31:07 +0800110void postcopy_fault_thread_notify(MigrationIncomingState *mis);
111
Dr. David Alan Gilbert1693c642018-03-12 17:20:59 +0000112/*
113 * To be called once at the start before any device initialisation
114 */
115void postcopy_infrastructure_init(void);
116
117/* Add a notifier to a list to be called when checking whether the devices
118 * can support postcopy.
119 * It's data is a *PostcopyNotifyData
120 * It should return 0 if OK, or a negative value on failure.
121 * On failure it must set the data->errp to an error.
122 *
123 */
124enum PostcopyNotifyReason {
125 POSTCOPY_NOTIFY_PROBE = 0,
Dr. David Alan Gilbertd3dff7a2018-03-12 17:21:01 +0000126 POSTCOPY_NOTIFY_INBOUND_ADVISE,
Dr. David Alan Gilbert6864a7b2018-03-12 17:21:06 +0000127 POSTCOPY_NOTIFY_INBOUND_LISTEN,
Dr. David Alan Gilbert46343572018-03-12 17:21:20 +0000128 POSTCOPY_NOTIFY_INBOUND_END,
Dr. David Alan Gilbert1693c642018-03-12 17:20:59 +0000129};
130
131struct PostcopyNotifyData {
132 enum PostcopyNotifyReason reason;
Dr. David Alan Gilbert1693c642018-03-12 17:20:59 +0000133};
134
135void postcopy_add_notifier(NotifierWithReturn *nn);
136void postcopy_remove_notifier(NotifierWithReturn *n);
137/* Call the notifier list set by postcopy_add_start_notifier */
138int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp);
139
Peter Xu095c12a2022-03-01 16:39:06 +0800140void postcopy_thread_create(MigrationIncomingState *mis,
141 QemuThread *thread, const char *name,
142 void *(*fn)(void *), int joinable);
143
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000144struct PostCopyFD;
145
146/* ufd is a pointer to the struct uffd_msg *TODO: more Portable! */
147typedef int (*pcfdhandler)(struct PostCopyFD *pcfd, void *ufd);
Dr. David Alan Gilbertd488b342018-03-12 17:21:15 +0000148/* Notification to wake, either on place or on reception of
149 * a fault on something that's already arrived (race)
150 */
151typedef int (*pcfdwake)(struct PostCopyFD *pcfd, RAMBlock *rb, uint64_t offset);
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000152
153struct PostCopyFD {
154 int fd;
155 /* Data to pass to handler */
156 void *data;
157 /* Handler to be called whenever we get a poll event */
158 pcfdhandler handler;
Dr. David Alan Gilbertd488b342018-03-12 17:21:15 +0000159 /* Notification to wake shared client */
160 pcfdwake waker;
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000161 /* A string to use in error messages */
162 const char *idstr;
163};
164
165/* Register a userfaultfd owned by an external process for
166 * shared memory.
167 */
168void postcopy_register_shared_ufd(struct PostCopyFD *pcfd);
169void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd);
zhaolichang3a4452d2020-09-17 15:50:21 +0800170/* Call each of the shared 'waker's registered telling them of
Dr. David Alan Gilbertd488b342018-03-12 17:21:15 +0000171 * availability of a block.
172 */
173int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset);
Dr. David Alan Gilbert5efc3562018-03-12 17:21:14 +0000174/* postcopy_wake_shared: Notify a client ufd that a page is available
175 *
176 * Returns 0 on success
177 *
178 * @pcfd: Structure with fd, handler and name as above
179 * @client_addr: Address in the client program, not QEMU
180 * @rb: The RAMBlock the page is in
181 */
182int postcopy_wake_shared(struct PostCopyFD *pcfd, uint64_t client_addr,
183 RAMBlock *rb);
Dr. David Alan Gilbert096bf4c2018-03-12 17:21:12 +0000184/* Callback from shared fault handlers to ask for a page */
185int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
186 uint64_t client_addr, uint64_t offset);
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000187
Peter Xu36f62f12022-07-07 14:55:02 -0400188/* Hard-code channels for now for postcopy preemption */
189enum PostcopyChannels {
190 RAM_CHANNEL_PRECOPY = 0,
191 RAM_CHANNEL_POSTCOPY = 1,
192 RAM_CHANNEL_MAX,
193};
194
manish.mishra6720c2b2022-12-20 18:44:18 +0000195void postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file);
Peter Xufc063a72023-02-08 15:28:11 -0500196void postcopy_preempt_setup(MigrationState *s);
Peter Xu5655aab2023-02-08 15:28:13 -0500197int postcopy_preempt_establish_channel(MigrationState *s);
Peter Xu4146b772024-06-19 18:30:40 -0400198bool postcopy_is_paused(MigrationStatus status);
Peter Xu36f62f12022-07-07 14:55:02 -0400199
Dr. David Alan Gilberteb59db52015-11-05 18:10:55 +0000200#endif