Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1maCFt-0007Oq-A8 for mharc-grub-devel@gnu.org; Tue, 12 Oct 2021 03:31:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42640) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maCFa-0007G1-Vn for grub-devel@gnu.org; Tue, 12 Oct 2021 03:31:04 -0400 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]:38693) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1maCFY-0006hO-J4 for grub-devel@gnu.org; Tue, 12 Oct 2021 03:31:02 -0400 Received: by mail-pg1-x530.google.com with SMTP id s75so13111081pgs.5 for ; Tue, 12 Oct 2021 00:31:00 -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=o6a44CCeogZREj9WoK8j45VjdzoP2wYkE0wP/H6L+4o=; b=fAcY4Px3rBngsSrn5RC9wg79VXpQhx7n2fXMu5J53StWsg+mY/WnFi5laeM08TpqrZ Ah+3rCoHEXV26LYc/PFJDl9jR5cegQeJG4g+O1osVLx+9q8KDemG0jjJKn55pvKUEwpp xOwd38xhL+G+4CO7xBkqCHAd2RckGCk0JUxW4= 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=o6a44CCeogZREj9WoK8j45VjdzoP2wYkE0wP/H6L+4o=; b=PaBrvaYIC7YLa6NdJ6p3G+TzCBw5n6INnVerqeaCHgOG1dTo2OXgjWkPFvScPliZPP rVk9yS3BTVmSlO2ykPYWYUTbzfOdVtRrTUIuc1BxkIK5mKf3hoykRM2CXSOcruxHwRPP 5vBBFnueWN9R18/UN0eTiGKpxCmhIGDRMAGIu2eA3I8KbzesoPZ14G3jipFl29c4huP+ tcZ3yqFoauejHsBs/0sJds3/UHoGIoQz320R+Ca34hym2bq1drXYn3ibXh7Mopz9FvBG czzyY61rL2whZQKiQhkngBKEqIte50qKeaQ+pnctnFrEfTD22FJtwCQtuVITamfubq2B 7NSQ== X-Gm-Message-State: AOAM5315y/oZPABsi5kZ9jJBTXVYDH9VAgy+cYKvW6MY1ofAL3zkt/lO ifq0M4ZQN+cHGKZVAwMmBWBnvJVXrva0Hw== X-Google-Smtp-Source: ABdhPJyofloTLfflpR/aW0KvD10oxtzFAUD+g20mVdijn38I5tEJlnajgkwEK0VTVjgW3JZ19csoig== X-Received: by 2002:a63:3f0b:: with SMTP id m11mr5010243pga.274.1634023859082; Tue, 12 Oct 2021 00:30:59 -0700 (PDT) Received: from localhost ([2001:4479:e300:600:41fe:d15:934f:4029]) by smtp.gmail.com with ESMTPSA id z124sm9960044pfb.108.2021.10.12.00.30.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Oct 2021 00:30:58 -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 Subject: [PATCH 11/19] efi: mm: Extract function to add memory regions Date: Tue, 12 Oct 2021 18:30:00 +1100 Message-Id: <20211012073008.4059133-12-dja@axtens.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211012073008.4059133-1-dja@axtens.net> References: <20211012073008.4059133-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::530; envelope-from=dja@axtens.net; helo=mail-pg1-x530.google.com X-Spam_score_int: -1 X-Spam_score: -0.2 X-Spam_bar: / X-Spam_report: (-0.2 / 5.0 requ) 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: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Oct 2021 07:31:07 -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 --- grub-core/kern/efi/mm.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c index 4d276bc87a4c..cfc6a67fc0cc 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c @@ -504,7 +504,7 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, addr = grub_efi_allocate_pages_real (start, pages, GRUB_EFI_ALLOCATE_ADDRESS, - GRUB_EFI_LOADER_CODE); + GRUB_EFI_LOADER_CODE); if (! addr) grub_fatal ("cannot allocate conventional memory %p with %u pages", (void *) ((grub_addr_t) start), @@ -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"); /* 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"); 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, "cannot get memory map"); 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.30.2