Module Name:    src
Committed By:   jmcneill
Date:           Sun Oct 28 10:17:47 UTC 2018

Modified Files:
        src/sys/stand/efiboot: efiacpi.c efiboot.c exec.c version
        src/sys/stand/efiboot/bootaa64: Makefile

Log Message:
Make EFI runtime services available to the kernel. Bump version to 1.5.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/stand/efiboot/efiacpi.c
cvs rdiff -u -r1.9 -r1.10 src/sys/stand/efiboot/efiboot.c
cvs rdiff -u -r1.7 -r1.8 src/sys/stand/efiboot/exec.c
cvs rdiff -u -r1.5 -r1.6 src/sys/stand/efiboot/version
cvs rdiff -u -r1.4 -r1.5 src/sys/stand/efiboot/bootaa64/Makefile

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/stand/efiboot/efiacpi.c
diff -u src/sys/stand/efiboot/efiacpi.c:1.2 src/sys/stand/efiboot/efiacpi.c:1.3
--- src/sys/stand/efiboot/efiacpi.c:1.2	Tue Oct 23 10:12:59 2018
+++ src/sys/stand/efiboot/efiacpi.c	Sun Oct 28 10:17:47 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: efiacpi.c,v 1.2 2018/10/23 10:12:59 jmcneill Exp $ */
+/* $NetBSD: efiacpi.c,v 1.3 2018/10/28 10:17:47 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -103,6 +103,9 @@ efi_acpi_create_fdt(void)
 	fdt_setprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,acpi-root-table", (uint64_t)(uintptr_t)acpi_root);
 	if (smbios3_table)
 		fdt_setprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,smbios-table", (uint64_t)(uintptr_t)smbios3_table);
+#ifdef EFIBOOT_RUNTIME_ADDRESS
+	fdt_setprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-system-table", (uint64_t)(uintptr_t)ST);
+#endif
 
 	fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "acpi");
 	fdt_setprop_string(fdt, fdt_path_offset(fdt, "/acpi"), "compatible", "netbsd,acpi");

Index: src/sys/stand/efiboot/efiboot.c
diff -u src/sys/stand/efiboot/efiboot.c:1.9 src/sys/stand/efiboot/efiboot.c:1.10
--- src/sys/stand/efiboot/efiboot.c:1.9	Fri Oct 12 22:08:04 2018
+++ src/sys/stand/efiboot/efiboot.c	Sun Oct 28 10:17:47 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: efiboot.c,v 1.9 2018/10/12 22:08:04 jmcneill Exp $ */
+/* $NetBSD: efiboot.c,v 1.10 2018/10/28 10:17:47 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <jmcne...@invisible.ca>
@@ -34,6 +34,8 @@
 
 #include <sys/reboot.h>
 
+#include <libfdt.h>
+
 EFI_HANDLE IH;
 EFI_DEVICE_PATH *efi_bootdp;
 EFI_LOADED_IMAGE *efi_li;
@@ -41,7 +43,7 @@ EFI_LOADED_IMAGE *efi_li;
 int howto = 0;
 
 static EFI_PHYSICAL_ADDRESS heap_start;
-static UINTN heap_size = 1 * 1024 * 1024;
+static UINTN heap_size = 8 * 1024 * 1024;
 static EFI_EVENT delay_ev = 0;
 
 EFI_STATUS EFIAPI efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE *);
@@ -92,18 +94,92 @@ efi_main(EFI_HANDLE imageHandle, EFI_SYS
 	return EFI_SUCCESS;
 }
 
+#ifdef EFIBOOT_RUNTIME_ADDRESS
+static uint64_t
+efi_runtime_alloc_va(uint64_t npages)
+{
+	static uint64_t va = EFIBOOT_RUNTIME_ADDRESS;
+	static uint64_t sz = EFIBOOT_RUNTIME_SIZE;
+	uint64_t nva;
+
+	if (sz < (npages * EFI_PAGE_SIZE))
+		panic("efi_acpi_alloc_va: couldn't allocate %" PRIu64 " pages", npages);
+
+	nva = va;
+	va += (npages * EFI_PAGE_SIZE);
+	sz -= (npages * EFI_PAGE_SIZE);
+
+	return nva;
+}
+#endif
+
+#ifdef EFIBOOT_RUNTIME_ADDRESS
+static void
+efi_set_virtual_address_map(EFI_MEMORY_DESCRIPTOR *memmap, UINTN nentries, UINTN mapkey, UINTN descsize, UINT32 descver)
+{
+	EFI_MEMORY_DESCRIPTOR *md, *vmd, *vmemmap;
+	EFI_STATUS status;
+	int n, nrt;
+	void *fdt;
+
+	fdt = efi_fdt_data();
+
+	vmemmap = alloc(nentries * descsize);
+	if (vmemmap == NULL)
+		panic("FATAL: couldn't allocate virtual memory map");
+
+	for (n = 0, nrt = 0, vmd = vmemmap, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
+		if ((md->Attribute & EFI_MEMORY_RUNTIME) == 0)
+			continue;
+		md->VirtualStart = efi_runtime_alloc_va(md->NumberOfPages);
+
+		switch (md->Type) {
+		case EfiRuntimeServicesCode:
+			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-code", md->PhysicalStart);
+			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-code", md->VirtualStart);
+			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-code", md->NumberOfPages * EFI_PAGE_SIZE);
+			break;
+		case EfiRuntimeServicesData:
+			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-data", md->PhysicalStart);
+			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-data", md->VirtualStart);
+			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-data", md->NumberOfPages * EFI_PAGE_SIZE);
+			break;
+		default:
+			break;
+		}
+
+		*vmd = *md;
+		vmd = NextMemoryDescriptor(vmd, descsize);
+		++nrt;
+	}
+
+	status = uefi_call_wrapper(RT->SetVirtualAddressMap, 4, nrt * descsize, descsize, descver, vmemmap);
+	if (EFI_ERROR(status)) {
+		printf("WARNING: SetVirtualAddressMap failed\n");
+		return;
+	}
+}
+#endif
+
 void
 efi_cleanup(void)
 {
 	EFI_STATUS status;
+	EFI_MEMORY_DESCRIPTOR *memmap;
 	UINTN nentries, mapkey, descsize;
 	UINT32 descver;
 
-	LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
+	memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
 
 	status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, mapkey);
-	if (EFI_ERROR(status))
+	if (EFI_ERROR(status)) {
 		printf("WARNING: ExitBootServices failed\n");
+		return;
+	}
+
+#ifdef EFIBOOT_RUNTIME_ADDRESS
+	efi_set_virtual_address_map(memmap, nentries, mapkey, descsize, descver);
+#endif
 }
 
 void

Index: src/sys/stand/efiboot/exec.c
diff -u src/sys/stand/efiboot/exec.c:1.7 src/sys/stand/efiboot/exec.c:1.8
--- src/sys/stand/efiboot/exec.c:1.7	Fri Oct 12 22:08:04 2018
+++ src/sys/stand/efiboot/exec.c	Sun Oct 28 10:17:47 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: exec.c,v 1.7 2018/10/12 22:08:04 jmcneill Exp $ */
+/* $NetBSD: exec.c,v 1.8 2018/10/28 10:17:47 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <jmcne...@invisible.ca>
@@ -168,10 +168,14 @@ exec_netbsd(const char *fname, const cha
 		efi_fdt_initrd(initrd_addr, initrd_size);
 		efi_fdt_bootargs(args);
 		efi_fdt_memory_map();
-		efi_fdt_fini();
 	}
 
 	efi_cleanup();
+
+	if (efi_fdt_size() > 0) {
+		efi_fdt_fini();
+	}
+
 	efi_boot_kernel(marks);
 
 	/* This should not happen.. */

