git: 2555f175b368 - main - Move kstack_contains() and GET_STACK_USAGE() to MD machine/stack.h

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 01 Feb 2023 23:41:33 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=2555f175b368569dd98e1bd2b6bd095c933faed7

commit 2555f175b368569dd98e1bd2b6bd095c933faed7
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-01-31 22:47:40 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-02-01 22:59:26 +0000

    Move kstack_contains() and GET_STACK_USAGE() to MD machine/stack.h
    
    Reviewed by:    jhb
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D38320
---
 sys/amd64/include/proc.h            |  9 ---------
 sys/amd64/include/stack.h           | 24 ++++++++++++++++++++++++
 sys/arm/arm/ptrace_machdep.c        |  1 +
 sys/arm/include/proc.h              | 11 -----------
 sys/arm/include/stack.h             | 19 +++++++++++++++++++
 sys/arm64/arm64/debug_monitor.c     |  1 +
 sys/arm64/arm64/elf32_machdep.c     |  1 +
 sys/arm64/arm64/freebsd32_machdep.c |  1 +
 sys/arm64/arm64/ptrace_machdep.c    |  1 +
 sys/arm64/include/proc.h            | 12 ------------
 sys/arm64/include/stack.h           | 18 ++++++++++++++++++
 sys/arm64/linux/linux_sysvec.c      |  2 +-
 sys/ddb/db_ps.c                     |  2 ++
 sys/geom/geom_io.c                  |  1 +
 sys/i386/include/proc.h             |  7 -------
 sys/i386/include/stack.h            | 23 +++++++++++++++++++++++
 sys/kern/subr_epoch.c               |  2 ++
 sys/netgraph/ng_base.c              |  2 ++
 sys/powerpc/include/proc.h          | 12 ------------
 sys/powerpc/include/stack.h         | 19 +++++++++++++++++++
 sys/riscv/include/proc.h            | 11 -----------
 sys/riscv/include/stack.h           | 19 +++++++++++++++++++
 sys/sys/proc.h                      |  7 -------
 sys/x86/x86/stack_machdep.c         |  2 +-
 24 files changed, 136 insertions(+), 71 deletions(-)

diff --git a/sys/amd64/include/proc.h b/sys/amd64/include/proc.h
index 6181df35261e..8015fe5da81a 100644
--- a/sys/amd64/include/proc.h
+++ b/sys/amd64/include/proc.h
@@ -97,15 +97,6 @@ struct mdproc {
 
 #ifdef	_KERNEL
 
-/* Get the current kernel thread stack usage. */
-#define GET_STACK_USAGE(total, used) do {				\
-	struct thread	*td = curthread;				\
-	(total) = td->td_kstack_pages * PAGE_SIZE;			\
-	(used) = (char *)td->td_kstack +				\
-	    td->td_kstack_pages * PAGE_SIZE -				\
-	    (char *)&td;						\
-} while (0)
-
 struct proc_ldt *user_ldt_alloc(struct proc *, int);
 void user_ldt_free(struct thread *);
 struct sysarch_args;
diff --git a/sys/amd64/include/stack.h b/sys/amd64/include/stack.h
index 091ae33893d4..ff21ee28b5a3 100644
--- a/sys/amd64/include/stack.h
+++ b/sys/amd64/include/stack.h
@@ -3,4 +3,28 @@
  */
 /* $FreeBSD$ */
 
+#ifndef _MACHINE_STACK_H_
+#define	_MACHINE_STACK_H_
+
 #include <x86/stack.h>
