libpthread_md.c revision 132332
1132332Smarcel/*
2132332Smarcel * Copyright (c) 2004 David Xu <davidxu@freebsd.org>
3132332Smarcel * All rights reserved.
4132332Smarcel *
5132332Smarcel * Redistribution and use in source and binary forms, with or without
6132332Smarcel * modification, are permitted provided that the following conditions
7132332Smarcel * are met:
8132332Smarcel * 1. Redistributions of source code must retain the above copyright
9132332Smarcel *    notice, this list of conditions and the following disclaimer.
10132332Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11132332Smarcel *    notice, this list of conditions and the following disclaimer in the
12132332Smarcel *    documentation and/or other materials provided with the distribution.
13132332Smarcel *
14132332Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15132332Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16132332Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17132332Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18132332Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19132332Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20132332Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21132332Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22132332Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23132332Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24132332Smarcel * SUCH DAMAGE.
25132332Smarcel */
26132332Smarcel
27132332Smarcel#include <sys/cdefs.h>
28132332Smarcel__FBSDID("$FreeBSD: head/lib/libthread_db/arch/i386/libpthread_md.c 132332 2004-07-18 04:17:15Z marcel $");
29132332Smarcel
30132332Smarcel#include <string.h>
31132332Smarcel#include <sys/types.h>
32132332Smarcel#include <proc_service.h>
33132332Smarcel#include <thread_db.h>
34132332Smarcel#include <machine/npx.h>
35132332Smarcel
36132332Smarcel#include "libpthread_db.h"
37132332Smarcel
38132332Smarcelstatic int has_xmm_regs;
39132332Smarcel
40132332Smarcelvoid
41132332Smarcelpt_reg_to_ucontext(const struct reg *r, ucontext_t *uc)
42132332Smarcel{
43132332Smarcel	memcpy(&uc->uc_mcontext.mc_fs, &r->r_fs, 18*4);
44132332Smarcel	uc->uc_mcontext.mc_gs = r->r_gs;
45132332Smarcel}
46132332Smarcel
47132332Smarcelvoid
48132332Smarcelpt_ucontext_to_reg(const ucontext_t *uc, struct reg *r)
49132332Smarcel{
50132332Smarcel	memcpy(&r->r_fs, &uc->uc_mcontext.mc_fs, 18*4);
51132332Smarcel	r->r_gs = uc->uc_mcontext.mc_gs;
52132332Smarcel}
53132332Smarcel
54132332Smarcelvoid
55132332Smarcelpt_fpreg_to_ucontext(const struct fpreg* r, ucontext_t *uc)
56132332Smarcel{
57132332Smarcel	if (!has_xmm_regs)
58132332Smarcel		memcpy(&uc->uc_mcontext.mc_fpstate, r,
59132332Smarcel			sizeof(struct save87));
60132332Smarcel	else {
61132332Smarcel		int i;
62132332Smarcel		struct savexmm *sx = (struct savexmm *)&uc->uc_mcontext.mc_fpstate;
63132332Smarcel		memcpy(&sx->sv_env, &r->fpr_env, sizeof(r->fpr_env));
64132332Smarcel		for (i = 0; i < 8; ++i)
65132332Smarcel			memcpy(&sx->sv_fp[i].fp_acc, &r->fpr_acc[i], 10);
66132332Smarcel	}
67132332Smarcel}
68132332Smarcel
69132332Smarcelvoid
70132332Smarcelpt_ucontext_to_fpreg(const ucontext_t *uc, struct fpreg *r)
71132332Smarcel{
72132332Smarcel	if (!has_xmm_regs)
73132332Smarcel		memcpy(r, &uc->uc_mcontext.mc_fpstate, sizeof(struct save87));
74132332Smarcel	else {
75132332Smarcel		int i;
76132332Smarcel		struct savexmm *sx = (struct savexmm *)&uc->uc_mcontext.mc_fpstate;
77132332Smarcel		memcpy(&r->fpr_env, &sx->sv_env, sizeof(r->fpr_env));
78132332Smarcel		for (i = 0; i < 8; ++i)
79132332Smarcel			memcpy(&r->fpr_acc[i], &sx->sv_fp[i].fp_acc, 10);
80132332Smarcel	}
81132332Smarcel}
82132332Smarcel
83132332Smarcelvoid
84132332Smarcelpt_md_init(void)
85132332Smarcel{
86132332Smarcel	ucontext_t uc;
87132332Smarcel
88132332Smarcel	getcontext(&uc);
89132332Smarcel	if (uc.uc_mcontext.mc_fpformat == _MC_FPFMT_XMM)
90132332Smarcel	    has_xmm_regs = 1;
91132332Smarcel}
92132332Smarcel
93132332Smarcelint
94132332Smarcelpt_reg_sstep(struct reg *reg, int step)
95132332Smarcel{
96132332Smarcel	unsigned int old;
97132332Smarcel
98132332Smarcel	old = reg->r_eflags;
99132332Smarcel	if (step)
100132332Smarcel		reg->r_eflags |= 0x0100;
101132332Smarcel	else
102132332Smarcel		reg->r_eflags &= ~0x0100;
103132332Smarcel	return (old != reg->r_eflags); /* changed ? */
104132332Smarcel}
105132332Smarcel
106