1277166Sjhibbits/*-
2277166Sjhibbits * Copyright (c) 2014 Justin Hibbits
3277166Sjhibbits * All rights reserved.
4277166Sjhibbits *
5277166Sjhibbits * Redistribution and use in source and binary forms, with or without
6277166Sjhibbits * modification, are permitted provided that the following conditions
7277166Sjhibbits * are met:
8277166Sjhibbits * 1. Redistributions of source code must retain the above copyright
9277166Sjhibbits *    notice, this list of conditions and the following disclaimer.
10277166Sjhibbits * 2. Redistributions in binary form must reproduce the above copyright
11277166Sjhibbits *    notice, this list of conditions and the following disclaimer in the
12277166Sjhibbits *    documentation and/or other materials provided with the distribution.
13277166Sjhibbits *
14277166Sjhibbits * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15277166Sjhibbits * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16277166Sjhibbits * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17277166Sjhibbits * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18277166Sjhibbits * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19277166Sjhibbits * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20277166Sjhibbits * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21277166Sjhibbits * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22277166Sjhibbits * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23277166Sjhibbits * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24277166Sjhibbits * SUCH DAMAGE.
25277166Sjhibbits *
26277166Sjhibbits */
27277166Sjhibbits
28277166Sjhibbits#include <sys/cdefs.h>
29277166Sjhibbits__FBSDID("$FreeBSD$");
30277166Sjhibbits
31277166Sjhibbits#include "opt_compat.h"
32277166Sjhibbits
33277166Sjhibbits#include <sys/param.h>
34277166Sjhibbits#include <sys/systm.h>
35277166Sjhibbits#include <sys/malloc.h>
36277166Sjhibbits#include <sys/proc.h>
37277166Sjhibbits#include <sys/ptrace.h>
38277166Sjhibbits#include <sys/sysent.h>
39277166Sjhibbits#include <machine/altivec.h>
40277166Sjhibbits#include <machine/cpu.h>
41277166Sjhibbits#include <machine/md_var.h>
42277166Sjhibbits#include <machine/pcb.h>
43277166Sjhibbits
44277166Sjhibbitsint
45277166Sjhibbitscpu_ptrace(struct thread *td, int req, void *addr, int data)
46277166Sjhibbits{
47277166Sjhibbits	int error;
48277166Sjhibbits	struct pcb *pcb;
49277166Sjhibbits	struct vec vec;
50277166Sjhibbits
51277166Sjhibbits	pcb = td->td_pcb;
52277166Sjhibbits
53277166Sjhibbits	bzero(&vec, sizeof(vec));
54277166Sjhibbits
55277166Sjhibbits	error = EINVAL;
56277166Sjhibbits	switch (req) {
57277166Sjhibbits	case PT_GETVRREGS:
58277166Sjhibbits		if (!(cpu_features & PPC_FEATURE_HAS_ALTIVEC))
59277166Sjhibbits			break;
60277166Sjhibbits
61277166Sjhibbits		if (pcb->pcb_flags & PCB_VEC) {
62277166Sjhibbits			save_vec_nodrop(td);
63277166Sjhibbits			memcpy(&vec, &pcb->pcb_vec, sizeof(vec));
64277166Sjhibbits		}
65277166Sjhibbits		error = copyout(&vec, addr, sizeof(vec));
66277166Sjhibbits		break;
67277166Sjhibbits	case PT_SETVRREGS:
68277166Sjhibbits		if (!(cpu_features & PPC_FEATURE_HAS_ALTIVEC))
69277166Sjhibbits			break;
70277166Sjhibbits		error = copyin(addr, &vec, sizeof(vec));
71277166Sjhibbits		if (error == 0) {
72277166Sjhibbits			pcb->pcb_flags |= PCB_VEC;
73277166Sjhibbits			memcpy(&pcb->pcb_vec, &vec, sizeof(vec));
74277166Sjhibbits		}
75277166Sjhibbits		break;
76277166Sjhibbits
77277166Sjhibbits	default:
78277166Sjhibbits		break;
79277166Sjhibbits	}
80277166Sjhibbits
81277166Sjhibbits	return (error);
82277166Sjhibbits}
83