1/* $NetBSD: freebsd_syscall.c,v 1.7 2023/10/05 19:41:06 ad Exp $ */ 2 3/*- 4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__KERNEL_RCSID(0, "$NetBSD: freebsd_syscall.c,v 1.7 2023/10/05 19:41:06 ad Exp $"); 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/proc.h> 38#include <sys/signal.h> 39#include <sys/syscall.h> 40#include <sys/syscallvar.h> 41 42#include <uvm/uvm_extern.h> 43 44#include <machine/cpu.h> 45#include <machine/psl.h> 46#include <machine/userret.h> 47 48#include <compat/sys/signal.h> 49 50#include <compat/freebsd/freebsd_machdep.h> 51#include <compat/freebsd/freebsd_syscall.h> 52 53void freebsd_syscall(struct trapframe *); 54 55void 56freebsd_syscall_intern(struct proc *p) 57{ 58 59 p->p_md.md_syscall = freebsd_syscall; 60} 61 62/* 63 * syscall(frame): 64 * System call request from POSIX system call gate interface to kernel. 65 * Like trap(), argument is call by reference. 66 */ 67void 68freebsd_syscall(struct trapframe *frame) 69{ 70 char *params; 71 const struct sysent *callp; 72 struct lwp *l; 73 struct proc *p; 74 int error; 75 size_t argsize; 76 register_t code, args[8], rval[2]; 77 78 l = curlwp; 79 p = l->l_proc; 80 81 code = frame->tf_eax; 82 callp = p->p_emul->e_sysent; 83 params = (char *)frame->tf_esp + sizeof(int); 84 85 switch (code) { 86 case SYS_syscall: 87 /* 88 * Code is first argument, followed by actual args. 89 */ 90 error = ufetch_long((void *)params, (u_long *)&code); 91 if (error) 92 goto bad; 93 params += sizeof(int); 94 break; 95 case SYS___syscall: 96 /* 97 * Like syscall, but code is a quad, so as to maintain 98 * quad alignment for the rest of the arguments. 99 */ 100 error = ufetch_long((void *)(params + 101 _QUAD_LOWWORD * sizeof(int)), 102 (u_long *)&code); 103 if (error) 104 goto bad; 105 params += sizeof(quad_t); 106 break; 107 default: 108 break; 109 } 110 111 code &= (SYS_NSYSENT - 1); 112 callp += code; 113 argsize = callp->sy_argsize; 114 if (argsize) { 115 error = copyin(params, (void *)args, argsize); 116 if (error) 117 goto bad; 118 } 119 120 if (!__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_entry)) 121 || (error = trace_enter(code, callp, args)) == 0) { 122 rval[0] = 0; 123 rval[1] = frame->tf_edx; /* need to keep edx for shared FreeBSD bins */ 124 error = sy_call(callp, l, args, rval); 125 } 126 127 switch (error) { 128 case 0: 129 frame->tf_eax = rval[0]; 130 frame->tf_edx = rval[1]; 131 frame->tf_eflags &= ~PSL_C; /* carry bit */ 132 break; 133 case ERESTART: 134 /* 135 * The offset to adjust the PC by depends on whether we entered 136 * the kernel through the trap or call gate. We pushed the 137 * size of the instruction into tf_err on entry. 138 */ 139 frame->tf_eip -= frame->tf_err; 140 break; 141 case EJUSTRETURN: 142 /* nothing to do */ 143 break; 144 default: 145 bad: 146 frame->tf_eax = error; 147 frame->tf_eflags |= PSL_C; /* carry bit */ 148 break; 149 } 150 151 if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_return))) 152 trace_exit(code, callp, args, rval, error); 153 154 userret(l); 155} 156