From nobody Tue Jan 04 10:50:21 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 746C21936E95; Tue, 4 Jan 2022 10:50:24 +0000 (UTC) (envelope-from git@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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JSqC30YTVz4RVN; Tue, 4 Jan 2022 10:50:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id BFFA4176C2; Tue, 4 Jan 2022 10:50:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 204AoLmu045233; Tue, 4 Jan 2022 10:50:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 204AoLI8045227; Tue, 4 Jan 2022 10:50:21 GMT (envelope-from git) Date: Tue, 4 Jan 2022 10:50:21 GMT Message-Id: <202201041050.204AoLI8045227@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Andrew Turner Subject: git: 7b7f391db4f0 - stable/13 - Allow ddb and dtrace use the DMAP region on arm64 List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: andrew X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 7b7f391db4f087f064c457ee80e765a0284c58e2 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1641293423; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=cmju/gQxMlgW+4rDObvOp4IAI4AlNHp+ZYzaOIYNkEY=; b=j6aQCnyEakwwLmsiWoneAOS/UpYB5KDQwnhsCh1GjhZPkk2iVLGcSt19kaSDqqwYvJ3Oy4 LGumarMdVouUDR3VNZGYxerJXsDzCrDb2gEFpjeLOr/og1QIl4iLHoNX5HQN20QH6Ek4Qa CJzKPjdDibaFTmfYihZQYHnyUbDZvE/0NiArn9kVbERMLaUlw9kh4mXLd49iPm2kus6hIO oCcl/ss7ST4N6O5T43MZnt343MOPQsBB4Js+UbVDc2yZ4Rgs5+Bv642nddUORVv+4lh5b4 wYabMK1XTgUWg5BF/oH+ECbqJwmJBzFPBaBr3MImHxtoL8Dbga7h7oriK/fh2g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1641293423; a=rsa-sha256; cv=none; b=sD+m4VUjhTcZisiAqffzcAVgm8UYkjzVCRaVjFKLjdEGvsNUAoTkX5L1KefApaZTNOkHrM vi0XWeNdG8+oPu5MVkWVPD8Rr7Xw33UDhz+UNeeWeSfc7kF9oQ4qkuEML59SaQTRXgf3xB 5Ca8fypPjtGLQrgHM0VT2cDdSBGN1utJdIIiiuIumkQkdv4Zty6PoTBvwlPscznIKdLlad pXrmjtUycDDtdjnzPvo6QzlldTj+rMfigoMPgT8/xL77Ko3W6+uFfe9RTexYErR7O9Hp40 QAVv3O5FKsrGqlH0TyYCcRjHblja1bhUhp3pAjwEL4UU4mD8+7VQO1rW7Mj5oA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by andrew: URL: https://cgit.FreeBSD.org/src/commit/?id=7b7f391db4f087f064c457ee80e765a0284c58e2 commit 7b7f391db4f087f064c457ee80e765a0284c58e2 Author: Andrew Turner AuthorDate: 2021-09-21 17:10:57 +0000 Commit: Andrew Turner CommitDate: 2022-01-04 10:49:51 +0000 Allow ddb and dtrace use the DMAP region on arm64 When writing to memory on arm64 we may be trying to be accessing a read-only page. In this case try to access via the DMAP region to get a writable location. While here simplify writing data in DDB and stop trashing the size as it is passed into the cache handling functions. Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D32053 (cherry picked from commit 3d2533f5c29fbf6e63c5e408ba13c2294a7612fd) --- sys/arm64/arm64/db_interface.c | 33 +++++++++------------------------ sys/arm64/arm64/machdep.c | 36 ++++++++++++++++++++++++++++++++++++ sys/arm64/include/cpufunc.h | 1 + sys/cddl/dev/fbt/aarch64/fbt_isa.c | 6 +++++- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/sys/arm64/arm64/db_interface.c b/sys/arm64/arm64/db_interface.c index 5138bf3f1cab..289886658905 100644 --- a/sys/arm64/arm64/db_interface.c +++ b/sys/arm64/arm64/db_interface.c @@ -154,39 +154,24 @@ db_write_bytes(vm_offset_t addr, size_t size, char *data) jmp_buf jb; void *prev_jb; char *dst; + size_t i; int ret; - uint64_t tmp64; - uint32_t tmp32; - uint16_t tmp16; prev_jb = kdb_jmpbuf(jb); ret = setjmp(jb); if (ret == 0) { - if (size == 8 && (addr & 7) == 0) { - dst = (char *)&tmp64; - while (size-- > 0) - *dst++ = *data++; - *((uint64_t *)addr) = tmp64; - } else if (size == 4 && (addr & 3) == 0) { - dst = (char *)&tmp32; - while (size-- > 0) - *dst++ = *data++; - *((uint32_t *)addr) = tmp32; - } else if (size == 2 && (addr & 1) == 0) { - dst = (char *)&tmp16; - while (size-- > 0) - *dst++ = *data++; - *((uint32_t *)addr) = tmp16; + if (!arm64_get_writable_addr(addr, &addr)) { + ret = 1; } else { dst = (char *)addr; - while (size-- > 0) + for (i = 0; i < size; i++) *dst++ = *data++; - } - dsb(ish); + dsb(ish); - /* Clean D-cache and invalidate I-cache */ - cpu_dcache_wb_range(addr, (vm_size_t)size); - cpu_icache_sync_range(addr, (vm_size_t)size); + /* Clean D-cache and invalidate I-cache */ + cpu_dcache_wb_range(addr, (vm_size_t)size); + cpu_icache_sync_range(addr, (vm_size_t)size); + } } (void)kdb_jmpbuf(prev_jb); diff --git a/sys/arm64/arm64/machdep.c b/sys/arm64/arm64/machdep.c index 67f6f66241f8..9ade7dde99fa 100644 --- a/sys/arm64/arm64/machdep.c +++ b/sys/arm64/arm64/machdep.c @@ -383,6 +383,42 @@ init_proc0(vm_offset_t kstack) serror_enable(); } +/* + * Get an address to be used to write to kernel data that may be mapped + * read-only, e.g. to patch kernel code. + */ +bool +arm64_get_writable_addr(vm_offset_t addr, vm_offset_t *out) +{ + vm_paddr_t pa; + + /* Check if the page is writable */ + if (PAR_SUCCESS(arm64_address_translate_s1e1w(addr))) { + *out = addr; + return (true); + } + + /* + * Find the physical address of the given page. + */ + if (!pmap_klookup(addr, &pa)) { + return (false); + } + + /* + * If it is within the DMAP region and is writable use that. + */ + if (PHYS_IN_DMAP(pa)) { + addr = PHYS_TO_DMAP(pa); + if (PAR_SUCCESS(arm64_address_translate_s1e1w(addr))) { + *out = addr; + return (true); + } + } + + return (false); +} + typedef struct { uint32_t type; uint64_t phys_start; diff --git a/sys/arm64/include/cpufunc.h b/sys/arm64/include/cpufunc.h index 94af62380de3..16133903841f 100644 --- a/sys/arm64/include/cpufunc.h +++ b/sys/arm64/include/cpufunc.h @@ -246,6 +246,7 @@ int arm64_icache_sync_range_checked(vm_offset_t, vm_size_t); void arm64_dcache_wbinv_range(vm_offset_t, vm_size_t); void arm64_dcache_inv_range(vm_offset_t, vm_size_t); void arm64_dcache_wb_range(vm_offset_t, vm_size_t); +bool arm64_get_writable_addr(vm_offset_t, vm_offset_t *); #endif /* _KERNEL */ #endif /* _MACHINE_CPUFUNC_H_ */ diff --git a/sys/cddl/dev/fbt/aarch64/fbt_isa.c b/sys/cddl/dev/fbt/aarch64/fbt_isa.c index 6a21c7f403de..adc355e3296b 100644 --- a/sys/cddl/dev/fbt/aarch64/fbt_isa.c +++ b/sys/cddl/dev/fbt/aarch64/fbt_isa.c @@ -79,8 +79,12 @@ fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval) void fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val) { + vm_offset_t addr; - *fbt->fbtp_patchpoint = val; + if (!arm64_get_writable_addr((vm_offset_t)fbt->fbtp_patchpoint, &addr)) + panic("%s: Unable to write new instruction", __func__); + + *(fbt_patchval_t *)addr = val; cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4); }