+
+#ifdef _SYS_PROC_H_
+
+/* Get the current kernel thread stack usage. */
+#define GET_STACK_USAGE(total, used) do {				\
+	struct thread	*td = curthread;				\
+	(total) = td->td_kstack_pages * PAGE_SIZE;			\
+	(used) = (char *)td->td_kstack +				\
+	    td->td_kstack_pages * PAGE_SIZE -				\
+	    (char *)&td;						\
+} while (0)
+
+static __inline bool
+kstack_contains(struct thread *td, vm_offset_t va, size_t len)
+{
+	return (va >= td->td_kstack && va + len >= va &&
+	    va + len <= td->td_kstack + td->td_kstack_pages * PAGE_SIZE);
+}
+#endif	/* _SYS_PROC_H_ */
+
+#endif
diff --git a/sys/arm/arm/ptrace_machdep.c b/sys/arm/arm/ptrace_machdep.c
index 3edadbd72ddf..a347a1dfac95 100644
--- a/sys/arm/arm/ptrace_machdep.c
+++ b/sys/arm/arm/ptrace_machdep.c
@@ -32,6 +32,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/proc.h>
 #include <sys/ptrace.h>
 #include <sys/reg.h>
+#include <machine/pcb.h>
 #ifdef VFP
 #include <machine/vfp.h>
 #endif
diff --git a/sys/arm/include/proc.h b/sys/arm/include/proc.h
index 76b05b335420..9db28358cc39 100644
--- a/sys/arm/include/proc.h
+++ b/sys/arm/include/proc.h
@@ -56,15 +56,4 @@ struct mdproc {
 
 #define	KINFO_PROC_SIZE 816
 
-#ifdef _KERNEL
-#include <machine/pcb.h>
-
-/* Get the current kernel thread stack usage. */
-#define	GET_STACK_USAGE(total, used) do {				\
-	struct thread *td = curthread;					\
-	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
-	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
-} while (0)
-
-#endif  /* _KERNEL */
 #endif /* !_MACHINE_PROC_H_ */
diff --git a/sys/arm/include/stack.h b/sys/arm/include/stack.h
index 4bc384f775bc..e8d130517be5 100644
--- a/sys/arm/include/stack.h
+++ b/sys/arm/include/stack.h
@@ -63,6 +63,25 @@ struct linker_file;
 void unwind_module_loaded(struct linker_file *);
 void unwind_module_unloaded(struct linker_file *);
 
+#ifdef _SYS_PROC_H_
+
+#include <machine/pcb.h>
+
+/* Get the current kernel thread stack usage. */
+#define	GET_STACK_USAGE(total, used) do {				\
+	struct thread *td = curthread;					\
+	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
+	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
+} while (0)
+
+static __inline bool
+kstack_contains(struct thread *td, vm_offset_t va, size_t len)
+{
+	return (va >= td->td_kstack && va + len >= va &&
+	    va + len <= td->td_kstack + td->td_kstack_pages * PAGE_SIZE);
+}
+#endif	/* _SYS_PROC_H_ */
+
 #endif
 
 #endif /* !_MACHINE_STACK_H_ */
diff --git a/sys/arm64/arm64/debug_monitor.c b/sys/arm64/arm64/debug_monitor.c
index 2ec76c9a2f33..52bcf1e5e603 100644
--- a/sys/arm64/arm64/debug_monitor.c
+++ b/sys/arm64/arm64/debug_monitor.c
@@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$");
 #include <machine/cpu.h>
 #include <machine/debug_monitor.h>
 #include <machine/kdb.h>
+#include <machine/pcb.h>
 
 #ifdef DDB
 #include <ddb/ddb.h>
diff --git a/sys/arm64/arm64/elf32_machdep.c b/sys/arm64/arm64/elf32_machdep.c
index 627973ecfd3d..7b346ed81b2c 100644
--- a/sys/arm64/arm64/elf32_machdep.c
+++ b/sys/arm64/arm64/elf32_machdep.c
@@ -52,6 +52,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/vnode.h>
 
 #include <machine/elf.h>
+#include <machine/pcb.h>
 #ifdef VFP
 #include <machine/vfp.h>
 #endif
diff --git a/sys/arm64/arm64/freebsd32_machdep.c b/sys/arm64/arm64/freebsd32_machdep.c
index 9b62802efbc5..5fadef74df87 100644
--- a/sys/arm64/arm64/freebsd32_machdep.c
+++ b/sys/arm64/arm64/freebsd32_machdep.c
@@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/sysent.h>
 #include <sys/sysproto.h>
 #include <machine/armreg.h>
