From owner-svn-src-all@freebsd.org Mon Jun 8 08:52:04 2020 Return-Path: Delivered-To: svn-src-all@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 077CC347C37; Mon, 8 Jun 2020 08:52:04 +0000 (UTC) (envelope-from arichardson@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 49gRnv5tdmz3gc5; Mon, 8 Jun 2020 08:52:03 +0000 (UTC) (envelope-from arichardson@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 B392621AD6; Mon, 8 Jun 2020 08:52:03 +0000 (UTC) (envelope-from arichardson@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0588q3V9010872; Mon, 8 Jun 2020 08:52:03 GMT (envelope-from arichardson@FreeBSD.org) Received: (from arichardson@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0588q25X010868; Mon, 8 Jun 2020 08:52:02 GMT (envelope-from arichardson@FreeBSD.org) Message-Id: <202006080852.0588q25X010868@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: arichardson set sender to arichardson@FreeBSD.org using -f From: Alex Richardson Date: Mon, 8 Jun 2020 08:52:02 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r361905 - in head/sys/riscv: include riscv X-SVN-Group: head X-SVN-Commit-Author: arichardson X-SVN-Commit-Paths: in head/sys/riscv: include riscv X-SVN-Commit-Revision: 361905 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Jun 2020 08:52:04 -0000 Author: arichardson Date: Mon Jun 8 08:52:02 2020 New Revision: 361905 URL: https://svnweb.freebsd.org/changeset/base/361905 Log: RISC-V: Check that the DTB doesn't overlap with kernel This can happen with very large kernels (e.g. ones embedding a root filesystem). The DTB written by OpenSBI/BBL is quite small so this is unlikely to hit important data, but if it does this can result in very confusing and hard-to-debug crashes. Add a KASSERT() and a verbose print to catch this problem with debug kernels. While this will not print any output by default if it fails (that would depend on EARLY_PRINTF), at least the kernel now halts reliably instead of randomly crashing. Reviewed By: mhorne Differential Revision: https://reviews.freebsd.org/D25153 Modified: head/sys/riscv/include/machdep.h head/sys/riscv/riscv/genassym.c head/sys/riscv/riscv/locore.S head/sys/riscv/riscv/machdep.c Modified: head/sys/riscv/include/machdep.h ============================================================================== --- head/sys/riscv/include/machdep.h Mon Jun 8 08:51:57 2020 (r361904) +++ head/sys/riscv/include/machdep.h Mon Jun 8 08:52:02 2020 (r361905) @@ -42,6 +42,7 @@ struct riscv_bootparams { vm_offset_t kern_phys; /* Kernel base (physical) addr */ vm_offset_t kern_stack; vm_offset_t dtbp_virt; /* Device tree blob virtual addr */ + vm_offset_t dtbp_phys; /* Device tree blob physical addr */ }; extern vm_paddr_t physmap[PHYS_AVAIL_ENTRIES]; Modified: head/sys/riscv/riscv/genassym.c ============================================================================== --- head/sys/riscv/riscv/genassym.c Mon Jun 8 08:51:57 2020 (r361904) +++ head/sys/riscv/riscv/genassym.c Mon Jun 8 08:52:02 2020 (r361905) @@ -106,3 +106,4 @@ ASSYM(RISCV_BOOTPARAMS_KERN_PHYS, offsetof(struct risc ASSYM(RISCV_BOOTPARAMS_KERN_STACK, offsetof(struct riscv_bootparams, kern_stack)); ASSYM(RISCV_BOOTPARAMS_DTBP_VIRT, offsetof(struct riscv_bootparams, dtbp_virt)); +ASSYM(RISCV_BOOTPARAMS_DTBP_PHYS, offsetof(struct riscv_bootparams, dtbp_phys)); Modified: head/sys/riscv/riscv/locore.S ============================================================================== --- head/sys/riscv/riscv/locore.S Mon Jun 8 08:51:57 2020 (r361904) +++ head/sys/riscv/riscv/locore.S Mon Jun 8 08:52:02 2020 (r361905) @@ -221,6 +221,7 @@ va: and t1, a1, t1 add t0, t0, t1 sd t0, RISCV_BOOTPARAMS_DTBP_VIRT(sp) + sd a1, RISCV_BOOTPARAMS_DTBP_PHYS(sp) mv a0, sp call _C_LABEL(initriscv) /* Off we go */ Modified: head/sys/riscv/riscv/machdep.c ============================================================================== --- head/sys/riscv/riscv/machdep.c Mon Jun 8 08:51:57 2020 (r361904) +++ head/sys/riscv/riscv/machdep.c Mon Jun 8 08:52:02 2020 (r361905) @@ -776,8 +776,19 @@ fake_preload_metadata(struct riscv_bootparams *rvbp) PRELOAD_PUSH_VALUE(uint32_t, 0); preload_metadata = (caddr_t)fake_preload; + /* Check if bootloader clobbered part of the kernel with the DTB. */ + KASSERT(rvbp->dtbp_phys + dtb_size <= rvbp->kern_phys || + rvbp->dtbp_phys >= rvbp->kern_phys + (lastaddr - KERNBASE), + ("FDT (%lx-%lx) and kernel (%lx-%lx) overlap", rvbp->dtbp_phys, + rvbp->dtbp_phys + dtb_size, rvbp->kern_phys, + rvbp->kern_phys + (lastaddr - KERNBASE))); KASSERT(fake_size < sizeof(fake_preload), ("Too many fake_preload items")); + + if (boothowto & RB_VERBOSE) + printf("FDT phys (%lx-%lx), kernel phys (%lx-%lx)\n", + rvbp->dtbp_phys, rvbp->dtbp_phys + dtb_size, + rvbp->kern_phys, rvbp->kern_phys + (lastaddr - KERNBASE)); return (lastaddr); }