Skip to contentSkip to conversation

Detecting qemu from guest on arm/hvf (apple arm)

DISCUSSION John-Mark Gurney 3 participants 4 messages Feb 16, 2023 – Feb 21, 2023

lore
Share & tools
lore
#0 lore raw

Hello,

I was wondering what the best way to detect that FreeBSD is running under qemu/hvf on Apple ARM? FreeBSD doesn't see the ACPI FADT table, so I'm wondering if keying off of something like the QEMU0002 device in ACPI is the best way? Or is there another option?

Thanks for your help.

Signature · 3 lines
  John-Mark Gurney				Voice: +1 415 225 5579

     "All that I will do, has been done, All that I have, has not."
#1 lore raw
Quoted 7 lines · John-Mark Gurney

Hello,

I was wondering what the best way to detect that FreeBSD is running under qemu/hvf on Apple ARM? FreeBSD doesn't see the ACPI FADT table, so I'm wondering if keying off of something like the QEMU0002 device in ACPI is the best way? Or is there another option?

I guess the question is, why do you want to know? Typically the guest OS shouldn't care about whether it's running on bare metal, under a hypervisor, or under emulated QEMU, except to the extent that it wants to use specific features, in which case the question is more "how do I query for the existence of feature X?".

thanks
-- PMM

#2 lore raw

Hello Peter,

btw, please keep me CC'd, as I'm not subscribed to the mailing list.

Quoted 1 line

I guess the question is, why do you want to know? Typically

The main reason is that FreeBSD (and other operating systems) adjust some of the clock periods to be longer for better effeciency. When FreeBSD detects that it's a VM, is slows down hz to 100 instead of 1000:
https://cgit.freebsd.org/src/tree/sys/kern/subr_param.c#n177

And in my case, if I didn't set hz to 100 manually, even an idle machine would consume tons of host CPU, and on top of it (I'm not sure why), it would run at a constant >1000 int/s, compared to running hz at 100 where the int/s would drop to around 50/s...

I'm not sure how good/bad the time counter is on ARM hardware as I'm more familar w/ x86 time counter hardware, and I do know that there's a large variety of quality of time counter hardware and the expense to accessing it.

FreeBSD is using the ARM MPCore Timecounter, and it reports:
kern.timecounter.tc.ARM MPCore Timecounter.quality: 1000 kern.timecounter.tc.ARM MPCore Timecounter.frequency: 24000000 kern.timecounter.tc.ARM MPCore Timecounter.counter: 2182594876 kern.timecounter.tc.ARM MPCore Timecounter.mask: 4294967295

There isn't any other time counter hardware reported (except dummy, but that isn't usable)...

FreeBSD has code to detect hyperv and xen, and detect VMWare through SMBios.

Quoted 5 lines

the guest OS shouldn't care about whether it's running on bare metal, under a hypervisor, or under emulated QEMU, except to the extent that it wants to use specific features, in which case the question is more "how do I query for the existence of feature X?".

Thanks for your help.

Signature · 3 lines
  John-Mark Gurney				Voice: +1 415 225 5579

     "All that I will do, has been done, All that I have, has not."
#3 lore raw
In reply to John-Mark Gurney
Quoted 6 lines · John-Mark Gurney

Hello,

I was wondering what the best way to detect that FreeBSD is running under qemu/hvf on Apple ARM? FreeBSD doesn't see the ACPI FADT table, so I'm wondering if keying off of something like the QEMU0002 device in ACPI is the best way? Or is there another option?

Aside from Peter's suggestion to check for actual features that matter, if you genuinely need to detect a specific platform, the virt-what tool is generally what I point people to. It has loads of checks in it. It can detect QEMU on aarch64, but doesn't distinguish KVM unless SMBIOS is present too. The 'virt' machine type will include SMBIOS, so I expect the KVM check will probably trigger for HVF too.

http://git.annexia.org/?p=virt-what.git;a=blob;f=virt-what.in;h=01e9acaf689416d9bff6eaca1b849dc4e798a0af;hb=HEAD#l340

This is wrong from virt-what POV, but I presume for your needs, you'd be happy to detect *any* hypervisor as you'll want to tweak the clock frequency for both HVF and KVM VMs

If you do come up with any tweaks to it, patches would be welcome.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|