ptrace.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. /*
  2. * linux/arch/arm/kernel/ptrace.c
  3. *
  4. * By Ross Biro 1/23/92
  5. * edited by Linus Torvalds
  6. * ARM modifications Copyright (C) 2000 Russell King
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/smp.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/user.h>
  18. #include <linux/security.h>
  19. #include <linux/init.h>
  20. #include <linux/signal.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/perf_event.h>
  23. #include <linux/hw_breakpoint.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/system.h>
  26. #include <asm/traps.h>
  27. #include "ptrace.h"
  28. #define REG_PC 15
  29. #define REG_PSR 16
  30. /*
  31. * does not yet catch signals sent when the child dies.
  32. * in exit.c or in signal.c.
  33. */
  34. #if 0
  35. /*
  36. * Breakpoint SWI instruction: SWI &9F0001
  37. */
  38. #define BREAKINST_ARM 0xef9f0001
  39. #define BREAKINST_THUMB 0xdf00 /* fill this in later */
  40. #else
  41. /*
  42. * New breakpoints - use an undefined instruction. The ARM architecture
  43. * reference manual guarantees that the following instruction space
  44. * will produce an undefined instruction exception on all CPUs:
  45. *
  46. * ARM: xxxx 0111 1111 xxxx xxxx xxxx 1111 xxxx
  47. * Thumb: 1101 1110 xxxx xxxx
  48. */
  49. #define BREAKINST_ARM 0xe7f001f0
  50. #define BREAKINST_THUMB 0xde01
  51. #endif
  52. struct pt_regs_offset {
  53. const char *name;
  54. int offset;
  55. };
  56. #define REG_OFFSET_NAME(r) \
  57. {.name = #r, .offset = offsetof(struct pt_regs, ARM_##r)}
  58. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  59. static const struct pt_regs_offset regoffset_table[] = {
  60. REG_OFFSET_NAME(r0),
  61. REG_OFFSET_NAME(r1),
  62. REG_OFFSET_NAME(r2),
  63. REG_OFFSET_NAME(r3),
  64. REG_OFFSET_NAME(r4),
  65. REG_OFFSET_NAME(r5),
  66. REG_OFFSET_NAME(r6),
  67. REG_OFFSET_NAME(r7),
  68. REG_OFFSET_NAME(r8),
  69. REG_OFFSET_NAME(r9),
  70. REG_OFFSET_NAME(r10),
  71. REG_OFFSET_NAME(fp),
  72. REG_OFFSET_NAME(ip),
  73. REG_OFFSET_NAME(sp),
  74. REG_OFFSET_NAME(lr),
  75. REG_OFFSET_NAME(pc),
  76. REG_OFFSET_NAME(cpsr),
  77. REG_OFFSET_NAME(ORIG_r0),
  78. REG_OFFSET_END,
  79. };
  80. /**
  81. * regs_query_register_offset() - query register offset from its name
  82. * @name: the name of a register
  83. *
  84. * regs_query_register_offset() returns the offset of a register in struct
  85. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  86. */
  87. int regs_query_register_offset(const char *name)
  88. {
  89. const struct pt_regs_offset *roff;
  90. for (roff = regoffset_table; roff->name != NULL; roff++)
  91. if (!strcmp(roff->name, name))
  92. return roff->offset;
  93. return -EINVAL;
  94. }
  95. /**
  96. * regs_query_register_name() - query register name from its offset
  97. * @offset: the offset of a register in struct pt_regs.
  98. *
  99. * regs_query_register_name() returns the name of a register from its
  100. * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
  101. */
  102. const char *regs_query_register_name(unsigned int offset)
  103. {
  104. const struct pt_regs_offset *roff;
  105. for (roff = regoffset_table; roff->name != NULL; roff++)
  106. if (roff->offset == offset)
  107. return roff->name;
  108. return NULL;
  109. }
  110. /**
  111. * regs_within_kernel_stack() - check the address in the stack
  112. * @regs: pt_regs which contains kernel stack pointer.
  113. * @addr: address which is checked.
  114. *
  115. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  116. * If @addr is within the kernel stack, it returns true. If not, returns false.
  117. */
  118. bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  119. {
  120. return ((addr & ~(THREAD_SIZE - 1)) ==
  121. (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
  122. }
  123. /**
  124. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  125. * @regs: pt_regs which contains kernel stack pointer.
  126. * @n: stack entry number.
  127. *
  128. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  129. * is specified by @regs. If the @n th entry is NOT in the kernel stack,
  130. * this returns 0.
  131. */
  132. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  133. {
  134. unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  135. addr += n;
  136. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  137. return *addr;
  138. else
  139. return 0;
  140. }
  141. /*
  142. * this routine will get a word off of the processes privileged stack.
  143. * the offset is how far from the base addr as stored in the THREAD.
  144. * this routine assumes that all the privileged stacks are in our
  145. * data space.
  146. */
  147. static inline long get_user_reg(struct task_struct *task, int offset)
  148. {
  149. return task_pt_regs(task)->uregs[offset];
  150. }
  151. /*
  152. * this routine will put a word on the processes privileged stack.
  153. * the offset is how far from the base addr as stored in the THREAD.
  154. * this routine assumes that all the privileged stacks are in our
  155. * data space.
  156. */
  157. static inline int
  158. put_user_reg(struct task_struct *task, int offset, long data)
  159. {
  160. struct pt_regs newregs, *regs = task_pt_regs(task);
  161. int ret = -EINVAL;
  162. newregs = *regs;
  163. newregs.uregs[offset] = data;
  164. if (valid_user_regs(&newregs)) {
  165. regs->uregs[offset] = data;
  166. ret = 0;
  167. }
  168. return ret;
  169. }
  170. static inline int
  171. read_u32(struct task_struct *task, unsigned long addr, u32 *res)
  172. {
  173. int ret;
  174. ret = access_process_vm(task, addr, res, sizeof(*res), 0);
  175. return ret == sizeof(*res) ? 0 : -EIO;
  176. }
  177. static inline int
  178. read_instr(struct task_struct *task, unsigned long addr, u32 *res)
  179. {
  180. int ret;
  181. if (addr & 1) {
  182. u16 val;
  183. ret = access_process_vm(task, addr & ~1, &val, sizeof(val), 0);
  184. ret = ret == sizeof(val) ? 0 : -EIO;
  185. *res = val;
  186. } else {
  187. u32 val;
  188. ret = access_process_vm(task, addr & ~3, &val, sizeof(val), 0);
  189. ret = ret == sizeof(val) ? 0 : -EIO;
  190. *res = val;
  191. }
  192. return ret;
  193. }
  194. /*
  195. * Get value of register `rn' (in the instruction)
  196. */
  197. static unsigned long
  198. ptrace_getrn(struct task_struct *child, unsigned long insn)
  199. {
  200. unsigned int reg = (insn >> 16) & 15;
  201. unsigned long val;
  202. val = get_user_reg(child, reg);
  203. if (reg == 15)
  204. val += 8;
  205. return val;
  206. }
  207. /*
  208. * Get value of operand 2 (in an ALU instruction)
  209. */
  210. static unsigned long
  211. ptrace_getaluop2(struct task_struct *child, unsigned long insn)
  212. {
  213. unsigned long val;
  214. int shift;
  215. int type;
  216. if (insn & 1 << 25) {
  217. val = insn & 255;
  218. shift = (insn >> 8) & 15;
  219. type = 3;
  220. } else {
  221. val = get_user_reg (child, insn & 15);
  222. if (insn & (1 << 4))
  223. shift = (int)get_user_reg (child, (insn >> 8) & 15);
  224. else
  225. shift = (insn >> 7) & 31;
  226. type = (insn >> 5) & 3;
  227. }
  228. switch (type) {
  229. case 0: val <<= shift; break;
  230. case 1: val >>= shift; break;
  231. case 2:
  232. val = (((signed long)val) >> shift);
  233. break;
  234. case 3:
  235. val = (val >> shift) | (val << (32 - shift));
  236. break;
  237. }
  238. return val;
  239. }
  240. /*
  241. * Get value of operand 2 (in a LDR instruction)
  242. */
  243. static unsigned long
  244. ptrace_getldrop2(struct task_struct *child, unsigned long insn)
  245. {
  246. unsigned long val;
  247. int shift;
  248. int type;
  249. val = get_user_reg(child, insn & 15);
  250. shift = (insn >> 7) & 31;
  251. type = (insn >> 5) & 3;
  252. switch (type) {
  253. case 0: val <<= shift; break;
  254. case 1: val >>= shift; break;
  255. case 2:
  256. val = (((signed long)val) >> shift);
  257. break;
  258. case 3:
  259. val = (val >> shift) | (val << (32 - shift));
  260. break;
  261. }
  262. return val;
  263. }
  264. #define OP_MASK 0x01e00000
  265. #define OP_AND 0x00000000
  266. #define OP_EOR 0x00200000
  267. #define OP_SUB 0x00400000
  268. #define OP_RSB 0x00600000
  269. #define OP_ADD 0x00800000
  270. #define OP_ADC 0x00a00000
  271. #define OP_SBC 0x00c00000
  272. #define OP_RSC 0x00e00000
  273. #define OP_ORR 0x01800000
  274. #define OP_MOV 0x01a00000
  275. #define OP_BIC 0x01c00000
  276. #define OP_MVN 0x01e00000
  277. static unsigned long
  278. get_branch_address(struct task_struct *child, unsigned long pc, unsigned long insn)
  279. {
  280. u32 alt = 0;
  281. switch (insn & 0x0e000000) {
  282. case 0x00000000:
  283. case 0x02000000: {
  284. /*
  285. * data processing
  286. */
  287. long aluop1, aluop2, ccbit;
  288. if ((insn & 0x0fffffd0) == 0x012fff10) {
  289. /*
  290. * bx or blx
  291. */
  292. alt = get_user_reg(child, insn & 15);
  293. break;
  294. }
  295. if ((insn & 0xf000) != 0xf000)
  296. break;
  297. aluop1 = ptrace_getrn(child, insn);
  298. aluop2 = ptrace_getaluop2(child, insn);
  299. ccbit = get_user_reg(child, REG_PSR) & PSR_C_BIT ? 1 : 0;
  300. switch (insn & OP_MASK) {
  301. case OP_AND: alt = aluop1 & aluop2; break;
  302. case OP_EOR: alt = aluop1 ^ aluop2; break;
  303. case OP_SUB: alt = aluop1 - aluop2; break;
  304. case OP_RSB: alt = aluop2 - aluop1; break;
  305. case OP_ADD: alt = aluop1 + aluop2; break;
  306. case OP_ADC: alt = aluop1 + aluop2 + ccbit; break;
  307. case OP_SBC: alt = aluop1 - aluop2 + ccbit; break;
  308. case OP_RSC: alt = aluop2 - aluop1 + ccbit; break;
  309. case OP_ORR: alt = aluop1 | aluop2; break;
  310. case OP_MOV: alt = aluop2; break;
  311. case OP_BIC: alt = aluop1 & ~aluop2; break;
  312. case OP_MVN: alt = ~aluop2; break;
  313. }
  314. break;
  315. }
  316. case 0x04000000:
  317. case 0x06000000:
  318. /*
  319. * ldr
  320. */
  321. if ((insn & 0x0010f000) == 0x0010f000) {
  322. unsigned long base;
  323. base = ptrace_getrn(child, insn);
  324. if (insn & 1 << 24) {
  325. long aluop2;
  326. if (insn & 0x02000000)
  327. aluop2 = ptrace_getldrop2(child, insn);
  328. else
  329. aluop2 = insn & 0xfff;
  330. if (insn & 1 << 23)
  331. base += aluop2;
  332. else
  333. base -= aluop2;
  334. }
  335. read_u32(child, base, &alt);
  336. }
  337. break;
  338. case 0x08000000:
  339. /*
  340. * ldm
  341. */
  342. if ((insn & 0x00108000) == 0x00108000) {
  343. unsigned long base;
  344. unsigned int nr_regs;
  345. if (insn & (1 << 23)) {
  346. nr_regs = hweight16(insn & 65535) << 2;
  347. if (!(insn & (1 << 24)))
  348. nr_regs -= 4;
  349. } else {
  350. if (insn & (1 << 24))
  351. nr_regs = -4;
  352. else
  353. nr_regs = 0;
  354. }
  355. base = ptrace_getrn(child, insn);
  356. read_u32(child, base + nr_regs, &alt);
  357. break;
  358. }
  359. break;
  360. case 0x0a000000: {
  361. /*
  362. * bl or b
  363. */
  364. signed long displ;
  365. /* It's a branch/branch link: instead of trying to
  366. * figure out whether the branch will be taken or not,
  367. * we'll put a breakpoint at both locations. This is
  368. * simpler, more reliable, and probably not a whole lot
  369. * slower than the alternative approach of emulating the
  370. * branch.
  371. */
  372. displ = (insn & 0x00ffffff) << 8;
  373. displ = (displ >> 6) + 8;
  374. if (displ != 0 && displ != 4)
  375. alt = pc + displ;
  376. }
  377. break;
  378. }
  379. return alt;
  380. }
  381. static int
  382. swap_insn(struct task_struct *task, unsigned long addr,
  383. void *old_insn, void *new_insn, int size)
  384. {
  385. int ret;
  386. ret = access_process_vm(task, addr, old_insn, size, 0);
  387. if (ret == size)
  388. ret = access_process_vm(task, addr, new_insn, size, 1);
  389. return ret;
  390. }
  391. static void
  392. add_breakpoint(struct task_struct *task, struct debug_info *dbg, unsigned long addr)
  393. {
  394. int nr = dbg->nsaved;
  395. if (nr < 2) {
  396. u32 new_insn = BREAKINST_ARM;
  397. int res;
  398. res = swap_insn(task, addr, &dbg->bp[nr].insn, &new_insn, 4);
  399. if (res == 4) {
  400. dbg->bp[nr].address = addr;
  401. dbg->nsaved += 1;
  402. }
  403. } else
  404. printk(KERN_ERR "ptrace: too many breakpoints\n");
  405. }
  406. /*
  407. * Clear one breakpoint in the user program. We copy what the hardware
  408. * does and use bit 0 of the address to indicate whether this is a Thumb
  409. * breakpoint or an ARM breakpoint.
  410. */
  411. static void clear_breakpoint(struct task_struct *task, struct debug_entry *bp)
  412. {
  413. unsigned long addr = bp->address;
  414. union debug_insn old_insn;
  415. int ret;
  416. if (addr & 1) {
  417. ret = swap_insn(task, addr & ~1, &old_insn.thumb,
  418. &bp->insn.thumb, 2);
  419. if (ret != 2 || old_insn.thumb != BREAKINST_THUMB)
  420. printk(KERN_ERR "%s:%d: corrupted Thumb breakpoint at "
  421. "0x%08lx (0x%04x)\n", task->comm,
  422. task_pid_nr(task), addr, old_insn.thumb);
  423. } else {
  424. ret = swap_insn(task, addr & ~3, &old_insn.arm,
  425. &bp->insn.arm, 4);
  426. if (ret != 4 || old_insn.arm != BREAKINST_ARM)
  427. printk(KERN_ERR "%s:%d: corrupted ARM breakpoint at "
  428. "0x%08lx (0x%08x)\n", task->comm,
  429. task_pid_nr(task), addr, old_insn.arm);
  430. }
  431. }
  432. void ptrace_set_bpt(struct task_struct *child)
  433. {
  434. struct pt_regs *regs;
  435. unsigned long pc;
  436. u32 insn;
  437. int res;
  438. regs = task_pt_regs(child);
  439. pc = instruction_pointer(regs);
  440. if (thumb_mode(regs)) {
  441. printk(KERN_WARNING "ptrace: can't handle thumb mode\n");
  442. return;
  443. }
  444. res = read_instr(child, pc, &insn);
  445. if (!res) {
  446. struct debug_info *dbg = &child->thread.debug;
  447. unsigned long alt;
  448. dbg->nsaved = 0;
  449. alt = get_branch_address(child, pc, insn);
  450. if (alt)
  451. add_breakpoint(child, dbg, alt);
  452. /*
  453. * Note that we ignore the result of setting the above
  454. * breakpoint since it may fail. When it does, this is
  455. * not so much an error, but a forewarning that we may
  456. * be receiving a prefetch abort shortly.
  457. *
  458. * If we don't set this breakpoint here, then we can
  459. * lose control of the thread during single stepping.
  460. */
  461. if (!alt || predicate(insn) != PREDICATE_ALWAYS)
  462. add_breakpoint(child, dbg, pc + 4);
  463. }
  464. }
  465. /*
  466. * Ensure no single-step breakpoint is pending. Returns non-zero
  467. * value if child was being single-stepped.
  468. */
  469. void ptrace_cancel_bpt(struct task_struct *child)
  470. {
  471. int i, nsaved = child->thread.debug.nsaved;
  472. child->thread.debug.nsaved = 0;
  473. if (nsaved > 2) {
  474. printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
  475. nsaved = 2;
  476. }
  477. for (i = 0; i < nsaved; i++)
  478. clear_breakpoint(child, &child->thread.debug.bp[i]);
  479. }
  480. void user_disable_single_step(struct task_struct *task)
  481. {
  482. task->ptrace &= ~PT_SINGLESTEP;
  483. ptrace_cancel_bpt(task);
  484. }
  485. void user_enable_single_step(struct task_struct *task)
  486. {
  487. task->ptrace |= PT_SINGLESTEP;
  488. }
  489. /*
  490. * Called by kernel/ptrace.c when detaching..
  491. */
  492. void ptrace_disable(struct task_struct *child)
  493. {
  494. user_disable_single_step(child);
  495. }
  496. /*
  497. * Handle hitting a breakpoint.
  498. */
  499. void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
  500. {
  501. siginfo_t info;
  502. ptrace_cancel_bpt(tsk);
  503. info.si_signo = SIGTRAP;
  504. info.si_errno = 0;
  505. info.si_code = TRAP_BRKPT;
  506. info.si_addr = (void __user *)instruction_pointer(regs);
  507. force_sig_info(SIGTRAP, &info, tsk);
  508. }
  509. static int break_trap(struct pt_regs *regs, unsigned int instr)
  510. {
  511. ptrace_break(current, regs);
  512. return 0;
  513. }
  514. static struct undef_hook arm_break_hook = {
  515. .instr_mask = 0x0fffffff,
  516. .instr_val = 0x07f001f0,
  517. .cpsr_mask = PSR_T_BIT,
  518. .cpsr_val = 0,
  519. .fn = break_trap,
  520. };
  521. static struct undef_hook thumb_break_hook = {
  522. .instr_mask = 0xffff,
  523. .instr_val = 0xde01,
  524. .cpsr_mask = PSR_T_BIT,
  525. .cpsr_val = PSR_T_BIT,
  526. .fn = break_trap,
  527. };
  528. static int thumb2_break_trap(struct pt_regs *regs, unsigned int instr)
  529. {
  530. unsigned int instr2;
  531. void __user *pc;
  532. /* Check the second half of the instruction. */
  533. pc = (void __user *)(instruction_pointer(regs) + 2);
  534. if (processor_mode(regs) == SVC_MODE) {
  535. instr2 = *(u16 *) pc;
  536. } else {
  537. get_user(instr2, (u16 __user *)pc);
  538. }
  539. if (instr2 == 0xa000) {
  540. ptrace_break(current, regs);
  541. return 0;
  542. } else {
  543. return 1;
  544. }
  545. }
  546. static struct undef_hook thumb2_break_hook = {
  547. .instr_mask = 0xffff,
  548. .instr_val = 0xf7f0,
  549. .cpsr_mask = PSR_T_BIT,
  550. .cpsr_val = PSR_T_BIT,
  551. .fn = thumb2_break_trap,
  552. };
  553. static int __init ptrace_break_init(void)
  554. {
  555. register_undef_hook(&arm_break_hook);
  556. register_undef_hook(&thumb_break_hook);
  557. register_undef_hook(&thumb2_break_hook);
  558. return 0;
  559. }
  560. core_initcall(ptrace_break_init);
  561. /*
  562. * Read the word at offset "off" into the "struct user". We
  563. * actually access the pt_regs stored on the kernel stack.
  564. */
  565. static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
  566. unsigned long __user *ret)
  567. {
  568. unsigned long tmp;
  569. if (off & 3 || off >= sizeof(struct user))
  570. return -EIO;
  571. tmp = 0;
  572. if (off == PT_TEXT_ADDR)
  573. tmp = tsk->mm->start_code;
  574. else if (off == PT_DATA_ADDR)
  575. tmp = tsk->mm->start_data;
  576. else if (off == PT_TEXT_END_ADDR)
  577. tmp = tsk->mm->end_code;
  578. else if (off < sizeof(struct pt_regs))
  579. tmp = get_user_reg(tsk, off >> 2);
  580. return put_user(tmp, ret);
  581. }
  582. /*
  583. * Write the word at offset "off" into "struct user". We
  584. * actually access the pt_regs stored on the kernel stack.
  585. */
  586. static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
  587. unsigned long val)
  588. {
  589. if (off & 3 || off >= sizeof(struct user))
  590. return -EIO;
  591. if (off >= sizeof(struct pt_regs))
  592. return 0;
  593. return put_user_reg(tsk, off >> 2, val);
  594. }
  595. /*
  596. * Get all user integer registers.
  597. */
  598. static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
  599. {
  600. struct pt_regs *regs = task_pt_regs(tsk);
  601. return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
  602. }
  603. /*
  604. * Set all user integer registers.
  605. */
  606. static int ptrace_setregs(struct task_struct *tsk, void __user *uregs)
  607. {
  608. struct pt_regs newregs;
  609. int ret;
  610. ret = -EFAULT;
  611. if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
  612. struct pt_regs *regs = task_pt_regs(tsk);
  613. ret = -EINVAL;
  614. if (valid_user_regs(&newregs)) {
  615. *regs = newregs;
  616. ret = 0;
  617. }
  618. }
  619. return ret;
  620. }
  621. /*
  622. * Get the child FPU state.
  623. */
  624. static int ptrace_getfpregs(struct task_struct *tsk, void __user *ufp)
  625. {
  626. return copy_to_user(ufp, &task_thread_info(tsk)->fpstate,
  627. sizeof(struct user_fp)) ? -EFAULT : 0;
  628. }
  629. /*
  630. * Set the child FPU state.
  631. */
  632. static int ptrace_setfpregs(struct task_struct *tsk, void __user *ufp)
  633. {
  634. struct thread_info *thread = task_thread_info(tsk);
  635. thread->used_cp[1] = thread->used_cp[2] = 1;
  636. return copy_from_user(&thread->fpstate, ufp,
  637. sizeof(struct user_fp)) ? -EFAULT : 0;
  638. }
  639. #ifdef CONFIG_IWMMXT
  640. /*
  641. * Get the child iWMMXt state.
  642. */
  643. static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
  644. {
  645. struct thread_info *thread = task_thread_info(tsk);
  646. if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
  647. return -ENODATA;
  648. iwmmxt_task_disable(thread); /* force it to ram */
  649. return copy_to_user(ufp, &thread->fpstate.iwmmxt, IWMMXT_SIZE)
  650. ? -EFAULT : 0;
  651. }
  652. /*
  653. * Set the child iWMMXt state.
  654. */
  655. static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
  656. {
  657. struct thread_info *thread = task_thread_info(tsk);
  658. if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
  659. return -EACCES;
  660. iwmmxt_task_release(thread); /* force a reload */
  661. return copy_from_user(&thread->fpstate.iwmmxt, ufp, IWMMXT_SIZE)
  662. ? -EFAULT : 0;
  663. }
  664. #endif
  665. #ifdef CONFIG_CRUNCH
  666. /*
  667. * Get the child Crunch state.
  668. */
  669. static int ptrace_getcrunchregs(struct task_struct *tsk, void __user *ufp)
  670. {
  671. struct thread_info *thread = task_thread_info(tsk);
  672. crunch_task_disable(thread); /* force it to ram */
  673. return copy_to_user(ufp, &thread->crunchstate, CRUNCH_SIZE)
  674. ? -EFAULT : 0;
  675. }
  676. /*
  677. * Set the child Crunch state.
  678. */
  679. static int ptrace_setcrunchregs(struct task_struct *tsk, void __user *ufp)
  680. {
  681. struct thread_info *thread = task_thread_info(tsk);
  682. crunch_task_release(thread); /* force a reload */
  683. return copy_from_user(&thread->crunchstate, ufp, CRUNCH_SIZE)
  684. ? -EFAULT : 0;
  685. }
  686. #endif
  687. #ifdef CONFIG_VFP
  688. /*
  689. * Get the child VFP state.
  690. */
  691. static int ptrace_getvfpregs(struct task_struct *tsk, void __user *data)
  692. {
  693. struct thread_info *thread = task_thread_info(tsk);
  694. union vfp_state *vfp = &thread->vfpstate;
  695. struct user_vfp __user *ufp = data;
  696. vfp_sync_hwstate(thread);
  697. /* copy the floating point registers */
  698. if (copy_to_user(&ufp->fpregs, &vfp->hard.fpregs,
  699. sizeof(vfp->hard.fpregs)))
  700. return -EFAULT;
  701. /* copy the status and control register */
  702. if (put_user(vfp->hard.fpscr, &ufp->fpscr))
  703. return -EFAULT;
  704. return 0;
  705. }
  706. /*
  707. * Set the child VFP state.
  708. */
  709. static int ptrace_setvfpregs(struct task_struct *tsk, void __user *data)
  710. {
  711. struct thread_info *thread = task_thread_info(tsk);
  712. union vfp_state *vfp = &thread->vfpstate;
  713. struct user_vfp __user *ufp = data;
  714. vfp_sync_hwstate(thread);
  715. /* copy the floating point registers */
  716. if (copy_from_user(&vfp->hard.fpregs, &ufp->fpregs,
  717. sizeof(vfp->hard.fpregs)))
  718. return -EFAULT;
  719. /* copy the status and control register */
  720. if (get_user(vfp->hard.fpscr, &ufp->fpscr))
  721. return -EFAULT;
  722. vfp_flush_hwstate(thread);
  723. return 0;
  724. }
  725. #endif
  726. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  727. /*
  728. * Convert a virtual register number into an index for a thread_info
  729. * breakpoint array. Breakpoints are identified using positive numbers
  730. * whilst watchpoints are negative. The registers are laid out as pairs
  731. * of (address, control), each pair mapping to a unique hw_breakpoint struct.
  732. * Register 0 is reserved for describing resource information.
  733. */
  734. static int ptrace_hbp_num_to_idx(long num)
  735. {
  736. if (num < 0)
  737. num = (ARM_MAX_BRP << 1) - num;
  738. return (num - 1) >> 1;
  739. }
  740. /*
  741. * Returns the virtual register number for the address of the
  742. * breakpoint at index idx.
  743. */
  744. static long ptrace_hbp_idx_to_num(int idx)
  745. {
  746. long mid = ARM_MAX_BRP << 1;
  747. long num = (idx << 1) + 1;
  748. return num > mid ? mid - num : num;
  749. }
  750. /*
  751. * Handle hitting a HW-breakpoint.
  752. */
  753. static void ptrace_hbptriggered(struct perf_event *bp, int unused,
  754. struct perf_sample_data *data,
  755. struct pt_regs *regs)
  756. {
  757. struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
  758. long num;
  759. int i;
  760. siginfo_t info;
  761. for (i = 0; i < ARM_MAX_HBP_SLOTS; ++i)
  762. if (current->thread.debug.hbp[i] == bp)
  763. break;
  764. num = (i == ARM_MAX_HBP_SLOTS) ? 0 : ptrace_hbp_idx_to_num(i);
  765. info.si_signo = SIGTRAP;
  766. info.si_errno = (int)num;
  767. info.si_code = TRAP_HWBKPT;
  768. info.si_addr = (void __user *)(bkpt->trigger);
  769. force_sig_info(SIGTRAP, &info, current);
  770. }
  771. /*
  772. * Set ptrace breakpoint pointers to zero for this task.
  773. * This is required in order to prevent child processes from unregistering
  774. * breakpoints held by their parent.
  775. */
  776. void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
  777. {
  778. memset(tsk->thread.debug.hbp, 0, sizeof(tsk->thread.debug.hbp));
  779. }
  780. /*
  781. * Unregister breakpoints from this task and reset the pointers in
  782. * the thread_struct.
  783. */
  784. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  785. {
  786. int i;
  787. struct thread_struct *t = &tsk->thread;
  788. for (i = 0; i < ARM_MAX_HBP_SLOTS; i++) {
  789. if (t->debug.hbp[i]) {
  790. unregister_hw_breakpoint(t->debug.hbp[i]);
  791. t->debug.hbp[i] = NULL;
  792. }
  793. }
  794. }
  795. static u32 ptrace_get_hbp_resource_info(void)
  796. {
  797. u8 num_brps, num_wrps, debug_arch, wp_len;
  798. u32 reg = 0;
  799. num_brps = hw_breakpoint_slots(TYPE_INST);
  800. num_wrps = hw_breakpoint_slots(TYPE_DATA);
  801. debug_arch = arch_get_debug_arch();
  802. wp_len = arch_get_max_wp_len();
  803. reg |= debug_arch;
  804. reg <<= 8;
  805. reg |= wp_len;
  806. reg <<= 8;
  807. reg |= num_wrps;
  808. reg <<= 8;
  809. reg |= num_brps;
  810. return reg;
  811. }
  812. static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type)
  813. {
  814. struct perf_event_attr attr;
  815. ptrace_breakpoint_init(&attr);
  816. /* Initialise fields to sane defaults. */
  817. attr.bp_addr = 0;
  818. attr.bp_len = HW_BREAKPOINT_LEN_4;
  819. attr.bp_type = type;
  820. attr.disabled = 1;
  821. return register_user_hw_breakpoint(&attr, ptrace_hbptriggered, tsk);
  822. }
  823. static int ptrace_gethbpregs(struct task_struct *tsk, long num,
  824. unsigned long __user *data)
  825. {
  826. u32 reg;
  827. int idx, ret = 0;
  828. struct perf_event *bp;
  829. struct arch_hw_breakpoint_ctrl arch_ctrl;
  830. if (num == 0) {
  831. reg = ptrace_get_hbp_resource_info();
  832. } else {
  833. idx = ptrace_hbp_num_to_idx(num);
  834. if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
  835. ret = -EINVAL;
  836. goto out;
  837. }
  838. bp = tsk->thread.debug.hbp[idx];
  839. if (!bp) {
  840. reg = 0;
  841. goto put;
  842. }
  843. arch_ctrl = counter_arch_bp(bp)->ctrl;
  844. /*
  845. * Fix up the len because we may have adjusted it
  846. * to compensate for an unaligned address.
  847. */
  848. while (!(arch_ctrl.len & 0x1))
  849. arch_ctrl.len >>= 1;
  850. if (idx & 0x1)
  851. reg = encode_ctrl_reg(arch_ctrl);
  852. else
  853. reg = bp->attr.bp_addr;
  854. }
  855. put:
  856. if (put_user(reg, data))
  857. ret = -EFAULT;
  858. out:
  859. return ret;
  860. }
  861. static int ptrace_sethbpregs(struct task_struct *tsk, long num,
  862. unsigned long __user *data)
  863. {
  864. int idx, gen_len, gen_type, implied_type, ret = 0;
  865. u32 user_val;
  866. struct perf_event *bp;
  867. struct arch_hw_breakpoint_ctrl ctrl;
  868. struct perf_event_attr attr;
  869. if (num == 0)
  870. goto out;
  871. else if (num < 0)
  872. implied_type = HW_BREAKPOINT_RW;
  873. else
  874. implied_type = HW_BREAKPOINT_X;
  875. idx = ptrace_hbp_num_to_idx(num);
  876. if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
  877. ret = -EINVAL;
  878. goto out;
  879. }
  880. if (get_user(user_val, data)) {
  881. ret = -EFAULT;
  882. goto out;
  883. }
  884. bp = tsk->thread.debug.hbp[idx];
  885. if (!bp) {
  886. bp = ptrace_hbp_create(tsk, implied_type);
  887. if (IS_ERR(bp)) {
  888. ret = PTR_ERR(bp);
  889. goto out;
  890. }
  891. tsk->thread.debug.hbp[idx] = bp;
  892. }
  893. attr = bp->attr;
  894. if (num & 0x1) {
  895. /* Address */
  896. attr.bp_addr = user_val;
  897. } else {
  898. /* Control */
  899. decode_ctrl_reg(user_val, &ctrl);
  900. ret = arch_bp_generic_fields(ctrl, &gen_len, &gen_type);
  901. if (ret)
  902. goto out;
  903. if ((gen_type & implied_type) != gen_type) {
  904. ret = -EINVAL;
  905. goto out;
  906. }
  907. attr.bp_len = gen_len;
  908. attr.bp_type = gen_type;
  909. attr.disabled = !ctrl.enabled;
  910. }
  911. ret = modify_user_hw_breakpoint(bp, &attr);
  912. out:
  913. return ret;
  914. }
  915. #endif
  916. long arch_ptrace(struct task_struct *child, long request,
  917. unsigned long addr, unsigned long data)
  918. {
  919. int ret;
  920. unsigned long __user *datap = (unsigned long __user *) data;
  921. switch (request) {
  922. case PTRACE_PEEKUSR:
  923. ret = ptrace_read_user(child, addr, datap);
  924. break;
  925. case PTRACE_POKEUSR:
  926. ret = ptrace_write_user(child, addr, data);
  927. break;
  928. case PTRACE_GETREGS:
  929. ret = ptrace_getregs(child, datap);
  930. break;
  931. case PTRACE_SETREGS:
  932. ret = ptrace_setregs(child, datap);
  933. break;
  934. case PTRACE_GETFPREGS:
  935. ret = ptrace_getfpregs(child, datap);
  936. break;
  937. case PTRACE_SETFPREGS:
  938. ret = ptrace_setfpregs(child, datap);
  939. break;
  940. #ifdef CONFIG_IWMMXT
  941. case PTRACE_GETWMMXREGS:
  942. ret = ptrace_getwmmxregs(child, datap);
  943. break;
  944. case PTRACE_SETWMMXREGS:
  945. ret = ptrace_setwmmxregs(child, datap);
  946. break;
  947. #endif
  948. case PTRACE_GET_THREAD_AREA:
  949. ret = put_user(task_thread_info(child)->tp_value,
  950. datap);
  951. break;
  952. case PTRACE_SET_SYSCALL:
  953. task_thread_info(child)->syscall = data;
  954. ret = 0;
  955. break;
  956. #ifdef CONFIG_CRUNCH
  957. case PTRACE_GETCRUNCHREGS:
  958. ret = ptrace_getcrunchregs(child, datap);
  959. break;
  960. case PTRACE_SETCRUNCHREGS:
  961. ret = ptrace_setcrunchregs(child, datap);
  962. break;
  963. #endif
  964. #ifdef CONFIG_VFP
  965. case PTRACE_GETVFPREGS:
  966. ret = ptrace_getvfpregs(child, datap);
  967. break;
  968. case PTRACE_SETVFPREGS:
  969. ret = ptrace_setvfpregs(child, datap);
  970. break;
  971. #endif
  972. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  973. case PTRACE_GETHBPREGS:
  974. ret = ptrace_gethbpregs(child, addr,
  975. (unsigned long __user *)data);
  976. break;
  977. case PTRACE_SETHBPREGS:
  978. ret = ptrace_sethbpregs(child, addr,
  979. (unsigned long __user *)data);
  980. break;
  981. #endif
  982. default:
  983. ret = ptrace_request(child, request, addr, data);
  984. break;
  985. }
  986. return ret;
  987. }
  988. asmlinkage int syscall_trace(int why, struct pt_regs *regs, int scno)
  989. {
  990. unsigned long ip;
  991. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  992. return scno;
  993. if (!(current->ptrace & PT_PTRACED))
  994. return scno;
  995. /*
  996. * Save IP. IP is used to denote syscall entry/exit:
  997. * IP = 0 -> entry, = 1 -> exit
  998. */
  999. ip = regs->ARM_ip;
  1000. regs->ARM_ip = why;
  1001. current_thread_info()->syscall = scno;
  1002. /* the 0x80 provides a way for the tracing parent to distinguish
  1003. between a syscall stop and SIGTRAP delivery */
  1004. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  1005. ? 0x80 : 0));
  1006. /*
  1007. * this isn't the same as continuing with a signal, but it will do
  1008. * for normal use. strace only continues with a signal if the
  1009. * stopping signal is not SIGTRAP. -brl
  1010. */
  1011. if (current->exit_code) {
  1012. send_sig(current->exit_code, current, 1);
  1013. current->exit_code = 0;
  1014. }
  1015. regs->ARM_ip = ip;
  1016. return current_thread_info()->syscall;
  1017. }