git: 8b9023cfc185 - main - sys: Add td_kstack_top inline helper function
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 27 May 2026 14:59:27 UTC
The branch main has been updated by jhb:
URL: https://cgit.FreeBSD.org/src/commit/?id=8b9023cfc1851a18518111d286ebd59b573008c1
commit 8b9023cfc1851a18518111d286ebd59b573008c1
Author: John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2026-04-27 19:04:22 +0000
Commit: John Baldwin <jhb@FreeBSD.org>
CommitDate: 2026-05-27 13:45:26 +0000
sys: Add td_kstack_top inline helper function
This function returns a pointer to the top of the kstack.
Reviewed by: kib, andrew (arm changes)
Sponsored by: AFRL, DARPA
Pull Request: https://ron-dev.freebsd.org/FreeBSD/src/pulls/23
---
sys/amd64/amd64/vm_machdep.c | 3 +--
sys/amd64/include/stack.h | 6 ++----
sys/arm/arm/machdep.c | 3 +--
sys/arm/arm/vm_machdep.c | 3 +--
sys/arm/include/stack.h | 3 +--
sys/arm64/arm64/machdep.c | 3 +--
sys/arm64/arm64/vm_machdep.c | 3 +--
sys/arm64/include/stack.h | 3 +--
sys/ddb/db_ps.c | 3 +--
sys/i386/i386/machdep.c | 4 ++--
sys/i386/i386/vm_machdep.c | 4 ++--
sys/powerpc/include/stack.h | 3 +--
sys/powerpc/powerpc/exec_machdep.c | 4 ++--
sys/powerpc/powerpc/machdep.c | 4 ++--
sys/riscv/include/stack.h | 3 +--
sys/riscv/riscv/machdep.c | 3 +--
sys/riscv/riscv/vm_machdep.c | 3 +--
sys/sys/proc.h | 6 ++++++
sys/vm/vm_glue.c | 2 +-
19 files changed, 29 insertions(+), 37 deletions(-)
diff --git a/sys/amd64/amd64/vm_machdep.c b/sys/amd64/amd64/vm_machdep.c
index 244d82ee8ebb..1de891680f94 100644
--- a/sys/amd64/amd64/vm_machdep.c
+++ b/sys/amd64/amd64/vm_machdep.c
@@ -377,8 +377,7 @@ cpu_thread_alloc(struct thread *td)
void
cpu_thread_new_kstack(struct thread *td)
{
- td->td_md.md_stack_base = td->td_kstack +
- td->td_kstack_pages * PAGE_SIZE;
+ td->td_md.md_stack_base = td_kstack_top(td);
td->td_frame = (struct trapframe *)td->td_md.md_stack_base - 1;
}
diff --git a/sys/amd64/include/stack.h b/sys/amd64/include/stack.h
index 3c27266b775b..765a8e78daed 100644
--- a/sys/amd64/include/stack.h
+++ b/sys/amd64/include/stack.h
@@ -13,16 +13,14 @@
#define GET_STACK_USAGE(total, used) do { \
struct thread *td = curthread; \
(total) = td->td_kstack_pages * PAGE_SIZE; \
- (used) = td->td_kstack + td->td_kstack_pages * PAGE_SIZE - \
- (char *)&td; \
+ (used) = td_kstack_top(td) - (char *)&td; \
} while (0)
static __inline bool
kstack_contains(struct thread *td, vm_offset_t va, size_t len)
{
return (va >= (vm_offset_t)td->td_kstack && va + len >= va &&
- va + len <= (vm_offset_t)td->td_kstack + td->td_kstack_pages *
- PAGE_SIZE);
+ va + len <= (vm_offset_t)td_kstack_top(td));
}
#endif /* _SYS_PROC_H_ */
diff --git a/sys/arm/arm/machdep.c b/sys/arm/arm/machdep.c
index a06e6773cd49..989adef3478d 100644
--- a/sys/arm/arm/machdep.c
+++ b/sys/arm/arm/machdep.c
@@ -378,8 +378,7 @@ init_proc0(void *kstack)
proc_linkup0(&proc0, &thread0);
thread0.td_kstack = kstack;
thread0.td_kstack_pages = kstack_pages;
- thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
- thread0.td_kstack_pages * PAGE_SIZE) - 1;
+ thread0.td_pcb = (struct pcb *)td_kstack_top(&thread0) - 1;
thread0.td_pcb->pcb_flags = 0;
thread0.td_pcb->pcb_fpflags = 0;
thread0.td_pcb->pcb_vfpcpu = -1;
diff --git a/sys/arm/arm/vm_machdep.c b/sys/arm/arm/vm_machdep.c
index 559c90509f11..a8a4b6b8c7be 100644
--- a/sys/arm/arm/vm_machdep.c
+++ b/sys/arm/arm/vm_machdep.c
@@ -246,8 +246,7 @@ cpu_thread_alloc(struct thread *td)
void
cpu_thread_new_kstack(struct thread *td)
{
- td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_pages *
- PAGE_SIZE) - 1;
+ td->td_pcb = (struct pcb *)td_kstack_top(td) - 1;
/*
* Ensure td_frame is aligned to an 8 byte boundary as it will be
* placed into the stack pointer which must be 8 byte aligned in
diff --git a/sys/arm/include/stack.h b/sys/arm/include/stack.h
index e80d3dc060fd..6af9418aba42 100644
--- a/sys/arm/include/stack.h
+++ b/sys/arm/include/stack.h
@@ -76,8 +76,7 @@ static __inline bool
kstack_contains(struct thread *td, vm_offset_t va, size_t len)
{
return (va >= (vm_offset_t)td->td_kstack && va + len >= va &&
- va + len <= (vm_offset_t)td->td_kstack + td->td_kstack_pages *
- PAGE_SIZE - sizeof(struct pcb));
+ va + len <= (vm_offset_t)td_kstack_top(td) - sizeof(struct pcb));
}
#endif /* _SYS_PROC_H_ */
diff --git a/sys/arm64/arm64/machdep.c b/sys/arm64/arm64/machdep.c
index d219c737c215..6b6dd2c510a2 100644
--- a/sys/arm64/arm64/machdep.c
+++ b/sys/arm64/arm64/machdep.c
@@ -443,8 +443,7 @@ init_proc0(void *kstack)
#if defined(PERTHREAD_SSP)
thread0.td_md.md_canary = boot_canary;
#endif
- thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
- thread0.td_kstack_pages * PAGE_SIZE) - 1;
+ thread0.td_pcb = (struct pcb *)td_kstack_top(&thread0) - 1;
thread0.td_pcb->pcb_flags = 0;
thread0.td_pcb->pcb_fpflags = 0;
thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
diff --git a/sys/arm64/arm64/vm_machdep.c b/sys/arm64/arm64/vm_machdep.c
index 4fe7c76d473d..29a1c8667e7d 100644
--- a/sys/arm64/arm64/vm_machdep.c
+++ b/sys/arm64/arm64/vm_machdep.c
@@ -266,8 +266,7 @@ cpu_thread_alloc(struct thread *td)
void
cpu_thread_new_kstack(struct thread *td)
{
- td->td_pcb = (struct pcb *)(td->td_kstack +
- td->td_kstack_pages * PAGE_SIZE) - 1;
+ td->td_pcb = (struct pcb *)td_kstack_top(td) - 1;
td->td_frame = (struct trapframe *)STACKALIGN(
(struct trapframe *)td->td_pcb - 1);
}
diff --git a/sys/arm64/include/stack.h b/sys/arm64/include/stack.h
index 19e9e837e3ee..b89019fbbec6 100644
--- a/sys/arm64/include/stack.h
+++ b/sys/arm64/include/stack.h
@@ -51,8 +51,7 @@ static __inline bool
kstack_contains(struct thread *td, vm_offset_t va, size_t len)
{
return (va >= (vm_offset_t)td->td_kstack && va + len >= va &&
- va + len <= (vm_offset_t)td->td_kstack + td->td_kstack_pages *
- PAGE_SIZE - sizeof(struct pcb));
+ va + len <= (vm_offset_t)td_kstack_top(td) - sizeof(struct pcb));
}
#endif /* _SYS_PROC_H_ */
diff --git a/sys/ddb/db_ps.c b/sys/ddb/db_ps.c
index 8e027997ea75..59dcde453b93 100644
--- a/sys/ddb/db_ps.c
+++ b/sys/ddb/db_ps.c
@@ -358,8 +358,7 @@ DB_SHOW_COMMAND(thread, db_show_thread)
if (td->td_name[0] != '\0')
db_printf(" name: %s\n", td->td_name);
db_printf(" pcb: %p\n", td->td_pcb);
- db_printf(" stack: %p-%p\n", td->td_kstack,
- td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1);
+ db_printf(" stack: %p-%p\n", td->td_kstack, td_kstack_top(td) - 1);
db_printf(" flags: %#x ", td->td_flags);
db_printf(" pflags: %#x\n", td->td_pflags);
db_printf(" state: ");
diff --git a/sys/i386/i386/machdep.c b/sys/i386/i386/machdep.c
index f9afb9afe45f..71992f18e8fb 100644
--- a/sys/i386/i386/machdep.c
+++ b/sys/i386/i386/machdep.c
@@ -1492,8 +1492,8 @@ init386(int first)
PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
/* Initialize the tss (except for the final esp0) early for vm86. */
- common_tss0.tss_esp0 = (vm_offset_t)thread0.td_kstack +
- thread0.td_kstack_pages * PAGE_SIZE - VM86_STACK_SPACE;
+ common_tss0.tss_esp0 = (vm_offset_t)td_kstack_top(&thread0) -
+ VM86_STACK_SPACE;
common_tss0.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
common_tss0.tss_ioopt = sizeof(struct i386tss) << 16;
gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
diff --git a/sys/i386/i386/vm_machdep.c b/sys/i386/i386/vm_machdep.c
index d59d98542241..72fe83d3fdd4 100644
--- a/sys/i386/i386/vm_machdep.c
+++ b/sys/i386/i386/vm_machdep.c
@@ -89,7 +89,7 @@ get_pcb_user_save_td(struct thread *td)
{
char *p;
- p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
+ p = td_kstack_top(td) -
roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN);
KASSERT(__is_aligned(p, XSAVE_AREA_ALIGN),
("Unaligned pcb_user_save area"));
@@ -110,7 +110,7 @@ get_pcb_td(struct thread *td)
{
char *p;
- p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
+ p = td_kstack_top(td) -
roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN) -
sizeof(struct pcb);
return ((struct pcb *)p);
diff --git a/sys/powerpc/include/stack.h b/sys/powerpc/include/stack.h
index 928256b26468..0fb9a929128b 100644
--- a/sys/powerpc/include/stack.h
+++ b/sys/powerpc/include/stack.h
@@ -46,8 +46,7 @@ static __inline bool
kstack_contains(struct thread *td, vm_offset_t va, size_t len)
{
return (va >= (vm_offset_t)td->td_kstack && va + len >= va &&
- va + len <= (vm_offset_t)td->td_kstack + td->td_kstack_pages *
- PAGE_SIZE - sizeof(struct pcb));
+ va + len <= (vm_offset_t)td_kstack_top(td) - sizeof(struct pcb));
}
#endif /* _SYS_PROC_H_ */
diff --git a/sys/powerpc/powerpc/exec_machdep.c b/sys/powerpc/powerpc/exec_machdep.c
index 1778737be610..18e0ba004a13 100644
--- a/sys/powerpc/powerpc/exec_machdep.c
+++ b/sys/powerpc/powerpc/exec_machdep.c
@@ -1087,8 +1087,8 @@ cpu_thread_new_kstack(struct thread *td)
{
struct pcb *pcb;
- pcb = (struct pcb *)__align_down(td->td_kstack + td->td_kstack_pages *
- PAGE_SIZE - sizeof(struct pcb), 0x40);
+ pcb = (struct pcb *)__align_down(td_kstack_top(td) - sizeof(struct pcb),
+ 0x40);
td->td_pcb = pcb;
td->td_frame = (struct trapframe *)pcb - 1;
}
diff --git a/sys/powerpc/powerpc/machdep.c b/sys/powerpc/powerpc/machdep.c
index a975bebebaad..e1e0885e3ecf 100644
--- a/sys/powerpc/powerpc/machdep.c
+++ b/sys/powerpc/powerpc/machdep.c
@@ -487,8 +487,8 @@ powerpc_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, void *mdp,
/*
* Finish setting up thread0.
*/
- thread0.td_pcb = (struct pcb *)__align_down(thread0.td_kstack +
- thread0.td_kstack_pages * PAGE_SIZE - sizeof(struct pcb), 16);
+ thread0.td_pcb = (struct pcb *)__align_down(td_kstack_top(&thread0) -
+ sizeof(struct pcb), 16);
bzero((void *)thread0.td_pcb, sizeof(struct pcb));
pc->pc_curpcb = thread0.td_pcb;
diff --git a/sys/riscv/include/stack.h b/sys/riscv/include/stack.h
index 03b5794c2b13..7de9f05b567b 100644
--- a/sys/riscv/include/stack.h
+++ b/sys/riscv/include/stack.h
@@ -61,8 +61,7 @@ static __inline bool
kstack_contains(struct thread *td, vm_offset_t va, size_t len)
{
return (va >= (vm_offset_t)td->td_kstack && va + len >= va &&
- va + len <= (vm_offset_t)td->td_kstack + td->td_kstack_pages *
- PAGE_SIZE - sizeof(struct pcb));
+ va + len <= (vm_offset_t)td_kstack_top(td) - sizeof(struct pcb));
}
#endif /* _SYS_PROC_H_ */
diff --git a/sys/riscv/riscv/machdep.c b/sys/riscv/riscv/machdep.c
index 91219676454b..47d9636492db 100644
--- a/sys/riscv/riscv/machdep.c
+++ b/sys/riscv/riscv/machdep.c
@@ -296,8 +296,7 @@ init_proc0(void *kstack)
proc_linkup0(&proc0, &thread0);
thread0.td_kstack = kstack;
thread0.td_kstack_pages = KSTACK_PAGES;
- thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
- thread0.td_kstack_pages * PAGE_SIZE) - 1;
+ thread0.td_pcb = (struct pcb *)td_kstack_top(&thread0) - 1;
thread0.td_pcb->pcb_fpflags = 0;
thread0.td_frame = &proc0_tf;
pcpup->pc_curpcb = thread0.td_pcb;
diff --git a/sys/riscv/riscv/vm_machdep.c b/sys/riscv/riscv/vm_machdep.c
index b078695ae718..1810fe88283c 100644
--- a/sys/riscv/riscv/vm_machdep.c
+++ b/sys/riscv/riscv/vm_machdep.c
@@ -61,8 +61,7 @@
void
cpu_thread_new_kstack(struct thread *td)
{
- td->td_pcb = (struct pcb *)(td->td_kstack +
- td->td_kstack_pages * PAGE_SIZE) - 1;
+ td->td_pcb = (struct pcb *)td_kstack_top(td) - 1;
/*
* td->td_frame + TF_SIZE will be the saved kernel stack pointer whilst
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index 3f2af5583a19..ed69a09422e2 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -1327,6 +1327,12 @@ td_get_sched(struct thread *td)
return ((struct td_sched *)&td[1]);
}
+static __inline char *
+td_kstack_top(struct thread *td)
+{
+ return (td->td_kstack + ptoa(td->td_kstack_pages));
+}
+
static __inline void
ruxreset(struct rusage_ext *rux)
{
diff --git a/sys/vm/vm_glue.c b/sys/vm/vm_glue.c
index 191711ec7765..02e1c993529e 100644
--- a/sys/vm/vm_glue.c
+++ b/sys/vm/vm_glue.c
@@ -739,7 +739,7 @@ intr_prof_stack_use(struct thread *td, struct trapframe *frame)
if (TRAPF_USERMODE(frame))
return;
- stack_top = td->td_kstack + td->td_kstack_pages * PAGE_SIZE;
+ stack_top = td_kstack_top(td);
current = (char *)&stack_top;
/*