xref: /freebsd/lib/libthread_db/arch/riscv/libpthread_md.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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/cdefs.h>
36 #include <sys/types.h>
37 #include <string.h>
38 #include <thread_db.h>
39 
40 #include "libpthread_db.h"
41 
42 void
43 pt_reg_to_ucontext(const struct reg *r, ucontext_t *uc)
44 {
45 	mcontext_t *mc;
46 
47 	mc = &uc->uc_mcontext;
48 
49 	memcpy(mc->mc_gpregs.gp_t, r->t, sizeof(mc->mc_gpregs.gp_t));
50 	memcpy(mc->mc_gpregs.gp_s, r->s, sizeof(mc->mc_gpregs.gp_s));
51 	memcpy(mc->mc_gpregs.gp_a, r->a, sizeof(mc->mc_gpregs.gp_a));
52 	mc->mc_gpregs.gp_ra = r->ra;
53 	mc->mc_gpregs.gp_sp = r->sp;
54 	mc->mc_gpregs.gp_gp = r->gp;
55 	mc->mc_gpregs.gp_tp = r->tp;
56 	mc->mc_gpregs.gp_sepc = r->sepc;
57 	mc->mc_gpregs.gp_sstatus = r->sstatus;
58 }
59 
60 void
61 pt_ucontext_to_reg(const ucontext_t *uc, struct reg *r)
62 {
63 	const mcontext_t *mc;
64 
65 	mc = &uc->uc_mcontext;
66 
67 	memcpy(r->t, mc->mc_gpregs.gp_t, sizeof(mc->mc_gpregs.gp_t));
68 	memcpy(r->s, mc->mc_gpregs.gp_s, sizeof(mc->mc_gpregs.gp_s));
69 	memcpy(r->a, mc->mc_gpregs.gp_a, sizeof(mc->mc_gpregs.gp_a));
70 	r->ra = mc->mc_gpregs.gp_ra;
71 	r->sp = mc->mc_gpregs.gp_sp;
72 	r->gp = mc->mc_gpregs.gp_gp;
73 	r->tp = mc->mc_gpregs.gp_tp;
74 	r->sepc = mc->mc_gpregs.gp_sepc;
75 	r->sstatus = mc->mc_gpregs.gp_sstatus;
76 }
77 
78 void
79 pt_fpreg_to_ucontext(const struct fpreg *r __unused, ucontext_t *uc __unused)
80 {
81 	mcontext_t *mc = &uc->uc_mcontext;
82 
83 	memcpy(&mc->mc_fpregs, r, sizeof(*r));
84 	mc->mc_flags |= _MC_FP_VALID;
85 }
86 
87 void
88 pt_ucontext_to_fpreg(const ucontext_t *uc __unused, struct fpreg *r __unused)
89 {
90 	const mcontext_t *mc = &uc->uc_mcontext;
91 
92 	if (mc->mc_flags & _MC_FP_VALID)
93 		memcpy(r, &mc->mc_fpregs, sizeof(*r));
94 	else
95 		memset(r, 0, sizeof(*r));
96 }
97 
98 void
99 pt_md_init(void)
100 {
101 }
102 
103 int
104 pt_reg_sstep(struct reg *reg __unused, int step __unused)
105 {
106 
107 	return (0);
108 }
109