From nobody Tue Feb 10 03:39:04 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1658407029; cv=none; d=zohomail.com; s=zohoarc; b=M7jy1YemYA6dm+jidizleG6mCdaPtSTzlxWcvDT4QqX4TYifQWQa/pq5CwD9O/UDHGp2GU1wYe5STFquMQJd1y4jsgkFiZ/5DZEI5BZoecWOUNXb1xbTniOOPR38xNlDPvwnj/yi7M/EiBhXxyPNSQtCHjpBW8PTAnjl8IQRppY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658407029; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ewsIbER1SRBWS1JrbrcFLWG+2/3TGq+isiGbthcclPE=; b=gLTausgP4OxGteIed28nNi95jJSGmzOLsHaAF0Xk4AUCeEBTXT0C0m22OfY/guQoOXgkYs/dFfl5xBJVcsR3zkfmAR78D+D7At38YUsLXBqJFrMBFNOWjjOKzSZTcuvT1/6sURCFs2u3sTNIJAt1eE76bLjlck8Q5bD45zfcjEM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1658407029587820.0870619492666; Thu, 21 Jul 2022 05:37:09 -0700 (PDT) Received: from localhost ([::1]:42818 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oEVQP-0001Lf-PC for importer@patchew.org; Thu, 21 Jul 2022 08:37:05 -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 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 Received: by mail-ed1-f70.google.com with SMTP id o13-20020a056402438d00b0043aa846b2d2so1027978edc.8 for ; Thu, 21 Jul 2022 05:29:42 -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) 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= X-MC-Unique: 2WFpAiUHMgiyE9HFIIbW-g-1 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) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: "Jason A . Donenfeld" , "Michael S . Tsirkin" , Eduardo Habkost , =?UTF-8?q?Philippe=20Mathieu-Daud=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 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1658407031008100001 Content-Type: text/plain; charset="utf-8" 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; } =20 +static bool load_pvh(X86MachineState *x86ms, FWCfgState *fw_cfg, const cha= r *kernel_filename, + const char *initrd_filename, uint32_t initrd_max, con= st 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 =3D NULL; + + mapped_file =3D 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 =3D mapped_file; + + initrd_data =3D g_mapped_file_get_contents(mapped_file); + initrd_size =3D g_mapped_file_get_length(mapped_file); + if (initrd_size >=3D 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 =3D (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 =3D 0; + option_rom[nb_option_roms].name =3D "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 =3D 0; + hwaddr real_addr, prot_addr, cmdline_addr; FILE *f; char *vmode; MachineState *machine =3D MACHINE(x86ms); @@ -785,6 +841,8 @@ void x86_load_linux(X86MachineState *x86ms, const char *kernel_cmdline =3D machine->kernel_cmdline; SevKernelLoaderContext sev_load_ctx =3D {}; =20 + initrd_max =3D x86ms->below_4g_mem_size - acpi_data_size - 1; + /* Align to 16 bytes as a paranoia measure */ cmdline_size =3D (strlen(kernel_cmdline) + 16) & ~15; =20 @@ -806,9 +864,7 @@ void x86_load_linux(X86MachineState *x86ms, } =20 /* kernel protocol version */ - if (ldl_p(header + 0x202) =3D=3D 0x53726448) { - protocol =3D lduw_p(header + 0x206); - } else { + if (ldl_p(header + 0x202) !=3D 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 =3D NULL; - - mapped_file =3D 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 =3D mapped_file; - - initrd_data =3D g_mapped_file_get_contents(mapped_file); - initrd_size =3D g_mapped_file_get_length(mapped_file); - initrd_max =3D x86ms->below_4g_mem_size - acpi_data_size -= 1; - if (initrd_size >=3D initrd_max) { - fprintf(stderr, "qemu: initrd is too large, cannot sup= port." - "(max: %"PRIu32", need %"PRId64")\n", - initrd_max, (uint64_t)initrd_size); - exit(1); - } - - initrd_addr =3D (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 =3D 0; - option_rom[nb_option_roms].name =3D "pvh.bin"; - nb_option_roms++; - return; - } + } protocol =3D 0; + } else { + protocol =3D lduw_p(header + 0x206); } =20 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 we= ll. + * Therefore here just limit initrd_max to the available memory be= low 4G. */ - initrd_max =3D UINT32_MAX; } else if (protocol >=3D 0x203) { - initrd_max =3D ldl_p(header + 0x22c); + initrd_max =3D MIN(initrd_max, ldl_p(header + 0x22c)); } else { - initrd_max =3D 0x37ffffff; - } - - if (initrd_max >=3D x86ms->below_4g_mem_size - acpi_data_size) { - initrd_max =3D x86ms->below_4g_mem_size - acpi_data_size - 1; + initrd_max =3D MIN(initrd_max, 0x37ffffff); } =20 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr); @@ -1008,7 +1015,7 @@ void x86_load_linux(X86MachineState *x86ms, exit(1); } =20 - initrd_addr =3D (initrd_max - initrd_size) & ~4095; + hwaddr initrd_addr =3D (initrd_max - initrd_size) & ~4095; =20 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr); fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); --=20 2.36.1