From db89fd247a2cb976e1f3fbe1ff5e714375e01bd2 Mon Sep 17 00:00:00 2001
From: Chris Johns <chrisj@rtems.org>
Date: Wed, 21 Jul 2021 17:59:40 +1000
Subject: [PATCH rtems-libbsd v4 12/19] kern: Add kernel trace support (KTR)

Update #4475
---
 README.md                                  |  9 +++++++++
 freebsd/sys/kern/kern_intr.c               |  6 ++----
 freebsd/sys/kern/kern_ktr.c                | 14 +++++++++++---
 freebsd/sys/kern/kern_synch.c              | 10 ++--------
 freebsd/sys/kern/subr_prf.c                |  6 ++++++
 freebsd/sys/sys/systm.h                    | 17 ++++++++++++++++-
 freebsd/sys/vm/uma_core.c                  |  8 ++++++++
 libbsd.py                                  |  2 ++
 rtemsbsd/include/rtems/bsd/local/opt_alq.h |  0
 rtemsbsd/include/rtems/bsd/local/opt_ktr.h |  0
 rtemsbsd/rtems/rtems-kernel-init.c         |  7 +++++++
 rtemsbsd/rtems/rtems-kernel-thread.c       |  3 +++
 12 files changed, 66 insertions(+), 16 deletions(-)
 create mode 100644 rtemsbsd/include/rtems/bsd/local/opt_alq.h
 create mode 100644 rtemsbsd/include/rtems/bsd/local/opt_ktr.h

diff --git a/README.md b/README.md
index 55eac1d1..703e35b8 100644
--- a/README.md
+++ b/README.md
@@ -222,6 +222,15 @@ configure with:
 The LibBSD Waf support splits the options and converts them to uppercase and
 adds them -D options on the compiler command line.
 
+The list is:
+
+ bootverbose:     Verbose boot of the kernel
+ verbose_sysinit: Verbose printing of all the SYSINIT calls
+ bus_debug:       Bus debugging support
+ ktr:             Kernel trace
+ ktr_verbose:     Verbose kernel trace
+ invariants:      Invariants build of the kernel
+
 SMP Requirements
 ----------------
 
diff --git a/freebsd/sys/kern/kern_intr.c b/freebsd/sys/kern/kern_intr.c
index 30cb5d64..88ea9e63 100644
--- a/freebsd/sys/kern/kern_intr.c
+++ b/freebsd/sys/kern/kern_intr.c
@@ -189,7 +189,9 @@ ithread_update(struct intr_thread *ithd)
 	_Thread_Set_name(td->td_thread, ie->ie_fullname);
 #endif /* __rtems__ */
 #ifdef KTR
+#ifndef __rtems__
 	sched_clear_tdname(td);
+#endif /* __rtems__ */
 #endif
 	thread_lock(td);
 #ifndef __rtems__
@@ -1242,11 +1244,7 @@ ithread_loop(void *arg)
 	int wake;
 
 	td = curthread;
-#ifndef __rtems__
 	p = td->td_proc;
-#else /* __rtems__ */
-	p = NULL;
-#endif /* __rtems__ */
 	ithd = (struct intr_thread *)arg;
 	KASSERT(ithd->it_thread == td,
 	    ("%s: ithread and proc linkage out of sync", __func__));
diff --git a/freebsd/sys/kern/kern_ktr.c b/freebsd/sys/kern/kern_ktr.c
index bf925d47..cf5c98a4 100644
--- a/freebsd/sys/kern/kern_ktr.c
+++ b/freebsd/sys/kern/kern_ktr.c
@@ -1,3 +1,5 @@
+#include <machine/rtems-bsd-kernel-space.h>
+
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  *
@@ -34,13 +36,15 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include "opt_ddb.h"
-#include "opt_ktr.h"
-#include "opt_alq.h"
+#include <rtems/bsd/local/opt_ddb.h>
+#include <rtems/bsd/local/opt_ktr.h>
+#include <rtems/bsd/local/opt_alq.h>
 
 #include <sys/param.h>
 #include <sys/queue.h>