+#include <machine/pcb.h>
 #ifdef VFP
 #include <machine/vfp.h>
 #endif
diff --git a/sys/arm64/arm64/ptrace_machdep.c b/sys/arm64/arm64/ptrace_machdep.c
index 01135978b39a..079391ac102c 100644
--- a/sys/arm64/arm64/ptrace_machdep.c
+++ b/sys/arm64/arm64/ptrace_machdep.c
@@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/ucontext.h>
 
 #include <machine/armreg.h>
+#include <machine/pcb.h>
 
 /* Only used to get/set 32bits VFP regs */
 int
diff --git a/sys/arm64/include/proc.h b/sys/arm64/include/proc.h
index 15361a0e3788..9a22fe43833a 100644
--- a/sys/arm64/include/proc.h
+++ b/sys/arm64/include/proc.h
@@ -72,16 +72,4 @@ struct mdproc {
 #define	KINFO_PROC_SIZE	1088
 #define	KINFO_PROC32_SIZE 816
 
-#ifdef _KERNEL
-
-#include <machine/pcb.h>
-
-#define	GET_STACK_USAGE(total, used) do {				\
-	struct thread *td = curthread;					\
-	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
-	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
-} while (0)
-
-#endif
-
 #endif /* !_MACHINE_PROC_H_ */
diff --git a/sys/arm64/include/stack.h b/sys/arm64/include/stack.h
index 4b1d190df595..4c4c41bf9516 100644
--- a/sys/arm64/include/stack.h
+++ b/sys/arm64/include/stack.h
@@ -39,4 +39,22 @@ struct unwind_state {
 
 bool unwind_frame(struct thread *, struct unwind_state *);
 
+#ifdef _SYS_PROC_H_
+
+#include <machine/pcb.h>
+
+#define	GET_STACK_USAGE(total, used) do {				\
+	struct thread *td = curthread;					\
+	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
+	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
+} while (0)
+
+static __inline bool
+kstack_contains(struct thread *td, vm_offset_t va, size_t len)
+{
+	return (va >= td->td_kstack && va + len >= va &&
+	    va + len <= td->td_kstack + td->td_kstack_pages * PAGE_SIZE);
+}
+#endif	/* _SYS_PROC_H_ */
+
 #endif /* !_MACHINE_STACK_H_ */
diff --git a/sys/arm64/linux/linux_sysvec.c b/sys/arm64/linux/linux_sysvec.c
index 41ac2912be29..9a82dc94b6ac 100644
--- a/sys/arm64/linux/linux_sysvec.c
+++ b/sys/arm64/linux/linux_sysvec.c
@@ -71,7 +71,7 @@ __FBSDID("$FreeBSD$");
 #include <arm64/linux/linux_sigframe.h>
 
 #include <machine/md_var.h>
-
+#include <machine/pcb.h>
 #ifdef VFP
 #include <machine/vfp.h>
 #endif
diff --git a/sys/ddb/db_ps.c b/sys/ddb/db_ps.c
index a5245528ca83..5d713264d975 100644
--- a/sys/ddb/db_ps.c
+++ b/sys/ddb/db_ps.c
@@ -49,6 +49,8 @@ __FBSDID("$FreeBSD$");
 
 #include <ddb/ddb.h>
 
+#include <machine/stack.h>
+
 #define PRINT_NONE	0
 #define PRINT_ARGS	1
 
diff --git a/sys/geom/geom_io.c b/sys/geom/geom_io.c
index 20e31b9b921b..777f698b6f1f 100644
--- a/sys/geom/geom_io.c
+++ b/sys/geom/geom_io.c
@@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/stack.h>
 #include <sys/sysctl.h>
 #include <sys/vmem.h>
+#include <machine/stack.h>
 #include <machine/stdarg.h>
 
 #include <sys/errno.h>
