aboutsummaryrefslogtreecommitdiff
path: root/kernel/arch/i386
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/arch/i386')
-rw-r--r--kernel/arch/i386/contextspfunc.S56
-rw-r--r--kernel/arch/i386/execregs.c132
-rw-r--r--kernel/arch/i386/execregs.h69
-rw-r--r--kernel/arch/i386/execsp.S112
-rw-r--r--kernel/arch/i386/h_execregs.S85
-rw-r--r--kernel/arch/i386/signalsphandler.S56
-rw-r--r--kernel/arch/i386/stack_pointer.h35
-rw-r--r--kernel/arch/i386/threadspfunc.S43
8 files changed, 588 insertions, 0 deletions
diff --git a/kernel/arch/i386/contextspfunc.S b/kernel/arch/i386/contextspfunc.S
new file mode 100644
index 000000000000..cc097cc61325
--- /dev/null
+++ b/kernel/arch/i386/contextspfunc.S
@@ -0,0 +1,56 @@
+/* $NetBSD: contextspfunc.S,v 1.1 2025/04/21 02:33:44 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 CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * 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: contextspfunc.S,v 1.1 2025/04/21 02:33:44 riastradh Exp $")
+
+/*
+ * contextspfunc()
+ *
+ * makecontext(3) function. Store the stack pointer on entry at
+ * the global variable contextsp and call contextdone.
+ */
+ENTRY(contextspfunc)
+ call getpc_eax
+ addl $_GLOBAL_OFFSET_TABLE_,%eax
+ movl _C_LABEL(contextsp)@GOT(%eax),%eax
+ movl %esp,(%eax)
+ jmp _C_LABEL(contextdone)@PLT
+END(contextspfunc)
+
+ .text
+ _ALIGN_TEXT
+ .local getpc_eax
+ .type getpc_eax,@function
+getpc_eax:
+ movl (%esp),%eax
+ ret
+END(getpc_eax)
diff --git a/kernel/arch/i386/execregs.c b/kernel/arch/i386/execregs.c
new file mode 100644
index 000000000000..477cd82989e9
--- /dev/null
+++ b/kernel/arch/i386/execregs.c
@@ -0,0 +1,132 @@
+/* $NetBSD: execregs.c,v 1.1 2025/02/27 00:55:32 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 CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * 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: execregs.c,v 1.1 2025/02/27 00:55:32 riastradh Exp $");
+
+#include "execregs.h"
+
+#include <errno.h>
+#include <spawn.h>
+#include <stddef.h>
+#include <unistd.h>
+
+extern char **environ;
+
+static unsigned long
+nonnull(unsigned long x)
+{
+
+ x |= x << 8;
+ x |= x << 16;
+ return x;
+}
+
+int
+execregschild(char *path)
+{
+ register long edi __asm("edi") = nonnull('d');
+ register long esi __asm("esi") = nonnull('s');
+ /* ebp: frame pointer, can't touch that here, but it'll be nonnull */
+ /* ebx: ps_strings, passed to child */
+ register long edx __asm("edx") = nonnull('x');
+ register long ecx __asm("ecx") = nonnull('c');
+ register long eax __asm("eax") = nonnull('a');
+
+ char *argv[] = {path, NULL};
+ char **envp = environ;
+
+ /*
+ * Not perfect -- compiler might use some registers for
+ * stack/argument transfers, but all the arguments are nonnull
+ * so this is probably a good test anyway.
+ */
+ __asm volatile("" :
+ "+r"(edi),
+ "+r"(esi),
+ "+r"(edx),
+ "+r"(ecx),
+ "+r"(eax)
+ :: "memory");
+
+ return execve(path, argv, envp);
+}
+
+pid_t
+spawnregschild(char *path, int fd)
+{
+ register long edi __asm("edi") = nonnull('d');
+ register long esi __asm("esi") = nonnull('s');
+ /* ebp: frame pointer, can't touch that here, but it'll be nonnull */
+ /* ebx: ps_strings, passed to child */
+ register long edx __asm("edx") = nonnull('x');
+ register long ecx __asm("ecx") = nonnull('c');
+ register long eax __asm("eax") = nonnull('a');
+
+ char *argv[] = {path, NULL};
+ char **envp = environ;
+ posix_spawn_file_actions_t fileacts;
+ posix_spawnattr_t attr;
+ pid_t pid;
+ int error;
+
+ error = posix_spawn_file_actions_init(&fileacts);
+ if (error)
+ goto out;
+ error = posix_spawn_file_actions_adddup2(&fileacts, fd, STDOUT_FILENO);
+ if (error)
+ goto out;
+ error = posix_spawnattr_init(&attr);
+ if (error)
+ goto out;
+
+ /*
+ * Not perfect -- compiler might use some registers for
+ * stack/argument transfers, but all the arguments are nonnull
+ * so this is probably a good test anyway.
+ */
+ __asm volatile("" :
+ "+r"(edi),
+ "+r"(esi),
+ "+r"(edx),
+ "+r"(ecx),
+ "+r"(eax)
+ :: "memory");
+
+ error = posix_spawn(&pid, path, &fileacts, &attr, argv, envp);
+ if (error)
+ goto out;
+
+out: posix_spawnattr_destroy(&attr);
+ posix_spawn_file_actions_destroy(&fileacts);
+ if (error) {
+ errno = error;
+ return -1;
+ }
+ return 0;
+}
diff --git a/kernel/arch/i386/execregs.h b/kernel/arch/i386/execregs.h
new file mode 100644
index 000000000000..f512bbabc1c7
--- /dev/null
+++ b/kernel/arch/i386/execregs.h
@@ -0,0 +1,69 @@
+/* $NetBSD: execregs.h,v 1.1 2025/02/27 00:55:32 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 CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * 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_I386_EXECREGS_H
+#define TESTS_KERNEL_ARCH_I386_EXECREGS_H
+
+#include <sys/cdefs.h>
+
+#define NEXECREGS 6
+
+#ifndef _LOCORE
+
+#include <unistd.h>
+
+/*
+ * Ordered by struct trapframe in sys/arch/i386/include/frame.h for
+ * convenience of auditing. Must match h_execregs.S.
+ */
+static const char *const regname[] = {
+ /* gs/fs/es/ds: segment registers, not really registers */
+ "edi",
+ "esi",
+ "ebp",
+ /* ebx: ps_strings */
+ "edx",
+ "ecx",
+ "eax",
+ /* trapno: not a register */
+ /* err: not a register */
+ /* eip: instruction pointer */
+ /* cs: segment register */
+ /* eflags */
+ /* esp: stack pointer */
+ /* ss: stack selector */
+};
+
+__CTASSERT(NEXECREGS == __arraycount(regname));
+
+int execregschild(char *);
+pid_t spawnregschild(char *, int);
+
+#endif /* _LOCORE */
+
+#endif /* TESTS_KERNEL_ARCH_I386_EXECREGS_H */
diff --git a/kernel/arch/i386/execsp.S b/kernel/arch/i386/execsp.S
new file mode 100644
index 000000000000..133af37715ad
--- /dev/null
+++ b/kernel/arch/i386/execsp.S
@@ -0,0 +1,112 @@
+/* $NetBSD: execsp.S,v 1.1 2025/04/20 22:33:13 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 CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * 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: execsp.S,v 1.1 2025/04/20 22:33:13 riastradh Exp $")
+
+/*
+ * void execsp_start(void (*cleanup@edx)(void), void *obj_main@ecx,
+ * struct ps_strings *ps_strings@ebx)
+ *
+ * ELF entry point. Saves the stack pointer in startsp and defers
+ * to the usual csu __start routine.
+ */
+ENTRY(execsp_start)
+ call getpc_eax
+ addl $_GLOBAL_OFFSET_TABLE_,%eax
+ movl _C_LABEL(startsp)@GOT(%eax),%eax
+ movl %esp,(%eax)
+ jmp _C_LABEL(__start)@PLT
+END(execsp_start)
+
+/*
+ * void execsp_ctor(void)
+ *
+ * ELF constructor. Saves the stack pointer in ctorsp and
+ * returns.
+ */
+ENTRY(execsp_ctor)
+ call getpc_eax
+ addl $_GLOBAL_OFFSET_TABLE_,%eax
+ movl _C_LABEL(ctorsp)@GOT(%eax),%eax
+ movl %esp,(%eax)
+ ret
+END(execsp_ctor)
+
+ /* Make execsp_ctor a constructor. */
+ .section .ctors,"aw",@progbits
+ .p2align 2
+ .long _C_LABEL(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)
+ call getpc_eax
+ addl $_GLOBAL_OFFSET_TABLE_,%eax
+ movl _C_LABEL(mainsp)@GOT(%eax),%eax
+ movl %esp,(%eax)
+ 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)
+ call getpc_eax
+ addl $_GLOBAL_OFFSET_TABLE_,%eax
+ movl _C_LABEL(dtorsp)@GOT(%eax),%eax
+ movl %esp,(%eax)
+ jmp _C_LABEL(execsp_main)@PLT
+END(execsp_dtor)
+
+ /* Make execsp_ctor a destructor. */
+ .section .dtors,"aw",@progbits
+ .p2align 2
+ .long _C_LABEL(execsp_dtor)
+
+ .text
+ _ALIGN_TEXT
+ .local getpc_eax
+ .type getpc_eax,@function
+getpc_eax:
+ movl (%esp),%eax
+ ret
+END(getpc_eax)
diff --git a/kernel/arch/i386/h_execregs.S b/kernel/arch/i386/h_execregs.S
new file mode 100644
index 000000000000..ac9b843f9d86
--- /dev/null
+++ b/kernel/arch/i386/h_execregs.S
@@ -0,0 +1,85 @@
+/* $NetBSD: h_execregs.S,v 1.1 2025/02/27 00:55:32 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 CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * 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 <sys/syscall.h>
+
+#include <machine/asm.h>
+
+#include "execregs.h"
+
+ENTRY(execregs_start)
+ andl $-0x4,%esp /* align stack to 4-byte boundary */
+
+ /* store registers to a buffer on stack */
+ subl $(NEXECREGS*4),%esp /* space for NEXECREGS registers */
+ movl %edi,0*4(%esp) /* order matches execregs.h */
+ movl %esi,1*4(%esp)
+ movl %ebp,2*4(%esp)
+ movl %edx,3*4(%esp)
+ movl %ecx,4*4(%esp)
+ movl %eax,5*4(%esp)
+
+ /* call write(STDOUT_FILENO, regs, sizeof(regs)) */
+ movl %esp,%eax /* eax := regs */
+ pushl $(NEXECREGS*4) /* arg2 := sizeof(regs) */
+ pushl %eax /* arg1 := regs */
+ pushl $0x1 /* arg0 := STDOUT_FILENO */
+ call execregs_write
+
+ jb 2f /* bail if write failed */
+ cmpl $(NEXECREGS*4),%eax /* bail if wrote wrong # of bytes */
+ jne 2f
+
+ /* call exit(0) */
+ pushl $0 /* arg0 := 0 */
+1: call execregs_exit
+ hlt /* paranoia */
+
+2: /* call exit(127) */
+ pushl $127 /* arg0 := 127 */
+ jmp 1b
+END(execregs_start)
+
+ENTRY(execregs_write)
+ movl $SYS_write,%eax /* syscall number */
+ int $0x80
+ retl
+END(execregs_write)
+
+ENTRY(execregs_exit)
+ movl $SYS_exit,%eax /* syscall number */
+ int $0x80
+ hlt
+END(execregs_exit)
+
+/* main stub to simplify linking */
+ENTRY(main)
+ hlt
+END(main)
diff --git a/kernel/arch/i386/signalsphandler.S b/kernel/arch/i386/signalsphandler.S
new file mode 100644
index 000000000000..418825e78ab6
--- /dev/null
+++ b/kernel/arch/i386/signalsphandler.S
@@ -0,0 +1,56 @@
+/* $NetBSD: signalsphandler.S,v 1.1 2025/04/20 22:33:13 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 CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * 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.S,v 1.1 2025/04/20 22:33:13 riastradh Exp $")
+
+/*
+ * signalsphandler(signo@esp[4])
+ *
+ * Signal handler. Store the stack pointer on entry at the global
+ * variable signalsp and return.
+ */
+ENTRY(signalsphandler)
+ call getpc_eax
+ addl $_GLOBAL_OFFSET_TABLE_,%eax
+ movl _C_LABEL(signalsp)@GOT(%eax),%eax
+ movl %esp,(%eax)
+ ret
+END(signalsphandler)
+
+ .text
+ _ALIGN_TEXT
+ .local getpc_eax
+ .type getpc_eax,@function
+getpc_eax:
+ movl (%esp),%eax
+ ret
+END(getpc_eax)
diff --git a/kernel/arch/i386/stack_pointer.h b/kernel/arch/i386/stack_pointer.h
new file mode 100644
index 000000000000..6c95a28ab7b6
--- /dev/null
+++ b/kernel/arch/i386/stack_pointer.h
@@ -0,0 +1,35 @@
+/* $NetBSD: stack_pointer.h,v 1.1 2025/04/20 22:33:13 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 CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * 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_I386_STACK_POINTER_H
+#define TESTS_KERNEL_ARCH_I386_STACK_POINTER_H
+
+#define MISALIGN_SP __asm __volatile("addl $-1,%esp")
+#define FIX_SP __asm __volatile("addl $1,%esp")
+
+#endif /* TESTS_KERNEL_ARCH_I386_STACK_POINTER_H */
diff --git a/kernel/arch/i386/threadspfunc.S b/kernel/arch/i386/threadspfunc.S
new file mode 100644
index 000000000000..af03ff4c550e
--- /dev/null
+++ b/kernel/arch/i386/threadspfunc.S
@@ -0,0 +1,43 @@
+/* $NetBSD: threadspfunc.S,v 1.1 2025/04/21 02:33:44 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 CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * 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: threadspfunc.S,v 1.1 2025/04/21 02:33:44 riastradh Exp $")
+
+/*
+ * void *threadspfunc(void *cookie@esp[4])
+ *
+ * pthread_create(3) function. Return the stack pointer on entry.
+ */
+ENTRY(threadspfunc)
+ mov %esp,%eax
+ ret
+END(threadspfunc)