xref: /freebsd/lib/libthread_db/arch/amd64/libpthread_md.c (revision d876124d6ae9d56da5b4ff4c6015efd1d0c9222a)
1 /*
2  * Copyright (c) 2004 David Xu <davidxu@freebsd.org>
3  * Copyright (c) 2004 Marcel Moolenaar
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/procfs.h>
32 #include <string.h>
33 #include <ucontext.h>
34 
35 void
36 pt_reg_to_ucontext(const struct reg *r, ucontext_t *uc)
37 {
38 	mcontext_t *mc = &uc->uc_mcontext;
39 
40 	mc->mc_rdi = r->r_rdi;
41 	mc->mc_rsi = r->r_rsi;
42 	mc->mc_rdx = r->r_rdx;
43 	mc->mc_rcx = r->r_rcx;
44 	mc->mc_r8 = r->r_r8;
45 	mc->mc_r9 = r->r_r9;
46 	mc->mc_rax = r->r_rax;
47 	mc->mc_rbx = r->r_rbx;
48 	mc->mc_rbp = r->r_rbp;
49 	mc->mc_r10 = r->r_r10;
50 	mc->mc_r11 = r->r_r11;
51 	mc->mc_r12 = r->r_r12;
52 	mc->mc_r13 = r->r_r13;
53 	mc->mc_r14 = r->r_r14;
54 	mc->mc_r15 = r->r_r15;
55 	mc->mc_rip = r->r_rip;
56 	mc->mc_cs = r->r_cs;
57 	mc->mc_rflags = r->r_rflags;
58 	mc->mc_rsp = r->r_rsp;
59 	mc->mc_ss = r->r_ss;
60 }
61 
62 void
63 pt_ucontext_to_reg(const ucontext_t *uc, struct reg *r)
64 {
65 	const mcontext_t *mc = &uc->uc_mcontext;
66 
67 	r->r_rdi = mc->mc_rdi;
68 	r->r_rsi = mc->mc_rsi;
69 	r->r_rdx = mc->mc_rdx;
70 	r->r_rcx = mc->mc_rcx;
71 	r->r_r8 = mc->mc_r8;
72 	r->r_r9 = mc->mc_r9;
73 	r->r_rax = mc->mc_rax;
74 	r->r_rbx = mc->mc_rbx;
75 	r->r_rbp = mc->mc_rbp;
76 	r->r_r10 = mc->mc_r10;
77 	r->r_r11 = mc->mc_r11;
78 	r->r_r12 = mc->mc_r12;
79 	r->r_r13 = mc->mc_r13;
80 	r->r_r14 = mc->mc_r14;
81 	r->r_r15 = mc->mc_r15;
82 	r->r_rip = mc->mc_rip;
83 	r->r_cs = mc->mc_cs;
84 	r->r_rflags = mc->mc_rflags;
85 	r->r_rsp = mc->mc_rsp;
86 	r->r_ss = mc->mc_ss;
87 }
88 
89 void
90 pt_fpreg_to_ucontext(const struct fpreg* r, ucontext_t *uc)
91 {
92 	memcpy(&uc->uc_mcontext.mc_fpstate, r, sizeof(r));
93 }
94 
95 void
96 pt_ucontext_to_fpreg(const ucontext_t *uc, struct fpreg *r)
97 {
98 	memcpy(r, &uc->uc_mcontext.mc_fpstate, sizeof(r));
99 }
100 
101 void
102 pt_md_init(void)
103 {
104 	/* Nothing to do */
105 }
106 
107 int
108 pt_reg_sstep(struct reg *reg, int step)
109 {
110 	register_t old;
111 
112 	old = reg->r_rflags;
113 	if (step)
114 		reg->r_rflags |= 0x0100;
115 	else
116 		reg->r_rflags &= ~0x0100;
117 	return (old != reg->r_rflags); /* changed ? */
118 }
119