diff --git a/sys/i386/include/proc.h b/sys/i386/include/proc.h
index 7affe60edab9..d693500f2c3e 100644
--- a/sys/i386/include/proc.h
+++ b/sys/i386/include/proc.h
@@ -66,13 +66,6 @@ struct mdproc {
 
 #include <machine/md_var.h>
 
-/* Get the current kernel thread stack usage. */
-#define GET_STACK_USAGE(total, used) do {				\
-	struct thread	*td = curthread;				\
-	(total) = (vm_offset_t)get_pcb_td(td) - td->td_kstack;		\
-	(used) = (vm_offset_t)get_pcb_td(td) - (vm_offset_t)&td;	\
-} while (0)
-
 void 	set_user_ldt(struct mdproc *);
 struct 	proc_ldt *user_ldt_alloc(struct mdproc *, int);
 void 	user_ldt_free(struct thread *);
diff --git a/sys/i386/include/stack.h b/sys/i386/include/stack.h
index 091ae33893d4..773aca1c66d9 100644
--- a/sys/i386/include/stack.h
+++ b/sys/i386/include/stack.h
@@ -3,4 +3,27 @@
  */
 /* $FreeBSD$ */
 
+#ifndef _MACHINE_STACK_H_
+#define	_MACHINE_STACK_H_
+
 #include <x86/stack.h>
+
+#ifdef _SYS_PROC_H_
+
+/* Get the current kernel thread stack usage. */
+#define GET_STACK_USAGE(total, used) do {				\
+	struct thread	*td = curthread;				\
+	(total) = (vm_offset_t)get_pcb_td(td) - td->td_kstack;		\
+	(used) = (vm_offset_t)get_pcb_td(td) - (vm_offset_t)&td;	\
+} while (0)
+
+static __inline bool
+kstack_contains(struct thread *td, vm_offset_t va, size_t len)
+{
+	return (va >= td->td_kstack && va + len >= va &&
+	    va + len <= td->td_kstack + td->td_kstack_pages * PAGE_SIZE);
+}
+
+#endif	/* _SYS_PROC_H_ */
+
+#endif
diff --git a/sys/kern/subr_epoch.c b/sys/kern/subr_epoch.c
index 98a560e44c9d..2a0144412399 100644
--- a/sys/kern/subr_epoch.c
+++ b/sys/kern/subr_epoch.c
@@ -56,6 +56,8 @@ __FBSDID("$FreeBSD$");
 #include <vm/vm_kern.h>
 #include <vm/uma.h>
 
+#include <machine/stack.h>
+
 #include <ck_epoch.h>
 
 #ifdef __amd64__
diff --git a/sys/netgraph/ng_base.c b/sys/netgraph/ng_base.c
index 092231850f18..205b6041053b 100644
--- a/sys/netgraph/ng_base.c
+++ b/sys/netgraph/ng_base.c
@@ -66,6 +66,8 @@
 #include <machine/cpu.h>
 #include <vm/uma.h>
 
+#include <machine/stack.h>
+
 #include <net/netisr.h>
 #include <net/vnet.h>
 
diff --git a/sys/powerpc/include/proc.h b/sys/powerpc/include/proc.h
index 2c6a00536b8a..0f8d36bfe856 100644
--- a/sys/powerpc/include/proc.h
+++ b/sys/powerpc/include/proc.h
@@ -59,16 +59,4 @@ struct mdproc {
 #define	KINFO_PROC_SIZE 816
 #endif
 
-#ifdef _KERNEL
-
-#include <machine/pcb.h>
-
-/* Get the current kernel thread stack usage. */
-#define	GET_STACK_USAGE(total, used) do {				\
-	struct thread *td = curthread;					\
-	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
-	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
-} while (0)
-#endif
-
 #endif /* !_MACHINE_PROC_H_ */
