From owner-svn-src-stable@freebsd.org Tue Jul 28 04:58:02 2020 Return-Path: Delivered-To: svn-src-stable@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 728AF37CF36; Tue, 28 Jul 2020 04:58:02 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BG4Dp2NLDz3bQ8; Tue, 28 Jul 2020 04:58:02 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 34F2F9772; Tue, 28 Jul 2020 04:58:02 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 06S4w2Gl014909; Tue, 28 Jul 2020 04:58:02 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 06S4w28H014908; Tue, 28 Jul 2020 04:58:02 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <202007280458.06S4w28H014908@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Tue, 28 Jul 2020 04:58:02 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r363632 - stable/12/stand/efi/loader X-SVN-Group: stable-12 X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: stable/12/stand/efi/loader X-SVN-Commit-Revision: 363632 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 28 Jul 2020 04:58:02 -0000 Author: kevans Date: Tue Jul 28 04:58:01 2020 New Revision: 363632 URL: https://svnweb.freebsd.org/changeset/base/363632 Log: MFC r355132: Support kernels larger than EFI_STAGING_SIZE in loader.efi With a very large kernel or module the staging area may be too small to hold it. When this is the case try to allocate more space before failing in the efi copyin/copyout/readin functions. Modified: stable/12/stand/efi/loader/copy.c Directory Properties: stable/12/ (props changed) Modified: stable/12/stand/efi/loader/copy.c ============================================================================== --- stable/12/stand/efi/loader/copy.c Tue Jul 28 03:25:31 2020 (r363631) +++ stable/12/stand/efi/loader/copy.c Tue Jul 28 04:58:01 2020 (r363632) @@ -185,7 +185,7 @@ out: #endif #endif -EFI_PHYSICAL_ADDRESS staging, staging_end; +EFI_PHYSICAL_ADDRESS staging, staging_end, staging_base; int stage_offset_set = 0; ssize_t stage_offset; @@ -224,6 +224,7 @@ efi_copy_init(void) EFI_ERROR_CODE(status)); return (status); } + staging_base = staging; staging_end = staging + nr_pages * EFI_PAGE_SIZE; #if defined(__aarch64__) || defined(__arm__) @@ -240,6 +241,66 @@ efi_copy_init(void) return (0); } +static bool +efi_check_space(vm_offset_t end) +{ + EFI_PHYSICAL_ADDRESS addr; + EFI_STATUS status; + unsigned long nr_pages; + + /* There is already enough space */ + if (end <= staging_end) + return (true); + + end = roundup2(end, EFI_PAGE_SIZE); + nr_pages = EFI_SIZE_TO_PAGES(end - staging_end); + +#if defined(__i386__) || defined(__amd64__) + /* X86 needs all memory to be allocated under the 1G boundary */ + if (end > 1024*1024*1024) + goto before_staging; +#endif + + /* Try to allocate more space after the previous allocation */ + addr = staging_end; + status = BS->AllocatePages(AllocateAddress, EfiLoaderData, nr_pages, + &addr); + if (!EFI_ERROR(status)) { + staging_end = staging_end + nr_pages * EFI_PAGE_SIZE; + return (true); + } + +before_staging: + /* Try allocating space before the previous allocation */ + if (staging < nr_pages * EFI_PAGE_SIZE) { + printf("Not enough space before allocation\n"); + return (false); + } + addr = staging - nr_pages * EFI_PAGE_SIZE; +#if defined(__aarch64__) || defined(__arm__) + /* See efi_copy_init for why this is needed */ + addr = rounddown2(addr, 2 * 1024 * 1024); +#endif + nr_pages = EFI_SIZE_TO_PAGES(staging_base - addr); + status = BS->AllocatePages(AllocateAddress, EfiLoaderData, nr_pages, + &addr); + if (!EFI_ERROR(status)) { + /* + * Move the old allocation and update the state so + * translation still works. + */ + staging_base = addr; + memmove((void *)staging_base, (void *)staging, + staging_end - staging); + stage_offset -= (staging - staging_base); + staging = staging_base; + return (true); + } + + printf("efi_check_space: Unable to expand staging area\n"); + return (false); +} + void * efi_translate(vm_offset_t ptr) { @@ -257,7 +318,7 @@ efi_copyin(const void *src, vm_offset_t dest, const si } /* XXX: Callers do not check for failure. */ - if (dest + stage_offset + len > staging_end) { + if (!efi_check_space(dest + stage_offset + len)) { errno = ENOMEM; return (-1); } @@ -283,7 +344,7 @@ ssize_t efi_readin(readin_handle_t fd, vm_offset_t dest, const size_t len) { - if (dest + stage_offset + len > staging_end) { + if (!efi_check_space(dest + stage_offset + len)) { errno = ENOMEM; return (-1); }