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