ptrace.c 38 KB

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