1/* $NetBSD: process_machdep.c,v 1.29 2014/02/15 22:20:41 dsl Exp $ */
2
3/*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * This file may seem a bit stylized, but that so that it's easier to port.
34 * Functions to be implemented here are:
35 *
36 * process_read_regs(proc, regs)
37 * Get the current user-visible register set from the process
38 * and copy it into the regs structure (<machine/reg.h>).
39 * The process is stopped at the time read_regs is called.
40 *
41 * process_write_regs(proc, regs)
42 * Update the current register set from the passed in regs
43 * structure. Take care to avoid clobbering special CPU
44 * registers or privileged bits in the PSL.
45 * The process is stopped at the time write_regs is called.
46 *
47 * process_sstep(proc)
48 * Arrange for the process to trap after executing a single instruction.
49 *
50 * process_set_pc(proc)
51 * Set the process's program counter.
52 */
53
54
55#include <sys/cdefs.h>
56__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.29 2014/02/15 22:20:41 dsl Exp $");
57
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <sys/time.h>
61#include <sys/kernel.h>
62#include <sys/proc.h>
63#include <sys/vnode.h>
64#include <sys/ptrace.h>
65
66#include <machine/psl.h>
67#include <machine/reg.h>
68#include <machine/segments.h>
69#include <x86/fpu.h>
70
71static inline struct trapframe *process_frame(struct lwp *);
72#if 0
73static inline int verr_gdt(struct pmap *, int sel);
74static inline int verr_ldt(struct pmap *, int sel);
75#endif
76
77static inline struct trapframe *
78process_frame(struct lwp *l)
79{
80
81 return (l->l_md.md_regs);
82}
83
84int
85process_read_regs(struct lwp *l, struct reg *regs)
86{
87 struct trapframe *tf = process_frame(l);
88
89#define copy_to_reg(reg, REG, idx) regs->regs[_REG_##REG] = tf->tf_##reg;
90 _FRAME_GREG(copy_to_reg)
91#undef copy_to_reg
92
93 return (0);
94}
95
96int
97process_read_fpregs(struct lwp *l, struct fpreg *regs, size_t *sz)
98{
99
100 process_read_fpregs_xmm(l, &regs->fxstate);
101
102 return 0;
103}
104
105int
106process_write_regs(struct lwp *l, const struct reg *regp)
107{
108 struct trapframe *tf = process_frame(l);
109 int error;
110 const long *regs = regp->regs;
111
112 /*
113 * Check for security violations.
114 * Note that struct regs is compatible with
115 * the __gregs array in mcontext_t.
116 */
117 error = cpu_mcontext_validate(l, (const mcontext_t *)regs);
118 if (error != 0)
119 return error;
120
121#define copy_to_frame(reg, REG, idx) tf->tf_##reg = regs[_REG_##REG];
122 _FRAME_GREG(copy_to_frame)
123#undef copy_to_frame
124
125 return (0);
126}
127
128int
129process_write_fpregs(struct lwp *l, const struct fpreg *regs, size_t sz)
130{
131
132 process_write_fpregs_xmm(l, &regs->fxstate);
133 return 0;
134}
135
136int
137process_sstep(struct lwp *l, int sstep)
138{
139 struct trapframe *tf = process_frame(l);
140
141 if (sstep)
142 tf->tf_rflags |= PSL_T;
143 else
144 tf->tf_rflags &= ~PSL_T;
145
146 return (0);
147}
148
149int
150process_set_pc(struct lwp *l, void *addr)
151{
152 struct trapframe *tf = process_frame(l);
153
154 if ((uint64_t)addr > VM_MAXUSER_ADDRESS)
155 return EINVAL;
156 tf->tf_rip = (uint64_t)addr;
157
158 return (0);
159}
160