git: 093fffa296d0 - main - sys: Use __is_aligned and __align_down for some kstack alignment operations

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Tue, 27 Jan 2026 18:31:46 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=093fffa296d009de8cebf713a58ddbf876f05c5c

commit 093fffa296d009de8cebf713a58ddbf876f05c5c
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2026-01-27 18:31:23 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2026-01-27 18:31:23 +0000

    sys: Use __is_aligned and __align_down for some kstack alignment operations
    
    Reviewed by:    kib, jhibbits
    Effort:         CHERI upstreaming
    Sponsored by:   AFRL, DARPA
    Differential Revision:  https://reviews.freebsd.org/D54840
---
 sys/amd64/amd64/vm_machdep.c       | 3 +--
 sys/i386/i386/vm_machdep.c         | 3 ++-
 sys/powerpc/powerpc/exec_machdep.c | 4 ++--
 sys/powerpc/powerpc/machdep.c      | 5 ++---
 sys/powerpc/powerpc/vm_machdep.c   | 4 ++--
 5 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/sys/amd64/amd64/vm_machdep.c b/sys/amd64/amd64/vm_machdep.c
index c763ff58680e..2e180003e93d 100644
--- a/sys/amd64/amd64/vm_machdep.c
+++ b/sys/amd64/amd64/vm_machdep.c
@@ -93,8 +93,7 @@ set_top_of_stack_td(struct thread *td)
 struct savefpu *
 get_pcb_user_save_td(struct thread *td)
 {
-	KASSERT(((vm_offset_t)td->td_md.md_usr_fpu_save %
-	    XSAVE_AREA_ALIGN) == 0,
+	KASSERT(__is_aligned(td->td_md.md_usr_fpu_save, XSAVE_AREA_ALIGN),
 	    ("Unaligned pcb_user_save area ptr %p td %p",
 	    td->td_md.md_usr_fpu_save, td));
 	return (td->td_md.md_usr_fpu_save);
diff --git a/sys/i386/i386/vm_machdep.c b/sys/i386/i386/vm_machdep.c
index 80b45070d896..698d3b26813c 100644
--- a/sys/i386/i386/vm_machdep.c
+++ b/sys/i386/i386/vm_machdep.c
@@ -91,7 +91,8 @@ get_pcb_user_save_td(struct thread *td)
 
 	p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
 	    roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN);
-	KASSERT((p % XSAVE_AREA_ALIGN) == 0, ("Unaligned pcb_user_save area"));
+	KASSERT(__is_aligned(p, XSAVE_AREA_ALIGN),
+	    ("Unaligned pcb_user_save area"));
 	return ((union savefpu *)p);
 }
 
diff --git a/sys/powerpc/powerpc/exec_machdep.c b/sys/powerpc/powerpc/exec_machdep.c
index 318927e01360..00c04b4ddbaa 100644
--- a/sys/powerpc/powerpc/exec_machdep.c
+++ b/sys/powerpc/powerpc/exec_machdep.c
@@ -1082,8 +1082,8 @@ cpu_thread_alloc(struct thread *td)
 {
 	struct pcb *pcb;
 
-	pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
-	    sizeof(struct pcb)) & ~0x3fUL);
+	pcb = (struct pcb *)__align_down(td->td_kstack + td->td_kstack_pages *
+	    PAGE_SIZE - 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 e9979712aa9c..f4a065e1ce46 100644
--- a/sys/powerpc/powerpc/machdep.c
+++ b/sys/powerpc/powerpc/machdep.c
@@ -488,9 +488,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 *)
-	    ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
-	    sizeof(struct pcb)) & ~15UL);
+	thread0.td_pcb = (struct pcb *)__align_down(thread0.td_kstack +
+	    thread0.td_kstack_pages * PAGE_SIZE - sizeof(struct pcb), 16);
 	bzero((void *)thread0.td_pcb, sizeof(struct pcb));
 	pc->pc_curpcb = thread0.td_pcb;
 
diff --git a/sys/powerpc/powerpc/vm_machdep.c b/sys/powerpc/powerpc/vm_machdep.c
index 1fd853783cc8..00fdc301a7e7 100644
--- a/sys/powerpc/powerpc/vm_machdep.c
+++ b/sys/powerpc/powerpc/vm_machdep.c
@@ -123,8 +123,8 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 	if (td1 == curthread)
 		cpu_update_pcb(td1);
 
-	pcb = (struct pcb *)((td2->td_kstack +
-	    td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x3fUL);
+	pcb = (struct pcb *)__align_down(td2->td_kstack +
+	    td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb), 0x40);
 	td2->td_pcb = pcb;
 
 	/* Copy the pcb */