+#ifndef __rtems__
 #include <sys/alq.h>
+#endif /* __rtems__ */
 #include <sys/cons.h>
 #include <sys/cpuset.h>
 #include <sys/kdb.h>
@@ -111,6 +115,7 @@ SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD,
 SYSCTL_UQUAD(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD,
     &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel");
 
+#ifndef __rtems__
 static int
 sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS)
 {
@@ -133,6 +138,7 @@ SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask,
     CTLFLAG_RWTUN | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0,
     sysctl_debug_ktr_cpumask, "S",
     "Bitmask of CPUs on which KTR logging is enabled");
+#endif /* __rtems__ */
 
 static int
 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
@@ -221,11 +227,13 @@ sysctl_debug_ktr_entries(SYSCTL_HANDLER_ARGS)
 	/* Disable ktr temporarily. */
 	mask = ktr_mask;
 	ktr_mask = 0;
+#ifndef __rtems__
 	/* Wait for threads to go idle. */
 	if ((error = quiesce_all_cpus("ktrent", PCATCH)) != 0) {
 		ktr_mask = mask;
 		return (error);
 	}
+#endif /* __rtems__ */
 	if (ktr_buf != ktr_buf_init)
 		oldbuf = ktr_buf;
 	else
diff --git a/freebsd/sys/kern/kern_synch.c b/freebsd/sys/kern/kern_synch.c
index 49555858..5f30821f 100644
--- a/freebsd/sys/kern/kern_synch.c
+++ b/freebsd/sys/kern/kern_synch.c
@@ -138,9 +138,7 @@ int
 _sleep(void *ident, struct lock_object *lock, int priority,
     const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags)
 {
-#ifndef __rtems__
 	struct thread *td;
-#endif /* __rtems__ */
 	struct lock_class *class;
 	uintptr_t lock_state;
 #ifndef __rtems__
@@ -150,9 +148,7 @@ _sleep(void *ident, struct lock_object *lock, int priority,
 #endif /* __rtems__ */
 	WITNESS_SAVE_DECL(lock_witness);
 
-#ifndef __rtems__
 	td = curthread;
-#endif /* __rtems__ */
 #ifdef KTRACE
 	if (KTRPOINT(td, KTR_CSW))
 		ktrcsw(1, 0, wmesg);
@@ -165,10 +161,10 @@ _sleep(void *ident, struct lock_object *lock, int priority,
 #ifndef __rtems__
 	KASSERT(TD_IS_RUNNING(td), ("_sleep: curthread not running"));
 	KASSERT(td->td_epochnest == 0, ("sleeping in an epoch section"));
+#endif /* __rtems__ */
 	if (priority & PDROP)
 		KASSERT(lock != NULL && lock != &Giant.lock_object,
 		    ("PDROP requires a non-Giant lock"));
-#endif /* __rtems__ */
 	if (lock != NULL)
 		class = LOCK_CLASS(lock);
 	else
@@ -181,10 +177,8 @@ _sleep(void *ident, struct lock_object *lock, int priority,
 		return (0);
 	}
 	catch = priority & PCATCH;
-	pri = priority & PRIMASK;
-#else /* __rtems__ */
-	pri = priority;
 #endif /* __rtems__ */
+	pri = priority & PRIMASK;
 
 #ifndef __rtems__
 	KASSERT(!TD_ON_SLEEPQ(td), ("recursive sleep"));
diff --git a/freebsd/sys/kern/subr_prf.c b/freebsd/sys/kern/subr_prf.c
index 702fbe4d..63e12d7c 100644
--- a/freebsd/sys/kern/subr_prf.c
+++ b/freebsd/sys/kern/subr_prf.c
@@ -218,6 +218,7 @@ uprintf(const char *fmt, ...)
 	va_end(ap);
 	return (retval);
 }
+#endif /* __rtems__ */
 
 /*
  * tprintf and vtprintf print on the controlling terminal associated with the
@@ -236,6 +237,7 @@ tprintf(struct proc *p, int pri, const char *fmt, ...)
 void
 vtprintf(struct proc *p, int pri, const char *fmt, va_list ap)
 {
+#ifndef __rtems__
 	struct tty *tp = NULL;
 	int flags = 0;
 	struct putchar_arg pca;
@@ -271,8 +273,12 @@ vtprintf(struct proc *p, int pri, const char *fmt, va_list ap)
 	if (sess != NULL)
 		sess_release(sess);
 	msgbuftrigger = 1;
+#else /* __rtems__ */
+	vprintf(fmt, ap);
+#endif /* __rtems__ */
 }
 
+#ifndef __rtems__
 /*
  * Ttyprintf displays a message on a tty; it should be used only by
  * the tty driver, or anything that knows the underlying tty will not
diff --git a/freebsd/sys/sys/systm.h b/freebsd/sys/sys/systm.h
index aae31704..29320f93 100644
--- a/freebsd/sys/sys/systm.h
+++ b/freebsd/sys/sys/systm.h
@@ -83,7 +83,8 @@ extern int boothowto;		/* reboot flags, from console subsystem */
 extern int bootverbose;		/* nonzero to print verbose messages */
 #else /* __rtems__ */
 #ifdef BOOTVERBOSE
-extern int bootverbose;		/* nonzero to print verbose messages */
+extern int rtems_bsd_bootverbose; /* nonzero to print verbose messages */
+#define bootverbose rtems_bsd_bootverbose
 #else
 #define bootverbose    0        /* Remove all verbose code for the standard RTEMS build */
 #endif /* BOOTVERBOSE */
@@ -407,6 +408,20 @@ int	copyout_nofault(const void * _Nonnull __restrict kaddr,
 	    void * __restrict udaddr, size_t len);
 
 #else /* __rtems__ */
+static inline int
+copystr(const void * _Nonnull __restrict kfaddr,
+	    void * _Nonnull __restrict kdaddr, size_t len,
+	    size_t * __restrict lencopied)
+{
+	if (lencopied != NULL) {
+		*lencopied = len;
+	}
+
+	memcpy(kdaddr, kfaddr, len);
+
+	return (0);
+}
+
 static inline int
 copyinstr(const void * __restrict udaddr, void * __restrict kaddr,
 	    size_t len, size_t * __restrict lencopied)
diff --git a/freebsd/sys/vm/uma_core.c b/freebsd/sys/vm/uma_core.c
index 8c3a84b4..0efdb12f 100644
--- a/freebsd/sys/vm/uma_core.c
+++ b/freebsd/sys/vm/uma_core.c
@@ -2609,8 +2609,10 @@ uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
 
 	/* This is the fast path allocation */
+#ifndef __rtems__
 	CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d",
 	    curthread, zone->uz_name, zone, flags);
