ptrace.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /* By Ross Biro 1/23/92 */
  2. /*
  3. * Pentium III FXSR, SSE support
  4. * Gareth Hughes <gareth@valinux.com>, May 2000
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/smp.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/regset.h>
  14. #include <linux/tracehook.h>
  15. #include <linux/user.h>
  16. #include <linux/elf.h>
  17. #include <linux/security.h>
  18. #include <linux/audit.h>
  19. #include <linux/seccomp.h>
  20. #include <linux/signal.h>
  21. #include <linux/perf_event.h>
  22. #include <linux/hw_breakpoint.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/system.h>
  26. #include <asm/processor.h>
  27. #include <asm/i387.h>
  28. #include <asm/debugreg.h>
  29. #include <asm/ldt.h>
  30. #include <asm/desc.h>
  31. #include <asm/prctl.h>
  32. #include <asm/proto.h>
  33. #include <asm/hw_breakpoint.h>
  34. #include "tls.h"
  35. #define CREATE_TRACE_POINTS
  36. #include <trace/events/syscalls.h>
  37. enum x86_regset {
  38. REGSET_GENERAL,
  39. REGSET_FP,
  40. REGSET_XFP,
  41. REGSET_IOPERM64 = REGSET_XFP,
  42. REGSET_XSTATE,
  43. REGSET_TLS,
  44. REGSET_IOPERM32,
  45. };
  46. struct pt_regs_offset {
  47. const char *name;
  48. int offset;
  49. };
  50. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  51. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  52. static const struct pt_regs_offset regoffset_table[] = {
  53. #ifdef CONFIG_X86_64
  54. REG_OFFSET_NAME(r15),
  55. REG_OFFSET_NAME(r14),
  56. REG_OFFSET_NAME(r13),
  57. REG_OFFSET_NAME(r12),
  58. REG_OFFSET_NAME(r11),
  59. REG_OFFSET_NAME(r10),
  60. REG_OFFSET_NAME(r9),
  61. REG_OFFSET_NAME(r8),
  62. #endif
  63. REG_OFFSET_NAME(bx),
  64. REG_OFFSET_NAME(cx),
  65. REG_OFFSET_NAME(dx),
  66. REG_OFFSET_NAME(si),
  67. REG_OFFSET_NAME(di),
  68. REG_OFFSET_NAME(bp),
  69. REG_OFFSET_NAME(ax),
  70. #ifdef CONFIG_X86_32
  71. REG_OFFSET_NAME(ds),
  72. REG_OFFSET_NAME(es),
  73. REG_OFFSET_NAME(fs),
  74. REG_OFFSET_NAME(gs),
  75. #endif
  76. REG_OFFSET_NAME(orig_ax),
  77. REG_OFFSET_NAME(ip),
  78. REG_OFFSET_NAME(cs),
  79. REG_OFFSET_NAME(flags),
  80. REG_OFFSET_NAME(sp),
  81. REG_OFFSET_NAME(ss),
  82. REG_OFFSET_END,
  83. };
  84. /**
  85. * regs_query_register_offset() - query register offset from its name
  86. * @name: the name of a register
  87. *
  88. * regs_query_register_offset() returns the offset of a register in struct
  89. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  90. */
  91. int regs_query_register_offset(const char *name)
  92. {
  93. const struct pt_regs_offset *roff;
  94. for (roff = regoffset_table; roff->name != NULL; roff++)
  95. if (!strcmp(roff->name, name))
  96. return roff->offset;
  97. return -EINVAL;
  98. }
  99. /**
  100. * regs_query_register_name() - query register name from its offset
  101. * @offset: the offset of a register in struct pt_regs.
  102. *
  103. * regs_query_register_name() returns the name of a register from its
  104. * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
  105. */
  106. const char *regs_query_register_name(unsigned int offset)
  107. {
  108. const struct pt_regs_offset *roff;
  109. for (roff = regoffset_table; roff->name != NULL; roff++)
  110. if (roff->offset == offset)
  111. return roff->name;
  112. return NULL;
  113. }
  114. static const int arg_offs_table[] = {
  115. #ifdef CONFIG_X86_32
  116. [0] = offsetof(struct pt_regs, ax),
  117. [1] = offsetof(struct pt_regs, dx),
  118. [2] = offsetof(struct pt_regs, cx)
  119. #else /* CONFIG_X86_64 */
  120. [0] = offsetof(struct pt_regs, di),
  121. [1] = offsetof(struct pt_regs, si),
  122. [2] = offsetof(struct pt_regs, dx),
  123. [3] = offsetof(struct pt_regs, cx),
  124. [4] = offsetof(struct pt_regs, r8),
  125. [5] = offsetof(struct pt_regs, r9)
  126. #endif
  127. };
  128. /*
  129. * does not yet catch signals sent when the child dies.
  130. * in exit.c or in signal.c.
  131. */
  132. /*
  133. * Determines which flags the user has access to [1 = access, 0 = no access].
  134. */
  135. #define FLAG_MASK_32 ((unsigned long) \
  136. (X86_EFLAGS_CF | X86_EFLAGS_PF | \
  137. X86_EFLAGS_AF | X86_EFLAGS_ZF | \
  138. X86_EFLAGS_SF | X86_EFLAGS_TF | \
  139. X86_EFLAGS_DF | X86_EFLAGS_OF | \
  140. X86_EFLAGS_RF | X86_EFLAGS_AC))
  141. /*
  142. * Determines whether a value may be installed in a segment register.
  143. */
  144. static inline bool invalid_selector(u16 value)
  145. {
  146. return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
  147. }
  148. #ifdef CONFIG_X86_32
  149. #define FLAG_MASK FLAG_MASK_32
  150. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  151. {
  152. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  153. return &regs->bx + (regno >> 2);
  154. }
  155. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  156. {
  157. /*
  158. * Returning the value truncates it to 16 bits.
  159. */
  160. unsigned int retval;
  161. if (offset != offsetof(struct user_regs_struct, gs))
  162. retval = *pt_regs_access(task_pt_regs(task), offset);
  163. else {
  164. if (task == current)
  165. retval = get_user_gs(task_pt_regs(task));
  166. else
  167. retval = task_user_gs(task);
  168. }
  169. return retval;
  170. }
  171. static int set_segment_reg(struct task_struct *task,
  172. unsigned long offset, u16 value)
  173. {
  174. /*
  175. * The value argument was already truncated to 16 bits.
  176. */
  177. if (invalid_selector(value))
  178. return -EIO;
  179. /*
  180. * For %cs and %ss we cannot permit a null selector.
  181. * We can permit a bogus selector as long as it has USER_RPL.
  182. * Null selectors are fine for other segment registers, but
  183. * we will never get back to user mode with invalid %cs or %ss
  184. * and will take the trap in iret instead. Much code relies
  185. * on user_mode() to distinguish a user trap frame (which can
  186. * safely use invalid selectors) from a kernel trap frame.
  187. */
  188. switch (offset) {
  189. case offsetof(struct user_regs_struct, cs):
  190. case offsetof(struct user_regs_struct, ss):
  191. if (unlikely(value == 0))
  192. return -EIO;
  193. default:
  194. *pt_regs_access(task_pt_regs(task), offset) = value;
  195. break;
  196. case offsetof(struct user_regs_struct, gs):
  197. if (task == current)
  198. set_user_gs(task_pt_regs(task), value);
  199. else
  200. task_user_gs(task) = value;
  201. }
  202. return 0;
  203. }
  204. #else /* CONFIG_X86_64 */
  205. #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
  206. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
  207. {
  208. BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
  209. return &regs->r15 + (offset / sizeof(regs->r15));
  210. }
  211. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  212. {
  213. /*
  214. * Returning the value truncates it to 16 bits.
  215. */
  216. unsigned int seg;
  217. switch (offset) {
  218. case offsetof(struct user_regs_struct, fs):
  219. if (task == current) {
  220. /* Older gas can't assemble movq %?s,%r?? */
  221. asm("movl %%fs,%0" : "=r" (seg));
  222. return seg;
  223. }
  224. return task->thread.fsindex;
  225. case offsetof(struct user_regs_struct, gs):
  226. if (task == current) {
  227. asm("movl %%gs,%0" : "=r" (seg));
  228. return seg;
  229. }
  230. return task->thread.gsindex;
  231. case offsetof(struct user_regs_struct, ds):
  232. if (task == current) {
  233. asm("movl %%ds,%0" : "=r" (seg));
  234. return seg;
  235. }
  236. return task->thread.ds;
  237. case offsetof(struct user_regs_struct, es):
  238. if (task == current) {
  239. asm("movl %%es,%0" : "=r" (seg));
  240. return seg;
  241. }
  242. return task->thread.es;
  243. case offsetof(struct user_regs_struct, cs):
  244. case offsetof(struct user_regs_struct, ss):
  245. break;
  246. }
  247. return *pt_regs_access(task_pt_regs(task), offset);
  248. }
  249. static int set_segment_reg(struct task_struct *task,
  250. unsigned long offset, u16 value)
  251. {
  252. /*
  253. * The value argument was already truncated to 16 bits.
  254. */
  255. if (invalid_selector(value))
  256. return -EIO;
  257. switch (offset) {
  258. case offsetof(struct user_regs_struct,fs):
  259. /*
  260. * If this is setting fs as for normal 64-bit use but
  261. * setting fs_base has implicitly changed it, leave it.
  262. */
  263. if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
  264. task->thread.fs != 0) ||
  265. (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
  266. task->thread.fs == 0))
  267. break;
  268. task->thread.fsindex = value;
  269. if (task == current)
  270. loadsegment(fs, task->thread.fsindex);
  271. break;
  272. case offsetof(struct user_regs_struct,gs):
  273. /*
  274. * If this is setting gs as for normal 64-bit use but
  275. * setting gs_base has implicitly changed it, leave it.
  276. */
  277. if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
  278. task->thread.gs != 0) ||
  279. (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
  280. task->thread.gs == 0))
  281. break;
  282. task->thread.gsindex = value;
  283. if (task == current)
  284. load_gs_index(task->thread.gsindex);
  285. break;
  286. case offsetof(struct user_regs_struct,ds):
  287. task->thread.ds = value;
  288. if (task == current)
  289. loadsegment(ds, task->thread.ds);
  290. break;
  291. case offsetof(struct user_regs_struct,es):
  292. task->thread.es = value;
  293. if (task == current)
  294. loadsegment(es, task->thread.es);
  295. break;
  296. /*
  297. * Can't actually change these in 64-bit mode.
  298. */
  299. case offsetof(struct user_regs_struct,cs):
  300. if (unlikely(value == 0))
  301. return -EIO;
  302. #ifdef CONFIG_IA32_EMULATION
  303. if (test_tsk_thread_flag(task, TIF_IA32))
  304. task_pt_regs(task)->cs = value;
  305. #endif
  306. break;
  307. case offsetof(struct user_regs_struct,ss):
  308. if (unlikely(value == 0))
  309. return -EIO;
  310. #ifdef CONFIG_IA32_EMULATION
  311. if (test_tsk_thread_flag(task, TIF_IA32))
  312. task_pt_regs(task)->ss = value;
  313. #endif
  314. break;
  315. }
  316. return 0;
  317. }
  318. #endif /* CONFIG_X86_32 */
  319. static unsigned long get_flags(struct task_struct *task)
  320. {
  321. unsigned long retval = task_pt_regs(task)->flags;
  322. /*
  323. * If the debugger set TF, hide it from the readout.
  324. */
  325. if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  326. retval &= ~X86_EFLAGS_TF;
  327. return retval;
  328. }
  329. static int set_flags(struct task_struct *task, unsigned long value)
  330. {
  331. struct pt_regs *regs = task_pt_regs(task);
  332. /*
  333. * If the user value contains TF, mark that
  334. * it was not "us" (the debugger) that set it.
  335. * If not, make sure it stays set if we had.
  336. */
  337. if (value & X86_EFLAGS_TF)
  338. clear_tsk_thread_flag(task, TIF_FORCED_TF);
  339. else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  340. value |= X86_EFLAGS_TF;
  341. regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
  342. return 0;
  343. }
  344. static int putreg(struct task_struct *child,
  345. unsigned long offset, unsigned long value)
  346. {
  347. switch (offset) {
  348. case offsetof(struct user_regs_struct, cs):
  349. case offsetof(struct user_regs_struct, ds):
  350. case offsetof(struct user_regs_struct, es):
  351. case offsetof(struct user_regs_struct, fs):
  352. case offsetof(struct user_regs_struct, gs):
  353. case offsetof(struct user_regs_struct, ss):
  354. return set_segment_reg(child, offset, value);
  355. case offsetof(struct user_regs_struct, flags):
  356. return set_flags(child, value);
  357. #ifdef CONFIG_X86_64
  358. case offsetof(struct user_regs_struct,fs_base):
  359. if (value >= TASK_SIZE_OF(child))
  360. return -EIO;
  361. /*
  362. * When changing the segment base, use do_arch_prctl
  363. * to set either thread.fs or thread.fsindex and the
  364. * corresponding GDT slot.
  365. */
  366. if (child->thread.fs != value)
  367. return do_arch_prctl(child, ARCH_SET_FS, value);
  368. return 0;
  369. case offsetof(struct user_regs_struct,gs_base):
  370. /*
  371. * Exactly the same here as the %fs handling above.
  372. */
  373. if (value >= TASK_SIZE_OF(child))
  374. return -EIO;
  375. if (child->thread.gs != value)
  376. return do_arch_prctl(child, ARCH_SET_GS, value);
  377. return 0;
  378. #endif
  379. }
  380. *pt_regs_access(task_pt_regs(child), offset) = value;
  381. return 0;
  382. }
  383. static unsigned long getreg(struct task_struct *task, unsigned long offset)
  384. {
  385. switch (offset) {
  386. case offsetof(struct user_regs_struct, cs):
  387. case offsetof(struct user_regs_struct, ds):
  388. case offsetof(struct user_regs_struct, es):
  389. case offsetof(struct user_regs_struct, fs):
  390. case offsetof(struct user_regs_struct, gs):
  391. case offsetof(struct user_regs_struct, ss):
  392. return get_segment_reg(task, offset);
  393. case offsetof(struct user_regs_struct, flags):
  394. return get_flags(task);
  395. #ifdef CONFIG_X86_64
  396. case offsetof(struct user_regs_struct, fs_base): {
  397. /*
  398. * do_arch_prctl may have used a GDT slot instead of
  399. * the MSR. To userland, it appears the same either
  400. * way, except the %fs segment selector might not be 0.
  401. */
  402. unsigned int seg = task->thread.fsindex;
  403. if (task->thread.fs != 0)
  404. return task->thread.fs;
  405. if (task == current)
  406. asm("movl %%fs,%0" : "=r" (seg));
  407. if (seg != FS_TLS_SEL)
  408. return 0;
  409. return get_desc_base(&task->thread.tls_array[FS_TLS]);
  410. }
  411. case offsetof(struct user_regs_struct, gs_base): {
  412. /*
  413. * Exactly the same here as the %fs handling above.
  414. */
  415. unsigned int seg = task->thread.gsindex;
  416. if (task->thread.gs != 0)
  417. return task->thread.gs;
  418. if (task == current)
  419. asm("movl %%gs,%0" : "=r" (seg));
  420. if (seg != GS_TLS_SEL)
  421. return 0;
  422. return get_desc_base(&task->thread.tls_array[GS_TLS]);
  423. }
  424. #endif
  425. }
  426. return *pt_regs_access(task_pt_regs(task), offset);
  427. }
  428. static int genregs_get(struct task_struct *target,
  429. const struct user_regset *regset,
  430. unsigned int pos, unsigned int count,
  431. void *kbuf, void __user *ubuf)
  432. {
  433. if (kbuf) {
  434. unsigned long *k = kbuf;
  435. while (count >= sizeof(*k)) {
  436. *k++ = getreg(target, pos);
  437. count -= sizeof(*k);
  438. pos += sizeof(*k);
  439. }
  440. } else {
  441. unsigned long __user *u = ubuf;
  442. while (count >= sizeof(*u)) {
  443. if (__put_user(getreg(target, pos), u++))
  444. return -EFAULT;
  445. count -= sizeof(*u);
  446. pos += sizeof(*u);
  447. }
  448. }
  449. return 0;
  450. }
  451. static int genregs_set(struct task_struct *target,
  452. const struct user_regset *regset,
  453. unsigned int pos, unsigned int count,
  454. const void *kbuf, const void __user *ubuf)
  455. {
  456. int ret = 0;
  457. if (kbuf) {
  458. const unsigned long *k = kbuf;
  459. while (count >= sizeof(*k) && !ret) {
  460. ret = putreg(target, pos, *k++);
  461. count -= sizeof(*k);
  462. pos += sizeof(*k);
  463. }
  464. } else {
  465. const unsigned long __user *u = ubuf;
  466. while (count >= sizeof(*u) && !ret) {
  467. unsigned long word;
  468. ret = __get_user(word, u++);
  469. if (ret)
  470. break;
  471. ret = putreg(target, pos, word);
  472. count -= sizeof(*u);
  473. pos += sizeof(*u);
  474. }
  475. }
  476. return ret;
  477. }
  478. static void ptrace_triggered(struct perf_event *bp, int nmi,
  479. struct perf_sample_data *data,
  480. struct pt_regs *regs)
  481. {
  482. int i;
  483. struct thread_struct *thread = &(current->thread);
  484. /*
  485. * Store in the virtual DR6 register the fact that the breakpoint
  486. * was hit so the thread's debugger will see it.
  487. */
  488. for (i = 0; i < HBP_NUM; i++) {
  489. if (thread->ptrace_bps[i] == bp)
  490. break;
  491. }
  492. thread->debugreg6 |= (DR_TRAP0 << i);
  493. }
  494. /*
  495. * Walk through every ptrace breakpoints for this thread and
  496. * build the dr7 value on top of their attributes.
  497. *
  498. */
  499. static unsigned long ptrace_get_dr7(struct perf_event *bp[])
  500. {
  501. int i;
  502. int dr7 = 0;
  503. struct arch_hw_breakpoint *info;
  504. for (i = 0; i < HBP_NUM; i++) {
  505. if (bp[i] && !bp[i]->attr.disabled) {
  506. info = counter_arch_bp(bp[i]);
  507. dr7 |= encode_dr7(i, info->len, info->type);
  508. }
  509. }
  510. return dr7;
  511. }
  512. static int
  513. ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
  514. struct task_struct *tsk, int disabled)
  515. {
  516. int err;
  517. int gen_len, gen_type;
  518. struct perf_event_attr attr;
  519. /*
  520. * We should have at least an inactive breakpoint at this
  521. * slot. It means the user is writing dr7 without having
  522. * written the address register first
  523. */
  524. if (!bp)
  525. return -EINVAL;
  526. err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
  527. if (err)
  528. return err;
  529. attr = bp->attr;
  530. attr.bp_len = gen_len;
  531. attr.bp_type = gen_type;
  532. attr.disabled = disabled;
  533. return modify_user_hw_breakpoint(bp, &attr);
  534. }
  535. /*
  536. * Handle ptrace writes to debug register 7.
  537. */
  538. static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
  539. {
  540. struct thread_struct *thread = &(tsk->thread);
  541. unsigned long old_dr7;
  542. int i, orig_ret = 0, rc = 0;
  543. int enabled, second_pass = 0;
  544. unsigned len, type;
  545. struct perf_event *bp;
  546. data &= ~DR_CONTROL_RESERVED;
  547. old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
  548. restore:
  549. /*
  550. * Loop through all the hardware breakpoints, making the
  551. * appropriate changes to each.
  552. */
  553. for (i = 0; i < HBP_NUM; i++) {
  554. enabled = decode_dr7(data, i, &len, &type);
  555. bp = thread->ptrace_bps[i];
  556. if (!enabled) {
  557. if (bp) {
  558. /*
  559. * Don't unregister the breakpoints right-away,
  560. * unless all register_user_hw_breakpoint()
  561. * requests have succeeded. This prevents
  562. * any window of opportunity for debug
  563. * register grabbing by other users.
  564. */
  565. if (!second_pass)
  566. continue;
  567. rc = ptrace_modify_breakpoint(bp, len, type,
  568. tsk, 1);
  569. if (rc)
  570. break;
  571. }
  572. continue;
  573. }
  574. rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
  575. if (rc)
  576. break;
  577. }
  578. /*
  579. * Make a second pass to free the remaining unused breakpoints
  580. * or to restore the original breakpoints if an error occurred.
  581. */
  582. if (!second_pass) {
  583. second_pass = 1;
  584. if (rc < 0) {
  585. orig_ret = rc;
  586. data = old_dr7;
  587. }
  588. goto restore;
  589. }
  590. return ((orig_ret < 0) ? orig_ret : rc);
  591. }
  592. /*
  593. * Handle PTRACE_PEEKUSR calls for the debug register area.
  594. */
  595. static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
  596. {
  597. struct thread_struct *thread = &(tsk->thread);
  598. unsigned long val = 0;
  599. if (n < HBP_NUM) {
  600. struct perf_event *bp;
  601. bp = thread->ptrace_bps[n];
  602. if (!bp)
  603. return 0;
  604. val = bp->hw.info.address;
  605. } else if (n == 6) {
  606. val = thread->debugreg6;
  607. } else if (n == 7) {
  608. val = thread->ptrace_dr7;
  609. }
  610. return val;
  611. }
  612. static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
  613. unsigned long addr)
  614. {
  615. struct perf_event *bp;
  616. struct thread_struct *t = &tsk->thread;
  617. struct perf_event_attr attr;
  618. if (!t->ptrace_bps[nr]) {
  619. ptrace_breakpoint_init(&attr);
  620. /*
  621. * Put stub len and type to register (reserve) an inactive but
  622. * correct bp
  623. */
  624. attr.bp_addr = addr;
  625. attr.bp_len = HW_BREAKPOINT_LEN_1;
  626. attr.bp_type = HW_BREAKPOINT_W;
  627. attr.disabled = 1;
  628. bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
  629. /*
  630. * CHECKME: the previous code returned -EIO if the addr wasn't
  631. * a valid task virtual addr. The new one will return -EINVAL in
  632. * this case.
  633. * -EINVAL may be what we want for in-kernel breakpoints users,
  634. * but -EIO looks better for ptrace, since we refuse a register
  635. * writing for the user. And anyway this is the previous
  636. * behaviour.
  637. */
  638. if (IS_ERR(bp))
  639. return PTR_ERR(bp);
  640. t->ptrace_bps[nr] = bp;
  641. } else {
  642. int err;
  643. bp = t->ptrace_bps[nr];
  644. attr = bp->attr;
  645. attr.bp_addr = addr;
  646. err = modify_user_hw_breakpoint(bp, &attr);
  647. if (err)
  648. return err;
  649. }
  650. return 0;
  651. }
  652. /*
  653. * Handle PTRACE_POKEUSR calls for the debug register area.
  654. */
  655. int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
  656. {
  657. struct thread_struct *thread = &(tsk->thread);
  658. int rc = 0;
  659. /* There are no DR4 or DR5 registers */
  660. if (n == 4 || n == 5)
  661. return -EIO;
  662. if (n == 6) {
  663. thread->debugreg6 = val;
  664. goto ret_path;
  665. }
  666. if (n < HBP_NUM) {
  667. rc = ptrace_set_breakpoint_addr(tsk, n, val);
  668. if (rc)
  669. return rc;
  670. }
  671. /* All that's left is DR7 */
  672. if (n == 7) {
  673. rc = ptrace_write_dr7(tsk, val);
  674. if (!rc)
  675. thread->ptrace_dr7 = val;
  676. }
  677. ret_path:
  678. return rc;
  679. }
  680. /*
  681. * These access the current or another (stopped) task's io permission
  682. * bitmap for debugging or core dump.
  683. */
  684. static int ioperm_active(struct task_struct *target,
  685. const struct user_regset *regset)
  686. {
  687. return target->thread.io_bitmap_max / regset->size;
  688. }
  689. static int ioperm_get(struct task_struct *target,
  690. const struct user_regset *regset,
  691. unsigned int pos, unsigned int count,
  692. void *kbuf, void __user *ubuf)
  693. {
  694. if (!target->thread.io_bitmap_ptr)
  695. return -ENXIO;
  696. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  697. target->thread.io_bitmap_ptr,
  698. 0, IO_BITMAP_BYTES);
  699. }
  700. /*
  701. * Called by kernel/ptrace.c when detaching..
  702. *
  703. * Make sure the single step bit is not set.
  704. */
  705. void ptrace_disable(struct task_struct *child)
  706. {
  707. user_disable_single_step(child);
  708. #ifdef TIF_SYSCALL_EMU
  709. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  710. #endif
  711. }
  712. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  713. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  714. #endif
  715. long arch_ptrace(struct task_struct *child, long request,
  716. unsigned long addr, unsigned long data)
  717. {
  718. int ret;
  719. unsigned long __user *datap = (unsigned long __user *)data;
  720. switch (request) {
  721. /* read the word at location addr in the USER area. */
  722. case PTRACE_PEEKUSR: {
  723. unsigned long tmp;
  724. ret = -EIO;
  725. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
  726. break;
  727. tmp = 0; /* Default return condition */
  728. if (addr < sizeof(struct user_regs_struct))
  729. tmp = getreg(child, addr);
  730. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  731. addr <= offsetof(struct user, u_debugreg[7])) {
  732. addr -= offsetof(struct user, u_debugreg[0]);
  733. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  734. }
  735. ret = put_user(tmp, datap);
  736. break;
  737. }
  738. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  739. ret = -EIO;
  740. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
  741. break;
  742. if (addr < sizeof(struct user_regs_struct))
  743. ret = putreg(child, addr, data);
  744. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  745. addr <= offsetof(struct user, u_debugreg[7])) {
  746. addr -= offsetof(struct user, u_debugreg[0]);
  747. ret = ptrace_set_debugreg(child,
  748. addr / sizeof(data), data);
  749. }
  750. break;
  751. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  752. return copy_regset_to_user(child,
  753. task_user_regset_view(current),
  754. REGSET_GENERAL,
  755. 0, sizeof(struct user_regs_struct),
  756. datap);
  757. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  758. return copy_regset_from_user(child,
  759. task_user_regset_view(current),
  760. REGSET_GENERAL,
  761. 0, sizeof(struct user_regs_struct),
  762. datap);
  763. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  764. return copy_regset_to_user(child,
  765. task_user_regset_view(current),
  766. REGSET_FP,
  767. 0, sizeof(struct user_i387_struct),
  768. datap);
  769. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  770. return copy_regset_from_user(child,
  771. task_user_regset_view(current),
  772. REGSET_FP,
  773. 0, sizeof(struct user_i387_struct),
  774. datap);
  775. #ifdef CONFIG_X86_32
  776. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  777. return copy_regset_to_user(child, &user_x86_32_view,
  778. REGSET_XFP,
  779. 0, sizeof(struct user_fxsr_struct),
  780. datap) ? -EIO : 0;
  781. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  782. return copy_regset_from_user(child, &user_x86_32_view,
  783. REGSET_XFP,
  784. 0, sizeof(struct user_fxsr_struct),
  785. datap) ? -EIO : 0;
  786. #endif
  787. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  788. case PTRACE_GET_THREAD_AREA:
  789. if ((int) addr < 0)
  790. return -EIO;
  791. ret = do_get_thread_area(child, addr,
  792. (struct user_desc __user *)data);
  793. break;
  794. case PTRACE_SET_THREAD_AREA:
  795. if ((int) addr < 0)
  796. return -EIO;
  797. ret = do_set_thread_area(child, addr,
  798. (struct user_desc __user *)data, 0);
  799. break;
  800. #endif
  801. #ifdef CONFIG_X86_64
  802. /* normal 64bit interface to access TLS data.
  803. Works just like arch_prctl, except that the arguments
  804. are reversed. */
  805. case PTRACE_ARCH_PRCTL:
  806. ret = do_arch_prctl(child, data, addr);
  807. break;
  808. #endif
  809. default:
  810. ret = ptrace_request(child, request, addr, data);
  811. break;
  812. }
  813. return ret;
  814. }
  815. #ifdef CONFIG_IA32_EMULATION
  816. #include <linux/compat.h>
  817. #include <linux/syscalls.h>
  818. #include <asm/ia32.h>
  819. #include <asm/user32.h>
  820. #define R32(l,q) \
  821. case offsetof(struct user32, regs.l): \
  822. regs->q = value; break
  823. #define SEG32(rs) \
  824. case offsetof(struct user32, regs.rs): \
  825. return set_segment_reg(child, \
  826. offsetof(struct user_regs_struct, rs), \
  827. value); \
  828. break
  829. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  830. {
  831. struct pt_regs *regs = task_pt_regs(child);
  832. switch (regno) {
  833. SEG32(cs);
  834. SEG32(ds);
  835. SEG32(es);
  836. SEG32(fs);
  837. SEG32(gs);
  838. SEG32(ss);
  839. R32(ebx, bx);
  840. R32(ecx, cx);
  841. R32(edx, dx);
  842. R32(edi, di);
  843. R32(esi, si);
  844. R32(ebp, bp);
  845. R32(eax, ax);
  846. R32(eip, ip);
  847. R32(esp, sp);
  848. case offsetof(struct user32, regs.orig_eax):
  849. /*
  850. * A 32-bit debugger setting orig_eax means to restore
  851. * the state of the task restarting a 32-bit syscall.
  852. * Make sure we interpret the -ERESTART* codes correctly
  853. * in case the task is not actually still sitting at the
  854. * exit from a 32-bit syscall with TS_COMPAT still set.
  855. */
  856. regs->orig_ax = value;
  857. if (syscall_get_nr(child, regs) >= 0)
  858. task_thread_info(child)->status |= TS_COMPAT;
  859. break;
  860. case offsetof(struct user32, regs.eflags):
  861. return set_flags(child, value);
  862. case offsetof(struct user32, u_debugreg[0]) ...
  863. offsetof(struct user32, u_debugreg[7]):
  864. regno -= offsetof(struct user32, u_debugreg[0]);
  865. return ptrace_set_debugreg(child, regno / 4, value);
  866. default:
  867. if (regno > sizeof(struct user32) || (regno & 3))
  868. return -EIO;
  869. /*
  870. * Other dummy fields in the virtual user structure
  871. * are ignored
  872. */
  873. break;
  874. }
  875. return 0;
  876. }
  877. #undef R32
  878. #undef SEG32
  879. #define R32(l,q) \
  880. case offsetof(struct user32, regs.l): \
  881. *val = regs->q; break
  882. #define SEG32(rs) \
  883. case offsetof(struct user32, regs.rs): \
  884. *val = get_segment_reg(child, \
  885. offsetof(struct user_regs_struct, rs)); \
  886. break
  887. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  888. {
  889. struct pt_regs *regs = task_pt_regs(child);
  890. switch (regno) {
  891. SEG32(ds);
  892. SEG32(es);
  893. SEG32(fs);
  894. SEG32(gs);
  895. R32(cs, cs);
  896. R32(ss, ss);
  897. R32(ebx, bx);
  898. R32(ecx, cx);
  899. R32(edx, dx);
  900. R32(edi, di);
  901. R32(esi, si);
  902. R32(ebp, bp);
  903. R32(eax, ax);
  904. R32(orig_eax, orig_ax);
  905. R32(eip, ip);
  906. R32(esp, sp);
  907. case offsetof(struct user32, regs.eflags):
  908. *val = get_flags(child);
  909. break;
  910. case offsetof(struct user32, u_debugreg[0]) ...
  911. offsetof(struct user32, u_debugreg[7]):
  912. regno -= offsetof(struct user32, u_debugreg[0]);
  913. *val = ptrace_get_debugreg(child, regno / 4);
  914. break;
  915. default:
  916. if (regno > sizeof(struct user32) || (regno & 3))
  917. return -EIO;
  918. /*
  919. * Other dummy fields in the virtual user structure
  920. * are ignored
  921. */
  922. *val = 0;
  923. break;
  924. }
  925. return 0;
  926. }
  927. #undef R32
  928. #undef SEG32
  929. static int genregs32_get(struct task_struct *target,
  930. const struct user_regset *regset,
  931. unsigned int pos, unsigned int count,
  932. void *kbuf, void __user *ubuf)
  933. {
  934. if (kbuf) {
  935. compat_ulong_t *k = kbuf;
  936. while (count >= sizeof(*k)) {
  937. getreg32(target, pos, k++);
  938. count -= sizeof(*k);
  939. pos += sizeof(*k);
  940. }
  941. } else {
  942. compat_ulong_t __user *u = ubuf;
  943. while (count >= sizeof(*u)) {
  944. compat_ulong_t word;
  945. getreg32(target, pos, &word);
  946. if (__put_user(word, u++))
  947. return -EFAULT;
  948. count -= sizeof(*u);
  949. pos += sizeof(*u);
  950. }
  951. }
  952. return 0;
  953. }
  954. static int genregs32_set(struct task_struct *target,
  955. const struct user_regset *regset,
  956. unsigned int pos, unsigned int count,
  957. const void *kbuf, const void __user *ubuf)
  958. {
  959. int ret = 0;
  960. if (kbuf) {
  961. const compat_ulong_t *k = kbuf;
  962. while (count >= sizeof(*k) && !ret) {
  963. ret = putreg32(target, pos, *k++);
  964. count -= sizeof(*k);
  965. pos += sizeof(*k);
  966. }
  967. } else {
  968. const compat_ulong_t __user *u = ubuf;
  969. while (count >= sizeof(*u) && !ret) {
  970. compat_ulong_t word;
  971. ret = __get_user(word, u++);
  972. if (ret)
  973. break;
  974. ret = putreg32(target, pos, word);
  975. count -= sizeof(*u);
  976. pos += sizeof(*u);
  977. }
  978. }
  979. return ret;
  980. }
  981. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  982. compat_ulong_t caddr, compat_ulong_t cdata)
  983. {
  984. unsigned long addr = caddr;
  985. unsigned long data = cdata;
  986. void __user *datap = compat_ptr(data);
  987. int ret;
  988. __u32 val;
  989. switch (request) {
  990. case PTRACE_PEEKUSR:
  991. ret = getreg32(child, addr, &val);
  992. if (ret == 0)
  993. ret = put_user(val, (__u32 __user *)datap);
  994. break;
  995. case PTRACE_POKEUSR:
  996. ret = putreg32(child, addr, data);
  997. break;
  998. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  999. return copy_regset_to_user(child, &user_x86_32_view,
  1000. REGSET_GENERAL,
  1001. 0, sizeof(struct user_regs_struct32),
  1002. datap);
  1003. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1004. return copy_regset_from_user(child, &user_x86_32_view,
  1005. REGSET_GENERAL, 0,
  1006. sizeof(struct user_regs_struct32),
  1007. datap);
  1008. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1009. return copy_regset_to_user(child, &user_x86_32_view,
  1010. REGSET_FP, 0,
  1011. sizeof(struct user_i387_ia32_struct),
  1012. datap);
  1013. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1014. return copy_regset_from_user(
  1015. child, &user_x86_32_view, REGSET_FP,
  1016. 0, sizeof(struct user_i387_ia32_struct), datap);
  1017. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1018. return copy_regset_to_user(child, &user_x86_32_view,
  1019. REGSET_XFP, 0,
  1020. sizeof(struct user32_fxsr_struct),
  1021. datap);
  1022. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1023. return copy_regset_from_user(child, &user_x86_32_view,
  1024. REGSET_XFP, 0,
  1025. sizeof(struct user32_fxsr_struct),
  1026. datap);
  1027. case PTRACE_GET_THREAD_AREA:
  1028. case PTRACE_SET_THREAD_AREA:
  1029. return arch_ptrace(child, request, addr, data);
  1030. default:
  1031. return compat_ptrace_request(child, request, addr, data);
  1032. }
  1033. return ret;
  1034. }
  1035. #endif /* CONFIG_IA32_EMULATION */
  1036. #ifdef CONFIG_X86_64
  1037. static struct user_regset x86_64_regsets[] __read_mostly = {
  1038. [REGSET_GENERAL] = {
  1039. .core_note_type = NT_PRSTATUS,
  1040. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1041. .size = sizeof(long), .align = sizeof(long),
  1042. .get = genregs_get, .set = genregs_set
  1043. },
  1044. [REGSET_FP] = {
  1045. .core_note_type = NT_PRFPREG,
  1046. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1047. .size = sizeof(long), .align = sizeof(long),
  1048. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1049. },
  1050. [REGSET_XSTATE] = {
  1051. .core_note_type = NT_X86_XSTATE,
  1052. .size = sizeof(u64), .align = sizeof(u64),
  1053. .active = xstateregs_active, .get = xstateregs_get,
  1054. .set = xstateregs_set
  1055. },
  1056. [REGSET_IOPERM64] = {
  1057. .core_note_type = NT_386_IOPERM,
  1058. .n = IO_BITMAP_LONGS,
  1059. .size = sizeof(long), .align = sizeof(long),
  1060. .active = ioperm_active, .get = ioperm_get
  1061. },
  1062. };
  1063. static const struct user_regset_view user_x86_64_view = {
  1064. .name = "x86_64", .e_machine = EM_X86_64,
  1065. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1066. };
  1067. #else /* CONFIG_X86_32 */
  1068. #define user_regs_struct32 user_regs_struct
  1069. #define genregs32_get genregs_get
  1070. #define genregs32_set genregs_set
  1071. #define user_i387_ia32_struct user_i387_struct
  1072. #define user32_fxsr_struct user_fxsr_struct
  1073. #endif /* CONFIG_X86_64 */
  1074. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1075. static struct user_regset x86_32_regsets[] __read_mostly = {
  1076. [REGSET_GENERAL] = {
  1077. .core_note_type = NT_PRSTATUS,
  1078. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1079. .size = sizeof(u32), .align = sizeof(u32),
  1080. .get = genregs32_get, .set = genregs32_set
  1081. },
  1082. [REGSET_FP] = {
  1083. .core_note_type = NT_PRFPREG,
  1084. .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
  1085. .size = sizeof(u32), .align = sizeof(u32),
  1086. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1087. },
  1088. [REGSET_XFP] = {
  1089. .core_note_type = NT_PRXFPREG,
  1090. .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
  1091. .size = sizeof(u32), .align = sizeof(u32),
  1092. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1093. },
  1094. [REGSET_XSTATE] = {
  1095. .core_note_type = NT_X86_XSTATE,
  1096. .size = sizeof(u64), .align = sizeof(u64),
  1097. .active = xstateregs_active, .get = xstateregs_get,
  1098. .set = xstateregs_set
  1099. },
  1100. [REGSET_TLS] = {
  1101. .core_note_type = NT_386_TLS,
  1102. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1103. .size = sizeof(struct user_desc),
  1104. .align = sizeof(struct user_desc),
  1105. .active = regset_tls_active,
  1106. .get = regset_tls_get, .set = regset_tls_set
  1107. },
  1108. [REGSET_IOPERM32] = {
  1109. .core_note_type = NT_386_IOPERM,
  1110. .n = IO_BITMAP_BYTES / sizeof(u32),
  1111. .size = sizeof(u32), .align = sizeof(u32),
  1112. .active = ioperm_active, .get = ioperm_get
  1113. },
  1114. };
  1115. static const struct user_regset_view user_x86_32_view = {
  1116. .name = "i386", .e_machine = EM_386,
  1117. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1118. };
  1119. #endif
  1120. /*
  1121. * This represents bytes 464..511 in the memory layout exported through
  1122. * the REGSET_XSTATE interface.
  1123. */
  1124. u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
  1125. void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
  1126. {
  1127. #ifdef CONFIG_X86_64
  1128. x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1129. #endif
  1130. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1131. x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1132. #endif
  1133. xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
  1134. }
  1135. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1136. {
  1137. #ifdef CONFIG_IA32_EMULATION
  1138. if (test_tsk_thread_flag(task, TIF_IA32))
  1139. #endif
  1140. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1141. return &user_x86_32_view;
  1142. #endif
  1143. #ifdef CONFIG_X86_64
  1144. return &user_x86_64_view;
  1145. #endif
  1146. }
  1147. static void fill_sigtrap_info(struct task_struct *tsk,
  1148. struct pt_regs *regs,
  1149. int error_code, int si_code,
  1150. struct siginfo *info)
  1151. {
  1152. tsk->thread.trap_no = 1;
  1153. tsk->thread.error_code = error_code;
  1154. memset(info, 0, sizeof(*info));
  1155. info->si_signo = SIGTRAP;
  1156. info->si_code = si_code;
  1157. info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
  1158. }
  1159. void user_single_step_siginfo(struct task_struct *tsk,
  1160. struct pt_regs *regs,
  1161. struct siginfo *info)
  1162. {
  1163. fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
  1164. }
  1165. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  1166. int error_code, int si_code)
  1167. {
  1168. struct siginfo info;
  1169. fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
  1170. /* Send us the fake SIGTRAP */
  1171. force_sig_info(SIGTRAP, &info, tsk);
  1172. }
  1173. #ifdef CONFIG_X86_32
  1174. # define IS_IA32 1
  1175. #elif defined CONFIG_IA32_EMULATION
  1176. # define IS_IA32 is_compat_task()
  1177. #else
  1178. # define IS_IA32 0
  1179. #endif
  1180. /*
  1181. * We must return the syscall number to actually look up in the table.
  1182. * This can be -1L to skip running any syscall at all.
  1183. */
  1184. asmregparm long syscall_trace_enter(struct pt_regs *regs)
  1185. {
  1186. long ret = 0;
  1187. /*
  1188. * If we stepped into a sysenter/syscall insn, it trapped in
  1189. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  1190. * If user-mode had set TF itself, then it's still clear from
  1191. * do_debug() and we need to set it again to restore the user
  1192. * state. If we entered on the slow path, TF was already set.
  1193. */
  1194. if (test_thread_flag(TIF_SINGLESTEP))
  1195. regs->flags |= X86_EFLAGS_TF;
  1196. /* do the secure computing check first */
  1197. secure_computing(regs->orig_ax);
  1198. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1199. ret = -1L;
  1200. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  1201. tracehook_report_syscall_entry(regs))
  1202. ret = -1L;
  1203. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1204. trace_sys_enter(regs, regs->orig_ax);
  1205. if (unlikely(current->audit_context)) {
  1206. if (IS_IA32)
  1207. audit_syscall_entry(AUDIT_ARCH_I386,
  1208. regs->orig_ax,
  1209. regs->bx, regs->cx,
  1210. regs->dx, regs->si);
  1211. #ifdef CONFIG_X86_64
  1212. else
  1213. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1214. regs->orig_ax,
  1215. regs->di, regs->si,
  1216. regs->dx, regs->r10);
  1217. #endif
  1218. }
  1219. return ret ?: regs->orig_ax;
  1220. }
  1221. asmregparm void syscall_trace_leave(struct pt_regs *regs)
  1222. {
  1223. bool step;
  1224. if (unlikely(current->audit_context))
  1225. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1226. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1227. trace_sys_exit(regs, regs->ax);
  1228. /*
  1229. * If TIF_SYSCALL_EMU is set, we only get here because of
  1230. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  1231. * We already reported this syscall instruction in
  1232. * syscall_trace_enter().
  1233. */
  1234. step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
  1235. !test_thread_flag(TIF_SYSCALL_EMU);
  1236. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  1237. tracehook_report_syscall_exit(regs, step);
  1238. }