1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "qemu/cutils.h"
15#include "qemu/error-report.h"
16#include "system/runstate.h"
17#include "qapi/error.h"
18#include "migration.h"
19#include "migration/global_state.h"
20#include "migration/vmstate.h"
21#include "trace.h"
22
23typedef struct {
24 uint32_t size;
25
26
27
28
29
30 uint8_t runstate[32];
31 uint8_t has_vm_was_suspended;
32 uint8_t vm_was_suspended;
33 uint8_t unused[66];
34
35 RunState state;
36 bool received;
37} GlobalState;
38
39static GlobalState global_state;
40
41static void global_state_do_store(RunState state)
42{
43 const char *state_str = RunState_str(state);
44 assert(strlen(state_str) < sizeof(global_state.runstate));
45 strpadcpy((char *)global_state.runstate, sizeof(global_state.runstate),
46 state_str, '\0');
47 global_state.has_vm_was_suspended = true;
48 global_state.vm_was_suspended = vm_get_suspended();
49
50 memset(global_state.unused, 0, sizeof(global_state.unused));
51}
52
53void global_state_store(void)
54{
55 global_state_do_store(runstate_get());
56}
57
58void global_state_store_running(void)
59{
60 global_state_do_store(RUN_STATE_RUNNING);
61}
62
63bool global_state_received(void)
64{
65 return global_state.received;
66}
67
68RunState global_state_get_runstate(void)
69{
70 return global_state.state;
71}
72
73static bool global_state_needed(void *opaque)
74{
75 return migrate_get_current()->store_global_state;
76}
77
78static int global_state_post_load(void *opaque, int version_id)
79{
80 GlobalState *s = opaque;
81 Error *local_err = NULL;
82 int r;
83 char *runstate = (char *)s->runstate;
84
85 s->received = true;
86 trace_migrate_global_state_post_load(runstate);
87
88 if (strnlen((char *)s->runstate,
89 sizeof(s->runstate)) == sizeof(s->runstate)) {
90
91
92
93
94
95
96
97 s->runstate[sizeof(s->runstate) - 1] = '\0';
98 }
99 r = qapi_enum_parse(&RunState_lookup, runstate, -1, &local_err);
100
101 if (r == -1) {
102 if (local_err) {
103 error_report_err(local_err);
104 }
105 return -EINVAL;
106 }
107 s->state = r;
108
109
110
111
112
113
114
115 vm_set_suspended(s->vm_was_suspended || r == RUN_STATE_SUSPENDED);
116
117 return 0;
118}
119
120static int global_state_pre_save(void *opaque)
121{
122 GlobalState *s = opaque;
123
124 trace_migrate_global_state_pre_save((char *)s->runstate);
125 s->size = strnlen((char *)s->runstate, sizeof(s->runstate)) + 1;
126 assert(s->size <= sizeof(s->runstate));
127
128 return 0;
129}
130
131static const VMStateDescription vmstate_globalstate = {
132 .name = "globalstate",
133 .version_id = 1,
134 .minimum_version_id = 1,
135 .post_load = global_state_post_load,
136 .pre_save = global_state_pre_save,
137 .needed = global_state_needed,
138 .fields = (const VMStateField[]) {
139 VMSTATE_UINT32(size, GlobalState),
140 VMSTATE_BUFFER(runstate, GlobalState),
141 VMSTATE_UINT8(has_vm_was_suspended, GlobalState),
142 VMSTATE_UINT8(vm_was_suspended, GlobalState),
143 VMSTATE_BUFFER(unused, GlobalState),
144 VMSTATE_END_OF_LIST()
145 },
146};
147
148void register_global_state(void)
149{
150
151 strcpy((char *)&global_state.runstate, "");
152 global_state.received = false;
153 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state);
154}
155