1*87307123Sgson /* $NetBSD: isqemu.h,v 1.6 2021/12/15 09:19:28 gson Exp $ */
2427032d9Schristos
3427032d9Schristos /*-
4427032d9Schristos * Copyright (c) 2013 The NetBSD Foundation, Inc.
5427032d9Schristos * All rights reserved.
6427032d9Schristos *
7427032d9Schristos * This code is derived from software contributed to The NetBSD Foundation
8427032d9Schristos * by Christos Zoulas.
9427032d9Schristos *
10427032d9Schristos * Redistribution and use in source and binary forms, with or without
11427032d9Schristos * modification, are permitted provided that the following conditions
12427032d9Schristos * are met:
13427032d9Schristos * 1. Redistributions of source code must retain the above copyright
14427032d9Schristos * notice, this list of conditions and the following disclaimer.
15427032d9Schristos * 2. Redistributions in binary form must reproduce the above copyright
16427032d9Schristos * notice, this list of conditions and the following disclaimer in the
17427032d9Schristos * documentation and/or other materials provided with the distribution.
18427032d9Schristos * 3. Neither the name of The NetBSD Foundation nor the names of its
19427032d9Schristos * contributors may be used to endorse or promote products derived
20427032d9Schristos * from this software without specific prior written permission.
21427032d9Schristos *
22427032d9Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23427032d9Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24427032d9Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25427032d9Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26427032d9Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27427032d9Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28427032d9Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29427032d9Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30427032d9Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31427032d9Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32427032d9Schristos * POSSIBILITY OF SUCH DAMAGE.
33427032d9Schristos */
34427032d9Schristos #include <sys/param.h>
35427032d9Schristos #include <sys/sysctl.h>
363b030f47Sgson #include <sys/drvctlio.h>
373b030f47Sgson #include <sys/ioctl.h>
383b030f47Sgson #include <fcntl.h>
39427032d9Schristos #include <stdbool.h>
4059b4f8a0Sgson #include <stdlib.h>
41427032d9Schristos #include <string.h>
42427032d9Schristos #include <errno.h>
43427032d9Schristos #include <err.h>
443b030f47Sgson #include <unistd.h>
45427032d9Schristos
46a4fee3e8Smartin static __inline bool
isQEMU(void)47427032d9Schristos isQEMU(void) {
483b030f47Sgson struct devlistargs dla = {
493b030f47Sgson .l_devname = "qemufwcfg0",
503b030f47Sgson .l_childname = NULL,
513b030f47Sgson .l_children = 0,
523b030f47Sgson };
533b030f47Sgson int fd = open(DRVCTLDEV, O_RDONLY, 0);
543b030f47Sgson if (fd == -1)
553b030f47Sgson return false;
563b030f47Sgson if (ioctl(fd, DRVLISTDEV, &dla) == -1) {
573b030f47Sgson close(fd);
583b030f47Sgson return false;
593b030f47Sgson }
603b030f47Sgson close(fd);
613b030f47Sgson return true;
623b030f47Sgson }
633b030f47Sgson
643b030f47Sgson static __inline bool
isQEMU_TCG(void)653b030f47Sgson isQEMU_TCG(void) {
6657ecabebSchristos #if defined(__i386__) || defined(__x86_64__)
67427032d9Schristos char name[1024];
68427032d9Schristos size_t len = sizeof(name);
69427032d9Schristos
70427032d9Schristos if (sysctlbyname("machdep.cpu_brand", name, &len, NULL, 0) == -1) {
71427032d9Schristos if (errno == ENOENT)
72427032d9Schristos return false;
73427032d9Schristos err(EXIT_FAILURE, "sysctl");
74427032d9Schristos }
75*87307123Sgson if (strstr(name, "QEMU") == NULL)
7657ecabebSchristos return false;
77*87307123Sgson if (sysctlbyname("machdep.hypervisor", name, &len, NULL, 0) == -1) {
78*87307123Sgson if (errno == ENOENT)
79*87307123Sgson return true;
80*87307123Sgson err(EXIT_FAILURE, "sysctl");
81*87307123Sgson }
82*87307123Sgson if (strcmp(name, "KVM") == 0)
83*87307123Sgson return false;
84*87307123Sgson return true;
8557ecabebSchristos #endif
86*87307123Sgson return false;
87427032d9Schristos }
88427032d9Schristos
89427032d9Schristos #ifdef TEST
90427032d9Schristos int
main(void)91427032d9Schristos main(void) {
92427032d9Schristos return isQEMU();
93427032d9Schristos }
94427032d9Schristos #endif
95