procfs_fpregs.c revision 147692
190716Sbde/*-
21541Srgrimes * Copyright (c) 1993 Jan-Simon Pendry
31541Srgrimes * Copyright (c) 1993
41541Srgrimes *	The Regents of the University of California.  All rights reserved.
51541Srgrimes *
61541Srgrimes * This code is derived from software contributed to Berkeley by
71541Srgrimes * Jan-Simon Pendry.
81541Srgrimes *
91541Srgrimes * Redistribution and use in source and binary forms, with or without
101541Srgrimes * modification, are permitted provided that the following conditions
111541Srgrimes * are met:
121541Srgrimes * 1. Redistributions of source code must retain the above copyright
131541Srgrimes *    notice, this list of conditions and the following disclaimer.
141541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer in the
161541Srgrimes *    documentation and/or other materials provided with the distribution.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
3322521Sdyson *	@(#)procfs_fpregs.c	8.2 (Berkeley) 6/15/94
341541Srgrimes *
3522521Sdyson * From:
3690716Sbde *	$Id: procfs_regs.c,v 3.2 1993/12/15 09:40:17 jsp Exp $
3750477Speter * $FreeBSD: head/sys/fs/procfs/procfs_fpregs.c 147692 2005-06-30 07:49:22Z peter $
381541Srgrimes */
391541Srgrimes
40147692Speter#include "opt_compat.h"
41147692Speter
421541Srgrimes#include <sys/param.h>
4376166Smarkm#include <sys/systm.h>
4476166Smarkm#include <sys/lock.h>
4576166Smarkm#include <sys/mutex.h>
461541Srgrimes#include <sys/proc.h>
4784637Sdes#include <sys/ptrace.h>
4887321Sdes#include <sys/uio.h>
4976166Smarkm
501541Srgrimes#include <machine/reg.h>
5176166Smarkm
5287321Sdes#include <fs/pseudofs/pseudofs.h>
5377031Sru#include <fs/procfs/procfs.h>
5476166Smarkm
55147692Speter#ifdef COMPAT_IA32
56147692Speter#include <sys/procfs.h>
57147692Speter#include <machine/fpu.h>
58147692Speter#include <compat/ia32/ia32_reg.h>
59147692Speter
60147692Speterextern struct sysentvec ia32_freebsd_sysvec;
61147692Speter/*
62147692Speter * PROC(write, fpregs, td2, &r) becomes
63147692Speter * proc_write_fpregs(td2, &r)   or
64147692Speter * proc_write_fpregs32(td2, &r32)
65147692Speter *
66147692Speter * UIOMOVE_FROMBUF(r, uio) becomes
67147692Speter * uiomove_frombuf(&r, sizeof(r), uio)  or
68147692Speter * uiomove_frombuf(&r32, sizeof(r32), uio)
69147692Speter */
70147692Speter#define	PROC(d, w, t, r)	wrap32 ? \
71147692Speter	proc_ ## d ## _ ## w ## 32(t, r ## 32) : \
72147692Speter	proc_ ## d ## _ ## w(t, r)
73147692Speter#define	UIOMOVE_FROMBUF(k, u)	wrap32 ? \
74147692Speter	uiomove_frombuf(& k ## 32, sizeof(k ## 32), u) : \
75147692Speter	uiomove_frombuf(& k, sizeof(k), u)
76147692Speter#else
77147692Speter#define	PROC(d, w, t, r)	proc_ ## d ## _ ## w(t, r)
78147692Speter#define	UIOMOVE_FROMBUF(k, u)	uiomove_frombuf(& k, sizeof(k), u)
79147692Speter#endif
80147692Speter
811541Srgrimesint
8287321Sdesprocfs_doprocfpregs(PFS_FILL_ARGS)
831541Srgrimes{
841541Srgrimes	int error;
851541Srgrimes	struct fpreg r;
86147692Speter	struct thread *td2;
87147692Speter#ifdef COMPAT_IA32
88147692Speter	struct fpreg32 r32;
89147692Speter	int wrap32 = 0;
90147692Speter#endif
911541Srgrimes
9294622Sjhb	PROC_LOCK(p);
93136004Sdas	KASSERT(p->p_lock > 0, ("proc not held"));
9496886Sjhb	if (p_candebug(td, p)) {
9594622Sjhb		PROC_UNLOCK(p);
9690716Sbde		return (EPERM);
9794622Sjhb	}
981541Srgrimes
99120665Snectar	/* XXXKSE: */
100147692Speter	td2 = FIRST_THREAD_IN_PROC(p);
101147692Speter#ifdef COMPAT_IA32
102147692Speter	if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) {
103147692Speter		if (td2->td_proc->p_sysent != &ia32_freebsd_sysvec) {
104147692Speter			PROC_UNLOCK(p);
105147692Speter			return (EINVAL);
106147692Speter		}
107147692Speter		wrap32 = 1;
108147692Speter	}
109147692Speter#endif
110147692Speter	error = PROC(read, fpregs, td2, &r);
111114734Srwatson	if (error == 0) {
112114734Srwatson		PROC_UNLOCK(p);
113147692Speter		error = UIOMOVE_FROMBUF(r, uio);
114114734Srwatson		PROC_LOCK(p);
115114734Srwatson	}
1161541Srgrimes	if (error == 0 && uio->uio_rw == UIO_WRITE) {
11799072Sjulian		if (!P_SHOULDSTOP(p))
1181541Srgrimes			error = EBUSY;
1191541Srgrimes		else
12090716Sbde			/* XXXKSE: */
121147692Speter			error = PROC(write, fpregs, td2, &r);
1221541Srgrimes	}
12394622Sjhb	PROC_UNLOCK(p);
1241541Srgrimes
1251541Srgrimes	uio->uio_offset = 0;
1261541Srgrimes	return (error);
1271541Srgrimes}
128