| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 
 | # -*- mode: python -*-
# qemu_helpers: Helpers to find QEMU and handle its quirks
# Copyright © 2014 Andy Lutomirski
# Licensed under the GPLv2, which is available in the virtme distribution
# as a file called LICENSE with SHA-256 hash:
# 8177f97513213526df2cf6184d8ff986c675afb514d4e68a404010521b880643
import os
from typing import List, Optional
class Arch(object):
    def __init__(self, name) -> None:
        self.virtmename = name
        self.qemuname = name
        self.linuxname = name
        self.gccname = name
    defconfig_target = 'defconfig'
    @staticmethod
    def serial_dev_name(index) -> str:
        return 'ttyS%d' % index
    @staticmethod
    def qemuargs(is_native) -> List[str]:
        return []
    @staticmethod
    def virtio_dev_type(virtiotype) -> str:
        # Return a full name for a virtio device.  It would be
        # nice if QEMU abstracted this away, but it doesn't.
        return 'virtio-%s-pci' % virtiotype
    @staticmethod
    def earlyconsole_args() -> List[str]:
        return []
    @staticmethod
    def serial_console_args() -> List[str]:
        return []
    @staticmethod
    def qemu_nodisplay_args() -> List[str]:
        return ['-vga', 'none', '-display', 'none']
    @staticmethod
    def config_base() -> List[str]:
        return []
    def kimg_path(self) -> str:
        return 'arch/%s/boot/bzImage' % self.linuxname
    @staticmethod
    def dtb_path() -> Optional[str]:
        return None
class Arch_unknown(Arch):
    @staticmethod
    def qemuargs(is_native):
        return Arch.qemuargs(is_native)
class Arch_x86(Arch):
    def __init__(self, name):
        Arch.__init__(self, name)
        self.linuxname = 'x86'
        self.defconfig_target = '%s_defconfig' % name
    @staticmethod
    def qemuargs(is_native):
        ret = Arch.qemuargs(is_native)
        # Add a watchdog.  This is useful for testing.
        ret.extend(['-device', 'i6300esb'])
        if is_native and os.access('/dev/kvm', os.R_OK):
            # If we're likely to use KVM, request a full-featured CPU.
            # (NB: if KVM fails, this will cause problems.  We should probe.)
            ret.extend(['-cpu', 'host'])  # We can't migrate regardless.
        return ret
    @staticmethod
    def earlyconsole_args():
        return ['earlyprintk=serial,ttyS0,115200']
    @staticmethod
    def serial_console_args():
        return ['console=ttyS0']
    @staticmethod
    def config_base():
        return ['CONFIG_SERIO=y',
                'CONFIG_PCI=y',
                'CONFIG_INPUT=y',
                'CONFIG_INPUT_KEYBOARD=y',
                'CONFIG_KEYBOARD_ATKBD=y',
                'CONFIG_SERIAL_8250=y',
                'CONFIG_SERIAL_8250_CONSOLE=y',
                'CONFIG_X86_VERBOSE_BOOTUP=y',
                'CONFIG_VGA_CONSOLE=y',
                'CONFIG_FB=y',
                'CONFIG_FB_VESA=y',
                'CONFIG_FRAMEBUFFER_CONSOLE=y',
                'CONFIG_RTC_CLASS=y',
                'CONFIG_RTC_HCTOSYS=y',
                'CONFIG_RTC_DRV_CMOS=y',
                'CONFIG_HYPERVISOR_GUEST=y',
                'CONFIG_PARAVIRT=y',
                'CONFIG_KVM_GUEST=y',
                # Depending on the host kernel, virtme can nest!
                'CONFIG_KVM=y',
                'CONFIG_KVM_INTEL=y',
                'CONFIG_KVM_AMD=y',
            ]
class Arch_arm(Arch):
    def __init__(self):
        Arch.__init__(self, 'arm')
        self.defconfig_target = 'vexpress_defconfig'
    @staticmethod
    def qemuargs(is_native):
        ret = Arch.qemuargs(is_native)
        # Emulate a vexpress-a15.
        ret.extend(['-M', 'vexpress-a15'])
        # TODO: consider adding a PCI bus (and figuring out how)
        # TODO: This won't boot unless -dtb is set, but we need to figure
        # out how to find the dtb file.
        return ret
    @staticmethod
    def virtio_dev_type(virtiotype):
        return 'virtio-%s-device' % virtiotype
    @staticmethod
    def earlyconsole_args():
        return ['earlyprintk=serial,ttyAMA0,115200']
    @staticmethod
    def serial_console_args():
        return ['console=ttyAMA0']
    def kimg_path(self):
        return 'arch/arm/boot/zImage'
    def dtb_path(self):
        return 'arch/arm/boot/dts/vexpress-v2p-ca15-tc1.dtb'
