procfs_regs.c revision 168758
1169689Skan/*-
2169689Skan * Copyright (c) 1993 Jan-Simon Pendry
3169689Skan * Copyright (c) 1993
4169689Skan *	The Regents of the University of California.  All rights reserved.
5169689Skan *
6169689Skan * This code is derived from software contributed to Berkeley by
7169689Skan * Jan-Simon Pendry.
8169689Skan *
9169689Skan * Redistribution and use in source and binary forms, with or without
10169689Skan * modification, are permitted provided that the following conditions
11169689Skan * are met:
12169689Skan * 1. Redistributions of source code must retain the above copyright
13169689Skan *    notice, this list of conditions and the following disclaimer.
14169689Skan * 2. Redistributions in binary form must reproduce the above copyright
15169689Skan *    notice, this list of conditions and the following disclaimer in the
16169689Skan *    documentation and/or other materials provided with the distribution.
17169689Skan * 4. Neither the name of the University nor the names of its contributors
18169689Skan *    may be used to endorse or promote products derived from this software
19169689Skan *    without specific prior written permission.
20169689Skan *
21169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31169689Skan * SUCH DAMAGE.
32169689Skan *
33169689Skan *	@(#)procfs_regs.c	8.4 (Berkeley) 6/15/94
34169689Skan *
35169689Skan * From:
36169689Skan *	$Id: procfs_regs.c,v 3.2 1993/12/15 09:40:17 jsp Exp $
37169689Skan * $FreeBSD: head/sys/fs/procfs/procfs_regs.c 168758 2007-04-15 13:24:03Z des $
38169689Skan */
39169689Skan
40169689Skan#include "opt_compat.h"
41169689Skan
42169689Skan#include <sys/param.h>
43169689Skan#include <sys/systm.h>
44169689Skan#include <sys/lock.h>
45169689Skan#include <sys/mutex.h>
46169689Skan#include <sys/proc.h>
47169689Skan#include <sys/ptrace.h>
48169689Skan#include <sys/uio.h>
49169689Skan
50169689Skan#include <machine/reg.h>
51169689Skan
52169689Skan#include <fs/pseudofs/pseudofs.h>
53169689Skan#include <fs/procfs/procfs.h>
54169689Skan
55169689Skan#ifdef COMPAT_IA32
56169689Skan#include <sys/procfs.h>
57169689Skan#include <machine/fpu.h>
58169689Skan#include <compat/ia32/ia32_reg.h>
59169689Skan
60169689Skanextern struct sysentvec ia32_freebsd_sysvec;
61169689Skan/*
62169689Skan * PROC(write, regs, td2, &r) becomes
63169689Skan * proc_write_regs(td2, &r)   or
64169689Skan * proc_write_regs32(td2, &r32)
65169689Skan *
66169689Skan * UIOMOVE_FROMBUF(r, uio) becomes
67169689Skan * uiomove_frombuf(&r, sizeof(r), uio)  or
68169689Skan * uiomove_frombuf(&r32, sizeof(r32), uio)
69169689Skan */
70169689Skan#define	PROC(d, w, t, r)	wrap32 ? \
71169689Skan	proc_ ## d ## _ ## w ## 32(t, r ## 32) : \
72169689Skan	proc_ ## d ## _ ## w(t, r)
73169689Skan#define	UIOMOVE_FROMBUF(k, u)	wrap32 ? \
74169689Skan	uiomove_frombuf(& k ## 32, sizeof(k ## 32), u) : \
75169689Skan	uiomove_frombuf(& k, sizeof(k), u)
76169689Skan#else
77169689Skan#define	PROC(d, w, t, r)	proc_ ## d ## _ ## w(t, r)
78169689Skan#define	UIOMOVE_FROMBUF(k, u)	uiomove_frombuf(& k, sizeof(k), u)
79169689Skan#endif
80169689Skan
81169689Skanint
82169689Skanprocfs_doprocregs(PFS_FILL_ARGS)
83169689Skan{
84169689Skan	int error;
85169689Skan	struct reg r;
86169689Skan	struct thread *td2;
87169689Skan#ifdef COMPAT_IA32
88169689Skan	struct reg32 r32;
89169689Skan	int wrap32 = 0;
90169689Skan#endif
91169689Skan
92169689Skan	if (uio->uio_offset != 0)
93169689Skan		return (0);
94169689Skan
95169689Skan	PROC_LOCK(p);
96169689Skan	PROC_ASSERT_HELD(p);
97169689Skan	if (p_candebug(td, p)) {
98169689Skan		PROC_UNLOCK(p);
99169689Skan		return (EPERM);
100169689Skan	}
101169689Skan
102169689Skan	/* XXXKSE: */
103169689Skan	td2 = FIRST_THREAD_IN_PROC(p);
104169689Skan#ifdef COMPAT_IA32
105169689Skan	if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) {
106169689Skan		if (td2->td_proc->p_sysent != &ia32_freebsd_sysvec) {
107169689Skan			PROC_UNLOCK(p);
108169689Skan			return (EINVAL);
109169689Skan		}
110169689Skan		wrap32 = 1;
111169689Skan	}
112169689Skan#endif
113169689Skan	error = PROC(read, regs, td2, &r);
114169689Skan	if (error == 0) {
115169689Skan		PROC_UNLOCK(p);
116169689Skan		error = UIOMOVE_FROMBUF(r, uio);
117169689Skan		PROC_LOCK(p);
118169689Skan	}
119169689Skan	if (error == 0 && uio->uio_rw == UIO_WRITE) {
120169689Skan		if (!P_SHOULDSTOP(p))
121169689Skan			error = EBUSY;
122169689Skan		else
123169689Skan			/* XXXKSE: */
124169689Skan			error = PROC(write, regs, td2, &r);
125169689Skan	}
126169689Skan	PROC_UNLOCK(p);
127169689Skan
128169689Skan	return (error);
129169689Skan}
130169689Skan