1331722Seadler/*
2132950Sdavidxu * Copyright (c) 2004 David Xu <davidxu@freebsd.org>
3132332Smarcel * Copyright (c) 2004 Marcel Moolenaar
4132332Smarcel * All rights reserved.
5132332Smarcel *
6132332Smarcel * Redistribution and use in source and binary forms, with or without
7132332Smarcel * modification, are permitted provided that the following conditions
8132332Smarcel * are met:
9132332Smarcel *
10132332Smarcel * 1. Redistributions of source code must retain the above copyright
11132332Smarcel *    notice, this list of conditions and the following disclaimer.
12132332Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13132332Smarcel *    notice, this list of conditions and the following disclaimer in the
14132332Smarcel *    documentation and/or other materials provided with the distribution.
15132332Smarcel *
16132332Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17132332Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18132332Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19132332Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20132332Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21132332Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22132332Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23132332Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24132332Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25132332Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26132332Smarcel */
27132332Smarcel
28132332Smarcel#include <sys/cdefs.h>
29132332Smarcel__FBSDID("$FreeBSD$");
30132332Smarcel
31132360Smarcel#include <sys/procfs.h>
32169985Sdelphij#include <string.h>
33181059Smarcel#include <thread_db.h>
34132360Smarcel#include <ucontext.h>
35132360Smarcel
36181059Smarcel#include "libpthread_db.h"
37181059Smarcel
38132332Smarcelvoid
39132332Smarcelpt_reg_to_ucontext(const struct reg *r, ucontext_t *uc)
40132332Smarcel{
41132950Sdavidxu	mcontext_t *mc = &uc->uc_mcontext;
42132950Sdavidxu
43132950Sdavidxu	mc->mc_rdi = r->r_rdi;
44132950Sdavidxu	mc->mc_rsi = r->r_rsi;
45132950Sdavidxu	mc->mc_rdx = r->r_rdx;
46132950Sdavidxu	mc->mc_rcx = r->r_rcx;
47132950Sdavidxu	mc->mc_r8 = r->r_r8;
48132950Sdavidxu	mc->mc_r9 = r->r_r9;
49132950Sdavidxu	mc->mc_rax = r->r_rax;
50132950Sdavidxu	mc->mc_rbx = r->r_rbx;
51132950Sdavidxu	mc->mc_rbp = r->r_rbp;
52132950Sdavidxu	mc->mc_r10 = r->r_r10;
53132950Sdavidxu	mc->mc_r11 = r->r_r11;
54132950Sdavidxu	mc->mc_r12 = r->r_r12;
55132950Sdavidxu	mc->mc_r13 = r->r_r13;
56132950Sdavidxu	mc->mc_r14 = r->r_r14;
57132950Sdavidxu	mc->mc_r15 = r->r_r15;
58132950Sdavidxu	mc->mc_rip = r->r_rip;
59132950Sdavidxu	mc->mc_cs = r->r_cs;
60132950Sdavidxu	mc->mc_rflags = r->r_rflags;
61132950Sdavidxu	mc->mc_rsp = r->r_rsp;
62132950Sdavidxu	mc->mc_ss = r->r_ss;
63132332Smarcel}
64132332Smarcel
65132332Smarcelvoid
66132332Smarcelpt_ucontext_to_reg(const ucontext_t *uc, struct reg *r)
67132332Smarcel{
68132950Sdavidxu	const mcontext_t *mc = &uc->uc_mcontext;
69132950Sdavidxu
70132950Sdavidxu	r->r_rdi = mc->mc_rdi;
71132950Sdavidxu	r->r_rsi = mc->mc_rsi;
72132950Sdavidxu	r->r_rdx = mc->mc_rdx;
73132950Sdavidxu	r->r_rcx = mc->mc_rcx;
74132950Sdavidxu	r->r_r8 = mc->mc_r8;
75132950Sdavidxu	r->r_r9 = mc->mc_r9;
76132950Sdavidxu	r->r_rax = mc->mc_rax;
77132950Sdavidxu	r->r_rbx = mc->mc_rbx;
78132950Sdavidxu	r->r_rbp = mc->mc_rbp;
79132950Sdavidxu	r->r_r10 = mc->mc_r10;
80132950Sdavidxu	r->r_r11 = mc->mc_r11;
81132950Sdavidxu	r->r_r12 = mc->mc_r12;
82132950Sdavidxu	r->r_r13 = mc->mc_r13;
83132950Sdavidxu	r->r_r14 = mc->mc_r14;
84132950Sdavidxu	r->r_r15 = mc->mc_r15;
85132950Sdavidxu	r->r_rip = mc->mc_rip;
86132950Sdavidxu	r->r_cs = mc->mc_cs;
87132950Sdavidxu	r->r_rflags = mc->mc_rflags;
88132950Sdavidxu	r->r_rsp = mc->mc_rsp;
89132950Sdavidxu	r->r_ss = mc->mc_ss;
90132332Smarcel}
91132332Smarcel
92132332Smarcelvoid
93132332Smarcelpt_fpreg_to_ucontext(const struct fpreg* r, ucontext_t *uc)
94132332Smarcel{
95224684Smarius
96224684Smarius	memcpy(&uc->uc_mcontext.mc_fpstate, r, sizeof(*r));
97132332Smarcel}
98132332Smarcel
99132332Smarcelvoid
100132332Smarcelpt_ucontext_to_fpreg(const ucontext_t *uc, struct fpreg *r)
101132332Smarcel{
102224684Smarius
103224684Smarius	memcpy(r, &uc->uc_mcontext.mc_fpstate, sizeof(*r));
104132332Smarcel}
105132332Smarcel
106132332Smarcelvoid
107132332Smarcelpt_md_init(void)
108132332Smarcel{
109224684Smarius
110132950Sdavidxu	/* Nothing to do */
111132332Smarcel}
112132332Smarcel
113132332Smarcelint
114132332Smarcelpt_reg_sstep(struct reg *reg, int step)
115132332Smarcel{
116132950Sdavidxu	register_t old;
117132950Sdavidxu
118132950Sdavidxu	old = reg->r_rflags;
119132950Sdavidxu	if (step)
120132950Sdavidxu		reg->r_rflags |= 0x0100;
121132950Sdavidxu	else
122132950Sdavidxu		reg->r_rflags &= ~0x0100;
123132950Sdavidxu	return (old != reg->r_rflags); /* changed ? */
124132332Smarcel}
125