ptrace.c 38 KB

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