xref: /freebsd/sys/fs/procfs/procfs_fpregs.c (revision 2883703e00a4a50e97f2ee18676d8cef1687f032)
1a76d60f0SBruce Evans /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1993 Jan-Simon Pendry
3df8bae1dSRodney W. Grimes  * Copyright (c) 1993
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
7df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33996c772fSJohn Dyson  *	@(#)procfs_fpregs.c	8.2 (Berkeley) 6/15/94
34df8bae1dSRodney W. Grimes  *
35996c772fSJohn Dyson  * From:
36a76d60f0SBruce Evans  *	$Id: procfs_regs.c,v 3.2 1993/12/15 09:40:17 jsp Exp $
37c3aac50fSPeter Wemm  * $FreeBSD$
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
4062919d78SPeter Wemm #include "opt_compat.h"
4162919d78SPeter Wemm 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43fb919e4dSMark Murray #include <sys/systm.h>
44fb919e4dSMark Murray #include <sys/lock.h>
45fb919e4dSMark Murray #include <sys/mutex.h>
46df8bae1dSRodney W. Grimes #include <sys/proc.h>
473da32491SDag-Erling Smørgrav #include <sys/ptrace.h>
482883703eSKonstantin Belousov #include <sys/sysent.h>
493a669c52SDag-Erling Smørgrav #include <sys/uio.h>
50fb919e4dSMark Murray 
51df8bae1dSRodney W. Grimes #include <machine/reg.h>
52fb919e4dSMark Murray 
533a669c52SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs.h>
5499d300a1SRuslan Ermilov #include <fs/procfs/procfs.h>
55fb919e4dSMark Murray 
5662919d78SPeter Wemm #ifdef COMPAT_IA32
5762919d78SPeter Wemm #include <sys/procfs.h>
5862919d78SPeter Wemm #include <machine/fpu.h>
5962919d78SPeter Wemm #include <compat/ia32/ia32_reg.h>
6062919d78SPeter Wemm 
6162919d78SPeter Wemm /*
6262919d78SPeter Wemm  * PROC(write, fpregs, td2, &r) becomes
6362919d78SPeter Wemm  * proc_write_fpregs(td2, &r)   or
6462919d78SPeter Wemm  * proc_write_fpregs32(td2, &r32)
6562919d78SPeter Wemm  *
6662919d78SPeter Wemm  * UIOMOVE_FROMBUF(r, uio) becomes
6762919d78SPeter Wemm  * uiomove_frombuf(&r, sizeof(r), uio)  or
6862919d78SPeter Wemm  * uiomove_frombuf(&r32, sizeof(r32), uio)
6962919d78SPeter Wemm  */
7062919d78SPeter Wemm #define	PROC(d, w, t, r)	wrap32 ? \
7162919d78SPeter Wemm 	proc_ ## d ## _ ## w ## 32(t, r ## 32) : \
7262919d78SPeter Wemm 	proc_ ## d ## _ ## w(t, r)
7362919d78SPeter Wemm #define	UIOMOVE_FROMBUF(k, u)	wrap32 ? \
7462919d78SPeter Wemm 	uiomove_frombuf(& k ## 32, sizeof(k ## 32), u) : \
7562919d78SPeter Wemm 	uiomove_frombuf(& k, sizeof(k), u)
7662919d78SPeter Wemm #else
7762919d78SPeter Wemm #define	PROC(d, w, t, r)	proc_ ## d ## _ ## w(t, r)
7862919d78SPeter Wemm #define	UIOMOVE_FROMBUF(k, u)	uiomove_frombuf(& k, sizeof(k), u)
7962919d78SPeter Wemm #endif
8062919d78SPeter Wemm 
81df8bae1dSRodney W. Grimes int
823a669c52SDag-Erling Smørgrav procfs_doprocfpregs(PFS_FILL_ARGS)
83df8bae1dSRodney W. Grimes {
84df8bae1dSRodney W. Grimes 	int error;
85df8bae1dSRodney W. Grimes 	struct fpreg r;
8662919d78SPeter Wemm 	struct thread *td2;
8762919d78SPeter Wemm #ifdef COMPAT_IA32
8862919d78SPeter Wemm 	struct fpreg32 r32;
8962919d78SPeter Wemm 	int wrap32 = 0;
9062919d78SPeter Wemm #endif
91df8bae1dSRodney W. Grimes 
92302762c3SDag-Erling Smørgrav 	if (uio->uio_offset != 0)
93302762c3SDag-Erling Smørgrav 		return (0);
94302762c3SDag-Erling Smørgrav 
95ff7299d9SJohn Baldwin 	PROC_LOCK(p);
96616b5f90SDavid Schultz 	KASSERT(p->p_lock > 0, ("proc not held"));
97f44d9e24SJohn Baldwin 	if (p_candebug(td, p)) {
98ff7299d9SJohn Baldwin 		PROC_UNLOCK(p);
99a76d60f0SBruce Evans 		return (EPERM);
100ff7299d9SJohn Baldwin 	}
101df8bae1dSRodney W. Grimes 
102a76d60f0SBruce Evans 	/* XXXKSE: */
10362919d78SPeter Wemm 	td2 = FIRST_THREAD_IN_PROC(p);
10462919d78SPeter Wemm #ifdef COMPAT_IA32
1052883703eSKonstantin Belousov 	if (SV_CURPROC_FLAG(SV_ILP32)) {
1062883703eSKonstantin Belousov 		if ((td2->td_proc->p_sysent->sv_flags & SV_ILP32) == 0) {
10762919d78SPeter Wemm 			PROC_UNLOCK(p);
10862919d78SPeter Wemm 			return (EINVAL);
10962919d78SPeter Wemm 		}
11062919d78SPeter Wemm 		wrap32 = 1;
11162919d78SPeter Wemm 	}
11262919d78SPeter Wemm #endif
11362919d78SPeter Wemm 	error = PROC(read, fpregs, td2, &r);
114587ffa45SRobert Watson 	if (error == 0) {
115587ffa45SRobert Watson 		PROC_UNLOCK(p);
11662919d78SPeter Wemm 		error = UIOMOVE_FROMBUF(r, uio);
117587ffa45SRobert Watson 		PROC_LOCK(p);
118587ffa45SRobert Watson 	}
119df8bae1dSRodney W. Grimes 	if (error == 0 && uio->uio_rw == UIO_WRITE) {
120e602ba25SJulian Elischer 		if (!P_SHOULDSTOP(p))
121df8bae1dSRodney W. Grimes 			error = EBUSY;
122df8bae1dSRodney W. Grimes 		else
123a76d60f0SBruce Evans 			/* XXXKSE: */
12462919d78SPeter Wemm 			error = PROC(write, fpregs, td2, &r);
125df8bae1dSRodney W. Grimes 	}
126ff7299d9SJohn Baldwin 	PROC_UNLOCK(p);
127df8bae1dSRodney W. Grimes 
128df8bae1dSRodney W. Grimes 	return (error);
129df8bae1dSRodney W. Grimes }
130