From 2555f175b368569dd98e1bd2b6bd095c933faed7 Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Wed, 1 Feb 2023 00:47:40 +0200 Subject: [PATCH] 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 6181df35261..8015fe5da81 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 091ae33893d..ff21ee28b5a 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 + +#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 3edadbd72dd..a347a1dfac9 100644 --- a/sys/arm/arm/ptrace_machdep.c +++ b/sys/arm/arm/ptrace_machdep.c @@ -32,6 +32,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #ifdef VFP #include #endif diff --git a/sys/arm/include/proc.h b/sys/arm/include/proc.h index 76b05b33542..9db28358cc3 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 - -/* 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 4bc384f775b..e8d130517be 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 + +/* 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 2ec76c9a2f3..52bcf1e5e60 100644 --- a/sys/arm64/arm64/debug_monitor.c +++ b/sys/arm64/arm64/debug_monitor.c @@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #ifdef DDB #include diff --git a/sys/arm64/arm64/elf32_machdep.c b/sys/arm64/arm64/elf32_machdep.c index 627973ecfd3..7b346ed81b2 100644 --- a/sys/arm64/arm64/elf32_machdep.c +++ b/sys/arm64/arm64/elf32_machdep.c @@ -52,6 +52,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #ifdef VFP #include #endif diff --git a/sys/arm64/arm64/freebsd32_machdep.c b/sys/arm64/arm64/freebsd32_machdep.c index 9b62802efbc..5fadef74df8 100644 --- a/sys/arm64/arm64/freebsd32_machdep.c +++ b/sys/arm64/arm64/freebsd32_machdep.c @@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #ifdef VFP #include #endif diff --git a/sys/arm64/arm64/ptrace_machdep.c b/sys/arm64/arm64/ptrace_machdep.c index 01135978b39..079391ac102 100644 --- a/sys/arm64/arm64/ptrace_machdep.c +++ b/sys/arm64/arm64/ptrace_machdep.c @@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include /* Only used to get/set 32bits VFP regs */ int diff --git a/sys/arm64/include/proc.h b/sys/arm64/include/proc.h index 15361a0e378..9a22fe43833 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 - -#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 4b1d190df59..4c4c41bf951 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 + +#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 41ac2912be2..9a82dc94b6a 100644 --- a/sys/arm64/linux/linux_sysvec.c +++ b/sys/arm64/linux/linux_sysvec.c @@ -71,7 +71,7 @@ __FBSDID("$FreeBSD$"); #include #include - +#include #ifdef VFP #include #endif diff --git a/sys/ddb/db_ps.c b/sys/ddb/db_ps.c index a5245528ca8..5d713264d97 100644 --- a/sys/ddb/db_ps.c +++ b/sys/ddb/db_ps.c @@ -49,6 +49,8 @@ __FBSDID("$FreeBSD$"); #include +#include + #define PRINT_NONE 0 #define PRINT_ARGS 1 diff --git a/sys/geom/geom_io.c b/sys/geom/geom_io.c index 20e31b9b921..777f698b6f1 100644 --- a/sys/geom/geom_io.c +++ b/sys/geom/geom_io.c @@ -53,6 +53,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include diff --git a/sys/i386/include/proc.h b/sys/i386/include/proc.h index 7affe60edab..d693500f2c3 100644 --- a/sys/i386/include/proc.h +++ b/sys/i386/include/proc.h @@ -66,13 +66,6 @@ struct mdproc { #include -/* 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 091ae33893d..773aca1c66d 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 + +#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 98a560e44c9..2a014441239 100644 --- a/sys/kern/subr_epoch.c +++ b/sys/kern/subr_epoch.c @@ -56,6 +56,8 @@ __FBSDID("$FreeBSD$"); #include #include +#include + #include #ifdef __amd64__ diff --git a/sys/netgraph/ng_base.c b/sys/netgraph/ng_base.c index 092231850f1..205b6041053 100644 --- a/sys/netgraph/ng_base.c +++ b/sys/netgraph/ng_base.c @@ -66,6 +66,8 @@ #include #include +#include + #include #include diff --git a/sys/powerpc/include/proc.h b/sys/powerpc/include/proc.h index 2c6a00536b8..0f8d36bfe85 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 - -/* 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 c433a9fe09e..953afd6f0aa 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 + +/* 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 648c529f432..ce0a6267530 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 - -/* 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 82f851096d7..566081c3ebd 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 + +/* 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 2da5d8edee6..2ad4505405c 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 1243137d2ea..5d7dfd251b0 100644 --- a/sys/x86/x86/stack_machdep.c +++ b/sys/x86/x86/stack_machdep.c @@ -45,7 +45,7 @@ __FBSDID("$FreeBSD$"); #include #include -#include +#include #ifdef __i386__ #define PCB_FP(pcb) ((pcb)->pcb_ebp)