From patchwork Mon Mar 8 13:50:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 395422 Delivered-To: patch@linaro.org Received: by 2002:a17:906:a383:0:0:0:0 with SMTP id k3csp1834891ejz; Mon, 8 Mar 2021 05:54:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJyormJ/R/fZtwhfwXS0rdY0OxX3j7fYD8KxoEAs09TrnyVfUbGDFGnTxsPxa1BV2l+o2IV4 X-Received: by 2002:a02:cbb2:: with SMTP id v18mr23397039jap.4.1615211668403; Mon, 08 Mar 2021 05:54:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615211668; cv=none; d=google.com; s=arc-20160816; b=mg+Wjg5vuDJ1LIf3qCyTnR+ZbDdNxnj5h+w7MvKfAqmvCGG1GyNrXLF0CbZo3uxk56 Qhf2NZ27BTI4xalrgCg2F8pRkc281MpdUDEep77TJlbzHrd3lx+7/eeMVqtttev2hq14 DmQxy9Lh7kWE4fQb722mvWirScNyGOc1k9+AoAmCv2nNVkU2U3E/Zeh7rNdD6N3nHTcS E9YcNBzqrMIgMaTc3EiD6aiDyUQB0HdIzXregAV4bQd4bWBENJ9G7x2YQa1jw4aflQ/b fytQAz9SJkHcigNKXr7yFpoughQt/Yf1gtqqZ+Surx0+/tZV2gF3jc+sQWS8l+E2uzl2 gS9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=CR8IvXeaMjbmmjcnKO1Mvlz93ruIxw19ZKqjVnLky5o=; b=jhUmkJcg5Rp2KY6aooJj6fNf3z+vZNMOvVYzmusdJ0oZPyL8aqQRNDvmgkE5mJu5j6 DcKnSdSDqGthHf5MMoxb4efn5xJghe+tjaSXrMEhFIyYknRAJ4k1y8nsDgoJxsZYyJoe 2yl/Oy4CAkHtwGSjE3bGbLwV5fISqHBMfgYCVQaxOKJZa2DVd+0rBGA8pRRpxsq7jMp9 zRJsmfWEcEcRmaJydl+zrH9X5WURYWicQSKIQaqvopHJAKEC/I6lJQyzty83i5JYyukI o7vpKPSwnbJ21b/K+CdCUrwidECqv12wNQ2k6OWXATp2dsCmstfDcEyGSK66D3xGk4Jf xziw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=jK2neIyW; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id j7si10581559iow.89.2021.03.08.05.54.28 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 08 Mar 2021 05:54:28 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=jK2neIyW; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:45902 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lJGL5-0001U2-L9 for patch@linaro.org; Mon, 08 Mar 2021 08:54:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:53678) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lJGIF-0006gd-A5 for qemu-devel@nongnu.org; Mon, 08 Mar 2021 08:51:31 -0500 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]:34412) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lJGI8-0001HL-V4 for qemu-devel@nongnu.org; Mon, 08 Mar 2021 08:51:30 -0500 Received: by mail-wm1-x330.google.com with SMTP id n11-20020a05600c4f8bb029010e5cf86347so2804834wmq.1 for ; Mon, 08 Mar 2021 05:51:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CR8IvXeaMjbmmjcnKO1Mvlz93ruIxw19ZKqjVnLky5o=; b=jK2neIyWgFy2LPGx2vjqtlVSAARf+4xUWbg9jw+ow97HgO925lNWZQ0T9slt1XxdXz FUooTOcQaZLrWhXLmQ9vNI2auH6o9lkmwzRTSEwBECZVUcXtvhWy4GmN/YfQ3sRMqa4X OsOY1QPunc6/TfDMwM9L89KGpX5KjqGMOyfUlWtTzCXZ8w2aD2Kb9Zap/LXEjx0c89pb At0/2ax/8Zce+La7DlBoNP1Mj1e2naM3lZ5lMJQv7L6O6+GJwsv7dLeOylnIbSvRHP/w +raHedh2gl1vf3t1M/+cEhQ4r4AQnOP1xzAogymmwkMqzuI+vJB+FTcbGD1SZofEgFtj cpXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CR8IvXeaMjbmmjcnKO1Mvlz93ruIxw19ZKqjVnLky5o=; b=O5CXMun3CaSbD18To/Cl9wEMwGRGVXGymlJpY5/XgmIqKA8XYLl09VBBBiEYFZfBF1 FsQUnmEFg3ZZvHtqVfm6/gD5PAcqdTSbC6JLlmKTbUy6jkTXwBVw2vhCXwSFxk2BVSyv vB3lJzZf+GT1KefSRPlCZ/J2E1imItK3K6n3vya6VE9qTiiCfdQW/g3inDIGJkY3ycFl jgPqN98fVlViC6+iHkNGSQcI31QBRV4f0fqw6PriJIHSW1XIEH3bCcgfcFKh6ODb4jDF UOhWxRekqSCI+UY1H5wlp9t7zoU8KEMp0aE2HugFNCVHP7TlLsL24T2Z9/++9EQFqicJ TAng== X-Gm-Message-State: AOAM530Ud4zg0nNR3YnnaCuBVYYscaLO0n/Iimj67trLQr3NuD8ke8v7 ngTGclBuv4k67TiNgdMMjdYFFA== X-Received: by 2002:a7b:ce19:: with SMTP id m25mr22489739wmc.74.1615211483426; Mon, 08 Mar 2021 05:51:23 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id x6sm19597897wmj.32.2021.03.08.05.51.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Mar 2021 05:51:17 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 148AE1FF9C; Mon, 8 Mar 2021 13:51:06 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Subject: [PULL 13/18] hw/core: implement a guest-loader to support static hypervisor guests Date: Mon, 8 Mar 2021 13:50:59 +0000 Message-Id: <20210308135104.24903-14-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210308135104.24903-1-alex.bennee@linaro.org> References: <20210308135104.24903-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::330; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x330.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alistair Francis , =?utf-8?q?Alex_Benn=C3=A9?= =?utf-8?q?e?= , qemu-devel@nongnu.org, =?utf-8?q?Philippe_Ma?= =?utf-8?q?thieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Hypervisors, especially type-1 ones, need the firmware/bootcode to put their initial guest somewhere in memory and pass the information to it via platform data. The guest-loader is modelled after the generic loader for exactly this sort of purpose: $QEMU $ARGS -kernel ~/xen.git/xen/xen \ -append "dom0_mem=1G,max:1G loglvl=all guest_loglvl=all" \ -device guest-loader,addr=0x42000000,kernel=Image,bootargs="root=/dev/sda2 ro console=hvc0 earlyprintk=xen" \ -device guest-loader,addr=0x47000000,initrd=rootfs.cpio Signed-off-by: Alex Bennée Reviewed-by: Alistair Francis Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20210303173642.3805-5-alex.bennee@linaro.org> -- 2.20.1 Tested-by: Christian Borntraeger Tested-by: Christian Borntraeger diff --git a/hw/core/guest-loader.h b/hw/core/guest-loader.h new file mode 100644 index 0000000000..07f4b4884b --- /dev/null +++ b/hw/core/guest-loader.h @@ -0,0 +1,34 @@ +/* + * Guest Loader + * + * Copyright (C) 2020 Linaro + * Written by Alex Bennée + * (based on the generic-loader by Li Guang ) + * + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#ifndef GUEST_LOADER_H +#define GUEST_LOADER_H + +#include "hw/qdev-core.h" +#include "qom/object.h" + +struct GuestLoaderState { + /* */ + DeviceState parent_obj; + + /* */ + uint64_t addr; + char *kernel; + char *args; + char *initrd; +}; + +#define TYPE_GUEST_LOADER "guest-loader" +OBJECT_DECLARE_SIMPLE_TYPE(GuestLoaderState, GUEST_LOADER) + +#endif diff --git a/hw/core/guest-loader.c b/hw/core/guest-loader.c new file mode 100644 index 0000000000..bde44e27b4 --- /dev/null +++ b/hw/core/guest-loader.c @@ -0,0 +1,145 @@ +/* + * Guest Loader + * + * Copyright (C) 2020 Linaro + * Written by Alex Bennée + * (based on the generic-loader by Li Guang ) + * + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +/* + * Much like the generic-loader this is treated as a special device + * inside QEMU. However unlike the generic-loader this device is used + * to load guest images for hypervisors. As part of that process the + * hypervisor needs to have platform information passed to it by the + * lower levels of the stack (e.g. firmware/bootloader). If you boot + * the hypervisor directly you use the guest-loader to load the Dom0 + * or equivalent guest images in the right place in the same way a + * boot loader would. + * + * This is only relevant for full system emulation. + */ + +#include "qemu/osdep.h" +#include "hw/core/cpu.h" +#include "hw/sysbus.h" +#include "sysemu/dma.h" +#include "hw/loader.h" +#include "hw/qdev-properties.h" +#include "qapi/error.h" +#include "qemu/module.h" +#include "guest-loader.h" +#include "sysemu/device_tree.h" +#include "hw/boards.h" + +/* + * Insert some FDT nodes for the loaded blob. + */ +static void loader_insert_platform_data(GuestLoaderState *s, int size, + Error **errp) +{ + MachineState *machine = MACHINE(qdev_get_machine()); + void *fdt = machine->fdt; + g_autofree char *node = g_strdup_printf("/chosen/module@0x%08" PRIx64, + s->addr); + uint64_t reg_attr[2] = {cpu_to_be64(s->addr), cpu_to_be64(size)}; + + if (!fdt) { + error_setg(errp, "Cannot modify FDT fields if the machine has none"); + return; + } + + qemu_fdt_add_subnode(fdt, node); + qemu_fdt_setprop(fdt, node, "reg", ®_attr, sizeof(reg_attr)); + + if (s->kernel) { + const char *compat[2] = { "multiboot,module", "multiboot,kernel" }; + if (qemu_fdt_setprop_string_array(fdt, node, "compatible", + (char **) &compat, + ARRAY_SIZE(compat)) < 0) { + error_setg(errp, "couldn't set %s/compatible", node); + return; + } + if (s->args) { + if (qemu_fdt_setprop_string(fdt, node, "bootargs", s->args) < 0) { + error_setg(errp, "couldn't set %s/bootargs", node); + } + } + } else if (s->initrd) { + const char *compat[2] = { "multiboot,module", "multiboot,ramdisk" }; + if (qemu_fdt_setprop_string_array(fdt, node, "compatible", + (char **) &compat, + ARRAY_SIZE(compat)) < 0) { + error_setg(errp, "couldn't set %s/compatible", node); + return; + } + } +} + +static void guest_loader_realize(DeviceState *dev, Error **errp) +{ + GuestLoaderState *s = GUEST_LOADER(dev); + char *file = s->kernel ? s->kernel : s->initrd; + int size = 0; + + /* Perform some error checking on the user's options */ + if (s->kernel && s->initrd) { + error_setg(errp, "Cannot specify a kernel and initrd in same stanza"); + return; + } else if (!s->kernel && !s->initrd) { + error_setg(errp, "Need to specify a kernel or initrd image"); + return; + } else if (!s->addr) { + error_setg(errp, "Need to specify the address of guest blob"); + return; + } else if (s->args && !s->kernel) { + error_setg(errp, "Boot args only relevant to kernel blobs"); + } + + /* Default to the maximum size being the machine's ram size */ + size = load_image_targphys_as(file, s->addr, current_machine->ram_size, + NULL); + if (size < 0) { + error_setg(errp, "Cannot load specified image %s", file); + return; + } + + /* Now the image is loaded we need to update the platform data */ + loader_insert_platform_data(s, size, errp); +} + +static Property guest_loader_props[] = { + DEFINE_PROP_UINT64("addr", GuestLoaderState, addr, 0), + DEFINE_PROP_STRING("kernel", GuestLoaderState, kernel), + DEFINE_PROP_STRING("bootargs", GuestLoaderState, args), + DEFINE_PROP_STRING("initrd", GuestLoaderState, initrd), + DEFINE_PROP_END_OF_LIST(), +}; + +static void guest_loader_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + + dc->realize = guest_loader_realize; + device_class_set_props(dc, guest_loader_props); + dc->desc = "Guest Loader"; + set_bit(DEVICE_CATEGORY_MISC, dc->categories); +} + +static TypeInfo guest_loader_info = { + .name = TYPE_GUEST_LOADER, + .parent = TYPE_DEVICE, + .instance_size = sizeof(GuestLoaderState), + .class_init = guest_loader_class_init, +}; + +static void guest_loader_register_type(void) +{ + type_register_static(&guest_loader_info); +} + +type_init(guest_loader_register_type) diff --git a/MAINTAINERS b/MAINTAINERS index ab3118429b..0134cad491 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2018,6 +2018,11 @@ F: hw/core/generic-loader.c F: include/hw/core/generic-loader.h F: docs/generic-loader.txt +Guest Loader +M: Alex Bennée +S: Maintained +F: hw/core/guest-loader.c + Intel Hexadecimal Object File Loader M: Su Hang S: Maintained diff --git a/hw/core/meson.build b/hw/core/meson.build index 032576f571..9cd72edf51 100644 --- a/hw/core/meson.build +++ b/hw/core/meson.build @@ -37,6 +37,8 @@ softmmu_ss.add(files( 'clock-vmstate.c', )) +softmmu_ss.add(when: 'CONFIG_TCG', if_true: files('guest-loader.c')) + specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: files( 'machine-qmp-cmds.c', 'numa.c',