diff --git a/sys/powerpc/include/stack.h b/sys/powerpc/include/stack.h
index c433a9fe09eb..953afd6f0aa4 100644
--- a/sys/powerpc/include/stack.h
+++ b/sys/powerpc/include/stack.h
@@ -33,4 +33,23 @@ extern int trapexit[];
 extern int asttrapexit[];
 extern int end[];
 
+#ifdef _SYS_PROC_H_
+
+#include <machine/pcb.h>
+
+/* Get the current kernel thread stack usage. */
+#define	GET_STACK_USAGE(total, used) do {				\
+	struct thread *td = curthread;					\
+	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
+	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
+} while (0)
+
+static __inline bool
+kstack_contains(struct thread *td, vm_offset_t va, size_t len)
+{
+	return (va >= td->td_kstack && va + len >= va &&
+	    va + len <= td->td_kstack + td->td_kstack_pages * PAGE_SIZE);
+}
+#endif	/* _SYS_PROC_H_ */
+
 #endif /* !_MACHINE_STACK_H_ */
diff --git a/sys/riscv/include/proc.h b/sys/riscv/include/proc.h
index 648c529f4322..ce0a62675308 100644
--- a/sys/riscv/include/proc.h
+++ b/sys/riscv/include/proc.h
@@ -45,15 +45,4 @@ struct mdproc {
 
 #define	KINFO_PROC_SIZE	1088
 
-#ifdef _KERNEL
-#include <machine/pcb.h>
-
-/* Get the current kernel thread stack usage. */
-#define	GET_STACK_USAGE(total, used) do {				\
-	struct thread *td = curthread;					\
-	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
-	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
-} while (0)
-
-#endif  /* _KERNEL */
 #endif /* !_MACHINE_PROC_H_ */
diff --git a/sys/riscv/include/stack.h b/sys/riscv/include/stack.h
index 82f851096d7b..566081c3ebd0 100644
--- a/sys/riscv/include/stack.h
+++ b/sys/riscv/include/stack.h
@@ -48,4 +48,23 @@ struct unwind_state {
 
 bool unwind_frame(struct thread *, struct unwind_state *);
 
+#ifdef _SYS_PROC_H_
+
+#include <machine/pcb.h>
+
+/* Get the current kernel thread stack usage. */
+#define	GET_STACK_USAGE(total, used) do {				\
+	struct thread *td = curthread;					\
+	(total) = td->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb);	\
+	(used) = td->td_kstack + (total) - (vm_offset_t)&td;		\
+} while (0)
+
+static __inline bool
+kstack_contains(struct thread *td, vm_offset_t va, size_t len)
+{
+	return (va >= td->td_kstack && va + len >= va &&
+	    va + len <= td->td_kstack + td->td_kstack_pages * PAGE_SIZE);
+}
+#endif	/* _SYS_PROC_H_ */
+
 #endif /* !_MACHINE_STACK_H_ */
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index 2da5d8edee6d..2ad4505405c8 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -1324,13 +1324,6 @@ curthread_pflags2_restore(int save)
 	curthread->td_pflags2 &= save;
 }
 
-static __inline bool
-kstack_contains(struct thread *td, vm_offset_t va, size_t len)
-{
-	return (va >= td->td_kstack && va + len >= va &&
-	    va + len <= td->td_kstack + td->td_kstack_pages * PAGE_SIZE);
-}
-
 static __inline __pure2 struct td_sched *
 td_get_sched(struct thread *td)
 {
diff --git a/sys/x86/x86/stack_machdep.c b/sys/x86/x86/stack_machdep.c
index 1243137d2ea0..5d7dfd251b0d 100644
--- a/sys/x86/x86/stack_machdep.c
+++ b/sys/x86/x86/stack_machdep.c
@@ -45,7 +45,7 @@ __FBSDID("$FreeBSD$");
 #include <vm/vm_param.h>
 #include <vm/pmap.h>
 
-#include <x86/stack.h>
+#include <machine/stack.h>
 
 #ifdef __i386__
 #define	PCB_FP(pcb)	((pcb)->pcb_ebp)