xref: /freebsd/lib/libthread_db/arch/riscv/libpthread_md.c (revision 1db64f89363c97858961c4df0b7d02f3223723cf)
1 /*-
2  * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * Portions of this software were developed by SRI International and the
6  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
8  *
9  * Portions of this software were developed by the University of Cambridge
10  * Computer Laboratory as part of the CTSRD Project, with support from the
11  * UK Higher Education Innovation Fund (HEIF).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 #include <string.h>
37 #include <thread_db.h>
38 
39 #include "libpthread_db.h"
40 
41 void
42 pt_reg_to_ucontext(const struct reg *r, ucontext_t *uc)
43 {
44 	mcontext_t *mc;
45 
46 	mc = &uc->uc_mcontext;
47 
48 	memcpy(mc->mc_gpregs.gp_t, r->t, sizeof(mc->mc_gpregs.gp_t));
49 	memcpy(mc->mc_gpregs.gp_s, r->s, sizeof(mc->mc_gpregs.gp_s));
50 	memcpy(mc->mc_gpregs.gp_a, r->a, sizeof(mc->mc_gpregs.gp_a));
51 	mc->mc_gpregs.gp_ra = r->ra;
52 	mc->mc_gpregs.gp_sp = r->sp;
53 	mc->mc_gpregs.gp_gp = r->gp;
54 	mc->mc_gpregs.gp_tp = r->tp;
55 	mc->mc_gpregs.gp_sepc = r->sepc;
56 	mc->mc_gpregs.gp_sstatus = r->sstatus;
57 }
58 
59 void
60 pt_ucontext_to_reg(const ucontext_t *uc, struct reg *r)
61 {
62 	const mcontext_t *mc;
63 
64 	mc = &uc->uc_mcontext;
65 
66 	memcpy(r->t, mc->mc_gpregs.gp_t, sizeof(mc->mc_gpregs.gp_t));
67 	memcpy(r->s, mc->mc_gpregs.gp_s, sizeof(mc->mc_gpregs.gp_s));
68 	memcpy(r->a, mc->mc_gpregs.gp_a, sizeof(mc->mc_gpregs.gp_a));
69 	r->ra = mc->mc_gpregs.gp_ra;
70 	r->sp = mc->mc_gpregs.gp_sp;
71 	r->gp = mc->mc_gpregs.gp_gp;
72 	r->tp = mc->mc_gpregs.gp_tp;
73 	r->sepc = mc->mc_gpregs.gp_sepc;
74 	r->sstatus = mc->mc_gpregs.gp_sstatus;
75 }
76 
77 void
78 pt_fpreg_to_ucontext(const struct fpreg *r __unused, ucontext_t *uc __unused)
79 {
80 	mcontext_t *mc = &uc->uc_mcontext;
81 
82 	memcpy(&mc->mc_fpregs, r, sizeof(*r));
83 	mc->mc_flags |= _MC_FP_VALID;
84 }
85 
86 void
87 pt_ucontext_to_fpreg(const ucontext_t *uc __unused, struct fpreg *r __unused)
88 {
89 	const mcontext_t *mc = &uc->uc_mcontext;
90 
91 	if (mc->mc_flags & _MC_FP_VALID)
92 		memcpy(r, &mc->mc_fpregs, sizeof(*r));
93 	else
94 		memset(r, 0, sizeof(*r));
95 }
96 
97 void
98 pt_md_init(void)
99 {
100 }
101 
102 int
103 pt_reg_sstep(struct reg *reg __unused, int step __unused)
104 {
105 
106 	return (0);
107 }
108