1
0
mirror of https://git.FreeBSD.org/src.git synced 2026-06-02 11:24:32 +00:00

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
This commit is contained in:
John Baldwin
2026-04-27 15:04:22 -04:00
parent fce595344c
commit 8b9023cfc1
19 changed files with 29 additions and 37 deletions
+1 -2
View File
@@ -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;
}
+2 -4
View File
@@ -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_ */
+1 -2
View File
@@ -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;
+1 -2
View File
@@ -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
+1 -2
View File
@@ -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_ */
+1 -2
View File
@@ -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;
+1 -2
View File
@@ -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);
}
+1 -2
View File
@@ -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_ */
+1 -2
View File
@@ -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: ");
+2 -2
View File
@@ -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);
+2 -2
View File
@@ -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);
+1 -2
View File
@@ -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_ */
+2 -2
View File
@@ -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;
}
+2 -2
View File
@@ -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;
+1 -2
View File
@@ -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_ */
+1 -2
View File
@@ -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;
+1 -2
View File
@@ -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
+6
View File
@@ -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)
{
+1 -1
View File
@@ -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;
/*