xref: /freebsd/lib/libthread_db/arch/i386/libpthread_md.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
1*5e53a4f9SPedro F. Giffuni /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*5e53a4f9SPedro F. Giffuni  *
43c1e38eaSMarcel Moolenaar  * Copyright (c) 2004 David Xu <davidxu@freebsd.org>
53c1e38eaSMarcel Moolenaar  * All rights reserved.
63c1e38eaSMarcel Moolenaar  *
73c1e38eaSMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
83c1e38eaSMarcel Moolenaar  * modification, are permitted provided that the following conditions
93c1e38eaSMarcel Moolenaar  * are met:
103c1e38eaSMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
113c1e38eaSMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
123c1e38eaSMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
133c1e38eaSMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
143c1e38eaSMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
153c1e38eaSMarcel Moolenaar  *
163c1e38eaSMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
173c1e38eaSMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183c1e38eaSMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193c1e38eaSMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
203c1e38eaSMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
213c1e38eaSMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
223c1e38eaSMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233c1e38eaSMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
243c1e38eaSMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253c1e38eaSMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263c1e38eaSMarcel Moolenaar  * SUCH DAMAGE.
273c1e38eaSMarcel Moolenaar  */
283c1e38eaSMarcel Moolenaar 
293c1e38eaSMarcel Moolenaar #include <sys/cdefs.h>
303c1e38eaSMarcel Moolenaar __FBSDID("$FreeBSD$");
313c1e38eaSMarcel Moolenaar 
323c1e38eaSMarcel Moolenaar #include <sys/types.h>
333c1e38eaSMarcel Moolenaar #include <machine/npx.h>
34820c1c55SMarcel Moolenaar #include <string.h>
35820c1c55SMarcel Moolenaar #include <thread_db.h>
363c1e38eaSMarcel Moolenaar 
373c1e38eaSMarcel Moolenaar #include "libpthread_db.h"
383c1e38eaSMarcel Moolenaar 
393c1e38eaSMarcel Moolenaar static int has_xmm_regs;
403c1e38eaSMarcel Moolenaar 
413c1e38eaSMarcel Moolenaar void
423c1e38eaSMarcel Moolenaar pt_reg_to_ucontext(const struct reg *r, ucontext_t *uc)
433c1e38eaSMarcel Moolenaar {
443c1e38eaSMarcel Moolenaar 	memcpy(&uc->uc_mcontext.mc_fs, &r->r_fs, 18*4);
453c1e38eaSMarcel Moolenaar 	uc->uc_mcontext.mc_gs = r->r_gs;
463c1e38eaSMarcel Moolenaar }
473c1e38eaSMarcel Moolenaar 
483c1e38eaSMarcel Moolenaar void
493c1e38eaSMarcel Moolenaar pt_ucontext_to_reg(const ucontext_t *uc, struct reg *r)
503c1e38eaSMarcel Moolenaar {
513c1e38eaSMarcel Moolenaar 	memcpy(&r->r_fs, &uc->uc_mcontext.mc_fs, 18*4);
523c1e38eaSMarcel Moolenaar 	r->r_gs = uc->uc_mcontext.mc_gs;
533c1e38eaSMarcel Moolenaar }
543c1e38eaSMarcel Moolenaar 
553c1e38eaSMarcel Moolenaar void
563c1e38eaSMarcel Moolenaar pt_fpreg_to_ucontext(const struct fpreg* r, ucontext_t *uc)
573c1e38eaSMarcel Moolenaar {
583c1e38eaSMarcel Moolenaar 	if (!has_xmm_regs)
593c1e38eaSMarcel Moolenaar 		memcpy(&uc->uc_mcontext.mc_fpstate, r,
603c1e38eaSMarcel Moolenaar 			sizeof(struct save87));
613c1e38eaSMarcel Moolenaar 	else {
623c1e38eaSMarcel Moolenaar 		int i;
633c1e38eaSMarcel Moolenaar 		struct savexmm *sx = (struct savexmm *)&uc->uc_mcontext.mc_fpstate;
643c1e38eaSMarcel Moolenaar 		memcpy(&sx->sv_env, &r->fpr_env, sizeof(r->fpr_env));
653c1e38eaSMarcel Moolenaar 		for (i = 0; i < 8; ++i)
663c1e38eaSMarcel Moolenaar 			memcpy(&sx->sv_fp[i].fp_acc, &r->fpr_acc[i], 10);
673c1e38eaSMarcel Moolenaar 	}
683c1e38eaSMarcel Moolenaar }
693c1e38eaSMarcel Moolenaar 
703c1e38eaSMarcel Moolenaar void
713c1e38eaSMarcel Moolenaar pt_ucontext_to_fpreg(const ucontext_t *uc, struct fpreg *r)
723c1e38eaSMarcel Moolenaar {
733c1e38eaSMarcel Moolenaar 	if (!has_xmm_regs)
743c1e38eaSMarcel Moolenaar 		memcpy(r, &uc->uc_mcontext.mc_fpstate, sizeof(struct save87));
753c1e38eaSMarcel Moolenaar 	else {
763c1e38eaSMarcel Moolenaar 		int i;
77c7d7e597SDimitry Andric 		const struct savexmm *sx = (const struct savexmm *)&uc->uc_mcontext.mc_fpstate;
783c1e38eaSMarcel Moolenaar 		memcpy(&r->fpr_env, &sx->sv_env, sizeof(r->fpr_env));
793c1e38eaSMarcel Moolenaar 		for (i = 0; i < 8; ++i)
803c1e38eaSMarcel Moolenaar 			memcpy(&r->fpr_acc[i], &sx->sv_fp[i].fp_acc, 10);
813c1e38eaSMarcel Moolenaar 	}
823c1e38eaSMarcel Moolenaar }
833c1e38eaSMarcel Moolenaar 
843c1e38eaSMarcel Moolenaar void
858d7681bbSDoug Rabson pt_fxsave_to_ucontext(const char* r, ucontext_t *uc)
868d7681bbSDoug Rabson {
878d7681bbSDoug Rabson 	if (has_xmm_regs)
888d7681bbSDoug Rabson 		memcpy(&uc->uc_mcontext.mc_fpstate, r, sizeof(struct savexmm));
898d7681bbSDoug Rabson }
908d7681bbSDoug Rabson 
918d7681bbSDoug Rabson void
928d7681bbSDoug Rabson pt_ucontext_to_fxsave(const ucontext_t *uc, char *r)
938d7681bbSDoug Rabson {
948d7681bbSDoug Rabson 	if (has_xmm_regs)
958d7681bbSDoug Rabson 		memcpy(r, &uc->uc_mcontext.mc_fpstate, sizeof(struct savexmm));
968d7681bbSDoug Rabson }
978d7681bbSDoug Rabson 
988d7681bbSDoug Rabson void
993c1e38eaSMarcel Moolenaar pt_md_init(void)
1003c1e38eaSMarcel Moolenaar {
1013c1e38eaSMarcel Moolenaar 	ucontext_t uc;
1023c1e38eaSMarcel Moolenaar 
1033c1e38eaSMarcel Moolenaar 	getcontext(&uc);
1043c1e38eaSMarcel Moolenaar 	if (uc.uc_mcontext.mc_fpformat == _MC_FPFMT_XMM)
1053c1e38eaSMarcel Moolenaar 	    has_xmm_regs = 1;
1063c1e38eaSMarcel Moolenaar }
1073c1e38eaSMarcel Moolenaar 
1083c1e38eaSMarcel Moolenaar int
1093c1e38eaSMarcel Moolenaar pt_reg_sstep(struct reg *reg, int step)
1103c1e38eaSMarcel Moolenaar {
1113c1e38eaSMarcel Moolenaar 	unsigned int old;
1123c1e38eaSMarcel Moolenaar 
1133c1e38eaSMarcel Moolenaar 	old = reg->r_eflags;
1143c1e38eaSMarcel Moolenaar 	if (step)
1153c1e38eaSMarcel Moolenaar 		reg->r_eflags |= 0x0100;
1163c1e38eaSMarcel Moolenaar 	else
1173c1e38eaSMarcel Moolenaar 		reg->r_eflags &= ~0x0100;
1183c1e38eaSMarcel Moolenaar 	return (old != reg->r_eflags); /* changed ? */
1193c1e38eaSMarcel Moolenaar }
1203c1e38eaSMarcel Moolenaar 
121