xref: /netbsd/sys/arch/usermode/usermode/syscall.c (revision d86cb3c3)
1 /* $NetBSD: syscall.c,v 1.26 2023/04/03 17:58:41 gutteridge Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
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 __KERNEL_RCSID(0, "$NetBSD: syscall.c,v 1.26 2023/04/03 17:58:41 gutteridge Exp $");
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.h>
36 #include <sys/lwp.h>
37 #include <sys/sched.h>
38 #include <sys/ktrace.h>
39 #include <sys/syscall.h>
40 #include <sys/syscallvar.h>
41 #include <sys/syscallargs.h>
42 
43 #include <sys/userret.h>
44 #include <machine/pcb.h>
45 #include <machine/thunk.h>
46 #include <machine/machdep.h>
47 
48 void
md_child_return(struct lwp * l)49 md_child_return(struct lwp *l)
50 {
51 	register_t rval[2];
52 	struct pcb *pcb = lwp_getpcb(l);
53 	ucontext_t *ucp = &pcb->pcb_userret_ucp;
54 
55 	/* return value zero */
56 	rval[0] = 0;
57 	rval[1] = 0;
58 	md_syscall_set_returnargs(l, ucp, 0, rval);
59 
60 	aprint_debug("child return! lwp %p\n", l);
61 	userret(l);
62 }
63 
64 /*
65  * Process the tail end of a posix_spawn() for the child.
66  */
67 void
cpu_spawn_return(struct lwp * l)68 cpu_spawn_return(struct lwp *l)
69 {
70 
71 	userret(l);
72 }
73 
74 extern const char *const syscallnames[];
75 
76 static void syscall_args_print(lwp_t *l, int code, int nargs, int argsize,
77 	register_t *args);
78 static void syscall_retvals_print(lwp_t *l, lwp_t *clwp,
79 	int code, int nargs, register_t *args, int error, register_t *rval);
80 
81 
82 void
syscall(void)83 syscall(void)
84 {
85 	lwp_t *l = curlwp;
86 	const struct proc * const p = l->l_proc;
87 	const struct sysent *callp;
88 	struct pcb *pcb = lwp_getpcb(l);
89 	ucontext_t *ucp = &pcb->pcb_userret_ucp;
90 	register_t copyargs[2+SYS_MAXSYSARGS];
91 	register_t *args;
92 	register_t rval[2];
93 	uint32_t code, opcode;
94 	uint nargs, argsize;
95 	int error;
96 
97 	/* system call accounting */
98 	curcpu()->ci_data.cpu_nsyscall++;
99 	LWP_CACHE_CREDS(l, l->l_proc);
100 
101 	/* XXX do we want do do emulation? */
102 	md_syscall_get_opcode(ucp, &opcode);
103 	md_syscall_get_syscallnumber(ucp, &code);
104 	code &= (SYS_NSYSENT -1);
105 
106 	callp   = p->p_emul->e_sysent + code;
107 	nargs   = callp->sy_narg;
108 	argsize = callp->sy_argsize;
109 
110 	args  = copyargs;
111 	rval[0] = rval[1] = 0;
112 	error = md_syscall_getargs(l, ucp, nargs, argsize, args);
113 
114 #if 0
115 	aprint_debug("syscall no. %d, ", code);
116 	aprint_debug("nargs %d, argsize %d =>  ", nargs, argsize);
117 	thunk_printf_debug("syscall no. %d, ", code);
118 	thunk_printf_debug("nargs %d, argsize %d =>  ", nargs, argsize);
119 #endif
120 
121 	/*
122 	 * TODO change the pre and post printing into functions so they can be
123 	 * easily adjusted and dont clobber up this space
124 	 */
125 
126 	if (!error)
127 		syscall_args_print(l, code, nargs, argsize, args);
128 
129 	md_syscall_inc_pc(ucp, opcode);
130 
131 	if (!error) {
132 		error = sy_invoke(callp, l, args, rval, code);
133 	}
134 
135 	syscall_retvals_print(l, curlwp, code, nargs, args, error, rval);
136 
137 //out:
138 	switch (error) {
139 	default:
140 		/* FALLTHROUGH */
141 	case 0:
142 		md_syscall_set_returnargs(l, ucp, error, rval);
143 		/* FALLTHROUGH */
144 	case EJUSTRETURN:
145 		break;
146 	case ERESTART:
147 		md_syscall_dec_pc(ucp, opcode);
148 		/* nothing to do */
149 		break;
150 	}
151 	//thunk_printf_debug("end of syscall : return to userland\n");
152 //if (code != 4) thunk_printf("userret() code %d\n", code);
153 }
154 
155 
156 static void
syscall_args_print(lwp_t * l,int code,int nargs,int argsize,register_t * args)157 syscall_args_print(lwp_t *l, int code, int nargs, int argsize, register_t *args)
158 {
159 	char **argv, **envp;
160 
161 return;
162 	if (code != 4) {
163 		thunk_printf("lwp %p, code %3d, nargs %d, argsize %3d\t%s(",
164 			l, code, nargs, argsize, syscallnames[code]);
165 		switch (code) {
166 		case 5:
167 			thunk_printf("\"%s\", %"PRIx32", %"PRIx32"", (char *) (args[0]), (uint) args[1], (uint) args[2]);
168 			break;
169 		case 33:
170 			thunk_printf("\"%s\", %"PRIx32"", (char *) (args[0]), (uint) args[1]);
171 			break;
172 		case 50:
173 			thunk_printf("\"%s\"", (char *) (args[0]));
174 			break;
175 		case 58:
176 			thunk_printf("\"%s\", %"PRIx32", %"PRIx32"", (char *) (args[0]), (uint) (args[1]), (uint) args[2]);
177 			break;
178 		case 59:
179 			thunk_printf("\"%s\", [", (char *) (args[0]));
180 			argv = (char **) (args[1]);
181 			if (*argv) {
182 				while (*argv) {
183 					thunk_printf("\"%s\", ", *argv);
184 					argv++;
185 				}
186 				thunk_printf("\b\b");
187 			}
188 			thunk_printf("], [");
189 			envp = (char **) (args[2]);
190 			if (*envp) {
191 				while (*envp) {
192 					thunk_printf("\"%s\", ", *envp);
193 					envp++;
194 				}
195 				thunk_printf("\b\b");
196 			}
197 			thunk_printf("]");
198 			break;
199 		default:
200 			for (int i = 0; i < nargs; i++)
201 				thunk_printf("%"PRIx32", ", (uint) args[i]);
202 			if (nargs)
203 				thunk_printf("\b\b");
204 		}
205 		thunk_printf(") ");
206 	}
207 #if 0
208 	if ((code == 4)) {
209 //		thunk_printf_debug("[us] %s", (char *) args[1]);
210 		thunk_printf("[us] %s", (char *) args[1]);
211 	}
212 #endif
213 }
214 
215 
216 static void
syscall_retvals_print(lwp_t * l,lwp_t * clwp,int code,int nargs,register_t * args,int error,register_t * rval)217 syscall_retvals_print(lwp_t *l, lwp_t *clwp, int code, int nargs, register_t *args, int error, register_t *rval)
218 {
219 	char const *errstr;
220 
221 return;
222 	switch (error) {
223 	case EJUSTRETURN:
224 		errstr = "EJUSTRETURN";
225 		break;
226 	case EAGAIN:
227 		errstr = "EGAIN";
228 		break;
229 	default:
230 		errstr = "OK";
231 	}
232 	if (code != 4)
233 		thunk_printf("=> %s: %d, (%"PRIx32", %"PRIx32")\n",
234 			errstr, error, (uint) (rval[0]), (uint) (rval[1]));
235 }
236