blob: ecae94121184271b821cae81d25dab9ebcd762ac [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
16/* Return true if the host supports everything we need to do postcopy-ram */
Peter Xu74c38cf2023-04-25 21:15:14 -040017bool postcopy_ram_supported_by_host(MigrationIncomingState *mis,
18 Error **errp);
Dr. David Alan Gilberteb59db52015-11-05 18:10:55 +000019
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000020/*
Dr. David Alan Gilbertf0a227a2015-11-05 18:11:04 +000021 * Make all of RAM sensitive to accesses to areas that haven't yet been written
22 * and wire up anything necessary to deal with it.
23 */
Wei Yang2a7eb142019-10-10 09:13:15 +080024int postcopy_ram_incoming_setup(MigrationIncomingState *mis);
Dr. David Alan Gilbertf0a227a2015-11-05 18:11:04 +000025
26/*
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +000027 * Initialise postcopy-ram, setting the RAM to a state where we can go into
28 * postcopy later; must be called prior to any precopy.
29 * called from ram.c's similarly named ram_postcopy_incoming_init
30 */
David Hildenbrandc1361802018-06-20 22:27:36 +020031int postcopy_ram_incoming_init(MigrationIncomingState *mis);
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +000032
33/*
34 * At the end of a migration where postcopy_ram_incoming_init was called.
35 */
36int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis);
37
38/*
Dr. David Alan Gilbertf9527102015-11-05 18:11:20 +000039 * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
40 * however leaving it until after precopy means that most of the precopy
41 * data is still THPd
42 */
43int postcopy_ram_prepare_discard(MigrationIncomingState *mis);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000044
45/*
46 * Called at the start of each RAMBlock by the bitmap code.
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000047 */
Wei Yang810cf2b2019-07-24 09:07:21 +080048void postcopy_discard_send_init(MigrationState *ms, const char *name);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000049
50/*
51 * Called by the bitmap code for each chunk to discard.
52 * May send a discard message, may just leave it queued to
53 * be sent later.
54 * @start,@length: a range of pages in the migration bitmap in the
55 * RAM block passed to postcopy_discard_send_init() (length=1 is one page)
56 */
Wei Yang810cf2b2019-07-24 09:07:21 +080057void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
58 unsigned long length);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000059
60/*
61 * Called at the end of each RAMBlock by the bitmap code.
Wei Yang810cf2b2019-07-24 09:07:21 +080062 * Sends any outstanding discard messages.
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000063 */
Wei Yang810cf2b2019-07-24 09:07:21 +080064void postcopy_discard_send_finish(MigrationState *ms);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000065
Dr. David Alan Gilbert696ed9a2015-11-05 18:11:10 +000066/*
67 * Place a page (from) at (host) efficiently
68 * There are restrictions on how 'from' must be mapped, in general best
69 * to use other postcopy_ routines to allocate.
70 * returns 0 on success
71 */
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +000072int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
Alexey Perevalov8be46202017-10-05 14:13:18 +030073 RAMBlock *rb);
Dr. David Alan Gilbert696ed9a2015-11-05 18:11:10 +000074
75/*
76 * Place a zero page at (host) atomically
77 * returns 0 on success
78 */
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +000079int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
Alexey Perevalov8be46202017-10-05 14:13:18 +030080 RAMBlock *rb);
Dr. David Alan Gilbert696ed9a2015-11-05 18:11:10 +000081
Juan Quintelabac3b212017-04-24 16:50:35 +020082/* The current postcopy state is read/set by postcopy_state_get/set
83 * which update it atomically.
84 * The state is updated as postcopy messages are received, and
85 * in general only one thread should be writing to the state at any one
86 * time, initially the main thread and then the listen thread;
87 * Corner cases are where either thread finishes early and/or errors.
88 * The state is checked as messages are received to ensure that
89 * the source is sending us messages in the correct order.
90 * The state is also used by the RAM reception code to know if it
91 * has to place pages atomically, and the cleanup code at the end of
92 * the main thread to know if it has to delay cleanup until the end
93 * of postcopy.
94 */
95typedef enum {
96 POSTCOPY_INCOMING_NONE = 0, /* Initial state - no postcopy */
97 POSTCOPY_INCOMING_ADVISE,
98 POSTCOPY_INCOMING_DISCARD,
99 POSTCOPY_INCOMING_LISTENING,
100 POSTCOPY_INCOMING_RUNNING,
101 POSTCOPY_INCOMING_END
102} PostcopyState;
103
Juan Quintelabac3b212017-04-24 16:50:35 +0200104PostcopyState postcopy_state_get(void);
105/* Set the state and return the old state */
106PostcopyState postcopy_state_set(PostcopyState new_state);
107
Peter Xu9ab7ef92018-02-08 18:31:07 +0800108void postcopy_fault_thread_notify(MigrationIncomingState *mis);
109
Dr. David Alan Gilbert1693c642018-03-12 17:20:59 +0000110/*
111 * To be called once at the start before any device initialisation
112 */
113void postcopy_infrastructure_init(void);
114
115/* Add a notifier to a list to be called when checking whether the devices
116 * can support postcopy.
117 * It's data is a *PostcopyNotifyData
118 * It should return 0 if OK, or a negative value on failure.
119 * On failure it must set the data->errp to an error.
120 *
121 */
122enum PostcopyNotifyReason {
123 POSTCOPY_NOTIFY_PROBE = 0,
Dr. David Alan Gilbertd3dff7a2018-03-12 17:21:01 +0000124 POSTCOPY_NOTIFY_INBOUND_ADVISE,
Dr. David Alan Gilbert6864a7b2018-03-12 17:21:06 +0000125 POSTCOPY_NOTIFY_INBOUND_LISTEN,
Dr. David Alan Gilbert46343572018-03-12 17:21:20 +0000126 POSTCOPY_NOTIFY_INBOUND_END,
Dr. David Alan Gilbert1693c642018-03-12 17:20:59 +0000127};
128
129struct PostcopyNotifyData {
130 enum PostcopyNotifyReason reason;
Dr. David Alan Gilbert1693c642018-03-12 17:20:59 +0000131};
132
133void postcopy_add_notifier(NotifierWithReturn *nn);
134void postcopy_remove_notifier(NotifierWithReturn *n);
135/* Call the notifier list set by postcopy_add_start_notifier */
136int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp);
137
Peter Xu095c12a2022-03-01 16:39:06 +0800138void postcopy_thread_create(MigrationIncomingState *mis,
139 QemuThread *thread, const char *name,
140 void *(*fn)(void *), int joinable);
141
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000142struct PostCopyFD;
143
144/* ufd is a pointer to the struct uffd_msg *TODO: more Portable! */
145typedef int (*pcfdhandler)(struct PostCopyFD *pcfd, void *ufd);
Dr. David Alan Gilbertd488b342018-03-12 17:21:15 +0000146/* Notification to wake, either on place or on reception of
147 * a fault on something that's already arrived (race)
148 */
149typedef int (*pcfdwake)(struct PostCopyFD *pcfd, RAMBlock *rb, uint64_t offset);
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000150
151struct PostCopyFD {
152 int fd;
153 /* Data to pass to handler */
154 void *data;
155 /* Handler to be called whenever we get a poll event */
156 pcfdhandler handler;
Dr. David Alan Gilbertd488b342018-03-12 17:21:15 +0000157 /* Notification to wake shared client */
158 pcfdwake waker;
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000159 /* A string to use in error messages */
160 const char *idstr;
161};
162
163/* Register a userfaultfd owned by an external process for
164 * shared memory.
165 */
166void postcopy_register_shared_ufd(struct PostCopyFD *pcfd);
167void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd);
zhaolichang3a4452d2020-09-17 15:50:21 +0800168/* Call each of the shared 'waker's registered telling them of
Dr. David Alan Gilbertd488b342018-03-12 17:21:15 +0000169 * availability of a block.
170 */
171int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset);
Dr. David Alan Gilbert5efc3562018-03-12 17:21:14 +0000172/* postcopy_wake_shared: Notify a client ufd that a page is available
173 *
174 * Returns 0 on success
175 *
176 * @pcfd: Structure with fd, handler and name as above
177 * @client_addr: Address in the client program, not QEMU
178 * @rb: The RAMBlock the page is in
179 */
180int postcopy_wake_shared(struct PostCopyFD *pcfd, uint64_t client_addr,
181 RAMBlock *rb);
Dr. David Alan Gilbert096bf4c2018-03-12 17:21:12 +0000182/* Callback from shared fault handlers to ask for a page */
183int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
184 uint64_t client_addr, uint64_t offset);
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000185
Peter Xu36f62f12022-07-07 14:55:02 -0400186/* Hard-code channels for now for postcopy preemption */
187enum PostcopyChannels {
188 RAM_CHANNEL_PRECOPY = 0,
189 RAM_CHANNEL_POSTCOPY = 1,
190 RAM_CHANNEL_MAX,
191};
192
manish.mishra6720c2b2022-12-20 18:44:18 +0000193void postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file);
Peter Xufc063a72023-02-08 15:28:11 -0500194void postcopy_preempt_setup(MigrationState *s);
Peter Xu5655aab2023-02-08 15:28:13 -0500195int postcopy_preempt_establish_channel(MigrationState *s);
Peter Xu36f62f12022-07-07 14:55:02 -0400196
Dr. David Alan Gilberteb59db52015-11-05 18:10:55 +0000197#endif