blob: 6d2b3cf124e880447dfc5ddc8d8250608c5c8042 [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 */
Alexey Perevalovd7651f12017-09-19 19:47:56 +030017bool postcopy_ram_supported_by_host(MigrationIncomingState *mis);
Dr. David Alan Gilberteb59db52015-11-05 18:10:55 +000018
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000019/*
Dr. David Alan Gilbertf0a227a2015-11-05 18:11:04 +000020 * Make all of RAM sensitive to accesses to areas that haven't yet been written
21 * and wire up anything necessary to deal with it.
22 */
Wei Yang2a7eb142019-10-10 09:13:15 +080023int postcopy_ram_incoming_setup(MigrationIncomingState *mis);
Dr. David Alan Gilbertf0a227a2015-11-05 18:11:04 +000024
25/*
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +000026 * Initialise postcopy-ram, setting the RAM to a state where we can go into
27 * postcopy later; must be called prior to any precopy.
28 * called from ram.c's similarly named ram_postcopy_incoming_init
29 */
David Hildenbrandc1361802018-06-20 22:27:36 +020030int postcopy_ram_incoming_init(MigrationIncomingState *mis);
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +000031
32/*
33 * At the end of a migration where postcopy_ram_incoming_init was called.
34 */
35int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis);
36
37/*
Dr. David Alan Gilbertf9527102015-11-05 18:11:20 +000038 * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
39 * however leaving it until after precopy means that most of the precopy
40 * data is still THPd
41 */
42int postcopy_ram_prepare_discard(MigrationIncomingState *mis);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000043
44/*
45 * Called at the start of each RAMBlock by the bitmap code.
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000046 */
Wei Yang810cf2b2019-07-24 09:07:21 +080047void postcopy_discard_send_init(MigrationState *ms, const char *name);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000048
49/*
50 * Called by the bitmap code for each chunk to discard.
51 * May send a discard message, may just leave it queued to
52 * be sent later.
53 * @start,@length: a range of pages in the migration bitmap in the
54 * RAM block passed to postcopy_discard_send_init() (length=1 is one page)
55 */
Wei Yang810cf2b2019-07-24 09:07:21 +080056void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
57 unsigned long length);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000058
59/*
60 * Called at the end of each RAMBlock by the bitmap code.
Wei Yang810cf2b2019-07-24 09:07:21 +080061 * Sends any outstanding discard messages.
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000062 */
Wei Yang810cf2b2019-07-24 09:07:21 +080063void postcopy_discard_send_finish(MigrationState *ms);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +000064
Dr. David Alan Gilbert696ed9a2015-11-05 18:11:10 +000065/*
66 * Place a page (from) at (host) efficiently
67 * There are restrictions on how 'from' must be mapped, in general best
68 * to use other postcopy_ routines to allocate.
69 * returns 0 on success
70 */
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +000071int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
Alexey Perevalov8be46202017-10-05 14:13:18 +030072 RAMBlock *rb);
Dr. David Alan Gilbert696ed9a2015-11-05 18:11:10 +000073
74/*
75 * Place a zero page at (host) atomically
76 * returns 0 on success
77 */
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +000078int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
Alexey Perevalov8be46202017-10-05 14:13:18 +030079 RAMBlock *rb);
Dr. David Alan Gilbert696ed9a2015-11-05 18:11:10 +000080
Juan Quintelabac3b212017-04-24 16:50:35 +020081/* The current postcopy state is read/set by postcopy_state_get/set
82 * which update it atomically.
83 * The state is updated as postcopy messages are received, and
84 * in general only one thread should be writing to the state at any one
85 * time, initially the main thread and then the listen thread;
86 * Corner cases are where either thread finishes early and/or errors.
87 * The state is checked as messages are received to ensure that
88 * the source is sending us messages in the correct order.
89 * The state is also used by the RAM reception code to know if it
90 * has to place pages atomically, and the cleanup code at the end of
91 * the main thread to know if it has to delay cleanup until the end
92 * of postcopy.
93 */
94typedef enum {
95 POSTCOPY_INCOMING_NONE = 0, /* Initial state - no postcopy */
96 POSTCOPY_INCOMING_ADVISE,
97 POSTCOPY_INCOMING_DISCARD,
98 POSTCOPY_INCOMING_LISTENING,
99 POSTCOPY_INCOMING_RUNNING,
100 POSTCOPY_INCOMING_END
101} PostcopyState;
102
Juan Quintelabac3b212017-04-24 16:50:35 +0200103PostcopyState postcopy_state_get(void);
104/* Set the state and return the old state */
105PostcopyState postcopy_state_set(PostcopyState new_state);
106
Peter Xu9ab7ef92018-02-08 18:31:07 +0800107void postcopy_fault_thread_notify(MigrationIncomingState *mis);
108
Dr. David Alan Gilbert1693c642018-03-12 17:20:59 +0000109/*
110 * To be called once at the start before any device initialisation
111 */
112void postcopy_infrastructure_init(void);
113
114/* Add a notifier to a list to be called when checking whether the devices
115 * can support postcopy.
116 * It's data is a *PostcopyNotifyData
117 * It should return 0 if OK, or a negative value on failure.
118 * On failure it must set the data->errp to an error.
119 *
120 */
121enum PostcopyNotifyReason {
122 POSTCOPY_NOTIFY_PROBE = 0,
Dr. David Alan Gilbertd3dff7a2018-03-12 17:21:01 +0000123 POSTCOPY_NOTIFY_INBOUND_ADVISE,
Dr. David Alan Gilbert6864a7b2018-03-12 17:21:06 +0000124 POSTCOPY_NOTIFY_INBOUND_LISTEN,
Dr. David Alan Gilbert46343572018-03-12 17:21:20 +0000125 POSTCOPY_NOTIFY_INBOUND_END,
Dr. David Alan Gilbert1693c642018-03-12 17:20:59 +0000126};
127
128struct PostcopyNotifyData {
129 enum PostcopyNotifyReason reason;
130 Error **errp;
131};
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
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000138struct PostCopyFD;
139
140/* ufd is a pointer to the struct uffd_msg *TODO: more Portable! */
141typedef int (*pcfdhandler)(struct PostCopyFD *pcfd, void *ufd);
Dr. David Alan Gilbertd488b342018-03-12 17:21:15 +0000142/* Notification to wake, either on place or on reception of
143 * a fault on something that's already arrived (race)
144 */
145typedef int (*pcfdwake)(struct PostCopyFD *pcfd, RAMBlock *rb, uint64_t offset);
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000146
147struct PostCopyFD {
148 int fd;
149 /* Data to pass to handler */
150 void *data;
151 /* Handler to be called whenever we get a poll event */
152 pcfdhandler handler;
Dr. David Alan Gilbertd488b342018-03-12 17:21:15 +0000153 /* Notification to wake shared client */
154 pcfdwake waker;
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000155 /* A string to use in error messages */
156 const char *idstr;
157};
158
159/* Register a userfaultfd owned by an external process for
160 * shared memory.
161 */
162void postcopy_register_shared_ufd(struct PostCopyFD *pcfd);
163void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd);
zhaolichang3a4452d2020-09-17 15:50:21 +0800164/* Call each of the shared 'waker's registered telling them of
Dr. David Alan Gilbertd488b342018-03-12 17:21:15 +0000165 * availability of a block.
166 */
167int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset);
Dr. David Alan Gilbert5efc3562018-03-12 17:21:14 +0000168/* postcopy_wake_shared: Notify a client ufd that a page is available
169 *
170 * Returns 0 on success
171 *
172 * @pcfd: Structure with fd, handler and name as above
173 * @client_addr: Address in the client program, not QEMU
174 * @rb: The RAMBlock the page is in
175 */
176int postcopy_wake_shared(struct PostCopyFD *pcfd, uint64_t client_addr,
177 RAMBlock *rb);
Dr. David Alan Gilbert096bf4c2018-03-12 17:21:12 +0000178/* Callback from shared fault handlers to ask for a page */
179int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
180 uint64_t client_addr, uint64_t offset);
Dr. David Alan Gilbert00fa4fc2018-03-12 17:21:04 +0000181
Dr. David Alan Gilberteb59db52015-11-05 18:10:55 +0000182#endif