blob: 01212c88fcc4ffc490926f35d7af7861d2006e11 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
Jeff Dike54f9a392006-12-06 20:34:52 -08007#include <stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <errno.h>
9#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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' Giarrusso972410b2006-03-31 02:30:21 -080017#include "uml-config.h"
Jeff Dike91b165c2006-09-25 23:33:00 -070018#include "user_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20int 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
27int 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
34int 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
41int 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
Paolo 'Blaisorblade' Giarrusso972410b2006-03-31 02:30:21 -080048/* All the below stuff is of interest for TT mode only */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static void write_debugregs(int pid, unsigned long *regs)
50{
51 struct user *dummy;
52 int nregs, i;
53
54 dummy = NULL;
Jeff Dike91b165c2006-09-25 23:33:00 -070055 nregs = ARRAY_SIZE(dummy->u_debugreg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 for(i = 0; i < nregs; i++){
57 if((i == 4) || (i == 5)) continue;
58 if(ptrace(PTRACE_POKEUSR, pid, &dummy->u_debugreg[i],
59 regs[i]) < 0)
60 printk("write_debugregs - ptrace failed on "
Paolo 'Blaisorblade' Giarrusso802e3072006-04-10 22:53:32 -070061 "register %d, value = 0x%lx, errno = %d\n", i,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 regs[i], errno);
63 }
64}
65
66static void read_debugregs(int pid, unsigned long *regs)
67{
68 struct user *dummy;
69 int nregs, i;
70
71 dummy = NULL;
Jeff Dike91b165c2006-09-25 23:33:00 -070072 nregs = ARRAY_SIZE(dummy->u_debugreg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 for(i = 0; i < nregs; i++){
74 regs[i] = ptrace(PTRACE_PEEKUSR, pid,
75 &dummy->u_debugreg[i], 0);
76 }
77}
78
79/* Accessed only by the tracing thread */
80static unsigned long kernel_debugregs[8] = { [ 0 ... 7 ] = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82void arch_enter_kernel(void *task, int pid)
83{
84 read_debugregs(pid, TASK_DEBUGREGS(task));
85 write_debugregs(pid, kernel_debugregs);
86}
87
88void arch_leave_kernel(void *task, int pid)
89{
90 read_debugregs(pid, kernel_debugregs);
91 write_debugregs(pid, TASK_DEBUGREGS(task));
92}
93
Paolo 'Blaisorblade' Giarrusso972410b2006-03-31 02:30:21 -080094#ifdef UML_CONFIG_PT_PROXY
95/* Accessed only by the tracing thread */
96static int debugregs_seq;
97
98/* Only called by the ptrace proxy */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099void ptrace_pokeuser(unsigned long addr, unsigned long data)
100{
101 if((addr < offsetof(struct user, u_debugreg[0])) ||
102 (addr > offsetof(struct user, u_debugreg[7])))
103 return;
104 addr -= offsetof(struct user, u_debugreg[0]);
105 addr = addr >> 2;
106 if(kernel_debugregs[addr] == data) return;
107
108 kernel_debugregs[addr] = data;
109 debugregs_seq++;
110}
111
112static void update_debugregs_cb(void *arg)
113{
114 int pid = *((int *) arg);
115
116 write_debugregs(pid, kernel_debugregs);
117}
118
Paolo 'Blaisorblade' Giarrusso972410b2006-03-31 02:30:21 -0800119/* Optimized out in its header when not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120void update_debugregs(int seq)
121{
122 int me;
123
124 if(seq == debugregs_seq) return;
125
126 me = os_getpid();
127 initial_thread_cb(update_debugregs_cb, &me);
128}
Paolo 'Blaisorblade' Giarrusso972410b2006-03-31 02:30:21 -0800129#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*
132 * Overrides for Emacs so that we follow Linus's tabbing style.
133 * Emacs will notice this stuff at the end of the file and automatically
134 * adjust the settings for this buffer only. This must remain at the end
135 * of the file.
136 * ---------------------------------------------------------------------------
137 * Local variables:
138 * c-file-style: "linux"
139 * End:
140 */