ptrace.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. /* By Ross Biro 1/23/92 */
  2. /*
  3. * Pentium III FXSR, SSE support
  4. * Gareth Hughes <gareth@valinux.com>, May 2000
  5. *
  6. * BTS tracing
  7. * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/errno.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/regset.h>
  16. #include <linux/tracehook.h>
  17. #include <linux/user.h>
  18. #include <linux/elf.h>
  19. #include <linux/security.h>
  20. #include <linux/audit.h>
  21. #include <linux/seccomp.h>
  22. #include <linux/signal.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/hw_breakpoint.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/system.h>
  29. #include <asm/processor.h>
  30. #include <asm/i387.h>
  31. #include <asm/debugreg.h>
  32. #include <asm/ldt.h>
  33. #include <asm/desc.h>
  34. #include <asm/prctl.h>
  35. #include <asm/proto.h>
  36. #include <asm/ds.h>
  37. #include <asm/hw_breakpoint.h>
  38. #include "tls.h"
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/syscalls.h>
  41. enum x86_regset {
  42. REGSET_GENERAL,
  43. REGSET_FP,
  44. REGSET_XFP,
  45. REGSET_IOPERM64 = REGSET_XFP,
  46. REGSET_TLS,
  47. REGSET_IOPERM32,
  48. };
  49. struct pt_regs_offset {
  50. const char *name;
  51. int offset;
  52. };
  53. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  54. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  55. static const struct pt_regs_offset regoffset_table[] = {
  56. #ifdef CONFIG_X86_64
  57. REG_OFFSET_NAME(r15),
  58. REG_OFFSET_NAME(r14),
  59. REG_OFFSET_NAME(r13),
  60. REG_OFFSET_NAME(r12),
  61. REG_OFFSET_NAME(r11),
  62. REG_OFFSET_NAME(r10),
  63. REG_OFFSET_NAME(r9),
  64. REG_OFFSET_NAME(r8),
  65. #endif
  66. REG_OFFSET_NAME(bx),
  67. REG_OFFSET_NAME(cx),
  68. REG_OFFSET_NAME(dx),
  69. REG_OFFSET_NAME(si),
  70. REG_OFFSET_NAME(di),
  71. REG_OFFSET_NAME(bp),
  72. REG_OFFSET_NAME(ax),
  73. #ifdef CONFIG_X86_32
  74. REG_OFFSET_NAME(ds),
  75. REG_OFFSET_NAME(es),
  76. REG_OFFSET_NAME(fs),
  77. REG_OFFSET_NAME(gs),
  78. #endif
  79. REG_OFFSET_NAME(orig_ax),
  80. REG_OFFSET_NAME(ip),
  81. REG_OFFSET_NAME(cs),
  82. REG_OFFSET_NAME(flags),
  83. REG_OFFSET_NAME(sp),
  84. REG_OFFSET_NAME(ss),
  85. REG_OFFSET_END,
  86. };
  87. /**
  88. * regs_query_register_offset() - query register offset from its name
  89. * @name: the name of a register
  90. *
  91. * regs_query_register_offset() returns the offset of a register in struct
  92. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  93. */
  94. int regs_query_register_offset(const char *name)
  95. {
  96. const struct pt_regs_offset *roff;
  97. for (roff = regoffset_table; roff->name != NULL; roff++)
  98. if (!strcmp(roff->name, name))
  99. return roff->offset;
  100. return -EINVAL;
  101. }
  102. /**
  103. * regs_query_register_name() - query register name from its offset
  104. * @offset: the offset of a register in struct pt_regs.
  105. *
  106. * regs_query_register_name() returns the name of a register from its
  107. * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
  108. */
  109. const char *regs_query_register_name(unsigned int offset)
  110. {
  111. const struct pt_regs_offset *roff;
  112. for (roff = regoffset_table; roff->name != NULL; roff++)
  113. if (roff->offset == offset)
  114. return roff->name;
  115. return NULL;
  116. }
  117. static const int arg_offs_table[] = {
  118. #ifdef CONFIG_X86_32
  119. [0] = offsetof(struct pt_regs, ax),
  120. [1] = offsetof(struct pt_regs, dx),
  121. [2] = offsetof(struct pt_regs, cx)
  122. #else /* CONFIG_X86_64 */
  123. [0] = offsetof(struct pt_regs, di),
  124. [1] = offsetof(struct pt_regs, si),
  125. [2] = offsetof(struct pt_regs, dx),
  126. [3] = offsetof(struct pt_regs, cx),
  127. [4] = offsetof(struct pt_regs, r8),
  128. [5] = offsetof(struct pt_regs, r9)
  129. #endif
  130. };
  131. /**
  132. * regs_get_argument_nth() - get Nth argument at function call
  133. * @regs: pt_regs which contains registers at function entry.
  134. * @n: argument number.
  135. *
  136. * regs_get_argument_nth() returns @n th argument of a function call.
  137. * Since usually the kernel stack will be changed right after function entry,
  138. * you must use this at function entry. If the @n th entry is NOT in the
  139. * kernel stack or pt_regs, this returns 0.
  140. */
  141. unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned int n)
  142. {
  143. if (n < ARRAY_SIZE(arg_offs_table))
  144. return *(unsigned long *)((char *)regs + arg_offs_table[n]);
  145. else {
  146. /*
  147. * The typical case: arg n is on the stack.
  148. * (Note: stack[0] = return address, so skip it)
  149. */
  150. n -= ARRAY_SIZE(arg_offs_table);
  151. return regs_get_kernel_stack_nth(regs, 1 + n);
  152. }
  153. }
  154. /*
  155. * does not yet catch signals sent when the child dies.
  156. * in exit.c or in signal.c.
  157. */
  158. /*
  159. * Determines which flags the user has access to [1 = access, 0 = no access].
  160. */
  161. #define FLAG_MASK_32 ((unsigned long) \
  162. (X86_EFLAGS_CF | X86_EFLAGS_PF | \
  163. X86_EFLAGS_AF | X86_EFLAGS_ZF | \
  164. X86_EFLAGS_SF | X86_EFLAGS_TF | \
  165. X86_EFLAGS_DF | X86_EFLAGS_OF | \
  166. X86_EFLAGS_RF | X86_EFLAGS_AC))
  167. /*
  168. * Determines whether a value may be installed in a segment register.
  169. */
  170. static inline bool invalid_selector(u16 value)
  171. {
  172. return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
  173. }
  174. #ifdef CONFIG_X86_32
  175. #define FLAG_MASK FLAG_MASK_32
  176. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  177. {
  178. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  179. return &regs->bx + (regno >> 2);
  180. }
  181. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  182. {
  183. /*
  184. * Returning the value truncates it to 16 bits.
  185. */
  186. unsigned int retval;
  187. if (offset != offsetof(struct user_regs_struct, gs))
  188. retval = *pt_regs_access(task_pt_regs(task), offset);
  189. else {
  190. if (task == current)
  191. retval = get_user_gs(task_pt_regs(task));
  192. else
  193. retval = task_user_gs(task);
  194. }
  195. return retval;
  196. }
  197. static int set_segment_reg(struct task_struct *task,
  198. unsigned long offset, u16 value)
  199. {
  200. /*
  201. * The value argument was already truncated to 16 bits.
  202. */
  203. if (invalid_selector(value))
  204. return -EIO;
  205. /*
  206. * For %cs and %ss we cannot permit a null selector.
  207. * We can permit a bogus selector as long as it has USER_RPL.
  208. * Null selectors are fine for other segment registers, but
  209. * we will never get back to user mode with invalid %cs or %ss
  210. * and will take the trap in iret instead. Much code relies
  211. * on user_mode() to distinguish a user trap frame (which can
  212. * safely use invalid selectors) from a kernel trap frame.
  213. */
  214. switch (offset) {
  215. case offsetof(struct user_regs_struct, cs):
  216. case offsetof(struct user_regs_struct, ss):
  217. if (unlikely(value == 0))
  218. return -EIO;
  219. default:
  220. *pt_regs_access(task_pt_regs(task), offset) = value;
  221. break;
  222. case offsetof(struct user_regs_struct, gs):
  223. if (task == current)
  224. set_user_gs(task_pt_regs(task), value);
  225. else
  226. task_user_gs(task) = value;
  227. }
  228. return 0;
  229. }
  230. #else /* CONFIG_X86_64 */
  231. #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
  232. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
  233. {
  234. BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
  235. return &regs->r15 + (offset / sizeof(regs->r15));
  236. }
  237. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  238. {
  239. /*
  240. * Returning the value truncates it to 16 bits.
  241. */
  242. unsigned int seg;
  243. switch (offset) {
  244. case offsetof(struct user_regs_struct, fs):
  245. if (task == current) {
  246. /* Older gas can't assemble movq %?s,%r?? */
  247. asm("movl %%fs,%0" : "=r" (seg));
  248. return seg;
  249. }
  250. return task->thread.fsindex;
  251. case offsetof(struct user_regs_struct, gs):
  252. if (task == current) {
  253. asm("movl %%gs,%0" : "=r" (seg));
  254. return seg;
  255. }
  256. return task->thread.gsindex;
  257. case offsetof(struct user_regs_struct, ds):
  258. if (task == current) {
  259. asm("movl %%ds,%0" : "=r" (seg));
  260. return seg;
  261. }
  262. return task->thread.ds;
  263. case offsetof(struct user_regs_struct, es):
  264. if (task == current) {
  265. asm("movl %%es,%0" : "=r" (seg));
  266. return seg;
  267. }
  268. return task->thread.es;
  269. case offsetof(struct user_regs_struct, cs):
  270. case offsetof(struct user_regs_struct, ss):
  271. break;
  272. }
  273. return *pt_regs_access(task_pt_regs(task), offset);
  274. }
  275. static int set_segment_reg(struct task_struct *task,
  276. unsigned long offset, u16 value)
  277. {
  278. /*
  279. * The value argument was already truncated to 16 bits.
  280. */
  281. if (invalid_selector(value))
  282. return -EIO;
  283. switch (offset) {
  284. case offsetof(struct user_regs_struct,fs):
  285. /*
  286. * If this is setting fs as for normal 64-bit use but
  287. * setting fs_base has implicitly changed it, leave it.
  288. */
  289. if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
  290. task->thread.fs != 0) ||
  291. (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
  292. task->thread.fs == 0))
  293. break;
  294. task->thread.fsindex = value;
  295. if (task == current)
  296. loadsegment(fs, task->thread.fsindex);
  297. break;
  298. case offsetof(struct user_regs_struct,gs):
  299. /*
  300. * If this is setting gs as for normal 64-bit use but
  301. * setting gs_base has implicitly changed it, leave it.
  302. */
  303. if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
  304. task->thread.gs != 0) ||
  305. (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
  306. task->thread.gs == 0))
  307. break;
  308. task->thread.gsindex = value;
  309. if (task == current)
  310. load_gs_index(task->thread.gsindex);
  311. break;
  312. case offsetof(struct user_regs_struct,ds):
  313. task->thread.ds = value;
  314. if (task == current)
  315. loadsegment(ds, task->thread.ds);
  316. break;
  317. case offsetof(struct user_regs_struct,es):
  318. task->thread.es = value;
  319. if (task == current)
  320. loadsegment(es, task->thread.es);
  321. break;
  322. /*
  323. * Can't actually change these in 64-bit mode.
  324. */
  325. case offsetof(struct user_regs_struct,cs):
  326. if (unlikely(value == 0))
  327. return -EIO;
  328. #ifdef CONFIG_IA32_EMULATION
  329. if (test_tsk_thread_flag(task, TIF_IA32))
  330. task_pt_regs(task)->cs = value;
  331. #endif
  332. break;
  333. case offsetof(struct user_regs_struct,ss):
  334. if (unlikely(value == 0))
  335. return -EIO;
  336. #ifdef CONFIG_IA32_EMULATION
  337. if (test_tsk_thread_flag(task, TIF_IA32))
  338. task_pt_regs(task)->ss = value;
  339. #endif
  340. break;
  341. }
  342. return 0;
  343. }
  344. #endif /* CONFIG_X86_32 */
  345. static unsigned long get_flags(struct task_struct *task)
  346. {
  347. unsigned long retval = task_pt_regs(task)->flags;
  348. /*
  349. * If the debugger set TF, hide it from the readout.
  350. */
  351. if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  352. retval &= ~X86_EFLAGS_TF;
  353. return retval;
  354. }
  355. static int set_flags(struct task_struct *task, unsigned long value)
  356. {
  357. struct pt_regs *regs = task_pt_regs(task);
  358. /*
  359. * If the user value contains TF, mark that
  360. * it was not "us" (the debugger) that set it.
  361. * If not, make sure it stays set if we had.
  362. */
  363. if (value & X86_EFLAGS_TF)
  364. clear_tsk_thread_flag(task, TIF_FORCED_TF);
  365. else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  366. value |= X86_EFLAGS_TF;
  367. regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
  368. return 0;
  369. }
  370. static int putreg(struct task_struct *child,
  371. unsigned long offset, unsigned long value)
  372. {
  373. switch (offset) {
  374. case offsetof(struct user_regs_struct, cs):
  375. case offsetof(struct user_regs_struct, ds):
  376. case offsetof(struct user_regs_struct, es):
  377. case offsetof(struct user_regs_struct, fs):
  378. case offsetof(struct user_regs_struct, gs):
  379. case offsetof(struct user_regs_struct, ss):
  380. return set_segment_reg(child, offset, value);
  381. case offsetof(struct user_regs_struct, flags):
  382. return set_flags(child, value);
  383. #ifdef CONFIG_X86_64
  384. case offsetof(struct user_regs_struct,fs_base):
  385. if (value >= TASK_SIZE_OF(child))
  386. return -EIO;
  387. /*
  388. * When changing the segment base, use do_arch_prctl
  389. * to set either thread.fs or thread.fsindex and the
  390. * corresponding GDT slot.
  391. */
  392. if (child->thread.fs != value)
  393. return do_arch_prctl(child, ARCH_SET_FS, value);
  394. return 0;
  395. case offsetof(struct user_regs_struct,gs_base):
  396. /*
  397. * Exactly the same here as the %fs handling above.
  398. */
  399. if (value >= TASK_SIZE_OF(child))
  400. return -EIO;
  401. if (child->thread.gs != value)
  402. return do_arch_prctl(child, ARCH_SET_GS, value);
  403. return 0;
  404. #endif
  405. }
  406. *pt_regs_access(task_pt_regs(child), offset) = value;
  407. return 0;
  408. }
  409. static unsigned long getreg(struct task_struct *task, unsigned long offset)
  410. {
  411. switch (offset) {
  412. case offsetof(struct user_regs_struct, cs):
  413. case offsetof(struct user_regs_struct, ds):
  414. case offsetof(struct user_regs_struct, es):
  415. case offsetof(struct user_regs_struct, fs):
  416. case offsetof(struct user_regs_struct, gs):
  417. case offsetof(struct user_regs_struct, ss):
  418. return get_segment_reg(task, offset);
  419. case offsetof(struct user_regs_struct, flags):
  420. return get_flags(task);
  421. #ifdef CONFIG_X86_64
  422. case offsetof(struct user_regs_struct, fs_base): {
  423. /*
  424. * do_arch_prctl may have used a GDT slot instead of
  425. * the MSR. To userland, it appears the same either
  426. * way, except the %fs segment selector might not be 0.
  427. */
  428. unsigned int seg = task->thread.fsindex;
  429. if (task->thread.fs != 0)
  430. return task->thread.fs;
  431. if (task == current)
  432. asm("movl %%fs,%0" : "=r" (seg));
  433. if (seg != FS_TLS_SEL)
  434. return 0;
  435. return get_desc_base(&task->thread.tls_array[FS_TLS]);
  436. }
  437. case offsetof(struct user_regs_struct, gs_base): {
  438. /*
  439. * Exactly the same here as the %fs handling above.
  440. */
  441. unsigned int seg = task->thread.gsindex;
  442. if (task->thread.gs != 0)
  443. return task->thread.gs;
  444. if (task == current)
  445. asm("movl %%gs,%0" : "=r" (seg));
  446. if (seg != GS_TLS_SEL)
  447. return 0;
  448. return get_desc_base(&task->thread.tls_array[GS_TLS]);
  449. }
  450. #endif
  451. }
  452. return *pt_regs_access(task_pt_regs(task), offset);
  453. }
  454. static int genregs_get(struct task_struct *target,
  455. const struct user_regset *regset,
  456. unsigned int pos, unsigned int count,
  457. void *kbuf, void __user *ubuf)
  458. {
  459. if (kbuf) {
  460. unsigned long *k = kbuf;
  461. while (count > 0) {
  462. *k++ = getreg(target, pos);
  463. count -= sizeof(*k);
  464. pos += sizeof(*k);
  465. }
  466. } else {
  467. unsigned long __user *u = ubuf;
  468. while (count > 0) {
  469. if (__put_user(getreg(target, pos), u++))
  470. return -EFAULT;
  471. count -= sizeof(*u);
  472. pos += sizeof(*u);
  473. }
  474. }
  475. return 0;
  476. }
  477. static int genregs_set(struct task_struct *target,
  478. const struct user_regset *regset,
  479. unsigned int pos, unsigned int count,
  480. const void *kbuf, const void __user *ubuf)
  481. {
  482. int ret = 0;
  483. if (kbuf) {
  484. const unsigned long *k = kbuf;
  485. while (count > 0 && !ret) {
  486. ret = putreg(target, pos, *k++);
  487. count -= sizeof(*k);
  488. pos += sizeof(*k);
  489. }
  490. } else {
  491. const unsigned long __user *u = ubuf;
  492. while (count > 0 && !ret) {
  493. unsigned long word;
  494. ret = __get_user(word, u++);
  495. if (ret)
  496. break;
  497. ret = putreg(target, pos, word);
  498. count -= sizeof(*u);
  499. pos += sizeof(*u);
  500. }
  501. }
  502. return ret;
  503. }
  504. static void ptrace_triggered(struct perf_event *bp, void *data)
  505. {
  506. int i;
  507. struct thread_struct *thread = &(current->thread);
  508. /*
  509. * Store in the virtual DR6 register the fact that the breakpoint
  510. * was hit so the thread's debugger will see it.
  511. */
  512. for (i = 0; i < HBP_NUM; i++) {
  513. if (thread->ptrace_bps[i] == bp)
  514. break;
  515. }
  516. thread->debugreg6 |= (DR_TRAP0 << i);
  517. }
  518. /*
  519. * Walk through every ptrace breakpoints for this thread and
  520. * build the dr7 value on top of their attributes.
  521. *
  522. */
  523. static unsigned long ptrace_get_dr7(struct perf_event *bp[])
  524. {
  525. int i;
  526. int dr7 = 0;
  527. struct arch_hw_breakpoint *info;
  528. for (i = 0; i < HBP_NUM; i++) {
  529. if (bp[i] && !bp[i]->attr.disabled) {
  530. info = counter_arch_bp(bp[i]);
  531. dr7 |= encode_dr7(i, info->len, info->type);
  532. }
  533. }
  534. return dr7;
  535. }
  536. /*
  537. * Handle ptrace writes to debug register 7.
  538. */
  539. static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
  540. {
  541. struct thread_struct *thread = &(tsk->thread);
  542. unsigned long old_dr7;
  543. int i, orig_ret = 0, rc = 0;
  544. int enabled, second_pass = 0;
  545. unsigned len, type;
  546. int gen_len, gen_type;
  547. struct perf_event *bp;
  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. thread->ptrace_bps[i] = NULL;
  570. unregister_hw_breakpoint(bp);
  571. }
  572. continue;
  573. }
  574. /*
  575. * We shoud have at least an inactive breakpoint at this
  576. * slot. It means the user is writing dr7 without having
  577. * written the address register first
  578. */
  579. if (!bp) {
  580. rc = -EINVAL;
  581. break;
  582. }
  583. rc = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
  584. if (rc)
  585. break;
  586. /*
  587. * This is a temporary thing as bp is unregistered/registered
  588. * to simulate modification
  589. */
  590. bp = modify_user_hw_breakpoint(bp, bp->attr.bp_addr, gen_len,
  591. gen_type, bp->callback,
  592. tsk, true);
  593. thread->ptrace_bps[i] = NULL;
  594. if (!bp) { /* incorrect bp, or we have a bug in bp API */
  595. rc = -EINVAL;
  596. break;
  597. }
  598. if (IS_ERR(bp)) {
  599. rc = PTR_ERR(bp);
  600. bp = NULL;
  601. break;
  602. }
  603. thread->ptrace_bps[i] = bp;
  604. }
  605. /*
  606. * Make a second pass to free the remaining unused breakpoints
  607. * or to restore the original breakpoints if an error occurred.
  608. */
  609. if (!second_pass) {
  610. second_pass = 1;
  611. if (rc < 0) {
  612. orig_ret = rc;
  613. data = old_dr7;
  614. }
  615. goto restore;
  616. }
  617. return ((orig_ret < 0) ? orig_ret : rc);
  618. }
  619. /*
  620. * Handle PTRACE_PEEKUSR calls for the debug register area.
  621. */
  622. static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
  623. {
  624. struct thread_struct *thread = &(tsk->thread);
  625. unsigned long val = 0;
  626. if (n < HBP_NUM) {
  627. struct perf_event *bp;
  628. bp = thread->ptrace_bps[n];
  629. if (!bp)
  630. return 0;
  631. val = bp->hw.info.address;
  632. } else if (n == 6) {
  633. val = thread->debugreg6;
  634. } else if (n == 7) {
  635. val = ptrace_get_dr7(thread->ptrace_bps);
  636. }
  637. return val;
  638. }
  639. static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
  640. unsigned long addr)
  641. {
  642. struct perf_event *bp;
  643. struct thread_struct *t = &tsk->thread;
  644. if (!t->ptrace_bps[nr]) {
  645. /*
  646. * Put stub len and type to register (reserve) an inactive but
  647. * correct bp
  648. */
  649. bp = register_user_hw_breakpoint(addr, HW_BREAKPOINT_LEN_1,
  650. HW_BREAKPOINT_W,
  651. ptrace_triggered, tsk,
  652. false);
  653. } else {
  654. bp = t->ptrace_bps[nr];
  655. t->ptrace_bps[nr] = NULL;
  656. bp = modify_user_hw_breakpoint(bp, addr, bp->attr.bp_len,
  657. bp->attr.bp_type,
  658. bp->callback,
  659. tsk,
  660. bp->attr.disabled);
  661. }
  662. if (!bp)
  663. return -EIO;
  664. /*
  665. * CHECKME: the previous code returned -EIO if the addr wasn't a
  666. * valid task virtual addr. The new one will return -EINVAL in this
  667. * case.
  668. * -EINVAL may be what we want for in-kernel breakpoints users, but
  669. * -EIO looks better for ptrace, since we refuse a register writing
  670. * for the user. And anyway this is the previous behaviour.
  671. */
  672. if (IS_ERR(bp))
  673. return PTR_ERR(bp);
  674. t->ptrace_bps[nr] = bp;
  675. return 0;
  676. }
  677. /*
  678. * Handle PTRACE_POKEUSR calls for the debug register area.
  679. */
  680. int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
  681. {
  682. struct thread_struct *thread = &(tsk->thread);
  683. int rc = 0;
  684. /* There are no DR4 or DR5 registers */
  685. if (n == 4 || n == 5)
  686. return -EIO;
  687. if (n == 6) {
  688. thread->debugreg6 = val;
  689. goto ret_path;
  690. }
  691. if (n < HBP_NUM) {
  692. rc = ptrace_set_breakpoint_addr(tsk, n, val);
  693. if (rc)
  694. return rc;
  695. }
  696. /* All that's left is DR7 */
  697. if (n == 7)
  698. rc = ptrace_write_dr7(tsk, val);
  699. ret_path:
  700. return rc;
  701. }
  702. /*
  703. * These access the current or another (stopped) task's io permission
  704. * bitmap for debugging or core dump.
  705. */
  706. static int ioperm_active(struct task_struct *target,
  707. const struct user_regset *regset)
  708. {
  709. return target->thread.io_bitmap_max / regset->size;
  710. }
  711. static int ioperm_get(struct task_struct *target,
  712. const struct user_regset *regset,
  713. unsigned int pos, unsigned int count,
  714. void *kbuf, void __user *ubuf)
  715. {
  716. if (!target->thread.io_bitmap_ptr)
  717. return -ENXIO;
  718. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  719. target->thread.io_bitmap_ptr,
  720. 0, IO_BITMAP_BYTES);
  721. }
  722. #ifdef CONFIG_X86_PTRACE_BTS
  723. /*
  724. * A branch trace store context.
  725. *
  726. * Contexts may only be installed by ptrace_bts_config() and only for
  727. * ptraced tasks.
  728. *
  729. * Contexts are destroyed when the tracee is detached from the tracer.
  730. * The actual destruction work requires interrupts enabled, so the
  731. * work is deferred and will be scheduled during __ptrace_unlink().
  732. *
  733. * Contexts hold an additional task_struct reference on the traced
  734. * task, as well as a reference on the tracer's mm.
  735. *
  736. * Ptrace already holds a task_struct for the duration of ptrace operations,
  737. * but since destruction is deferred, it may be executed after both
  738. * tracer and tracee exited.
  739. */
  740. struct bts_context {
  741. /* The branch trace handle. */
  742. struct bts_tracer *tracer;
  743. /* The buffer used to store the branch trace and its size. */
  744. void *buffer;
  745. unsigned int size;
  746. /* The mm that paid for the above buffer. */
  747. struct mm_struct *mm;
  748. /* The task this context belongs to. */
  749. struct task_struct *task;
  750. /* The signal to send on a bts buffer overflow. */
  751. unsigned int bts_ovfl_signal;
  752. /* The work struct to destroy a context. */
  753. struct work_struct work;
  754. };
  755. static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
  756. {
  757. void *buffer = NULL;
  758. int err = -ENOMEM;
  759. err = account_locked_memory(current->mm, current->signal->rlim, size);
  760. if (err < 0)
  761. return err;
  762. buffer = kzalloc(size, GFP_KERNEL);
  763. if (!buffer)
  764. goto out_refund;
  765. context->buffer = buffer;
  766. context->size = size;
  767. context->mm = get_task_mm(current);
  768. return 0;
  769. out_refund:
  770. refund_locked_memory(current->mm, size);
  771. return err;
  772. }
  773. static inline void free_bts_buffer(struct bts_context *context)
  774. {
  775. if (!context->buffer)
  776. return;
  777. kfree(context->buffer);
  778. context->buffer = NULL;
  779. refund_locked_memory(context->mm, context->size);
  780. context->size = 0;
  781. mmput(context->mm);
  782. context->mm = NULL;
  783. }
  784. static void free_bts_context_work(struct work_struct *w)
  785. {
  786. struct bts_context *context;
  787. context = container_of(w, struct bts_context, work);
  788. ds_release_bts(context->tracer);
  789. put_task_struct(context->task);
  790. free_bts_buffer(context);
  791. kfree(context);
  792. }
  793. static inline void free_bts_context(struct bts_context *context)
  794. {
  795. INIT_WORK(&context->work, free_bts_context_work);
  796. schedule_work(&context->work);
  797. }
  798. static inline struct bts_context *alloc_bts_context(struct task_struct *task)
  799. {
  800. struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
  801. if (context) {
  802. context->task = task;
  803. task->bts = context;
  804. get_task_struct(task);
  805. }
  806. return context;
  807. }
  808. static int ptrace_bts_read_record(struct task_struct *child, size_t index,
  809. struct bts_struct __user *out)
  810. {
  811. struct bts_context *context;
  812. const struct bts_trace *trace;
  813. struct bts_struct bts;
  814. const unsigned char *at;
  815. int error;
  816. context = child->bts;
  817. if (!context)
  818. return -ESRCH;
  819. trace = ds_read_bts(context->tracer);
  820. if (!trace)
  821. return -ESRCH;
  822. at = trace->ds.top - ((index + 1) * trace->ds.size);
  823. if ((void *)at < trace->ds.begin)
  824. at += (trace->ds.n * trace->ds.size);
  825. if (!trace->read)
  826. return -EOPNOTSUPP;
  827. error = trace->read(context->tracer, at, &bts);
  828. if (error < 0)
  829. return error;
  830. if (copy_to_user(out, &bts, sizeof(bts)))
  831. return -EFAULT;
  832. return sizeof(bts);
  833. }
  834. static int ptrace_bts_drain(struct task_struct *child,
  835. long size,
  836. struct bts_struct __user *out)
  837. {
  838. struct bts_context *context;
  839. const struct bts_trace *trace;
  840. const unsigned char *at;
  841. int error, drained = 0;
  842. context = child->bts;
  843. if (!context)
  844. return -ESRCH;
  845. trace = ds_read_bts(context->tracer);
  846. if (!trace)
  847. return -ESRCH;
  848. if (!trace->read)
  849. return -EOPNOTSUPP;
  850. if (size < (trace->ds.top - trace->ds.begin))
  851. return -EIO;
  852. for (at = trace->ds.begin; (void *)at < trace->ds.top;
  853. out++, drained++, at += trace->ds.size) {
  854. struct bts_struct bts;
  855. error = trace->read(context->tracer, at, &bts);
  856. if (error < 0)
  857. return error;
  858. if (copy_to_user(out, &bts, sizeof(bts)))
  859. return -EFAULT;
  860. }
  861. memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
  862. error = ds_reset_bts(context->tracer);
  863. if (error < 0)
  864. return error;
  865. return drained;
  866. }
  867. static int ptrace_bts_config(struct task_struct *child,
  868. long cfg_size,
  869. const struct ptrace_bts_config __user *ucfg)
  870. {
  871. struct bts_context *context;
  872. struct ptrace_bts_config cfg;
  873. unsigned int flags = 0;
  874. if (cfg_size < sizeof(cfg))
  875. return -EIO;
  876. if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
  877. return -EFAULT;
  878. context = child->bts;
  879. if (!context)
  880. context = alloc_bts_context(child);
  881. if (!context)
  882. return -ENOMEM;
  883. if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
  884. if (!cfg.signal)
  885. return -EINVAL;
  886. return -EOPNOTSUPP;
  887. context->bts_ovfl_signal = cfg.signal;
  888. }
  889. ds_release_bts(context->tracer);
  890. context->tracer = NULL;
  891. if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
  892. int err;
  893. free_bts_buffer(context);
  894. if (!cfg.size)
  895. return 0;
  896. err = alloc_bts_buffer(context, cfg.size);
  897. if (err < 0)
  898. return err;
  899. }
  900. if (cfg.flags & PTRACE_BTS_O_TRACE)
  901. flags |= BTS_USER;
  902. if (cfg.flags & PTRACE_BTS_O_SCHED)
  903. flags |= BTS_TIMESTAMPS;
  904. context->tracer =
  905. ds_request_bts_task(child, context->buffer, context->size,
  906. NULL, (size_t)-1, flags);
  907. if (unlikely(IS_ERR(context->tracer))) {
  908. int error = PTR_ERR(context->tracer);
  909. free_bts_buffer(context);
  910. context->tracer = NULL;
  911. return error;
  912. }
  913. return sizeof(cfg);
  914. }
  915. static int ptrace_bts_status(struct task_struct *child,
  916. long cfg_size,
  917. struct ptrace_bts_config __user *ucfg)
  918. {
  919. struct bts_context *context;
  920. const struct bts_trace *trace;
  921. struct ptrace_bts_config cfg;
  922. context = child->bts;
  923. if (!context)
  924. return -ESRCH;
  925. if (cfg_size < sizeof(cfg))
  926. return -EIO;
  927. trace = ds_read_bts(context->tracer);
  928. if (!trace)
  929. return -ESRCH;
  930. memset(&cfg, 0, sizeof(cfg));
  931. cfg.size = trace->ds.end - trace->ds.begin;
  932. cfg.signal = context->bts_ovfl_signal;
  933. cfg.bts_size = sizeof(struct bts_struct);
  934. if (cfg.signal)
  935. cfg.flags |= PTRACE_BTS_O_SIGNAL;
  936. if (trace->ds.flags & BTS_USER)
  937. cfg.flags |= PTRACE_BTS_O_TRACE;
  938. if (trace->ds.flags & BTS_TIMESTAMPS)
  939. cfg.flags |= PTRACE_BTS_O_SCHED;
  940. if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
  941. return -EFAULT;
  942. return sizeof(cfg);
  943. }
  944. static int ptrace_bts_clear(struct task_struct *child)
  945. {
  946. struct bts_context *context;
  947. const struct bts_trace *trace;
  948. context = child->bts;
  949. if (!context)
  950. return -ESRCH;
  951. trace = ds_read_bts(context->tracer);
  952. if (!trace)
  953. return -ESRCH;
  954. memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
  955. return ds_reset_bts(context->tracer);
  956. }
  957. static int ptrace_bts_size(struct task_struct *child)
  958. {
  959. struct bts_context *context;
  960. const struct bts_trace *trace;
  961. context = child->bts;
  962. if (!context)
  963. return -ESRCH;
  964. trace = ds_read_bts(context->tracer);
  965. if (!trace)
  966. return -ESRCH;
  967. return (trace->ds.top - trace->ds.begin) / trace->ds.size;
  968. }
  969. /*
  970. * Called from __ptrace_unlink() after the child has been moved back
  971. * to its original parent.
  972. */
  973. void ptrace_bts_untrace(struct task_struct *child)
  974. {
  975. if (unlikely(child->bts)) {
  976. free_bts_context(child->bts);
  977. child->bts = NULL;
  978. }
  979. }
  980. #endif /* CONFIG_X86_PTRACE_BTS */
  981. /*
  982. * Called by kernel/ptrace.c when detaching..
  983. *
  984. * Make sure the single step bit is not set.
  985. */
  986. void ptrace_disable(struct task_struct *child)
  987. {
  988. user_disable_single_step(child);
  989. #ifdef TIF_SYSCALL_EMU
  990. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  991. #endif
  992. }
  993. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  994. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  995. #endif
  996. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  997. {
  998. int ret;
  999. unsigned long __user *datap = (unsigned long __user *)data;
  1000. switch (request) {
  1001. /* read the word at location addr in the USER area. */
  1002. case PTRACE_PEEKUSR: {
  1003. unsigned long tmp;
  1004. ret = -EIO;
  1005. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  1006. addr >= sizeof(struct user))
  1007. break;
  1008. tmp = 0; /* Default return condition */
  1009. if (addr < sizeof(struct user_regs_struct))
  1010. tmp = getreg(child, addr);
  1011. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  1012. addr <= offsetof(struct user, u_debugreg[7])) {
  1013. addr -= offsetof(struct user, u_debugreg[0]);
  1014. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  1015. }
  1016. ret = put_user(tmp, datap);
  1017. break;
  1018. }
  1019. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  1020. ret = -EIO;
  1021. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  1022. addr >= sizeof(struct user))
  1023. break;
  1024. if (addr < sizeof(struct user_regs_struct))
  1025. ret = putreg(child, addr, data);
  1026. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  1027. addr <= offsetof(struct user, u_debugreg[7])) {
  1028. addr -= offsetof(struct user, u_debugreg[0]);
  1029. ret = ptrace_set_debugreg(child,
  1030. addr / sizeof(data), data);
  1031. }
  1032. break;
  1033. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1034. return copy_regset_to_user(child,
  1035. task_user_regset_view(current),
  1036. REGSET_GENERAL,
  1037. 0, sizeof(struct user_regs_struct),
  1038. datap);
  1039. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1040. return copy_regset_from_user(child,
  1041. task_user_regset_view(current),
  1042. REGSET_GENERAL,
  1043. 0, sizeof(struct user_regs_struct),
  1044. datap);
  1045. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1046. return copy_regset_to_user(child,
  1047. task_user_regset_view(current),
  1048. REGSET_FP,
  1049. 0, sizeof(struct user_i387_struct),
  1050. datap);
  1051. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1052. return copy_regset_from_user(child,
  1053. task_user_regset_view(current),
  1054. REGSET_FP,
  1055. 0, sizeof(struct user_i387_struct),
  1056. datap);
  1057. #ifdef CONFIG_X86_32
  1058. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1059. return copy_regset_to_user(child, &user_x86_32_view,
  1060. REGSET_XFP,
  1061. 0, sizeof(struct user_fxsr_struct),
  1062. datap) ? -EIO : 0;
  1063. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1064. return copy_regset_from_user(child, &user_x86_32_view,
  1065. REGSET_XFP,
  1066. 0, sizeof(struct user_fxsr_struct),
  1067. datap) ? -EIO : 0;
  1068. #endif
  1069. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1070. case PTRACE_GET_THREAD_AREA:
  1071. if (addr < 0)
  1072. return -EIO;
  1073. ret = do_get_thread_area(child, addr,
  1074. (struct user_desc __user *) data);
  1075. break;
  1076. case PTRACE_SET_THREAD_AREA:
  1077. if (addr < 0)
  1078. return -EIO;
  1079. ret = do_set_thread_area(child, addr,
  1080. (struct user_desc __user *) data, 0);
  1081. break;
  1082. #endif
  1083. #ifdef CONFIG_X86_64
  1084. /* normal 64bit interface to access TLS data.
  1085. Works just like arch_prctl, except that the arguments
  1086. are reversed. */
  1087. case PTRACE_ARCH_PRCTL:
  1088. ret = do_arch_prctl(child, data, addr);
  1089. break;
  1090. #endif
  1091. /*
  1092. * These bits need more cooking - not enabled yet:
  1093. */
  1094. #ifdef CONFIG_X86_PTRACE_BTS
  1095. case PTRACE_BTS_CONFIG:
  1096. ret = ptrace_bts_config
  1097. (child, data, (struct ptrace_bts_config __user *)addr);
  1098. break;
  1099. case PTRACE_BTS_STATUS:
  1100. ret = ptrace_bts_status
  1101. (child, data, (struct ptrace_bts_config __user *)addr);
  1102. break;
  1103. case PTRACE_BTS_SIZE:
  1104. ret = ptrace_bts_size(child);
  1105. break;
  1106. case PTRACE_BTS_GET:
  1107. ret = ptrace_bts_read_record
  1108. (child, data, (struct bts_struct __user *) addr);
  1109. break;
  1110. case PTRACE_BTS_CLEAR:
  1111. ret = ptrace_bts_clear(child);
  1112. break;
  1113. case PTRACE_BTS_DRAIN:
  1114. ret = ptrace_bts_drain
  1115. (child, data, (struct bts_struct __user *) addr);
  1116. break;
  1117. #endif /* CONFIG_X86_PTRACE_BTS */
  1118. default:
  1119. ret = ptrace_request(child, request, addr, data);
  1120. break;
  1121. }
  1122. return ret;
  1123. }
  1124. #ifdef CONFIG_IA32_EMULATION
  1125. #include <linux/compat.h>
  1126. #include <linux/syscalls.h>
  1127. #include <asm/ia32.h>
  1128. #include <asm/user32.h>
  1129. #define R32(l,q) \
  1130. case offsetof(struct user32, regs.l): \
  1131. regs->q = value; break
  1132. #define SEG32(rs) \
  1133. case offsetof(struct user32, regs.rs): \
  1134. return set_segment_reg(child, \
  1135. offsetof(struct user_regs_struct, rs), \
  1136. value); \
  1137. break
  1138. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  1139. {
  1140. struct pt_regs *regs = task_pt_regs(child);
  1141. switch (regno) {
  1142. SEG32(cs);
  1143. SEG32(ds);
  1144. SEG32(es);
  1145. SEG32(fs);
  1146. SEG32(gs);
  1147. SEG32(ss);
  1148. R32(ebx, bx);
  1149. R32(ecx, cx);
  1150. R32(edx, dx);
  1151. R32(edi, di);
  1152. R32(esi, si);
  1153. R32(ebp, bp);
  1154. R32(eax, ax);
  1155. R32(eip, ip);
  1156. R32(esp, sp);
  1157. case offsetof(struct user32, regs.orig_eax):
  1158. /*
  1159. * A 32-bit debugger setting orig_eax means to restore
  1160. * the state of the task restarting a 32-bit syscall.
  1161. * Make sure we interpret the -ERESTART* codes correctly
  1162. * in case the task is not actually still sitting at the
  1163. * exit from a 32-bit syscall with TS_COMPAT still set.
  1164. */
  1165. regs->orig_ax = value;
  1166. if (syscall_get_nr(child, regs) >= 0)
  1167. task_thread_info(child)->status |= TS_COMPAT;
  1168. break;
  1169. case offsetof(struct user32, regs.eflags):
  1170. return set_flags(child, value);
  1171. case offsetof(struct user32, u_debugreg[0]) ...
  1172. offsetof(struct user32, u_debugreg[7]):
  1173. regno -= offsetof(struct user32, u_debugreg[0]);
  1174. return ptrace_set_debugreg(child, regno / 4, value);
  1175. default:
  1176. if (regno > sizeof(struct user32) || (regno & 3))
  1177. return -EIO;
  1178. /*
  1179. * Other dummy fields in the virtual user structure
  1180. * are ignored
  1181. */
  1182. break;
  1183. }
  1184. return 0;
  1185. }
  1186. #undef R32
  1187. #undef SEG32
  1188. #define R32(l,q) \
  1189. case offsetof(struct user32, regs.l): \
  1190. *val = regs->q; break
  1191. #define SEG32(rs) \
  1192. case offsetof(struct user32, regs.rs): \
  1193. *val = get_segment_reg(child, \
  1194. offsetof(struct user_regs_struct, rs)); \
  1195. break
  1196. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  1197. {
  1198. struct pt_regs *regs = task_pt_regs(child);
  1199. switch (regno) {
  1200. SEG32(ds);
  1201. SEG32(es);
  1202. SEG32(fs);
  1203. SEG32(gs);
  1204. R32(cs, cs);
  1205. R32(ss, ss);
  1206. R32(ebx, bx);
  1207. R32(ecx, cx);
  1208. R32(edx, dx);
  1209. R32(edi, di);
  1210. R32(esi, si);
  1211. R32(ebp, bp);
  1212. R32(eax, ax);
  1213. R32(orig_eax, orig_ax);
  1214. R32(eip, ip);
  1215. R32(esp, sp);
  1216. case offsetof(struct user32, regs.eflags):
  1217. *val = get_flags(child);
  1218. break;
  1219. case offsetof(struct user32, u_debugreg[0]) ...
  1220. offsetof(struct user32, u_debugreg[7]):
  1221. regno -= offsetof(struct user32, u_debugreg[0]);
  1222. *val = ptrace_get_debugreg(child, regno / 4);
  1223. break;
  1224. default:
  1225. if (regno > sizeof(struct user32) || (regno & 3))
  1226. return -EIO;
  1227. /*
  1228. * Other dummy fields in the virtual user structure
  1229. * are ignored
  1230. */
  1231. *val = 0;
  1232. break;
  1233. }
  1234. return 0;
  1235. }
  1236. #undef R32
  1237. #undef SEG32
  1238. static int genregs32_get(struct task_struct *target,
  1239. const struct user_regset *regset,
  1240. unsigned int pos, unsigned int count,
  1241. void *kbuf, void __user *ubuf)
  1242. {
  1243. if (kbuf) {
  1244. compat_ulong_t *k = kbuf;
  1245. while (count > 0) {
  1246. getreg32(target, pos, k++);
  1247. count -= sizeof(*k);
  1248. pos += sizeof(*k);
  1249. }
  1250. } else {
  1251. compat_ulong_t __user *u = ubuf;
  1252. while (count > 0) {
  1253. compat_ulong_t word;
  1254. getreg32(target, pos, &word);
  1255. if (__put_user(word, u++))
  1256. return -EFAULT;
  1257. count -= sizeof(*u);
  1258. pos += sizeof(*u);
  1259. }
  1260. }
  1261. return 0;
  1262. }
  1263. static int genregs32_set(struct task_struct *target,
  1264. const struct user_regset *regset,
  1265. unsigned int pos, unsigned int count,
  1266. const void *kbuf, const void __user *ubuf)
  1267. {
  1268. int ret = 0;
  1269. if (kbuf) {
  1270. const compat_ulong_t *k = kbuf;
  1271. while (count > 0 && !ret) {
  1272. ret = putreg32(target, pos, *k++);
  1273. count -= sizeof(*k);
  1274. pos += sizeof(*k);
  1275. }
  1276. } else {
  1277. const compat_ulong_t __user *u = ubuf;
  1278. while (count > 0 && !ret) {
  1279. compat_ulong_t word;
  1280. ret = __get_user(word, u++);
  1281. if (ret)
  1282. break;
  1283. ret = putreg32(target, pos, word);
  1284. count -= sizeof(*u);
  1285. pos += sizeof(*u);
  1286. }
  1287. }
  1288. return ret;
  1289. }
  1290. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  1291. compat_ulong_t caddr, compat_ulong_t cdata)
  1292. {
  1293. unsigned long addr = caddr;
  1294. unsigned long data = cdata;
  1295. void __user *datap = compat_ptr(data);
  1296. int ret;
  1297. __u32 val;
  1298. switch (request) {
  1299. case PTRACE_PEEKUSR:
  1300. ret = getreg32(child, addr, &val);
  1301. if (ret == 0)
  1302. ret = put_user(val, (__u32 __user *)datap);
  1303. break;
  1304. case PTRACE_POKEUSR:
  1305. ret = putreg32(child, addr, data);
  1306. break;
  1307. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1308. return copy_regset_to_user(child, &user_x86_32_view,
  1309. REGSET_GENERAL,
  1310. 0, sizeof(struct user_regs_struct32),
  1311. datap);
  1312. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1313. return copy_regset_from_user(child, &user_x86_32_view,
  1314. REGSET_GENERAL, 0,
  1315. sizeof(struct user_regs_struct32),
  1316. datap);
  1317. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1318. return copy_regset_to_user(child, &user_x86_32_view,
  1319. REGSET_FP, 0,
  1320. sizeof(struct user_i387_ia32_struct),
  1321. datap);
  1322. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1323. return copy_regset_from_user(
  1324. child, &user_x86_32_view, REGSET_FP,
  1325. 0, sizeof(struct user_i387_ia32_struct), datap);
  1326. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1327. return copy_regset_to_user(child, &user_x86_32_view,
  1328. REGSET_XFP, 0,
  1329. sizeof(struct user32_fxsr_struct),
  1330. datap);
  1331. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1332. return copy_regset_from_user(child, &user_x86_32_view,
  1333. REGSET_XFP, 0,
  1334. sizeof(struct user32_fxsr_struct),
  1335. datap);
  1336. case PTRACE_GET_THREAD_AREA:
  1337. case PTRACE_SET_THREAD_AREA:
  1338. #ifdef CONFIG_X86_PTRACE_BTS
  1339. case PTRACE_BTS_CONFIG:
  1340. case PTRACE_BTS_STATUS:
  1341. case PTRACE_BTS_SIZE:
  1342. case PTRACE_BTS_GET:
  1343. case PTRACE_BTS_CLEAR:
  1344. case PTRACE_BTS_DRAIN:
  1345. #endif /* CONFIG_X86_PTRACE_BTS */
  1346. return arch_ptrace(child, request, addr, data);
  1347. default:
  1348. return compat_ptrace_request(child, request, addr, data);
  1349. }
  1350. return ret;
  1351. }
  1352. #endif /* CONFIG_IA32_EMULATION */
  1353. #ifdef CONFIG_X86_64
  1354. static const struct user_regset x86_64_regsets[] = {
  1355. [REGSET_GENERAL] = {
  1356. .core_note_type = NT_PRSTATUS,
  1357. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1358. .size = sizeof(long), .align = sizeof(long),
  1359. .get = genregs_get, .set = genregs_set
  1360. },
  1361. [REGSET_FP] = {
  1362. .core_note_type = NT_PRFPREG,
  1363. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1364. .size = sizeof(long), .align = sizeof(long),
  1365. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1366. },
  1367. [REGSET_IOPERM64] = {
  1368. .core_note_type = NT_386_IOPERM,
  1369. .n = IO_BITMAP_LONGS,
  1370. .size = sizeof(long), .align = sizeof(long),
  1371. .active = ioperm_active, .get = ioperm_get
  1372. },
  1373. };
  1374. static const struct user_regset_view user_x86_64_view = {
  1375. .name = "x86_64", .e_machine = EM_X86_64,
  1376. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1377. };
  1378. #else /* CONFIG_X86_32 */
  1379. #define user_regs_struct32 user_regs_struct
  1380. #define genregs32_get genregs_get
  1381. #define genregs32_set genregs_set
  1382. #define user_i387_ia32_struct user_i387_struct
  1383. #define user32_fxsr_struct user_fxsr_struct
  1384. #endif /* CONFIG_X86_64 */
  1385. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1386. static const struct user_regset x86_32_regsets[] = {
  1387. [REGSET_GENERAL] = {
  1388. .core_note_type = NT_PRSTATUS,
  1389. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1390. .size = sizeof(u32), .align = sizeof(u32),
  1391. .get = genregs32_get, .set = genregs32_set
  1392. },
  1393. [REGSET_FP] = {
  1394. .core_note_type = NT_PRFPREG,
  1395. .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
  1396. .size = sizeof(u32), .align = sizeof(u32),
  1397. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1398. },
  1399. [REGSET_XFP] = {
  1400. .core_note_type = NT_PRXFPREG,
  1401. .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
  1402. .size = sizeof(u32), .align = sizeof(u32),
  1403. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1404. },
  1405. [REGSET_TLS] = {
  1406. .core_note_type = NT_386_TLS,
  1407. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1408. .size = sizeof(struct user_desc),
  1409. .align = sizeof(struct user_desc),
  1410. .active = regset_tls_active,
  1411. .get = regset_tls_get, .set = regset_tls_set
  1412. },
  1413. [REGSET_IOPERM32] = {
  1414. .core_note_type = NT_386_IOPERM,
  1415. .n = IO_BITMAP_BYTES / sizeof(u32),
  1416. .size = sizeof(u32), .align = sizeof(u32),
  1417. .active = ioperm_active, .get = ioperm_get
  1418. },
  1419. };
  1420. static const struct user_regset_view user_x86_32_view = {
  1421. .name = "i386", .e_machine = EM_386,
  1422. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1423. };
  1424. #endif
  1425. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1426. {
  1427. #ifdef CONFIG_IA32_EMULATION
  1428. if (test_tsk_thread_flag(task, TIF_IA32))
  1429. #endif
  1430. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1431. return &user_x86_32_view;
  1432. #endif
  1433. #ifdef CONFIG_X86_64
  1434. return &user_x86_64_view;
  1435. #endif
  1436. }
  1437. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  1438. int error_code, int si_code)
  1439. {
  1440. struct siginfo info;
  1441. tsk->thread.trap_no = 1;
  1442. tsk->thread.error_code = error_code;
  1443. memset(&info, 0, sizeof(info));
  1444. info.si_signo = SIGTRAP;
  1445. info.si_code = si_code;
  1446. /* User-mode ip? */
  1447. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  1448. /* Send us the fake SIGTRAP */
  1449. force_sig_info(SIGTRAP, &info, tsk);
  1450. }
  1451. #ifdef CONFIG_X86_32
  1452. # define IS_IA32 1
  1453. #elif defined CONFIG_IA32_EMULATION
  1454. # define IS_IA32 is_compat_task()
  1455. #else
  1456. # define IS_IA32 0
  1457. #endif
  1458. /*
  1459. * We must return the syscall number to actually look up in the table.
  1460. * This can be -1L to skip running any syscall at all.
  1461. */
  1462. asmregparm long syscall_trace_enter(struct pt_regs *regs)
  1463. {
  1464. long ret = 0;
  1465. /*
  1466. * If we stepped into a sysenter/syscall insn, it trapped in
  1467. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  1468. * If user-mode had set TF itself, then it's still clear from
  1469. * do_debug() and we need to set it again to restore the user
  1470. * state. If we entered on the slow path, TF was already set.
  1471. */
  1472. if (test_thread_flag(TIF_SINGLESTEP))
  1473. regs->flags |= X86_EFLAGS_TF;
  1474. /* do the secure computing check first */
  1475. secure_computing(regs->orig_ax);
  1476. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1477. ret = -1L;
  1478. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  1479. tracehook_report_syscall_entry(regs))
  1480. ret = -1L;
  1481. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1482. trace_sys_enter(regs, regs->orig_ax);
  1483. if (unlikely(current->audit_context)) {
  1484. if (IS_IA32)
  1485. audit_syscall_entry(AUDIT_ARCH_I386,
  1486. regs->orig_ax,
  1487. regs->bx, regs->cx,
  1488. regs->dx, regs->si);
  1489. #ifdef CONFIG_X86_64
  1490. else
  1491. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1492. regs->orig_ax,
  1493. regs->di, regs->si,
  1494. regs->dx, regs->r10);
  1495. #endif
  1496. }
  1497. return ret ?: regs->orig_ax;
  1498. }
  1499. asmregparm void syscall_trace_leave(struct pt_regs *regs)
  1500. {
  1501. if (unlikely(current->audit_context))
  1502. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1503. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  1504. trace_sys_exit(regs, regs->ax);
  1505. if (test_thread_flag(TIF_SYSCALL_TRACE))
  1506. tracehook_report_syscall_exit(regs, 0);
  1507. /*
  1508. * If TIF_SYSCALL_EMU is set, we only get here because of
  1509. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  1510. * We already reported this syscall instruction in
  1511. * syscall_trace_enter(), so don't do any more now.
  1512. */
  1513. if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
  1514. return;
  1515. /*
  1516. * If we are single-stepping, synthesize a trap to follow the
  1517. * system call instruction.
  1518. */
  1519. if (test_thread_flag(TIF_SINGLESTEP) &&
  1520. tracehook_consider_fatal_signal(current, SIGTRAP))
  1521. send_sigtrap(current, regs, 0, TRAP_BRKPT);
  1522. }