| 1 | /*	$OpenBSD: syscall_mi.h,v 1.37 2024/12/27 11:57:16 mpi Exp $	*/ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1982, 1986, 1989, 1993 |
| 5 | *	The Regents of the University of California. 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 | * 3. Neither the name of the University nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | * SUCH DAMAGE. |
| 30 | * |
| 31 | *	@(#)kern_xxx.c	8.2 (Berkeley) 11/14/93 |
| 32 | */ |
| 33 | |
| 34 | #include <sys/param.h> |
| 35 | #include <sys/pledge.h> |
| 36 | #include <sys/acct.h> |
| 37 | #include <sys/tracepoint.h> |
| 38 | #include <sys/syscall.h> |
| 39 | #include <sys/signalvar.h> |
| 40 | #include <uvm/uvm_extern.h> |
| 41 | |
| 42 | #ifdef KTRACE |
| 43 | #include <sys/ktrace.h> |
| 44 | #endif |
| 45 | |
| 46 | #include "dt.h" |
| 47 | #if NDT > 0 |
| 48 | #include <dev/dt/dtvar.h> |
| 49 | #endif |
| 50 | |
| 51 | /* |
| 52 | * Check if a system call is entered from precisely correct location |
| 53 | */ |
| 54 | static inline int |
| 55 | pin_check(struct proc *p, register_t code) |
| 56 | { |
| 57 | 	extern char sigcodecall[], sigcoderet[], sigcodecall[]; |
| 58 | 	struct pinsyscall *pin = NULL, *ppin, *plibcpin; |
| 59 | 	struct process *pr = p->p_p; |
| 60 | 	vaddr_t addr; |
| 61 | 	int error = 0; |
| 62 | |
| 63 | 	/* point at start of syscall instruction */ |
| 64 | 	addr = (vaddr_t)PROC_PC(p) - (vaddr_t)(sigcoderet - sigcodecall); |
| 65 | 	ppin = &pr->ps_pin; |
| 66 | 	plibcpin = &pr->ps_libcpin; |
| 67 | |
| 68 | 	/* |
| 69 | 	 * System calls come from the following places, checks are ordered |
| 70 | 	 * by most common case: |
| 71 | 	 * 1) dynamic binary: syscalls in libc.so (in the ps_libcpin region) |
| 72 | 	 * 2a) static binary: syscalls in main program (in the ps_pin region) |
| 73 | 	 * 2b) dynamic binary: syscalls in ld.so (in the ps_pin region) |
| 74 | 	 * 3) sigtramp, containing only sigreturn(2) |
| 75 | 	 */ |
| 76 | 	if (plibcpin->pn_pins && |
| 77 | 	 addr >= plibcpin->pn_start && addr < plibcpin->pn_end) |
| 78 | 		pin = plibcpin; |
| 79 | 	else if (ppin->pn_pins && |
| 80 | 	 addr >= ppin->pn_start && addr < ppin->pn_end) |
| 81 | 		pin = ppin; |
| 82 | 	else if (PROC_PC(p) == pr->ps_sigcoderet) { |
| 83 | 		if (code == SYS_sigreturn) |
| 84 | 			return (0); |
| 85 | 		error = EPERM; |
| 86 | 		goto die; |
| 87 | 	} |
| 88 | 	if (pin) { |
| 89 | 		if (code >= pin->pn_npins || pin->pn_pins[code] == 0) |
| 90 | 			error = ENOSYS; |
| 91 | 		else if (pin->pn_pins[code] + pin->pn_start == addr) |
| 92 | 			; /* correct location */ |
| 93 | 		else if (pin->pn_pins[code] == (u_int)-1) |
| 94 | 			; /* multiple locations, hopefully a boring operation */ |
| 95 | 		else |
| 96 | 			error = ENOSYS; |
| 97 | 	} else |
| 98 | 		error = ENOSYS; |
| 99 | 	if (error == 0) |
| 100 | 		return (0); |
| 101 | die: |
| 102 | #ifdef KTRACE |
| 103 | 	if (KTRPOINT(p, KTR_PINSYSCALL)) |
| 104 | 		ktrpinsyscall(p, error, code, addr); |
| 105 | #endif |
| 106 | 	KERNEL_LOCK(); |
| 107 | 	/* XXX remove or simplify this uprintf() call after OpenBSD 7.5 release */ |
| 108 | 	uprintf("%s[%d]: pinsyscalls addr %lx code %ld, pinoff 0x%x " |
| 109 | 	 "(pin%s %d %lx-%lx %lx) (libcpin%s %d %lx-%lx %lx) error %d\n", |
| 110 | 	 p->p_p->ps_comm, p->p_p->ps_pid, addr, code, |
| 111 | 	 (pin && code < pin->pn_npins) ? pin->pn_pins[code] : -1, |
| 112 | 	 pin == ppin ? "(Y)" : "", ppin->pn_npins, |
| 113 | 	 ppin->pn_start, ppin->pn_end, ppin->pn_end - ppin->pn_start, |
| 114 | 	 pin == plibcpin ? "(Y)" : "", plibcpin->pn_npins, |
| 115 | 	 plibcpin->pn_start, plibcpin->pn_end, plibcpin->pn_end - plibcpin->pn_start, |
| 116 | 	 error); |
| 117 | p->p_p->ps_acflag |= APINSYS; |
| 118 | |
| 119 | 	/* Try to stop threads immediately, because this process is suspect */ |
| 120 | 	if (P_HASSIBLING(p)) |
| 121 | 		single_thread_set(p, SINGLE_UNWIND | SINGLE_DEEP); |
| 122 | 	/* Send uncatchable SIGABRT for coredump */ |
| 123 | 	sigabort(p); |
| 124 | 	KERNEL_UNLOCK(); |
| 125 | 	return (error); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * The MD setup for a system call has been done; here's the MI part. |
| 130 | */ |
| 131 | static inline int |
| 132 | mi_syscall(struct proc *p, register_t code, const struct sysent *callp, |
| 133 | register_t *argp, register_t retval[2]) |
| 134 | { |
| 135 | 	uint64_t tval; |
| 136 | 	int lock = !(callp->sy_flags & SY_NOLOCK); |
| 137 | 	int error, pledged; |
| 138 | |
| 139 | 	/* refresh the thread's cache of the process's creds */ |
| 140 | 	refreshcreds(p); |
| 141 | |
| 142 | #ifdef SYSCALL_DEBUG |
| 143 | 	KERNEL_LOCK(); |
| 144 | 	scdebug_call(p, code, argp); |
| 145 | 	KERNEL_UNLOCK(); |
| 146 | #endif |
| 147 | 	TRACEPOINT(raw_syscalls, sys_enter, code, NULL); |
| 148 | #if NDT > 0 |
| 149 | 	DT_ENTER(syscall, code, callp->sy_argsize, argp); |
| 150 | #endif |
| 151 | #ifdef KTRACE |
| 152 | 	if (KTRPOINT(p, KTR_SYSCALL)) { |
| 153 | 		/* convert to mask, then include with code */ |
| 154 | 		ktrsyscall(p, code, callp->sy_argsize, argp); |
| 155 | 	} |
| 156 | #endif |
| 157 | |
| 158 | 	/* SP must be within MAP_STACK space */ |
| 159 | 	if (!uvm_map_inentry(p, &p->p_spinentry, PROC_STACK(p), |
| 160 | 	 "[%s]%d/%d sp=%lx inside %lx-%lx: not MAP_STACK\n", |
| 161 | 	 uvm_map_inentry_sp, p->p_vmspace->vm_map.sserial)) |
| 162 | 		return (EPERM); |
| 163 | |
| 164 | 	if ((error = pin_check(p, code))) |
| 165 | 		return (error); |
| 166 | |
| 167 | 	pledged = (p->p_p->ps_flags & PS_PLEDGE); |
| 168 | 	if (pledged && (error = pledge_syscall(p, code, &tval))) { |
| 169 | 		KERNEL_LOCK(); |
| 170 | 		error = pledge_fail(p, error, tval); |
| 171 | 		KERNEL_UNLOCK(); |
| 172 | 		return (error); |
| 173 | 	} |
| 174 | 	if (lock) |
| 175 | 		KERNEL_LOCK(); |
| 176 | 	error = (*callp->sy_call)(p, argp, retval); |
| 177 | 	if (lock) |
| 178 | 		KERNEL_UNLOCK(); |
| 179 | |
| 180 | 	return (error); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Finish MI stuff on return, after the registers have been set |
| 185 | */ |
| 186 | static inline void |
| 187 | mi_syscall_return(struct proc *p, register_t code, int error, |
| 188 | const register_t retval[2]) |
| 189 | { |
| 190 | #ifdef SYSCALL_DEBUG |
| 191 | 	KERNEL_LOCK(); |
| 192 | 	scdebug_ret(p, code, error, retval); |
| 193 | 	KERNEL_UNLOCK(); |
| 194 | #endif |
| 195 | #if NDT > 0 |
| 196 | 	DT_LEAVE(syscall, code, error, retval[0], retval[1]); |
| 197 | #endif |
| 198 | 	TRACEPOINT(raw_syscalls, sys_exit, code, NULL); |
| 199 | |
| 200 | 	userret(p); |
| 201 | |
| 202 | #ifdef KTRACE |
| 203 | 	if (KTRPOINT(p, KTR_SYSRET)) |
| 204 | 		ktrsysret(p, code, error, retval); |
| 205 | #endif |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Finish MI stuff for a new process/thread to return |
| 210 | */ |
| 211 | static inline void |
| 212 | mi_child_return(struct proc *p) |
| 213 | { |
| 214 | #if defined(SYSCALL_DEBUG) || defined(KTRACE) || NDT > 0 |
| 215 | 	int code = (p->p_flag & P_THREAD) ? SYS___tfork : |
| 216 | 	 (p->p_p->ps_flags & PS_PPWAIT) ? SYS_vfork : SYS_fork; |
| 217 | 	const register_t child_retval[2] = { 0, 1 }; |
| 218 | #endif |
| 219 | |
| 220 | 	TRACEPOINT(sched, on__cpu, NULL); |
| 221 | |
| 222 | #ifdef SYSCALL_DEBUG |
| 223 | 	KERNEL_LOCK(); |
| 224 | 	scdebug_ret(p, code, 0, child_retval); |
| 225 | 	KERNEL_UNLOCK(); |
| 226 | #endif |
| 227 | #if NDT > 0 |
| 228 | 	DT_LEAVE(syscall, code, 0, child_retval[0], child_retval[1]); |
| 229 | #endif |
| 230 | 	TRACEPOINT(raw_syscalls, sys_exit, code, NULL); |
| 231 | |
| 232 | 	userret(p); |
| 233 | |
| 234 | #ifdef KTRACE |
| 235 | 	if (KTRPOINT(p, KTR_SYSRET)) |
| 236 | 		ktrsysret(p, code, 0, child_retval); |
| 237 | #endif |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Do the specific processing necessary for an AST |
| 242 | */ |
| 243 | static inline void |
| 244 | mi_ast(struct proc *p, int resched) |
| 245 | { |
| 246 | 	if (p->p_flag & P_OWEUPC) { |
| 247 | 		KERNEL_LOCK(); |
| 248 | 		ADDUPROF(p); |
| 249 | 		KERNEL_UNLOCK(); |
| 250 | 	} |
| 251 | 	if (resched) |
| 252 | 		preempt(); |
| 253 | |
| 254 | 	/* |
| 255 | 	 * XXX could move call to userret() here, but |
| 256 | 	 * hppa calls ast() in syscall return and sh calls |
| 257 | 	 * it after userret() |
| 258 | 	 */ |
| 259 | } |