Index: src/sys/stand/efiboot/version
diff -u src/sys/stand/efiboot/version:1.5 src/sys/stand/efiboot/version:1.6
--- src/sys/stand/efiboot/version:1.5	Fri Oct 26 20:56:35 2018
+++ src/sys/stand/efiboot/version	Sun Oct 28 10:17:47 2018
@@ -1,4 +1,4 @@
-$NetBSD: version,v 1.5 2018/10/26 20:56:35 mrg Exp $
+$NetBSD: version,v 1.6 2018/10/28 10:17:47 jmcneill Exp $
 
 NOTE ANY CHANGES YOU MAKE TO THE EFI BOOTLOADER HERE.  The format of this
 file is important - make sure the entries are appended on end, last item
@@ -9,3 +9,4 @@ is taken as the current.
 1.2:	Add environment variable support.
 1.3:	Add ACPI support.
 1.4:	Add bootfile support.
+1.5:	EFI runtime support.

Index: src/sys/stand/efiboot/bootaa64/Makefile
diff -u src/sys/stand/efiboot/bootaa64/Makefile:1.4 src/sys/stand/efiboot/bootaa64/Makefile:1.5
--- src/sys/stand/efiboot/bootaa64/Makefile:1.4	Sun Oct 21 00:57:38 2018
+++ src/sys/stand/efiboot/bootaa64/Makefile	Sun Oct 28 10:17:47 2018
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.4 2018/10/21 00:57:38 jmcneill Exp $
+# $NetBSD: Makefile,v 1.5 2018/10/28 10:17:47 jmcneill Exp $
 
 PROG=		bootaa64.efi
 OBJFMT=		binary
@@ -9,6 +9,8 @@ EXTRA_SOURCES+=	cache.S
 
 COPTS+=		-mgeneral-regs-only -fno-jump-tables
 CFLAGS+=	-DEFIBOOT_ALIGN=0x200000
+CFLAGS+=	-DEFIBOOT_RUNTIME_ADDRESS=0xffff800000000000L
+CFLAGS+=	-DEFIBOOT_RUNTIME_SIZE=0x40000000UL
 CFLAGS+=	-DEFIBOOT_ACPI
 
 .include "${.CURDIR}/../Makefile.efiboot"

Reply via email to