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