master
c 538 lines 14.3 KB
Raw
1 /*
2 * Privileged RAPL MSR helper commands for QEMU
3 *
4 * Copyright (C) 2024 Red Hat, Inc. <aharivel@redhat.com>
5 *
6 * Author: Anthony Harivel <aharivel@redhat.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "qemu/osdep.h"
22 #include <getopt.h>
23 #include <sys/ioctl.h>
24 #ifdef CONFIG_LIBCAP_NG
25 #include <cap-ng.h>
26 #endif
27 #include <pwd.h>
28 #include <grp.h>
29
30 #include "qemu/help-texts.h"
31 #include "qapi/error.h"
32 #include "qemu/cutils.h"
33 #include "qemu/main-loop.h"
34 #include "qemu/module.h"
35 #include "qemu/error-report.h"
36 #include "qemu/config-file.h"
37 #include "qemu-version.h"
38 #include "qapi/error.h"
39 #include "qemu/error-report.h"
40 #include "qemu/log.h"
41 #include "qemu/systemd.h"
42 #include "io/channel.h"
43 #include "io/channel-socket.h"
44 #include "trace/control.h"
45 #include "qemu-version.h"
46 #include "rapl-msr-index.h"
47
48 #define MSR_PATH_TEMPLATE "/dev/cpu/%u/msr"
49
50 static char *socket_path;
51 static char *pidfile;
52 static enum { RUNNING, TERMINATE, TERMINATING } state;
53 static QIOChannelSocket *server_ioc;
54 static int server_watch;
55 static int num_active_sockets = 1;
56 static bool verbose;
57
58 #ifdef CONFIG_LIBCAP_NG
59 static int uid = -1;
60 static int gid = -1;
61 #endif
62
63 static void compute_default_paths(void)
64 {
65 g_autofree char *state = qemu_get_local_state_dir();
66
67 socket_path = g_build_filename(state, "run", "qemu-vmsr-helper.sock", NULL);
68 pidfile = g_build_filename(state, "run", "qemu-vmsr-helper.pid", NULL);
69 }
70
71 static int is_intel_processor(void)
72 {
73 int ebx, ecx, edx;
74
75 /* Execute CPUID instruction with eax=0 (basic identification) */
76 asm volatile (
77 "cpuid"
78 : "=b" (ebx), "=c" (ecx), "=d" (edx)
79 : "a" (0)
80 );
81
82 /*
83 * Check if processor is "GenuineIntel"
84 * 0x756e6547 = "Genu"
85 * 0x49656e69 = "ineI"
86 * 0x6c65746e = "ntel"
87 */
88 return (ebx == 0x756e6547) && (edx == 0x49656e69) && (ecx == 0x6c65746e);
89 }
90
91 static int is_rapl_enabled(void)
92 {
93 const char *path = "/sys/class/powercap/intel-rapl/enabled";
94 FILE *file = fopen(path, "r");
95 int value = 0;
96
97 if (file != NULL) {
98 if (fscanf(file, "%d", &value) != 1) {
99 error_report("INTEL RAPL not enabled");
100 }
101 fclose(file);
102 } else {
103 error_report("Error opening %s", path);
104 }
105
106 return value;
107 }
108
109 /*
110 * Check if the TID that request the MSR read
111 * belongs to the peer. It be should a TID of a vCPU.
112 */
113 static bool is_tid_present(pid_t pid, pid_t tid)
114 {
115 g_autofree char *tidPath = g_strdup_printf("/proc/%d/task/%d", pid, tid);
116
117 /* Check if the TID directory exists within the PID directory */
118 if (access(tidPath, F_OK) == 0) {
119 return true;
120 }
121
122 error_report("Failed to open /proc at %s", tidPath);
123 return false;
124 }
125
126 /*
127 * Only the RAPL MSR in target/i386/cpu.h are allowed
128 */
129 static bool is_msr_allowed(uint32_t reg)
130 {
131 switch (reg) {
132 case MSR_RAPL_POWER_UNIT:
133 case MSR_PKG_POWER_LIMIT:
134 case MSR_PKG_ENERGY_STATUS:
135 case MSR_PKG_POWER_INFO:
136 return true;
137 default:
138 return false;
139 }
140 }
141
142 static uint64_t vmsr_read_msr(uint32_t msr_register, unsigned int cpu_id)
143 {
144 int fd;
145 uint64_t result = 0;
146
147 g_autofree char *path = g_strdup_printf(MSR_PATH_TEMPLATE, cpu_id);
148
149 fd = open(path, O_RDONLY);
150 if (fd < 0) {
151 error_report("Failed to open MSR file at %s", path);
152 return result;
153 }
154
155 if (pread(fd, &result, sizeof(result), msr_register) != sizeof(result)) {
156 error_report("Failed to read MSR");
157 result = 0;
158 }
159
160 close(fd);
161 return result;
162 }
163
164 static void usage(const char *name)
165 {
166 (printf) (
167 "Usage: %s [OPTIONS] FILE\n"
168 "Virtual RAPL MSR helper program for QEMU\n"
169 "\n"
170 " -h, --help display this help and exit\n"
171 " -V, --version output version information and exit\n"
172 "\n"
173 " -d, --daemon run in the background\n"
174 " -f, --pidfile=PATH PID file when running as a daemon\n"
175 " (default '%s')\n"
176 " -k, --socket=PATH path to the unix socket\n"
177 " (default '%s')\n"
178 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
179 " specify tracing options\n"
180 #ifdef CONFIG_LIBCAP_NG
181 " -u, --user=USER user to drop privileges to\n"
182 " -g, --group=GROUP group to drop privileges to\n"
183 #endif
184 "\n"
185 QEMU_HELP_BOTTOM "\n"
186 , name, pidfile, socket_path);
187 }
188
189 static void version(const char *name)
190 {
191 printf(
192 "%s " QEMU_FULL_VERSION "\n"
193 "Written by Anthony Harivel.\n"
194 "\n"
195 QEMU_COPYRIGHT "\n"
196 "This is free software; see the source for copying conditions. There is NO\n"
197 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
198 , name);
199 }
200
201 typedef struct VMSRHelperClient {
202 QIOChannelSocket *ioc;
203 Coroutine *co;
204 } VMSRHelperClient;
205
206 static void coroutine_fn vh_co_entry(void *opaque)
207 {
208 VMSRHelperClient *client = opaque;
209 Error *local_err = NULL;
210 unsigned int peer_pid;
211 uint32_t request[3];
212 uint64_t vmsr;
213 int r;
214
215 if (!qio_channel_set_blocking(QIO_CHANNEL(client->ioc),
216 false, &local_err)) {
217 goto out;
218 }
219
220 qio_channel_set_follow_coroutine_ctx(QIO_CHANNEL(client->ioc), true);
221
222 /*
223 * Check peer credentials
224 */
225 r = qio_channel_get_peerpid(QIO_CHANNEL(client->ioc),
226 &peer_pid,
227 &local_err);
228 if (r < 0) {
229 goto out;
230 }
231
232 for (;;) {
233 /*
234 * Read the requested MSR
235 * Only RAPL MSR in rapl-msr-index.h is allowed
236 */
237 r = qio_channel_read_all_eof(QIO_CHANNEL(client->ioc),
238 (char *) &request, sizeof(request), &local_err);
239 if (r <= 0) {
240 break;
241 }
242
243 if (!is_msr_allowed(request[0])) {
244 error_report("Requested unallowed msr: %d", request[0]);
245 break;
246 }
247
248 vmsr = vmsr_read_msr(request[0], request[1]);
249
250 if (!is_tid_present(peer_pid, request[2])) {
251 error_report("Requested TID not in peer PID: %d %d",
252 peer_pid, request[2]);
253 vmsr = 0;
254 }
255
256 r = qio_channel_write_all(QIO_CHANNEL(client->ioc),
257 (char *) &vmsr,
258 sizeof(vmsr),
259 &local_err);
260 if (r < 0) {
261 break;
262 }
263 }
264
265 out:
266 if (local_err) {
267 if (!verbose) {
268 error_free(local_err);
269 } else {
270 error_report_err(local_err);
271 }
272 }
273
274 object_unref(OBJECT(client->ioc));
275 g_free(client);
276 }
277
278 static gboolean accept_client(QIOChannel *ioc,
279 GIOCondition cond,
280 gpointer opaque)
281 {
282 QIOChannelSocket *cioc;
283 VMSRHelperClient *vmsrh;
284
285 cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
286 NULL);
287 if (!cioc) {
288 return TRUE;
289 }
290
291 vmsrh = g_new(VMSRHelperClient, 1);
292 vmsrh->ioc = cioc;
293 vmsrh->co = qemu_coroutine_create(vh_co_entry, vmsrh);
294 qemu_coroutine_enter(vmsrh->co);
295
296 return TRUE;
297 }
298
299 static void termsig_handler(int signum)
300 {
301 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
302 qemu_notify_event();
303 }
304
305 static void close_server_socket(void)
306 {
307 assert(server_ioc);
308
309 g_source_remove(server_watch);
310 server_watch = -1;
311 object_unref(OBJECT(server_ioc));
312 num_active_sockets--;
313 }
314
315 #ifdef CONFIG_LIBCAP_NG
316 static int drop_privileges(void)
317 {
318 /* clear all capabilities */
319 capng_clear(CAPNG_SELECT_BOTH);
320
321 if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
322 CAP_SYS_RAWIO) < 0) {
323 return -1;
324 }
325
326 return 0;
327 }
328 #endif
329
330 int main(int argc, char **argv)
331 {
332 const char *sopt = "hVk:f:dT:u:g:vq";
333 struct option lopt[] = {
334 { "help", no_argument, NULL, 'h' },
335 { "version", no_argument, NULL, 'V' },
336 { "socket", required_argument, NULL, 'k' },
337 { "pidfile", required_argument, NULL, 'f' },
338 { "daemon", no_argument, NULL, 'd' },
339 { "trace", required_argument, NULL, 'T' },
340 { "verbose", no_argument, NULL, 'v' },
341 { NULL, 0, NULL, 0 }
342 };
343 int opt_ind = 0;
344 int ch;
345 Error *local_err = NULL;
346 bool daemonize = false;
347 bool pidfile_specified = false;
348 bool socket_path_specified = false;
349 unsigned socket_activation;
350
351 struct sigaction sa_sigterm;
352 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
353 sa_sigterm.sa_handler = termsig_handler;
354 sigaction(SIGTERM, &sa_sigterm, NULL);
355 sigaction(SIGINT, &sa_sigterm, NULL);
356 sigaction(SIGHUP, &sa_sigterm, NULL);
357
358 signal(SIGPIPE, SIG_IGN);
359
360 error_init(argv[0]);
361 module_call_init(MODULE_INIT_TRACE);
362 module_call_init(MODULE_INIT_QOM);
363 qemu_add_opts(&qemu_trace_opts);
364 qemu_init_exec_dir(argv[0]);
365
366 compute_default_paths();
367
368 /*
369 * Sanity check
370 * 1. cpu must be Intel cpu
371 * 2. RAPL must be enabled
372 */
373 if (!is_intel_processor()) {
374 error_report("error: CPU is not INTEL cpu");
375 exit(EXIT_FAILURE);
376 }
377
378 if (!is_rapl_enabled()) {
379 error_report("error: RAPL driver not enable");
380 exit(EXIT_FAILURE);
381 }
382
383 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
384 switch (ch) {
385 case 'k':
386 g_free(socket_path);
387 socket_path = g_strdup(optarg);
388 socket_path_specified = true;
389 if (socket_path[0] != '/') {
390 error_report("socket path must be absolute");
391 exit(EXIT_FAILURE);
392 }
393 break;
394 case 'f':
395 g_free(pidfile);
396 pidfile = g_strdup(optarg);
397 pidfile_specified = true;
398 break;
399 #ifdef CONFIG_LIBCAP_NG
400 case 'u': {
401 unsigned long res;
402 struct passwd *userinfo = getpwnam(optarg);
403 if (userinfo) {
404 uid = userinfo->pw_uid;
405 } else if (qemu_strtoul(optarg, NULL, 10, &res) == 0 &&
406 (uid_t)res == res) {
407 uid = res;
408 } else {
409 error_report("invalid user '%s'", optarg);
410 exit(EXIT_FAILURE);
411 }
412 break;
413 }
414 case 'g': {
415 unsigned long res;
416 struct group *groupinfo = getgrnam(optarg);
417 if (groupinfo) {
418 gid = groupinfo->gr_gid;
419 } else if (qemu_strtoul(optarg, NULL, 10, &res) == 0 &&
420 (gid_t)res == res) {
421 gid = res;
422 } else {
423 error_report("invalid group '%s'", optarg);
424 exit(EXIT_FAILURE);
425 }
426 break;
427 }
428 #else
429 case 'u':
430 case 'g':
431 error_report("-%c not supported by this %s", ch, argv[0]);
432 exit(1);
433 #endif
434 case 'd':
435 daemonize = true;
436 break;
437 case 'v':
438 verbose = true;
439 break;
440 case 'T':
441 trace_opt_parse(optarg);
442 break;
443 case 'V':
444 version(argv[0]);
445 exit(EXIT_SUCCESS);
446 break;
447 case 'h':
448 usage(argv[0]);
449 exit(EXIT_SUCCESS);
450 break;
451 case '?':
452 error_report("Try `%s --help' for more information.", argv[0]);
453 exit(EXIT_FAILURE);
454 }
455 }
456
457 if (!trace_init_backends()) {
458 exit(EXIT_FAILURE);
459 }
460 trace_init_file();
461 qemu_set_log(LOG_TRACE, &error_fatal);
462
463 socket_activation = check_socket_activation();
464 if (socket_activation == 0) {
465 SocketAddress saddr;
466 saddr = (SocketAddress){
467 .type = SOCKET_ADDRESS_TYPE_UNIX,
468 .u.q_unix.path = socket_path,
469 };
470 server_ioc = qio_channel_socket_new();
471 if (qio_channel_socket_listen_sync(server_ioc, &saddr,
472 1, &local_err) < 0) {
473 object_unref(OBJECT(server_ioc));
474 error_report_err(local_err);
475 return 1;
476 }
477 } else {
478 /* Using socket activation - check user didn't use -p etc. */
479 if (socket_path_specified) {
480 error_report("Unix socket can't be set when"
481 "using socket activation");
482 exit(EXIT_FAILURE);
483 }
484
485 /* Can only listen on a single socket. */
486 if (socket_activation > 1) {
487 error_report("%s does not support socket activation"
488 "with LISTEN_FDS > 1",
489 argv[0]);
490 exit(EXIT_FAILURE);
491 }
492 server_ioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD,
493 &local_err);
494 if (server_ioc == NULL) {
495 error_reportf_err(local_err,
496 "Failed to use socket activation: ");
497 exit(EXIT_FAILURE);
498 }
499 }
500
501 qemu_init_main_loop(&error_fatal);
502
503 server_watch = qio_channel_add_watch(QIO_CHANNEL(server_ioc),
504 G_IO_IN,
505 accept_client,
506 NULL, NULL);
507
508 if (daemonize) {
509 if (daemon(0, 0) < 0) {
510 error_report("Failed to daemonize: %s", strerror(errno));
511 exit(EXIT_FAILURE);
512 }
513 }
514
515 if (daemonize || pidfile_specified) {
516 qemu_write_pidfile(pidfile, &error_fatal);
517 }
518
519 #ifdef CONFIG_LIBCAP_NG
520 if (drop_privileges() < 0) {
521 error_report("Failed to drop privileges: %s", strerror(errno));
522 exit(EXIT_FAILURE);
523 }
524 #endif
525
526 info_report("Listening on %s", socket_path);
527
528 state = RUNNING;
529 do {
530 main_loop_wait(false);
531 if (state == TERMINATE) {
532 state = TERMINATING;
533 close_server_socket();
534 }
535 } while (num_active_sockets > 0);
536
537 exit(EXIT_SUCCESS);
538 }