From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nYimr-00061p-2E for mharc-grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:33 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50326) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nYime-0005xl-0K for grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:20 -0400 Received: from [2607:f8b0:4864:20::1033] (port=43883 helo=mail-pj1-x1033.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nYimc-0005Ht-A4 for grub-devel@gnu.org; Mon, 28 Mar 2022 02:23:19 -0400 Received: by mail-pj1-x1033.google.com with SMTP id n7-20020a17090aab8700b001c6aa871860so14483799pjq.2 for ; Sun, 27 Mar 2022 23:23:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KrOt1njgwP7VaigY+G2eVYWbOFDvxmvhDVOXAfTdtHY=; b=FE1eZlwsefiC7Rly/Qqj02HQ7SyMQ1OfhfY02M5cJ+SzgJYny9dMmhCQm99PQoyVgX oQ1c6xQRxw1/mN9ertskx2X0i1z/O4ZTzdWbw9m7fli/QVZ8AM61fvMgQMaC9W/uo+uM p5zbT93+ldegtIutfhbbPvQe8eanXJJ5cnY3w= 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=KrOt1njgwP7VaigY+G2eVYWbOFDvxmvhDVOXAfTdtHY=; b=BTCaxAlqGVbszvhVT8KjR0UISkJ50bfAaiYhYk/Z+ewxPCPWDvJYaaKapmxWjUTlV6 +2qe0Gj9MNbJLekCZJhhOAxchGvhE1oLfUbAppK/wiCdR09XOUrz0gGbWaY+l/dgMYWH D/zvgCQo04/FoLq5EVOvpRqRtDHr2NhWAFWOCuA2xS3tROsg0uXlbjkEeMCOax+Gv+Z/ DARCephDNTrJ9aU8HKLevTodPdYMReOxeodgsbRJcbpHSp4ktImiZlw8BYaJdfZgDrfS mp3g6u+lXUjp4pciUvAX6iwnImBmKAJFV34BRsppq3MOVG5cPlsF36brkDs2IJFA9ICN x8Cw== X-Gm-Message-State: AOAM5306o2O4iN0dHn6BPr6WauDCaloUk85qsWl4Wvp7/hA0ramb5Jcg bPGqQm3Z3fpZA0t4zdIofsAnl+4VIAlf/g== X-Google-Smtp-Source: ABdhPJxAIi8E+O0NfSrvWqxxMteCgo1Wuc2WJHOMkT9RI3A92BzFaJxWUDeVwsT0g+NqrAXqaRrZnQ== X-Received: by 2002:a17:902:cecb:b0:154:6f46:a5d6 with SMTP id d11-20020a170902cecb00b001546f46a5d6mr24659090plg.92.1648448596805; Sun, 27 Mar 2022 23:23:16 -0700 (PDT) Received: from localhost ([2001:4479:e000:e400:743e:bc3e:ec72:bf20]) by smtp.gmail.com with ESMTPSA id 21-20020a630115000000b00382a0895661sm11770480pgb.11.2022.03.27.23.23.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Mar 2022 23:23:16 -0700 (PDT) From: Daniel Axtens To: grub-devel@gnu.org Cc: leif@nuviainc.com, stefanb@linux.ibm.com, ps@pks.im, dkiper@net-space.pl, Daniel Axtens Subject: [PATCH v2 08/15] efi: mm: Extract function to add memory regions Date: Mon, 28 Mar 2022 17:22:33 +1100 Message-Id: <20220328062240.878781-9-dja@axtens.net> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220328062240.878781-1-dja@axtens.net> References: <20220328062240.878781-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::1033 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=dja@axtens.net; helo=mail-pj1-x1033.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, 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: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Mar 2022 06:23:20 -0000 From: Patrick Steinhardt In preparation of support for runtime-allocating additional memory region, this patch extracts the function to retrieve the EFI memory map and add a subset of it to GRUB's own memory regions. Signed-off-by: Patrick Steinhardt Signed-off-by: Daniel Axtens --- grub-core/kern/efi/mm.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c index 0bccd24f304f..c8d3968f7f68 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c @@ -556,8 +556,8 @@ print_memory_map (grub_efi_memory_descriptor_t *memory_map, } #endif -void -grub_efi_mm_init (void) +static grub_err_t +grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes) { grub_efi_memory_descriptor_t *memory_map; grub_efi_memory_descriptor_t *memory_map_end; @@ -570,7 +570,7 @@ grub_efi_mm_init (void) /* Prepare a memory region to store two memory maps. */ memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE)); if (! memory_map) - grub_fatal ("cannot allocate memory"); + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory for memory map"); /* Obtain descriptors for available memory. */ map_size = MEMORY_MAP_SIZE; @@ -588,14 +588,14 @@ grub_efi_mm_init (void) memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (map_size)); if (! memory_map) - grub_fatal ("cannot allocate memory"); + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory for new memory map"); mm_status = grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0); } if (mm_status < 0) - grub_fatal ("cannot get memory map"); + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "error fetching memory map from EFI"); memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size); @@ -610,7 +610,7 @@ grub_efi_mm_init (void) /* Allocate memory regions for GRUB's memory management. */ add_memory_regions (filtered_memory_map, desc_size, - filtered_memory_map_end, BYTES_TO_PAGES (DEFAULT_HEAP_SIZE)); + filtered_memory_map_end, BYTES_TO_PAGES (required_bytes)); #if 0 /* For debug. */ @@ -628,6 +628,15 @@ grub_efi_mm_init (void) /* Release the memory maps. */ grub_efi_free_pages ((grub_addr_t) memory_map, 2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE)); + + return GRUB_ERR_NONE; +} + +void +grub_efi_mm_init (void) +{ + if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE) != GRUB_ERR_NONE) + grub_fatal ("%s", grub_errmsg); } #if defined (__aarch64__) || defined (__arm__) || defined (__riscv) -- 2.32.0