]>
Commit | Line | Data |
---|---|---|
eb59db53 DDAG |
1 | /* |
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 */ | |
74c38cf7 PX |
17 | bool postcopy_ram_supported_by_host(MigrationIncomingState *mis, |
18 | Error **errp); | |
eb59db53 | 19 | |
f0a227ad DDAG |
20 | /* |
21 | * 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 | */ | |
2a7eb148 | 24 | int postcopy_ram_incoming_setup(MigrationIncomingState *mis); |
f0a227ad | 25 | |
1caddf8a DDAG |
26 | /* |
27 | * 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 | */ | |
c136180c | 31 | int postcopy_ram_incoming_init(MigrationIncomingState *mis); |
1caddf8a DDAG |
32 | |
33 | /* | |
34 | * At the end of a migration where postcopy_ram_incoming_init was called. | |
35 | */ | |
36 | int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis); | |
37 | ||
f9527107 DDAG |
38 | /* |
39 | * 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 | */ | |
43 | int postcopy_ram_prepare_discard(MigrationIncomingState *mis); | |
e0b266f0 DDAG |
44 | |
45 | /* | |
46 | * Called at the start of each RAMBlock by the bitmap code. | |
e0b266f0 | 47 | */ |
810cf2bb | 48 | void postcopy_discard_send_init(MigrationState *ms, const char *name); |
e0b266f0 DDAG |
49 | |
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 | */ | |
810cf2bb WY |
57 | void postcopy_discard_send_range(MigrationState *ms, unsigned long start, |
58 | unsigned long length); | |
e0b266f0 DDAG |
59 | |
60 | /* | |
61 | * Called at the end of each RAMBlock by the bitmap code. | |
810cf2bb | 62 | * Sends any outstanding discard messages. |
e0b266f0 | 63 | */ |
810cf2bb | 64 | void postcopy_discard_send_finish(MigrationState *ms); |
e0b266f0 | 65 | |
696ed9a9 DDAG |
66 | /* |
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 | */ | |
df9ff5e1 | 72 | int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from, |
8be4620b | 73 | RAMBlock *rb); |
696ed9a9 DDAG |
74 | |
75 | /* | |
76 | * Place a zero page at (host) atomically | |
77 | * returns 0 on success | |
78 | */ | |
df9ff5e1 | 79 | int postcopy_place_page_zero(MigrationIncomingState *mis, void *host, |
8be4620b | 80 | RAMBlock *rb); |
696ed9a9 | 81 | |
bac3b212 JQ |
82 | /* 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 | */ | |
95 | typedef 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 | ||
bac3b212 JQ |
104 | PostcopyState postcopy_state_get(void); |
105 | /* Set the state and return the old state */ | |
106 | PostcopyState postcopy_state_set(PostcopyState new_state); | |
107 | ||
9ab7ef9b PX |
108 | void postcopy_fault_thread_notify(MigrationIncomingState *mis); |
109 | ||
1693c64c DDAG |
110 | /* |
111 | * To be called once at the start before any device initialisation | |
112 | */ | |
113 | void 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 | */ | |
122 | enum PostcopyNotifyReason { | |
123 | POSTCOPY_NOTIFY_PROBE = 0, | |
d3dff7a5 | 124 | POSTCOPY_NOTIFY_INBOUND_ADVISE, |
6864a7b5 | 125 | POSTCOPY_NOTIFY_INBOUND_LISTEN, |
46343570 | 126 | POSTCOPY_NOTIFY_INBOUND_END, |
1693c64c DDAG |
127 | }; |
128 | ||
129 | struct PostcopyNotifyData { | |
130 | enum PostcopyNotifyReason reason; | |
131 | Error **errp; | |
132 | }; | |
133 | ||
134 | void postcopy_add_notifier(NotifierWithReturn *nn); | |
135 | void postcopy_remove_notifier(NotifierWithReturn *n); | |
136 | /* Call the notifier list set by postcopy_add_start_notifier */ | |
137 | int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp); | |
138 | ||
095c12a4 PX |
139 | void postcopy_thread_create(MigrationIncomingState *mis, |
140 | QemuThread *thread, const char *name, | |
141 | void *(*fn)(void *), int joinable); | |
142 | ||
00fa4fc8 DDAG |
143 | struct PostCopyFD; |
144 | ||
145 | /* ufd is a pointer to the struct uffd_msg *TODO: more Portable! */ | |
146 | typedef int (*pcfdhandler)(struct PostCopyFD *pcfd, void *ufd); | |
d488b349 DDAG |
147 | /* Notification to wake, either on place or on reception of |
148 | * a fault on something that's already arrived (race) | |
149 | */ | |
150 | typedef int (*pcfdwake)(struct PostCopyFD *pcfd, RAMBlock *rb, uint64_t offset); | |
00fa4fc8 DDAG |
151 | |
152 | struct PostCopyFD { | |
153 | int fd; | |
154 | /* Data to pass to handler */ | |
155 | void *data; | |
156 | /* Handler to be called whenever we get a poll event */ | |
157 | pcfdhandler handler; | |
d488b349 DDAG |
158 | /* Notification to wake shared client */ |
159 | pcfdwake waker; | |
00fa4fc8 DDAG |
160 | /* A string to use in error messages */ |
161 | const char *idstr; | |
162 | }; | |
163 | ||
164 | /* Register a userfaultfd owned by an external process for | |
165 | * shared memory. | |
166 | */ | |
167 | void postcopy_register_shared_ufd(struct PostCopyFD *pcfd); | |
168 | void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd); | |
3a4452d8 | 169 | /* Call each of the shared 'waker's registered telling them of |
d488b349 DDAG |
170 | * availability of a block. |
171 | */ | |
172 | int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset); | |
5efc3564 DDAG |
173 | /* postcopy_wake_shared: Notify a client ufd that a page is available |
174 | * | |
175 | * Returns 0 on success | |
176 | * | |
177 | * @pcfd: Structure with fd, handler and name as above | |
178 | * @client_addr: Address in the client program, not QEMU | |
179 | * @rb: The RAMBlock the page is in | |
180 | */ | |
181 | int postcopy_wake_shared(struct PostCopyFD *pcfd, uint64_t client_addr, | |
182 | RAMBlock *rb); | |
096bf4c8 DDAG |
183 | /* Callback from shared fault handlers to ask for a page */ |
184 | int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb, | |
185 | uint64_t client_addr, uint64_t offset); | |
00fa4fc8 | 186 | |
36f62f11 PX |
187 | /* Hard-code channels for now for postcopy preemption */ |
188 | enum PostcopyChannels { | |
189 | RAM_CHANNEL_PRECOPY = 0, | |
190 | RAM_CHANNEL_POSTCOPY = 1, | |
191 | RAM_CHANNEL_MAX, | |
192 | }; | |
193 | ||
6720c2b3 | 194 | void postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file); |
fc063a7b | 195 | void postcopy_preempt_setup(MigrationState *s); |
5655aab0 | 196 | int postcopy_preempt_establish_channel(MigrationState *s); |
36f62f11 | 197 | |
eb59db53 | 198 | #endif |