Dr. David Alan Gilbert | eb59db5 | 2015-11-05 18:10:55 +0000 | [diff] [blame] | 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 */ |
Alexey Perevalov | d7651f1 | 2017-09-19 19:47:56 +0300 | [diff] [blame] | 17 | bool postcopy_ram_supported_by_host(MigrationIncomingState *mis); |
Dr. David Alan Gilbert | eb59db5 | 2015-11-05 18:10:55 +0000 | [diff] [blame] | 18 | |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 19 | /* |
Dr. David Alan Gilbert | f0a227a | 2015-11-05 18:11:04 +0000 | [diff] [blame] | 20 | * 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 | */ |
| 23 | int postcopy_ram_enable_notify(MigrationIncomingState *mis); |
| 24 | |
| 25 | /* |
Dr. David Alan Gilbert | 1caddf8 | 2015-11-05 18:11:03 +0000 | [diff] [blame] | 26 | * 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 | */ |
| 30 | int postcopy_ram_incoming_init(MigrationIncomingState *mis, size_t ram_pages); |
| 31 | |
| 32 | /* |
| 33 | * At the end of a migration where postcopy_ram_incoming_init was called. |
| 34 | */ |
| 35 | int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis); |
| 36 | |
| 37 | /* |
Dr. David Alan Gilbert | f952710 | 2015-11-05 18:11:20 +0000 | [diff] [blame] | 38 | * 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 | */ |
| 42 | int postcopy_ram_prepare_discard(MigrationIncomingState *mis); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Called at the start of each RAMBlock by the bitmap code. |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 46 | * Returns a new PDS |
| 47 | */ |
| 48 | PostcopyDiscardState *postcopy_discard_send_init(MigrationState *ms, |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 49 | const char *name); |
| 50 | |
| 51 | /* |
| 52 | * Called by the bitmap code for each chunk to discard. |
| 53 | * May send a discard message, may just leave it queued to |
| 54 | * be sent later. |
| 55 | * @start,@length: a range of pages in the migration bitmap in the |
| 56 | * RAM block passed to postcopy_discard_send_init() (length=1 is one page) |
| 57 | */ |
| 58 | void postcopy_discard_send_range(MigrationState *ms, PostcopyDiscardState *pds, |
| 59 | unsigned long start, unsigned long length); |
| 60 | |
| 61 | /* |
| 62 | * Called at the end of each RAMBlock by the bitmap code. |
| 63 | * Sends any outstanding discard messages, frees the PDS. |
| 64 | */ |
| 65 | void postcopy_discard_send_finish(MigrationState *ms, |
| 66 | PostcopyDiscardState *pds); |
| 67 | |
Dr. David Alan Gilbert | 696ed9a | 2015-11-05 18:11:10 +0000 | [diff] [blame] | 68 | /* |
| 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 Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 74 | int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from, |
Alexey Perevalov | 8be4620 | 2017-10-05 14:13:18 +0300 | [diff] [blame] | 75 | RAMBlock *rb); |
Dr. David Alan Gilbert | 696ed9a | 2015-11-05 18:11:10 +0000 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Place a zero page at (host) atomically |
| 79 | * returns 0 on success |
| 80 | */ |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 81 | int postcopy_place_page_zero(MigrationIncomingState *mis, void *host, |
Alexey Perevalov | 8be4620 | 2017-10-05 14:13:18 +0300 | [diff] [blame] | 82 | RAMBlock *rb); |
Dr. David Alan Gilbert | 696ed9a | 2015-11-05 18:11:10 +0000 | [diff] [blame] | 83 | |
Juan Quintela | bac3b21 | 2017-04-24 16:50:35 +0200 | [diff] [blame] | 84 | /* 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 | */ |
| 97 | typedef 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 | |
Dr. David Alan Gilbert | 696ed9a | 2015-11-05 18:11:10 +0000 | [diff] [blame] | 106 | /* |
| 107 | * Allocate a page of memory that can be mapped at a later point in time |
| 108 | * using postcopy_place_page |
| 109 | * Returns: Pointer to allocated page |
| 110 | */ |
| 111 | void *postcopy_get_tmp_page(MigrationIncomingState *mis); |
| 112 | |
Juan Quintela | bac3b21 | 2017-04-24 16:50:35 +0200 | [diff] [blame] | 113 | PostcopyState postcopy_state_get(void); |
| 114 | /* Set the state and return the old state */ |
| 115 | PostcopyState postcopy_state_set(PostcopyState new_state); |
| 116 | |
Peter Xu | 9ab7ef9 | 2018-02-08 18:31:07 +0800 | [diff] [blame] | 117 | void postcopy_fault_thread_notify(MigrationIncomingState *mis); |
| 118 | |
Dr. David Alan Gilbert | eb59db5 | 2015-11-05 18:10:55 +0000 | [diff] [blame] | 119 | #endif |