Home | History | Annotate | Line # | Download | only in aarch64
execregs.c revision 1.1
      1 /*	$NetBSD: execregs.c,v 1.1 2025/02/27 00:55:31 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2025 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __RCSID("$NetBSD: execregs.c,v 1.1 2025/02/27 00:55:31 riastradh Exp $");
     31 
     32 #include "execregs.h"
     33 
     34 #include <errno.h>
     35 #include <spawn.h>
     36 #include <stddef.h>
     37 #include <unistd.h>
     38 
     39 extern char **environ;
     40 
     41 static unsigned long
     42 nonnull(unsigned long x)
     43 {
     44 
     45 	x |= x << 8;
     46 	x |= x << 16;
     47 	x |= x << 32;
     48 	return x;
     49 }
     50 
     51 int
     52 execregschild(char *path)
     53 {
     54 	/* x0: used to pass exec arg0, nonnull anyway (path) */
     55 	/* x1: used to pass exec arg1, nonnull anyway (argv) */
     56 	/* x2: used to pass exec arg2, nonnull anyway (environ) */
     57 	register long x3 __asm("x3") = nonnull(3);
     58 	register long x4 __asm("x4") = nonnull(4);
     59 	register long x5 __asm("x5") = nonnull(5);
     60 	register long x6 __asm("x6") = nonnull(6);
     61 	register long x7 __asm("x7") = nonnull(7);
     62 	register long x8 __asm("x8") = nonnull(8);
     63 	register long x9 __asm("x9") = nonnull(9);
     64 	register long x10 __asm("x10") = nonnull(10);
     65 	register long x11 __asm("x11") = nonnull(11);
     66 	register long x12 __asm("x12") = nonnull(12);
     67 	register long x13 __asm("x13") = nonnull(13);
     68 	register long x14 __asm("x14") = nonnull(14);
     69 	register long x15 __asm("x15") = nonnull(15);
     70 	register long x16 __asm("x16") = nonnull(16);
     71 	register long x17 __asm("x17") = nonnull(17);
     72 	register long x18 __asm("x18") = nonnull(18);
     73 	register long x19 __asm("x19") = nonnull(19);
     74 	register long x20 __asm("x20") = nonnull(20);
     75 	register long x21 __asm("x21") = nonnull(21);
     76 	register long x22 __asm("x22") = nonnull(22);
     77 	register long x23 __asm("x23") = nonnull(23);
     78 	register long x24 __asm("x24") = nonnull(24);
     79 	register long x25 __asm("x25") = nonnull(25);
     80 	register long x26 __asm("x26") = nonnull(26);
     81 	register long x27 __asm("x27") = nonnull(27);
     82 	register long x28 __asm("x28") = nonnull(28);
     83 	/* x29: frame pointer, nonnull anyway */
     84 	/* x30: link register, nonnull anyway */
     85 
     86 	char *argv[] = {path, NULL};
     87 	char **envp = environ;
     88 
     89 	/*
     90 	 * Not perfect -- compiler might use some registers for
     91 	 * stack/argument transfers, but all the arguments are nonnull
     92 	 * so this is probably a good test anyway.
     93 	 */
     94 	__asm volatile("" :
     95 	    "+r"(x3),
     96 	    "+r"(x4),
     97 	    "+r"(x5),
     98 	    "+r"(x6),
     99 	    "+r"(x7),
    100 	    "+r"(x8),
    101 	    "+r"(x9),
    102 	    "+r"(x10),
    103 	    "+r"(x11),
    104 	    "+r"(x12),
    105 	    "+r"(x13),
    106 	    "+r"(x14),
    107 	    "+r"(x15),
    108 	    "+r"(x16),
    109 	    "+r"(x17)
    110 	    :: "memory");
    111 	/* pacify gcc error: more than 30 operands in 'asm' */
    112 	__asm volatile("" :
    113 	    "+r"(x18),
    114 	    "+r"(x19),
    115 	    "+r"(x20),
    116 	    "+r"(x21),
    117 	    "+r"(x22),
    118 	    "+r"(x23),
    119 	    "+r"(x24),
    120 	    "+r"(x25),
    121 	    "+r"(x26),
    122 	    "+r"(x27),
    123 	    "+r"(x28)
    124 	    :: "memory");
    125 
    126 	return execve(path, argv, envp);
    127 }
    128 
    129 pid_t
    130 spawnregschild(char *path, int fd)
    131 {
    132 	/* x0: used to pass posix_spawn arg0, nonnull anyway (&pid) */
    133 	/* x1: used to pass posix_spawn arg1, nonnull anyway (path) */
    134 	/* x2: used to pass posix_spawn arg2, nonnull anyway (&fileacts) */
    135 	/* x3: used to pass posix_spawn arg3, nonnull anyway (&attr) */
    136 	/* x4: used to pass posix_spawn arg3, nonnull anyway (argv) */
    137 	/* x5: used to pass posix_spawn arg3, nonnull anyway (environ) */
    138 	register long x6 __asm("x6") = nonnull(6);
    139 	register long x7 __asm("x7") = nonnull(7);
    140 	register long x8 __asm("x8") = nonnull(8);
    141 	register long x9 __asm("x9") = nonnull(9);
    142 	register long x10 __asm("x10") = nonnull(10);
    143 	register long x11 __asm("x11") = nonnull(11);
    144 	register long x12 __asm("x12") = nonnull(12);
    145 	register long x13 __asm("x13") = nonnull(13);
    146 	register long x14 __asm("x14") = nonnull(14);
    147 	register long x15 __asm("x15") = nonnull(15);
    148 	register long x16 __asm("x16") = nonnull(16);
    149 	register long x17 __asm("x17") = nonnull(17);
    150 	register long x18 __asm("x18") = nonnull(18);
    151 	register long x19 __asm("x19") = nonnull(19);
    152 	register long x20 __asm("x20") = nonnull(20);
    153 	register long x21 __asm("x21") = nonnull(21);
    154 	register long x22 __asm("x22") = nonnull(22);
    155 	register long x23 __asm("x23") = nonnull(23);
    156 	register long x24 __asm("x24") = nonnull(24);
    157 	register long x25 __asm("x25") = nonnull(25);
    158 	register long x26 __asm("x26") = nonnull(26);
    159 	register long x27 __asm("x27") = nonnull(27);
    160 	register long x28 __asm("x28") = nonnull(28);
    161 	/* x29: frame pointer, nonnull anyway */
    162 	/* x30: link register, nonnull anyway */
    163 
    164 	char *argv[] = {path, NULL};
    165 	char **envp = environ;
    166 	posix_spawn_file_actions_t fileacts;
    167 	posix_spawnattr_t attr;
    168 	pid_t pid;
    169 	int error;
    170 
    171 	error = posix_spawn_file_actions_init(&fileacts);
    172 	if (error)
    173 		goto out;
    174 	error = posix_spawn_file_actions_adddup2(&fileacts, fd, STDOUT_FILENO);
    175 	if (error)
    176 		goto out;
    177 	error = posix_spawnattr_init(&attr);
    178 	if (error)
    179 		goto out;
    180 
    181 	/*
    182 	 * Not perfect -- compiler might use some registers for
    183 	 * stack/argument transfers, but all the arguments are nonnull
    184 	 * so this is probably a good test anyway.
    185 	 */
    186 	__asm volatile("" :
    187 	    "+r"(x6),
    188 	    "+r"(x7),
    189 	    "+r"(x8),
    190 	    "+r"(x9),
    191 	    "+r"(x10),
    192 	    "+r"(x11),
    193 	    "+r"(x12),
    194 	    "+r"(x13),
    195 	    "+r"(x14),
    196 	    "+r"(x15),
    197 	    "+r"(x16),
    198 	    "+r"(x17),
    199 	    "+r"(x18),
    200 	    "+r"(x19),
    201 	    "+r"(x20)
    202 	    :: "memory");
    203 	/* pacify gcc error: more than 30 operands in 'asm' */
    204 	__asm volatile("" :
    205 	    "+r"(x21),
    206 	    "+r"(x22),
    207 	    "+r"(x23),
    208 	    "+r"(x24),
    209 	    "+r"(x25),
    210 	    "+r"(x26),
    211 	    "+r"(x27),
    212 	    "+r"(x28)
    213 	    :: "memory");
    214 
    215 	error = posix_spawn(&pid, path, &fileacts, &attr, argv, envp);
    216 	if (error)
    217 		goto out;
    218 
    219 out:	posix_spawnattr_destroy(&attr);
    220 	posix_spawn_file_actions_destroy(&fileacts);
    221 	if (error) {
    222 		errno = error;
    223 		return -1;
    224 	}
    225 	return 0;
    226 }
    227