From owner-freebsd-arm@freebsd.org Thu Feb 13 17:37:11 2020 Return-Path: Delivered-To: freebsd-arm@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 39E371CE2A4 for ; Thu, 13 Feb 2020 17:37:11 +0000 (UTC) (envelope-from marklmi@yahoo.com) Received: from sonic306-20.consmr.mail.gq1.yahoo.com (sonic306-20.consmr.mail.gq1.yahoo.com [98.137.68.83]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 48JNxK3xF5z4GHP for ; Thu, 13 Feb 2020 17:37:09 +0000 (UTC) (envelope-from marklmi@yahoo.com) X-YMail-OSG: U.fpZ_YVM1nWsBfcb83iKVGssJbSwUSPeOSsGgPD0eOudwZMLr9tXweTcmYWrQh X_fZSsOE.D9dR7dvwienK0Rk7Icyuv3WFJWfHij0fLwtL7ZPOBGSd0quERH1BDQsqUoyd3Qa4BJ9 27I8x0Z9crGvkaQxLStfYri0h0nzb07IpVHMti_pTSFBQ8FniWkjZlDEHEmguG7aLXgIj591D2dz wECIAl35qDiR1Ii.R0j..rnZlg9zpZnlU8fBYQ3ZVKCDCfqCGrASP5N28I6pCBPK3inlw1MIRDhv C8rTMvtFKebVBHDtH3A.0gTD1uFCA7C523YjIbKdQX5UXUeTQrAb1r_GleBy1AtL.vJXwImC4TVQ JvPgDWn3erCfwl1KYomDir4au2jC4__5qwpoc959W2Zua6zOV4nWQnMQHjqiBAAgyv0phS65GVFz OfD5EGBCODEsvmcpzsq2tDlYc6Sa9pZeMm1Mnrfp5w7kkaS1LRV95L5v_1r3O3mERlXYyv.WoYRW ZPrAA0zRJ_2HU.iiJB.oMX7tRVjG0LWDQweHBDMtZENRhmKxPBnIz6BEAbVZFGDqVwv5_U5jKQ32 pxA2eESDijteZ9cWB.u3N143Xve44PXwLksGaXq8qAWTdYmx0dx1.ISF3YiRXST45yOEsAbHKk2Q nsE9Zd_G7z2a.WYUJBhjbiNR92.KyBrU7pX6p5OwJ_nSKVRetIaMyW9sbG3NDqNGeSfIG0EhDbSF osBHRG8TiusGJncWWKWAoHG8vPHsa29DyYVQQHfch1BsHhbEyZ_WltsUvfjPVMgfgBigGRS7UnjT OvLz3k17CXL3y7spGpyh0g.KgAaRUePlyqV31BuxcJwFg3riWMKx3KhSr1TZAmCywFjoNcwZHiCl MqeE3HIuvM7kO4YLHMDbS6gS6U.eaqcqaRVdW6YRrhJHe0EfTgbr.3IUOKbRX17UTBNF.phFEBrM cLQ4qp2pWrVraOkUHuoPhelkNox4hm8h7lypwABOmgUzpnGx4C.U4BVYHMaz9TqFQcX_LfGTV0IM mwKcWqlmaJrVy5pO8TdncvI6CsGe1zll8F74kBqc9EWEuWmxb0Fo4ibGxgKNFRO71Piq8Np7lZzg q_Bwrg.RWehtEA09S46WShvGy74fhhyiYgOFnZJKRXlDAXlNlOamBz7xUx862APKKMbz5OUEQMsV ZjLZVWxyOj8TQHwlLp3mI1VASc.4Pl51n88ezv6UBexm4TVxyS5BGixBPIB5HKx__EgJXMXbz3vJ PM18maON1iavXSpFp8gUcEAMbO7ls7DEbXuEX9XICtAJb0R89znSuYZnhCY7yqm_iYH4FmGWr_sZ VYH.65kXyhmaYGsuz79NmnQHGF15a02pL6oZjV1CgyJoSzV0NDy5.2_72XuI716NnBRaIjyEZs.B 5vhoGI1KKYJ.zzSfEVd6yoyI17_KT34UzG78- Received: from sonic.gate.mail.ne1.yahoo.com by sonic306.consmr.mail.gq1.yahoo.com with HTTP; Thu, 13 Feb 2020 17:37:07 +0000 Received: by smtp432.mail.bf1.yahoo.com (Oath Hermes SMTP Server) with ESMTPA ID 0cda293c4329448304f88e5bf71e15df; Thu, 13 Feb 2020 17:37:01 +0000 (UTC) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.60.0.2.5\)) Subject: Re: A investigative hack that makes (for example) head -r356529 boot and operate normally an RPi4B (finally!): protect all armstub8-gic.bin's loaded content from replacement by the kernel From: Mark Millard In-Reply-To: Date: Thu, 13 Feb 2020 09:36:59 -0800 Cc: Ralf Wenk , Andrew Turner , Oleksandr Tymoshenko , freebsd-arm , Emmanuel Vadot Content-Transfer-Encoding: quoted-printable Message-Id: <36CF6E4B-5607-4752-B2DF-C265BCFB95BA@yahoo.com> References: <7E7605DC-021D-448A-8459-8EC26BA9836D.ref@yahoo.com> <7E7605DC-021D-448A-8459-8EC26BA9836D@yahoo.com> To: Kyle Evans X-Mailer: Apple Mail (2.3608.60.0.2.5) X-Rspamd-Queue-Id: 48JNxK3xF5z4GHP X-Spamd-Bar: -- X-Spamd-Result: default: False [-2.18 / 15.00]; R_SPF_ALLOW(-0.20)[+ptr:yahoo.com]; FREEMAIL_FROM(0.00)[yahoo.com]; MV_CASE(0.50)[]; RCPT_COUNT_FIVE(0.00)[6]; TO_DN_ALL(0.00)[]; DKIM_TRACE(0.00)[yahoo.com:+]; SUBJECT_HAS_EXCLAIM(0.00)[]; DMARC_POLICY_ALLOW(-0.50)[yahoo.com,reject]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_LAST(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[yahoo.com]; ASN(0.00)[asn:36647, ipnet:98.137.64.0/21, country:US]; MID_RHS_MATCH_FROM(0.00)[]; SH_EMAIL_ZRD(0.00)[0.0.0.2,0.0.0.0,0.0.0.1,0.0.0.3]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-0.77)[-0.767,0]; R_DKIM_ALLOW(-0.20)[yahoo.com:s=s2048]; FROM_HAS_DN(0.00)[]; SH_EMAIL_DBL_DONT_QUERY_IPS(0.00)[0.0.0.0,0.0.0.3,0.0.0.2,0.0.0.1]; NEURAL_HAM_LONG(-0.91)[-0.909,0]; MIME_GOOD(-0.10)[text/plain]; DWL_DNSWL_NONE(0.00)[yahoo.com.dwl.dnswl.org : 127.0.5.0]; IP_SCORE_FREEMAIL(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[83.68.137.98.list.dnswl.org : 127.0.5.0]; IP_SCORE(0.00)[ip: (2.92), ipnet: 98.137.64.0/21(0.82), asn: 36647(0.66), country: US(-0.05)]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-arm@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Porting FreeBSD to ARM processors." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Feb 2020 17:37:11 -0000 On 2020-Feb-13, at 08:50, Mark Millard wrote: > On 2020-Feb-13, at 07:22, Kyle Evans wrote: >=20 >> On Thu, Feb 13, 2020 at 9:05 AM Ralf Wenk = wrote: >>>=20 >>> On 2020-02-13 at 15:26 +0100 Ralf Wenk wrote: >>>> On 2020-02-13 at 7:49 -0600 Kyle Evans wrote: >>>>> On Thu, Feb 13, 2020 at 7:43 AM Ralf Wenk = wrote: >>>>>>=20 >>>>>> On 2020-02-12 at 18:00 -0800 Mark Millard wrote via freebsd-arm: >>>>>>> [...] >>>>>>>=20 >>>>>>> # svnlite diff /usr/src/sys/dev/fdt/fdt_common.c >>>>>>> Index: /usr/src/sys/dev/fdt/fdt_common.c >>>>>>> = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >>>>>>> --- /usr/src/sys/dev/fdt/fdt_common.c (revision 357529) >>>>>>> +++ /usr/src/sys/dev/fdt/fdt_common.c (working copy) >>>>>>> @@ -485,7 +485,18 @@ >>>>>>>=20 >>>>>>> tuples =3D res_len / tuple_size; >>>>>>> reservep =3D (pcell_t *)&reserve; >>>>>>> +#ifdef __aarch64__ >>>>>>> + //HACK!!! >>>>>>> + // Reserve the first few pages, for example to >>>>>>> + // preserve armstub8-gic.bin or armstub.bin >>>>>>> + // content. >>>>>>> + mr[0].mr_start=3D 0; >>>>>>> + mr[0].mr_size=3D 2*4096; >>>>>>> + tuples++; >>>>>>> + for (i =3D 1; i < tuples; i++) { >>>>>>> +#else >>>>>>> for (i =3D 0; i < tuples; i++) { >>>>>>> +#endif >>>>>>>=20 >>>>>>> rv =3D fdt_data_to_res(reservep, addr_cells, = size_cells, >>>>>>> (u_long *)&mr[i].mr_start, (u_long = *)&mr[i].mr_size); >>>>>>> @@ -512,6 +523,11 @@ >>>>>>>=20 >>>>>>> root =3D OF_finddevice("/reserved-memory"); >>>>>>> if (root =3D=3D -1) { >>>>>>> + // Fail over to checking for and handling = memreserve, >>>>>>> + // such as for a RPi4B. >>>>>>> + if (0 =3D=3D = fdt_get_reserved_regions(reserved,mreserved)) >>>>>>> + return (0); >>>>>>> + >>>>>>> return (ENXIO); >>>>>>> } >>>>>>>=20 >>>>>>=20 >>>>>> I can confirm that with your patch(es) my RPi3 does not freeze = any more >>>>>> when loading mac_ntpd.ko. The patches are applied against = r357853M. >>>=20 >>> An reboot is working again too. >>>=20 >>>>> Have you tested the RPi3 with just this second hunk of patch to >>>>> fallover to memreserve, or is the first hunk definitely required = as >>>>> well? >>>>=20 >>>> Good question. I tested both hunks together. >>>> Will try what happens when just applying the second and report = back. >>>=20 >>> Here it is: >>> Without the first hunk the system freezes again when loading = mac_ntpd.ko. >>> Also the CPU information during boot for CPUs 1 to 3 looks strange = again. >>>=20 >>=20 >> Yeah- I see it now; both armstubs are about 5k. I've raised an >> issue[0] with upstream for armstub/rpi bits to work out the proper >> solution, because I don't necessarily want to commit the workaround. >> I'll throw up the second hunk on phabricator for review by = #arm/#arm64 >> folks, because that seems to me the proper fallback. >>=20 >> I also discovered some issues when trying to read /memreserve/ with >> our dtc and filed a PR[1] to fix those. >>=20 >> Thanks, >>=20 >> Kyle Evans >>=20 >> [0] https://github.com/raspberrypi/tools/issues/107 >> [1] https://github.com/davidchisnall/dtc/pull/59 >=20 > The DTB information below is from: >=20 > U-Boot> fdt addr 0x7ef2000=20 > U-Boot> fdt print / =20 >=20 > on a RPi4B 4 GiByte. >=20 > On at least the RPi4B memreserve is not what causes > the first page to be excluded: >=20 > memreserve =3D <0x3b400000 0x04c00000>; >=20 > Nor is memory@0 the cause: >=20 > memory@0 { > device_type =3D "memory"; > reg =3D <0x00000000 0x00000000 0x3b400000 0x00000000 = 0x40000000 0xbc000000>; > }; >=20 > (That also skips the memreserve area.) >=20 > I do not find anything in the DTB that indicates > to exclude the first page. >=20 > My hypothesis is that the FreeBSD code excludes > the page based on some less obvious relationship > that I'm not identifying. >=20 > There is the cpu-rlease-addr information that seems > to refer to some 1st memory page content: >=20 > cpus { > #address-cells =3D <0x00000001>; > #size-cells =3D <0x00000000>; > enable-method =3D "brcm,bcm2836-smp"; > phandle =3D <0x000000be>; > cpu@0 { > device_type =3D "cpu"; > compatible =3D "arm,cortex-a72"; > reg =3D <0x00000000>; > enable-method =3D "spin-table"; > cpu-release-addr =3D <0x00000000 0x000000d8>; > phandle =3D <0x0000001d>; > }; > cpu@1 { > device_type =3D "cpu"; > compatible =3D "arm,cortex-a72"; > reg =3D <0x00000001>; > enable-method =3D "spin-table"; > cpu-release-addr =3D <0x00000000 0x000000e0>; > phandle =3D <0x0000001e>; > }; > cpu@2 { > device_type =3D "cpu"; > compatible =3D "arm,cortex-a72"; > reg =3D <0x00000002>; > enable-method =3D "spin-table"; > cpu-release-addr =3D <0x00000000 0x000000e8>; > phandle =3D <0x0000001f>; > }; > cpu@3 { > device_type =3D "cpu"; > compatible =3D "arm,cortex-a72"; > reg =3D <0x00000003>; > enable-method =3D "spin-table"; > cpu-release-addr =3D <0x00000000 0x000000f0>; > phandle =3D <0x00000020>; > }; > }; Looking at the code there is: /* Load the physical memory ranges */ efihdr =3D (struct efi_map_header *)preload_search_info(kmdp, MODINFO_METADATA | MODINFOMD_EFI_MAP); if (efihdr !=3D NULL) add_efi_map_entries(efihdr); #ifdef FDT else { /* Grab physical memory regions information from device = tree. */ if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) !=3D 0) panic("Cannot get physical memory regions"); arm_physmem_hardware_regions(mem_regions, = mem_regions_sz); } if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) =3D=3D 0) arm_physmem_exclude_regions(mem_regions, mem_regions_sz, EXFLAG_NODUMP | EXFLAG_NOALLOC); #endif /* Exclude the EFI framebuffer from our view of physical memory. = */ efifb =3D (struct efi_fb *)preload_search_info(kmdp, MODINFO_METADATA | MODINFOMD_EFI_FB); if (efifb !=3D NULL) arm_physmem_exclude_region(efifb->fb_addr, = efifb->fb_size, EXFLAG_NOALLOC); . . . if (boothowto & RB_VERBOSE) { print_efi_map_entries(efihdr); arm_physmem_print_tables(); } It looks to me like the boot -v text: Type Physical Virtual #Pages Attr Reserved 000000000000 0 00000001 WB=20 ConventionalMemory 000000001000 1000 00007ef1 WB=20 BootServicesData 000007ef2000 7ef2000 0000001c WB=20 ConventionalMemory 000007f0e000 7f0e000 00029f93 WB=20 BootServicesData 000031ea1000 31ea1000 00000001 WB=20 LoaderData 000031ea2000 31ea2000 00008001 WB=20 LoaderCode 000039ea3000 39ea3000 000000a6 WB=20 Reserved 000039f49000 39f49000 00000007 WB=20 BootServicesData 000039f50000 39f50000 00000001 WB=20 Reserved 000039f51000 39f51000 00000002 WB=20 RuntimeServicesData 000039f53000 39f53000 00000001 WB RUNTIME Reserved 000039f54000 39f54000 00000001 WB=20 BootServicesData 000039f55000 39f55000 00000002 WB=20 RuntimeServicesData 000039f57000 39f57000 00000001 WB RUNTIME LoaderData 000039f58000 39f58000 00001408 WB=20 RuntimeServicesCode 00003b360000 3b360000 00000010 WB RUNTIME LoaderData 00003b370000 3b370000 00000090 WB=20 BootServicesData 000040000000 40000000 000bc000 WB=20 MemoryMappedIO 0000fe100000 fe100000 00000001 RUNTIME is from print_efi_map_entries via the efihdr instead of from the FreeBSD FDT code and the DTB. So is it u-boot that provides the efihdr for which add_efi_map_entries generated those regions? That might explain why I do not find matching DTB material for all of it. =3D=3D=3D Mark Millard marklmi at yahoo.com ( dsl-only.net went away in early 2018-Mar)