ptrace.c 40 KB

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