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