Jeff Dike | 48b2018 | 2007-05-06 14:51:00 -0700 | [diff] [blame^] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
Jeff Dike | 54f9a39 | 2006-12-06 20:34:52 -0800 | [diff] [blame] | 7 | #include <stddef.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <errno.h> |
| 9 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "ptrace_user.h" |
| 11 | /* Grr, asm/user.h includes asm/ptrace.h, so has to follow ptrace_user.h */ |
| 12 | #include <asm/user.h> |
| 13 | #include "kern_util.h" |
| 14 | #include "sysdep/thread.h" |
| 15 | #include "user.h" |
| 16 | #include "os.h" |
Paolo 'Blaisorblade' Giarrusso | 972410b | 2006-03-31 02:30:21 -0800 | [diff] [blame] | 17 | #include "uml-config.h" |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 18 | #include "user_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | int ptrace_getregs(long pid, unsigned long *regs_out) |
| 21 | { |
| 22 | if (ptrace(PTRACE_GETREGS, pid, 0, regs_out) < 0) |
| 23 | return -errno; |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | int ptrace_setregs(long pid, unsigned long *regs) |
| 28 | { |
| 29 | if (ptrace(PTRACE_SETREGS, pid, 0, regs) < 0) |
| 30 | return -errno; |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | int ptrace_getfpregs(long pid, unsigned long *regs) |
| 35 | { |
| 36 | if (ptrace(PTRACE_GETFPREGS, pid, 0, regs) < 0) |
| 37 | return -errno; |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | int ptrace_setfpregs(long pid, unsigned long *regs) |
| 42 | { |
| 43 | if (ptrace(PTRACE_SETFPREGS, pid, 0, regs) < 0) |
| 44 | return -errno; |
| 45 | return 0; |
| 46 | } |
| 47 | |
Jeff Dike | 48b2018 | 2007-05-06 14:51:00 -0700 | [diff] [blame^] | 48 | #ifdef UML_CONFIG_MODE_TT |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static void write_debugregs(int pid, unsigned long *regs) |
| 51 | { |
| 52 | struct user *dummy; |
| 53 | int nregs, i; |
| 54 | |
| 55 | dummy = NULL; |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 56 | nregs = ARRAY_SIZE(dummy->u_debugreg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | for(i = 0; i < nregs; i++){ |
| 58 | if((i == 4) || (i == 5)) continue; |
| 59 | if(ptrace(PTRACE_POKEUSR, pid, &dummy->u_debugreg[i], |
| 60 | regs[i]) < 0) |
| 61 | printk("write_debugregs - ptrace failed on " |
Paolo 'Blaisorblade' Giarrusso | 802e307 | 2006-04-10 22:53:32 -0700 | [diff] [blame] | 62 | "register %d, value = 0x%lx, errno = %d\n", i, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | regs[i], errno); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static void read_debugregs(int pid, unsigned long *regs) |
| 68 | { |
| 69 | struct user *dummy; |
| 70 | int nregs, i; |
| 71 | |
| 72 | dummy = NULL; |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 73 | nregs = ARRAY_SIZE(dummy->u_debugreg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | for(i = 0; i < nregs; i++){ |
| 75 | regs[i] = ptrace(PTRACE_PEEKUSR, pid, |
| 76 | &dummy->u_debugreg[i], 0); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* Accessed only by the tracing thread */ |
| 81 | static unsigned long kernel_debugregs[8] = { [ 0 ... 7 ] = 0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | void arch_enter_kernel(void *task, int pid) |
| 84 | { |
| 85 | read_debugregs(pid, TASK_DEBUGREGS(task)); |
| 86 | write_debugregs(pid, kernel_debugregs); |
| 87 | } |
| 88 | |
| 89 | void arch_leave_kernel(void *task, int pid) |
| 90 | { |
| 91 | read_debugregs(pid, kernel_debugregs); |
| 92 | write_debugregs(pid, TASK_DEBUGREGS(task)); |
| 93 | } |
| 94 | |
Paolo 'Blaisorblade' Giarrusso | 972410b | 2006-03-31 02:30:21 -0800 | [diff] [blame] | 95 | #ifdef UML_CONFIG_PT_PROXY |
| 96 | /* Accessed only by the tracing thread */ |
| 97 | static int debugregs_seq; |
| 98 | |
| 99 | /* Only called by the ptrace proxy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | void ptrace_pokeuser(unsigned long addr, unsigned long data) |
| 101 | { |
| 102 | if((addr < offsetof(struct user, u_debugreg[0])) || |
| 103 | (addr > offsetof(struct user, u_debugreg[7]))) |
| 104 | return; |
| 105 | addr -= offsetof(struct user, u_debugreg[0]); |
| 106 | addr = addr >> 2; |
| 107 | if(kernel_debugregs[addr] == data) return; |
| 108 | |
| 109 | kernel_debugregs[addr] = data; |
| 110 | debugregs_seq++; |
| 111 | } |
| 112 | |
| 113 | static void update_debugregs_cb(void *arg) |
| 114 | { |
| 115 | int pid = *((int *) arg); |
| 116 | |
| 117 | write_debugregs(pid, kernel_debugregs); |
| 118 | } |
| 119 | |
Paolo 'Blaisorblade' Giarrusso | 972410b | 2006-03-31 02:30:21 -0800 | [diff] [blame] | 120 | /* Optimized out in its header when not defined */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | void update_debugregs(int seq) |
| 122 | { |
| 123 | int me; |
| 124 | |
| 125 | if(seq == debugregs_seq) return; |
| 126 | |
| 127 | me = os_getpid(); |
| 128 | initial_thread_cb(update_debugregs_cb, &me); |
| 129 | } |
Paolo 'Blaisorblade' Giarrusso | 972410b | 2006-03-31 02:30:21 -0800 | [diff] [blame] | 130 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Jeff Dike | 48b2018 | 2007-05-06 14:51:00 -0700 | [diff] [blame^] | 132 | #endif |