+#endif /* __rtems__ */
 
 	if (flags & M_WAITOK) {
 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
@@ -2698,9 +2700,11 @@ zalloc_start:
 	 */
 	bucket = cache->uc_freebucket;
 	if (bucket != NULL && bucket->ub_cnt > 0) {
+#ifndef __rtems__
 		CTR2(KTR_UMA,
 		    "uma_zalloc: zone %s(%p) swapping empty with alloc",
 		    zone->uz_name, zone);
+#endif /* __rtems__ */
 		cache->uc_freebucket = cache->uc_allocbucket;
 		cache->uc_allocbucket = bucket;
 		goto zalloc_start;
@@ -2784,8 +2788,10 @@ zalloc_start:
 	 * will use the just filled bucket.
 	 */
 	bucket = zone_alloc_bucket(zone, udata, domain, flags);
+#ifndef __rtems__
 	CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
 	    zone->uz_name, zone, bucket);
+#endif /* __rtems__ */
 	if (bucket != NULL) {
 		ZONE_LOCK(zone);
 		critical_enter();
@@ -2835,9 +2841,11 @@ uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
 	random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
 
 	/* This is the fast path allocation */
+#ifndef __rtems__
 	CTR5(KTR_UMA,
 	    "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d",
 	    curthread, zone->uz_name, zone, domain, flags);
+#endif /* __rtems__ */
 
 	if (flags & M_WAITOK) {
 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
diff --git a/libbsd.py b/libbsd.py
index e6d9bc00..c9b4e99c 100644
--- a/libbsd.py
+++ b/libbsd.py
@@ -195,6 +195,7 @@ class rtems(builder.Module):
                 'rtems/rtems-kernel-init.c',
                 'rtems/rtems-kernel-irqs.c',
                 'rtems/rtems-kernel-jail.c',
+                'rtems/rtems-kernel-lockmgr.c',
                 'rtems/rtems-kernel-malloc.c',
                 'rtems/rtems-kernel-mbuf.c',
                 'rtems/rtems-kernel-mtxpool.c',
@@ -497,6 +498,7 @@ class base(builder.Module):
                 'sys/kern/kern_event.c',
                 'sys/kern/kern_hhook.c',
                 'sys/kern/kern_intr.c',
+                'sys/kern/kern_ktr.c',
                 'sys/kern/kern_khelp.c',
                 'sys/kern/kern_linker.c',
                 'sys/kern/kern_mbuf.c',
diff --git a/rtemsbsd/include/rtems/bsd/local/opt_alq.h b/rtemsbsd/include/rtems/bsd/local/opt_alq.h
new file mode 100644
index 00000000..e69de29b
diff --git a/rtemsbsd/include/rtems/bsd/local/opt_ktr.h b/rtemsbsd/include/rtems/bsd/local/opt_ktr.h
new file mode 100644
index 00000000..e69de29b
diff --git a/rtemsbsd/rtems/rtems-kernel-init.c b/rtemsbsd/rtems/rtems-kernel-init.c
index f76e7cd7..eb958ede 100644
--- a/rtemsbsd/rtems/rtems-kernel-init.c
+++ b/rtemsbsd/rtems/rtems-kernel-init.c
@@ -44,6 +44,7 @@
 #include <sys/types.h>
 #include <sys/systm.h>
 #include <sys/kernel.h>
+#include <sys/ktr.h>
 #include <sys/sysctl.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
@@ -187,5 +188,11 @@ rtems_bsd_initialize(void)
 	mutex_init();
 	mi_startup();
 
+#ifdef KTR
+	ktr_verbose = 10;
+	ktr_mask = KTR_ALL;
+	ktr_mask = KTR_GEN | KTR_LOCK | KTR_VFS | KTR_VOP | KTR_BUF | KTR_MALLOC | KTR_SYSC | KTR_RUNQ;
+#endif
+
 	return RTEMS_SUCCESSFUL;
 }
diff --git a/rtemsbsd/rtems/rtems-kernel-thread.c b/rtemsbsd/rtems/rtems-kernel-thread.c
index 3e1e44b9..49ec6df7 100644
--- a/rtemsbsd/rtems/rtems-kernel-thread.c
+++ b/rtemsbsd/rtems/rtems-kernel-thread.c
@@ -260,6 +260,9 @@ rtems_bsd_thread_start(struct thread **td_ptr, void (*func)(void *), void *arg,
 		BSD_ASSERT(td != NULL);
 
 		_Thread_Set_name(thread, name);
+#ifdef KTR
+		strlcpy(td->td_name, name, sizeof(td->td_name));
+#endif
 
 		if (rtems_bsd_thread_ready_to_start) {
 			sc = rtems_task_start(task_id, (rtems_task_entry) func,
-- 
2.24.1

