ptrace.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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,
  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. if (ptrace_get_breakpoints(tsk) < 0)
  547. return -ESRCH;
  548. data &= ~DR_CONTROL_RESERVED;
  549. old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
  550. restore:
  551. /*
  552. * Loop through all the hardware breakpoints, making the
  553. * appropriate changes to each.
  554. */
  555. for (i = 0; i < HBP_NUM; i++) {
  556. enabled = decode_dr7(data, i, &len, &type);
  557. bp = thread->ptrace_bps[i];
  558. if (!enabled) {
  559. if (bp) {
  560. /*
  561. * Don't unregister the breakpoints right-away,
  562. * unless all register_user_hw_breakpoint()
  563. * requests have succeeded. This prevents
  564. * any window of opportunity for debug
  565. * register grabbing by other users.
  566. */
  567. if (!second_pass)
  568. continue;
  569. rc = ptrace_modify_breakpoint(bp, len, type,
  570. tsk, 1);
  571. if (rc)
  572. break;
  573. }
  574. continue;
  575. }
  576. rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
  577. if (rc)
  578. break;
  579. }
  580. /*
  581. * Make a second pass to free the remaining unused breakpoints
  582. * or to restore the original breakpoints if an error occurred.
  583. */
  584. if (!second_pass) {
  585. second_pass = 1;
  586. if (rc < 0) {
  587. orig_ret = rc;
  588. data = old_dr7;
  589. }
  590. goto restore;
  591. }
  592. ptrace_put_breakpoints(tsk);
  593. return ((orig_ret < 0) ? orig_ret : rc);
  594. }
  595. /*
  596. * Handle PTRACE_PEEKUSR calls for the debug register area.
  597. */
  598. static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
  599. {
  600. struct thread_struct *thread = &(tsk->thread);
  601. unsigned long val = 0;
  602. if (n < HBP_NUM) {
  603. struct perf_event *bp;
  604. if (ptrace_get_breakpoints(tsk) < 0)
  605. return -ESRCH;
  606. bp = thread->ptrace_bps[n];
  607. if (!bp)
  608. val = 0;
  609. else
  610. val = bp->hw.info.address;
  611. ptrace_put_breakpoints(tsk);
  612. } else if (n == 6) {
  613. val = thread->debugreg6;
  614. } else if (n == 7) {
  615. val = thread->ptrace_dr7;
  616. }
  617. return val;
  618. }
  619. static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
  620. unsigned long addr)
  621. {
  622. struct perf_event *bp;
  623. struct thread_struct *t = &tsk->thread;
  624. struct perf_event_attr attr;
  625. int err = 0;
  626. if (ptrace_get_breakpoints(tsk) < 0)
  627. return -ESRCH;
  628. if (!t->ptrace_bps[nr]) {
  629. ptrace_breakpoint_init(&attr);
  630. /*
  631. * Put stub len and type to register (reserve) an inactive but
  632. * correct bp
  633. */
  634. attr.bp_addr = addr;
  635. attr.bp_len = HW_BREAKPOINT_LEN_1;
  636. attr.bp_type = HW_BREAKPOINT_W;
  637. attr.disabled = 1;
  638. bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
  639. NULL, tsk);
  640. /*
  641. * CHECKME: the previous code returned -EIO if the addr wasn't
  642. * a valid task virtual addr. The new one will return -EINVAL in
  643. * this case.
  644. * -EINVAL may be what we want for in-kernel breakpoints users,
  645. * but -EIO looks better for ptrace, since we refuse a register
  646. * writing for the user. And anyway this is the previous
  647. * behaviour.
  648. */
  649. if (IS_ERR(bp)) {
  650. err = PTR_ERR(bp);
  651. goto put;
  652. }
  653. t->ptrace_bps[nr] = bp;
  654. } else {
  655. bp = t->ptrace_bps[nr];
  656. attr = bp->attr;
  657. attr.bp_addr = addr;
  658. err = modify_user_hw_breakpoint(bp, &attr);
  659. }
  660. put:
  661. ptrace_put_breakpoints(tsk);
  662. return err;
  663. }
  664. /*
  665. * Handle PTRACE_POKEUSR calls for the debug register area.
  666. */
  667. static int ptrace_set_debugreg(struct task_struct *tsk, int n,
  668. unsigned long val)
  669. {
  670. struct thread_struct *thread = &(tsk->thread);
  671. int rc = 0;
  672. /* There are no DR4 or DR5 registers */
  673. if (n == 4 || n == 5)
  674. return -EIO;
  675. if (n == 6) {
  676. thread->debugreg6 = val;
  677. goto ret_path;
  678. }
  679. if (n < HBP_NUM) {
  680. rc = ptrace_set_breakpoint_addr(tsk, n, val);
  681. if (rc)
  682. return rc;
  683. }
  684. /* All that's left is DR7 */
  685. if (n == 7) {
  686. rc = ptrace_write_dr7(tsk, val);
  687. if (!rc)
  688. thread->ptrace_dr7 = val;
  689. }
  690. ret_path:
  691. return rc;
  692. }
  693. /*
  694. * These access the current or another (stopped) task's io permission
  695. * bitmap for debugging or core dump.
  696. */
  697. static int ioperm_active(struct task_struct *target,
  698. const struct user_regset *regset)
  699. {
  700. return target->thread.io_bitmap_max / regset->size;
  701. }
  702. static int ioperm_get(struct task_struct *target,
  703. const struct user_regset *regset,
  704. unsigned int pos, unsigned int count,
  705. void *kbuf, void __user *ubuf)
  706. {
  707. if (!target->thread.io_bitmap_ptr)
  708. return -ENXIO;
  709. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  710. target->thread.io_bitmap_ptr,
  711. 0, IO_BITMAP_BYTES);
  712. }
  713. /*
  714. * Called by kernel/ptrace.c when detaching..
  715. *
  716. * Make sure the single step bit is not set.
  717. */
  718. void ptrace_disable(struct task_struct *child)
  719. {
  720. user_disable_single_step(child);
  721. #ifdef TIF_SYSCALL_EMU
  722. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  723. #endif
  724. }
  725. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  726. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  727. #endif
  728. long arch_ptrace(struct task_struct *child, long request,
  729. unsigned long addr, unsigned long data)
  730. {
  731. int ret;
  732. unsigned long __user *datap = (unsigned long __user *)data;
  733. switch (request) {
  734. /* read the word at location addr in the USER area. */
  735. case PTRACE_PEEKUSR: {
  736. unsigned long tmp;
  737. ret = -EIO;
  738. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
  739. break;
  740. tmp = 0; /* Default return condition */
  741. if (addr < sizeof(struct user_regs_struct))
  742. tmp = getreg(child, addr);
  743. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  744. addr <= offsetof(struct user, u_debugreg[7])) {
  745. addr -= offsetof(struct user, u_debugreg[0]);
  746. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  747. }
  748. ret = put_user(tmp, datap);
  749. break;
  750. }
  751. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  752. ret = -EIO;
  753. if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
  754. break;
  755. if (addr < sizeof(struct user_regs_struct))
  756. ret = putreg(child, addr, data);
  757. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  758. addr <= offsetof(struct user, u_debugreg[7])) {
  759. addr -= offsetof(struct user, u_debugreg[0]);
  760. ret = ptrace_set_debugreg(child,
  761. addr / sizeof(data), data);
  762. }
  763. break;
  764. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  765. return copy_regset_to_user(child,
  766. task_user_regset_view(current),
  767. REGSET_GENERAL,
  768. 0, sizeof(struct user_regs_struct),
  769. datap);
  770. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  771. return copy_regset_from_user(child,
  772. task_user_regset_view(current),
  773. REGSET_GENERAL,
  774. 0, sizeof(struct user_regs_struct),
  775. datap);
  776. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  777. return copy_regset_to_user(child,
  778. task_user_regset_view(current),
  779. REGSET_FP,
  780. 0, sizeof(struct user_i387_struct),
  781. datap);
  782. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  783. return copy_regset_from_user(child,
  784. task_user_regset_view(current),
  785. REGSET_FP,
  786. 0, sizeof(struct user_i387_struct),
  787. datap);
  788. #ifdef CONFIG_X86_32
  789. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  790. return copy_regset_to_user(child, &user_x86_32_view,
  791. REGSET_XFP,
  792. 0, sizeof(struct user_fxsr_struct),
  793. datap) ? -EIO : 0;
  794. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  795. return copy_regset_from_user(child, &user_x86_32_view,
  796. REGSET_XFP,
  797. 0, sizeof(struct user_fxsr_struct),
  798. datap) ? -EIO : 0;
  799. #endif
  800. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  801. case PTRACE_GET_THREAD_AREA:
  802. if ((int) addr < 0)
  803. return -EIO;
  804. ret = do_get_thread_area(child, addr,
  805. (struct user_desc __user *)data);
  806. break;
  807. case PTRACE_SET_THREAD_AREA:
  808. if ((int) addr < 0)
  809. return -EIO;
  810. ret = do_set_thread_area(child, addr,
  811. (struct user_desc __user *)data, 0);
  812. break;
  813. #endif
  814. #ifdef CONFIG_X86_64
  815. /* normal 64bit interface to access TLS data.
  816. Works just like arch_prctl, except that the arguments
  817. are reversed. */
  818. case PTRACE_ARCH_PRCTL:
  819. ret = do_arch_prctl(child, data, addr);
  820. break;
  821. #endif
  822. default:
  823. ret = ptrace_request(child, request, addr, data);
  824. break;
  825. }
  826. return ret;
  827. }
  828. #ifdef CONFIG_IA32_EMULATION
  829. #include <linux/compat.h>
  830. #include <linux/syscalls.h>
  831. #include <asm/ia32.h>
  832. #include <asm/user32.h>
  833. #define R32(l,q) \
  834. case offsetof(struct user32, regs.l): \
  835. regs->q = value; break
  836. #define SEG32(rs) \
  837. case offsetof(struct user32, regs.rs): \
  838. return set_segment_reg(child, \
  839. offsetof(struct user_regs_struct, rs), \
  840. value); \
  841. break
  842. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  843. {
  844. struct pt_regs *regs = task_pt_regs(child);
  845. switch (regno) {
  846. SEG32(cs);
  847. SEG32(ds);
  848. SEG32(es);
  849. SEG32(fs);
  850. SEG32(gs);
  851. SEG32(ss);
  852. R32(ebx, bx);
  853. R32(ecx, cx);
  854. R32(edx, dx);
  855. R32(edi, di);
  856. R32(esi, si);
  857. R32(ebp, bp);
  858. R32(eax, ax);
  859. R32(eip, ip);
  860. R32(esp, sp);
  861. case offsetof(struct user32, regs.orig_eax):
  862. /*
  863. * A 32-bit debugger setting orig_eax means to restore
  864. * the state of the task restarting a 32-bit syscall.
  865. * Make sure we interpret the -ERESTART* codes correctly
  866. * in case the task is not actually still sitting at the
  867. * exit from a 32-bit syscall with TS_COMPAT still set.
  868. */
  869. regs->orig_ax = value;
  870. if (syscall_get_nr(child, regs) >= 0)
  871. task_thread_info(child)->status |= TS_COMPAT;
  872. break;
  873. case offsetof(struct user32, regs.eflags):
  874. return set_flags(child, value);
  875. case offsetof(struct user32, u_debugreg[0]) ...
  876. offsetof(struct user32, u_debugreg[7]):
  877. regno -= offsetof(struct user32, u_debugreg[0]);
  878. return ptrace_set_debugreg(child, regno / 4, value);
  879. default:
  880. if (regno > sizeof(struct user32) || (regno & 3))
  881. return -EIO;
  882. /*
  883. * Other dummy fields in the virtual user structure
  884. * are ignored
  885. */
  886. break;
  887. }
  888. return 0;
  889. }
  890. #undef R32
  891. #undef SEG32
  892. #define R32(l,q) \
  893. case offsetof(struct user32, regs.l): \
  894. *val = regs->q; break
  895. #define SEG32(rs) \
  896. case offsetof(struct user32, regs.rs): \
  897. *val = get_segment_reg(child, \
  898. offsetof(struct user_regs_struct, rs)); \
  899. break
  900. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  901. {
  902. struct pt_regs *regs = task_pt_regs(child);
  903. switch (regno) {
  904. SEG32(ds);
  905. SEG32(es);
  906. SEG32(fs);
  907. SEG32(gs);
  908. R32(cs, cs);
  909. R32(ss, ss);
  910. R32(ebx, bx);
  911. R32(ecx, cx);
  912. R32(edx, dx);
  913. R32(edi, di);
  914. R32(esi, si);
  915. R32(ebp, bp);
  916. R32(eax, ax);
  917. R32(orig_eax, orig_ax);
  918. R32(eip, ip);
  919. R32(esp, sp);
  920. case offsetof(struct user32, regs.eflags):
  921. *val = get_flags(child);
  922. break;
  923. case offsetof(struct user32, u_debugreg[0]) ...
  924. offsetof(struct user32, u_debugreg[7]):
  925. regno -= offsetof(struct user32, u_debugreg[0]);
  926. *val = ptrace_get_debugreg(child, regno / 4);
  927. break;
  928. default:
  929. if (regno > sizeof(struct user32) || (regno & 3))
  930. return -EIO;
  931. /*
  932. * Other dummy fields in the virtual user structure
  933. * are ignored
  934. */
  935. *val = 0;
  936. break;
  937. }
  938. return 0;
  939. }
  940. #undef R32
  941. #undef SEG32
  942. static int genregs32_get(struct task_struct *target,
  943. const struct user_regset *regset,
  944. unsigned int pos, unsigned int count,
  945. void *kbuf, void __user *ubuf)
  946. {
  947. if (kbuf) {
  948. compat_ulong_t *k = kbuf;
  949. while (count >= sizeof(*k)) {
  950. getreg32(target, pos, k++);
  951. count -= sizeof(*k);
  952. pos += sizeof(*k);
  953. }
  954. } else {
  955. compat_ulong_t __user *u = ubuf;
  956. while (count >= sizeof(*u)) {
  957. compat_ulong_t word;
  958. getreg32(target, pos, &word);
  959. if (__put_user(word, u++))
  960. return -EFAULT;
  961. count -= sizeof(*u);
  962. pos += sizeof(*u);
  963. }
  964. }
  965. return 0;
  966. }
  967. static int genregs32_set(struct task_struct *target,
  968. const struct user_regset *regset,
  969. unsigned int pos, unsigned int count,
  970. const void *kbuf, const void __user *ubuf)
  971. {
  972. int ret = 0;
  973. if (kbuf) {
  974. const compat_ulong_t *k = kbuf;
  975. while (count >= sizeof(*k) && !ret) {
  976. ret = putreg32(target, pos, *k++);
  977. count -= sizeof(*k);
  978. pos += sizeof(*k);
  979. }
  980. } else {
  981. const compat_ulong_t __user *u = ubuf;
  982. while (count >= sizeof(*u) && !ret) {
  983. compat_ulong_t word;
  984. ret = __get_user(word, u++);
  985. if (ret)
  986. break;
  987. ret = putreg32(target, pos, word);
  988. count -= sizeof(*u);
  989. pos += sizeof(*u);
  990. }
  991. }
  992. return ret;
  993. }
  994. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  995. compat_ulong_t caddr, compat_ulong_t cdata)
  996. {
  997. unsigned long addr = caddr;
  998. unsigned long data = cdata;
  999. void __user *datap = compat_ptr(data);
  1000. int ret;
  1001. __u32 val;
  1002. switch (request) {
  1003. case PTRACE_PEEKUSR:
  1004. ret = getreg32(child, addr, &val);
  1005. if (ret == 0)
  1006. ret = put_user(val, (__u32 __user *)datap);
  1007. break;
  1008. case PTRACE_POKEUSR:
  1009. ret = putreg32(child, addr, data);
  1010. break;
  1011. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1012. return copy_regset_to_user(child, &user_x86_32_view,
  1013. REGSET_GENERAL,
  1014. 0, sizeof(struct user_regs_struct32),
  1015. datap);
  1016. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1017. return copy_regset_from_user(child, &user_x86_32_view,
  1018. REGSET_GENERAL, 0,
  1019. sizeof(struct user_regs_struct32),
  1020. datap);
  1021. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1022. return copy_regset_to_user(child, &user_x86_32_view,
  1023. REGSET_FP, 0,
  1024. sizeof(struct user_i387_ia32_struct),
  1025. datap);
  1026. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1027. return copy_regset_from_user(
  1028. child, &user_x86_32_view, REGSET_FP,
  1029. 0, sizeof(struct user_i387_ia32_struct), datap);
  1030. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1031. return copy_regset_to_user(child, &user_x86_32_view,
  1032. REGSET_XFP, 0,
  1033. sizeof(struct user32_fxsr_struct),
  1034. datap);
  1035. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1036. return copy_regset_from_user(child, &user_x86_32_view,
  1037. REGSET_XFP, 0,
  1038. sizeof(struct user32_fxsr_struct),
  1039. datap);
  1040. case PTRACE_GET_THREAD_AREA:
  1041. case PTRACE_SET_THREAD_AREA:
  1042. return arch_ptrace(child, request, addr, data);
  1043. default:
  1044. return compat_ptrace_request(child, request, addr, data);
  1045. }
  1046. return ret;
  1047. }
  1048. #endif /* CONFIG_IA32_EMULATION */
  1049. #ifdef CONFIG_X86_64
  1050. static struct user_regset x86_64_regsets[] __read_mostly = {
  1051. [REGSET_GENERAL] = {
  1052. .core_note_type = NT_PRSTATUS,
  1053. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1054. .size = sizeof(long), .align = sizeof(long),
  1055. .get = genregs_get, .set = genregs_set
  1056. },
  1057. [REGSET_FP] = {
  1058. .core_note_type = NT_PRFPREG,
  1059. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1060. .size = sizeof(long), .align = sizeof(long),
  1061. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1062. },
  1063. [REGSET_XSTATE] = {
  1064. .core_note_type = NT_X86_XSTATE,
  1065. .size = sizeof(u64), .align = sizeof(u64),
  1066. .active = xstateregs_active, .get = xstateregs_get,
  1067. .set = xstateregs_set
  1068. },
  1069. [REGSET_IOPERM64] = {
  1070. .core_note_type = NT_386_IOPERM,
  1071. .n = IO_BITMAP_LONGS,
  1072. .size = sizeof(long), .align = sizeof(long),
  1073. .active = ioperm_active, .get = ioperm_get
  1074. },
  1075. };
  1076. static const struct user_regset_view user_x86_64_view = {
  1077. .name = "x86_64", .e_machine = EM_X86_64,
  1078. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1079. };
  1080. #else /* CONFIG_X86_32 */
  1081. #define user_regs_struct32 user_regs_struct
  1082. #define genregs32_get genregs_get
  1083. #define genregs32_set genregs_set
  1084. #define user_i387_ia32_struct user_i387_struct
  1085. #define user32_fxsr_struct user_fxsr_struct
  1086. #endif /* CONFIG_X86_64 */
  1087. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1088. static struct user_regset x86_32_regsets[] __read_mostly = {
  1089. [REGSET_GENERAL] = {
  1090. .core_note_type = NT_PRSTATUS,
  1091. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1092. .size = sizeof(u32), .align = sizeof(u32),
  1093. .get = genregs32_get, .set = genregs32_set
  1094. },
  1095. [REGSET_FP] = {
  1096. .core_note_type = NT_PRFPREG,
  1097. .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
  1098. .size = sizeof(u32), .align = sizeof(u32),
  1099. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1100. },
  1101. [REGSET_XFP] = {
  1102. .core_note_type = NT_PRXFPREG,
  1103. .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
  1104. .size = sizeof(u32), .align = sizeof(u32),
  1105. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1106. },
  1107. [REGSET_XSTATE] = {
  1108. .core_note_type = NT_X86_XSTATE,
  1109. .size = sizeof(u64), .align = sizeof(u64),
  1110. .active = xstateregs_active, .get = xstateregs_get,
  1111. .set = xstateregs_set
  1112. },
  1113. [REGSET_TLS] = {
  1114. .core_note_type = NT_386_TLS,
  1115. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1116. .size = sizeof(struct user_desc),
  1117. .align = sizeof(struct user_desc),
  1118. .active = regset_tls_active,
  1119. .get = regset_tls_get, .set = regset_tls_set
  1120. },
  1121. [REGSET_IOPERM32] = {
  1122. .core_note_type = NT_386_IOPERM,
  1123. .n = IO_BITMAP_BYTES / sizeof(u32),
  1124. .size = sizeof(u32), .align = sizeof(u32),
  1125. .active = ioperm_active, .get = ioperm_get
  1126. },
  1127. };
  1128. static const struct user_regset_view user_x86_32_view = {
  1129. .name = "i386", .e_machine = EM_386,
  1130. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1131. };
  1132. #endif
  1133. /*
  1134. * This represents bytes 464..511 in the memory layout exported through
  1135. * the REGSET_XSTATE interface.
  1136. */
  1137. u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
  1138. void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
  1139. {
  1140. #ifdef CONFIG_X86_64
  1141. x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1142. #endif
  1143. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1144. x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
  1145. #endif
  1146. xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
  1147. }
  1148. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1149. {
  1150. #ifdef CONFIG_IA32_EMULATION
  1151. if (test_tsk_thread_flag(task, TIF_IA32))
  1152. #endif
  1153. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1154. return &user_x86_32_view;
  1155. #endif
  1156. #ifdef CONFIG_X86_64
  1157. return &user_x86_64_view;
  1158. #endif
  1159. }
  1160. static void fill_sigtrap_info(struct task_struct *tsk,
  1161. struct pt_regs *regs,
  1162. int error_code, int si_code,
  1163. struct siginfo *info)
  1164. {
  1165. tsk->thread.trap_no = 1;
  1166. tsk->thread.error_code = error_code;
  1167. memset(info, 0, sizeof(*info));
  1168. info->si_signo = SIGTRAP;
  1169. info->si_code = si_code;
  1170. info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
  1171. }
  1172. void user_single_step_siginfo(struct task_struct *tsk,
  1173. struct pt_regs *regs,
  1174. struct siginfo *info)
  1175. {
  1176. fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
  1177. }
  1178. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  1179. int error_code, int si_code)
  1180. {
  1181. struct siginfo info;
  1182. fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
  1183. /* Send us the fake SIGTRAP */
  1184. force_sig_info(SIGTRAP, &info, tsk);
  1185. }
  1186. #ifdef CONFIG_X86_32
  1187. # define IS_IA32 1
  1188. #elif defined CONFIG_IA32_EMULATION
  1189. # define IS_IA32 is_compat_task()
  1190. #else
  1191. # define IS_IA32 0
  1192. #endif
  1193. /*
  1194. * We must return the syscall number to actually look up in the table.
  1195. * This can be -1L to skip running any syscall at all.
  1196. */
  1197. long syscall_trace_enter(struct pt_regs *regs)
  1198. {
  1199. long ret = 0;
  1200. /*
  1201. * If we stepped into a sysenter/syscall insn, it trapped in
  1202. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  1203. * If user-mode had set TF itself, then it's still clear from
  1204. * do_debug() and we need to set it again to restore the user
  1205. * state. If we entered on the slow path, TF was already set.
  1206. */
  1207. if (test_thread_flag(TIF_SINGLESTEP))
  1208. regs->flags |= X86_EFLAGS_TF;
  1209. /* do the secure computing check first */
  1210. secure_computing(regs->orig_ax);
  1211. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1212. ret = -1L;
  1213. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  1214. tracehook_report_syscall_entry(regs))
  1215. ret = -1L;
  1216. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1217. trace_sys_enter(regs, regs->orig_ax);
  1218. if (IS_IA32)
  1219. audit_syscall_entry(AUDIT_ARCH_I386,
  1220. regs->orig_ax,
  1221. regs->bx, regs->cx,
  1222. regs->dx, regs->si);
  1223. #ifdef CONFIG_X86_64
  1224. else
  1225. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1226. regs->orig_ax,
  1227. regs->di, regs->si,
  1228. regs->dx, regs->r10);
  1229. #endif
  1230. return ret ?: regs->orig_ax;
  1231. }
  1232. void syscall_trace_leave(struct pt_regs *regs)
  1233. {
  1234. bool step;
  1235. audit_syscall_exit(regs);
  1236. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1237. trace_sys_exit(regs, regs->ax);
  1238. /*
  1239. * If TIF_SYSCALL_EMU is set, we only get here because of
  1240. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  1241. * We already reported this syscall instruction in
  1242. * syscall_trace_enter().
  1243. */
  1244. step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
  1245. !test_thread_flag(TIF_SYSCALL_EMU);
  1246. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  1247. tracehook_report_syscall_exit(regs, step);
  1248. }