registers.c 2.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (C) 2004 PathScale, Inc
J
Jeff Dike 已提交
3
 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
4 5 6 7
 * Licensed under the GPL
 */

#include <errno.h>
8
#include <sys/ptrace.h>
9
#ifdef __i386__
J
Jeff Dike 已提交
10
#include <sys/user.h>
11
#endif
12 13
#include <longjmp.h>
#include <sysdep/ptrace_user.h>
L
Linus Torvalds 已提交
14 15 16

int save_fp_registers(int pid, unsigned long *fp_regs)
{
J
Jeff Dike 已提交
17
	if (ptrace(PTRACE_GETFPREGS, pid, 0, fp_regs) < 0)
18 19
		return -errno;
	return 0;
L
Linus Torvalds 已提交
20 21 22 23
}

int restore_fp_registers(int pid, unsigned long *fp_regs)
{
J
Jeff Dike 已提交
24
	if (ptrace(PTRACE_SETFPREGS, pid, 0, fp_regs) < 0)
25 26
		return -errno;
	return 0;
L
Linus Torvalds 已提交
27 28
}

29 30
#ifdef __i386__
int have_fpx_regs = 1;
31 32
int save_fpx_registers(int pid, unsigned long *fp_regs)
{
J
Jeff Dike 已提交
33
	if (ptrace(PTRACE_GETFPXREGS, pid, 0, fp_regs) < 0)
34 35 36 37 38 39
		return -errno;
	return 0;
}

int restore_fpx_registers(int pid, unsigned long *fp_regs)
{
J
Jeff Dike 已提交
40
	if (ptrace(PTRACE_SETFPXREGS, pid, 0, fp_regs) < 0)
41 42 43 44
		return -errno;
	return 0;
}

J
Jeff Dike 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
int get_fp_registers(int pid, unsigned long *regs)
{
	if (have_fpx_regs)
		return save_fpx_registers(pid, regs);
	else
		return save_fp_registers(pid, regs);
}

int put_fp_registers(int pid, unsigned long *regs)
{
	if (have_fpx_regs)
		return restore_fpx_registers(pid, regs);
	else
		return restore_fp_registers(pid, regs);
}

61 62
void arch_init_registers(int pid)
{
J
Jeff Dike 已提交
63
	struct user_fpxregs_struct fpx_regs;
64 65
	int err;

J
Jeff Dike 已提交
66
	err = ptrace(PTRACE_GETFPXREGS, pid, 0, &fpx_regs);
J
Jeff Dike 已提交
67
	if (!err)
68 69
		return;

J
Jeff Dike 已提交
70
	if (errno != EIO)
71 72 73 74 75
		panic("check_ptrace : PTRACE_GETFPXREGS failed, errno = %d",
		      errno);

	have_fpx_regs = 0;
}
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#else

int get_fp_registers(int pid, unsigned long *regs)
{
	return save_fp_registers(pid, regs);
}

int put_fp_registers(int pid, unsigned long *regs)
{
	return restore_fp_registers(pid, regs);
}

#endif

unsigned long get_thread_reg(int reg, jmp_buf *buf)
{
	switch (reg) {
#ifdef __i386__
A
Al Viro 已提交
94
	case HOST_IP:
95
		return buf[0]->__eip;
A
Al Viro 已提交
96
	case HOST_SP:
97
		return buf[0]->__esp;
A
Al Viro 已提交
98
	case HOST_BP:
99 100
		return buf[0]->__ebp;
#else
A
Al Viro 已提交
101
	case HOST_IP:
102
		return buf[0]->__rip;
A
Al Viro 已提交
103
	case HOST_SP:
104
		return buf[0]->__rsp;
A
Al Viro 已提交
105
	case HOST_BP:
106 107 108 109 110 111 112 113
		return buf[0]->__rbp;
#endif
	default:
		printk(UM_KERN_ERR "get_thread_regs - unknown register %d\n",
		       reg);
		return 0;
	}
}