From patchwork Thu Jul 21 12:29:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12925046 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 36FCFC43334 for ; Thu, 21 Jul 2022 12:37:01 +0000 (UTC) Received: from localhost ([::1]:42818 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEVQK-0001Lf-2p for qemu-devel@archiver.kernel.org; Thu, 21 Jul 2022 08:37:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35802) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEVJL-0004Jm-Oh for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:29:48 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:54636) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEVJJ-0004pF-Oe for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:29:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658406583; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ewsIbER1SRBWS1JrbrcFLWG+2/3TGq+isiGbthcclPE=; b=Hen0wk3Hr8GWBGi1ZMjAg5UcpvnZGnpHJvZZPA/FsWu4sMlkFi+QhZFPHejU8M0sPwQc2c x4PpDnGIUgaBvaBfwachMjTL+DLi9WJYZtZYMO6ZTNsHs8hBc8Mi2wXDYpJYL4AdduoZuN Ju37Foweapj1GNypdwTPagrulWmH9UI= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-467-2WFpAiUHMgiyE9HFIIbW-g-1; Thu, 21 Jul 2022 08:29:42 -0400 X-MC-Unique: 2WFpAiUHMgiyE9HFIIbW-g-1 Received: by mail-ed1-f70.google.com with SMTP id o13-20020a056402438d00b0043aa846b2d2so1027978edc.8 for ; Thu, 21 Jul 2022 05:29:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ewsIbER1SRBWS1JrbrcFLWG+2/3TGq+isiGbthcclPE=; b=nyyxMo7UwzdMnt1KSK5ikax2Jmj6gSNRLVRHdeZy9oKBKAuk/lrG3+j4ya60d/wOmj ZmxH08QjZihIRYYJG57kexLC/dfwBchmuaXneLSc0t2AoqNUQYTDyTJU6JFsLTxmZqVY NW1fSwXtf+ZCzdT2nX6arQ000FZbPj9vk20g+444ot1wiF0ijG+ypg+VjjVfZvTSeI06 Q7odEDEI6NFUbIBSmJMYJEK2qGPTdVvaXfaq04K38mS55f29uy7rwJ6NQcESQXNbqUkB okwrx7JRgpV1AzXYNDhUdnczH3ccDR1dFZJHbojaxUILLxoAvWLsRB6+12xAVDGIUbpR 3Qrw== X-Gm-Message-State: AJIora8wRe+j3MOjWw9K07hqmzfF+WAdw88XzYMAcHUkuXRQ1vX3hGAq EwYm0TnThlbjQROBxSPYcmUjH6KvAZ3OSkWMKJl/o7iGs3Kv0CnXF1irHc34M5knBMR97XwItFO p6l2yKkIEpeHGlPI9ZrnEoAVSbIq/ZPCTPDMEmiBGwIRH43DPYcKEUqMQN2alFSDunn8= X-Received: by 2002:a05:6402:298a:b0:43a:76f8:a75c with SMTP id eq10-20020a056402298a00b0043a76f8a75cmr57480478edb.216.1658406581117; Thu, 21 Jul 2022 05:29:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1soFEMbCX6i0FOnn8pjPd/ilMPIAt4W46t9+tl1AEDfquYVYy06fqhtcU3mYGxGyOsAxzuIEQ== X-Received: by 2002:a05:6402:298a:b0:43a:76f8:a75c with SMTP id eq10-20020a056402298a00b0043a76f8a75cmr57480443edb.216.1658406580815; Thu, 21 Jul 2022 05:29:40 -0700 (PDT) Received: from goa-sendmail ([93.56.169.184]) by smtp.gmail.com with ESMTPSA id l10-20020a170906938a00b006f3ef214daesm829380ejx.20.2022.07.21.05.29.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jul 2022 05:29:40 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: "Jason A . Donenfeld" , "Michael S . Tsirkin" , Eduardo Habkost , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Laurent Vivier Subject: [PATCH 1/4] hw/i386: extract PVH load to a separate function Date: Thu, 21 Jul 2022 14:29:34 +0200 Message-Id: <20220721122937.729959-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220721122937.729959-1-pbonzini@redhat.com> References: <20220721122937.729959-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Only keep the code x86_load_linux the code that is relevant to the Linux boot protocol. Signed-off-by: Paolo Bonzini --- hw/i386/x86.c | 131 ++++++++++++++++++++++++++------------------------ 1 file changed, 69 insertions(+), 62 deletions(-) diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 6003b4b2df..449edb076e 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -763,6 +763,62 @@ static bool load_elfboot(const char *kernel_filename, return true; } +static bool load_pvh(X86MachineState *x86ms, FWCfgState *fw_cfg, const char *kernel_filename, + const char *initrd_filename, uint32_t initrd_max, const char *kernel_cmdline, + size_t kernel_size, uint8_t *header) +{ + if (!load_elfboot(kernel_filename, kernel_size, + header, pvh_start_addr, fw_cfg)) { + return false; + } + + fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, + strlen(kernel_cmdline) + 1); + fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline); + + fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header)); + fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, + header, sizeof(header)); + + /* load initrd */ + if (initrd_filename) { + GMappedFile *mapped_file; + gsize initrd_size; + gchar *initrd_data; + GError *gerr = NULL; + + mapped_file = g_mapped_file_new(initrd_filename, false, &gerr); + if (!mapped_file) { + fprintf(stderr, "qemu: error reading initrd %s: %s\n", + initrd_filename, gerr->message); + exit(1); + } + x86ms->initrd_mapped_file = mapped_file; + + initrd_data = g_mapped_file_get_contents(mapped_file); + initrd_size = g_mapped_file_get_length(mapped_file); + if (initrd_size >= initrd_max) { + fprintf(stderr, "qemu: initrd is too large, cannot support." + "(max: %"PRIu32", need %"PRId64")\n", + initrd_max, (uint64_t)initrd_size); + exit(1); + } + + hwaddr initrd_addr = (initrd_max - initrd_size) & ~4095; + + fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); + fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); + fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, + initrd_size); + } + + option_rom[nb_option_roms].bootindex = 0; + option_rom[nb_option_roms].name = "pvh.bin"; + nb_option_roms++; + + return true; +} + void x86_load_linux(X86MachineState *x86ms, FWCfgState *fw_cfg, int acpi_data_size, @@ -774,7 +830,7 @@ void x86_load_linux(X86MachineState *x86ms, int dtb_size, setup_data_offset; uint32_t initrd_max; uint8_t header[8192], *setup, *kernel; - hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0; + hwaddr real_addr, prot_addr, cmdline_addr; FILE *f; char *vmode; MachineState *machine = MACHINE(x86ms); @@ -785,6 +841,8 @@ void x86_load_linux(X86MachineState *x86ms, const char *kernel_cmdline = machine->kernel_cmdline; SevKernelLoaderContext sev_load_ctx = {}; + initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1; + /* Align to 16 bytes as a paranoia measure */ cmdline_size = (strlen(kernel_cmdline) + 16) & ~15; @@ -806,9 +864,7 @@ void x86_load_linux(X86MachineState *x86ms, } /* kernel protocol version */ - if (ldl_p(header + 0x202) == 0x53726448) { - protocol = lduw_p(header + 0x206); - } else { + if (ldl_p(header + 0x202) != 0x53726448) { /* * This could be a multiboot kernel. If it is, let's stop treating it * like a Linux kernel. @@ -826,58 +882,14 @@ void x86_load_linux(X86MachineState *x86ms, * If load_elfboot() is successful, populate the fw_cfg info. */ if (pvh_enabled && - load_elfboot(kernel_filename, kernel_size, - header, pvh_start_addr, fw_cfg)) { + load_pvh(x86ms, fw_cfg, kernel_filename, initrd_filename, + initrd_max, kernel_cmdline, kernel_size, header)) { fclose(f); - - fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, - strlen(kernel_cmdline) + 1); - fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline); - - fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header)); - fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, - header, sizeof(header)); - - /* load initrd */ - if (initrd_filename) { - GMappedFile *mapped_file; - gsize initrd_size; - gchar *initrd_data; - GError *gerr = NULL; - - mapped_file = g_mapped_file_new(initrd_filename, false, &gerr); - if (!mapped_file) { - fprintf(stderr, "qemu: error reading initrd %s: %s\n", - initrd_filename, gerr->message); - exit(1); - } - x86ms->initrd_mapped_file = mapped_file; - - initrd_data = g_mapped_file_get_contents(mapped_file); - initrd_size = g_mapped_file_get_length(mapped_file); - initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1; - if (initrd_size >= initrd_max) { - fprintf(stderr, "qemu: initrd is too large, cannot support." - "(max: %"PRIu32", need %"PRId64")\n", - initrd_max, (uint64_t)initrd_size); - exit(1); - } - - initrd_addr = (initrd_max - initrd_size) & ~4095; - - fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); - fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); - fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, - initrd_size); - } - - option_rom[nb_option_roms].bootindex = 0; - option_rom[nb_option_roms].name = "pvh.bin"; - nb_option_roms++; - return; - } + } protocol = 0; + } else { + protocol = lduw_p(header + 0x206); } if (protocol < 0x200 || !(header[0x211] & 0x01)) { @@ -914,17 +926,12 @@ void x86_load_linux(X86MachineState *x86ms, * support the 64-bit boot protocol (specifically the ext_ramdisk_image * field). * - * Therefore here just limit initrd_max to UINT32_MAX simply as well. + * Therefore here just limit initrd_max to the available memory below 4G. */ - initrd_max = UINT32_MAX; } else if (protocol >= 0x203) { - initrd_max = ldl_p(header + 0x22c); + initrd_max = MIN(initrd_max, ldl_p(header + 0x22c)); } else { - initrd_max = 0x37ffffff; - } - - if (initrd_max >= x86ms->below_4g_mem_size - acpi_data_size) { - initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1; + initrd_max = MIN(initrd_max, 0x37ffffff); } fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr); @@ -1008,7 +1015,7 @@ void x86_load_linux(X86MachineState *x86ms, exit(1); } - initrd_addr = (initrd_max - initrd_size) & ~4095; + hwaddr initrd_addr = (initrd_max - initrd_size) & ~4095; fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); From patchwork Thu Jul 21 12:29:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12925049 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D23E8C433EF for ; Thu, 21 Jul 2022 12:42:06 +0000 (UTC) Received: from localhost ([::1]:51324 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEVVF-0007Sv-Ks for qemu-devel@archiver.kernel.org; Thu, 21 Jul 2022 08:42:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35830) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEVJN-0004Mb-FP for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:29:49 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:56065) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEVJK-0004pd-7h for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:29:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658406585; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nug0Z3uObNETTTT5YBKRKiT3PKhNyxP+xiqNdaE6mko=; b=MMnJ8NaefWECB6MCDubTnFuZy3l87ueOMmIyfsGoAAaSV1s5nxMe+YIj+PN/VsOf4RqcsS gY+Yp8y07hhMSJkD+hpnQhB3f+r1xbGG4rtjSsLiPi2i3Oa1jU9weG7MEkc78gkeG4SHnA 0x8g7+JQ5yIhjaAI9IEI+ChGRKUFXnc= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-53-Y9y2kbzJPfiakWLozGO6QA-1; Thu, 21 Jul 2022 08:29:44 -0400 X-MC-Unique: Y9y2kbzJPfiakWLozGO6QA-1 Received: by mail-ed1-f71.google.com with SMTP id x21-20020a05640226d500b0043abb7ac086so1019034edd.14 for ; Thu, 21 Jul 2022 05:29:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nug0Z3uObNETTTT5YBKRKiT3PKhNyxP+xiqNdaE6mko=; b=fCf5IWsSRALPRXgHmYwegEuqwnTUuZZi2uE64/rgXR5jbx0Fo794PJah6+I6ShXWMP ioKCWMa16yRLJDyL/LopzrSEJqGag3lDfyvlNXT399BVh5oc/ZYY+Fpj6R+Cx0vuyXV0 ApoJgU+sIB1KkJ8HXajTiMZ9wP/qaHxhrTb7cxyJSsWjMm6eK2JgKjcJv0Hh62Dn3nmX evaTdTdNCBdirrKpJKwZ/GiqFFIgG4WfSaUa9O2BHLj5Xn3pmL+qq0uBMYozcvm++AdT fNTdHbrzN5t9li9Im821CW9r+XYi1KXXtZpzF6qDbE+5sLls/8tVipZUQz4zf6iNRCnu Wfrw== X-Gm-Message-State: AJIora/u7h+MhIFps3UzBKIfHbwFNHoNbm305bMzFN3oxIAps7aFXr9d ycQm6ZAD6mQ7hvCRwS6m0jkGu7IvA8+wq8VgHYbrcxRw+yIzQLa8bRnWFkAwaq131AS+CA+kOX3 HzftbNwzZj+2u0Ax8JMdZwsyUcoVAOgwgkB+Ja1zPR900MzsXU5CNDO7oayhRABX2Ulk= X-Received: by 2002:a17:907:7394:b0:72b:44ff:5cec with SMTP id er20-20020a170907739400b0072b44ff5cecmr42185982ejc.670.1658406582649; Thu, 21 Jul 2022 05:29:42 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u7Sw7+S+1g33emtvfwu3W9S3QjvZAW5yq02oFVXSBZsrEdU/uJkpepb4z2yzwJk8JrETrtjA== X-Received: by 2002:a17:907:7394:b0:72b:44ff:5cec with SMTP id er20-20020a170907739400b0072b44ff5cecmr42185942ejc.670.1658406582301; Thu, 21 Jul 2022 05:29:42 -0700 (PDT) Received: from goa-sendmail ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id w4-20020a50fa84000000b0043586bee560sm919681edr.68.2022.07.21.05.29.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jul 2022 05:29:41 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: "Jason A . Donenfeld" , "Michael S . Tsirkin" , Eduardo Habkost , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Laurent Vivier Subject: [PATCH 2/4] hw/i386: define a struct for Linux boot protocol data Date: Thu, 21 Jul 2022 14:29:35 +0200 Message-Id: <20220721122937.729959-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220721122937.729959-1-pbonzini@redhat.com> References: <20220721122937.729959-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" In preparation for moving parts of x86_load_linux to separate function, define a struct with the data that needs to be passed back and forth. Signed-off-by: Paolo Bonzini --- hw/i386/x86.c | 116 +++++++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 54 deletions(-) diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 449edb076e..253a6ff536 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -819,22 +819,30 @@ static bool load_pvh(X86MachineState *x86ms, FWCfgState *fw_cfg, const char *ker return true; } +typedef struct LinuxBootData { + uint16_t protocol; + uint8_t header[8192]; + hwaddr prot_addr; + size_t kernel_size; + uint8_t *kernel; + size_t setup_data_offset; +} LinuxBootData; + void x86_load_linux(X86MachineState *x86ms, FWCfgState *fw_cfg, int acpi_data_size, bool pvh_enabled) { bool linuxboot_dma_enabled = X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma_enabled; - uint16_t protocol; - int setup_size, kernel_size, cmdline_size; - int dtb_size, setup_data_offset; + int setup_size, cmdline_size; + int dtb_size; uint32_t initrd_max; - uint8_t header[8192], *setup, *kernel; - hwaddr real_addr, prot_addr, cmdline_addr; + uint8_t *setup; + hwaddr real_addr, cmdline_addr; FILE *f; char *vmode; MachineState *machine = MACHINE(x86ms); - struct setup_data *setup_data; + LinuxBootData data = { 0 }; const char *kernel_filename = machine->kernel_filename; const char *initrd_filename = machine->initrd_filename; const char *dtb_filename = machine->dtb; @@ -854,17 +862,17 @@ void x86_load_linux(X86MachineState *x86ms, exit(1); } - kernel_size = get_file_size(f); - if (!kernel_size || - fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) != - MIN(ARRAY_SIZE(header), kernel_size)) { + data.kernel_size = get_file_size(f); + if (!data.kernel_size || + fread(data.header, 1, MIN(ARRAY_SIZE(data.header), data.kernel_size), f) != + MIN(ARRAY_SIZE(data.header), data.kernel_size)) { fprintf(stderr, "qemu: could not load kernel '%s': %s\n", kernel_filename, strerror(errno)); exit(1); } /* kernel protocol version */ - if (ldl_p(header + 0x202) != 0x53726448) { + if (ldl_p(data.header + 0x202) != 0x53726448) { /* * This could be a multiboot kernel. If it is, let's stop treating it * like a Linux kernel. @@ -873,7 +881,7 @@ void x86_load_linux(X86MachineState *x86ms, * header before to load it. */ if (load_multiboot(x86ms, fw_cfg, f, kernel_filename, initrd_filename, - kernel_cmdline, kernel_size, header)) { + kernel_cmdline, data.kernel_size, data.header)) { return; } /* @@ -883,35 +891,35 @@ void x86_load_linux(X86MachineState *x86ms, */ if (pvh_enabled && load_pvh(x86ms, fw_cfg, kernel_filename, initrd_filename, - initrd_max, kernel_cmdline, kernel_size, header)) { + initrd_max, kernel_cmdline, data.kernel_size, data.header)) { fclose(f); return; } - protocol = 0; + data.protocol = 0; } else { - protocol = lduw_p(header + 0x206); + data.protocol = lduw_p(data.header + 0x206); } - if (protocol < 0x200 || !(header[0x211] & 0x01)) { + if (data.protocol < 0x200 || !(data.header[0x211] & 0x01)) { /* Low kernel */ real_addr = 0x90000; cmdline_addr = 0x9a000 - cmdline_size; - prot_addr = 0x10000; - } else if (protocol < 0x202) { + data.prot_addr = 0x10000; + } else if (data.protocol < 0x202) { /* High but ancient kernel */ real_addr = 0x90000; cmdline_addr = 0x9a000 - cmdline_size; - prot_addr = 0x100000; + data.prot_addr = 0x100000; } else { /* High and recent kernel */ real_addr = 0x10000; cmdline_addr = 0x20000; - prot_addr = 0x100000; + data.prot_addr = 0x100000; } /* highest address for loading the initrd */ - if (protocol >= 0x20c && - lduw_p(header + 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) { + if (data.protocol >= 0x20c && + lduw_p(data.header + 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) { /* * Linux has supported initrd up to 4 GB for a very long time (2007, * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013), @@ -928,8 +936,8 @@ void x86_load_linux(X86MachineState *x86ms, * * Therefore here just limit initrd_max to the available memory below 4G. */ - } else if (protocol >= 0x203) { - initrd_max = MIN(initrd_max, ldl_p(header + 0x22c)); + } else if (data.protocol >= 0x203) { + initrd_max = MIN(initrd_max, ldl_p(data.header + 0x22c)); } else { initrd_max = MIN(initrd_max, 0x37ffffff); } @@ -940,11 +948,11 @@ void x86_load_linux(X86MachineState *x86ms, sev_load_ctx.cmdline_data = (char *)kernel_cmdline; sev_load_ctx.cmdline_size = strlen(kernel_cmdline) + 1; - if (protocol >= 0x202) { - stl_p(header + 0x228, cmdline_addr); + if (data.protocol >= 0x202) { + stl_p(data.header + 0x228, cmdline_addr); } else { - stw_p(header + 0x20, 0xA33F); - stw_p(header + 0x22, cmdline_addr - real_addr); + stw_p(data.header + 0x20, 0xA33F); + stw_p(data.header + 0x22, cmdline_addr - real_addr); } /* handle vga= parameter */ @@ -968,7 +976,7 @@ void x86_load_linux(X86MachineState *x86ms, exit(1); } } - stw_p(header + 0x1fa, video_mode); + stw_p(data.header + 0x1fa, video_mode); } /* loader type */ @@ -977,13 +985,13 @@ void x86_load_linux(X86MachineState *x86ms, * If this code is substantially changed, you may want to consider * incrementing the revision. */ - if (protocol >= 0x200) { - header[0x210] = 0xB0; + if (data.protocol >= 0x200) { + data.header[0x210] = 0xB0; } /* heap */ - if (protocol >= 0x201) { - header[0x211] |= 0x80; /* CAN_USE_HEAP */ - stw_p(header + 0x224, cmdline_addr - real_addr - 0x200); + if (data.protocol >= 0x201) { + data.header[0x211] |= 0x80; /* CAN_USE_HEAP */ + stw_p(data.header + 0x224, cmdline_addr - real_addr - 0x200); } /* load initrd */ @@ -993,7 +1001,7 @@ void x86_load_linux(X86MachineState *x86ms, gchar *initrd_data; GError *gerr = NULL; - if (protocol < 0x200) { + if (data.protocol < 0x200) { fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n"); exit(1); } @@ -1023,30 +1031,30 @@ void x86_load_linux(X86MachineState *x86ms, sev_load_ctx.initrd_data = initrd_data; sev_load_ctx.initrd_size = initrd_size; - stl_p(header + 0x218, initrd_addr); - stl_p(header + 0x21c, initrd_size); + stl_p(data.header + 0x218, initrd_addr); + stl_p(data.header + 0x21c, initrd_size); } /* load kernel and setup */ - setup_size = header[0x1f1]; + setup_size = data.header[0x1f1]; if (setup_size == 0) { setup_size = 4; } setup_size = (setup_size + 1) * 512; - if (setup_size > kernel_size) { + if (setup_size > data.kernel_size) { fprintf(stderr, "qemu: invalid kernel header\n"); exit(1); } - kernel_size -= setup_size; + data.kernel_size -= setup_size; setup = g_malloc(setup_size); - kernel = g_malloc(kernel_size); + data.kernel = g_malloc(data.kernel_size); fseek(f, 0, SEEK_SET); if (fread(setup, 1, setup_size, f) != setup_size) { fprintf(stderr, "fread() failed\n"); exit(1); } - if (fread(kernel, 1, kernel_size, f) != kernel_size) { + if (fread(data.kernel, 1, data.kernel_size, f) != data.kernel_size) { fprintf(stderr, "fread() failed\n"); exit(1); } @@ -1054,7 +1062,7 @@ void x86_load_linux(X86MachineState *x86ms, /* append dtb to kernel */ if (dtb_filename) { - if (protocol < 0x209) { + if (data.protocol < 0x209) { fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n"); exit(1); } @@ -1066,13 +1074,13 @@ void x86_load_linux(X86MachineState *x86ms, exit(1); } - setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16); - kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size; - kernel = g_realloc(kernel, kernel_size); + data.setup_data_offset = QEMU_ALIGN_UP(data.kernel_size, 16); + data.kernel_size = data.setup_data_offset + sizeof(struct setup_data) + dtb_size; + data.kernel = g_realloc(data.kernel, data.kernel_size); - stq_p(header + 0x250, prot_addr + setup_data_offset); + stq_p(data.header + 0x250, data.prot_addr + data.setup_data_offset); - setup_data = (struct setup_data *)(kernel + setup_data_offset); + struct setup_data *setup_data = (struct setup_data *)(data.kernel + data.setup_data_offset); setup_data->next = 0; setup_data->type = cpu_to_le32(SETUP_DTB); setup_data->len = cpu_to_le32(dtb_size); @@ -1088,14 +1096,14 @@ void x86_load_linux(X86MachineState *x86ms, * file the user passed in. */ if (!sev_enabled()) { - memcpy(setup, header, MIN(sizeof(header), setup_size)); + memcpy(setup, data.header, MIN(sizeof(data.header), setup_size)); } - fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr); - fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size); - fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size); - sev_load_ctx.kernel_data = (char *)kernel; - sev_load_ctx.kernel_size = kernel_size; + fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, data.prot_addr); + fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, data.kernel_size); + fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, data.kernel, data.kernel_size); + sev_load_ctx.kernel_data = (char *)data.kernel; + sev_load_ctx.kernel_size = data.kernel_size; fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr); fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size); From patchwork Thu Jul 21 12:29:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12925048 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BE04BC433EF for ; Thu, 21 Jul 2022 12:40:46 +0000 (UTC) Received: from localhost ([::1]:50572 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEVTw-0006ye-NY for qemu-devel@archiver.kernel.org; Thu, 21 Jul 2022 08:40:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35832) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEVJN-0004Mt-H0 for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:29:49 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:55777) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEVJL-0004qT-NY for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:29:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658406587; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RXiXFEEBa1mv6ZvamF/0ld902yL9HoMwAaQ7edK3ppo=; b=RBqbhBeqXS49OZeu/FZ+PnOW3nL3+N18JRKYHkOXNf4/5iXIdCgzn3Qt7+BTVecTbCtbom /3KmLtM0aTfRZJ/GSUdRbaRhmn1WLlBXURPpBv5vFmWLoAHaYOlrXIMlThr1LWYwV+vzYZ DA0IaQ+QxlnW2bj/1DMOD8wYAaq68FI= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-167-ehGtWeJKMA6XgmvXaW9biQ-1; Thu, 21 Jul 2022 08:29:45 -0400 X-MC-Unique: ehGtWeJKMA6XgmvXaW9biQ-1 Received: by mail-ej1-f71.google.com with SMTP id qw22-20020a1709066a1600b0072f43c1f59bso724446ejc.6 for ; Thu, 21 Jul 2022 05:29:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RXiXFEEBa1mv6ZvamF/0ld902yL9HoMwAaQ7edK3ppo=; b=U0lUFXhd4Vy65tOF7oCsPb9AmPnka2qAN2ZUZ1RgEwTbT1Amua5Kyg7Qp0Cch2U87W fu/QUuHwZLJPxN5Y2fM8/CJep0Te73pEcvFvZncvYZ4M/LPiJ3+e42YzTBCP3XC5YVhu uOcgq+bHeWoARq7Ds/OFf3Pc90HemEhgWO19W0O+aC+NwRKCirDJfehUhOmBrLbehHTT 5TQ3V3I4RYTGrBzfG1qesG+Sqsf6+iDz7xpQmD/bvMNBCXVJa/nvCuAxWuDbmAj5s0Rn RpUWekHdp5ETLKfcgF+I9ke6U9GXd29w4nwdGHTPG2BPMrC8j0XXNTFxpL7Bzu2dpJk5 wCgA== X-Gm-Message-State: AJIora8NOsdx8VjaNS2rP15yyPUSDEWbyGZOlLKgOgBrXN4CWdygQINU cyFlCwS3aHo1k9h1GUdv/kuZ22pdqpgIxsN4kn41cNvE4L1RTmxQzJyvS7lm3w4pkG1AXAhZgkz AGyk9kUkr6qzJS8wPHQzn8WDKpwmEKoI7KDqLGxR2sfTlYAiBGywRbYEpc20mNoapDAM= X-Received: by 2002:aa7:da91:0:b0:43b:a4c1:6ec8 with SMTP id q17-20020aa7da91000000b0043ba4c16ec8mr13963551eds.425.1658406584207; Thu, 21 Jul 2022 05:29:44 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uPRdiafp7fY6sNBrw+TA5r0cCn2itCIj+5He8c88PSut644noYjvmjrF56E0MeOKnm89Vtjg== X-Received: by 2002:aa7:da91:0:b0:43b:a4c1:6ec8 with SMTP id q17-20020aa7da91000000b0043ba4c16ec8mr13963496eds.425.1658406583695; Thu, 21 Jul 2022 05:29:43 -0700 (PDT) Received: from goa-sendmail ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id d2-20020a170906304200b0072f42ca292bsm796426ejd.129.2022.07.21.05.29.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jul 2022 05:29:42 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: "Jason A . Donenfeld" , "Michael S . Tsirkin" , Eduardo Habkost , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Laurent Vivier Subject: [PATCH 3/4] hw/i386: extract handling of setup data linked list Date: Thu, 21 Jul 2022 14:29:36 +0200 Message-Id: <20220721122937.729959-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220721122937.729959-1-pbonzini@redhat.com> References: <20220721122937.729959-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" In preparation for the introduction of a second setup data block, manage the linked list and the reallocation of data.kernel in a separate function. Unlike the code that's being moved, the function can be called more than once. Signed-off-by: Paolo Bonzini --- hw/i386/x86.c | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 253a6ff536..564bf3834b 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -828,6 +828,34 @@ typedef struct LinuxBootData { size_t setup_data_offset; } LinuxBootData; +static void *add_setup_data(LinuxBootData *data, uint32_t size, uint32_t type) +{ + struct setup_data *setup_data; + size_t prev_setup_data_offset = data->setup_data_offset; + + if (data->protocol < 0x209) { + fprintf(stderr, "qemu: Linux kernel too old to add setup data\n"); + exit(1); + } + + data->setup_data_offset = QEMU_ALIGN_UP(data->kernel_size, 16); + data->kernel_size = data->setup_data_offset + sizeof(struct setup_data) + size; + data->kernel = g_realloc(data->kernel, data->kernel_size); + + if (prev_setup_data_offset) { + setup_data = (struct setup_data *)(data->kernel + prev_setup_data_offset); + setup_data->next = cpu_to_le64(data->prot_addr + data->setup_data_offset); + } else { + stq_p(data->header + 0x250, data->prot_addr + data->setup_data_offset); + } + + setup_data = (struct setup_data *)(data->kernel + data->setup_data_offset); + setup_data->next = 0; + setup_data->type = cpu_to_le32(type); + setup_data->len = cpu_to_le32(size); + return setup_data->data; +} + void x86_load_linux(X86MachineState *x86ms, FWCfgState *fw_cfg, int acpi_data_size, @@ -1062,11 +1090,6 @@ void x86_load_linux(X86MachineState *x86ms, /* append dtb to kernel */ if (dtb_filename) { - if (data.protocol < 0x209) { - fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n"); - exit(1); - } - dtb_size = get_image_size(dtb_filename); if (dtb_size <= 0) { fprintf(stderr, "qemu: error reading dtb %s: %s\n", @@ -1074,18 +1097,8 @@ void x86_load_linux(X86MachineState *x86ms, exit(1); } - data.setup_data_offset = QEMU_ALIGN_UP(data.kernel_size, 16); - data.kernel_size = data.setup_data_offset + sizeof(struct setup_data) + dtb_size; - data.kernel = g_realloc(data.kernel, data.kernel_size); - - stq_p(data.header + 0x250, data.prot_addr + data.setup_data_offset); - - struct setup_data *setup_data = (struct setup_data *)(data.kernel + data.setup_data_offset); - setup_data->next = 0; - setup_data->type = cpu_to_le32(SETUP_DTB); - setup_data->len = cpu_to_le32(dtb_size); - - load_image_size(dtb_filename, setup_data->data, dtb_size); + void *dtb = add_setup_data(&data, dtb_size, SETUP_DTB); + load_image_size(dtb_filename, dtb, dtb_size); } /* From patchwork Thu Jul 21 12:29:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12925052 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 799F1C43334 for ; Thu, 21 Jul 2022 12:45:20 +0000 (UTC) Received: from localhost ([::1]:59082 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEVYN-0004sg-91 for qemu-devel@archiver.kernel.org; Thu, 21 Jul 2022 08:45:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35846) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEVJP-0004Qu-5K for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:29:51 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:36190) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oEVJM-0004qs-So for qemu-devel@nongnu.org; Thu, 21 Jul 2022 08:29:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658406588; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Sceov1CLy0D+Gutd0UzwXapyWU3DMBeCOe8zNIq1+H8=; b=IBtmwWQpzGPPUnMRHH81JJdnKlMJ54oCq56U08eTzXAAbFBUSSqxmL4sVN8a5AQS5qlIZQ y0UKq+8eefCA6n7ztn/cznR2ro/+iyXNpsJM+ZpFQC3b/ToXTLgIZ4Jq1vs0aWtKrcY08W utdZO7+Kl6yL2SXWY/4JzFCa436dog0= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-208-gn8BlLTgN3CF2Q_TH69psQ-1; Thu, 21 Jul 2022 08:29:47 -0400 X-MC-Unique: gn8BlLTgN3CF2Q_TH69psQ-1 Received: by mail-ed1-f70.google.com with SMTP id z20-20020a05640240d400b0043a82d9d65fso1046176edb.0 for ; Thu, 21 Jul 2022 05:29:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Sceov1CLy0D+Gutd0UzwXapyWU3DMBeCOe8zNIq1+H8=; b=jnnFL1aO2LzLVoFLOtknUhrvc8czqomBaY+vaVDL3EpG9xJ9W1VLjfYWqRF3G3i2Iq rOEd9bNeTgKTIr7kuX1I3sRD38JBXSNAJpOq5boEq0+8Zj3A0o2SxZhov1l8TaJdlvJc deQLkSD9S9ds56PJzvg3E/f25wxOMnnnbS3+CecC63oE4oSQZvn+9ULsxqsbuqIyUXeh 0Cr4zLRRywkC8pjuWr03ntX0adZ/EQpDp8AZnykMjch98BD8JzUm5BXxWGpgZZ7IfBpp MxDYbbVxRshtbEeYwMHLmDA0T67L5pDDqaYVFtMQrpfRSylAa6ikaaJBbdmyLFq43zxY YPUQ== X-Gm-Message-State: AJIora/Qv4/A6ybeyG62JQqJcnda1hArh4Fx0NRwhzRQax7btnpXIhV1 53cmahVj5c7Az935zRxx3mCqTZD2fTJi5CqshomvYDBTnKCrlr6poMzGhysuoBxgKlVFKGuKhG5 HzY+lg67m5vdHi5N0I+fOjjf3GrcXT8yObVcKFyc/0inYbU04uOHwY5P67128FJvZLMA= X-Received: by 2002:a17:907:2722:b0:72b:6d79:1a46 with SMTP id d2-20020a170907272200b0072b6d791a46mr40559978ejl.713.1658406585777; Thu, 21 Jul 2022 05:29:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ukYdiFbsjdhJzyosrMfJokyXZhLE7oFKaS5eYoTuDbZXqmjlZJqtcbvNdjWUdLv5MQ1H/sdA== X-Received: by 2002:a17:907:2722:b0:72b:6d79:1a46 with SMTP id d2-20020a170907272200b0072b6d791a46mr40559932ejl.713.1658406585380; Thu, 21 Jul 2022 05:29:45 -0700 (PDT) Received: from goa-sendmail ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.gmail.com with ESMTPSA id we10-20020a170907234a00b006fec4ee28d0sm801504ejb.189.2022.07.21.05.29.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jul 2022 05:29:44 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: "Jason A . Donenfeld" , "Michael S . Tsirkin" , Eduardo Habkost , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Laurent Vivier Subject: [PATCH 4/4] hw/i386: pass RNG seed via setup_data entry Date: Thu, 21 Jul 2022 14:29:37 +0200 Message-Id: <20220721122937.729959-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220721122937.729959-1-pbonzini@redhat.com> References: <20220721122937.729959-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -11 X-Spam_score: -1.2 X-Spam_bar: - X-Spam_report: (-1.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, PP_MIME_FAKE_ASCII_TEXT=0.999, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: "Jason A. Donenfeld" Tiny machines optimized for fast boot time generally don't use EFI, which means a random seed has to be supplied some other way. For this purpose, Linux (≥5.20) supports passing a seed in the setup_data table with SETUP_RNG_SEED, specially intended for hypervisors, kexec, and specialized bootloaders. The linked commit shows the upstream kernel implementation. Link: https://git.kernel.org/tip/tip/c/68b8e9713c8 Signed-off-by: Jason A. Donenfeld Message-Id: <20220719115300.104095-1-Jason@zx2c4.com> [Mostly rewritten to preserve guest ABI, but still starting from Jason's code. - Paolo] Signed-off-by: Paolo Bonzini --- hw/i386/pc.c | 1 + hw/i386/x86.c | 31 ++++++++++++++++++++ include/hw/i386/x86.h | 2 ++ include/standard-headers/asm-x86/bootparam.h | 1 + 4 files changed, 35 insertions(+) diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 774cb2bf07..d456fbb166 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -112,6 +112,7 @@ const size_t pc_compat_7_0_len = G_N_ELEMENTS(pc_compat_7_0); GlobalProperty pc_compat_6_2[] = { { "virtio-mem", "unplugged-inaccessible", "off" }, + { TYPE_X86_MACHINE, "linuxboot-seed", "off" }, }; const size_t pc_compat_6_2_len = G_N_ELEMENTS(pc_compat_6_2); diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 564bf3834b..c5d01e084a 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -26,6 +26,7 @@ #include "qemu/cutils.h" #include "qemu/units.h" #include "qemu/datadir.h" +#include "qemu/guest-random.h" #include "qapi/error.h" #include "qapi/qmp/qerror.h" #include "qapi/qapi-visit-common.h" @@ -1088,6 +1089,12 @@ void x86_load_linux(X86MachineState *x86ms, } fclose(f); + if (x86ms->linuxboot_seed != ON_OFF_AUTO_OFF && + (data.protocol >= 0x209 || x86ms->linuxboot_seed == ON_OFF_AUTO_ON)) { + void *seed = add_setup_data(&data, 32, SETUP_RNG_SEED); + qemu_guest_getrandom_nofail(seed, 32); + } + /* append dtb to kernel */ if (dtb_filename) { dtb_size = get_image_size(dtb_filename); @@ -1247,6 +1254,23 @@ static void x86_machine_set_smm(Object *obj, Visitor *v, const char *name, visit_type_OnOffAuto(v, name, &x86ms->smm, errp); } +static void x86_machine_get_linuxboot_seed(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + X86MachineState *x86ms = X86_MACHINE(obj); + OnOffAuto linuxboot_seed = x86ms->linuxboot_seed; + + visit_type_OnOffAuto(v, name, &linuxboot_seed, errp); +} + +static void x86_machine_set_linuxboot_seed(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + X86MachineState *x86ms = X86_MACHINE(obj); + + visit_type_OnOffAuto(v, name, &x86ms->linuxboot_seed, errp); +} + bool x86_machine_is_acpi_enabled(const X86MachineState *x86ms) { if (x86ms->acpi == ON_OFF_AUTO_OFF) { @@ -1397,6 +1421,7 @@ static void x86_machine_initfn(Object *obj) x86ms->acpi = ON_OFF_AUTO_AUTO; x86ms->pit = ON_OFF_AUTO_AUTO; x86ms->pic = ON_OFF_AUTO_AUTO; + x86ms->linuxboot_seed = ON_OFF_AUTO_AUTO; x86ms->pci_irq_mask = ACPI_BUILD_PCI_IRQS; x86ms->oem_id = g_strndup(ACPI_BUILD_APPNAME6, 6); x86ms->oem_table_id = g_strndup(ACPI_BUILD_APPNAME8, 8); @@ -1435,6 +1460,12 @@ static void x86_machine_class_init(ObjectClass *oc, void *data) object_class_property_set_description(oc, X86_MACHINE_PIT, "Enable i8254 PIT"); + object_class_property_add(oc, X86_MACHINE_LINUXBOOT_SEED, "OnOffAuto", + x86_machine_get_linuxboot_seed, x86_machine_set_linuxboot_seed, + NULL, NULL); + object_class_property_set_description(oc, X86_MACHINE_LINUXBOOT_SEED, + "Pass random number seed to -kernel Linux image"); + object_class_property_add(oc, X86_MACHINE_PIC, "OnOffAuto", x86_machine_get_pic, x86_machine_set_pic, diff --git a/include/hw/i386/x86.h b/include/hw/i386/x86.h index 9089bdd99c..edf0f6799e 100644 --- a/include/hw/i386/x86.h +++ b/include/hw/i386/x86.h @@ -67,6 +67,7 @@ struct X86MachineState { OnOffAuto acpi; OnOffAuto pit; OnOffAuto pic; + OnOffAuto linuxboot_seed; char *oem_id; char *oem_table_id; @@ -91,6 +92,7 @@ struct X86MachineState { #define X86_MACHINE_OEM_ID "x-oem-id" #define X86_MACHINE_OEM_TABLE_ID "x-oem-table-id" #define X86_MACHINE_BUS_LOCK_RATELIMIT "bus-lock-ratelimit" +#define X86_MACHINE_LINUXBOOT_SEED "linuxboot-seed" #define TYPE_X86_MACHINE MACHINE_TYPE_NAME("x86") OBJECT_DECLARE_TYPE(X86MachineState, X86MachineClass, X86_MACHINE) diff --git a/include/standard-headers/asm-x86/bootparam.h b/include/standard-headers/asm-x86/bootparam.h index 072e2ed546..b2aaad10e5 100644 --- a/include/standard-headers/asm-x86/bootparam.h +++ b/include/standard-headers/asm-x86/bootparam.h @@ -10,6 +10,7 @@ #define SETUP_EFI 4 #define SETUP_APPLE_PROPERTIES 5 #define SETUP_JAILHOUSE 6 +#define SETUP_RNG_SEED 9 #define SETUP_INDIRECT (1<<31)