procfs_fpregs.c revision 216120
178266Snik/*-
278266Snik * Copyright (c) 1993 Jan-Simon Pendry
378266Snik * Copyright (c) 1993
4174425Sdougb *	The Regents of the University of California.  All rights reserved.
5174425Sdougb *
6174425Sdougb * This code is derived from software contributed to Berkeley by
778266Snik * Jan-Simon Pendry.
878266Snik *
978266Snik * Redistribution and use in source and binary forms, with or without
1078266Snik * modification, are permitted provided that the following conditions
11174425Sdougb * are met:
12174425Sdougb * 1. Redistributions of source code must retain the above copyright
1378266Snik *    notice, this list of conditions and the following disclaimer.
14174425Sdougb * 2. Redistributions in binary form must reproduce the above copyright
15174425Sdougb *    notice, this list of conditions and the following disclaimer in the
16174425Sdougb *    documentation and/or other materials provided with the distribution.
1778266Snik * 4. Neither the name of the University nor the names of its contributors
18174425Sdougb *    may be used to endorse or promote products derived from this software
1978266Snik *    without specific prior written permission.
20174425Sdougb *
21174425Sdougb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2278266Snik * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23174425Sdougb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24174425Sdougb * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2578266Snik * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26174425Sdougb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27174425Sdougb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28174425Sdougb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2978266Snik * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30174425Sdougb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31174425Sdougb * SUCH DAMAGE.
32174425Sdougb *
33174425Sdougb *	@(#)procfs_fpregs.c	8.2 (Berkeley) 6/15/94
34174425Sdougb *
3578266Snik * From:
36174425Sdougb *	$Id: procfs_regs.c,v 3.2 1993/12/15 09:40:17 jsp Exp $
37174425Sdougb * $FreeBSD: head/sys/fs/procfs/procfs_fpregs.c 216120 2010-12-02 12:44:51Z kib $
3878266Snik */
39174425Sdougb
40174425Sdougb#include "opt_compat.h"
4178266Snik
42174425Sdougb#include <sys/param.h>
43174425Sdougb#include <sys/systm.h>
44174425Sdougb#include <sys/lock.h>
4578266Snik#include <sys/mutex.h>
46174425Sdougb#include <sys/proc.h>
47174425Sdougb#include <sys/ptrace.h>
4878266Snik#include <sys/sysent.h>
49174425Sdougb#include <sys/uio.h>
50174425Sdougb
51174425Sdougb#include <machine/reg.h>
5278266Snik
5378266Snik#include <fs/pseudofs/pseudofs.h>
5478266Snik#include <fs/procfs/procfs.h>
5578266Snik
5678266Snik#ifdef COMPAT_FREEBSD32
5778266Snik#include <sys/procfs.h>
5878266Snik#include <machine/fpu.h>
5978266Snik
6078266Snik/*
6178266Snik * PROC(write, fpregs, td2, &r) becomes
6278266Snik * proc_write_fpregs(td2, &r)   or
6378266Snik * proc_write_fpregs32(td2, &r32)
6478266Snik *
6578266Snik * UIOMOVE_FROMBUF(r, uio) becomes
6678266Snik * uiomove_frombuf(&r, sizeof(r), uio)  or
6778266Snik * uiomove_frombuf(&r32, sizeof(r32), uio)
6878266Snik */
69174425Sdougb#define	PROC(d, w, t, r)	wrap32 ? \
70174425Sdougb	proc_ ## d ## _ ## w ## 32(t, r ## 32) : \
71174425Sdougb	proc_ ## d ## _ ## w(t, r)
72174425Sdougb#define	UIOMOVE_FROMBUF(k, u)	wrap32 ? \
73174425Sdougb	uiomove_frombuf(& k ## 32, sizeof(k ## 32), u) : \
7478266Snik	uiomove_frombuf(& k, sizeof(k), u)
75174425Sdougb#else
7678266Snik#define	PROC(d, w, t, r)	proc_ ## d ## _ ## w(t, r)
77174425Sdougb#define	UIOMOVE_FROMBUF(k, u)	uiomove_frombuf(& k, sizeof(k), u)
78174425Sdougb#endif
79174425Sdougb
8078266Snikint
81174425Sdougbprocfs_doprocfpregs(PFS_FILL_ARGS)
8278266Snik{
83174425Sdougb	int error;
84174425Sdougb	struct fpreg r;
8578266Snik	struct thread *td2;
86174425Sdougb#ifdef COMPAT_FREEBSD32
8778266Snik	struct fpreg32 r32;
88174425Sdougb	int wrap32 = 0;
89174425Sdougb#endif
90174425Sdougb
9178266Snik	if (uio->uio_offset != 0)
92174425Sdougb		return (0);
93174425Sdougb
9478266Snik	PROC_LOCK(p);
95174425Sdougb	KASSERT(p->p_lock > 0, ("proc not held"));
96174425Sdougb	if (p_candebug(td, p)) {
9778266Snik		PROC_UNLOCK(p);
98174425Sdougb		return (EPERM);
99174425Sdougb	}
10078266Snik	if (!P_SHOULDSTOP(p)) {
101174425Sdougb		PROC_UNLOCK(p);
102174425Sdougb		return (EBUSY);
103174425Sdougb	}
104174425Sdougb
105174425Sdougb	/* XXXKSE: */
10678266Snik	td2 = FIRST_THREAD_IN_PROC(p);
10778266Snik#ifdef COMPAT_FREEBSD32
10878266Snik	if (SV_CURPROC_FLAG(SV_ILP32)) {
10978266Snik		if ((td2->td_proc->p_sysent->sv_flags & SV_ILP32) == 0) {
11078266Snik			PROC_UNLOCK(p);
111174425Sdougb			return (EINVAL);
112174425Sdougb		}
11378266Snik		wrap32 = 1;
114174425Sdougb	}
115174425Sdougb#endif
11678266Snik	error = PROC(read, fpregs, td2, &r);
117174425Sdougb	if (error == 0) {
118174425Sdougb		PROC_UNLOCK(p);
11978266Snik		error = UIOMOVE_FROMBUF(r, uio);
120174425Sdougb		PROC_LOCK(p);
121174425Sdougb	}
122174425Sdougb	if (error == 0 && uio->uio_rw == UIO_WRITE) {
123174425Sdougb		if (!P_SHOULDSTOP(p))
124174425Sdougb			error = EBUSY;
125174425Sdougb		else
126174425Sdougb			/* XXXKSE: */
12778266Snik			error = PROC(write, fpregs, td2, &r);
128174425Sdougb	}
12978266Snik	PROC_UNLOCK(p);
130174425Sdougb
131174425Sdougb	return (error);
13278266Snik}
133174425Sdougb