1 /* $NetBSD: syscall.c,v 1.27 2023/10/05 19:41:06 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) 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.27 2023/10/05 19:41:06 ad 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 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 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 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 100 /* XXX do we want do do emulation? */ 101 md_syscall_get_opcode(ucp, &opcode); 102 md_syscall_get_syscallnumber(ucp, &code); 103 code &= (SYS_NSYSENT -1); 104 105 callp = p->p_emul->e_sysent + code; 106 nargs = callp->sy_narg; 107 argsize = callp->sy_argsize; 108 109 args = copyargs; 110 rval[0] = rval[1] = 0; 111 error = md_syscall_getargs(l, ucp, nargs, argsize, args); 112 113 #if 0 114 aprint_debug("syscall no. %d, ", code); 115 aprint_debug("nargs %d, argsize %d => ", nargs, argsize); 116 thunk_printf_debug("syscall no. %d, ", code); 117 thunk_printf_debug("nargs %d, argsize %d => ", nargs, argsize); 118 #endif 119 120 /* 121 * TODO change the pre and post printing into functions so they can be 122 * easily adjusted and dont clobber up this space 123 */ 124 125 if (!error) 126 syscall_args_print(l, code, nargs, argsize, args); 127 128 md_syscall_inc_pc(ucp, opcode); 129 130 if (!error) { 131 error = sy_invoke(callp, l, args, rval, code); 132 } 133 134 syscall_retvals_print(l, curlwp, code, nargs, args, error, rval); 135 136 //out: 137 switch (error) { 138 default: 139 /* FALLTHROUGH */ 140 case 0: 141 md_syscall_set_returnargs(l, ucp, error, rval); 142 /* FALLTHROUGH */ 143 case EJUSTRETURN: 144 break; 145 case ERESTART: 146 md_syscall_dec_pc(ucp, opcode); 147 /* nothing to do */ 148 break; 149 } 150 //thunk_printf_debug("end of syscall : return to userland\n"); 151 //if (code != 4) thunk_printf("userret() code %d\n", code); 152 } 153 154 155 static void 156 syscall_args_print(lwp_t *l, int code, int nargs, int argsize, register_t *args) 157 { 158 char **argv, **envp; 159 160 return; 161 if (code != 4) { 162 thunk_printf("lwp %p, code %3d, nargs %d, argsize %3d\t%s(", 163 l, code, nargs, argsize, syscallnames[code]); 164 switch (code) { 165 case 5: 166 thunk_printf("\"%s\", %"PRIx32", %"PRIx32"", (char *) (args[0]), (uint) args[1], (uint) args[2]); 167 break; 168 case 33: 169 thunk_printf("\"%s\", %"PRIx32"", (char *) (args[0]), (uint) args[1]); 170 break; 171 case 50: 172 thunk_printf("\"%s\"", (char *) (args[0])); 173 break; 174 case 58: 175 thunk_printf("\"%s\", %"PRIx32", %"PRIx32"", (char *) (args[0]), (uint) (args[1]), (uint) args[2]); 176 break; 177 case 59: 178 thunk_printf("\"%s\", [", (char *) (args[0])); 179 argv = (char **) (args[1]); 180 if (*argv) { 181 while (*argv) { 182 thunk_printf("\"%s\", ", *argv); 183 argv++; 184 } 185 thunk_printf("\b\b"); 186 } 187 thunk_printf("], ["); 188 envp = (char **) (args[2]); 189 if (*envp) { 190 while (*envp) { 191 thunk_printf("\"%s\", ", *envp); 192 envp++; 193 } 194 thunk_printf("\b\b"); 195 } 196 thunk_printf("]"); 197 break; 198 default: 199 for (int i = 0; i < nargs; i++) 200 thunk_printf("%"PRIx32", ", (uint) args[i]); 201 if (nargs) 202 thunk_printf("\b\b"); 203 } 204 thunk_printf(") "); 205 } 206 #if 0 207 if ((code == 4)) { 208 // thunk_printf_debug("[us] %s", (char *) args[1]); 209 thunk_printf("[us] %s", (char *) args[1]); 210 } 211 #endif 212 } 213 214 215 static void 216 syscall_retvals_print(lwp_t *l, lwp_t *clwp, int code, int nargs, register_t *args, int error, register_t *rval) 217 { 218 char const *errstr; 219 220 return; 221 switch (error) { 222 case EJUSTRETURN: 223 errstr = "EJUSTRETURN"; 224 break; 225 case EAGAIN: 226 errstr = "EGAIN"; 227 break; 228 default: 229 errstr = "OK"; 230 } 231 if (code != 4) 232 thunk_printf("=> %s: %d, (%"PRIx32", %"PRIx32")\n", 233 errstr, error, (uint) (rval[0]), (uint) (rval[1])); 234 } 235