#include <sys/cdefs.h>
__RCSID("$NetBSD: execregs.c,v 1.1 2025/02/27 00:55:31 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;
x |= x << 32;
return x;
}
int
execregschild(char *path)
{
register long x3 __asm("x3") = nonnull(3);
register long x4 __asm("x4") = nonnull(4);
register long x5 __asm("x5") = nonnull(5);
register long x6 __asm("x6") = nonnull(6);
register long x7 __asm("x7") = nonnull(7);
register long x8 __asm("x8") = nonnull(8);
register long x9 __asm("x9") = nonnull(9);
register long x10 __asm("x10") = nonnull(10);
register long x11 __asm("x11") = nonnull(11);
register long x12 __asm("x12") = nonnull(12);
register long x13 __asm("x13") = nonnull(13);
register long x14 __asm("x14") = nonnull(14);
register long x15 __asm("x15") = nonnull(15);
register long x16 __asm("x16") = nonnull(16);
register long x17 __asm("x17") = nonnull(17);
register long x18 __asm("x18") = nonnull(18);
register long x19 __asm("x19") = nonnull(19);
register long x20 __asm("x20") = nonnull(20);
register long x21 __asm("x21") = nonnull(21);
register long x22 __asm("x22") = nonnull(22);
register long x23 __asm("x23") = nonnull(23);
register long x24 __asm("x24") = nonnull(24);
register long x25 __asm("x25") = nonnull(25);
register long x26 __asm("x26") = nonnull(26);
register long x27 __asm("x27") = nonnull(27);
register long x28 __asm("x28") = nonnull(28);
char *argv[] = {path, NULL};
char **envp = environ;
__asm volatile("" :
"+r"(x3),
"+r"(x4),
"+r"(x5),
"+r"(x6),
"+r"(x7),
"+r"(x8),
"+r"(x9),
"+r"(x10),
"+r"(x11),
"+r"(x12),
"+r"(x13),
"+r"(x14),
"+r"(x15),
"+r"(x16),
"+r"(x17)
:: "memory");
__asm volatile("" :
"+r"(x18),
"+r"(x19),
"+r"(x20),
"+r"(x21),
"+r"(x22),
"+r"(x23),
"+r"(x24),
"+r"(x25),
"+r"(x26),
"+r"(x27),
"+r"(x28)
:: "memory");
return execve(path, argv, envp);
}
pid_t
spawnregschild(char *path, int fd)
{
register long x6 __asm("x6") = nonnull(6);
register long x7 __asm("x7") = nonnull(7);
register long x8 __asm("x8") = nonnull(8);
register long x9 __asm("x9") = nonnull(9);
register long x10 __asm("x10") = nonnull(10);
register long x11 __asm("x11") = nonnull(11);
register long x12 __asm("x12") = nonnull(12);
register long x13 __asm("x13") = nonnull(13);
register long x14 __asm("x14") = nonnull(14);
register long x15 __asm("x15") = nonnull(15);
register long x16 __asm("x16") = nonnull(16);
register long x17 __asm("x17") = nonnull(17);
register long x18 __asm("x18") = nonnull(18);
register long x19 __asm("x19") = nonnull(19);
register long x20 __asm("x20") = nonnull(20);
register long x21 __asm("x21") = nonnull(21);
register long x22 __asm("x22") = nonnull(22);
register long x23 __asm("x23") = nonnull(23);
register long x24 __asm("x24") = nonnull(24);
register long x25 __asm("x25") = nonnull(25);
register long x26 __asm("x26") = nonnull(26);
register long x27 __asm("x27") = nonnull(27);
register long x28 __asm("x28") = nonnull(28);
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;
__asm volatile("" :
"+r"(x6),
"+r"(x7),
"+r"(x8),
"+r"(x9),
"+r"(x10),
"+r"(x11),
"+r"(x12),
"+r"(x13),
"+r"(x14),
"+r"(x15),
"+r"(x16),
"+r"(x17),
"+r"(x18),
"+r"(x19),
"+r"(x20)
:: "memory");
__asm volatile("" :
"+r"(x21),
"+r"(x22),
"+r"(x23),
"+r"(x24),
"+r"(x25),
"+r"(x26),
"+r"(x27),
"+r"(x28)
:: "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;
}