git: e9a697bf20cc - main - STACKALIGN: Reimplement in terms of __align_down
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 06 Feb 2026 15:31:12 UTC
The branch main has been updated by jhb:
URL: https://cgit.FreeBSD.org/src/commit/?id=e9a697bf20ccbb2a19f6ad01fa3e0c14e0390847
commit e9a697bf20ccbb2a19f6ad01fa3e0c14e0390847
Author: John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2026-02-06 15:30:50 +0000
Commit: John Baldwin <jhb@FreeBSD.org>
CommitDate: 2026-02-06 15:30:50 +0000
STACKALIGN: Reimplement in terms of __align_down
This changes STACKALIGN to be type-preserving when operating on
pointers.
Reviewed by: brooks, kib
Effort: CHERI upstreaming
Sponsored by: AFRL, DARPA
Differential Revision: https://reviews.freebsd.org/D54920
---
lib/libc/aarch64/gen/makecontext.c | 3 ++-
lib/libc/riscv/gen/makecontext.c | 3 ++-
sys/arm/arm/exec_machdep.c | 2 +-
sys/arm/arm/machdep.c | 2 +-
sys/arm/arm/vm_machdep.c | 4 ++--
sys/arm/include/param.h | 1 -
sys/arm64/arm64/exec_machdep.c | 2 +-
sys/arm64/arm64/vm_machdep.c | 2 +-
sys/arm64/include/param.h | 1 -
sys/arm64/linux/linux_sysvec.c | 2 +-
sys/riscv/include/param.h | 1 -
sys/riscv/riscv/exec_machdep.c | 2 +-
sys/sys/param.h | 3 +++
13 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/lib/libc/aarch64/gen/makecontext.c b/lib/libc/aarch64/gen/makecontext.c
index 8c9b4edd7a15..2d0ee1043a9b 100644
--- a/lib/libc/aarch64/gen/makecontext.c
+++ b/lib/libc/aarch64/gen/makecontext.c
@@ -74,7 +74,8 @@ __makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
va_end(ap);
/* Set the stack */
- gp->gp_sp = STACKALIGN(ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
+ gp->gp_sp = STACKALIGN((uintptr_t)ucp->uc_stack.ss_sp +
+ ucp->uc_stack.ss_size);
/* Arrange for return via the trampoline code. */
gp->gp_elr = (__register_t)_ctx_start;
gp->gp_x[19] = (__register_t)func;
diff --git a/lib/libc/riscv/gen/makecontext.c b/lib/libc/riscv/gen/makecontext.c
index e5371d082b2c..8a6498167f13 100644
--- a/lib/libc/riscv/gen/makecontext.c
+++ b/lib/libc/riscv/gen/makecontext.c
@@ -80,7 +80,8 @@ __makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
va_end(ap);
/* Set the stack */
- gp->gp_sp = STACKALIGN(ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
+ gp->gp_sp = STACKALIGN((uintptr_t)ucp->uc_stack.ss_sp +
+ ucp->uc_stack.ss_size);
/* Arrange for return via the trampoline code. */
gp->gp_sepc = (__register_t)_ctx_start;
gp->gp_s[0] = (__register_t)func;
diff --git a/sys/arm/arm/exec_machdep.c b/sys/arm/arm/exec_machdep.c
index 011085841938..8ce00fc87a08 100644
--- a/sys/arm/arm/exec_machdep.c
+++ b/sys/arm/arm/exec_machdep.c
@@ -307,7 +307,7 @@ sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
fp--;
/* make the stack aligned */
- fp = (struct sigframe *)STACKALIGN(fp);
+ fp = STACKALIGN(fp);
/* Populate the siginfo frame. */
bzero(&frame, sizeof(frame));
get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
diff --git a/sys/arm/arm/machdep.c b/sys/arm/arm/machdep.c
index dc2205023820..60d63cebca3d 100644
--- a/sys/arm/arm/machdep.c
+++ b/sys/arm/arm/machdep.c
@@ -655,7 +655,7 @@ initarm(struct arm_boot_params *abp)
}
#endif
- return ((void *)STACKALIGN(thread0.td_pcb));
+ return (STACKALIGN(thread0.td_pcb));
}
#endif /* FDT */
diff --git a/sys/arm/arm/vm_machdep.c b/sys/arm/arm/vm_machdep.c
index 5f478b54cd66..bee1c705fbbd 100644
--- a/sys/arm/arm/vm_machdep.c
+++ b/sys/arm/arm/vm_machdep.c
@@ -129,7 +129,7 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
pcb2->pcb_regs.sf_r4 = (register_t)fork_return;
pcb2->pcb_regs.sf_r5 = (register_t)td2;
pcb2->pcb_regs.sf_lr = (register_t)fork_trampoline;
- pcb2->pcb_regs.sf_sp = STACKALIGN(td2->td_frame);
+ pcb2->pcb_regs.sf_sp = (register_t)STACKALIGN(td2->td_frame);
pcb2->pcb_regs.sf_tpidrurw = (register_t)get_tls();
#ifdef VFP
@@ -194,7 +194,7 @@ cpu_copy_thread(struct thread *td, struct thread *td0)
td->td_pcb->pcb_regs.sf_r4 = (register_t)fork_return;
td->td_pcb->pcb_regs.sf_r5 = (register_t)td;
td->td_pcb->pcb_regs.sf_lr = (register_t)fork_trampoline;
- td->td_pcb->pcb_regs.sf_sp = STACKALIGN(td->td_frame);
+ td->td_pcb->pcb_regs.sf_sp = (register_t)STACKALIGN(td->td_frame);
td->td_frame->tf_spsr &= ~PSR_C;
td->td_frame->tf_r0 = 0;
diff --git a/sys/arm/include/param.h b/sys/arm/include/param.h
index fa9737608038..8e6f7b1d9bd6 100644
--- a/sys/arm/include/param.h
+++ b/sys/arm/include/param.h
@@ -47,7 +47,6 @@
#include <sys/_align.h>
#define STACKALIGNBYTES (8 - 1)
-#define STACKALIGN(p) ((u_int)(p) & ~STACKALIGNBYTES)
#ifndef MACHINE
#define MACHINE "arm"
diff --git a/sys/arm64/arm64/exec_machdep.c b/sys/arm64/arm64/exec_machdep.c
index 207e10ede96c..9f9b74e6a0e5 100644
--- a/sys/arm64/arm64/exec_machdep.c
+++ b/sys/arm64/arm64/exec_machdep.c
@@ -838,7 +838,7 @@ sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
/* Make room, keeping the stack aligned */
fp = (struct sigframe *)addr;
fp--;
- fp = (struct sigframe *)STACKALIGN(fp);
+ fp = STACKALIGN(fp);
/* Copy the sigframe out to the user's stack. */
if (copyout(&frame, fp, sizeof(*fp)) != 0) {
diff --git a/sys/arm64/arm64/vm_machdep.c b/sys/arm64/arm64/vm_machdep.c
index 0134feb65b6a..b46cb8793409 100644
--- a/sys/arm64/arm64/vm_machdep.c
+++ b/sys/arm64/arm64/vm_machdep.c
@@ -100,7 +100,7 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
ptrauth_fork(td2, td1);
- tf = (struct trapframe *)STACKALIGN((struct trapframe *)pcb2 - 1);
+ tf = STACKALIGN((struct trapframe *)pcb2 - 1);
bcopy(td1->td_frame, tf, sizeof(*tf));
tf->tf_x[0] = 0;
tf->tf_x[1] = 0;
diff --git a/sys/arm64/include/param.h b/sys/arm64/include/param.h
index f3cdd4e44bc0..919dea4fb3cf 100644
--- a/sys/arm64/include/param.h
+++ b/sys/arm64/include/param.h
@@ -41,7 +41,6 @@
#include <sys/_align.h>
#define STACKALIGNBYTES (16 - 1)
-#define STACKALIGN(p) ((uint64_t)(p) & ~STACKALIGNBYTES)
#ifndef MACHINE
#define MACHINE "arm64"
diff --git a/sys/arm64/linux/linux_sysvec.c b/sys/arm64/linux/linux_sysvec.c
index ac05820f89bc..9afb6fb143d6 100644
--- a/sys/arm64/linux/linux_sysvec.c
+++ b/sys/arm64/linux/linux_sysvec.c
@@ -338,7 +338,7 @@ linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
/* Make room, keeping the stack aligned */
fp--;
- fp = (struct l_sigframe *)STACKALIGN(fp);
+ fp = STACKALIGN(fp);
get_mcontext(td, &uc.uc_mcontext, 0);
uc.uc_sigmask = *mask;
diff --git a/sys/riscv/include/param.h b/sys/riscv/include/param.h
index 471d8b354b82..e1cf50df7d14 100644
--- a/sys/riscv/include/param.h
+++ b/sys/riscv/include/param.h
@@ -37,7 +37,6 @@
#include <sys/_align.h>
#define STACKALIGNBYTES (16 - 1)
-#define STACKALIGN(p) ((uint64_t)(p) & ~STACKALIGNBYTES)
#ifndef MACHINE
#define MACHINE "riscv"
diff --git a/sys/riscv/riscv/exec_machdep.c b/sys/riscv/riscv/exec_machdep.c
index b1f96b1426a7..4e0d1f2a7e5c 100644
--- a/sys/riscv/riscv/exec_machdep.c
+++ b/sys/riscv/riscv/exec_machdep.c
@@ -370,7 +370,7 @@ sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
/* Make room, keeping the stack aligned */
fp--;
- fp = (struct sigframe *)STACKALIGN(fp);
+ fp = STACKALIGN(fp);
/* Fill in the frame to copy out */
bzero(&frame, sizeof(frame));
diff --git a/sys/sys/param.h b/sys/sys/param.h
index 65a244311fee..27e8e0f14e77 100644
--- a/sys/sys/param.h
+++ b/sys/sys/param.h
@@ -179,6 +179,9 @@
#ifndef MAXDUMPPGS
#define MAXDUMPPGS (DFLTPHYS/PAGE_SIZE)
#endif
+#ifdef STACKALIGNBYTES
+#define STACKALIGN(p) (__align_down(p, STACKALIGNBYTES + 1))
+#endif
/*
* Constants related to network buffer management.