:name: X86-KVM Linux
:description: This script runs Linux on a bare x86 platform in KVM.

$name?="x86-kvm"
mach create $name

$bin?=@https://dl.antmicro.com/projects/renode/virt_x86--linux.bin-s_7459328-fec50f291c065d8b13ce4d49abc96ea71af349df
machine LoadPlatformDescription @platforms/cpus/x86-kvm.repl

showAnalyzer uart

macro reset
"""
    # skip the header -- based on akvm copy_kernel()
    sysbus LoadBinary $bin 0x100000 sysbus.cpu 0x4000

    # setup boot params (aka Zero Page) - for more info please refer to https://docs.kernel.org/arch/x86/zero-page.html
    sysbus WriteASCIIString 0x90880 "console=ttyS0 root=/dev/vda earlyprintk loglevel=7 rootwait"
    # pass address of cmdline in `cmd_line_ptr` field
    sysbus WriteDoubleWord 0x90228 0x90880
    # pass (RAM size - 1MiB) / 1024
    sysbus WriteDoubleWord 0x901e0 0x7fc00

    # init flat protected mode
    # for more info please refer to kernel boot protocol
    sysbus.cpu SetDescriptor CS 0x10 0x0 0xffffffff 0xc09b00
    sysbus.cpu SetDescriptor DS 0x18 0x0 0xffffffff 0xc09300
    sysbus.cpu SetDescriptor ES 0x18 0x0 0xffffffff 0xc09300
    sysbus.cpu SetDescriptor SS 0x18 0x0 0xffffffff 0xc09300
    sysbus.cpu SetDescriptor FS 0x18 0x0 0xffffffff 0xc09300
    sysbus.cpu SetDescriptor GS 0x18 0x0 0xffffffff 0xc09300

    sysbus.cpu EIP 0x100000

    # address to kernel param args
    sysbus.cpu ESI 0x90000

    # enable protected mode
    sysbus.cpu CR0 0x60000011

    sysbus.cpu EFLAGS 0x2
"""
runMacro $reset