class Arch_aarch64(Arch):
    def __init__(self):
        Arch.__init__(self, 'aarch64')
        self.qemuname = 'aarch64'
        self.linuxname = 'arm64'
        self.gccname = 'aarch64'
    @staticmethod
    def qemuargs(is_native):
        ret = Arch.qemuargs(is_native)
        if is_native:
            ret.extend(['-M', 'virt,gic-version=host'])
            ret.extend(['-cpu', 'host'])
        else:
            # Emulate a fully virtual system.
            ret.extend(['-M', 'virt'])
            # Despite being called qemu-system-aarch64, QEMU defaults to
            # emulating a 32-bit CPU.  Override it.
            ret.extend(['-cpu', 'cortex-a57'])
        return ret
    @staticmethod
    def virtio_dev_type(virtiotype):
        return 'virtio-%s-device' % virtiotype
    @staticmethod
    def earlyconsole_args():
        return ['earlyprintk=serial,ttyAMA0,115200']
    @staticmethod
    def serial_console_args():
        return ['console=ttyAMA0']
    def kimg_path(self):
        return 'arch/arm64/boot/Image'
class Arch_ppc64(Arch):
    def __init__(self):
        Arch.__init__(self, 'ppc64')
        self.defconfig_target = 'ppc64_defconfig'
        self.qemuname = 'ppc64'
        self.linuxname = 'powerpc'
        self.gccname = 'ppc64'
    def qemuargs(self, is_native):
        ret = Arch.qemuargs(is_native)
        ret.extend(['-M', 'pseries'])
        return ret
    def kimg_path(self):
        # Apparently SLOF (QEMU's bundled firmware?) can't boot a zImage.
        return 'vmlinux'
class Arch_riscv64(Arch):
    def __init__(self):
        Arch.__init__(self, 'riscv64')
        self.defconfig_target = 'defconfig'
        self.qemuname = 'riscv64'
        self.linuxname = 'riscv'
        self.gccname = 'riscv64'
    def qemuargs(self, is_native):
        ret = Arch.qemuargs(is_native)
        ret.extend(['-machine', 'virt'])
        ret.extend(['-bios', 'default'])
        return ret
    @staticmethod
    def serial_console_args():
        return ['console=ttyS0']
    def kimg_path(self):
        return 'arch/riscv/boot/Image'
class Arch_sparc64(Arch):
    def __init__(self):
        Arch.__init__(self, 'sparc64')
        self.defconfig_target = 'sparc64_defconfig'
        self.qemuname = 'sparc64'
        self.linuxname = 'sparc'
        self.gccname = 'sparc64'
    def qemuargs(self, is_native):
        ret = Arch.qemuargs(is_native)
        return ret
    def kimg_path(self):
        return 'arch/sparc/boot/image'
    def qemu_nodisplay_args(self):
        # qemu-system-sparc fails to boot if -display none is set.
        return ['-nographic', '-vga', 'none']
class Arch_s390x(Arch):
    def __init__(self):
        Arch.__init__(self, 's390x')
        self.qemuname = 's390x'
        self.linuxname = 's390'
        self.gccname = 's390x'
    @staticmethod
    def virtio_dev_type(virtiotype):
        return 'virtio-%s-ccw' % virtiotype
    def qemuargs(self, is_native):
        ret = Arch.qemuargs(is_native)
        # Ask for the latest version of s390-ccw
        ret.extend(['-M', 's390-ccw-virtio'])
        # To be able to configure a console, we need to get rid of the
        # default console
        ret.extend(['-nodefaults'])
        ret.extend(['-device', 'sclpconsole,chardev=console'])
        return ret
    @staticmethod
    def config_base():
        return ['CONFIG_MARCH_Z900=y']
ARCHES = {arch.virtmename: arch for arch in [
    Arch_x86('x86_64'),
    Arch_x86('i386'),
    Arch_arm(),
    Arch_aarch64(),
    Arch_ppc64(),
    Arch_riscv64(),
    Arch_sparc64(),
    Arch_s390x(),
]}
def get(arch: str) -> Arch:
    if arch in ARCHES:
        return ARCHES[arch]
    else:
        return Arch_unknown(arch)
 |