| 1 | # ex:ts=8 sw=4: | ||||||||||||||||||||||||||||
| 2 | # $OpenBSD$ | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright (c) 2026 Dick Olsson <hi@dickolsson.com> | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||||||||||||||||||||||||||||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||||||||||||||||||||||||||||
| 8 | # copyright notice and this permission notice appear in all copies. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||||||||||||||||||||||||||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||||||||||||||||||||||||||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||||||||||||||||||||||||||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||||||||||||||||||||||||||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||||||||||||||||||||||||||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||||||||||||||||||||||||||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # App::FuguVM::Arch - the architecture table of the supported guests. | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | # The names are the OpenBSD architecture names, amd64 and arm64, | ||||||||||||||||||||||||||||
| 23 | # because the guest is OpenBSD. The QEMU spellings stay inside the | ||||||||||||||||||||||||||||
| 24 | # table. The module uses core Perl only, and it loads no other module | ||||||||||||||||||||||||||||
| 25 | # of the distribution. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | package App::FuguVM::Arch; | ||||||||||||||||||||||||||||
| 28 | 1 | our $VERSION = '0.3.1'; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | my %TABLE = ( | |||||||||||||||||||||||||||
| 31 | arm64 => { | ||||||||||||||||||||||||||||
| 32 | qemu_binary => 'qemu-system-aarch64', | ||||||||||||||||||||||||||||
| 33 | machine => 'virt,highmem=off', | ||||||||||||||||||||||||||||
| 34 | tcg_cpu => 'cortex-a57', | ||||||||||||||||||||||||||||
| 35 | host_machines => [ 'aarch64', 'arm64' ], | ||||||||||||||||||||||||||||
| 36 | firmware_paths => [ | ||||||||||||||||||||||||||||
| 37 | '/opt/homebrew/share/qemu/edk2-aarch64-code.fd', | ||||||||||||||||||||||||||||
| 38 | '/usr/local/share/qemu/edk2-aarch64-code.fd', | ||||||||||||||||||||||||||||
| 39 | '/usr/share/qemu-efi-aarch64/QEMU_EFI.fd', | ||||||||||||||||||||||||||||
| 40 | '/usr/share/AAVMF/AAVMF_CODE.fd', | ||||||||||||||||||||||||||||
| 41 | '/usr/share/qemu/edk2-aarch64-code.fd', | ||||||||||||||||||||||||||||
| 42 | ], | ||||||||||||||||||||||||||||
| 43 | firmware_glob => | ||||||||||||||||||||||||||||
| 44 | '/opt/homebrew/Cellar/qemu/*/share/qemu/edk2-aarch64-code.fd', | ||||||||||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||||||||||
| 46 | amd64 => { | ||||||||||||||||||||||||||||
| 47 | qemu_binary => 'qemu-system-x86_64', | ||||||||||||||||||||||||||||
| 48 | machine => 'q35', | ||||||||||||||||||||||||||||
| 49 | tcg_cpu => 'qemu64', | ||||||||||||||||||||||||||||
| 50 | host_machines => [ 'x86_64', 'amd64' ], | ||||||||||||||||||||||||||||
| 51 | firmware_paths => [ | ||||||||||||||||||||||||||||
| 52 | '/opt/homebrew/share/qemu/edk2-x86_64-code.fd', | ||||||||||||||||||||||||||||
| 53 | '/usr/local/share/qemu/edk2-x86_64-code.fd', | ||||||||||||||||||||||||||||
| 54 | '/usr/share/qemu/edk2-x86_64-code.fd', | ||||||||||||||||||||||||||||
| 55 | '/usr/share/OVMF/OVMF_CODE_4M.fd', | ||||||||||||||||||||||||||||
| 56 | '/usr/share/OVMF/OVMF_CODE.fd', | ||||||||||||||||||||||||||||
| 57 | '/usr/share/edk2/ovmf/OVMF_CODE.fd', | ||||||||||||||||||||||||||||
| 58 | ], | ||||||||||||||||||||||||||||
| 59 | firmware_glob => | ||||||||||||||||||||||||||||
| 60 | '/opt/homebrew/Cellar/qemu/*/share/qemu/edk2-x86_64-code.fd', | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # The x86 code files are split flash images, and -bios | ||||||||||||||||||||||||||||
| 63 | # cannot load them. Each code file boots through two | ||||||||||||||||||||||||||||
| 64 | # pflash devices, with the variable-store template that | ||||||||||||||||||||||||||||
| 65 | # sits beside it in the same directory. | ||||||||||||||||||||||||||||
| 66 | firmware_vars => { | ||||||||||||||||||||||||||||
| 67 | 'edk2-x86_64-code.fd' => 'edk2-i386-vars.fd', | ||||||||||||||||||||||||||||
| 68 | 'OVMF_CODE_4M.fd' => 'OVMF_VARS_4M.fd', | ||||||||||||||||||||||||||||
| 69 | 'OVMF_CODE.fd' => 'OVMF_VARS.fd', | ||||||||||||||||||||||||||||
| 70 | }, | ||||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # $class->new($name): | ||||||||||||||||||||||||||||
| 75 | # Return the architecture object for amd64 or arm64. Return | ||||||||||||||||||||||||||||
| 76 | # undef for every other name. The comparison is case sensitive: | ||||||||||||||||||||||||||||
| 77 | # AMD64 is an unknown value. | ||||||||||||||||||||||||||||
| 78 | 20 | ▶ | sub new ( $class, $name ) | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||
| 80 | 20 | ▶ | return if !defined $name || !exists $TABLE{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $name and exists $TABLE{$name}
MC/DC100%2/2
defined $name and exists $TABLE{$name}
defined $name
exists $TABLE{$name}
Truth table100%3/3
A and B
A= defined $nameB= exists $TABLE{$name}
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 16 | return bless { name => $name, %{ $TABLE{$name} } }, $class; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # $class->names: | ||||||||||||||||||||||||||||
| 86 | # Return the supported names, sorted. A diagnostic names them, | ||||||||||||||||||||||||||||
| 87 | # so the message and the table cannot disagree. | ||||||||||||||||||||||||||||
| 88 | 1 | ▶ | sub names ($) | ||||||||||||||||||||||||||
|
Subroutine
names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 1 | my @names = sort keys %TABLE; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | return @names; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 2 | ▶ | sub name ($self) | ||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 2 | return $self->{name}; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | ▶ | sub qemu_binary ($self) | ||||||||||||||||||||||||||
|
Subroutine
qemu_binary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||
| 102 | 2 | return $self->{qemu_binary}; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2 | ▶ | sub machine ($self) | ||||||||||||||||||||||||||
|
Subroutine
machine: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 2 | return $self->{machine}; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | sub tcg_cpu ($self) | ||||||||||||||||||||||||||
|
Subroutine
tcg_cpu: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 2 | return $self->{tcg_cpu}; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # $arch->firmware_paths: | ||||||||||||||||||||||||||||
| 116 | # Return the ordered list of absolute firmware paths. | ||||||||||||||||||||||||||||
| 117 | 4 | ▶ | sub firmware_paths ($self) | ||||||||||||||||||||||||||
|
Subroutine
firmware_paths: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||
| 119 | 4 | return @{ $self->{firmware_paths} }; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # $arch->firmware_glob: | ||||||||||||||||||||||||||||
| 123 | # Return one glob pattern for the versioned Homebrew paths. | ||||||||||||||||||||||||||||
| 124 | 2 | ▶ | sub firmware_glob ($self) | ||||||||||||||||||||||||||
|
Subroutine
firmware_glob: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | 2 | return $self->{firmware_glob}; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # $arch->firmware_vars_path($code_path): | ||||||||||||||||||||||||||||
| 130 | # Return the variable-store template beside a firmware code | ||||||||||||||||||||||||||||
| 131 | # file. Return undef when the architecture boots the code file | ||||||||||||||||||||||||||||
| 132 | # with -bios alone. | ||||||||||||||||||||||||||||
| 133 | 13 | ▶ | sub firmware_vars_path ( $self, $code_path ) | ||||||||||||||||||||||||||
|
Subroutine
firmware_vars_path: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 13 | my $vars = $self->{firmware_vars}; | |||||||||||||||||||||||||||
| 136 | 13 | ▶ | return if !defined $vars; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 8 | my $slash = rindex( $code_path, '/' ); | |||||||||||||||||||||||||||
| 139 | 8 | my $name = substr( $code_path, $slash + 1 ); | |||||||||||||||||||||||||||
| 140 | 8 | ▶ | return if !exists $vars->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 8 | return substr( $code_path, 0, $slash + 1 ) . $vars->{$name}; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # $arch->accelerator($os, $host_machine, $kvm): | ||||||||||||||||||||||||||||
| 146 | # Return kvm, hvf or tcg for one host. QEMU cannot accelerate a | ||||||||||||||||||||||||||||
| 147 | # guest whose instruction set is not the host's, so a host | ||||||||||||||||||||||||||||
| 148 | # machine of an other architecture always gives tcg. The caller | ||||||||||||||||||||||||||||
| 149 | # gives $^O, the uname machine, and whether /dev/kvm is | ||||||||||||||||||||||||||||
| 150 | # writable. The method calls no syscall, so a test proves every | ||||||||||||||||||||||||||||
| 151 | # branch on any host. | ||||||||||||||||||||||||||||
| 152 | 18 | ▶ | sub accelerator ( $self, $os, $host_machine, $kvm ) | ||||||||||||||||||||||||||
|
Subroutine
accelerator: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | { | ||||||||||||||||||||||||||||
| 154 | 18 | ▶ | return 'tcg' if !$self->_host_matches($host_machine); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 12 | ▶ | return 'kvm' if $os eq 'linux' && $kvm; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$os eq "linux" and $kvm
MC/DC100%2/2
$os eq "linux" and $kvm
$os eq "linux"
$kvm
Truth table100%3/3
A and B
A= $os eq "linux"B= $kvm
| |||||||||||||||||||||||||||||
| 156 | 8 | ▶ | return 'hvf' if $os eq 'darwin'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 6 | return 'tcg'; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # $arch->_host_matches($host_machine): | ||||||||||||||||||||||||||||
| 162 | # Report if the host machine name belongs to this architecture. | ||||||||||||||||||||||||||||
| 163 | # A host machine has more than one name: Linux says aarch64 | ||||||||||||||||||||||||||||
| 164 | # where Darwin and OpenBSD say arm64. | ||||||||||||||||||||||||||||
| 165 | 18 | ▶ | sub _host_matches ( $self, $host_machine ) | ||||||||||||||||||||||||||
|
Subroutine
_host_matches: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | 18 | ▶ | return 0 if !defined $host_machine; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 18 | ▶ | return ( grep { $_ eq $host_machine } @{ $self->{host_machines} } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | ? 1 | ||||||||||||||||||||||||||||
| 171 | : 0; | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 1 | 1; | |||||||||||||||||||||||||||