NetBSD Problem Report #59327

From www@netbsd.org  Sat Apr 19 07:24:52 2025
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits)
	 client-signature RSA-PSS (2048 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id D1F841A923D
	for <gnats-bugs@gnats.NetBSD.org>; Sat, 19 Apr 2025 07:24:52 +0000 (UTC)
Message-Id: <20250419072451.91E061A923E@mollari.NetBSD.org>
Date: Sat, 19 Apr 2025 07:24:51 +0000 (UTC)
From: rokuyama.rk@gmail.com
Reply-To: rokuyama.rk@gmail.com
To: gnats-bugs@NetBSD.org
Subject: user stack pointer is not aligned properly
X-Send-Pr-Version: www-1.0

>Number:         59327
>Category:       kern
>Synopsis:       user stack pointer is not aligned properly
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kern-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Apr 19 07:25:00 +0000 2025
>Last-Modified:  Mon Apr 28 15:35:01 +0000 2025
>Originator:     Rin Okuyama
>Release:        10.99.14 (and prior)
>Organization:
Internet Initiative Japan Inc.
>Environment:
NetBSD erlite 10.99.14 NetBSD 10.99.14 (OCTEON) #5: Sat Apr 19 14:40:33 JST 2025  rin@sakaizumii.local:/home/rin/src/sys/arch/evbmips/compile/OCTEON evbmips mipsn64eb
>Description:
User stack pointer should be aligned to (1) 16-byte boundaries for
n{32,64}, and (2) 8-byte boundaries for o{32,64} ABIs, respectively.

However, our codes do not take care of it, except for makecontext(3).

For other architectures, similar bugs have been found and fixed with
GCC updates; newer versions of GCC much more eagerly optimizes codes
by using constraints for usp.

However, for mips, (a) SIMD-based, i.e., alignment-peaky optimization
is not used (IIUC), and (b) for many cases, usp may be aligned to
8-byte boundaries at least, even without any care.

These are probably reasons why the problem had been left uncovered.

Checklist for testing and fixing misaligned stack pointers:
[tested/pass] aarch64
[ ] aarch64 compat32
[tested/pass] alpha
[ ] arm32
[tested/pass] hppa
[tested/pass] i386
[ ] ia64
[ ] m68k
[ ] mips o32
[tested/pass] mips64 n32
[ ] mips64 n64
[ ] mips64 compat n32
[ ] powerpc
[ ] powerpc64
[ ] powerpc64 compat32
[ ] riscv32
[tested/pass] riscv64
[ ] riscv64 compat32
[ ] sh3
[tested/pass] sparc
[ ] sparc64
[ ] sparc64 compat32
[ ] vax
[tested/pass] x86_64
[tested/pass] x86_64 compat32
>How-To-Repeat:
Code inspection during unsuccessful trials for PR port-evbmips/59236.
>Fix:
Suggested patches:
https://gist.github.com/rokuyama/48aaa64604efb4321521e59b9dc92da8

I've confirmed that there's no regression for full ATF run for
evbmips**n**64-eb (n64) on erlite3 (with HAVE_JEMALLOC=510).

Note that many rump-based cases do not work for evbmips64-eb (n32),
with and without the fix.

>Release-Note:

>Audit-Trail:
From: Taylor R Campbell <riastradh@NetBSD.org>
To: Rin Okuyama <rokuyama.rk@gmail.com>
Cc: gnats-bugs@netbsd.org, port-mips-maintainer@netbsd.org,
	gnats-admin@netbsd.org, netbsd-bugs@netbsd.org
Subject: Re: port-mips/59327: user stack pointer is not aligned properly
Date: Sat, 19 Apr 2025 11:11:52 +0000

 > Date: Sat, 19 Apr 2025 07:25:00 +0000 (UTC)
 > From: rokuyama.rk@gmail.com
 > 
 > User stack pointer should be aligned to (1) 16-byte boundaries for
 > n{32,64}, and (2) 8-byte boundaries for o{32,64} ABIs, respectively.
 > 
 > However, our codes do not take care of it, except for makecontext(3).
 > [...]
 > Suggested patches:
 > https://gist.github.com/rokuyama/48aaa64604efb4321521e59b9dc92da8

 This looks good but I think the most important step is to have
 automatic tests to verify stack alignment requirements on all
 architectures, for any entry points from the kernel:

 1. program start after exec
 2. signal handler
 3. signal return (see PR 58149 for this weird case)
 4. (anything else I'm forgetting?)

 We already have tests/kernel/t_signal_and_sp.c for testing PR 58149 on
 aarch64; we should:

 1. extend it with other architectures,
 2. make sure it matches a STACK_ALIGNBYTES definition rather than
    hard-coding 16 in the test,
 3. add an entry point stub that saves the initial stack pointer in a
    global variable and calls the real __start so we can verify it, and
 4. add a statically linked helper program to do the same so we check
    both ld.elf_so entry and kernel entry.

From: Taylor R Campbell <riastradh@NetBSD.org>
To: Rin Okuyama <rokuyama.rk@gmail.com>
Cc: gnats-bugs@netbsd.org,
	port-mips-maintainer@netbsd.org, gnats-admin@netbsd.org, netbsd-bugs@netbsd.org
Subject: Re: port-mips/59327: user stack pointer is not aligned properly
Date: Sat, 19 Apr 2025 19:58:35 +0000

 This is a multi-part message in MIME format.
 --=_tarl6EbRoME+6V+rh/yI3C5H/YL5MT1L

 Here's a draft to add automatic tests on aarch64, amd64, alpha, and
 mips, with WIP on hppa, for whether the stack is appropriately aligned
 on various entry points to userland.  alpha and mips fail a lot of
 these tests.  More work to be done, just sharing this now in case you
 want to try it out.

 --=_tarl6EbRoME+6V+rh/yI3C5H/YL5MT1L
 Content-Type: text/plain; charset="ISO-8859-1"; name="pr59327-stackalign"
 Content-Transfer-Encoding: quoted-printable
 Content-Disposition: attachment; filename="pr59327-stackalign.patch"

 # HG changeset patch
 # User Taylor R Campbell <riastradh@NetBSD.org>
 # Date 1745077373 0
 #      Sat Apr 19 15:42:53 2025 +0000
 # Branch trunk
 # Node ID a15ce6172f14912aed11abf8edfd050b30aab9be
 # Parent  b752b098d2ee168dcb79a215c54e8ec0ed634f09
 # EXP-Topic riastradh-pr59327-stackalign
 WIP: h_execregs: Properly request to statically link this.

 Adding -static to LDFLAGS doesn't work because we end up passing
 various other flags that cause it to come out as a dynamic executable
 anyway, which would crash on SIGSEGV early at startup -- except that
 the entry point exits before calling the normal start routine, so by
 accident it works.

 XXX cite the PR

 diff -r b752b098d2ee -r a15ce6172f14 tests/kernel/Makefile
 --- a/tests/kernel/Makefile	Fri Apr 18 02:16:16 2025 +0000
 +++ b/tests/kernel/Makefile	Sat Apr 19 15:42:53 2025 +0000
 @@ -142,7 +142,8 @@ CPPFLAGS.t_execregs.c+=3D	-I${.CURDIR}/../
  CPPFLAGS.t_execregs.c+=3D	-DHAVE_EXECREGS_TEST
  SRCS.t_execregs+=3D	t_execregs.c
  SRCS.t_execregs+=3D	execregs.c
 -LDFLAGS.h_execregs+=3D	-static -Wl,-e,execregs_start
 +LDSTATIC.h_execregs=3D	-static
 +LDFLAGS.h_execregs+=3D	-Wl,-e,execregs_start
  .else
  SRCS.h_execregs=3D	h_execregs_unimpl.c
  .endif
 # HG changeset patch
 # User Taylor R Campbell <riastradh@NetBSD.org>
 # Date 1745073355 0
 #      Sat Apr 19 14:35:55 2025 +0000
 # Branch trunk
 # Node ID d7ffc903763d7fcf264ee539e89b315b081d289d
 # Parent  a15ce6172f14912aed11abf8edfd050b30aab9be
 # EXP-Topic riastradh-pr59327-stackalign
 Test stack pointer alignment in various scenarios.

 1. elf entry point
 2. main function
 3. signal handler

 This extends the test that was previously written for:

 PR kern/58149: aarch64: Cannot return from a signal handler if SP was
 misaligned when the signal arrived

 With any luck, this will help us to systematically eradicate misaligned
 stack pointers as hypothesized in:

 PR port-mips/59236: Multiple segfaults in erlite3 boot

 diff -r a15ce6172f14 -r d7ffc903763d distrib/sets/lists/debug/mi
 --- a/distrib/sets/lists/debug/mi	Sat Apr 19 15:42:53 2025 +0000
 +++ b/distrib/sets/lists/debug/mi	Sat Apr 19 14:35:55 2025 +0000
 @@ -1775,6 +1775,8 @@
  ./usr/libdata/debug/usr/tests/kernel/arch/i386/t_ptrace_waitpid.debug	test=
 s-obsolete		obsolete,compattestfile
  ./usr/libdata/debug/usr/tests/kernel/h_cloexec.debug			tests-kernel-tests	=
 debug,atf,compattestfile
  ./usr/libdata/debug/usr/tests/kernel/h_execregs.debug			tests-kernel-tests=
 	debug,atf,compattestfile
 +./usr/libdata/debug/usr/tests/kernel/h_execsp_dynamic.debug		tests-kernel-=
 tests	debug,atf,compattestfile
 +./usr/libdata/debug/usr/tests/kernel/h_execsp_static.debug		tests-kernel-t=
 ests	debug,atf,compattestfile
  ./usr/libdata/debug/usr/tests/kernel/h_fexecve.debug			tests-kernel-tests	=
 debug,atf,compattestfile
  ./usr/libdata/debug/usr/tests/kernel/h_fpufork.debug			tests-kernel-tests	=
 debug,atf,compattestfile
  ./usr/libdata/debug/usr/tests/kernel/h_getprocpath.debug		tests-kernel-tes=
 ts	debug,atf,compattestfile
 diff -r a15ce6172f14 -r d7ffc903763d distrib/sets/lists/tests/mi
 --- a/distrib/sets/lists/tests/mi	Sat Apr 19 15:42:53 2025 +0000
 +++ b/distrib/sets/lists/tests/mi	Sat Apr 19 14:35:55 2025 +0000
 @@ -2271,6 +2271,8 @@
  ./usr/tests/kernel/arch/x86				tests-obsolete		obsolete
  ./usr/tests/kernel/h_cloexec				tests-kernel-tests	compattestfile,atf
  ./usr/tests/kernel/h_execregs				tests-kernel-tests	compattestfile,atf
 +./usr/tests/kernel/h_execsp_dynamic			tests-kernel-tests	compattestfile,atf
 +./usr/tests/kernel/h_execsp_static			tests-kernel-tests	compattestfile,atf
  ./usr/tests/kernel/h_fexecve				tests-kernel-tests	compattestfile,atf
  ./usr/tests/kernel/h_fpufork				tests-kernel-tests	compattestfile,atf
  ./usr/tests/kernel/h_getprocpath			tests-kernel-tests	compattestfile,atf
 diff -r a15ce6172f14 -r d7ffc903763d tests/kernel/Makefile
 --- a/tests/kernel/Makefile	Sat Apr 19 15:42:53 2025 +0000
 +++ b/tests/kernel/Makefile	Sat Apr 19 14:35:55 2025 +0000
 @@ -53,6 +53,8 @@ TESTS_SH+=3D	t_umount
  BINDIR=3D		${TESTSDIR}
  PROGS+=3D		h_cloexec
  PROGS+=3D		h_execregs
 +PROGS+=3D		h_execsp_dynamic
 +PROGS+=3D		h_execsp_static
  PROGS+=3D		h_fexecve
  PROGS+=3D		h_fpufork
  PROGS+=3D		h_getprocpath
 @@ -93,11 +95,32 @@ LDADD.t_timeleft+=3D	-lpthread
  CPPFLAGS+=3D	-D_KERNTYPES
  CPPFLAGS.t_unmount.c+=3D	-D_KMEMUSER -D__EXPOSE_MOUNT
 =20
 -ARCH_INCS_DIR:=3D	${.PARSEDIR}/arch/${MACHINE_ARCH}
 -.if exists(${ARCH_INCS_DIR}/stack_pointer.h)
 -CPPFLAGS+=3D	-I${ARCH_INCS_DIR} -DHAVE_STACK_POINTER_H
 +ARCHDIR:=3D		${.PARSEDIR}/arch/${MACHINE_ARCH}
 +.PATH:			${ARCHDIR}
 +
 +.if exists(${ARCHDIR}/stack_pointer.h)
 +CPPFLAGS.t_signal_and_sp.c+=3D	-I${ARCHDIR} -DHAVE_STACK_POINTER_H
 +.endif
 +
 +SRCS.t_signal_and_sp+=3D		t_signal_and_sp.c
 +.if exists(${ARCHDIR}/signalsphandler.S)
 +CPPFLAGS.t_signal_and_sp.c+=3D	-DHAVE_SIGNALSPHANDLER
 +SRCS.t_signal_and_sp+=3D		signalsphandler.S
  .endif
 =20
 +SRCS.h_execsp_dynamic+=3D		h_execsp.c
 +SRCS.h_execsp_static+=3D		h_execsp.c
 +.if exists(${ARCHDIR}/execsp.S)
 +CPPFLAGS.t_signal_and_sp.c+=3D	-DHAVE_EXECSP
 +SRCS.h_execsp_dynamic+=3D		execsp.S
 +SRCS.h_execsp_static+=3D		execsp.S
 +LDFLAGS.h_execsp_dynamic+=3D	-Wl,-e,execsp_start
 +LDFLAGS.h_execsp_static+=3D	-Wl,-e,execsp_start
 +.else
 +CPPFLAGS.h_execsp.c+=3D		-Dexecsp_main=3Dmain
 +.endif
 +LDSTATIC.h_execsp_static=3D	-static
 +
  .PATH:			${NETBSDSRCDIR}/sys/kern
  TESTS_C+=3D		t_extent
  SRCS.t_extent=3D		t_extent.c subr_extent.c
 @@ -135,8 +158,6 @@ CLEANFILES+=3D	t_subr_prf.c
  LDADD.h_segv+=3D	-lm
 =20
  .if exists(arch/${MACHINE_ARCH}/execregs.h)
 -ARCHDIR:=3D		${.PARSEDIR}/arch/${MACHINE_ARCH}
 -.PATH:			${ARCHDIR}
  CPPFLAGS.t_execregs.c+=3D	-I${ARCHDIR}
  CPPFLAGS.t_execregs.c+=3D	-I${.CURDIR}/../lib/libc/gen # isqemu.h
  CPPFLAGS.t_execregs.c+=3D	-DHAVE_EXECREGS_TEST
 diff -r a15ce6172f14 -r d7ffc903763d tests/kernel/arch/aarch64/execsp.S
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/aarch64/execsp.S	Sat Apr 19 14:35:55 2025 +0000
 @@ -0,0 +1,60 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#define	_LOCORE
 +
 +#include <machine/asm.h>
 +
 +RCSID("$NetBSD$")
 +
 +/*
 + * void execsp_start(void (*cleanup@x0)(void), void *obj_main@x1,
 + *     struct ps_strings *ps_strings@x2)
 + *
 + *	ELF entry point.  Saves the stack pointer in startsp and defers
 + *	to the usual csu __start routine.
 + */
 +ENTRY(execsp_start)
 +	mov	x16, sp
 +	adrp	x17, :pg_hi21:_C_LABEL(startsp)
 +	str	x16, [x17, #:lo12:_C_LABEL(startsp)]
 +	b	_C_LABEL(__start)
 +END(execsp_start)
 +
 +/*
 + * int main(int argc@x0, char **argv@x1, ...)
 + *
 + *	Main function.  Saves the stack pointer in mainsp and defers to
 + *	the C execsp_main in h_execsp.c.
 + */
 +ENTRY(main)
 +	mov	x16, sp
 +	adrp	x17, :pg_hi21:_C_LABEL(mainsp)
 +	str	x16, [x17, #:lo12:_C_LABEL(mainsp)]
 +	b	_C_LABEL(execsp_main)
 +END(main)
 diff -r a15ce6172f14 -r d7ffc903763d tests/kernel/arch/aarch64/signalsphand=
 ler.S
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/aarch64/signalsphandler.S	Sat Apr 19 14:35:55 2025 =
 +0000
 @@ -0,0 +1,46 @@
 +/*	$NetBSD: h_execregs.S,v 1.1 2025/02/27 00:55:31 riastradh Exp $	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#define	_LOCORE
 +
 +#include <machine/asm.h>
 +
 +RCSID("$NetBSD$")
 +
 +/*
 + * signalsphandler(signo@x0)
 + *
 + *	Signal handler.  Store the stack pointer on entry at the global
 + *	variable signalsp and return.
 + */
 +ENTRY(signalsphandler)
 +	mov	x0, sp
 +	adrp	x1, :pg_hi21:_C_LABEL(signalsp)
 +	str	x0, [x1, #:lo12:_C_LABEL(signalsp)]
 +	ret
 +END(signalsphandler)
 diff -r a15ce6172f14 -r d7ffc903763d tests/kernel/arch/aarch64/stack_pointe=
 r.h
 --- a/tests/kernel/arch/aarch64/stack_pointer.h	Sat Apr 19 15:42:53 2025 +0=
 000
 +++ b/tests/kernel/arch/aarch64/stack_pointer.h	Sat Apr 19 14:35:55 2025 +0=
 000
 @@ -1,4 +1,4 @@
 -/* $NetBSD: stack_pointer.h,v 1.1 2024/04/22 07:24:22 pho Exp $ */
 +/*	$NetBSD: stack_pointer.h,v 1.1 2024/04/22 07:24:22 pho Exp $	*/
 =20
  /*
   * Copyright (c) 2024 The NetBSD Foundation, Inc.
 @@ -26,18 +26,8 @@
   * POSSIBILITY OF SUCH DAMAGE.
   */
 =20
 -#include <sys/stdbool.h>
 -#include <sys/stdint.h>
 -
 -#define LOAD_SP(var)					\
 -	do {						\
 -		register void* tmp __asm__("x0");	\
 -		__asm__(				\
 -			"mov %0, sp"			\
 -			: "=3Dr"(tmp)			\
 -		);					\
 -		(var) =3D tmp;				\
 -	} while (0)
 +#ifndef	TESTS_KERNEL_ARCH_AARCH64_STACK_POINTER_H
 +#define	TESTS_KERNEL_ARCH_AARCH64_STACK_POINTER_H
 =20
  #define MISALIGN_SP				\
  	__asm__ volatile (			\
 @@ -49,8 +39,4 @@
  		"add sp, sp, #8"		\
  	)
 =20
 -static inline bool
 -is_sp_aligned(uintptr_t sp)
 -{
 -	return sp % 16 =3D=3D 0;
 -}
 +#endif	/* TESTS_KERNEL_ARCH_AARCH64_STACK_POINTER_H */
 diff -r a15ce6172f14 -r d7ffc903763d tests/kernel/arch/x86_64/execsp.S
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/x86_64/execsp.S	Sat Apr 19 14:35:55 2025 +0000
 @@ -0,0 +1,74 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#define	_LOCORE
 +
 +#include <machine/asm.h>
 +
 +RCSID("$NetBSD$")
 +
 +/*
 + * void execsp_start(void (*cleanup@rbx)(void), void *obj_main@rcx,
 + *     struct ps_strings *ps_strings@rbx)
 + *
 + *	ELF entry point.  Saves the stack pointer in startsp and defers
 + *	to the usual csu __start routine.
 + */
 +ENTRY(execsp_start)
 +	movq	%rsp,_C_LABEL(startsp)(%rip)
 +	/*
 +	 * No adjustment like in main because entry point is special
 +	 * and the amd64 csu __start routine takes care of it.
 +	 *
 +	 * XXX Why don't we just arrange to align it in the kernel
 +	 * anyway?
 +	 */
 +	jmp	_C_LABEL(__start)
 +END(execsp_start)
 +
 +/*
 + * int main(int argc@rdi, char **argv@rsi, ...)
 + *
 + *	Main function.  Saves the stack pointer in mainsp and defers to
 + *	the C execsp_main in h_execsp.c.
 + */
 +ENTRY(main)
 +	/*
 +	 * `The end of the input argument area shall be aligned on a
 +	 *  [16-byte] boundary.  In other words, the value of (%rsp + 8)
 +	 *  is always a multiple of 16 when control is transferred to
 +	 *  the function entry point.'
 +	 *
 +	 * To make it convenient for t_signal_and_sp.c, we subtract 8
 +	 * from %rsp in order to get something congruent to zero modulo
 +	 * the stack alignemnt.
 +	 */
 +	movq	%rsp,_C_LABEL(mainsp)(%rip)
 +	addq	$-8,_C_LABEL(mainsp)(%rip)
 +	jmp	_C_LABEL(execsp_main)
 +END(main)
 diff -r a15ce6172f14 -r d7ffc903763d tests/kernel/arch/x86_64/signalsphandl=
 er.S
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/x86_64/signalsphandler.S	Sat Apr 19 14:35:55 2025 +=
 0000
 @@ -0,0 +1,55 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#define	_LOCORE
 +
 +#include <machine/asm.h>
 +
 +RCSID("$NetBSD$")
 +
 +/*
 + * signalsphandler(signo@rdi)
 + *
 + *	Signal handler.  Store the stack pointer on entry at the global
 + *	variable signalsp and return.
 + */
 +ENTRY(signalsphandler)
 +	/*
 +	 * `The end of the input argument area shall be aligned on a
 +	 *  [16-byte] boundary.  In other words, the value of (%rsp + 8)
 +	 *  is always a multiple of 16 when control is transferred to
 +	 *  the function entry point.'
 +	 *
 +	 * To make it convenient for t_signal_and_sp.c, we subtract 8
 +	 * from %rsp in order to get something congruent to zero modulo
 +	 * the stack alignemnt.
 +	 */
 +	movq	%rsp,_C_LABEL(signalsp)(%rip)
 +	addq	$-8,_C_LABEL(signalsp)(%rip)
 +	ret
 +END(signalsphandler)
 diff -r a15ce6172f14 -r d7ffc903763d tests/kernel/arch/x86_64/stack_pointer=
 .h
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/x86_64/stack_pointer.h	Sat Apr 19 14:35:55 2025 +00=
 00
 @@ -0,0 +1,35 @@
 +/*	$NetBSD$	*/
 +
 +/*
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#ifndef	TESTS_KERNEL_ARCH_X86_64_STACK_POINTER_H
 +#define	TESTS_KERNEL_ARCH_X86_64_STACK_POINTER_H
 +
 +#define MISALIGN_SP	__asm __volatile("addq $-1,%rsp")
 +#define FIX_SP		__asm __volatile("addq $1,%rsp")
 +
 +#endif	/* TESTS_KERNEL_ARCH_X86_64_STACK_POINTER_H */
 diff -r a15ce6172f14 -r d7ffc903763d tests/kernel/h_execsp.c
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/h_execsp.c	Sat Apr 19 14:35:55 2025 +0000
 @@ -0,0 +1,63 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#include <sys/cdefs.h>
 +__RCSID("$NetBSD$");
 +
 +#include <err.h>
 +#include <string.h>
 +#include <unistd.h>
 +
 +#include "h_execsp.h"
 +
 +/*
 + * The machine-dependent execsp.S assembly routines will initialize
 + * startsp and mainsp and then call execsp_main on program startup.
 + */
 +void *startsp;
 +void *mainsp;
 +
 +int execsp_main(void);
 +int
 +execsp_main(void)
 +{
 +	struct execsp execsp;
 +	ssize_t nwrit;
 +
 +	memset(&execsp, 0, sizeof(execsp));
 +	execsp.startsp =3D startsp;
 +	execsp.mainsp =3D mainsp;
 +
 +	nwrit =3D write(STDOUT_FILENO, &execsp, sizeof(execsp));
 +	if (nwrit =3D=3D -1)
 +		err(1, "write");
 +	if ((size_t)nwrit !=3D sizeof(execsp))
 +		errx(1, "wrote %zu !=3D %zu", (size_t)nwrit, sizeof(execsp));
 +
 +	return 0;
 +}
 diff -r a15ce6172f14 -r d7ffc903763d tests/kernel/h_execsp.h
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/h_execsp.h	Sat Apr 19 14:35:55 2025 +0000
 @@ -0,0 +1,44 @@
 +/*	$NetBSD: t_signal_and_sp.c,v 1.1 2024/04/22 07:24:22 pho Exp $	*/
 +
 +/*
 + * Copyright (c) 2024 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#ifndef	TESTS_KERNEL_H_EXECSP_H
 +#define	TESTS_KERNEL_H_EXECSP_H
 +
 +/*
 + * struct execsp
 + *
 + *	Structure passed from the h_execsp_* programs to the
 + *	t_signal_and_sp test, giving the stack pointer as it was at the
 + *	ELF entry point and the main function.
 + */
 +struct execsp {
 +	void	*startsp;
 +	void	*mainsp;
 +};
 +
 +#endif	/* TESTS_KERNEL_H_EXECSP_H */
 diff -r a15ce6172f14 -r d7ffc903763d tests/kernel/t_signal_and_sp.c
 --- a/tests/kernel/t_signal_and_sp.c	Sat Apr 19 15:42:53 2025 +0000
 +++ b/tests/kernel/t_signal_and_sp.c	Sat Apr 19 14:35:55 2025 +0000
 @@ -1,4 +1,4 @@
 -/* $NetBSD: t_signal_and_sp.c,v 1.1 2024/04/22 07:24:22 pho Exp $ */
 +/*	$NetBSD: t_signal_and_sp.c,v 1.1 2024/04/22 07:24:22 pho Exp $	*/
 =20
  /*
   * Copyright (c) 2024 The NetBSD Foundation, Inc.
 @@ -26,32 +26,259 @@
   * POSSIBILITY OF SUCH DAMAGE.
   */
 =20
 +#include <sys/cdefs.h>
 +__RCSID("$NetBSD$");
 +
 +#include <sys/wait.h>
 +
 +#include <machine/param.h>
 +
  #include <atf-c.h>
 +#include <limits.h>
 +#include <poll.h>
 +#include <signal.h>
 +#include <stdint.h>
 +#include <stdio.h>
 +#include <string.h>
 +#include <time.h>
 +#include <unistd.h>
 +
 +#include "h_execsp.h"
 +#include "h_macros.h"
 +
 +#ifdef HAVE_STACK_POINTER_H
 +#  include "stack_pointer.h"
 +#endif
 =20
 -#if defined(HAVE_STACK_POINTER_H)
 -#  include <signal.h>
 -#  include <string.h>
 -#  include <sys/stdint.h>
 -#  include <sys/time.h>
 -#  include "stack_pointer.h"
 +#ifdef HAVE_SIGNALSPHANDLER
 +void signalsphandler(int);	/* signalsphandler.S assembly routine */
 +#endif
 +
 +void *volatile signalsp;
 +
 +static void
 +test_execsp(const struct atf_tc *tc, const char *prog)
 +{
 +#ifdef STACK_ALIGNBYTES
 +	char h_execsp[PATH_MAX];
 +	struct execsp execsp;
 +	int fd[2];
 +	pid_t pid;
 +	struct pollfd pollfd;
 +	int nfds;
 +	ssize_t nread;
 +	int status;
 +
 +	/*
 +	 * Determine the full path to the helper program.
 +	 */
 +	RL(snprintf(h_execsp, sizeof(h_execsp), "%s/%s",
 +		atf_tc_get_config_var(tc, "srcdir"), prog));
 =20
 -static volatile void* stack_pointer =3D NULL;
 -static void on_alarm(int sig __attribute__((__unused__)))
 -{
 +	/*
 +	 * Create a pipe to read a bundle of stack pointer samples from
 +	 * the child, and fork the child.
 +	 */
 +	RL(pipe(fd));
 +	RL(pid =3D vfork());
 +	if (pid =3D=3D 0) {		/* child */
 +		char *const argv[] =3D {h_execsp, NULL};
 +
 +		if (dup2(fd[1], STDOUT_FILENO) =3D=3D -1)
 +			_exit(1);
 +		if (closefrom(STDERR_FILENO + 1) =3D=3D -1)
 +			_exit(2);
 +		if (execve(argv[0], argv, NULL) =3D=3D -1)
 +			_exit(3);
 +		_exit(4);
 +	}
 +
 +	/*
 +	 * Close the writing end so, if something goes wrong in the
 +	 * child, we don't hang indefinitely waiting for output.
 +	 */
 +	RL(close(fd[1]));
 +
  	/*
 -	 * Store the stack pointer into a variable so that we can test if
 -	 * it's aligned.
 +	 * Wait up to 5sec for the child to return an answer.  Any more
 +	 * than that, and we kill it.  The child is mostly hand-written
 +	 * assembly routines where lots can go wrong, so don't bother
 +	 * waiting if it gets stuck in a loop.
 +	 */
 +	pollfd.fd =3D fd[0];
 +	pollfd.events =3D POLLIN;
 +	RL(nfds =3D poll(&pollfd, 1, 5*1000/*ms*/));
 +	if (nfds =3D=3D 0) {
 +		fprintf(stderr, "child hung, killing\n");
 +		RL(kill(pid, SIGKILL));
 +	}
 +
 +	/*
 +	 * Read a bundle of stack pointer samples from the child.
  	 */
 -	LOAD_SP(stack_pointer);
 +	RL(nread =3D read(fd[0], &execsp, sizeof(execsp)));
 +	ATF_CHECK_MSG((size_t)nread =3D=3D sizeof(execsp),
 +	    "nread=3D%zu sizeof(execsp)=3D%zu",
 +	    (size_t)nread, sizeof(execsp));
 +
 +	/*
 +	 * Wait for the child to terminate and report failure if it
 +	 * didn't exit cleanly.
 +	 */
 +	RL(waitpid(pid, &status, 0));
 +	if (WIFSIGNALED(status)) {
 +		atf_tc_fail_nonfatal("child exited on signal %d (%s)",
 +		    WTERMSIG(status), strsignal(WTERMSIG(status)));
 +	} else if (!WIFEXITED(status)) {
 +		atf_tc_fail_nonfatal("child exited status=3D0x%x", status);
 +	} else {
 +		ATF_CHECK_MSG(WEXITSTATUS(status) =3D=3D 0,
 +		    "child exited with code %d",
 +		    WEXITSTATUS(status));
 +	}
 =20
  	/*
 -	 * Now we are going to return from a signal
 -	 * handler. __sigtramp_siginfo_2 will call setcontext(2) with a
 -	 * ucontext provided by the kernel. When that fails it will call
 -	 * _Exit(2) with the errno, and the test will fail.
 +	 * Now that we have reaped the child, stop here if the stack
 +	 * pointer samples are bogus; otherwise verify they are all
 +	 * aligned.
 +	 */
 +	if ((size_t)nread !=3D sizeof(execsp))
 +		return;		/* failed already */
 +
 +	printf("start sp @ %p\n", execsp.startsp);
 +	printf("main sp @ %p\n", execsp.mainsp);
 +
 +	ATF_CHECK_MSG(((uintptr_t)execsp.startsp & STACK_ALIGNBYTES) =3D=3D 0,
 +	    "elf entry point was called with misaligned sp: %p",
 +	    execsp.startsp);
 +
 +	ATF_CHECK_MSG(((uintptr_t)execsp.mainsp & STACK_ALIGNBYTES) =3D=3D 0,
 +	    "main function was called with misaligned sp: %p",
 +	    execsp.mainsp);
 +
 +	/*
 +	 * Leave a reminder on architectures for which we haven't
 +	 * implemented execsp_start.S.
  	 */
 +	if (execsp.startsp =3D=3D NULL || execsp.mainsp =3D=3D NULL)
 +		atf_tc_skip("Not fully supported on this architecture");
 +#else
 +	atf_tc_skip("Unknown STACK_ALIGNBYTES on this architecture");
 +#endif
  }
 +
 +ATF_TC(execsp_dynamic);
 +ATF_TC_HEAD(execsp_dynamic, tc)
 +{
 +	atf_tc_set_md_var(tc, "descr",
 +	    "Verify stack pointer is aligned on dynamic program start");
 +}
 +ATF_TC_BODY(execsp_dynamic, tc)
 +{
 +	test_execsp(tc, "h_execsp_dynamic");
 +}
 +
 +ATF_TC(execsp_static);
 +ATF_TC_HEAD(execsp_static, tc)
 +{
 +	atf_tc_set_md_var(tc, "descr",
 +	    "Verify stack pointer is aligned on static program start");
 +}
 +ATF_TC_BODY(execsp_static, tc)
 +{
 +	test_execsp(tc, "h_execsp_static");
 +}
 +
 +ATF_TC(signalsp);
 +ATF_TC_HEAD(signalsp, tc)
 +{
 +	atf_tc_set_md_var(tc, "descr",
 +	    "Verify stack pointer is aligned on entry to signal handler");
 +}
 +ATF_TC_BODY(signalsp, tc)
 +{
 +#if defined STACK_ALIGNBYTES && defined HAVE_SIGNALSPHANDLER
 +	struct sigaction sa;
 +
 +	memset(&sa, 0, sizeof(sa));
 +	sa.sa_handler =3D &signalsphandler;
 +	RL(sigaction(SIGUSR1, &sa, NULL));
 +	RL(raise(SIGUSR1));
 +
 +	ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) =3D=3D 0,
 +	    "signal handler was called with a misaligned sp: %p",
 +	    signalsp);
 +#else
 +	atf_tc_skip("Not implemented on this platform");
  #endif
 +}
 +
 +ATF_TC(signalsp_sigaltstack);
 +ATF_TC_HEAD(signalsp_sigaltstack, tc)
 +{
 +	atf_tc_set_md_var(tc, "descr",
 +	    "Verify stack pointer is aligned on entry to signal handler"
 +	    " with maximally misaligned sigaltstack");
 +}
 +ATF_TC_BODY(signalsp_sigaltstack, tc)
 +{
 +#if defined STACK_ALIGNBYTES && HAVE_SIGNALSPHANDLER
 +	char *stack;
 +	struct sigaction sa;
 +	struct sigaltstack ss;
 +	unsigned i;
 +
 +	memset(&sa, 0, sizeof(sa));
 +	sa.sa_handler =3D &signalsphandler;
 +	sa.sa_flags =3D SA_ONSTACK;
 +	RL(sigaction(SIGUSR1, &sa, NULL));
 +
 +	/*
 +	 * Allocate a signal stack with enough slop to try all possible
 +	 * misalignments of the stack pointer.  Print it to stderr so
 +	 * it always appears in atf output before shenanigans happen.
 +	 */
 +	REQUIRE_LIBC(stack =3D malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
 +	fprintf(stderr, "stack @ [%p, %p)",
 +	    stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
 +
 +	/*
 +	 * Try with all alignments of high addresses.
 +	 */
 +	for (i =3D 0; i <=3D STACK_ALIGNBYTES; i++) {
 +		ss.ss_sp =3D stack;
 +		ss.ss_size =3D SIGSTKSZ + i;
 +		ss.ss_flags =3D 0;
 +		RL(sigaltstack(&ss, NULL));
 +
 +		signalsp =3D NULL;
 +		RL(raise(SIGUSR1));
 +		ATF_CHECK(signalsp !=3D NULL);
 +		ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) =3D=3D 0,
 +		    "[%u] signal handler was called with a misaligned sp: %p",
 +		    i, signalsp);
 +	}
 +
 +	/*
 +	 * Try with all alignments of low addresses.
 +	 */
 +	for (i =3D 0; i <=3D STACK_ALIGNBYTES; i++) {
 +		ss.ss_sp =3D stack + i;
 +		ss.ss_size =3D SIGSTKSZ;
 +		ss.ss_flags =3D 0;
 +		RL(sigaltstack(&ss, NULL));
 +
 +		signalsp =3D NULL;
 +		RL(raise(SIGUSR1));
 +		ATF_CHECK(signalsp !=3D NULL);
 +		ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) =3D=3D 0,
 +		    "[%u] signal handler was called with a misaligned sp: %p",
 +		    i, signalsp);
 +	}
 +#else
 +	atf_tc_skip("Not implemented on this platform");
 +#endif
 +}
 =20
  ATF_TC(misaligned_sp_and_signal);
  ATF_TC_HEAD(misaligned_sp_and_signal, tc)
 @@ -62,14 +289,14 @@ ATF_TC_HEAD(misaligned_sp_and_signal, tc
  }
  ATF_TC_BODY(misaligned_sp_and_signal, tc)
  {
 -#if defined(HAVE_STACK_POINTER_H)
 +#if defined STACK_ALIGNBYTES && defined HAVE_STACK_POINTER_H
  	/*
  	 * Set up a handler for SIGALRM.
  	 */
  	struct sigaction sa;
  	memset(&sa, 0, sizeof(sa));
 -	sa.sa_handler =3D &on_alarm;
 -	ATF_REQUIRE(sigaction(SIGALRM, &sa, NULL) =3D=3D 0);
 +	sa.sa_handler =3D &signalsphandler;
 +	RL(sigaction(SIGALRM, &sa, NULL));
 =20
  	/*
  	 * Set up an interval timer so that we receive SIGALRM after 50 ms.
 @@ -77,7 +304,7 @@ ATF_TC_BODY(misaligned_sp_and_signal, tc
  	struct itimerval itv;
  	memset(&itv, 0, sizeof(itv));
  	itv.it_value.tv_usec =3D 1000 * 50;
 -	ATF_REQUIRE(setitimer(ITIMER_MONOTONIC, &itv, NULL) =3D=3D 0);
 +	RL(setitimer(ITIMER_MONOTONIC, &itv, NULL));
 =20
  	/*
  	 * Now misalign the SP. Wait for the signal to arrive and see what
 @@ -85,12 +312,12 @@ ATF_TC_BODY(misaligned_sp_and_signal, tc
  	 * access memory.
  	 */
  	MISALIGN_SP;
 -	while (stack_pointer =3D=3D NULL) {
 +	while (signalsp =3D=3D NULL) {
  		/*
  		 * Make sure the compiler does not optimize this busy loop
  		 * away.
  		 */
 -		__asm__("" : : : "memory");
 +		__asm__("" ::: "memory");
  	}
  	/*
  	 * We could successfully return from a signal handler. Now we
 @@ -102,9 +329,9 @@ ATF_TC_BODY(misaligned_sp_and_signal, tc
  	 * But was the stack pointer aligned when we were on the signal
  	 * handler?
  	 */
 -	ATF_CHECK_MSG(is_sp_aligned((uintptr_t)stack_pointer),
 +	ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) =3D=3D 0,
  	    "signal handler was called with a misaligned sp: %p",
 -	    stack_pointer);
 +	    signalsp);
  #else
  	atf_tc_skip("Not implemented for this platform");
  #endif
 @@ -112,6 +339,11 @@ ATF_TC_BODY(misaligned_sp_and_signal, tc
 =20
  ATF_TP_ADD_TCS(tp)
  {
 +
 +	ATF_TP_ADD_TC(tp, execsp_dynamic);
 +	ATF_TP_ADD_TC(tp, execsp_static);
  	ATF_TP_ADD_TC(tp, misaligned_sp_and_signal);
 +	ATF_TP_ADD_TC(tp, signalsp);
 +	ATF_TP_ADD_TC(tp, signalsp_sigaltstack);
  	return atf_no_error();
  }
 # HG changeset patch
 # User Taylor R Campbell <riastradh@NetBSD.org>
 # Date 1745075915 0
 #      Sat Apr 19 15:18:35 2025 +0000
 # Branch trunk
 # Node ID efb233e8e830d26fe319b7761f800368fd82c74a
 # Parent  d7ffc903763d7fcf264ee539e89b315b081d289d
 # EXP-Topic riastradh-pr59327-stackalign
 t_signal_and_sp: Check sp on elf constructor/destructor entry too.

 XXX Should maybe test both .ctors/.dtors and .init/fini_array.

 PR port-evbmips/59236: Multiple segfaults in erlite3 boot

 diff -r d7ffc903763d -r efb233e8e830 tests/kernel/arch/aarch64/execsp.S
 --- a/tests/kernel/arch/aarch64/execsp.S	Sat Apr 19 14:35:55 2025 +0000
 +++ b/tests/kernel/arch/aarch64/execsp.S	Sat Apr 19 15:18:35 2025 +0000
 @@ -47,14 +47,53 @@ ENTRY(execsp_start)
  END(execsp_start)
 =20
  /*
 + * void execsp_ctor(void)
 + *
 + *	ELF constructor.  Saves the stack pointer in ctorsp and
 + *	returns.
 + */
 +ENTRY(execsp_ctor)
 +	mov	x16, sp
 +	adrp	x17, :pg_hi21:_C_LABEL(ctorsp)
 +	str	x16, [x17, #:lo12:_C_LABEL(ctorsp)]
 +	ret
 +END(execsp_ctor)
 +
 +	/* Make execsp_ctor a constructor. */
 +	.section .init_array,"aw",%init_array
 +	.p2align 3
 +	.xword	execsp_ctor
 +
 +/*
   * int main(int argc@x0, char **argv@x1, ...)
   *
 - *	Main function.  Saves the stack pointer in mainsp and defers to
 - *	the C execsp_main in h_execsp.c.
 + *	Main function.  Saves the stack pointer in mainsp and returns
 + *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
 + *	been initialized.
   */
  ENTRY(main)
  	mov	x16, sp
  	adrp	x17, :pg_hi21:_C_LABEL(mainsp)
  	str	x16, [x17, #:lo12:_C_LABEL(mainsp)]
 +	mov	x0, #0
 +	ret
 +END(main)
 +
 +/*
 + * void execsp_dtor(void)
 + *
 + *	ELF destructor.  Saves the stack pointer in dtorsp and defers
 + *	to the C execsp_main in h_execsp.c to report the stack pointers
 + *	back to the t_signal_and_sp parent.
 + */
 +ENTRY(execsp_dtor)
 +	mov	x16, sp
 +	adrp	x17, :pg_hi21:_C_LABEL(dtorsp)
 +	str	x16, [x17, #:lo12:_C_LABEL(dtorsp)]
  	b	_C_LABEL(execsp_main)
 -END(main)
 +END(execsp_dtor)
 +
 +	/* Make execsp_ctor a destructor. */
 +	.section .fini_array,"aw",%fini_array
 +	.p2align 3
 +	.xword	execsp_dtor
 diff -r d7ffc903763d -r efb233e8e830 tests/kernel/arch/x86_64/execsp.S
 --- a/tests/kernel/arch/x86_64/execsp.S	Sat Apr 19 14:35:55 2025 +0000
 +++ b/tests/kernel/arch/x86_64/execsp.S	Sat Apr 19 15:18:35 2025 +0000
 @@ -52,12 +52,12 @@ ENTRY(execsp_start)
  END(execsp_start)
 =20
  /*
 - * int main(int argc@rdi, char **argv@rsi, ...)
 + * void execsp_ctor(void)
   *
 - *	Main function.  Saves the stack pointer in mainsp and defers to
 - *	the C execsp_main in h_execsp.c.
 + *	ELF constructor.  Saves the stack pointer in ctorsp and
 + *	returns.
   */
 -ENTRY(main)
 +ENTRY(execsp_ctor)
  	/*
  	 * `The end of the input argument area shall be aligned on a
  	 *  [16-byte] boundary.  In other words, the value of (%rsp + 8)
 @@ -68,7 +68,44 @@ ENTRY(main)
  	 * from %rsp in order to get something congruent to zero modulo
  	 * the stack alignemnt.
  	 */
 +	movq	%rsp,_C_LABEL(ctorsp)(%rip)
 +	addq	$-8,_C_LABEL(ctorsp)(%rip)
 +	ret
 +END(execsp_ctor)
 +
 +	/* Make execsp_ctor a constructor. */
 +	.section .ctors,"aw",@progbits
 +	.p2align 3
 +	.quad	execsp_ctor
 +
 +/*
 + * int main(int argc@rdi, char **argv@rsi, ...)
 + *
 + *	Main function.  Saves the stack pointer in mainsp and returns
 + *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
 + *	been initialized.
 + */
 +ENTRY(main)
  	movq	%rsp,_C_LABEL(mainsp)(%rip)
  	addq	$-8,_C_LABEL(mainsp)(%rip)
 +	xorl	%eax,%eax
 +	ret
 +END(main)
 +
 +/*
 + * void execsp_dtor(void)
 + *
 + *	ELF destructor.  Saves the stack pointer in dtorsp and defers
 + *	to the C execsp_main in h_execsp.c to report the stack pointers
 + *	back to the t_signal_and_sp parent.
 + */
 +ENTRY(execsp_dtor)
 +	movq	%rsp,_C_LABEL(dtorsp)(%rip)
 +	addq	$-8,_C_LABEL(dtorsp)(%rip)
  	jmp	_C_LABEL(execsp_main)
 -END(main)
 +END(execsp_dtor)
 +
 +	/* Make execsp_ctor a destructor. */
 +	.section .dtors,"aw",@progbits
 +	.p2align 3
 +	.quad	execsp_dtor
 diff -r d7ffc903763d -r efb233e8e830 tests/kernel/h_execsp.c
 --- a/tests/kernel/h_execsp.c	Sat Apr 19 14:35:55 2025 +0000
 +++ b/tests/kernel/h_execsp.c	Sat Apr 19 15:18:35 2025 +0000
 @@ -37,10 +37,13 @@
 =20
  /*
   * The machine-dependent execsp.S assembly routines will initialize
 - * startsp and mainsp and then call execsp_main on program startup.
 + * startsp, ctorsp, mainsp, and dtorsp, and then call execsp_main on
 + * program startup.
   */
  void *startsp;
 +void *ctorsp;
  void *mainsp;
 +void *dtorsp;
 =20
  int execsp_main(void);
  int
 @@ -51,7 +54,9 @@ execsp_main(void)
 =20
  	memset(&execsp, 0, sizeof(execsp));
  	execsp.startsp =3D startsp;
 +	execsp.ctorsp =3D ctorsp;
  	execsp.mainsp =3D mainsp;
 +	execsp.dtorsp =3D dtorsp;
 =20
  	nwrit =3D write(STDOUT_FILENO, &execsp, sizeof(execsp));
  	if (nwrit =3D=3D -1)
 diff -r d7ffc903763d -r efb233e8e830 tests/kernel/h_execsp.h
 --- a/tests/kernel/h_execsp.h	Sat Apr 19 14:35:55 2025 +0000
 +++ b/tests/kernel/h_execsp.h	Sat Apr 19 15:18:35 2025 +0000
 @@ -34,11 +34,14 @@
   *
   *	Structure passed from the h_execsp_* programs to the
   *	t_signal_and_sp test, giving the stack pointer as it was at the
 - *	ELF entry point and the main function.
 + *	ELF entry point, an ELF constructor, the main function, and an
 + *	ELF destructor.
   */
  struct execsp {
  	void	*startsp;
 +	void	*ctorsp;
  	void	*mainsp;
 +	void	*dtorsp;
  };
 =20
  #endif	/* TESTS_KERNEL_H_EXECSP_H */
 diff -r d7ffc903763d -r efb233e8e830 tests/kernel/t_signal_and_sp.c
 --- a/tests/kernel/t_signal_and_sp.c	Sat Apr 19 14:35:55 2025 +0000
 +++ b/tests/kernel/t_signal_and_sp.c	Sat Apr 19 15:18:35 2025 +0000
 @@ -146,21 +146,34 @@ test_execsp(const struct atf_tc *tc, con
  		return;		/* failed already */
 =20
  	printf("start sp @ %p\n", execsp.startsp);
 +	printf("ctor sp @ %p\n", execsp.ctorsp);
  	printf("main sp @ %p\n", execsp.mainsp);
 +	printf("dtor sp @ %p\n", execsp.dtorsp);
 =20
  	ATF_CHECK_MSG(((uintptr_t)execsp.startsp & STACK_ALIGNBYTES) =3D=3D 0,
  	    "elf entry point was called with misaligned sp: %p",
  	    execsp.startsp);
 =20
 +	ATF_CHECK_MSG(((uintptr_t)execsp.ctorsp & STACK_ALIGNBYTES) =3D=3D 0,
 +	    "elf constructor was called with misaligned sp: %p",
 +	    execsp.ctorsp);
 +
  	ATF_CHECK_MSG(((uintptr_t)execsp.mainsp & STACK_ALIGNBYTES) =3D=3D 0,
  	    "main function was called with misaligned sp: %p",
  	    execsp.mainsp);
 =20
 +	ATF_CHECK_MSG(((uintptr_t)execsp.dtorsp & STACK_ALIGNBYTES) =3D=3D 0,
 +	    "elf destructor was called with misaligned sp: %p",
 +	    execsp.dtorsp);
 +
  	/*
  	 * Leave a reminder on architectures for which we haven't
  	 * implemented execsp_start.S.
  	 */
 -	if (execsp.startsp =3D=3D NULL || execsp.mainsp =3D=3D NULL)
 +	if (execsp.startsp =3D=3D NULL ||
 +	    execsp.ctorsp =3D=3D NULL ||
 +	    execsp.mainsp =3D=3D NULL ||
 +	    execsp.dtorsp =3D=3D NULL)
  		atf_tc_skip("Not fully supported on this architecture");
  #else
  	atf_tc_skip("Unknown STACK_ALIGNBYTES on this architecture");
 # HG changeset patch
 # User Taylor R Campbell <riastradh@NetBSD.org>
 # Date 1745082299 0
 #      Sat Apr 19 17:04:59 2025 +0000
 # Branch trunk
 # Node ID 85af35b9e0c55e3d2f0a06d5cb03569dbe9c02fe
 # Parent  efb233e8e830d26fe319b7761f800368fd82c74a
 # EXP-Topic riastradh-pr59327-stackalign
 t_signal_and_sp: Add alpha support.

 Turns out alpha gets confused by misaligned $sp when a signal is
 delivered too.

 PR kern/58149: Cannot return from a signal handler if SP was
 misaligned when the signal arrived

 diff -r efb233e8e830 -r 85af35b9e0c5 tests/kernel/arch/alpha/execsp.S
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/alpha/execsp.S	Sat Apr 19 17:04:59 2025 +0000
 @@ -0,0 +1,105 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#define	_LOCORE
 +
 +#include <machine/asm.h>
 +
 +RCSID("$NetBSD$")
 +
 +	.set noat
 +	.text
 +
 +/*
 + * void execsp_start(void *stackpointer@a0, void (*cleanup@a1)(void),
 + *     void *obj_main@a2, struct ps_strings *ps_strings@a3)
 + *
 + *	ELF entry point.  Saves the stack pointer in startsp and defers
 + *	to the usual csu __start routine.
 + */
 +LEAF(execsp_start, 4)
 +	LDGP(pv)
 +	ldq	at_reg, startsp(gp)	!literal
 +	stq	sp, 0(at_reg)
 +	ldq	pv, __start(gp)		!literal
 +	jmp	(pv), __start
 +END(execsp_start)
 +
 +/*
 + * void execsp_ctor(void)
 + *
 + *	ELF constructor.  Saves the stack pointer in ctorsp and
 + *	returns.
 + */
 +LEAF(execsp_ctor, 0)
 +	LDGP(pv)
 +	ldq	at_reg, ctorsp(gp)	!literal
 +	stq	sp, 0(at_reg)
 +	RET
 +END(execsp_ctor)
 +
 +	/* Make execsp_ctor a constructor. */
 +	.pushsection .ctors,"aw",@progbits
 +	.p2align 3
 +	.quad	execsp_ctor
 +	.popsection
 +
 +/*
 + * int main(int argc@a0, char **argv@a1, ...)
 + *
 + *	Main function.  Saves the stack pointer in mainsp and returns
 + *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
 + *	been initialized.
 + */
 +LEAF(main, 2)
 +	LDGP(pv)
 +	ldq	at_reg, mainsp(gp)	!literal
 +	stq	sp, 0(at_reg)
 +	RET
 +END(main)
 +
 +/*
 + * void execsp_dtor(void)
 + *
 + *	ELF destructor.  Saves the stack pointer in dtorsp and defers
 + *	to the C execsp_main in h_execsp.c to report the stack pointers
 + *	back to the t_signal_and_sp parent.
 + */
 +LEAF(execsp_dtor, 0)
 +	LDGP(pv)
 +	ldq	at_reg, dtorsp(gp)	!literal
 +	stq	sp, 0(at_reg)
 +	ldq	pv, execsp_main(gp)	!literal
 +	jmp	(pv), execsp_main
 +END(execsp_dtor)
 +
 +	/* Make execsp_ctor a destructor. */
 +	.pushsection .dtors,"aw",@progbits
 +	.p2align 3
 +	.quad	execsp_dtor
 +	.popsection
 diff -r efb233e8e830 -r 85af35b9e0c5 tests/kernel/arch/alpha/signalsphandle=
 r.S
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/alpha/signalsphandler.S	Sat Apr 19 17:04:59 2025 +0=
 000
 @@ -0,0 +1,49 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#define	_LOCORE
 +
 +#include <machine/asm.h>
 +
 +RCSID("$NetBSD$")
 +
 +	.set noat
 +	.text
 +
 +/*
 + * signalsphandler(signo@a0)
 + *
 + *	Signal handler.  Store the stack pointer on entry at the global
 + *	variable signalsp and return.
 + */
 +LEAF(signalsphandler, 1)
 +	LDGP(pv)
 +	ldq	at_reg, signalsp(gp)	!literal
 +	stq	sp, 0(at_reg)
 +	RET
 +END(signalsphandler)
 diff -r efb233e8e830 -r 85af35b9e0c5 tests/kernel/arch/alpha/stack_pointer.h
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/alpha/stack_pointer.h	Sat Apr 19 17:04:59 2025 +0000
 @@ -0,0 +1,35 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#ifndef	TESTS_KERNEL_ARCH_ALPHA_STACK_POINTER_H
 +#define	TESTS_KERNEL_ARCH_ALPHA_STACK_POINTER_H
 +
 +#define MISALIGN_SP	__asm __volatile("lda $sp,-1($sp)" ::: "memory")
 +#define FIX_SP		__asm __volatile("lda $sp,1($sp)" ::: "memory")
 +
 +#endif	/* TESTS_KERNEL_ARCH_ALPHA_STACK_POINTER_H */
 diff -r efb233e8e830 -r 85af35b9e0c5 tests/kernel/t_signal_and_sp.c
 --- a/tests/kernel/t_signal_and_sp.c	Sat Apr 19 15:18:35 2025 +0000
 +++ b/tests/kernel/t_signal_and_sp.c	Sat Apr 19 17:04:59 2025 +0000
 @@ -255,6 +255,11 @@ ATF_TC_BODY(signalsp_sigaltstack, tc)
  	fprintf(stderr, "stack @ [%p, %p)",
  	    stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
 =20
 +#ifdef __alpha__
 +	atf_tc_expect_fail("PR port-evbmips/59236:"
 +	    " Multiple segfaults in erlite3 boot");
 +#endif
 +
  	/*
  	 * Try with all alignments of high addresses.
  	 */
 @@ -311,6 +316,12 @@ ATF_TC_BODY(misaligned_sp_and_signal, tc
  	sa.sa_handler =3D &signalsphandler;
  	RL(sigaction(SIGALRM, &sa, NULL));
 =20
 +#ifdef __alpha__
 +	atf_tc_expect_fail("PR kern/58149:"
 +	    " Cannot return from a signal handler"
 +	    " if SP was misaligned when the signal arrived");
 +#endif
 +
  	/*
  	 * Set up an interval timer so that we receive SIGALRM after 50 ms.
  	 */
 # HG changeset patch
 # User Taylor R Campbell <riastradh@NetBSD.org>
 # Date 1745089078 0
 #      Sat Apr 19 18:57:58 2025 +0000
 # Branch trunk
 # Node ID 4650ffe0718a4559d5b753b3b0c4925c58a5921e
 # Parent  85af35b9e0c55e3d2f0a06d5cb03569dbe9c02fe
 # EXP-Topic riastradh-pr59327-stackalign
 t_signal_and_sp: Add mips support.

 PR port-evbmips/59236: Multiple segfaults in erlite3 boot

 PR kern/58149: Cannot return from a signal handler if SP was
 misaligned when the signal arrived

 diff -r 85af35b9e0c5 -r 4650ffe0718a sys/arch/mips/include/mips_param.h
 --- a/sys/arch/mips/include/mips_param.h	Sat Apr 19 17:04:59 2025 +0000
 +++ b/sys/arch/mips/include/mips_param.h	Sat Apr 19 18:57:58 2025 +0000
 @@ -76,6 +76,12 @@
  #define	MACHINE "mips"
  #endif
 =20
 +#if defined(__mips_n64) || defined(__mips_n32)
 +#define	STACK_ALIGNBYTES	(16 - 1)
 +#else
 +#define	STACK_ALIGNBYTES	(8 - 1)
 +#endif
 +
  #define	ALIGNBYTES32		(sizeof(double) - 1)
  #define	ALIGN32(p)		(((uintptr_t)(p) + ALIGNBYTES32) &~ALIGNBYTES32)
 =20
 diff -r 85af35b9e0c5 -r 4650ffe0718a tests/kernel/Makefile
 --- a/tests/kernel/Makefile	Sat Apr 19 17:04:59 2025 +0000
 +++ b/tests/kernel/Makefile	Sat Apr 19 18:57:58 2025 +0000
 @@ -95,7 +95,11 @@ LDADD.t_timeleft+=3D	-lpthread
  CPPFLAGS+=3D	-D_KERNTYPES
  CPPFLAGS.t_unmount.c+=3D	-D_KMEMUSER -D__EXPOSE_MOUNT
 =20
 +.if exists(${.PARSEDIR}/arch/${MACHINE_ARCH})
  ARCHDIR:=3D		${.PARSEDIR}/arch/${MACHINE_ARCH}
 +.else
 +ARCHDIR:=3D		${.PARSEDIR}/arch/${MACHINE_CPU}
 +.endif
  .PATH:			${ARCHDIR}
 =20
  .if exists(${ARCHDIR}/stack_pointer.h)
 @@ -121,6 +125,11 @@ CPPFLAGS.h_execsp.c+=3D		-Dexecsp_main=3Dmai
  .endif
  LDSTATIC.h_execsp_static=3D	-static
 =20
 +.if ${MACHINE_CPU} =3D=3D "mips"
 +CPPFLAGS.execsp.S+=3D		-I${NETBSDSRCDIR}/lib/libc/arch/mips
 +CPPFLAGS.signalsphandler.S+=3D	-I${NETBSDSRCDIR}/lib/libc/arch/mips
 +.endif
 +
  .PATH:			${NETBSDSRCDIR}/sys/kern
  TESTS_C+=3D		t_extent
  SRCS.t_extent=3D		t_extent.c subr_extent.c
 diff -r 85af35b9e0c5 -r 4650ffe0718a tests/kernel/arch/mips/execsp.S
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/mips/execsp.S	Sat Apr 19 18:57:58 2025 +0000
 @@ -0,0 +1,126 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#define	_LOCORE
 +
 +#include <machine/asm.h>
 +
 +RCSID("$NetBSD$")
 +
 +#include "SYS.h"
 +
 +	.text
 +
 +/*
 + * void execsp_start(void *stackpointer@a0, void (*cleanup@a1)(void),
 + *     void *obj_main@a2, struct ps_strings *ps_strings@a3)
 + *
 + *	ELF entry point.  Saves the stack pointer in startsp and defers
 + *	to the usual csu __start routine.
 + */
 +LEAF(execsp_start)
 +	move	t0, sp			/* save stack pointer just in case */
 +
 +	PIC_PROLOGUE(execsp_start)	/* gp setup, sets t3 */
 +
 +	PTR_LA	t1, _C_LABEL(startsp)	/* load t1 :=3D &startsp */
 +	 NOP_L				/* load hazard */
 +	PTR_S	t0, 0(t1)		/* store startsp :=3D stack pointer */
 +
 +	PIC_TAILCALL(__start)		/* gp restore, uses t3 */
 +END(execsp_start)
 +
 +/*
 + * void execsp_ctor(void)
 + *
 + *	ELF constructor.  Saves the stack pointer in ctorsp and
 + *	returns.
 + */
 +LEAF(execsp_ctor)
 +	move	t0, sp			/* save stack pointer just in case */
 +
 +	PIC_PROLOGUE(execsp_ctor)	/* gp setup, sets t3 */
 +
 +	PTR_LA	t1, _C_LABEL(ctorsp)	/* load t1 :=3D &ctorsp */
 +	 NOP_L				/* load hazard */
 +	PTR_S	t0, 0(t1)		/* store ctorsp :=3D stack pointer */
 +
 +	PIC_RETURN()			/* gp restore, uses t3 */
 +END(execsp_ctor)
 +
 +	/* Make execsp_ctor a constructor. */
 +	.pushsection .ctors,"aw",@progbits
 +	.p2align PTR_SCALESHIFT
 +	PTR_WORD	execsp_ctor
 +	.popsection
 +
 +/*
 + * int main(int argc@a0, char **argv@a1, ...)
 + *
 + *	Main function.  Saves the stack pointer in mainsp and returns
 + *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
 + *	been initialized.
 + */
 +LEAF(main)
 +	move	t0, sp			/* save stack pointer just in case */
 +
 +	PIC_PROLOGUE(main)		/* gp setup, sets t3 */
 +
 +	PTR_LA	t1, _C_LABEL(mainsp)	/* load t1 :=3D &mainsp */
 +	 NOP_L				/* load hazard */
 +	PTR_S	t0, 0(t1)		/* store mainsp :=3D stack pointer */
 +
 +	move	v0, zero		/* return 0 */
 +
 +	PIC_RETURN()			/* gp restore, uses t3 */
 +END(main)
 +
 +/*
 + * void execsp_dtor(void)
 + *
 + *	ELF destructor.  Saves the stack pointer in dtorsp and defers
 + *	to the C execsp_main in h_execsp.c to report the stack pointers
 + *	back to the t_signal_and_sp parent.
 + */
 +LEAF(execsp_dtor)
 +	move	t0, sp			/* save stack pointer just in case */
 +
 +	PIC_PROLOGUE(execsp_dtor)	/* gp setup, sets t3 */
 +
 +	PTR_LA	t1, _C_LABEL(dtorsp)	/* load t1 :=3D &dtorsp */
 +	 NOP_L				/* load hazard */
 +	PTR_S	t0, 0(t1)		/* store dtorsp :=3D stack pointer */
 +
 +	PIC_TAILCALL(execsp_main)	/* gp restore, uses t3 */
 +END(execsp_dtor)
 +
 +	/* Make execsp_ctor a destructor. */
 +	.pushsection .dtors,"aw",@progbits
 +	.p2align PTR_SCALESHIFT
 +	PTR_WORD	execsp_dtor
 +	.popsection
 diff -r 85af35b9e0c5 -r 4650ffe0718a tests/kernel/arch/mips/signalsphandler=
 .S
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/mips/signalsphandler.S	Sat Apr 19 18:57:58 2025 +00=
 00
 @@ -0,0 +1,55 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#define	_LOCORE
 +
 +#include <machine/asm.h>
 +
 +RCSID("$NetBSD$")
 +
 +#include "SYS.h"
 +
 +	.text
 +
 +/*
 + * signalsphandler(signo@a0)
 + *
 + *	Signal handler.  Store the stack pointer on entry at the global
 + *	variable signalsp and return.
 + */
 +LEAF(signalsphandler)
 +	move	t0, sp			/* save stack pointer just in case */
 +
 +	PIC_PROLOGUE(signalsphandler)	/* gp setup, sets t3 */
 +
 +	PTR_LA	t1, _C_LABEL(signalsp)	/* load t1 :=3D &signalsp */
 +	 NOP_L				/* load hazard */
 +	PTR_S	t0, 0(t1)		/* store signalsp :=3D stack pointer */
 +
 +	PIC_RETURN()
 +END(signalsphandler)
 diff -r 85af35b9e0c5 -r 4650ffe0718a tests/kernel/arch/mips/stack_pointer.h
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/mips/stack_pointer.h	Sat Apr 19 18:57:58 2025 +0000
 @@ -0,0 +1,35 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#ifndef	TESTS_KERNEL_ARCH_MIPS_STACK_POINTER_H
 +#define	TESTS_KERNEL_ARCH_MIPS_STACK_POINTER_H
 +
 +#define MISALIGN_SP	__asm __volatile("addiu $sp,$sp,-1" ::: "memory")
 +#define FIX_SP		__asm __volatile("addiu $sp,$sp,1" ::: "memory")
 +
 +#endif	/* TESTS_KERNEL_ARCH_MIPS_STACK_POINTER_H */
 diff -r 85af35b9e0c5 -r 4650ffe0718a tests/kernel/t_signal_and_sp.c
 --- a/tests/kernel/t_signal_and_sp.c	Sat Apr 19 17:04:59 2025 +0000
 +++ b/tests/kernel/t_signal_and_sp.c	Sat Apr 19 18:57:58 2025 +0000
 @@ -255,7 +255,7 @@ ATF_TC_BODY(signalsp_sigaltstack, tc)
  	fprintf(stderr, "stack @ [%p, %p)",
  	    stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
 =20
 -#ifdef __alpha__
 +#if defined __alpha__ || defined __mips__
  	atf_tc_expect_fail("PR port-evbmips/59236:"
  	    " Multiple segfaults in erlite3 boot");
  #endif
 @@ -316,7 +316,7 @@ ATF_TC_BODY(misaligned_sp_and_signal, tc
  	sa.sa_handler =3D &signalsphandler;
  	RL(sigaction(SIGALRM, &sa, NULL));
 =20
 -#ifdef __alpha__
 +#if defined __alpha__ || defined __mips__
  	atf_tc_expect_fail("PR kern/58149:"
  	    " Cannot return from a signal handler"
  	    " if SP was misaligned when the signal arrived");
 # HG changeset patch
 # User Taylor R Campbell <riastradh@NetBSD.org>
 # Date 1745092499 0
 #      Sat Apr 19 19:54:59 2025 +0000
 # Branch trunk
 # Node ID f4d9ab8a392c24b7af72b2f153a588c9ff6faf22
 # Parent  4650ffe0718a4559d5b753b3b0c4925c58a5921e
 # EXP-Topic riastradh-pr59327-stackalign
 WIP: t_signal_and_sp: Add hppa support.

 XXX Doesn't work yet -- h_execsp_static crashes with SIGSEGV.

 diff -r 4650ffe0718a -r f4d9ab8a392c sys/arch/hppa/include/param.h
 --- a/sys/arch/hppa/include/param.h	Sat Apr 19 18:57:58 2025 +0000
 +++ b/sys/arch/hppa/include/param.h	Sat Apr 19 19:54:59 2025 +0000
 @@ -79,6 +79,8 @@
  #define	MCLBYTES	(1 << MCLSHIFT)	/* large enough for ether MTU */
  #define	MCLOFSET	(MCLBYTES - 1)
 =20
 +#define	STACK_ALIGNBYTES	(64 - 1)
 +
  /*
   * Size of kernel malloc arena in logical pages
   */
 diff -r 4650ffe0718a -r f4d9ab8a392c tests/kernel/arch/hppa/execsp.S
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/hppa/execsp.S	Sat Apr 19 19:54:59 2025 +0000
 @@ -0,0 +1,109 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#define	_LOCORE
 +
 +#include <machine/asm.h>
 +
 +RCSID("$NetBSD$")
 +
 +/*
 + * void execsp_start(struct ps_strings *ps_strings@arg0,
 + *     void (*cleanup@arg1)(void), void *obj_main@arg2)
 + *
 + *	ELF entry point.  Saves the stack pointer in startsp and defers
 + *	to the usual csu __start routine.
 + */
 +LEAF_ENTRY(execsp_start)
 +	addil	LT%_C_LABEL(startsp), %r19	/* r20 :=3D &startsp */
 +	ldw	RT%_C_LABEL(startsp)(%r1), %r20
 +
 +	/* PIC_TAILCALL(__start), if we had it */
 +	addil	LT%_C_LABEL(__start), %r19	/* r1 :=3D __start */
 +	ldw	RT%_C_LABEL(__start)(%r1), %r1
 +	bv	%r0(%r1)			/* jump to __start */
 +	 stw	%sp, 0(%r20)			/* startsp :=3D sp */
 +EXIT(execsp_start)
 +
 +/*
 + * void execsp_ctor(void)
 + *
 + *	ELF constructor.  Saves the stack pointer in ctorsp and
 + *	returns.
 + */
 +LEAF_ENTRY(execsp_ctor)
 +	addil	LT%_C_LABEL(ctorsp), %r19	/* r1 :=3D &ctorsp */
 +	ldw	RT%_C_LABEL(ctorsp)(%r1), %r1
 +	bv	%r0(%rp)			/* return */
 +	 stw	%sp, 0(%r1)			/* ctorsp :=3D sp */
 +EXIT(execsp_ctor)
 +
 +	/* Make execsp_ctor a constructor. */
 +	.pushsection .ctors,"aw",@progbits
 +	.p2align 2
 +	.word	execsp_ctor
 +	.popsection
 +
 +/*
 + * int main(int argc@arg0, char **argv@arg1, ...)
 + *
 + *	Main function.  Saves the stack pointer in mainsp and returns
 + *	zero.  We will call execsp_main in execsp_dtor once dtorsp has
 + *	been initialized.
 + */
 +LEAF_ENTRY(main)
 +	addil	LT%_C_LABEL(mainsp), %r19	/* r1 :=3D &mainsp */
 +	ldw	RT%_C_LABEL(mainsp)(%r1), %r1
 +	stw	%sp, 0(%r1)			/* ctorsp :=3D sp */
 +	bv	%r0(%rp)			/* return... */
 +	 copy	%r0, %ret0			/* ...zero */
 +EXIT(main)
 +
 +/*
 + * void execsp_dtor(void)
 + *
 + *	ELF destructor.  Saves the stack pointer in dtorsp and defers
 + *	to the C execsp_main in h_execsp.c to report the stack pointers
 + *	back to the t_signal_and_sp parent.
 + */
 +LEAF_ENTRY(execsp_dtor)
 +	addil	LT%_C_LABEL(dtorsp), %r19	/* r20 :=3D &dtorsp */
 +	ldw	RT%_C_LABEL(dtorsp)(%r1), %r20
 +
 +	/* PIC_TAILCALL(__start), if we had it */
 +	addil	LT%_C_LABEL(execsp_main), %r19	/* r1 :=3D execsp_main */
 +	ldw	RT%_C_LABEL(execsp_main)(%r1), %r1
 +	bv	%r0(%r1)			/* jump to execsp_main */
 +	 stw	%sp, 0(%r20)			/* startsp :=3D sp */
 +EXIT(execsp_dtor)
 +
 +	/* Make execsp_ctor a destructor. */
 +	.pushsection .dtors,"aw",@progbits
 +	.p2align 2
 +	.word	execsp_dtor
 +	.popsection
 diff -r 4650ffe0718a -r f4d9ab8a392c tests/kernel/arch/hppa/signalsphandler=
 .S
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/hppa/signalsphandler.S	Sat Apr 19 19:54:59 2025 +00=
 00
 @@ -0,0 +1,48 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#define	_LOCORE
 +
 +#include <machine/asm.h>
 +
 +RCSID("$NetBSD$")
 +
 +	.text
 +
 +/*
 + * signalsphandler(signo@arg0)
 + *
 + *	Signal handler.  Store the stack pointer on entry at the global
 + *	variable signalsp and return.
 + */
 +LEAF_ENTRY(signalsphandler)
 +	addil	LT%_C_LABEL(signalsp), %r19	/* r1 :=3D &signalsp */
 +	ldw	RT%_C_LABEL(signalsp)(%r1), %r1
 +	bv	%r0(%rp)			/* return */
 +	 stw	%sp, 0(%r1)			/* signalsp :=3D sp */
 +EXIT(signalsphandler)
 diff -r 4650ffe0718a -r f4d9ab8a392c tests/kernel/arch/hppa/stack_pointer.h
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/tests/kernel/arch/hppa/stack_pointer.h	Sat Apr 19 19:54:59 2025 +0000
 @@ -0,0 +1,35 @@
 +/*	$NetBSD$	*/
 +
 +/*-
 + * Copyright (c) 2025 The NetBSD Foundation, Inc.
 + * All rights reserved.
 + *
 + * Redistribution and use in source and binary forms, with or without
 + * modification, are permitted provided that the following conditions
 + * are met:
 + * 1. Redistributions of source code must retain the above copyright
 + *    notice, this list of conditions and the following disclaimer.
 + * 2. Redistributions in binary form must reproduce the above copyright
 + *    notice, this list of conditions and the following disclaimer in the
 + *    documentation and/or other materials provided with the distribution.
 + *
 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTO=
 RS
 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIM=
 ITED
 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICU=
 LAR
 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTO=
 RS
 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF =
 THE
 + * POSSIBILITY OF SUCH DAMAGE.
 + */
 +
 +#ifndef	TESTS_KERNEL_ARCH_HPPA_STACK_POINTER_H
 +#define	TESTS_KERNEL_ARCH_HPPA_STACK_POINTER_H
 +
 +#define MISALIGN_SP	__asm __volatile("ldo 1(%%sp),%%sp" ::: "memory")
 +#define FIX_SP		__asm __volatile("ldo -1(%%sp),%%sp" ::: "memory")
 +
 +#endif	/* TESTS_KERNEL_ARCH_HPPA_STACK_POINTER_H */

 --=_tarl6EbRoME+6V+rh/yI3C5H/YL5MT1L--

Responsible-Changed-From-To: port-mips-maintainer->kern-bug-people
Responsible-Changed-By: riastradh@NetBSD.org
Responsible-Changed-When: Sun, 20 Apr 2025 14:50:34 +0000
Responsible-Changed-Why:
This bug affects more than just mips, so let's make it a kern bug and
maintain a checklist of which architectures have been tested and fixed.


From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Sun, 20 Apr 2025 22:31:01 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 20 22:31:01 UTC 2025

 Modified Files:
 	src/distrib/sets/lists/debug: mi
 	src/distrib/sets/lists/tests: mi
 	src/tests/kernel: Makefile t_signal_and_sp.c
 	src/tests/kernel/arch/aarch64: stack_pointer.h
 Added Files:
 	src/tests/kernel: h_execsp.c h_execsp.h
 	src/tests/kernel/arch/aarch64: execsp.S signalsphandler.S
 	src/tests/kernel/arch/x86_64: execsp.S signalsphandler.S
 	    stack_pointer.h

 Log Message:
 Test stack pointer alignment in various scenarios.

 1. elf entry point
 2. main function
 3. signal handler

 Extend the test to amd64 while here -- fortunately both aarch64 and
 amd64 pass, but others, such as mips, will fail:

 PR kern/59327: user stack pointer is not aligned properly

 This extends the test that was previously written for:

 PR kern/58149: aarch64: Cannot return from a signal handler if SP was
 misaligned when the signal arrived

 With any luck, this will help us to systematically eradicate misaligned
 stack pointers as hypothesized to be the reason for:

 PR port-mips/59236: Multiple segfaults in erlite3 boot


 To generate a diff of this commit:
 cvs rdiff -u -r1.476 -r1.477 src/distrib/sets/lists/debug/mi
 cvs rdiff -u -r1.1369 -r1.1370 src/distrib/sets/lists/tests/mi
 cvs rdiff -u -r1.87 -r1.88 src/tests/kernel/Makefile
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/h_execsp.c \
     src/tests/kernel/h_execsp.h
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/t_signal_and_sp.c
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/aarch64/execsp.S \
     src/tests/kernel/arch/aarch64/signalsphandler.S
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/aarch64/stack_pointer.h
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/x86_64/execsp.S \
     src/tests/kernel/arch/x86_64/signalsphandler.S \
     src/tests/kernel/arch/x86_64/stack_pointer.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Sun, 20 Apr 2025 22:31:26 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 20 22:31:25 UTC 2025

 Modified Files:
 	src/tests/kernel: h_execsp.c h_execsp.h t_signal_and_sp.c
 	src/tests/kernel/arch/aarch64: execsp.S
 	src/tests/kernel/arch/x86_64: execsp.S

 Log Message:
 t_signal_and_sp: Check sp on elf constructor/destructor entry too.

 XXX Should maybe test both .ctors/.dtors and .init/fini_array, but
 for now I'm limiting this to whatever gcc uses by default for each
 architecture.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/h_execsp.c \
     src/tests/kernel/h_execsp.h
 cvs rdiff -u -r1.2 -r1.3 src/tests/kernel/t_signal_and_sp.c
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/aarch64/execsp.S
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/x86_64/execsp.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Sun, 20 Apr 2025 22:31:58 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 20 22:31:58 UTC 2025

 Modified Files:
 	src/tests/kernel: t_signal_and_sp.c
 Added Files:
 	src/tests/kernel/arch/alpha: execsp.S signalsphandler.S stack_pointer.h

 Log Message:
 t_signal_and_sp: Add alpha support.

 Turns out alpha too gets confused by by misaligned sigaltstack, and
 by misaligned sp in the interrupted code, when a signal is delivered.

 PR kern/59327: user stack pointer is not aligned properly

 PR kern/58149: Cannot return from a signal handler if SP was
 misaligned when the signal arrived


 To generate a diff of this commit:
 cvs rdiff -u -r1.3 -r1.4 src/tests/kernel/t_signal_and_sp.c
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/alpha/execsp.S \
     src/tests/kernel/arch/alpha/signalsphandler.S \
     src/tests/kernel/arch/alpha/stack_pointer.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Sun, 20 Apr 2025 22:32:26 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 20 22:32:25 UTC 2025

 Modified Files:
 	src/sys/arch/mips/include: mips_param.h
 	src/tests/kernel: Makefile t_signal_and_sp.c
 Added Files:
 	src/tests/kernel/arch/mips: execsp.S signalsphandler.S stack_pointer.h

 Log Message:
 t_signal_and_sp: Add mips support.

 PR kern/59327: user stack pointer is not aligned properly

 PR kern/58149: Cannot return from a signal handler if SP was
 misaligned when the signal arrived

 Stack pointer misaligment in some cases hypothesized to be a possible
 cause of:

 PR port-evbmips/59236: Multiple segfaults in erlite3 boot


 To generate a diff of this commit:
 cvs rdiff -u -r1.52 -r1.53 src/sys/arch/mips/include/mips_param.h
 cvs rdiff -u -r1.88 -r1.89 src/tests/kernel/Makefile
 cvs rdiff -u -r1.4 -r1.5 src/tests/kernel/t_signal_and_sp.c
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/mips/execsp.S \
     src/tests/kernel/arch/mips/signalsphandler.S \
     src/tests/kernel/arch/mips/stack_pointer.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Sun, 20 Apr 2025 22:32:49 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 20 22:32:49 UTC 2025

 Modified Files:
 	src/sys/arch/hppa/include: param.h
 Added Files:
 	src/tests/kernel/arch/hppa: execsp.S signalsphandler.S stack_pointer.h

 Log Message:
 t_signal_and_sp: Add hppa support.

 Fortunately, hppa -- which uses monster 64-byte(!) stack alignment --
 looks good already.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.30 -r1.31 src/sys/arch/hppa/include/param.h
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/hppa/execsp.S \
     src/tests/kernel/arch/hppa/signalsphandler.S \
     src/tests/kernel/arch/hppa/stack_pointer.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Sun, 20 Apr 2025 22:33:13 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 20 22:33:13 UTC 2025

 Modified Files:
 	src/sys/arch/i386/include: param.h
 	src/tests/kernel: t_signal_and_sp.c
 Added Files:
 	src/tests/kernel/arch/i386: execsp.S signalsphandler.S stack_pointer.h

 Log Message:
 t_signal_and_sp: Add i386 support.

 i386 too is confused by misaligned sigaltstack or esp on interrupt.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.88 -r1.89 src/sys/arch/i386/include/param.h
 cvs rdiff -u -r1.5 -r1.6 src/tests/kernel/t_signal_and_sp.c
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/i386/execsp.S \
     src/tests/kernel/arch/i386/signalsphandler.S \
     src/tests/kernel/arch/i386/stack_pointer.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Sun, 20 Apr 2025 22:33:42 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 20 22:33:41 UTC 2025

 Modified Files:
 	src/sys/arch/sparc/include: param.h
 Added Files:
 	src/tests/kernel/arch/sparc: execsp.S signalsphandler.S stack_pointer.h

 Log Message:
 t_signal_and_sp: Add sparc support.

 Seems fine!  Would be nice if we had a well-known macro to abstract
 PIC vs non-PIC `sethi/ori/(ld)' like I invented here, to reduce
 needless #ifdef __PIC__ conditionals.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.75 -r1.76 src/sys/arch/sparc/include/param.h
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/sparc/execsp.S \
     src/tests/kernel/arch/sparc/signalsphandler.S \
     src/tests/kernel/arch/sparc/stack_pointer.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel/arch/riscv
Date: Sun, 20 Apr 2025 22:34:07 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 20 22:34:07 UTC 2025

 Added Files:
 	src/tests/kernel/arch/riscv: execsp.S signalsphandler.S stack_pointer.h

 Log Message:
 t_signal_sp: Add riscv support.

 riscv64 looks good, haven't tested riscv32.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/riscv/execsp.S \
     src/tests/kernel/arch/riscv/signalsphandler.S \
     src/tests/kernel/arch/riscv/stack_pointer.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel/arch/alpha
Date: Mon, 21 Apr 2025 02:31:23 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Mon Apr 21 02:31:22 UTC 2025

 Modified Files:
 	src/tests/kernel/arch/alpha: execsp.S

 Log Message:
 t_signal_and_sp: Fix main function on alpha so it returns zero.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/alpha/execsp.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Mon, 21 Apr 2025 02:33:45 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Mon Apr 21 02:33:45 UTC 2025

 Modified Files:
 	src/tests/kernel: Makefile t_signal_and_sp.c
 Added Files:
 	src/tests/kernel/arch/aarch64: contextspfunc.S threadspfunc.S
 	src/tests/kernel/arch/alpha: contextspfunc.S threadspfunc.S
 	src/tests/kernel/arch/hppa: contextspfunc.S threadspfunc.S
 	src/tests/kernel/arch/i386: contextspfunc.S threadspfunc.S
 	src/tests/kernel/arch/mips: contextspfunc.S threadspfunc.S
 	src/tests/kernel/arch/riscv: contextspfunc.S threadspfunc.S
 	src/tests/kernel/arch/sparc: contextspfunc.S threadspfunc.S
 	src/tests/kernel/arch/x86_64: contextspfunc.S threadspfunc.S

 Log Message:
 t_signal_and_sp: Test makecontext and pthread_create stack alignment.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.89 -r1.90 src/tests/kernel/Makefile
 cvs rdiff -u -r1.6 -r1.7 src/tests/kernel/t_signal_and_sp.c
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/aarch64/contextspfunc.S \
     src/tests/kernel/arch/aarch64/threadspfunc.S
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/alpha/contextspfunc.S \
     src/tests/kernel/arch/alpha/threadspfunc.S
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/hppa/contextspfunc.S \
     src/tests/kernel/arch/hppa/threadspfunc.S
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/i386/contextspfunc.S \
     src/tests/kernel/arch/i386/threadspfunc.S
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/mips/contextspfunc.S \
     src/tests/kernel/arch/mips/threadspfunc.S
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/riscv/contextspfunc.S \
     src/tests/kernel/arch/riscv/threadspfunc.S
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/sparc/contextspfunc.S \
     src/tests/kernel/arch/sparc/threadspfunc.S
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/x86_64/contextspfunc.S \
     src/tests/kernel/arch/x86_64/threadspfunc.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Mon, 21 Apr 2025 03:47:32 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Mon Apr 21 03:47:32 UTC 2025

 Modified Files:
 	src/tests/kernel: t_signal_and_sp.c
 	src/tests/kernel/arch/mips: threadspfunc.S

 Log Message:
 t_signal_and_sp: Fix threadspfunc on mips.

 1. Writing branch delay slots requires `.set noreorder'.  Got used to
    reading and writing RISCy code with branch delay slots ages ago,
    still haven't gotten used to having to tell the assembler `no, I
    really want you to assemble the instructions I wrote, as I wrote
    them, and not some other instructions in some other order'.

 2. Return value is v0 on mips, not a0 like modern mips^W^Wriscv.

 With this, the threadsp test passes on mips.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.7 -r1.8 src/tests/kernel/t_signal_and_sp.c
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/mips/threadspfunc.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Mon, 21 Apr 2025 03:48:07 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Mon Apr 21 03:48:07 UTC 2025

 Modified Files:
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 t_signal_and_sp: Test makecontext with uc_link too.

 This exercises the resumecontext path, which currently leaves the
 stack pointer misaligned on mipsn64 (note: not mips64, which is
 64-bit mips with n32 ABI).

 Also add some assertions about the stack pointer lying within the
 expected stack range and mark riscv inexplicably broken for the
 pthread_create sp.  And mark the hppa context/thread tests broken --
 not yet sure what's wrong with them.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.8 -r1.9 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Mon, 21 Apr 2025 12:06:08 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Mon Apr 21 12:06:08 UTC 2025

 Modified Files:
 	src/tests/kernel: t_signal_and_sp.c
 	src/tests/kernel/arch/hppa: contextspfunc.S
 	src/tests/kernel/arch/x86_64: threadspfunc.S

 Log Message:
 t_signal_and_sp: Fix threadsp test to actually test the thread sp.

 Copypasta error had it testing alignment of signalsp instead, and
 since it was always null, that always passed.  Mark it xfail on mips
 now as originally expected.

 While here, deal with some other issues:

 - Test was failing on riscv for me because I haven't updated this
   `current' VM in a while so it didn't have the fix for 57721
   (pthread_attr_setstack incorrectly adjusts address as if for guard
   page).  Don't mark it xfail.

 - Fix amd64 threadspfunc.S to adjust rsp like in all the other amd64
   stubs so it's congruent to 0 mod 16, not congruent to 8 mod 16.

 - Fix hppa contextspfunc.S to use separate registers for separate
   purposes at the same time, instead of expecting addil to preserve
   %r1 AND yield a result we use later in %r1.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.9 -r1.10 src/tests/kernel/t_signal_and_sp.c
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/hppa/contextspfunc.S
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/x86_64/threadspfunc.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: Rin Okuyama <rokuyama.rk@gmail.com>
To: gnats-bugs@NetBSD.org, netbsd-bugs@NetBSD.org,
 "source-changes-d@netbsd.org" <source-changes-d@NetBSD.org>
Cc: 
Subject: Re: kern/59327: user stack pointer is not aligned properly
Date: Mon, 21 Apr 2025 23:19:33 +0900

 Sorry, I forgot to mention PR#59327.

 rin
 -------- Forwarded Message --------
 Subject: CVS commit: src/tests/kernel/arch/mips
 Date: Mon, 21 Apr 2025 14:17:38 +0000
 From: Rin Okuyama <rin@netbsd.org>
 Reply-To: source-changes-d@NetBSD.org
 To: source-changes-full@NetBSD.org

 Module Name:	src
 Committed By:	rin
 Date:		Mon Apr 21 14:17:38 UTC 2025

 Modified Files:
 	src/tests/kernel/arch/mips: stack_pointer.h

 Log Message:
 t_signal_and_sp: mips: Fix {MISALIGN,FIX}_SP() for !o32

 Use `daddiu` instead of `addiu` as done in <mips/asm.h>.

 Otherwise, +/-1 is added to lower-32-bits of sp, and then
 results are sign-extended to whole-64-bits register (oops!!).


 To generate a diff of this commit:
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/mips/stack_pointer.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.


From: Rin Okuyama <rokuyama.rk@gmail.com>
To: gnats-bugs@netbsd.org, netbsd-bugs@NetBSD.org
Cc: 
Subject: Re: kern/59327: user stack pointer is not aligned properly
Date: Mon, 21 Apr 2025 23:26:07 +0900

 I've updated the patch for mips to ver. 2:
 https://gist.github.com/rokuyama/48aaa64604efb4321521e59b9dc92da8

 In this version, usp is aligned also for {,netbsd32_}sendsig_siginfo.

 With this patch, all cases of t_signal_and_sp successfully pass on
 erlite3, both for n64 and n32 userlands.

 However, unfortunately, this doesn't help the original problem,
 PR port-evbmips/59236.

 Thanks,
 rin

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Thu, 24 Apr 2025 01:52:38 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Apr 24 01:52:38 UTC 2025

 Modified Files:
 	src/lib/libc/arch/sparc/gen: _lwp.c makecontext.c
 	src/sys/arch/sparc/sparc: compat_16_machdep.c machdep.c sunos_machdep.c

 Log Message:
 sparc: Use STACK_ALIGNBYTES macro when aligning stack pointer.

 Prompted by:

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.7 -r1.8 src/lib/libc/arch/sparc/gen/_lwp.c
 cvs rdiff -u -r1.3 -r1.4 src/lib/libc/arch/sparc/gen/makecontext.c
 cvs rdiff -u -r1.6 -r1.7 src/sys/arch/sparc/sparc/compat_16_machdep.c
 cvs rdiff -u -r1.342 -r1.343 src/sys/arch/sparc/sparc/machdep.c
 cvs rdiff -u -r1.24 -r1.25 src/sys/arch/sparc/sparc/sunos_machdep.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Thu, 24 Apr 2025 16:57:27 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Apr 24 16:57:27 UTC 2025

 Modified Files:
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 t_signal_and_sp: Mark signalsp xfail on mips n32/n64.

 The kernel takes an initially aligned stack pointer and subtracts
 sizeof(struct sigframe_siginfo) or sizeof(struct sigframe_siginfo32)
 from it -- that is, 872=0x368 or 792=0x318 bytes, respectively.  But
 n32 and n64 require 16-byte stack alignment.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.10 -r1.11 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Thu, 24 Apr 2025 17:00:25 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Apr 24 17:00:25 UTC 2025

 Modified Files:
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 t_signal_and_sp: contextsplink test may not be broken on n64.

 n32 and n64 have the same stack alignment requirement (16-byte), so
 if there were a problem it would probably manifest on n32 too.  But
 the only things that __resumecontext does with a misaligned sp are:

 1. syscall getcontext (not even via the syscall stub, but via the
    syscall instruction directly), which is unlikely to care; and

 2. tail-call setcontext, which does a syscall (again, unlikely to
    care) that will discard the caller's sp and replace it altogether.

 So I'm not sure we can even test potential adverse consequences of
 misalignment inside __resumecontext, short of interposing our own
 setcontext symbol to artificially verify its stack alignment before
 passing on to the syscall -- which, while feasible, is maybe not
 worth the effort.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.11 -r1.12 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Thu, 24 Apr 2025 23:51:03 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Apr 24 23:51:03 UTC 2025

 Modified Files:
 	src/sys/arch/amd64/amd64: machdep.c netbsd32_machdep.c
 	src/sys/arch/amd64/include: param.h
 	src/sys/arch/i386/i386: machdep.c
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 x86: Make sure esp is aligned when delivering signal.

 While here, use STACK_ALIGNBYTES consistently for the alignment mask
 (or STACK_ALIGNBYTES32 in amd64 for the compat32 alignment mask).

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.373 -r1.374 src/sys/arch/amd64/amd64/machdep.c
 cvs rdiff -u -r1.141 -r1.142 src/sys/arch/amd64/amd64/netbsd32_machdep.c
 cvs rdiff -u -r1.40 -r1.41 src/sys/arch/amd64/include/param.h
 cvs rdiff -u -r1.845 -r1.846 src/sys/arch/i386/i386/machdep.c
 cvs rdiff -u -r1.12 -r1.13 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/lib/libc/arch/mips/gen
Date: Thu, 24 Apr 2025 23:51:44 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Apr 24 23:51:44 UTC 2025

 Modified Files:
 	src/lib/libc/arch/mips/gen: makecontext.c

 Log Message:
 mips/makecontext.c: Sort includes.

 No functional change intended.

 Preparation for:

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.8 -r1.9 src/lib/libc/arch/mips/gen/makecontext.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/lib/libc/arch/mips/gen
Date: Thu, 24 Apr 2025 23:54:14 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Apr 24 23:54:14 UTC 2025

 Modified Files:
 	src/lib/libc/arch/mips/gen: makecontext.c

 Log Message:
 mips/makecontext.c: Use STACK_ALIGNBYTES for clarity.

 No functional change intended.

 Tidying to make stack alignment more obvious, prompted by:

 PR kern/59327: user stack pointer is not aligned properly

 Part of a patch by rin@.


 To generate a diff of this commit:
 cvs rdiff -u -r1.9 -r1.10 src/lib/libc/arch/mips/gen/makecontext.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/sys/arch/mips/mips
Date: Thu, 24 Apr 2025 23:55:18 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Apr 24 23:55:18 UTC 2025

 Modified Files:
 	src/sys/arch/mips/mips: mips_machdep.c

 Log Message:
 mips: Make sure stack is aligned on exec.

 The only caller of setregs, sys/kern/kern_exec.c execve_runproc,
 already guarantees STACK_ALIGNBYTES alignment, so let's assert it but
 out of paranoia also clear the bits anyway just in case for non-
 DIAGNOSTIC builds.

 Based on a patch by rin@.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.307 -r1.308 src/sys/arch/mips/mips/mips_machdep.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/lib/libc/arch/sparc/gen
Date: Thu, 24 Apr 2025 23:56:25 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Apr 24 23:56:25 UTC 2025

 Modified Files:
 	src/lib/libc/arch/sparc/gen: _lwp.c makecontext.c

 Log Message:
 sparc _lwp_makecontext, makecontext: Sort includes.

 No functional change intended.

 Tidying up for:

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.9 -r1.10 src/lib/libc/arch/sparc/gen/_lwp.c
 cvs rdiff -u -r1.5 -r1.6 src/lib/libc/arch/sparc/gen/makecontext.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/lib/libc/arch/mips/gen
Date: Thu, 24 Apr 2025 23:56:50 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Apr 24 23:56:50 UTC 2025

 Modified Files:
 	src/lib/libc/arch/mips/gen: _lwp.c

 Log Message:
 lib/libc/arch/mips/gen/_lwp.c: Sort includes.

 No functional change intended.

 Preparation for:

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.9 -r1.10 src/lib/libc/arch/mips/gen/_lwp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Fri, 25 Apr 2025 00:03:16 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Apr 25 00:03:16 UTC 2025

 Modified Files:
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 t_signal_and_sp: Check alignment of default thread stack too.

 Not just with pthread_attr_setstack.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.13 -r1.14 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/lib/libc/arch/mips/gen
Date: Fri, 25 Apr 2025 00:08:38 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Apr 25 00:08:38 UTC 2025

 Modified Files:
 	src/lib/libc/arch/mips/gen: _resumecontext.S

 Log Message:
 mips __resumecontext: Align private stack frame size.

 On mips64, UCONTEXT_SIZE is 664=0x298 (n32) or 744=0x2e8 (n64), but
 sp is required to have 16-byte alignment in the ABI, so we need to
 round the stack frame allocation up to 0x2a0 or 0x2f0.

 No functional change intended on mips o32.  Unlikely to have a
 practical change on mips n32/n64, but possible if signal delivery
 wins a race -- until we fix sp alignment in signal handler calls,
 coming soon.

 Based on a patch by rin@.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.13 -r1.14 src/lib/libc/arch/mips/gen/_resumecontext.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Fri, 25 Apr 2025 00:26:59 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Apr 25 00:26:59 UTC 2025

 Modified Files:
 	src/sys/arch/mips/include: frame.h mips_param.h
 	src/sys/arch/mips/mips: compat_16_machdep.c netbsd32_machdep.c
 	    sig_machdep.c
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 mips: Align stack pointer on entry to signal handler.

 Based on a patch by rin@.  The variant approach I took puts the stack
 frame allocation and alignment logic in one place (getframe, used by
 sendsig_siginfo for native (n64, on mips), netbsd32_sendsig_siginfo
 for compat32 (n32/o32, on mips), and sendsig_sigcontext (compat 1.6))
 and reduces the chance of provoking compiler exploitation of
 undefined behaviour by doing arithmetic in uintptr_t rather than in
 pointers to large aligned structs.  This also ensures the resulting
 pointer is aligned for the object (struct siginfo_sigframe, struct
 siginfo_sigframe32, struct sigcontext), not just for the ABI stack
 alignment.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.12 -r1.13 src/sys/arch/mips/include/frame.h
 cvs rdiff -u -r1.53 -r1.54 src/sys/arch/mips/include/mips_param.h
 cvs rdiff -u -r1.23 -r1.24 src/sys/arch/mips/mips/compat_16_machdep.c \
     src/sys/arch/mips/mips/netbsd32_machdep.c
 cvs rdiff -u -r1.25 -r1.26 src/sys/arch/mips/mips/sig_machdep.c
 cvs rdiff -u -r1.14 -r1.15 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Fri, 25 Apr 2025 00:59:27 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Apr 25 00:59:27 UTC 2025

 Modified Files:
 	src/sys/arch/alpha/alpha: compat_16_machdep.c machdep.c
 	src/sys/arch/alpha/include: frame.h
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 alpha: Align signal pointer on entry to signal handler.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.23 -r1.24 src/sys/arch/alpha/alpha/compat_16_machdep.c
 cvs rdiff -u -r1.382 -r1.383 src/sys/arch/alpha/alpha/machdep.c
 cvs rdiff -u -r1.10 -r1.11 src/sys/arch/alpha/include/frame.h
 cvs rdiff -u -r1.15 -r1.16 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/lib/libc/arch/hppa/gen
Date: Fri, 25 Apr 2025 01:17:12 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Apr 25 01:17:11 UTC 2025

 Modified Files:
 	src/lib/libc/arch/hppa/gen: _lwp.c makecontext.c

 Log Message:
 hppa _lwp_makecontext, makecontext: Sort includes.

 No functional change intended.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.5 -r1.6 src/lib/libc/arch/hppa/gen/_lwp.c
 cvs rdiff -u -r1.6 -r1.7 src/lib/libc/arch/hppa/gen/makecontext.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Fri, 25 Apr 2025 01:18:24 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Apr 25 01:18:24 UTC 2025

 Modified Files:
 	src/lib/libc/arch/mips/gen: _lwp.c
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 mips _lwp_makecontext: Align sp.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.10 -r1.11 src/lib/libc/arch/mips/gen/_lwp.c
 cvs rdiff -u -r1.16 -r1.17 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src
Date: Fri, 25 Apr 2025 01:18:39 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Apr 25 01:18:38 UTC 2025

 Modified Files:
 	src/lib/libc/arch/hppa/gen: _lwp.c
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 hppa _lwp_makecontext: Align stack pointer.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.6 -r1.7 src/lib/libc/arch/hppa/gen/_lwp.c
 cvs rdiff -u -r1.17 -r1.18 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/lib/libc/arch/hppa/gen
Date: Fri, 25 Apr 2025 01:18:51 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Apr 25 01:18:51 UTC 2025

 Modified Files:
 	src/lib/libc/arch/hppa/gen: makecontext.c

 Log Message:
 hppa makecontext: Use STACK_ALIGNBYTES to refer to stack alignment.

 No functional change intended -- just make it clearer and easier to
 search.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.7 -r1.8 src/lib/libc/arch/hppa/gen/makecontext.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel/arch/arm
Date: Fri, 25 Apr 2025 02:24:01 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Apr 25 02:24:01 UTC 2025

 Added Files:
 	src/tests/kernel/arch/arm: contextspfunc.S execsp.S signalsphandler.S
 	    stack_pointer.h threadspfunc.S

 Log Message:
 t_signal_and_sp: Draft add arm support.

 Compile-tested only, let's see how this goes.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/arm/contextspfunc.S \
     src/tests/kernel/arch/arm/execsp.S \
     src/tests/kernel/arch/arm/signalsphandler.S \
     src/tests/kernel/arch/arm/stack_pointer.h \
     src/tests/kernel/arch/arm/threadspfunc.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Sat, 26 Apr 2025 03:01:13 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sat Apr 26 03:01:13 UTC 2025

 Modified Files:
 	src/tests/kernel: Makefile

 Log Message:
 tests/kernel/Makefile: Add DPSRCS for t_signal_and_sp.c parts.

 This way if you add, e.g., arch/foo/execsp.S, then t_signal_and_sp.c
 will be recompiled with -DHAVE_EXECSP even if you don't make clean.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.90 -r1.91 src/tests/kernel/Makefile

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Sat, 26 Apr 2025 12:21:37 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sat Apr 26 12:21:37 UTC 2025

 Modified Files:
 	src/tests/kernel: Makefile

 Log Message:
 tests/kernel/Makefile: Back out DPSRCS for t_signal_and_sp helpers.

 Doesn't work the way I'd hoped for update builds -- I had tested it
 only with a .h file, not with the .S files:

 #    create  kernel//work/src/tests/kernel/arch/arm/execsp.d
 CC=/work/tools/bin/arm--netbsdelf-eabi-gcc /work/tools/bin/nbmkdep -f /work/src/tests/kernel/arch/arm/execsp.d.tmp  -- --sysroot=/branches/hosts-current/evbearmv5 -D_KERNTYPES -I/work/src/tests/kernel/..    -x assembler-with-cpp /work/src/tests/kernel/arch/arm/execsp.S &&  mv -f /work/src/tests/kernel/arch/arm/execsp.d.tmp /work/src/tests/kernel/arch/arm/execsp.d

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.92 -r1.93 src/tests/kernel/Makefile

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Sat, 26 Apr 2025 19:05:35 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sat Apr 26 19:05:35 UTC 2025

 Modified Files:
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 t_signal_and_sp: Get STACK_ALIGNBYTES from sys/param.h.

 Fills in a default of __ALIGNBYTES if machine/param.h doesn't define
 it.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.18 -r1.19 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Sat, 26 Apr 2025 19:23:06 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sat Apr 26 19:23:06 UTC 2025

 Modified Files:
 	src/tests/kernel: t_signal_and_sp.c

 Log Message:
 t_signal_and_sp: Define __EXPOSE_STACK to get STACK_ALIGNBYTES.

 This is a bit silly: sys/param.h unconditionally defines
 STACK_ALIGNBYTES if machine/param.h does, but if machine/param.h
 doesn't, then it only provides the default

 #define STACK_ALIGNBYTES __ALIGNBYTES

 under the condition that __EXPOSE_STACK is defined.

 But using __EXPOSE_STACK here is a lower-risk change than touching
 sys/param.h.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.19 -r1.20 src/tests/kernel/t_signal_and_sp.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Valery Ushakov" <uwe@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel/arch/sh3
Date: Sat, 26 Apr 2025 22:34:52 +0000

 Module Name:	src
 Committed By:	uwe
 Date:		Sat Apr 26 22:34:52 UTC 2025

 Added Files:
 	src/tests/kernel/arch/sh3: asm.h contextspfunc.S execsp.S
 	    signalsphandler.S stack_pointer.h threadspfunc.S

 Log Message:
 t_signal_and_sp: add sh3 support

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/sh3/asm.h \
     src/tests/kernel/arch/sh3/contextspfunc.S \
     src/tests/kernel/arch/sh3/execsp.S \
     src/tests/kernel/arch/sh3/signalsphandler.S \
     src/tests/kernel/arch/sh3/stack_pointer.h \
     src/tests/kernel/arch/sh3/threadspfunc.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Valery Ushakov" <uwe@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel
Date: Sat, 26 Apr 2025 23:49:56 +0000

 Module Name:	src
 Committed By:	uwe
 Date:		Sat Apr 26 23:49:56 UTC 2025

 Modified Files:
 	src/tests/kernel: t_signal_and_sp.c
 	src/tests/kernel/arch/sh3: signalsphandler.S

 Log Message:
 t_signal_and_sp: sh3 - mark expected failures

 misaligned_sp_and_signal and signalsp_sigaltstack currently fail on
 sh3 b/c the stack is not force-aligned for signal handlers.  Make
 signalsphandler more robust by not touching the stack - we can save
 r12 (GOT) in a register.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.20 -r1.21 src/tests/kernel/t_signal_and_sp.c
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/sh3/signalsphandler.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel/arch/sh3
Date: Sun, 27 Apr 2025 00:03:46 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 27 00:03:46 UTC 2025

 Modified Files:
 	src/tests/kernel/arch/sh3: execsp.S

 Log Message:
 t_signal_and_sp: Fix RCSID in sh3/execsp.S.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/sh3/execsp.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel/arch/arm
Date: Sun, 27 Apr 2025 15:38:50 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 27 15:38:50 UTC 2025

 Modified Files:
 	src/tests/kernel/arch/arm: signalsphandler.S

 Log Message:
 t_signal_and_sp arm/signalsphandler.S: Fix pasto.

 Copied & pasted this from contextspfunc.S and forgot to change one
 reference to contextsp to signalsp instead, oops.

 Should resolve:

 *** Check failed: /work/src/tests/kernel/t_signal_and_sp.c:440: signalsp != NULL not met

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/arm/signalsphandler.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel/arch/arm
Date: Sun, 27 Apr 2025 15:43:43 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 27 15:43:43 UTC 2025

 Modified Files:
 	src/tests/kernel/arch/arm: execsp.S

 Log Message:
 t_signal_and_sp arm/execsp.S: Try using registers that aren't in use.

 The first draft of this was instead using all the registers that
 _are_ in use on entry to the elf entry point, which understandably
 confused __start when we jumped to it after trashing all its inputs.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/arm/execsp.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/lib/libc/arch/alpha/gen
Date: Sun, 27 Apr 2025 16:15:27 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 27 16:15:27 UTC 2025

 Modified Files:
 	src/lib/libc/arch/alpha/gen: _lwp.c makecontext.c

 Log Message:
 lib/libc/arch/alpha/gen/_lwp.c, makecontext.c: Sort includes.

 No functional change intended.

 Preparation for tidying for:

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.8 -r1.9 src/lib/libc/arch/alpha/gen/_lwp.c
 cvs rdiff -u -r1.7 -r1.8 src/lib/libc/arch/alpha/gen/makecontext.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/lib/libc/arch/alpha/gen
Date: Sun, 27 Apr 2025 16:15:40 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 27 16:15:40 UTC 2025

 Modified Files:
 	src/lib/libc/arch/alpha/gen: _lwp.c makecontext.c

 Log Message:
 lib/libc/arch/alpha: Say STACK_ALIGNBYTES, not 0xf, where relevant.

 No functional change intended.

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.9 -r1.10 src/lib/libc/arch/alpha/gen/_lwp.c
 cvs rdiff -u -r1.8 -r1.9 src/lib/libc/arch/alpha/gen/makecontext.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel/arch/mips
Date: Sun, 27 Apr 2025 16:49:55 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sun Apr 27 16:49:54 UTC 2025

 Modified Files:
 	src/tests/kernel/arch/mips: contextspfunc.S execsp.S signalsphandler.S

 Log Message:
 t_signal_and_sp: Try to fix mips o32 tests.

 Evidently, for o32, PIC_PROLOGUE (which expands to .cpload) must be
 the very first instruction of the function, or else it will get the
 $gp calculation wrong.

 Since it seems that the mips PIC_PROLOGUE does not, after all, mess
 with the stack, let's just dispense with the temporary copy of sp in
 t0 which I had added in paranoia over what magic might happen inside
 PIC_PROLOGUE.  (Such paranoia is not entirely unjustified -- for
 example, the sh3 PIC_PROLOGUE _does_ push data on the stack, though
 this can be bypassed with PIC_PROLOGUE_NOSAVE.)

 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/mips/contextspfunc.S \
     src/tests/kernel/arch/mips/execsp.S \
     src/tests/kernel/arch/mips/signalsphandler.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Martin Husemann" <martin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/59327 CVS commit: src/tests/kernel/arch/arm
Date: Mon, 28 Apr 2025 15:31:13 +0000

 Module Name:	src
 Committed By:	martin
 Date:		Mon Apr 28 15:31:13 UTC 2025

 Modified Files:
 	src/tests/kernel/arch/arm: execsp.S

 Log Message:
 Make main() actually return zero. From Riastradh.
 PR kern/59327: user stack pointer is not aligned properly


 To generate a diff of this commit:
 cvs rdiff -u -r1.2 -r1.3 src/tests/kernel/arch/arm/execsp.S

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2025 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.