Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 PathScale, Inc |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <errno.h> |
| 7 | #include <string.h> |
Allan Graves | fad1c45 | 2005-10-04 14:53:52 -0400 | [diff] [blame] | 8 | #include <setjmp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include "ptrace_user.h" |
| 10 | #include "uml-config.h" |
| 11 | #include "skas_ptregs.h" |
| 12 | #include "registers.h" |
| 13 | #include "user.h" |
| 14 | |
| 15 | /* These are set once at boot time and not changed thereafter */ |
| 16 | |
| 17 | static unsigned long exec_regs[HOST_FRAME_SIZE]; |
| 18 | static unsigned long exec_fp_regs[HOST_FP_SIZE]; |
| 19 | |
| 20 | void init_thread_registers(union uml_pt_regs *to) |
| 21 | { |
| 22 | memcpy(to->skas.regs, exec_regs, sizeof(to->skas.regs)); |
| 23 | memcpy(to->skas.fp, exec_fp_regs, sizeof(to->skas.fp)); |
| 24 | } |
| 25 | |
| 26 | static int move_registers(int pid, int int_op, int fp_op, |
| 27 | union uml_pt_regs *regs) |
| 28 | { |
| 29 | if(ptrace(int_op, pid, 0, regs->skas.regs) < 0) |
| 30 | return(-errno); |
| 31 | |
| 32 | if(ptrace(fp_op, pid, 0, regs->skas.fp) < 0) |
| 33 | return(-errno); |
| 34 | |
| 35 | return(0); |
| 36 | } |
| 37 | |
| 38 | void save_registers(int pid, union uml_pt_regs *regs) |
| 39 | { |
| 40 | int err; |
| 41 | |
| 42 | err = move_registers(pid, PTRACE_GETREGS, PTRACE_GETFPREGS, regs); |
| 43 | if(err) |
| 44 | panic("save_registers - saving registers failed, errno = %d\n", |
| 45 | -err); |
| 46 | } |
| 47 | |
| 48 | void restore_registers(int pid, union uml_pt_regs *regs) |
| 49 | { |
| 50 | int err; |
| 51 | |
| 52 | err = move_registers(pid, PTRACE_SETREGS, PTRACE_SETFPREGS, regs); |
| 53 | if(err) |
| 54 | panic("restore_registers - saving registers failed, " |
| 55 | "errno = %d\n", -err); |
| 56 | } |
| 57 | |
| 58 | void init_registers(int pid) |
| 59 | { |
| 60 | int err; |
| 61 | |
| 62 | err = ptrace(PTRACE_GETREGS, pid, 0, exec_regs); |
| 63 | if(err) |
| 64 | panic("check_ptrace : PTRACE_GETREGS failed, errno = %d", |
| 65 | err); |
| 66 | |
| 67 | err = ptrace(PTRACE_GETFPREGS, pid, 0, exec_fp_regs); |
| 68 | if(err) |
| 69 | panic("check_ptrace : PTRACE_GETFPREGS failed, errno = %d", |
| 70 | err); |
| 71 | } |
| 72 | |
Jeff Dike | e2216fe | 2006-02-07 12:58:43 -0800 | [diff] [blame] | 73 | void get_safe_registers(unsigned long *regs, unsigned long *fp_regs) |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 74 | { |
| 75 | memcpy(regs, exec_regs, HOST_FRAME_SIZE * sizeof(unsigned long)); |
Jeff Dike | e2216fe | 2006-02-07 12:58:43 -0800 | [diff] [blame] | 76 | if(fp_regs != NULL) |
| 77 | memcpy(fp_regs, exec_fp_regs, |
| 78 | HOST_FP_SIZE * sizeof(unsigned long)); |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Allan Graves | fad1c45 | 2005-10-04 14:53:52 -0400 | [diff] [blame] | 81 | void get_thread_regs(union uml_pt_regs *uml_regs, void *buffer) |
| 82 | { |
| 83 | struct __jmp_buf_tag *jmpbuf = buffer; |
| 84 | |
| 85 | UPT_SET(uml_regs, RIP, jmpbuf->__jmpbuf[JB_PC]); |
| 86 | UPT_SET(uml_regs, RSP, jmpbuf->__jmpbuf[JB_RSP]); |
| 87 | UPT_SET(uml_regs, RBP, jmpbuf->__jmpbuf[JB_RBP]); |
| 88 | } |