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