xref: /freebsd/sys/fs/procfs/procfs_fpregs.c (revision 29363fb4)
1a76d60f0SBruce Evans /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1993 Jan-Simon Pendry
5df8bae1dSRodney W. Grimes  * Copyright (c) 1993
6df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
9df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
10df8bae1dSRodney W. Grimes  *
11df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
12df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
13df8bae1dSRodney W. Grimes  * are met:
14df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
16df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
18df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
20df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
21df8bae1dSRodney W. Grimes  *    without specific prior written permission.
22df8bae1dSRodney W. Grimes  *
23df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
34df8bae1dSRodney W. Grimes  *
35996c772fSJohn Dyson  * From:
36a76d60f0SBruce Evans  *	$Id: procfs_regs.c,v 3.2 1993/12/15 09:40:17 jsp Exp $
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40fb919e4dSMark Murray #include <sys/systm.h>
41fb919e4dSMark Murray #include <sys/lock.h>
42fb919e4dSMark Murray #include <sys/mutex.h>
43df8bae1dSRodney W. Grimes #include <sys/proc.h>
443da32491SDag-Erling Smørgrav #include <sys/ptrace.h>
453a669c52SDag-Erling Smørgrav #include <sys/uio.h>
46fb919e4dSMark Murray 
473a669c52SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs.h>
4899d300a1SRuslan Ermilov #include <fs/procfs/procfs.h>
49fb919e4dSMark Murray 
50841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
5131d1b816SDmitry Chagin #include <sys/sysent.h>
5262919d78SPeter Wemm #include <sys/procfs.h>
5362919d78SPeter Wemm #include <machine/fpu.h>
5462919d78SPeter Wemm 
5562919d78SPeter Wemm /*
5662919d78SPeter Wemm  * PROC(write, fpregs, td2, &r) becomes
5762919d78SPeter Wemm  * proc_write_fpregs(td2, &r)   or
5862919d78SPeter Wemm  * proc_write_fpregs32(td2, &r32)
5962919d78SPeter Wemm  *
6062919d78SPeter Wemm  * UIOMOVE_FROMBUF(r, uio) becomes
6162919d78SPeter Wemm  * uiomove_frombuf(&r, sizeof(r), uio)  or
6262919d78SPeter Wemm  * uiomove_frombuf(&r32, sizeof(r32), uio)
6362919d78SPeter Wemm  */
6462919d78SPeter Wemm #define	PROC(d, w, t, r)	wrap32 ? \
6562919d78SPeter Wemm 	proc_ ## d ## _ ## w ## 32(t, r ## 32) : \
6662919d78SPeter Wemm 	proc_ ## d ## _ ## w(t, r)
6762919d78SPeter Wemm #define	UIOMOVE_FROMBUF(k, u)	wrap32 ? \
6862919d78SPeter Wemm 	uiomove_frombuf(& k ## 32, sizeof(k ## 32), u) : \
6962919d78SPeter Wemm 	uiomove_frombuf(& k, sizeof(k), u)
7062919d78SPeter Wemm #else
7162919d78SPeter Wemm #define	PROC(d, w, t, r)	proc_ ## d ## _ ## w(t, r)
7262919d78SPeter Wemm #define	UIOMOVE_FROMBUF(k, u)	uiomove_frombuf(& k, sizeof(k), u)
7362919d78SPeter Wemm #endif
7462919d78SPeter Wemm 
75df8bae1dSRodney W. Grimes int
procfs_doprocfpregs(PFS_FILL_ARGS)763a669c52SDag-Erling Smørgrav procfs_doprocfpregs(PFS_FILL_ARGS)
77df8bae1dSRodney W. Grimes {
78df8bae1dSRodney W. Grimes 	int error;
79df8bae1dSRodney W. Grimes 	struct fpreg r;
8062919d78SPeter Wemm 	struct thread *td2;
81841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
8262919d78SPeter Wemm 	struct fpreg32 r32;
8362919d78SPeter Wemm 	int wrap32 = 0;
8462919d78SPeter Wemm #endif
85df8bae1dSRodney W. Grimes 
86302762c3SDag-Erling Smørgrav 	if (uio->uio_offset != 0)
87302762c3SDag-Erling Smørgrav 		return (0);
88302762c3SDag-Erling Smørgrav 
89ff7299d9SJohn Baldwin 	PROC_LOCK(p);
90d2871337SMark Johnston 	PROC_ASSERT_HELD(p);
91f44d9e24SJohn Baldwin 	if (p_candebug(td, p)) {
92ff7299d9SJohn Baldwin 		PROC_UNLOCK(p);
93a76d60f0SBruce Evans 		return (EPERM);
94ff7299d9SJohn Baldwin 	}
95847e02e9SKonstantin Belousov 	if (!P_SHOULDSTOP(p)) {
96847e02e9SKonstantin Belousov 		PROC_UNLOCK(p);
97847e02e9SKonstantin Belousov 		return (EBUSY);
98847e02e9SKonstantin Belousov 	}
99df8bae1dSRodney W. Grimes 
10062919d78SPeter Wemm 	td2 = FIRST_THREAD_IN_PROC(p);
101841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
1022883703eSKonstantin Belousov 	if (SV_CURPROC_FLAG(SV_ILP32)) {
103a5c1afadSDmitry Chagin 		if (SV_PROC_FLAG(td2->td_proc, SV_ILP32) == 0) {
10462919d78SPeter Wemm 			PROC_UNLOCK(p);
10562919d78SPeter Wemm 			return (EINVAL);
10662919d78SPeter Wemm 		}
10762919d78SPeter Wemm 		wrap32 = 1;
108352aaa51SMark Johnston 		memset(&r32, 0, sizeof(r32));
109352aaa51SMark Johnston 	} else
11062919d78SPeter Wemm #endif
111352aaa51SMark Johnston 		memset(&r, 0, sizeof(r));
11262919d78SPeter Wemm 	error = PROC(read, fpregs, td2, &r);
113587ffa45SRobert Watson 	if (error == 0) {
114587ffa45SRobert Watson 		PROC_UNLOCK(p);
11562919d78SPeter Wemm 		error = UIOMOVE_FROMBUF(r, uio);
116587ffa45SRobert Watson 		PROC_LOCK(p);
117587ffa45SRobert Watson 	}
118df8bae1dSRodney W. Grimes 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
119e602ba25SJulian Elischer 		if (!P_SHOULDSTOP(p))
120df8bae1dSRodney W. Grimes 			error = EBUSY;
121df8bae1dSRodney W. Grimes 		else
122a76d60f0SBruce Evans 			/* XXXKSE: */
12362919d78SPeter Wemm 			error = PROC(write, fpregs, td2, &r);
124df8bae1dSRodney W. Grimes 	}
125ff7299d9SJohn Baldwin 	PROC_UNLOCK(p);
126df8bae1dSRodney W. Grimes 
127df8bae1dSRodney W. Grimes 	return (error);
128df8bae1dSRodney W. Grimes }
129