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