tick/idle/powerpc: Do not register idle states with CPUIDLE_FLAG_TIMER_STOP set in...
[linux/fpc-iii.git] / arch / xtensa / kernel / ptrace.c
blob4d54b481123b64e286f45f6fc03121a0d5024474
1 // TODO some minor issues
2 /*
3 * This file is subject to the terms and conditions of the GNU General Public
4 * License. See the file "COPYING" in the main directory of this archive
5 * for more details.
7 * Copyright (C) 2001 - 2007 Tensilica Inc.
9 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
10 * Chris Zankel <chris@zankel.net>
11 * Scott Foehner<sfoehner@yahoo.com>,
12 * Kevin Chea
13 * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/mm.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/smp.h>
22 #include <linux/security.h>
23 #include <linux/signal.h>
25 #include <asm/pgtable.h>
26 #include <asm/page.h>
27 #include <asm/uaccess.h>
28 #include <asm/ptrace.h>
29 #include <asm/elf.h>
30 #include <asm/coprocessor.h>
33 void user_enable_single_step(struct task_struct *child)
35 child->ptrace |= PT_SINGLESTEP;
38 void user_disable_single_step(struct task_struct *child)
40 child->ptrace &= ~PT_SINGLESTEP;
44 * Called by kernel/ptrace.c when detaching to disable single stepping.
47 void ptrace_disable(struct task_struct *child)
49 /* Nothing to do.. */
52 int ptrace_getregs(struct task_struct *child, void __user *uregs)
54 struct pt_regs *regs = task_pt_regs(child);
55 xtensa_gregset_t __user *gregset = uregs;
56 unsigned long wb = regs->windowbase;
57 int i;
59 if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
60 return -EIO;
62 __put_user(regs->pc, &gregset->pc);
63 __put_user(regs->ps & ~(1 << PS_EXCM_BIT), &gregset->ps);
64 __put_user(regs->lbeg, &gregset->lbeg);
65 __put_user(regs->lend, &gregset->lend);
66 __put_user(regs->lcount, &gregset->lcount);
67 __put_user(regs->windowstart, &gregset->windowstart);
68 __put_user(regs->windowbase, &gregset->windowbase);
69 __put_user(regs->threadptr, &gregset->threadptr);
71 for (i = 0; i < XCHAL_NUM_AREGS; i++)
72 __put_user(regs->areg[i],
73 gregset->a + ((wb * 4 + i) % XCHAL_NUM_AREGS));
75 return 0;
78 int ptrace_setregs(struct task_struct *child, void __user *uregs)
80 struct pt_regs *regs = task_pt_regs(child);
81 xtensa_gregset_t *gregset = uregs;
82 const unsigned long ps_mask = PS_CALLINC_MASK | PS_OWB_MASK;
83 unsigned long ps;
84 unsigned long wb, ws;
86 if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
87 return -EIO;
89 __get_user(regs->pc, &gregset->pc);
90 __get_user(ps, &gregset->ps);
91 __get_user(regs->lbeg, &gregset->lbeg);
92 __get_user(regs->lend, &gregset->lend);
93 __get_user(regs->lcount, &gregset->lcount);
94 __get_user(ws, &gregset->windowstart);
95 __get_user(wb, &gregset->windowbase);
96 __get_user(regs->threadptr, &gregset->threadptr);
98 regs->ps = (regs->ps & ~ps_mask) | (ps & ps_mask) | (1 << PS_EXCM_BIT);
100 if (wb >= XCHAL_NUM_AREGS / 4)
101 return -EFAULT;
103 if (wb != regs->windowbase || ws != regs->windowstart) {
104 unsigned long rotws, wmask;
106 rotws = (((ws | (ws << WSBITS)) >> wb) &
107 ((1 << WSBITS) - 1)) & ~1;
108 wmask = ((rotws ? WSBITS + 1 - ffs(rotws) : 0) << 4) |
109 (rotws & 0xF) | 1;
110 regs->windowbase = wb;
111 regs->windowstart = ws;
112 regs->wmask = wmask;
115 if (wb != 0 && __copy_from_user(regs->areg + XCHAL_NUM_AREGS - wb * 4,
116 gregset->a, wb * 16))
117 return -EFAULT;
119 if (__copy_from_user(regs->areg, gregset->a + wb * 4,
120 (WSBITS - wb) * 16))
121 return -EFAULT;
123 return 0;
127 int ptrace_getxregs(struct task_struct *child, void __user *uregs)
129 struct pt_regs *regs = task_pt_regs(child);
130 struct thread_info *ti = task_thread_info(child);
131 elf_xtregs_t __user *xtregs = uregs;
132 int ret = 0;
134 if (!access_ok(VERIFY_WRITE, uregs, sizeof(elf_xtregs_t)))
135 return -EIO;
137 #if XTENSA_HAVE_COPROCESSORS
138 /* Flush all coprocessor registers to memory. */
139 coprocessor_flush_all(ti);
140 ret |= __copy_to_user(&xtregs->cp0, &ti->xtregs_cp,
141 sizeof(xtregs_coprocessor_t));
142 #endif
143 ret |= __copy_to_user(&xtregs->opt, &regs->xtregs_opt,
144 sizeof(xtregs->opt));
145 ret |= __copy_to_user(&xtregs->user,&ti->xtregs_user,
146 sizeof(xtregs->user));
148 return ret ? -EFAULT : 0;
151 int ptrace_setxregs(struct task_struct *child, void __user *uregs)
153 struct thread_info *ti = task_thread_info(child);
154 struct pt_regs *regs = task_pt_regs(child);
155 elf_xtregs_t *xtregs = uregs;
156 int ret = 0;
158 if (!access_ok(VERIFY_READ, uregs, sizeof(elf_xtregs_t)))
159 return -EFAULT;
161 #if XTENSA_HAVE_COPROCESSORS
162 /* Flush all coprocessors before we overwrite them. */
163 coprocessor_flush_all(ti);
164 coprocessor_release_all(ti);
166 ret |= __copy_from_user(&ti->xtregs_cp, &xtregs->cp0,
167 sizeof(xtregs_coprocessor_t));
168 #endif
169 ret |= __copy_from_user(&regs->xtregs_opt, &xtregs->opt,
170 sizeof(xtregs->opt));
171 ret |= __copy_from_user(&ti->xtregs_user, &xtregs->user,
172 sizeof(xtregs->user));
174 return ret ? -EFAULT : 0;
177 int ptrace_peekusr(struct task_struct *child, long regno, long __user *ret)
179 struct pt_regs *regs;
180 unsigned long tmp;
182 regs = task_pt_regs(child);
183 tmp = 0; /* Default return value. */
185 switch(regno) {
187 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
188 tmp = regs->areg[regno - REG_AR_BASE];
189 break;
191 case REG_A_BASE ... REG_A_BASE + 15:
192 tmp = regs->areg[regno - REG_A_BASE];
193 break;
195 case REG_PC:
196 tmp = regs->pc;
197 break;
199 case REG_PS:
200 /* Note: PS.EXCM is not set while user task is running;
201 * its being set in regs is for exception handling
202 * convenience. */
203 tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
204 break;
206 case REG_WB:
207 break; /* tmp = 0 */
209 case REG_WS:
211 unsigned long wb = regs->windowbase;
212 unsigned long ws = regs->windowstart;
213 tmp = ((ws>>wb) | (ws<<(WSBITS-wb))) & ((1<<WSBITS)-1);
214 break;
216 case REG_LBEG:
217 tmp = regs->lbeg;
218 break;
220 case REG_LEND:
221 tmp = regs->lend;
222 break;
224 case REG_LCOUNT:
225 tmp = regs->lcount;
226 break;
228 case REG_SAR:
229 tmp = regs->sar;
230 break;
232 case SYSCALL_NR:
233 tmp = regs->syscall;
234 break;
236 default:
237 return -EIO;
239 return put_user(tmp, ret);
242 int ptrace_pokeusr(struct task_struct *child, long regno, long val)
244 struct pt_regs *regs;
245 regs = task_pt_regs(child);
247 switch (regno) {
248 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
249 regs->areg[regno - REG_AR_BASE] = val;
250 break;
252 case REG_A_BASE ... REG_A_BASE + 15:
253 regs->areg[regno - REG_A_BASE] = val;
254 break;
256 case REG_PC:
257 regs->pc = val;
258 break;
260 case SYSCALL_NR:
261 regs->syscall = val;
262 break;
264 default:
265 return -EIO;
267 return 0;
270 long arch_ptrace(struct task_struct *child, long request,
271 unsigned long addr, unsigned long data)
273 int ret = -EPERM;
274 void __user *datap = (void __user *) data;
276 switch (request) {
277 case PTRACE_PEEKTEXT: /* read word at location addr. */
278 case PTRACE_PEEKDATA:
279 ret = generic_ptrace_peekdata(child, addr, data);
280 break;
282 case PTRACE_PEEKUSR: /* read register specified by addr. */
283 ret = ptrace_peekusr(child, addr, datap);
284 break;
286 case PTRACE_POKETEXT: /* write the word at location addr. */
287 case PTRACE_POKEDATA:
288 ret = generic_ptrace_pokedata(child, addr, data);
289 break;
291 case PTRACE_POKEUSR: /* write register specified by addr. */
292 ret = ptrace_pokeusr(child, addr, data);
293 break;
295 case PTRACE_GETREGS:
296 ret = ptrace_getregs(child, datap);
297 break;
299 case PTRACE_SETREGS:
300 ret = ptrace_setregs(child, datap);
301 break;
303 case PTRACE_GETXTREGS:
304 ret = ptrace_getxregs(child, datap);
305 break;
307 case PTRACE_SETXTREGS:
308 ret = ptrace_setxregs(child, datap);
309 break;
311 default:
312 ret = ptrace_request(child, request, addr, data);
313 break;
316 return ret;
319 void do_syscall_trace(void)
322 * The 0x80 provides a way for the tracing parent to distinguish
323 * between a syscall stop and SIGTRAP delivery
325 ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
328 * this isn't the same as continuing with a signal, but it will do
329 * for normal use. strace only continues with a signal if the
330 * stopping signal is not SIGTRAP. -brl
332 if (current->exit_code) {
333 send_sig(current->exit_code, current, 1);
334 current->exit_code = 0;
338 void do_syscall_trace_enter(struct pt_regs *regs)
340 if (test_thread_flag(TIF_SYSCALL_TRACE)
341 && (current->ptrace & PT_PTRACED))
342 do_syscall_trace();
344 #if 0
345 audit_syscall_entry(...);
346 #endif
349 void do_syscall_trace_leave(struct pt_regs *regs)
351 if ((test_thread_flag(TIF_SYSCALL_TRACE))
352 && (current->ptrace & PT_PTRACED))
353 do_syscall_trace();