Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 1 | #include <sys/epoll.h> |
| 2 | #include <sys/un.h> |
| 3 | #include <sys/types.h> |
| 4 | #include <sys/socket.h> |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 5 | #include <dirent.h> |
| 6 | |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 7 | #include "kvm/epoll.h" |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 8 | #include "kvm/kvm-ipc.h" |
| 9 | #include "kvm/rwsem.h" |
| 10 | #include "kvm/read-write.h" |
| 11 | #include "kvm/util.h" |
| 12 | #include "kvm/kvm.h" |
| 13 | #include "kvm/builtin-debug.h" |
| 14 | #include "kvm/strbuf.h" |
| 15 | #include "kvm/kvm-cpu.h" |
| 16 | #include "kvm/8250-serial.h" |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 17 | |
Lai Jiangshan | a9aae6c | 2011-12-20 17:08:50 +0800 | [diff] [blame] | 18 | struct kvm_ipc_head { |
| 19 | u32 type; |
| 20 | u32 len; |
| 21 | }; |
| 22 | |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 23 | #define KVM_IPC_MAX_MSGS 16 |
| 24 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 25 | #define KVM_SOCK_SUFFIX ".sock" |
| 26 | #define KVM_SOCK_SUFFIX_LEN ((ssize_t)sizeof(KVM_SOCK_SUFFIX) - 1) |
| 27 | |
| 28 | extern __thread struct kvm_cpu *current_kvm_cpu; |
| 29 | static void (*msgs[KVM_IPC_MAX_MSGS])(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg); |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 30 | static DECLARE_RWSEM(msgs_rwlock); |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 31 | static int server_fd; |
| 32 | static struct kvm__epoll epoll; |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 33 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 34 | static int kvm__create_socket(struct kvm *kvm) |
| 35 | { |
| 36 | char full_name[PATH_MAX]; |
Andre Przywara | a2583db | 2015-07-17 17:02:09 +0100 | [diff] [blame] | 37 | int s; |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 38 | struct sockaddr_un local; |
| 39 | int len, r; |
| 40 | |
| 41 | /* This usually 108 bytes long */ |
| 42 | BUILD_BUG_ON(sizeof(local.sun_path) < 32); |
| 43 | |
| 44 | snprintf(full_name, sizeof(full_name), "%s/%s%s", |
| 45 | kvm__get_dir(), kvm->cfg.guest_name, KVM_SOCK_SUFFIX); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 46 | |
| 47 | s = socket(AF_UNIX, SOCK_STREAM, 0); |
Michael Ellerman | 211370d | 2013-02-06 19:19:12 +1100 | [diff] [blame] | 48 | if (s < 0) { |
| 49 | perror("socket"); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 50 | return s; |
Michael Ellerman | 211370d | 2013-02-06 19:19:12 +1100 | [diff] [blame] | 51 | } |
| 52 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 53 | local.sun_family = AF_UNIX; |
| 54 | strlcpy(local.sun_path, full_name, sizeof(local.sun_path)); |
| 55 | len = strlen(local.sun_path) + sizeof(local.sun_family); |
| 56 | r = bind(s, (struct sockaddr *)&local, len); |
Andre Przywara | ef5b941 | 2019-06-07 18:01:21 +0100 | [diff] [blame] | 57 | /* Check for an existing socket file */ |
| 58 | if (r < 0 && errno == EADDRINUSE) { |
| 59 | r = connect(s, (struct sockaddr *)&local, len); |
| 60 | if (r == 0) { |
| 61 | /* |
| 62 | * If we could connect, there is already a guest |
| 63 | * using this same name. This should not happen |
| 64 | * for PID derived names, but could happen for user |
| 65 | * provided guest names. |
| 66 | */ |
| 67 | pr_err("Guest socket file %s already exists.", |
| 68 | full_name); |
| 69 | r = -EEXIST; |
| 70 | goto fail; |
| 71 | } |
| 72 | if (errno == ECONNREFUSED) { |
| 73 | /* |
| 74 | * This is a ghost socket file, with no-one listening |
| 75 | * on the other end. Since kvmtool will only bind |
| 76 | * above when creating a new guest, there is no |
| 77 | * danger in just removing the file and re-trying. |
| 78 | */ |
| 79 | unlink(full_name); |
| 80 | pr_info("Removed ghost socket file \"%s\".", full_name); |
| 81 | r = bind(s, (struct sockaddr *)&local, len); |
| 82 | } |
| 83 | } |
Michael Ellerman | 211370d | 2013-02-06 19:19:12 +1100 | [diff] [blame] | 84 | if (r < 0) { |
| 85 | perror("bind"); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 86 | goto fail; |
Michael Ellerman | 211370d | 2013-02-06 19:19:12 +1100 | [diff] [blame] | 87 | } |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 88 | |
| 89 | r = listen(s, 5); |
Michael Ellerman | 211370d | 2013-02-06 19:19:12 +1100 | [diff] [blame] | 90 | if (r < 0) { |
| 91 | perror("listen"); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 92 | goto fail; |
Michael Ellerman | 211370d | 2013-02-06 19:19:12 +1100 | [diff] [blame] | 93 | } |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 94 | |
| 95 | return s; |
| 96 | |
| 97 | fail: |
| 98 | close(s); |
| 99 | return r; |
| 100 | } |
| 101 | |
| 102 | void kvm__remove_socket(const char *name) |
| 103 | { |
| 104 | char full_name[PATH_MAX]; |
| 105 | |
| 106 | snprintf(full_name, sizeof(full_name), "%s/%s%s", |
| 107 | kvm__get_dir(), name, KVM_SOCK_SUFFIX); |
| 108 | unlink(full_name); |
| 109 | } |
| 110 | |
| 111 | int kvm__get_sock_by_instance(const char *name) |
| 112 | { |
| 113 | int s, len, r; |
| 114 | char sock_file[PATH_MAX]; |
| 115 | struct sockaddr_un local; |
| 116 | |
| 117 | snprintf(sock_file, sizeof(sock_file), "%s/%s%s", |
| 118 | kvm__get_dir(), name, KVM_SOCK_SUFFIX); |
| 119 | s = socket(AF_UNIX, SOCK_STREAM, 0); |
| 120 | |
| 121 | local.sun_family = AF_UNIX; |
| 122 | strlcpy(local.sun_path, sock_file, sizeof(local.sun_path)); |
| 123 | len = strlen(local.sun_path) + sizeof(local.sun_family); |
| 124 | |
Andre Przywara | d77bd4f | 2015-07-17 17:02:14 +0100 | [diff] [blame] | 125 | r = connect(s, (struct sockaddr *)&local, len); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 126 | if (r < 0 && errno == ECONNREFUSED) { |
Andre Przywara | 67f9f7b | 2019-06-07 18:01:20 +0100 | [diff] [blame] | 127 | /* Clean up the ghost socket file */ |
| 128 | unlink(local.sun_path); |
| 129 | pr_info("Removed ghost socket file \"%s\".", sock_file); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 130 | return r; |
| 131 | } else if (r < 0) { |
| 132 | return r; |
| 133 | } |
| 134 | |
| 135 | return s; |
| 136 | } |
| 137 | |
Milan Kocian | 22fb13f | 2013-06-06 23:15:16 +0200 | [diff] [blame] | 138 | static bool is_socket(const char *base_path, const struct dirent *dent) |
| 139 | { |
| 140 | switch (dent->d_type) { |
| 141 | case DT_SOCK: |
| 142 | return true; |
| 143 | |
| 144 | case DT_UNKNOWN: { |
| 145 | char path[PATH_MAX]; |
| 146 | struct stat st; |
| 147 | |
| 148 | sprintf(path, "%s/%s", base_path, dent->d_name); |
| 149 | if (stat(path, &st)) |
| 150 | return false; |
| 151 | |
| 152 | return S_ISSOCK(st.st_mode); |
| 153 | } |
| 154 | default: |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 159 | int kvm__enumerate_instances(int (*callback)(const char *name, int fd)) |
| 160 | { |
| 161 | int sock; |
| 162 | DIR *dir; |
Michal Rostecki | d62653e | 2016-04-17 17:40:10 +0200 | [diff] [blame] | 163 | struct dirent *entry; |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 164 | int ret = 0; |
Milan Kocian | 22fb13f | 2013-06-06 23:15:16 +0200 | [diff] [blame] | 165 | const char *path; |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 166 | |
Milan Kocian | 22fb13f | 2013-06-06 23:15:16 +0200 | [diff] [blame] | 167 | path = kvm__get_dir(); |
| 168 | |
| 169 | dir = opendir(path); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 170 | if (!dir) |
| 171 | return -errno; |
| 172 | |
| 173 | for (;;) { |
Michal Rostecki | d62653e | 2016-04-17 17:40:10 +0200 | [diff] [blame] | 174 | entry = readdir(dir); |
| 175 | if (!entry) |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 176 | break; |
Michal Rostecki | d62653e | 2016-04-17 17:40:10 +0200 | [diff] [blame] | 177 | if (is_socket(path, entry)) { |
| 178 | ssize_t name_len = strlen(entry->d_name); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 179 | char *p; |
| 180 | |
| 181 | if (name_len <= KVM_SOCK_SUFFIX_LEN) |
| 182 | continue; |
| 183 | |
Michal Rostecki | d62653e | 2016-04-17 17:40:10 +0200 | [diff] [blame] | 184 | p = &entry->d_name[name_len - KVM_SOCK_SUFFIX_LEN]; |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 185 | if (memcmp(KVM_SOCK_SUFFIX, p, KVM_SOCK_SUFFIX_LEN)) |
| 186 | continue; |
| 187 | |
| 188 | *p = 0; |
Michal Rostecki | d62653e | 2016-04-17 17:40:10 +0200 | [diff] [blame] | 189 | sock = kvm__get_sock_by_instance(entry->d_name); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 190 | if (sock < 0) |
| 191 | continue; |
Michal Rostecki | d62653e | 2016-04-17 17:40:10 +0200 | [diff] [blame] | 192 | ret = callback(entry->d_name, sock); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 193 | close(sock); |
| 194 | if (ret < 0) |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | closedir(dir); |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | int kvm_ipc__register_handler(u32 type, void (*cb)(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg)) |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 205 | { |
| 206 | if (type >= KVM_IPC_MAX_MSGS) |
| 207 | return -ENOSPC; |
| 208 | |
| 209 | down_write(&msgs_rwlock); |
| 210 | msgs[type] = cb; |
| 211 | up_write(&msgs_rwlock); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Lai Jiangshan | 50dc18a | 2011-12-20 17:08:51 +0800 | [diff] [blame] | 216 | int kvm_ipc__send(int fd, u32 type) |
| 217 | { |
| 218 | struct kvm_ipc_head head = {.type = type, .len = 0,}; |
| 219 | |
Asias He | ca08826 | 2012-01-09 17:19:26 +0800 | [diff] [blame] | 220 | if (write_in_full(fd, &head, sizeof(head)) < 0) |
Lai Jiangshan | 50dc18a | 2011-12-20 17:08:51 +0800 | [diff] [blame] | 221 | return -1; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | int kvm_ipc__send_msg(int fd, u32 type, u32 len, u8 *msg) |
| 227 | { |
| 228 | struct kvm_ipc_head head = {.type = type, .len = len,}; |
| 229 | |
Asias He | ca08826 | 2012-01-09 17:19:26 +0800 | [diff] [blame] | 230 | if (write_in_full(fd, &head, sizeof(head)) < 0) |
Lai Jiangshan | 50dc18a | 2011-12-20 17:08:51 +0800 | [diff] [blame] | 231 | return -1; |
| 232 | |
Asias He | fd5b45a | 2012-01-09 17:18:01 +0800 | [diff] [blame] | 233 | if (write_in_full(fd, msg, len) < 0) |
Lai Jiangshan | 50dc18a | 2011-12-20 17:08:51 +0800 | [diff] [blame] | 234 | return -1; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 239 | static int kvm_ipc__handle(struct kvm *kvm, int fd, u32 type, u32 len, u8 *data) |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 240 | { |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 241 | void (*cb)(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg); |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 242 | |
Lai Jiangshan | 44a56bf | 2011-12-20 17:08:49 +0800 | [diff] [blame] | 243 | if (type >= KVM_IPC_MAX_MSGS) |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 244 | return -ENOSPC; |
| 245 | |
| 246 | down_read(&msgs_rwlock); |
Lai Jiangshan | 44a56bf | 2011-12-20 17:08:49 +0800 | [diff] [blame] | 247 | cb = msgs[type]; |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 248 | up_read(&msgs_rwlock); |
| 249 | |
| 250 | if (cb == NULL) { |
Lai Jiangshan | 44a56bf | 2011-12-20 17:08:49 +0800 | [diff] [blame] | 251 | pr_warning("No device handles type %u\n", type); |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 252 | return -ENODEV; |
| 253 | } |
| 254 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 255 | cb(kvm, fd, type, len, data); |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int kvm_ipc__new_conn(int fd) |
| 261 | { |
| 262 | int client; |
| 263 | struct epoll_event ev; |
| 264 | |
| 265 | client = accept(fd, NULL, NULL); |
| 266 | if (client < 0) |
| 267 | return -1; |
| 268 | |
| 269 | ev.events = EPOLLIN | EPOLLRDHUP; |
| 270 | ev.data.fd = client; |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 271 | if (epoll_ctl(epoll.fd, EPOLL_CTL_ADD, client, &ev) < 0) { |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 272 | close(client); |
| 273 | return -1; |
| 274 | } |
| 275 | |
| 276 | return client; |
| 277 | } |
| 278 | |
| 279 | static void kvm_ipc__close_conn(int fd) |
| 280 | { |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 281 | epoll_ctl(epoll.fd, EPOLL_CTL_DEL, fd, NULL); |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 282 | close(fd); |
| 283 | } |
| 284 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 285 | static int kvm_ipc__receive(struct kvm *kvm, int fd) |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 286 | { |
Lai Jiangshan | a9aae6c | 2011-12-20 17:08:50 +0800 | [diff] [blame] | 287 | struct kvm_ipc_head head; |
| 288 | u8 *msg = NULL; |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 289 | u32 n; |
| 290 | |
Lai Jiangshan | a9aae6c | 2011-12-20 17:08:50 +0800 | [diff] [blame] | 291 | n = read(fd, &head, sizeof(head)); |
| 292 | if (n != sizeof(head)) |
| 293 | goto done; |
| 294 | |
| 295 | msg = malloc(head.len); |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 296 | if (msg == NULL) |
| 297 | goto done; |
| 298 | |
Lai Jiangshan | a9aae6c | 2011-12-20 17:08:50 +0800 | [diff] [blame] | 299 | n = read_in_full(fd, msg, head.len); |
| 300 | if (n != head.len) |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 301 | goto done; |
| 302 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 303 | kvm_ipc__handle(kvm, fd, head.type, head.len, msg); |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 304 | |
Asias He | 8e463c6 | 2011-12-22 10:10:42 +0800 | [diff] [blame] | 305 | return 0; |
| 306 | |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 307 | done: |
| 308 | free(msg); |
Asias He | 8e463c6 | 2011-12-22 10:10:42 +0800 | [diff] [blame] | 309 | return -1; |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 312 | static void kvm_ipc__handle_event(struct kvm *kvm, struct epoll_event *ev) |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 313 | { |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 314 | int fd = ev->data.fd; |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 315 | |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 316 | if (fd == server_fd) { |
| 317 | int client, r; |
Sasha Levin | a4d8c55 | 2012-10-24 12:52:27 -0400 | [diff] [blame] | 318 | |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 319 | client = kvm_ipc__new_conn(fd); |
| 320 | /* |
| 321 | * Handle multiple IPC cmd at a time |
| 322 | */ |
| 323 | do { |
| 324 | r = kvm_ipc__receive(kvm, client); |
| 325 | } while (r == 0); |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 326 | |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 327 | } else if (ev->events & (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) { |
| 328 | kvm_ipc__close_conn(fd); |
| 329 | } else { |
| 330 | kvm_ipc__receive(kvm, fd); |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 331 | } |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 334 | static void kvm__pid(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg) |
| 335 | { |
| 336 | pid_t pid = getpid(); |
| 337 | int r = 0; |
| 338 | |
| 339 | if (type == KVM_IPC_PID) |
| 340 | r = write(fd, &pid, sizeof(pid)); |
| 341 | |
| 342 | if (r < 0) |
| 343 | pr_warning("Failed sending PID"); |
| 344 | } |
| 345 | |
| 346 | static void handle_stop(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg) |
| 347 | { |
| 348 | if (WARN_ON(type != KVM_IPC_STOP || len)) |
| 349 | return; |
| 350 | |
Will Deacon | 2aa76b2 | 2015-11-05 18:33:00 +0000 | [diff] [blame] | 351 | kvm__reboot(kvm); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* Pause/resume the guest using SIGUSR2 */ |
| 355 | static int is_paused; |
| 356 | |
| 357 | static void handle_pause(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg) |
| 358 | { |
| 359 | if (WARN_ON(len)) |
| 360 | return; |
| 361 | |
| 362 | if (type == KVM_IPC_RESUME && is_paused) { |
| 363 | kvm->vm_state = KVM_VMSTATE_RUNNING; |
Sasha Levin | 4346fd8 | 2012-09-17 10:03:32 +0200 | [diff] [blame] | 364 | kvm__continue(kvm); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 365 | } else if (type == KVM_IPC_PAUSE && !is_paused) { |
| 366 | kvm->vm_state = KVM_VMSTATE_PAUSED; |
| 367 | ioctl(kvm->vm_fd, KVM_KVMCLOCK_CTRL); |
Sasha Levin | 4346fd8 | 2012-09-17 10:03:32 +0200 | [diff] [blame] | 368 | kvm__pause(kvm); |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 369 | } else { |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | is_paused = !is_paused; |
| 374 | } |
| 375 | |
| 376 | static void handle_vmstate(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg) |
| 377 | { |
| 378 | int r = 0; |
| 379 | |
| 380 | if (type == KVM_IPC_VMSTATE) |
| 381 | r = write(fd, &kvm->vm_state, sizeof(kvm->vm_state)); |
| 382 | |
| 383 | if (r < 0) |
| 384 | pr_warning("Failed sending VMSTATE"); |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Serialize debug printout so that the output of multiple vcpus does not |
| 389 | * get mixed up: |
| 390 | */ |
| 391 | static int printout_done; |
| 392 | |
| 393 | static void handle_sigusr1(int sig) |
| 394 | { |
| 395 | struct kvm_cpu *cpu = current_kvm_cpu; |
| 396 | int fd = kvm_cpu__get_debug_fd(); |
| 397 | |
| 398 | if (!cpu || cpu->needs_nmi) |
| 399 | return; |
| 400 | |
| 401 | dprintf(fd, "\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id); |
| 402 | kvm_cpu__show_registers(cpu); |
| 403 | kvm_cpu__show_code(cpu); |
| 404 | kvm_cpu__show_page_tables(cpu); |
| 405 | fflush(stdout); |
| 406 | printout_done = 1; |
| 407 | } |
| 408 | |
| 409 | static void handle_debug(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg) |
| 410 | { |
| 411 | int i; |
| 412 | struct debug_cmd_params *params; |
| 413 | u32 dbg_type; |
| 414 | u32 vcpu; |
| 415 | |
| 416 | if (WARN_ON(type != KVM_IPC_DEBUG || len != sizeof(*params))) |
| 417 | return; |
| 418 | |
| 419 | params = (void *)msg; |
| 420 | dbg_type = params->dbg_type; |
| 421 | vcpu = params->cpu; |
| 422 | |
| 423 | if (dbg_type & KVM_DEBUG_CMD_TYPE_SYSRQ) |
| 424 | serial8250__inject_sysrq(kvm, params->sysrq); |
| 425 | |
| 426 | if (dbg_type & KVM_DEBUG_CMD_TYPE_NMI) { |
| 427 | if ((int)vcpu >= kvm->nrcpus) |
| 428 | return; |
| 429 | |
| 430 | kvm->cpus[vcpu]->needs_nmi = 1; |
| 431 | pthread_kill(kvm->cpus[vcpu]->thread, SIGUSR1); |
| 432 | } |
| 433 | |
| 434 | if (!(dbg_type & KVM_DEBUG_CMD_TYPE_DUMP)) |
| 435 | return; |
| 436 | |
| 437 | for (i = 0; i < kvm->nrcpus; i++) { |
| 438 | struct kvm_cpu *cpu = kvm->cpus[i]; |
| 439 | |
| 440 | if (!cpu) |
| 441 | continue; |
| 442 | |
| 443 | printout_done = 0; |
| 444 | |
| 445 | kvm_cpu__set_debug_fd(fd); |
| 446 | pthread_kill(cpu->thread, SIGUSR1); |
| 447 | /* |
| 448 | * Wait for the vCPU to dump state before signalling |
| 449 | * the next thread. Since this is debug code it does |
| 450 | * not matter that we are burning CPU time a bit: |
| 451 | */ |
| 452 | while (!printout_done) |
| 453 | sleep(0); |
| 454 | } |
| 455 | |
| 456 | close(fd); |
| 457 | |
| 458 | serial8250__inject_sysrq(kvm, 'p'); |
| 459 | } |
| 460 | |
| 461 | int kvm_ipc__init(struct kvm *kvm) |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 462 | { |
Yang Bai | e21e8ff | 2012-02-10 17:55:35 +0800 | [diff] [blame] | 463 | int ret; |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 464 | int sock = kvm__create_socket(kvm); |
Sasha Levin | 21b3c2c | 2011-12-06 14:15:30 +0200 | [diff] [blame] | 465 | struct epoll_event ev = {0}; |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 466 | |
| 467 | server_fd = sock; |
| 468 | |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 469 | ret = epoll__init(kvm, &epoll, "kvm-ipc", |
| 470 | kvm_ipc__handle_event); |
| 471 | if (ret) { |
| 472 | pr_err("Failed starting IPC thread"); |
Yang Bai | e21e8ff | 2012-02-10 17:55:35 +0800 | [diff] [blame] | 473 | goto err; |
| 474 | } |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 475 | |
Sasha Levin | 47f72b9 | 2011-10-28 11:01:54 +0200 | [diff] [blame] | 476 | ev.events = EPOLLIN | EPOLLET; |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 477 | ev.data.fd = sock; |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 478 | if (epoll_ctl(epoll.fd, EPOLL_CTL_ADD, sock, &ev) < 0) { |
Michael Ellerman | 211370d | 2013-02-06 19:19:12 +1100 | [diff] [blame] | 479 | pr_err("Failed adding socket to epoll"); |
Yang Bai | e21e8ff | 2012-02-10 17:55:35 +0800 | [diff] [blame] | 480 | ret = -EFAULT; |
| 481 | goto err_epoll; |
| 482 | } |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 483 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 484 | kvm_ipc__register_handler(KVM_IPC_PID, kvm__pid); |
| 485 | kvm_ipc__register_handler(KVM_IPC_DEBUG, handle_debug); |
| 486 | kvm_ipc__register_handler(KVM_IPC_PAUSE, handle_pause); |
| 487 | kvm_ipc__register_handler(KVM_IPC_RESUME, handle_pause); |
| 488 | kvm_ipc__register_handler(KVM_IPC_STOP, handle_stop); |
| 489 | kvm_ipc__register_handler(KVM_IPC_VMSTATE, handle_vmstate); |
| 490 | signal(SIGUSR1, handle_sigusr1); |
| 491 | |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 492 | return 0; |
Yang Bai | e21e8ff | 2012-02-10 17:55:35 +0800 | [diff] [blame] | 493 | |
Yang Bai | e21e8ff | 2012-02-10 17:55:35 +0800 | [diff] [blame] | 494 | err_epoll: |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 495 | epoll__exit(&epoll); |
| 496 | close(server_fd); |
Yang Bai | e21e8ff | 2012-02-10 17:55:35 +0800 | [diff] [blame] | 497 | err: |
| 498 | return ret; |
Sasha Levin | 4b1adda | 2011-10-16 21:57:54 +0200 | [diff] [blame] | 499 | } |
Sasha Levin | 49a8afd | 2012-09-17 10:03:30 +0200 | [diff] [blame] | 500 | base_init(kvm_ipc__init); |
Sasha Levin | c733c80 | 2011-10-25 13:30:53 +0200 | [diff] [blame] | 501 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 502 | int kvm_ipc__exit(struct kvm *kvm) |
Sasha Levin | c733c80 | 2011-10-25 13:30:53 +0200 | [diff] [blame] | 503 | { |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 504 | epoll__exit(&epoll); |
Sasha Levin | c733c80 | 2011-10-25 13:30:53 +0200 | [diff] [blame] | 505 | close(server_fd); |
Sasha Levin | c733c80 | 2011-10-25 13:30:53 +0200 | [diff] [blame] | 506 | |
Sasha Levin | e106372 | 2012-09-05 10:31:56 +0200 | [diff] [blame] | 507 | kvm__remove_socket(kvm->cfg.guest_name); |
| 508 | |
Jean-Philippe Brucker | d30d948 | 2023-06-06 14:04:24 +0100 | [diff] [blame] | 509 | return 0; |
Sasha Levin | c733c80 | 2011-10-25 13:30:53 +0200 | [diff] [blame] | 510 | } |
Sasha Levin | 49a8afd | 2012-09-17 10:03:30 +0200 | [diff] [blame] | 511 | base_exit(kvm_ipc__exit); |